CcCompilers · Lesson 5 of 7

Optimization — Where the Magic Lives

Naively translated code is slow. Optimizers transform the program — hundreds of passes, each a small rewrite that provably preserves behavior — until the output beats what you'd write by hand.

Text
What -O2 does to your code (each pass, repeatedly):

  constant folding     3 * 60          ->  180
  constant propagation x=5; y=x+1      ->  y=6
  dead code removal    if(false){...}  ->  (gone)
  common subexpression (a*b) + (a*b)   ->  t=a*b; t+t
  function inlining    call sq(x)      ->  x*x  (no call)
  loop invariant hoist for(...){k=n*4} ->  k=n*4; for(...)
  strength reduction   i * 2           ->  i << 1
  loop unrolling       4 iterations    ->  straight-line code
  vectorization        sum loop        ->  SIMD: 8 adds/instr

Passes enable each other: inlining exposes constants,
folding kills branches, killing branches exposes more.
That's why they run in a loop until nothing changes.
C
// Try on godbolt.org — gcc -O2:
int sum_to(int n) {
    int total = 0;
    for (int i = 1; i <= n; i++)
        total += i;
    return total;
}

// The compiler recognizes the pattern and emits the
// CLOSED FORM — no loop at all:
//   n * (n + 1) / 2   (a few instructions, O(1))

// And this function:
int always_42(void) {
    int x = 6, y = 7;
    return x * y;
}
// compiles to:
//   mov eax, 42
//   ret

The contract is the 'as-if' rule: the optimizer may do anything as long as observable behavior is unchanged. This is also where undefined behavior gets teeth — in C, signed overflow is UB, so the compiler assumes it never happens and deletes your 'if (x + 1 < x)' overflow check as dead code. The optimizer isn't malicious; it's holding you to the language's rules.

✦ Tip
Practical takeaways: write clear code — the optimizer handles micro-tricks better than you, and clear code optimizes better. Debug builds (-O0) are slow on purpose (variables stay in memory so debuggers can see them). And when a benchmark shows 0ns, the optimizer probably deleted your unused computation entirely.