Lecture 20: Dataflow Analysis Frameworks 11 Mar 02

Slides:



Advertisements
Similar presentations
Partial Orderings Section 8.6.
Advertisements

CSCI 115 Chapter 6 Order Relations and Structures.
Relations Relations on a Set. Properties of Relations.
1 CS 201 Compiler Construction Data Flow Framework.
Chapter 7 Relations : the second time around
Foundations of Data-Flow Analysis. Basic Questions Under what circumstances is the iterative algorithm used in the data-flow analysis correct? How precise.
Discrete Mathematics Lecture 4 Harper Langston New York University.
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.
FSM Decomposition using Partitions on States 290N: The Unknown Component Problem Lecture 24.
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.
Administrative stuff Office hours: After class on Tuesday.
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.
Recap: Reaching defns algorithm From last time: reaching defns worklist algo We want to avoid using structure of the domain outside of the flow functions.
Orderings and Bounds Parallel FSM Decomposition Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 10 Update and modified by Marek.
1 CS 201 Compiler Construction Lecture 4 Data Flow Framework.
Partial Orderings: Selected Exercises
Claus Brabrand, ITU, Denmark DATA-FLOW ANALYSISMar 25, 2009 Static Analysis: Data-Flow Analysis II Claus Brabrand IT University of Copenhagen (
Partially Ordered Sets (POSets)
Relations Chapter 9.
Sets, POSets, and Lattice © Marcelo d’Amorim 2010.
Chapter 6. Order Relations and Structure
Λλ 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.
MIT Foundations of Dataflow Analysis Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Solving fixpoint equations
Machine-Independent Optimizations Ⅱ CS308 Compiler Theory1.
Chapter 9. Chapter Summary Relations and Their Properties n-ary Relations and Their Applications (not currently included in overheads) Representing Relations.
Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation.
CS 267: Automated Verification Lecture 3: Fixpoints and Temporal Properties Instructor: Tevfik Bultan.
Sets, Relations, and Lattices
A Logic of Partially Satisfied Constraints Nic Wilson Cork Constraint Computation Centre Computer Science, UCC.
CS 614: Theory and Construction of Compilers Lecture 17 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Sets and Subsets Set A set is a collection of well-defined objects (elements/members). The elements of the set are said to belong to (or be contained in)
Compiler Principles Fall Compiler Principles Lecture 11: Loop Optimizations Roman Manevich Ben-Gurion University.
Set Theory Concepts Set – A collection of “elements” (objects, members) denoted by upper case letters A, B, etc. elements are lower case brackets are used.
Relations and Functions ORDERED PAIRS AND CARTESIAN PRODUCT An ordered pair consists of two elements, say a and b, in which one of them, say a is designated.
Binary Relations Definition: A binary relation R from a set A to a set B is a subset R ⊆ A × B. Example: Let A = { 0, 1,2 } and B = {a,b} {( 0, a), (
Advanced Digital Designs Jung H. Kim. Chapter 1. Sets, Relations, and Lattices.
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
Semilattices presented by Niko Simonson, CSS 548, Autumn 2012 Semilattice City, © 2009 Nora Shader.
Iterative Dataflow Problems Taken largely from notes of Alex Aiken (UC Berkeley) and Martin Rinard (MIT) Dataflow information used in optimization Several.
Compiler Principles Fall Compiler Principles Lecture 9: Dataflow & Optimizations 2 Roman Manevich Ben-Gurion University of the Negev.
DFA foundations Simone Campanoni
Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation is a relation on.
Partial Orderings: Selected Exercises
Fixpoints and Reachability
CSE 2813 Discrete Structures
Unit-III Algebraic Structures
Partial Orders.
Partial Orderings CSE 2813 Discrete Structures.
Partial Orders (POSETs)
Equivalence Relations
Simone Campanoni DFA foundations Simone Campanoni
LATTICES AND BOOLEAN ALGEBRA
Global optimizations.
Introductory Material
Discrete Math (2) Haiming Chen Associate Professor, PhD
Concurrent Models of Computation
Sungho Kang Yonsei University
Fall Compiler Principles Lecture 10: Global Optimizations
Data Flow Analysis Compiler Design
Background material.
Background material.
Live variables and copy propagation
Introductory Material
Presentation transcript:

Lecture 20: Dataflow Analysis Frameworks 11 Mar 02

Live Variable Analysis x = y+1 y =2*z if (d) z = x if (c) x = y+z z = 1 What are the live variables at each program point? Method: Define sets of live variables Build constraints Solve constraints L1 L2 L3 L4 L5 L6 L7 L8 L9 L10 L11 L12 CS 412/413 Spring 2002 Introduction to Compilers

Derive Constraints Constraints for each instruction: x = y+1 y =2*z if (d) z = x if (c) x = y+z z = 1 Constraints for each instruction: in[I]=(out[I]-def[I])  use[I] Constraints for control flow: out[B] =  in[B’] L1 L2 L3 L4 L5 L6 L7 L8 L9 L10 L11 B’  succ(B) L12 CS 412/413 Spring 2002 Introduction to Compilers

Derive Constraints x = y+1 y =2*z if (d) if (c) x = y+z z = 1 z = x L1 L1 = L2  {c} L2 L2 = L3  L11 L3 L3 = (L4-{x})  {y} L4 L4 = (L5-{y})  {z} L5 L5 = L6  {d} L6 L6 = L7  L9 L7 L7 = (L8-{x})  {y,z} L8 L8 = L9 L9 L9 = L10-{z} L10 L10 = L1 L11 L11 = (L12-{z})  {x} L12 CS 412/413 Spring 2002 Introduction to Compilers

Initialization x = y+1 y =2*z if (d) if (c) x = y+z z = 1 z = x L1={} L1 = L2  {c} L2 ={} L2 = L3  L11 L3 ={} L3 = (L4-{x})  {y} L4 ={} L4 = (L5-{y})  {z} L5 ={} L5 = L6  {d} L6 ={} L6 = L7  L9 L7 ={} L7 = (L8-{x})  {y,z} L8 ={} L8 = L9 L9 ={} L9 = L10-{z} L10 ={} L10 = L1 L11 ={} L11 = (L12-{z})  {x} L12 ={} CS 412/413 Spring 2002 Introduction to Compilers

Iteration 1 x = y+1 y =2*z if (d) if (c) x = y+z z = 1 z = x L1={x,y,z,c,d} L1 = L2  {c} L2 ={x,y,z,d} L2 = L3  L11 L3 ={y,z,d} L3 = (L4-{x})  {y} L4 ={z,d} L4 = (L5-{y})  {z} L5 ={y,z,d} L5 = L6  {d} L6 ={y,z} L6 = L7  L9 L7 ={y,z} L7 = (L8-{x})  {y,z} L8 ={} L8 = L9 L9 ={} L9 = L10-{z} L10 ={} L10 = L1 L11 ={x} L11 = (L12-{z})  {x} L12 ={} CS 412/413 Spring 2002 Introduction to Compilers

Iteration 2 x = y+1 y =2*z if (d) if (c) x = y+z z = 1 z = x L1={x,y,z,c,d} L1 = L2  {c} L2 ={x,y,z,c,d} L2 = L3  L11 L3 ={y,z,c,d} L3 = (L4-{x})  {y} L4 ={x,z,c,d} L4 = (L5-{y})  {z} L5 ={x,y,z,c,d} L5 = L6  {d} L6 ={x,y,z,c,d} L6 = L7  L9 L7 ={y,z,c,d} L7 = (L8-{x})  {y,z} L8 ={x,y,c,d} L8 = L9 L9 ={x,y,c,d} L9 = L10-{z} L10 ={x,y,z,c,d} L10 = L1 L11 ={x} L11 = (L12-{z})  {x} L12 ={} CS 412/413 Spring 2002 Introduction to Compilers

Fixed-point! x = y+1 y =2*z if (d) if (c) x = y+z z = 1 z = x L1={x,y,z,c,d} L1 = L2  {c} L2 ={x,y,z,c,d} L2 = L3  L11 L3 ={y,z,c,d} L3 = (L4-{x})  {y} L4 ={x,z,c,d} L4 = (L5-{y})  {z} L5 ={x,y,z,c,d} L5 = L6  {d} L6 ={x,y,z,c,d} L6 = L7  L9 L7 ={y,z,c,d} L7 = (L8-{x})  {y,z} L8 ={x,y,c,d} L8 = L9 L9 ={x,y,c,d} L9 = L10-{z} L10 ={x,y,z,c,d} L10 = L1 L11 ={x} L11 = (L12-{z})  {x} L12 ={} CS 412/413 Spring 2002 Introduction to Compilers

Final Result x = y+1 y =2*z x live here ! if (d) Final result: sets if (c) x = y+z z = 1 L1={x,y,z,c,d} L2 ={x,y,z,c,d} L3 ={y,z,c,d} x live here ! L4 ={x,z,c,d} L5 ={x,y,z,c,d} L6 ={x,y,z,c,d} L7 ={y,z,c,d} L8 ={x,y,c,d} Final result: sets of live variables at each program point L9 ={x,y,c,d} L10 ={x,y,z,c,d} L11 ={x} L12 ={} CS 412/413 Spring 2002 Introduction to Compilers

Characterize All Executions L1={x,y,z,c,d} if (c) The analysis detects that there is an execution which uses the value x = y+1 L2 ={x,y,z,c,d} L3 ={y,z,c,d} x = y+1 y =2*z if (d) L4 ={x,z,c,d} L5 ={x,y,z,c,d} L6 ={x,y,z,c,d} L7 ={y,z,c,d} x = y+z L8 ={x,y,c,d} L9 ={x,y,c,d} z = 1 L10 ={x,y,z,c,d} L11 ={x} z = x L12 ={} CS 412/413 Spring 2002 Introduction to Compilers

Generalization Live variable analysis and detection of available copies are similar: Define some information that they need to compute Build constraints for the information Solve constraints iteratively: The information always “increases” during iteration Eventually, it reaches a fixed point. We would like a general framework Framework applicable to many other analyses Live variable/copy propagation = instances of the framework CS 412/413 Spring 2002 Introduction to Compilers

Dataflow Analysis Framework Dataflow analysis = a common framework for many compiler analyses Computes some information at each program point The computed information characterizes all possible executions of the program Basic methodology: Describe information about the program using an algebraic structure called lattice Build constraints which show how instructions and control flow modify the information in the lattice Iteratively solve constraints CS 412/413 Spring 2002 Introduction to Compilers

Lattices and Partial Orders Lattice definition uses the concept of partial order relation A partial order (P,) consists of: A set P A partial order relation  which is: 1. Reflexive x  x 2. Anti-symmetric x  y, y  x  x = y 3. Transitive: x  y, y  z  x  z Called “partial order” because not all elements are comparable CS 412/413 Spring 2002 Introduction to Compilers

Lattices and Lower/Upper Bounds Lattice definition uses the concept of lower and upper bounds If (P,) is a partial order and S  P, then: 1. xP is a lower bound of S if x  y, for all yS 2. xP is an upper bound of S if y  x, for all yS There may be multiple lower and upper bounds of the same set S CS 412/413 Spring 2002 Introduction to Compilers

LUB and GLB If (P,) is a partial order and S  P, then: Define least upper bounds (LUB) and greatest lower bounds (GLB) If (P,) is a partial order and S  P, then: 1. xP is GLB of S if: a) x is an lower bound of S b) y  x, for any lower bound y of S 2. xP is a LUB of S if: a) x is an upper bound of S b) x  y, for any upper bound y of S … are GLB and LUB unique? CS 412/413 Spring 2002 Introduction to Compilers

Lattices A pair (L,) is a lattice if: 1. (L,) is a partial order 2. Any finite subset S  L has a LUB and a GLB Can define two operators in lattices: 1. Meet operator: x  y = GLB({x,y}) 2. Join operator: x  y = LUB({x,y}) Meet and join are well-defined for lattices CS 412/413 Spring 2002 Introduction to Compilers

Complete Lattices A pair (L,) is a complete lattice if: 1. (L,) is a partial order 2. Any subset S  L has a LUB and a GLB Can define meet and join operators Can also define two special elements: 1. Bottom element:  = GLB(L) 2. Top element:  = LUB(L) All finite lattices are complete CS 412/413 Spring 2002 Introduction to Compilers

Example Lattice Consider S = {a,b,c} and its power set P = {, {a}, {b}, {c}, {a,b}, {b,c}, {a,c} {a,b,c}} Define partial order as set inclusion: XY Reflexive X  Y Anti-symmetric X  Y, Y  X  X = Y Transitive X  Y, Y  Z  X  Z Also, for any two elements of P, there is a set which includes both and another set which is included in both Therefore (P,) is a (complete) lattice CS 412/413 Spring 2002 Introduction to Compilers

Hasse Diagrams Hasse diagram = graphical representation of a lattice where x is below y when x  y and x  y {a} {b} {c} {a,b} {a,c} {b,c} {a,b,c}  CS 412/413 Spring 2002 Introduction to Compilers

Power Set Lattice {a} {b} {c} {a,b} {a,c} {b,c} {a,b,c}  Partial order:  (set inclusion) Meet:  (set intersection) Join:  (set union) Top element: {a,b,c} (whole set) Bottom element:  (empty set) {a} {b} {c} {a,b} {a,c} {b,c} {a,b,c}  CS 412/413 Spring 2002 Introduction to Compilers

Reversed Lattice {a,b} {a,c} {b,c} {a} {b} {c}  {a,b,c} Partial order:  (set inclusion) Meet:  (set union) Join:  (set intersection) Top element:  (empty set) Bottom element: {a,b,c} (whole set) {a,b} {a,c} {b,c} {a} {b} {c}  {a,b,c} CS 412/413 Spring 2002 Introduction to Compilers

Relation To Analysis of Programs Information computed by live variable analysis and available copies can be expressed as elements of lattices Live variables: if V is the set of all variables in the program and P the power set of V, then: - (P,) is a lattice - sets of live variables are elements of this lattice CS 412/413 Spring 2002 Introduction to Compilers

Relation To Analysis of Programs Copy Propagation: - V is the set of all variables in the program - V x V the cartesian product representing all possible copy instructions - P the power set of V x V Then: - (P,) is a lattice - sets of available copies are lattice elements CS 412/413 Spring 2002 Introduction to Compilers

More About Lattices In a lattice (L, ), the following are equivalent: 1. x  y 2. x  y = x 3. x  y = y Note: meet and join operations were defined using the partial order relation CS 412/413 Spring 2002 Introduction to Compilers

Proof Prove that x  y implies x  y = x: x is a lower bound of {x,y} All lower bounds of {x,y} are less than x,y In particular, they are less than x Prove that x  y = x implies x  y : x is less than x and y In particular, x is less than y CS 412/413 Spring 2002 Introduction to Compilers

Proof Prove that x  y implies x  y = y: y is an upper bound of {x,y} All upper bounds of {x,y} greater than x,y In particular, they are greater than y Prove that x  y = y implies x  y : y is a upper bound of {x,y} y is greater than x and y In particular, y is greater than x CS 412/413 Spring 2002 Introduction to Compilers

Properties of Meet and Join The meet and join operators are: 1. Associative (x  y)  z = x  (y  z) 2. Commutative x  y = y  x 3. Idempotent: x  x = x Property: If “” is an associative, commutative, and idempotent operator, then the relation “” defined as x  y iff x  y = x is a partial order Above property provides an alternative definition of a partial orders and lattices starting from the meet (join) operator CS 412/413 Spring 2002 Introduction to Compilers

Using Lattices Assume information we want to compute in a program is expressed using a lattice L To compute the information at each program point we need to: Determine how each instruction in the program changes the information in the lattice Determine how lattice information changes at join/split points in the control flow CS 412/413 Spring 2002 Introduction to Compilers

Transfer Functions Dataflow analysis defines a transfer function F : L  L for each instruction in the program Describes how the instruction modifies the information in the lattice Consider in[I] is information before I, and out[I] is information after I Forward analysis: out[I] = F(in[I]) Backward analysis: in[I] = F(out[I]) CS 412/413 Spring 2002 Introduction to Compilers

Basic Blocks Can extend the concept of transfer function to basic blocks using function composition Consider: Basic block B consists of instructions (I1, …, In) with transfer functions F1, …, Fn in[B] is information before B out[B] is information after B Forward analysis: out[B] = Fn(…(F1(in[B]))) Backward analysis: in[I] = F1(… (Fn(out[i]))) CS 412/413 Spring 2002 Introduction to Compilers

Split/Join Points Dataflow analysis uses meet/join operations at split/join points in the control flow Consider in[B] is lattice information at beginning of block B and out[B] is lattice information at end of B Forward analysis: in[B] =  {out[B’] | B’pred(B)} Backward analysis: out[B] =  {in[B’] | B’succ(B)} Can alternatively use join operation  (equivalent to using the meet operation  in the reversed lattice) CS 412/413 Spring 2002 Introduction to Compilers