Skip to content

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.

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 if join again: the first block that runs after the if, 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.

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?

The control flow, its exit and the blocks asked about
Its post-dominator tree
0: entry1: header2: body3: then4: next5: exit
0: entry1: header2: body3: then4: next5: exit
Input
  • Exit or block asked about
Output
  • 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.

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 - next
int 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
Full example
// 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) - next
let 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
Full example
// 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 - next
const 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
Full example
// 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 - next
int 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
Full example

Whether 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.