Group 16 - Compiler Design Term Paper

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Code Optimization Techniques

(Function Preserving)

Bachelor of Technology
Computer Science and Engineering

Submitted By

Ronit kundu(13000119051)
Kaustav Dey(13000119056)
Subham Ghosh(13000119058)

MONTH 2021

Techno Main Salt Lake


EM-4/1, Sector-V, Salt Lake
Kolkata- 700091
West Bengal
India
TABLE OF CONTENTS

1. Abstract
2. Introduction
3. Body
3.1. Why optimize?
3.2. When and Where to optimize?
3.3. Phases of Optimization
3.4. Types of Code Optimization
3.4.1. Machine Independent Optimization
3.4.2. Machine Dependent Optimization
3.5. Function preserving transformation
3.5.1. Common sub expression elimination
3.5.2. Copy Propagation
3.5.3. Dead Code elimination
3.5.4. Constant folding
3.5.5. Code movement
3.5.6. Strength reduction
3.6. Advantages of optimization
4. Conclusion
5. References

TISL/CSE/Term-Paper/Semester- 2
1. Abstract
The main motive of this paper is to present the need of the code optimization and work done
in the field of the code optimization. The improvement within the quality of code remains an
enormous issue from the sooner days. Typically it's troublesome for a computer programmer
to seek out that a part of code consumes a lot of resources and thence result in an inefficient
code. Antecedently most of the improvement were done manually or are often same as
statically those results in variety of issues to the computer programmer and additionally had a
number of the restrictions. However, of late many compilers area unit obtainable that makes
the improvement to be performed dynamically. During this treatise work, an endeavor has
been created to style and implement a system that may mechanically optimize the code so as
to reduce the quality of the code specified the code becomes a lot of economical.

2. 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.

3. Body

3.1. 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.

TISL/CSE/Term-Paper/Semester- 3
3.2. When and Where 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.

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.

3.3. Phases of Optimization

There are generally two phases of 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.

3.4. Types of Code Optimization

The optimization process can be classified into two types:-

3.4.1. 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:

TISL/CSE/Term-Paper/Semester- 4
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.

3.4.2. 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.

3.5. 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  are:-
1. Common sub expression elimination
2. Copy propagation
3. Dead-code elimination
4. Constant folding
5. Code movement
6. 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.

TISL/CSE/Term-Paper/Semester- 5
3.5.1. 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:

3.5.2. Copy propagation

TISL/CSE/Term-Paper/Semester- 6
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.

3.5.3. 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’ condition is dead code because this condition will never satisfied.
3.5.4. Constant folding

In this technique,

TISL/CSE/Term-Paper/Semester- 7
 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.

3.5.5. 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.

3.5.6. Strength reduction

In this technique,

TISL/CSE/Term-Paper/Semester- 8
 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.

3.6. Advantages of 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
Code Optimization is the interesting concept and it is always the continuous process to
device the new method of optimizing the code. Various research in the field of the code
optimization is going on and due to the importance of this concept in the field of
programming and computing we in our future work will extend research in the field of the
code optimization.

5. References
https://www.ijtre.com/images/scripts/2015030511.pdf

https://edurev.in/studytube/Function-Preserving-Transformations-Compiler-
Desig/1f4598ab-01b0-437b-aca9-ddaa48c60b0a_t#

TISL/CSE/Term-Paper/Semester- 9
https://www.geeksforgeeks.org/code-optimization-in-compiler-design/

https://www.tutorialspoint.com/compiler_design/compiler_design_code
_optimization.htm

https://www.gatevidyalay.com/code-optimization-techniques/

TISL/CSE/Term-Paper/Semester- 10

You might also like