What is local optimization (peephole) in compiler design?
Local optimization, also called peephole optimization, is a technique where the compiler looks at a small set of instructions (a "peephole") and tries to replace them with a more efficient sequence without changing the program's behavior.
Click to reveal answer
beginner
Why is peephole optimization called 'local' optimization?
Because it focuses on a small, localized part of the code—usually just a few instructions at a time—rather than analyzing the entire program or large blocks of code.
Click to reveal answer
beginner
Give an example of a simple peephole optimization.
Replacing a sequence like "LOAD A; LOAD A" with just "LOAD A" because loading the same value twice is redundant.
Click to reveal answer
intermediate
What kinds of improvements can peephole optimization achieve?
It can reduce the number of instructions, remove redundant operations, simplify calculations, and improve execution speed and code size.
Click to reveal answer
intermediate
What is a limitation of peephole optimization?
It only looks at small instruction windows and cannot optimize across larger code blocks or consider global program behavior.
Click to reveal answer
What does peephole optimization primarily focus on?
ASmall sequences of instructions
BEntire program structure
CMemory allocation
DUser interface design
✗ Incorrect
Peephole optimization looks at small windows of instructions to improve them locally.
Which of the following is an example of peephole optimization?
AChanging variable names for clarity
BReplacing 'LOAD A; LOAD A' with 'LOAD A'
CRearranging functions in a file
DAdding comments to code
✗ Incorrect
Removing redundant loads is a classic peephole optimization.
What is a key benefit of peephole optimization?
AImproves code readability
BIncreases program size
CReduces instruction count
DChanges program logic
✗ Incorrect
Peephole optimization reduces instructions to make code faster and smaller.
Which is NOT a characteristic of peephole optimization?
ASimplifies calculations
BWorks on small instruction windows
CRemoves redundant instructions
DAnalyzes entire program flow
✗ Incorrect
Peephole optimization does not analyze the entire program, only small parts.
Why might peephole optimization miss some improvements?
AIt only looks at small parts of code
BIt changes program logic
CIt requires user input
DIt only works on source code comments
✗ Incorrect
Because peephole optimization focuses on small instruction sets, it can miss larger optimization opportunities.
Explain what local optimization (peephole) is and why it is useful in compiler design.
Think about how looking at just a few instructions can help make code faster or smaller.
You got /4 concepts.
Describe a simple example of peephole optimization and its effect on the code.
Consider removing repeated instructions or simplifying calculations.
You got /3 concepts.
Practice
(1/5)
1. What is the main goal of local optimization (peephole) in compiler design?
easy
A. To improve small sequences of instructions by removing redundancies
B. To rewrite the entire program in a different language
C. To increase the number of instructions for clarity
D. To change the program's output for optimization
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 A
Quick Check:
Local optimization = remove redundancies [OK]
Hint: Focus on small instruction sequences and redundancy removal [OK]
Common Mistakes:
Thinking it rewrites the whole program
Believing it changes program output
Assuming it adds more instructions
2. Which of the following is a correct example of a peephole optimization?
easy
A. Replacing LOAD A; LOAD A with LOAD A
B. Changing variable names for readability
C. Adding comments to the code
D. Rearranging functions in the source file
Solution
Step 1: Identify peephole optimization examples
Peephole optimization looks at small instruction sequences to remove redundancies.
Step 2: Evaluate each option
Replacing LOAD A; LOAD A with LOAD A removes duplicate instructions, which is a classic peephole optimization. Options A, B, and D do not optimize instructions.