Lecture 04 – Numerical Abstractions Eran Yahav 1.

Slides:



Advertisements
Similar presentations
Continuing Abstract Interpretation We have seen: 1.How to compile abstract syntax trees into control-flow graphs 2.Lattices, as structures that describe.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Lecture 15 – Dataflow Analysis Eran Yahav 1
Lecture 02 – Structural Operational Semantics (SOS) Eran Yahav 1.
Lecture 16 – Dataflow Analysis Eran Yahav 1 Reference: Dragon 9, 12.
Lecture 03 – Background + intro AI + dataflow and connection to AI Eran Yahav 1.
Foundations of Data-Flow Analysis. Basic Questions Under what circumstances is the iterative algorithm used in the data-flow analysis correct? How precise.
1 Basic abstract interpretation theory. 2 The general idea §a semantics l any definition style, from a denotational definition to a detailed interpreter.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Worklist algorithm Initialize all d i to the empty set Store all nodes onto a worklist while worklist is not empty: –remove node n from worklist –apply.
Programming Language Semantics Denotational Semantics Chapter 5 Based on a lecture by Martin Abadi.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
1 Iterative Program Analysis Part I Mooly Sagiv Tel Aviv University Textbook: Principles of Program.
Data Flow Analysis Compiler Design Nov. 3, 2005.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
1 Iterative Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
Data Flow Analysis Compiler Design Nov. 8, 2005.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs, Data-flow Analysis Data-flow Frameworks --- today’s.
1 Program Analysis Systematic Domain Design Mooly Sagiv Tel Aviv University Textbook: Principles.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Programming Language Semantics Denotational Semantics Chapter 5 Part III Based on a lecture by Martin Abadi.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Claus Brabrand, ITU, Denmark DATA-FLOW ANALYSISMar 25, 2009 Static Analysis: Data-Flow Analysis II Claus Brabrand IT University of Copenhagen (
Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Claus Brabrand ((( ))) Associate Professor, Ph.D. ((( Programming, Logic, and.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
1 Tentative Schedule u Today: Theory of abstract interpretation u May 5 Procedures u May 15, Orna Grumberg u May 12 Yom Hatzamaut u May.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Constant Propagation. The constant propagation framework is different from all the data-flow problems discussed so far, in that It has an unbounded set.
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
MIT Foundations of Dataflow Analysis Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Solving fixpoint equations
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 11: Abstract Interpretation III Roman Manevich Ben-Gurion University.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 14: Numerical Abstractions Roman Manevich Ben-Gurion University.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 14: Numerical Abstractions Roman Manevich Ben-Gurion University.
1 Bisimulations as a Technique for State Space Reductions.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 12: Abstract Interpretation IV Roman Manevich Ben-Gurion University.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 13: Abstract Interpretation V Roman Manevich Ben-Gurion University.
Compiler Principles Fall Compiler Principles Lecture 11: Loop Optimizations Roman Manevich Ben-Gurion University.
Program Analysis and Verification
Program Analysis and Verification
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
1 Numeric Abstract Domains Mooly Sagiv Tel Aviv University Adapted from Antoine Mine.
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Program Analysis and Verification Noam Rinetzky Lecture 8: Abstract Interpretation 1 Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav.
Chaotic Iterations Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Chaotic Iterations Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Iterative Dataflow Problems Taken largely from notes of Alex Aiken (UC Berkeley) and Martin Rinard (MIT) Dataflow information used in optimization Several.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 8: Static Analysis II Roman Manevich Ben-Gurion University.
Dataflow Analysis CS What I s Dataflow Analysis? Static analysis reasoning about flow of data in program Different kinds of data: constants, variables,
Compiler Principles Fall Compiler Principles Lecture 9: Dataflow & Optimizations 2 Roman Manevich Ben-Gurion University of the Negev.
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
Spring 2017 Program Analysis and Verification
Spring 2016 Program Analysis and Verification
Textbook: Principles of Program Analysis
Spring 2016 Program Analysis and Verification
Iterative Program Analysis Abstract Interpretation
Program Analysis and Verification
Flow Analysis Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM.
Data Flow Analysis Compiler Design
Presentation transcript:

Lecture 04 – Numerical Abstractions Eran Yahav 1

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

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

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

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

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

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))?

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

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

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

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

“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)

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: }

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

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)

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

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

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 …

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

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 …

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  

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: }

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

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

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

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

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

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 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 

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] ……   --  ……

Constant Propagation is Non Distributive  Consider the transformer f =  [y=x*x]  #  Consider two states  1,  2   1(x) = 1   2(x) = (  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)

Intervals Abstraction x y 1 y  [3,6] x  [1,4]

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)

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)]

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

Widening 36

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

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

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

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

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

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

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

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

Operations on Polyhedra 45

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

Back to a bit of dataflow analysis… 47

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

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

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

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

 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

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 )

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 }

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

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)

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.

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)

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

[ 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

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

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 }

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 }

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))

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 }

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

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

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

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

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

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

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