Skip to content

8.3 Find the blocks where the dominance of a block ends

You get the dominance frontier of each block of a control-flow graph, which holds the blocks where the dominance of that block ends.

You want to know where the effect of a block ends. A variable assigned in one branch of an if keeps its new value only until the two branches join. There, a path that skips the branch comes in, so the value is no longer certain. A block d dominates a block n if every path from the entry to n passes through d. The blocks where a path through d first joins a path that skips d are called the dominance frontier of d. Typical such blocks are the block where the two branches of an if join, seen from each branch, and the header of a loop, seen from the blocks of the loop. You need this for:

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. Where does the dominance of each block end?

The control flow and its entry block
Its dominance frontiers
0: entry1: header2: body3: then4: next5: exit
0: entry1: header2: body3: then4: next5: exit
Input
  • Entry block
Output
  • Block of the frontier graph
  • From a block to a block of its frontier

The picture after shows the frontier graph that the code returns: all blocks, and an edge from each block to each block in its dominance frontier. The blocks stay in their places, and the edges of the control flow are not drawn.

The frontiers follow from the dominators. See Find the nodes that every path from the entry passes through.

Dominators dominators = Dominators.of(BiAdjacentGraph.of(cfg), entry);
Graph frontiers = ControlFlow.dominanceFrontiers(dominators);
// frontiers: header -> header, body -> header, then -> next, next -> header
Full example
let dominators = Dominators::of(&BiAdjacentGraph::of(&cfg), entry);
let frontiers = control_flow::dominance_frontiers(&dominators);
// frontiers: header -> header, body -> header, then -> next, next -> header
Full example
const dominators = Dominators.of(BiAdjacentGraph.of(cfg), entry);
const frontiers = ControlFlow.dominanceFrontiers(dominators);
// frontiers: header -> header, body -> header, then -> next, next -> header
Full example
Dominators dominators = Dominators.Of(BiAdjacentGraph.Of(cfg), entry);
Graph frontiers = ControlFlow.DominanceFrontiers(dominators);
// frontiers: header -> header, body -> header, then -> next, next -> header
Full example

A block is in the dominance frontier of d if d dominates one of its in-nodes but does not strictly dominate the block itself. A block d strictly dominates a block if it dominates that block and is not that block.

then dominates only itself, and its only out-node is next. So next is the one block in the dominance frontier of then. It is where the path through then and the path that skips it join. The frontiers of body and of next hold header. Both dominate next, which is an in-node of header. They do not dominate header, which is reached from entry as well. header is in its own frontier, since it dominates next but does not strictly dominate itself. entry and exit lie on no cycle and dominate every block they reach, so their frontiers are empty.