Sub topic 15
Dominators and Natural Loops
This chapter assumes you can draw a control flow graph (CFG) from basic blocks and that you know the iterative data flow method from the earlier chapters. Dominators are the tool compilers use to find loops in a CFG. Once the loops are found, optimizations such as loop-invariant code motion can be applied to them.
Dominators
Node d dominates node n (written d dom n) if every path from the entry node to n passes through d. Three facts follow from the definition:
- Every node dominates itself.
- The entry node dominates every reachable node.
- The immediate dominator of
n, written idom(n), is the closest strict dominator ofn. It is the dominator ofn(other thann) that every other strict dominator ofnalso dominates.
The immediate dominators form a tree, the dominator tree, rooted at the entry. A node's dominators are exactly its ancestors in that tree, including the node itself.
Computing dominators
Dominators are a forward, must analysis, like available expressions. The meet operator is intersection, and the equations are:
- Dom(entry) = {entry}
- Dom(n) = {n} ∪ (intersection of Dom(p) over all predecessors p of n)
Every node except the entry starts at the set of all nodes and shrinks until nothing changes. Starting from the full set matters. Starting from the empty set would make every intersection empty and give wrong answers.
A worked example
The graph below has a loop through blocks 2 to 5.
Working through the equations gives these dominator sets:
| Node | Dom | idom |
|---|---|---|
| 1 | {1} | none |
| 2 | {1, 2} | 1 |
| 3 | {1, 2, 3} | 2 |
| 4 | {1, 2, 4} | 2 |
| 5 | {1, 2, 5} | 2 |
| 6 | {1, 2, 5, 6} | 5 |
Take Dom(2) as an example. B2 has predecessors B1 and B5, so Dom(2) = {2} ∪ ({1} ∩ {1, 2, 5}) = {1, 2}. Neither B3 nor B4 dominates B5, because a path can avoid either one.
Back edges and natural loops
A back edge is an edge t → h where h dominates t. Here 5 → 2 is a back edge because B2 dominates B5. The head h is the loop header.
The natural loop of a back edge t → h is h plus every node that can reach t without passing through h. Two properties make it a proper loop:
- It has a single entry, the header, which dominates every node in the loop.
- It has at least one path that returns to the header, namely the back edge.
To build the loop, start with {h, t}. Then repeatedly add the predecessors of each newly added node, never expanding past h. For 5 → 2, B5 pulls in B3 and B4, and both of those pull in B2, which is already present. The loop is {2, 3, 4, 5}. B1 and B6 are outside it.
Code
This program computes dominators iteratively, finds back edges, and builds the natural loop of each one. It uses the graph above.
def dominators(nodes, preds, entry):
dom = {n: set(nodes) for n in nodes}
dom[entry] = {entry}
changed = True
while changed:
changed = False
for n in nodes:
if n == entry:
continue
new = set(nodes)
for p in preds[n]:
new &= dom[p]
new |= {n}
if new != dom[n]:
dom[n] = new
changed = True
return dom
def natural_loop(preds, header, tail):
loop = {header, tail}
stack = [tail] if tail != header else []
while stack:
m = stack.pop()
for p in preds[m]:
if p not in loop:
loop.add(p)
stack.append(p)
return loop
nodes = [1, 2, 3, 4, 5, 6]
succ = {1: [2], 2: [3, 4], 3: [5], 4: [5], 5: [2, 6], 6: []}
preds = {n: [m for m in nodes if n in succ[m]] for n in nodes}
dom = dominators(nodes, preds, 1)
for n in nodes:
print(n, sorted(dom[n]))
back_edges = [(t, h) for t in nodes for h in succ[t] if h in dom[t]]
print("back edges:", back_edges)
for t, h in back_edges:
print("loop of", (t, h), sorted(natural_loop(preds, h, t)))It prints the six dominator sets from the table, then back edges: [(5, 2)] and loop of (5, 2) [2, 3, 4, 5].
Nested loops and shared headers
- If two back edges have the same header, the usual convention is to merge their natural loops into one loop.
- Loop A is nested in loop B when A's header is in B and A ≠ B. Otherwise, two natural loops are either disjoint or nested. An inner loop contains no other loop.
Why loops matter for optimization
Compilers spend most of their effort on inner loops, because programs spend most of their run time there. Loop-invariant code motion moves a computation whose operands do not change inside the loop into a preheader. The preheader is a new block placed just before the header, and all edges that entered the header from outside the loop are redirected to it. Dominators are what make this safe. The moved definition must dominate all loop exits where its value is used, and it must be the only definition of its variable in the loop.
Common errors
- Initializing Dom sets to empty instead of the full node set.
- Forgetting that a node dominates itself.
- Letting the loop-building step walk past the header. This pulls in nodes before the loop.
- Treating two predecessors as both required. Dominance needs every path, so the meet is an intersection.
Exercise
A CFG has edges 1→2, 2→3, 3→4, 4→3, 4→5, 5→2, 5→6. Find the back edges and the natural loop of each. Which loop is nested in the other?
Show answer
Dom(2) = {1,2}, Dom(3) = {1,2,3}, Dom(4) = {1,2,3,4}, Dom(5) = {1,2,3,4,5}. The back edges are 4→3 (3 dominates 4) and 5→2 (2 dominates 5). The loop of 4→3 is {3,4}. The loop of 5→2 is {2,3,4,5}, found by walking back from 5 through 4 and 3 and stopping at 2. The loop {3,4} is nested inside {2,3,4,5}, because its header, 3, lies in the larger loop.