Group 16 - Compiler Design

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Code Optimization Techniques

(Function preserving)

GROUP – 16 SECTION- A

RONIT KUNDU – 13000119051


KAUSTAV DEY – 13000119056
SUBHAM GHOSH - 13000119058
Content

◉ INTRODUCTION
◉ TYPES OF CODE OPTIMIZATION
◉ FUNCTION PRESERVING TRANSFORMATIONS
◉ ADVANTAGES OF CODE OPTIMIZATION
◉ CONCLUSION
◉ ACKNOWLEDGEMENT
◉ RESOURCES
1 INTRODUCTION
-Code optimization
-When to optimize?
-Why optimize?
-Where to optimize?
Introduction
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 (i.e. CPU, Memory) 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.
When to optimize?

Optimization of the code is often performed at the end of the development


stage since:
◉ It reduces readability
◉ Adds code that is used to increase the performance.
Why optimize?

◉ 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.
Where to optimize?
We can apply optimization in:- 
◉ Source program
Optimizing the source program involves making changes to the algorithm
or changing the loop structures. User is the actor here.
◉ Intermediate Code
Optimizing the intermediate code involves changing the address
calculations and transforming the procedure calls involved. Here compiler
is the actor.
◉ Target Code
Optimizing the target code is done by the compiler. Usage of registers,
select and move instructions is part of optimization involved in the target
code.
TYPES OF CODE
2 OPTIMIZATION
-Machine Independent Optimization
-Machine-dependent Optimization
Machine Independent optimization
◉ In this optimization, the compiler takes in the intermediate code and
transforms a part of the code that does not involve any CPU registers
and/or absolute memory locations. For example:

◉ This code involves repeated assignment of the identifier item, which if we


put this way:

should not only save the CPU cycles, but can be used on any processor.
Machine Dependent optimization

◉ Machine-dependent optimization is done after the target code has been


generated and when the code is transformed according to the target
machine architecture.
◉  It involves CPU registers and may have absolute memory references rather
than relative references.
◉ Machine-dependent optimizers put efforts to take maximum advantage of
memory hierarchy.
Function Preserving
3 Transformations
-Common sub expression elimination
-Copy propagation
-Dead-code elimination
-Constant folding
-Code movement
-Strength reduction
Function preserving transformation
◉ There are a number of ways in which a compiler can improve a program
without changing the function it computes.
◉ The transformations  
o Common sub expression elimination
o Copy propagation
o  Dead-code elimination
o Constant folding
o Code movement
o Strength reduction
◉ Frequently, a program will include several calculations of the  same value,
such as an   offset in an array. Some of the duplicate calculations cannot be
avoided by the programmer because they lie below the level of detail
accessible within the source language.
Common sub expression elimination
The expression that has been already computed before and appears again in
the code for computation is called as Common Sub-Expression.
In this technique:
◉ As the name suggests, it involves eliminating the common sub expressions.
◉ The redundant expressions are eliminated to avoid their re-computation.
◉ The already computed result is used in the further program when required.
Example
Copy propagation

In this technique,
◉ If some variable has been assigned some constant value, then it replaces
that variable with its constant value in the further program during
compilation.
◉ The condition is that the value of variable must not get alter in between.
Example
pi = 3.14
radius = 10
Area of circle = pi x radius x radius 
Here,
◉ This technique substitutes the value of variables ‘pi’ and ‘radius’ at compile
time.
◉ It then evaluates the expression 3.14 x 10 x 10.
◉ The expression is then replaced with its result 314.
◉ This saves the time at run time.
Dead-code elimination

In this technique,
◉ As the name suggests, it involves eliminating the dead code.
◉ The statements of the code which either never executes or are
unreachable or their output is never used are eliminated.
Example

Here, ‘if’ statement is dead code because this condition will never get
satisfied.
Constant folding

In this technique,
◉ As the name suggests, it involves folding the constants.
◉ The expressions that contain the operands having constant values at
compile time are evaluated.
◉ Those expressions are then replaced with their respective results.
Example

Circumference of Circle = (22/7) x Diameter


Here,
◉ This technique evaluates the expression 22/7 at compile time.
◉ The expression is then replaced with its result 3.14.
◉ This saves the time at run time.
Code movement

In this technique,
◉ As the name suggests, it involves movement of the code.
◉ The code present inside the loop is moved out if it does not matter whether
it is present inside or outside.
◉ Such a code unnecessarily gets execute again and again with each iteration
of the loop.
◉ This leads to the wastage of time at run time.
Example

Here x=y+z is present inside the loop but does not matter if it’s present
inside or outside, so it’s moved out to stop unnecessary execution.
Strength reduction

In this technique,
◉ As the name suggests, it involves reducing the strength of expressions.
◉ This technique replaces the expensive and costly operators with the simple
and cheaper ones.
Example

Here,
◉ The expression “A x 2” is replaced with the expression “A + A”.
◉ This is because the cost of multiplication operator is higher than that of
addition operator.
4 Advantages
Advantages of code optimization

The optimized code has the following advantages-


◉ Optimized code has faster execution speed.
◉ Optimized code utilizes the memory efficiently.
◉ Optimized code gives better performance.
4 Conclusion
Conclusion

◉ Code optimization is the 5th phase of the compiler


◉ It will perform at the end of the development stage
◉ We can reduce Time complexity and Memory consumption of source code
◉ Optimization removes inefficiencies in the source code
Acknowledgement

We would like to express my special thanks of gratitude to our Professor


Mr Tapan Chowdhury who gave us the golden opportunity to do this
wonderful presentation on this topic, which helped us in doing a lot of
researches and we come to know about so many new things. It helped us
increase our knowledge and skills.
Resources

◉ https://www.ijtre.com/images/scripts/2015030511.pdf
◉ https://www.geeksforgeeks.org/code-optimization-in-compiler-design/
◉ https://edurev.in/studytube/Function-Preserving-Transformations-Compil
er-
Desig/1f4598ab-01b0-437b-aca9-ddaa48c60b0a_t#
◉ https://
www.tutorialspoint.com/compiler_design/compiler_design_code_optimiza
tion.htm
◉ https://www.gatevidyalay.com/code-optimization-techniques/
Thanks!

You might also like