Intermediate Code Generation · Compiler Design

Code Improvement (Optimization) Analysis

Watch lectures

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 + -O levels — 3 marks, frequent.
Budget AppAndroid App

Pocketly on Android

Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.

← All Compiler Design modules