Search

Search papers and lessons

Dominator Tree

Level 3 · 9 min read

A dominator tree records, for each block of a program, the closest block that every path from the start must pass through before reaching it.

On this page

  • Colour = topic mix
  • Systems
  • Discrete math
  • Algorithms
  • Sign in to see your progress

TL;DR

In a control-flow graph with a single entry block, a block dd dominates a block nn when every path from the entry to nn passes through dd. The dominators of nn always form a chain, so each block has one closest dominator, its immediate dominator. Linking every block to its immediate dominator gives a tree, the dominator tree, which shows the structure of the program's branches and loops in one picture.

Where you'll see it: in the construction of SSA form, in finding the natural loops that a compiler optimises, and in deciding where a value can safely be moved.

Intuition

Checkpoints on the road

Picture a city where every route from home to the office crosses the same bridge. Then the bridge is a checkpoint that every trip must pass. If there are two bridges and each route uses only one of them, no single bridge is a checkpoint. Dominance is the checkpoint relation for a program. Block dd is a checkpoint for block nn when no way of running the program reaches nn without first running dd.

Checkpoints nest. If the office is past the post office, and the post office is past the bank, then the bank is a checkpoint for the office too. Writing each block under its closest checkpoint gives a family tree: the start is the root, and each block hangs under the last checkpoint before it.

Why a compiler wants this tree

A loop has a single block that every trip around it goes through: the loop header. Code that sits inside the loop is dominated by the header. A compiler that knows the dominators can recognise loops without any special rule for for or while. It just looks for an edge that points back to one of its own dominators. The tree also answers "where can I place a new instruction so that it runs before everything that needs it?" That answer is again a question about checkpoints.

The math

Let G=(V,E)G = (V, E) be a control-flow graph with entry r∈Vr \in V. Every node is reachable from rr.

  • Dominance. A node dd dominates nn, written d dom nd \text{ dom } n, when every path from rr to nn contains dd. Every node dominates itself. A node dd strictly dominates nn when dd dominates nn and d≠nd \ne n.
  • Dominator sets. Dom⁡(n)\operatorname{Dom}(n) is the set of all nodes that dominate nn. The entry satisfies Dom⁡(r)={r}\operatorname{Dom}(r) = \{r\}.
  • Immediate dominator. For n≠rn \ne r, the immediate dominator idom⁡(n)\operatorname{idom}(n) is the strict dominator of nn that is dominated by every other strict dominator of nn. It is the closest checkpoint.
  • Dominator tree. The tree with vertex set VV, root rr, and an edge from idom⁡(n)\operatorname{idom}(n) to nn for every n≠rn \ne r.

Two facts make the tree possible. First, Dom⁡(n)\operatorname{Dom}(n) is totally ordered by dominance: if aa and bb both dominate nn, then one of them dominates the other. Second, every node other than rr has exactly one immediate dominator. Together they mean the strict dominators of nn form a chain, and the closest one is unique.

The equations

Dominator sets satisfy a fixed-point equation. The entry is special, and every other node is the node itself plus whatever all its predecessors share:

Dom⁡(r)={r},Dom⁡(n)={n}∪⋂p∈pred⁡(n)Dom⁡(p)(n≠r)\operatorname{Dom}(r) = \{r\}, \qquad \operatorname{Dom}(n) = \{n\} \cup \bigcap_{p \in \operatorname{pred}(n)} \operatorname{Dom}(p) \quad (n \ne r)

Here pred⁡(n)\operatorname{pred}(n) is the set of predecessors of nn, and ∩\cap is set intersection. The idea: a path to nn arrives through some predecessor pp, so anything that dominates nn must dominate every predecessor's paths too, and nn dominates itself. Starting from Dom⁡(n)=V\operatorname{Dom}(n) = V for every n≠rn \ne r and repeating the update until nothing changes gives the answer.

Algorithm
Iterative dominator computation
  1. Set Dom⁡(r)={r}\operatorname{Dom}(r) = \{r\}. For every other node set Dom⁡(n)=V\operatorname{Dom}(n) = V.
  2. Repeat, for each node n≠rn \ne r in any order: compute Dom⁡(n)={n}∪⋂p∈pred⁡(n)Dom⁡(p)\operatorname{Dom}(n) = \{n\} \cup \bigcap_{p \in \operatorname{pred}(n)} \operatorname{Dom}(p).
  3. Stop when a full pass changes no set.
  4. For each n≠rn \ne r, the immediate dominator is the member of Dom⁡(n)∖{n}\operatorname{Dom}(n) \setminus \{n\} whose own dominator set is largest. Its set has one more element than the others in the chain.

The sets only shrink, so the loop ends. Practical compilers use faster methods, such as the one by Cooper, Harvey and Kennedy, which works on a reverse postorder of the graph and stores only the immediate dominator of each node.

Worked examples

Example 1: a single loop (tiny)

Example
Dominators of the counting loop

Take the CFG from the control-flow graph lesson: B1 goes to B2, B2 goes to B3 and to B4, and B3 goes back to B2.

Step 1: the entry is B1, so Dom⁡(B1)={B1}\operatorname{Dom}(B1) = \{B1\}.

Step 2: every path from B1 to B2 starts at B1, and B1 is the only way in, so Dom⁡(B2)={B1,B2}\operatorname{Dom}(B2) = \{B1, B2\}.

Step 3: every path to B3 passes through B2, because B3's only predecessor is B2. So Dom⁡(B3)={B1,B2,B3}\operatorname{Dom}(B3) = \{B1, B2, B3\}.

Step 4: the same holds for B4, whose only predecessor is B2. So Dom⁡(B4)={B1,B2,B4}\operatorname{Dom}(B4) = \{B1, B2, B4\}.

Step 5: the immediate dominators are idom⁡(B2)=B1\operatorname{idom}(B2) = B1, idom⁡(B3)=B2\operatorname{idom}(B3) = B2 and idom⁡(B4)=B2\operatorname{idom}(B4) = B2.

Interpretation: the dominator tree is the chain B1 to B2, with B3 and B4 as two children of B2. The edge from B3 back to B2 points to a node that dominates B3, which is the signature of a loop.

Example 2: an if-else with a merge (standard)

Example
Dominators at a join point

A block B1 tests a condition. It goes to B2 when the condition holds and to B3 otherwise. Both B2 and B3 then go to B4.

Step 1: Dom⁡(B1)={B1}\operatorname{Dom}(B1) = \{B1\}.

Step 2: Dom⁡(B2)={B1,B2}\operatorname{Dom}(B2) = \{B1, B2\}, because B2's only predecessor is B1.

Step 3: Dom⁡(B3)={B1,B3}\operatorname{Dom}(B3) = \{B1, B3\}, by the same reasoning.

Step 4: B4 has two predecessors, B2 and B3. Apply the equation: Dom⁡(B4)={B4}∪(Dom⁡(B2)∩Dom⁡(B3))={B4}∪({B1,B2}∩{B1,B3})={B1,B4}\operatorname{Dom}(B4) = \{B4\} \cup (\operatorname{Dom}(B2) \cap \operatorname{Dom}(B3)) = \{B4\} \cup (\{B1, B2\} \cap \{B1, B3\}) = \{B1, B4\}.

Step 5: so idom⁡(B4)=B1\operatorname{idom}(B4) = B1. Neither B2 nor B3 is a checkpoint for B4, because each branch avoids the other.

Interpretation: the tree is B1 with three children, B2, B3 and B4. The intersection step is what removes B2 and B3 from the dominators of the join point. A reader who expected B2 to lie on every path to B4 would be wrong, and the equation shows why.

Example 3: a loop nested under a header (realistic)

Example
Finding a loop body

Consider a function with an entry E, then A, then a loop header B. B goes to C, C goes back to B, and B also exits to D.

Edges: E to A, A to B, B to C, C to B, B to D.

Step 1: Dom⁡(E)={E}\operatorname{Dom}(E) = \{E\}.

Step 2: A's only predecessor is E, so Dom⁡(A)={E,A}\operatorname{Dom}(A) = \{E, A\}.

Step 3: B has predecessors A and C. Try the fixed point. Suppose Dom⁡(C)={E,A,B,C}\operatorname{Dom}(C) = \{E, A, B, C\}. Then Dom⁡(B)={B}∪({E,A}∩{E,A,B,C})={E,A,B}\operatorname{Dom}(B) = \{B\} \cup (\{E, A\} \cap \{E, A, B, C\}) = \{E, A, B\}. This agrees with the assumption that B's dominators are EE, AA and BB.

Step 4: C's only predecessor is B, so Dom⁡(C)={C}∪{E,A,B}={E,A,B,C}\operatorname{Dom}(C) = \{C\} \cup \{E, A, B\} = \{E, A, B, C\}. The assumption was right.

Step 5: D's only predecessor is B, so Dom⁡(D)={E,A,B,D}\operatorname{Dom}(D) = \{E, A, B, D\}.

Step 6: the immediate dominators are idom⁡(A)=E\operatorname{idom}(A) = E, idom⁡(B)=A\operatorname{idom}(B) = A, idom⁡(C)=B\operatorname{idom}(C) = B and idom⁡(D)=B\operatorname{idom}(D) = B.

Step 7: the edge C to B is a back edge, because B dominates C. That marks the loop: its header is B and its body contains C.

Interpretation: the tree is a path E, A, B followed by a fork into C and D. A compiler reads the loop directly from the tree and the back edge, with no special case for loops in the source language.

Tradeoffs and failure modes

SituationProblemBetter choice
The graph is smallThe simple iterative equations are easy to implement and checkKeep the iterative method
The graph is large with many blocksIteration over full sets costs time and memoryUse a faster algorithm such as Cooper, Harvey and Kennedy, which stores one immediate dominator per block
The graph has several entriesDominance needs one root, so the tree is not definedAdd a single virtual entry that leads to every real entry
The graph has unreachable blocksTheir dominator sets are not meaningfulRemove unreachable blocks first, then compute dominators
The question is about paths that avoid a blockDominance only answers "must pass through"Use post-dominance, computed on the reversed graph, for "must pass before leaving"

A missing edge in the control-flow graph changes the answer: an extra checkpoint can appear where none exists. Dominators are only as correct as the graph underneath them.

History

Dominance was introduced by Reese T. Prosser in 1959 for analysing the flow of programs. Frances E. Allen's work on control-flow analysis at IBM in the 1970s used dominators to find loops. The iterative set equations described here are the standard textbook method, and faster algorithms (Lengauer and Tarjan in 1979, Cooper, Harvey and Kennedy in the early 2000s) made the tree practical for large compilers. It caught on because it turns a messy question about all paths into a tree that a compiler can walk.

Recap

Dominance says which blocks every path must pass through. The sets satisfy a simple equation with an intersection over predecessors, and the immediate dominators form a tree. That tree identifies loops through back edges and gives the placement points needed by SSA form. The tree is itself a tree of nodes ordered by dominance, which is a special case of a partial order.

Check yourself

QuestionWhy is the immediate dominator of a block unique?Show answer

The strict dominators of any block form a chain: for any two of them, one dominates the other. In a chain there is exactly one element that is dominated by all the others, which is the closest one. That element is the immediate dominator, so it is unique.

QuestionIn Example 2, suppose the edge from B3 to B4 is removed, so that B4 is reached only from B2. What is Dom(B4)?Show answer

B4 now has the single predecessor B2, so Dom⁡(B4)={B4}∪Dom⁡(B2)={B1,B2,B4}\operatorname{Dom}(B4) = \{B4\} \cup \operatorname{Dom}(B2) = \{B1, B2, B4\} and idom⁡(B4)=B2\operatorname{idom}(B4) = B2. (B3 becomes unreachable, so under the usual rule it would be deleted first.) Removing an edge changed the checkpoints, which is why dominators must be recomputed from the graph.

QuestionIn Example 3, compute Dom(C) and state its immediate dominator.Show answer

C's only predecessor is B, and Dom⁡(B)={E,A,B}\operatorname{Dom}(B) = \{E, A, B\}, so Dom⁡(C)={E,A,B,C}\operatorname{Dom}(C) = \{E, A, B, C\}. The immediate dominator of C is B, the closest strict dominator: E and A both come before B on every path.

QuestionA back edge goes from C to B. What does it tell a compiler about the blocks inside the loop?Show answer

The back edge goes to a node that dominates its source, so B is the header of a loop whose body includes C. Every path into the loop body passes through B, so B is a safe place to put instructions that must run before the body, and the loop body is B together with the blocks that can reach C without passing through B.

Sources