Compiler Design · Data Flow Analysis
Data-Flow Analysis over Basic Blocks: Computing Live-In and Live-Out Sets
Overview
Liveness analysis determines which variables are alive before and after a basic block. It is performed per basic block rather than per line because per-line analysis is very time consuming and a compiler must be fast.
The resulting in-set is used by the compiler, notably in register allocation.
Key concepts
- Basic-block granularity: Liveness analysis is not performed line by line; it is performed on basic blocks.
- in (live-in): The set of all variables that are live before the basic block.
- out (live-out): The set of all variables that are live after the basic block.
- Sets of variables: Both in and out are simply sets of variables; the purpose of the algorithm is to find the in-set and out-set of variables for a basic block.
- Relationship between the sets: The two sets can be the same, can be different, or can have an intersection — anything is possible.
- Why out is computed: The compiler generally uses the in-set, but the out-set is computed because whatever is out of one basic block becomes the in of the next basic block.
- Use in register allocation: If the in-set says variables A and B are alive before the basic block, they might be used inside it, so the compiler tries to save them in registers if possible.
How it works
- Take a basic block (not individual lines).
- Compute in (live-in): the set of variables live before the basic block.
- Compute out (live-out): the set of variables live after the basic block.
- Use the in-set for compiler decisions such as register allocation; the out-set of a block becomes the in-set of the next block.
Live-in and live-out across blocks
Drawing the diagram…
Shows that the out-set of a basic block becomes the in-set of its successor block.
Worked examples
For a basic block with in = {A, B} and out = {C, D}:
| Set | Name | Variables | Meaning |
|---|---|---|---|
| in | live-in | {A, B} | A and B are alive before the basic block |
| out | live-out | {C, D} | C and D are alive after the basic block |
The sets may be identical, may be disjoint, or may overlap (intersection possible).
Comparison
| Set | Also called | Meaning | Use |
|---|---|---|---|
| in | live-in | Variables live before the basic block | Used by the compiler, e.g. in register allocation (save live variables in registers) |
| out | live-out | Variables live after the basic block | Not directly used; computed because out of one block becomes in of the next block |
Revision summary
- Liveness analysis is done per basic block, not per line, because per-line analysis is very time consuming and the compiler must be fast.
- in / live-in = set of variables live before the basic block.
- out / live-out = set of variables live after the basic block.
- Both are sets of variables; they may be equal, different, or overlapping.
- The compiler mainly uses the in-set — for example, to keep live variables in registers during register allocation.
- The out-set is computed because out of a block feeds the in of the next block.
Previous-year exam insights
- Liveness is a backward data-flow analysis: the equations are in(B) = use(B) ∪ (out(B) − def(B)) and out(B) = ∪ in(S) for all successors S of B, where use(B) is the set of variables used in B before being defined and def(B) is the set of variables defined (assigned) in B.
Asked inGATE 2023GATE 2026 - A variable is live at a point if there exists a path from that point to a use of the variable before any redefinition; otherwise it is dead.
Live variable analysis is a may-analysis (union at merge points) and is used for dead-code elimination as well as register allocation.
Asked inGATE 2023GATE 2026 - The live-variable sets are computed by iterating the data-flow equations to a fixed point, initialising all in and out sets to ∅ (for liveness) and repeating until no set changes; the solution is the least fixed point.
- For a basic block, the transfer function can be summarised by the pair (use, def): in = use ∪ (out − def).
Variables that are defined before any use in the block are killed (removed from out); variables used before any definition are generated (added to in).
Asked inGATE 2023GATE 2026 - At the exit of the program (a block with no successors), out = ∅;
at a branch, out is the union of the in-sets of all successor blocks, so a variable live on any outgoing path is live at the exit of the block.
Asked inGATE 2023GATE 2026 - Liveness information is also used for dead-code elimination (an assignment to a variable that is not live-out and not used later is dead) and for constructing interference graphs in register allocation (two variables live at the same point interfere and cannot share a register).