Skip to content

7.1 Find the nodes that every path from the entry passes through

You get the immediate dominator of each node: the nearest other node that every path from the entry to it passes through.

You want to know what is certain to have happened before a node. A node d dominates a node n if every path from the entry to n passes through d. The dominators of a node are:

  • in the control-flow graph of a method, the blocks that have always run before a block runs, so that a value computed there is always available and a check there has always been made;
  • in the calls of a program, the methods that every path of calls from main to a method passes through;
  • in a network, the checkpoints that all traffic from the outside to a host passes.

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. Which blocks have always run when next runs, and which of them is the nearest? And which is the nearest for exit?

The control flow, its entry and the blocks asked about
The blocks that dominate the block named next
0: entry1: header2: body3: then4: next5: exit
0: entry1: header2: body3: then4: next5: exit
Input
  • Entry or block asked about
Output
  • Dominator

In the picture after, the blocks that dominate next are green, next itself included. body is the nearest of the others, so it is the immediate dominator of next.

The analysis follows the edges against their direction as well, so it takes a BiAdjacentGraphBiAdjacentGraphBiAdjacentGraphBiAdjacentGraph, which BiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Of builds from the graph:

// The analysis follows edges against their direction too, so it takes a
// BiAdjacentGraph.
Dominators dominators = Dominators.of(BiAdjacentGraph.of(cfg), entry);
int immediateDominatorOfNext = dominators.immediateDominatorOf(next); // 2 - body
int immediateDominatorOfExit = dominators.immediateDominatorOf(exit); // 1 - header
boolean exitReachable = dominators.isReachable(exit); // true
// All blocks that dominate next: next, its immediate dominator, that one's, and so
// on.
IntList dominatorsOfNext = new IntArrayList();
for (int d = next; d != -1; d = dominators.immediateDominatorOf(d)) {
dominatorsOfNext.add(d);
}
// dominatorsOfNext: [4, 2, 1, 0] - next, body, header, entry
Full example
// The analysis follows edges against their direction too, so it takes a
// BiAdjacentGraph.
let dominators = Dominators::of(&BiAdjacentGraph::of(&cfg), entry);
// Some(2) - body
let immediate_dominator_of_next = dominators.immediate_dominator_of(next);
// Some(1) - header
let immediate_dominator_of_exit = dominators.immediate_dominator_of(exit);
let exit_reachable = dominators.is_reachable(exit); // true
// All blocks that dominate next: next, its immediate dominator, that one's, and so
// on.
let mut dominators_of_next = Vec::new();
let mut d = Some(next);
while let Some(block) = d {
dominators_of_next.push(block);
d = dominators.immediate_dominator_of(block);
}
// dominators_of_next: [4, 2, 1, 0] - next, body, header, entry
Full example
// The analysis follows edges against their direction too, so it takes a
// BiAdjacentGraph.
const dominators = Dominators.of(BiAdjacentGraph.of(cfg), entry);
const immediateDominatorOfNext = dominators.immediateDominatorOf(next); // 2 - body
// 1 - header
const immediateDominatorOfExit = dominators.immediateDominatorOf(exit);
const exitReachable = dominators.isReachable(exit); // true
// All blocks that dominate next: next, its immediate dominator, that one's, and so
// on.
const dominatorsOfNext: number[] = [];
for (let d = next; d !== -1; d = dominators.immediateDominatorOf(d)) {
dominatorsOfNext.push(d);
}
// dominatorsOfNext: [4, 2, 1, 0] - next, body, header, entry
Full example
// The analysis follows edges against their direction too, so it takes a
// BiAdjacentGraph.
Dominators dominators = Dominators.Of(BiAdjacentGraph.Of(cfg), entry);
// 2 - body
int immediateDominatorOfNext = dominators.ImmediateDominatorOf(next);
// 1 - header
int immediateDominatorOfExit = dominators.ImmediateDominatorOf(exit);
bool exitReachable = dominators.IsReachable(exit); // true
// All blocks that dominate next: next, its immediate dominator, that one's, and
// so on.
List<int> dominatorsOfNext = [];
for (int d = next; d != -1; d = dominators.ImmediateDominatorOf(d))
{
dominatorsOfNext.Add(d);
}
// dominatorsOfNext: [4, 2, 1, 0] - next, body, header, entry
Full example

body is the immediate dominator of next. header is the immediate dominator of exit, since it is the only in-node of exit. Following the immediate dominators up from next gives all blocks that dominate next: the green ones in the picture after. The walk ends at the entry, which has no immediate dominator. The same works on any graph with an entry.