Data flow analyses: constant propagation, liveness analysis and common subexpression elimination
Global data-flow analysis on the flow graph: the IN/OUT equations, liveness (backward), reaching definitions and constant propagation (forward), and available expressions for common subexpression elimination, solved by iteration to a fixed point.
Transfer functions are set operations, and constant propagation works on a small lattice.
Concept notes
1. The data-flow framework
A data-flow analysis computes, for every basic block , a fact at its entry, , and at its exit, . Two ingredients define it:
a transfer function that describes what one block does to the fact, usually ;
a meet operator that combines facts where paths join: union for "on some path" questions, intersection for "on every path" questions.
Analysis
Direction
Meet
Fact at a point
Used for
Reaching definitions
forward
union
definitions that may reach
constant propagation, use-def chains
Live variables
backward
union
variables that may be used later
dead code, register allocation
Available expressions
forward
intersection
expressions already computed on every path
global CSE
Constant propagation
forward
lattice meet
variable = known constant or not
constant folding across blocks
Forward analyses compute from and take from the predecessors; backward analyses compute from and take from the successors.
The iterative algorithm starts from an initial value (empty set for union problems; the set of all expressions for available expressions, except at the entry), applies the equations to every block, and repeats until nothing changes. Because the sets only grow (or only shrink) and are finite, a fixed point is always reached.
2. Liveness analysis
A variable is live at a point if some path from uses before any redefinition of . Otherwise it is dead at .
For each block, find:
: variables used in before any definition in (upward-exposed uses);
: variables assigned in .
with at the exit. Work backwards. Inside a block, go instruction by instruction from the bottom: for x = y op z, remove x, then add y and z.
Uses: an assignment to a variable that is dead right after it is dead code; two variables that are never live at the same time can share a register. The number of registers needed is at least the largest number of variables live at one point.
The diagram shows the fixed point for a small loop. Note that b is live at the entry of B3 although B3 redefines it: a = b reads b first.
Live variables at the entry and exit of each block of a loop (n is defined before the code)
3. Reaching definitions
A definition of variable (an instruction that assigns ) reaches a point if there is a path from to along which is not redefined.
For each block:
: definitions in that survive to its end (the last definition of each variable in );
: all other definitions in the program of the variables that defines.
Start with (or ) and iterate. With a loop, a definition inside the loop flows back to the loop header, so the header usually sees both the initial definition and the loop definition of the same variable.
Use: if exactly one definition of reaches a use, and that definition is x = constant, the use can be replaced by the constant. If two different definitions reach (say s = 0 before a loop and s = s + i inside it), is not a constant there. Reaching definitions also build use-def chains, which link every use to the definitions that may supply its value.
4. Constant propagation
Global constant propagation finds, at each point, whether each variable has a known constant value. Each variable takes a value from a three-level lattice:
UNDEF (top, ): no definition seen yet;
a constant ;
NAC (bottom, ): not a constant (different values on different paths, or depends on input).
Meet at a join point, for one variable:
Meet
UNDEF
NAC
UNDEF
UNDEF
NAC
if , else NAC
NAC
NAC
NAC
NAC
NAC
Transfer for x = y op z: if y and z are both constants, x is the folded constant; if either is NAC, x is NAC; otherwise UNDEF. For x = input or a call result, x is NAC.
The analysis is forward. Run it to a fixed point, then replace every constant use and fold.
Key property: the framework is not distributive. With x = 2, y = 3 on one path and x = 3, y = 2 on the other, z = x + y is 5 on both paths, but meeting first gives x and y as NAC, so z is reported NAC. The answer is safe (never wrong), but may miss some constants.
5. Available expressions and common subexpression elimination
An expression y op z is available at point if on every path from the entry to it is computed and, after the last computation, neither y nor z is reassigned.
For each block:
: expressions computed in and not later killed in ;
: expressions that use a variable assigned in .
with and every other initialised to the set of all expressions (so the intersection does not lose facts on the first pass through a loop).
Global CSE: if y op z is available at the start of and computes it again before redefining y or z, replace the recomputation by a copy from a temporary that holds it on every incoming path. The intersection is what makes this safe: one path that kills the expression is enough to block the reuse.
Compare: liveness asks "will it be used later?" (backward, any path); availability asks "was it already computed?" (forward, all paths).
Formulas to remember
Liveness (backward)
Reaching definitions (forward)
Available expressions (forward, all paths)
Constant lattice meetNAC meet anything is NAC
Common mistakes
Running liveness forwards; it is a backward analysis, OUT comes from the successors.
Putting a variable in use only because it appears in the block; it must be read before being assigned in that block.
Using union for available expressions; one path that kills the expression makes it unavailable.
Initialising available-expressions OUT sets to empty for non-entry blocks, which loses expressions around loops.
In constant propagation, treating UNDEF meet c as NAC; it is c.
Forgetting that a definition inside a loop flows back to the loop header and reaches it along with the definition before the loop.
Worked examples
Try each step yourself, then reveal the working.
Example 1: Liveness on a loop
numerical
Find IN and OUT of every block (n is defined before B1).
B1: a = 0, b = 1 → B2. B2: c = a + b, if c > n goto B4 → B3, B4. B3: a = b, b = c, goto B2 → B2. B4: return c.
Step 1: Write use and def for each block.
Try it on paper first.
Example 2: Constant propagation across a join
numerical
B1: x = 2, y = 3 → B2, B3. B2: z = x + y, w = 1 → B4. B3: z = y + 2, w = 2 → B4. B4: u = z * x, v = w + 1. Which of u and v are constants?
Step 1: Propagate through each branch.
Try it on paper first.
Example 3: Available expressions decide global CSE
numerical
B1: t1 = a + b, if t1 > 0 goto B3 → B2, B3. B2: a = 5 → B4. B3: x = a + b → B4. B4: y = a + b. Which recomputations of a + b can be removed?
Step 1: Compute OUT of each block for the expression a + b.
Try it on paper first.
Example 4: Reaching definitions in a loop
numerical
B1: d1 i = 0, d2 s = 0 → B2. B2: d3 s = s + i, d4 i = i + 1, if i < n goto B2 → B2, B3. B3: d5 s = s * 2. Find IN of B2 and B3.
Step 1: Write gen and kill.
Try it on paper first.
Previous year questions
6 official GATE CSE questions on this topic, newest first.
10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.
Mark as mastered
Loading your progress…
My revision notes
Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.