8.5 Find which conditions decide whether a block runs
You get the control dependence of a graph: for each block, the branches that decide whether it runs.
Problem
Section titled “Problem”Which condition decides whether a statement runs? Often it is the condition of the if or loop that
the statement is written in. After a break, a continue, an early return or a goto, it can be
a branch elsewhere. A statement can also depend on several branches. A block of a control-flow
graph is control dependent on a branch if the outcome of the
branch decides whether the block runs. You need this for:
- a tool that extracts the statements that matter for a value, and with them the conditions that decide whether they run;
- a security analysis that asks which checks guard a call;
- a tool that explains code: “this statement runs only if the input is not blank”.
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. exit is its only exit. Which branch decides whether each block runs?
- Exit block
- Block of the dependence graph
- From a branch to a block it decides
The picture after shows the dependence graph that the code returns: all blocks, and an edge from each deciding block to the blocks it decides. The blocks stay in their places, and the edges of the control flow are not drawn.
Solution
Section titled “Solution”Control dependence follows from the post-dominators of the control-flow graph, in three steps:
- Compute the post-dominators. See Find the nodes that every path to the exit passes through.
- Compute their dominance frontiers. See Find the blocks where the dominance of a block ends.
- Turn the result around, which gives its transposed graph.
// Control dependence is the transposed graph of the dominance frontiers of the// post-dominators.Dominators postDominators = Dominators.of(BiAdjacentGraph.of(cfg).transposed(), exit);Graph dependence = BiAdjacentGraph.of(ControlFlow.dominanceFrontiers(postDominators)).transposed();// dependence: header -> header, header -> body, header -> next, body -> then// Control dependence is the transposed graph of the dominance frontiers of the// post-dominators.let post_dominators = Dominators::of(&BiAdjacentGraph::of(&cfg).transposed(), exit);let dependence = BiAdjacentGraph::of(&control_flow::dominance_frontiers(&post_dominators)) .transposed();// dependence: header -> header, header -> body, header -> next, body -> then// Control dependence is the transposed graph of the dominance frontiers of the// post-dominators.const postDominators = Dominators.of(BiAdjacentGraph.of(cfg).transposed(), exit);const dependence = BiAdjacentGraph.of(ControlFlow.dominanceFrontiers(postDominators)).transposed();// dependence: header -> header, header -> body, header -> next, body -> then// Control dependence is the transposed graph of the dominance frontiers of the// post-dominators.Dominators postDominators = Dominators.Of(BiAdjacentGraph.Of(cfg).Transposed(), exit);Graph dependence = BiAdjacentGraph.Of(ControlFlow.DominanceFrontiers(postDominators)) .Transposed();// dependence: header -> header, header -> body, header -> next, body -> thenThe loop condition in header decides whether body and next run, and
whether header runs again. The if in body decides whether then runs. entry and exit
depend on no branch: they always run.
Good to know
Section titled “Good to know”- Why the transposed graph. In the post-dominators, the dominance frontier of a block holds the branches that the block depends on. Turned around, the graph has an edge from each branch to the blocks it decides.
- Which outcome. The result names the branch, not the outcome. A block
ythat depends on a branchxruns whenxcontinues with an out-node ofxthatypost-dominates.Ancestry.ofAncestry::of_dominatorsAncestry.ofAncestry.Ofon the post-dominators answers this withAncestry.isAncestorOfAncestry::is_ancestor_ofAncestry.isAncestorOfAncestry.IsAncestorOf.nextpost-dominatesbodybut notexit, so it runs whenheadercontinues withbody. - Several exits. A method that returns in several places has several exits: compute the
post-dominators from all of them, with
Dominators.ofDominators::of_entriesDominators.ofDominators.Ofon the transposed graph. - Endless loops. A block from which no exit can be reached has no
immediate post-dominator, and the result reads as if such blocks were not there. In the result,
they depend on no branch. A branch that either enters such a loop or goes on to the rest of the
method is not shown as deciding the blocks after it, although it decides whether they run. Pass
the exit and one block of each such loop as the entries of the transposed graph to
Dominators.ofDominators::of_entriesDominators.ofDominators.Of. See Find the nodes that every path to the exit passes through.
API used
Section titled “API used”Dominators.of(BiAdjacentGraph, int)Dominators::ofDominators.of(graph: BiAdjacentGraph, entry: number)Dominators.Of(BiAdjacentGraph, int)BiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.OfBiAdjacentGraph.transposed()BiAdjacentGraph::transposedBiAdjacentGraph.transposedBiAdjacentGraph.TransposedControlFlow.dominanceFrontiers(Dominators)control_flow::dominance_frontiersControlFlow.dominanceFrontiersControlFlow.DominanceFrontiersDominators.of(BiAdjacentGraph, IntList)Dominators::of_entriesDominators.of(graph: BiAdjacentGraph, entries: IntList)Dominators.Of(BiAdjacentGraph, ReadOnlySpan<int>)Ancestry.of(Dominators)Ancestry::of_dominatorsAncestry.of(dominators: Dominators)Ancestry.Of(Dominators)Ancestry.isAncestorOf(int, int)Ancestry::is_ancestor_ofAncestry.isAncestorOfAncestry.IsAncestorOf