Skip to content

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.

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”.

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?

The control flow and its exit block
Its control dependence
0: entry1: header2: body3: then4: next5: exit
0: entry1: header2: body3: then4: next5: exit
Input
  • Exit block
Output
  • 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.

Control dependence follows from the post-dominators of the control-flow graph, in three steps:

  1. Compute the post-dominators. See Find the nodes that every path to the exit passes through.
  2. Compute their dominance frontiers. See Find the blocks where the dominance of a block ends.
  3. 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
Full example
// 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
Full example
// 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
Full example
// 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
Full example

The 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.