Professional Documents
Culture Documents
23 Code Optimization 06-03-2024
23 Code Optimization 06-03-2024
Compiler phases
– Front end (3 phases)
– back end Dr.K.Ragavan, SCOPE, VIT Vellore 5
Dr.K.Ragavan, SCOPE, VIT Vellore 6
Compile Time Evaluation
• we optimize the code at the compile time – two techniques
– constant folding
– constant propogation
• Constant Folding – technique of evaluating an expression whose
operands are known to be constants at compile time itself
Example: Length = (22/7)*d
• Above 22/7 is a constant expression
• So first solve the constant expression before and keep the
constant value – so called folding the constant and place the value
at compile time since evaluated at compile time and keep the
value of reuse
Dr.K.Ragavan, SCOPE, VIT Vellore 7
Dr.K.Ragavan, SCOPE, VIT Vellore 8
Example – finding area of circle
c = 7;
k = 0;
for (i = 0; i < N; i++)
{
y[i] = k;
k = k + c;
} Dr.K.Ragavan, SCOPE, VIT Vellore 15
Loop Optimization
Loops are important targets for optimization
because programs with loops tend to spend a
lot of time executing those loops.
There are three important techniques in
optimizing loops:
Code motion
Induction variable elimination
Strength reduction.
Dr.K.Ragavan, SCOPE, VIT Vellore 16
Code motion lets us move unnecessary code out of
a loop.
Consider the following loop:
for (i = 0; i < N*M; i++)
{
z[i] = a[i] + b[i];
}
We optimize the
code in inner loop
Dr.K.Ragavan, SCOPE, VIT Vellore
17
An induction variable is a variable whose
value is derived from the loop iteration
variable’s value.