CAPSTONE: A TINY OPTIMIZING PASS

Everything in this path, in one pass over three-address code

L320 MINCOMPILERS
PREDICT

x = 5; y = x * 2; p = &x; *p = 7; z = x + y; print z. A constant propagation pass replaces each variable with its known value. It tracks constants by name only and ignores stores through pointers. What does the optimised program print?

An optimiser rewrites three-address code into cheaper code with the same behaviour. Constant propagation and folding walk forward, remember which variables hold known constants, and compute x * 2 at compile time. Dead-code elimination walks backward with a live set and deletes definitions nobody reads.

Both rules need a safety condition. A store through a pointer, or a call, can change any variable whose address was taken, so the pass forgets those constants. A call can print, so it is never dead just because its result is unused.

▶ SIMULATION
ORIGINAL · 9 instrs
01 x = 5
02 y = x * 2
03 p = &x
04 *p = 7
05 z = x + y
06 t = call tick()
07 u = y - 10
08 print z
09 print y
OPTIMISED · 9 instrs
not optimised yet
OUTPUT
original: —
optimised: —
original program loaded
rulessafe: stores and calls forget address-taken constants; only pure code is removed
folded / removed0 folded · removed: —
peak live values (registers needed)3 → 3
✓ optimised program prints the same as the original✓ no instruction with a side effect was removed
SCHEDULE
01[PASS]UNSAFE: a store through a pointer never changes a named variable
02[ORIGINAL]run the original program; record what it prints
03[PASS]constant propagation + folding (forward)
04[PASS]dead-code elimination (backward)
05[OPTIMISED]run the optimised program
06[CHECK]compare the two outputs
Start of unsafe: fold through *p scenario. 6 steps.
// fold: remember constants by NAME only
x = 5; p = &x; *p = 7;
z = x + y      // 'x is 5' => z = 15   (x is 7)

// dce: drop any def whose result is unused
t = call tick()   // t unused => deleted, and so is the print

The invariant strip compares outputs and watches what DCE removes. UNSAFE: FOLD THROUGH *p prints 15 instead of 17. UNSAFE: DROP UNUSED CALLS loses the tick line. SAFE PASS prints the same three lines with 7 of 9 instructions and one fewer value live at the peak.

BREAK IT NOT YET

Turn on one unsafe rule at a point in the pass where it changes what the program prints. Then try it after the step it would affect.

GOAL: the optimised program prints something different

FIX IT NOT YET

This pass folds and removes dead code, and breaks two programs. Make it safe: a store or a call may change any variable whose address was taken, and only const, bin and addr instructions may be deleted. The grader runs original and optimised on six programs, and still expects the pure one to shrink.

Mental model:

an optimisation is a proof, not a pattern. Folding is a proof that a value is known; deleting is a proof that nothing can observe the instruction. Aliases and side effects are where those proofs fail, so a correct pass is conservative exactly there and aggressive everywhere else.

IN PRODUCTION

LLVM's SCCP, InstCombine and DCE, GCC's tree-ssa passes, V8's TurboFan, and the “works in debug, wrong in release” bug report. Alias analysis and the C strict-aliasing rule decide how much a real optimiser may assume about pointers.

IN INTERVIEWS

“Implement constant folding and dead-code elimination over this IR” or “Why might this optimisation be unsafe?” Show the forward constant map and the backward live set, then name aliasing through pointers and side-effecting calls as the two places the pass must give up.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.