Code Optimization
Code Optimization
Introduction
• Code Optimization is the optional part of a
compiler which can be applied after
intermediate code generation.
• It refers to the technique used by compiler
to improve the execution efficiency of the
object code.
• Code optimization is nothing, but actually
a code improvement.
• Code optimization should reduce
execution time or space taken by object
program.
• Increase in program efficiency should not
change the semantic analysis of the
program.
• Algorithm should not be modified in any
sense.
Classification of Code Optimization
• Machine Dependent
• Machine Independent
Machine Dependent
• Machine Dependent optimization requires
knowledge of target machine.
• It depends on target machine for
instruction set used & addressing modes
used for instructions to produce efficient
target code.
Creteria
• Machine’s Registers should be utilized
efficiently.
• Efficient utilization of instruction set of a
machine.
• Allocation of sufficient number of
resources to improve the execution
efficiency of the program.
• Immediate instructions should be used
whenever required.
Machine Independent
• Machine Independent can be achieved of
target machine for which compiler is
generating code i.e. optimization is not
dependent on machine’s platform.
Creteria
• By Elimination of common sub expression.
• Eliminating unreachable code from the
program.
• Elimination of loop invariant computation.
• By elimination of induction variable.
• By improving the Algorithm Structure.
Advantages
• Program after optimization will occupy less
memory space.
• Program will run faster on performing
optimization
• Produce better object language program.
Principal Sources of Optimization
• We can apply various type of
transformations on the code to increase
the performance of the code.
• Various Principal Sources are
1. Local Optimization
– Common subexpression elimination.
– Constant folding
– Dead code elimination
2. Loop Optimization
– Code Motion
– Induction variable elimination
– Reduction in Strength
3. Data Flow analysis
4. Algorithm Optimization
Local Optimization
Common Sub expression Elimination :
• We can avoid recomputing of the
expression, if its value is already been
computed.
A=B+C+D T1=B+C
E=B+C+M A=T1+D
E=T1+M
Dead Code Elimination
• The dead code is useless code which can
be removed from the program.
a=1
a=1;
if (a==2)
:
{
:
a=a+2;
:
}
Constant Folding
• In this method of optimization, constant
expressions are calculated during
compilation.
Const a = 5; b = 7;
b = a+2;
Loop Optimization
• 90-10 Rule -> Inner loops in a program is
a place where program spend large
amount of time.
• 90-10 Rule states that 90% of the time is
spent in 10% of the code.
• Loops are the greatest source of
optimization.
• If number of instructions are less in inner
loops. The running time will be less.
Code Motion
• It is a technique which moves the code
outside the loop. If expression lying in loop
remains unchanged even after executing
the loop for several times. That expression
can be placed outside the loop.
a=1, b=2; a=1, b=2;
x=a*b;
for (i=1; i<=10; i++)
for (i=1; i<=10; i++)
{ {
sum = sum + i ;
x=a*b;
}
sum = sum + i ;
}
Elimination of Induction Variables
• This optimization will decrease the total
number of instructions and will decrease
the execution time of loop.
• A variable is called induction variable of a
loop if every time it changes its value, it is
incremented or decremented by some
constant.
t1=4*i; t1 = t1 + 4
i= i +1;
loop
loop
Reduction in Strength
• Some operators have different strength
e.g. Strength of * is higher than +.
• Higher Strength operators can be replaced
by lower strength operatos.
for (i=1; i<=5; i++) int a =0;
for (i=1; i<=5; i++)
{
{
a= i * 2; ; a = a+2 ;
}
}
Algorithm Optimization
• Algorithm Optimization is the important
source of optimization in the running time
of a program.
• We can’t optimization until and unless we
don’t have a good algorithm.
• Good algorithm means having less time
complexity.
Running Time of Algo’s
• Non Optimized Bubble Sort = 100 n2 Microseconds
• Optimized Bubble Sort = 50 n2 Microseconds
• Quick Sort = 500 n log n microseconds
• Where n = no. of items to be sorted
Algo. 100 1000
Bubble Sort 1 sec 100 sec
Optimized .5 sec 50 sec
Bubble Sort
Quick Sort .1 sec 1.5 sec
QUICK Sort is 5 times faster than optimized bubble sort for n=100 & 33 times
faster for n=1000.
Data Flow Analysis
• In Data Flow analysis, we analyze the flow
of data from one block to another block of
the program.
• The whole program is divided into blocks.
• Blocks are joined to make a flow graph.
• Finding all such information's can help to
apply optimization called data flow
analysis.
Basic Blocks
• Basic Blocks is a sequence of consecutive
statements in which flow of control enters
at the beginning and when entered are
executed in a sequence without halt or
possibility of branch except at end.
Algorithm for partitioning into
Blocks
1. First determine the Leaders by using Following
Rules.
– The first statement is a Leader.
– Any target statement of conditional or unconditional
goto is a Leader.
– Any Statement that Immediately Follows a goto or
unconditional goto is a Leader.
2. Basic Block is formed by starting at the Leader
Statement and ending just before the next
Leader Statement.
Example
• Consider following program code for computing dot
product of two vectors a and b of length 20. Partition it
into basic blocks :
prod =0 ;
i= 1;
do
{
prod = prod + a[i] * b[i];
i=i+1;
} While (i<=20);
Three Address Code
1. prod=0
2. i =1
3. t1=4*i
4. t2=add(a) -4
5. t3=t2[t1]
6. t4 = addr (b) -4
7. t5 = t4[t1]
8. t6 = t3 * t5
9. prod = prod + t6
10. i = i+1
11. If i <= 20 goto (3)
• BLOCK 1 1.
2.
prod=0
i =1
1. t1=4*i
2. t2=add(a) -4
• BLOCK 2 3. t3=t2[t1]
4. t4 = addr (b) -4
5. t5 = t4[t1]
6. t6 = t3 * t5
7. prod = prod + t6
8. i = i+1
9. If i <= 20 goto (3)
FLOW GRAPH
• A flow graph is a directed graph which
shows the relationships between basic
blocks
• Nodes of the flow graph are represented
by basic blocks
• Edges of flow graph are represented by
directed arrows which tells the order of
execution.
• BLOCK 1 1.
2.
prod=0
i =1
1. t1=4*i
2. t2=add(a) -4
• BLOCK 2 3. t3=t2[t1]
4. t4 = addr (b) -4
5. t5 = t4[t1]
6. t6 = t3 * t5
7. prod = prod + t6
8. i = i+1
9. If i <= 20 goto (3)
LOOP Optimization (Code
Motion)
1. prod=0 1. prod=0
2. i =1 2. i =1
1. t2= add(a) -4
1. t1=4*i 2. t4 = addr (b) -4
2. t2=add(a) -4
3. t3=t2[t1] 1. t1=4*i
4. t4 = addr (b) -4 2. t3=t2[t1]
5. t5 = t4[t1] 3. t5 = t4[t1]
6. t6 = t3 * t5 4. t6 = t3 * t5
7. prod = prod + t6 5. prod = prod + t6
8. i = i+1 6. i = i+1
9. If i <= 20 goto (3) 7. If i <= 20 goto (3)
LOOP Optimization (Induction Variable
Elimination)
1. prod=0 1. prod=0
2. i =1
1. t1=4*i 1. t1=0
2. t3=t2[t1]
3.
1. t1=t1+4
4. t5 = t4[t1]
2. t3=t2[t1]
5. t6 = t3 * t5
3. t5 = t4[t1]
6. prod = prod + t6
4. t6 = t3 * t5
7. i = i+1
5. prod = prod + t6
8. If i <= 20 goto (3)
6. If t1 <= 76 goto B2
DAG Representation of Basic
Blocks
• A directed Acyclia graph which represents
a single basic blocks
Or
• It is a directed graph with no cycles which
shows how value computed by each
statement in basic block is used in
subsequent statements in the block.
• DAG can have nodes with the following types
of label :
– Leaf nodes are labeled by identifiers, variable
names and constant.
– Interior nodes contain operators.
– Interior node also contain computed values i.e.
result or identifier is attached to the operator.
• If operator and variable are both children of
same parent, then variable should be
connected to its parent using R.H.S. edge.
Example: DAG for BB
t1
*
t1 := 4 * i
4 i
t1 := 4 * i
t3 := 4 * i
t2 := t1 + t3 if (i <= 20)goto L1
+ t2 <= (L1)
* t1, t3
i 20
4 i
36
Example: DAG construction
from BB
t1 := 4 * i
* t1
4 i
37
Example: DAG construction
from BB
t1 := 4 * i
t2 := a [ t1 ]
[] t2
* t1
a 4 i
38
Example: DAG construction
from BB
t1 := 4 * i
t2 := a [ t1 ]
t3 := 4 * i
[] t2
* t1, t3
a 4 i
39
Example: DAG construction
from BB
t1 := 4 * i
t2 := a [ t1 ]
t3 := 4 * i
t4 := b [ t3 ]
t4 [] [] t2
* t1, t3
b a 4 i
40
Example: DAG construction
from BB
t1 := 4 * i
t2 := a [ t1 ]
t3 := 4 * i + t5
t4 := b [ t3 ]
t5 := t2 + t4
t4 [] [] t2
* t1, t3
b a 4 i
41
Example: DAG construction
from BB
t1 := 4 * i
t2 := a [ t1 ]
t3 := 4 * i + t5,i
t4 := b [ t3 ]
t5 := t2 + t4
t4 [] [] t2
i := t5
* t1, t3
b a 4 i
42
1. t1=4*i
2. t2=add(a) -4
3. t3=t2[t1]
4. t4 = addr (b) -4
5. t5 = t4[t1]
6. t6 = t3 * t5
7. prod = prod + t6
8. i = i+1
9. If i <= 20 goto (1)
Applications of DAG
• It helps to determine common sub
expressions i.e.
a=4*i a, b
*
b=4*i
4 i
• DAG helps to determine which identifiers
are used in the block or leaf nodes
• For example
a=b+c a
+
b c
• DAG helps to determine which statements
compute values which could be used
outside the block.
Global Data Flow Analysis
• Collect information about the whole
program.
• Distribute the information to each block in
the flow graph.
• In Data Flow analysis, we analyze the flow
of data from one block to another block of
a program.
• It determines definition and use of data in
a program.
• The whole program is divided into blocks.
• Blocks are joined to make a flow graph.
• Finding all such information can help to
apply optimization called data flow
analysis.
Terms used in Global data flow
analysis
• Definition
– Identifier a means an assignment to “a”.
• Use
– Identifier “b” means any occurrence of b as an
operand.
• E.g. statement a= b+c is said to define ‘a’
and to use ‘b’ and ‘c’.
• Point
– It is a position before or after any statement.
• Components of data flow equations
– Sets containing collected information
• in set: information coming into the BB from outside (following
flow of data)
• gen set: information generated/collected within the BB
• kill set: definitions outside BB that define same variables as
are defined in BB.
• out set: information leaving the BB
– Out[B]=gen[B] U in[B] – kill [B]
– In [B] = U out [P]
P
Where P=Predecessor of B
Calculate in[B] and out[B] for
given flow graph.
1: I=2
Block B1
2 : J=I+1
3: I=1 Block B2
4 : J=J+1 Block B3
Block B4 5:J=J-4
Block B5
Block Gen[B] Kill[B]
B1 Gen[B1] = {1,2} Kill [B1]={3,4,5}
B2 Gen[B2]={3} Kill[B2]={1}
B3 Gen[B3]={4} Kill[B3]={2,5}
B4 Gen[B4]={5} Kill[B4]={2,4}
B5 Gen[B5]={ } Kill[B5]={ }