Unit-Iv: Machine Independent Code Optimization
Unit-Iv: Machine Independent Code Optimization
UNIT–IV
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.
Page 1 Page 2
Compiler Design UNIT-IV Compiler Design UNIT-IV
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.
Page 3 Page 4
Compiler Design UNIT-IV Compiler Design UNIT-IV
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;
Page 5 Page 6
Compiler Design UNIT-IV Compiler Design UNIT-IV
Page 7 Page 8
Compiler Design UNIT-IV Compiler Design UNIT-IV
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.
Page 9 Page 10
Compiler Design UNIT-IV Compiler Design UNIT-IV
Page 11 Page 12
Compiler Design UNIT-IV Compiler Design UNIT-IV
{
...
}
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
Loop-invariant code is partially redundant and can be eliminated by using a code-motion technique. n=j+1;
Another example of a partially redundant code can be: }
if (condition) k=n;
{
a = y OP z; 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.
else
Page 13 Page 14
Compiler Design UNIT-IV Compiler Design UNIT-IV
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
Page 15 Page 16