Documente Academic
Documente Profesional
Documente Cultură
Optimizations
p
reduce the number of instructions
reduce the cost of instructions
reduce the number of times an instruction is executed
Code Optimization
Why
Reduce pprogrammers
g
burden
Allow programmers to concentrate on high level concept
y g about pperformance issues
Without worrying
Target
Reduce execution time
Reduce space
Sometimes these are tradeoffs
Sometimes,
Types
Intermediate code level
We are looking at this part now
Assembly level
Instruction selection, register allocation, etc.
Code Optimization
Architecture Independent:
Common Sub expression Elimination
p g
Constant Propagation
Dead Code Elimination
Architecture Dependent:
Instruction selection
Register allocation
Control
flow
analysis
Data flow
analysis
y
Transformation
Code
optimizer
7
Classifications of Optimization
techniques
Peephole optimizations
Local Optimizations (straight-line code (basic block))
p
Global Optimizations
Intra-procedural (Consider entire procedures - data flow
analysis, control flow analysis )
Inter-procedural (Consider entire programs - problems
of parameter passing, aliasing, calling scope )
Loop Optimizations
Avoid redundancy:
y somethingg alreadyy computed
p
need
not be computed again
E
Extract
more information
i f
i about
b
code:
d More info
better code generation
Redundancy elimination
Redundancy elimination = determining that two
computations
p
are equivalent
q
and eliminating
g one.
There are several types of redundancy elimination:
Value numbering
Associates symbolic values to computations and identifies expressions
that have the same value
Constant/Copy propagation
Identifies variables that have constant/copy values and uses the
constants/copies in place of the variables.
variables
Code Optimization
Techniques
C
Constant
propagation
i
Constant folding
Algebraic simplification, strength reduction
Copy propagation
Common subexpression elimination
Unreacheable code elimination
Dead code elimination
Loop Optimization
Function related
Function inlining, function cloning
Most of the optimization goals are identified through
experience, but techniques requires theoretical foundation
12
Example:
area := (22.0/7.0) * r ** 2
area := 3.14286 * r ** 2
13
14
Constant Propagation
p g
What does it mean?
Given an assignment x = c,
c where c is a constant,
constant replace
later uses of x with uses of c, provided there are no
interveningg assignments
g
to x.
When is it performed?
Early in the optimization process.
What is the result?
Smaller code
Fewer registers
15
x := b * c + 5
temp := b * c
a := temp
x := temp + 5
17
Thi is
This
i a basic
b i block
bl k
t1 = a*a
t2 = a*b
t3 = 2*t2
t4 = t1+t3
t5 = b*b
t6 = t4+ t5
B1
(3) t1=4*I
---------------------------------(11) I = t7
(12) If I <= 20 goto (3)
B2
B1
i=1
t1=4*I
---------------------------------I = t7
If I <= 20 goto B2
B2
the end.
Basic Block
Partitioning Algorithm
1. Identify leader statements (i.e. the first statements
of basic blocks) by using the following rules:
(i) The first statement in the program is a leader
(ii) Any statement that
h iis the
h target off a b
branch
h
statement is a leader (for most intermediate
languages these are statements with
an associated
i t d label)
l b l)
(iii) Any statement that immediately follows a branch
or return statement is a leader
(AhoSethiUllman)
(1) prod := 0
(2) i := 1
(3) t1 := 4 * i
((4)) t2 := a[t1]
[ ]
(5) t3 := 4 * i
(6) t4 := b[t3]
(7) t5 := t2 * t4
(8) t6 := prod + t5
(9) prod := t6
(10) t7 := i + 1
(11) i := t7
(12) if i <= 20 goto (3)
Three-address code
(AhoSethiUllman)
Basic Blocks:
B2 (3) t1 := 4 * i
(4) t2 := a[t1]
(5) t3 := 4 * i
(6) t4 := b[t3]
(7) t5 := t2 * t4
(8) t6 := prod + t5
(9) prod := t6
((10)) t7 := i + 1
(11) i := t7
(12) if i <= 20 goto (3)
B3 (13)
B1
B2
B3
R l (2)
Rule
B2 (3) t1 := 4 * i
((4)) t2 := a[t1]
[ ]
(5) t3 := 4 * i
(6) t4 := b[t3]
(7) t5 ::= t2 * t4
(8) t6 := prod + t5
(9) prod := t6
(10) t7 := i + 1
(11) i := t7
(12) if i <= 20 goto (3)
B3 (13)
B1
B2
B3
R l (2)
Rule
B2 (3) t1 := 4 * i
((4)) t2 := a[t1]
[ ]
(5) t3 := 4 * i
(6) t4 := b[t3]
(7) t5 ::= t2 * t4
(8) t6 := prod + t5
(9) prod := t6
(10) t7 := i + 1
(11) i := t7
(12) if i <= 20 goto (3)
B3 (13)
B1
B2
B3
R l (2)
Rule
B2 (3) t1 := 4 * i
((4)) t2 := a[t1]
[ ]
(5) t3 := 4 * i
(6) t4 := b[t3]
(7) t5 ::= t2 * t4
(8) t6 := prod + t5
(9) prod := t6
(10) t7 := i + 1
(11) i := t7
(12) if i <= 20 goto (3)
Rule (2)
B3 (13)
c := a
d := b + c
d := b + c
Example in array index calculations
c[i+1]
[ ] := a[i+1]
[ ] + b[i+1]
[ ]
During address computation, i+1 should be reused
Not visible in high level code, but in intermediate
code
Local &
Global
Optimization
x = a + b
...
y = a + b
t = a + b
x = t
...
y = t
46
t1 = a + b
c = t1
t2 = m * n
d = t2
t3 = b + d
e = t3
f = t1
1
g = -b
h = t1 /* commutative */
a = j + a
k = t2
j = t3
a = -b
b
if t2 go to L
47
48
Common Sub-expression
Evaluation
1
x:=a+b
a:= b
z : = a + b + 10
a + b is not a
common sub-expression
in 1 and 4
49
y := 5
x := 2 * z
x := 2 * z
A variable is dead if it is never used after last definition
Eliminate assignments to dead variables
Need to do data flow analysis to find dead variables
Code Motion
Moving code from one part of the program to other
without modifying the algorithm
Reduce size of the program
Reduce execution frequency of the code subjected to
movement
52
Code Motion
1. Code Space
p
reduction: Similar to common subexpression elimination but with the objective to
reduce code size.
Example: Code hoisting
if (a< b) then
z := x ** 2
else
y := x ** 2 + 10
temp : = x ** 2
if (a< b) then
z := temp
else
y := temp + 10
x ** 2 is computed once in both cases, but the code size in the second
case reduces.
53
Code Motion
Loop invariant detection and code motion
If the result of a statement or expression does not
change within a loop, and it has no external side-effect
Computation
p
can be moved to the outside of the loopp
Example 1:
for (i=0;
(i 0; ii<n;
n; ii++)) a[i] ::= a[i] + x/y;
for (i=0; i<n; i++) { c := x/y; a[i] := a[i] + c; } //
three address code
c := x/y; for (i=0; i<n; i++) a[i] := a[i] + c;
Example 2 :
while ( i < (max-2) )
Equivalent to:
t := max - 2
while ( i < t )
54
Code Motion
Safety of Code movement
Movement of an expression e from a basic block bi to
another block bj, is safe if it does not introduce any new
occurrence of e along any path.
55
Strength Reduction
57
i=1;
while(i<=100)
{a[i]=0;
++i;
a[i]=0;
++i;
}
Loop fusion
Example
for i=1 to N do
A[i] = B[i] + 1
endfor
for i=1 to N do
C[i] = A[i] / 2
Endfor
After Loop Fusion
for i=1 to N do
A[i] = B[i] + 1
C[i] = A[i] / 2
End for
for(i=1;i<=10;++i)
f (j
for
(j=1;j<=10;++j)
1 j 10 j)
{ a[i][j]=0;
a[i][i]=1;
a[i][i]
1; }
Strength reduction
Replace expensive operations
Replace Multiply by Shift
A := A * 4;
Can be replaced by 2-bit left shift (signed/unsigned)
E g , x ::= x * 8 x ::= x << 3
E.g.,
But must worry about overflow if language does
A := A / 44;
If unsigned, can replace with shift right
E.g., x := x * 8 x := x << 3
Language may allow it anyway (traditional C)
Code Optimization
Peephole and local analysis generally involve
Constant foldingg
Algebraic simplification, strength reduction
Global analysis
y ggenerallyy requires
q
Control flow graph
Data flow analysis
y
Loop optimization