0
0
Compiler Designknowledge~10 mins

Local optimization (peephole) in Compiler Design - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Local optimization (peephole)
Input: Sequence of instructions
Select small window (peephole)
Analyze instructions in window
Apply optimization rules if pattern matches
Replace with optimized instructions
Move window forward
Repeat
Output optimized code
Local optimization scans small groups of instructions to find and replace inefficient patterns with better ones, improving code step-by-step.
Execution Sample
Compiler Design
LOAD R1, 0
ADD R1, 5
STORE R1, X
This code loads 0 into R1, adds 5, then stores R1 to X. Peephole optimization can simplify this.
Analysis Table
StepWindow InstructionsPattern Matched?ActionResulting Instructions
1LOAD R1, 0 ADD R1, 5Yes (Add after Load 0)Replace with LOAD R1, 5LOAD R1, 5 STORE R1, X
2LOAD R1, 5 STORE R1, XNoNo changeLOAD R1, 5 STORE R1, X
3STORE R1, XNoNo changeLOAD R1, 5 STORE R1, X
💡 No more windows to analyze; optimization complete.
State Tracker
VariableStartAfter Step 1After Step 2Final
InstructionsLOAD R1, 0 ADD R1, 5 STORE R1, XLOAD R1, 5 STORE R1, XLOAD R1, 5 STORE R1, XLOAD R1, 5 STORE R1, X
Key Insights - 2 Insights
Why does peephole optimization only look at a small window of instructions?
Because it focuses on local patterns to simplify or improve code without needing to analyze the entire program, as shown in Step 1 where only two instructions are checked.
What happens if no pattern matches in the current window?
The instructions remain unchanged and the window moves forward, as seen in Steps 2 and 3 where no changes occur.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at Step 1. What optimization was applied?
ARemoved STORE instruction
BAdded an extra ADD instruction
CReplaced LOAD 0 and ADD 5 with LOAD 5
DNo optimization applied
💡 Hint
Check the 'Action' and 'Resulting Instructions' columns in Step 1.
At which step does the peephole window find no matching pattern?
ABoth Step 2 and Step 3
BStep 2
CStep 3
DStep 1
💡 Hint
Look at the 'Pattern Matched?' column for Steps 2 and 3.
If the initial code had 'ADD R1, 0' instead of 'ADD R1, 5', what would the optimization likely do at Step 1?
ANo change because ADD 0 does nothing
BReplace LOAD 0 and ADD 0 with LOAD 0
CReplace LOAD 0 and ADD 0 with LOAD 5
DRemove the LOAD instruction
💡 Hint
Consider how adding zero affects the value and what peephole optimization tries to simplify.
Concept Snapshot
Local optimization (peephole):
- Examines small instruction windows
- Matches patterns to simplify or improve code
- Replaces inefficient sequences with better ones
- Moves window forward and repeats
- Improves code locally without full program analysis
Full Transcript
Local optimization, also called peephole optimization, works by looking at small groups of instructions in a program. It checks if these instructions match known inefficient patterns. If they do, it replaces them with better instructions. For example, loading zero and then adding five can be replaced by loading five directly. The process repeats by moving the small window forward until the whole code is checked. If no pattern matches, the instructions stay the same. This method improves code step-by-step without analyzing the entire program at once.