Data-flow analysis
Data-flow facts describe program points at block entry and exit. The join operator depends on whether the property needs one possible path or every incoming path; choosing union by habit is a common source of wrong answers.
8.1 Directions, transfer functions and fixed points
For block , let IN and OUT describe its entry and exit. Pred and Succ are predecessor and successor blocks. A forward problem transports facts along execution; a backward problem transports demands against execution. Iterate until all equations hold, using a worklist to revisit affected neighbours.
| Analysis | Direction | Path question | Combine at joins |
|---|---|---|---|
| Liveness | Backward | Can some continuation use this value? | Union over successors |
| Available expressions | Forward | Was this expression computed without invalidation on every incoming path? | Intersection over predecessors |
| Constant propagation | Forward | Do all incoming values agree on one constant? | Per-variable agreement join |
For forward analyses, restrict attention to blocks reachable from entry or explicitly represent unreachable flow. An intersection over an unreachable predecessor must not invent or destroy facts about actual executions. Standard analyses conservatively approximate feasible paths; branch correlations are not automatically understood.
8.2 Liveness and dead assignments
Past papers · CS 2014–2026 (23 papers): 5 questions, 9 marks
A variable is live at a program point when its current value may be used on some continuation before any redefinition. For a block, USE contains variables read before any definition of that variable inside the block; DEF contains variables defined in the block. Compute USE with statement order: it is not the set of all names appearing on right-hand sides.
For one statement x=y+z, USE is and DEF is . For x=x+1, x belongs to both sets: the old value is read before the new value is installed. For consecutive statements without intervening control transfers, OUTIN.
An assignment to x is removable if x is not live immediately after it and evaluating the RHS has no required observable effect. Deletion may make earlier computations dead, so repeat or propagate the change. Unreachable code and dead values are different concepts.
For register allocation, simultaneously needed distinct values interfere and cannot occupy the same register. A compiler builds an interference graph from liveness; colouring assigns registers, and spills handle excess demand. A peak live count gives a lower bound on required registers, but arbitrary interference graphs can need more colours than that lower bound. Special handling of copies may permit coalescing equal values.
8.3 Available expressions and global CSE
Past papers · CS 2014–2026 (23 papers): 2 questions, 4 marks
An expression is available at a point if every path from entry to that point has evaluated it and no later assignment on the path has changed one of its operands. Let be the finite universe of candidate expressions. In a block, KILL contains expressions invalidated by assignments; GEN contains expressions computed in the block whose operand values survive to block exit.
For x=x+y, the RHS computes a value using the old x, but the assignment changes x. Consequently the expression x+y is not generated as available at exit under current variable names. For z=x+y; x=1, it is similarly absent from block GEN. Statement-level scanning avoids these mistakes.
Availability proves redundancy, but code generation must also provide a usable representative of the previous value on every path. If different branches compute into different temporaries, introduce suitable copies or a join value; one branch's temporary is not automatically defined on the other. Partial redundancy on only some paths requires a different transformation and is not enough for ordinary global CSE.
8.4 Constant propagation and agreement
Track an abstract value for each variable: UNDEF (no information yet from a reachable computation in the analysis), a particular constant , or NAC (not a single known constant). UNDEF is an analysis state, not permission to execute a language-level uninitialised read. Unknown input parameters start at NAC. Keep unreachable blocks separate from reachable unknown values.
Ordinary nonrelational propagation loses correlations. If one branch sets (x,y)=(2,5) and the other (3,4), each variable joins to NAC although x+y is always 7 on those two paths. Conditional propagation may ignore a branch proved unreachable; ordinary propagation over all CFG edges need not do so. Do not claim a variable constant merely because one predecessor assigns a constant or one test run produced it.
Last-minute recall
- Mark direction, boundary and join before solving data-flow equations.
- Iterate through loops until every set stops changing.
- USE means used before a definition in the same block.
- Liveness propagates backward with union.
- Available expressions propagate forward with intersection.
- An operand redefinition kills an expression even if the expression was just evaluated.
- Constant propagation requires agreement on every considered incoming path.
- UNDEF, unknown input and unreachable code are different states.