Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 04 – Numerical Abstractions Eran Yahav 1.

Similar presentations


Presentation on theme: "Lecture 04 – Numerical Abstractions Eran Yahav 1."— Presentation transcript:

1 Lecture 04 – Numerical Abstractions Eran Yahav 1

2 Previously…  Trace semantics  Collecting semantics  Lattices  Galois connection  Least fixed point 2

3 Galois Connection   C  (C)  (A)A   (C)  A  C   (A) 3

4 Best (Induced) Transformer   C A C’ A’ How do we figure out the abstract effect  S  # of a statement S ? ? S#S# SS 4

5 Sound Abstract Transformer   C A C’ A’ S#S# SS  Aopt    S    (A)   S  # (A) 5

6 Soundness of Induced Analysis RD  F(RD  )   F(F(RD  )) …  lfp(F) TR  G(TR  )   G(G(TR  )) …  G n (TR  ) … lfp(G)   (lfp(G))   (lfp(G))  lfp(   G   )  lfp(F) 6

7 Trivial Example 7 RD  F(RD  )   F(F(RD  )) …  lfp(F) TR  G(TR  )   G(G(TR  )) …  G n (TR  ) … lfp(G)   (lfp(G))   (lfp(G))  lfp(   G   )  lfp(F) x = 42; while(?) x++; x = 42; // x  { + } while(?) x++; // x  { + } is x = 17 possible in lfp(G)? is it in  (lfp(F))?

8 Today  A few more words about Lattices, Galois Connections, and friends  Numerical Abstractions  parity  signs  constant  interval  octagon  polyhedra 8

9 Complete Lattices  A complete lattice (L,  ) is a poset such that all subsets have least upper bounds as well as greatest lower bounds  In particular   =  =  L is the least element (bottom)   =  L =  is the greatest element (top)  Why do we care? (roughly speaking)  use a lattice to represent properties of a program  join operation  handle information reaching a program point from multiple sources  meet operation  handle restrictions (e.g., conditions) 9

10 Galois Connection  Connect two lattices  (C,  c ) representing “concrete” information  (A,  a ) representing abstract information  Using two functions   : C  A abstraction function   : A  C concretization function  such that   (C)  a A  C  c  (A)  Alternatively   and  are order-preserving (monotone)   a  A  (  (a))  a a   c  C c  c  (  (c)) 10

11 Galois Connection  Why do we care? (roughly)  captures intuition: values in one lattice used to represent values in another lattice in a conservative manner  In a Galois Connection  and  determine one another, enough to define one, and can compute the other   (c) =  { a | c   (a) }   (a) =  { c |  (c)  a }  global soundness theorem allows us to extend “local soundness” of individual operations to soundness of LFP computation 11

12 “Analysis Algorithm”  Complete lattice (A,  a, , , ,  )   S  # (a) the abstract transformer for each statement  a 1  a 2 join operation  a 1  a 2 check for convergence (fixed point) 12 a =  while a  f(a) do a = f(a)

13 Parity Abstraction 13 1: while (x !=1 ) do { 2: if (x % 2) == 0 { 3: x := x / 2; 4: } else { 5: x := x * 3 + 1; 6: assert (x %2 ==0); 7: } 8: }

14 Parity Abstraction   C  (C)  (A)A   (C)  A  C   (A) 14 program traces abstract state x  E

15 Parity Abstraction 11 11 C  3(  2(  1(C)))  1 (  2(  3(A3))) A3   (C)  A  C   (A) 15 set of traces abstract state x  E  1(C)  33 33 set of states 22 22  1(  1(C))  cartesian abstraction  2(  3(A3))  3(A3)

16 Some traces of the example program x = 3 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (10)-> 6:assert 10 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (5)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (16)- > 6:assert 16 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (8)-> 1:x!=1 - > 2:xmod2==0 -> 3:x=x/2 (4)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (2)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (1) x = 5 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (16)-> 6:assert 16 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (8)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (4)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (2)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (1) x = 7 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (22)-> 6:assert 22 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (11)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (34)-> 6:assert 34 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (17)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (52)-> 6:assert 52 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (26)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (13)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (40)-> 6:assert 40 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (20)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (10)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (5)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (16)-> 6:assert 16 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (8)-> … 16

17 Some traces of the example program x = 9 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (28)-> 6:assert 28 mod2==0 - >1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (14)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (7)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (22)-> 6:assert 22 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (11)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (34)-> 6:assert 34 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (17)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (52)- > 6:assert 52 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (26)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (13)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (40)-> 6:assert 40 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (20)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (10)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (5)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (16)-> 6:assert 16 mod2==0 ->… x = 11 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (34)-> 6:assert 34 mod2==0 - >1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (17)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (52)-> 6:assert 52 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (26)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (13)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (40)-> 6:assert 40 mod2==0 ->1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (20)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (10)-> 1:x!=1 -> 2:xmod2==0 -> 3:x=x/2 (5)-> 1:x!=1 -> 2:xmod2!=0 -> 5:x=x*3+1 (16)-> 6:assert 16 mod2==0 ->… 17

18 Collecting Semantics (label 6) 18 1:x!=1->2:xmod2!=0->5:x=x*3+1(10)->6:assert 10 mod2==0 1:x!=1->2:xmod2!=0->5:x=x*3+1(10)->6:assert 10 mod2==0->1:x!=1- >2:xmod2==0->3:x=x/2(5)->1:x!=1->2:xmod2!=0->5:x=x*3+1(16)-> 6:assert 16 mod2==0 1:x!=1->2:xmod2!=0->5:x=x*3+1(16)->6:assert 16 mod2==0 1:x!=1->2:xmod2!=0->5:x=x*3+1(22)->6:assert 22 mod2==0 1:x!=1->2:xmod2!=0->5:x=x*3+1(22)->6:assert 22 mod2==0->1:x!=1- >2:xmod2==0->3:x=x/2(11)->1:x!=1->2:xmod2!=0->5:x=x*3+1(34)-> 6:assert 34 mod2==0 1:x!=1->2:xmod2!=0->5:x=x*3+1(22)->6:assert 22 mod2==0->1:x!=1- >2:xmod2==0->3:x=x/2(11)->1:x!=1->2:xmod2!=0->5:x=x*3+1(34)->6:assert 34 mod2==0->1:x!=1->2:xmod2==0->3:x=x/2(17)->1:x!=1->2:xmod2!=0-> 5:x=x*3+1(52)->6:assert 52 mod2==0 …

19 From Set of Traces to Set of States 19 1:x!=1->2:xmod2!=0->5:x=x*3+1(10)->6:assert 10 mod2==0 1:x!=1->2:xmod2!=0->5:x=x*3+1(10)->6:assert 10 mod2==0->1:x!=1- >2:xmod2==0->3:x=x/2(5)->1:x!=1->2:xmod2!=0->5:x=x*3+1(16)-> 6:assert 16 mod2==0 1:x!=1->2:xmod2!=0->5:x=x*3+1(16)->6:assert 16 mod2==0 1:x!=1->2:xmod2!=0->5:x=x*3+1(22)->6:assert 22 mod2==0 1:x!=1->2:xmod2!=0->5:x=x*3+1(22)->6:assert 22 mod2==0->1:x!=1- >2:xmod2==0->3:x=x/2(11)->1:x!=1->2:xmod2!=0->5:x=x*3+1(34)-> 6:assert 34 mod2==0 1:x!=1->2:xmod2!=0->5:x=x*3+1(22)->6:assert 22 mod2==0->1:x!=1- >2:xmod2==0->3:x=x/2(11)->1:x!=1->2:xmod2!=0->5:x=x*3+1(34)->6:assert 34 mod2==0->1:x!=1->2:xmod2==0->3:x=x/2(17)->1:x!=1->2:xmod2!=0-> 5:x=x*3+1(52)->6:assert 52 mod2==0 … 6: x  10 6: x  16 6: x  22 6: x  34 6: x  52

20 Set of States  still unbounded  can abstract it using parity  cannot compute it through the concrete semantics  need to compute directly in the abstract 20 6: x  10 6: x  16 6: x  22 6: x  34 6: x  52 …

21 Parity Abstraction  concrete state: Var  Z  abstract state: Var  { ,E,O,  }  even / odd   non-initialized (bottom)   either even or odd (top)  Transformers:   x = x / 2  # (  ) = ?   x = x*3 + 1  # (  ) =  x is even, then  [x  O]  x is odd, then  [x  E] 21 E O  

22 Parity Abstraction 22 1: while (x !=1 ) do { // x  {E, O} 2: if (x % 2) == 0 { // x  {E} 3: x := x / 2; // x  {E,O} 4: } else { // x  {O} 5: x := x * 3 + 1; // x  {E} 6: assert (x %2 ==0); // x  {E} 7: } 8: }

23 Where does the Galois Connection help me?  Establish Galois connection  Show each individual transformer is sound  Show each individual transformer is monotonic  soundness of the analysis is guaranteed  global soundness theorem 23

24 Sign Abstraction 24  - +  0  concrete state: Var  Z  abstract state: Var  { ,0,+,-,  }  zero, positive, negative   non-initialized (bottom)   (top)

25 Example ++  ++  xyi main(int i) { int x=3,y=1; do { y = y + 1; } while(--i > 0) assert 0 < x + y } ++  ++    xyi ++  ++  ++  ++  ++  ++  25

26 Sign and Parity 26 (slide from Patrick Cousot) (-,E) ( ,E) ( 0,E) (,)(,) (+,E) ( ,  ) (-,O) ( ,O) (+,O) (+,  )(-,  )

27 Constant Abstraction 27 … …   --  0 1   State = (Var  Z  )  No information Variable not a constant (infinite lattice, finite height)

28 Constant Abstraction  L = ( (Var  Z  ) ,  )   1   2 iff  v:  1(v)   2(v)   ordering in the Z   lattice  Examples:  [x  , y  42, z   ]  [x  , y  42, z  73]  [x  , y  42, z  73]  [x  , y  42, z   ] 28

29 29 Constant Abstraction A: AExp  (State  Z) A  a1 op a2  = A  a1  op A  a2  A  x  =  (x) otherwise  If  =  A  n  = n otherwise  If  =  StmtMeaning [x := a] lab  If  =   [x  A  a  ] otherwise [skip] lab  [b] lab 

30 Example 30 [x :=42] 1 ; [y:=73] 2 ; (if [?] 3 then [z:=x+y] 4 else [z:=12] 5 [w:=z] 6 ); [X  , y  , z  , w   ] [X  42, y  , z  , w   ] [X  42, y  73, z  , w   ] [X  42, y  73, z  115, w   ] [X  42, y  73, z  12, w   ] [X  42, y  73, z  12, w  12] [X  42, y  73, z  , w  12] ……   --  012115 ……

31 Constant Propagation is Non Distributive  Consider the transformer f =  [y=x*x]  #  Consider two states  1,  2   1(x) = 1   2(x) = -1 31 (  1   2)(x) =  f(  1   2) maps y to  f(  1) maps y to 1 f(  2) maps y to 1 f(  1)  f(  2) maps y to 1 f(  1   2)  f(  1)  f(  2)

32 Intervals Abstraction 32 023 1 2 3 4 5 4 6 x y 1 y  [3,6] x  [1,4]

33 Interval Lattice  [0,0][-1,-1][-2,-2] [-2,-1] [-2,0] [1,1][2,2] [-1,0] [0,1][1,2] … [-1,1][0,2] [-2,1][-1,2] [-2,2] …… [2,  ] … [1,  ] [0,  ] [-1,  ] [-2,  ] … … … … [- ,  ] … … [- ,-2] … [- ,-1] [- ,0] [- ,1] [- ,2] … … … … 33 (infinite lattice, infinite height)

34 Example 34 int x = 0; if (?) x++; x  [0,0] x   x  [0,1] x  [0,2] x=0 if x++ if x++ exit x  [0,0] x  [1,1] x  [1,2] [a1,a2]  [b1,b2] = [min(a1,b1), max(a2,b2)]

35 Example 35 int x = 0; while(?) x++; [0,0] [0,1][0,2][0,3][0,4] [0,5] … What now?

36 Widening 36

37 An Algorithm for Computing Over-Approximation of lfp lfp(f)   n  N f n (  ) l =  while f(l)  l do l = f(l)  guarantee convergence even in infinite-height lattices with widening 37 above the lfp use widening instead of join

38 Widening  useful also in the finite case 38 int x = 0; while(x<10000) x++;

39 Cartesian vs. Relational Abstractions  cartesian (also called independent-attribute) abstraction abstracts each variable separately  set of points abstracted by a point of sets { (1,2), (3,4), (5,6) } => ({1,3,5},(2,4,6)} losing relationship between variables  e.g., intervals, constants, signs, parity  relational abstraction tracks relationships between variables 39

40 Octagon Abstraction  abstract state is an intersection of linear inequalities of the form  x  y  c 40  captures relationships common in programs (array access)

41 Example proc incr (x:int) returns (y:int) begin y = x+1; end var i:int; begin i = 0; while (i<=10) do i = incr(i); done; end 41

42 Result with Octagon proc incr (x : int) returns (y : int) { /* [|x>=0; -x+10>=0|] */ y = x + 1; /* [|x>=0; -x+10>=0; -x+y-1>=0; x+y-1>=0; y-1>=0; -x-y+21>=0;x-y+1>=0; -y+11>=0|] */ } begin /* top */ i = 0; /* [|i>=0; -i+11>=0|] */ while i <= 10 do /* [|i>=0; -i+10>=0|] */ i = incr(i); /* [|i-1>=0; -i+11>=0|] */ done; /* [|i-11>=0; -i+11>=0|] */ end 42

43 Polyhedral Abstraction  abstract state is an intersection of linear inequalities of the form a 1 x 2 +a 2 x 2 +…a n x n  c  represent a set of points by their convex hull 43 (image from http://www.cs.sunysb.edu/~algorith/files/convex-hull.shtml)http://www.cs.sunysb.edu/~algorith/files/convex-hull.shtml

44 McCarthy 91 function proc MC (n : int) returns (r : int) var t1 : int, t2 : int; begin /* top */ if n > 100 then /* [|n-101>=0|] */ r = n - 10; /* [|-n+r+10=0; n-101>=0|] */ else /* [|-n+100>=0|] */ t1 = n + 11; /* [|-n+t1-11=0; -n+100>=0|] */ t2 = MC(t1); /* [|-n+t1-11=0; -n+100>=0; -n+t2-1>=0; t2-91>=0|] */ r = MC(t2); /* [|-n+t1-11=0; -n+100>=0; -n+t2-1>=0; t2-91>=0; r-t2+10>=0; r-91>=0|] */ endif; /* [|-n+r+10>=0; r-91>=0|] */ end var a : int, b : int; begin /* top */ b = MC(a); /* [|-a+b+10>=0; b-91>=0|] */ end if (n>=101) then n-10 else 91 44

45 Operations on Polyhedra 45

46 Recap  Cartesian  parity – finite  signs – finite  constant – infinite lattice, finite height  interval – infinite height  Relational  octagon – infinite  polyhedra – infinite 46

47 Back to a bit of dataflow analysis… 47

48 Recap  Represent properties of a program using a lattice (L, , , , ,  )  A continuous function f: L  L  Monotone function when L satisfies ACC implies continuous  Kleene’s fixedpoint theorem  lfp(f) =  n  N f n (  )  A constructive method for computing the lfp 48

49 Some required notation 49 blocks : Stmt  P(Blocks) blocks([x := a] lab ) = {[x := a] lab } blocks([skip] lab ) = {[skip] lab } blocks(S1; S2) = blocks(S1)  blocks(S2) blocks(if [b] lab then S1 else S2) = {[b] lab }  blocks(S1)  blocks(S2) blocks(while [b] lab do S) = {[b] lab }  blocks(S) FV: (BExp  AExp)  Var Variables used in an expression AExp(a) = all non-unit expressions in the arithmetic expression a similarly AExp(b) for a boolean expression b

50 Available Expressions Analysis 50 For each program point, which expressions must have already been computed, and not later modified, on all paths to the program point [x := a+b] 1 ; [y := a*b] 2 ; while [y > a+b] 3 ( [a := a + 1] 4 ; [x := a + b] 5 ) (a+b) always available at label 3

51 Available Expressions Analysis  Property space  in AE, out AE : Lab   (AExp)  Mapping a label to set of arithmetic expressions available at that label  Dataflow equations  Flow equations – how to join incoming dataflow facts  Effect equations - given an input set of expressions S, what is the effect of a statement 51

52  in AE (lab) =   when lab is the initial label   { out AE (lab’) | lab’  pred(lab) } otherwise  out AE (lab) = … 52 Blockout (lab) [x := a] lab in(lab) \ { a’  AExp | x  FV(a’) } U { a’  AExp(a) | x  FV(a’) } [skip] lab in(lab) [b] lab in(lab) U AExp(b) Available Expressions Analysis From now on going to drop the AE subscript when clear from context

53 Transfer Functions 1: x = a+b 2: y:=a*b 3: y > a+b 4: a=a+1 5: x=a+b out(1) = in(1) U { a+b } out(2) = in(2) U { a*b } in(1) =  in(2) = out(1) in(3) = out(2)  out(5) in(4) = out(3) in(5) = out(4) out(4) = in(4) \ { a+b,a*b,a+1 } out(5) = in(5) U { a+b } out(3) = in(3) U { a+ b } 53 [x := a+b] 1 ; [y := a*b] 2 ; while [y > a+b] 3 ( [a := a + 1] 4 ; [x := a + b] 5 )

54 Solution 1: x = a+b 2: y:=a*b 3: y > a+b 4: a=a+1 5: x=a+b in(2) = out(1) = { a + b } out(2) = { a+b, a*b } out(4) =  out(5) = { a+b } in(4) = out(3) = { a+ b } 54 in(1) =  in(3) = { a + b }

55 Kill/Gen 55 Blockout (lab) [x := a] lab in(lab) \ { a’  AExp | x  FV(a’) } U { a’  AExp(a) | x  FV(a’) } [skip] lab in(lab) [b] lab in(lab) U AExp(b) Blockkillgen [x := a] lab { a’  AExp | x  FV(a’) } { a’  AExp(a) | x  FV(a’) } [skip] lab  [b] lab  AExp(b) out(lab) = in(lab) \ kill(B lab ) U gen(B lab ) B lab = block at label lab

56 Why solution with largest sets? 56 1: z = x+y 2: true 3: skip out(1) = in(1) U { x+y } in(1) =  in(2) = out(1)  out(3) in(3) = out(2) out(3) = in(3) out(2) = in(2) [z := x+y] 1 ; while [true] 2 ( [skip] 3 ; ) in(1) =  After simplification: in(2) = in(2)  { x+y } Solutions: {x+y} or  in(2) = out(1)  out(3) in(3) = out(2)

57 Reaching Definitions Revisited 57 Blockout (lab) [x := a] lab in(lab) \ { (x,l) | l  Lab } U { (x,lab) } [skip] lab in(lab) [b] lab in(lab) Blockkillgen [x := a] lab { (x,l) | l  Lab }{ (x,lab) } [skip] lab  [b] lab  For each program point, which assignments may have been made and not overwritten, when program execution reaches this point along some path.

58 Why solution with smallest sets? 58 1: z = x+y 2: true 3: skip out(1) = ( in(1) \ { (z,?) } ) U { (z,1) } in(1) = { (x,?),(y,?),(z,?) } in(2) = out(1) U out(3) in(3) = out(2) out(3) = in(3) out(2) = in(2) [z := x+y] 1 ; while [true] 2 ( [skip] 3 ; ) in(1) = { (x,?),(y,?),(z,?) } After simplification: in(2) = in(2) U { (x,?),(y,?),(z,1) } Many solutions: any superset of { (x,?),(y,?),(z,1) } in(2) = out(1) U out(3) in(3) = out(2)

59 Live Variables 59 For each program point, which variables may be live at the exit from the point. [ x :=2] 1 ; [y:=4] 2 ; [x:=1] 3 ; (if [y>x] 4 then [z:=y] 5 else [z:=y*y] 6 ); [x:=z] 7

60 [ x :=2] 1 ; [y:=4] 2 ; [x:=1] 3 ; (if [y>x] 4 then [z:=y] 5 else [z:=y*y] 6 ); [x:=z] 7 Live Variables 1: x := 2 2: y:=4 4: y > x 5: z := y 7: x := z 6: z = y*y 3: x:=1

61 1: x := 2 2: y:=4 4: y > x 5: z := y 7: x := z 61 [ x :=2] 1 ; [y:=4] 2 ; [x:=1] 3 ; (if [y>x] 4 then [z:=y] 5 else [z:=y*y] 6 ); [x:=z] 7 6: z = y*y Live Variables Blockkillgen [x := a] lab { x }{ FV(a) } [skip] lab  [b] lab  FV(b) 3: x:=1

62 1: x := 2 2: y:=4 4: y > x 5: z := y 7: x := z 62 [ x :=2] 1 ; [y:=4] 2 ; [x:=1] 3 ; (if [y>x] 4 then [z:=y] 5 else [z:=y*y] 6 ); [x:=z] 7 6: z = y*y Live Variables: solution Blockkillgen [x := a] lab { x }{ FV(a) } [skip] lab  [b] lab  FV(b) 3: x:=1 in(1) =  out(1) = in(2) =  out(2) = in(3) = { y } out(3) = in(4) = { x,y } in(7) = { z } out(7) =  out(6) = { z } in(6) = { y } out(5) = { z } in(5) = { y } in(4) = { x,y } out(4) = { y }

63 Why solution with smallest set? 63 1: x>1 2: skip out(1) = in(2) U in(3) out(2) = in(1) out(3) =  in(2) = out(2) while [x>1] 1 ( [skip] 2 ; ) [x := x+1] 3 ; After simplification: in(1) = in(1) U { x } Many solutions: any superset of { x } 3: x := x + 1 in(3) = { x } in(1) = out(1)  { x }

64 Monotone Frameworks   is  or   CFG edges go either forward or backwards  Entry labels are either initial program labels or final program labels (when going backwards)  Initial is an initial state (or final when going backwards)  f lab is the transfer function associated with the blocks B lab 64 In(lab) =  { out(lab’) | (lab’,lab)  CFG edges } Initialwhen lab  Entry labels otherwise out(lab) = f lab (in(lab))

65 Forward vs. Backward Analyses 65 1: x := 2 2: y:=4 4: y > x 5: z := y 7: x := z 6: z = y*y 1: x := 2 2: y:=4 4: y > x 5: z := y 7: x := z 6: z = y*y {(x,1), (y,?), (z,?) } { (x,?), (y,?), (z,?) } {(x,1), (y,2), (z,?) }  { z } { y }

66 Must vs. May Analyses  When  is  - must analysis  Want largest sets the solves the equation system  Properties hold on all paths reaching a label (exiting a label, for backwards)  When  is  - may analysis  Want smallest sets that solve the equation system  Properties hold at least on one path reaching a label (existing a label, for backwards) 66

67 Example: Reaching Definition  L =  (Var×Lab) is partially ordered by    is   L satisfies the Ascending Chain Condition because Var × Lab is finite (for a given program) 67

68 Example: Available Expressions  L =  (AExp) is partially ordered by    is   L satisfies the Ascending Chain Condition because AExp is finite (for a given program) 68

69 Analyses Summary 69 Reaching Definitions Available Expressions Live Variables L  (Var x Lab)  (AExp)  (Var)    AExp  Initial{ (x,?) | x  Var}  Entry labels{ init } final DirectionForward Backward F{ f: L  L |  k,g : f(val) = (val \ k) U g } f lab f lab (val) = (val \ kill)  gen

70 Analyses as Monotone Frameworks  Property space  Powerset  Clearly a complete lattice  Transformers  Kill/gen form  Monotone functions (let’s show it) 70

71 Monotonicity of Kill/Gen transformers  Have to show that x  x’ implies f(x)  f(x’)  Assume x  x’, then for kill set k and gen set g (x \ k) U g  (x’ \ k) U g  Technically, since we want to show it for all functions in F, we also have to show that the set is closed under function composition 71

72 Distributivity of Kill/Gen transformers  Have to show that f(x  y)  f(x)  f(y)  f(x  y) = ((x  y) \ k) U g = ((x \ k)  (y \ k)) U g = (((x \ k) U g)  ((y \ k) U g)) = f(x)  f(y)  Used distributivity of  and U  Works regardless of whether  is U or  72

73 73


Download ppt "Lecture 04 – Numerical Abstractions Eran Yahav 1."

Similar presentations


Ads by Google