0% found this document useful (0 votes)
4 views19 pages

Unit 5 - 2

The document discusses various techniques for code optimization, focusing on machine-independent optimization methods such as compile-time evaluation, common sub-expression elimination, and dead code elimination. It also highlights loop optimization strategies including code motion, loop unrolling, and loop fusion. Overall, the goal of these optimizations is to improve the efficiency of intermediate code generation and execution.

Uploaded by

pandeyakshay301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

Unit 5 - 2

The document discusses various techniques for code optimization, focusing on machine-independent optimization methods such as compile-time evaluation, common sub-expression elimination, and dead code elimination. It also highlights loop optimization strategies including code motion, loop unrolling, and loop fusion. Overall, the goal of these optimizations is to improve the efficiency of intermediate code generation and execution.

Uploaded by

pandeyakshay301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Unit 5(2)

Code Optimization
Machine-independent optimization
Loop optimization
DAG representation of basic blocks
Value numbers and algebraic laws,
Global data-flow analysis
Machine-Independent Optimization
• The process of intermediate code generation introduces much
inefficiency like:
• using variable instead of constants,
• extra copies of variable,
• repeated evaluation of expression.
• Use to improve the intermediate code to get a better target code.
• Transformed code does not involve any absolute memory location or
any CPU registers.
• It can change the structure of program sometimes of beyond
recognition like:
• unrolls loops,
• inline functions,
• eliminates some variables.
Transformations
• The optimization can be done locally or globally.
• If the transformation is applied on the same basic block then
• The kind of transformation is done locally otherwise
• The transformation is done globally
Types of code optimization
1. Compile Time Evaluation
2. Common Sub-expression Elimination
3. Variable Propagation
4. Code Movement
5. Strength Reduction
6. Dead Code Elimination
1. Compile Time Evaluation
• Compile time evaluation means shifting of
computations from run time to compilation time.
• There are two methods used to obtain the compile
time evaluation:
1) Folding
2) Constant Propagation
1) Folding
• In this method, the computation of constant is done at compile time
instead of execution time
• Example:
length=(22/7)*d
# Folding is implied by performing the computation of 22/7 at
compile time instead of execution time
2) Constant Propagation
• Value of variable is replaced and computation of an expression is
done at compilation time
• Example:
pi=3.14;
r=5;
Area=pi*r*r

->>> Area=3.14*r*r
2. Common Sub-expression
Elimination
• An expression (Common) appearing repeatedly in the program which
is computed previously.
Example:

T1: = 4*i T1: = 4*i


T2: = a[t1] T2: = a[t1]
T3: = 4*j T3: = 4*j
T4: = 4*i
T5: = n T5: = n
T6 := b[T4]+T5 T6 := b[T1]+T5
3. Variable Propagation
• Use of one variable instead of another.
Example:

x=pi Area=pi*r*r
……
…..
Area=x*r*r
4. Code Movement
• There are two basic goal of code movement
1. To reduce the size of code
2. To reduce the frequency of execution of code
Example: z=y*5;
for(i=0;i<=10;i++) for(i=0;i<=10;i++)
{ {
x=y*5; x=z;
…….. ……..
k=(y*5)+50; k=z+50;
} }
5. Strength Reduction
• Higher strength operators can be replaced by lower strength
operators
Example: temp=7
for(i=0;i<=50;i++)
for(i=0;i<=50;i++)
{
{
……
…….
count=temp;
count=i*7;
temp=temp+7;
…….
…….
}
}
6. Dead Code Elimination
• Dead code is the code which is never at a point in the program.
Example1:
i=j; …….
……. x=j+10;
x=i+10; …….
…….

Example2: i=0;
if(i==1) i=0;
{ a=x+10;}
Loop Optimization
• Code optimization can be significantly done in loops.
• Loop spends large amount of time.
• Methods for loop optimization:
1. Code Motion
2. Induction Variable and Strength Reduction
3. Loop Invariant
4. Loop Unrolling
5. Loop fusion
1. Code Motion
• Move some amount of codes outside of the loop.
Example:

while (i<=MAX-1) N=MAX-1


{ while (i<=n)
sum=sum+a[i]; {
} sum=sum+a[i];
}
2. Induction Variable and Strength
Reduction
• A variable x is called the induction variable of loop L if the value of
variable gets changed every time.
• Its either decremented or incremented by some constant.
• Use Strength Reduction temp=7
for(i=0;i<=50;i++) for(i=0;i<=50;i++)
{ {
……
……. count=temp;
count=i*7; temp=temp+7;
……. …….
} }
3. Loop Invariant
• Computation inside the loop is avoided.

for(i=0;i<=50;i++) T=a/b;
{ for(i=0;i<=50;i++)
k=i+a/b; {
} k=i+T;
}
4. Loop Unrolling
• The number of jumps and tests can be reduced by writing the code
two times:
i=1;
while(i<=100)
i=1; {
while(i<=100) a[i]=b[i];
{ i++;
a[i]=b[i]; a[i]=b[i];
i++; i++;
} }
5. Loop fusion
• Several loops can be merged in one loop.

for (i=1;i<=n;i++) for (i=1;i<=n*m;i++)


{ {
for (j=1;j<=m;j++) a[i]=10;
{ }
a[i][j]=10;
}
}
Example: Construct Basic Blocks
and data flow graph and identify
loop invariant statements
for (i=1;i<=n;i++) 100: i = 1
101: if i<=n goto 103
{
102: goto 111
j=1; 103: j=1
while (j<=n) 104: if j<=n goto 106
{ 105: goto 109
A=B*C/D; 106: A=B*C/D
j=j+1; 107: j=j+1
} 108: goto 104
109: i=i+1
}
110: goto 101
111:

You might also like