CD Unit 4 Compiler Design Jntuk r20
CD Unit 4 Compiler Design Jntuk r20
UNIT-IV
UNIT–V
Machine Independent Optimization. The principle sources of Optimization peephole
Optimization, Introduction to Date flow Analysis, Foundations of Data-Flow Analysis,
Constant Propagation, Partial Redundancy Elimination, Loops in Flow Graph.
Fig (a): Before Elimination Fig(b) After Common sub exp elimination
In above code d=a-d can be replaced by d=b. This technique optimizes the intermediate code. If we
eliminate common sub-expression in one block of code then it is called local common sub-expression
elimination. If we eliminate common sub-expressions among multiple blocks then it is called Global
Common sub-expression elimination. The above example shows local common sub-expression
elimination. In the below example this optimization is performed first locally and then globally.
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 1
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 2
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
In this example we assume that integers occupy four bytes. The assignment x = a[i] is translated into the
two three-address statements
t6 = 4*i
x = a[t6]
as shown in steps (14) and (15).
Similarly, a[ j ] = x becomes
t10 = 4*j
a[t10] = x
in steps (20) and (21).
Figure 5.3 is the flow graph for the program in Fig. 5.2. Block B1 is the entry node. All conditional and
unconditional jumps to statements in Fig. 5.2 have been replaced in Fig. 5.3 by jumps to the block of
which the statements are leaders. In Fig. 5.3, there are three loops. Blocks B2 and B3are loops by
themselves. Blocks B2, B3, B4, and B5 together form a loop, with B2 the only entry point.
t6 = 4*i t6 = 4*i
x = a[t6] x = a[t6]
t7 = 4*i t8 = 4*j
t8 = 4*j t9 = a[t8]
t9 = a[t8] a[t6] = t9
a[t7] = t9 a[t8] = x
t10 = 4*j gotoB2
a[t10] = x
goto B2
(a) Before. (b) After.
Figure 5.4: Local common-subexpression elimination
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 3
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 4
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
y=x
z=3+y
z=3+x
Example 2:
In the example below, the value assigned to i is never used, and the dead store can be eliminated. The
first assignment to global is dead, and the third assignment to global is unreachable; both can be
eliminated.
int global;
void f ()
{
int i;
i = 1; /* dead store */
global = 1; /* dead store */
global = 2;
return;
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 5
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
global = 3; /* unreachable */
}
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 6
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 7
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 8
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 9
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
First we apply local optimization on B1 and B2 independently. As shown in above figure. B1 contains
common sub-expression and copy propagations they are eliminated. After B1, optimization is
performed independently on B2.
After performing local optimization, global optimization is performed on B1 and B2. In this case B2
contains T6=a+b and T7=c+T6 whose values are already calculated in B1. So instead of recomputing
these expressions we can use T1 and T2 directly in B2. After global Optimization the modified code is
shown above figure.
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 10
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 11
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
Available Expression: An expression is said to be available at program point X if along the path it
reaches X. Also an expression is said to be available if none of its operands gets modified before its use.
In above figure expression b*C is available in Blocks B2 and B3. It is used to eliminate common sub
expressions.
Reaching Definition: A definition D reaches point X, if there is a path from the point immediately
following D to X such that D is not killed or not redefined along that path.
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 12
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
Loop-invariant code is partially redundant and can be eliminated by using a code-motion technique.
Another example of a partially redundant code can be:
if (condition)
{
a = y OP z;
}
else
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 13
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
{
...
}
c = y OP z;
We assume that the values of operands (y and z) are not changed from assignment of variable a to
variable c. Here, if the condition statement is true, then y OP z is computed twice, otherwise once. Code
motion can be used to eliminate this redundancy, as shown below:
if (condition)
{
...
tmp = y OP z;
a = tmp;
...
}
else
{
...
tmp = y OP z;
}
c = tmp;
Here, whether the condition is true or false; y OP z should be computed only once.
This code is partially redundant as the expression j + 1 is computed twice in a condition controlled loop.
To optimise this code, we can use partial redundant elimination through loop-invariant code motion
and common subexpression elimination to produce the following optimised code:
if(condition)
{
// code which does not alter j
n=j=1;
i=n;
}
else
{
//code which does not alter j
n=j+1;
}
k=n;
Here, we have removed the redundant assignment and calculation of k = j + 1, instead storing j + 1
inside the temporary variable n and only computed j + 1 once, increasing performance.
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 14
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
Consider the flow graph in above figure with entry node 1. The entry node dominates every node (this
statement is true for every flow graph). Node 2 dominates only itself, since control can reach any other
node along a path that begins with 1 -> 3. Node 3 dominates all but 1 and 2. Node 4 dominates all but 1,
2 and 3, since all paths from 1 must begin with l 2 3 4 or 1 3 4. Nodes 5 and 6 dominate only
themselves, since flow of control can skip around either by going through the other. Finally, 7
dominates 7, 8, 9, and 10; 8 dominates 8, 9, and 10; 9 and 10 dominate only themselves.
Dominator information can be represented in a tree called the dominator tree. In this tree, the entry
node is the root, and each node d dominates only its descendants.
The existence of dominator trees follows from a property of dominators: each node n has a unique
immediate dominator m that is the last dominator of n on any path from the entry node to n
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 15
Downloaded by star boy ([email protected])
lOMoARcPSD|36211483
Dr.B.Srinivas,Asst.Prof.,CSE,ACET,Surampalem
Prepared by Dr.MD.Sirajuddin, Assoc. Professor & Head, Dept. of I.T., KHIT Page 16
Downloaded by star boy ([email protected])