Dataflow Analysis Hal Perkins Winter 2008

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Advertisements

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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.
SSA.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
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.
A Deeper Look at Data-flow Analysis Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University.
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,
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
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 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.
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
1 CS 201 Compiler Construction Data Flow Analysis.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
Global Common Subexpression Elimination with Data-flow Analysis Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
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.
CSE P501 – Compiler Construction
Introduction to Optimization, II Value Numbering & Larger Scopes Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
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.
Global Redundancy Elimination: Computing Available Expressions Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
11/23/2015© Hal Perkins & UW CSEQ-1 CSE P 501 – Compilers Introduction to Optimization Hal Perkins Autumn 2009.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
3/2/2016© Hal Perkins & UW CSES-1 CSE P 501 – Compilers Optimizing Transformations Hal Perkins Autumn 2009.
Iterative Data-flow Analysis C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
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.
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
11/22/2016© Hal Perkins & UW CSER-1 CSE P 501 – Compilers Dataflow Analysis Hal Perkins Autumn 2009.
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
Code Optimization Overview and Examples
Introduction to Optimization
Data Flow Analysis Suman Jana
Lecture 5 Partial Redundancy Elimination
Global Redundancy Elimination: Computing Available Expressions COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda.
Dataflow Analysis Hal Perkins Autumn 2011
Topic 10: Dataflow Analysis
Introduction to Optimization
University Of Virginia
Optimizing Transformations Hal Perkins Autumn 2011
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.
Introduction to Optimization Hal Perkins Summer 2004
Optimizing Transformations Hal Perkins Winter 2008
Optimization through Redundancy Elimination: Value Numbering at Different Scopes COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith.
Static Single Assignment Form (SSA)
Optimizations using SSA
Data Flow Analysis Compiler Design
Introduction to Optimization
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
The Partitioning Algorithm for Detecting Congruent Expressions COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper.
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Optimizing Compilers CISC 673 Spring 2009 Data flow analysis
Presentation transcript:

Dataflow Analysis Hal Perkins Winter 2008 CSE P 501 – Compilers Dataflow Analysis Hal Perkins Winter 2008 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Agenda Initial example: dataflow analysis for common subexpression elimination Other analysis problems that work in the same framework 2/22/2019 © 2002-08 Hal Perkins & UW CSE

The Story So Far… Redundant expression elimination Local Value Numbering Superlocal Value Numbering Extends VN to EBBs SSA-like namespace Dominator VN Technique (DVNT) All of these propagate along forward edges None are global In particular, can’t handle back edges (loops) 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Dominator Value Numbering m0 = a0 + b0 n0 = a0 + b0 Most sophisticated algorithm so far Still misses some opportunities Can’t handle loops B C p0 = c0 + d0 r0 = c0 + d0 q0 = a0 + b0 r1 = c0 + d0 D E e0 = b0 + 18 s0 = a0 + b0 u0 = e0 + f0 e1 = a0 + 17 t0 = c0 + d0 u1 = e1 + f0 Au02: Cooper’s example F e2 = Φ(e0,e1) u2 = Φ(u0,u1) v0 = a0 + b0 w0 = c0 + d0 x0 = e2 + f0 G r2 = Φ(r0,r1) y0 = a0 + b0 z0 = c0 + d0 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Available Expressions Goal: use dataflow analysis to find common subexpressions whose range spans basic blocks Idea: calculate available expressions at beginning of each basic block Avoid re-evaluation of an available expression – use a copy operation 2/22/2019 © 2002-08 Hal Perkins & UW CSE

“Available” and Other Terms An expression e is defined at point p in the CFG if its value is computed at p Sometimes called definition site An expression e is killed at point p if one of its operands is defined at p Sometimes called kill site An expression e is available at point p if every path leading to p contains a prior definition of e and e is not killed between that definition and p 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Available Expression Sets For each block b, define AVAIL(b) – the set of expressions available on entry to b NKILL(b) – the set of expressions not killed in b DEF(b) – the set of expressions defined in b and not subsequently killed in b 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Computing Available Expressions AVAIL(b) is the set AVAIL(b) = xpreds(b) (DEF(x)  (AVAIL(x)  NKILL(x)) ) preds(b) is the set of b’s predecessors in the control flow graph This gives a system of simultaneous equations – a dataflow problem 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Name Space Issues In previous value-numbering algorithms, we used a SSA-like renaming to keep track of versions In global dataflow problems, we use the original namespace The KILL information captures when a value is no longer available 2/22/2019 © 2002-08 Hal Perkins & UW CSE

GCSE with Available Expressions For each block b, compute DEF(b) and NKILL(b) For each block b, compute AVAIL(b) For each block b, value number the block starting with AVAIL(b) Replace expressions in AVAIL(b) with references to the previously computed values 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Global CSE Replacement After analysis and before transformation, assign a global name to each expression e by hashing on e During transformation step At each evaluation of e, insert copy name(e ) = e At each reference to e, replace e with name(e ) 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Analysis Main problem – inserts extraneous copies at all definitions and uses of every e that appears in any AVAIL(b) But the extra copies are dead and easy to remove Useful copies often coalesce away when registers and temporaries are assigned Common strategy Insert copies that might be useful Let dead code elimination sort it out later 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Computing Available Expressions Big Picture Build control-flow graph Calculate initial local data – DEF(b) and NKILL(b) This only needs to be done once Iteratively calculate AVAIL(b) by repeatedly evaluating equations until nothing changes Another fixed-point algorithm 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Computing DEF and NKILL (1) For each block b with operations o1, o2, …, ok KILLED =  DEF(b) =  for i = k to 1 assume oi is “x = y + z” if (y  KILLED and z  KILLED) add “y + z” to DEF(b) add x to KILLED … 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Computing DEF and NKILL (2) After computing DEF and KILLED for a block b, NKILL(b) = { all expressions } for each expression e for each variable v  e if v  KILLED then NKILL(b) = NKILL(b) - e 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Computing Available Expressions Once DEF(b) and NKILL(b) are computed for all blocks b Worklist = { all blocks bi } while (Worklist  ) remove a block b from Worklist recompute AVAIL(b) if AVAIL(b) changed Worklist = Worklist  successors(b) 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Comparing Algorithms LVN – Local Value Numbering m = a + b n = a + b LVN – Local Value Numbering SVN – Superlocal Value Numbering DVN – Dominator-based Value Numbering GRE – Global Redundancy Elimination B C p = c + d r = c + d q = a + b r = c + d D E e = b + 18 s = a + b u = e + f e = a + 17 t = c + d u = e + f Au02: Cooper’s example F v = a + b w = c + d x = e + f G y = a + b z = c + d 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Comparing Algorithms (2) LVN => SVN => DVN form a strict hierarchy – later algorithms find a superset of previous information Global RE finds a somewhat different set Discovers e+f in F (computed in both D and E) Misses identical values if they have different names (e.g., a+b and c+d when a=c and b=d) Value Numbering catches this 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Scope of Analysis Larger context (EBBs, regions, global, interprocedural) sometimes helps More opportunities for optimizations But not always Introduces uncertainties about flow of control Usually only allows weaker analysis Sometimes has unwanted side effects Can create additional pressure on registers, for example 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Code Replication Sometimes replicating code increases opportunities – modify the code to create larger regions with simple control flow Two examples Cloning Inline substitution 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Cloning Idea: duplicate blocks with multiple predecessors Tradeoff More local optimization possibilities – larger blocks, fewer branches But: larger code size, may slow down if it interacts badly with cache 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Original VN Example A B C D E F G m = a + b n = a + b p = c + d r = c + d q = a + b r = c + d D E e = b + 18 s = a + b u = e + f e = a + 17 t = c + d u = e + f Au02: Cooper’s example F v = a + b w = c + d x = e + f G y = a + b z = c + d 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Example with cloning A B C G D E F F G G m = a + b n = a + b p = c + d r = c + d q = a + b r = c + d G y = a + b z = c + d D E e = b + 18 s = a + b u = e + f e = a + 17 t = c + d u = e + f Au02: Cooper’s example F F v = a + b w = c + d x = e + f v = a + b w = c + d x = e + f G G y = a + b z = c + d y = a + b z = c + d 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Inline Substitution Problem: an optimizer has to treat a procedure call as if it (could have) modified all globally reachable data Plus there is the basic expense of calling the procedure Inline Substitution: replace each call site with a copy of the called function body 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Inline Substitution Issues Pro More effective optimization – better local context and don’t need to invalidate local assumptions Eliminate overhead of normal function call Con Potential code bloat Need to manage recompilation when either caller or callee changes 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Dataflow analysis Global redundancy elimination is the first example of a dataflow analysis problem Many similar problems can be expressed in a similar framework Only the first part of the story – once we’ve discovered facts, we then need to use them to improve code 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Dataflow Analysis (1) A collection of techniques for compile-time reasoning about run-time values Almost always involves building a graph Trivial for basic blocks Control-flow graph or derivative for global problems Call graph or derivative for whole-program problems 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Dataflow Analysis (2) Usually formulated as a set of simultaneous equations (dataflow problem) Sets attached to nodes and edges Need a lattice (or semilattice) to describe values In particular, has an appropriate operator to combine values and an appropriate “bottom” or minimal value 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Dataflow Analysis (3) Desired solution is usually a meet over all paths (MOP) solution “What is true on every path from entry” “What can happen on any path from entry” Usually relates to safety of optimization 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Dataflow Analysis (4) Limitations Precision – “up to symbolic execution” Assumes all paths taken Sometimes cannot afford to compute full solution Arrays – classic analysis treats each array as a single fact Pointers – difficult, expensive to analyze Imprecision rapidly adds up For scalar values we can quickly solve simple problems 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Characterizing Dataflow Analysis All of these algorithms involve sets of facts about each basic block b IN(b) – facts true on entry to b OUT(b) – facts true on exit from b GEN(b) – facts created and not killed in b KILL(b) – facts killed in b These are related by the equation OUT(b) = GEN(b)  (IN(b) – KILL(b) Solve this iteratively for all blocks Sometimes information propagates forward; sometimes backward 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Efficiency of Dataflow Analysis The algorithms eventually terminate, but the expected time needed can be reduced by picking a good order to visit nodes in the CFG Forward problems – reverse postorder Backward problems - postorder 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Example:Live Variable Analysis A variable v is live at point p iff there is any path from p to a use of v along which v is not redefined Uses Register allocation – only live variables need a register (or temporary) Eliminating useless stores Detecting uses of uninitialized variables Improve SSA construction – only need Φ-function for variables that are live in a block 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Equations for Live Variables Sets USED(b) – variables used in b before being defined in b NOTDEF(b) – variables not defined in b LIVE(b) – variables live on exit from b Equation LIVE(b) = ssucc(b) USED(s)  (LIVE(s)  NOTDEF(s)) 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Example: Available Expressions This is the analysis we did earlier to eliminate redundant expression evaluation Equation: AVAIL(b) = xpreds(b) (DEF(x)  (AVAIL(x)  NKILL(x)) ) 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Example: Reaching Definitions A definition d of some variable v reaches operation i iff i reads the value of v and there is a path from d to i that does not define v Uses Find all of the possible definition points for a variable in an expression 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Equations for Reaching Definitions Sets DEFOUT(b) – set of definitions in b that reach the end of b (i.e., not subsequently redefined in b) SURVIVED(b) – set of all definitions not obscured by a definition in b REACHES(b) – set of definitions that reach b Equation REACHES(b) = ppreds(b) DEFOUT(p)  (REACHES(p)  SURVIVED(p)) 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Example: Very Busy Expressions An expression e is considered very busy at some point p if e is evaluated and used along every path that leaves p, and evaluating e at p would produce the same result as evaluating it at the original locations Uses Code hoisting – move e to p (reduces code size; no effect on execution time) 2/22/2019 © 2002-08 Hal Perkins & UW CSE

Equations for Very Busy Expressions Sets USED(b) – expressions used in b before they are killed KILLED(b) – expressions redefined in b before they are used VERYBUSY(b) – expressions very busy on exit from b Equation VERYBUSY(b) = ssucc(b) USED(s)  (VERYBUSY(s) - KILLED(s)) 2/22/2019 © 2002-08 Hal Perkins & UW CSE

And so forth… General framework for discovering facts about programs Although not the only possible story Next: what can we do with that information? 2/22/2019 © 2002-08 Hal Perkins & UW CSE