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.
Problems in this chapter
Section titled “Problems in this chapter”- 8.1 Find the loops of a function and how they nestYou find which blocks of a control-flow graph form a loop, which block is the header of each loop, and which loop lies in which.
- 8.2 Find loops that can be entered in more than one placeYou find the loops of a control-flow graph that can be entered at more than one block, which are called irreducible, and see how Graphity reports them.
- 8.3 Find the blocks where the dominance of a block endsYou get the dominance frontier of each block of a control-flow graph, which holds the blocks where the dominance of that block ends.
- 8.4 Find the blocks that need a phi-node for a variableYou find the blocks where SSA form (static single assignment) needs a phi-node for a variable: the iterated dominance frontier of the blocks that assign it.
- 8.5 Find which conditions decide whether a block runsYou get the control dependence of a graph: for each block, the branches that decide whether it runs.
The examples of this chapter use the control flow of three methods of a small notebook program:
showshowshowShowhas two nested loops;parseArgsparseArgsparseArgsParseArgsis a state machine;loadloadloadLoadhas a loop and anifin it.
Each problem shows the source of the method it analyzes.
The control-flow graph
Section titled “The control-flow graph”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.
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.
Dominance frontiers and phi-nodes
Section titled “Dominance frontiers and phi-nodes”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.
Control dependence
Section titled “Control dependence”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.
The graphs that the analyses take
Section titled “The graphs that the analyses take”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.