Basic Code Optimization — CSE, Constant Folding, Peephole
588 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Basic Code Optimization — CSE, Constant Folding, Peephole ## 🎯 Learning Objectives - Construct basic blocks and flow graphs - Apply common subexpression elimination (CSE) - Perform constant folding and propagation - Remove dead code - Use peephole optimization patterns * * * ## 1. Basic Blocks and Flow Graphs ###...

Basic Code Optimization — CSE, Constant Folding, Peephole
🎯 Learning Objectives
- Construct basic blocks and flow graphs
- Apply common subexpression elimination (CSE)
- Perform constant folding and propagation
- Remove dead code
- Use peephole optimization patterns
1. Basic Blocks and Flow Graphs
1.1 Basic Block
A basic block is a sequence of instructions with:
- One entry point (first instruction)
- One exit point (last instruction)
- No jumps in/out of the middle
1.2 Construction
pseudo1. Identify leaders: first instruction, jump targets, instructions after jumps 2. Each leader starts a new basic block 3. Block extends to the next leader (exclusive)
2. Common Optimizations
2.1 Constant Folding
tac// Before // After t1 = 2 * 3 t1 = 6 t2 = t1 + 4 t2 = 10
2.2 Constant Propagation
tac// Before // After x = 5 x = 5 y = x + 3 y = 8 z = x * y z = 40
2.3 Common Subexpression Elimination (CSE)
tac// Before // After t1 = a + b t1 = a + b t2 = a + b t2 = t1 // CSE! t3 = t1 * c t3 = t1 * c t4 = t1 * c t4 = t3 // CSE!
2.4 Dead Code Elimination
tac// Before // After x = 5 -- removed (x never used) y = 10 y = 10 z = y + 2 z = y + 2
3. Peephole Optimization
Replace small instruction sequences with better ones:
| Pattern | Replacement | Savings |
|---|---|---|
push a; pop a | (nothing) | 2 instructions |
goto L; L: ... | (nothing) | 1 instruction |
x = x + 0 | (nothing) | 1 instruction |
x = x * 1 | (nothing) | 1 instruction |
x = x * 2 | x = x << 1 | Replace multiply with shift |
4. 📝 Practice Questions
Q1: Optimize: t1 = a + b; t2 = a + b; t3 = c * d; t4 = c * d; t5 = t2 + t4Answer: t1 = a + b t3 = c * d t5 = t1 + t3Both CSE opportunities: t2 = t1, t4 = t3. After removing redundant copies: just t1, t3, t5. Q2: What is a basic block?Answer: A sequence of consecutive instructions with a single entry (first instruction) and single exit (last instruction), no internal jumps or jump targets. Basic blocks form the vertices of a control flow graph. Q3: Why is peephole optimization called "peephole"?Answer: It examines a small window (peephole) of instructions at a time, typically 2-5 instructions, looking for local optimization opportunities. The window slides across the code. Q4: What is the difference between constant folding and constant propagation?Answer: Constant folding evaluates constant expressions at compile time (e.g., 2*3 → 6). Constant propagation replaces variables with known constant values throughout the code (e.g., x=5 → replace all uses of x with 5). Q5: How does dead code elimination improve performance?Answer: It removes instructions that compute values never used later. This reduces code size, saves CPU cycles, and may enable further optimizations by removing dependencies.
5. 🔗 Cross-References
- Week 7 - Intermediate Code: TAC is the input to optimization
- Week 10 - Data Flow Analysis: Global optimization techniques
- Week 11 - Code Generation: Optimized TAC → assembly Join Discord PreviousIntermediate CodeNextHome