Q9Compiler Design
Question
2 marks
What is Peephole Optimization?
Answer
Peephole Optimization is a machine-code level optimization that examines a small sliding window (peephole) of consecutive instructions and replaces them with shorter or faster equivalent sequences.
Peephole optimization is a local optimization technique applied to the generated machine code or assembly code. The optimizer examines a small window of consecutive instructions (the 'peephole') and looks for patterns that can be replaced by more efficient instruction sequences.
- Redundant Load/Store Elimination: MOV R, x; MOV x, R → MOV R, x (the second instruction is redundant if R hasn't changed).
- Constant Folding: Replace compile-time constants with their computed values.
- Strength Reduction: Replace expensive operations: MUL R, 2 → ADD R, R or SHL R, 1.
- Unreachable Code Elimination: Remove instructions after an unconditional jump that are never reached.
- Jump Optimization: GOTO L1; L1: GOTO L2 → GOTO L2 (jump to jump elimination).
- Algebraic Simplification: ADD R, 0 → NOP; MUL R, 1 → NOP.