Problemintro
Live variable analysis
Also called liveness analysis, live variables.
For each program point, find the variables whose current value may be read on some path before being overwritten.
Live variable analysis is the backward may problem of dataflow analysis. A variable is live at a program point if some path in the control flow graph from reads before it writes ; otherwise is dead at , and its current value will never be used. The problem asks, for every basic block, for the set of variables live at its start and at its end. The answer tells the register allocator which values must be kept, and it tells dead code elimination which writes are useless. Liveness is a gen-kill problem on the reverse CFG, so the worklist dataflow algorithm computes its meet-over-all-paths solution exactly.
In plain words
A variable is live at a point if the value it holds there might still be read later. If every way forward overwrites the variable first, or never reads it again, the value is dead and its storage can be reused.
To find this, the compiler reads the program backward, from the returns toward the start. A read makes a variable live above it. A write makes it dead above it, since the old value is replaced. At a branch, a variable is live if it is live on at least one of the ways out. Loops carry needs from the top of the loop back to its bottom, so the backward walk repeats until nothing changes.
The answer can call a variable live when no real run reads it, but never the reverse.
Definition
Input. A control flow graph with blocks and edges, in three-address code, and the set of its variables, with .
Output. For every block , the set of variables live at the start of and the set live at its end.
The framework.
| property | value |
|---|---|
| direction | backward: the analysis runs on the reverse CFG |
| may or must | may: live if some path reads the value |
| domain | the subsets of |
| order | |
| meet | |
| and | , |
| boundary value | at the end of each block that ends in ret: local variables die when the procedure returns |
| start value elsewhere | () |
| transfer functions | , monotone and distributive |
| height |
The pages on dataflow use the meet convention: is the start value and the answer is the greatest fixed point, which for a may problem with the order means the smallest sets that satisfy the equations.
Lattice and height. is the set of subsets of . A strict chain adds one variable per step, so .
Transfer functions. Read the block from its end to its start. An instruction x = y op z has (the variables among its operands) and , and it is applied as , so x = x + 1 leaves x live. A terminator that reads a variable, such as br t L1 L2 or ret s, has or and an empty kill set. Composing the steps from the last one to the first gives , the variables that reads before it writes them (upward exposed), and , the variables that writes.
Equations. With the empty union equal to :
These are the forward equations of dataflow analysis on the reverse CFG: the edges are turned around, the ret blocks are the start nodes, and the roles of IN and OUT swap.
Meet over all paths. For a path from the end of to the end of a ret block, is the set of variables that reads before it writes them. The union over all such paths is the set of variables live at the end of along some CFG path.
The fixed point computed. The worklist dataflow algorithm on the reverse CFG starts all sets at and grows them. It stops at the maximal fixed-point solution, the smallest sets that satisfy the equations. Since the transfer functions are distributive, it equals MOP when every block can reach a ret block.
Soundness. If a run is at the end of and later reads the value that holds there, the part of the run between the two points follows a CFG path in which reads before writing it and do not write . Then , and going back along the path, each equation puts into the next OUT and IN sets. So in every solution of the equations, including runs that never return.
Precision. CFG paths that no run takes make the answer larger than the truth. In this program y is live at the start of entry, because the path through skip and then use reads y without a write. No run takes that path, since both branches test the same c. A check for uninitialized variables would warn about y here although no run reads it before writing it.
entry:
br c set skip
set:
y = 1
jump mid
skip:
jump mid
mid:
br c use other
use:
ret y
other:
ret c
Cost. With bit vectors of bits, one set operation costs word operations. The worklist algorithm applies at most transfer functions and unions, so it takes time in the worst case. Round-robin iteration in reverse postorder of the reverse CFG takes at most passes, where is the loop-connectedness depth of the reverse CFG (Kam-Ullman iteration bound), so time.
Variants
- Instruction-level liveness. After the block sets are known, one backward scan of each block gives the live set at every instruction. This is what register allocators use.
- A different boundary. If the procedure writes global variables or memory that the caller reads, those are live after every
ret, and is that set. It is not , and leaving it out would let dead code elimination delete stores that the caller needs. - Strongly live variables. Count a read only if the instruction that reads is itself useful. Then
x = y + 1makesylive only ifxis live after it: . This function distributes over but is not of gen-kill form, and it finds dead chains of assignments that ordinary liveness keeps. - Liveness on SSA form. In static single assignment form each variable has one definition, and the live range of a variable can be found by walking backward from each use to that definition, one variable at a time. A read in a phi-function counts at the end of the matching predecessor, not at the start of the block.
- Live ranges and intervals. Linear scan register allocation turns the live sets into one interval per variable over a linear order of the instructions.
Examples
The running example of dataflow analysis, with the parameters n and x:
entry:
i = 0
s = 0
jump head
head:
t = i < n
br t body done
body:
a = x * 2
u = i & 1
br u odd next
odd:
s = s + a
jump next
next:
i = i + 1
jump head
done:
ret s
| block | IN (live at the start) | OUT (live at the end) | ||
|---|---|---|---|---|
| entry | none | i, s | n, x | i, n, s, x |
| head | i, n | t | i, n, s, x | i, n, s, x |
| body | i, x | a, u | i, n, s, x | a, i, n, s, x |
| odd | a, s | s | a, i, n, s, x | i, n, s, x |
| next | i | i | i, n, s, x | i, n, s, x |
| done | s | none | s | none |
t is in but not in of head, because head writes t before the branch reads it. odd has s in both sets: it reads the old s before it writes the new one.
The worklist trace. The reverse CFG has done as its only start node, and its reverse postorder is done, head, next, odd, body, entry. The table gives each step: the block processed, the set it sends to its predecessors (its IN), and the predecessors whose OUT changed.
| step | block | sends | changed |
|---|---|---|---|
| 1 | done | s | head |
| 2 | head | i, n, s | entry, next |
| 3 | next | i, n, s | body, odd |
| 4 | odd | a, i, n, s | body |
| 5 | body | i, n, s, x | head |
| 6 | entry | n | none |
| 7 | head | i, n, s, x | entry, next |
| 8 | entry | n, x | none |
| 9 | next | i, n, s, x | body, odd |
| 10 | body | i, n, s, x | none |
| 11 | odd | a, i, n, s, x | none |
The first pass misses x in most blocks: x is read only in body, and that read reaches head, next and odd only by going around the loop. The algorithm processes 11 blocks, does 13 unions and changes an OUT set 11 times. No set changes more than 3 times, against the bound .
What the answer is used for. Just before br u odd next, the live variables are a, i, n, s, x and u, six at once, so the loop needs six registers unless one value is spilled. a is dead in next and head, so its register can hold another value there. If body had a further write w = i * i and nothing read w, then w would not be live after the write, and dead code elimination would delete it.
Known algorithms
| algorithm | time | notes |
|---|---|---|
| Worklist dataflow algorithm on the reverse CFG | worst case | the usual choice |
| Round-robin in reverse postorder of the reverse CFG | the Kam-Ullman iteration bound | |
| One backward search per variable | see the reduction below | |
| Backward walk from each use on SSA form | proportional to the total size of the live ranges | no bit vectors |
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| IFDS framework | O(E D^3) worst · average | O(N D^2 + C D^2) extra | deterministiciterative | 1 language |
| Worklist dataflow algorithm | O((n + m)(h + 1)) worst · average | Θ(n) extra | deterministiciterative | 1 language |
Bounds
The output can have entries: in a chain of blocks where block writes and the last block reads all of them, is live in every block after block , which is pairs with . So listing the live sets needs time in the worst case, and the search per variable is within a factor of it.
Reductions
- To graph reachability. Variable is live at the start of if and only if the CFG has a path with and for . One backward depth-first search from the blocks with finds all of them: it marks live at the end of each predecessor it visits, and it continues past that predecessor only if the predecessor does not write . This is graph reachability on the transpose graph of the CFG.
- To Datalog.
live(X, B) :- use(B, X).andlive(X, B) :- edge(B, S), live(X, S), not def(B, X).give ; see Datalog-based program analysis. - From uninitialized variables. A variable that is live at the start of the entry block and is not a parameter may be read before it is written on some path.
Applications
- Register allocation. Two variables interfere when one is live where the other is written; the interference graph of register allocation is built from the live sets at each instruction.
- Dead code elimination. A write to a variable that is dead after it, with no other effect, can be removed. See dead code elimination.
- SSA construction. Pruned SSA form places a phi-function for at a join only if is live there.
- Uninitialized variables. A non-parameter variable that is live at the entry is a possible use before initialization.
- Garbage collection. A compiler records, at each point where a collection can happen, which stack slots and registers hold live references, so the collector does not keep dead objects alive.
References
- Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Pearson Addison-Wesley, 2007, Section 9.2.5 (Live-Variable Analysis, with the use and def sets, the backward equations and the iterative algorithm)
- Keith D. Cooper and Linda Torczon, Engineering a Compiler, third edition, Morgan Kaufmann, 2022, Chapter 9 (Data-Flow Analysis), Section 9.2.2 (Live-Variable Analysis)
- Flemming Nielson, Hanne Riis Nielson and Chris Hankin, Principles of Program Analysis, Springer, 1999, Section 2.1.4 (Live Variables Analysis, stated with a join)
- Gary A. Kildall, A unified approach to global program optimization, Proceedings of the 1st ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages (POPL 1973), 194-206, the whole paper (the iterative algorithm on a meet semilattice, which solves this problem on the reverse graph)
Where this leads
- Array privatization
- Borrow checking
- Copy propagation
- Dead code elimination
- Interference graph
- Linear scan register allocation
- Loop-invariant code motion
- Pruned SSA form
- Register allocation
- Sreedhar out-of-SSA translation
- Linear scan register allocation on SSA form
- Superblock scheduling
- Trace scheduling
- Very busy expressions
- Weiser's slicing algorithm
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag GTY2TM Edit this page History Report an error Markdown