• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

What’s Being Compared

Compiler code optimization encompasses a variety of techniques operating at different stages and scopes. Peephole optimization, common subexpression elimination (CSE), constant propagation (with folding), and liveliness (live variable) analysis represent four fundamental approaches that differ in scope, level, and mechanism.

The Core Tension

Optimization is a trade-off between analysis cost and improvement. Local techniques (peephole) are cheap but miss global opportunities. Global techniques (CSE, CP) capture more but require expensive data-flow analysis. The techniques compound — constant propagation creates CSE opportunities, CSE reduces register pressure, and liveliness analysis enables further dead code elimination.

Comparison

DimensionPeepholeCSEConstant PropagationLiveliness Analysis
ScopeLocal (2-5 instructions)Local + GlobalLocal + GlobalGlobal (whole CFG)
LevelTarget instructionsIntermediate representation (TAC)Intermediate representation (TAC)Intermediate representation (TAC)
MechanismPattern matching & replacementAvailable-expression DFAReaching-definitions DFABackward DFA (USE/DEF)
What it removesRedundant loads/stores, no-opsRedundant recomputationRuntime constant evaluationDead variable assignments
Analysis costVery low (linear scan)Medium (DFA iteration)Medium (DFA iteration)Medium (backward DFA iteration)
DependencyRequires preceding CSE/CPBenefits from CPIndependentUsed by CSE and DCE
SafetyMust preserve flags/conditionsAlways safe (same operands)Conservative (must be sure)Conservative (assume live)

When to Choose Each

Peephole: Run last, after code generation. Best for cleaning up the final target instruction stream — removing redundant loads/stores and no-ops introduced by naive code generation.

CSE: Use when expressions repeat (especially in loops). Essential for array address calculations (a[i*cols+j]) and repeated field accesses.

Constant Propagation + Folding: Use always — the simplest and safest optimization. Creates cascading simplification opportunities for other optimizations.

Liveliness Analysis: Essential prerequisite for dead code elimination and register allocation. Run before register allocation to determine which values need registers.

The Insight

These four techniques form a pipeline: constant propagation simplifies expressions (creating redundancies) → CSE eliminates the redundancies → code generator produces naive target code → peephole cleans it up — while liveliness analysis provides liveness information for both CSE (available expressions) and dead code elimination after each step. Individually each saves a few percent; together they can double performance.

Connections