Sub topic 18
Live variable analysis
This chapter assumes you can split three-address code into basic blocks and draw a control flow graph (CFG). It also assumes you know the gen/kill, IN/OUT vocabulary from the data flow framework chapter, and basic set operations.
What the analysis answers
A variable x is live at a point p if some path from p reaches a use of x without first redefining x. If no such path exists, x is dead at p.
Compilers use this for two jobs:
- Dead code elimination. An assignment
x = ewhosexis dead right after it can be removed, providedehas no side effects. - Register allocation. Two variables that are live at the same time cannot share a register. Dead values free their registers.
The analysis goes backward because liveness depends on the future: whether a later statement reads the value.
Setting up the sets
For each basic block B, compute two local sets:
use[B]holds the variables read in B before any assignment to them in B.def[B]holds the variables assigned in B.
A variable can be in both sets. In s = s + a, the right side reads s first, so s is in use, and the assignment also puts it in def.
The equations are a backward may-analysis. The meet operator is union, because a variable is live if it is live along any path.
OUT[B] = union of IN[S] for every successor S of B
IN[B] = use[B] U (OUT[B] - def[B])
OUT[exit block] = empty set (or the variables the caller needs)In the framework's terms, use plays the role of gen and def plays the role of kill. The initial value for every IN is the empty set. Starting from empty and only adding is what makes the iteration converge to the least solution.
Computing use and def for a block
Scan the block from top to bottom, keeping both sets.
- For each statement, add every operand not already in
deftouse. - Then add the assigned variable to
def.
The order matters. Process the operands before the target, or x = x + 1 would wrongly leave x out of use.
Worked example
This is the program from the Python code later in the chapter.
- B1:
a = 1; s = 0 - B2:
s = s + a, then it loops back to B2 or exits to B3 - B3:
return s
The local sets are:
| Block | use | def |
|---|---|---|
| B1 | empty | a, s |
| B2 | s, a | s |
| B3 | s | empty |
Visit the blocks in reverse order (B3, B2, B1), which suits a backward problem.
| Pass | Block | OUT | IN |
|---|---|---|---|
| 1 | B3 | empty | s |
| 1 | B2 | s | s, a |
| 1 | B1 | s, a | empty |
| 2 | B2 | s, a | s, a |
| 3 | no change |
In pass 2, B2's OUT grows because its own IN flows back along the self-loop. Pass 3 changes nothing, so the iteration has reached a fixed point.
Read the result as follows:
ais live on entry to B2 and stays live around the loop, since every iteration reads it.- Nothing is live on entry to B1, so the program uses no uninitialized variable.
- Both
aandsare live out of B1, so neither initialization is dead.
The iterative solver
def live_variables(blocks, succ, use, defs):
live_in = {b: set() for b in blocks}
live_out = {b: set() for b in blocks}
changed = True
while changed:
changed = False
for b in reversed(blocks):
out = set()
for s in succ[b]:
out |= live_in[s]
inn = use[b] | (out - defs[b])
if out != live_out[b] or inn != live_in[b]:
live_out[b], live_in[b] = out, inn
changed = True
return live_in, live_out
# B1: a = 1; s = 0
# B2: s = s + a (loops back to B2, or exits to B3)
# B3: return s
blocks = ["B1", "B2", "B3"]
succ = {"B1": ["B2"], "B2": ["B2", "B3"], "B3": []}
use = {"B1": set(), "B2": {"s", "a"}, "B3": {"s"}}
defs = {"B1": {"a", "s"}, "B2": {"s"}, "B3": set()}
live_in, live_out = live_variables(blocks, succ, use, defs)
# live_in["B2"] == {"s", "a"} and live_in["B1"] == set()Visiting blocks in reverse order is only an optimization. Any order reaches the same fixed point, but the reverse order usually needs fewer passes on a backward problem.
Liveness inside a block
Block-level results tell you what is live at block boundaries. For a point inside a block, start from OUT[B] and walk the statements backward. For a statement x = y + z, apply these steps to the live set:
- Remove
x. - Add
yandz.
The order is important. Removing the target first and adding the operands second gives the right answer for x = x + 1. Before that statement, x is live.
Common errors
- Wrong direction. Computing IN from predecessors is a forward analysis, as in reaching definitions. Liveness uses successors.
- Wrong meet. Using intersection gives available-expressions behavior. Liveness is a may-analysis, so use union.
- Order inside a statement. Adding the target to
defbefore reading the operands loses uses such asx = x + 1. - Treating
defas "dead". An assignment in a block does not make a variable dead before the block. Only the equationuse[B] U (OUT[B] - def[B])decides that. - Ignoring loops. A variable read only on a later iteration is still live around the back edge.
Exercise
A block contains t = a + b; u = t * c; t = t + u. Compute use and def for it. Then find IN if OUT = {t}.
Show answer
Scanning top to bottom, a, b and c are read before being defined, so use = {a, b, c}. The variable t is read in the second and third statements only after it was assigned, so it is not in use. def = {t, u}. IN = use U (OUT - def) = {a, b, c} U ({t} - {t, u}) = {a, b, c}.
Exercise
In the example program, suppose B3 were return 0 instead of return s. What would be live on entry to B2 once the iteration finishes, and what would that say about the loop?
Show answer
use[B3] becomes empty. B2 still reads s and a, so use[B2] = {s, a} and IN[B2] = {s, a} U (OUT[B2] - {s}) = {s, a}. Both stay live around the loop, but s never reaches a use outside it. The loop computes a value nothing observes, so a compiler may treat the stores to s as dead only if it can show that the loop itself has no effect.