1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

Register Allocation Consists of two parts: Goal : minimize spills
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Lecture 11: Code Optimization CS 540 George Mason University.
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.
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:
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
Jeffrey D. Ullman Stanford University. 2  Generalizes: 1.Moving loop-invariant computations outside the loop. 2.Eliminating common subexpressions. 3.True.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
1 Partial Redundancy Elimination Finding the Right Place to Evaluate Expressions Four Necessary Data-Flow Problems.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
Partial Redundancy Elimination. Partial-Redundancy Elimination Minimize the number of expression evaluations By moving around the places where an expression.
EECS 583 – Class 6 Dataflow Analysis University of Michigan September 26, 2011.
Foundations of Data-Flow Analysis. Basic Questions Under what circumstances is the iterative algorithm used in the data-flow analysis correct? How precise.
1 Copy Propagation What does it mean? Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
More Dataflow Analysis CS153: Compilers Greg Morrisett.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
1 Copy Propagation What does it mean? – Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
2015/6/29\course\cpeg421-08s\Topic4-a.ppt1 Topic-I-C Dataflow Analysis.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
A UNIFIED APPROACH TO GLOBAL PROGRAM OPTIMIZATION Proseminar „Programmanalyse”, Prof. Dr. Heike Wehrheim Universität Paderborn, WS 2011/2012.
Ben Livshits Based in part of Stanford class slides from
1 CS 201 Compiler Construction Data Flow Analysis.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
1 Data-Flow Analysis Proving Little Theorems Data-Flow Equations Major Examples.
Code Optimization, Part III Global Methods Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Optimization software for apeNEXT Max Lukyanov,  apeNEXT : a VLIW architecture  Optimization basics  Software optimizer for apeNEXT  Current.
What’s in an optimizing compiler?
Formal Methods Program Slicing & Dataflow Analysis February 2015.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
MIT Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
Jeffrey D. Ullman Stanford University. 2 boolean x = true; while (x) {... // no change to x }  Doesn’t terminate.  Proof: only assignment to x is at.
12/5/2002© 2002 Hal Perkins & UW CSER-1 CSE 582 – Compilers Data-flow Analysis Hal Perkins Autumn 2002.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
EECS 583 – Class 6 Dataflow Analysis University of Michigan September 24, 2012.
Final Code Generation and Code Optimization.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Optimization Simone Campanoni
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
Data Flow Analysis Suman Jana
Iterative Dataflow Problems
Dataflow Testing G. Rothermel.
Machine-Independent Optimization
Topic 10: Dataflow Analysis
University Of Virginia
Code Optimization Chapter 10
Code Optimization Chapter 9 (1st ed. Ch.10)
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
Fall Compiler Principles Lecture 10: Loop Optimizations
Topic-4a Dataflow Analysis 2019/2/22 \course\cpeg421-08s\Topic4-a.ppt.
Final Code Generation and Code Optimization
Static Single Assignment
Presentation transcript:

1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example, knowledge about what expressions are available at some point helps in common subexpression elimination. IMPORTANT! Data flow analysis should never tell us that a transformation is safe when in fact it is not. It is better to not perform a valid optimization that to perform one that changes the function of the program.

2 Data flow analysis IMPORTANT! Data flow analysis should never tell us that a transformation is safe when in fact it is not. When doing data flow analysis we must be Conservative Do not consider information that may not preserve the behavior of the program Aggressive Try to collect information that is as exact as possible, so we can get the greatest benefit from our optimizations.

3 Global Iterative Data Flow Analysis Global: Performed on the flow graph Goal = to collect information at the beginning and end of each basic block Iterative: Construct data flow equations that describe how information flows through each basic block and solve them by iteratively converging on a solution.

4 Global Iterative Data Flow Analysis Components of data flow equations Sets containing collected information in set: information coming into the BB from outside (following flow of dats) gen set: information generated/collected within the BB kill set: information that, due to action within the BB, will affect what has been collected outside the BB out set: information leaving the BB Functions (operations on these sets) Transfer functions describe how information changes as it flows through a basic block Meet functions describe how information from multiple paths is combined.

5 Global Iterative Data Flow Analysis Algorithm sketch Typically, a bit vector is used to store the information. For example, in reaching definitions, each bit position corresponds to one definition. We use an iterative fixed-point algorithm. Depending on the nature of the problem we are solving, we may need to traverse each basic block in a forward (top-down) or backward direction. The order in which we "visit" each BB is not important in terms of algorithm correctness, but is important in terms of efficiency. In & Out sets should be initialized in a conservative and aggressive way. Initialize gen and kill sets Initialize in or out sets (depending on "direction") while there are no changes in in and out sets { for each BB { apply meet function apply transfer function }

6 Typical problems Reaching definitions For each use of a variable, find all definitions that reach it. Upward exposed uses For each definition of a variable, find all uses that it reaches. Live variables For a point p and a variable v, determine whether v is live at p. Available expressions Find all expressions whose value is available at some point p.

7 Reaching definitions Determine which definitions of a variable may reach each use of the variable. For each use, list the definitions that reach it. This is also called a ud- chain. In global data flow analysis, we collect such information at the endpoints of a basic block, but we can do additional local analysis within each block. Uses of reaching definitions : constant propagation we need to know that all the definitions that reach a variable assign it to the same constant copy propagation we need to know whether a particular copy statement is the only definition that reaches a use. code motion we need to know whether a computation is loop-invariant

8 Reaching definitions A definition D reaches a point p if there is a path from D to p along which D is not killed. A definition D of a variable x is killed when there is a redefinition of x. How can we represent the set of definitions reaching a point? Use a bit string of length n, where n is the number of definitions. Set bit i to 1 if definition i reaches that point, set it to 0 otherwise.

9 Reaching definitions What is safe? To assume that a definition reaches a point even if it turns out not to. The computed set of definitions reaching a point p will be a superset of the actual set of definitions reaching p Goal : make the set of reaching definitions as small as possible (i.e. as close to the actual set as possible)

10 Reaching definitions How are the gen and kill sets defined? gen[B] = {definitions that appear in B and reach the end of B} kill[B] = {all definitions that never reach the end of B} What is the direction of the analysis? forward out[B] = gen[B]  (in[B] - kill[B])

11 Reaching definitions What is the confluence operator? union in[B] =  out[P], over the predecessors P of B How do we initialize? start small Why? Because we want the resulting set to be as small as possible for each block B initialize out[B] = gen[B]

12 Upward Exposed Uses Determine what uses of a variable are reached by a specific definition of that variable. For each definition, list the uses that are reached by it. This is also called a du-chain. This is the dual of reaching definitions. Useful in instruction scheduling. du-chains and ud-chains are different: z>1 x=1 z>y x=2 y=x+1z=x+3 print z

13 Upward Exposed Uses What is the direction of the analysis? backward in[B] = use[B]  (out[B] - def[B]) How are the use and def sets defined? use[B] = {(s,x) | s is a use of x in B and there is no definition of x between the beginning of B and s} def[B] = {(s,x) | s is a use of x not in B and B contains a definition of x} What is the confluence operator? union out[B]=  in[S], over the successors S of B

14 Upward Exposed Uses How do we initialize? Start small for each block B initialize in[B] =  du- and ud- chains are useful in register allocation.

15 Available expressions Determine which expressions have already been evaluated at each point. A expression x+y is available at point p if every path from the entry to p evaluates x+y and after the last such evaluation prior to reaching p, there are no assignments to x or y Used in : global common subexpression elimination

16 Available expressions What is safe? To assume that an expression is not available at some point even if it may be. The computed set of available expressions at point p will be a subset of the actual set of available expressions at p The computed set of unavailable expressions at point p will be a superset of the actual set of unavailable expressions at p Goal : make the set of available expressions as large as possible (i.e. as close to the actual set as possible)

17 Available expressions How are the gen and kill sets defined? gen[B] = {expressions evaluated in B without subsequently redefining its operands} kill[B] = {expressions whose operands are redefined in B without reevaluating the expression afterwards} What is the direction of the analysis? forward out[B] = gen[B]  (in[B] - kill[B])

18 Available expressions What is the confluence operator? intersection in[B] =  out[P], over the predecessors P of B How do we initialize? start large for the first block B 1 initialize out[B 1 ] = gen[B 1 ] for each block B initialize out[B] = U-kill[B]

19 Live variables Determine whether a given variable is used along a path from a given point to the exit. A variable x is live at point p if there is a path from p to the exit along which the value of x is used before it is redefined. Otherwise, the variable is dead at that point. Used in : register allocation dead code elimination

20 Live variables What is safe? To assume that a variable is live at some point even if it may not be. The computed set of live variables at point p will be a superset of the actual set of live variables at p The computed set of dead variables at point p will be a subset of the actual set of dead variables at p Goal : make the set of live variables as small as possible (i.e. as close to the actual set as possible)

21 Live variables How are the def and use sets defined? def[B] = {variables defined in B before being used} /* kill */ use[B] = {variables used in B before being defined} /* gen */ What is the direction of the analysis? backward in[B] = use[B]  (out[B] - def[B])

22 Live variables What is the confluence operator? union out[B] =  in[S], over the successors S of B How do we initialize? start small for each block B initialize in[B] =  or in[B] = use[B]

23 Very Busy Expressions Determine whether an expression is evaluated in all paths from a point to the exit. An expression e is very busy at point p if no matter what path is taken from p, e will be evaluated before any of its operands are defined. Used in: Code hoisting If e is very busy at point p, we can move its evaluation at p. Does this make the generated code faster?

24 Very Busy Expressions What is safe? To assume that an expression is not very busy at some point even if it may be. The computed set of very busy expressions at point p will be a subset of the actual set of available expressions at p Goal : make the set of very busy expressions as large as possible (i.e. as close to the actual set as possible)

25 Very Busy Expressions How are the gen and kill sets defined? gen[B] = {all expressions evaluated in B before any definitions of their operands} kill[B] = {all expressions whose operands are defined in B before any possible re-evaluation} What is the direction of the analysis? backward in[B] = gen[B]  (out[B] - kill[B])

26 Very Busy Expressions What is the confluence operator? intersection out[B] =  in[S], over the successors S of B How do we initialize? start large for each block B initialize out[B] = U

27 General framework desired set as small as possible as large as possible resulting set larger than actual smaller than actual gen everything that may be true everything that must be true kill everything that must be false everything that may be false confluence union intersection initial start with small initial set start with large initial set example live variables (bwd) very busy expressions (bwd) example reaching definitions (fwd) available expressions (fwd)

28 Dataflow analysis example