Session 21
Session 21
Session 21
COURSE CODE:21CS3204R
The compiler optimization must preserve the semantics of the original program .
Optimization is classified broadly into two types:
1.Machine-Independent code optimization.
2. Machine-Dependent code optimization.
2
1.Machine-Independent code optimization.
• Machine independent optimization attempts to improve the intermediate code to get a
better target code. The part of the code which is transformed here does not involve any
absolute memory location or any CPU registers.
• Machine independent code optimization can be achieved using the following methods:
3
Function Preserving Optimization :
• Function Preserving optimizations deals with the code in a given function in an attempt of
reducing the computational time. It can be achieved by following methods:
1. Common Sub expression elimination
2. Compile Time Evaluation
3. Dead code elimination
4. Copy Propagation
5. Strength Reduction
6. Code movement /code motion
4
1. Common Sub expression elimination/Global common sub expression elimination:
Common Sub Expression is an expression which appears repeatedly in the program, which is
computed previously but the values of variables in the expression have not changed.
ex1: Un optimized code optimized code
a=b+c a=b+c
b=a-d b=a-d
c=b+c c=b+c
d=a-d
c=a-d
5
EX:2
Before elimination After Elimination
a = 10; a=10;
b = a + 1 * 2; b=a+1*2;
c = a + 1 * 2; d=b+a;
//’c’ has common expression as ‘b’
d = c + a;
6
Ex3:
Before elimination
x = 11;
y = 11 * 24;
z = x * 24;
//'z' has common expression as 'y' as 'x' can be evaluated directly as done in 'y'.
After elimination –
y = 11 * 24;
7
2. Compile Time Evaluation:
The evaluation done at compile time .
Compile time evaluation done in 2-ways
1. constant folding
2. constant propagation
8
1. constant folding:
Ex1: area= {22/7}*r*r;
Here 22/7 is calculated and result 3.14 is replaced
area=3.14*r*r;
Ex 2:
#define k 5
x = 2 * k
y = k + 5
This can be computed at compile time and the values of x and y
are :
x = 10
y = 10
9
2.Constant Propagation:
Here the constant replaces a variable.
f the value of a variable is a constant, then replace the variable with the constant. The variable may
not always be a constant.
Ex 1: pi=3.14, r=5
area=pi*r*r;
Ex 2:
x = 12.4
y = x/2.3
Evaluates x/2.3 as 12.4/2.3 at compile time.
10
3. Dead code elimination
It eliminates those statements which are never executed or if executed its output can
never be used.
Ex 1: Un optimized code optimized code
i=0; i=0;
if(i=1)
{
a=i+1;
}
11
Ex 2:
c = a * b
x = a
till
d = a * b + 4
//After elimination :
c = a * b
till
d = a * b + 4
12
Ex 3:
un optimized code optimized code
int add(int x, int y) int add(int x, int y)
{ {
int z; int z;
z=x+y; z=x+y;
Return z; return z;
Printf(“%d”,z)’ }
}
13
4. Copy Propagation
copy propagation is the process of replacing the occurrences of targets of direct assignments
with their values. A direct assignment is an instruction of the form x = y , which simply assigns
the value of y to x .
Ex:
Consider the following pseudocode:
int x = 14;
int y = 7 - x / 2;
return y * (28 / x + 2);
14
5. Strength Reduction
Replace expensive statement/ instruction with cheaper ones.
Ex1: b=a*2
b=b+2
Ex 2: x = 2 * y (costly) ⇒ x = y + y (cheaper)
x = 2 * y (costly) ⇒ x = y << 1 (cheaper)
15
6. Code movement /code motion
It is a process that moves code from inner to outer the loop without changing the
functionality.
Ex1: un optimized code optimized code
for(i=0;i<n;i++) x=y+z;
{ for(i=0;i<n;i++)
x=y+z; {
a[i]=6*i; a[i]=6*I;
} }
16
2.Machine-Dependent code optimization
17
Loop Optimization Techniques:
1. Code Motion or Frequency Reduction:
• The evaluation frequency of expression is reduced.
• The loop invariant statements are brought out of the loop.
18
a = 200;
while(a>0)
{
b = x + y;
if (a % b == 0)
printf(“%d”, a);
}
19
//This code can be further optimized as
a = 200;
b = x + y;
while(a>0)
{
if (a % b == 0}
printf(“%d”, a);
}
20
// Before loop jamming
for(int k=0;k<10;k++)
{
x = k*2;
}
for(int k=0;k<10;k++)
{
y = k+3;
}
21
3. Loop Unrolling:
•Loop unrolling is a technique used to increase the number of instructions executed between
executions of the loop branch logic. This reduces the number of times the loop branch logic is
executed.
•It helps in optimizing the execution time of the program by reducing the iterations.
•It increases the program’s speed by eliminating the loop control and test instructions.
EX 1:
//Before Loop Unrolling
for(int i=0;i<2;i++)
{
printf("Hello");
}
22
//After Loop Unrolling
printf("Hello");
printf("Hello");
23
Thank you
24