Module 5 - Code Optimization
Module 5 - Code Optimization
Code Optimization
r
Front Intermedi Generato Target
End ate Code Code
Progra
m
Compiler can
User can optimize
optimize
Compile time evaluation
• Compile time evaluation means shifting of computations from
run time to compile time.
• There are two methods used to obtain the compile time
evaluation.
Folding
• In the folding technique the computation of constant is done at
compile time instead of run time.
Example : length = (22/7)*d
• Here folding is implied by performing the computation of 22/7
at compile time.
Cont.,
Constant/ Variable propagation
• In this technique the value of variable is replaced and
computation of an expression is done at compilation
time.
Example : pi = 3.14; r = 5;
Area = pi * r * r;
• Here at the compilation time the value of pi is replaced
by 3.14 and r by 5 then computation of 3.14 * 5 * 5 is
done during compilation.
Principal Sources of
Optimization
Principal Sources of Optimization
Semantics-Preserving Transformations
1. Common sub expressions elimination
3. Code Motion
4. Copy Propagation
6. Constant folding
1. Common sub expressions elimination
• The common sub expression is an expression appearing
repeatedly in the program which is computed previously.
• If the operands of this sub expression do not get changed at all
then result of such sub expression is used instead of re-
computing it each time.
• Example:
t1 := 4 * i a=b+ a=b+c
c b=a-d
t2 := a[t1]
b=a- c=b+c
t3 := 4 * j
d d=b
t4 : = 4 * i c=b+
t5:= n c
t6 := b[t4]+t5 d=a-
d
2. Dead code elimination
• The variable is said to be dead at a point in a program if the
value contained into it is never been used.
• The code containing such a variable supposed to be a dead
code.
• Example:
i=0;
if(i==1)
Dead Code
{
a=x+5;
}
while(i<=max- N=max-1;
1) while(i<=N)
{ {
a=200; a=200;
sum=sum+a[i sum=sum+a[i];
while(a>0) b=x+y;
]; }
{ while(a>0)
} {
b=x+y;
a--;
if(a%b==0) if(a%b==0)
printf(“%d”,a) printf(“%d”,a)
;
4. Copy Propagation
• Copy propagation means use of one variable instead of
another.
• Example:
x = pi;
area = x * r * r;
x=a x=a
y=x* y=a*b
b z=a*c
z=x*c
area = pi * r * r;
5. Induction variable elimination & Reduction in
Strength
• Priority of certain operators is higher than others.
• For instance strength of * is higher than +.
• In this technique the higher strength operators can be replaced
by lower strength operators.
• Addition of a constant is cheaper than a multiplication. So we can
replace multiplication with an addition within the loop.
• Multiplication is cheaper than exponentiation. So we can replace
exponentiation with multiplication within the loop.
• Example:
for(i=1;i<=50;i++) temp=7;
{ for(i=1;i<=50;i++)
count = i*7; { count = temp;
} temp =
temp+7;
}
• Here, we get the count values as 7, 14, 21…. and so on.
Cont… [Another Example]
6. Constant Folding
• If the value of an expression is constant then use constant
instead of the expression
D2: y=y+2 B2
D3: x=y+2 B3
Sequence of Statements
Data flow equations for reaching
definitions – [if..else & loop]
Basic Block & Flow
Graph
Basic Blocks
• A basic block is a sequence of consecutive statements in which
flow of control enters at the beginning and leaves at the end
without halt or possibility of branching except at the end.
• The following sequence of three-address statements forms a
basic block:
t1 := a*a
t2 := a*b
t3 := 2*t2
t4 := t1+t3
t5 := b*b
t6 := t4+t5
Algorithm: Partition into basic blocks
Input: A sequence of three-address statements.
Output: A list of basic blocks with each three-address statement
in exactly one block.
Method:
1. We first determine the set of leaders, for that we use the
following rules:
a. The first statement is a leader.
b. Any statement that is the target of a conditional or unconditional goto is
a leader.
c. Any statement that immediately follows a goto or conditional goto
statement is a leader.
2. For each leader, its basic block consists of the leader and all
statements up to but not including the next leader or the end
of the program.
Example1: Partition into basic blocks
begin
prod := 0; (1) prod := 0 Leader
Block B1(2) i := 1
i := 1; (3) t1 := 4*i
Leader
do (4) t2 := a [t1]
(5) t3 := 4*i
prod := prod + a[t1] * (6) t4 :=b [t3]
b[t2]; (7) t5 := t2*t4
i := i+1; (8) t6 := prod +t5
(9) prod := t6 Block B2
while i<= 20 (10) t7 := i+1
end (11) i := t7
(12) if i<=20 goto
(3)
Three Address
Code
Flow Graph
• We can add flow-of-control information to the set of basic blocks making up a
program by constructing a direct graph called a flow graph.
• Nodes in the flow graph represent computations, and the edges represent the
flow of control.
• Example of flow graph for following three address code:
• Example:
• x : = x + 0 or x : = x * 1 can be eliminated from a basic block.
• The exponential statement x : = y * * 2 can be replaced by x : = y *
y.
Peephole Optimization
Peephole optimization
• Peephole optimization is a simple and effective
technique for locally improving target code.
4. Algebraic simplification
UNREACHABLE
CODE can be
removed
3. Flow of Control Optimization
• The unnecessary jumps can be eliminated in either the
intermediate code or the target code by the following types of
peephole optimizations.
• We can replace the jump sequence.
Goto L1
…… Goto L2
L1: goto L2
• Reduction in strength
• Certain machine instructions are cheaper than the other.
• In order to improve performance of the intermediate code we can
replace these instructions by equivalent cheaper instruction.
• For example, x * x is cheaper than x^2, x + x is cheaper than 2 * x, x
<< 1 is cheaper than x * 2, x >> 1 is cheaper than x/2
• Similarly addition and subtraction are cheaper than multiplication and
division. So we can add effectively equivalent addition and subtraction
for multiplication and division.
5. Use of Machine idioms
• The target instructions have equivalent machine instructions
for performing some operations.
• We can replace these target instructions by equivalent machine
instructions in order to improve the efficiency.
DAG
DAG
Cont…
DAG
DAG Representation of Basic Block
Example:
(1) t1 := 4*i t6, prod
(2) t2 := a [t1] +¿
(3) t3 := 4*i prod ∗ t5
a b
(8) t7 := i+1 4 i 1
(9) i := t7
(10) if i<=20
goto (1)
Generation of Code
from DAGs
Generation of Code from DAGs
• Methods generating code from DAGs are:
1. Rearranging Order
2. Heuristic ordering
3. Labeling algorithm
Rearranging Order
• The order of three address code affects the cost of the object
code being generated.
• By changing the order in which computations are done we can
obtain the object code with minimum cost.
• Example:
t1:=a+b t
t2:=c+d − 4
t
t3:=e-t2 t
1
− 3
t4:=t1-t3
Three Address +¿ e
+¿
t
2
Code a b
c d
Example: Rearranging Order
t2:=c+d
t1:=a+b
t2:=c+d
Re-arrange t3:=e-t2
t3:=e-t2 t1:=a+b
t4:=t1-t3 MOV a, R0 t4:=t1-t3
Three Address Code ADD b, R0 Three Address
Code MOV c, R0
MOV c, R1
ADD d, R1 ADD d, R0
MOV R0, t1 MOV e, R1
MOV e, R0 SUB R0, R1
SUB R1, R0 MOV a, R0
MOV t1, R1 ADD b, R0
SUB R0, R1 SUB R1, R0
MOV R1, t4 MOV R0, t4
Assembly Code Assembly Code
Algorithm: Heuristic Ordering
Obtain all the interior nodes. Consider these interior nodes as unlisted
nodes.
while(unlisted interior nodes remain)
{
pick up an unlisted node n, whose parents have been listed
list n;
while(the leftmost child m of n has no unlisted parent AND is not
leaf)
{
List m;
n=m;
}
}
Example: Heuristic Ordering
∗𝟏 𝐼𝑛𝑡𝑒𝑟𝑖𝑜𝑟 𝑛𝑜𝑑𝑒𝑠=123 4568
𝟐
+¿ − 𝟑𝑈𝑛𝑙𝑖𝑠𝑡𝑒𝑑𝑛𝑜𝑑𝑒𝑠=123 456 8
Pick up an unlisted node,
∗𝟒 whose parents have been listed
𝟏
−𝟓 𝟖 +¿ Left child of 1 =
𝟐
Parent 1 is listed so
Left child of 2 =
𝟔
Parent 5 is not listed so
𝟔 +¿ 𝟕𝑐 𝑑11 𝑒 12 list 2 can’t list 6
𝟗𝑎 𝑏 10
Listed Node 1 2
Example: Heuristic Ordering
∗𝟏 𝐼𝑛𝑡𝑒𝑟𝑖𝑜𝑟 𝑛𝑜𝑑𝑒𝑠=123 4568
+¿
𝟐 − 𝟑𝑈𝑛𝑙𝑖𝑠𝑡𝑒𝑑𝑛𝑜𝑑𝑒𝑠=123 456 8
Pick up an unlisted node,
∗𝟒 whose parents have been
listed
−𝟓 𝟖 +¿ Rightchild of 1 =
𝟑
Parent 1 is listed so
Leftchild of 3 =
𝟒
Parent 2,3 are listed
𝟔+¿ 𝟕𝑐 𝑑11 𝑒 12 list 3 so list 4
𝟗𝑎 𝑏 10
Listed Node 1 2 3 4
Example: Heuristic
Ordering
∗𝟏 𝐼𝑛𝑡𝑒𝑟𝑖𝑜𝑟 𝑛𝑜𝑑𝑒𝑠=123 4568
𝟐
+¿ − 𝟑𝑈𝑛𝑙𝑖𝑠𝑡𝑒𝑑𝑛𝑜𝑑𝑒𝑠=123 456 8
Pick up an unlisted node,
∗𝟒 whose parents have been
listed
−𝟓 𝟖 +¿ Leftchild of 4 =
𝟓
Parent 4 is listed so
Leftchild of 5 =
𝟔
Parent 2,5 are listed so
𝟔 +¿ 𝟕𝑐 𝑑11 𝑒 12 list 5 list 6
𝟗𝑎 𝑏 10
Listed Node 1 2 3 4 5 6
Example: Heuristic Ordering
∗𝟏 𝐼𝑛𝑡𝑒𝑟𝑖𝑜𝑟 𝑛𝑜𝑑𝑒𝑠=123 4568
𝟐
+¿ − 𝟑𝑈𝑛𝑙𝑖𝑠𝑡𝑒𝑑𝑛𝑜𝑑𝑒𝑠=123 456 8
Pick up an unlisted node,
∗𝟒 whose parents have been
listed
−𝟓 𝟖 +¿ Rightchild of 4 =
𝟖
Parent 4 is listed so
+¿
list 8
𝟔 𝟕𝑐 𝑑11 𝑒 12
𝟗𝑎 𝑏 10
Listed Node 1 2 3 4 5 6 8
Example: Heuristic Ordering
∗𝟏
Listed Node 1 2 3 4 5 6 8
𝟐
+¿ −𝟑 Reverse Order for three address code = 8 6
54321
∗𝟒 t8=d+e
t6=a+b
t5=t6-c
−𝟓 𝟖+¿ t4=t5*t8
t3=t4-e
Optimal Three
Address
code
𝟔 +¿ 𝟕𝑐 𝑑11 𝑒 12 t2=t6+t4
t1=t2*t3
𝟗𝑎 𝑏 10
Labeling Algorithm
• The labeling algorithm is used to find out how many registers
will be required by a program to complete its execution.
• Using labeling algorithm, the labeling can be done to tree by
visiting nodes in bottom up order.
• For computing the label at node n with the label L1 to left child
and label L2 to right child as,
𝐩𝐨𝐬𝐭𝐨𝐫𝐝𝐞𝐫 𝐭𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥 =𝐚𝐛𝐭 𝟏 𝐞𝐜𝐝𝐭 𝟐 𝐭 𝟑 𝐭 𝟒 So, this three address code will require
maximum 2 registers to complete its
execution.
Three Address Code
Flow Graph
Loops in Flow Graphs
Control Flow Graph (CFG)
• Control Flow Graph Loops
• A graph which may
contain loops, known as
strongly connected
components
• Loop
• A directed graph whose
nodes can reach all other
nodes along some path
• Goto statements can
create any loops whereas
break statements creates
addon exits
Dominators
• In a flow graph, a node ‘d’ is said to dominate node ‘n’ if
every path to node n from initial node goes through d only.
• This can be denoted as ’d dom n'.
• Every initial node dominates all the remaining nodes in the
flow graph.
• Every node dominates itself. 1
3 4
A flow
graph
Natural Loops
• There are two essential properties of natural loop:
1. A loop must have single entry point, called the header. This point dominates all
nodes in the loop.
2. There must be at least one way to iterate loop i.e. at least one path back to the loop
header.
• Back edge is an edge a b whose head, b dominates its tail, a
• Given a back edge n d, we define the natural loop of the edge to be d
plus the set of nodes that can reach n without going Header
through1 d.
• Node d is the header of the loop.
2
61 is natural loop.
3 4
6
Example
Inner Loops
• The inner loop is a loop that contains no other loop.
• Here the inner loop is 42 that mean edge given by 2-3-4.
5
Preheader
• Several loop transformation require us to move statements
“before the header”. [Ex: code motion, strength reduction, …]
• Therefore, begin treatment of a loop by creating a new block,
called the preheader.
• The preheader has only the header as successor.
Preheader
Header
B0
Reducible Flow Graph
• The reducible graph is a flow graph, if and only if we can
partition the edges into two disjoint groups, in which often
called the forward edges and backward edges.
• These edges have following properties,
1. The forward edge forms an acyclic graph in which every node has to
be reached from the initial node.
2. The back edges are such edges whose head dominates their tail.
1
3 4
5
Non-reducible Flow Graph
• A non reducible flow graph is a flow graph in which:
1. There are no back edges.
2. Forward edges may produce cycle in the graph.
2 3