Session 20
Session 20
Session 20
COURSE CODE:21CS3204R
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.
• 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.
2
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.
3
Types of Code Optimization: The optimization process can be broadly classified into two
types :
• 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.
• 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.
4
BASIC BLOCKS AND FLOW GRAPHS
Basic Block:
Basic block means sequence of consecutive statements which are always executed in
sequential manner.
Any basic block contains one entry point and one exit point.
--------
--------
------------
---------------
------------
5
Basic block should not contain conditional control statements(ie if, if-else, switch, for, while etc.) and
unconditional control statements(ie break, continue, goto, exit etc.) in the middle of the block. It may contain at
starting or ending of the block.
ex: x=a+b+c
This instruction is not three address code instruction.
It has 4 addreses (x,a,b,c) and on right hand side it has 2-operators.
Convert the expression into 3-address code instruction by considering temporary variables.
t1=a+b
t2=t1+c
x=t2
6
Now place this three address code in a block all the statements are executed in
sequential manner.
7
Basic Block Construction:
Rule 1: Determining the leader
statement 1: The first statement of a block is a leader.
statement 2: The target of conditional or un conditional statements are leaders.
statement 3: The statement following conditional or un conditional jumps is a
leader .
Rule 2: Determining the basic blocks.
The basic block is formed starting at the first statement of the leader and ends at the next leader
before statement.
8
Ex: algorithm to calculate the products
1. PROD=0
2. I=1
3. T2=addr(A)-4
4. T4=addr(B)-4
5. T1=4*I;
6. T3=T2[T1]
7. PROD=PROD+T3
8. I=I+1
9. If I<=20 goto(5)
10. J=j+1;
11. K=k+1
12. if( j<=5 goto (7)
13. I=i+j
Blocks leaders
i=i+j
PROD=0
I=1 1
T2= addr(A)-4
T4=addr(B)-4 5
7
T1=4*I
T3= T2[T1] 10
PROD=PROD+T3 13
I=I+1
If I<=20 goto (5)
J=j+1
K=k+1
If j<=5 goto(7)
10
BASIC BLOCK AND FLOW GRAPHS
Flow Graph:
A flow graph is a graphical representation of sequence of instructions with control flow
edges.
A flow graph can be defined at the intermediate code or target code level.
The nodes of flow graphs are the basic blocks and flow of control to immediately follow
the node connected by directed arrow.
11
FLOW GRAPH
Blocks
PROD=0
I=1
T2=addr(A)-4
T4=addr(B)-4
T1=4*I
3=T2[T1]
PROD=PROD+T3
I=I+1
If I<=20 goto(5) B2
J=j+1
K=k+1
If j<=5 goto(7)B3
i=i+j
12
OPTIMIZATION OF BASIC BLOCKS
Optimization is applied to the basic blocks after the intermediate code generation phase of
the compiler.
Optimization is the process of transforming a program that improves the code by
consuming fewer resources and delivering high speed. In optimization, high-level codes
are replaced by their equivalent efficient low-level codes.
Optimization of basic blocks can be machine-dependent or machine-independent.
13
There are two types of basic block optimizations:
• Structure preserving transformations
• Algebraic transformations
14
Structure-Preserving Transformations:
The structure-preserving transformation on basic blocks includes:
1. Dead Code Elimination
2. Common Sub expression Elimination
3. Renaming of Temporary variables
4. Interchange of two independent adjacent statements
15
1.Dead Code Elimination:
Dead code is defined as that part of the code that never executes during the program
execution. So, for optimization, such code or dead code is eliminated.
The code which is never executed during the program (Dead code) takes time so, for
optimization and speed, it is eliminated from the code.
Eliminating the dead code increases the speed of the program as the compiler does not
have to translate the dead code.
16
Example:
// Program with Dead code
int main()
{
x=2
if (x > 2)
cout << "code";
// Dead code
Else
cout << "Optimization";
return 0;
}
17
// Optimized Program without dead code
int main()
{
x = 2;
cout << "Optimization"; // Dead Code Eliminated
return 0;
}
18
2.Common Sub expression Elimination:
• In this technique, the sub-expression which are common are used frequently are
calculated only once and reused when needed. DAG ( Directed Acyclic Graph ) is used to
eliminate common sub expressions.
• Example:
19
3.Renaming of Temporary Variables:
• Statements containing instances of a temporary variable can be changed to instances of a
new temporary variable without changing the basic block value.
Example: Statement t = a + b can be changed to x = a + b where t is a temporary variable
and x is a new temporary variable without changing the value of the basic block.
20
4.Interchange of Two Independent Adjacent Statements:
• If a block has two adjacent statements which are independent can be interchanged without
affecting the basic block value.
Example:
t1 = a + b
t2 = c + d
These two independent statements of a block can be interchanged without affecting the value
of the block.
21
Algebraic Transformation:
• Countless algebraic transformations can be used to change the set of expressions
computed by a basic block into an algebraically equivalent set. Some of the algebraic
transformation on basic blocks includes:
1. Constant Folding
2. Copy Propagation
3. Strength Reduction
22
1. Constant Folding:
• Solve the constant terms which are continuous so that compiler does not need to solve
this expression.
Example:
x = 2 * 3 + y ⇒ x = 6 + y (Optimized code)
23
2. Copy Propagation:
It is of two types, Variable Propagation, and Constant Propagation.
Variable Propagation:
x=y ⇒ z = y + 2 (Optimized code)
z=x+2
Constant Propagation:
x=3 ⇒ z = 3 + a (Optimized code)
z=x+a
24
3. Strength Reduction:
Replace expensive statement/ instruction with cheaper ones.
x = 2 * y (costly) ⇒ x = y + y (cheaper)
x = 2 * y (costly) ⇒ x = y << 1 (cheaper)
25
Thank you
26