0% found this document useful (0 votes)
29 views16 pages

Unit 5 PCC

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)
29 views16 pages

Unit 5 PCC

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/ 16

Code Optimization

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.

Code Optimization is an approach to enhance the performance of the code.

The process of code optimization involves-


 Eliminating the unwanted code lines
 Rearranging the statements of the code
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.
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.
Code Optimization
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:

1. Compile Time Evaluation


2. Common sub-expression elimination
3. Dead Code Elimination
4. Code Movement
5. Strength Reduction
. Compile Time Evaluation-

Two techniques that falls under compile time evaluation are-


Code Optimization
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
Code Optimization
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-

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-

Code Before Optimization Code After Optimization

S1 = 4 x i S1 = 4 x i

S2 = a[S1] S2 = a[S1]
S3 = 4 x j S3 = 4 x j
S4 = 4 x i // Redundant Expression S5 = n
Code Optimization
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.
Code Optimization
 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=Ax2 B=A+A

Here,
 The expression “A x 2” is replaced with the expression “A + A”.
Code Optimization
 This is because the cost of multiplication operator is higher than that of
addition operator.

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.

Loop Optimization
Loop optimization is most valuable machine-independent optimization because program's inner
loop takes bulk to time of a programmer.

If we decrease the number of instructions in an inner loop then the running time of a program
may be improved even if we increase the amount of code outside that loop.

For loop optimization the following three techniques are important:

1. Code motion
2. Induction-variable elimination
3. Strength reduction

1.Code Motion:
Code motion is used to decrease the amount of code in loop. This transformation takes a
statement or expression which can be moved outside the loop body without affecting the
semantics of the program.

For example
In the while statement, the limit-2 equation is a loop invariant equation.

1. while (i<=limit-2) /*statement does not change limit*/


2. After code motion the result is as follows:
3. a= limit-2;
4. while(i<=a) /*statement does not change limit or a*/
Code Optimization

2.Induction-Variable Elimination
Induction variable elimination is used to replace variable from inner loop.

It can reduce the number of additions in a loop. It improves both code space and run time
performance.

In this figure, we can replace the assignment t4:=4*j by t4:=t4-4. The only problem which will be
arose that t4 does not have a value when we enter block B2 for the first time. So we place a
relation t4=4*j on entry to the block B2.

4. Reduction in Strength
Code Optimization
o Strength reduction is used to replace the expensive operation by the cheaper once on the
target machine.
o Addition of a constant is cheaper than a multiplication. So we can replace multiplication
with an addition within the loop.
o Multiplication is cheaper than exponentiation. So we can replace exponentiation with
multiplication within the loop.

Example:

1. while (i<10)
2. {
3. j= 3 * i+1;
4. a[j]=a[j]-2;
5. i=i+2;
6. }

After strength reduction the code will be:

1. s= 3*i+1;
2. while (i<10)
3. {
4. j=s;
5. a[j]= a[j]-2;
6. i=i+2;
7. s=s+6;
8. }

In the above code, it is cheaper to compute s=s+6 than j=3 *i


Code Optimization

Where to apply Optimization?

Now that we learned the need for optimization and its two types,now let’s see where to apply these
optimization.
 Source program: Optimizing the source program involves making changes to the algorithm or
changing the loop structures. The 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, and select
and move instructions are part of the optimization involved in the target code.
 Local Optimization: Transformations are applied to small basic blocks of statements.
Techniques followed are Local Value Numbering and Tree Height Balancing.
 Regional Optimization: Transformations are applied to Extended Basic Blocks. Techniques
followed are Super Local Value Numbering and Loop Unrolling.
 Global Optimization: Transformations are applied to large program segments that
include functions, procedures, and loops. Techniques followed are Live Variable Analysis
and Global Code Replacement.
 Inter procedural Optimization: As the name indicates, the optimizations are
applied inter procedural. Techniques followed are Inline Substitution and Procedure
Placement.

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:


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.

In compiler design, code optimization is a program transformation technique that tries to improve the
intermediate code to consume fewer resources such as CPU, memory, etc., resulting in faster machine
code.
There are two types of code optimization techniques.
1. Local optimization- This code optimization applies to a small block of statements. Examples of local
optimization are variable/constant propagation and common subexpression elimination.
2. Global optimization- This code optimization applies to large program segments like functions, loops,
procedures etc. An example of global optimization is data flow analysis.
This article will discuss a global optimization technique, i.e. data flow analysis.

Data Flow Analysis


Data flow analysis is a global code optimization technique. The compiler performs code optimization
efficiently by collecting all the information about a program and distributing it to each block of its control
flow graph (CFG). This process is known as data flow analysis.
Since optimization has to be done on the entire program, the whole program is examined, and the compiler
collects information.
Note: The control flow graph represents a program in blocks depicting how the control of the program is
being passed from one block to another.
We will proceed with our discussion with some basic terminologies in data flow analysis.
Basic Technologies
Below are some basic terminologies related to data flow analysis.
 Definition Point- A definition point is a point in a program that defines a data item.
 Reference Point- A reference point is a point in a program that contains a reference to a data item.
 Evaluation Point- An evaluation point is a point in a program that contains an expression to be
evaluated.
The below diagram shows an example of a definition point, a reference point, and an evaluation point in a
program.
Code Optimization

Data Flow Analysis Equation


The data flow analysis equation is used to collect information about a program block. The following is the
data flow analysis equation for a statement s-
Out[s] = gen[s] U In[s] - Kill[s]
where
Out[s] is the information at the end of the statement s.
gen[s] is the information generated by the statement s.
In[s] is the information at the beginning of the statement s.
Kill[s] is the information killed or removed by the statement s.
The main aim of the data flow analysis is to find a set of constraints on the In[s]’s and Out[s]’s for the
statement s.
The constraints include two types of constraints-
The transfer function
the Control Flow constraint.
Let’s discuss them.
Transfer Function
The semantics of the statement are the constraints for the data flow values before and after a statement.
For example, consider two statements x = y and z = x. Both these statements are executed.
Code Optimization
Thus, after execution, we can say that both x and z have the same value, i.e. y.
Thus, a transfer function depicts the relationship between the data flow values before and after a statement.
There are two types of transfer functions-
1. Forward propagation
2. Backward propagation
Let’s see both of these.
 Forward propagation
o In forward propagation, the transfer function is represented by Fs for any statement s.
o This transfer function accepts the data flow values before the statement and outputs the new data
flow value after the statement.
o Thus the new data flow or the output after the statement will be Out[s] = Fs(In[s]).
 Backward propagation
o The backward propagation is the converse of the forward propagation.
o After the statement, a data flow value is converted to a new data flow value before the statement
using this transfer function.
o Thus the new data flow or the output will be In[s] = Fs(Out[s]).
Control-Flow Constraints
The second set of constraints comes from the control flow. The control flow value of Si will be equal to the
control flow values into Si + 1 if block B contains statements S1, S2,........, Sn. That is:
IN[Si + 1] = OUT[Si], for all i = 1 , 2, .....,n – 1.
Now, we will discuss some data flow properties.
Data Flow Properties
Some properties of the data flow analysis are-
 Available expression
 Reaching definition
 Line variable
 Busy expression
We will discuss these properties one by one.
Available Expression
An expression a + b is said to be available at a program point x if none of its operands gets modified before
their use. It is used to eliminate common subexpressions.
An expression is available at its evaluation point.
Example:
Code Optimization

In the above example, the expression L1: 4 * i is an available expression since this expression is available
for blocks B2 and B3, and no operand is getting modified.
Reaching Definition
A definition D is reaching a point x if D is not killed or redefined before that point. It is generally used in
variable/constant propagation.
Example:

In the above example, D1 is a reaching definition for block B2 since the value of x is not changed (it is two
only) but D1 is not a reaching definition for block B3 because the value of x is changed to x + 2. This
means D1 is killed or redefined by D2.
Code Optimization
Live Variable
A variable x is said to be live at a point p if the variable's value is not killed or redefined by some block. If
the variable is killed or redefined, it is said to be dead.
It is generally used in register allocation and dead code elimination.
Example:

In the above example, the variable a is live at blocks B1,B2, B3 and B4 but is killed at block B5 since its
value is changed from 2 to b + c. Similarly, variable b is live at block B3 but is killed at block B4.
Busy Expression
An expression is said to be busy along a path if its evaluation occurs along that path, but none of its
operand definitions appears before it.
It is used for performing code movement optimization.
Frequently Asked Questions
Define code optimization.
In compiler design, code optimization is a program transformation technique that improves the
intermediate code to consume fewer resources such as CPU, memory, etc., resulting in faster machine
code.
There are two code optimization techniques- local optimization and global optimization.
Differentiate between local optimization and global optimization.
Local optimization applies to a small block of statements. Examples of local optimization are
variable/constant propagation and common subexpression elimination. Global optimization applies to large
program segments like functions, loops, procedures, etc. An example of global optimization is data flow
analysis.
What is a control flow graph?
A control flow graph represents a program in blocks depicting how the program's control is being passed
from one block to another.
What are In[s] and Out[s] in the data flow analysis equation?
Code Optimization
In the data flow analysis equation, In[s] represents the information at the beginning of the statement s, and
Out[s] means the information at the end of the statement s.
What is the reaching definition property of data flow analysis?
According to the reaching definition property of data flow analysis, a definition D is reaching a point x if D
is not killed or redefined before that point. It is generally used in variable/constant propagation.

You might also like