Sub topic 16
Iterating to a Fixed Point
Assume you know basic set operations, control-flow graphs, and the gen, kill, use, and def sets used by data-flow analyses. This chapter shows how to solve those equations repeatedly until the results stop changing.
What a fixed point means
A data-flow analysis assigns a set to each basic block. The equations use neighboring blocks to calculate new sets. For example, live-variable analysis is backward:
OUT[B] = union of IN[S]for every successorS.IN[B] = use[B] union (OUT[B] - def[B]).
A fixed point is reached when another complete pass produces no changes. At that point, every block satisfies its data-flow equations.
The initial values depend on the analysis:
| Analysis kind | Usual direction | Meet operation | Typical initial value |
|---|---|---|---|
| May analysis | Forward or backward | Union | Empty set |
| Must analysis | Forward or backward | Intersection | Universal set |
Boundary blocks may need special values. For example, IN[entry] is empty for available expressions.
The iterative algorithm
A simple algorithm is:
- Initialize every
INandOUTset. - Visit each block.
- Recompute its set using the analysis equations.
- If a set changed, mark the computation as changed.
- Repeat passes while at least one set changes.
For a backward analysis, visiting blocks in reverse control-flow order often reaches the fixed point faster. A worklist algorithm improves this further: after a block changes, schedule only its predecessors, because only they may need new values.
The following solver computes live variables. It revisits all blocks until no set changes.
def live_variables(blocks, successors, use, defs):
live_in = {block: set() for block in blocks}
live_out = {block: set() for block in blocks}
changed = True
while changed:
changed = False
for block in reversed(blocks):
new_out = set()
for successor in successors[block]:
new_out |= live_in[successor]
new_in = use[block] | (new_out - defs[block])
if new_in != live_in[block] or new_out != live_out[block]:
live_in[block] = new_in
live_out[block] = new_out
changed = True
return live_in, live_out
blocks = ["B1", "B2", "B3"]
successors = {"B1": ["B2"], "B2": ["B2", "B3"], "B3": []}
use = {"B1": set(), "B2": {"s", "a"}, "B3": {"s"}}
defs = {"B1": {"a", "s"}, "B2": {"s"}, "B3": set()}
live_in, live_out = live_variables(blocks, successors, use, defs)
print({block: sorted(values) for block, values in live_in.items()})The loop edge from B2 back to itself means the result cannot be found by a single top-to-bottom pass. Information must circulate through the cycle.
Common errors
- Using successors for a forward analysis or predecessors for a backward analysis.
- Applying union where intersection is required.
- Forgetting the boundary condition at the entry or exit.
- Stopping after one pass through a graph containing a loop.
- Treating an unchanged
INset as sufficient while ignoring a changedOUTset.
Exercise
Why does a loop in a control-flow graph usually require more than one data-flow pass?
Show answer
A loop sends information back to an earlier block, so that block may change after it was already visited. Further passes propagate the new information until no set changes.