Sub topic 19
Available expressions
This chapter assumes you can split three-address code into basic blocks, draw a control flow graph (CFG), and compute gen and kill sets for reaching definitions. It also assumes you are comfortable with set union, intersection and difference.
What "available" means
An expression x op y is available at a program point p if two conditions hold.
- Every path from the entry to p evaluates
x op y. - After the last such evaluation on each path, neither
xnoryis assigned again.
If x op y is available where it is computed again, the compiler can reuse the earlier value. This is global common subexpression elimination.
The word "every" makes this a must analysis. Reaching definitions and live variables are may analyses that ask whether some path has a property.
Gen and kill for a block
gen[B]holds the expressions B evaluates and does not invalidate later in B.kill[B]holds the expressions that have an operand defined in B.
Scan a block forward. On each instruction z = x op y, first add x op y to the gen set. Then remove every expression that contains z. The order matters, because it handles a = a + 1: the expression a + 1 is added and then removed, so it is not generated.
Equations
IN[entry] = empty set
IN[B] = intersection of OUT[P] for every predecessor P of B
OUT[B] = gen[B] U (IN[B] - kill[B])The only difference from reaching definitions is the meet operator, which is intersection instead of union.
| Analysis | Direction | Meet | Initial OUT of non-entry blocks |
|---|---|---|---|
| Reaching definitions | Forward | Union | Empty set |
| Live variables | Backward | Union | Empty set |
| Available expressions | Forward | Intersection | Universal set U |
Initializing to U is deliberate. Intersection can only shrink a set, so the solver must start at the top and move down. Starting at the empty set would make every loop look as though nothing is available, and it would never recover. The result would be safe but needlessly weak.
Worked example
The universe is U = {a+b, c*d}.
| Block | gen | kill |
|---|---|---|
| B1 | {a+b, c*d} | {} |
| B2 | {} | {a+b} |
| B3 | {a+b, c*d} | {} |
The iteration starts with OUT[B2] = OUT[B3] = U.
- IN[B1] = {}, so OUT[B1] = {a+b, c*d}.
- IN[B2] = OUT[B1] = {a+b, cd}, so OUT[B2] = {cd}.
- IN[B3] = OUT[B1] ∩ OUT[B2] = {cd}, so OUT[B3] = {a+b, cd}.
- A second pass changes nothing, so the solution is a fixed point.
At the entry to B3, c * d is available and a + b is not. The second c * d can be replaced by u. The second a + b must stay, because the path through B2 redefines a.
A solver you can run
def available_expressions(blocks, preds, gen, kill, universe, entry):
out = {b: set(universe) for b in blocks}
inn = {b: set() for b in blocks}
changed = True
while changed:
changed = False
for b in blocks:
if b == entry:
new_in = set()
else:
new_in = set(universe)
for p in preds[b]:
new_in &= out[p]
new_out = gen[b] | (new_in - kill[b])
if new_in != inn[b] or new_out != out[b]:
inn[b], out[b] = new_in, new_out
changed = True
return inn, out
blocks = ["B1", "B2", "B3"]
preds = {"B1": [], "B2": ["B1"], "B3": ["B1", "B2"]}
universe = {"a+b", "c*d"}
gen = {"B1": {"a+b", "c*d"}, "B2": set(), "B3": {"a+b", "c*d"}}
kill = {"B1": set(), "B2": {"a+b"}, "B3": set()}
inn, out = available_expressions(blocks, preds, gen, kill, universe, "B1")
for b in blocks:
print(b, sorted(inn[b]), sorted(out[b]))It prints:
B1 [] ['a+b', 'c*d']
B2 ['a+b', 'c*d'] ['c*d']
B3 ['c*d'] ['a+b', 'c*d']Loops and common mistakes
Back edges are where the initialization shows up. Suppose B2 has a self-loop. Then IN[B2] = OUT[B1] ∩ OUT[B2], which depends on OUT[B2] itself. Starting OUT[B2] at U lets the first pass use OUT[B1] alone. If B2 does not kill an expression, the expression stays available around the loop.
Watch for these errors:
- Computing gen without checking later redefinitions in the same block.
- Forgetting that
x = x + ygenerates nothing forx + y. - Treating
IN[entry]as U. It is the empty set, because nothing has been computed before the program starts. - Mixing up the union and intersection meets.
- Ignoring calls and pointer writes. A conservative compiler lets them kill any expression whose operands might be changed.
Using the result
If x op y is available at the start of a block and the block evaluates it before redefining an operand, the evaluation is redundant. To eliminate it safely, the compiler does the following.
- Find every earlier evaluation that reaches the redundant one.
- Copy each one into a new temporary
t. - Replace the redundant evaluation with
t.
This is needed because the earlier evaluations may have stored their results in different variables on different paths.
Exercise
Let B1 be p = a * b, B2 be q = a * b; b = b + 1, and B3 be r = p. The edges are B1→B2, B2→B2 and B2→B3. The universe is {ab, b+1}. Is ab available at the entry of B2?
Show answer
No. gen[B2] is empty. The expression ab is generated and then killed by the assignment to b. The expression b+1 is also not generated, because b is redefined after it is computed. kill[B2] is {ab, b+1}. OUT[B1] = {ab} and OUT[B2] = {}, so IN[B2] = OUT[B1] ∩ OUT[B2] = {}. The path through the back edge arrives with a changed b, so ab is not available at B2's entry.