0% found this document useful (0 votes)
3 views259 pages

Code Optimisation 1_ Basics

The document outlines the different phases of a compiler, including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It emphasizes the importance of code optimization to eliminate unnecessary instructions and improve runtime efficiency, highlighting techniques such as common subexpression elimination and dead code elimination. Additionally, it provides examples of how code can be transformed for optimization in practical scenarios.

Uploaded by

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

Code Optimisation 1_ Basics

The document outlines the different phases of a compiler, including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It emphasizes the importance of code optimization to eliminate unnecessary instructions and improve runtime efficiency, highlighting techniques such as common subexpression elimination and dead code elimination. Additionally, it provides examples of how code can be transformed for optimization in practical scenarios.

Uploaded by

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

Language Processors

Swati Jaiswal

Department of Computer Science and Engineering,


Visvesvaraya National Institute of Technology, Nagpur

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

Target machine code

Machine Dependent
Code Optimizer

Target machine code

3 / 45
Code Optimization

Runtime overhead is introduced when we naively translate


High level construct => Machine Code

4 / 45
Code Optimization

Runtime overhead is introduced when we naively translate


High level construct => Machine Code
Elimination of unnecessary instruction

4 / 45
Code Optimization

Runtime overhead is introduced when we naively translate


High level construct => Machine Code
Elimination of unnecessary instruction
Replacing a sequence of instructions by a faster sequence of instructions

4 / 45
Code Optimization

Can we write optimized code?

5 / 45
Code Optimization

Can we write optimized code?


Programs should be easy to maintain
Need high level constructs

5 / 45
Code Optimization

Can we write optimized code?


Programs should be easy to maintain
Need high level constructs
High level construct is translated to low level language
Source of redundancy

5 / 45
Code Optimization

Can we write optimized code?


Programs should be easy to maintain
Need high level constructs
High level construct is translated to low level language
Source of redundancy
Program should be efficient
Smart Compiler

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

Original code Transformed code

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

void quicksort (int m, int n)


{
int i, j;
int v, x;
if (n <= m) return ;

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] */

quicksort (m, j); quicksort (i+1, 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

Basic #Statements Before #Statements After


block Optimization Optimization
B1 4 6
B2 4 3
B3 4 3
B4 1 1
B5 9 3
B6 8 3

12 / 45
Part II

Peephole Optimizations
Machine Dependent Optimizations

Examine sliding window of target instructions (peephole)

14 / 45
Machine Dependent Optimizations

Examine sliding window of target instructions (peephole)


Replace by shorter or faster sequence of instructions

14 / 45
Peephole Optimizations

Eliminating redundant instructions


◮ Redundant loads and stores

15 / 45
Peephole Optimizations

Eliminating redundant instructions


◮ Redundant loads and stores
LD R0, a
ST a, R0

15 / 45
Peephole Optimizations

Eliminating redundant instructions


◮ Redundant loads and stores
LD R0, a
ST a, R0
◮ Unreachable code

15 / 45
Peephole Optimizations

Eliminating redundant instructions


◮ Redundant loads and stores
LD R0, a
ST a, R0
◮ Unreachable code
if (debug == 1) goto L1
goto L2
L1: print ...
L2:

15 / 45
Peephole Optimizations

Eliminating redundant instructions


◮ Redundant loads and stores
LD R0, a
ST a, R0
◮ Unreachable code
if (debug == 1) goto L1 if (debug != 1) goto L2
goto L2 ab
L1: print ... print ...
L2: L2:

15 / 45
Peephole Optimizations

Eliminating redundant instructions


◮ Redundant loads and stores
LD R0, a
ST a, R0
◮ Unreachable code
if (debug == 1) goto L1 if (debug != 1) goto L2
goto L2 ab
L1: print ... print ...
L2: L2:
Flow-of-Control Optimizations

15 / 45
Peephole Optimizations

Eliminating redundant instructions


◮ Redundant loads and stores
LD R0, a
ST a, R0
◮ Unreachable code
if (debug == 1) goto L1 if (debug != 1) goto L2
goto L2 ab
L1: print ... print ...
L2: L2:
Flow-of-Control Optimizations
goto L1
...
L1: goto L2

15 / 45
Peephole Optimizations

Eliminating redundant instructions


◮ Redundant loads and stores
LD R0, a
ST a, R0
◮ Unreachable code
if (debug == 1) goto L1 if (debug != 1) goto L2
goto L2 ab
L1: print ... print ...
L2: L2:
Flow-of-Control Optimizations
goto L1 goto L2
... ...
L1: goto L2 L1: goto L2

15 / 45
Peephole Optimizations

Eliminating redundant instructions


◮ Redundant loads and stores
LD R0, a
ST a, R0
◮ Unreachable code
if (debug == 1) goto L1 if (debug != 1) goto L2
goto L2 ab
L1: print ... print ...
L2: L2:
Flow-of-Control Optimizations
goto L1 goto L2
... ...
L1: goto L2 L1: goto L2
Algebraic simplifications

15 / 45
Use of Algebraic Identities

Apply algebraic identities

16 / 45
Use of Algebraic Identities

Apply algebraic identities


◮ x +0=0+x =x

16 / 45
Use of Algebraic Identities

Apply algebraic identities


◮ x +0=0+x =x
◮ x ×1=1×x =x

16 / 45
Use of Algebraic Identities

Apply algebraic identities


◮ x +0=0+x =x
◮ x ×1=1×x =x
Replace an expensive operation by a cheaper one

16 / 45
Use of Algebraic Identities

Apply algebraic identities


◮ x +0=0+x =x
◮ x ×1=1×x =x
Replace an expensive operation by a cheaper one
◮ x2 = x × x

16 / 45
Use of Algebraic Identities

Apply algebraic identities


◮ x +0=0+x =x
◮ x ×1=1×x =x
Replace an expensive operation by a cheaper one
◮ x2 = x × x
◮ 2×x =x +x

16 / 45
Use of Algebraic Identities

Apply algebraic identities


◮ x +0=0+x =x
◮ x ×1=1×x =x
Replace an expensive operation by a cheaper one
◮ x2 = x × x
◮ 2×x =x +x
◮ Multiplication by left shift and division by right shift

16 / 45
Use of Algebraic Identities

Apply algebraic identities


◮ x +0=0+x =x
◮ x ×1=1×x =x
Replace an expensive operation by a cheaper one
◮ x2 = x × x
◮ 2×x =x +x
◮ Multiplication by left shift and division by right shift
◮ ADD #1, R => INC R

16 / 45
Part III

Machine Independent Code Optimizations


Machine Independent Code Optimizations

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

Possibility of common subexpression elimination?

19 / 45
Local Common Subexpressions

a = b * c ∗ c
b = a + d
c = b * c
+ b

∗ a d0

b0 c0

Possibility of common subexpression elimination? NO!!

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

Since c is not live


c = b ∗ c is dead code

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

Since c is not live


c = b ∗ c is dead code

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

Now b = a + d is also dead code

21 / 45
Dead Code Elimination

a = b * c
b = a + d
c = b * c

a is live after this block


∗ a

b0 c0

Now b = a + d is also dead code

21 / 45
Dead Code Elimination

a = b * c
b = a + d
c = b * c

a is live after this block


∗ a

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

Replace use of a variable by another variable

24 / 45
Copy Propagation

Replace use of a variable by another variable


x=y
..
.
z =x∗w

24 / 45
Copy Propagation

Replace use of a variable by another variable


x=y x=y
.. ..
. .
z =x∗w z =y∗w

24 / 45
Copy Propagation

Replace use of a variable by another variable


x=y x=y
.. .. ..
. . .
z =x∗w z =y∗w z =y∗w

24 / 45
Copy Propagation

Replace use of a variable by another variable


x=y x=y
.. .. ..
. . .
z =x∗w z =y∗w z =y∗w
Could result in dead code or common subexpression

24 / 45
Code Movement

25 / 45
Code Movement

Reduction in code size

25 / 45
Code Movement

Reduction in code size


if (x < y )
u = x ∗y
else
v =x∗y

25 / 45
Code Movement

Reduction in code size


t = x ∗y
if (x < y )
if (x < y )
u = x ∗y
u=t
else
else
v =x∗y
v =t

25 / 45
Code Movement

Reduction in code size


t = x ∗y
if (x < y )
if (x < y )
u = x ∗y
u=t
else
else
v =x∗y
v =t
Reduction in execution frequency

25 / 45
Code Movement

Reduction in code size


t = x ∗y
if (x < y )
if (x < y )
u = x ∗y
u=t
else
else
v =x∗y
v =t
Reduction in execution frequency

if (x < y )
u = ...
else
v =x∗y
w =x∗y

25 / 45
Code Movement

Reduction in code size


t = x ∗y
if (x < y )
if (x < y )
u = x ∗y
u=t
else
else
v =x∗y
v =t
Reduction in execution frequency
if (x < y )
if (x < y ) u=t
u = ... t = x ∗y
else else
v =x∗y t = x ∗y
w =x∗y v =t
w=t

25 / 45
Loop Invariant Code Movement

26 / 45
Loop Invariant Code Movement

Move loop invariant code out of the loop

26 / 45
Loop Invariant Code Movement

Move loop invariant code out of the loop

while (x < y ) {
...
u = x ∗y
...}

26 / 45
Loop Invariant Code Movement

Move loop invariant code out of the loop


t =x∗y
while (x < y ) { while (x < y ) {
... ...
u = x ∗y u=t
...} ...
}

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

3 a = ... 4 a = ... Is this dead code?

5 5

6 use a

27 / 45
Dead Code Elimination

1 1

2 2

3 a = ... 4 a = ... Is this dead code?


No!!
5 5

6 use a

27 / 45
Dead Code Elimination

1 1

2 2

3 a = ... 4 a = ... Is this dead code?


No!!
5 5

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

3 a = ... 4 a = ... Is this dead code?

5 5

6 use a

27 / 45
Dead Code Elimination

1 1

2 2

3 a = ... 4 a = ... Is this dead code?


Yes!!
5 5

6 use a

27 / 45
Dead Code Elimination

An assignment x = . . . is dead code


1 1
if x is not live after the
2 2 assignment

3 a = ... 4 a = ...

5 5

6 use a

27 / 45
Dead Code Elimination

An assignment x = . . . is dead code


1 1
if x is not live after the
2 2 assignment
Such an assignment can be
3 a = ... 4 a = ... deleted

5 5

6 use a

27 / 45
Dead Code Elimination

An assignment x = . . . is dead code


1 1
if x is not live after the
2 2 assignment
Such an assignment can be
3 a = ... 4 a = ... deleted
Need to define liveness of
5 5 variables!!

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

6 a∗b Is the expression redundant?

28 / 45
Common Subexpression Elimination

1 1

2 2

3 a∗b 4 a∗b

5 5

6 a∗b Is the expression redundant?


Yes!!

28 / 45
Common Subexpression Elimination

1 1

2 2

3 a∗b 4 a∗b

5 5

6 a∗b Is the expression redundant?


Yes!!

28 / 45
Common Subexpression Elimination

1 1

2 2

3 a∗b 4 a∗b

5 5

6 a∗b Is the expression redundant?

28 / 45
Common Subexpression Elimination

1 1

2 2

3 a∗b 4 a∗b

5 5

6 a∗b Is the expression redundant?


No!!

28 / 45
Common Subexpression Elimination

1 1

2 2

3 a∗b 4 a∗b

5 5

6 a∗b Is the expression redundant?


No!!

28 / 45
Common Subexpression Elimination

1 1

2 2

3 a∗b 4 a∗b

5 5

6 a∗b Is the expression redundant?


No!!

28 / 45
Common Subexpression Elimination

1 1 the expression should be


evaluated along every path
2 2

3 a∗b 4 a∗b

5 5

6 a∗b Is the expression redundant?


No!!

28 / 45
Common Subexpression Elimination

1 1 the expression should be


evaluated along every path
2 a∗b

3 a∗b 4 a∗b

5 5

6 a∗b Is the expression redundant?

28 / 45
Common Subexpression Elimination

1 1 the expression should be


evaluated along every path
2 a∗b

3 a∗b 4 a∗b

5 5

6 a∗b Is the expression redundant?


Yes!!

28 / 45
Common Subexpression Elimination

1 1 the expression should be


evaluated along every path
2 a∗b

3 a∗b 4 a = ...

5 5

6 a∗b Is the expression redundant?

28 / 45
Common Subexpression Elimination

1 1 the expression should be


evaluated along every path
2 a∗b

3 a∗b 4 a = ...

5 5

6 a∗b Is the expression redundant?


No!!

28 / 45
Common Subexpression Elimination

1 1 the expression should be


evaluated along every path
2 a∗b

3 a∗b 4 a = ...

5 5

6 a∗b Is the expression redundant?


No!!

28 / 45
Common Subexpression Elimination

1 1 the expression should be


evaluated along every path
2 a∗b

3 a∗b 4 a = ...

5 5

6 a∗b Is the expression redundant?


No!!

28 / 45
Common Subexpression Elimination

1 1 the expression should be


evaluated along every path
2 a∗b
not followed by a definition of
any operand of the expression
3 a∗b 4 a = ...

5 5

6 a∗b Is the expression redundant?


No!!

28 / 45
Common Subexpression Elimination

1 1 the expression should be


evaluated along every path
2 a∗b
not followed by a definition of
any operand of the expression
3 a∗b 4 a = ...
need to define availability of
expressions!!
5 5

6 a∗b

28 / 45
29 / 45
Basic Blocks and Flow Graphs

Flow graphs are graph representation of intermediate code

30 / 45
Basic Blocks and Flow Graphs

Flow graphs are graph representation of intermediate code


◮ Nodes are basic blocks

30 / 45
Basic Blocks and Flow Graphs

Flow graphs are graph representation of intermediate code


◮ Nodes are basic blocks
◮ Edges represent which basic block follows which basic blocks

30 / 45
Basic Blocks and Flow Graphs

Flow graphs are graph representation of intermediate code


◮ Nodes are basic blocks
◮ Edges represent which basic block follows which basic blocks
Basic blocks are maximal sequences of consecutive three-address
instructions such that

30 / 45
Basic Blocks and Flow Graphs

Flow graphs are graph representation of intermediate code


◮ Nodes are basic blocks
◮ Edges represent which basic block follows which basic blocks
Basic blocks are maximal sequences of consecutive three-address
instructions such that
◮ there are no jumps in the middle of the basic block

30 / 45
Basic Blocks and Flow Graphs

Flow graphs are graph representation of intermediate code


◮ Nodes are basic blocks
◮ Edges represent which basic block follows which basic blocks
Basic blocks are maximal sequences of consecutive three-address
instructions such that
◮ there are no jumps in the middle of the basic block
◮ once a control enters the basic block, it will leave the block only at the
last instruction

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

Common sub-expression elimination


Copy propagation
Loop invariant code motion
Partial redundancy elimination
Induction variable elimination and strength reduction
Code opimization needs information about the program
◮ which expressions are being recomputed in a function?
◮ which definitions reach a point?
All such information is gathered through data-flow analysis

33 / 45
Control Flow Analysis

Control flow analysis is used to understand the structure of control-flow


graphs
Study different features such as loops, back edges
Loops are important to optimize
◮ Program spends a lot of times in loops and recursive cycles
Identify and understand the control-flow (if-then-else, for-loops)

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

Node d dominates node n ( written as d dom n)

if every path from entry to n goes through d

Every node dominates itself

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

Out Entry = {Entry }


{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

Out Entry = {Entry }


{1} 1 1

{1,2} 2 2
{1,2,4}
{1,2,3} 3 a ∗ b 4 a∗b

{1,2,5} 5 5 BI for Out is {Entry}

{1,2,5,6} 6 a ∗ b

38 / 45
Dominators

Out Entry = {Entry }


{1} 1 1

{1,2} 2 2
{1,2,4}
{1,2,3} 3 a ∗ b 4 a∗b

{1,2,5} 5 5 BI for Out is {Entry}


Out n is the set of dominators of n
{1,2,5,6} 6 a ∗ b

38 / 45
Dominators

Out Entry = {Entry


\ }
{1} 1 1
Inn = Out p
p ∈pred (n)
{1,2} 2 2
{1,2,4}
{1,2,3} 3 a ∗ b 4 a∗b

{1,2,5} 5 5 BI for Out is {Entry}


Out n is the set of dominators of n
{1,2,5,6} 6 a ∗ b

38 / 45
Dominators

Out Entry = {Entry


\ }
{1} 1 1
Inn = Out p
p ∈pred (n)
{1,2} 2 2
{1,2,4}
{1,2,3} 3 a ∗ b 4 a∗b

{1,2,5} 5 5 BI for Out is {Entry}


Out n is the set of dominators of n
{1,2,5,6} 6 a ∗ b
Inn and Out n are sets of basic
blocks

38 / 45
Dominators

Out Entry = {Entry


\ }
{1} 1 1
Inn = Out p
p ∈pred (n)
{1,2} 2 2
{1,2,4} Out n = {n} ∪ Inn
{1,2,3} 3 a ∗ b 4 a∗b

{1,2,5} 5 5 BI for Out is {Entry}


Out n is the set of dominators of n
{1,2,5,6} 6 a ∗ b
Inn and Out n are sets of basic
blocks

38 / 45
Dominators

Out Entry = {Entry


\ }
{1} 1 1
Inn = Out p
p ∈pred (n)
{1,2} 2 2
{1,2,4} Out n = {n} ∪ Inn
{1,2,3} 3 a ∗ b 4 a∗b

{1,2,5} 5 5 BI for Out is {Entry}


Out n is the set of dominators of n
{1,2,5,6} 6 a ∗ b
Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

38 / 45
Dominators

Out Entry = {Entry


\ }
1 1
Inn = Out p
p ∈pred (n)
2 2
Out n = {n} ∪ Inn
3 a∗b 4 a∗b

5 5 BI for Out is {Entry}


Out n is the set of dominators of n
6 a∗b
Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
2 2
Out n = {n} ∪ Inn
3 a∗b 4 a∗b

5 5 BI for Out is {Entry}


Out n is the set of dominators of n
6 a∗b
Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
2 2
{1,2,3,4,5,6}
Out n = {n} ∪ Inn
3 a∗b 4 a∗b
{1,2,3,4,5,6}
{1,2,3,4,5,6}
5 5 BI for Out is {Entry}
{1,2,3,4,5,6}
Out n is the set of dominators of n
6 a∗b
{1,2,3,4,5,6} Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
{1}
2 2
{1,2,3,4,5,6}
Out n = {n} ∪ Inn
3 a∗b 4 a∗b
{1,2,3,4,5,6}
{1,2,3,4,5,6}
5 5 BI for Out is {Entry}
{1,2,3,4,5,6}
Out n is the set of dominators of n
6 a∗b
{1,2,3,4,5,6} Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
{1}
2 2
{1,2}
Out n = {n} ∪ Inn
3 a∗b 4 a∗b
{1,2,3,4,5,6}
{1,2,3,4,5,6}
5 5 BI for Out is {Entry}
{1,2,3,4,5,6}
Out n is the set of dominators of n
6 a∗b
{1,2,3,4,5,6} Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
{1}
2 2
{1,2}
{1,2} Out n = {n} ∪ Inn
3 a∗b 4 a∗b
{1,2,3,4,5,6}
{1,2,3,4,5,6}
5 5 BI for Out is {Entry}
{1,2,3,4,5,6}
Out n is the set of dominators of n
6 a∗b
{1,2,3,4,5,6} Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
{1}
2 2
{1,2}
{1,2} Out n = {n} ∪ Inn
3 a∗b 4 a∗b
{1,2,3}
{1,2,3,4,5,6}
5 5 BI for Out is {Entry}
{1,2,3,4,5,6}
Out n is the set of dominators of n
6 a∗b
{1,2,3,4,5,6} Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
{1}
2 2
{1,2}
{1,2} {1,2} Out n = {n} ∪ Inn
3 a∗b 4 a∗b
{1,2,3}
{1,2,3,4,5,6}
5 5 BI for Out is {Entry}
{1,2,3,4,5,6}
Out n is the set of dominators of n
6 a∗b
{1,2,3,4,5,6} Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
{1}
2 2
{1,2}
{1,2} {1,2} Out n = {n} ∪ Inn
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
5 5 BI for Out is {Entry}
{1,2,3,4,5,6}
Out n is the set of dominators of n
6 a∗b
{1,2,3,4,5,6} Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
{1}
2 2
{1,2}
{1,2} {1,2} Out n = {n} ∪ Inn
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
{1,2} BI for Out is {Entry}
5 5
{1,2,3,4,5,6}
Out n is the set of dominators of n
6 a∗b
{1,2,3,4,5,6} Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
{1}
2 2
{1,2}
{1,2} {1,2} Out n = {n} ∪ Inn
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
{1,2} BI for Out is {Entry}
5 5
{1,2,5}
Out n is the set of dominators of n
6 a∗b
{1,2,3,4,5,6} Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
{1}
2 2
{1,2}
{1,2} {1,2} Out n = {n} ∪ Inn
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
{1,2} BI for Out is {Entry}
5 5
{1,2,5}
Out n is the set of dominators of n
{1,2,5}
6 a∗b
{1,2,3,4,5,6} Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

39 / 45
Dominators

Out Entry = {Entry


\ }
1 1
{1} Inn = Out p
p ∈pred (n)
{1}
2 2
{1,2}
{1,2} {1,2} Out n = {n} ∪ Inn
3 a∗b 4 a∗b
{1,2,3}
{1,2,4}
{1,2} BI for Out is {Entry}
5 5
{1,2,5}
Out n is the set of dominators of n
{1,2,5}
6 a∗b
{1,2,5,6} Inn and Out n are sets of basic
blocks
Initialization is N which is the set
of all basic blocks

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

Single entry node called header


1 1

2 2

3 a∗b 4 a∗b

5 5

6 a∗b

44 / 45
Natural Loops

Single entry node called header


1 1 Header dominates all nodes in the
loop
2 2

3 a∗b 4 a∗b

5 5

6 a∗b

44 / 45
Natural Loops

Single entry node called header


1 1 Header dominates all nodes in the
loop
2 2 There must be a back edge that
enters the loop header

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

Consider a back edge n → d

1 1

2 2

3 a∗b 4 a∗b

5 5

6 a∗b

45 / 45
Natural Loops

Consider a back edge n → d


stack = empty, loop={d}
1 1

2 2

3 a∗b 4 a∗b

5 5

6 a∗b

45 / 45
Natural Loops

Consider a back edge n → d


stack = empty, loop={d}
1 1 insert (n)

2 2

3 a∗b 4 a∗b

5 5

6 a∗b

45 / 45
Natural Loops

Consider a back edge n → d


stack = empty, loop={d}
1 1 insert (n)

2 2

3 a∗b 4 a∗b

5 5

procedure insert (m) {


6 a∗b

45 / 45
Natural Loops

Consider a back edge n → d


stack = empty, loop={d}
1 1 insert (n)

2 2

3 a∗b 4 a∗b

5 5

procedure insert (m) {


6 a∗b
if m< loop then {

45 / 45
Natural Loops

Consider a back edge n → d


stack = empty, loop={d}
1 1 insert (n)

2 2

3 a∗b 4 a∗b

5 5

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}

45 / 45
Natural Loops

Consider a back edge n → d


stack = empty, loop={d}
1 1 insert (n)

2 2

3 a∗b 4 a∗b

5 5

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

45 / 45
Natural Loops

Consider a back edge n → d


stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2

3 a∗b 4 a∗b

5 5

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

45 / 45
Natural Loops

Consider a back edge n → d


stack = empty, loop={d}
1 1 insert (n)
while stack is not empty do{
2 2 pop(m, stack)

3 a∗b 4 a∗b

5 5

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

45 / 45
Natural Loops

Consider a back edge n → d


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

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

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

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

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

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

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

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

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

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

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

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

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

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

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

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

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

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

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

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

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

procedure insert (m) {


6 a∗b
if m< loop then {
loop = loop ∪ {m}
push(m, stack); } }

45 / 45

You might also like