
1
Optimizations…
• intermediate code:
– pros: machine independent, optimization
opportunities which will not need to be
retargeted for another platform
– cons: yet another language
– possible representations:
• trees, three-address code (y := x op z) (or
quadruples), triple, polish notation…
• Optimization seeks to improve a program’s
utilization of some resource
– Execution time (most often)
– Code size
– Network messages sent
– Battery power used, etc.
• Optimization should not alter what the program
computes
– The answer must still be the same
• What to optimize:
– cost/performance argument (opt-s hard to implement,
increase compilation time…)
– code to consider based on number of: occurrences in
code, repetitions of execution, uses of compiled code
Optimization options
• Apply local optimizations in basic blocks
– single entry/exit point
– preserve outcome of computation
– select different representation for sequence in block
•Global optimization – across CFG (trace) of BBs
– harder to prove
– use flow analysis
•Intra-procedural optimizations
Local optimizations
• Algebraic simplification
– Some statements can be deleted:
x := x + 0
x := x * 1
– Some statements can be simplified:
x := x * 0 ⇒x := 0
y := y ** 2 ⇒y := y * y
x := x * 8 ⇒x := x << 3
x := x * 15 ⇒t := x << 4; x := t - x
Constant Folding
• Operations on constants can be computed at
compile time
– (algebraic simplifications)
• In general, if there is a statement
x := y op z
– And yand zare constants
– Then yopzcan be computed at compile time
• Example: x := 2 + 2 ⇒x := 4
• Example: if 2 < 0 jump L can be deleted
• In general –
– values of expressions involving compiler time
constants – replace unary/binary tree with a
constant node with corr. value
– operations involving constants as operand –
replace binary operation subtree with its non-
constant subtree