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.
Problem
Section titled “Problem”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
mainto a method passes through; - in a network, the checkpoints that all traffic from the outside to a host passes.
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. Which blocks have always run when next runs, and which of them is the nearest? And
which is the nearest for exit?
- Entry or block asked about
- 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.
Solution
Section titled “Solution”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 - bodyint immediateDominatorOfExit = dominators.immediateDominatorOf(exit); // 1 - headerboolean 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// The analysis follows edges against their direction too, so it takes a// BiAdjacentGraph.let dominators = Dominators::of(&BiAdjacentGraph::of(&cfg), entry);// Some(2) - bodylet immediate_dominator_of_next = dominators.immediate_dominator_of(next);// Some(1) - headerlet 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// 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 - headerconst 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// The analysis follows edges against their direction too, so it takes a// BiAdjacentGraph.Dominators dominators = Dominators.Of(BiAdjacentGraph.Of(cfg), entry);// 2 - bodyint immediateDominatorOfNext = dominators.ImmediateDominatorOf(next);// 1 - headerint 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, entrybody 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.
Good to know
Section titled “Good to know”- All dominators of a node are the nodes on the path from the root of the dominator tree to the node. The loop in the code collects them. See Build the dominator tree and ask it questions for how to ask only whether one node dominates another.
- Unreachable nodes are the nodes that the entry does not reach. They
have no immediate dominator, and neither has the entry: for both,
Dominators.immediateDominatorOfDominators::immediate_dominator_ofDominators.immediateDominatorOfDominators.ImmediateDominatorOfreturns-1None-1-1.Dominators.isReachableDominators::is_reachableDominators.isReachableDominators.IsReachabletells the two cases apart. - The graph the dominators were computed on comes back from
Dominators.graphDominators::graphDominators.graphDominators.Graph. The dominators keep it, so that the analyses built on them, such as the dominance frontiers, need nothing else. See Find the blocks where the dominance of a block ends.
API used
Section titled “API used”Dominators.of(BiAdjacentGraph, int)Dominators::ofDominators.of(graph: BiAdjacentGraph, entry: number)Dominators.Of(BiAdjacentGraph, int)Dominators.immediateDominatorOf(int)Dominators::immediate_dominator_ofDominators.immediateDominatorOfDominators.ImmediateDominatorOfDominators.isReachable(int)Dominators::is_reachableDominators.isReachableDominators.IsReachableDominators.graph()Dominators::graphDominators.graphDominators.GraphBiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Of