Sub topic 17
Reaching definitions
This chapter assumes you know basic blocks, control flow graphs (CFGs) and the set operations union and difference. The next chapters build on this analysis: live variables, available expressions and the general iterative solver.
What the analysis answers
A definition is a statement that may assign a value to a variable, such as d1: a = 1. A definition d reaches a point p if some path from d to p exists along which the variable is not redefined. Reaching definitions asks, for each block, which definitions might still be the current value of their variable on entry and on exit.
Compilers use the answer for constant propagation, for detecting uses of uninitialized variables, and for building use-def chains. Constant propagation can replace a use of x with a constant only if every definition that reaches the use assigns that same constant.
This is a may-analysis. A definition is in the set if it reaches along at least one path, so the meet operator is union.
The equations
OUT[B] = gen[B] U (IN[B] - kill[B])
IN[B] = union of OUT[P] for every predecessor P of B
IN[entry] = empty setgen[B]holds the definitions in B that reach the end of B. If B defines the same variable twice, only the last definition is ingen.kill[B]holds every other definition of any variable that B defines. "Every other" means across the whole program, not only in B.- The analysis is forward. Information flows from predecessors to successors.
A worked example
The CFG below has three blocks and four definitions. Block B2 loops to itself.
The variable a is defined by d1 and d3. The variable b is defined by d2 and d4. This gives the following gen and kill sets.
| Block | gen | kill |
|---|---|---|
| B1 | d1, d2 | d3, d4 |
| B2 | d3 | d1 |
| B3 | d4 | d2 |
Start with every OUT set empty. This is the correct start for a union analysis, because sets can only grow toward the fixed point. Process the blocks in order B1, B2, B3.
| Block | IN (round 1) | OUT (round 1) | IN (round 2) | OUT (round 2) |
|---|---|---|---|---|
| B1 | empty | d1, d2 | empty | d1, d2 |
| B2 | d1, d2 | d2, d3 | d1, d2, d3 | d2, d3 |
| B3 | d2, d3 | d3, d4 | d2, d3 | d3, d4 |
In round 1, B2 sees only OUT[B1], because OUT[B2] is still empty. In round 2 the back edge feeds d3 into IN[B2]. No OUT set changes in round 2, so the algorithm stops.
Two facts follow. Definition d1 does not reach B3, because B2 always runs first and kills it. Definition d3 reaches the entry of B2 itself, through the loop.
Solving it in code
This function implements the equations directly.
def reaching_definitions(blocks, pred, gen, kill):
inn = {b: set() for b in blocks}
out = {b: set() for b in blocks}
changed = True
while changed:
changed = False
for b in blocks:
inn[b] = set().union(*(out[p] for p in pred[b]))
new_out = gen[b] | (inn[b] - kill[b])
if new_out != out[b]:
out[b] = new_out
changed = True
return inn, out
blocks = ["B1", "B2", "B3"]
pred = {"B1": [], "B2": ["B1", "B2"], "B3": ["B2"]}
gen = {"B1": {"d1", "d2"}, "B2": {"d3"}, "B3": {"d4"}}
kill = {"B1": {"d3", "d4"}, "B2": {"d1"}, "B3": {"d2"}}
inn, out = reaching_definitions(blocks, pred, gen, kill)
for b in blocks:
print(b, sorted(inn[b]), sorted(out[b]))The output is:
B1 [] ['d1', 'd2']
B2 ['d1', 'd2', 'd3'] ['d2', 'd3']
B3 ['d2', 'd3'] ['d3', 'd4']The loop terminates because each OUT set can only grow and the set of definitions is finite. Visiting blocks in reverse postorder usually needs fewer rounds for a forward problem.
Common errors
- Killing only inside the block. A definition elsewhere in the program is killed by any block that redefines its variable, even if that definition is in a different block.
- Putting every definition in gen. If a block has
x = 1followed byx = 2, only the second is ingen. - Using intersection. Intersection belongs to available expressions. Reaching definitions uses union.
- Starting OUT at the universal set. This gives wrong answers for union analyses. Start empty.
- Forgetting the entry.
IN[entry]is empty. To detect uninitialized variables, add a pseudo-definition of each variable at the entry. If that pseudo-definition reaches a use, the variable may be uninitialized. - Ignoring aliasing. Pointer writes, array element writes and procedure calls may define variables indirectly. A safe analysis treats them as possible definitions that kill nothing.
Using the result
For a use of x in block B, the relevant definitions are those of x in IN[B]. If a definition of x earlier in B precedes the use, that definition alone applies. If every reaching definition assigns the same constant, constant propagation is safe. If a use has no reaching definition of its variable, the variable is certainly uninitialized there.
Exercise
For the example CFG, what is IN[B3], and why is d1 absent from it?
Show answer
IN[B3] = OUT[B2] = {d2, d3}. B2 is the only predecessor of B3. B2 redefines a, so d1 is in kill[B2] and cannot pass through B2. Every path from B1 to B3 goes through B2.
Exercise
Add an edge from B1 directly to B3. Compute IN[B3] and OUT[B3].
Show answer
IN[B3] = OUT[B1] union OUT[B2] = {d1, d2} union {d2, d3} = {d1, d2, d3}. OUT[B3] = {d4} union ({d1, d2, d3} - {d2}) = {d1, d3, d4}. Now d1 reaches B3 along the new path that skips B2.