Sub topic 17
Optimizations That Use Data Flow Results
Assume you know basic three-address code, control-flow graphs, gen/kill sets, and fixed-point iteration. This chapter connects those analyses to optimizations that preserve program behavior while reducing work.
Reaching Definitions and Constant Propagation
A reaching definition is an assignment that may reach a program point without being overwritten. If every reaching definition of a variable assigns the same constant, the compiler can replace the variable with that constant.
B1:
x = 4
y = x + 3
goto B2
B2:
z = y * 2The definition x = 4 reaches y = x + 3, so constant propagation changes the code to:
B1:
x = 4
y = 7
goto B2
B2:
z = 14This replacement is safe only when no other reaching definition gives x a different value.
Live Variables and Dead Code Elimination
A variable is live at a point if its current value may be read later. A definition whose result is never used can be removed.
a = read()
b = a + 1
c = 10
print(b)The assignment to c is dead because c is not live after that statement. Removing it does not change the output.
Be careful with side effects. In c = read(), the assignment may be dead, but the input operation is not removable if reading consumes input or raises an error.
Available Expressions and Common Subexpression Elimination
An expression is available at a point if it was already computed on every path to that point and none of its operands changed afterward.
t1 = a * b
...
t2 = a * bIf neither a nor b changes between the two statements, the compiler can reuse t1:
t1 = a * b
...
t2 = t1The analysis uses intersection at control-flow merges because the expression must be available along every incoming path.
| Optimization | Main analysis | Typical condition |
|---|---|---|
| Constant propagation | Reaching definitions | All relevant definitions give one constant |
| Dead code elimination | Live variables | Result is not live and operation has no side effect |
| Common subexpression elimination | Available expressions | Expression is available and operands are unchanged |
| Loop-invariant code motion | Reaching definitions and loop information | Expression has the same value on every iteration |
Loop-Invariant Code Motion
An expression inside a loop is loop-invariant when its value does not change between iterations. The compiler may move it before the loop, reducing repeated computation.
i = 0
t = a * b
while (i < n) {
x[i] = t + i
i = i + 1
}This transformation is safe only if evaluating a * b before the loop has the same value and side effects as evaluating it inside the loop. Memory writes, exceptions, aliases, and function calls can prevent motion.
Choosing a Safe Transformation
After changing code, the compiler often recomputes data flow information. One optimization can expose new dead assignments or common expressions.
Exercise
Can x = 5 be removed when x is not live afterward?
Show answer
Yes, if the assignment has no observable side effect and no later operation depends on its stored value.