Compiler Design · Liveness Analysis and Register Allocation
Formal Definition of Liveness of a Variable
Overview
Liveness analysis determines, for every point in a program, whether the value a variable currently holds will still be needed later. It matters because the registers holding live variables must be preserved, while registers holding dead variables can be reused.
Key concepts
- Live variable at statement N: A variable V is live at statement N if there exists a path in the control flow graph (CFG) from N to some statement M such that V is used at M, and for every statement K on the path from N to M, V is not defined (no redefinition of V between N and M).
- Used at the statement itself: A variable is also live at a statement if it is used at that statement itself.
- Dead variable: A variable is dead at a statement if neither condition holds — no reachable use exists before a redefinition.
- Use vs. definition: A variable is used if it appears on the right-hand side of a statement; it is defined if it appears on the left-hand side.
- Definition kills the variable: Whenever a variable is defined (LHS) and not used in the same statement, it is dead there, because its earlier value is no longer useful from that point.
This is the one exception to remember in the whole analysis. - Live range (life): All points from which a use is reachable without an intervening definition together form the live range of the variable.
- Analyse all variables at every point: Liveness must be computed for all variables at every point, not only the variables appearing in the current line.
A variable not defined at this point may be defined elsewhere and its value used elsewhere, so the registers allotted to it must be kept intact.
How it works
- Standing at line N, the variable is live if you can reach another line where it is used, with no definition of that variable on the way.
- Three conditions must all hold for liveness at a point: a use exists somewhere in the program, that use is reachable from this statement, and no definition of the variable lies on the path.
- If the use exists in the program but is unreachable from this point (e.g., control is diverted by an if statement), the variable is dead here.
- If a definition
D = ...lies on the path before the use, the new value is used at the use site and the old value is not, so the variable is dead at the earlier point and at the definition itself; it becomes live again at points after the definition from which the use is reachable without another definition. - Statement-by-statement liveness over the whole program works for small examples but is not what compilers do (it takes too much time); compilers work at the level of basic blocks.
Formulas
-
V is live at N ⇔ ∃ path N → M in the CFG such that V ∈ USE(M) and ∀ K on the path from N to M (excluding M): V ∉ DEF(K)
-
USE(M) = set of variables used (read, right-hand side) at statement M
-
DEF(K) = set of variables defined (written, left-hand side) at statement K
-
Special cases:
-
If V ∈ USE(N), then V is live at N.
-
If V ∈ DEF(N) and V ∉ USE(N), then V is dead at N (the definition kills the old value).
Worked examples
Example 1 — straight-line code. Statements execute in sequence; the program's variables are A, B, C, D, E. Statement 1 is A = B + C; a later statement uses D, e.g. ... D + 1 ....
- B is on the RHS → used → B is live at statement 1.
- C is on the RHS → used → C is live at statement 1.
- A is on the LHS → defined, not used → A is dead (the old value of A is not useful here).
- D is not used in statement 1, but there is a path from statement 1 to the statement using D with no definition of D in between → D is live at statement 1.
- E is not used anywhere from here onward (or is redefined by
E = ...before any use) → E is dead.
Example 2 — use not reachable. Control flow is diverted (e.g., by an if statement) so that the point where D is used cannot be reached from the current point. D is used somewhere in the program, but from this point the use is unreachable → D is dead at this point.
Example 3 — definition kills the variable. A statement D = ... lies on the path between the current point and the use of D.
- The new value of D is the one used at the use site.
- The old value of D available at the earlier point is not used there.
- Therefore D is dead at the earlier point (and at the definition itself).
- After the definition, at points from which the use is reachable without another definition, D is live again.
Comparison
| Situation at a statement | Status of the variable | Reason |
|---|---|---|
| Variable on RHS (used) | Live | Used at that statement |
| Variable on LHS (defined), not used | Dead | Definition kills the old value; old value not useful from here |
| Not in the statement, but a use is reachable with no intervening definition | Live | Path to a use exists with no definition on the way |
| Not in the statement, use exists in program but is unreachable from here | Dead | No path from this statement to the use |
| Not in the statement, use reachable only through a redefinition | Dead | Definition on the path kills the old value |
Revision summary
- Use = right-hand side; definition = left-hand side.
- A definition kills the variable: wherever a variable is defined and not used, it is dead at that statement.
- Liveness needs all three together: a use exists in the program, the use is reachable from this statement, and no definition lies on the path.
- Do not restrict liveness analysis to the variables in the current line — compute it for all variables at every point.
- A variable can be dead at a definition point and live again at points after it from which the use is reachable without another definition.
- Statement-by-statement analysis over the whole program is not what compilers do; compilers work at the level of basic blocks.
Previous-year exam insights
- Data-flow equations for liveness: IN(S) = USE(S) ∪ (OUT(S) − DEF(S)) and OUT(S) = union of IN of all successors of S in the CFG;
for a basic block, OUT(B) = union of IN(successors), IN(B) = USE(B) ∪ (OUT(B) − DEF(B)).
Asked inGATE 2021GATE 2015 - Liveness is a backward (backward-flow) data-flow analysis: information flows from a statement's successors to the statement, and OUT of a statement equals the union of IN of its successors (intersection is used for 'available expressions', not liveness).
Asked inGATE 2021 - A variable is live at a point if there is a path from that point to a use of the variable with no intervening redefinition.
A statement that both uses and defines the variable (e.g. x = x + 1) counts as a use, because the right-hand side is read before the assignment takes effect;
so the variable is live just before it.
A path starting at a statement that redefines the variable without using it makes the variable dead before that statement.
Asked inGATE 2015 - Interference for register allocation: two variables interfere (cannot share a register) if one is live at a point where the other is defined; variables live simultaneously at any program point need distinct registers.
- USE and DEF of a basic block are computed by scanning statements: DEF(B) is the set of variables defined before any use in B, and USE(B) is the set of variables used in B before any definition in B (upward-exposed uses).
Asked inGATE 2021 - Liveness sets are computed iteratively to a fixed point (initialise IN and OUT to empty and repeat until no change); the analysis terminates because the sets only grow and are bounded by the number of variables.