



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS430 2
Last lecture
This lecture
CS430 3
A compiler is a lot of fast stuff followed by some hard problems
→ The hard stuff is mostly in code generation and optimization → For superscalars, allocation & scheduling is particularly important
Instruction Selection
Register Allocation
Instruction Scheduling
Scanner Parser
Analysis & Optimization
O(n) O(n) O(n), O(n log n), … NP-Complete
Either fast or NP-Complete NP-Complete NP-Complete
words IR
IR asm asm asm ∞ regs
∞ regs
k regs
CS430 4
We assume the following model
What about the IR?
Branches, compares, & labels Memory tags Provision for multiple ops/cycle
Instruction Selection
Instruction Scheduling
Register Allocation
Analysis & Optimization
IR^ asm^ asm
∞ regs
regs
regs
regs
k regs
IR^ asm
CS430 5
Instruction selection
Instruction scheduling
Register allocation
These 3 problems are tightly coupled.
CS430 6
How hard are these problems?
Instruction selection
Instruction scheduling
Register allocation
CS430 7
Conventional wisdom says that we lose little by solving these problems independently
Instruction selection
Instruction scheduling
Register allocation
Optimal for
85% of blocks
Note: many fuzzy terms here!
CS430 8
Definition
Impact
Rule of thumb: expose as much derived information as possible
CS430 9
An example
The “best” shape for x+y+z depends on contextual knowledge → There may be several conflicting options
x + y + z x + y → t t1+ z → t
x + z → t t1+ y → t
y + z → t t1+ z → t
x y z
z
x y
y
x z
x
y z
Addition is commutative & associative for integers
source level intermediate level
CS430 10
Another example -- the case statement
Compiler must choose best implementation strategy No amount of massaging or transforming will convert one into another
CS430 11
→ Explicit loads and stores (to registers) → Explicit references to registers Either virtual or physical registers
→ load r1,
The key code quality issue is holding values in registers
Encoding this knowledge into the IR
Relies on a strong register allocator (register-register model)
CS430 19
Adding other operators
Mixed-type expressions
Typical Addition Table
CS430 20
What about evaluation order?
What about order of evaluating operands?
Taken to its logical conclusion, this creates Sethi-Ullman register allocation scheme (ASU p. 564)
CS430 21
Need to generate an initial IR form
The big picture
→ Actions on non-leaves occur after children are done
→ Identifiers load themselves & stack virtual register name → Operators emit appropriate code & stack resulting VR name → Assignment requires evaluation to an lvalue or an rvalue
CS430 22
Goal : Expr { $$ = $1; } ; Expr: Expr PLUS Term { t = NextRegister(); emit(add,$1,$3,t); $$ = t; } | Expr MINUS Term {…} | Term { $$ = $1; } ; Term: Term TIMES Factor { t = NextRegister(); emit(mult,$1,$3,t); $$ = t; }; | Term DIVIDES Factor {…} | Factor { $$ = $1; }; Factor: NUMBER { t = NextRegister(); emit(loadI,val($1),none, t ); $$ = t; } | ID { t1 = base($1); t2 = offset($1); t = NextRegister(); emit(loadAO,t1,t2,t); $$ = t; }
expr(node) { int result, t1, t2; switch ( type(node)) { case ×,÷,+,− : t1← expr( left child(node)); t2← expr( right child(node)); result ← NextRegister(); emit ( op(node), t1, t2, result); break; case IDENTIFIER: t1← base(node); t2← offset(node); result ← NextRegister(); emit (loadAO, t1, t2, result); break; case NUMBER: result ← NextRegister(); emit (loadI, val(node), none, result); break; } return result; }
CS430 23
lhs ← rhs
Strategy
→ lvalue is a register ⇒ move rhs ( register/register copy) → lvalue is an address ⇒ store rhs
→ Evaluate rvalue to its “ natural” type → Convert that value to the type of * lvalue
Unambiguous scalars go into registers
Ambiguous scalars or aggregates go into memory
CS430 24
What if the compiler cannot determine the rhs’s type?
Code for assignment becomes more complex
evaluate rhsevaluate rhsevaluate rhsevaluate rhs if type(lhs)if type(lhs)if type(lhs)if type(lhs) ≠≠≠≠ rhs.tagrhs.tagrhs.tagrhs.tag thenthenthenthen convert rhs to type(lhs)convert rhs to type(lhs)convert rhs to type(lhs)convert rhs to type(lhs) orororor signal a runsignal a runsignal a runsignal a run----time errortime errortime errortime error lhslhslhslhs ←←←← rhsrhsrhsrhs
This is much more complex than if it knew the types
CS430 25
Compile-time type-checking
needed
Optimization strategy
( assumes target machine with instruction-level parallelism)
CS430 26
The problem with reference counting
This adds 1 +, 1 -, 2 loads, & 2 stores
With extra functional units & large caches, this may become either cheap or free. What about power consumption?
evaluate rhsevaluate rhsevaluate rhsevaluate rhs lhslhslhslhs→→→→countcountcountcount ←←←← lhslhslhslhs→→→→countcountcountcount --- 1- 111 lhslhslhslhs ←←←← addr(rhs)addr(rhs)addr(rhs)addr(rhs) rhsrhsrhsrhs→→→→countcountcountcount ←←←← rhsrhsrhsrhs→→→→count + 1count + 1count + 1count + 1
Plus a check for zero at the end