Section 3.15

Data-flow analysis

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 nn of the CFG has an incoming fact In[n]\mathrm{In}[n], at its start, and an outgoing fact Out[n]\mathrm{Out}[n], at its end.

The join rule depends on the kind of question.

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.

  1. Set In[entry]\mathrm{In}[\mathrm{entry}] to the boundary fact. Set Out[n]\mathrm{Out}[n] 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.

  2. Put every node on a worklist, the list of nodes whose facts may need updating.

  3. Take a node nn off the worklist. Unless nn is the entry, set In[n]\mathrm{In}[n] to the join of Out[p]\mathrm{Out}[p] over all predecessors pp, the nodes with an edge to nn. Apply the transfer function of nn to In[n]\mathrm{In}[n].

  4. If the result differs from Out[n]\mathrm{Out}[n], store it as the new Out[n]\mathrm{Out}[n] and put every successor of nn, every node that nn has an edge to, on the worklist.

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

The edges are B1→B2\mathrm{B1} \to \mathrm{B2}, B2→B3\mathrm{B2} \to \mathrm{B3}, B2→B4\mathrm{B2} \to \mathrm{B4} and B3→B2\mathrm{B3} \to \mathrm{B2}. The transfer function of a block adds the variables it writes: B1 adds xx, B3 adds yy. The join at B2, which has two incoming edges, is intersection. The boundary fact is In[B1]=∅\mathrm{In}[\mathrm{B1}] = \emptyset, 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, {x,y}\{x, y\}.

Round 1:

Round 2 recomputes In[B2]={x}∩{x,y}={x}\mathrm{In}[\mathrm{B2}] = \{x\} \cap \{x, y\} = \{x\}. Nothing has changed, so the analysis has reached its fixed point.

Result: inside the loop, xx is certainly written, so y = x + 1 is accepted. At B4, y∉{x}y \notin \{x\}, 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 yy.

Two mistakes show why the rules are what they are.

Stated precisely

Order the facts by precision: write a⊑ba \sqsubseteq b when fact aa is at least as precise as fact bb, meaning every state that aa describes is also described by bb. For a may-set analysis, ⊑\sqsubseteq is ⊆\subseteq. For a must-set analysis, ⊑\sqsubseteq is ⊇\supseteq, because a larger must-set says more. The join a⊔ba \sqcup b is the most precise fact cc with a⊑ca \sqsubseteq c and b⊑cb \sqsubseteq c: 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 pred(n)\mathrm{pred}(n) be the set of predecessors of node nn, let fnf_n be its transfer function, and let ι\iota be the boundary fact, with the entry assumed to have no incoming edge. The data-flow equations are In[n]={ιif n=entry,⨆p∈pred(n)Out[p]otherwise,Out[n]=fn(In[n]).\mathrm{In}[n] = \begin{cases} \iota & \text{if } n = \mathrm{entry}, \\ \bigsqcup_{p \in \mathrm{pred}(n)} \mathrm{Out}[p] & \text{otherwise,} \end{cases} \qquad \mathrm{Out}[n] = f_n(\mathrm{In}[n]). Suppose every fnf_n is monotone (a⊑ba \sqsubseteq b implies fn(a)⊑fn(b)f_n(a) \sqsubseteq f_n(b)) 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 Fix[n]\mathrm{Fix}[n] for its value at node nn.

The ideal answer at nn applies the transfer functions along each CFG path from the entry to nn, starting from ι\iota, and joins the results over all such paths; write it AllPaths[n]\mathrm{AllPaths}[n]. 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: AllPaths[n]⊑Fix[n]\mathrm{AllPaths}[n] \sqsubseteq \mathrm{Fix}[n]. When every transfer function distributes over the join, f(a⊔b)=f(a)⊔f(b)f(a \sqcup b) = f(a) \sqcup f(b), 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 x∈[1,2]x \in [1, 2] and y∈[1,2]y \in [1, 2], but it has lost the fact that x+y=3x + y = 3. 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.

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