Definitioncore
Post-dominance
Also called postdominator, post-dominator tree.
Block p post-dominates block v if every path from v to the exit passes through p; the immediate post-dominators form a tree rooted at the exit.
Post-dominance is dominance with the arrows turned around. In a control flow graph with one exit, a block post-dominates a block if every path from to the exit passes through . So once control reaches , it must later reach , if the run ends normally. The closest such block is the immediate post-dominator, and these form the post-dominator tree, rooted at the exit. Post-dominance is the dominators problem on the transpose graph, so the same algorithms compute it. Its main use is control dependence: which branch decides whether a block runs.
Definition
Let be a control flow graph with a designated exit node , and assume for now that every node has a path to . If the procedure has several returning blocks, is a new node with an edge from each of them; nodes that cannot reach any return are treated below.
- post-dominates , written , if every path from to contains . Every node post-dominates itself, and post-dominates every node.
- strictly post-dominates if and .
- The immediate post-dominator of is the strict post-dominator of that every other strict post-dominator of post-dominates.
Let be the transpose of : the same nodes, every edge turned into , and as the start node. A path from to in is, read backward, a path from to in . So
Every fact about dominance carries over through this one line. Post-dominance is reflexive, antisymmetric and transitive on the nodes that reach ; the strict post-dominators of are totally ordered by post-dominance; exists and is unique for ; and the edges form a tree rooted at , the post-dominator tree, in which the post-dominators of are exactly the ancestors of . The proofs are those of the dominator tree, applied to .
Computing it
Run any dominator algorithm on from : the Cooper-Harvey-Kennedy iteration, which needs a reverse postorder of (not of ), or the Lengauer-Tarjan algorithm. The cost is the same as for dominators on a graph with the same and .
Nodes that cannot reach the exit
A procedure with a loop that never exits, such as an event loop, has nodes with no path to . For them the definition says that every post-dominates them, which is useless, and they are not reachable from in . Compilers fix this in one of three ways: add a fake edge to from some node of each such loop (so every node reaches ), treat those nodes as having no post-dominator, or use a weaker notion for non-terminating programs. The choice changes the post-dominator tree and the control dependences, so tools differ here. This is the main place where post-dominance is not a plain mirror of dominance: in a CFG every node is reachable from after cleanup, but not every node can reach .
Control dependence
Ferrante, Ottenstein and Warren (1987) define: is control dependent on if there is a path from to on which post-dominates every node after , and does not strictly post-dominate . An equivalent form: is control dependent on if has an edge to a successor with , and does not strictly post-dominate . In words, one edge out of forces control to reach , and another edge out of can avoid it. Cytron et al. (1991) show that the nodes on which is control dependent are the dominance frontier of in , the reverse dominance frontier.
Examples and non-examples
A loop with a branch inside
Take the toy IR program that adds the odd numbers below , with blocks B1 to B6 and the edges
B6 holds ret s, so it is the exit. Computed by the definition (delete and test whether still reaches B6):
| post-dominators of | ||
|---|---|---|
| B1 | B1, B2, B6 | B2 |
| B2 | B2, B6 | B6 |
| B3 | B3, B5, B2, B6 | B5 |
| B4 | B4, B5, B2, B6 | B5 |
| B5 | B5, B2, B6 | B2 |
| B6 | B6 | none |
The post-dominator tree has the edges , , , , . Compare the dominator tree of the same graph, which has , , , , . B3 dominates B5 but B5 post-dominates B3: the branch in B3 and its join point in B5 swap roles.
The control dependences, by the edge form of the definition: B4 is control dependent on B3 (the edge reaches it, the edge avoids it). B2, B3 and B5 are control dependent on B2, because the edge leads to them and the edge leaves the loop. B1 and B6 are control dependent on nothing: they run on every run that ends. In a program dependence graph these are the control edges.
A diamond
For B0: br a B1 B2, B1: jump B3, B2: jump B3, B3: ret x, the post-dominators of B0 are B0 and B3, so , the join point of the branch. This is why the classic stack-based scheme for GPU branches, which runs the two sides one after the other for different threads, joins the threads again at the immediate post-dominator of the branch: it is the first block that every thread must reach.
Several returns
If B4 holds ret 1 and B5 holds ret 0, the graph has two exits and no node post-dominates both. With a virtual exit and the edges , , the immediate post-dominator of B4 and of B5 is .
Non-examples
- "B5 runs after B3" is not post-dominance when B5 can be avoided. In the graph , , , , B5 does not post-dominate B3.
- Post-dominance is not dominance read in the other direction on the same nodes. B2 dominates B6 above, and B6 post-dominates B2, but B3 dominates B4 while B4 does not post-dominate B3.
- On a node in an infinite loop, without a fake exit edge, post-dominance is not defined in a useful way, as explained above.
Pitfalls
- Wrong order for the iteration. The iterative algorithm on needs a reverse postorder of a depth-first search of from . The reverse of a postorder of is not that order.
- Forgetting the virtual exit. With several returns, a dominator algorithm on has no single start node. Add first.
- Silently dropping non-terminating code. Nodes that cannot reach are not visited by a search of . If an optimization such as aggressive dead code elimination then treats them as dead, it can delete an infinite loop, which changes the behavior of the program.
Related definitions
- Dominators problem and dominator tree: the forward relation and its tree.
- Transpose graph: post-dominance is dominance on it.
- Control dependence and program dependence graph: built from post-dominance.
- Dominance frontier: computed on , it gives control dependence.
- Cooper-Harvey-Kennedy dominance and Lengauer-Tarjan algorithm: algorithms that compute post-dominators when run on .
References
- Jeanne Ferrante, Karl J. Ottenstein and Joe D. Warren, The program dependence graph and its use in optimization, ACM Transactions on Programming Languages and Systems 9(3), 1987, pages 319-349, the definitions of post-dominance, of the post-dominator tree and of control dependence on a control flow graph with a unique exit
- Ron Cytron, Jeanne Ferrante, Barry K. Rosen, Mark N. Wegman and F. Kenneth Zadeck, Efficiently computing static single assignment form and the control dependence graph, ACM Transactions on Programming Languages and Systems 13(4), 1991, pages 451-490, the section on the control dependence graph, which computes control dependences as dominance frontiers of the reverse control flow graph
- Steven S. Muchnick, Advanced Compiler Design and Implementation, Morgan Kaufmann, 1997, Chapter 7 (Control-Flow Analysis), dominators and postdominators
- Keith D. Cooper and Linda Torczon, Engineering a Compiler, third edition, Morgan Kaufmann, 2022, Chapter 9 (Data-Flow Analysis), dominance; the reverse control flow graph and reverse dominance frontiers in the treatment of dead code elimination
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag FWJCCP Edit this page History Report an error Markdown