09 Op Tim Ization
09 Op Tim Ization
Optimization
Marcus Denker
Optimization
Roadmap
Introduction Optimizations in the Back-end The Optimizer SSA Optimizations Advanced Optimizations
Marcus Denker
Optimization
Roadmap
Introduction Optimizations in the Back-end The Optimizer SSA Optimizations Advanced Optimizations
Marcus Denker
Optimization
>
>
Performance: faster execution > Size: smaller executable, smaller memory footprint
Tradeoffs:
Marcus Denker
Optimization
No Magic Bullet!
Opt(Q)?
Marcus Denker
Optimization
No Magic Bullet!
Opt(Q)?
L1 goto L1
Marcus Denker
Optimization
No Magic Bullet!
L1 goto L1
Marcus Denker
Halting problem!
Optimization
>
Rice (1953): For every compiler there is a modified compiler that generates shorter code. Proof: Assume there is a compiler U that generates the shortest optimized program Opt(P) for all P.
Assume P to be a program that does not stop and has no output Opt(P) will be L1 goto L1 Halting problem. Thus: U does not exist.
>
>
Marcus Denker
Optimization
>
Marcus Denker
Optimization
Roadmap
Introduction Optimizations in the Back-end The Optimizer SSA Optimizations Advanced Optimizations
Marcus Denker
10
Optimization
Marcus Denker
11
Optimization
Register Allocation
>
Temporary variables
non-overlapping temporaries can share one register
Passing arguments via registers Optimizing register allocation very important for good performance
Especially on x86
Marcus Denker
12
Optimization
Instruction Selection
>
For every expression, there are many ways to realize them for a processor
Example: Multiplication*2 can be done by bit-shift
>
Marcus Denker
13
Optimization
Peephole Optimization
store R,a;
imul 2,R;
ashl 2,R;
Optimization
Marcus Denker
15
Optimization
Different levels of IR
> >
>
Marcus Denker
16
Optimization
Roadmap
Introduction Optimizations in the Back-end The Optimizer SSA Optimizations Advanced Optimizations
Marcus Denker
17
Optimization
Constant Folding / Propagation Copy Propagation Algebraic Simplifications Strength Reduction Dead Code Elimination
Structure Simplifications
Marcus Denker
18
Optimization
Constant Folding
Evaluate constant expressions at compile time > Only possible when side-effect freeness guaranteed
>
c:= 4 false
Optimization
Constant Propagation
>
b := 3 c := 1 + b d := b + c
b := 3 c := 1 + 3 d := 3 + c
Marcus Denker
20
Optimization
Copy Propagation
for a statement x := y > replace later uses of x with y, if x and y have not been changed.
>
x := y c := 1 + x d := x + c
x := y c := 1 + y d := y + c
Optimization
Algebraic Simplifications
>
i true
Marcus Denker
22
Optimization
Strength Reduction
Replace expensive operations with simpler ones > Example: Multiplications replaced by additions
>
y := x * 2
y := x + x
Marcus Denker
23
Optimization
Dead Code
>
b := 3 c := 1 + 3 d := 3 + c
>
c := 1 + 3 d := 3 + c
if (false) {a := 5}
Marcus Denker
if (false) {}
24
Optimization
Simplify Structure
>
> >
Marcus Denker
25
Optimization
Marcus Denker
26
Optimization
Marcus Denker
27
Optimization
Common Subexpression: - There is another occurrence of the expression whose evaluation always precedes this one - operands remain unchanged
Local (inside one basic block): When building IR Global (complete flow-graph)
Marcus Denker
28
Optimization
Example CSE
b := a + 2 c := 4 * b b < c? b := 1
t1 := a + 2 b := t1 c := 4 * b b < c?
b := 1
d := a + 2
d := t1
Marcus Denker
29
Optimization
Loop Optimizations
>
> >
All optimizations help when applied to loop-bodies Some optimizations are loop specific
Marcus Denker
30
Optimization
>
Marcus Denker
31
Optimization
>
Optimization
>
>
Partial Redundancy: computation done more than once on some path in the flow-graph PRE: insert and delete code to minimize redundancy.
>
Marcus Denker
33
Optimization
Code Inlining
> >
All optimization up to know where local to one procedure Problem: procedures or functions are very short
Especially in good OO code!
>
Marcus Denker
34
Optimization
Example: Inlining
a := power2(b)
a := b * b
Marcus Denker
35
Optimization
Roadmap
Introduction Optimizations in the Back-end The Optimizer SSA Optimizations Advanced Optimizations
Marcus Denker
36
Optimization
Repeat: SSA
>
>
Marcus Denker
37
Optimization
Properties
>
> >
Marcus Denker
38
Optimization
>
Marcus Denker
39
Optimization
>
b := 3 c := 1 + b d := b + c
b := 3 c := 1 + 3 d := 3 + c
Marcus Denker
40
Optimization
Variables are assigned once > We know that we can replace all uses by the constant!
>
b1 := 3 c1 := 1 + b1 d1 := b1 + c1
b1 := 3 c1 := 1 + 3 d1 := 3 + c
Marcus Denker
41
Optimization
for a statement x := y > replace later uses of x with y, if x and y have not been changed.
>
x := y c := 1 + y d := y + c
x := y c := 1 + y d := y + c
Optimization
x1 := y1 c1 := 1 + x1 d1 := x1 + c1
x1 := y1 c1 := 1 + y1 d1 := y1 + c1
Marcus Denker
43
Optimization
> >
Variable is live if the list of uses is not empty. Dead definitions can be deleted
(If there is no side-effect)
Marcus Denker
44
Optimization
Roadmap
Introduction Optimizations in the Back-end The Optimizer SSA Optimizations Advanced Optimizations
Marcus Denker
45
Optimization
Advanced Optimizations
>
>
Inter-procedural optimizations
Global view, not just one procedure
Optimization
Iterative Process
There is no general right order of optimizations > One optimization generates new opportunities for a preceding one. > Optimization is an iterative process
>
Marcus Denker
47
Optimization
Introduction Optimizations in the Back-end The Optimizer SSA Optimizations Advanced Optimizations
Marcus Denker
48
Optimization
Literature
>
>
Marcus Denker
49
Optimization
Why do we optimize programs? Is there an optimal optimizer? Where in a compiler does optimization happen? Can you explain constant propagation?
Marcus Denker
50
Optimization
What makes SSA suitable for optimization? When is a definition of a variable live in SSA Form? Why dont we just optimize on the AST? Why do we need to optimize IR on different levels? In which order do we run the different optimizations?
Marcus Denker
51
Optimization
License
>
http://creativecommons.org/licenses/by-sa/2.5/
Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor.
Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above.
Marcus Denker 52