• No results found

Complexity of Constant Propagation?

N/A
N/A
Protected

Academic year: 2022

Share "Complexity of Constant Propagation?"

Copied!
675
0
0

Loading.... (view fulltext now)

Full text

(1)

Uday Khedker

(www.cse.iitb.ac.in/˜uday)

Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

September 2017

(2)

Part 1

About These Slides

(3)

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 book

• M. S. Hecht. Flow Analysis of Computer Programs. Elsevier North-Holland Inc. 1977.

These slides are being made available under GNU FDL v1.2 or later purely for academic or research use.

(4)

CS 618 General Frameworks: Outline

Outline

• Modelling General Flows

• Constant Propagation

• Strongly Live Variables Analysis (after mid-sem)

• Pointer Analyses (after mid-sem)

• Heap Reference Analysis (after mid-sem)

(5)

Precise Modelling of General Flows

(6)

CS 618

Complexity of Constant Propagation?

1 a=b+ 1 1 2 a=b+ 1 2

3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

(7)

1 a=b+ 1 1 2 a=b+ 1 2

3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

2 a=b+ 1 2 3 b=c+ 1 3 4 c=d+ 1 4 5 d= 2 5

Iteration #1

(8)

CS 618

Complexity of Constant Propagation?

1 a=b+ 1 1 2 a=b+ 1 2

3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

1 a=b+ 1 1 2 a=b+ 1 2

3 b=c+ 1 3 4 c=d+ 1 4 5 d= 2 5

Iteration #1

1 a=b+ 1 1 2 a=b+ 1 2

3 b=c+ 1 3 4 c= 3 4 5 d= 2 5

Iteration #2

(9)

1 a=b+ 1 1 2 a=b+ 1 2

3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

2 a=b+ 1 2 3 b=c+ 1 3 4 c=d+ 1 4 5 d= 2 5

Iteration #1

2 a=b+ 1 2 3 b=c+ 1 3 4 c= 3 4 5 d= 2 5

Iteration #2

1 a=b+ 1 1 2 a=b+ 1 2

3 b= 4 3 4 c= 3 4 5 d= 2 5

(10)

CS 618

Complexity of Constant Propagation?

1 a=b+ 1 1 2 a=b+ 1 2

3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

1 a=b+ 1 1 2 a=b+ 1 2

3 b=c+ 1 3 4 c=d+ 1 4 5 d= 2 5

Iteration #1

1 a=b+ 1 1 2 a=b+ 1 2

3 b=c+ 1 3 4 c= 3 4 5 d= 2 5

Iteration #2

1 a=b+ 1 1 2 a=b+ 1 2

3 b= 4 3 4 c= 3 4 5 d= 2 5

1 a= 5 1 2 a= 5 2

3 b= 3 3 4 c= 3 4 5 d= 2 5

(11)

X p1

X p2

Xp3

x f(x)

Paths Terminating atp2 Data Flow Value

p1,p2 x

p1,p2,p3,p2 f(x)

p1,p2,p3,p2,p3,p2 f(f(x)) =f2(x) p1,p2,p3,p2,p3,p2,p3,p2 f(f(f(x))) =f3(x)

. . . .

(12)

CS 618

Loop Closures of Flow Functions

X p1

X p2

Xp3

x f(x)

Paths Terminating atp2 Data Flow Value

p1,p2 x

p1,p2,p3,p2 f(x)

p1,p2,p3,p2,p3,p2 f(f(x)) =f2(x) p1,p2,p3,p2,p3,p2,p3,p2 f(f(f(x))) =f3(x)

. . . .

• For static analysis we need to summarize the value atp2by a value which is safe afteranyiteration.

f(x) =x⊓f(x)⊓f2(x)⊓f3(x)⊓f4(x)⊓. . .

(13)

X p1

X p2

Xp3

x f(x)

Paths Terminating atp2 Data Flow Value

p1,p2 x

p1,p2,p3,p2 f(x)

p1,p2,p3,p2,p3,p2 f(f(x)) =f2(x) p1,p2,p3,p2,p3,p2,p3,p2 f(f(f(x))) =f3(x)

. . . .

• For static analysis we need to summarize the value atp2by a value which is safe afteranyiteration.

f(x) =x⊓f(x)⊓f2(x)⊓f3(x)⊓f4(x)⊓. . .

• f is called theloop closureoff.

(14)

CS 618

Loop Closure Boundedness

• Boundedness off requires the existence of somek such that f(x) =x⊓f(x)⊓f2(x)⊓. . .⊓fk−1(x)

• This follows from the descending chain condition

• For efficiency, we need a constantk that is independent of the size of the lattice

(15)

• Flow functions in bit vector frameworks have constant Gen and Kill f(x) = x⊓f(x)⊓f2(x)⊓f3(x)⊓. . .

f2(x) = f(Gen∪(x−Kill))

= Gen∪((Gen∪(x−Kill))−Kill)

= Gen∪((Gen−Kill)∪(x−Kill))

= Gen∪(Gen−Kill)∪(x−Kill)

= Gen∪(x−Kill) = f(x) f(x) = x⊓f(x)

(16)

CS 618

Loop Closures in Bit Vector Frameworks

• Flow functions in bit vector frameworks have constant Gen and Kill f(x) = x⊓f(x)⊓f2(x)⊓f3(x)⊓. . .

f2(x) = f(Gen∪(x−Kill))

= Gen∪((Gen∪(x−Kill))−Kill)

= Gen∪((Gen−Kill)∪(x−Kill))

= Gen∪(Gen−Kill)∪(x−Kill)

= Gen∪(x−Kill) = f(x) f(x) = x⊓f(x)

• Loop Closures of Bit Vector Frameworks are 2-bounded.

(17)

• Flow functions in bit vector frameworks have constant Gen and Kill f(x) = x⊓f(x)⊓f2(x)⊓f3(x)⊓. . .

f2(x) = f(Gen∪(x−Kill))

= Gen∪((Gen∪(x−Kill))−Kill)

= Gen∪((Gen−Kill)∪(x−Kill))

= Gen∪(Gen−Kill)∪(x−Kill)

= Gen∪(x−Kill) = f(x) f(x) = x⊓f(x)

• Loop Closures of Bit Vector Frameworks are 2-bounded.

• Intuition: Since Gen and Kill are constant, same things are generated or killed in every application off.

Multiple applications off are not required unless the input value changes.

(18)

CS 618

Larger Values of Loop Closure Bounds

• Fast Frameworks≡2-bounded frameworks (eg. bit vector frameworks) Both these conditions must be satisfied

Separability

Data flow values of different entities are independent

Constant or Identity Flow Functions

Flow functions for an entity are either constant or identity

• Non-fast frameworks

At least one of the above conditions is violated

(19)

f :L→Lishbh1,bh2, . . . ,bhmiwherebhi computes the value ofbxi

(20)

CS 618

Separability

f :L→Lishbh1,bh2, . . . ,bhmiwherebhi computes the value ofbxi

Separable Non-Separable

Example: All bit vector frameworks Example: Constant Propagation

(21)

f :L→Lishbh1,bh2, . . . ,bhmiwherebhi computes the value ofbxi

Separable

h b x

1

, b x

2

, . . . , b x

m

i

f

h b y

1

, y b

2

, . . . , b y

m

i

Non-Separable

h b x

1

, b x

2

, . . . , b x

m

i

f

h b y

1

, b y

2

, . . . , y b

m

i

Example: All bit vector frameworks Example: Constant Propagation

(22)

CS 618

Separability

f :L→Lishbh1,bh2, . . . ,bhmiwherebhi computes the value ofbxi

Separable

h b x

1

, b x

2

, . . . , b x

m

i

bh2

h b y

1

, b y

2

, . . . , b y

m

i

Non-Separable

h b x

1

, b x

2

, . . . , b x

m

i

f

h b y

1

, b y

2

, . . . , y b

m

i

Example: All bit vector frameworks Example: Constant Propagation

(23)

f :L→Lishbh1,bh2, . . . ,bhmiwherebhi computes the value ofbxi

Separable

h b x

1

, b x

2

, . . . , b x

m

i

bh2

h b y

1

, b y

2

, . . . , b y

m

i

bh:bL →bL

Non-Separable

h b x

1

, b x

2

, . . . , b x

m

i

f

h b y

1

, b y

2

, . . . , y b

m

i

Example: All bit vector frameworks Example: Constant Propagation

(24)

CS 618

Separability

f :L→Lishbh1,bh2, . . . ,bhmiwherebhi computes the value ofbxi

Separable

h b x

1

, b x

2

, . . . , b x

m

i

bh2

h b y

1

, b y

2

, . . . , b y

m

i

bh:bL →bL

Non-Separable

h b x

1

, b x

2

, . . . , b x

m

i

bh2

h b y

1

, y b

2

, . . . , b y

m

i

Example: All bit vector frameworks Example: Constant Propagation

(25)

f :L→Lishbh1,bh2, . . . ,bhmiwherebhi computes the value ofbxi

Separable

h b x

1

, b x

2

, . . . , b x

m

i

bh2

h b y

1

, b y

2

, . . . , b y

m

i

bh:bL →bL

Non-Separable

h b x

1

, b x

2

, . . . , b x

m

i

bh2

h b y

1

, y b

2

, . . . , b y

m

i

bh:L→bL

Example: All bit vector frameworks Example: Constant Propagation

(26)

CS 618

Separability of Bit Vector Frameworks

• bL is{0,1},Lis{0,1}m

• ⊓b is either boolean AND or boolean OR

• ⊤b and⊥b are 0 or 1 depending on⊓.b

• bhis abit functionand could be one of the following:

Raise Lower Propagate Negate

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

(27)

• bL is{0,1},Lis{0,1}m

• ⊓b is either boolean AND or boolean OR

• ⊤b and⊥b are 0 or 1 depending on⊓.b

• bhis abit functionand could be one of the following:

Raise Lower Propagate Negate

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

⊤b

⊥b

(28)

CS 618

Larger Values of Loop Closure Bounds

1 a=b+ 1 1

2 a=b+ 1 2 3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

Composite flow function for the loop is

f(hva,vb,vc,vdi) =hvb+ 1,vc+ 1,vd+ 1,2i

(29)

1 a=b+ 1 1

2 a=b+ 1 2 3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

Composite flow function for the loop is

f(hva,vb,vc,vdi) =hvb+ 1,vc+ 1,vd+ 1,2i f is not 2-bounded because:

(30)

CS 618

Larger Values of Loop Closure Bounds

1 a=b+ 1 1

2 a=b+ 1 2 3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

Composite flow function for the loop is

f(hva,vb,vc,vdi) =hvb+ 1,vc+ 1,vd+ 1,2i f is not 2-bounded because:

f(hb⊤,⊤,b ⊤,b ⊤i)b = hb⊤, ⊤,b ⊤,b 2i

(31)

1 a=b+ 1 1

2 a=b+ 1 2 3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

Composite flow function for the loop is

f(hva,vb,vc,vdi) =hvb+ 1,vc+ 1,vd+ 1,2i f is not 2-bounded because:

f(hb⊤,⊤,b ⊤,b ⊤i)b = hb⊤, ⊤,b ⊤,b 2i f2(h⊤,b ⊤,b ⊤,b ⊤i)b = h⊤,b ⊤,b 3, 2i

(32)

CS 618

Larger Values of Loop Closure Bounds

1 a=b+ 1 1

2 a=b+ 1 2 3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

Composite flow function for the loop is

f(hva,vb,vc,vdi) =hvb+ 1,vc+ 1,vd+ 1,2i f is not 2-bounded because:

f(hb⊤,⊤,b ⊤,b ⊤i)b = hb⊤, ⊤,b ⊤,b 2i f2(h⊤,b ⊤,b ⊤,b ⊤i)b = h⊤,b ⊤,b 3, 2i f3(h⊤,b ⊤,b ⊤,b ⊤i)b = h⊤,b 4, 3, 2i

(33)

1 a=b+ 1 1

2 a=b+ 1 2 3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

Composite flow function for the loop is

f(hva,vb,vc,vdi) =hvb+ 1,vc+ 1,vd+ 1,2i f is not 2-bounded because:

f(hb⊤,⊤,b ⊤,b ⊤i)b = hb⊤, ⊤,b ⊤,b 2i f2(h⊤,b ⊤,b ⊤,b ⊤i)b = h⊤,b ⊤,b 3, 2i f3(h⊤,b ⊤,b ⊤,b ⊤i)b = h⊤,b 4, 3, 2i f4(h⊤,b ⊤,b ⊤,b ⊤i)b = h5, 4, 3, 2i

(34)

CS 618

Larger Values of Loop Closure Bounds

1 a=b+ 1 1

2 a=b+ 1 2 3 b=c+ 1 3 4 c=d+ 1 4

5 d= 2 5

Composite flow function for the loop is

f(hva,vb,vc,vdi) =hvb+ 1,vc+ 1,vd+ 1,2i f is not 2-bounded because:

f(hb⊤,⊤,b ⊤,b ⊤i)b = hb⊤, ⊤,b ⊤,b 2i f2(h⊤,b ⊤,b ⊤,b ⊤i)b = h⊤,b ⊤,b 3, 2i f3(h⊤,b ⊤,b ⊤,b ⊤i)b = h⊤,b 4, 3, 2i f4(h⊤,b ⊤,b ⊤,b ⊤i)b = h5, 4, 3, 2i f5(hb⊤,⊤,b ⊤,b ⊤i)b = h5, 4, 3, 2i

(35)

Constant Propagation

(36)

CS 618

Example of Constant Propagation

n1

a= 1 b= 2 c=a+b

n1

n2 c=a+b d=a∗b n2

n3

d=c−1 a= 2 b= 1 c=a+b

n3

(37)

n1

a= 1 b= 2 c=a+b

n1

n2 c=a+b d=a∗b n2

n3

d=c−1 a= 2 b= 1 c=a+b

n3

MoP hb⊤,⊤,b ⊤,b ⊤ib

h1,2,3,⊤ib

hb⊥,⊥,b 3,2i hb⊥,⊥,b 3,2i

h⊥,b ⊥,b 3,2i

h2,1,3,2i

(38)

CS 618

Example of Constant Propagation

n1

a= 1 b= 2 c=a+b

n1

n2 c=a+b d=a∗b n2

n3

d=c−1 a= 2 b= 1 c=a+b

n3

MoP hb⊤,⊤,b ⊤,b ⊤ib

h1,2,3,⊤ib

hb⊥,⊥,b 3,2i hb⊥,⊥,b 3,2i

h⊥,b ⊥,b 3,2i

h2,1,3,2i

MFP hb⊤,⊤,b ⊤,b ⊤ib

h1,2,3,⊤ib

hb⊥,⊥,b 3,⊥ib hb⊥,⊥,b ⊥,b ⊥ib

h⊥,b ⊥,b ⊥,b ⊥ib

h2,1,3,⊥ib

(39)

(⊤)b undef orud

−∞

. . .

−2 −1 0 1 2

. . .

(⊥)b nonconst ornc

b

⊓ hv,udi hv,nci hv,c1i hv,udi hv,udi hv,nci hv,c1i hv,nci hv,nci hv,nci hv,nci

hv,c2i hv,c2i hv,nci Ifc1=c2thenhv,c1ielsehv,nci

(40)

CS 618

Overall Lattice for Integer Constant Propagation

• Inn/Outn values are mappingsVar→bL: Inn,Outn∈Var→bL

• Overall latticeL is a set of mappingsVar→bL: L=Var→bL

• ⊓andb⊓get defined by⊑and⊑b

Partial order is restricted to data flow values of the same variable Data flow values of different variables are incomparable

(x,v1)⊑(y,v2) ⇔ x=y∧v1⊑vb 2

OR x7→v1⊑y 7→v2 ⇔ x=y∧v1⊑vb 2

For meet operation, we assume thatX is a total function Partial functions are made total by using⊤valueb

X⊓Y =

(x,v1⊓vb 2)|(x,v1)∈X,(x,v2)∈Y

OR X⊓Y =

x7→v1⊓vb 2|x 7→v1∈X,x7→v2∈Y

(41)

Accessing and manipulating a mappingX ⊆A→B

• X(a) denotes the image ofa∈A X(a)∈B

• X[a7→v] changes the image ofainX tov

X[a7→v] = (X− {(a,u)|u∈B}) ∪ {(a,v)}

(42)

CS 618

Defining Data Flow Equations for Constant Propagation

Inn =



BI={hy,udi |y ∈Var} n=Start

p∈pred(n)

Outp otherwise

Outn = fn(Inn)

fn(X) =











X[y 7→c] nisy =c,y ∈Var,c∈Const X[y 7→nc] nisinput(y),y ∈var

X[y 7→X(z)] nisy =z,y ∈Var,z ∈Var X[y 7→eval(e,X)] nisy =e,y ∈Var,e∈Expr

X otherwise

eval(e,X) =







nc a∈Opd(e)∩Var,X(a) =nc ud a∈Opd(e)∩Var,X(a) =ud

−X(a) eis −a X(a)⊕X(b) eis a⊕b

(43)

n1 input (e); n1

n2 a= 7;b= 2;f =e;

if (f>0) n2

n3 a= 2;

if (f e+ 2) n3

n4 b=c+ 1;

if (b7) n4

n6 if (f e+ 1) n6

n5 f =f+ 1; n5

n7 c=da; n7n8 d=a+b; n8

n d=a+ 1; n n e=a+b; n

false

true false

false

true false

true

true

(44)

CS 618

Example Program for Constant Propagation

n1 input (e); n1

n2 a= 7;b= 2;f =e;

if (f>0) n2

n3 a= 2;

if (f e+ 2) n3

n4 b=c+ 1;

if (b7) n4

n6 if (f e+ 1) n6

n5 f =f+ 1; n5

n7 c=da; n7n8 d=a+b; n8

n d=a+ 1; n n e=a+b; n

false

true false

false

true false

true

true

For readability, we have combined many statements in a single block. However, con- stant propagation requires every basic block to contain a single statement because of the presence of dependent parts in flow functions.

(45)

Iteration #1 iteration #2 iteration #3 iteration #4 Inn1 b,b,b,b,b,b

Outn1 b,b,b,b,b,b Inn2 b,b,b,b,b,b Outn2 7,2,b,b,b,b

Inn3 7,2,b,b,b,b b,2,b,3,b,b b,2,6,3,b,b b,b,6,3,b,b Outn3 2,2,b,b,b,b 2,2,b,3,b,b 2,2,6,3,b,b 2,b,6,3,b,b Inn4 2,2,b,b,b,b 2,2,b,3,b,b 2,2,6,3,b,b 2,b,6,3,b,b Outn4 2,b,b,b,b,b 2,b,b,3,b,b 2,7,6,3,b,b

Inn5 2,b,b,b,b,b 2,b,b,3,b,b 2,7,6,3,b,b Outn5 2,b,b,b,b,b 2,b,b,3,b,b 2,7,6,3,b,b

Inn6 2,2,b,b,b,b 2,2,b,3,b,b 2,2,6,3,b,b 2,b,6,3,b,b Outn6 2,2,b,b,b,b 2,2,b,3,b,b 2,2,6,3,b,b 2,b,6,3,b,b

Inn7 2,2,b,b,b,b 2,2,b,3,b,b 2,b,6,3,b,b Outn7 2,2,b,b,b,b 2,2,6,3,b,b 2,b,6,3,b,b

Inn8 2,2,b,b,b,b 2,2,b,3,b,b 2,2,6,3,b,b 2,b,6,3,b,b Outn8 2,2,b,4,b,b 2,2,b,4,b,b 2,2,6,4,b,b 2,b,6,b,b,b

Inn9 2,2,b,4,b,b 2,2,6,b,b,b 2,b,6,b,b,b Outn9 2,2,b,3,b,b 2,2,6,3,b,b 2,b,6,3,b,b Inn10 b,2,b,b,b,b b,2,b,3,b,b b,b,6,3,b,b

(46)

CS 618

Result of Constant Propagation

n1 input (e); n1

n2 a= 7;b= 2;f =e;

if (f>0) n2

n3 a= 2;

if (f e+ 2) n3

n4 b=c+ 1;

if (b7) n4

n6 if (f e+ 1) n6

n5 f =f+ 1; n5

n7 c=da; n7n8 d=a+b; n8

a= 2

n d=a+ 1; n a= 2 n e=a+b; n

false

true false

false

true false

true

true

(47)

n1 input (e); n1

n2 a= 7;b= 2;f =e;

if (f>0) n2

n3 a= 2;

if (f e+ 2) n3

n4 b=c+ 1;

if (b7) n4

n6 if (f e+ 1) n6

n5 f =f+ 1; n5

n7 c=da; n7

a= 2, d= 3

n8 d=a+b; n8

a= 2

n d=a+ 1; n a= 2 n e=a+b; n

false

true false

false

true false

true

true

(48)

CS 618

Result of Constant Propagation

n1 input (e); n1

n2 a= 7;b= 2;f =e;

if (f>0) n2

n3 a= 2;

if (f e+ 2) n3

n4 b=c+ 1;

if (b7) n4

c= 6

n6 if (f e+ 1) n6

n5 f =f+ 1; n5

n7 c=da; n7

a= 2, d= 3

n8 d=a+b; n8

a= 2

n d=a+ 1; n a= 2 n e=a+b; n

false

true false

false

true false

true

true

(49)

n1 input (e); n1

n2 a= 7;b= 2;f =e;

if (f>0) n2

n3 a= 2;

if (f e+ 2) n3

n4 b=c+ 1;

if (b7) n4

c= 6

n6 if (f e+ 1) n6

n5 f =f+ 1; n5

n7 c=da; n7

a= 2, d= 3

n8 d=a+b; n8

a= 2

n d=a+ 1; n a= 2 n e=a+b; n

false

true false

true false

true

true

(50)

CS 618

Monotonicity of Constant Propagation

Proof obligation: X1⊑X2 ⇒ fn(X1)⊑fn(X2) where,

fn(X) =











X[y7→c] nisy =c,y ∈Var,c∈Const (C1) X[y7→nc] nisinput(y),y ∈var (C2) X[y7→X(z)] nisy =z,y ∈Var,z ∈Var (C3) X[y7→eval(e,X)] nisy =e,y ∈Var,e∈Expr (C4)

X otherwise (C5)

• The proof obligation trivially follows for cases C1, C2, C3, and C5

• For case C4, it requires showing X1⊑X2 ⇒ eval(e,X1)⊑eval(e,X2) which follows from the definition ofeval(e,X)

(51)

n1

a= 1 b= 2 c=a+b

n1

n2 c=a+b d=a∗b n2

n3

d=c−1 a= 2 b= 1 c=a+b

n3

(52)

CS 618

Non-Distributivity of Constant Propagation

n1

a= 1 b= 2 c=a+b

n1

n2 c=a+b d=a∗b n2

n3

d=c−1 a= 2 b= 1 c=a+b

n3

a= 1,b= 2

• x=h1,2,3,?i(AlongOutn1 →Inn2)

(53)

n1

a= 1 b= 2 c=a+b

n1

n2 c=a+b d=a∗b n2

n3

d=c−1 a= 2 b= 1 c=a+b

n3

a= 1,b= 2

a= 2,b= 1

• x=h1,2,3,?i(AlongOutn1 →Inn2)

• y =h2,1,3,2i(AlongOutn3→Inn2)

(54)

CS 618

Non-Distributivity of Constant Propagation

n1

a= 1 b= 2 c=a+b

n1

n2 c=a+b d=a∗b n2

n3

d=c−1 a= 2 b= 1 c=a+b

n3

a= 1,b= 2

a= 2,b= 1

• x=h1,2,3,?i(AlongOutn1 →Inn2)

• y =h2,1,3,2i(AlongOutn3→Inn2)

• Function application before merging

f(x)⊓f(y) = f(h1,2,3,?i)⊓f(h2,1,3,2i)

= h1,2,3,2i ⊓ h2,1,3,2i

= hb⊥,⊥,b 3,2i

(55)

n1

a= 1 b= 2 c=a+b

n1

n2 c=a+b d=a∗b n2

n3

d=c−1 a= 2 b= 1 c=a+b

n3

a= 1,b= 2

a= 2,b= 1

• x=h1,2,3,?i(AlongOutn1 →Inn2)

• y =h2,1,3,2i(AlongOutn3→Inn2)

• Function application before merging

f(x)⊓f(y) = f(h1,2,3,?i)⊓f(h2,1,3,2i)

= h1,2,3,2i ⊓ h2,1,3,2i

= hb⊥,⊥,b 3,2i

• Function application after merging

f(x⊓y) = f(h1,2,3,?i ⊓ h2,1,3,2i)

= f(h⊥,b ⊥,b 3,2i)

= hb⊥,⊥,b ⊥,b ⊥ib

(56)

CS 618

Non-Distributivity of Constant Propagation

n1

a= 1 b= 2 c=a+b

n1

n2 c=a+b d=a∗b n2

n3

d=c−1 a= 2 b= 1 c=a+b

n3

a= 1,b= 2

a= 2,b= 1

• x=h1,2,3,?i(AlongOutn1 →Inn2)

• y =h2,1,3,2i(AlongOutn3→Inn2)

• Function application before merging

f(x)⊓f(y) = f(h1,2,3,?i)⊓f(h2,1,3,2i)

= h1,2,3,2i ⊓ h2,1,3,2i

= hb⊥,⊥,b 3,2i

• Function application after merging

f(x⊓y) = f(h1,2,3,?i ⊓ h2,1,3,2i)

= f(h⊥,b ⊥,b 3,2i)

= hb⊥,⊥,b ⊥,b ⊥ib

(57)

a= 1 b= 2

a= 2 b= 1

c=a+b

(58)

CS 618

Why is Constant Propagation Non-Distributive?

a= 1 b= 2

a= 2 b= 1

c=a+b

a= 1 a= 2 b= 1 b= 2

Possible combinations due to merging

(59)

a= 1 b= 2

a= 2 b= 1

c=a+b

a= 1 a= 2 b= 1 b= 2

Possible combinations due to merging

c=a+b= 3

• Correct combination.

(60)

CS 618

Why is Constant Propagation Non-Distributive?

a= 1 b= 2

a= 2 b= 1

c=a+b

a= 1 a= 2 b= 1 b= 2

Possible combinations due to merging

c=a+b= 3

• Correct combination.

(61)

a= 1 b= 2

a= 2 b= 1

c=a+b

a= 1 a= 2 b= 1 b= 2

Possible combinations due to merging

c=a+b= 2

• Wrong combination.

• Mutually exclusive information.

• No execution path along which this information holds.

(62)

CS 618

Why is Constant Propagation Non-Distributive?

a= 1 b= 2

a= 2 b= 1

c=a+b

a= 1 a= 2 b= 1 b= 2

Possible combinations due to merging

c=a+b= 4

• Wrong combination.

• Mutually exclusive information.

• No execution path along which this information holds.

(63)

n1 a=b n1

n2 a=b n1

n3 d= 2 n1

n4 a=b n1

n5 c=d n1

n6 a=b n1

n7 b=c n1

n8 a=b n1

n10 a=b nn109 a=b n1

(64)

CS 618

Tutorial Problem on Constant Propagation

How many iterations do we need?

n1 a=b n1

n2 a=b n1

n3 d= 2 n1

n4 a=b n1

n5 c=d n1

n6 a=b n1

n7 b=c n1

n8 a=b n1

n10 a=b nn109 a=b n1

2

(65)

n1 a=b n1

n2 a=b n1

n3 d= 2 n1

n4 a=b n1

n5 c=d n1

n6 a=b n1

n7 b=c n1

n8 a=b n1

n10 a=b nn109 a=b n1

3

(66)

CS 618

Tutorial Problem on Constant Propagation

How many iterations do we need?

n1 a=b n1

n2 a=b n1

n3 d= 2 n1

n4 a=b n1

n5 c=d n1

n6 a=b n1

n7 b=c n1

n8 a=b n1

n10 a=b nn109 a=b n1

4

(67)

n1 a=b n1

n2 a=b n1

n3 d= 2 n1

n4 a=b n1

n5 c=d n1

n6 a=b n1

n7 b=c n1

n8 a=b n1

n10 a=b nn109 a=b n1

5

(68)

CS 618

Tutorial Problem on Constant Propagation

How many iterations do we need?

n1 a=b n1

n2 a=b n1

n3 d= 2 n1

n4 a=b n1

n5 c=d n1

n6 a=b n1

n7 b=c n1

n8 a=b n1

n10 a=b nn109 a=b n1

6

(69)

n1 a=b n1

n2 a=b n1

n3 d= 2 n1

n4 a=b n1

n5 c=d n1

n6 a=b n1

n7 b=c n1

n8 a=b n1

n10 a=b nn109 a=b n1

• Every back edge occurs only once in the ifp fromn3ton1that goes vian5,n7, andn9.

• 5 + 1 iterations for computing data flow values (+1 iteration to detect convergence)

(70)

CS 618

Tutorial Problem on Constant Propagation

And now how many iterations do we need?

n1 a=b n1

n2 a=b n1

n3 a=b n1

n4 a=b n1

n5 b=c n1

n6 a=b n1

n7 c=d n1

n8 a=b n1

n10 a=b nn109 d = 2 n1

(71)

n1 a=b n1

n2 a=b n1

n3 a=b n1

n4 a=b n1

n5 b=c n1

n6 a=b n1

n7 c=d n1

n8 a=b n1

n10 a=b nn109 d = 2 n1

Back edgen10→n1 needs to be traversed once each for back edges n9→n8,n7→n6,n5→n4, and n3→n2 (in that order).

⇒ 8 + 1 iterations.

(72)

CS 618

Boundedness of Constant Propagation

1 a= 1 1

2 a= 1 2

3 a= 1 3

4 a=b+ 1 4

5 b=c+ 1 5

6 c=a+ 1 5

7 a= 1 1

(73)

1 a= 1 1

2 a= 1 2

3 a= 1 3

4 a=b+ 1 4

5 b=c+ 1 5

6 c=a+ 1 5

7 a= 1 1

Summary flow function:

(data flow value at node 7) f(hva,vb,vci) = h1⊓(vb+ 1),

(vc+ 1), (va+ 1) i

(74)

CS 618

Boundedness of Constant Propagation

1 a= 1 1

2 a= 1 2

3 a= 1 3

4 a=b+ 1 4

5 b=c+ 1 5

6 c=a+ 1 5

7 a= 1 1

Summary flow function:

(data flow value at node 7) f(hva,vb,vci) = h1⊓(vb+ 1),

(vc+ 1), (va+ 1) i

f0(⊤) = hb⊤,⊤,b ⊤ib f1(⊤) = h1, ⊤,b ⊤ib

(75)

1 a= 1 1

2 a= 1 2

3 a= 1 3

4 a=b+ 1 4

5 b=c+ 1 5

6 c=a+ 1 5

7 a= 1 1

Summary flow function:

(data flow value at node 7) f(hva,vb,vci) = h1⊓(vb+ 1),

(vc+ 1), (va+ 1) i

f0(⊤) = hb⊤,⊤,b ⊤ib f1(⊤) = h1, ⊤,b ⊤ib f2(⊤) = h1, ⊤,b 2i

(76)

CS 618

Boundedness of Constant Propagation

1 a= 1 1

2 a= 1 2

3 a= 1 3

4 a=b+ 1 4

5 b=c+ 1 5

6 c=a+ 1 5

7 a= 1 1

Summary flow function:

(data flow value at node 7) f(hva,vb,vci) = h1⊓(vb+ 1),

(vc+ 1), (va+ 1) i

f0(⊤) = hb⊤,⊤,b ⊤ib f1(⊤) = h1, ⊤,b ⊤ib f2(⊤) = h1, ⊤,b 2i f3(⊤) = h1, 3, 2i

(77)

1 a= 1 1

2 a= 1 2

3 a= 1 3

4 a=b+ 1 4

5 b=c+ 1 5

6 c=a+ 1 5

7 a= 1 1

Summary flow function:

(data flow value at node 7) f(hva,vb,vci) = h1⊓(vb+ 1),

(vc+ 1), (va+ 1) i

f0(⊤) = hb⊤,⊤,b ⊤ib f1(⊤) = h1, ⊤,b ⊤ib f2(⊤) = h1, ⊤,b 2i f3(⊤) = h1, 3, 2i f4(⊤) = hb⊥,3, 2i

(78)

CS 618

Boundedness of Constant Propagation

1 a= 1 1

2 a= 1 2

3 a= 1 3

4 a=b+ 1 4

5 b=c+ 1 5

6 c=a+ 1 5

7 a= 1 1

Summary flow function:

(data flow value at node 7) f(hva,vb,vci) = h1⊓(vb+ 1),

(vc+ 1), (va+ 1) i

f0(⊤) = hb⊤,⊤,b ⊤ib f1(⊤) = h1, ⊤,b ⊤ib f2(⊤) = h1, ⊤,b 2i f3(⊤) = h1, 3, 2i f4(⊤) = hb⊥,3, 2i f5(⊤) = h⊥,b 3, ⊥ib

(79)

1 a= 1 1

2 a= 1 2

3 a= 1 3

4 a=b+ 1 4

5 b=c+ 1 5

6 c=a+ 1 5

7 a= 1 1

Summary flow function:

(data flow value at node 7) f(hva,vb,vci) = h1⊓(vb+ 1),

(vc+ 1), (va+ 1) i

f0(⊤) = hb⊤,⊤,b ⊤ib f1(⊤) = h1, ⊤,b ⊤ib f2(⊤) = h1, ⊤,b 2i f3(⊤) = h1, 3, 2i f4(⊤) = hb⊥,3, 2i f5(⊤) = h⊥,b 3, ⊥ib f6(⊤) = h⊥,b ⊥,b ⊥ib

(80)

CS 618

Boundedness of Constant Propagation

1 a= 1 1

2 a= 1 2

3 a= 1 3

4 a=b+ 1 4

5 b=c+ 1 5

6 c=a+ 1 5

7 a= 1 1

Summary flow function:

(data flow value at node 7) f(hva,vb,vci) = h1⊓(vb+ 1),

(vc+ 1), (va+ 1) i

f0(⊤) = hb⊤,⊤,b ⊤ib f1(⊤) = h1, ⊤,b ⊤ib f2(⊤) = h1, ⊤,b 2i f3(⊤) = h1, 3, 2i f4(⊤) = hb⊥,3, 2i f5(⊤) = h⊥,b 3, ⊥ib f6(⊤) = h⊥,b ⊥,b ⊥ib f7(⊤) = h⊥,b ⊥,b ⊥ib

(81)

1 a= 1 1

2 a= 1 2

3 a= 1 3

4 a=b+ 1 4

5 b=c+ 1 5

6 c=a+ 1 5

7 a= 1 1

f(⊤) =

6 i=0fi(⊤)

(82)

CS 618

Boundedness of Constant Propagation

The moral of the story:

• The data flow value of every variable could change twice

(83)

The moral of the story:

• The data flow value of every variable could change twice

• In the worst case, only one change may happen in every step of a function application

(84)

CS 618

Boundedness of Constant Propagation

The moral of the story:

• The data flow value of every variable could change twice

• In the worst case, only one change may happen in every step of a function application

• Maximum number of steps: 2× |Var|

(85)

The moral of the story:

• The data flow value of every variable could change twice

• In the worst case, only one change may happen in every step of a function application

• Maximum number of steps: 2× |Var|

• Boundedness parameterk is (2× |Var|) + 1

(86)

CS 618

Conditional Constant Propagation

n1 input (e); n1

n2 a= 7;b= 2;f =e; if (f>0) n2

n3 a= 2;

if (f e+ 2) n3

n4 b=c+ 1;

if (b7) n4

n6 if (f e+ 1) n6

n5 f =f+ 1; n5

n7 c=da; n7n8 d=a+b; n8

n d=a+ 1; n n e=a+b; n

false

true false

false

true false

true

true

An execution trace of the program when the value read for variableeis some numberx0

(otherwise the loop will not be entered)

(87)

n1 input (e); n1

n2 a= 7;b= 2;f =e; if (f>0) n2

n3 a= 2;

if (f e+ 2) n3

n4 b=c+ 1;

if (b7) n4

n6 if (f e+ 1) n6

n5 f =f+ 1; n5

n7 c=da; n7n8 d=a+b; n8

n d=a+ 1; n n e=a+b; n

false

true false

false

true false

true

true

h2,2,?,?,x,xi

An execution trace of the program when the value read for variableeis some numberx0

(otherwise the loop will not be entered)

References

Related documents

CS 618 General Frameworks: Heap Reference Analysis 55/95.. The Moral of

• Pointer analysis enables not only precise data analysis but also precise control flow analysis. • Needs to scale to

• Pointer analysis enables not only precise data analysis but also precise control flow analysis. • Needs to scale to

◮ Due to “every control flow path nature”, computation of anticipable and available access paths uses ∩ as the confluence..

• Data flow analysis uses static representation of programs to compute summary information along paths.. CS 618 Interprocedural DFA: Issues in Interprocedural

(Jan. Predicting the effects of optimization on a procedure body. In Proceedings of the SIGPLAN’79 Symposium on Compiler Construction. Published as SIGPLAN Not. An efficient way to

Providing cer- tainty that avoided deforestation credits will be recognized in future climate change mitigation policy will encourage the development of a pre-2012 market in

The Congo has ratified CITES and other international conventions relevant to shark conservation and management, notably the Convention on the Conservation of Migratory