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

Code Optimization in Compiler Design (18100BTCSAII02853)

The document discusses code optimization in compiler design, focusing on techniques that improve intermediate code to enhance performance and resource efficiency. It outlines the objectives of optimization, the timing for performing it, and differentiates between machine-independent and machine-dependent optimization types. Key techniques include compile-time evaluation, common sub-expression elimination, dead code elimination, code movement, and strength reduction, all aimed at producing faster and more efficient code.

Uploaded by

Pranjali Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views13 pages

Code Optimization in Compiler Design (18100BTCSAII02853)

The document discusses code optimization in compiler design, focusing on techniques that improve intermediate code to enhance performance and resource efficiency. It outlines the objectives of optimization, the timing for performing it, and differentiates between machine-independent and machine-dependent optimization types. Key techniques include compile-time evaluation, common sub-expression elimination, dead code elimination, code movement, and strength reduction, all aimed at producing faster and more efficient code.

Uploaded by

Pranjali Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

SHRI VAISHNAV VIDYAPEETH VISHWAVIDYALAYA

SUBJECT: COMPILER DESIGN


SUBJECT CODE: BTCS-601

TOPIC: CODE OPTIMIZATION IN COMPILER DESIGN

SUBMITTED TO: SUBMITTED BY:


PROF. JUBER MIRZA
NAME: PRANJALI SAXENA
ENROLLMENT : 18100BTCSAII02853
BRANCH: CS-AI(IBM)
SEM: VIIth SEMESTER 4 Year
CODE OPTIMIZATION IN COMPILER DESIGN

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. Code Optimization is an approach to enhance the performance
of the code. 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 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 analysing 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

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 TECHNIQUES
Important code optimization techniques are-
1. Compile Time Evaluation
2. Common sub-expression elimination
3. Dead Code Elimination
4. Code Movement
5. Strength Reduction
1. Compile Time Evaluation-
Two techniques that falls under compile time evaluation are-

A) 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.
B) Constant 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.
2. Common Sub-Expression Elimination-
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-

Code Before Optimization Code After Optimization

S1 = 4 x i
S2 = a[S1] S1 = 4 x i
S3 = 4 x j S2 = a[S1]
S4 = 4 x i // S3 = 4 x j
Redundant Expression S5 = n
S5 = n S6 = b[S1] + S5
S6 = b[S4] + S5
3. 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-

Code Before Optimization Code After Optimization

for ( int j = 0 ; j < n ; j ++) x=y+z;


{ for ( int j = 0 ; j < n ; j ++)
x=y+z; {
a[j] = 6 x j; a[j] = 6 x j;
} }
4. 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-

Code Before Optimization Code After Optimization

i=0;
if (i == 1)
{ i=0;
a=x+5;
}
5. 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-

Code Before Optimization Code After Optimization

B=Ax 2 B=A+A

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

The optimized code has the following advantages-


• Optimized code has faster execution speed.
• Optimized code utilizes the memory efficiently.
• Optimized code gives better performance.

You might also like