Sub topic 13
Reaching Definitions
Prerequisites and purpose
Assume that you know basic set operations, three-address code, and control-flow graphs. A definition is an assignment that gives a variable a value. Reaching-definition analysis finds which definitions may reach each program point without being overwritten.
This is a forward, may-analysis. “Forward” means information flows from predecessors to successors. “May” means the result includes every definition that can reach a point along at least one path.
The working model
For each basic block B, compute four sets:
IN[B]: definitions that may reach the block's entry.OUT[B]: definitions that may reach the block's exit.GEN[B]: definitions made inBthat reach its exit.KILL[B]: other definitions of the same variables, whichBmakes impossible.
The transfer equations are:
| Set | Equation |
|---|---|
| Entry | `IN[B] = union of OUT[P]` for every predecessor `P` |
| Exit | `OUT[B] = GEN[B] union (IN[B] - KILL[B])` |
For the entry block, use IN[entry] = empty set. If a block has multiple predecessors, use union because a definition reaching through any path is relevant.
Worked control-flow graph
Consider these definitions:
B1:d1: a = 1,d2: b = 2B2:d3: a = b + 1; branch toB3orB4B3:d4: b = a + 2; jump back toB2B4:return a + b
For B2, d3 kills d1, because both define a. For B3, d4 kills d2, because both define b.
After iterating the equations to a fixed point:
| Block | `IN` | `OUT` |
|---|---|---|
| `B1` | `{}` | `{d1, d2}` |
| `B2` | `{d1, d2, d3, d4}` | `{d2, d3, d4}` |
| `B3` | `{d2, d3, d4}` | `{d3, d4}` |
| `B4` | `{d2, d3, d4}` | `{d2, d3, d4}` |
At B4, d3 is the only reaching definition of a, while either d2 or d4 may define b. The analysis keeps both possibilities because the loop may execute zero or more times.
Common errors
- Using intersection at joins. Reaching definitions uses union.
- Putting every assignment in
GEN. Only the last definition of each variable in a block can reach its exit. - Forgetting that
KILLincludes definitions in other blocks. - Stopping after one pass through a loop. Recompute until no
INorOUTset changes.
Exercise
A block contains d1: x = 4 followed by d2: x = 7. Which definition belongs to GEN for this block, and which earlier definition does it kill?
Show answer
GEN contains d2. The block kills d1 because d2 overwrites x before the block exits.