Recap of Lecture 10 and Homework Check
- Three styles: variable, fixed, hybrid encoding.
- MIPS formats: R 6|5|5|5|5|6, I 6|5|5|16, J 6|26.
- Branch offset = (target - (PC + 4)) / 4.
- Big constants: lui + ori in $at.
Homework check
- 1. Encode or $t3, $t4, $t5; lw $s0, -12($fp); j to 0x00400040.
- 2. Decode 0x00A63020 and 0xAFBF0004.
- 3. 200 instructions, 60% can be 16-bit: new size and saving?
Today's Plan (50 minutes)
0-5
Recap and homework check Lecture 10
5-8
Plan and learning outcomes
8-17
Module 1: Inside a compiler phases, levels, why architects care
17-33
Module 2: Optimizations at work four versions of one loop, measured in MARS
33-38
Module 3: How the ISA helps where variables live; four guidelines
38-45
Class activity name and apply optimizations
45-50
Mistakes, quiz and homework
Learning Outcomes (SLOs)
By the end of this lecture you will be able to:
SLO 11.1
Describe the phases of an optimizing compiler and its optimization levels.
→ CLO3 · C2 Understand
SLO 11.2
Apply register allocation, constant folding, CSE, code motion and strength reduction, and measure their effect.
→ CLO3 · C3 Apply
SLO 11.3
Explain how an instruction set can help or hinder the compiler writer.
→ CLO3 · C2 Understand
CLO3: Explain instruction set principles and advanced instruction-level and thread-level parallelism techniques.
Module 1: Inside a Compiler
The compiler is the real user of the ISA
Why Architects Care About Compilers
33 → 9
132 → 36 B
22 → 5
instructions
code size
loop instructions
clang 18, MIPS, -O0 → -O1
same C function
per element
A good translator
CPU time = IC x CPI x clock time. The compiler decides IC.
Word-by-word Urdu → English is long and clumsy; a skilled translator gives a short, clear sentence.
Structure of an Optimizing Compiler
Front end
High-level opt.
Global optimizer
Code generator
C, C++, Rust → IR
loops, inlining
- CSE, code motion,
- register allocation
- instruction selection,
- machine-dependent opt.
language-dependent ····················· shared IR ····················· ISA-dependent
One IR, many languages and many ISAs: clang made all of today's MIPS, RISC-V, ARM and x86 code.
NADRA counters
Token, data entry, verification, printing: each desk one job, the file moves on.
Optimization Levels
Phase ordering: passes help or block each other - the order of optimizations matters.
Module 2: Optimizations at Work
One loop, four versions, measured in MARS
Kinds of Optimization
Basic block: straight-line code - one way in (top), one way out (bottom).
Register Allocation
version 1: variables in memory
version 2: registers
- loop: lw $t1, 0($sp) # i
- lw $t2, 8($sp) # n
- slt $t1, $t1, $t2
- beq $t1, $zero, done
- lw $t1, 4($sp) # s
- ... 8 more, 3 of them
- ... more lw / sw
- j loop
- loop: beq $t0, $a1, done
- sll $t2, $t0, 2
- addu $t2, $a0, $t2
- lw $t3, 0($t2)
- addu $v1, $v1, $t3
- addiu $t0, $t0, 1
- j loop
16 → 7
9 → 1
Desk vs store
instructions per element
memory accesses per element
Hot tools stay on the desk.
Folding, CSE and Dead Code
Measured: 7 x 24 x 60 x 60 at run time = 6 instructions; folded = 2 (lui + ori).
A shopkeeper knows the price of a dozen eggs by heart - no multiplying for every customer.
Code Motion: Work Out of the Loop
before
after code motion
- loop: beq $t0, 4, done
- mul $t4, $s0, $s1 # every pass
- lw $t3, 0($a0)
- mul $t3, $t3, $t4
- ...
- mul $t4, $s0, $s1 # once
- loop: beq $t0, 4, done
- lw $t3, 0($a0)
- mul $t3, $t3, $t4
- ...
49 → 46
Metro bus card
Buy the monthly card once, not a ticket every morning. For 1000 passes: 999 multiplies saved.
instructions (4 passes)
MARS Instruction Counter
Strength Reduction and Loop Rotation
Strength reduction: i x 4 + base → p = p + 4. Rotation: one branch per pass.
What clang Really Produces (MIPS)
clang -O0: 22 per element
clang -O1: 5 per element
- loop: lw $1, 0($sp)
- lw $2, 8($sp)
- slt $1, $1, $2
- beqz $1, exit
- nop
- j body
- nop
- ... 15 more
- loop: lw $1, 0($4)
- addu $2, $1, $2
- addiu $5, $5, -1
- bnez $5, loop
- addiu $4, $4, 4 # delay slot
Same compiler, same ISA: 132 bytes at -O0, 36 bytes at -O1 - the optimizer matters as much as the ISA.
Module 3: How the ISA Helps
What compiler writers want from architects
Where Variables Live
aliasing
Two keys
- *p = 5;
- x = y + *q; // p == q ? then *q is 5 now
- // so *q must be loaded again
Two people can open the same shop: recount the stock before you trust it.
How an ISA Can Help the Compiler
Plus: ≥ 16 GPRs for register allocation. Compilers mostly use simple instructions (96%, Lecture 9).
Class Activity: Be the Compiler
7 min · pairs
In pairs. Name the optimization, then apply it.
area = 30 x 60 becomes li $t0, 1800. Which optimization?
Loop of 500 passes: a[i] = a[i] + rate x hours - rate and hours never change. Which optimization? Multiplies saved?
1000 elements: version 1 needs 16 instructions per element, version 2 needs 7. Instructions saved? Time saved at CPI 1, 2 GHz?
Rewrite with one addition: x = (a + b) x c; y = (a + b) / d
Common Mistakes
Wrong idea
Correct idea
✗ The ISA alone decides how fast code is.
✓ The compiler decides IC: MIPS -O0 132 B vs -O1 36 B.
✗ -O0 is the 'safest' choice for release code.
✓ -O0 is for debugging; release builds use -O2 (or -Os).
✗ Complex instructions help compilers.
✓ Compilers want simple, regular primitives.
✗ A compiler can always keep a variable in a register.
✓ Not when aliasing is possible (globals, pointers).
Recap Quiz
Answer on paper in 3 minutes, then we check together.
Q1
Name the four main phases of an optimizing compiler.
Q2
What is a basic block?
Q3
Why does register allocation matter most on a load-store machine? Use today's MARS numbers.
Q4
Replacing i x 4 + base by p = p + 4 is called ______.
Q5
Give two ISA features that help a compiler writer.
Homework and Next Lecture
- Read Hennessy and Patterson, Appendix A, section A.8.
- Decode 0x0211402A. Which C comparison does it compute?
- Estimate: n = 1,000,000 elements, 16 / 7 / 5 / 4 instructions per element, CPI 1, 1 GHz. Time of each version? Speedup of 4 over 1?
- Explain with your own example why aliasing stops a compiler from keeping *p in a register.
NEXT LECTURE
Lecture 12: ISA Case Studies
The same C function in MIPS, RISC-V, ARM and x86; fallacies and pitfalls of ISA design; Unit 2 review.