Code Optimisation 2 - Bit Vector Framework
Code Optimisation 2 - Bit Vector Framework
Uday Khedker
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
p p p
v = a∗b v = a∗b
a = v +2 v = a+2 vv =
=v+
+22
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
p p p
v = a∗b v = a∗b
a = v +2 v = a+2 vv =
=v+
+22
p p p
v = a∗b v = a∗b
a = v +2 v = a+2 vv =
=v+
+22
p p p
v = a∗b v = a∗b
a = v +2 v = a+2 vv =
=v+
+22
Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j
Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j
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
Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j
Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j
r-value occurrence
Value is only read, e.g. x,y,z in
x.sum = y.data + z.data
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
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
within n
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
within n
anywhere in n
(IIT Bombay) Bit Vector Frameworks 8 / 79
Defining Data Flow Analysis for Live Variables
Analysis
Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j
Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j
Edge based
Global Data Flow Properties
specifications
Ini Inj
Geni , Kill i Genj , Kill j
Out i Out j
Gen = {y }, Kill = {y }
y = y.lptr
Gen = {y }, Kill = {y }
y = y.lptr
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
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
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
if (a<12) 4
T
F
t1 = a+b
5 a = t1+c
print "Hi"
print "Hello" 6
We can repeat liveness analysis on the optimized code and then optimize
it further
This can continue as long code continues to change
Here we show the repeated application only to show the scope of further
optimizations
if (a<12) 4
T
F
t1 = a+b
5 a = t1+c
print "Hi"
print "Hello" 6
if (a<12) 4
T
F
t1 = a+b
5 a = t1+c
print "Hi"
print "Hello" 6
Some Observations
What Does Data Flow Analysis Involve?
Ax = b
Equations Solution
4w = x + y + 32
4x = y + z + 32
w = x = y = z = 16
4y = z + w + 32
4z = w + x + 32
Iteration 4 Iteration 5
Iteration 4 Iteration 5
Iteration 4 Iteration 5
Iteration 4 Iteration 5
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
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
Iteration 3 Iteration 4
Iteration 3 Iteration 4
Iteration 3 Iteration 4
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
Draw the control flow graph and perform live variables 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
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
a=a+i
5 i=i+1
6 return a
a=a+i
5 i=i+1
6 return a
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
n2 {c} ∅
n1 ∅ {x, y }
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
z=1
n5 if (c < 20) n5
T
T
n6 z = x n6
z=1
n5 if (c < 20) n5
T
T
n6 z = x n6
z=1
n5 if (c < 20) n5
T
T
n6 z = x n6
z=1
n5 if (c < 20) n5
T
T
n6 z = x n6
T
T
n6 z = x n6
T
T
n6 z = x n6
a=b=5
print b
print b
Init.
∅
a=b=5
∅
∅
print b
∅
∅
print b
∅
Iter.
Init. #1
∅
a=b=5
∅
∅
print b
∅
∅
print b
∅ ∅
Iter.
Init. #1
∅
a=b=5
∅
∅
print b
∅
∅ ∅
print b
∅ ∅
Iter.
Init. #1
∅
a=b=5
∅
∅
print b
∅ ∅
∅ ∅
print b
∅ ∅
Iter.
Init. #1
∅
a=b=5
∅
∅ {b}
print b
∅ ∅
∅ ∅
print b
∅ ∅
Iter.
Init. #1
∅
a=b=5
∅ {b}
∅ {b}
print b
∅ ∅
∅ ∅
print b
∅ ∅
Iter.
Init. #1
∅ ∅
a=b=5
∅ {b}
∅ {b}
print b
∅ ∅
∅ ∅
print b
∅ ∅
Iter. Iter.
Init. #1 #2
∅ ∅ ∅
a=b=5
∅ {b} {b}
∅ {b} {b}
print b
∅ ∅ {b}
∅ ∅ ∅
print b
∅ ∅ ∅
Iter. Iter.
Init. #1 #2 Init.
∅ ∅ ∅ {a, b}
a=b=5 a=b=5
∅ {b} {b} {a, b}
∅ ∅ ∅ {a, b}
print b print b
∅ ∅ ∅ ∅
∅ ∅ ∅ {a, b}
print b print b
∅ ∅ ∅ ∅ ∅
∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅
∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅
∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅
∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅
∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅
∅ ∅ ∅ {a, b} ∅
print b print b
∅ ∅ ∅ ∅ ∅
print y End
print y End
x = z + 10 i
Out i = {y }
print x, y j
print y End
x = z + 10 i
Out i = {y }
print x, y j
print y End
print y End
b1 x=abs(x)
b2 if (x < 0)
T F
b3 x=a+y x=a+z b4
b5 print (x)
b1 x=abs(x)
b2 if (x < 0)
T F
b3 x=a+y x=a+z b4
b5 print (x)
b2 if (x < 0)
T F
b3 x=a+y x=a+z b4
b5 print (x)
b5 print (x)
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)
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)
b4 if (x < 0)
T F
b5 x=c+1 x=d+1 b6
b7 print (x)
b7 print (x)
b7 print (x)
Context insensitivity
◮ Merges of information across all calling contexts
Flow insensitivity
◮ Disregards the control flow
No compromises
a∗b a∗b
p p p
a ∗ b is
available at p
Start Start Start
a∗b a∗b a∗b
a∗b a∗b
p p p
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
p p p
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
p p p
\ BI n is Start block
Inn = Out p otherwise
p∈pred(n)
\ BI n is Start block
Inn = Out p otherwise
p∈pred(n)
Alternatively,
Out n = fn (Inn ), where
fn (X ) = Genn ∪ (X − Kill n )
\ BI n is Start block
Inn = Out p otherwise
p∈pred(n)
Alternatively,
Out n = fn (Inn ), where
fn (X ) = Genn ∪ (X − Kill n )
\ BI n is Start block
Inn = Out p otherwise
p∈pred(n)
Alternatively,
Out n = fn (Inn ), where
fn (X ) = Genn ∪ (X − Kill n )
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.
6 d ∗e 6
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
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
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
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
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 }
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
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
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
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
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 }
Global Information
Local Information
Node
Global Information
Local Information
Node
Global Information
Local Information
Node
Global Information
Local Information
Node
Global Information
Local Information
Node
Global Information
Local Information
Node
Out i = {a ∗ b,
b ∗ c}
print a ∗ b j
t =a∗b i
Out i = ∅
print a ∗ b j
t =a∗b i
Out i = ∅
print a ∗ b j
print a ∗ b j
1 w =a+c 1
2 x =a∗c 2
=a+c 3
3 zy = a∗c
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
[ BI n is Start block
Inn = Out p otherwise
p∈pred(n)
1 x = 5+10
2 x1: x = 5 3 x =5
4 y = x +10
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
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
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
4 if (a<12)
F T
t1 : t1 = a+b
5 a3 :1 a = t1+c
6 print a
6 print a
{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
{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
{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
{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
{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
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
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
{a}
4 if (a<12)
F T
t1 : t1 = a+2
5 a31 : a = t1+3
6 print a
6 print a
6 print a
6 print a
1 if (b == 0) 1
False True
2 c = a/b 2 3 f (a/b) 3
1 if (b == 0) 1
False True
2 c = a/b 2 3 f (a/b) 3
1 if (b == 0) 1 1 if (b == 0) 1
1 if (b == 0) 1 1 if (b == 0) 1
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
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
Analysis of expressions
Exposition
Entity Manipulation
Availability Anticipability
Genn Expression Use Downwards Upwards
Kill n Expression Modification Anywhere Anywhere
Xi = f (Yi )
Yi = ⊓ Xj
Data Flow Information So far we have seen sets (or bit vectors).
Could be entities other than sets.
Xi = f (Yi )
Yi = ⊓ Xj
Data Flow Information So far we have seen sets (or bit vectors).
Could be entities other than sets.
Flow Function
Data Flow Information So far we have seen sets (or bit vectors).
Could be entities other than sets.
Flow Function
Confluence
Union Intersection
Forward Reaching Definitions Available Expressions
Backward Live Variables Anticipable Expressions
Bidirectional Partial Redundancy Elimination
(limited) (Original M-R Formulation)
Any Path
Confluence
Union Intersection
Forward Reaching Definitions Available Expressions
Backward Live Variables Anticipable Expressions
Bidirectional Partial Redundancy Elimination
(limited) (Original M-R Formulation)
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)
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)
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)
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)