0% found this document useful (0 votes)
90 views13 pages

Compiler Design

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.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views13 pages

Compiler Design

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.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Code Optimization

Bachelor of Technology
Computer Science and Engineering

Submitted By

ABHISHEK KUMAR (13000116143)

OCTOBER 2019

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
 Machine-independent Optimization

 Machine-dependent Optimization

 Basic Blocks

 Basic block identification

 Control Flow Graph

 Loop Optimization

 Dead-code Elimination

 Partially dead code

 Partial Redundancy
4. Conclusion
5. References

TMSL/CSE/Term-Paper/Semester-7 2
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. Keywords: Code Optimization, Compiler, Code Reduction, Inlining

TMSL/CSE/Term-Paper/Semester-7 3
Introduction

In compiler style, there's one in all the technique within which a region of code is
being reworked to provide additional economical still on improve the performance
such the output remains same, termed as “Optimization”. Code improvement aims
to create top quality code with best quality (time and space) such it mustn't have an
effect on the precise results of the code. it's chiefly supported the criterion to
preserve the linguistics equivalence of the program, such the rule should not be
changed. On a mean, the transformation ought to speed up the execution of the
program. Improvement includes finding a bottleneck, a important a part of the code
that is that the primary shopper of the required resources. Essentially Code
improvement issues on correctness, it suggests that the correctness of the generated
code mustn't be modified. The most aim of the code improvement is to create top
quality code with improved quality (time and space) while not moving the precise
results of the code. On victimization totally different improvement techniques, the
code is often optimized while not moving the first (actual) rule and final output
with the intent of high performance. Once performance is to be thought of, then
there ought to select associate rule that runs quickly and therefore the on the
market computing resources are getting used expeditiously. So, it are often
aforementioned that the target of improvement is to put in writing a code in such
the simplest way that may cut back each the memory still as speed. Basically, Code
improvement involves the utilization of rules and algorithms to the program phase
with the aim such the code becomes quicker, smaller, additional economical then
on. In theoretical perspective, the compiler improvement essentially refers to the
program improvement to realize performance within the execution. ASCII text file
improvement refers to the 3 aspects, a programming language code (front code),
associate programming language code that is generated by the compiler to the
acceptable programming language (intermediate code), the article code that is
generated from the programming language code for the execution of the particular
work. This work involves the implementation of 2 totally different techniques,
dead code elimination and customary sub expression elimination with the
employment of various tools so as to optimize the code.

TMSL/CSE/Term-Paper/Semester-7 4
BODY

Optimization is a program transformation technique, which tries to improve the


code by making it consume less resources (i.e. CPU, Memory) and deliver high
speed.
In optimization, high-level general programming constructs are replaced by very
efficient low-level programming codes. A code optimizing process must follow
the three rules given below:
 The output code must not, in any way, change the meaning of the program.
 Optimization should increase the speed of the program and if possible, the
program should demand less number of resources.
 Optimization should itself be fast and should not delay the overall
compiling process.
Efforts for an optimized code can be made at various levels of compiling the
process.
 At the beginning, users can change/rearrange the code or use better
algorithms to write the code.
 After generating intermediate code, the compiler can modify the
intermediate code by address calculations and improving loops.
 While producing the target machine code, the compiler can make use of
memory hierarchy and CPU registers.
Optimization can be categorized broadly into two types : machine independent
and machine dependent.

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:
do
{
item = 10;
value = value + item;

TMSL/CSE/Term-Paper/Semester-7 5
} while(value<100);
This code involves repeated assignment of the identifier item, which if we put this
way:
Item = 10;
do
{
value = value + item;
} while(value<100);
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.

Basic Blocks
Source codes generally have a number of instructions, which are always executed
in sequence and are considered as the basic blocks of the code. These basic blocks
do not have any jump statements among them, i.e., when the first instruction is
executed, all the instructions in the same basic block will be executed in their
sequence of appearance without losing the flow control of the program.
A program can have various constructs as basic blocks, like IF-THEN-ELSE,
SWITCH-CASE conditional statements and loops such as DO-WHILE, FOR, and
REPEAT-UNTIL, etc.

Basic block identification


We may use the following algorithm to find the basic blocks in a program:
 Search header statements of all the basic blocks from where a basic block
starts:
o First statement of a program.
o Statements that are target of any branch (conditional/unconditional).
o Statements that follow any branch statement.

TMSL/CSE/Term-Paper/Semester-7 6
 Header statements and the statements following them form a basic block.
 A basic block does not include any header statement of any other basic
block.
Basic blocks are important concepts from both code generation and optimization
point of view.

Basic blocks play an important role in identifying variables, which are being used
more than once in a single basic block. If any variable is being used more than
once, the register memory allocated to that variable need not be emptied unless
the block finishes execution.

Control Flow Graph


Basic blocks in a program can be represented by means of control flow graphs. A
control flow graph depicts how the program control is being passed among the
blocks. It is a useful tool that helps in optimization by help locating any unwanted
loops in the program.

TMSL/CSE/Term-Paper/Semester-7 7
Loop Optimization
Most programs run as a loop in the system. It becomes necessary to optimize the
loops in order to save CPU cycles and memory. Loops can be optimized by the
following techniques:
 Invariant code : A fragment of code that resides in the loop and computes
the same value at each iteration is called a loop-invariant code. This code
can be moved out of the loop by saving it to be computed only once, rather
than with each iteration.
 Induction analysis : A variable is called an induction variable if its value is
altered within the loop by a loop-invariant value.
 Strength reduction : There are expressions that consume more CPU cycles,
time, and memory. These expressions should be replaced with cheaper
expressions without compromising the output of expression. For example,
multiplication (x * 2) is expensive in terms of CPU cycles than (x << 1) and
yields the same result.

Dead-code Elimination
Dead code is one or more than one code statements, which are:

TMSL/CSE/Term-Paper/Semester-7 8
 Either never executed or unreachable,
 Or if executed, their output is never used.
Thus, dead code plays no role in any program operation and therefore it can
simply be eliminated.

Partially dead code


There are some code statements whose computed values are used only under
certain circumstances, i.e., sometimes the values are used and sometimes they are
not. Such codes are known as partially dead-code.

The above control flow graph depicts a chunk of program where variable „a‟ is
used to assign the output of expression „x * y‟. Let us assume that the value
assigned to „a‟ is never used inside the loop.Immediately after the control leaves
the loop, „a‟ is assigned the value of variable „z‟, which would be used later in the
program. We conclude here that the assignment code of „a‟ is never used
anywhere, therefore it is eligible to be eliminated.

TMSL/CSE/Term-Paper/Semester-7 9
Likewise, the picture above depicts that the conditional statement is always false,
implying that the code, written in true case, will never be executed, hence it can
be removed.

Partial Redundancy
Redundant expressions are computed more than once in parallel path, without any
change in operands.whereas partial-redundant expressions are computed more
than once in a path, without any change in operands. For example,

[redundant expression] [partially redundant expression]

Loop-invariant code is partially redundant and can be eliminated by using a code-


motion technique.
Another example of a partially redundant code can be:
If (condition)
{
a = y OP z;
}
else
{
...
}
c = y OP z;

TMSL/CSE/Term-Paper/Semester-7 10
We assume that the values of operands (y and z) are not changed from assignment
of variable a to variable c. Here, if the condition statement is true, then y OP z is
computed twice, otherwise once. Code motion can be used to eliminate this
redundancy, as shown below:
If (condition)
{
...
tmp = y OP z;
a = tmp;
...
}
else
{
...
tmp = y OP z;
}
c = tmp;
Here, whether the condition is true or false; y OP z should be computed only once.

TMSL/CSE/Term-Paper/Semester-7 11
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 our research in the field of the code optimization. A compiler translates the
code written in one language to some other language without changing the
meaning of the program. It is also expected that a compiler should make the target
code efficient and optimized in terms of time and space.
Compiler design principles provide an in-depth view of translation and
optimization process. Compiler design covers basic translation mechanism and
error detection & recovery. It includes lexical, syntax, and semantic analysis as
front end, and code generation and optimization as back-end.

TMSL/CSE/Term-Paper/Semester-7 12
REFERENCE
[1] Mr. Chirag H. Bhatt, Dr. Harshad B. Bhadka , “Peephole Optimization
Technique for analysis and review of Compile Design and Construction”, IOSR
Journal of Computer Engineering (IOSR-JCE), Volume 9, Issue 4 (Mar. - Apr.
2013).

[2] C. Brandolese, W. Fornaciari, F. Salice, D. Sciuto, “Source–Level Execution


Time Estimation of C Programs”, Proceedings of the ninth international
symposium on Hardware/software codesign.

[3] Maggie Johnson, “Code Optimization”, Handout 20 “August 08,2004.

[4] Michael E. Lee, “Optimization of Computer Programs in C ”, Ontek


Corporation, USA

[5]https://www.tutorialspoint.com/compiler_design/compiler_design_code_optimi
zation.htm

TMSL/CSE/Term-Paper/Semester-7 13

You might also like