Learning Outcomes
SLO | After this lecture you can… | CLO | Bloom |
|---|---|---|---|
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 |
§1 Inside a Compiler
§1.1 Why architects care about compilers
Slide L11-S06
Almost all software is written in a high-level language and translated by a compiler, so the instructions that actually run are the ones the compiler chooses. In CPU time = instruction count (IC) x CPI x clock cycle time, the compiler controls IC and also affects CPI (through instruction scheduling). That is why an ISA is designed together with its compilers. Real-world: a word-by-word Urdu-to-English translation is long and clumsy; a skilled translator gives a short, clear sentence with the same meaning.
Same C function (array sum), clang 18, MIPS | -O0 | -O1 |
|---|---|---|
Instructions in the function | 33 | 9 |
Code size (bytes) | 132 | 36 |
Loop instructions per element | 22 | 5 |
Memory accesses (lw/sw) per element | 9 | 1 |
sum.c - the C function compiled for every ISA in Lectures 11-12 (clang 18) |
§1.2 Structure of an optimizing compiler
Slide L11-S07
Phase | Job | Depends on |
|---|---|---|
1 Front end | Check the program; translate it into an intermediate representation (IR) - a simple, machine-independent code | the language |
2 High-level optimizations | Loop transformations, procedure inlining (copy a small procedure's body into the caller) | mostly neither |
3 Global optimizer | Local and global optimizations; register allocation | a little on the machine |
4 Code generator | Instruction selection for the target ISA; machine-dependent optimizations | the ISA |
Because the IR in the middle is shared, one compiler family (GCC, LLVM/clang) supports many languages and many ISAs. In this course one C file was compiled by clang for MIPS, RISC-V, ARM and x86 (Lecture 12). Real-world: a NADRA office - token desk, data entry, verification, printing - each counter does one job and passes the file on.
§1.3 Optimization levels and phase ordering
Slide L11-S08
Level | What it does | Used for |
|---|---|---|
-O0 | No optimization: every variable is kept in memory so the debugger can show it | Debugging |
-O1 | Basic, cheap optimizations, especially register allocation | Fast compile, decent code |
-O2 | Nearly all optimizations that do not trade size for speed (scheduling, global CSE, strength reduction) | Normal release builds |
-O3 | Adds aggressive ones that may grow the code (more inlining, loop unrolling, vectorization) | Maximum speed |
-Os | Prefer small code | Phones, microcontrollers |
Phase-ordering problem: optimizations interact - one pass can create or destroy chances for another - so the order of passes matters and no single order is best for every program. Register allocation is done near the end because earlier passes change how many registers are needed.
§2 Optimizations at Work
§2.1 Kinds of optimization
Slide L11-S10
A basic block is straight-line code with one entry (at the top) and one exit (at the bottom): no branch jumps into or out of its middle. Local optimizations work inside one basic block; global optimizations work across branches and loops.
Kind | Works on | Examples |
|---|---|---|
High-level | source or IR | procedure inlining |
Local | one basic block | common subexpression elimination (CSE), constant propagation and folding, stack height reduction |
Global | across branches and loops | global CSE, copy propagation, code motion, induction-variable elimination |
Register allocation | whole procedure | keep the most used variables in registers |
Processor-dependent | the target ISA | strength reduction, pipeline scheduling, shortest branch form |
§2.2 One loop, four versions (measured in MARS)
Slides L11-S11, S14
All four programs add the milk collected at a dairy collection centre on 8 days (120, 135, 128, 140, 132, 125, 138, 142 litres) and print 1060. Instructions per element were found by running each version with 8 and with 4 elements: (count for 8 - count for 4) / 4.
Version | Optimization | Instructions per element | Total, 8 elements | Total, 4 elements |
|---|---|---|---|---|
1 | none: i, n, s and a kept in the stack frame (like -O0) | 16 | 146 | 82 |
2 | register allocation | 7 | 67 | 39 |
3 | + strength reduction (pointer instead of i x 4) | 5 | 52 | 32 |
4 | + loop rotation (test at the bottom, like clang -O1) | 4 | 44 | 28 |
Version 1 (L11_O0.asm, set-up and loop): 9 lw/sw per element |
Version 4 (L11_rotated.asm, set-up and loop): 4 per element |
§2.3 Register allocation
Slide L11-S11
On a load-store machine every operand must be in a register, so keeping variables in registers is the most important optimization. Version 1 needs 16 instructions and 9 memory accesses per element; version 2 needs 7 and only one (the load of a[i]). Compilers allocate registers by graph colouring: each variable is a node; two nodes are joined when both values are needed at the same time (they 'interfere'); registers are colours and joined nodes must get different colours. If there are not enough colours, some variables are spilled (kept in memory). Graph colouring works well with 16 or more general-purpose registers. Real-world: keep the tools you use every minute on your desk, not in the store room.
§2.4 Constant folding and propagation, CSE, dead code
Slide L11-S12
Optimization | Before | After |
|---|---|---|
Constant folding | t = 7 * 24 * 60 * 60 | t = 604800 |
Constant propagation | x = 5; y = x * 2 | x = 5; y = 10 |
Common subexpression elimination | x = a*b + c; y = a*b - d | t = a*b; x = t + c; y = t - d |
Dead code elimination | if (DEBUG) print(...) with DEBUG = 0 | removed |
Measured in MARS (L11_fold.asm): computing 7 x 24 x 60 x 60 at run time took 6 instructions; the folded constant 604800 needs 2 (lui $at, 0x9; ori $a0, $at, 0x3a80), because 604800 does not fit in 16 bits. Real-world: a shopkeeper knows the price of a dozen eggs by heart and does not multiply 12 x price for every customer.
§2.5 Code motion
Slide L11-S13
Loop-invariant code motion moves a calculation whose operands do not change inside the loop to just before the loop. In L11_motion.asm price x qty was multiplied in all 4 passes; after the move the program ran 46 instead of 49 instructions with the same output (14000). For 1000 passes, 999 multiplies are saved. The compiler must prove the operands cannot change inside the loop - with globals and pointers it often cannot (§3.1). Real-world: buy a monthly Metro bus card once instead of a ticket every morning.
§2.6 Strength reduction and loop rotation
Slides L11-S14, S15
Strength reduction replaces an expensive operation by a cheaper one: base + i x 4 (sll + addu every pass) becomes a pointer that adds 4. The induction variable i disappears: 7 → 5 instructions per element (the same 7 vs 5 you measured in Practical 10). Loop rotation puts the test at the bottom: one check before the loop, then one bne per pass: 4 per element. For long arrays version 4 is about 16 / 4 = 4 times faster than version 1 (same CPI).
Real clang 18 output for the same C function (MIPS, -O1) - the complete loop:
clang -O1: 5 instructions per element |
The counter $5 counts down to 0 so that bnez needs no compare. Real MIPS hardware has a branch delay slot - the instruction after a branch always executes - and the compiler fills it with useful work (the pointer increment). MARS turns delay slots off by default; they return in Lecture 15. clang -O0 used 22 instructions per element, 9 of them loads and stores and 4 of them nops.
§3 How the ISA Helps
§3.1 Where variables live
Slide L11-S17
Area | Holds | Register allocation |
|---|---|---|
Stack | local variables, arguments (addressed from $sp / $fp) | easy - the main target |
Global data area | global variables, constants (fixed addresses) | harder - any code may change them |
Heap | objects created at run time, reached through pointers | hard - aliasing |
Aliasing: two different names refer to the same memory location. In *p = 5; x = y + *q; if p and q may point to the same place, the compiler must load *q again after the store; it cannot keep *q in a register. Real-world: when two people have keys to the same shop, you recount the stock before you trust your last count.
§3.2 Guidelines for ISA designers
Slide L11-S18
Guideline (Hennessy and Patterson) | Meaning | Example |
|---|---|---|
Provide regularity | Operations, data types and addressing modes are orthogonal (independent): any operation, any register, any mode | MIPS: 32 equal GPRs |
Provide primitives, not solutions | Simple building blocks; 'high-level' instructions rarely match what a language needs | VAX CALLS does too much |
Simplify trade-offs among alternatives | Make the best choice obvious when there are several ways | Few addressing modes |
Bind compile-time constants | Put known values in the instruction (immediates) | addi, lui + ori |
Also: provide at least 16 general-purpose registers (plus separate floating-point registers) so that register allocation works well. Compilers mostly use simple instructions: ten simple x86 instructions account for 96% of those executed (Lecture 9).
§4 Common Mistakes
Slide L11-S20
Wrong idea | Correct idea |
|---|---|
The ISA alone decides how fast code is. | The compiler decides the instruction count: MIPS -O0 132 bytes vs -O1 36 bytes for the same function. |
-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). |
Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Compiler | A program that translates a high-level language into machine instructions | A translator for a book |
Intermediate representation (IR) | A simple, machine-independent code inside the compiler | Notes in a common language before final translation |
Optimization | A change that makes a program faster or smaller without changing its result | A shorter route to the same shop |
Basic block | Straight-line code with one entry and one exit | A street with no side turnings |
Register allocation | Choosing which variables live in registers | Which tools stay on your desk |
Spill | Keeping a variable in memory because registers ran out | Putting a tool back in the store room |
Loop-invariant | A value that does not change inside a loop | The bus fare for the whole month |
Strength reduction | Replacing an expensive operation by a cheaper one | Adding Rs 50 each time instead of multiplying |
Induction variable | A variable that changes by a fixed step each pass (i, p) | Seat number moving one by one |
Aliasing | Two names for the same memory location | Two keys to the same shop |
Orthogonal | Independent: any choice of one thing works with any choice of another | Any shirt goes with any trousers |
Self-Check Questions
- Why does the compiler matter as much as the ISA for performance? (§1.1)
- Name the four phases of an optimizing compiler and say what each does. (§1.2)
- What does -O0 do and why is it used? (§1.3)
- What is a basic block? Give one local and one global optimization. (§2.1)
- Using the MARS table, what is the speedup of version 4 over version 1 for 1,000,000 elements? (§2.2)
- Explain graph colouring for register allocation in three sentences. (§2.3)
- Apply CSE to: x = (a + b) * c; y = (a + b) / d. (§2.4)
- For a 500-pass loop with a loop-invariant multiply, how many multiplies does code motion save? (§2.5)
- Why can a compiler not keep *q in a register after *p = 5? (§3.1)
- State the four guidelines for helping compiler writers. (§3.2)
Answers: speedup ≈ 16 / 4 = 4; t = a + b; x = t * c; y = t / d; 499.
Further Reading
- Hennessy and Patterson (2012): Appendix A, section A.8 (the role of compilers) and Figure A.19-A.20.
- Patterson and Hennessy, Computer Organization and Design: Chapter 2, section 2.12 (translating and starting a program).