TL;DR
In a control-flow graph with a single entry block, a block dominates a block when every path from the entry to passes through . The dominators of 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 is a checkpoint for block when no way of running the program reaches without first running .
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 be a control-flow graph with entry . Every node is reachable from .
- Dominance. A node dominates , written , when every path from to contains . Every node dominates itself. A node strictly dominates when dominates and .
- Dominator sets. is the set of all nodes that dominate . The entry satisfies .
- Immediate dominator. For , the immediate dominator is the strict dominator of that is dominated by every other strict dominator of . It is the closest checkpoint.
- Dominator tree. The tree with vertex set , root , and an edge from to for every .
Two facts make the tree possible. First, is totally ordered by dominance: if and both dominate , then one of them dominates the other. Second, every node other than has exactly one immediate dominator. Together they mean the strict dominators of 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:
Here is the set of predecessors of , and is set intersection. The idea: a path to arrives through some predecessor , so anything that dominates must dominate every predecessor's paths too, and dominates itself. Starting from for every and repeating the update until nothing changes gives the answer.
- Set . For every other node set .
- Repeat, for each node in any order: compute .
- Stop when a full pass changes no set.
- For each , the immediate dominator is the member of 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)
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 .
Step 2: every path from B1 to B2 starts at B1, and B1 is the only way in, so .
Step 3: every path to B3 passes through B2, because B3's only predecessor is B2. So .
Step 4: the same holds for B4, whose only predecessor is B2. So .
Step 5: the immediate dominators are , and .
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)
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: .
Step 2: , because B2's only predecessor is B1.
Step 3: , by the same reasoning.
Step 4: B4 has two predecessors, B2 and B3. Apply the equation: .
Step 5: so . 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)
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: .
Step 2: A's only predecessor is E, so .
Step 3: B has predecessors A and C. Try the fixed point. Suppose . Then . This agrees with the assumption that B's dominators are , and .
Step 4: C's only predecessor is B, so . The assumption was right.
Step 5: D's only predecessor is B, so .
Step 6: the immediate dominators are , , and .
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
| Situation | Problem | Better choice |
|---|---|---|
| The graph is small | The simple iterative equations are easy to implement and check | Keep the iterative method |
| The graph is large with many blocks | Iteration over full sets costs time and memory | Use a faster algorithm such as Cooper, Harvey and Kennedy, which stores one immediate dominator per block |
| The graph has several entries | Dominance needs one root, so the tree is not defined | Add a single virtual entry that leads to every real entry |
| The graph has unreachable blocks | Their dominator sets are not meaningful | Remove unreachable blocks first, then compute dominators |
| The question is about paths that avoid a block | Dominance 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 and . (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 , so . 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.