Group

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

College of engineering And Technology department of

Software Engineering

Principles of Compiler Design Group Assignment

Name…………………………………….Id

1.Lalisa Gutama………………………..Nsr/1688/20

2. Samuel Wasenu…………………….Nsr/1149/20

3. Abdi Abera………………………….Nsr/1951/20

4.Dagem zerihun………………………..Nsr/2164/20

5.Xahir Bakele………………………….Nsr/
Contents ………………………………………………………page

1. Introduction to code optimization………………………………4

 Definition of code optimization


 Importance and benefits of code optimization
 When and why to optimize

2. Objectives of code optization………………………………6

 Correctness
 Performance
 Compilation time
 Delay

3. Types of code optimization………………………………7

 Machine Independent Optimization


 Machine Dependent Optimization

4 Methods of code optimization

 Compile time Evaluation


 Variable propagation
 Constant propagation
 Constant folding

5 Function – preserving Transformation………………………………10

 Common sub –expression elimination


 Copy propagation
 Dead – code elimination
 Constant folding
 Function In lining
 Unreachable Code Elimination
 Function Cloning
6.Loop Optimization Techniques
 Code Motion or Frequency Reduction
 Loop Jamming
 Loop Unrolling
7. Conclusion…………………….17
8. Referance………………………18
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.

Advantages of Code Optimization:


Improved performance: Code optimization can result in code that executes
faster and uses fewer resources, leading to improved performance.
Reduction in code size: Code optimization can help reduce the size of the
generated code, making it easier to distribute and deploy.
Increased portability: Code optimization can result in code that is more portable
across different platforms, making it easier to target a wider range of hardware
and software.
Reduced power consumption: Code optimization can lead to code that
consumes less power, making it more energy-efficient.
Improved maintainability: Code optimization can result in code that is easier to
understand and maintain, reducing the cost of software maintenance.
Disadvantages of Code Optimization:

Increased compilation time: Code optimization can significantly increase the


compilation time, which can be a significant drawback when developing large
software systems.
Increased complexity: Code optimization can result in more complex code,
making it harder to understand and debug.
Potential for introducing bugs: Code optimization can introduce bugs into the
code if not done carefully, leading to unexpected behavior and errors.
Difficulty in assessing the effectiveness: It can be difficult to determine the
effectiveness of code optimization, making it hard to justify the time and
resources spent on the 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.
Objectives of code optimization
Code optimization is essential to enhance the execution and efficiency of
a source code.
It is mandatory to deliver efficient target code by lowering the number of
instructions in a program.

 Correctness: the optimization must note change the meaning of the


program.
 Performance: optimization should increase the speed and performance of
the program.
 Compilation time: the compilation time must be kept reasonable.
 Delay : the optimization process should not delay the overall compiling
process.
Types of Code optimization
Types of Code Optimization : The optimization process can be broadly
classified into two types :
1. Machine Independent Optimization : This code optimization phase
attempts to improve the intermediate code to get a better target code as the
output. The part of the intermediate code which is transformed here does not
involve any CPU registers or absolute memory locations.
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 the memory hierarchy.

Code Optimization is done in the following different ways:


Methods of Code optimization.

1. Compile Time Evaluation:

C.

(i) A = 2*(22.0/7.0)*r
Perform 2*(22.0/7.0)*r at compile time.
(ii) x = 12.4
y = x/2.3
Evaluate x/2.3 as 12.4/2.3 at compile time.

Output:

Requesting error……
./Solution.c:1:5: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'A'
(i) A = 2*(22.0/7.0)*r
2. Variable Propagation:

C
//Before Optimization
c=a*b
x=a
till
d=x*b+4

//After Optimization
c=a*b
x=a
till
d=a*b+4

Requesting error…../Solution.c:2:5: error: 'a' undeclared here (not in a function)


c=a*b
^
./Solution.c:2:9: error: 'b' undeclared here (not in a function)
c=a*b
^
./Solution.c:3:1: error: expected ',' or ';' before 'x'
x=a
^

3. Constant Propagation:
 If the value of a variable is a constant, then replace the variable with the
constant. The variable may not always be a constant.
Example:
 C
(i) A = 2*(22.0/7.0)*r

Performs 2*(22.0/7.0)*r at compile time.

(ii) x = 12.4

y = x/2.3

Evaluates x/2.3 as 12.4/2.3 at compile time.

(iii) int k=2;

if(k) go to L3;

It is evaluated as :

go to L3 ( Because k = 2 which implies condition is always true)

4. Constant Folding:

 Consider an expression : a = b op c and the values b and c are constants, then


the value of a can be computed at compile time.
Example:
 C

#define k 5

x=2*k

y=k+5
This can be computed at compile time and the values of x and y are :

x = 10

y = 10

Note: Difference between Constant Propagation and Constant Folding:


 In Constant Propagation, the variable is substituted with its assigned constant
where as in Constant Folding; the variables whose values can be computed at
compile time are considered and computed.

Function-Preserving Transformations

1. Copy Propagation:

 It is extension of constant propagation.


 After a is assigned to x, use a to replace x till a is assigned again to another
variable or value or expression.
 It helps in reducing the compile time as it reduces copying.
Example :
 C

//Before Optimization

c=a*b

x=a

till
d=x*b+4

//After Optimization

c=a*b

x=a

till

d=a*b+4

2. Common Sub Expression Elimination:

 In the above example, a*b and x*b is a common sub expression.

3. Dead Code Elimination:

 Copy propagation often leads to making assignment statements into dead


code.
 A variable is said to be dead if it is never used after its last definition.
 In order to find the dead variables, a data flow analysis should be done.
Example:
 C

c=a*b
x=a

till

d=a*b+4

//After elimination :

c=a*b

till

d=a*b+4

4. Unreachable Code Elimination:


First, Control Flow Graph should be constructed.

The block which does not have an incoming edge is an Unreachable code block.

After constant propagation and constant folding, the unreachable branches can be
eliminated.

C++

#include <iostream>

using namespace std;

int main() {
int num;

num=10;

cout << "GFG!";

return 0;

cout << num; //unreachable code

//after elimination of unreachable code

int main() {

int num;

num=10;

cout << "GFG!";

return 0;
}

5. Function Inlining:
Here, a function call is replaced by the body of the function itself.

This saves a lot of time in copying all the parameters, storing the return address,
etc.

10. Function Cloning:


Here, specialized codes for a function are created for different calling parameters.

Example: Function Overloading


11. Induction Variable and Strength Reduction:
An induction variable is used in the loop for the following kind of assignment i = i
+ constant. It is a kind of Loop Optimization Technique.

Strength reduction means replacing the high strength operator with a low strength.

Examples:

Example 1 :

Multiplication with powers of 2 can be replaced by shift left operator which is less

expensive than multiplication

a=a*16

// Can be modified as :

a = a<<4

Example 2 :

i = 1;

while (i<10)

y = i * 4;

}
//After Reduction

i=1

t=4

while( t<40)

y = t;

t = t + 4;

Loop Optimization Techniques:

1. Code Motion or Frequency Reduction:

The evaluation frequency of expression is reduced.

The loop invariant statements are brought out of the loop.

Example:

a = 200;

while(a>0)

b = x + y;
if (a % b == 0)

printf(“%d”, a);

//This code can be further optimized as

a = 200;

b = x + y;

while(a>0)

if (a % b == 0}

printf(“%d”, a);

2. Loop Jamming:
Two or more loops are combined in a single loop. It helps in reducing the compile
time.

Example:

// Before loop jamming

for(int k=0;k<10;k++)

{
x = k*2;

for(int k=0;k<10;k++)

y = k+3;

//After loop jamming

for(int k=0;k<10;k++)

x = k*2;

y = k+3;

3. Loop Unrolling:
It helps in optimizing the execution time of the program by reducing the iterations.

It increases the program’s speed by eliminating the loop control and test
instructions.

Example:

//Before Loop Unrolling


for(int i=0;i<2;i++)

printf("Hello");

//After Loop Unrolling

printf("Hello");

printf("Hello");

Conclusion

By adhering to the principles of source code optimization outlined in this


document, developers can enhance the performance, efficiency, and
maintainability of their code. By continuously optimizing and refining code,
developers can deliver high quality software applications that meet performance
requirements and provide a better user experience.

Source code optimization is an ongoing process that requires attention to detail,


careful analysis, and commitment to improving code quality.
Compilers: Principles, Techniques and Tools" by Alfred V Aho and Ravi
Sethi
Book Review: This book is widely known as the Dragon Book among professors,
students, and developers worldwide. It discusses the advancements in software
engineering, programming languages, and computer architecture. The book also
addresses the broader range of challenges that arise in software design and
development. It covers various topics in automata theory, such as regular
expressions, NFA, DFA, grammars, ambiguities, and techniques for handling
ambiguities. Additionally, the book provides detailed explanations of grammar
theory, parsers, handles, and prefixes.

You might also like