Unit 5
Unit 5
Code Optimization
Advantages-
Strength Reduction
In strength reduction optimization, operators that consume higher execution time
are replaced by the operators consuming less execution time.
b=a ²
Can be replaced by
B=a*a;
Initial code:
n = a * 2;
Optimized code:
b= a << 1;
//left shifting the bit
Initial code:
b = a / 2;
Optimized code:
b = a >> 1;
// right shifting the bit by one will give the same result
a = 10;
b = a + 1 * 2;
c = a + 1 * 2;
//’c’ has common expression as ‘b’
d = c + a;
After elimination –
a = 10;
b = a + 1 * 2;
d = b + a;
Loop Optimization
Loop Optimization is the process of increasing execution speed and reducing
the overheads associated with loops. It plays an important role in improving
cache performance and making effective use of parallel processing capabilities.
Most execution time of a scientific program is spent on loops.
Loop Optimization Techniques:
}
}
Optimized code:
a=10;
y=1;
z=2;
X=y+z;
while(a>0)
{
If(a%x==0)
{printf(“%d”,a);
}
}
2. Loop Unrolling:
Loop unrolling is a loop transformation technique that helps to optimize the
execution time of a program. We basically remove or reduce iterations. Loop
unrolling increases the program’s speed by eliminating loop control
instruction and loop test instructions.
Example:
Initial code:
Optimized code:
printf("Neha\n");
printf("Neha\n");
printf("Neha\n");
printf("Neha\n");
printf("Neha\n");
3. Loop Jamming:
Loop jamming is the combining the two or more loops in a single loop. It
reduces the time taken to compile the many number of loops.
Example:
Initial Code:
Optimized code:
for(int i=0; i<5; i++)
{
a = i + 5;
b = i + 10;
}
Data flow analysis in Compiler
It is the analysis of flow of data in control flow graph, i.e., the analysis
that determines the information regarding the definition and use of data
in program. With the help of this analysis, optimization can be done. In
general, its process in which values are computed using data flow
analysis. The data flow property represents information that can be
used for optimization.
Basic Terminologies –
Advantage –
It is used to eliminate common sub expressions.
1. x = a + b;
2. x=6*3
o In this code, the first assignment of x is useless. The value computer for
x is never used in the program.
o At compile time the expression 6*3 will be computed, simplifying the
second assignment statement to x = 18;
Some optimization needs more global information. For example, consider the
following code:
1. a = 1;
2. b = 2;
3. c = 3;
4. if (....) x = a + 5;
5. else x = b + 4;
6. c = x + 1;
In this code, at line 3 the initial assignment is useless and x +1 expression can
be simplified as 7.
But it is less obvious that how a compiler can discover these facts by looking
only at one or two consecutive statements. A more global analysis is required
so that the compiler knows the following things at each point in the program:
Video
Data flow analysis is used to discover this kind of property. The data flow
analysis can be performed on the program's control flow graph (CFG).
The control flow graph of a program is used to determine those parts of a
program to which a particular value assigned to a variable might propagate.