Compiler Construction Lecture 16 Data-Flow Analysis.

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 Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
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.
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.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Program analysis Mooly Sagiv html://
From last time: live variables Set D = 2 Vars Lattice: (D, v, ?, >, t, u ) = (2 Vars, µ, ;,Vars, [, Å ) x := y op z in out F x := y op z (out) = out –
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
From last time: Lattices A lattice is a tuple (S, v, ?, >, t, u ) such that: –(S, v ) is a poset – 8 a 2 S. ? v a – 8 a 2 S. a v > –Every two elements.
Data Flow Analysis Compiler Design Nov. 3, 2005.
From last time: reaching definitions For each use of a variable, determine what assignments could have set the value being read from the variable Information.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Program analysis Mooly Sagiv html://
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
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.
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
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.
San Diego October 4-7, 2006 Over 1,000 women in computing Events for undergraduates considering careers and graduate school Events for graduate students.
From last lecture x := y op z in out F x := y op z (in) = in [ x ! in(y) op in(z) ] where a op b =
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Overview of program analysis Mooly Sagiv html://
1 Program Analysis Systematic Domain Design Mooly Sagiv Tel Aviv University Textbook: Principles.
Projects. Dataflow analysis Dataflow analysis: what is it? A common framework for expressing algorithms that compute information about a program Why.
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.
From last time: reaching definitions For each use of a variable, determine what assignments could have set the value being read from the variable Information.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Data Flow Analysis Compiler Design Nov. 8, 2005.
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.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis: Data-flow frameworks –Classic.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Precision Going back to constant prop, in what cases would we lose precision?
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
Example x := read() v := a + b x := x + 1 w := x + 1 a := w v := a + b z := x + 1 t := a + b.
Machine-Independent Optimizations Ⅱ CS308 Compiler Theory1.
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
Program Analysis auxiliary information (hints, proof steps, types) Can come from compiler or user Goal: Automatically computes potentially useful information.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 12: Abstract Interpretation IV Roman Manevich Ben-Gurion University.
More Examples of Abstract Interpretation Register Allocation.
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 Mooly Sagiv Tel Aviv University Textbook: Principles of Program.
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 and Verification Spring 2014 Program Analysis and Verification Lecture 8: Static Analysis II Roman Manevich Ben-Gurion University.
Optimization Simone Campanoni
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,
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Spring 2017 Program Analysis and Verification
Dataflow analysis.
Abstract Interpretation Continued
G. Ramalingam Microsoft Research, India & K. V. Raghavan
Iterative Program Analysis Abstract Interpretation
Another example: constant prop
Dataflow analysis.
Fall Compiler Principles Lecture 10: Global Optimizations
Optimizations using SSA
Data Flow Analysis Compiler Design
Abstract Interpretation: Analysis Domain (D) Lattices
Presentation transcript:

Compiler Construction Lecture 16 Data-Flow Analysis

Goal of Data-Flow Analysis Automatically compute information about the program Use it to report errors to user (like type errors) Use it to optimize the program Works on control-flow graphs: x = 1 while (x < 10) { x = x + 2 }

How We Define It Abstract Domain D (Data-Flow Facts): which information to compute? – Example: interval for each variable x:[a,b], y:[a’,b’] Transfer Functions [[st]] for each statement st, how this statement affects the facts – Example:

Find Transfer Function: Plus If and we execute x= x+y then Suppose we have only two integer variables: x,y So we can let a’= a+c b’ = b+d c’=c d’ = d

Find Transfer Function: Minus If and we execute y= x-y then Suppose we have only two integer variables: x,y So we can let a’= a b’ = b c’= a - d d’ = b - c

Transfer Functions for Tests if (x > 1) { y = 1 / x } else { y = 42 }

Merging Data-Flow Facts if (x > 0) { y = x } else { y = -x – 50 } join

Handling Loops: Iterate Until Stabilizes x = 1 while (x < 10) { x = x + 2 } Compiler learned some facts!

Data-Flow Analysis Algorithm var facts : Map[Vertex,Domain] = Map.withDefault(empty) facts(entry) = initialValues // change while (there was change) pick edge (v1,statmt,v2) from CFG such that facts(v1) was changed facts(v2)=facts(v2) join [[statmt]](facts(v1)) } Order does not matter for the end result, as long as we do not permanently neglect any edge whose source was changed.

Handling Loops: Iterate Until Stabilizes x = 1 while (x < 10) { x = x + 2 } Compiler learned some facts!

Handling Loops: Iterate Until Stabilizes x = 1 while (x < n) { x = x + 2 } Compiler learns some facts, but only after long time n =

Handling Loops: Iterate Until Stabilizes var x : BigInt = 1 while (x < n) { x = x + 2 } For unknown program inputs it may be practically impossible to know how long it takes var n : BigInt = readInput() Solutions - smaller domain, e.g. only certain intervals [a,b] where a,b in {-∞,-127,-1,0,1,127,∞} - widening techniques (make it less precise on demand)

Size of analysis domain Interval analysis: D 1 = { [a,b] | a ≤ b, a,b  {-M,-127,-1,0,1,127,M-1}} U {  } Constant propagation: D 2 = { [a,a] | a  {-M,-(M-1),…,-2,-1,0,1,2,3,…,M-1}} U {  } suppose M is 2 63 |D 1 | = |D 2 | =

How many steps does the analysis take to finish (converge)? Interval analysis: D 1 = { [a,b] | a ≤ b, a,b  {-M,-127,-1,0,1,127,M-1}} U {  } Constant propagation: D 2 = { [a,a] | a  {-M,-(M-1),…,-2,-1,0,1,2,3,…,M-1}} U {  } suppose M is 2 63 With D 1 takes at most steps. With D 2 takes at most steps.

Termination Given by Length of Chains Interval analysis: D 1 = { [a,b] | a ≤ b, a,b  {-M,-127,-1,0,1,127,M-1}} U {  } Constant propagation: D 2 = { [a,a] | a  {-M,…,-2,-1,0,1,2,3,…,M-1}} U {  } U {T} suppose M is 2 63 Domain is a lattice. Maximal chain length = lattice height

Lattice for intervals [a,b] where a,b  {-M,-127,0,127,M-1}

Lattice

Data-Flow Analysis Ingredients

Transfer Function Pre-state Post-state S

Abstraction/Concretization Abstraction Function   Concretization Function 