Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Claus Brabrand ((( ))) Associate Professor, Ph.D. ((( Programming, Logic, and.

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

Claus Brabrand, UFPE, Brazil Aug 11, 2010DATA-FLOW ANALYSIS Claus Brabrand ((( ))) Associate Professor, Ph.D. ((( Programming, Logic, and.
Tutorial on Widening (and Narrowing) Hongseok Yang Seoul National University.
A Deeper Look at Data-flow Analysis Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University.
Claus Brabrand, ITU, Denmark STATIC ANALYSIS (DATA-FLOW ANALYSIS)Apr 09, 2008 Static Analysis: Data-Flow Analysis IV Claus Brabrand ITU - IT University.
Foundations of Data-Flow Analysis. Basic Questions Under what circumstances is the iterative algorithm used in the data-flow analysis correct? How precise.
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.
Claus Brabrand, ITU, Denmark Mar 23, 2009STATIC ANALYSIS (DATA-FLOW ANALYSIS) Static Analysis: Data-Flow Analysis I Claus Brabrand IT University of Copenhagen.
CS 536 Spring Global Optimizations Lecture 23.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Iterative Program Analysis Part I Mooly Sagiv Tel Aviv University Textbook: Principles of Program.
Programming Language Semantics Denotational Semantics Chapter 5 Part II.
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)
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.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Interprocedural Analysis Noam Rinetzky Mooly Sagiv Tel Aviv University Textbook Chapter 2.5.
¹ -Calculus Based on: “Model Checking”, E. Clarke and O. Grumberg (ch. 6, 7) “Symbolic Model Checking: 10^20 States and Beyond”, Burch, Clark, et al “Introduction.
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.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Prof. Aiken CS 294 Lecture 21 Abstract Interpretation Part 2.
1 CS 201 Compiler Construction Lecture 4 Data Flow Framework.
From last lecture We want to find a fixed point of F, that is to say a map m such that m = F(m) Define ?, which is ? lifted to be a map: ? = e. ? Compute.
Programming Language Semantics Denotational Semantics Chapter 5 Part III Based on a lecture by Martin Abadi.
Claus Brabrand, ITU, Denmark DATA-FLOW ANALYSISMar 25, 2009 Static Analysis: Data-Flow Analysis II Claus Brabrand IT University of Copenhagen (
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis: Data-flow frameworks –Classic.
Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Claus Brabrand ((( ))) Associate Professor, Ph.D. ((( Programming, Logic, and.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Partially Ordered Sets (POSets)
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
Set theory Sets: Powerful tool in computer science to solve real world problems. A set is a collection of distinct objects called elements. Traditionally,
Chapter 2 Languages.
Data Flow Analysis. 2 Source code parsed to produce AST AST transformed to CFG Data flow analysis operates on control flow graph (and other intermediate.
MIT Foundations of Dataflow Analysis Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Solving fixpoint equations
Compiler Construction Lecture 16 Data-Flow Analysis.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
Project 2 due … Project 2 due … Project 2 Project 2.
Set, Combinatorics, Probability & Number Theory Mathematical Structures for Computer Science Chapter 3 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Set,
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 9: Abstract Interpretation I Roman Manevich Ben-Gurion University.
CS 267: Automated Verification Lecture 3: Fixpoints and Temporal Properties Instructor: Tevfik Bultan.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 12: Abstract Interpretation IV Roman Manevich Ben-Gurion University.
Supercompilation and Normalisation by Evaluation Gavin Mendel-Gleason & Geoff Hamilton Dublin City University.
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.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
Chap. 7 Relations: The Second Time Around
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis: Data-flow frameworks –Classic.
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Iterative Program Analysis Part II Mathematical Background Mooly Sagiv Tel Aviv University
Chaotic Iterations Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Chaotic Iterations Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
DFA foundations Simone Campanoni
How CTL model checking works
Another example: constant prop
Discrete Math (2) Haiming Chen Associate Professor, PhD
Concurrent Models of Computation
Data Flow Analysis Compiler Design
Lecture 20: Dataflow Analysis Frameworks 11 Mar 02
Class 33: Making Recursion M.C. Escher, Ascending and Descending
Lecture 10: Fixed Points ad Infinitum M.C. Escher, Moebius Ants
Formal methods in software development
Static Analysis: Data-Flow Analysis I
Presentation transcript:

Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Claus Brabrand ((( ))) Associate Professor, Ph.D. ((( Programming, Logic, and Semantics ))) IT University of Copenhagen A NALYSIS F LOW - D ATA [ HOW TO ANALYZE LANGUAGES AUTOMATICALLY ]

[ 2 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Agenda Relations: Crossproducts, powersets, and relations Lattices: Partial-Orders, least-upper-bound, and lattices Monotone Functions: Monotone Functions and Transfer Functions Fixed Points: Fixed Points and Solving Recursive Equations Putting it all together…: Example revisited

[ 3 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS

[ 4 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Quick recap: int x = 1; int y = 3; x = x+2;x y; print(x,y); E. E[x  1 ] E. E[y  3 ] E. E[x  E(x)  2 ] E. E[x  E(y), y  E(x)] [, ]  ENV L [, ] x y We (only) need 3 things:   A control-flow graph   A lattice   Transfer functions E. E

[ 5 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Example: Least upper bound Analyses use ’ ’ to combine information (at confluence points): x = 2;x = 0; truefalse... [x =, y = ]

Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Lattice

[ 7 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Lattice A Lattice is a Partial Order (L, ) …where ’ S’ exists for all subsets S  L Examples (of lattices): Non-Examples (of non-lattices): Simplification: if we restrict to finite height lattices (which are the ones used in practice); then ’ ’ only has to exist for each pair of elements (i.e., not for all subsets of elements) Why? ”We must be able to combine information”

[ 8 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Power-Lattices Powerset Lattices (as Hasse Diagrams): P ({x,y}) = { Ø, {x}, {y}, {x,y} } …ordered under ’ ’ = ’  ’ (i.e., subset inclusion): P ({ x+1, y*2 }) P ({ a, b, c }) P ({ x, y })

[ 9 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Agenda Relations: Crossproducts, powersets, and relations Lattices: Partial-Orders, least-upper-bound, and lattices Monotone Functions: Monotone Functions and Transfer Functions Fixed Points: Fixed Points and Solving Recursive Equations Putting it all together…: Example revisited

[ 10 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Transfer Functions Constant Transfer functions: b = true; f () = Analysing value-of-’b’ Analysing sign-of-’x’ x = 7; f () = transfer function

[ 11 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Transfer Functions Unary Transfer functions: b = !b; Analysing value-of-’b’ Analysing sign-of-’x’ x = -x; ! - transfer function Exercise: what's this transfer function?

[ 12 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Transfer Functions Binary Transfer functions: b = b && c Analysing value-of-’b’ Analysing sign-of-’x’ x = x + n; transfer function (let's just assume ' c ' is false) assume ' c ' is false) (let's just assume ' n ' is 7) assume ' n ' is 7) Exercise: what's this transfer function?

Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Environments Runtime: Var  Val Analysis: Var  L (i.e., abstract values)

[ 14 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Say lattice L analyses ”constantness” of one single value: We need environments for analyzing: (tracking both ’v’ and ’w’) : i.e. we need lattice ’L  L’: Note: We need Transfer Functions on Environments!  = … w = v * v; (v =, w = ) ENV = ENV’ = L  L  {x,y}  L L |VAR|  VAR  L

[ 15 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Crossproduct Lattice Environment Lattice: ’L  L’:  = NB: if L is a lattice, then L  L is always a lattice

[ 16 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Exercise Calculate crossproduct lattice:  =

[ 17 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS (, ) Transfer Functions on Environment Lattices Constant Transfer functions: x = true; n = 7;  for ’x’for ’y’  for ’n’for ’m’ (, ) x y E. E[x = ] E. E[n = ] n m ENV transfer function (, )

[ 18 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS (, ) Transfer Functions on Environment Lattices Unary Transfer functions: x = !y; n = -m;  for ’x’for ’y’  for ’n’for ’m’ (, ) x y E. E[x = f not (E(y))] E. E[n = f - (E(m))] n m ENV transfer function (, ) EXERCISE: (what's the transfer func?)

[ 19 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Transfer Functions on Environment Lattices Binary Transfer functions: x = x && y; n = n + m;  (, ) for ’x’for ’y’  for ’n’for ’m’ (, ) x y E. E[x = E(x) L E(y)] E. E[x = E(n)  L E(m)] n m ENV transfer function &&

Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Monotonicity Monotone Transfer Functions

[ 21 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Monotone Functions Monotone function (on a lattice L): Note: this is not saying that ’ f ’ is ascending: Examples: f : L  L  x,y  L: x y  f(x) f(y)  x  L: x f(x) All the transfer functions you have seen were monotone! :-) f () = …on lattice: !

[ 22 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS More Examples: Exercise: Check monotonicity of ' f ' below: 1) f(X) = {a,b} 2) f(X) = X  {a} 3) f(X) = {a,b} \ X 4) f(X) = X C  x,y  L: x y  f(x) f(y) Monotonicity: …on lattice: P ({a,b,c}) P ({a,b,c}) = 2 {a,b,c} = 2 {a,b,c} Monotone Func’s (cont’d) …on lattice:  E. E[x = ] E. E[x = f not (E(y))] E. E[x = E(x) L E(y)] &&

[ 23 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Agenda Relations: Crossproducts, powersets, and relations Lattices: Partial-Orders, least-upper-bound, and lattices Monotone Functions: Monotone Functions and Transfer Functions Fixed Points: Fixed Points and Solving Recursive Equations Putting it all together…: Example revisited

[ 24 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Fixed-Points A fixed-point for a function …is an element such that: Example: Function: …over P ({ a, b, c }) ' f ' has many fixed-points: { c } { a, c } … { a, b, c } f : L  L l  L l = f( l ) f(X) = X  {c} LEAST fixed point In fact, anything that includes ' c '

[ 25 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Another Example Another Example: x = 1; x = x+1; fixed- point! not a fixed- point! a = b = f x=1 (a) c = b d d = f x=x+1 (c) a=a= b=b= d=d= Recursive equations: fixed- point! c=c= LEAST

[ 26 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Fixed-Point Theorem! If: ’ f ’ is a monotone function: …over a lattice L (with finite height): Then: ’ f ’ is guaranteed to have a unique least fixed-point …which is computable as: Intuition: f : L  L fix(f) = f i ( ) i≥0 f( ) f(f( ))...until equality Proof is quite simple [cf. Notes, p.13 top]

[ 27 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Now you can… Solve ANY recursive equations (involving monotone functions over lattices): x = f(x,y,z) y = g(x,y,z) z = h(x,y,z) Which means that you can solve any recursive data-flow analysis equation! :-)

[ 28 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Agenda Relations: Crossproducts and relations Lattices: Partial-Orders, least-upper-bound, and lattices Monotone Functions: Monotone Functions and Transfer Functions Fixed Points: Fixed Points and Solving Recursive Equations Putting it all together…: Example revisited

[ 29 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS All you need is…: int x = 1; int y = 3; x = x+2;x y; print(x,y); E. E[x  1 ] E. E[y  3 ] E. E[x  E(x)  2 ] E. E[x  E(y), y  E(x)] [, ]  ENV L [, ] x y... We (only) need 3 things:   A control-flow graph   A lattice   Transfer functions int x = 1; int y = 3; if (...) { x = x+2; } else { x y; } print(x,y); Given program:

[ 30 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Solve Equations :-) One big lattice: E.g., (L |VAR| ) |PP| 1 big abstract value vector: [,,..., ]  (L |VAR| ) |PP| 1 big transfer function: F : (L |VAR| ) |PP|  (L |VAR| ) |PP| Compute fixed-point (simply): Start with bottom value vector ( ) Iterate transfer function ‘F’ (until nothing changes) Done; print out (or use) solution…! :-) [, ] (L |VAR| ) |PP|

[ 31 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Agenda Relations: Crossproducts, powersets, and relations Lattices: Partial-Orders, least-upper-bound, and lattices Monotone Functions: Monotone Functions and Transfer Functions Fixed Points: Fixed Points and Solving Recursive Equations Putting it all together…: Example revisited

[ 32 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS x = 0; x = x+1; output x;  a = b = f x=0 (a) c = b d d = f x=x+1 (c) e = d The Entire Process :-) 3. Recursive equations: x = 0; do { x = x+1; } while (…); output x; Program: 1. Control-flow graph: T  4. one ”big” transfer function: T((a,b,c,d,e)) = (,f x=0 (a),b d,f x=x+1 (c),d) |VAR|*|PP| = 1*5 = 5 …over a ”big” power-lattice: T T T 0 ( ) T 1 ( ) T 2 ( ) T 3 ( ) T a = b = d = c = e = T 5 ( ) T = LEAST FIXED POINT ANOTHER FIXED POINT 5. Solve rec. equations…: 2. Transfer functions: solution T 4 ( ) T f x=0 ( l ) = f x=x+1 ( l ) = l  L

[ 33 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Exercise: Repeat this process for program (of two vars): i.e., determine…: 1) Control-flow graph 2) Transfer functions 3) Recursive equations 4) One ”big” transfer function 5) Solve recursive equations :-) x = 1; y = 0; while (v>w) { x y; } y = y+1; …using lattice:

[ 34 ] Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Now, please: 3’ recap Please spend 3' on thinking about and writing down the main ideas and points from the lecture – now!: After 1 day After 1 week After 3 weeks After 2 weeks Immediately