Intermediate Code Generation · Compiler Design

Local Optimization

Watch lectures

Local Optimization

1. Scope

Local optimization works within a single basic block (no control flow) — cheap, always safe, big cumulative win.

2. Techniques (exam list)

Opt Before → After
Constant folding x=2*3 → x=6
Constant propagation a=5; b=a+1 → b=6
Copy propagation a=b; c=a+1 → c=b+1
Common subexpr (local) t1=b+c; t2=b+c → reuse t1
Dead code a=5 (never used) → delete
Strength reduction x*2 → x<<1, x*0 → 0
Algebraic x+0→x, x*1→x

3. Worked

t1 = 2*3     → t1 = 6        (fold)
a = t1       → a = 6         (propagate)
b = a + 0    → b = a (=6)    (algebraic + propagate)
c = b        → (c unused) delete

DAG per block finds local commons automatically.

4. Limits

Cannot move across labels/jumps; needs global/loop opts for cross-block wins.

5. Exam Points

  • List local opts with before/after — 7 marks, very frequent.
  • Optimize given block (fold+propagate+dead) — 5-7 marks, frequent.
  • DAG for local CSE — 3 marks, frequent.
AI LearningWeb Platform

Coursify — Personal AI Course Generator

Struggling with a difficult subject or concept? Turn any engineering topic into a structured course with interactive lessons, quizzes, and research-backed explanations.

← All Compiler Design modules