Sub topic 20
Iterating to a fixed point
Prerequisites
This chapter assumes you can build a control flow graph from three-address code. It also assumes you know the gen, kill, IN and OUT sets for reaching definitions, live variables and available expressions. You should be comfortable with union, intersection and set difference.
Why equations need iteration
In a graph without loops, you could solve the data flow equations in one sweep. Process the blocks in order, and every IN set depends only on blocks you have already finished. A loop breaks this. The IN set of the loop header depends on the OUT set of the loop body, and that depends on the header's IN set.
A circular system has no direct solution order. The standard answer is to guess a starting value for every OUT set, apply the equations repeatedly, and stop when a full pass changes nothing. That stable assignment is a fixed point of the equations.
The round-robin algorithm
For a forward analysis, the algorithm is:
- Set OUT[entry] to the boundary value. Set every other OUT[B] to the initial value for the analysis.
- For each block B in a fixed order, compute IN[B] by applying the meet operator to the OUT sets of B's predecessors. Then compute OUT[B] = gen[B] ∪ (IN[B] − kill[B]).
- If any OUT set changed during the pass, go back to step 2. Otherwise stop.
A backward analysis runs the same loop with the roles swapped. It computes OUT[B] from the IN sets of successors, then IN[B] = use[B] ∪ (OUT[B] − def[B]). It starts from the exit block.
Why it terminates
The transfer function f(X) = gen ∪ (X − kill) is monotone. If X grows, f(X) never shrinks. Take a may-analysis such as reaching definitions, which starts from empty sets. Each OUT set can only grow. Each set is bounded by the finite number of definitions in the program. A pass that reports a change adds at least one element somewhere, so the number of changing passes is finite.
A must-analysis such as available expressions works in the opposite direction. It starts from the full set and sets only shrink until they stop.
Worked example: reaching definitions
Consider this graph. Block B1 holds d1: i = 1 and d2: j = 0. Block B2 is a loop that branches back to itself or on to B3. It holds d3: j = j + i and d4: i = i + 1. Block B3 holds d5: k = j.
| Block | gen | kill |
|---|---|---|
| B1 | d1, d2 | d3, d4 |
| B2 | d3, d4 | d1, d2 |
| B3 | d5 | none |
Pass 1 uses the order B1, B2, B3 and starts with every OUT set empty:
- OUT[B1] = {d1, d2}.
- IN[B2] = OUT[B1] ∪ OUT[B2] = {d1, d2}, so OUT[B2] = {d3, d4}.
- IN[B3] = {d3, d4}, so OUT[B3] = {d3, d4, d5}.
Pass 2 recomputes IN[B2] = {d1, d2} ∪ {d3, d4} = {d1, d2, d3, d4}. Then OUT[B2] = {d3, d4} ∪ ∅ = {d3, d4}, which is unchanged. No OUT set changed, so the solver stops. Definitions d1 and d2 still reach the top of B2 on the first trip through the loop, and d3 and d4 reach it on later trips.
A generic solver in Python
This solver handles any forward analysis. You pass in the meet operator and the initial value. The second part shows why that initial value matters.
def solve_forward(blocks, pred, gen, kill, meet, boundary, init):
out = {b: set(init) for b in blocks}
inn = {b: set() for b in blocks}
passes = 0
changed = True
while changed:
changed = False
passes += 1
for b in blocks:
if not pred[b]:
inn[b] = set(boundary)
else:
inn[b] = meet([out[p] for p in pred[b]])
new = gen[b] | (inn[b] - kill[b])
if new != out[b]:
out[b] = new
changed = True
return inn, out, passes
def union(sets):
return set().union(*sets)
def intersect(sets):
return set.intersection(*sets)
blocks = ["B1", "B2", "B3"]
pred = {"B1": [], "B2": ["B1", "B2"], "B3": ["B2"]}
# Reaching definitions: may-analysis, start from empty sets.
gen = {"B1": {"d1", "d2"}, "B2": {"d3", "d4"}, "B3": {"d5"}}
kill = {"B1": {"d3", "d4"}, "B2": {"d1", "d2"}, "B3": set()}
inn, out, passes = solve_forward(blocks, pred, gen, kill, union, set(), set())
print(passes, sorted(inn["B2"])) # 2 ['d1', 'd2', 'd3', 'd4']
# Available expressions: B1 computes e, nothing kills it.
gen_e = {"B1": {"e"}, "B2": set(), "B3": set()}
kill_e = {b: set() for b in blocks}
for init in ({"e"}, set()):
inn, out, _ = solve_forward(blocks, pred, gen_e, kill_e, intersect, set(), init)
print(sorted(inn["B2"])) # ['e'] for the universal set, [] for emptyChoosing the initial value
| Analysis | Direction | Meet | Boundary | Initial interior value |
|---|---|---|---|---|
| Reaching definitions | Forward | Union | OUT[entry] = ∅ | ∅ |
| Live variables | Backward | Union | IN[exit] = ∅ | ∅ |
| Available expressions | Forward | Intersection | OUT[entry] = ∅ | All expressions |
Order and speed
Any visiting order reaches the same fixed point, but some orders reach it sooner. A forward analysis converges fastest when blocks are visited in reverse postorder of a depth-first traversal. A backward analysis converges fastest in postorder. If d is the largest number of back edges on any acyclic path, reaching definitions converges in about d + 1 changing passes, plus one pass to confirm nothing changes. A worklist variant keeps only the blocks whose inputs changed. It does the same job with less work.
Common errors
- Starting a must-analysis from empty sets instead of the universal set.
- Using intersection for live variables or reaching definitions, or union for available expressions.
- Computing a backward analysis in forward order, so IN and OUT swap roles.
- Stopping after a pass in which only IN sets changed. The test is on OUT sets for a forward analysis, or on IN sets for a backward one.
- Applying the entry boundary to a block that has predecessors. The entry value belongs to the entry block only.
Exercise
Why does an available-expressions solver initialize OUT of every non-entry block to the set of all expressions?
Show answer
The meet is intersection, so an empty starting set would make every intersection empty. An expression computed before a loop would then never reach the loop header. Starting from the full set lets the sets shrink only when some path fails to provide the expression, so the solver reaches the largest correct fixed point.
Exercise
A forward reaching-definitions analysis is solved in reverse postorder on a graph where the most back edges on any acyclic path is 2. What is the largest number of passes the solver needs, counting the pass that confirms no change?
Show answer
At most d + 1 = 3 passes change something, and one more pass confirms the fixed point, so 4 passes at most.