Sub topic 3
Reaching definitions
This chapter assumes you know basic control flow graphs, set union and difference, and how a compiler splits code into basic blocks. It covers reaching definitions first, then the other analyses in the plan, and ends with GATE-style strategies.
Reaching definitions
A definition is an assignment such as x = 1. A definition reaches a point if some path from the definition to that point has no other assignment to the same variable.
For each block B, the analysis uses these sets:
- gen[B]: definitions created in B that survive to its end.
- kill[B]: definitions in other blocks that B overwrites.
- IN[B] = union of OUT[P] over every predecessor P.
- OUT[B] = gen[B] ∪ (IN[B] − kill[B]).
def reaching_definitions(blocks, preds, gen, kill):
ins = {b: set() for b in blocks}
outs = {b: set() for b in blocks}
changed = True
while changed:
changed = False
for b in blocks:
new_in = set()
for p in preds[b]:
new_in |= outs[p]
new_out = gen[b] | (new_in - kill[b])
if new_in != ins[b] or new_out != outs[b]:
ins[b], outs[b] = new_in, new_out
changed = True
return ins, outs
blocks = ["B1", "B2", "B3"]
preds = {"B1": [], "B2": ["B1", "B2"], "B3": ["B2"]}
gen = {"B1": {"d1", "d2"}, "B2": {"d3", "d4"}, "B3": set()}
kill = {"B1": {"d3", "d4"}, "B2": {"d1", "d2"}, "B3": set()}
ins, outs = reaching_definitions(blocks, preds, gen, kill)
print(sorted(ins["B3"])) # ['d3', 'd4']Live variable analysis
A variable is live at a point if its current value may be read later before being overwritten. This is a backward analysis: OUT[B] is the union of IN[S] over successors S, and IN[B] = use[B] ∪ (OUT[B] − def[B]). Dead code elimination removes assignments whose target is not live afterward.
Available expressions
An expression is available at a point if it has been computed on every path to that point and its operands have not changed since. This is a forward analysis with intersection over predecessors, and IN[entry] is the empty set. Common subexpression elimination reuses an available value instead of recomputing it.
Iterating to a fixed point
The solver repeats the equations until no IN or OUT set changes. The sets only grow (for reaching definitions) or only shrink (for available expressions), so the loop always stops. The result does not depend on the order in which blocks are visited, though visiting in reverse postorder usually converges faster.
Optimizations that use data flow results
- Constant propagation replaces a use of x with a constant when only one definition of x reaches that use.
- Dead code elimination deletes an assignment to a variable that is not live afterward.
- Common subexpression elimination reuses an expression listed as available.
- Loop-invariant code motion moves a computation out of a loop when all its operands are defined outside the loop.
Static single assignment form
Static single assignment (SSA) is a form where each variable is assigned exactly once. At join points, a phi function selects the correct version of a variable. SSA makes reaching definitions explicit in the names themselves.
GATE problem patterns and solving strategies
GATE questions usually give a small CFG and ask for the IN set of a block or the number of iterations. Write the gen and kill sets first, then fill a table of IN and OUT by iterating in block order. Check that the final table is stable.
Exercise
Exercise
In the example above, which definition of y reaches print y in B3?
Show answer
d4 (y = y + x). The definition d2 is killed inside B2 by d4 on every path through the loop, so it never reaches B3.