7.3 Find the nodes that every path to the exit passes through
You find what is certain to happen after a node: its post-dominators, and the post-dominator tree they form.
Problem
Section titled “Problem”You want to know what is certain to happen after a node. A node p post-dominates a node n if
every path from n to an exit passes through p. Whatever happens after n, p follows. The
post-dominators of a node answer questions such as these:
- where the branches of an
ifjoin again: the first block that runs after theif, whichever branch is taken; - where code is sure to run before a method returns, such as cleanup;
- which branch decides whether a block runs. This is called the control dependence of the block, and it follows from the post-dominators. See Find which conditions decide whether a block runs.
Example
Section titled “Example”The method loadloadloadLoad of a small program reads the notes
of a notebook from a file:
List<Note> load(Path file) { List<String> lines = read(file); // entry List<Note> notes = new ArrayList<>(); for (int i = 0; i < lines.size(); i++) { // header: i < lines.size() String line = lines.get(i); // body if (!line.isBlank()) { notes.add(parse(line)); // then } } // next: i++ return notes; // exit}fn load(file: &Path) -> Vec<Note> { let lines = read(file); // entry let mut notes = Vec::new(); let mut i = 0; while i < lines.len() { // header: i < lines.len() let line = &lines[i]; // body if !line.trim().is_empty() { notes.push(parse(line)); // then } i += 1; // next } notes // exit}function load(file: string): Note[] { const lines = read(file); // entry const notes: Note[] = []; for (let i = 0; i < lines.length; i++) { // header: i < lines.length const line = lines[i]; // body if (line.trim() !== "") { notes.push(parse(line)); // then } } // next: i++ return notes; // exit}List<Note> Load(string file){ string[] lines = Read(file); // entry var notes = new List<Note>(); for (int i = 0; i < lines.Length; i++) // header: i < lines.Length { string line = lines[i]; // body if (!string.IsNullOrWhiteSpace(line)) { notes.Add(Parse(line)); // then } } // next: i++ return notes; // exit}The graph cfg holds its control flow. This
control-flow graph has a node for each
basic block and an edge from each block to each block that can run
right after it. exit is its only exit. Which block is the first to run after body in every case,
whether then runs or not? Which block is it after header? And what is the post-dominator tree?
- Exit or block asked about
- Block with its post-dominators as ancestors
- From immediate post-dominator to block
The picture after shows the post-dominator tree that the code returns: an edge from each block to
the blocks it immediately post-dominates. Its root is exit, and its
edges point against the control flow.
Solution
Section titled “Solution”The post-dominators are the dominators of the transposed graph, with the exits as its entries. So
turn every edge around with BiAdjacentGraph.transposedBiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.Transposed and compute the dominators
from the exit. See Find the nodes that every path from the entry passes through.
// The post-dominators are the dominators of the transposed graph, from the exit.// Dominators takes a BiAdjacentGraph, which also holds the in-nodes.Dominators postDominators = Dominators.of(BiAdjacentGraph.of(cfg).transposed(), exit);int joinOfBody = postDominators.immediateDominatorOf(body); // 4 - nextint joinOfHeader = postDominators.immediateDominatorOf(header); // 5 - exit
// The post-dominator tree: an edge from each block to the blocks it immediately// post-dominates.Graph tree = Graphs.dominatorTree(postDominators);// tree: header -> entry, header -> next, next -> body, next -> then, exit -> header// The post-dominators are the dominators of the transposed graph, from the exit.// Dominators takes a BiAdjacentGraph, which also holds the in-nodes.let post_dominators = Dominators::of(&BiAdjacentGraph::of(&cfg).transposed(), exit);let join_of_body = post_dominators.immediate_dominator_of(body); // Some(4) - nextlet join_of_header = post_dominators.immediate_dominator_of(header); // Some(5) - exit
// The post-dominator tree: an edge from each block to the blocks it immediately// post-dominates.let tree = graphs::dominator_tree(&post_dominators);// tree: header -> entry, header -> next, next -> body, next -> then, exit -> header// The post-dominators are the dominators of the transposed graph, from the exit.// Dominators takes a BiAdjacentGraph, which also holds the in-nodes.const postDominators = Dominators.of(BiAdjacentGraph.of(cfg).transposed(), exit);const joinOfBody = postDominators.immediateDominatorOf(body); // 4 - nextconst joinOfHeader = postDominators.immediateDominatorOf(header); // 5 - exit
// The post-dominator tree: an edge from each block to the blocks it immediately// post-dominates.const tree = Graphs.dominatorTree(postDominators);// tree: header -> entry, header -> next, next -> body, next -> then, exit -> header// The post-dominators are the dominators of the transposed graph, from the exit.// Dominators takes a BiAdjacentGraph, which also holds the in-nodes.Dominators postDominators = Dominators.Of(BiAdjacentGraph.Of(cfg).Transposed(), exit);int joinOfBody = postDominators.ImmediateDominatorOf(body); // 4 - nextint joinOfHeader = postDominators.ImmediateDominatorOf(header); // 5 - exit
// The post-dominator tree: an edge from each block to the blocks it immediately// post-dominates.Graph tree = Graphs.DominatorTree(postDominators);// tree: header -> entry, header -> next, next -> body, next -> then,// exit -> headerWhether body continues with then or not, next follows. next is where the two branches join,
and it is the immediate post-dominator of body. The immediate post-dominator of header is
exit. No other block lies on every path from header to exit, since the edge header -> exit
is itself such a path. The tree has the node IDs of the control-flow
graph.
Good to know
Section titled “Good to know”- No exit, no immediate post-dominator. A node from which no exit can be
reached, such as a block in an endless loop, has
no immediate post-dominator:
Dominators.immediateDominatorOfDominators::immediate_dominator_ofDominators.immediateDominatorOfDominators.ImmediateDominatorOfreturns-1None-1-1for it, andDominators.isReachableDominators::is_reachableDominators.isReachableDominators.IsReachablereturns false. If you need one, pass the exit and one block of each such loop as the entries of the transposed graph toDominators.ofDominators::of_entriesDominators.ofDominators.Of. The effect is the same as adding an extra exit with an edge from that block. - Several exits. A method that returns in several places has several exits. Pass them all as
entries of the transposed graph to
Dominators.ofDominators::of_entriesDominators.ofDominators.Of. See Find dominators from several entries.
API used
Section titled “API used”Dominators.of(BiAdjacentGraph, int)Dominators::ofDominators.of(graph: BiAdjacentGraph, entry: number)Dominators.Of(BiAdjacentGraph, int)BiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.OfBiAdjacentGraph.transposed()BiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.TransposedDominators.immediateDominatorOf(int)Dominators::immediate_dominator_ofDominators.immediateDominatorOfDominators.ImmediateDominatorOfGraphs.dominatorTree(Dominators)graphs::dominator_treeGraphs.dominatorTreeGraphs.DominatorTreeDominators.of(BiAdjacentGraph, IntList)Dominators::of_entriesDominators.of(graph: BiAdjacentGraph, entries: IntList)Dominators.Of(BiAdjacentGraph, ReadOnlySpan<int>)Dominators.isReachable(int)Dominators::is_reachableDominators.isReachableDominators.IsReachable