Skip to content
GATEiit

GATE CSE · Compiler Design

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.

Before you start

Concept notes

1. The data-flow framework

A data-flow analysis computes, for every basic block B, a fact at its entry, IN[B], and at its exit, OUT[B]. Two ingredients define it:

  • a transfer function that describes what one block does to the fact, usually fB(X)=genB∪(X−killB);
  • 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 OUT from IN and take IN[B] from the predecessors; backward analyses compute IN from OUT and take OUT[B] 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 v is live at a point p if some path from p uses v before any redefinition of v. Otherwise it is dead at p.

For each block, find:

  • useB: variables used in B before any definition in B (upward-exposed uses);
  • defB: variables assigned in B.

OUT[B]=⋃S∈succ(B)IN[S],IN[B]=useB∪(OUT[B]−defB)

with OUT=∅ 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.

B1a = 0b = 1B2c = a + bif c > n goto B4B3a = bb = cgoto B2B4return cIN = { n }OUT = { a, b, n }IN = { a, b, n }OUT = { b, c, n }IN = { c }OUT = { }IN = { b, c, n }OUT = { a, b, n }
Live variables at the entry and exit of each block of a loop (n is defined before the code)

3. Reaching definitions

A definition d of variable x (an instruction that assigns x) reaches a point p if there is a path from d to p along which x is not redefined.

For each block:

  • genB: definitions in B that survive to its end (the last definition of each variable in B);
  • killB: all other definitions in the program of the variables that B defines.

IN[B]=⋃P∈pred(B)OUT[P],OUT[B]=genB∪(IN[B]−killB)

Start with OUT[B]=genB (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 x 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), x 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 c;
  • NAC (bottom, ⊥): not a constant (different values on different paths, or depends on input).

Meet at a join point, for one variable:

Meet UNDEF c2 NAC
UNDEF UNDEF c2 NAC
c1 c1 c1 if c1=c2, 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 p if on every path from the entry to p it is computed and, after the last computation, neither y nor z is reassigned.

For each block:

  • e_genB: expressions computed in B and not later killed in B;
  • e_killB: expressions that use a variable assigned in B.

IN[B]=⋂P∈pred(B)OUT[P],OUT[B]=e_genB∪(IN[B]−e_killB)

with IN[entry]=∅ and every other OUT 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 B and B 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)
IN[B]=useB∪(OUT[B]−defB),OUT[B]=⋃S∈succ(B)IN[S]
Reaching definitions (forward)
OUT[B]=genB∪(IN[B]−killB),IN[B]=⋃P∈pred(B)OUT[P]
Available expressions (forward, all paths)
OUT[B]=egen∪(IN[B]−ekill),IN[B]=⋂P∈pred(B)OUT[P]
Constant lattice meetNAC meet anything is NAC
c∧c=c,c1∧c2=NAC (c1≠c2),UNDEF∧v=v

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.

  1. 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?

  1. 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?

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

  1. Step 1: Write gen and kill.

    Try it on paper first.

Previous year questions

6 official GATE CSE questions on this topic, newest first.

Practise these 6 PYQs →

Topic test

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.