0% found this document useful (0 votes)
4 views

Module 5 - Code Optimization

Module 5 discusses code optimization techniques used in compilers, including compile-time evaluation methods like folding and constant propagation. It covers principal sources of optimization such as common sub-expression elimination, dead code elimination, and peephole optimization, which improve code efficiency. The module also introduces data flow analysis and basic block transformations to enhance the quality of generated code.

Uploaded by

himacharan262
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Module 5 - Code Optimization

Module 5 discusses code optimization techniques used in compilers, including compile-time evaluation methods like folding and constant propagation. It covers principal sources of optimization such as common sub-expression elimination, dead code elimination, and peephole optimization, which improve code efficiency. The module also introduces data flow analysis and basic block transformations to enhance the quality of generated code.

Uploaded by

himacharan262
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 72

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

2. Dead code elimination

3. Code Motion

4. Copy Propagation

5. Induction variable elimination & Reduction in Strength

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;
}

• If statement is a dead code as this condition will never get


3. Code Motion
• Optimization can be obtained by moving some amount of code
outside the loop and placing it just before entering in the loop.
• This method is also called loop invariant computation.
• Example:

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

• Ex: pi = 22/7  pi = 3.14..


Global Data Flow
Analysis
Global Data Flow Analysis
• To do code optimization and code generation
• Compiler needs to collect information about the whole program
• Distribute it to each block in the whole program
• Data flow equations are the equations representing the
expressions that are appearing in the flow graph.
• Data flow information can be collected by setting up and
solving systems of equations that relate information at various
points in a program.
• The data flow equation written in a form of equation such that,

• Data flow equation can be read as “the information at the end of a


statement is either generated within a statement, or enters at the
beginning and is not killed as control flows through the statement”.
Path
• A path from p1 to pn is sequence of points p1, p2, …
pn such that for each i between 1 and n-1, either,
i. Pi – point immediately preceding the statement
Pi+1 – point immediately following that statement in
the same block

ii. Pi – End of some block


Pi+1 – beginning of a successor block
Reaching Definition
• A definition reaches at the point if there is a path from to
along which is not killed.
• A definition of variable is killed when there is a redefinition of
.
D1: y=2 B1

D2: y=y+2 B2

D3: x=y+2 B3

• The definition is reaching definition for block , but the


definition is not reaching definition for block , because it is
killed by definition in block .
Data Flow Properties
• A program point containing the definition is called Definition
point.
• A program point at which a reference to a data item is made is
called Reference point.
• A program point at which some evaluating expression is given
is called Evaluation point.
W1:x=3 Definition
point
W2: y=x Reference
point
W3: z=a*b Evaluation
point
Dataflow analysis of structured
programs
S  id:E | S; S | if E then S else S | do S while E
E  id+id | id
Example:
Data flow equations for reaching
definitions – [statement(s)]
Single
Statement Newly generated definition, d for a
All previous definition for a will be
killed except the newly generated
definition, d

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:

Construction of Flow Graph prod=0 Block B1


1. Draw directed edge from block1 to i=1
block2 if t1 := 4*i
a. Conditional or unconditional jump t2 := a [t1]
from block1 to block2 t3 := 4*i
Flow t4 :=b [t3]
b. Block2 immediately follows block1
Graph t5 := t2*t4 Block B2
t6 := prod +t5
prod := t6
t7 := i+1
i := t7
if i<=20 goto B2
Transformation on
Basic Blocks
Transformation on Basic Blocks
• A number of transformations can be applied to a basic block
without changing the set of expressions computed by the block.
• Many of these transformations are useful for improving the
quality of the code.
• Types of transformations are:
I. Structure preserving transformation
II. Algebraic transformation
I. Structure Preserving Transformations
• Structure-preserving transformations on basic blocks are:
1. Common sub-expression elimination
2. Dead-code elimination
3. Renaming of temporary variables
4. Interchange of two independent adjacent statements
1. Common sub-expression elimination
• Consider the basic block,
a:= b+c
b:= a-d
c:= b+c
d:= a-d
• The second and fourth statements compute the same
expression, hence this basic block may be transformed into the
equivalent block:
a:= b+c
b:= a-d
c:= b+c
d:= b
2. Dead-code elimination
• Suppose s dead, that is, never subsequently used, at the point where the
statement appears in a basic block.
• Above statement may be safely removed without changing the value of the
basic block.
3. Renaming of temporary variables
• Suppose we have a statement
t:=b+c, where t is a temporary variable.
• If we change this statement to
u:= b+c, where u is a new temporary variable,
• Change all uses of this instance of t to u, then the value of the
basic block is not changed.
• In fact, we can always transform a basic block into an
equivalent block in which each statement that defines a
temporary defines a new temporary.
• We call such a basic block a normal-form block.
4. Interchange of two independent adjacent
statements
• Suppose we have a block with the two adjacent statements,
t1:= b+c
t2:= x+y
• Then we can interchange the two statements without affecting
the value of the block if and only if neither nor is and neither
nor is .
• A normal-form basic block permits all statement interchanges
that are possible.
II. Algebraic Transformation
• Countless algebraic transformation can be used to change
the set of expressions computed by the basic block into an
algebraically equivalent set.

• The useful ones are those that simplify expressions or


replace expensive operations by cheaper one.

• 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.

• Replaces short sequences of target instructions


(PEEPHOLE) by a shorter or faster sequence.
• It is applied to improve the performance of the target
program

• Peephole is a small, moving window on the target


program.
Techniques of peephole
optimization
1. Redundant Loads & Stores

2. Unreachable code elimination

3. Flow of Control Optimization

4. Algebraic simplification

5. Use of Machine idioms


Redundant Loads & Stores
• Especially the redundant loads and stores can be
eliminated in following type of transformations.
• Example:
MOV R0,x
MOV x,R0
• We can eliminate the second instruction since x is in
already R0 unless it is referred by some other statements
[preceded by any label].
2. Unreachable code elimination

OPTIMIZED CODE after


removing unnecessary jump
statements

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

• It may be possible to eliminate the statement L1: goto L2


provided it is preceded by an unconditional jump. Similarly, the
sequence can be replaced by:
If a<b goto L1
…… If a<b goto L2
L1: goto L2
4. Algebraic simplification
• Peephole optimization is an effective technique for algebraic
simplification.
• The statements such as x = x + 0 or x := x* 1 can be eliminated
by peephole optimization.

• 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.

• Example: Some machines have auto-increment or auto-


decrement addressing modes.
• These modes can be used in code for statement like i=i+1 
INCR i
i=i-1  DECR i
DAG Representation of
Basic Block
Algorithm: DAG Construction
We assume the three address statement could of following types:
Case (i) x:=y op z
Case (ii) x:=op y
Case (iii) x:=y
With the help of following steps the DAG can be constructed.
• Step 1: If y is undefined then create node(y). Similarly if z is undefined
create a node (z)
• Step 2:
Case(i) create a node(op) whose left child is node(y) and node(z) will
be the right child. Also check for any common sub expressions.
Case (ii) determine whether is a node labeled op, such node will have
a child node(y).
Case (iii) node n will be node(y).
• Step 3: Delete x from list of identifiers for node(x). Append x to the list of
attached identifiers for node n found in 2.
Examples

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

(4) t4 :=b [t3] t4


(5) t5 := t2*t4 [] t2
[] ≤(1)
t1,t3
(6) t6 := prod +t5
(7) prod := t6
∗ +¿ t7, i 20

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,

• We start in bottom-up fashion and label left leaf as 1 and right


leaf as 0.
Example: Labeling Algorithm
t1:=a+b
t2:=c+d 𝑳𝒂𝒃𝒆𝒍(𝒏)=¿
t3:=e-t2
t4:=t1-t3
Three Address Code 𝟐t
−4
𝟐t
t
1
−3
𝟏
+¿ 𝟏e
+¿
𝟏t
2
𝟏a b𝟎
𝟏c d𝟎

𝐩𝐨𝐬𝐭𝐨𝐫𝐝𝐞𝐫 𝐭𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥 =𝐚𝐛𝐭 𝟏 𝐞𝐜𝐝𝐭 𝟐 𝐭 𝟑 𝐭 𝟒 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

• Node 1 is initial node and it dominates every node as it is


Cont.,
Dominator
tree

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
61 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 42 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

You might also like