Code Optimisation 1_ Basics
Code Optimisation 1_ Basics
Swati Jaiswal
2024
Different Phases of a Compiler
Lexical Analysis
Syntax Analysis
Semantic Analysis
Intermediate Code Generator
Code Optimizer
Code Generator
2 / 45
Code Optimizer
Intermediate representation
Machine-Independent
Code Optimizer
Intermediate representation
Code Generator
Machine Dependent
Code Optimizer
3 / 45
Code Optimization
4 / 45
Code Optimization
4 / 45
Code Optimization
4 / 45
Code Optimization
5 / 45
Code Optimization
5 / 45
Code Optimization
5 / 45
Code Optimization
5 / 45
Code Optimization
6 / 45
Code Optimization
Machine Independent
6 / 45
Code Optimization
Machine Independent
Machine Dependent
6 / 45
Code Optimization
Machine Independent
◮ Improve intermediate code to generate better target code
Machine Dependent
6 / 45
Code Optimization
Machine Independent
◮ Improve intermediate code to generate better target code
Machine Dependent
◮ Use properties of target machine
6 / 45
Part I
Motivating Example
Motivating Example
a =b ×c
8 / 45
Motivating Example
a =b ×c
g =a−e
8 / 45
Motivating Example
a =b ×c
g =a−e
d =b ×c
8 / 45
Motivating Example
a =b ×c
g =a−e
d =b ×c
e =f +d
8 / 45
Motivating Example
a =b ×c
g =a−e
d =b ×c
e =f +d
print e
8 / 45
Motivating Example
a =b ×c
g =a−e
Can this program be optimized?
d =b ×c
e =f +d
print e
8 / 45
Motivating Example
a =b ×c
g =a−e
d =b ×c
e =f +d
print e
8 / 45
Motivating Example
a =b ×c Expression b × c is
computed twice and
g =a−e values of b and c does not
change in between the two
d =b ×c computations
Common subexpression
elimination
e =f +d
print e
8 / 45
Motivating Example
a =b ×c
g =a−e
d=a
e =f +d
print e
8 / 45
Motivating Example
a =b ×c
g =a−e
d=a
e =f +d
print e
8 / 45
Motivating Example
a =b ×c
Use a instead of d in the
expression e = f + d
g =a−e
as values of a and d are not
modified in between the
d=a assignment and use
Copy propagation
e =f +d
print e
8 / 45
Motivating Example
a =b ×c
g =a−e
d=a
e =f +a
print e
8 / 45
Motivating Example
a =b ×c
g =a−e
d=a
e =f +a
print e
8 / 45
Motivating Example
a =b ×c
Variables g and d are not
g =a−e used in the program
Assignment to them can be
d=a considered as dead code
Dead code elimination
e =f +a
print e
8 / 45
Motivating Example
a =b ×c
g =a−e
d=a
e =f +a
print e
8 / 45
Motivating Example
Original code
a =b ×c
g =a−e
d =b ×c
e =f +d
print e
8 / 45
Motivating Example
a =b ×c a =b ×c
g =a−e g =a−e
d =b ×c d =b ×c
e =f +d e =f +a
print e print e
8 / 45
Motivating Example
i = m - 1; j = n; v = a[n];
while (1) {
do i = i + 1; while (a[i] < v);
do j = j - 1; while (a[j] > v);
if (i >= j) break;
x = a[i]; a[i] = a[j]; a[j] = x; /* swap a[i], a[j] */
}
x = a[i]; a[i] = a[n]; a[n] = x; /* swap a[i], a[n] */
9 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
t11 = 4*i
x = a [t6]
x = a [t11]
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t13]
B6
a[t7]=t9
a[t12]=t14
t10 = 4*j
t15 = 4*n
a[t10]=x
a[t15]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
t11 = 4*i
x = a [t6]
x = a [t11]
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t13]
B6
a[t7]=t9
a[t12]=t14
t10 = 4*j
t15 = 4*n
a[t10]=x
a[t15]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
t11 = 4*i
x = a [t6]
x = a [t11]
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t13]
B6
a[t6]=t9
a[t12]=t14
t10 = 4*j
t15 = 4*n
a[t8]=x
a[t15]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
t11 = 4*i
x = a [t6]
x = a [t11]
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t13]
B6
a[t6]=t9
a[t12]=t14
t10 = 4*j
t15 = 4*n
a[t8]=x
a[t15]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
t11 = 4*i
x = a [t6]
x = a [t11]
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t13]
B6
a[t6]=t9
a[t11]=t14
t10 = 4*j
t15 = 4*n
a[t8]=x
a[t13]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
t11 = 4*i
x = a [t6]
x = a [t11]
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t13]
B6
a[t6]=t9
a[t11]=t14
t10 = 4*j
t15 = 4*n
a[t8]=x
a[t13]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
t11 = 4*i
x = t3
x = t3
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t13]
B6
a[t2]=t9
a[t2]=t14
t10 = 4*j
t15 = 4*n
a[t8]=x
a[t13]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
x = t3
x = t3
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t13]
B6
a[t2]=t9
a[t2]=t14
t10 = 4*j
t15 = 4*n
a[t8]=x
a[t13]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
x = t3
x = t3
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t13]
B6
a[t2]=t5
a[t2]=t14
t10 = 4*j
t15 = 4*n
a[t8]=x
a[t13]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
x = t3
x = t3
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t13]
B6
a[t2]=t5
a[t2]=t14
t10 = 4*j
t15 = 4*n
a[t8]=x
a[t13]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
x = t3
x = t3
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t1]
B6
a[t2]=t5
a[t2]=t14
t10 = 4*j
t15 = 4*n
a[t8]=x
a[t13]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
x = t3
x = t3
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t1]
B6
a[t2]=t5
a[t2]=t14
t10 = 4*j
t15 = 4*n
a[t8]=x
a[t13]=x
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
x = t3
x = t3
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t1]
B6
a[t2]=t5
a[t2]=t14
t10 = 4*j
t15 = 4*n
a[t8]=t3
a[t13]=t3
goto B2
10 / 45
Motivating Example
i = m-1
j=n
B1 t1 = 4*n
v = a[t1]
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
t6 = 4*i
x = t3
x = t3
t7 = 4*i
t12 = 4*i
t8=4*j
t13=4*n
B5 t9=a[t8]
t14=a[t1]
B6
a[t2]=t5
a[t2]=t14
t10 = 4*j
t15 = 4*n
a[t8]=t3
a[t13]=t3
goto B2
10 / 45
Motivating Example
i = m-1
j=n
t1 = 4*n
B1 v = a[t1]
t2 = 4 * i
t4 = 4 * j
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
a[t2]=t5 t14=a[t1]
B5 a[t8]=t3 a[t2]=t14 B6
goto B2 a[t13]=t3
11 / 45
Motivating Example
i = m-1
j=n
t1 = 4*n
B1 v = a[t1]
t2 = 4 * i
t4 = 4 * j
i = i+1
t2 = 4 * i
B2 t3 = a[t2]
if t3<v goto B2
j=j-1
t4 = 4 * j
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
a[t2]=t5 t14=a[t1]
B5 a[t8]=t3 a[t2]=t14 B6
goto B2 a[t13]=t3
11 / 45
Motivating Example
i = m-1
j=n
t1 = 4*n
B1 v = a[t1]
t2 = 4 * i
t4 = 4 * j
i = i+1
t2 = t2 + 4
B2 t3 = a[t2]
if t3<v goto B2
j=j-1
t4 = t4 - 4
B3 t5 = a[t4]
if t5>v goto B3
B4 if i >= j goto B6
a[t2]=t5 t14=a[t1]
B5 a[t8]=t3 a[t2]=t14 B6
goto B2 a[t13]=t3
11 / 45
Motivating Example
i = m-1
j=n
t1 = 4*n
B1 v = a[t1]
t2 = 4 * i
t4 = 4 * j
i = i+1
t2 = t2 + 4
B2 t3 = a[t2]
if t3<v goto B2
j= j - 1
t4 = t4 - 4
B3 t5 = a[t4]
if t5>v goto B3
B4 if t2 >= t4 goto B6
a[t2]=t5 t14=a[t1]
B5 a[t8]=t3 a[t2]=t14 B6
goto B2 a[t13]=t3
11 / 45
Motivating Example
i = m-1
j=n
t1 = 4*n
B1 v = a[t1]
t2 = 4 * i
t4 = 4 * j
i = i+1
t2 = t2 + 4
B2 t3 = a[t2]
if t3<v goto B2
j=j-1
t4 = t4 - 4
B3 t5 = a[t4]
if t5>v goto B3
B4 if t2 >= t4 goto B6
a[t2]=t5 t14=a[t1]
B5 a[t8]=t3 a[t2]=t14 B6
goto B2 a[t13]=t3
11 / 45
Motivating Example
i = m-1
j=n
t1 = 4*n
B1 v = a[t1]
t2 = 4 * i
t4 = 4 * j
i = i+1
t2 = t2 + 4
B2 t3 = a[t2]
if t3<v goto B2
j=j-1
t4 = t4 - 4
B3 t5 = a[t4]
if t5>v goto B3
B4 if t2 >= t4 goto B6
a[t2]=t5 t14=a[t1]
B5 a[t8]=t3 a[t2]=t14 B6
goto B2 a[t13]=t3
11 / 45
Motivating Example - Optimization Benefits
12 / 45
Part II
Peephole Optimizations
Machine Dependent Optimizations
14 / 45
Machine Dependent Optimizations
14 / 45
Peephole Optimizations
15 / 45
Peephole Optimizations
15 / 45
Peephole Optimizations
15 / 45
Peephole Optimizations
15 / 45
Peephole Optimizations
15 / 45
Peephole Optimizations
15 / 45
Peephole Optimizations
15 / 45
Peephole Optimizations
15 / 45
Peephole Optimizations
15 / 45
Use of Algebraic Identities
16 / 45
Use of Algebraic Identities
16 / 45
Use of Algebraic Identities
16 / 45
Use of Algebraic Identities
16 / 45
Use of Algebraic Identities
16 / 45
Use of Algebraic Identities
16 / 45
Use of Algebraic Identities
16 / 45
Use of Algebraic Identities
16 / 45
Part III
Intraprocedural
18 / 45
Machine Independent Code Optimizations
Intraprocedural
◮ Local- Restricted to a basic block
18 / 45
Machine Independent Code Optimizations
Intraprocedural
◮ Local- Restricted to a basic block
◮ Global- Restricted within a procedure/function
need to consider how information flows among basic blocks
18 / 45
Machine Independent Code Optimizations
Intraprocedural
◮ Local- Restricted to a basic block
◮ Global- Restricted within a procedure/function
need to consider how information flows among basic blocks
Interprocedural- Multiple procedures
18 / 45
Local Common Subexpressions
a = b * c
b = a + d
c = b * c
19 / 45
Local Common Subexpressions
a = b * c
b = a + d
c = b * c
∗ a
b0 c0
19 / 45
Local Common Subexpressions
a = b * c
b = a + d
c = b * c
+ b
∗ a d0
b0 c0
19 / 45
Local Common Subexpressions
a = b * c ∗ c
b = a + d
c = b * c
+ b
∗ a d0
b0 c0
19 / 45
Local Common Subexpressions
a = b * c ∗ c
b = a + d
c = b * c
+ b
∗ a d0
b0 c0
19 / 45
Local Common Subexpressions
a = b * c ∗ c
b = a + d
c = b * c
+ b
∗ a d0
b0 c0
19 / 45
Local Common Subexpressions
a = b * c ∗ c
d = a + d
c = b * c
+ b
Lets update the example
∗ a d0
b0 c0
20 / 45
Local Common Subexpressions
a = b * c ∗ c
d = a + d
c = b * c
+ d
Lets update the example
∗ a d0
b0 c0
20 / 45
Local Common Subexpressions
a = b * c
d = a + d
c = b * c
+ d
Lets update the example
∗ a, c d0
b0 c0
20 / 45
Dead Code Elimination
a = b * c ∗ c
b = a + d
c = b * c
+ b
a is live after this block
∗ a d0
b0 c0
21 / 45
Dead Code Elimination
a = b * c ∗ c
b = a + d
c = b * c
+ b
a is live after this block
∗ a d0
b0 c0
21 / 45
Dead Code Elimination
a = b * c
b = a + d
c = b * c
+ b
a is live after this block
∗ a d0
b0 c0
21 / 45
Dead Code Elimination
a = b * c
b = a + d
c = b * c
+ b
a is live after this block
∗ a d0
b0 c0
21 / 45
Dead Code Elimination
a = b * c
b = a + d
c = b * c
b0 c0
21 / 45
Dead Code Elimination
a = b * c
b = a + d
c = b * c
b0 c0
Transformed code:
a =b∗c
21 / 45
Global Code Optimizations
Constant Folding
Constant Propagation
Copy Propagation
Code Movement
Dead code elimination
Common subexpression elimination
22 / 45
Compile-time Evaluation
Constant Folding
23 / 45
Compile-time Evaluation
Constant Folding
x = 2∗π∗r
23 / 45
Compile-time Evaluation
Constant Folding
x = 2∗π∗r
Constant Propagation
23 / 45
Compile-time Evaluation
Constant Folding
x = 2∗π∗r
Constant Propagation
x = 25
..
.
y =x∗3
23 / 45
Compile-time Evaluation
Constant Folding
x = 2∗π∗r
Constant Propagation
x = 25 x = 25
.. ..
. .
y =x∗3 y = 25 ∗ 3
23 / 45
Compile-time Evaluation
Constant Folding
x = 2∗π∗r
Constant Propagation
x = 25 x = 25 x = 25
.. .. ..
. . .
y =x∗3 y = 25 ∗ 3 y = 75
23 / 45
Copy Propagation
24 / 45
Copy Propagation
24 / 45
Copy Propagation
24 / 45
Copy Propagation
24 / 45
Copy Propagation
24 / 45
Copy Propagation
24 / 45
Code Movement
25 / 45
Code Movement
25 / 45
Code Movement
25 / 45
Code Movement
25 / 45
Code Movement
25 / 45
Code Movement
if (x < y )
u = ...
else
v =x∗y
w =x∗y
25 / 45
Code Movement
25 / 45
Loop Invariant Code Movement
26 / 45
Loop Invariant Code Movement
26 / 45
Loop Invariant Code Movement
while (x < y ) {
...
u = x ∗y
...}
26 / 45
Loop Invariant Code Movement
26 / 45
Dead Code Elimination
1 1
2 2
3 a = ... 4 a = ...
5 5
6 use a
27 / 45
Dead Code Elimination
1 1
2 2
5 5
6 use a
27 / 45
Dead Code Elimination
1 1
2 2
6 use a
27 / 45
Dead Code Elimination
1 1
2 2
6 use a
27 / 45
Dead Code Elimination
1 1
2 2
3 a = ... 4 a = ...
5 5
6 use a
27 / 45
Dead Code Elimination
1 1
2 2
5 5
6 use a
27 / 45
Dead Code Elimination
1 1
2 2
6 use a
27 / 45
Dead Code Elimination
3 a = ... 4 a = ...
5 5
6 use a
27 / 45
Dead Code Elimination
5 5
6 use a
27 / 45
Dead Code Elimination
6 use a
27 / 45
Common Subexpression Elimination
1 1
2 2
3 a∗b 4 a∗b
5 5
6 a∗b
28 / 45
Common Subexpression Elimination
1 1
2 2
3 a∗b 4 a∗b
5 5
28 / 45
Common Subexpression Elimination
1 1
2 2
3 a∗b 4 a∗b
5 5
28 / 45
Common Subexpression Elimination
1 1
2 2
3 a∗b 4 a∗b
5 5
28 / 45
Common Subexpression Elimination
1 1
2 2
3 a∗b 4 a∗b
5 5
28 / 45
Common Subexpression Elimination
1 1
2 2
3 a∗b 4 a∗b
5 5
28 / 45
Common Subexpression Elimination
1 1
2 2
3 a∗b 4 a∗b
5 5
28 / 45
Common Subexpression Elimination
1 1
2 2
3 a∗b 4 a∗b
5 5
28 / 45
Common Subexpression Elimination
3 a∗b 4 a∗b
5 5
28 / 45
Common Subexpression Elimination
3 a∗b 4 a∗b
5 5
28 / 45
Common Subexpression Elimination
3 a∗b 4 a∗b
5 5
28 / 45
Common Subexpression Elimination
3 a∗b 4 a = ...
5 5
28 / 45
Common Subexpression Elimination
3 a∗b 4 a = ...
5 5
28 / 45
Common Subexpression Elimination
3 a∗b 4 a = ...
5 5
28 / 45
Common Subexpression Elimination
3 a∗b 4 a = ...
5 5
28 / 45
Common Subexpression Elimination
5 5
28 / 45
Common Subexpression Elimination
6 a∗b
28 / 45
29 / 45
Basic Blocks and Flow Graphs
30 / 45
Basic Blocks and Flow Graphs
30 / 45
Basic Blocks and Flow Graphs
30 / 45
Basic Blocks and Flow Graphs
30 / 45
Basic Blocks and Flow Graphs
30 / 45
Basic Blocks and Flow Graphs
30 / 45
Partitioning three-address Instructions into Basic Blocks
Algorithm
31 / 45
Partitioning three-address Instructions into Basic Blocks
Algorithm
Identify the leaders, that is, the first instruction in some basic block
31 / 45
Partitioning three-address Instructions into Basic Blocks
Algorithm
Identify the leaders, that is, the first instruction in some basic block
For each leader, basic block consists of the leader instruction and all
instructions up to
31 / 45
Partitioning three-address Instructions into Basic Blocks
Algorithm
Identify the leaders, that is, the first instruction in some basic block
For each leader, basic block consists of the leader instruction and all
instructions up to
◮ but not including the next leader or
31 / 45
Partitioning three-address Instructions into Basic Blocks
Algorithm
Identify the leaders, that is, the first instruction in some basic block
For each leader, basic block consists of the leader instruction and all
instructions up to
◮ but not including the next leader or
◮ the end of the intermediate program
31 / 45
Partitioning three-address Instructions into Basic Blocks
Algorithm
Identify the leaders, that is, the first instruction in some basic block
For each leader, basic block consists of the leader instruction and all
instructions up to
◮ but not including the next leader or
◮ the end of the intermediate program
Leader instructions are
31 / 45
Partitioning three-address Instructions into Basic Blocks
Algorithm
Identify the leaders, that is, the first instruction in some basic block
For each leader, basic block consists of the leader instruction and all
instructions up to
◮ but not including the next leader or
◮ the end of the intermediate program
Leader instructions are
First instruction of the intermediate code
31 / 45
Partitioning three-address Instructions into Basic Blocks
Algorithm
Identify the leaders, that is, the first instruction in some basic block
For each leader, basic block consists of the leader instruction and all
instructions up to
◮ but not including the next leader or
◮ the end of the intermediate program
Leader instructions are
First instruction of the intermediate code
Target of conditional or unconditional jump
31 / 45
Partitioning three-address Instructions into Basic Blocks
Algorithm
Identify the leaders, that is, the first instruction in some basic block
For each leader, basic block consists of the leader instruction and all
instructions up to
◮ but not including the next leader or
◮ the end of the intermediate program
Leader instructions are
First instruction of the intermediate code
Target of conditional or unconditional jump
Instruction immediately following conditional or unconditional jump
31 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++)
{
for (y =0;y <10;y ++)
{
z=x+y;
}
}
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
6: y = y + 1
7: goto 4
8: x = x + 1
9: goto 2
10: . . .
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
6: y = y + 1
First instruction 7: goto 4
8: x = x + 1
9: goto 2
10: . . .
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
6: y = y + 1
First instruction 7: goto 4
8: x = x + 1
9: goto 2
10: . . .
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
6: y = y + 1
First instruction 7: goto 4
Target of conditional or 8: x = x + 1
unconditional jumps
9: goto 2
10: . . .
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
6: y = y + 1
First instruction 7: goto 4
Target of conditional or 8: x = x + 1
unconditional jumps
9: goto 2
10: . . .
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
6: y = y + 1
First instruction 7: goto 4
Target of conditional or 8: x = x + 1
unconditional jumps
9: goto 2
10: . . .
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
6: y = y + 1
First instruction 7: goto 4
Target of conditional or 8: x = x + 1
unconditional jumps
9: goto 2
10: . . .
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
6: y = y + 1
First instruction 7: goto 4
Target of conditional or 8: x = x + 1
unconditional jumps
9: goto 2
10: . . .
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
6: y = y + 1
First instruction 7: goto 4
Target of conditional or 8: x = x + 1
unconditional jumps
9: goto 2
Instruction immediately
following conditional or 10: . . .
unconditional jump
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
6: y = y + 1
First instruction 7: goto 4
Target of conditional or 8: x = x + 1
unconditional jumps
9: goto 2
Instruction immediately
following conditional or 10: . . .
unconditional jump
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
6: y = y + 1
First instruction 7: goto 4
Target of conditional or 8: x = x + 1
unconditional jumps
9: goto 2
Instruction immediately
following conditional or 10: . . .
unconditional jump
32 / 45
Example for Basic Block Construction
for (x =0; x <10; x ++) 1: x = 0
{
for (y =0;y <10;y ++) 2: if (x > 9) goto 10
{
z=x+y; 3: y = 0
}
} 4: if (y > 9) goto 8
5: z = x + y
1
6: y = y + 1
2 7: goto 4
8: x = x + 1
10 3 9: goto 2
10: . . .
4
8,9 5,6,7
32 / 45
Examples of Machine-Independant Optimizations
33 / 45
Control Flow Analysis
34 / 45
Identifying Back Edges and Nodes in Loops
1 1
2 2
3 a∗b 4 a∗b
5 5
6 a∗b
35 / 45
Identifying Back Edges and Nodes in Loops
1 1
2 2
3 a∗b 4 a∗b
How to identify a back edge?
5 5
6 a∗b
35 / 45
Identifying Back Edges and Nodes in Loops
1 1
2 2
3 a∗b 4 a∗b
How to identify nodes in a loop?
5 5
6 a∗b
35 / 45
Dominators
36 / 45
Dominators
1 1
2 2
3 a∗b 4 a∗b
Does node 1 dom node 5? => Yes!!
5 5
6 a∗b
37 / 45
Dominators
1 1
2 2
3 a∗b 4 a∗b
Does node 1 dom node 5? => Yes!!
5 5
6 a∗b
37 / 45
Dominators
1 1
2 2
3 a∗b 4 a∗b
Does node 1 dom node 5? => Yes!!
5 5
Does node 3 dom node 6? => No!!
6 a∗b
37 / 45
Dominators
1 1
2 2
3 a∗b 4 a∗b
Does node 1 dom node 5? => Yes!!
5 5
Does node 3 dom node 6? => No!!
6 a∗b
37 / 45
Dominators
1 1
2 2
3 a∗b 4 a∗b
Does node 1 dom node 5? => Yes!!
5 5
Does node 3 dom node 6? => No!!
6 a∗b
if x, n then x dom n iff
x dominates all the predecessors of n
37 / 45
Dominators
1 1
2 2
3 a∗b 4 a∗b
5 5
6 a∗b
38 / 45
Dominators
{1} 1 1
2 2
3 a∗b 4 a∗b
5 5
6 a∗b
38 / 45
Dominators
{1} 1 1
{1,2} 2 2
3 a∗b 4 a∗b
5 5
6 a∗b
38 / 45
Dominators
{1} 1 1
{1,2} 2 2
{1,2,3} 3 a ∗ b 4 a∗b
5 5
6 a∗b
38 / 45
Dominators
{1} 1 1
{1,2} 2 2
{1,2,4}
{1,2,3} 3 a ∗ b 4 a∗b
5 5
6 a∗b
38 / 45
Dominators
{1} 1 1
{1,2} 2 2
{1,2,4}
{1,2,3} 3 a ∗ b 4 a∗b
{1,2,5} 5 5
6 a∗b
38 / 45
Dominators
{1} 1 1
{1,2} 2 2
{1,2,4}
{1,2,3} 3 a ∗ b 4 a∗b
{1,2,5} 5 5
{1,2,5,6} 6 a ∗ b
38 / 45
Dominators
{1,2} 2 2
{1,2,4}
{1,2,3} 3 a ∗ b 4 a∗b
{1,2,5} 5 5
{1,2,5,6} 6 a ∗ b
38 / 45
Dominators
{1,2} 2 2
{1,2,4}
{1,2,3} 3 a ∗ b 4 a∗b
{1,2,5,6} 6 a ∗ b
38 / 45
Dominators
{1,2} 2 2
{1,2,4}
{1,2,3} 3 a ∗ b 4 a∗b
38 / 45
Dominators
38 / 45
Dominators
38 / 45
Dominators
38 / 45
Dominators
38 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominators
39 / 45
Dominator Tree
1
2
1 1
{1}
3 4 5
2 2
{1,2}
6
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
5 5
{1,2,5}
6 a∗b
{1,2,5,6}
40 / 45
Dominator Tree
1
2
1 1
{1}
3 4 5
2 2
{1,2}
6
3 a∗b 4 a∗b
{1,2,3}
{1,2,4} Entry node is the root
5 5
{1,2,5}
6 a∗b
{1,2,5,6}
40 / 45
Dominator Tree
1
2
1 1
{1}
3 4 5
2 2
{1,2}
6
3 a∗b 4 a∗b
{1,2,3}
{1,2,4} Entry node is the root
5 5
{1,2,5} Each node dominates only its
descendants in the tree
6 a∗b
{1,2,5,6}
40 / 45
Dominator Tree
1
2
1 1
{1}
3 4 5
2 2
{1,2}
6
3 a∗b 4 a∗b
{1,2,3}
{1,2,4} Entry node is the root
5 5
{1,2,5} Each node dominates only its
descendants in the tree
6 a∗b
{1,2,5,6} n has a unique immediate
dominator to m
40 / 45
Dominator Tree
1
2
1 1
{1}
3 4 5
2 2
{1,2}
6
3 a∗b 4 a∗b
{1,2,3}
{1,2,4} Entry node is the root
5 5
{1,2,5} Each node dominates only its
descendants in the tree
6 a∗b
{1,2,5,6} n has a unique immediate
dominator to m
m idom n: if d, n and d dom n
then d dom m
40 / 45
Dominator Tree
1
2
1 1
{}
3 4 5
2 2
{1}
6
3 a∗b 4 a∗b
{1,2}
{1,2}
5 5
{1,2}
6 a∗b
{1,2,5}
41 / 45
Dominator Tree
1
2
1 1
{}
3 4 5
2 2
{1}
6
3 a∗b 4 a∗b
{1,2}
{1,2}
5 5
{1,2}
Strict Dominator
6 a∗b
{1,2,5}
41 / 45
Dominator Tree
1
2
1 1
{}
3 4 5
2 2
{1}
6
3 a∗b 4 a∗b
{1,2}
{1,2}
5 5
{1,2}
Strict Dominator
6 a∗b
{1,2,5}
m sdom n: if m dom n and m, n
41 / 45
Dominator
1
2
1 1
{1}
3 4 5
2 2
{1,2}
6
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
5 5
{1,2,5}
6 a∗b
{1,2,5,6}
42 / 45
Dominator
1
2
1 1
{1}
3 4 5
2 2
{1,2}
6
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
Transitive: if a dom b and b dom c
5 5 then a dom c
{1,2,5}
6 a∗b
{1,2,5,6}
42 / 45
Dominator
1
2
1 1
{1}
3 4 5
2 2
{1,2}
6
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
Transitive: if a dom b and b dom c
5 5 then a dom c
{1,2,5}
Antisymmetric: for a,b, a dom b
6 a∗b and b dom a does not hold
{1,2,5,6}
42 / 45
Dominator
1
2
1 1
{1}
3 4 5
2 2
{1,2}
6
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
Transitive: if a dom b and b dom c
5 5 then a dom c
{1,2,5}
Antisymmetric: for a,b, a dom b
6 a∗b and b dom a does not hold
{1,2,5,6}
if a and b are two dominators of n,
then either a dom b or b dom a
must hold
42 / 45
What is a Back Edge?
1 1
{1}
2 2
{1,2}
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
5 5
{1,2,5}
6 a∗b
{1,2,5,6}
43 / 45
What is a Back Edge?
1 1
{1}
2 2
{1,2} An edge (x,y) is a Backedge
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
5 5
{1,2,5}
6 a∗b
{1,2,5,6}
43 / 45
What is a Back Edge?
1 1
{1}
2 2
{1,2} An edge (x,y) is a Backedge iff y dom x
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
5 5
{1,2,5}
6 a∗b
{1,2,5,6}
43 / 45
What is a Back Edge?
1 1
{1}
2 2
{1,2} An edge (x,y) is a Backedge iff y dom x
Is (5,2) a backedge?
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
5 5
{1,2,5}
6 a∗b
{1,2,5,6}
43 / 45
What is a Back Edge?
1 1
{1}
2 2
{1,2} An edge (x,y) is a Backedge iff y dom x
Is (5,2) a backedge?
3 a∗b 4 a∗b
{1,2,3}
{1,2,4} does 2 dom 5?
5 5
{1,2,5}
6 a∗b
{1,2,5,6}
43 / 45
What is a Back Edge?
1 1
{1}
2 2
{1,2} An edge (x,y) is a Backedge iff y dom x
Is (5,2) a backedge?
3 a∗b 4 a∗b
{1,2,3}
{1,2,4} does 2 dom 5? YES!!
5 5
{1,2,5}
6 a∗b
{1,2,5,6}
43 / 45
What is a Back Edge?
1 1
{1}
2 2
{1,2} An edge (x,y) is a Backedge iff y dom x
Is (5,2) a backedge?
3 a∗b 4 a∗b
{1,2,3}
{1,2,4} does 2 dom 5? YES!!
5 5 Is (5,6) a backedge?
{1,2,5}
6 a∗b
{1,2,5,6}
43 / 45
What is a Back Edge?
1 1
{1}
2 2
{1,2} An edge (x,y) is a Backedge iff y dom x
Is (5,2) a backedge?
3 a∗b 4 a∗b
{1,2,3}
{1,2,4} does 2 dom 5? YES!!
5 5 Is (5,6) a backedge?
{1,2,5}
does 6 dom 5?
6 a∗b
{1,2,5,6}
43 / 45
What is a Back Edge?
1 1
{1}
2 2
{1,2} An edge (x,y) is a Backedge iff y dom x
Is (5,2) a backedge?
3 a∗b 4 a∗b
{1,2,3}
{1,2,4} does 2 dom 5? YES!!
5 5 Is (5,6) a backedge?
{1,2,5}
does 6 dom 5? N0!!
6 a∗b
{1,2,5,6}
43 / 45
Natural Loops
1 1
2 2
3 a∗b 4 a∗b
5 5
6 a∗b
44 / 45
Natural Loops
2 2
3 a∗b 4 a∗b
5 5
6 a∗b
44 / 45
Natural Loops
3 a∗b 4 a∗b
5 5
6 a∗b
44 / 45
Natural Loops
3 a∗b 4 a∗b
5 5
6 a∗b
44 / 45
Natural Loops
1 1
2 2
3 a∗b 4 a∗b
5 5
6 a∗b
45 / 45
Natural Loops
1 1
2 2
3 a∗b 4 a∗b
5 5
6 a∗b
45 / 45
Natural Loops
2 2
3 a∗b 4 a∗b
5 5
6 a∗b
45 / 45
Natural Loops
2 2
3 a∗b 4 a∗b
5 5
6 a∗b
45 / 45
Natural Loops
2 2
3 a∗b 4 a∗b
5 5
45 / 45
Natural Loops
2 2
3 a∗b 4 a∗b
5 5
45 / 45
Natural Loops
2 2
3 a∗b 4 a∗b
5 5
45 / 45
Natural Loops
2 2
3 a∗b 4 a∗b
5 5
45 / 45
Natural Loops
3 a∗b 4 a∗b
5 5
45 / 45
Natural Loops
3 a∗b 4 a∗b
5 5
45 / 45
Natural Loops
5 5
45 / 45
Natural Loops
Back Edge 5 → 2
stack = empty Consider a back edge n → d
loop = {2}
stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2 pop(m, stack)
for each predecessor p of m invoke
3 a∗b 4 a∗b insert (p) }
5 5
45 / 45
Natural Loops
Back Edge 5 → 2
stack = empty Consider a back edge n → d
loop = {2,5}
stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2 pop(m, stack)
for each predecessor p of m invoke
3 a∗b 4 a∗b insert (p) }
5 5
45 / 45
Natural Loops
Back Edge 5 → 2
stack = {5} Consider a back edge n → d
loop = {2,5}
stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2 pop(m, stack)
for each predecessor p of m invoke
3 a∗b 4 a∗b insert (p) }
5 5
45 / 45
Natural Loops
Back Edge 5 → 2
stack = empty Consider a back edge n → d
loop = {2,5}
stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2 pop(m, stack)
for each predecessor p of m invoke
3 a∗b 4 a∗b insert (p) }
5 5
45 / 45
Natural Loops
Back Edge 5 → 2
stack = empty Consider a back edge n → d
loop = {2,5,3}
stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2 pop(m, stack)
for each predecessor p of m invoke
3 a∗b 4 a∗b insert (p) }
5 5
45 / 45
Natural Loops
Back Edge 5 → 2
stack = {3} Consider a back edge n → d
loop = {2,5,3}
stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2 pop(m, stack)
for each predecessor p of m invoke
3 a∗b 4 a∗b insert (p) }
5 5
45 / 45
Natural Loops
Back Edge 5 → 2
stack = {3} Consider a back edge n → d
loop = {2,5,3,4}
stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2 pop(m, stack)
for each predecessor p of m invoke
3 a∗b 4 a∗b insert (p) }
5 5
45 / 45
Natural Loops
Back Edge 5 → 2
stack = {3,4} Consider a back edge n → d
loop = {2,5,3,4}
stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2 pop(m, stack)
for each predecessor p of m invoke
3 a∗b 4 a∗b insert (p) }
5 5
45 / 45
Natural Loops
Back Edge 5 → 2
stack = {3} Consider a back edge n → d
loop = {2,5,3,4}
stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2 pop(m, stack)
for each predecessor p of m invoke
3 a∗b 4 a∗b insert (p) }
5 5
45 / 45
Natural Loops
Back Edge 5 → 2
stack = empty Consider a back edge n → d
loop = {2,5,3,4}
stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2 pop(m, stack)
for each predecessor p of m invoke
3 a∗b 4 a∗b insert (p) }
5 5
45 / 45