Unit 5 - 2
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:
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:
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.