Algorithm Search⌘K

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 pp post-dominates a block vv if every path from vv to the exit passes through pp. So once control reaches vv, it must later reach pp, 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 G=(N,E,r)G = (N, E, r) be a control flow graph with a designated exit node xx, and assume for now that every node has a path to xx. If the procedure has several returning blocks, xx is a new node with an edge from each of them; nodes that cannot reach any return are treated below.

  • pp post-dominates vv, written p  pdom  vp \;\mathrm{pdom}\; v, if every path from vv to xx contains pp. Every node post-dominates itself, and xx post-dominates every node.
  • pp strictly post-dominates vv if p  pdom  vp \;\mathrm{pdom}\; v and p≠vp \ne v.
  • The immediate post-dominator ipdom(v)\mathrm{ipdom}(v) of v≠xv \ne x is the strict post-dominator of vv that every other strict post-dominator of vv post-dominates.

Let GTG^T be the transpose of GG: the same nodes, every edge (a,b)(a, b) turned into (b,a)(b, a), and xx as the start node. A path from vv to xx in GG is, read backward, a path from xx to vv in GTG^T. So

p  pdom  v in G  ⟺  p dominates v in GT with start x.p \;\mathrm{pdom}\; v \text{ in } G \iff p \text{ dominates } v \text{ in } G^T \text{ with start } x.

Every fact about dominance carries over through this one line. Post-dominance is reflexive, antisymmetric and transitive on the nodes that reach xx; the strict post-dominators of vv are totally ordered by post-dominance; ipdom(v)\mathrm{ipdom}(v) exists and is unique for v≠xv \ne x; and the edges ipdom(v)→v\mathrm{ipdom}(v) \to v form a tree rooted at xx, the post-dominator tree, in which the post-dominators of vv are exactly the ancestors of vv. The proofs are those of the dominator tree, applied to GTG^T.

Computing it

Run any dominator algorithm on GTG^T from xx: the Cooper-Harvey-Kennedy iteration, which needs a reverse postorder of GTG^T (not of GG), or the Lengauer-Tarjan algorithm. The cost is the same as for dominators on a graph with the same nn and mm.

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 xx. For them the definition says that every pp post-dominates them, which is useless, and they are not reachable from xx in GTG^T. Compilers fix this in one of three ways: add a fake edge to xx from some node of each such loop (so every node reaches xx), 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 rr after cleanup, but not every node can reach xx.

Control dependence

Ferrante, Ottenstein and Warren (1987) define: YY is control dependent on XX if there is a path from XX to YY on which YY post-dominates every node after XX, and YY does not strictly post-dominate XX. An equivalent form: YY is control dependent on XX if XX has an edge to a successor SS with Y  pdom  SY \;\mathrm{pdom}\; S, and YY does not strictly post-dominate XX. In words, one edge out of XX forces control to reach YY, and another edge out of XX can avoid it. Cytron et al. (1991) show that the nodes on which YY is control dependent are the dominance frontier of YY in GTG^T, 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 nn, with blocks B1 to B6 and the edges

B1→B2,  B2→B3,  B2→B6,  B3→B4,  B3→B5,  B4→B5,  B5→B2.B1 \to B2,\; B2 \to B3,\; B2 \to B6,\; B3 \to B4,\; B3 \to B5,\; B4 \to B5,\; B5 \to B2.

B6 holds ret s, so it is the exit. Computed by the definition (delete pp and test whether vv still reaches B6):

vv post-dominators of vv ipdom(v)\mathrm{ipdom}(v)
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 B6→B2B6 \to B2, B2→B1B2 \to B1, B2→B5B2 \to B5, B5→B3B5 \to B3, B5→B4B5 \to B4. Compare the dominator tree of the same graph, which has B1→B2B1 \to B2, B2→B3B2 \to B3, B2→B6B2 \to B6, B3→B4B3 \to B4, B3→B5B3 \to B5. 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 B3→B4B3 \to B4 reaches it, the edge B3→B5B3 \to B5 avoids it). B2, B3 and B5 are control dependent on B2, because the edge B2→B3B2 \to B3 leads to them and the edge B2→B6B2 \to B6 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 ipdom(B0)=B3\mathrm{ipdom}(B0) = B3, 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 xx and the edges B4→xB4 \to x, B5→xB5 \to x, the immediate post-dominator of B4 and of B5 is xx.

Non-examples

  • "B5 runs after B3" is not post-dominance when B5 can be avoided. In the graph B3→B4B3 \to B4, B3→B5B3 \to B5, B4→xB4 \to x, B5→xB5 \to x, 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 GTG^T needs a reverse postorder of a depth-first search of GTG^T from xx. The reverse of a postorder of GG is not that order.
  • Forgetting the virtual exit. With several returns, a dominator algorithm on GTG^T has no single start node. Add xx first.
  • Silently dropping non-terminating code. Nodes that cannot reach xx are not visited by a search of GTG^T. 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.

References

  1. 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
  2. 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
  3. Steven S. Muchnick, Advanced Compiler Design and Implementation, Morgan Kaufmann, 1997, Chapter 7 (Control-Flow Analysis), dominators and postdominators
  4. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/FWJCCP/text.yaml