Data-flow analysis
Data-flow analysis computes properties about program values across control-flow graphs (e.g., reaching definitions, live variables, available expressions) by solving equations on a lattice using iterative fixed-point methods.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Data-flow analysis is used by compiler engineers during optimisation and code generation to determine properties such as which definitions reach a use, which variables are live, and which expressions are already computed. These results enable optimisations like dead-code elimination, register allocation improvements, common-subexpression elimination and correct placement of code motion.
Key ideas
Start with definitions:
- Basic block: a straight-line code sequence with a single entry and single exit; no jumps except at the end and no jump targets except at the beginning. (Draw each basic block as a rectangle containing its statements.)
- Control-flow graph (CFG): a directed graph whose nodes are basic blocks and whose edges represent possible flow of control. (Draw nodes as rectangles and arrows for edges; label entry and exit.)
- Data-flow fact: a boolean or set-valued property attached to program points (entry to a block, exit from a block) describing something about values (e.g., set of definitions reaching this point).
- Transfer function: for a node n, the function that maps IN[n] to OUT[n] describing how facts change across n.
- Meet operator (⊓): combines facts from multiple control-flow predecessors (examples: set union, set intersection). Must be associative, commutative and idempotent for analyses. Union corresponds to may analyses (facts that may hold on some path); intersection corresponds to must analyses (facts that hold on all paths).
- Forward problem: IN at a node depends on predecessors' OUT. Example: reaching definitions.
- Backward problem: OUT at a node depends on successors' IN. Example: live variables.
- Gen and Kill (or Def and Use): for bitvector problems, each node has a gen set (facts generated by node) and a kill set (facts invalidated by node).
General equation structure for bitvector frameworks:
- Forward:
OUT[n] = transfer_n(IN[n]),IN[n] = meet_{p in preds(n)} OUT[p]. - Backward:
IN[n] = transfer_n(OUT[n]),OUT[n] = meet_{s in succs(n)} IN[s].
Common concrete forms (for classical problems):
- Reaching definitions (forward, meet = union):
OUT[n] = gen[n] ∪ (IN[n] − kill[n]),IN[n] = ⋃_{p ∈ preds(n)} OUT[p]. Boundary:IN[entry] = ∅. - Live variables (backward, meet = union):
IN[n] = use[n] ∪ (OUT[n] − def[n]),OUT[n] = ⋃_{s ∈ succs(n)} IN[s]. Boundary:OUT[exit] = ∅. - Available expressions (forward, meet = intersection):
OUT[n] = gen[n] ∪ (IN[n] − kill[n]),IN[n] = ⋂_{p ∈ preds(n)} OUT[p]. Boundary:IN[entry] = Uwhere U is the universal set of all expressions (top element for intersection frameworks).
Mathematical framework and convergence:
- Facts are elements of a lattice (a partially ordered set with meet and join). Bitvector facts use the powerset lattice (sets ordered by ⊆) which has finite height for finite programs.
- Transfer functions must be monotone: if x ≤ y then f(x) ≤ f(y). For finite-height lattices, repeated application of monotone functions using meet converges to a fixed point; with standard initializations it yields the least fixed point for may (union) analyses and the greatest fixed point for must (intersection) analyses. In practice compilers use iterative worklist algorithms to reach the fixed point.
What to draw for an exam diagram: draw the CFG with nodes labelled n1,n2,...; below or beside each node draw the gen and kill sets; draw arrows for preds/succs; if tracing iteration, draw a table with rows for each iteration showing IN[n] and OUT[n] for every node until no change.
Formulas
OUT[n] = gen[n] ∪ (IN[n] − kill[n]) — gen and kill form for forward bitvector problems. Symbols: OUT[n], IN[n] are sets of facts at node n (no SI units). Applies when meet operator is union and facts are sets.
IN[n] = ⋃_{p ∈ preds(n)} OUT[p] — IN is union of predecessors' OUT for forward analyses. Symbols: preds(n) = set of predecessor nodes of n.
IN[n] = use[n] ∪ (OUT[n] − def[n]) — backward bitvector form (live variables). Symbols: use[n] set of variables used in n; def[n] set defined in n.
OUT[n] = ⋃_{s ∈ succs(n)} IN[s] — OUT is union of successors' IN for backward analyses. Symbols: succs(n) = set of successor nodes of n.
For iterative algorithm (worklist) pseudocode syntax to remember:
initialize IN and OUT
worklist = all nodes
while worklist not empty:
n = remove(worklist)
if analysis is forward:
IN[n] = meet over preds(n) of OUT
newOUT = transfer_n(IN[n])
if newOUT != OUT[n]: OUT[n] = newOUT; add successors(n) to worklist
else (analysis is backward):
OUT[n] = meet over succs(n) of IN
newIN = transfer_n(OUT[n])
if newIN != IN[n]: IN[n] = newIN; add predecessors(n) to worklist
(Here transfer_n uses gen/kill or appropriate function.)
Fixed-point condition: IN[n] and OUT[n] stabilized (no change across an iteration) for all n.
Worked examples
Example 1 — Reaching definitions (forward). Program (numbered nodes): 1: x = 1 (def d1) 2: if (...) goto 4 else goto 3 3: x = 2 (def d3) 4: y = x + 3 (def d4) CFG edges: 1→2, 2→3, 2→4, 3→4. Compute gen/kill (only x defs matter for clarity):
- gen[1]={d1}, kill[1]={d3}
- gen[2]=∅, kill[2]=∅
- gen[3]={d3}, kill[3]={d1}
- gen[4]={d4}, kill[4]=∅
Equations:
OUT[n] = gen[n] ∪ (IN[n] − kill[n]),IN[n] = ⋃ preds OUT[p]. BoundaryIN[1]=∅. Iteration steps (showing IN then OUT): Iteration 0: IN[1]=∅ ⇒ OUT[1] = {d1}. IN[2] = OUT[1] = {d1} ⇒ OUT[2] = gen2 ∪ ({d1} − ∅) = {d1}. IN[3] = OUT[2] = {d1} ⇒ OUT[3] = {d3} ∪ ({d1} − {d1}) = {d3}. IN[4] = OUT[2] ∪ OUT[3] = {d1} ∪ {d3} = {d1,d3} ⇒ OUT[4] = {d4} ∪ ({d1,d3} − ∅) = {d1,d3,d4}. Iteration 1: Recompute IN[2] = OUT[1] = {d1} (no change), OUTs unchanged -> fixed point. Final reaching definitions at entry of block 4: IN[4] = {d1, d3}.
Example 2 — Live variables (backward). Program basic blocks and CFG:
1: x = a + b (def={x}, use={a,b})
2: if x>0 goto 4 else goto 3 (use={x})
3: y = x + 1 (def={y}, use={x})
4: z = y + x (def={z}, use={y,x})
5: return z (use={z})
Edges: 1→2, 2→3, 2→4, 3→4, 4→5. Boundary OUT[5]=∅.
We use IN[n] = use[n] ∪ (OUT[n] − def[n]), OUT[n] = ⋃ succs IN[s].
Step-by-step backward propagation:
- OUT[5]=∅ ⇒ IN[5]=use5∪(∅−{}) = {z}.
- OUT[4]=IN[5] = {z} ⇒ IN[4]=use4 ∪ (OUT4 − def4) = {y,x} ∪ ({z} − {z}) = {y,x}.
- OUT[3]=IN[4] = {y,x} ⇒ IN[3]=use3 ∪ (OUT3 − def3) = {x} ∪ ({y,x} − {y}) = {x}.
- OUT[2]=IN[3] ∪ IN[4] = {x} ∪ {y,x} = {x,y} ⇒ IN[2]=use2 ∪ (OUT2 − def2) = {x} ∪ ({x,y} − ∅) = {x,y}.
- OUT[1]=IN[2] = {x,y} ⇒ IN[1]=use1 ∪ (OUT1 − def1) = {a,b} ∪ ({x,y} − {x}) = {a,b,y}. No further change occurs; fixed point reached. Final important facts: IN[1]={a,b,y}, IN[4]={y,x}, OUT[4]={z}.
These worked examples show the direction (forward/backward), initialization and iterative stabilization.
Common mistakes
- Confusing gen/kill with use/def; remember gen/kill describe facts produced/invalidated, while use/def describe variable use and assignment.
- Forgetting monotonicity requirement when defining custom transfer functions; non-monotone functions can prevent convergence.
- Updating neighbors incorrectly in the worklist algorithm (e.g., only adding predecessors for forward analysis).
- Not iterating to fixed point: stopping after one pass can give incorrect answers at merge points.
For your exam
Typical question forms: definitions (define CFG, basic block, lattice, monotone function), short notes (compare forward vs backward analyses), derivations (derive equations for reaching definitions or liveness), algorithm tracing (iterate equations on a small CFG), and proofs of convergence (use finite lattice assumption).
Full-marks answers should include:
- Precise definitions with bolded terms and clear assumptions (finite program, finite set of facts).
- Correct equations and clearly stated boundary conditions.
- A small, labelled CFG if required, with gen/kill or use/def sets shown.
- A step-by-step iterative trace till fixed point for algorithm questions; show initial values and each iteration's IN/OUT for every node.
- For proofs, mention lattice, monotonicity and finite-height imply termination of iterative method; or mention widening if infinite.
Quick check
- Is liveness a forward or backward data-flow problem?
- For reaching definitions, what is the meet operator?
- Give the equation to compute OUT[n] for a forward bitvector problem.
Answers: 1. Backward; 2. Union; 3.
OUT[n] = gen[n] ∪ (IN[n] − kill[n]).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?