Compiler Optimizations Presentation
Compiler Optimizations Presentation
1. Source Code
2. Intermediate Code
(3) t1 = 4 * i
(4) t2 = a[t1]
(5) t3 = 4 * i
(6) t4 = b[t3]
(7) t5 = t2 * t4
(8) t6 = prod + t5 B2
(9) prod = t6
(10) t7 = i + 1
(11) i = t7
(12) if i <= 20 goto (3)
(13) ……. B3
5/21/2019 VNIT Nagpur 11
Building Control Flow Graph(CFG)
There is a directed edge from basic block B1 to
basic block B2 in the CFG if:
1. The leader of B2 immediately follows the last
statement of B1, and the last statement of B1 is
not an unconditional branch.
2. The leader of B2 is a target of the last statement of
B1 which is a branch.
A basic block whose leader is the first intermediate
language statement is called the start node.
B1
B2
B3
1
1
2
3 2 3
4 4
5 6
7 5 6 7
8
8
9 10
9 10
5/21/2019 VNIT Nagpur 15
Identifying Loops
A strongly-connected component (SCC) of a graph
G = (N, E, s) is a subgraph G’ = (N’, E’, s’) in
which there is a path from each node in N’ to every
node in N’.
2 3
8
9 10
8
9 10
a program.
For this we must take into consideration the data-flow
and the control flow in the program and set up
dataflow equations for each basic block.
To set up dataflow equations we need to compute
gen, kill, in and out sets defined as follows:
B1 d1: i := m-1
d2: j := n
d3: a := u1 Gen(B1) = {d1, d2, d3}
Kill(B1) = {d4, d5, d6, d7}
B3 Gen(B3) = {d6}
d6: a := u2 kill (B3) = {d3}
in(B1) =
out(B1) = {d1, d2, d3}
in(B2) =
out(B2) = {d4, d5} To simplify the representation, the in(B)
and out(B) sets are represented by
in(B3) = bit strings. Assuming the representation
out(B3) = {d6}
d1d2d3 d4d5d6d7 we obtain:
in(B4) =
out(B4) = {d7}
Initial
Block
in(B) out(B)
B1 000 0000 111 0000
B2 000 0000 000 1100
B3 000 0000 000 0010
B4 000 0000 000 0001
Gen(B3) = {d6}
B1 d1: i := m-1 kill (B3) = {d3}
d2: j := n
d3: a := u1 Gen(B4) = {d7}
kill (B4) = {d1, d4}
B2 d4: i := i+1
d5: j :=j - 1 First Iteration
Block
in(B) out(B)
B3 B1 000 0000 111 0000
d6: a := u2 B2 111 0010 001 1110
B3 011 1100 010 1110
B4 d7: i := u3 B4 011 1100 011 0101
B2 d4: i := i+1
d5: j :=j - 1
B3
d6: a := u2 Second Iteration
Block
on(B) out(B)
B4 d7: i := u3 B1 000 0000 111 0000
B2 111 1110 001 1110
B3 001 1110 000 1110
B4 001 1110 001 0111
5/21/2019 VNIT Nagpur 28
Final Values
in(B1) =
out(B1) = {d1, d2, d3}
B3
d6: a := u2 Second Iteration
Block
on(B) out(B)
B4 d7: i := u3 B1 000 0000 111 0000
B2 111 1110 001 1110
B3 001 1110 000 1110
B4 001 1110 001 0111
5/21/2019 VNIT Nagpur 29
Data Flow Analysis
Computing u-d chains
If the use of name a in block B is preceded by its
definition in the block B, then u-d chain of a will
contain the last definition of a prior to this use of a in
the block B.
If the use of name a in the block B is not preceded by
its definition in the block B, then the u-d chain for
this use of a consists of all definitions of a in in(B).
Pre-header
B2 d4: i := i+1
d5: j :=j - 1
B3
d6: a := u2
B4 d7: i := u3
x= 1
x= 2
y =x
x= 2 Pre-header
y =x
x=3
x= 2
y =x
x= 3 Pre-header
x=2
y =x
B2 h =a+c b=a+d
B3
e=f+1
j = a+c
k =f+1
B4
h = e*f
B2 { a+c } Φ
B3 { a+d, f+1} { b+c, e*f}
B2 h =x + y b= x + y
B3
….
j=x+y
….
B4 common
sub-expression
B2 temp = x +y temp = x + y
B3
h = temp b= temp
….
j = temp
….
B4
Point p
a =x
b = a+1
B2 c = c+ b
a = b*2
if a < 9 goto b2
B3 return c
B3 {c} Φ Φ {c}
B2 {a,c} {a,c}
B3 Φ {c}
Advantage
The above transformation will reduce the number of
times conditional will be tested to N. That means it
reduces the frequency of execution of the
conditional statement
Disadvantages
1. Loop structure may become more complex
for (i=1;i<N;i++)
{
a[i] = a[i] + k1;
d[i] = a[i] –b[i] + k2;
}
After Fusion
Disadvantage
It may lead to formation of loops with more
complex flow of control.
for(i=1;i<=N;i++)
a[i] = (x + y) * b[i];
Before Peeling
while(i<=100)
{
a[i] = 0; i++;
a[i] = 0; i++;
}
After Unrolling
s1
s2
s3 s4
S2 – S3
S1 S4