Constant Propagation. The constant propagation framework is different from all the data-flow problems discussed so far, in that It has an unbounded set.

Slides:



Advertisements
Similar presentations
DATAFLOW TESTING DONE BY A.PRIYA, 08CSEE17, II- M.s.c [C.S].
Advertisements

Continuing Abstract Interpretation We have seen: 1.How to compile abstract syntax trees into control-flow graphs 2.Lattices, as structures that describe.
1 Induction Variables Region-Based Analysis Meet and Closure of Transfer Functions Affine Functions of Reference Variables.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Jeffrey D. Ullman Stanford University. 2  A set of nodes N and edges E is a region if: 1.There is a header h in N that dominates all nodes in N. 2.If.
Data Flow Analysis. Goal: make assertions about the data usage in a program Use these assertions to determine if and when optimizations are legal Local:
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
1 CS 201 Compiler Construction Data Flow Framework.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Foundations of Data-Flow Analysis. Basic Questions Under what circumstances is the iterative algorithm used in the data-flow analysis correct? How precise.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
1 Constant Propagation A More Complex Semilattice A Nondistributive Framework.
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. 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.
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.
Constraints for reaching definitions Using may-point-to information: out = in [ { x ! s | x 2 may-point-to(p) } Using must-point-to aswell: out = in –
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.
Projects. Dataflow analysis Dataflow analysis: what is it? A common framework for expressing algorithms that compute information about a program Why.
1 Data-Flow Frameworks Lattice-Theoretic Formulation Meet-Over-Paths Solution Monotonicity/Distributivity.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
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.
1 CS 201 Compiler Construction Lecture 4 Data Flow Framework.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
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.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis: Data-flow frameworks –Classic.
Handouts Software Testing and Quality Assurance Theory and Practice Chapter 5 Data Flow Testing
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving.
Precision Going back to constant prop, in what cases would we lose precision?
Example x := read() v := a + b x := x + 1 w := x + 1 a := w v := a + b z := x + 1 t := a + b.
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.
Presented By Dr. Shazzad Hosain Asst. Prof., EECS, NSU
Machine-Independent Optimizations Ⅱ CS308 Compiler Theory1.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
CS 614: Theory and Construction of Compilers Lecture 17 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Program Representations. Representing programs Goals.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
Optimization Simone Campanoni
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Yet More Data flow analysis John Cavazos.
Compiler Principles Fall Compiler Principles Lecture 9: Dataflow & Optimizations 2 Roman Manevich Ben-Gurion University of the Negev.
DFA foundations Simone Campanoni
Phoenix Based Dynamic Slicing Debugging Tool Eric Cheng Lin Xu Matt Gruskin Ravi Ramaseshan Microsoft Phoenix Intern Team (Summer '06)
Software Testing and QA Theory and Practice (Chapter 5: Data Flow Testing) © Naik & Tripathy 1 Software Testing and Quality Assurance Theory and Practice.
Data Flow Analysis Suman Jana
Iterative Dataflow Problems
Dataflow analysis.
Fall Compiler Principles Lecture 8: Loop Optimizations
Global optimizations.
University Of Virginia
Dataflow analysis.
Fall Compiler Principles Lecture 10: Global Optimizations
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
COMPILERS Liveness Analysis
Software Testing and QA Theory and Practice (Chapter 5: Data Flow Testing) © Naik & Tripathy 1 Software Testing and Quality Assurance Theory and Practice.
Presentation transcript:

Constant Propagation

The constant propagation framework is different from all the data-flow problems discussed so far, in that It has an unbounded set of possible data-flow values, even for a fixed control flow graph It is not distributive

Data-Flow Values for the Constant Propagation Framework The set of data-flow values is a product lattice, with one component for each variable in a program. The lattice for a variable consists of the following: All constants appropriate for the type of the variable The value NAC, which stands for not-a- constant The value UNDEF, which stands for undefined

NAC and UNDEF A variable is mapped to NAC if it is determined not to have a constant value. The variable may have been assigned an input value, or derived from a variable that is not a constant, or assigned different constants along different paths that lead to the same program point. A variable is mapped UNDEF if nothing may yet to be asserted; presumably, no definition of the variable has been discovered to reach the point in question.

Example: An Integer Variable UNDEF … … NAC For all value v, UNDEF  v = v and NAC  v = NAC. For any constant c, c  c = c. For any two distinct constants c 1 and c 2, c 1  c 2 = NAC. A data-flow value for this framework is a map m from each variable to one of the values in its constant semilattice.

The Meet for the Constant Propagation Framework The semilattice of data-flow values is the product of the semilattices for variables Two maps m 1 and m 2, m 1  m 2, if and only if for all variables v, m 1 ( v )  m 2 ( v ) Two maps m 1 and m 2, m 1  m 2 = m 3, if and only if for all variables v, m 3 ( v ) = m 1 ( v )  m 2 ( v )

Transfer Functions for the Constant Propagation Framework The set F consists of certain transfer functions that accept a map of variables to values in the constant semilattice and return another such map F contains the identity function I F contains the constant transfer function for the ENTRY node that returns m 0, where m 0 ( v ) = UNDEF, for all variables v

Transfer Functions for the Constant Propagation Framework Let f s be the transfer function of statement s, and m 2 = f s ( m 1 ) 1. If s is not an assignment, then f s is I 2. If s is an assignment to variable x = e, then m 2 ( v ) = m 1 ( v ), for all variables v  x, and (a) If e is a constant c, then m 2 ( x ) = c (b) If e is of the form y + z, then m 1 ( y ) + m 1 ( z ) if m 1 ( y ) and m 1 ( z ) constant m 2 ( x ) = NAC if either m 1 ( y ) or m 1 ( z ) is NAC UNDEF otherwise (c) If e is any other expression, then m 2 ( x ) = NAC

Monotonicity of the Constant Propagation Framework Case 1: I is monotone Case 2(a): m 2 ( x ) = c, f s is monotone Case 2(b): m 1 ( y ) m 1 ( z ) m 2 ( x ) UNDEFUNDEF UNDEF c 2 UNDEF NAC NAC UNDEFUNDEF c 1 c 2 c 1 + c 2 NAC NAC UNDEF NAC NAC c 2 NAC NAC NAC Case 2(c): m 2 ( x ) = NAC, f s is monotone

Nondistributivity of the Constant Propagation Framework x = 2 y = 3 x = 3 y = 2 z = x + y EXIT m m ( x ) m ( y ) m ( z ) m 0 UNDEF UNDEF UNDEF f 1 ( m 0 ) 2 3 UNDEF f 2 ( m 0 ) 3 2 UNDEF f 1 ( m 0 )  f 2 ( m 0 ) NAC NAC UNDEF f 3 ( f 1 ( m 0 )  f 2 ( m 0 )) NAC NAC NAC f 3 ( f 1 ( m 0 )) f 3 ( f 2 ( m 0 )) f 3 ( f 1 ( m 0 ))  f 3 ( f 2 ( m 0 )) NAC NAC 5 f 3 ( f 1 ( m 0 )  f 2 ( m 0 )) < f 3 ( f 1 ( m 0 ))  f 3 ( f 2 ( m 0 )) ENTRY

Interpretation of the Results The value UNDEF is used for two purposes: Initialize the ENTRY node: variables are undefined at the beginning of the execution Initialize the interior program points before the iterations: for lack of information at the beginning of the iterative process, we approximate the solution with the top element

Interpretation of the Results When UNDEF shows up at some program point, it means that no definitions have been observed for that variable along any of the paths leading up to that program point. If all the definitions reaching a program point have the same constant value, the variable is considered a constant even though it may not be defined along some path.

Example if Q goto B 2 if Q’ goto B 5 x = 10 = x B2B2 B3B3 B1B1 B4B4 B5B5 B6B6 B7B7