Algorithm Search⌘K

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 xx is live at a program point pp if some path in the control flow graph from pp reads xx before it writes xx; otherwise xx is dead at pp, 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 nn blocks and mm edges, in three-address code, and the set VV of its variables, with ∣V∣=v|V| = v.

Output. For every block BB, the set IN[B]⊆V\mathrm{IN}[B] \subseteq V of variables live at the start of BB and the set OUT[B]\mathrm{OUT}[B] 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 LL the subsets of VV
order ⊑\sqsubseteq ⊇\supseteq
meet ⊓\sqcap ∪\cup
⊤\top and ⊥\bot ⊤=∅\top = \emptyset, ⊥=V\bot = V
boundary value ι\iota ∅\emptyset at the end of each block that ends in ret: local variables die when the procedure returns
start value elsewhere ∅\emptyset (⊤\top)
transfer functions fB(X)=useB∪(X−defB)f_B(X) = use_B \cup (X - def_B), monotone and distributive
height hh vv

The pages on dataflow use the meet convention: ⊤\top is the start value and the answer is the greatest fixed point, which for a may problem with the order ⊇\supseteq means the smallest sets that satisfy the equations.

Lattice and height. LL is the set of subsets of VV. A strict chain adds one variable per step, so h=vh = v.

Transfer functions. Read the block from its end to its start. An instruction x = y op z has gen={y,z}gen = \{y, z\} (the variables among its operands) and kill={x}kill = \{x\}, and it is applied as X↦gen∪(X−kill)X \mapsto gen \cup (X - kill), so x = x + 1 leaves x live. A terminator that reads a variable, such as br t L1 L2 or ret s, has gen={t}gen = \{t\} or {s}\{s\} and an empty kill set. Composing the steps from the last one to the first gives useBuse_B, the variables that BB reads before it writes them (upward exposed), and defBdef_B, the variables that BB writes.

Equations. With the empty union equal to ∅\emptyset:

OUT[B]=(ι if B ends in ret)∪⋃S∈succ(B)IN[S],IN[B]=useB∪(OUT[B]−defB).\mathrm{OUT}[B] = \Big(\iota \text{ if } B \text{ ends in } \texttt{ret}\Big) \cup \bigcup_{S \in \mathrm{succ}(B)} \mathrm{IN}[S], \qquad \mathrm{IN}[B] = use_B \cup (\mathrm{OUT}[B] - def_B).

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 π\pi from the end of BB to the end of a ret block, fπ(∅)f_\pi(\emptyset) is the set of variables that π\pi reads before it writes them. The union over all such paths is the set of variables live at the end of BB along some CFG path.

The fixed point computed. The worklist dataflow algorithm on the reverse CFG starts all sets at ∅\emptyset 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 BB and later reads the value that xx holds there, the part of the run between the two points follows a CFG path B,B1,…,BkB, B_1, \ldots, B_k in which BkB_k reads xx before writing it and B1,…,Bk−1B_1, \ldots, B_{k-1} do not write xx. Then x∈useBk⊆IN[Bk]x \in use_{B_k} \subseteq \mathrm{IN}[B_k], and going back along the path, each equation puts xx into the next OUT and IN sets. So x∈OUT[B]x \in \mathrm{OUT}[B] 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 vv bits, one set operation costs O(v/w)O(v / w) word operations. The worklist algorithm applies at most n(v+1)n(v + 1) transfer functions and m(v+1)m(v + 1) unions, so it takes O((n+m)v2/w)O((n + m) v^2 / w) time in the worst case. Round-robin iteration in reverse postorder of the reverse CFG takes at most d+2d + 2 passes, where dd is the loop-connectedness depth of the reverse CFG (Kam-Ullman iteration bound), so O((d+2)(n+m)v/w)O((d + 2)(n + m) v / w) 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 ι\iota is that set. It is not ⊤=∅\top = \emptyset, 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 + 1 makes y live only if x is live after it: f(X)=(X−{x})∪({y} if x∈X)f(X) = (X - \{x\}) \cup (\{y\} \text{ if } x \in X). This function distributes over ∪\cup 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 useBuse_B defBdef_B 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 defdef but not in useuse 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 h=7h = 7.

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 O((n+m)v2/w)O((n + m) v^2 / w) worst case the usual choice
Round-robin in reverse postorder of the reverse CFG O((d+2)(n+m)v/w)O((d + 2)(n + m) v / w) the Kam-Ullman iteration bound
One backward search per variable O(v(n+m))O(v (n + m)) see the reduction below
Backward walk from each use on SSA form proportional to the total size of the live ranges no bit vectors
AlgorithmTimeSpacePropertiesCode
IFDS frameworkO(E D^3) worst · averageO(N D^2 + C D^2) extradeterministiciterative1 language
Worklist dataflow algorithmO((n + m)(h + 1)) worst · averageΘ(n) extradeterministiciterative1 language

Bounds

The output can have Θ(nv)\Theta(n v) entries: in a chain of nn blocks where block ii writes xix_i and the last block reads all of them, xix_i is live in every block after block ii, which is n(n−1)/2n(n - 1) / 2 pairs with v=nv = n. So listing the live sets needs Ω(nv)\Omega(n v) time in the worst case, and the search per variable is within a factor (n+m)/n(n + m) / n of it.

Reductions

  • To graph reachability. Variable xx is live at the start of BB if and only if the CFG has a path B=B0,B1,…,BkB = B_0, B_1, \ldots, B_k with x∈useBkx \in use_{B_k} and x∉defBix \notin def_{B_i} for i<ki < k. One backward depth-first search from the blocks with x∈usex \in use finds all of them: it marks xx live at the end of each predecessor it visits, and it continues past that predecessor only if the predecessor does not write xx. This is graph reachability on the transpose graph of the CFG.
  • To Datalog. live(X, B) :- use(B, X). and live(X, B) :- edge(B, S), live(X, S), not def(B, X). give IN\mathrm{IN}; 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 xx at a join only if xx 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

  1. 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)
  2. 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)
  3. 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)
  4. 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

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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a contract.yaml