Every time you compile a program, your compiler quietly discards code that could never run. The trick that makes this possible is called constant propagation: the compiler tracks which variables are always the same value, rewrites every expression that depends on them, and then throws away any branch whose condition is already decided.
The idea is surprisingly old. You can trace it to simple constant folding — replacing 2 + 3 with 5 at compile time — but the powerful modern form is Sparse Conditional Constant Propagation (SCCP), introduced by Mark Wegman and Frank Kenneth Zadeck in 1991. SCCP walks the control-flow graph (CFG) of a function, marking each variable with one of three states on a lattice: unknown (not yet seen), constant (always the same value), or overdefined (different values reach this point). Whenever a value collapses to a constant, every expression and every branch that depends on it is immediately updated.
The payoff is dramatic: a whole if block can vanish because the compiler proved its condition is always false. The code that was compiled away never runs, never wastes memory, and never hides a bug.
Comments
Loading comments...