0% found this document useful (0 votes)
17 views

Lec07 Code Optimzation

Code optimisation ppt

Uploaded by

Cherish Mahajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lec07 Code Optimzation

Code optimisation ppt

Uploaded by

Cherish Mahajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CODE OPTIMIZATION

5TH PHASE OF COMPILER CONSTRUCTION

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

If the value of a variable is a constant, then replace the variable by the


constant
➢ It is not the constant definition, but a variable is assigned to a constant
➢ The variable may not always be a constant
Example:
pi := 3.14286
area = pi * r ** 2
area = 3.14286 * r ** 2
Example:
N := 10; C := 2;
for (i:=0; i<N; i++) { s := s + i*C; }
for (i:=0; i<10; i++) { s := s + i*2; }

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

Propagation: only substitute a variable by its assigned constant


Folding: Consider variables whose values can be computed at compilation time
4
and controls whose decision can be determined at compilation time
ALGEBRAIC SIMPLIFICATION AND STRENGTH
REDUCTION
More general form of constant folding, e.g.,
x + 0 =x x –0 = x
x / 1 =x x * 1=x
x * 0 =0
Repeatedly apply the rules
(y * 1 + 0) / 1 = y

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)

May not appear to be code improvement, but opens up


scope for other optimizations
6
COMMON SUBEXPRESSION ELIMINATION
Identify common sub-expression present in different expression,
compute once, and use the result in all the places.

The definition of the variables involved should not change


a := b * c temp := b * c
… a := temp
… …
x := b * c + 5 x := temp + 5

7
COMMON SUBEXPRESSION ELIMINATION

 Local common subexpression elimination


 Performed within basic blocks
 Algorithm sketch:
’ Traverse Block from top to bottom
’ Maintain table of expressions evaluated so far
 if any operand of the expression is redefined, remove it from the
table
’ Modify applicable instructions as you go
 generate temporary variable, store the expression in it and use the
variable next time the expression is encountered

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

Global common subexpression elimination

◦Performed on flow graph


◦Requires available expression information

In addition to finding what expressions are available at the endpoints of


basic blocks, we need to know where each of those expressions was
most recently evaluated (which block and which position within that
block).

10
COMMON SUBEXPRESSION ELIMINATION

1 x:=a+b

2 a:= b 3

“a + b” is not a common sub- expression in 1 and 4

z : = a + b + 10 4

None of the variable involved should be modified in any path

11
UNREACHEABLE CODE ELIMINATION

 Construct the control flow graph

 Unreachable code block will not have an incoming


edge
 After constant propagation/folding, unreachable
branches can be eliminated

12
DEAD CODE ELIMINATION

 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

Ineffective statements
x := y + 1 (immediately redefined, eliminate!)
y := 5 → y := 5
x := 2 * z x := 2 * z

13
LOOP OPTIMIZATION

Consumes 90% of the execution time


 a larger payoff to optimize the code within a loop
Techniques
➢ Loop invariant detection and code motion
➢ Induction variable elimination
➢ Strength reduction in loops
➢ Loop unrolling
➢ Loop fusion

14
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 can be moved
to outside of the loop
◦ Example
for (i=0; i<n; i++)
{a[i] := a[i] + x/y;}

c := x/y;
for (i=0; i<n; i++)
{ a[i] := a[i] + c; }
15
CODE MOTION

 Identify invariant expression:


for(i=0; i<n; i++)
a[i] = a[i] + (x*x)/(y*y);
 Hoist the expression out of the loop:

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

for (i = 0; i< n; i++) { S }

◦ Unroll loop four times:

for (i = 0; i < n-3; i+=4) { S; S; S; S; }


for ( ; i < n; i++) S;

◦ Get rid of the conditional branches, if possible


◦ Allow optimization to cross multiple iterations of the loop
Especially for parallel instruction execution
◦ Space time tradeoff
Increase in code size, reduce some instructions 20
LOOP FUSION

Before Loop Fusion After Loop Fusion


Example
for i=1 to N do for i=1 to N do
A[i] = B[i] + 1 A[i] = B[i] + 1
Endfor C[i] = A[i] / 2
for i=1 to N do D[i] = 1 / C[i+1]
C[i] = A[i] / 2 endfor
Cannot
endfor fuse the
for i=1 to N do third
loop
D[i] = 1 / C[i+1]
endfor 21
FUNCTION INLINING
Replace a function call with the body of the function
Save a lot of copying of the parameters, return address, etc.
In the code fragment below, the function add() can be expanded inline at the call site in
the function sub().
int add (int x, int y) Expanding add() at the call site in sub() yields
{
return x + y; int add (int x, int y)
}
int sub (int x, int y) {
{ return x + -y
return add (x, -y); }
} Can be further optimized
int add (int x, int y)
{

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

 Create specialized code for a function for different calling


parameters
void f(int x[], int n, int m) {for(int i=0; i
For a call f(a, 10, 1), create a specialized version of f:
void f1(int x[])
{ for(int i=0; i<n;i++) { x[i] = x[i] + i*m; } }
• For another call f(b, p, 0), create another version f2(…)

Can be useful for parallel processing

23

You might also like