Unit 5 PCC
Unit 5 PCC
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:
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-
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.
The expression that has been already computed before and appears again in the code
for computation
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-
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-
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-
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-
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.
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.
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.
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. }
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. }
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.
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.
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.
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.