Definition
Data-flow analysis is a static analysis §3.1, a way to reason about every run of a program without running it, that computes a fact for each point of the program. It propagates facts along the edges of the control-flow graph §3.13 (CFG), the graph of possible transfers between blocks of instructions. Each instruction changes the incoming fact according to a transfer function. Where several paths meet, a join rule combines their facts into one. The computation repeats until no fact changes.
The idea
A compiler wants to warn when a variable can be read before anything was written to it. The C code if (c) x = 1; y = x; reads x after two possible paths: on one path x was written, on the other it was not. Checking each path separately does not work in general, because a loop gives a program infinitely many paths.
Data-flow analysis works per program point instead of per path. It attaches to each point one summary fact, here the set of variables that are certainly written, and asks each instruction how it changes that fact. Writing x adds x to the set. Where the two paths of the if meet, the set holds what both paths guarantee: x is in it only if both paths wrote x. One path did not, so the read in y = x gets a warning. A loop is handled by going around it again until the facts stop changing. The price of one fact per point is precision: after a merge, the analysis no longer knows which path produced which part of the fact.
How it works
An analysis is defined by five choices. Each node of the CFG has an incoming fact , at its start, and an outgoing fact , at its end.
The domain, the set of possible facts: for example sets of variables, sets of definitions, or a range of possible values for each variable. The facts are ordered from more precise to less precise, as the abstract domain §3.6 entry explains.
The direction. A forward analysis carries facts from the entry along the edges, as in the initialization example. A backward analysis carries facts from the exit against the edges. Live-variable analysis is backward: a variable is live at a point if some later instruction reads it before anything overwrites it.
A transfer function for each node, which maps the fact before the node to the fact after it (for a backward analysis, after to before).
The join rule, which combines the facts arriving on several edges into one fact.
The boundary fact at the entry (at the exit, for a backward analysis), which states what is known before the code runs.
The join rule depends on the kind of question.
A may-analysis asks what can hold on at least one path. Its fact must keep every possibility from every incoming path, so its join on sets is union. Reaching definitions §3.16, which finds the assignments that may have produced the current value of a variable, and live-variable analysis are may-analyses.
A must-analysis asks what holds on every path. Its fact may keep only what all incoming paths guarantee, so its join on sets is intersection. Definite initialization and available expressions (expressions certainly computed already, whose value can be reused) are must-analyses.
A value analysis keeps, for each variable, a description of its possible values, and joins two descriptions into the most precise description that covers both. An interval analysis §3.9, which describes each value by a range, joins the ranges and into . That range also admits 2, because a single range cannot express the gap.
In every case the joined fact must be true whichever incoming path ran.
The worklist algorithm §14.1, described by Gary Kildall in 1973 [258], computes the facts of a forward analysis.
Set to the boundary fact. Set of every node to the most precise value of the domain: the empty set for a may-analysis, the set of all items for a must-analysis. The iteration then loses precision only where some path forces it to.
Put every node on a worklist, the list of nodes whose facts may need updating.
Take a node off the worklist. Unless is the entry, set to the join of over all predecessors , the nodes with an edge to . Apply the transfer function of to .
If the result differs from , store it as the new and put every successor of , every node that has an edge to, on the worklist.
Repeat steps 3 and 4 until the worklist is empty.
The facts that remain form a fixed point §3.22: applying any join and any transfer function again changes nothing.
The iteration ends when two conditions hold. Every transfer function is monotone, meaning a less precise input never gives a more precise output. And every fact can lose precision only a finite number of times, which holds for sets drawn from a finite program. A range of integers can grow a very large or unlimited number of times, so interval analysis uses widening §3.23, a step that jumps to a much coarser range to force the end.
Worked example
Take definite initialization: a forward must-analysis whose fact is the set of variables certainly written. The program has four blocks and reads an input c, which the analysis does not track.
B1, the entry:
x = 0, then go to B2.B2:
if c goto B4; otherwise go to B3.B3:
y = x + 1, then go back to B2.B4:
return y.
The edges are , , and . The transfer function of a block adds the variables it writes: B1 adds , B3 adds . The join at B2, which has two incoming edges, is intersection. The boundary fact is , the empty set: nothing is written before the code runs. The outgoing fact of every block starts at the most precise must-fact, the set of all variables, .
Round 1:
B1: .
B2: . B2 writes nothing, so .
B3: , so the read of in
y = x + 1is safe. .B4: .
Round 2 recomputes . Nothing has changed, so the analysis has reached its fixed point.
Result: inside the loop, is certainly written, so y = x + 1 is accepted. At B4, , so return y may read an unwritten variable, and the analysis warns. The warning is real: if c is true at the first test, the run goes B1, B2, B4 and never writes .
Two mistakes show why the rules are what they are.
Using union at B2 gives , so counts as written at B4 and the real bug disappears. A must-question needs intersection.
Starting at instead of gives, in round 1, . Then , and the analysis warns that
y = x + 1reads an unwritten . Round 1 also gives . Round 2 recomputes , unchanged, so the false alarm §1.22 stays. This result is still safe, because it never claims that a variable is written when it might not be, but it is less precise.
Stated precisely
Order the facts by precision: write when fact is at least as precise as fact , meaning every state that describes is also described by . For a may-set analysis, is . For a must-set analysis, is , because a larger must-set says more. The join is the most precise fact with and : union for may-sets, intersection for must-sets. The entry on partial orders and lattices §19.2.6 treats such orders in general.
For a forward analysis, let be the set of predecessors of node , let be its transfer function, and let be the boundary fact, with the entry assumed to have no incoming edge. The data-flow equations are Suppose every is monotone ( implies ) and the order has no infinite chain of facts that each lose precision. Then the worklist algorithm, started from the most precise facts, ends with the most precise solution of the equations, called the least fixed point; write for its value at node .
The ideal answer at applies the transfer functions along each CFG path from the entry to , starting from , and joins the results over all such paths; write it . Many texts call it the meet-over-all-paths solution, because they order facts the other way round and call the combination a meet. The fixed point is sound with respect to it: . When every transfer function distributes over the join, , the two are equal. Analyses whose transfer functions add one fixed set of facts and remove another, called the gen and kill sets, are distributive; reaching definitions is one. Constant propagation, which tracks whether a variable holds one known constant, is not.
Both guarantees describe real runs only under three assumptions: the CFG contains every transfer a run can make; each transfer function describes every effect its instructions can have, including writes through pointers and effects of calls; and the boundary fact describes every state in which the code can start.
Limits and common mistakes
A join forgets which path a fact came from. After if (c) { x = 1; y = 2; } else { x = 2; y = 1; }, an interval analysis knows and , but it has lost the fact that . A property that depends on such a link between values needs path sensitivity §3.11 or a relational abstract domain §15.12, which records relations between variables.
Swapping may and must is the most dangerous mistake. Union where intersection is needed turns “written on one path” into “written”, and an uninitialized read passes the check.
One pass is not enough when the CFG has a loop. Facts produced late in the loop reach the loop’s head only on the next round.
No join repairs a wrong graph or a wrong transfer function. A missing CFG edge, an unmodeled call, or a write through a pointer that the transfer function ignores makes the result unsound, however correct the join is.
An analysis of one function does not see across calls. Following facts through calls needs interprocedural summaries §15.14 or context sensitivity §3.12.
Questions answered
What is the join rule where two control-flow paths meet?
The join rule combines the facts arriving on the incoming paths into one fact that remains true whichever of those paths ran, and that is otherwise as precise as possible. Which operation that is depends on the question the data-flow analysis asks.
For a may-question, which asks what can hold on at least one path (which assignments may have produced a value, which values a variable may have), the join keeps every possibility: set union. If a register holds 1 on one path and 3 on the other, the set of its possible values after the merge is .
For a must-question, which asks what holds on every path (which bytes are certainly initialized), the join keeps only what all paths guarantee: set intersection. If one path wrote bytes and the other wrote bytes , only byte 1 is certainly written after the merge: . Union here would wrongly allow reading byte 0 after the second path.
For a range of values, the join is the smallest range that covers both: and join to .
At the head of a loop, the join is applied again on every round until the facts stop changing; a single pass is not enough.
Related entries: Control-flow graph (CFG) (§3.13), Fixed point (§3.22), Scratch memory (§2.24), Reaching definitions (§3.16), Widening and narrowing (§3.23). Further reading: [258].