📖 Scenario: You are working on a simple compiler that translates assembly-like instructions. Sometimes, small sequences of instructions can be improved to run faster or use fewer steps. This is called local optimization using the peephole technique.Imagine you have a short list of instructions, and you want to find and replace inefficient patterns with better ones.
🎯 Goal: Build a small peephole optimizer that scans a list of instructions and replaces a specific inefficient pattern with a better one.For example, replace the sequence LOAD A followed by STORE A with a single MOVE A instruction.
📋 What You'll Learn
Create a list called instructions with the exact sequence: 'LOAD A', 'STORE A', 'ADD B', 'LOAD C', 'STORE C'
Create a variable called optimized_instructions initialized as an empty list
Use a while loop with an index variable i to scan through instructions
Replace every occurrence of 'LOAD A' followed immediately by 'STORE A' with 'MOVE A' in optimized_instructions, copying other instructions unchanged
💡 Why This Matters
🌍 Real World
Compilers use peephole optimization to improve small parts of generated code, making programs run faster and use less memory.
💼 Career
Understanding peephole optimization is important for compiler developers, performance engineers, and anyone working on low-level code optimization.
Progress0 / 4 steps
1
Create the initial instruction list
Create a list called instructions with these exact strings in order: 'LOAD A', 'STORE A', 'ADD B', 'LOAD C', 'STORE C'.
Compiler Design
Hint
Use square brackets [] to create a list and separate items with commas.
2
Prepare the optimized instructions list and index
Create an empty list called optimized_instructions and a variable i set to 0 to use as an index for scanning.
Compiler Design
Hint
Use [] to create an empty list and assign 0 to i.
3
Scan and optimize instructions using a while loop
Use a while loop that runs while i is less than the length of instructions. Inside the loop, check if the current instruction is 'LOAD A' and the next instruction is 'STORE A'. If yes, append 'MOVE A' to optimized_instructions and increase i by 2. Otherwise, append the current instruction and increase i by 1.
Compiler Design
Hint
Remember to check the next instruction exists before accessing it to avoid errors.
4
Complete the peephole optimization
Add a final comment line # Peephole optimization complete after the loop to mark the end of the optimization process.
Compiler Design
Hint
This comment helps others understand where the optimization ends.
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.