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

The Problem

Code generation produces naive, literal translations of IR instructions. These often contain redundant loads/stores, dead stores, and inefficient instruction sequences that a broader global optimizer missed or that only appear after register allocation. A simple, fast post-processing pass can clean up these local inefficiencies.

Core Idea

Peephole optimization is a simple machine-dependent optimization technique that examines a small sliding window (the “peephole”) of consecutive target instructions and replaces inefficient patterns with better ones. Common patterns include: redundant load/store elimination, constant folding, strength reduction, dead code elimination, and algebraic simplifications like x = x + 0 → nop.

How It Works

The peephole optimizer scans the instruction stream with a fixed-size window (typically 2-5 instructions). For each window position, it checks against a set of pattern templates. When a pattern matches, it replaces the matched instructions with the optimized replacement. The window is then repositioned to check for cascading opportunities. Common patterns: ST R1, M; LD M, R1ST R1, M (remove redundant load), ADD #0nop (remove no-op addition), MUL #2ADD same (strength reduction).

Visual Explanation

peephole Before Before Peephole: LD R1, x ST y, R1 LD R1, y ADD R1, #0 ST z, R1 Window Peephole Window Before->Window After After Peephole: LD R1, x ST y, R1 ST z, R1 Window->After Patterns Patterns: 1. ST y,R; LD R,y → del LD 2. ADD #0 → nop 3. ST x,R; LD R,x → del LD Patterns->Window match

Semantic Network

semantic_peephole THIS Peephole Optimization PRE1 Code Optimization THIS--PRE1 built from PRE2 Code Generation THIS--PRE2 applied after — in code generation CON1 Common Subexpression Elimination THIS--CON1 contrasts with — local vs global CON2 Constant Propagation THIS--CON2 contrasts with — post-generation vs IR-level REL1 Code Generator Design Issues THIS--REL1 related

Key Properties

  • Local scope: Examines only a small window of instructions (typically 2-5)
  • Pattern-based: Defined by before/after template pairs
  • Machine-dependent: Patterns are specific to the target instruction set
  • Post-generation: Applied after code generation or during the final phase
  • Redundant instruction elimination: The most common peephole improvement

Connections

Edge Cases & Gotchas

  • Cascading effect: One peephole optimization can create an opportunity for another — the optimizer must iterate until no more patterns match
  • Oversized window: A larger window catches more patterns but costs more to match — most implementations keep it small
  • Architecture-specific: A peephole optimization on x86 may not apply to ARM — patterns must be defined per target
  • Safety: Must preserve program semantics — pattern matching must be conservative about flags and condition codes