7.2 Build the dominator tree and ask it questions
You build the tree of immediate dominators as a graph, and answer questions about dominance with an ancestor index.
Problem
Section titled “Problem”You have a control-flow graph and many questions of the same
kind: if this block runs, has that block always run before it? A block that every
path from the entry to a block n passes
through is called a dominator of n. The dominators of all blocks form a tree, called the dominator
tree. In this tree, the dominators of n are its ancestors. Many analyses walk this tree or ask it
questions. Does this block dominate that one? Which is the lowest block that dominates both? Which
blocks does a block dominate? You need this for:
- a compiler that walks the dominator tree to convert code to SSA form (static single assignment) and to move code;
- an analyzer that asks whether a check dominates every use of a value;
- a tool that lists the code that runs only after a given block has run: the blocks that this block dominates.
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. What is its dominator tree? Does body dominate next? Does then? Which is the
lowest block that dominates both then and next?
- Entry or block asked about
- Block with its dominators as ancestors
- From immediate dominator to block
The picture after shows the dominator tree that the code returns: an edge from each block to the blocks it immediately dominates.
Solution
Section titled “Solution”The dominators come from Dominators.ofDominators::ofDominators.ofDominators.Of. See
Find the nodes that every path from the entry passes through.
// Dominators takes a BiAdjacentGraph, which also holds the in-nodes.Dominators dominators = Dominators.of(BiAdjacentGraph.of(cfg), entry);Graph tree = Graphs.dominatorTree(dominators);// tree: entry -> header, header -> body, header -> exit, body -> then, body -> next
// Does one block dominate another? An ancestor question on the dominator tree.Ancestry dominance = Ancestry.of(dominators);boolean bodyDominatesNext = dominance.isAncestorOf(body, next); // trueboolean thenDominatesNext = dominance.isAncestorOf(then, next); // false// 2 - bodyint lowestCommonDominator = dominance.lowestCommonAncestorOf(then, next);// Dominators takes a BiAdjacentGraph, which also holds the in-nodes.let dominators = Dominators::of(&BiAdjacentGraph::of(&cfg), entry);let tree = graphs::dominator_tree(&dominators);// tree: entry -> header, header -> body, header -> exit, body -> then, body -> next
// Does one block dominate another? An ancestor question on the dominator tree.let dominance = Ancestry::of_dominators(&dominators);let body_dominates_next = dominance.is_ancestor_of(body, next); // truelet then_dominates_next = dominance.is_ancestor_of(then, next); // false// Some(2) - bodylet lowest_common_dominator = dominance.lowest_common_ancestor_of(then, next);// Dominators takes a BiAdjacentGraph, which also holds the in-nodes.const dominators = Dominators.of(BiAdjacentGraph.of(cfg), entry);const tree = Graphs.dominatorTree(dominators);// tree: entry -> header, header -> body, header -> exit, body -> then, body -> next
// Does one block dominate another? An ancestor question on the dominator tree.const dominance = Ancestry.of(dominators);const bodyDominatesNext = dominance.isAncestorOf(body, next); // trueconst thenDominatesNext = dominance.isAncestorOf(then, next); // false// 2 - bodyconst lowestCommonDominator = dominance.lowestCommonAncestorOf(then, next);// Dominators takes a BiAdjacentGraph, which also holds the in-nodes.Dominators dominators = Dominators.Of(BiAdjacentGraph.Of(cfg), entry);Graph tree = Graphs.DominatorTree(dominators);// tree: entry -> header, header -> body, header -> exit, body -> then,// body -> next
// Does one block dominate another? An ancestor question on the dominator tree.Ancestry dominance = Ancestry.Of(dominators);bool bodyDominatesNext = dominance.IsAncestorOf(body, next); // truebool thenDominatesNext = dominance.IsAncestorOf(then, next); // false// 2 - bodyint lowestCommonDominator = dominance.LowestCommonAncestorOf(then, next);The tree has the node IDs of the control-flow graph, and its
root is the entry. Every question about dominance is a question about
ancestors in this tree. body is an ancestor of next, and then is not. body is also the
lowest common ancestor of then and next, so it is the
lowest block that dominates both.
Good to know
Section titled “Good to know”- No graph needed for questions.
Ancestry.ofAncestry::of_dominatorsAncestry.ofAncestry.Ofshares the immediate dominators instead of building the tree as a graph; build the graph only to walk it or to draw it. - Constant time. The ancestor index answers each question in logarithmic time at most. For many
questions, build
PreorderAncestry.ofPreorderAncestry::ofPreorderAncestry.ofPreorderAncestry.Offrom it:PreorderAncestry.isAncestorOfPreorderAncestry::is_ancestor_ofPreorderAncestry.isAncestorOfPreorderAncestry.IsAncestorOfanswers whether one block dominates another in constant time, andPreorderAncestry.subtreeOfPreorderAncestry::subtree_ofPreorderAncestry.subtreeOfPreorderAncestry.SubtreeOflists the blocks that a block dominates. See Test many times whether one node is an ancestor of another and List a node and all its descendants. - Unreachable nodes. A node that the entry does not reach dominates
only itself and is the root of a tree of its own, which makes the dominator tree a
forest.
Dominators.isReachableDominators::is_reachableDominators.isReachableDominators.IsReachabletells such a node apart.
API used
Section titled “API used”Graphs.dominatorTree(Dominators)graphs::dominator_treeGraphs.dominatorTreeGraphs.DominatorTreeAncestry.of(Dominators)Ancestry::of_dominatorsAncestry.of(dominators: Dominators)Ancestry.Of(Dominators)Ancestry.isAncestorOf(int, int)Ancestry::is_ancestor_ofAncestry.isAncestorOfAncestry.IsAncestorOfAncestry.lowestCommonAncestorOf(int, int)Ancestry::lowest_common_ancestor_ofAncestry.lowestCommonAncestorOfAncestry.LowestCommonAncestorOfPreorderAncestry.of(Ancestry)PreorderAncestry::ofPreorderAncestry.ofPreorderAncestry.OfPreorderAncestry.isAncestorOf(int, int)PreorderAncestry::is_ancestor_ofPreorderAncestry.isAncestorOfPreorderAncestry.IsAncestorOfPreorderAncestry.subtreeOf(int)PreorderAncestry::subtree_ofPreorderAncestry.subtreeOfPreorderAncestry.SubtreeOfDominators.of(BiAdjacentGraph, int)Dominators::ofDominators.of(graph: BiAdjacentGraph, entry: number)Dominators.Of(BiAdjacentGraph, int)Dominators.isReachable(int)Dominators::is_reachableDominators.isReachableDominators.IsReachableBiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Of