Code Optimization in Compiler Design: Kunal Jangra Shivam Tripathi Tushar Jain
Code Optimization in Compiler Design: Kunal Jangra Shivam Tripathi Tushar Jain
Optimization in
Compiler Design
Kunal Jangra
Shivam Tripathi
Tushar Jain
Code Optimization
The code optimization in the synthesis phase is a program transformation technique, which tries
to improve the intermediate code by making it consume fewer resources so that faster-running
machine code will result.
Compiler optimizing process should meet the following objectives :
• The optimization must be correct, it must not, in any way, change the meaning of the program.
• Optimization should increase the speed and performance of the program.
• The compilation time must be kept reasonable.
• The optimization process should not delay the overall compiling process.
Objectives of compiler optimizing
• The optimization must be correct, it must not, in any way, change the meaning of the
program.
• Optimization should increase the speed and performance of the program.
• The optimization process should not delay the overall compiling process.
When to Optimize?
Optimization of the code is often performed at the end of the development stage since it
reduces readability and adds code that is used to increase the performance.
Why Optimize?
optimizing an algorithm is beyond the scope of the code optimization phase. So the
program is optimized. And it may involve reducing the size of the code. So optimization
helps to:
• Reduce the space consumed and increases the speed of compilation.
• Manually analyzing datasets involves a lot of time. Hence, we make use of software like
Tableau for data analysis. Similarly, manually performing the optimization is also tedious
and is better done using a code optimizer.
• An optimized code often promotes re-usability.
Types of Code Optimization
• Global Optimization:
Transformations are applied to large program segments that includes
functions,procedures and loops.
• Local Optimization:
Transformations are applied to small blocks of statements.The local optimization is
done prior to global optimization.
PRINCIPAL SOURCES OF OPTIMISATION
Assignments of the form f : = g called copy statements, or copies for short. The idea behind the copy-
propagation transformation is to use g for f, whenever possible after the copy statement f: = g. Copy
propagation means use of one variable instead of another. This may not appear to be an improvement, but as
we shall see it gives us an opportunity to eliminate x.
• For example:
x=Pi;
A=x*r*r;
The optimization using copy propagation can be done as follows: A=Pi*r*r;
Here the variable x is eliminated
Dead-Code Eliminations:
A variable is live at a point in a program if its value can be used subsequently; otherwise, it is dead at that point. A
related idea is dead or useless code, statements that compute values that never get used. While the programmer is
unlikely to introduce any dead code intentionally, it may appear as the result of previous transformations.
Example:
i=0;
if(i=1)
{
a=b+5;
}
Here, ‘if’ statement is dead code because this condition will never get satisfied.
Loop Optimizations:
In loops, especially in the inner loops, programs tend to spend the bulk
of their time. The running time of a program may be improved if the number of
instructions in an inner loop is decreased, even if we increase the amount of
code outside that loop.
Three techniques are important for loop optimization:
Ø Code motion, which moves code outside a loop;
Ø Induction-variable elimination, which we apply to replace variables from
inner loop.
Ø Reduction in strength, which replaces and expensive operation by a cheaper
one, such as a multiplication by an addition.