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

Slides:



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

Lecture 24 MAS 714 Hartmut Klauck
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
Chair of Software Engineering From Program slicing to Abstract Interpretation Dr. Manuel Oriol.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
Claus Brabrand, ITU, Denmark Feb 26, 2008WHITE-BOX TESTING White-Box Testing Claus Brabrand [ ] ( “FÅP”: First-year Project Course, ITU,
Claus Brabrand, ITU, Denmark STATIC ANALYSIS (DATA-FLOW ANALYSIS)Apr 09, 2008 Static Analysis: Data-Flow Analysis IV Claus Brabrand ITU - IT University.
1 Introduction to Computability Theory Lecture15: Reductions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
Lecture 02 – Structural Operational Semantics (SOS) Eran Yahav 1.
CPSC 411, Fall 2008: Set 12 1 CPSC 411 Design and Analysis of Algorithms Set 12: Undecidability Prof. Jennifer Welch Fall 2008.
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.
1 Undecidability Andreas Klappenecker [based on slides by Prof. Welch]
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CS 536 Spring Global Optimizations Lecture 23.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
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 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING White-Box Testing Claus Brabrand [ ] ( “FÅP”: First-year Project Course, ITU,
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.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Administrative stuff Office hours: After class on Tuesday.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
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.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
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.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
1 First order theories. 2 Satisfiability The classic SAT problem: given a propositional formula , is  satisfiable ? Example:  Let x 1,x 2 be propositional.
UNIT II Decision Making And Branching Decision Making And Looping
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
The Java Programming Language
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Introduction to Java Java Translation Program Structure
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 12: Abstract Interpretation IV Roman Manevich Ben-Gurion University.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 First order theories (Chapter 1, Sections 1.4 – 1.5) From the slides for the book “Decision procedures” by D.Kroening and O.Strichman.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Types and Programming Languages
Compiler Principles Fall Compiler Principles Lecture 11: Loop Optimizations Roman Manevich Ben-Gurion University.
Application development with Java Lecture 6 Rina Zviel-Girshin.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Great Theoretical Ideas in Computer Science
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
Data Flow Analysis Compiler Design
Static Analysis: Data-Flow Analysis I
Presentation transcript:

Claus Brabrand, UFPE, Brazil Aug 04, 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 04, 2010DATA-FLOW ANALYSIS Abstract "Data-Flow Analysis": In this 3*3 hour mini course we will look at data-flow analysis. Rather than just look at the classical "monotone framework" analyses (which are usually synonymous with teaching data-flow analysis: reaching definitions, live variables, available expressions, and very busy expressions), we will instead take one step backwards and look at the general theory and practice behind these analyses. The idea is that you will then learn how to design your own customized data-flow analyses for automatically analyzing whatever aspects of programming languages you want to. (From this perspective, the monotone framework analyses are just special cases.) Keywords: - undecidability, approximation, control-flow graphs, partial-orders, lattices, transfer functions, monotonicity, [how to solve] fixed-point equations – and how all of these things combine to enable you to design data-flow analyses.

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

[ 4 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Agenda Introduction: Undecidability, Reduction, and Approximation Data-flow Analysis: Quick tour of everything & running example Control-Flow Graphs: Control-flow, data-flow, and confluence ”Science-Fiction Math”: (next monday) Lattice theory, monotonicity, and fixed-points Putting it all together…: (next wednesday) Example revisited

[ 5 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Notes on Static Analysis ”Lecture Notes on Static Analysis” by Michael I. Schwartzbach (Aarhus University) Chapter 1, 2, 4, 5, 6 (until p. 19) (Excl. ”pointers”) Claims to be "not overly formal", but the math involved can be quite challenging (at times)…

[ 6 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Quiz: Optimization? If you want a fast C-program, should you use: LOOP 1: LOOP 2 (optimized by programmer): i.e., ”array-version” or ”optimized pointer-version” ? b = a; for (i = 0; i < N; i++) { *b = *b * 2000; *b = *b / 10000; b++; } for (i = 0; i < N; i++) { a[i] = a[i] * 2000; a[i] = a[i] / 10000; } ? ? …or…

[ 7 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Results (of running the programs): Compilers use highly sophisticated static analyses for optimization! (you'll learn how to do this!!!) Recommendation: focus on writing clear code for people (and compilers) to understand! Answer:

[ 8 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Purpose (of Data-Flow Analysis): Gather information (on running behavior of program) ”  program points” Usage (of static analysis): Basis for subsequent…: Error Detection Data-Flow Analysis Static Analysis Optimization information  program points

[ 9 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Analyses for Error Detection Example Analyses: ”Symbol Checking”: Catch (dynamic) symbol errors ”Type Checking”: Catch (dynamic) type errors ”Initialized Variable Analysis”: Catch unintialized variables … … … …

[ 10 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Analyses for Optimization Example Analyses: ”Constant Propagation Analysis”: Precompute constants (e.g., replace ’ 5*x+z ’ by ’ 42 ’) ”Live Variables Analysis”: Dead-code elimination (e.g., get rid of unused variable ’ z ’) ”Available Expressions Analysis”: Avoid recomputing already computed exprs (cache results) … … … …

Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Conceptual Motivation Undecidability Reduction principle Approximation

[ 12 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Rice’s Theorem (1953) Examples: does program ’P’ always halt when run? is the value of integer variable ’x’ always positive? does variable ’x’ always have the same value? which variables can pointer ’p’ point to? does expression ’E’ always evaluate to true? what are the possible outputs of program ’P’? … “Any interesting problem about the runtime behavior of a program* is undecidable” -- Rice’s Theorem [paraphrased] (1953) “Any interesting problem about the runtime behavior of a program* is undecidable” -- Rice’s Theorem [paraphrased] (1953) *) written in a turing-complete language

[ 13 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Undecidability (self-referentiality) Consider "The Book-of-all-Books": This book contains the titles of all books that do not have a self-reference (i.e. don't contain their title inside) Finitely many books; i.e.: We can sit down & figure out whether to include or not... Q: What about "The Book-of-all-Books" ; Should it be included or not? "Self-referential paradox" (many guises): e.g. "This sentence is false" "The Bible" "War and P e ac e" "Programm i n g La nguages, An Interp.- B a s e d Approach"... The Book-of-all-Books

[ 14 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Termination Undecidable! Assume termination is decidable (in Java); i.e.  some program, halts: Program  bool Q: Does P 0 loop or terminate...?:) Hence: halts cannot exist! i.e., "Termination is undecidable" Program p 0 = read_program("P 0.java"); if (halts(p 0 )) loop(); else halt(); -- P 0.java -- bool halts(Program p) {... } *) for turing-complete languages

[ 15 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Rice’s Theorem (1953) Examples: does program ’P’ always halt? is the value of integer variable ’x’ always positive? does variable ’x’ always have the same value? which variables can pointer ’p’ point to? does expression ’E’ always evaluate to true? what are the possible outputs of program ’P’? … “Any interesting problem about the runtime behavior program* is undecidable” -- Rice’s Theorem [paraphrased] (1953) “Any interesting problem about the runtime behavior program* is undecidable” -- Rice’s Theorem [paraphrased] (1953) reduction *) written in a turing-complete language

[ 16 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Reduction: solve always-pos  solve halts 1)Assume ’x-is-always-pos(P)’ is decidable 2)Given P (here’s how we could solve ’halts(P)’): 3)Construct (veeeeery clever) reduction program R: 4)Run ”supposedly decidable” analysis: res = 5)Deduce from result: if (res) then P loops!; else P halts :-) 6)THUS: ’x-is-always-pos(P)’ must be undecidable! -- R.java -- int x = 1; P /* insert program P here :-) */ x = -1; x-is-always-positive(R)

[ 17 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Reduction Principle Reduction principle (in short): Example: Exercise: Carry out reduction + whole explanation for: ” which variables can pointer ’q’ point to?”  (P) undecidable  [solve  (P)  solve  (P)]  (P) undecidable ’halts(P)’ undecidable  [solve ’x-is-always-pos(P)’  solve ’halts(P)’] ’x-is-always-pos(P)’ undecidable reduction

[ 18 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Answer 1)Assume ’which-var-q-points-to(P)’ is decidable: 2)Given P (here’s how to (cleverly) decide halts(P)): 3)Construct (veeeeery clever) reduction program R: 4)Run ’which-var-q-points-to(R)’ = res 5)If (null  res) P halts! else; P loops! :-) 6)THUS: ’which-var-q-points-to(P)’ must be undecidable! ptr q = 0xffff; P /* insert program P (assume w/o 'q') */ q = null; -- R.java --

[ 19 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Undecidability Undecidability means that…: …no-one can decide this line (for all programs)! However(!)… halts loops ?

[ 20 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS “Side-Stepping Undecidability” Compilers use safe approximations (computed via ”static analyses”) such that: However, just because it’s undecidable, doesn’t mean there aren’t (good) approximations! Indeed, the whole area of static analysis works on “side-stepping undecidability”: ok error ok error Okay!Dunno? Error!

[ 21 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS “Side-Stepping Undecidability” Unsafe approximation: For testing it may be okay to ”abandon” safety and use unsafe approximations: However, just because it’s undecidable, doesn’t mean there aren’t (good) approximations! Indeed, the whole area of static analysis works on “side-stepping undecidability”: halts loops unsafe approximation Here are some programs for you to (manually) consider ! ok error

[ 22 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS “Slack” Undecidability means: “there’ll always be a slack”: However, still useful: (possible interpretations of “Dunno?”): Treat as error (i.e., reject program): “Sorry, program not accepted!” Treat as warning (i.e., warn programmer): “Here are some potential problems: …” halts loops Dunno?.. Okay!.

[ 23 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Soundness & Completeness Soundness: Analysis reports no errors  Really are no errors Completeness: Analysis reports an error  Really is an error ok error Sound analysis ok error Complete analysis …or alternative (equivalent) formulation, via ”contra-position”: P  Q  Q   P  Really no error(s)  Analysis reports no error(s) Really are error(s)  Analysis reports error(s)

[ 24 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Example: Type Checking Will this program have type error (when run)? Undecidable (because of reduction): Type error  evaluates to true void f() { var b; if ( ) { b = 42; } else { b = true; }... if (b)...; // error is b is '42' }

[ 25 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Example: Type Checking Hence, languages use static requirements: All variables must be declared And have only one type (throughout the program) This is (very) easy to check (i.e., "type-checking") void f() { bool b; // instead of ”var b;” if ( ) { b = 42; } else { b = true; } Static compiler error: Regardless of what evaluates to when run

[ 26 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Agenda Introduction: Undecidability, Reduction, and Approximation Data-flow Analysis: Quick tour & running example Control-Flow Graphs: Control-flow, data-flow, and confluence ”Science-Fiction Math”: Lattice theory, monotonicity, and fixed-points Putting it all together…: Example revisited

Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS 5’ Crash Course on Data-Flow Analysis Claus Brabrand ((( ))) Associate Professor, Ph.D. ((( Programming, Logic, and Semantics ))) IT University of Copenhagen

[ 28 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS IDEA: We (only) need 3 things: A control-flow graph A lattice Transfer functions Example: “(integer) constant propagation” “Simulate runtime execution at compile-time using abstract values” Data-Flow Analysis

[ 29 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS int x = 1; int y = 3; if (...) { x = x+2; } else { x y; } print(x,y);  int x = 1; int y = 3; x = x+2;x y; print(x,y); truefalse Control-flow graph... We (only) need 3 things:  A control-flow graph  A lattice  Transfer functions Given program:

[ 30 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS A Lattice Lattice L of abstract values of interest and their relationships (i.e. ordering “  ”): Induces least-upper-bound operator: for combining information ~ “I don't know (could be anything)” ~ “we haven’t analyzed yet” ·· ·· “top” “bottom” We (only) need 3 things:  A control-flow graph  A lattice  Transfer functions

[ 31 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Data-Flow Analysis 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

[ 32 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Agenda Introduction: Undecidability, Reduction, and Approximation Data-flow Analysis: Quick tour & running example Control-Flow Graphs: Control-flow, data-flow, and confluence ”Science-Fiction Math”: Lattice theory, monotonicity, and fixed-points Putting it all together…: Example revisited

[ 33 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Control Structures Control Structures: Statements (or Expr’s) that affect ”flow of control”: if-else : if : if ( Exp ) Stm 1 else Stm 2 if ( Exp ) Stm Stm 1 Exp truefalse Stm 2 confluence Stm Exp true false confluence The expression must be of type boolean; if it evaluates to true, the given statement is executed, otherwise not. The expression must be of type boolean; if it evaluates to true, Statement-1 is executed, otherwise Statement-2 is executed. [syntax] [semantics] [syntax] [semantics]

[ 34 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Control Structures (cont’d) while : for : while ( Exp ) Stm for (Exp 1 ; Exp 2 ; Exp 3 ) Stm Equivalent to: The expression must be of type boolean; if it evaluates to false, the given statement is skipped, otherwise it is executed and afterwards the expression is evaluated again. If it is still true, the statement is executed again. This is continued until the expression evaluates to false. { Exp1; while ( Exp2 ) { Stm Exp3; } } Stm Exp true false confluence Exp 1 ; Exp 2 true false Stm confluence Exp 3 ; [syntax] [semantics] [syntax] [semantics]

[ 35 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS int x = 1; int y = 3; if (a>b) { x = x+2; } else { x y; } print(x,y);  int x = 1; int y = 3; x = x+2;x y; print(x,y); truefalse Control-flow graph a>b Given program:

[ 36 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Exercise: Draw a Control-Flow Graph for: public static void main ( String[] args ) { int mi, ma; if (args.length == 0) System.out.println("No numbers"); else { mi = ma = Integer.parseInt(args[0]); for (int i=1; i < args.length; i++) { int obs = Integer.parseInt(args[i]); if (obs > ma) ma = obs; else if (mi < obs) mi = obs; } System.out.println(”min=" + mi + "," + "max=" + ma); }} if else for if else if

[ 37 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Control-Flow Graph CFG: int mi, ma; System.out.println ("No numbers"); args.length == 0 mi = ma = Integer.parseInt(args[0]); int i=1; i < args.length i++; System.out.println(”min=" + mi + "," + "max=" + ma); int obs = Integer.parseInt(args[i]); obs > ma ma = obs; mi < obs mi = obs; true false true false true false true false

[ 38 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Control Structures (cont’d 2 ) do-while : exercise ” ?: ”; ”conditional expression”: ” || ”; ”lazy disjunction” (aka., ”short-cut  ”): ” && ”; ”lazy conjunction” (aka., ”short-cut  ”): switch : switch ( Exp ) { Swb * } case Exp : Stm * break; do Stm while ( Exp ); default : Stm * break; Swb: Exp 1 ? Exp 2 : Exp 3 Exp 1 || Exp 2 Exp 1 && Exp 2

[ 39 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Control Structures (cont’d 3 ) try-catch-finally (exceptions): return / break / continue : ”method invocation”: e.g.; ”recursive method invocation”: e.g.; ”virtual dispatching”: e.g.; try Stm 1 catch ( Exp ) Stm 2 finally Stm 3 f(x) return ;return Exp ; break ;continue ; f(x)

[ 40 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Control Structures (cont’d 4 ) ”function pointers”: e.g.; ”higher-order functions”: e.g.; ”dynamic evaluation”: e.g.; Some constructions (and thus languages) require a separate control-flow analysis for determining control-flow in order to do data-flow analysis (*f)(x) f. x.(f x) eval( some-string-which-has-been-dynamically-computed )

[ 41 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Agenda Introduction: Undecidability, Reduction, and Approximation Data-flow Analysis: Quick tour & running example Control-Flow Graphs: Control-flow, data-flow, and confluence ”Science-Fiction Math”: Lattice theory, monotonicity, and fixed-points Putting it all together…: Example revisited

Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS MATH

[ 43 ] Claus Brabrand, UFPE, Brazil Aug 04, 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

[ 44 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Crossproduct: ’  ’ Crossproduct (binary operator on sets): Given sets: A = { 0, 1 } B = { true, false } A  B = { (0,true), (0,false), (1,true), (1,false) } i.e., creates sets of pairs Exercise: A  A = Z  Z = (A  A)  B = { (0,0), (0,1), (1,0), (1,1) } { (0,0), (0,1), (0,1), …, (1,0), (1,1), …, (42,87), … } { ((0,0),true), ((0,1),true), …, ((1,1),false) }

[ 45 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Powersets : ’ P (S)’ Powerset (unary operator on sets): Given set "S = { A, B }"; P (S) = { Ø, {A}, {B}, {A,B} = S } i.e., creates the set of all subsets (of the set) Note: Exercise: P (Z) = P (Z  Z) = If a set S has |S| elements; How many elements does P (S) have? X  S  X  P (S) { Ø, {0}, {1}, {2}, …, {0,1}, … {13,42,87}, … Z } ’ P (S)’ is (therefore) often written ’2 S ’ Answer: 2 |S| { Ø, {(0,0)}, {(1,1)}, …, {(0,0),(3,2),(4,9)}, … Z  Z }

[ 46 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Relations Example: “equals” relation: Signature: Relation is: Written as: as a short-hand for: … and as: as a short-hand for: Example: “less-than” relation: Signature: Relation is: Written as: as a short-hand for: … and as: as a short-hand for: 2  3 (2,3)  ‘=’ ‘=’  Z  Z (2,2)  ‘=’ 2 = 2 (8,7)  ‘<’ ‘<’  Z  Z (7,8)  ‘<’ 8 < 7 7 < 8 equals = { (0,0), (1,1), (2,2), (3,3), (4,4), … } less-than = { (0,1), (0,2), (0,3), …, (1,2), (1,3), … } …same as saying: ’=’  P (Z  Z) …same as saying: ’<’  P (Z  Z)

[ 47 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Exercises Example: “less-than-or-equal-to” relation: Signature: Relation is: Written as: as a short-hand for: … and as: as a short-hand for: Example: “is-congruent-modulo-3” relation: Signature: Relation is: Written as: as a short-hand for: … and as: as a short-hand for: 3  2 (3,2)  ‘  ’ ‘  ’  Z  Z (2,3)  ‘  ’ 2  3 (7,8)  ‘  3 ’ ‘  3 ’  Z  Z (6,9)  ‘  3 ’ 7   3 9 ‘  ’ = { (0,0), (0,1), (0,2), …, (1,1), (1,1), …, (2,3), … } ‘  3 ’ = { (0,0), (0,3), (0,6), …, (1,1), (1,4), …, (6,9), … } …same as saying: ’’  P (Z  Z) …same as saying: ’  ’  P (Z  Z) …same as saying: ’ ’  P (Z  Z) …same as saying: ’  3 ’  P (Z  Z)

[ 48 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Equivalence Relation Let ‘~’ be a binary relation over set A: ‘~’  A  A ~ is an equivalence relation iff: Reflexive: Symmetric: Transitive:  x  A: x ~ x  x,y  A: x ~ y  y ~ x  x,y,z  A: x ~ y  y ~ z  x ~ z

[ 49 ] Claus Brabrand, UFPE, Brazil Aug 04, 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

[ 50 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Partial-Order A Partial-Order is a structure (S, ): S is a set ’ ’ is a binary relation on S (i.e., ’ ’  S  S) satisfying: Reflexivity: Transitivity: Anti-Symmetry:  x  S: x x  x,y  S: x y  y x  x = y  x,y,z  S: x y  y z  x z

[ 51 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Visualization: Hasse Diagram Partial-Order (S, ):  Reflexive: Transitive: Anti-Symmetric: Hasse Diagram:  x  S: x x  x,y  S: x y  y x  x = y  x,y,z  S: x y  y z  x z

[ 52 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Exercise (Hasse Diagram) Given Hasse Diagram: Write down partial order (B, ): Set B = { … } Relation ’ ’: Signature All elements of the relation (i.e., ’ ’ = { … } ) Give example of element in ’ ’ (w/ + w/o shorthand) Again, but for an element not in the relation

[ 53 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Example Partial-Orders Lattice Examples (as Hasse Diagrams): …depending on what is analysed for! 2 or more

Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS Least Upper Bound ' '

[ 55 ] Claus Brabrand, UFPE, Brazil Aug 04, 2010DATA-FLOW ANALYSIS ”Least Upper Bound” Upper bound: We say that ’z’ is an upper bound for set ’X’ …written if Least upper bound: We say that ’z’ is the least upper bound of set ’X’ …written if  x  X: x z X z z = X X z   z’: X z’  z z’ upper bound least

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