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.
Problem
Section titled “Problem”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:
- finding where a variable assigned in
dmay have another value from another path. Compilers place the phi-nodes of SSA form (static single assignment) there. See Find the blocks that need a phi-node for a variable. - finding which branches decide whether a block runs. See Find which conditions decide whether a block runs.
- a data-flow analysis that merges its results only at these blocks, where they can differ, not at every block where paths join.
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. Where does the dominance of each block end?
- Entry block
- 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.
Solution
Section titled “Solution”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 -> headerlet dominators = Dominators::of(&BiAdjacentGraph::of(&cfg), entry);let frontiers = control_flow::dominance_frontiers(&dominators);// frontiers: header -> header, body -> header, then -> next, next -> headerconst dominators = Dominators.of(BiAdjacentGraph.of(cfg), entry);const frontiers = ControlFlow.dominanceFrontiers(dominators);// frontiers: header -> header, body -> header, then -> next, next -> headerDominators dominators = Dominators.Of(BiAdjacentGraph.Of(cfg), entry);Graph frontiers = ControlFlow.DominanceFrontiers(dominators);// frontiers: header -> header, body -> header, then -> next, next -> headerA 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.
Good to know
Section titled “Good to know”- A block in its own frontier, as
headerhere, is typical of the header of a loop: the result can have self-loops. - Quadratic size. The frontiers can grow with the square of the number of blocks, for example
for deeply nested
do-whileloops. For placing phi-nodes,IteratedDominanceFrontierIteratedDominanceFrontierIteratedDominanceFrontierIteratedDominanceFrontieranswers without building them. See Find the blocks that need a phi-node for a variable. - Built anew on every call.
ControlFlow.dominanceFrontierscontrol_flow::dominance_frontiersControlFlow.dominanceFrontiersControlFlow.DominanceFrontiersdoes not keep its result; keep it in a variable to use it more than once.
API used
Section titled “API used”ControlFlow.dominanceFrontiers(Dominators)control_flow::dominance_frontiersControlFlow.dominanceFrontiersControlFlow.DominanceFrontiersDominators.of(BiAdjacentGraph, int)Dominators::ofDominators.of(graph: BiAdjacentGraph, entry: number)Dominators.Of(BiAdjacentGraph, int)BiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.OfIteratedDominanceFrontier.of(Dominators)IteratedDominanceFrontier::ofIteratedDominanceFrontier.ofIteratedDominanceFrontier.Of