Skip to content

8 Control-flow analysis

This chapter shows the graph of the paths through a function, and what compilers and analyzers read from it: loops, dominance frontiers, phi-nodes and control dependence.

Compilers, static analyzers, decompilers and bytecode tools all look at a function the same way: as a graph of the paths that its execution can take. With the library graphity-controlflowgraphity-controlflow@kobayami/graphity-controlflowGraphity.ControlFlow, you read from that graph where the loops are and how they nest, where the dominance of a block ends, where SSA form (static single assignment) needs phi-nodes, and which branch decides whether a block runs. The chapter builds on the chapter Dominance. The library is part of graphitygraphity-all@kobayami/graphityGraphity.All, which brings all of Graphity. It can also be added on its own.

The examples of this chapter use the control flow of three methods of a small notebook program:

  • showshowshowShow has two nested loops;
  • parseArgsparseArgsparseArgsParseArgs is a state machine;
  • loadloadloadLoad has a loop and an if in it.

Each problem shows the source of the method it analyzes.

A basic block is a stretch of code that runs from top to bottom without a jump into it or out of it, except at its start and at its end. The control-flow graph has a node for each basic block and an edge a -> b if execution can continue with block b after block a. The entry is the block where the function starts. An exit is a block where it returns.

void show(List<Note> notes) {
out.println(notes.size() + " notes"); // entry
for (Note note : notes) { // nextNote: another note?
List<String> lines = format(note).lines().toList(); // format
for (String line : lines) { // nextLine: another line?
out.println(" " + line); // print
}
}
out.flush(); // exit
}
fn show(notes: &[Note]) {
println!("{} notes", notes.len()); // entry
for note in notes { // nextNote: another note?
let text = format(note); // format
for line in text.lines() { // nextLine: another line?
println!(" {line}"); // print
}
}
let _ = io::stdout().flush(); // exit
}
function show(notes: Note[]): void {
out.write(`${notes.length} notes\n`); // entry
for (const note of notes) { // nextNote: another note?
const lines = format(note).split("\n"); // format
for (const line of lines) { // nextLine: another line?
out.write(` ${line}\n`); // print
}
}
out.end(); // exit
}
void Show(List<Note> notes)
{
output.WriteLine($"{notes.Count} notes"); // entry
foreach (Note note in notes) // nextNote: another note?
{
string[] lines = Format(note).Split('\n'); // format
foreach (string line in lines) // nextLine: another line?
{
output.WriteLine(" " + line); // print
}
}
output.Flush(); // exit
}

This method showshowshowShow of a small notebook program prints its notes. Its control-flow graph has six blocks: the code before the loops, the test of each loop, the code that each loop repeats, and the code after the loops.

0: entry1: nextNote2: format3: nextLine4: print5: exit

A loop is a set of blocks that execution can run through again and again. The block through which a loop is entered is its header. That is where the test of a for or while loop sits. Every block of a loop lies on a cycle through its header. The body of a loop is its header with all the other blocks of the loop. Loops nest: in showshowshowShow, the loop over the lines lies in the body of the loop over the notes. The first has the header nextLine, and the second has the header nextNote. A loop that can be entered at more than one block is irreducible. Code with goto can produce such a loop.

A block d dominates a block n if every path from the entry to n passes through d. Following the paths from d, you come to blocks where this ends. These blocks are the dominance frontier of d. They are the first blocks after d on the paths from d that d does not strictly dominate. Typical such blocks are the block where the two branches of an if join, and the header of a loop.

In SSA form, every variable is assigned exactly once. Where paths with different assignments of a variable join, a phi-node picks the value of the path that was taken. The phi-nodes of a variable go into the iterated dominance frontier of the blocks that assign it. That is their dominance frontier, then the dominance frontier of the blocks found, and so on until no block is added.

A block is control dependent on a branch if the outcome of the branch decides whether the block runs. In loadloadloadLoad, the if decides whether the line is parsed. Control dependence follows from the dominance frontiers of the post-dominators. The post-dominators are the dominators of the transposed graph, with the exits as its entries.

Graphity takes the control-flow graph as a plain GraphGraphGraphGraph, built from your own representation of the code. The blocks are node IDs, and their instructions stay in your own data. Most analyses of this chapter follow edges against their direction as well, from a block to its in-nodes. So LoopsLoopsLoopsLoops and DominatorsDominatorsDominatorsDominators take a BiAdjacentGraphBiAdjacentGraphBiAdjacentGraphBiAdjacentGraph, which adds the in-nodes of every node to a graph. Build that graph once with BiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Of and pass it to both. The dominance frontiers and the placement of phi-nodes take the dominators computed on it.