Algorithm Search⌘K

Conceptintro

Dataflow analysis

Also called data-flow analysis, flow analysis.

Computes facts that hold at each program point on all paths, by propagating them along the control flow graph to a fixed point.

Dataflow analysis is a family of static analyses that compute, for each point of a program, a fact that holds whenever execution reaches that point: which definitions can reach it, which variables are still needed, which expressions are already computed. It works on the control flow graph: it gives each basic block a transfer function, writes one equation per block, and solves the equations by iteration to a fixed point. This page is the concept. The exact definitions are on monotone dataflow framework and distributive dataflow framework, the bit-vector class is gen-kill problem, the solver is the worklist dataflow algorithm, and the two possible answers are the meet-over-all-paths solution and the maximal fixed-point solution.

In plain words

A compiler wants to know things about a program before it runs it. Is this value used again later? Was this sum already computed on every way to get here? The compiler cannot try every input, so it reasons about the shape of the program instead.

It draws the program as a map of straight-line pieces joined by jumps. At each piece it keeps a small note of what it knows so far. Each piece has a rule that turns the note at its start into the note at its end. Where two ways meet, the compiler combines the notes into one note that is true on both ways. It goes around the map and updates the notes. A loop sends notes back to earlier pieces, so one pass is not enough. When a full round changes no note, the notes are stable, and the compiler uses them.

The idea

Points and facts. A program point is the point just before or just after a block (or an instruction). The analysis attaches to each point a value from a set LL of facts. For reaching definitions a fact is a set of definition sites; for constant propagation it is a map from variables to "this constant" or "not a constant". The examples here use the toy IR of the area: three-address code in basic blocks, with jump, br and ret as terminators.

The order on facts. Facts are ordered by how much they claim. A partial order ⊑\sqsubseteq is reflexive, antisymmetric and transitive. A meet semilattice (L,⊓)(L, \sqcap) is a set with a binary operation ⊓\sqcap that is idempotent, commutative and associative; it defines the order x⊑yx \sqsubseteq y if and only if x⊓y=xx \sqcap y = x, and then x⊓yx \sqcap y is the greatest lower bound of xx and yy. This page and the other dataflow pages use this meet convention of Kildall and the dragon book. ⊤\top is the identity of meet (x⊓⊤=xx \sqcap \top = x for all xx); it is the optimistic start value, "no information against any claim yet". Lower means a weaker claim: more conservative, but still safe. The height hh of LL is the largest kk such that a strict chain x0⊐x1⊐⋯⊐xkx_0 \sqsupset x_1 \sqsupset \cdots \sqsupset x_k exists. A join semilattice is the same structure with the order turned upside down, and turning the order upside down maps this convention onto the one of abstract interpretation, where ⊥\bot is the start value and the answer is a least fixed point.

Transfer functions and direction. Each block BB has a transfer function fB:L→Lf_B : L \to L. A forward problem maps the fact at the start of BB to the fact at its end; a backward problem maps the fact at the end to the fact at the start, so it runs on the reverse CFG. The boundary value ι\iota is the fact at the entry block (forward) or after each ret block (backward). It is a fact about the outside world, and it is not ⊤\top in general.

The equations. For a forward problem, with the empty meet equal to ⊤\top:

IN[B]=(ι if B is the entry)⊓⊓P∈pred(B)OUT[P],OUT[B]=fB(IN[B]).\mathrm{IN}[B] = \Big(\iota \text{ if } B \text{ is the entry}\Big) \sqcap \mathop{\large\sqcap}_{P \in \mathrm{pred}(B)} \mathrm{OUT}[P], \qquad \mathrm{OUT}[B] = f_B(\mathrm{IN}[B]).

For a backward problem, swap IN with OUT and predecessors with successors, and put ι\iota in the meet of each block that ends in ret.

May and must. The meet says what happens where paths join. In a may problem a fact holds if it holds on some path, so the meet is union and ⊤=∅\top = \emptyset. In a must problem a fact holds only if it holds on every path, so the meet is intersection and ⊤\top is the set of all facts. Reaching definitions and live variables are may problems; available expressions and very busy expressions are must problems.

The ideal answer and the computed answer. The ideal answer at the start of BB is the meet over all paths from the entry to BB of the composed transfer functions of each path, applied to ι\iota. Iteration from ⊤\top computes instead the maximal fixed point of the equations. When every fBf_B is monotone, MFP ⊑\sqsubseteq MOP, so MFP is safe; when every fBf_B distributes over ⊓\sqcap, the two are equal (Kildall 1973; Kam and Ullman 1977). The MOP-MFP coincidence theorem states this.

Why it is safe, and why it is not exact. Every real execution follows a path of the CFG, so a fact that holds on all CFG paths holds on every execution. The converse fails: the CFG also has paths that no input takes, for example both arms of two branches on the same test. So the analysis is conservative. It cannot be exact in general. Put a read of y after a piece of code that halts only on some inputs: y is truly live before that code only if the code can halt, so an exact liveness analysis would decide the halting problem. Rice's theorem gives the same limit for every nontrivial property of program behavior. Even the meet over all CFG paths cannot be computed for every monotone framework (Kam and Ullman 1977).

Cost. With a lattice of height hh, a CFG with nn blocks and mm edges, and a worklist, each block value falls at most hh times, so the solver does O(nh)O(n h) transfer applications and O(mh)O(m h) meets. For a bit-vector problem over uu facts, h=uh = u and one set operation costs O(u/w)O(u / w) machine words, with ww the word size.

Examples

The running example of the dataflow pages has the parameters n and x. It sums x * 2 over the odd values of i below n.

entry:
  i = 0
  s = 0
  jump head
head:
  t = i < n
  br t body done
body:
  a = x * 2
  u = i & 1
  br u odd next
odd:
  s = s + a
  jump next
next:
  i = i + 1
  jump head
done:
  ret s

The CFG has six blocks and seven edges: entry to head, head to body, head to done, body to odd, body to next, odd to next, and next to head, which closes the loop. On the input n = 5, x = 3 the program returns 12.

A may problem: live variables. A variable is live at a point if some path from that point reads it before it is written. The analysis is backward with union, and the boundary value after ret is the empty set. At the start of each block it gives:

block live at the start available at the start
entry n, x none
head i, n, s, x none
body i, n, s, x i < n
odd a, i, n, s, x i < n, x * 2, i & 1
next i, n, s, x i < n, x * 2, i & 1
done s i < n

Only n and x are live at the entry: they are the parameters, and every other name is written before it is read. a is live only between body and odd, so a register allocator can reuse its register in the rest of the loop.

A must problem: available expressions. An expression is available at a point if every path to the point computes it and changes none of its operands after that. The analysis is forward with intersection, and the boundary value at the entry is the empty set. In the third column of the table, x * 2 is available in odd, so s = s + a could reuse it, but it is not available in head: the path from entry never computed it. If the analysis started the entry at ⊤\top (all expressions) instead of the empty set, it would claim that x * 2 is available in head. That is the boundary trap of must problems.

The loop needs more than one pass. For live variables the worklist processes 11 blocks before it stops, not 6, because x and a become live in body only after the facts travel back around the loop. The trace is on the worklist dataflow algorithm page.

Non-example: a precise answer that dataflow analysis misses. Constant propagation on two branches that set (x,y)(x, y) to (2,3)(2, 3) and to (3,2)(3, 2), followed by z = x + y, gives zz = "not a constant", although z=5z = 5 on every path. The meet merges xx and yy separately before the sum. The constant propagation problem is monotone but not distributive, so its MFP can be strictly below its MOP.

Where it is used

References

  1. Gary A. Kildall, A unified approach to global program optimization, Proceedings of the 1st ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages (POPL 1973), 194-206, the whole paper (facts on a meet semilattice, one optimizing function per node, the iterative algorithm, and constant propagation and common subexpressions as instances)
  2. John B. Kam and Jeffrey D. Ullman, Monotone data flow analysis frameworks, Acta Informatica 7(3), 1977, 305-317, the whole paper (monotone frameworks, the maximal fixed point, and the proof that no algorithm computes the meet over all paths solution of every monotone framework)
  3. Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Pearson Addison-Wesley, 2007, Section 9.2 (Introduction to Data-Flow Analysis, with reaching definitions, live variables and available expressions) and Section 9.3 (Foundations of Data-Flow Analysis)
  4. Keith D. Cooper and Linda Torczon, Engineering a Compiler, third edition, Morgan Kaufmann, 2022, Chapter 9 (Data-Flow Analysis), Section 9.2 (Iterative Data-Flow Analysis)
  5. Flemming Nielson, Hanne Riis Nielson and Chris Hankin, Principles of Program Analysis, Springer, 1999, Chapter 2 (Data Flow Analysis), Sections 2.1 to 2.4

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/8XWVZX/text.yaml