Code Improvement (Optimization) Analysis
1. Goal
Optimization rewrites IR to run faster / smaller without changing meaning. Analysis first proves safety (what is live, what is constant, what aliases).
2. When and What
| Level | Examples |
|---|---|
| Peephole/local | constant folding, strength reduction, dead-store elim |
| Loop | code motion, unrolling, induction-var elim |
| Global | common-subexpr elim, copy propagation, inlining |
Criteria: must be safe (preserve semantics), profitable (worth it), applied at right level (source/IR/machine).
3. Analysis → Transform Pipeline
build CFG → data-flow analysis ( reaching defs, live vars, avail exprs ) → prove safe → transform → (validate)
Example: x=2*3 folds to x=6 only after proving no overflow trap change; a=b; c=a propagates only if a not redefined.
4. Cost Model
Compilers estimate frequency (loops ×10) vs size (unroll duplicates code). -O0 (debug) vs -O2/-O3 (release).
5. Exam Points
- Define optimization + analysis role — 5 marks, very frequent.
- List types with examples — 5 marks, frequent.
- Safe vs profitable +
-Olevels — 3 marks, frequent.