Skip to content

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.

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.

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?

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

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); // true
boolean thenDominatesNext = dominance.isAncestorOf(then, next); // false
// 2 - body
int lowestCommonDominator = dominance.lowestCommonAncestorOf(then, next);
Full example
// 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); // true
let then_dominates_next = dominance.is_ancestor_of(then, next); // false
// Some(2) - body
let lowest_common_dominator = dominance.lowest_common_ancestor_of(then, next);
Full example
// 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); // true
const thenDominatesNext = dominance.isAncestorOf(then, next); // false
// 2 - body
const lowestCommonDominator = dominance.lowestCommonAncestorOf(then, next);
Full example
// 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); // true
bool thenDominatesNext = dominance.IsAncestorOf(then, next); // false
// 2 - body
int lowestCommonDominator = dominance.LowestCommonAncestorOf(then, next);
Full example

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.