Compiler Design · Liveness Analysis and Register Allocation
Purpose of Liveness Analysis and Register Spilling
Overview
Liveness analysis determines, at each program point, which variables may still be needed.
It matters because the number of registers is small compared with the number of variables, so the compiler must decide which variables to keep in registers and which to spill to memory.
Liveness analysis also supports dead code elimination.
Key concepts
- Liveness analysis: performed for two purposes — register allocation and dead code elimination.
- Live variable: a variable is live at a point in the program if its value may be required while executing the current statement or somewhere in the future of the program.
- Dead variable: a variable that is not live; its value is not required anywhere later in the program.
- Spilling: taking a variable that currently occupies a register and writing it back to memory (main memory / RAM) so that the register can be freed and given to another variable that is needed now.
- Register pressure: the number of registers in any computer is limited, while the number of variables in a program can be any number; registers are very small in number compared with variables, so all variables cannot be kept in registers.
- On-demand need: the variable used by the statement being executed must get a register right now, even if every register currently holds a live variable.
How it works
- Consider a program of many statements (not necessarily basic blocks; basic block analysis is done separately later). Suppose the statement about to execute is
x = y + 1. - Before this statement executes, the value of
ymust be fetched from memory into a register. In general, when a program executes, variables are kept in registers both for reading and for writing. - Assume four registers R1, R2, R3, R4, allotted to variables A, B, C, D respectively by the time execution reaches
x = y + 1. Variableyis in memory and must be brought into a register, so one register must be freed. - Use liveness analysis to decide which register to free: the choice determines how many reads and writes happen in the future.
- If some variable in a register is dead, overwrite that register — no memory save is needed.
- If all variables in registers are live, pick one register by any algorithm ("randomly"), save that variable's value to memory, and load
yinto the register. This is spilling. - Liveness analysis is done at every statement: at each point, find which variables are live there and decide which register to allot to the variable needed at that point.
A spilled live variable can later be reloaded into a register that has become dead.
Formulas
- No formulas are given in this section.
Worked examples
Example 1: A dead variable frees a register (no spill to memory needed)
Statement being executed: x = y + 1
Register state before the statement:
| Register | Variable |
|---|---|
| R1 | A |
| R2 | B |
| R3 | C |
| R4 | D |
Liveness result at this point: A, B, C are live; D is dead.
- D is dead, so D's value is not needed anywhere in the future.
- D is spilled — since it is not required at all later, its value need not even be written to memory; the register is simply overwritten.
- R4 is allotted to
y. - Generated code:
- move
yto R4 - add 1 to
y - store the result to
x
- move
This is the lucky case: one variable (D) was found dead, so its register could be reused.
Example 2: All variables live — spilling with a save to memory
Same statement x = y + 1, same register state (R1 = A, R2 = B, R3 = C, R4 = D), but now A, B, C, D are all live and y is also live.
ymust get a register now because it is the current need (on demand).- Pick one register by some algorithm ("randomly" — any picking algorithm).
- Suppose the register holding A is chosen. Save A's value to memory (main memory / RAM), then store
y's value in that register. A is thrown out andyis brought in — this is spilling. - The current statement executes with
yin the register. A is definitely live, meaning it will definitely be needed somewhere in the future. - At a later statement, liveness analysis is done again. Suppose the registers then hold
y, B, C, D andy, B, C are found to be live.
A is needed again and D is dead, so D's register is overwritten with A (A's spilled value is reloaded into D's register).
Comparison
Situation at the statement needing y | Action on the chosen register | Cost |
|---|---|---|
| A variable in some register is dead (e.g., D) | Overwrite the dead variable's register; no need to save its value to memory | Cheapest — no memory write |
| All variables in registers are live | Spill: save the chosen variable's value to memory, then load y into that register | Extra memory write (and later reload) |
Revision summary
- Liveness analysis serves two purposes: register allocation and dead code elimination.
- A variable is live at a point if it may be used in the current statement or anywhere in the future; otherwise it is dead.
- Live variables should be preserved in registers as long as possible.
- Spilling = writing a variable's value back to memory to free its register.
- If the variable chosen for spilling is dead, no memory save is needed — just overwrite the register.
- If all variables are live, some register must still be given to the currently needed variable; pick one by any algorithm and spill it to memory.
- Liveness analysis is performed at every statement to decide register allotment.
- A spilled live variable can later be reloaded into a register that has become dead (e.g., A reloaded into D's register).
Previous-year exam insights
- A variable is live at a program point if there is a path from that point to a use of the variable before any redefinition (reassignment) of it;
liveness is a may-analysis computed by backward dataflow: LiveOut(B) = union of LiveIn of successors, LiveIn(B) = Use(B) ∪ (LiveOut(B) − Def(B)).
Asked inGATE 2025GATE 2010 - Interference graph: two variables interfere (cannot share a register) if one is live at a definition of the other;
graph colouring with k colours (= number of registers) gives an allocation, and a node that cannot be coloured must be spilled.
Asked inGATE 2010ISRO 2020 - The minimum number of registers needed to evaluate an expression/basic block without spilling equals the maximum number of simultaneously live values (the maximum register pressure over the block), computed by simulating the liveness of temporaries statement by statement.
Asked inGATE 2010ISRO 2020 - Spilling a variable inserts a store after each definition and a load before each use of that variable, so spilling increases the number of memory accesses (and instruction count) even though it reduces register pressure; a spilled variable may be reloaded once a register becomes free.
Asked inISRO 2020 - Dead code elimination removes assignments to variables that are dead (never used afterwards); such an assignment is useless and can be deleted, which is the second use of liveness information besides register allocation.
Asked inGATE 2025 - Register allocation assigns registers to variables/temporaries, while register assignment maps each variable to a specific register; both rely on liveness, and neither changes the computed result of the program, only the number of memory accesses.
Asked inGATE 2025