1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.

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

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.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
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.
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.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
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,
CS 536 Spring Global Optimizations Lecture 23.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
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.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Ben Livshits Based in part of Stanford class slides from
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
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.
Data Flow Analysis Compiler Baojian Hua
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.
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.
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.
Final Code Generation and Code Optimization.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Data flow analysis John Cavazos University.
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.
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
Data Flow Analysis Suman Jana
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
Data Flow Analysis Compiler Design
Topic-4a Dataflow Analysis 2019/2/22 \course\cpeg421-08s\Topic4-a.ppt.
Dataflow Analysis Hal Perkins Winter 2008
Final Code Generation and Code Optimization
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2009 Data flow analysis
Presentation transcript:

1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected global information regarding the program’s structure For performing global code optimizations global information must be collected regarding values of program variables. –Local optimizations involve statements from same basic block –Global optimizations involve statements from different basic blocks  data flow analysis is performed to collect global information that drives global optimizations

2 Local and Global Optimization

Applications of Data Flow Analysis Applicability of code optimizations Symbolic debugging of code Static error checking Type inference ……. 3

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 evaluation. Definition d of variable v reaches a point p if there exists a path from immediately after d to p such that definition d is not killed along the path. Definition d is killed along a path between two points if there exists an assignment to variable v along the path. 4

Example 5 d reaches u along path 2 & d does not reach u along path 1 Since there exists a path from d to u along which d is not killed (i.e., path 2 ), d reaches u.

Reaching Definitions Contd. Unambiguous Definition: X = ….; Ambiguous Definition: *p = ….; p may point to X For computing reaching definitions, typically we only consider kills by unambiguous definitions. 6 X=.. *p=.. Does definition of X reach here ? Yes

Computing Reaching Definitions At each program point p, we compute the set of definitions that reach point p. Reaching definitions are computed by solving a system of equations (data flow equations). 7 d1 : X=… IN[B] OUT[B] GEN[B] ={ d1 } KILL[B]={ d2, d3 } d2 : X=… d3 : X=…

Data Flow Equations 8 GEN[B]: Definitions within B that reach the end of B. KILL[B]: Definitions that never reach the end of B due to redefinitions of variables in B. IN[B]: Definitions that reach B’s entry. OUT[B]: Definitions that reach B’s exit.

Reaching Definitions Contd. Forward problem – information flows forward in the direction of edges. May problem – there is a path along which definition reaches a point but it does not always reach the point. Therefore in a May problem the meet operator is the Union operator. 9

Applications of Reaching Definitions Constant Propagation/folding Copy Propagation 10

2. Available Expressions An expression is generated at a point if it is computed at that point. An expression is killed by redefinitions of operands of the expression. An expression A+B is available at a point if every path from the start node to the point evaluates A+B and after the last evaluation of A+B on each path there is no redefinition of either A or B (i.e., A+B is not killed). 11

Available Expressions Available expressions problem computes: at each program point the set of expressions available at that point. 12

Data Flow Equations 13 GEN[B]: Expressions computed within B that are available at the end of B. KILL[B]: Expressions whose operands are redefined in B. IN[B]: Expressions available at B’s entry. OUT[B]: Expressions available at B’s exit.

Available Expressions Contd. Forward problem – information flows forward in the direction of edges. Must problem – expression is definitely available at a point along all paths. Therefore in a Must problem the meet operator is the Intersection operator. Application: A 14

3. Live Variable Analysis A path is X-clear if it contains no definition of X. A variable X is live at point p if there exists a X- clear path from p to a use of X; otherwise X is dead at p. 15 Live Variable Analysis Computes: At each program point p identify the set of variables that are live at p.

Data Flow Equations 16 GEN[B]: Variables that are used in B prior to their definition in B. KILL[B]: Variables definitely assigned value in B before any use of that variable in B. IN[B]: Variables live at B’s entry. OUT[B]: Variables live at B’s exit.

Live Variables Contd. Backward problem – information flows backward in reverse of the direction of edges. May problem – there exists a path along which a use is encountered. Therefore in a May problem the meet operator is the Union operator. 17

Applications of Live Variables Register Allocation Dead Code Elimination 18

Summary May/UnionMust/Intersecti on ForwardReaching Definitions Available Expressions BackwardLive Variables 19

Conservative Analysis Optimizations that we apply must be Safe => the data flow facts we compute should definitely be true (not simply possibly true). Two main reasons that cause results of analysis to be conservative: 1. Control Flow 2. Pointers & Aliasing 20

Conservative Analysis 1. Control Flow – we assume that all paths are executable; however, some may be infeasible. 21 X+Y is always available if we exclude infeasible paths.

Conservative Analysis 2. Pointers & Aliasing – we may not know what a pointer points to. 1. X = 5 2. *p = … // p may or may not point to X 3. … = X Constant propagation: assume p does point to X (i.e., in statement 3, X cannot be replaced by 5). Dead Code Elimination: assume p does not point to X (i.e., statement 1 cannot be deleted). 22

Representation of Data Flow Sets Bit vectors – used to represent sets because we are computing binary information. –Does a definition reach a point ? T or F –Is an expression available/very busy ? T or F –Is a variable live ? T or F For each expression, variable, definition we have one bit – intersection and union operations can be implemented using bitwise and & or operations. 23

Solving Data Flow Equations 24

Solving Data Flow Equations 25

Solving Data Flow Equations 26