Local optimization (peephole) in Compiler Design - Time & Space Complexity
Local optimization, or peephole optimization, improves small parts of code to run faster.
We want to know how the time to optimize grows as the code size grows.
Analyze the time complexity of the following peephole optimization code snippet.
for i in range(len(instructions) - window_size + 1):
window = instructions[i:i+window_size]
if can_optimize(window):
optimized = optimize(window)
instructions[i:i+window_size] = optimized
This code looks at small groups of instructions and tries to replace them with faster ones.
- Primary operation: Sliding over the instruction list with a fixed-size window.
- How many times: Once for each instruction minus the window size plus one.
As the number of instructions grows, the optimizer checks more windows, but each window is small and fixed.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of checks grows directly with the number of instructions.
Time Complexity: O(n)
This means the time to optimize grows in a straight line as the code gets longer.
[X] Wrong: "Peephole optimization takes constant time no matter how big the code is."
[OK] Correct: The optimizer must check each part of the code, so more code means more checks.
Understanding how local optimization scales helps you explain compiler efficiency clearly and confidently.
"What if the window size grew with the input size? How would the time complexity change?"
Practice
local optimization (peephole) in compiler design?Solution
Step 1: Understand local optimization scope
Local optimization focuses on small instruction sequences, not the whole program.Step 2: Identify the goal of peephole optimization
Peephole optimization removes unnecessary or redundant instructions to make code faster and smaller without changing its meaning.Final Answer:
To improve small sequences of instructions by removing redundancies -> Option AQuick Check:
Local optimization = remove redundancies [OK]
- Thinking it rewrites the whole program
- Believing it changes program output
- Assuming it adds more instructions
Solution
Step 1: Identify peephole optimization examples
Peephole optimization looks at small instruction sequences to remove redundancies.Step 2: Evaluate each option
ReplacingLOAD A; LOAD AwithLOAD Aremoves duplicate instructions, which is a classic peephole optimization. Options A, B, and D do not optimize instructions.Final Answer:
Replacing LOAD A; LOAD A with LOAD A -> Option AQuick Check:
Remove duplicate instructions = peephole optimization [OK]
- Confusing code readability changes with optimization
- Thinking comments affect optimization
- Assuming rearranging functions is peephole optimization
LOAD R1, #5 ADD R1, R1, #0 STORE R1, 100(R2)
What will be the optimized sequence after applying peephole optimization?
Solution
Step 1: Identify redundant instructions
The instructionADD R1, R1, #0adds zero, which does not change the value in R1, so it is redundant.Step 2: Remove redundant instruction
Removing the redundant ADD instruction leavesLOAD R1, #5andSTORE R1, 100(R2).Final Answer:
LOAD R1, #5 STORE R1, 100(R2) -> Option CQuick Check:
Remove add zero = optimized sequence [OK]
- Keeping redundant add zero instruction
- Removing load instruction incorrectly
- Misunderstanding instruction effects
Original: LOAD R2, #10 SUB R2, R2, #0 STORE R2, 200(R3) Optimized: LOAD R2, #10 SUB R2, R2, #10 STORE R2, 200(R3)
Solution
Step 1: Compare original and optimized subtraction
Original subtracts zero, which does nothing. Optimized subtracts ten, which changes the value incorrectly.Step 2: Identify the error in optimization
Replacing subtraction by zero with subtraction by ten changes program meaning, which is incorrect for peephole optimization.Final Answer:
The subtraction of zero was incorrectly replaced with subtraction of ten -> Option DQuick Check:
Incorrect instruction change = error in peephole optimization [OK]
- Changing instruction values incorrectly
- Removing necessary instructions
- Assuming all changes are optimizations
LOAD R3, #0 ADD R3, R3, #5 ADD R3, R3, #0 STORE R3, 300(R4)
Which peephole optimization sequence correctly optimizes this code without changing its meaning?
Solution
Step 1: Analyze the original instructions
Loading zero into R3, then adding 5, then adding 0, is equivalent to loading 5 directly into R3.Step 2: Apply peephole optimization
Replace the sequence with a single instructionLOAD R3, #5and then store. This removes redundant instructions without changing meaning.Final Answer:
LOAD R3, #5 STORE R3, 300(R4) -> Option BQuick Check:
Combine load and add zero to direct load [OK]
- Keeping redundant add zero
- Not combining load and add
- Removing load instruction incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
