Lec07 Code Optimzation
Lec07 Code Optimzation
1
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 2
CONSTANT PROPAGATION
Requirement:
After a constant assignment to the variable
Until next assignment of the variable 3
Perform data flow analysis to determine the propagation
CONSTANT FOLDING
Evaluation of an expression with constant operands to replace the expression with single value
Example:
area := (22.0/7.0) * r ** 2 area := 3.14286 * r ** 2
Example:
#define M 10
x := 2 * M; x := 20
If (M < 0) goto L can be eliminated
y := 10 * 5
y := 50
Difference between Constant Propagation and Folding
Strength reduction
Replace expensive operations
A^2 =A*A
5
COPY PROPAGATION
Given an assignment x = y, replace later uses of x with uses of
y, provided there are no intervening assignments to x or y.
➢ If y is reassigned in between, then this action cannot be performed
Example:
x[i] = a; x[i] = a;
sum = x[i] + a; sum = a + a;
Example
x := y; s : = x * f(x)
s : = y * f(y)
7
COMMON SUBEXPRESSION ELIMINATION
t = a + b
x = a +b x = t
... ... 8
y = a +b y = t
COMMON SUBEXPRESSION ELIMINATION
cc = a + b t1 = a + b
d = m *n c = t1
e = b + d t2 = m * n
f = a + b d = t2
g = - b t3 = b + d
h = b + a e = t3
a = j + a f = t1
k = m *n g = -b
j = b + d h = t1 /a /* commutative */ = j +
aa = - b a = j + a
if m * n go to L k = t2
j = t3
a = -b
if t2 go to L
9
COMMON SUBEXPRESSION ELIMINATION
10
COMMON SUBEXPRESSION ELIMINATION
1 x:=a+b
2 a:= b 3
z : = a + b + 10 4
11
UNREACHEABLE CODE ELIMINATION
12
DEAD CODE ELIMINATION
Ineffective statements
x := y + 1 (immediately redefined, eliminate!)
y := 5 → y := 5
x := 2 * z x := 2 * z
13
LOOP OPTIMIZATION
14
LOOP INVARIANT DETECTION AND CODE MOTION
c := x/y;
for (i=0; i<n; i++)
{ a[i] := a[i] + c; }
15
CODE MOTION
c = (x*x)/(y*y);
for(i=0; i<n; i++)
a[i] = a[i] + c;
16
STRENGTH REDUCTION IN LOOPS
◦ Example
s := 0;
for (i=0; i<n; i++) { v := 4 * i; s := s + v; )
s := 0;
for (i=0; i<n; i++) { v := v + 4; s := s + v; )
17
INDUCTION VARIABLE ELIMINATION
The code fragment below has three induction variables (i1, i2, and i3)
that can be replaced with one induction variable, thus eliminating two
induction variables.
int a[SIZE];
int b[SIZE]; void f (void)
{
int i1, i2, i3;
for (i1 = 0, i2 = 0, i3 = 0; i1 < SIZE; i1++)
a[i2++] = b[i3++];
return;
18
}
INDUCTION VARIABLE ELIMINATION
The code fragment below shows the loop after induction variable elimination.
int a[SIZE];
int a[SIZE];
int b[SIZE]; int b[SIZE]; void f (void)
{
void f (void) int i1, i2, i3;
{ for (i1 = 0, i2 = 0, i3 = 0; i1 < SIZE;
i1++)
int i1;
a[i2++] = b[i3++];
return;
for (i1 = 0; i1 < SIZE; i1 ++) }
a[i1] = b[i1];
return;
} 19
LOOP UNROLLING
◦ Execute loop body multiple times at each iteration
return x-y
}
Function inlining usually increases code space, which is affected by the size of the22
inlined function, the number of call sites that are inlined, and the opportunities for
additional optimizations after inlining.
FUNCTION CLONING
23