EECS 583 – Class 6 Dataflow Analysis University of Michigan September 24, 2012.

Slides:



Advertisements
Similar presentations
SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
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.
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.
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.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
Program Representations. Representing programs Goals.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
EECS 583 – Class 6 Dataflow Analysis University of Michigan September 26, 2011.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Organization Introduction Classifications of Optimization techniques
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.
Data Flow Analysis Compiler Design Nov. 3, 2005.
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.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
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.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
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.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Ben Livshits Based in part of Stanford class slides from
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
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?
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.
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
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.
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.
Dataflow II: Finish Dataflow Analysis, Start on Classical Optimizations EECS 483 – Lecture 24 University of Michigan Wednesday, November 29, 2006.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Announcements & Reading Material
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
EECS 583 – Class 8 Classic Optimization University of Michigan October 3, 2011.
CS 598 Scripting Languages Design and Implementation 9. Constant propagation and Type Inference.
EECS 583 – Class 5 Hyperblocks, Control Height Reduction University of Michigan September 21, 2011.
EECS 583 – Class 5 Hyperblocks, Control Height Reduction University of Michigan September 19, 2012.
Optimization Simone Campanoni
Control Flow IV: Control Flow Optimizations Start Dataflow Analysis
EECS 583 – Class 2 Control Flow Analysis
Dataflow Testing G. Rothermel.
Fall Compiler Principles Lecture 8: Loop Optimizations
Topic 10: Dataflow Analysis
Factored Use-Def Chains and Static Single Assignment Forms
EECS 583 – Class 6 More Dataflow Analysis
EECS 583 – Class 8 Classic Optimization
Optimizations using SSA
Data Flow Analysis Compiler Design
EECS 583 – Class 7 Static Single Assignment Form
Static Single Assignment
EECS 583 – Class 8 Finish SSA, Start on Classic Optimization
EECS 583 – Class 4 If-conversion
EECS 583 – Class 4 If-conversion
EECS 583 – Class 5 Dataflow Analysis
EECS 583 – Class 7 Static Single Assignment Form
EECS 583 – Class 4 If-conversion
Presentation transcript:

EECS 583 – Class 6 Dataflow Analysis University of Michigan September 24, 2012

- 1 - Announcements & Reading Material v Homework 1 due tonight at midnight »Remember, submit single.tgz file to andrew.eecs.umich.edu:/y/submit/uniquename_hw1.tgz v Roadmap »Next 5 classes focus on program analysis + optimization »HW2 will focus on optimization in LLVM v Today’s class »Compilers: Principles, Techniques, and Tools, A. Aho, R. Sethi, and J. Ullman, Addison-Wesley, (Sections: 10.5, 10.6, 10.9, 10.10) v Material for Wednesday »“Practical Improvements to the Construction and Destruction of Static Single Assignment Form,” P. Briggs, K. Cooper, T. Harvey, and L. Simpson, Software--Practice and Experience, 28(8), July 1998, pp

- 2 - From Last Time: Dataflow Analysis Introduction Which VRs contain useful data values? (liveness or upward exposed uses) Which definitions may reach this point? (reaching defns) Which definitions are guaranteed to reach this point? (available defns) Which uses below are exposed? (downward exposed uses) Pick an arbitrary point in the program r1 = r2 + r3 r6 = r4 – r5 r4 = 4 r6 = 8 r6 = r2 + r3 r7 = r4 – r5 Dataflow analysis – Collection of information that summarizes the creation/destruction of values in a program. Used to identify legal optimization opportunities.

- 3 - Live Variable (Liveness) Analysis v Defn: For each point p in a program and each variable y, determine whether y can be used before being redefined starting at p v Algorithm sketch »For each BB, y is live if it is used before defined in the BB or it is live leaving the block »Backward dataflow analysis as propagation occurs from uses upwards to defs v 4 sets »GEN = set of external variables consumed in the BB »KILL = set of external variable uses killed by the BB  equivalent to set of variables defined by the BB »IN = set of variables that are live at the entry point of a BB »OUT = set of variables that are live at the exit point of a BB

- 4 - Computing GEN/KILL Sets For Each BB for each basic block in the procedure, X, do GEN(X) = 0 KILL(X) = 0 for each operation in reverse sequential order in X, op, do for each destination operand of op, dest, do GEN(X) -= dest KILL(X) += dest endfor for each source operand of op, src, do GEN(X) += src KILL(X) -= src endfor

- 5 - Compute IN/OUT Sets for all BBs initialize IN(X) to 0 for all basic blocks X change = 1 while (change) do change = 0 for each basic block in procedure, X, do old_IN = IN(X) OUT(X) = Union(IN(Y)) for all successors Y of X IN(X) = GEN(X) + (OUT(X) – KILL(X)) if (old_IN != IN(X)) then change = 1 endif endfor

- 6 - Example – Liveness Computation OUT = Union(IN(succs)) IN = GEN + (OUT – KILL ) 1. r1 = MEM[r2+0] 2. r2 = MEM[r1 + 1] 3. r8 = r1 * r2 4. r1 = r r3 = r5 – r1 6. r7 = r3 * 2 7. r2 = 0 8. r7 = r1 + r2 9: r3 = 4 10: r3 = r3 + r7 11: r1 = r2 – r8 12: r3 = r1 * 2 BB1 BB2BB3 BB4

- 7 - Class Problem 1. r1 = 3 2. r2 = r3 3. r3 = r4 4. r1 = r r7 = r1 * r2 6. r4 = r r4 = r3 + r2 8. r8 = 8 9. r9 = r7 + r8 Compute liveness Calculate GEN/KILL for each BB Calculate IN/OUT for each BB

- 8 - Reaching Definition Analysis (rdefs) v A definition of a variable x is an operation that assigns, or may assign, a value to x v A definition d reaches a point p if there is a path from the point immediately following d to p such that d is not “killed” along that path v A definition of a variable is killed between 2 points when there is another definition of that variable along the path »r1 = r2 + r3 kills previous definitions of r1 v Liveness vs Reaching defs »Liveness  variables (e.g., virtual registers), don’t care about specific users »Reaching defs  operations, each def is different »Forward dataflow analysis as propagation occurs from defs downwards (liveness was backward analysis)

- 9 - Compute Rdef GEN/KILL Sets for each BB for each basic block in the procedure, X, do GEN(X) = 0 KILL(X) = 0 for each operation in sequential order in X, op, do for each destination operand of op, dest, do G = op K = {all ops which define dest – op} GEN(X) = G + (GEN(X) – K) KILL(X) = K + (KILL(X) – G) endfor GEN = set of definitions created by an operation KILL = set of definitions destroyed by an operation - Assume each operation only has 1 destination for simplicity so just keep track of “ops”..

Compute Rdef IN/OUT Sets for all BBs initialize IN(X) = 0 for all basic blocks X initialize OUT(X) = GEN(X) for all basic blocks X change = 1 while (change) do change = 0 for each basic block in procedure, X, do old_OUT = OUT(X) IN(X) = Union(OUT(Y)) for all predecessors Y of X OUT(X) = GEN(X) + (IN(X) – KILL(X)) if (old_OUT != OUT(X)) then change = 1 endif endfor IN = set of definitions reaching the entry of BB OUT = set of definitions leaving BB

Example Rdef Calculation 1. r1 = MEM[r2+0] 2. r2 = MEM[r1 + 1] 3. r8 = r1 * r2 4. r1 = r r3 = r5 – r1 6. r7 = r3 * 2 7. r2 = 0 8. r7 = r1 + r2 9. r3 = r3 = r3 + r7 11. r1 = r2 – r8 12. r3 = r1 * 2 BB1 BB2BB3 BB4 IN = Union(OUT(preds)) OUT = GEN + (IN – KILL )

Class Problem 1. r1 = 3 2. r2 = r3 3. r3 = r4 4. r1 = r r7 = r1 * r2 6. r4 = r r4 = r3 + r2 8. r8 = 8 9. r9 = r7 + r8 Compute reaching defs Calculate GEN/KILL for each BB Calculate IN/OUT for each BB

DU/UD Chains v Convenient way to access/use reaching defs info v Def-Use chains »Given a def, what are all the possible consumers of the operand produced »Maybe consumer v Use-Def chains »Given a use, what are all the possible producers of the operand consumed »Maybe producer

Example – DU/UD Chains 1. r1 = 3 2. r2 = r3 3. r3 = r4 4. r1 = r r7 = r1 * r2 6. r4 = r r4 = r3 8. r8 = 8 9. r9 = r7 + r8

Generalizing Dataflow Analysis v Transfer function »How information is changed by “something” (BB) »OUT = GEN + (IN – KILL) /* forward analysis */ »IN = GEN + (OUT – KILL) /* backward analysis */ v Meet function »How information from multiple paths is combined »IN = Union(OUT(predecessors)) /* forward analysis */ »OUT = Union(IN(successors)) /* backward analysis */ v Generalized dataflow algorithm »while (change)  change = false  for each BB u apply meet function u apply transfer functions u if any changes  change = true

Beyond Liveness or Upward Exposed Uses v Upward exposed defs »IN = GEN + (OUT – KILL) »OUT = Union(IN(successors)) »Walk ops reverse order  GEN += dest; KILL += dest v Downward exposed uses »IN = Union(OUT(predecessors)) »OUT = GEN + (IN-KILL) »Walk ops forward order  GEN += src; KILL -= src;  GEN -= dest; KILL += dest; v Downward exposed defs »IN = Union(OUT(predecessors)) »OUT = GEN + (IN-KILL) »Walk ops forward order  GEN += dest; KILL += dest;

What About All Path Problems? v Up to this point »Any path problems (maybe relations)  Definition reaches along some path  Some sequence of branches in which def reaches  Lots of defs of the same variable may reach a point »Use of Union operator in meet function v All-path: Definition guaranteed to reach »Regardless of sequence of branches taken, def reaches »Can always count on this »Only 1 def can be guaranteed to reach »Availability (as opposed to reaching)  Available definitions  Available expressions (could also have reaching expressions, but not that useful)

Reaching vs Available Definitions 1:r1 = r2 + r3 2:r6 = r4 – r5 3:r4 = 4 4:r6 = 8 5:r6 = r2 + r3 6:r7 = r4 – r5 1,2,3,4 reach 1 available 1,2 reach 1,2 available 1,3,4 reach 1,3,4 available 1,2 reach 1,2 available

Available Definition Analysis (Adefs) v A definition d is available at a point p if along all paths from d to p, d is not killed v Remember, a definition of a variable is killed between 2 points when there is another definition of that variable along the path »r1 = r2 + r3 kills previous definitions of r1 v Algorithm »Forward dataflow analysis as propagation occurs from defs downwards »Use the Intersect function as the meet operator to guarantee the all-path requirement »GEN/KILL/IN/OUT similar to reaching defs  Initialization of IN/OUT is the tricky part

Compute GEN/KILL Sets for each BB (Adefs) for each basic block in the procedure, X, do GEN(X) = 0 KILL(X) = 0 for each operation in sequential order in X, op, do for each destination operand of op, dest, do G = op K = {all ops which define dest – op} GEN(X) = G + (GEN(X) – K) KILL(X) = K + (KILL(X) – G) endfor Exactly the same as reaching defs !!!

Compute IN/OUT Sets for all BBs (Adefs) U = universal set of all operations in the Procedure IN(0) = 0 OUT(0) = GEN(0) for each basic block in procedure, W, (W != 0), do IN(W) = 0 OUT(W) = U – KILL(W) change = 1 while (change) do change = 0 for each basic block in procedure, X, do old_OUT = OUT(X) IN(X) = Intersect(OUT(Y)) for all predecessors Y of X OUT(X) = GEN(X) + (IN(X) – KILL(X)) if (old_OUT != OUT(X)) then change = 1 endif endfor

Available Expression Analysis (Aexprs) v An expression is a RHS of an operation »r2 = r3 + r4, r3+r4 is an expression v An expression e is available at a point p if along all paths from e to p, e is not killed v An expression is killed between 2 points when one of its source operands are redefined »r1 = r2 + r3 kills all expressions involving r1 v Algorithm »Forward dataflow analysis as propagation occurs from defs downwards »Use the Intersect function as the meet operator to guarantee the all-path requirement »Looks exactly like adefs, except GEN/KILL/IN/OUT are the RHS’s of operations rather than the LHS’s

Computation of Aexpr GEN/KILL Sets for each basic block in the procedure, X, do GEN(X) = 0 KILL(X) = 0 for each operation in sequential order in X, op, do K = 0 for each destination operand of op, dest, do K += {all ops which use dest} endfor if (op not in K) G = op else G = 0 GEN(X) = G + (GEN(X) – K) KILL(X) = K + (KILL(X) – G) endfor We can also formulate the GEN/KILL slightly differently so you do not need to break up instructions like “r2 = r2 + 1”.

Class Problem - Aexprs Calculation 1: r1 = r6 * r9 2: r2 = r : r5 = r3 * r4 4: r1 = r : r3 = r3 * r4 6: r8 = r3 * 2 7: r7 = r3 * r4 8: r1 = r : r7 = r : r8 = r : r1 = r3 * r4 12: r3 = r6 * r9

Some Things to Think About v Liveness and rdefs are basically the same thing »All dataflow is basically the same with a few parameters  Meaning of gen/kill – src vs dest, variable vs operation  Backward / Forward  All paths / some paths (must/may) u So far, we have looked at may analysis algorithms u How do you adjust to do must algorithms? v Dataflow can be slow »How to implement it efficiently?  Forward analysis – DFS order  Backward analysis – PostDFS order »How to represent the info? v Predicates »Throw a monkey wrench into this stuff »So, how are predicates handled?