Beruflich Dokumente
Kultur Dokumente
Code Optimization
Why
Reduce programmers burden
Allow programmers to concentrate on high level concept
Without worrying about performance issues
Target
Reduce execution time
Reduce space
Sometimes, these are tradeoffs
Types
Intermediate code level
We are looking at this part now
Code Optimization
Scope
Peephole analysis
Within one or a few instructions
Local analysis
Within a basic block
Global analysis
Entire procedure or within a certain scope
Inter-procedural analysis
Beyond a procedure, consider the entire program
Code Optimization
Techniques
Constant propagation
Constant folding
Algebraic simplification, strength reduction
Copy propagation
Common subexpression elimination
Unreachable code elimination
Dead code elimination
Loop Optimization
Function related
Function inlining, function cloning
E.g.
N := 10; C := 2;
for (i:=0; i<N; i++) { s := s + i*C; }
for (i:=0; i<10; i++) { s := s + i*2; }
If (C) go to go to
The other branch, if any, can be eliminated by other optimizations
Requirement:
After a constant assignment to the variable
Until next assignment of the variable
Perform data flow analysis to determine the propagation
x0x
x/1x
Strength reduction
Replace expensive operations
E.g., x := x * 8 x := x << 3
s := y * f(y)
a := b + c
c := a
d := b + c
Function cloning
Create specialized code for a function for different calling
parameters
Techniques
Loop invariant detection and code motion
Induction variable elimination
Strength reduction in loops
Loop unrolling
Loop peeling
Loop fusion
Loop peeling
Similar to unrolling
But unroll the first and/or last few iterations
for i=1 to N do
A[i] = B[i] + 1
C[i] = A[i] / 2
D[i] = 1 / C[i+1]
endfor
Is this correct?
Actually, cannot fuse
the third loop
Code Optimization
Will cover a subset of the techniques
Control flow graph
To facilitate data flow analysis for optimization
To facilitate loop identification
Loop optimization
Function optimization
Alias analysis (pointers)
Depend on the time, some topics may be skipped