0% found this document useful (0 votes)
25 views263 pages

Code Optimisation 2 - Bit Vector Framework

The document presents lecture notes on Bit Vector Data Flow Frameworks as part of the CS618 Program Analysis course at IIT Bombay. It covers various analyses such as Live Variables, Available Expressions, and Reaching Definitions, along with their definitions and equations. The slides are intended for academic use and are based on foundational texts in program analysis.

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)
25 views263 pages

Code Optimisation 2 - Bit Vector Framework

The document presents lecture notes on Bit Vector Data Flow Frameworks as part of the CS618 Program Analysis course at IIT Bombay. It covers various analyses such as Live Variables, Available Expressions, and Reaching Definitions, along with their definitions and equations. The slides are intended for academic use and are based on foundational texts in program analysis.

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

Bit Vector Data Flow Frameworks

Uday Khedker

Department of Computer Science and Engineering,


Indian Institute of Technology, Bombay

Slides from Program Analysis Course, IITB


Part I

About These Slides


Copyright
These slides constitute the lecture notes for CS618 Program Analysis course at
IIT Bombay and have been made available as teaching material accompanying
the book:

Uday Khedker, Amitabha Sanyal, and Bageshri Karkare. Data Flow


Analysis: Theory and Practice. CRC Press (Taylor and Francis Group).
2009.
(Indian edition published by Ane Books in 2013)

Apart from the above book, some slides are based on the material from the
following books
M. S. Hecht. Flow Analysis of Computer Programs. Elsevier
North-Holland Inc. 1977.
F. Nielson, H. R. Nielson, and C. Hankin. Principles of Program Analysis.
Springer-Verlag. 1998.
These slides are being made available under GNU FDL v1.2 or later purely for
academic or research use.
(IIT Bombay) Bit Vector Frameworks 3 / 79
Outline

Live Variables Analysis


Observations about Data Flow Analysis
Available Expressions Analysis
Anticipable Expressions Analysis
Reaching Definitions Analysis
Common Features of Bit Vector Frameworks

(IIT Bombay) Bit Vector Frameworks 4 / 79


Part II

Live Variables Analysis


Defining Live Variables Analysis
A variable v is live at a program point p, if some
path from p to program exit contains an r-value oc-
currence of v which is not preceded by an l-value
occurrence of v .

Start Start Start

p p p

v = a∗b v = a∗b

a = v +2 v = a+2 vv =
=v+
+22

End End End

(IIT Bombay) Bit Vector Frameworks 6 / 79


Defining Live Variables Analysis
A variable v is live at a program point p, if some
path from p to program exit contains an r-value oc-
currence of v which is not preceded by an l-value
occurrence of v .

v is live at p
Start Start Start

p p p

v = a∗b v = a∗b

a = v +2 v = a+2 vv =
=v+
+22

End End End

(IIT Bombay) Bit Vector Frameworks 6 / 79


Defining Live Variables Analysis
A variable v is live at a program point p, if some
path from p to program exit contains an r-value oc-
currence of v which is not preceded by an l-value
occurrence of v .

v is live at p v is not live at p


Start Start Start

p p p

v = a∗b v = a∗b

a = v +2 v = a+2 vv =
=v+
+22

End End End

(IIT Bombay) Bit Vector Frameworks 6 / 79


Defining Live Variables Analysis
A variable v is live at a program point p, if some
path from p to program exit contains an r-value oc-
currence of v which is not preceded by an l-value
occurrence of v .

v is live at p v is not live at p v is live at p


Start Start Start

p p p

v = a∗b v = a∗b

a = v +2 v = a+2 vv =
=v+
+22

End End End

(IIT Bombay) Bit Vector Frameworks 6 / 79


Defining Live Variables Analysis
A variable v is live at a program point p, if some
path from p to program exit contains an r-value oc- Path based
currence of v which is not preceded by an l-value specification
occurrence of v .

v is live at p v is not live at p v is live at p


Start Start Start

p p p

v = a∗b v = a∗b

a = v +2 v = a+2 vv =
=v+
+22

End End End

(IIT Bombay) Bit Vector Frameworks 6 / 79


Defining Data Flow Analysis for Live Variables Analysis

Ink = Genk ∪ (Out k − Kill k )


Genk , Kill k
Out k = Ini ∪ Inj

Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j

(IIT Bombay) Bit Vector Frameworks 7 / 79


Defining Data Flow Analysis for Live Variables Analysis

Single statements or Maximal groups


Basic Blocks ≡
of sequentially executed statements

Ink = Genk ∪ (Out k − Kill k )


Genk , Kill k
Out k = Ini ∪ Inj

Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j

(IIT Bombay) Bit Vector Frameworks 7 / 79


Defining Data Flow Analysis for Live Variables Analysis

Single statements or Maximal groups


Basic Blocks ≡
of sequentially executed statements

Ink = Genk ∪ (Out k − Kill k )


Genk , Kill k
Out k = Ini ∪ Inj

Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j

Control Transfer
(IIT Bombay) Bit Vector Frameworks 7 / 79
Defining Data Flow Analysis for Live Variables Analysis

Ink = Genk ∪ (Out k − Kill k )


Genk , Kill k
Out k = Ini ∪ Inj

Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j

(IIT Bombay) Bit Vector Frameworks 7 / 79


Defining Data Flow Analysis for Live Variables Analysis

Ink = Genk ∪ (Out k − Kill k )


Genk , Kill k
Out k = Ini ∪ Inj

Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j

Local Data Flow Properties


(IIT Bombay) Bit Vector Frameworks 7 / 79
Local Data Flow Properties for Live Variables
Analysis

Genn = { v | variable v is used in basic block n and


is not preceded by a definition of v }
Kill n = { v | basic block n contains a definition of v }

(IIT Bombay) Bit Vector Frameworks 8 / 79


Local Data Flow Properties for Live Variables
Analysis

r-value occurrence
Value is only read, e.g. x,y,z in
x.sum = y.data + z.data

Genn = { v | variable v is used in basic block n and


is not preceded by a definition of v }
Kill n = { v | basic block n contains a definition of v }

(IIT Bombay) Bit Vector Frameworks 8 / 79


Local Data Flow Properties for Live Variables
Analysis

l-value occurrence
r-value occurrence Value is modified e.g. y in
Value is only read, e.g. x,y,z in y = x.lptr
x.sum = y.data + z.data

Genn = { v | variable v is used in basic block n and


is not preceded by a definition of v }
Kill n = { v | basic block n contains a definition of v }

(IIT Bombay) Bit Vector Frameworks 8 / 79


Local Data Flow Properties for Live Variables
Analysis

l-value occurrence
r-value occurrence Value is modified e.g. y in
Value is only read, e.g. x,y,z in y = x.lptr
x.sum = y.data + z.data

Genn = { v | variable v is used in basic block n and


is not preceded by a definition of v }
Kill n = { v | basic block n contains a definition of v }

within n

(IIT Bombay) Bit Vector Frameworks 8 / 79


Local Data Flow Properties for Live Variables
Analysis

l-value occurrence
r-value occurrence Value is modified e.g. y in
Value is only read, e.g. x,y,z in y = x.lptr
x.sum = y.data + z.data

Genn = { v | variable v is used in basic block n and


is not preceded by a definition of v }
Kill n = { v | basic block n contains a definition of v }

within n
anywhere in n
(IIT Bombay) Bit Vector Frameworks 8 / 79
Defining Data Flow Analysis for Live Variables
Analysis

Ink = Genk ∪ (Out k − Kill k )


Genk , Kill k
Out k = Ini ∪ Inj

Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j

(IIT Bombay) Bit Vector Frameworks 9 / 79


Defining Data Flow Analysis for Live Variables
Analysis

Global Data Flow Properties

Ink = Genk ∪ (Out k − Kill k )


Genk , Kill k
Out k = Ini ∪ Inj

Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j

(IIT Bombay) Bit Vector Frameworks 9 / 79


Defining Data Flow Analysis for Live Variables
Analysis

Edge based
Global Data Flow Properties
specifications

Ink = Genk ∪ (Out k − Kill k )


Genk , Kill k
Out k = Ini ∪ Inj

Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j

(IIT Bombay) Bit Vector Frameworks 9 / 79


Data Flow Equations For Live Variables Analysis

Inn = (Out n − Kill n ) ∪ Genn



 [BI n is End block
Out n = Ins otherwise

s∈succ(n)

(IIT Bombay) Bit Vector Frameworks 10 / 79


Data Flow Equations For Live Variables Analysis

Inn = (Out n − Kill n ) ∪ Genn



 [BI n is End block
Out n = Ins otherwise

s∈succ(n)

Inn and Out n are sets of variables

(IIT Bombay) Bit Vector Frameworks 10 / 79


Data Flow Equations For Live Variables Analysis

Inn = (Out n − Kill n ) ∪ Genn



 [BI n is End block
Out n = Ins otherwise

s∈succ(n)

Inn and Out n are sets of variables


BI is boundary information representing the effect of calling contexts
◮ ∅ for local variables except for the values being returned
◮ set of global variables used further in any calling context
(can be safely approximated by the set of all global variables)

(IIT Bombay) Bit Vector Frameworks 10 / 79


Data Flow Equations for Our Example
In1 = (Out 1 − Kill 1 ) ∪ Gen1
1 w=x
Out 1 = In2
In2 = (Out 2 − Kill 2 ) ∪ Gen2
2 while (x.data < MAX) Out 2 = In3 ∪ In4
In3 = (Out 3 − Kill 3 ) ∪ Gen3
4 y = x.lptr x = x.rptr 3 Out 3 = In2
In4 = (Out 4 − Kill 4 ) ∪ Gen4
Out 4 = In5
5 z = New class of z
In5 = (Out 5 − Kill 5 ) ∪ Gen5
Out 5 = In6
6 y = y.lptr In6 = (Out 6 − Kill 6 ) ∪ Gen6
Out 6 = In7
7 z.sum = x.data + y.data In7 = (Out 7 − Kill 7 ) ∪ Gen7
Out 7 = In8
In8 = (Out 8 − Kill 8 ) ∪ Gen8
8 return z.sum
Out 8 = ∅
(IIT Bombay) Bit Vector Frameworks 11 / 79
Data Flow Equations for Our Example
In1 = (Out 1 − Kill 1 ) ∪ Gen1
1 w=x
Out 1 = In2
In2 = (Out 2 − Kill 2 ) ∪ Gen2
2 while (x.data < MAX) Out 2 = In3 ∪ In4
In3 = (Out 3 − Kill 3 ) ∪ Gen3
4 y = x.lptr x = x.rptr 3 Out 3 = In2
In4 = (Out 4 − Kill 4 ) ∪ Gen4
Out 4 = In5
5 z = New class of z
In5 = (Out 5 − Kill 5 ) ∪ Gen5
Out 5 = In6
6 y = y.lptr In6 = (Out 6 − Kill 6 ) ∪ Gen6
Out 6 = In7
7 z.sum = x.data + y.data In7 = (Out 7 − Kill 7 ) ∪ Gen7
Out 7 = In8
In8 = (Out 8 − Kill 8 ) ∪ Gen8
8 return z.sum
Out 8 = ∅
(IIT Bombay) Bit Vector Frameworks 11 / 79
Data Flow Equations for Our Example
In1 = (Out 1 − Kill 1 ) ∪ Gen1
1 w=x
Out 1 = In2
In2 = (Out 2 − Kill 2 ) ∪ Gen2
2 while (x.data < MAX) Out 2 = In3 ∪ In4
In3 = (Out 3 − Kill 3 ) ∪ Gen3
4 y = x.lptr x = x.rptr 3 Out 3 = In2
In4 = (Out 4 − Kill 4 ) ∪ Gen4
Out 4 = In5
5 z = New class of z
In5 = (Out 5 − Kill 5 ) ∪ Gen5
Out 5 = In6
6 y = y.lptr In6 = (Out 6 − Kill 6 ) ∪ Gen6
Out 6 = In7
7 z.sum = x.data + y.data In7 = (Out 7 − Kill 7 ) ∪ Gen7
Out 7 = In8
In8 = (Out 8 − Kill 8 ) ∪ Gen8
8 return z.sum
Out 8 = ∅
(IIT Bombay) Bit Vector Frameworks 11 / 79
Data Flow Equations for Our Example
In1 = (Out 1 − Kill 1 ) ∪ Gen1
1 w=x
Out 1 = In2
In2 = (Out 2 − Kill 2 ) ∪ Gen2
2 while (x.data < MAX) Out 2 = In3 ∪ In4
In3 = (Out 3 − Kill 3 ) ∪ Gen3
4 y = x.lptr x = x.rptr 3 Out 3 = In2
In4 = (Out 4 − Kill 4 ) ∪ Gen4
Out 4 = In5
5 z = New class of z
In5 = (Out 5 − Kill 5 ) ∪ Gen5
Out 5 = In6
6 y = y.lptr In6 = (Out 6 − Kill 6 ) ∪ Gen6
Out 6 = In7
7 z.sum = x.data + y.data In7 = (Out 7 − Kill 7 ) ∪ Gen7
Out 7 = In8
In8 = (Out 8 − Kill 8 ) ∪ Gen8
8 return z.sum
Out 8 = ∅
(IIT Bombay) Bit Vector Frameworks 11 / 79
Data Flow Equations for Our Example
In1 = (Out 1 − Kill 1 ) ∪ Gen1
1 w=x
Out 1 = In2
In2 = (Out 2 − Kill 2 ) ∪ Gen2
2 while (x.data < MAX) Out 2 = In3 ∪ In4
In3 = (Out 3 − Kill 3 ) ∪ Gen3
4 y = x.lptr x = x.rptr 3 Out 3 = In2
In4 = (Out 4 − Kill 4 ) ∪ Gen4
Out 4 = In5
5 z = New class of z
In5 = (Out 5 − Kill 5 ) ∪ Gen5
Out 5 = In6
6 y = y.lptr In6 = (Out 6 − Kill 6 ) ∪ Gen6
Out 6 = In7
7 z.sum = x.data + y.data In7 = (Out 7 − Kill 7 ) ∪ Gen7
Out 7 = In8
In8 = (Out 8 − Kill 8 ) ∪ Gen8
8 return z.sum
Out 8 = ∅
(IIT Bombay) Bit Vector Frameworks 11 / 79
Data Flow Equations for Our Example
In1 = (Out 1 − Kill 1 ) ∪ Gen1
1 w=x
Out 1 = In2
In2 = (Out 2 − Kill 2 ) ∪ Gen2
2 while (x.data < MAX) Out 2 = In3 ∪ In4
In3 = (Out 3 − Kill 3 ) ∪ Gen3
4 y = x.lptr x = x.rptr 3 Out 3 = In2
In4 = (Out 4 − Kill 4 ) ∪ Gen4
Out 4 = In5
5 z = New class of z
In5 = (Out 5 − Kill 5 ) ∪ Gen5
Out 5 = In6
6 y = y.lptr In6 = (Out 6 − Kill 6 ) ∪ Gen6
Out 6 = In7
7 z.sum = x.data + y.data In7 = (Out 7 − Kill 7 ) ∪ Gen7
Out 7 = In8
In8 = (Out 8 − Kill 8 ) ∪ Gen8
8 return z.sum
Out 8 = ∅
(IIT Bombay) Bit Vector Frameworks 11 / 79
Data Flow Equations for Our Example
In1 = (Out 1 − Kill 1 ) ∪ Gen1
1 w=x
Out 1 = In2
In2 = (Out 2 − Kill 2 ) ∪ Gen2
2 while (x.data < MAX) Out 2 = In3 ∪ In4
In3 = (Out 3 − Kill 3 ) ∪ Gen3
4 y = x.lptr x = x.rptr 3 Out 3 = In2
In4 = (Out 4 − Kill 4 ) ∪ Gen4
Out 4 = In5
5 z = New class of z
In5 = (Out 5 − Kill 5 ) ∪ Gen5
Out 5 = In6
6 y = y.lptr Cyclic Dependence In6 = (Out 6 − Kill 6 ) ∪ Gen6
Out 6 = In7
7 z.sum = x.data + y.data In7 = (Out 7 − Kill 7 ) ∪ Gen7
Out 7 = In8
In8 = (Out 8 − Kill 8 ) ∪ Gen8
8 return z.sum
Out 8 = ∅
(IIT Bombay) Bit Vector Frameworks 11 / 79
Performing Live Variables Analysis
Gen = {x}, Kill = {w }
w=x

Gen = {x}, Kill = ∅


while (x.data < MAX)

Gen = {x}, Kill = {y } Gen = {x}, Kill = {x}


y = x.lptr x = x.rptr

Gen = ∅, Kill = {z}


z = New class of z

Gen = {y }, Kill = {y }
y = y.lptr

Gen = {x, y , z}, Kill = ∅


z.sum = x.data + y.data

Gen = {z}, Kill = ∅


return z.sum

(IIT Bombay) Bit Vector Frameworks 12 / 79


Performing Live Variables Analysis
Gen = {x}, Kill = {w }
w=x

Gen = {x}, Kill = ∅


while (x.data < MAX)

Gen = {x}, Kill = {y } Gen = {x}, Kill = {x}


y = x.lptr x = x.rptr

Gen = ∅, Kill = {z}


z = New class of z

Gen = {y }, Kill = {y } Gen and Kill need not be


y = y.lptr mutually exclusive

Gen = {x, y , z}, Kill = ∅


z.sum = x.data + y.data

Gen = {z}, Kill = ∅


return z.sum

(IIT Bombay) Bit Vector Frameworks 12 / 79


Performing Live Variables Analysis
Gen = {x}, Kill = {w }
w=x

Gen = {x}, Kill = ∅


while (x.data < MAX)

Gen = {x}, Kill = {y } Gen = {x}, Kill = {x}


y = x.lptr x = x.rptr

Gen = ∅, Kill = {z}


z = New class of z

Gen = {y }, Kill = {y }
y = y.lptr

Gen = {x, y , z}, Kill = ∅


z.sum = x.data + y.data
z is an r-value occurrence and
Gen = {z}, Kill = ∅ not an l-value occurrence
return z.sum

(IIT Bombay) Bit Vector Frameworks 12 / 79


Performing Live Variables Analysis
∅ Gen = {x}, Kill = {w }
∅ w=x

∅ Gen = {x}, Kill = ∅


∅ while (x.data < MAX)

∅ Gen = {x}, Kill = {y } ∅ Gen = {x}, Kill = {x}


y = x.lptr ∅ x = x.rptr

∅ Gen = ∅, Kill = {z}
z = New class of z

∅ Gen = {y }, Kill = {y }
y = y.lptr

∅ Gen = {x, y , z}, Kill = ∅
z.sum = x.data + y.data Initialization

∅ Gen = {z}, Kill = ∅
return z.sum

(IIT Bombay) Bit Vector Frameworks 12 / 79
Performing Live Variables Analysis
{x} Gen = {x}, Kill = {w }
Ignoring MAX be- w=x
{x}
cause we are doing
analysis for pointer {x} Gen = {x}, Kill = ∅
variables w, x, y, z {x} while (x.data < MAX)
{x} Gen = {x}, Kill = {y } {x} Gen = {x}, Kill = {x}
{x, y } y = x.lptr ∅ x = x.rptr

{x, y } Gen = ∅, Kill = {z}


{x, y , z} z = New class of z

{x, y , z} Gen = {y }, Kill = {y } Traversal


{x, y , z} y = y.lptr

{x, y , z} Gen = {x, y , z}, Kill = ∅


{z} z.sum = x.data + y.data

{z} Gen = {z}, Kill = ∅


return z.sum

(IIT Bombay) Bit Vector Frameworks 12 / 79
Performing Live Variables Analysis
{x} Gen = {x}, Kill = {w } {x}
Ignoring MAX be- w=x
{x} {x}
cause we are doing
analysis for pointer {x} Gen = {x}, Kill = ∅ {x}
variables w, x, y, z {x} while (x.data < MAX) {x}
{x} Gen = {x}, Kill = {y } {x} {x} Gen = {x}, Kill = {x} {x}
{x, y } y = x.lptr {x, y } ∅ x = x.rptr {x}
{x, y } Gen = ∅, Kill = {z} {x, y }
{x, y , z} z = New class of z {x, y , z}
{x, y , z} Gen = {y }, Kill = {y } {x, y , z} Traversal
{x, y , z} y = y.lptr {x, y , z}
{x, y , z} Gen = {x, y , z}, Kill = ∅ {x, y , z}
Iteration #1
{z} z.sum = x.data + y.data {z}
{z} Gen = {z}, Kill = ∅ {z}
return z.sum
∅ ∅
(IIT Bombay) Bit Vector Frameworks 12 / 79
Performing Live Variables Analysis
{x} Gen = {x}, Kill = {w } {x}
Ignoring MAX be- w=x
{x} {x}
cause we are doing
analysis for pointer {x} Gen = {x}, Kill = ∅ {x}
variables w, x, y, z {x} while (x.data < MAX) {x}
{x} Gen = {x}, Kill = {y } {x} {x} Gen = {x}, Kill = {x} {x}
{x, y } y = x.lptr {x, y } ∅ x = x.rptr {x}
{x, y } Gen = ∅, Kill = {z} {x, y }
{x, y , z} z = New class of z {x, y , z}
{x, y , z} Gen = {y }, Kill = {y } {x, y , z} Traversal
{x, y , z} y = y.lptr {x, y , z}
{x, y , z} Gen = {x, y , z}, Kill = ∅ {x, y , z}
Iteration #2
{z} z.sum = x.data + y.data {z}
{z} Gen = {z}, Kill = ∅ {z}
return z.sum
∅ ∅
(IIT Bombay) Bit Vector Frameworks 12 / 79
Performing Live Variables Analysis
Local data flow properties when basic blocks contain multiple statements

Gen = {x}, Kill = {w }


w=x

Gen = {x}, Kill = ∅


while (x.data < MAX)

Gen = {x}, Kill = {y , z} Gen = {x}, Kill = {x}


y = x.lptr x = x.rptr
z = New class of z
y = y.lptr
z.sum = x.data + y.data
return z.sum

(IIT Bombay) Bit Vector Frameworks 13 / 79


Local Data Flow Properties for Live Variables
Analysis

Inn = Genn ∪ (Out n − Kill n )

Genn : Use not preceded by definition

Kill n : Definition anywhere in a block

(IIT Bombay) Bit Vector Frameworks 14 / 79


Local Data Flow Properties for Live Variables
Analysis

Inn = Genn ∪ (Out n − Kill n )

Genn : Use not preceded by definition

Upwards exposed use

Kill n : Definition anywhere in a block

Stop the effect from being propagated across a block

(IIT Bombay) Bit Vector Frameworks 14 / 79


Local Data Flow Properties for Live Variables
Analysis

Example
Case Local Information Explanation
basic block

1 v 6∈ Genn v 6∈ Kill n

2 v ∈ Genn v 6∈ Kill n

3 v 6∈ Genn v ∈ Kill n

4 v ∈ Genn v ∈ Kill n

(IIT Bombay) Bit Vector Frameworks 15 / 79


Local Data Flow Properties for Live Variables
Analysis

Example
Case Local Information Explanation
basic block
a =b+c liveness of v is unaffected
1 v 6∈ Genn v 6∈ Kill n b =c ∗d by the basic block
a =b+c v becomes live
2 v ∈ Genn v 6∈ Kill n b =v ∗d before the basic block
a =b+c
v =c ∗d
v ceases to be live
3 v 6∈ Genn v ∈ Kill n OR before the basic block
v =a+b
c =v ∗d

a =v +c liveness of v is killed
4 v ∈ Genn v ∈ Kill n v =c ∗d but v becomes live
before the basic block

(IIT Bombay) Bit Vector Frameworks 15 / 79


Using Data Flow Information of Live Variables
Analysis

Used for register allocation


If variable x is live in a basic block b, it is a potential candidate for
register allocation

(IIT Bombay) Bit Vector Frameworks 16 / 79


Using Data Flow Information of Live Variables
Analysis

Used for register allocation


If variable x is live in a basic block b, it is a potential candidate for
register allocation

Used for dead code elimination


If variable x is not live after an assignment x = . . ., then the assignment is
redundant and can be deleted as dead code

(IIT Bombay) Bit Vector Frameworks 16 / 79


Tutorial Problem 1: Perform Dead Code Elimination

a = 4
b = 2
c = 3 1
m = c*2

if (a≤m) 2
F T
3 a = a+1

if (a<12) 4
T
F
t1 = a+b
5 a = t1+c
print "Hi"

print "Hello" 6

(IIT Bombay) Bit Vector Frameworks 17 / 79


Tutorial Problem 1: Perform Dead Code Elimination
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 {a, b, c} {a, t1}
F T 6 ∅ ∅
3 a = a+1

if (a<12) 4
T
F
t1 = a+b
5 a = t1+c
print "Hi"

print "Hello" 6

(IIT Bombay) Bit Vector Frameworks 17 / 79


Tutorial Problem 1: Perform Dead Code Elimination
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 {a, b, c} {a, t1}
F T 6 ∅ ∅
3 a = a+1 Global Data Flow Information
Iteration #1 Iteration #2
if (a<12) 4 Out In Out In
T 6 ∅ ∅
F 5 ∅ {a, b, c}
t1 = a+b
5 a = t1+c 4 {a, b, c} {a, b, c}
print "Hi" 3 ∅ {a}
2 {a, b, c} {a, b, c, m}
print "Hello" 6 1 {a, b, c, m} ∅

(IIT Bombay) Bit Vector Frameworks 17 / 79


Tutorial Problem 1: Perform Dead Code Elimination
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 {a, b, c} {a, t1}
F T 6 ∅ ∅
3 a = a+1 Global Data Flow Information
Iteration #1 Iteration #2
if (a<12) 4 Out In Out In
T 6 ∅ ∅ ∅ ∅
F 5 ∅ {a, b, c} ∅ {a, b, c}
t1 = a+b
5 a = t1+c 4 {a, b, c} {a, b, c} {a, b, c} {a, b, c}
print "Hi" 3 ∅ {a} {a, b, c, m} {a, b, c, m}
2 {a, b, c} {a, b, c, m} {a, b, c, m} {a, b, c, m}
print "Hello" 6 1 {a, b, c, m} ∅ {a, b, c, m} ∅

(IIT Bombay) Bit Vector Frameworks 17 / 79


Tutorial Problem 1: Perform Dead Code Elimination
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 {a, b, c} {a, t1}
F T 6 ∅ ∅
3 a = a+1 Global Data Flow Information
Iteration #1 Iteration #2
if (a<12) 4 Out In Out In
T 6 ∅ ∅ ∅ ∅
F 5 ∅ {a, b, c} ∅ {a, b, c}
t1 = a+b
5 a = t1+c 4 {a, b, c} {a, b, c} {a, b, c} {a, b, c}
print "Hi" 3 ∅ {a} {a, b, c, m} {a, b, c, m}
2 {a, b, c} {a, b, c, m} {a, b, c, m} {a, b, c, m}
print "Hello" 6 1 {a, b, c, m} ∅ {a, b, c, m} ∅

(IIT Bombay) Bit Vector Frameworks 17 / 79


Tutorial Problem 1: Observeations About Round #1
of DCE

We can repeat liveness analysis on the optimized code and then optimize
it further
This can continue as long code continues to change

A better approach would be to perform strong liveness analysis


The code needs to be optimized only once

Here we show the repeated application only to show the scope of further
optimizations

(IIT Bombay) Bit Vector Frameworks 18 / 79


Tutorial Problem 1: Round #2 of DCE
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 {a, b} {t1}
F T 6 ∅ ∅
3 a = a+1

if (a<12) 4
T
F
t1 = a+b
5 a = t1+c
print "Hi"

print "Hello" 6

(IIT Bombay) Bit Vector Frameworks 19 / 79


Tutorial Problem 1: Round #2 of DCE
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 {a, b} {t1}
F T 6 ∅ ∅
3 a = a+1 Global Data Flow Information
Iteration #1 Iteration #2
if (a<12) 4 Out In Out In
T 6 ∅ ∅
F 5 ∅ {a, b}
t1 = a+b
5 a = t1+c 4 {a, b} {a, b}
print "Hi" 3 ∅ {a}
2 {a, b} {a, b, m}
print "Hello" 6 1 {a, b, m} ∅

(IIT Bombay) Bit Vector Frameworks 19 / 79


Tutorial Problem 1: Round #2 of DCE
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 {a, b} {t1}
F T 6 ∅ ∅
3 a = a+1 Global Data Flow Information
Iteration #1 Iteration #2
if (a<12) 4 Out In Out In
T 6 ∅ ∅ ∅ ∅
F 5 ∅ {a, b} ∅ {a, b}
t1 = a+b
5 a = t1+c 4 {a, b} {a, b} {a, b} {a, b}
print "Hi" 3 ∅ {a} {a, b, m} {a, b, m}
2 {a, b} {a, b, m} {a, b, m} {a, b, m}
print "Hello" 6 1 {a, b, m} ∅ {a, b, m} ∅

(IIT Bombay) Bit Vector Frameworks 19 / 79


Tutorial Problem 1: Round #2 of DCE
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 {a, b} {t1}
F T 6 ∅ ∅
3 a = a+1 Global Data Flow Information
Iteration #1 Iteration #2
if (a<12) 4 Out In Out In
T 6 ∅ ∅ ∅ ∅
F 5 ∅ {a, b} ∅ {a, b}
t1 = a+b
5 a = t1+c 4 {a, b} {a, b} {a, b} {a, b}
print "Hi" 3 ∅ {a} {a, b, m} {a, b, m}
2 {a, b} {a, b, m} {a, b, m} {a, b, m}
print "Hello" 6 1 {a, b, m} ∅ {a, b, m} ∅

(IIT Bombay) Bit Vector Frameworks 19 / 79


Tutorial Problem 1: Round #3 of DCE
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 ∅ ∅
F T 6 ∅ ∅
3 a = a+1

if (a<12) 4
T
F
t1 = a+b
5 a = t1+c
print "Hi"

print "Hello" 6

(IIT Bombay) Bit Vector Frameworks 20 / 79


Tutorial Problem 1: Round #3 of DCE
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 ∅ ∅
F T 6 ∅ ∅
3 a = a+1 Global Data Flow Information
Iteration #1 Iteration #2
if (a<12) 4 Out In Out In
T 6 ∅ ∅
F 5 ∅ ∅
t1 = a+b
5 a = t1+c 4 ∅ {a}
print "Hi" 3 ∅ {a}
2 {a} {a, m}
print "Hello" 6 1 {a, m} ∅

(IIT Bombay) Bit Vector Frameworks 20 / 79


Tutorial Problem 1: Round #3 of DCE
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 ∅ ∅
F T 6 ∅ ∅
3 a = a+1 Global Data Flow Information
Iteration #1 Iteration #2
if (a<12) 4 Out In Out In
T 6 ∅ ∅ ∅ ∅
F 5 ∅ ∅ ∅ ∅
t1 = a+b
5 a = t1+c 4 ∅ {a} ∅ {a}
print "Hi" 3 ∅ {a} {a, m} {a, m}
2 {a} {a, m} {a, m} {a, m}
print "Hello" 6 1 {a, m} ∅ {a, m} ∅

(IIT Bombay) Bit Vector Frameworks 20 / 79


Tutorial Problem 1: Round #3 of DCE
Local Data Flow Information
a = 4 Gen Kill
b = 2 1 ∅ {a, b, c, m}
c = 3 1
m = c*2 2 {a, m} ∅
3 {a} {a}
4 {a} ∅
if (a≤m) 2 5 ∅ ∅
F T 6 ∅ ∅
3 a = a+1 Global Data Flow Information
Iteration #1 Iteration #2
if (a<12) 4 Out In Out In
T 6 ∅ ∅ ∅ ∅
F 5 ∅ ∅ ∅ ∅
t1 = a+b
5 a = t1+c 4 ∅ {a} ∅ {a}
print "Hi" 3 ∅ {a} {a, m} {a, m}
2 {a} {a, m} {a, m} {a, m}
print "Hello" 6 1 {a, m} ∅ {a, m} ∅

(IIT Bombay) Bit Vector Frameworks 20 / 79


Part III

Some Observations
What Does Data Flow Analysis Involve?

Defining the analysis.


Formulating the analysis.

Performing the analysis.

(IIT Bombay) Bit Vector Frameworks 22 / 79


What Does Data Flow Analysis Involve?

Defining the analysis. Define the properties of execution paths


Formulating the analysis.

Performing the analysis.

(IIT Bombay) Bit Vector Frameworks 22 / 79


What Does Data Flow Analysis Involve?

Defining the analysis. Define the properties of execution paths


Formulating the analysis. Define data flow equations
◮ Linear simultaneous equations on sets rather than numbers
◮ Later we will generalize the domain of values
Performing the analysis.

(IIT Bombay) Bit Vector Frameworks 22 / 79


What Does Data Flow Analysis Involve?

Defining the analysis. Define the properties of execution paths


Formulating the analysis. Define data flow equations
◮ Linear simultaneous equations on sets rather than numbers
◮ Later we will generalize the domain of values
Performing the analysis. Solve data flow equations for the given program
flow graph

(IIT Bombay) Bit Vector Frameworks 22 / 79


What Does Data Flow Analysis Involve?

Defining the analysis. Define the properties of execution paths


Formulating the analysis. Define data flow equations
◮ Linear simultaneous equations on sets rather than numbers
◮ Later we will generalize the domain of values
Performing the analysis. Solve data flow equations for the given program
flow graph
Many unanswered questions
Initial value? Termination? Complexity? Properties of Solutions?

(IIT Bombay) Bit Vector Frameworks 22 / 79


A Digression: Iterative Solution of Linear
Simultaneous Equations

Simultaneous equations represented in the form of the product of a matrix


of coefficients (A) with the vector of unknowns (x)

Ax = b

Start with approximate values


Compute new values repeatedly from old values
Two classical methods
◮ Gauss-Seidel Method (Gauss: 1823, 1826), (Seidel: 1874)
◮ Jacobi Method (Jacobi: 1845)

(IIT Bombay) Bit Vector Frameworks 23 / 79


A Digression: An Example of Iterative Solution of
Linear Simultaneous Equations

Equations Solution
4w = x + y + 32
4x = y + z + 32
w = x = y = z = 16
4y = z + w + 32
4z = w + x + 32

Rewrite the equations to define w , x, y , and z


w = 0.25x + 0.25y + 8
x = 0.25y + 0.25z + 8
y = 0.25z + 0.25w + 8
z = 0.25w + 0.25x + 8
Assume some initial values of w0 , x0 , y0 , and z0
Compute wi , xi , yi , and zi within some margin of error

(IIT Bombay) Bit Vector Frameworks 24 / 79


A Digression: Gauss-Seidel Method
Equations Initial Values Error Margin
w = 0.25x + 0.25y + 8 w0 = 24 wi +1 − wi ≤ 0.35
x = 0.25y + 0.25z + 8 x0 = 24 xi +1 − xi ≤ 0.35
y = 0.25z + 0.25w + 8 y0 = 24 yi +1 − yi ≤ 0.35
z = 0.25w + 0.25x + 8 z0 = 24 zi +1 − zi ≤ 0.35

Iteration 1 Iteration 2 Iteration 3

Iteration 4 Iteration 5

(IIT Bombay) Bit Vector Frameworks 25 / 79


A Digression: Gauss-Seidel Method
Equations Initial Values Error Margin
w = 0.25x + 0.25y + 8 w0 = 24 wi +1 − wi ≤ 0.35
x = 0.25y + 0.25z + 8 x0 = 24 xi +1 − xi ≤ 0.35
y = 0.25z + 0.25w + 8 y0 = 24 yi +1 − yi ≤ 0.35
z = 0.25w + 0.25x + 8 z0 = 24 zi +1 − zi ≤ 0.35

Iteration 1 Iteration 2 Iteration 3


w1 = 6 + 6 + 8 = 20
x1 = 6 + 6 + 8 = 20
y1 = 6 + 6 + 8 = 20
z1 = 6 + 6 + 8 = 20

Iteration 4 Iteration 5

(IIT Bombay) Bit Vector Frameworks 25 / 79


A Digression: Gauss-Seidel Method
Equations Initial Values Error Margin
w = 0.25x + 0.25y + 8 w0 = 24 wi +1 − wi ≤ 0.35
x = 0.25y + 0.25z + 8 x0 = 24 xi +1 − xi ≤ 0.35
y = 0.25z + 0.25w + 8 y0 = 24 yi +1 − yi ≤ 0.35
z = 0.25w + 0.25x + 8 z0 = 24 zi +1 − zi ≤ 0.35

Iteration 1 Iteration 2 Iteration 3


w1 = 6 + 6 + 8 = 20 w2 = 5 + 5 + 8 = 18
x1 = 6 + 6 + 8 = 20 x2 = 5 + 5 + 8 = 18
y1 = 6 + 6 + 8 = 20 y2 = 5 + 5 + 8 = 18
z1 = 6 + 6 + 8 = 20 z2 = 5 + 5 + 8 = 18

Iteration 4 Iteration 5

(IIT Bombay) Bit Vector Frameworks 25 / 79


A Digression: Gauss-Seidel Method
Equations Initial Values Error Margin
w = 0.25x + 0.25y + 8 w0 = 24 wi +1 − wi ≤ 0.35
x = 0.25y + 0.25z + 8 x0 = 24 xi +1 − xi ≤ 0.35
y = 0.25z + 0.25w + 8 y0 = 24 yi +1 − yi ≤ 0.35
z = 0.25w + 0.25x + 8 z0 = 24 zi +1 − zi ≤ 0.35

Iteration 1 Iteration 2 Iteration 3


w1 = 6 + 6 + 8 = 20 w2 = 5 + 5 + 8 = 18 w3 = 4.5 + 4.5 + 8 = 17
x1 = 6 + 6 + 8 = 20 x2 = 5 + 5 + 8 = 18 x3 = 4.5 + 4.5 + 8 = 17
y1 = 6 + 6 + 8 = 20 y2 = 5 + 5 + 8 = 18 y3 = 4.5 + 4.5 + 8 = 17
z1 = 6 + 6 + 8 = 20 z2 = 5 + 5 + 8 = 18 z3 = 4.5 + 4.5 + 8 = 17

Iteration 4 Iteration 5

(IIT Bombay) Bit Vector Frameworks 25 / 79


A Digression: Gauss-Seidel Method
Equations Initial Values Error Margin
w = 0.25x + 0.25y + 8 w0 = 24 wi +1 − wi ≤ 0.35
x = 0.25y + 0.25z + 8 x0 = 24 xi +1 − xi ≤ 0.35
y = 0.25z + 0.25w + 8 y0 = 24 yi +1 − yi ≤ 0.35
z = 0.25w + 0.25x + 8 z0 = 24 zi +1 − zi ≤ 0.35

Iteration 1 Iteration 2 Iteration 3


w1 = 6 + 6 + 8 = 20 w2 = 5 + 5 + 8 = 18 w3 = 4.5 + 4.5 + 8 = 17
x1 = 6 + 6 + 8 = 20 x2 = 5 + 5 + 8 = 18 x3 = 4.5 + 4.5 + 8 = 17
y1 = 6 + 6 + 8 = 20 y2 = 5 + 5 + 8 = 18 y3 = 4.5 + 4.5 + 8 = 17
z1 = 6 + 6 + 8 = 20 z2 = 5 + 5 + 8 = 18 z3 = 4.5 + 4.5 + 8 = 17

Iteration 4 Iteration 5
w4 = 4.25 + 4.25 + 8 = 16.5
x4 = 4.25 + 4.25 + 8 = 16.5
y4 = 4.25 + 4.25 + 8 = 16.5
z4 = 4.25 + 4.25 + 8 = 16.5

(IIT Bombay) Bit Vector Frameworks 25 / 79


A Digression: Gauss-Seidel Method
Equations Initial Values Error Margin
w = 0.25x + 0.25y + 8 w0 = 24 wi +1 − wi ≤ 0.35
x = 0.25y + 0.25z + 8 x0 = 24 xi +1 − xi ≤ 0.35
y = 0.25z + 0.25w + 8 y0 = 24 yi +1 − yi ≤ 0.35
z = 0.25w + 0.25x + 8 z0 = 24 zi +1 − zi ≤ 0.35

Iteration 1 Iteration 2 Iteration 3


w1 = 6 + 6 + 8 = 20 w2 = 5 + 5 + 8 = 18 w3 = 4.5 + 4.5 + 8 = 17
x1 = 6 + 6 + 8 = 20 x2 = 5 + 5 + 8 = 18 x3 = 4.5 + 4.5 + 8 = 17
y1 = 6 + 6 + 8 = 20 y2 = 5 + 5 + 8 = 18 y3 = 4.5 + 4.5 + 8 = 17
z1 = 6 + 6 + 8 = 20 z2 = 5 + 5 + 8 = 18 z3 = 4.5 + 4.5 + 8 = 17

Iteration 4 Iteration 5
w4 = 4.25 + 4.25 + 8 = 16.5 w5 = 4.125 + 4.125 + 8 = 16.25
x4 = 4.25 + 4.25 + 8 = 16.5 x5 = 4.125 + 4.125 + 8 = 16.25
y4 = 4.25 + 4.25 + 8 = 16.5 y5 = 4.125 + 4.125 + 8 = 16.25
z4 = 4.25 + 4.25 + 8 = 16.5 z5 = 4.125 + 4.125 + 8 = 16.25

(IIT Bombay) Bit Vector Frameworks 25 / 79


A Digression: Jacobi Method
Use values from the current iteration wherever possible
Equations Initial Values Error Margin
w = 0.25x + 0.25y + 8 w0 = 24 wi +1 − wi ≤ 0.35
x = 0.25y + 0.25z + 8 x0 = 24 xi +1 − xi ≤ 0.35
y = 0.25z + 0.25w + 8 y0 = 24 yi +1 − yi ≤ 0.35
z = 0.25w + 0.25x + 8 z0 = 24 zi +1 − zi ≤ 0.35
Iteration 1 Iteration 2

Iteration 3 Iteration 4

(IIT Bombay) Bit Vector Frameworks 26 / 79


A Digression: Jacobi Method
Use values from the current iteration wherever possible
Equations Initial Values Error Margin
w = 0.25x + 0.25y + 8 w0 = 24 wi +1 − wi ≤ 0.35
x = 0.25y + 0.25z + 8 x0 = 24 xi +1 − xi ≤ 0.35
y = 0.25z + 0.25w + 8 y0 = 24 yi +1 − yi ≤ 0.35
z = 0.25w + 0.25x + 8 z0 = 24 zi +1 − zi ≤ 0.35
Iteration 1 Iteration 2
w1 = 6 + 6 + 8 = 20
x1 = 6 + 6 + 8 = 20
y1 = 6 + 5 + 8 = 19
z1 = 5 + 5 + 8 = 18

Iteration 3 Iteration 4

(IIT Bombay) Bit Vector Frameworks 26 / 79


A Digression: Jacobi Method
Use values from the current iteration wherever possible
Equations Initial Values Error Margin
w = 0.25x + 0.25y + 8 w0 = 24 wi +1 − wi ≤ 0.35
x = 0.25y + 0.25z + 8 x0 = 24 xi +1 − xi ≤ 0.35
y = 0.25z + 0.25w + 8 y0 = 24 yi +1 − yi ≤ 0.35
z = 0.25w + 0.25x + 8 z0 = 24 zi +1 − zi ≤ 0.35
Iteration 1 Iteration 2
w1 = 6 + 6 + 8 = 20 w2 = 5 + 4.75 + 8 = 17.75
x1 = 6 + 6 + 8 = 20 x2 = 4.75 + 4.5 + 8 = 17.25
y1 = 6 + 5 + 8 = 19 y2 = 4.5 + 4.4375 + 8 = 16.935
z1 = 5 + 5 + 8 = 18 z2 = 4.4375 + 4.375 + 8 = 16.8125

Iteration 3 Iteration 4

(IIT Bombay) Bit Vector Frameworks 26 / 79


A Digression: Jacobi Method
Use values from the current iteration wherever possible
Equations Initial Values Error Margin
w = 0.25x + 0.25y + 8 w0 = 24 wi +1 − wi ≤ 0.35
x = 0.25y + 0.25z + 8 x0 = 24 xi +1 − xi ≤ 0.35
y = 0.25z + 0.25w + 8 y0 = 24 yi +1 − yi ≤ 0.35
z = 0.25w + 0.25x + 8 z0 = 24 zi +1 − zi ≤ 0.35
Iteration 1 Iteration 2
w1 = 6 + 6 + 8 = 20 w2 = 5 + 4.75 + 8 = 17.75
x1 = 6 + 6 + 8 = 20 x2 = 4.75 + 4.5 + 8 = 17.25
y1 = 6 + 5 + 8 = 19 y2 = 4.5 + 4.4375 + 8 = 16.935
z1 = 5 + 5 + 8 = 18 z2 = 4.4375 + 4.375 + 8 = 16.8125

Iteration 3 Iteration 4
w3 = 4.3125 + 4.23375 + 8 = 16.54625
x3 = 4.23375 + 4.23375 + 8 = 16.436875
y3 = 4.23375 + 4.1365625 + 8 = 16.370
z3 = 4.1365625 + 4.11 + 8 = 16.34375
(IIT Bombay) Bit Vector Frameworks 26 / 79
A Digression: Jacobi Method
Use values from the current iteration wherever possible
Equations Initial Values Error Margin
w = 0.25x + 0.25y + 8 w0 = 24 wi +1 − wi ≤ 0.35
x = 0.25y + 0.25z + 8 x0 = 24 xi +1 − xi ≤ 0.35
y = 0.25z + 0.25w + 8 y0 = 24 yi +1 − yi ≤ 0.35
z = 0.25w + 0.25x + 8 z0 = 24 zi +1 − zi ≤ 0.35
Iteration 1 Iteration 2
w1 = 6 + 6 + 8 = 20 w2 = 5 + 4.75 + 8 = 17.75
x1 = 6 + 6 + 8 = 20 x2 = 4.75 + 4.5 + 8 = 17.25
y1 = 6 + 5 + 8 = 19 y2 = 4.5 + 4.4375 + 8 = 16.935
z1 = 5 + 5 + 8 = 18 z2 = 4.4375 + 4.375 + 8 = 16.8125

Iteration 3 Iteration 4
w3 = 4.3125 + 4.23375 + 8 = 16.54625 w4 = 16.20172
x3 = 4.23375 + 4.23375 + 8 = 16.436875 x4 = 16.17844
y3 = 4.23375 + 4.1365625 + 8 = 16.370 y4 = 16.13637
z3 = 4.1365625 + 4.11 + 8 = 16.34375 z4 = 16.09504
(IIT Bombay) Bit Vector Frameworks 26 / 79
Our Method of Performing Data Flow Analysis

Round robin iteration using the Jacobi method


(use the values from the current iteration wherever possible)
Unknowns are the data flow variables Ini and Out i
Domain of values is not numbers
Computation in a fixed order
◮ either forward (reverse post order) traversal, or
◮ backward (post order) traversal
over the control flow graph

(IIT Bombay) Bit Vector Frameworks 27 / 79


Tutorial Problem 2 for Liveness Analysis

Draw the control flow graph and perform live variables analysis

int f(int m, int n, int k)


{
int a,i;

for (i=m-1; i<k; i++)


{ if (i>=n)
a = n;
a = a+i;
}
return a;
}

(IIT Bombay) Bit Vector Frameworks 28 / 79


Tutorial Problem 2 for Liveness Analysis

Draw the control flow graph and perform live variables analysis

1 i=m-1
int f(int m, int n, int k)
{
int a,i; 2 if(i<k)
T
for (i=m-1; i<k; i++) F
{ if (i>=n) 3 if (i>=n)
a = n; T
a = a+i; F
4 a=n
}
return a;
} a=a+i
5 i=i+1
6 return a

(IIT Bombay) Bit Vector Frameworks 28 / 79


The Semantics of Return Statement for Live
Variables Analysis

“return a” is modelled by the statement “return value in stack = a”

If we assume that the return statement is executed within the block

If we assume that the return statement is executed outside of the block


and along the edge connecting the procedure to its caller

(IIT Bombay) Bit Vector Frameworks 29 / 79


The Semantics of Return Statement for Live
Variables Analysis

“return a” is modelled by the statement “return value in stack = a”

If we assume that the return statement is executed within the block


⇒ BI can be ∅
If we assume that the return statement is executed outside of the block
and along the edge connecting the procedure to its caller
⇒ a ∈ BI

(IIT Bombay) Bit Vector Frameworks 29 / 79


Solution of Tutorial Problem 2

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Gen Kill Out In Out In
6 {a} ∅
5 {a, i} {a, i}
4 {n} {a}
3 {i, n} ∅
2 {i, k} ∅
1 {m} {i}

(IIT Bombay) Bit Vector Frameworks 30 / 79


Solution of Tutorial Problem 2

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Gen Kill Out In Out In
6 {a} ∅ ∅ {a}
5 {a, i} {a, i} ∅ {a, i}
4 {n} {a} {a, i} {i, n}
3 {i, n} ∅ {a, i, n} {a, i, n}
2 {i, k} ∅ {a, i, n} {a, i, k, n}
1 {m} {i} {a, i, k, n} {a, k, m, n}

(IIT Bombay) Bit Vector Frameworks 30 / 79


Solution of Tutorial Problem 2

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Gen Kill Out In Out In
6 {a} ∅ ∅ {a}
5 {a, i} {a, i} ∅ {a, i} {a, i, k, n} {a, i, k, n}
4 {n} {a} {a, i} {i, n} {a, i, k, n} {i, k, n}
3 {i, n} ∅ {a, i, n} {a, i, n} {a, i, k, n} {a, i, k, n}
2 {i, k} ∅ {a, i, n} {a, i, k, n} {a, i, k, n}
1 {m} {i} {a, i, k, n} {a, k, m, n}

(IIT Bombay) Bit Vector Frameworks 30 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 2
Is a live at the exit of node 5 at the
1 i=m-1 end of iteration 1? Why?
(We have used post order traversal)

2 if(i<k)
T
F
3 if (i>=n)
T
F
4 a=n

a=a+i
5 i=i+1
6 return a

(IIT Bombay) Bit Vector Frameworks 31 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 2
Is a live at the exit of node 5 at the
1 i=m-1 end of iteration 1? Why?
(We have used post order traversal)

2 if(i<k) Is a live at the exit of node 5 at the


end of iteration 2? Why?
T
F (We have used post order traversal)
3 if (i>=n)
T
F
4 a=n

a=a+i
5 i=i+1
6 return a

(IIT Bombay) Bit Vector Frameworks 31 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 2
Is a live at the exit of node 5 at the
1 i=m-1 end of iteration 1? Why?
(We have used post order traversal)

2 if(i<k) Is a live at the exit of node 5 at the


end of iteration 2? Why?
T
F (We have used post order traversal)
3 if (i>=n)
Show an execution path along which a
T is live at the exit of node 5
F
4 a=n

a=a+i
5 i=i+1
6 return a

(IIT Bombay) Bit Vector Frameworks 31 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 2
Is a live at the exit of node 5 at the
1 i=m-1 end of iteration 1? Why?
(We have used post order traversal)

2 if(i<k) Is a live at the exit of node 5 at the


end of iteration 2? Why?
T
F (We have used post order traversal)
3 if (i>=n)
Show an execution path along which a
T is live at the exit of node 5
F
4 a=n Show an execution path along which a
is live at the exit of node 3
a=a+i
5 i=i+1
6 return a

(IIT Bombay) Bit Vector Frameworks 31 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 2
Is a live at the exit of node 5 at the
1 i=m-1 end of iteration 1? Why?
(We have used post order traversal)

2 if(i<k) Is a live at the exit of node 5 at the


end of iteration 2? Why?
T
F (We have used post order traversal)
3 if (i>=n)
Show an execution path along which a
T is live at the exit of node 5
F
4 a=n Show an execution path along which a
is live at the exit of node 3
a=a+i 1 → 2 → 3 → 5 → 2 → ...
5 i=i+1
6 return a

(IIT Bombay) Bit Vector Frameworks 31 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 2
Is a live at the exit of node 5 at the
1 i=m-1 end of iteration 1? Why?
(We have used post order traversal)

2 if(i<k) Is a live at the exit of node 5 at the


end of iteration 2? Why?
T
F (We have used post order traversal)
3 if (i>=n)
Show an execution path along which a
T is live at the exit of node 5
F
4 a=n Show an execution path along which a
is live at the exit of node 3
a=a+i 1 → 2 → 3 → 5 → 2 → ...
5 i=i+1
6 return a Show an execution path along which a
is not live at the exit of node 3

(IIT Bombay) Bit Vector Frameworks 31 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 2
Is a live at the exit of node 5 at the
1 i=m-1 end of iteration 1? Why?
(We have used post order traversal)

2 if(i<k) Is a live at the exit of node 5 at the


end of iteration 2? Why?
T
F (We have used post order traversal)
3 if (i>=n)
Show an execution path along which a
T is live at the exit of node 5
F
4 a=n Show an execution path along which a
is live at the exit of node 3
a=a+i 1 → 2 → 3 → 5 → 2 → ...
5 i=i+1
6 return a Show an execution path along which a
is not live at the exit of node 3
1 → 2 → 3 → 4 → 2 → ...
(IIT Bombay) Bit Vector Frameworks 31 / 79
Tutorial Problem 3 for Liveness Analysis
Also write a C program for this CFG without using goto or break

x =1
n1 y =2 n1

n2 if (c) n2

T
x =y +1
n3 y =2∗z n3
if (d)
T
F F
n4 x = y + z n4

z=1
n5 if (c < 20) n5

F
T
n6 z = x n6
(IIT Bombay) Bit Vector Frameworks 32 / 79
Tutorial Problem 3 for Liveness Analysis
Also write a C program for this CFG without using goto or break

x =1
n1 y =2 n1
void f()
{ int x, y, z;
n2 if (c) n2 int c, d;
x = 1;
T y = 2;
x =y +1 if (c)
n3 y =2∗z n3 { do
if (d)
{ x = y+1;
T y = 2*z;
F F if (d)
n4 x = y + z n4
x = y+z;
z=1 z = 1;
n5 if (c < 20) n5 } while (c < 20);
F
}
T z = x;
n6 z = x n6 }
(IIT Bombay) Bit Vector Frameworks 32 / 79
Solution of Tutorial Problem 3

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Gen Kill Out In Out In
n6 {x} {z}
n5 {c} {z}
n4 {y , z} {x}
n3 {y , z, d} {x, y }

n2 {c} ∅

n1 ∅ {x, y }

(IIT Bombay) Bit Vector Frameworks 33 / 79


Solution of Tutorial Problem 3

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Gen Kill Out In Out In
n6 {x} {z} ∅ {x}
n5 {c} {z} {x} {x, c}
n4 {y , z} {x} {x, c} {y , z, c}
n3 {y , z, d} {x, y } {x, y , {y , z,
z, c} c, d}
{x, y , z, {x, y , z,
n2 {c} ∅
c, d} c, d}
{x, y , z,
n1 ∅ {x, y } c, d} {z, c, d}

(IIT Bombay) Bit Vector Frameworks 33 / 79


Solution of Tutorial Problem 3

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Gen Kill Out In Out In
n6 {x} {z} ∅ {x}
n5 {c} {z} {x} {x, c} {x, y , z, c, d} {x, y , c, d}
n4 {y , z} {x} {x, c} {y , z, c} {x, y , c, d} {y , z, c, d}
n3 {y , z, d} {x, y } {x, y , {y , z, {x, y , z, c, d}
z, c} c, d}
{x, y , z, {x, y , z,
n2 {c} ∅
c, d} c, d}
{x, y , z,
n1 ∅ {x, y } c, d} {z, c, d}

(IIT Bombay) Bit Vector Frameworks 33 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 3
x =1
n1 y =2 n1
Why is z live at the exit of n5 ?
n2 if (c) n2

T
x =y +1
n3 y =2∗z n3
if (d)
T
F F
n4 x = y + z n4

z=1
n5 if (c < 20) n5

T
T
n6 z = x n6

(IIT Bombay) Bit Vector Frameworks 34 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 3
x =1
n1 y =2 n1
Why is z live at the exit of n5 ?
n2 if (c) n2 Why is z not live at the entry of n5 ?
T
x =y +1
n3 y =2∗z n3
if (d)
T
F F
n4 x = y + z n4

z=1
n5 if (c < 20) n5

T
T
n6 z = x n6

(IIT Bombay) Bit Vector Frameworks 34 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 3
x =1
n1 y =2 n1
Why is z live at the exit of n5 ?
n2 if (c) n2 Why is z not live at the entry of n5 ?
T Why is x live at the exit of n3 inspite
x =y +1 of being killed in n4 ?
n3 y =2∗z n3
if (d)
T
F F
n4 x = y + z n4

z=1
n5 if (c < 20) n5

T
T
n6 z = x n6

(IIT Bombay) Bit Vector Frameworks 34 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 3
x =1
n1 y =2 n1
Why is z live at the exit of n5 ?
n2 if (c) n2 Why is z not live at the entry of n5 ?
T Why is x live at the exit of n3 inspite
x =y +1 of being killed in n4 ?
n3 y =2∗z n3
if (d) Identify the instance of dead code
elimination
T
F F
n4 x = y + z n4

z=1
n5 if (c < 20) n5

T
T
n6 z = x n6

(IIT Bombay) Bit Vector Frameworks 34 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 3
x =1
n1 y =2 n1
Why is z live at the exit of n5 ?
n2 if (c) n2 Why is z not live at the entry of n5 ?
T Why is x live at the exit of n3 inspite
x =y +1 of being killed in n4 ?
n3 y =2∗z n3
if (d) Identify the instance of dead code
elimination z = x in n6
T
F F
n4 x = y + z n4

z=1
n5 if (c < 20) n5

T
T
n6 z = x n6

(IIT Bombay) Bit Vector Frameworks 34 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 3
x =1
n1 y =2 n1
Why is z live at the exit of n5 ?
n2 if (c) n2 Why is z not live at the entry of n5 ?
T Why is x live at the exit of n3 inspite
x =y +1 of being killed in n4 ?
n3 y =2∗z n3
if (d) Identify the instance of dead code
elimination z = x in n6
T
F F Would the first round of dead code
n4 x = y + z n4
elimination cause liveness information
z=1
to change?
n5 if (c < 20) n5

T
T
n6 z = x n6

(IIT Bombay) Bit Vector Frameworks 34 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 3
x =1
n1 y =2 n1
Why is z live at the exit of n5 ?
n2 if (c) n2 Why is z not live at the entry of n5 ?
T Why is x live at the exit of n3 inspite
x =y +1 of being killed in n4 ?
n3 y =2∗z n3
if (d) Identify the instance of dead code
elimination z = x in n6
T
F F Would the first round of dead code
n4 x = y + z n4
elimination cause liveness information
z=1
to change? Yes
n5 if (c < 20) n5

T
T
n6 z = x n6

(IIT Bombay) Bit Vector Frameworks 34 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 3
x =1
n1 y =2 n1
Why is z live at the exit of n5 ?
n2 if (c) n2 Why is z not live at the entry of n5 ?
T Why is x live at the exit of n3 inspite
x =y +1 of being killed in n4 ?
n3 y =2∗z n3
if (d) Identify the instance of dead code
elimination z = x in n6
T
F F Would the first round of dead code
n4 x = y + z n4
elimination cause liveness information
z=1
to change? Yes
n5 if (c < 20) n5
Would the second round of liveness
T analysis lead to further dead code
T
elimination?
n6 z = x n6

(IIT Bombay) Bit Vector Frameworks 34 / 79


Interpreting the Result of Liveness Analysis for
Tutorial Problem 3
x =1
n1 y =2 n1
Why is z live at the exit of n5 ?
n2 if (c) n2 Why is z not live at the entry of n5 ?
T Why is x live at the exit of n3 inspite
x =y +1 of being killed in n4 ?
n3 y =2∗z n3
if (d) Identify the instance of dead code
elimination z = x in n6
T
F F Would the first round of dead code
n4 x = y + z n4
elimination cause liveness information
z=1
to change? Yes
n5 if (c < 20) n5
Would the second round of liveness
T analysis lead to further dead code
T
elimination? Yes
n6 z = x n6

(IIT Bombay) Bit Vector Frameworks 34 / 79


Choice of Initialization

What should be the initial value of internal nodes?

The role of boundary info BI explained later in the context of available


expressions analysis

(IIT Bombay) Bit Vector Frameworks 35 / 79


Choice of Initialization

What should be the initial value of internal nodes?


Confluence is ∪
Identity of ∪ is ∅

The role of boundary info BI explained later in the context of available


expressions analysis

(IIT Bombay) Bit Vector Frameworks 35 / 79


Choice of Initialization

What should be the initial value of internal nodes?


Confluence is ∪
Identity of ∪ is ∅
We begin with ∅ and let the sets at each program point grow
A revisit to a program point
◮ may consider a new execution path
◮ more variables may be found to be live
◮ a variable found to be live earlier does not become dead

The role of boundary info BI explained later in the context of available


expressions analysis

(IIT Bombay) Bit Vector Frameworks 35 / 79


How Does the Initialization Affect the Solution?

a=b=5

print b

print b

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Init.


a=b=5


print b


print b

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter.
Init. #1

a=b=5


print b


print b
∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter.
Init. #1

a=b=5


print b

∅ ∅
print b
∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter.
Init. #1

a=b=5


print b
∅ ∅

∅ ∅
print b
∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter.
Init. #1

a=b=5

∅ {b}
print b
∅ ∅

∅ ∅
print b
∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter.
Init. #1

a=b=5
∅ {b}

∅ {b}
print b
∅ ∅

∅ ∅
print b
∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter.
Init. #1
∅ ∅
a=b=5
∅ {b}

∅ {b}
print b
∅ ∅

∅ ∅
print b
∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter. Iter.
Init. #1 #2
∅ ∅ ∅
a=b=5
∅ {b} {b}

∅ {b} {b}
print b
∅ ∅ {b}

∅ ∅ ∅
print b
∅ ∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter. Iter.
Init. #1 #2 Init.

∅ ∅ ∅ {a, b}
a=b=5 a=b=5
∅ {b} {b} {a, b}

∅ {b} {b} {a, b}


print b print b
∅ ∅ {b} {a, b}

∅ ∅ ∅ {a, b}
print b print b
∅ ∅ ∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter. Iter. Iter.


Init. #1 #2 Init. #1
∅ ∅ ∅ {a, b}
a=b=5 a=b=5
∅ {b} {b} {a, b}

∅ {b} {b} {a, b}


print b print b
∅ ∅ {b} {a, b}

∅ ∅ ∅ {a, b}
print b print b
∅ ∅ ∅ ∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter. Iter. Iter.


Init. #1 #2 Init. #1
∅ ∅ ∅ {a, b}
a=b=5 a=b=5
∅ {b} {b} {a, b}

∅ {b} {b} {a, b}


print b print b
∅ ∅ {b} {a, b}

∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter. Iter. Iter.


Init. #1 #2 Init. #1
∅ ∅ ∅ {a, b}
a=b=5 a=b=5
∅ {b} {b} {a, b}

∅ {b} {b} {a, b}


print b print b
∅ ∅ {b} {a, b} {a, b}

∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter. Iter. Iter.


Init. #1 #2 Init. #1
∅ ∅ ∅ {a, b}
a=b=5 a=b=5
∅ {b} {b} {a, b}

∅ {b} {b} {a, b} {a, b}


print b print b
∅ ∅ {b} {a, b} {a, b}

∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter. Iter. Iter.


Init. #1 #2 Init. #1
∅ ∅ ∅ {a, b}
a=b=5 a=b=5
∅ {b} {b} {a, b} {a, b}

∅ {b} {b} {a, b} {a, b}


print b print b
∅ ∅ {b} {a, b} {a, b}

∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter. Iter. Iter.


Init. #1 #2 Init. #1
∅ ∅ ∅ {a, b} ∅
a=b=5 a=b=5
∅ {b} {b} {a, b} {a, b}

∅ {b} {b} {a, b} {a, b}


print b print b
∅ ∅ {b} {a, b} {a, b}

∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅

(IIT Bombay) Bit Vector Frameworks 36 / 79


How Does the Initialization Affect the Solution?

Iter. Iter. Iter.


Init. #1 #2 Init. #1
∅ ∅ ∅ {a, b} ∅
a=b=5 a=b=5
∅ {b} {b} {a, b} {a, b}

∅ {b} {b} {a, b} {a, b}


print b print b
∅ ∅ {b} {a, b} {a, b}

∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅

a is spuriously marked live

(IIT Bombay) Bit Vector Frameworks 36 / 79


Soundness and Precision of Live Variables Analysis
Consider dead code elimination based on liveness information

(IIT Bombay) Bit Vector Frameworks 37 / 79


Soundness and Precision of Live Variables Analysis
Consider dead code elimination based on liveness information

Spurious inclusion of a non-live variable


x = y + 10 i
Out i = {x, y }
print y j

print y End

(IIT Bombay) Bit Vector Frameworks 37 / 79


Soundness and Precision of Live Variables Analysis
Consider dead code elimination based on liveness information

Spurious inclusion of a non-live variable


x = y + 10 i
◮ A dead assignment may not be eliminated
◮ Solution is sound but may be imprecise Out i = {x, y }
print y j

print y End

(IIT Bombay) Bit Vector Frameworks 37 / 79


Soundness and Precision of Live Variables Analysis
Consider dead code elimination based on liveness information

Spurious inclusion of a non-live variable


x = y + 10 i
◮ A dead assignment may not be eliminated
◮ Solution is sound but may be imprecise Out i = {x, y }
print y j
Spurious exclusion of a live variable
print y End

x = z + 10 i
Out i = {y }
print x, y j

print y End

(IIT Bombay) Bit Vector Frameworks 37 / 79


Soundness and Precision of Live Variables Analysis
Consider dead code elimination based on liveness information

Spurious inclusion of a non-live variable


x = y + 10 i
◮ A dead assignment may not be eliminated
◮ Solution is sound but may be imprecise Out i = {x, y }
print y j
Spurious exclusion of a live variable
◮ A useful assignment may be eliminated print y End
◮ Solution is unsound

x = z + 10 i
Out i = {y }
print x, y j

print y End

(IIT Bombay) Bit Vector Frameworks 37 / 79


Soundness and Precision of Live Variables Analysis
Consider dead code elimination based on liveness information

Spurious inclusion of a non-live variable


x = y + 10 i
◮ A dead assignment may not be eliminated
◮ Solution is sound but may be imprecise Out i = {x, y }
print y j
Spurious exclusion of a live variable
◮ A useful assignment may be eliminated print y End
◮ Solution is unsound
Given L2 ⊇ L1 representing liveness information
◮ Using L2 in place of L1 is sound x = z + 10 i
◮ Using L1 in place of L2 may not be sound Out i = {y }
print x, y j

print y End

(IIT Bombay) Bit Vector Frameworks 37 / 79


Soundness and Precision of Live Variables Analysis
Consider dead code elimination based on liveness information

Spurious inclusion of a non-live variable


x = y + 10 i
◮ A dead assignment may not be eliminated
◮ Solution is sound but may be imprecise Out i = {x, y }
print y j
Spurious exclusion of a live variable
◮ A useful assignment may be eliminated print y End
◮ Solution is unsound
Given L2 ⊇ L1 representing liveness information
◮ Using L2 in place of L1 is sound x = z + 10 i
◮ Using L1 in place of L2 may not be sound Out i = {y }
The smallest set of all live variables is most precise print x, y j

◮ Since liveness sets grow (confluence is ∪), we print y End


choose ∅ as the initial conservative value

(IIT Bombay) Bit Vector Frameworks 37 / 79


Termination, Convergence, and Complexity

For live variables analysis,


◮ The set of all variables is finite, and
◮ the confluence operation (i.e. meet) is union, hence
◮ the set associated with a data flow variable can only grow
⇒ Termination is guaranteed

(IIT Bombay) Bit Vector Frameworks 38 / 79


Termination, Convergence, and Complexity

For live variables analysis,


◮ The set of all variables is finite, and
◮ the confluence operation (i.e. meet) is union, hence
◮ the set associated with a data flow variable can only grow
⇒ Termination is guaranteed
Since initial value is ∅, live variables analysis converges on the smallest set

(IIT Bombay) Bit Vector Frameworks 38 / 79


Conservative Nature of Analysis (1)

b1 x=abs(x)

b2 if (x < 0)
T F
b3 x=a+y x=a+z b4

b5 print (x)

(IIT Bombay) Bit Vector Frameworks 39 / 79


Conservative Nature of Analysis (1)

abs(n) returns the absolute value of n

b1 x=abs(x)

b2 if (x < 0)
T F
b3 x=a+y x=a+z b4

b5 print (x)

(IIT Bombay) Bit Vector Frameworks 39 / 79


Conservative Nature of Analysis (1)

abs(n) returns the absolute value of n


Is y live on entry to block b2?
b1 x=abs(x)

b2 if (x < 0)
T F
b3 x=a+y x=a+z b4

b5 print (x)

(IIT Bombay) Bit Vector Frameworks 39 / 79


Conservative Nature of Analysis (1)

abs(n) returns the absolute value of n


Is y live on entry to block b2?
b1 x=abs(x)
By execution semantics, NO
Path b1→b2→b3 is an infeasible
b2 if (x < 0)
execution path
T F
b3 x=a+y x=a+z b4

b5 print (x)

(IIT Bombay) Bit Vector Frameworks 39 / 79


Conservative Nature of Analysis (1)

abs(n) returns the absolute value of n


Is y live on entry to block b2?
b1 x=abs(x)
By execution semantics, NO
Path b1→b2→b3 is an infeasible
b2 if (x < 0)
execution path
T F A compiler makes conservative
b3 x=a+y x=a+z b4 assumptions:
All branch outcomes are possible
⇒ Consider every path in CFG as a po-
b5 print (x)
tential execution path

(IIT Bombay) Bit Vector Frameworks 39 / 79


Conservative Nature of Analysis (1)

abs(n) returns the absolute value of n


Is y live on entry to block b2?
b1 x=abs(x)
By execution semantics, NO
Path b1→b2→b3 is an infeasible
b2 if (x < 0)
execution path
T F A compiler makes conservative
b3 x=a+y x=a+z b4 assumptions:
All branch outcomes are possible
⇒ Consider every path in CFG as a po-
b5 print (x)
tential execution path
Our analysis concludes that y is live on
entry to block b2

(IIT Bombay) Bit Vector Frameworks 39 / 79


Conservative Nature of Analysis (2)

b1 if (x < 0)
T F
b2 a=a+y x=a+z b3

b4 if (x < 0)
T F
b5 x=c+1 x=d+1 b6

b7 print (x)

(IIT Bombay) Bit Vector Frameworks 40 / 79


Conservative Nature of Analysis (2)

Is d live on entry to block b2?

b1 if (x < 0)
T F
b2 a=a+y x=a+z b3

b4 if (x < 0)
T F
b5 x=c+1 x=d+1 b6

b7 print (x)

(IIT Bombay) Bit Vector Frameworks 40 / 79


Conservative Nature of Analysis (2)

Is d live on entry to block b2?


By execution semantics, NO
b1 if (x < 0) Path b1→b2→b4→b6 is an infeasible
execution path
T F
b2 a=a+y x=a+z b3

b4 if (x < 0)
T F
b5 x=c+1 x=d+1 b6

b7 print (x)

(IIT Bombay) Bit Vector Frameworks 40 / 79


Conservative Nature of Analysis (2)

Is d live on entry to block b2?


By execution semantics, NO
b1 if (x < 0) Path b1→b2→b4→b6 is an infeasible
execution path
T F
Is c live on entry to block b3?
b2 a=a+y x=a+z b3 Path b1→b3→b4→b6 is a feasible
execution path
b4 if (x < 0)
T F
b5 x=c+1 x=d+1 b6

b7 print (x)

(IIT Bombay) Bit Vector Frameworks 40 / 79


Conservative Nature of Analysis (2)

Is d live on entry to block b2?


By execution semantics, NO
b1 if (x < 0) Path b1→b2→b4→b6 is an infeasible
execution path
T F
Is c live on entry to block b3?
b2 a=a+y x=a+z b3 Path b1→b3→b4→b6 is a feasible
execution path
b4 if (x < 0) A compiler make conservative assumptions
⇒ our analysis is path insensitive
T F
Note: It is flow sensitive (i.e. information
b5 x=c+1 x=d+1 b6 is computed for every control flow points)

b7 print (x)

(IIT Bombay) Bit Vector Frameworks 40 / 79


Conservative Nature of Analysis (2)

Is d live on entry to block b2?


By execution semantics, NO
b1 if (x < 0) Path b1→b2→b4→b6 is an infeasible
execution path
T F
Is c live on entry to block b3?
b2 a=a+y x=a+z b3 Path b1→b3→b4→b6 is a feasible
execution path
b4 if (x < 0) A compiler make conservative assumptions
⇒ our analysis is path insensitive
T F
Note: It is flow sensitive (i.e. information
b5 x=c+1 x=d+1 b6 is computed for every control flow points)
Our analysis concludes that d is live at the
b7 print (x) entry of b2

(IIT Bombay) Bit Vector Frameworks 40 / 79


Conservative Nature of Analysis (2)

Is d live on entry to block b2?


By execution semantics, NO
b1 if (x < 0) Path b1→b2→b4→b6 is an infeasible
execution path
T F
Is c live on entry to block b3?
b2 a=a+y x=a+z b3 Path b1→b3→b4→b6 is a feasible
execution path
b4 if (x < 0) A compiler make conservative assumptions
⇒ our analysis is path insensitive
T F
Note: It is flow sensitive (i.e. information
b5 x=c+1 x=d+1 b6 is computed for every control flow points)
Our analysis concludes that d is live at the
b7 print (x) entry of b2
Is c live at the entry of b3?

(IIT Bombay) Bit Vector Frameworks 40 / 79


Conservative Nature of Analysis at Intraprocedural
Level

We assume that all paths are potentially executable


Our analysis is path insensitive
◮ The data flow information at a program point p is path insensitive
◦ information at p is merged along all paths reaching p
◮ The data flow information reaching p is computed path insensitively
◦ information is merged at all shared points in paths reaching p
◦ may generate spurious information due to non-distributive flow
functions

(IIT Bombay) Bit Vector Frameworks 41 / 79


Conservative Nature of Analysis at Interprocedural
Level

Context insensitivity
◮ Merges of information across all calling contexts
Flow insensitivity
◮ Disregards the control flow

(IIT Bombay) Bit Vector Frameworks 42 / 79


What About Soundness of Analysis Results?

No compromises

(IIT Bombay) Bit Vector Frameworks 43 / 79


Part IV

Available Expressions Analysis


Defining Available Expressions Analysis

An expression e is available at a program point p, if


every path from program entry to p contains an evaluation of e
which is not followed by a definition of any operand of e.

Start Start Start


a∗b a∗b a∗b

a∗b a∗b

a∗b a= a∗b a∗b

p p p

End End End

(IIT Bombay) Bit Vector Frameworks 45 / 79


Defining Available Expressions Analysis

An expression e is available at a program point p, if


every path from program entry to p contains an evaluation of e
which is not followed by a definition of any operand of e.

a ∗ b is
available at p
Start Start Start
a∗b a∗b a∗b

a∗b a∗b

a∗b a= a∗b a∗b

p p p

End End End

(IIT Bombay) Bit Vector Frameworks 45 / 79


Defining Available Expressions Analysis

An expression e is available at a program point p, if


every path from program entry to p contains an evaluation of e
which is not followed by a definition of any operand of e.

a ∗ b is a ∗ b is not
available at p available at p
Start Start Start
a∗b a∗b a∗b

a∗b a∗b

a∗b a= a∗b a∗b

p p p

End End End

(IIT Bombay) Bit Vector Frameworks 45 / 79


Defining Available Expressions Analysis

An expression e is available at a program point p, if


every path from program entry to p contains an evaluation of e
which is not followed by a definition of any operand of e.

a ∗ b is a ∗ b is not a ∗ b is not
available at p available at p available at p
Start Start Start
a∗b a∗b a∗b

a∗b a∗b

a∗b a= a∗b a∗b

p p p

End End End

(IIT Bombay) Bit Vector Frameworks 45 / 79


Local Data Flow Properties for Available
Expressions Analysis

Genn = { e | expression e is evaluated in basic block n and


this evaluation is not followed by a definition of
any operand of e}

Kill n = { e | basic block n contains a definition of an operand of e}

Entity Manipulation Exposition


Genn Expression Use Downwards
Kill n Expression Modification Anywhere

(IIT Bombay) Bit Vector Frameworks 46 / 79


Data Flow Equations For Available Expressions
Analysis


 \ BI n is Start block
Inn = Out p otherwise

p∈pred(n)

Out n = Genn ∪ (Inn − Kill n )

(IIT Bombay) Bit Vector Frameworks 47 / 79


Data Flow Equations For Available Expressions
Analysis


 \ BI n is Start block
Inn = Out p otherwise

p∈pred(n)

Out n = Genn ∪ (Inn − Kill n )

Alternatively,
Out n = fn (Inn ), where

fn (X ) = Genn ∪ (X − Kill n )

(IIT Bombay) Bit Vector Frameworks 47 / 79


Data Flow Equations For Available Expressions
Analysis


 \ BI n is Start block
Inn = Out p otherwise

p∈pred(n)

Out n = Genn ∪ (Inn − Kill n )

Alternatively,
Out n = fn (Inn ), where

fn (X ) = Genn ∪ (X − Kill n )

Inn and Out n are sets of expressions

(IIT Bombay) Bit Vector Frameworks 47 / 79


Data Flow Equations For Available Expressions
Analysis


 \ BI n is Start block
Inn = Out p otherwise

p∈pred(n)

Out n = Genn ∪ (Inn − Kill n )

Alternatively,
Out n = fn (Inn ), where

fn (X ) = Genn ∪ (X − Kill n )

Inn and Out n are sets of expressions


BI is ∅ for expressions involving a local variable

(IIT Bombay) Bit Vector Frameworks 47 / 79


Using Data Flow Information of Available
Expressions Analysis

Common subexpression elimination

(IIT Bombay) Bit Vector Frameworks 48 / 79


Using Data Flow Information of Available
Expressions Analysis

Common subexpression elimination


◮ If an expression is available at the entry of a block n (Inn ) and

(IIT Bombay) Bit Vector Frameworks 48 / 79


Using Data Flow Information of Available
Expressions Analysis

Common subexpression elimination


◮ If an expression is available at the entry of a block n (Inn ) and
◮ a computation of the expression exists in n such that

(IIT Bombay) Bit Vector Frameworks 48 / 79


Using Data Flow Information of Available
Expressions Analysis

Common subexpression elimination


◮ If an expression is available at the entry of a block n (Inn ) and
◮ a computation of the expression exists in n such that
◮ it is not preceded by a definition of any of its operands (AntGenn )

(IIT Bombay) Bit Vector Frameworks 48 / 79


Using Data Flow Information of Available
Expressions Analysis

Common subexpression elimination


◮ If an expression is available at the entry of a block n (Inn ) and
◮ a computation of the expression exists in n such that
◮ it is not preceded by a definition of any of its operands (AntGenn )
Then the expression is redundant

Redundantn = Inn ∩ AntGenn

(IIT Bombay) Bit Vector Frameworks 48 / 79


Using Data Flow Information of Available
Expressions Analysis

Common subexpression elimination


◮ If an expression is available at the entry of a block n (Inn ) and
◮ a computation of the expression exists in n such that
◮ it is not preceded by a definition of any of its operands (AntGenn )
Then the expression is redundant

Redundantn = Inn ∩ AntGenn

A redundant expression is upwards exposed whereas the expressions in


Genn are downwards exposed

(IIT Bombay) Bit Vector Frameworks 48 / 79


An Example of Available Expressions Analysis

1 a∗b 1
b∗c
Let e1 ≡ a ∗ b, e2 ≡ b ∗ c, e3 ≡ c ∗ d, e4 ≡ d ∗ e

2 c ∗d 2

Node
Gen Kill Available Redund.

3 c =2 3 4 d =3 4 1 {e1 , e2 } 1100 ∅ 0000 ∅ 0000 ∅ 0000


2 {e3 } 0010 ∅ 0000 {e1 } 1000 ∅ 0000
3 ∅ 0000 {e2 , e3 } 0110 {e1 , e3 } 1010 ∅ 0000
d ∗e 4 ∅ 0000 {e3 , e4 } 0011 {e1 , e3 } 1010 ∅ 0000
5 a∗b 5 5 {e1 , e4 } 1001 ∅ 0000 {e1 } 1000 {e1 } 1000
6 {e4 } 0001 ∅ 0000 {e1 , e4 } 1001 {e4 } 0001

6 d ∗e 6

(IIT Bombay) Bit Vector Frameworks 49 / 79


An Example of Available Expressions Analysis

Initialisation
0000
1 a∗b 1
b∗c
1111
Let e1 ≡ a ∗ b, e2 ≡ b ∗ c, e3 ≡ c ∗ d, e4 ≡ d ∗ e
1111
2 c ∗d 2

Node
1111 Gen Kill Available Redund.
1111 1111
3 c =2 3 4 d =3 4 1 {e1 , e2 } 1100 ∅ 0000 ∅ 0000 ∅ 0000
1111 2 {e3 } 0010 ∅ 0000 {e1 } 1000 ∅ 0000
1111 3 ∅ 0000 {e2 , e3 } 0110 {e1 , e3 } 1010 ∅ 0000
1111 4 ∅ 0000 {e3 , e4 } 0011 {e1 , e3 } 1010 ∅ 0000
d ∗e
5 a∗b 5 5 {e1 , e4 } 1001 ∅ 0000 {e1 } 1000 {e1 } 1000
1111 6 {e4 } 0001 ∅ 0000 {e1 , e4 } 1001 {e4 } 0001
1111
6 d ∗e 6
1111

(IIT Bombay) Bit Vector Frameworks 49 / 79


An Example of Available Expressions Analysis

Iteration #1

0000
1 a∗b 1
b∗c
1100
Let e1 ≡ a ∗ b, e2 ≡ b ∗ c, e3 ≡ c ∗ d, e4 ≡ d ∗ e
1100
2 c ∗d 2

Node
1110 Gen Kill Available Redund.
1110 1110
3 c =2 3 4 d =3 4 1 {e1 , e2 } 1100 ∅ 0000 ∅ 0000 ∅ 0000
1000 2 {e3 } 0010 ∅ 0000 {e1 } 1000 ∅ 0000
1100 3 ∅ 0000 {e2 , e3 } 0110 {e1 , e3 } 1010 ∅ 0000
1000 4 ∅ 0000 {e3 , e4 } 0011 {e1 , e3 } 1010 ∅ 0000
d ∗e
5 a∗b 5 5 {e1 , e4 } 1001 ∅ 0000 {e1 } 1000 {e1 } 1000
1001 6 {e4 } 0001 ∅ 0000 {e1 , e4 } 1001 {e4 } 0001
1001
6 d ∗e 6
1001

(IIT Bombay) Bit Vector Frameworks 49 / 79


An Example of Available Expressions Analysis

Iteration #2

0000
1 a∗b 1
b∗c
1100
Let e1 ≡ a ∗ b, e2 ≡ b ∗ c, e3 ≡ c ∗ d, e4 ≡ d ∗ e
1000
2 c ∗d 2

Node
1010 Gen Kill Available Redund.
1010 1010
3 c =2 3 4 d =3 4 1 {e1 , e2 } 1100 ∅ 0000 ∅ 0000 ∅ 0000
1000 2 {e3 } 0010 ∅ 0000 {e1 } 1000 ∅ 0000
1000 3 ∅ 0000 {e2 , e3 } 0110 {e1 , e3 } 1010 ∅ 0000
1000 4 ∅ 0000 {e3 , e4 } 0011 {e1 , e3 } 1010 ∅ 0000
d ∗e
5 a∗b 5 5 {e1 , e4 } 1001 ∅ 0000 {e1 } 1000 {e1 } 1000
1001 6 {e4 } 0001 ∅ 0000 {e1 , e4 } 1001 {e4 } 0001
1001
6 d ∗e 6
1001

(IIT Bombay) Bit Vector Frameworks 49 / 79


An Example of Available Expressions Analysis

Final Result
0000
1 a∗b 1
b∗c
1100
Let e1 ≡ a ∗ b, e2 ≡ b ∗ c, e3 ≡ c ∗ d, e4 ≡ d ∗ e
1000
2 c ∗d 2

Node
1010 Gen Kill Available Redund.
1010 1010
3 c =2 3 4 d =3 4 1 {e1 , e2 } 1100 ∅ 0000 ∅ 0000 ∅ 0000
1000 2 {e3 } 0010 ∅ 0000 {e1 } 1000 ∅ 0000
1000 3 ∅ 0000 {e2 , e3 } 0110 {e1 , e3 } 1010 ∅ 0000
1000 4 ∅ 0000 {e3 , e4 } 0011 {e1 , e3 } 1010 ∅ 0000
d ∗e
5 a∗b 5 5 {e1 , e4 } 1001 ∅ 0000 {e1 } 1000 {e1 } 1000
1001 6 {e4 } 0001 ∅ 0000 {e1 , e4 } 1001 {e4 } 0001
1001
6 d ∗e 6
1001

(IIT Bombay) Bit Vector Frameworks 49 / 79


Tutorial Problem 2 for Available Expressions Analysis

d =a∗b
n1 e =b+c n1

n2 if (c) n2

c =a∗b
n3 a = b + c n3 n4 a = 10 n4

n5 if (d) n5

n6 print a, b, c, d n6
Expr = { a ∗ b, b + c }

(IIT Bombay) Bit Vector Frameworks 50 / 79


Solution of the Tutorial Problem 2

Bit vector a ∗ b b+c

Global Information
Local Information
Node

Iteration # 1 Changes in
iteration # 2 Redundant n
Genn Kill n AntGenn Inn Out n Inn Out n
n1 11 00 11
n2 00 00 00
n3 01 10 01
n4 00 11 10
n5 00 00 00
n6 00 00 00

(IIT Bombay) Bit Vector Frameworks 51 / 79


Solution of the Tutorial Problem 2

Bit vector a ∗ b b+c

Global Information
Local Information
Node

Iteration # 1 Changes in
iteration # 2 Redundant n
Genn Kill n AntGenn Inn Out n Inn Out n
n1 11 00 11 00 11
n2 00 00 00 11 11
n3 01 10 01 11 01
n4 00 11 10 11 00
n5 00 00 00 00 00
n6 00 00 00 00 00

(IIT Bombay) Bit Vector Frameworks 51 / 79


Solution of the Tutorial Problem 2

Bit vector a ∗ b b+c

Global Information
Local Information
Node

Iteration # 1 Changes in
iteration # 2 Redundant n
Genn Kill n AntGenn Inn Out n Inn Out n
n1 11 00 11 00 11
n2 00 00 00 11 11 00 00
n3 01 10 01 11 01 00
n4 00 11 10 11 00 00
n5 00 00 00 00 00
n6 00 00 00 00 00

(IIT Bombay) Bit Vector Frameworks 51 / 79


Solution of the Tutorial Problem 2

Bit vector a ∗ b b+c

Global Information
Local Information
Node

Iteration # 1 Changes in
iteration # 2 Redundant n
Genn Kill n AntGenn Inn Out n Inn Out n
n1 11 00 11 00 11 00
n2 00 00 00 11 11 00 00 00
n3 01 10 01 11 01 00 00
n4 00 11 10 11 00 00 00
n5 00 00 00 00 00 00
n6 00 00 00 00 00 00

(IIT Bombay) Bit Vector Frameworks 51 / 79


Tutorial Problem 3 for Available Expressions Analysis

c =a∗b
n1 d =b+c n1

n2 d =a+b n2

n3 d = b + c n3

a=5
n4 d =a+b n4 c = 10 n5

d =a+b
n6 print a, b, c, d n6

Expr = { a ∗ b, b + c, a + b }

(IIT Bombay) Bit Vector Frameworks 52 / 79


Solution of the Tutorial Problem 3

Bit vector a ∗ b b+c a+b

Global Information
Local Information
Node

Iteration # 1 Changes in Changes in


Iteration # 2 Iteration # 3 Redundant n
Genn Kill n AntGenn Inn Out n Inn Out n Inn Out n
n1 110 010 100
n2 001 000 001
n3 010 000 010
n4 001 101 000
n5 000 010 000
n6 001 000 001

(IIT Bombay) Bit Vector Frameworks 53 / 79


Solution of the Tutorial Problem 3

Bit vector a ∗ b b+c a+b

Global Information
Local Information
Node

Iteration # 1 Changes in Changes in


Iteration # 2 Iteration # 3 Redundant n
Genn Kill n AntGenn Inn Out n Inn Out n Inn Out n
n1 110 010 100 000 110
n2 001 000 001 110 111
n3 010 000 010 111 111
n4 001 101 000 111 011
n5 000 010 000 111 101
n6 001 000 001 101 101

(IIT Bombay) Bit Vector Frameworks 53 / 79


Solution of the Tutorial Problem 3

Bit vector a ∗ b b+c a+b

Global Information
Local Information
Node

Iteration # 1 Changes in Changes in


Iteration # 2 Iteration # 3 Redundant n
Genn Kill n AntGenn Inn Out n Inn Out n Inn Out n
n1 110 010 100 000 110
n2 001 000 001 110 111 100 101
n3 010 000 010 111 111 001 011
n4 001 101 000 111 011 011
n5 000 010 000 111 101 001 001
n6 001 000 001 101 101 001 001

(IIT Bombay) Bit Vector Frameworks 53 / 79


Solution of the Tutorial Problem 3

Bit vector a ∗ b b+c a+b

Global Information
Local Information
Node

Iteration # 1 Changes in Changes in


Iteration # 2 Iteration # 3 Redundant n
Genn Kill n AntGenn Inn Out n Inn Out n Inn Out n
n1 110 010 100 000 110
n2 001 000 001 110 111 100 101 000 001
n3 010 000 010 111 111 001 011
n4 001 101 000 111 011 011
n5 000 010 000 111 101 001 001
n6 001 000 001 101 101 001 001

(IIT Bombay) Bit Vector Frameworks 53 / 79


Solution of the Tutorial Problem 3

Bit vector a ∗ b b+c a+b

Global Information
Local Information
Node

Iteration # 1 Changes in Changes in


Iteration # 2 Iteration # 3 Redundant n
Genn Kill n AntGenn Inn Out n Inn Out n Inn Out n
n1 110 010 100 000 110 000
n2 001 000 001 110 111 100 101 000 001 000
n3 010 000 010 111 111 001 011 000
n4 001 101 000 111 011 011 000
n5 000 010 000 111 101 001 001 000
n6 001 000 001 101 101 001 001 001

(IIT Bombay) Bit Vector Frameworks 53 / 79


Solution of the Tutorial Problem 3

Bit vector a ∗ b b+c a+b

Global Information
Local Information
Node

Iteration # 1 Changes in Changes in


Iteration # 2 Iteration # 3 Redundant n
Genn Kill n AntGenn Inn Out n Inn Out n Inn Out n
n1 110 010 100 000 110 000
n2 001 000 001 110 111 100 101 000 001 000
n3 010 000 010 111 111 001 011 000
n4 001 101 000 111 011 011 000
n5 000 010 000 111 101 001 001 000
n6 001 000 001 101 101 001 001 001

Why do we need 3 iterations as against 2 for previous problems?

(IIT Bombay) Bit Vector Frameworks 53 / 79


Soundness and Precision of Available Expressions
Analysis
Consider common subexpression elimination based on availability information

(IIT Bombay) Bit Vector Frameworks 54 / 79


Soundness and Precision of Available Expressions
Analysis
Consider common subexpression elimination based on availability information

Spurious inclusion of a non-available expression a ∗ b


t =b∗c i

Out i = {a ∗ b,
b ∗ c}
print a ∗ b j

(IIT Bombay) Bit Vector Frameworks 54 / 79


Soundness and Precision of Available Expressions
Analysis
Consider common subexpression elimination based on availability information

Spurious inclusion of a non-available expression a ∗ b


◮ An occurrence of a ∗ b may be eliminated t =b∗c i
◮ Solution is unsound Out i = {a ∗ b,
b ∗ c}
t✘
a ∗❳
print ❳
✘ ✘
❳b j

(IIT Bombay) Bit Vector Frameworks 54 / 79


Soundness and Precision of Available Expressions
Analysis
Consider common subexpression elimination based on availability information

Spurious inclusion of a non-available expression a ∗ b


◮ An occurrence of a ∗ b may be eliminated t =b∗c i
◮ Solution is unsound Out i = {a ∗ b,
b ∗ c}
Spurious exclusion of an available expression t✘
a ∗❳
print ❳
✘ ✘
❳b j

t =a∗b i
Out i = ∅

print a ∗ b j

(IIT Bombay) Bit Vector Frameworks 54 / 79


Soundness and Precision of Available Expressions
Analysis
Consider common subexpression elimination based on availability information

Spurious inclusion of a non-available expression a ∗ b


◮ An occurrence of a ∗ b may be eliminated t =b∗c i
◮ Solution is unsound Out i = {a ∗ b,
b ∗ c}
Spurious exclusion of an available expression t✘
a ∗❳
print ❳
✘ ✘
❳b j
◮ An occurrence of a ∗ b may not be eliminated
◮ Solution is sound but may be imprecise

t =a∗b i
Out i = ∅

print a ∗ b j

(IIT Bombay) Bit Vector Frameworks 54 / 79


Soundness and Precision of Available Expressions
Analysis
Consider common subexpression elimination based on availability information

Spurious inclusion of a non-available expression a ∗ b


◮ An occurrence of a ∗ b may be eliminated t =b∗c i
◮ Solution is unsound Out i = {a ∗ b,
b ∗ c}
Spurious exclusion of an available expression t✘
a ∗❳
print ❳
✘ ✘
❳b j
◮ An occurrence of a ∗ b may not be eliminated
◮ Solution is sound but may be imprecise
Given A2 ⊇ A1 representing availability information
◮ Using A1 in place of A2 is sound t =a∗b i
◮ Using A2 in place of A1 may not be sound Out i = ∅

print a ∗ b j

(IIT Bombay) Bit Vector Frameworks 54 / 79


Soundness and Precision of Available Expressions
Analysis
Consider common subexpression elimination based on availability information

Spurious inclusion of a non-available expression a ∗ b


◮ An occurrence of a ∗ b may be eliminated t =b∗c i
◮ Solution is unsound Out i = {a ∗ b,
b ∗ c}
Spurious exclusion of an available expression t✘
a ∗❳
print ❳
✘ ✘
❳b j
◮ An occurrence of a ∗ b may not be eliminated
◮ Solution is sound but may be imprecise
Given A2 ⊇ A1 representing availability information
◮ Using A1 in place of A2 is sound t =a∗b i
◮ Using A2 in place of A1 may not be sound Out i = ∅
The largest set of available expressions is most precise print a ∗ b j
◮ Since availability sets shrink (confluence is ∩),
we choose U as the initial conservative value
(IIT Bombay) Bit Vector Frameworks 54 / 79
The Effect of BI and Initialization on a Solution

1 w =a+c 1

2 x =a∗c 2

=a+c 3
3 zy = a∗c

(IIT Bombay) Bit Vector Frameworks 55 / 79


The Effect of BI and Initialization on a Solution

Bit Vector
a+c a∗c

Initialization U Initialization ∅
1 w =a+c 1 BI Node
Inn Out n Inn Out n
1
2 x =a∗c 2 ∅ 2
3
1
=a+c 3
3 zy = a∗c U 2
3

(IIT Bombay) Bit Vector Frameworks 55 / 79


The Effect of BI and Initialization on a Solution

Bit Vector
a+c a∗c

Initialization U Initialization ∅
1 w =a+c 1 BI Node
Inn Out n Inn Out n
1 00 10
2 x =a∗c 2 ∅ 2 10 11
3 10 11
1
=a+c 3
3 zy = a∗c U 2
3

(IIT Bombay) Bit Vector Frameworks 55 / 79


The Effect of BI and Initialization on a Solution

Bit Vector
a+c a∗c

Initialization U Initialization ∅
1 w =a+c 1 BI Node
Inn Out n Inn Out n
1 00 10 00 10
2 x =a∗c 2 ∅ 2 10 11 00 01
3 10 11 00 11
1
=a+c 3
3 zy = a∗c U 2
3

(IIT Bombay) Bit Vector Frameworks 55 / 79


The Effect of BI and Initialization on a Solution

Bit Vector
a+c a∗c

Initialization U Initialization ∅
1 w =a+c 1 BI Node
Inn Out n Inn Out n
1 00 10 00 10
2 x =a∗c 2 ∅ 2 10 11 00 01
3 10 11 00 11
1 11 11
=a+c 3
3 zy = a∗c U 2 11 11
3 11 11

(IIT Bombay) Bit Vector Frameworks 55 / 79


The Effect of BI and Initialization on a Solution

Bit Vector
a+c a∗c

Initialization U Initialization ∅
1 w =a+c 1 BI Node
Inn Out n Inn Out n
1 00 10 00 10
2 x =a∗c 2 ∅ 2 10 11 00 01
3 10 11 00 11
1 11 11 11 11
=a+c 3
3 zy = a∗c U 2 11 11 00 01
3 11 11 01 11

(IIT Bombay) Bit Vector Frameworks 55 / 79


The Effect of BI and Initialization on a Solution

Bit Vector
a+c a∗c

Initialization U Initialization ∅
1 w =a+c 1 BI Node
Inn Out n Inn Out n
1 00 10 00 10
2 x =a∗c 2 ∅ 2 10 11 00 01
3 10 11 00 11
1 11 11 11 11
=a+c 3
3 zy = a∗c U 2 11 11 00 01
3 11 11 01 11
This represents the expected
availability information leading to
elimination of a + c in node 3
(a ∗ c is not redundant in node 3)
(IIT Bombay) Bit Vector Frameworks 55 / 79
The Effect of BI and Initialization on a Solution

Bit Vector
a+c a∗c

Initialization U Initialization ∅
1 w =a+c 1 BI Node
Inn Out n Inn Out n
1 00 10 00 10
2 x =a∗c 2 ∅ 2 10 11 00 01
3 10 11 00 11
1 11 11 11 11
=a+c 3
3 zy = a∗c U 2 11 11 00 01
3 11 11 01 11
This misses the
availability of a + c
in node 3
(IIT Bombay) Bit Vector Frameworks 55 / 79
The Effect of BI and Initialization on a Solution

This makes a ∗ c available


in node 3 although its Bit Vector
computation in node 3 is a+c a∗c
not redundant

Initialization U Initialization ∅
1 w =a+c 1 BI Node
Inn Out n Inn Out n
1 00 10 00 10
2 x =a∗c 2 ∅ 2 10 11 00 01
3 10 11 00 11
1 11 11 11 11
=a+c 3
3 zy = a∗c U 2 11 11 00 01
3 11 11 01 11

(IIT Bombay) Bit Vector Frameworks 55 / 79


The Effect of BI and Initialization on a Solution

This make a ∗ c available


in node 3 and but misses Bit Vector
the availability of a + c in a+c a∗c
node 3

Initialization U Initialization ∅
1 w =a+c 1 BI Node
Inn Out n Inn Out n
1 00 10 00 10
2 x =a∗c 2 ∅ 2 10 11 00 01
3 10 11 00 11
1 11 11 11 11
=a+c 3
3 zy = a∗c U 2 11 11 00 01
3 11 11 01 11

(IIT Bombay) Bit Vector Frameworks 55 / 79


The Effect of BI and Initialization on a Solution

Bit Vector
a+c a∗c

Initialization U Initialization ∅
1 w =a+c 1 BI Node
Inn Out n Inn Out n
1 00 10 00 10
∅ Sound & Sound &01
2 x =a∗c 2 2 10 11 00
Precise Imprecise
3 10 11 00 11
1 11 11 11 11
=a+c 3
3 zy = a∗c U 2 11
Unsound11 00
Unsound01
3 11 11 01 11

(IIT Bombay) Bit Vector Frameworks 55 / 79


Some Observations

Data flow equations do not require a particular order of computation


◮ Specification. Data flow equations define what needs to be computed
and not how it is to be computed
◮ Implementation. Round robin iterations perform the actual
computation
◮ Specification and implementation are distinct
Initialization governs the quality of solution found
◮ Only precision is affected, soundness is guaranteed
◮ Associated with “internal” nodes
BI depends on the semantics of the calling context
◮ May cause unsoundness
◮ Associated with “boundary” node (specified by data flow equations)
Does not vary with the method or order of traversal

(IIT Bombay) Bit Vector Frameworks 56 / 79


Part V

Reaching Definitions Analysis


Defining Reaching Definitions Analysis

A definition dx : x = e reaches a program point p if it appears (without a


redefinition of x) on some path from program entry to p
(x is a variable and e is an expression)

Application : Copy Propagation


A use of a variable x at a program point p can be replaced by y if
dx : x = y is the only definition which reaches p and y is not modified
between the point of dx and p.

(IIT Bombay) Bit Vector Frameworks 58 / 79


Using Reaching Definitions for Def-Use and Use-Def Chains

Def-Use Chains

a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = c∗2

2 if (a≤m)
F T
3 a2 : a = a+1

4 if (a<12)
F T
t11 : t1 = a+b
5 a3 : a = t1+c

6 print a

(IIT Bombay) Bit Vector Frameworks 59 / 79


Using Reaching Definitions for Def-Use and Use-Def Chains

Def-Use Chains

a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = c∗2

2 if (a≤m)
F T
3 a2 : a = a+1

4 if (a<12)
F T
t11 : t1 = a+b
5 a3 : a = t1+c

6 print a

(IIT Bombay) Bit Vector Frameworks 59 / 79


Using Reaching Definitions for Def-Use and Use-Def Chains

Def-Use Chains

a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = c∗2

2 if (a≤m)
F T
3 a2 : a = a+1

4 if (a<12)
F T
t11 : t1 = a+b
5 a3 : a = t1+c

6 print a

(IIT Bombay) Bit Vector Frameworks 59 / 79


Using Reaching Definitions for Def-Use and Use-Def Chains

Def-Use Chains Use-Def Chains

a1 : a = 4 a1 : a = 4
b : b=2 b : b=2
1 c11: c = 3 1 c11: c = 3
m1 : m = c∗2 m1 : m = c∗2

2 if (a≤m) 2 if (a≤m)
F T F T
3 a2 : a = a+1 3 a2 : a = a+1

4 if (a<12) 4 if (a<12)
F T F T
t11 : t1 = a+b t11 : t1 = a+b
5 a3 : a = t1+c 5 a3 : a = t1+c

6 print a 6 print a

(IIT Bombay) Bit Vector Frameworks 59 / 79


Using Reaching Definitions for Def-Use and Use-Def Chains

Def-Use Chains Use-Def Chains

a1 : a = 4 a1 : a = 4
b : b=2 b : b=2
1 c11: c = 3 1 c11: c = 3
m1 : m = c∗2 m1 : m = c∗2

2 if (a≤m) 2 if (a≤m)
F T F T
3 a2 : a = a+1 3 a2 : a = a+1

4 if (a<12) 4 if (a<12)
F T F T
t11 : t1 = a+b t11 : t1 = a+b
5 a3 : a = t1+c 5 a3 : a = t1+c

6 print a 6 print a

(IIT Bombay) Bit Vector Frameworks 59 / 79


Using Reaching Definitions for Def-Use and Use-Def Chains

Def-Use Chains Use-Def Chains

a1 : a = 4 a1 : a = 4
b : b=2 b : b=2
1 c11: c = 3 1 c11: c = 3
m1 : m = c∗2 m1 : m = c∗2
There is a need to distinguish
2 if (a≤m) between different occurrences 2 if (a≤m)
T of lexically identical definitions T
F F
3 a2 : a = a+1 3 a2 : a = a+1
Hence a definition is identified
by the label of the statement
4 if (a<12) 4 if (a<12)
F T F T
t11 : t1 = a+b t11 : t1 = a+b
5 a3 : a = t1+c 5 a3 : a = t1+c

6 print a 6 print a

(IIT Bombay) Bit Vector Frameworks 59 / 79


Defining Data Flow Analysis for Reaching
Definitions Analysis

Let dv be a definition of variable v

Genn = { dv | variable v is defined in basic block n and


this definition is not followed (within n)
by a definition of v }

Kill n = { dv | basic block n contains a definition of v }

Entity Manipulation Exposition


Genn Definition Occurrence Downwards
Kill n Definition Occurrence Anywhere

(IIT Bombay) Bit Vector Frameworks 60 / 79


Data Flow Equations for Reaching Definitions
Analysis


 [ BI n is Start block
Inn = Out p otherwise

p∈pred(n)

Out n = Genn ∪ (Inn − Kill n )


BI = {dx : x = undef | x ∈ Var}

Inn and Out n are sets of definitions

(IIT Bombay) Bit Vector Frameworks 61 / 79


The Role of Boundary Information for Reaching
Definitions Analysis

1 x = 5+10

2 x1: x = 5 3 x =5

4 y = x +10

Only one definition of x (x1 ) reaches node 4


Can we perform copy propagation in node 4 by replacing x by 5?
Boundary information x0: x = undef prohibits it for soundness

(IIT Bombay) Bit Vector Frameworks 62 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = c∗2

2 if (a≤m)
F T
3 a2 : a = a+1

4 if (a<12)
T
F
t1 : t1 = a+b
5 a3 :1 a = t1+c

6 print a

(IIT Bombay) Bit Vector Frameworks 63 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = c∗2

2 if (a≤m)
F T Local copy
3 a2 : a = a+1 propagation and
constant folding
4 if (a<12)
T
F
t1 : t1 = a+b
5 a3 :1 a = t1+c

6 print a

(IIT Bombay) Bit Vector Frameworks 63 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4 a1 : a = 4
b : b=2 b : b=2
1 c11: c = 3 1 c11: c = 3
m1 : m = c∗2 m1 : m = 6

2 if (a≤m) 2 if (a≤m)
F T Local copy F T
3 a2 : a = a+1 propagation and 3 a2 : a = a+1
constant folding
4 if (a<12) 4 if (a<12)
T T
F F
t1 : t1 = a+b t1 : t1 = a+b
5 a3 :1 a = t1+c 5 a3 :1 a = t1+c

6 print a 6 print a

(IIT Bombay) Bit Vector Frameworks 63 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2 Gen Kill
1 c11: c = 3 {a0 , a1 , a2 , a3 , b0 ,
m1 : m = 6 1 {a1 , b1 , c1 , m1 }
b 1 , c 0 , c 1 , m0 , m1 }
2 ∅ ∅
3 {a2 } {a0 , a1 , a2 , a3 }
2 if (a≤m) 4 ∅ ∅
T 5 {a3 } {a0 , a1 , a2 , a3 }
F 6 ∅ ∅
3 a2 : a = a+1

4 if (a<12)
F T
t1 : t1 = a+b
5 a3 :1 a = t1+c

6 print a

(IIT Bombay) Bit Vector Frameworks 64 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2 Gen Kill
1 c11: c = 3 {a0 , a1 , a2 , a3 , b0 ,
m1 : m = 6 1 {a1 , b1 , c1 , m1 }
b 1 , c 0 , c 1 , m0 , m1 }
2 ∅ ∅
3 {a2 } {a0 , a1 , a2 , a3 }
2 if (a≤m) 4 ∅ ∅
T 5 {a3 } {a0 , a1 , a2 , a3 }
F 6 ∅ ∅
3 a2 : a = a+1

Temporary variable t1 is ignored


4 if (a<12) For variable v , v0 denotes the
T definition v = undef
F
t1 : t1 = a+b This is used for defining BI
5 a3 :1 a = t1+c

6 print a

(IIT Bombay) Bit Vector Frameworks 64 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2 Gen Kill
1 c11: c = 3 {a0 , a1 , a2 , a3 , b0 ,
m1 : m = 6 1 {a1 , b1 , c1 , m1 }
b 1 , c 0 , c 1 , m0 , m1 }
2 ∅ ∅
3 {a2 } {a0 , a1 , a2 , a3 }
2 if (a≤m) 4 ∅ ∅
T 5 {a3 } {a0 , a1 , a2 , a3 }
F 6 ∅ ∅
3 a2 : a = a+1
Iteration #1
In Out
4 if (a<12) 1 {a0 , b0 , c0 , m0 } {a1 , b1 , c1 , m1 }
T 2 {a1 , b1 , c1 , m1 } {a1 , b1 , c1 , m1 }
F 3 {a1 , b1 , c1 , m1 } {a2 , b1 , c1 , m1 }
t1 : t1 = a+b
5 a3 :1 a = t1+c 4 {a1 , b1 , c1 , m1 } {a1 , b1 , c1 , m1 }
5 {a1 , b1 , c1 , m1 } {a3 , b1 , c1 , m1 }
6 {a1 , a3 , b1 , c1 , m1 } {a1 , a3 , b1 , c1 , m1 }
6 print a

(IIT Bombay) Bit Vector Frameworks 64 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2 Gen Kill
1 c11: c = 3 {a0 , a1 , a2 , a3 , b0 ,
m1 : m = 6 1 {a1 , b1 , c1 , m1 } b 1 , c 0 , c 1 , m0 , m1 }
2 ∅ ∅
3 {a2 } {a0 , a1 , a2 , a3 }
2 if (a≤m) 4 ∅ ∅
T 5 {a3 } {a0 , a1 , a2 , a3 }
F 6 ∅ ∅
3 a2 : a = a+1
Iteration #2
In Out
4 if (a<12) 1 {a0 , b0 , c0 , m0 } {a1 , b1 , c1 , m1 }
T 2 {a1 , a2 , b1 , c1 , m1 } {a1 , a2 , b1 , c1 , m1 }
F 3 {a1 , a2 , b1 , c1 , m1 } {a2 , b1 , c1 , m1 }
t1 : t1 = a+b
5 a3 :1 a = t1+c 4 {a1 , a2 , b1 , c1 , m1 } {a1 , a2 , b1 , c1 , m1 }
5 {a1 , a2 , b1 , c1 , m1 } {a3 , b1 , c1 , m1 }
6 {a1 , a2 , a3 , b1 , c1 , m1 } {a1 , a2 , a3 , b1 , c1 , m1 }
6 print a

(IIT Bombay) Bit Vector Frameworks 64 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = 6

{a1 , a2 , b1 , c1 , m1 }
2 if (a≤m)

F T {a1 , a2 , b1 , c1 , m1 }
3 a2 : a = a+1

{a1 , a2 , b1 , c1 , m1 }
4 if (a<12)
F T {a1 , a2 , b1 , c1 , m1 }
t1 : t1 = a+b
5 a31 : a = t1+c
{a1 , a2 , a3 , b1 , c1 , m1 }
6 print a

(IIT Bombay) Bit Vector Frameworks 65 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = 6

{a1 , a2 , b1 , c1 , m1 }
RHS of m1 is constant and hence
2 if (a≤m)
cannot change
F T {a1 , a2 , b1 , c1 , m1 }
3 a2 : a = a+1

{a1 , a2 , b1 , c1 , m1 }
4 if (a<12)
F T {a1 , a2 , b1 , c1 , m1 }
t1 : t1 = a+b
5 a31 : a = t1+c
{a1 , a2 , a3 , b1 , c1 , m1 }
6 print a

(IIT Bombay) Bit Vector Frameworks 65 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = 6

{a1 , a2 , b1 , c1 , m1 }
RHS of m1 is constant and hence
2 if (a≤6)
cannot change
F T {a1 , a2 , b1 , c1 , m1 }
In block 2, m can be replaced by 6
3 a2 : a = a+1

{a1 , a2 , b1 , c1 , m1 }
4 if (a<12)
F T {a1 , a2 , b1 , c1 , m1 }
t1 : t1 = a+b
5 a31 : a = t1+c
{a1 , a2 , a3 , b1 , c1 , m1 }
6 print a

(IIT Bombay) Bit Vector Frameworks 65 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = 6

{a1 , a2 , b1 , c1 , m1 }
RHS of m1 is constant and hence
2 if (a≤6)
cannot change
F T {a1 , a2 , b1 , c1 , m1 }
In block 2, m can be replaced by 6
3 a2 : a = a+1
RHS of b1 and c1 are constant and
{a1 , a2 , b1 , c1 , m1 } hence cannot change
4 if (a<12)
F T {a1 , a2 , b1 , c1 , m1 }
t1 : t1 = a+b
5 a31 : a = t1+c
{a1 , a2 , a3 , b1 , c1 , m1 }
6 print a

(IIT Bombay) Bit Vector Frameworks 65 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = 6

{a1 , a2 , b1 , c1 , m1 }
RHS of m1 is constant and hence
2 if (a≤6)
cannot change
F T {a1 , a2 , b1 , c1 , m1 }
In block 2, m can be replaced by 6
3 a2 : a = a+1
RHS of b1 and c1 are constant and
{a1 , a2 , b1 , c1 , m1 } hence cannot change
4 if (a<12) In block 5, b can be replaced by 2
and c can be replaced by 3
F T {a1 , a2 , b1 , c1 , m1 }
t11 : t1 = a+2
5 a3 : a = t1+3
{a1 , a2 , a3 , b1 , c1 , m1 }
6 print a

(IIT Bombay) Bit Vector Frameworks 65 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = 6

2 if (a≤6)

F T
3 a2 : a = a+1

{a}
4 if (a<12)
F T
t1 : t1 = a+2
5 a31 : a = t1+3

6 print a

(IIT Bombay) Bit Vector Frameworks 66 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = 6

So what is the advantage?


2 if (a≤6)

F T
3 a2 : a = a+1

{a}
4 if (a<12)
F T
t1 : t1 = a+2
5 a31 : a = t1+3

6 print a

(IIT Bombay) Bit Vector Frameworks 66 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = 6

So what is the advantage?


2 if (a≤6)

F T Dead Code Elimination


3 a2 : a = a+1

{a}
4 if (a<12)
F T
t1 : t1 = a+2
5 a31 : a = t1+3

6 print a

(IIT Bombay) Bit Vector Frameworks 66 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = 6
{a}

So what is the advantage?


2 if (a≤6)

F T Dead Code Elimination


3 a2 : a = a+1
Only a is live at the exit of 1
{a}
4 if (a<12)
F T
t1 : t1 = a+2
5 a31 : a = t1+3

6 print a

(IIT Bombay) Bit Vector Frameworks 66 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = 6
{a}

So what is the advantage?


2 if (a≤6)

F T Dead Code Elimination


3 a2 : a = a+1
Only a is live at the exit of 1
{a}
Assignments of b, c, and m are dead code
4 if (a<12)
F T
t1 : t1 = a+2
5 a31 : a = t1+3

6 print a

(IIT Bombay) Bit Vector Frameworks 66 / 79


Tutorial Problem for Copy Propagation
a1 : a = 4
b : b=2
1 c11: c = 3
m1 : m = 6
{a}

So what is the advantage?


2 if (a≤6)

F T Dead Code Elimination


3 a2 : a = a+1
Only a is live at the exit of 1
{a}
Assignments of b, c, and m are dead code
4 if (a<12)
T Can be deleted
F
t1 : t1 = a+2
5 a31 : a = t1+3

6 print a

(IIT Bombay) Bit Vector Frameworks 66 / 79


Part VI

Anticipable Expressions Analysis


Defining Anticipable Expressions Analysis

An expression e is anticipable at a program point p, if every path from p


to the program exit contains an evaluation of e which is not preceded by a
redefinition of any operand of e.
Application : Safety of Code Placement

(IIT Bombay) Bit Vector Frameworks 68 / 79


Safety of Code Placement

1 if (b == 0) 1

False True

2 c = a/b 2 3 f (a/b) 3

(IIT Bombay) Bit Vector Frameworks 69 / 79


Safety of Code Placement

1 if (b == 0) 1

False True

2 c = a/b 2 3 f (a/b) 3

Placing a/b at the exit of 1 is unsafe


(≡ can change the behaviour of
the optimized program)

(IIT Bombay) Bit Vector Frameworks 69 / 79


Safety of Code Placement

1 if (b == 0) 1 1 if (b == 0) 1

False True False True

2 c = a/b 2 3 f (a/b) 3 2 c = a/b 2 3 print a/b 3

Placing a/b at the exit of 1 is unsafe


(≡ can change the behaviour of
the optimized program)
??

(IIT Bombay) Bit Vector Frameworks 69 / 79


Safety of Code Placement

1 if (b == 0) 1 1 if (b == 0) 1

False True False True

2 c = a/b 2 3 f (a/b) 3 2 c = a/b 2 3 print a/b 3

Placing a/b at the exit of 1 is unsafe


(≡ can change the behaviour of
the optimized program)
??
A guarded computation of an expression should not be converted to an
unguarded computation

(IIT Bombay) Bit Vector Frameworks 69 / 79


Defining Data Flow Analysis for Anticipable
Expressions Analysis

Genn = { e | expression e is evaluated in basic block n and


this evaluation is not preceded (within n) by a
definition of any operand of e}

Kill n = { e | basic block n contains a definition of an operand of e}

Entity Manipulation Exposition


Genn Expression Use Upwards
Kill n Expression Modification Anywhere

(IIT Bombay) Bit Vector Frameworks 70 / 79


Data Flow Equations for Anticipable Expressions
Analysis

Inn = Genn ∪ (Out n − Kill n )



 \BI n is End block
Out n = Ins otherwise

s∈succ(n)

Inn and Out n are sets of expressions

(IIT Bombay) Bit Vector Frameworks 71 / 79


Tutorial Problem 1 for Anticipable Expressions
Analysis

a = 5;
n1 b = 10; n1

n2 e = b − c; n2
c = 6;

n4 d = b + c; n4
c = 2;
n3 d = b + c; n3
a = 10;

n5 d = b − c; n5

n6 d = b + c; n6
e = a ∗ b;

Expr = { a ∗ b, b + c, b − c }
(IIT Bombay) Bit Vector Frameworks 72 / 79
Solution of Tutorial Problem 1

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Genn Kill n Out n Inn Out n Inn
n6 110 000
n5 001 000
n4 010 011
n3 010 100
n2 001 011
n1 000 111

(IIT Bombay) Bit Vector Frameworks 73 / 79


Solution of Tutorial Problem 1

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Genn Kill n Out n Inn Out n Inn
n6 110 000 000 110
n5 001 000 110 111
n4 010 011 111 110
n3 010 100 110 010
n2 001 011 010 001
n1 000 111 001 000

(IIT Bombay) Bit Vector Frameworks 73 / 79


Solution of Tutorial Problem 1

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Genn Kill n Out n Inn Out n Inn
n6 110 000 000 110
n5 001 000 110 111
n4 010 011 111 110 001 010
n3 010 100 110 010
n2 001 011 010 001
n1 000 111 001 000

(IIT Bombay) Bit Vector Frameworks 73 / 79


Tutorial Problem 2 for Anticipable Expressions
Analysis

d = a ∗ b;
n1 if (d) n1

n3 c = a ∗ b; n3
n2 a = a ∗ b; n2
n4 if (c) n4

n5 d = c + d; n5
a = 5;

n6 print a ∗ b; n6
Expr = { a ∗ b, c + d }
(IIT Bombay) Bit Vector Frameworks 74 / 79
Solution of Tutorial Problem 2

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Genn Kill n Out n Inn Out n Inn
n6 10 00
n5 01 11
n4 00 00
n3 10 01
n2 10 10
n1 10 01

(IIT Bombay) Bit Vector Frameworks 75 / 79


Solution of Tutorial Problem 2

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Genn Kill n Out n Inn Out n Inn
n6 10 00 00 10
n5 01 11 10 01
n4 00 00 01 01
n3 10 01 01 10
n2 10 10 01 11
n1 10 01 10 10

(IIT Bombay) Bit Vector Frameworks 75 / 79


Solution of Tutorial Problem 2

Local Global Information


Block Information Iteration # 1 Change in iteration # 2
Genn Kill n Out n Inn Out n Inn
n6 10 00 00 10
n5 01 11 10 01 00
n4 00 00 01 01 00 00
n3 10 01 01 10 00
n2 10 10 01 11
n1 10 01 10 10

(IIT Bombay) Bit Vector Frameworks 75 / 79


Part VII

Common Features of Bit Vector


Data Flow Frameworks
Defining Local Data Flow Properties

Live variables analysis

Entity Manipulation Exposition


Genn Variable Use Upwards
Kill n Variable Modification Anywhere

Analysis of expressions

Exposition
Entity Manipulation
Availability Anticipability
Genn Expression Use Downwards Upwards
Kill n Expression Modification Anywhere Anywhere

(IIT Bombay) Bit Vector Frameworks 77 / 79


Common Form of Data Flow Equations

Xi = f (Yi )
Yi = ⊓ Xj

(IIT Bombay) Bit Vector Frameworks 78 / 79


Common Form of Data Flow Equations

Data Flow Information So far we have seen sets (or bit vectors).
Could be entities other than sets.

Xi = f (Yi )
Yi = ⊓ Xj

(IIT Bombay) Bit Vector Frameworks 78 / 79


Common Form of Data Flow Equations

Data Flow Information So far we have seen sets (or bit vectors).
Could be entities other than sets.

Flow Function

So far we have seen


constant Gen and Kill .
Xi = f (Yi ) Could be dependent Gen
Yi = ⊓ Xj and Kill .

(IIT Bombay) Bit Vector Frameworks 78 / 79


Common Form of Data Flow Equations

Data Flow Information So far we have seen sets (or bit vectors).
Could be entities other than sets.

Flow Function

So far we have seen


constant Gen and Kill .
Xi = f (Yi ) Could be dependent Gen
Yi = ⊓ Xj and Kill .

So far we have seen ∪ and ∩.


Confluence
Could be other operations.

(IIT Bombay) Bit Vector Frameworks 78 / 79


A Taxonomy of Bit Vector Data Flow Frameworks

Confluence
Union Intersection
Forward Reaching Definitions Available Expressions
Backward Live Variables Anticipable Expressions
Bidirectional Partial Redundancy Elimination
(limited) (Original M-R Formulation)

(IIT Bombay) Bit Vector Frameworks 79 / 79


A Taxonomy of Bit Vector Data Flow Frameworks

Any Path

Confluence
Union Intersection
Forward Reaching Definitions Available Expressions
Backward Live Variables Anticipable Expressions
Bidirectional Partial Redundancy Elimination
(limited) (Original M-R Formulation)

(IIT Bombay) Bit Vector Frameworks 79 / 79


A Taxonomy of Bit Vector Data Flow Frameworks

Any Path

All Paths

Confluence
Union Intersection
Forward Reaching Definitions Available Expressions
Backward Live Variables Anticipable Expressions
Bidirectional Partial Redundancy Elimination
(limited) (Original M-R Formulation)

(IIT Bombay) Bit Vector Frameworks 79 / 79


A Taxonomy of Bit Vector Data Flow Frameworks

Any Path

All Paths

Confluence
Union Intersection
Forward Reaching Definitions Available Expressions
Backward Live Variables Anticipable Expressions
Bidirectional Partial Redundancy Elimination
(limited) (Original M-R Formulation)

(IIT Bombay) Bit Vector Frameworks 79 / 79


A Taxonomy of Bit Vector Data Flow Frameworks

Any Path

All Paths

Confluence
Union Intersection
Forward Reaching Definitions Available Expressions
Backward Live Variables Anticipable Expressions
Bidirectional Partial Redundancy Elimination
(limited) (Original M-R Formulation)

(IIT Bombay) Bit Vector Frameworks 79 / 79


A Taxonomy of Bit Vector Data Flow Frameworks

Any Path

All Paths

Confluence
Union Intersection
Forward Reaching Definitions Available Expressions
Backward Live Variables Anticipable Expressions
Bidirectional Partial Redundancy Elimination
(limited) (Original M-R Formulation)

(IIT Bombay) Bit Vector Frameworks 79 / 79

You might also like