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.

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

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.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Instruction Scheduling John Cavazos University.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
SSA.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 More Control Flow John Cavazos University.
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.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
Lazy Code Motion Comp 512 Spring 2011
Partial Redundancy Elimination & Lazy Code Motion
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA Emery Berger University.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
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.
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.
Data Flow Analysis Compiler Design Nov. 8, 2005.
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.
Global Common Subexpression Elimination with Data-flow Analysis Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Structural Data-flow Analysis Algorithms: Allen-Cocke Interval Analysis Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
Introduction to Optimization, II Value Numbering & Larger Scopes Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
First Principles (with examples from value numbering) C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Register Allocation John Cavazos University.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Dependence Analysis and Loop Transformations.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
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.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
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.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Terminology, Principles, and Concerns, II With examples from superlocal value numbering (Ch 8 in EaC2e) Copyright 2011, Keith D. Cooper & Linda Torczon,
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment John Cavazos.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Iterative Data-flow Analysis C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
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.
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
11/22/2016© Hal Perkins & UW CSER-1 CSE P 501 – Compilers Dataflow Analysis Hal Perkins Autumn 2009.
Introduction to Optimization
Global Redundancy Elimination: Computing Available Expressions COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda.
Machine-Independent Optimization
Dataflow Analysis Hal Perkins Autumn 2011
Topic 10: Dataflow Analysis
Introduction to Optimization
Unit IV Code Generation
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.
Optimization through Redundancy Elimination: Value Numbering at Different Scopes COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith.
Data Flow Analysis Compiler Design
Dataflow Analysis Hal Perkins Winter 2008
Introduction to Optimization
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
Optimizing Compilers CISC 673 Spring 2009 Data flow analysis
Presentation transcript:

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 of Delaware

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 2 Data flow analysis Solving set of equations posed over graph representation (e.g., CFG) Based on any or all paths through program “Any path” or “All path” problems

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 3 Includes Infeasible Paths a = 1; if (a == 0) { a = 1; } if (a == 0) { a = 2; } Infeasible paths never actually taken by program, regardless of input Undecidable to distinguish from feasible

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 4 Data Flow-Based Optimizations Dead variable elimination a = 3; print a; x = 12; halt ) a = 3; print a; halt Copy propagation x = y; … use of x ) …use of y Partial redundancy a = 3*c + d; b = 3*c ) b = 3*c; a=b+d Constant propagation a = 3; b = 2; c = a+b ) a = 3; b = 2; c = 5

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 5 Example: Redundancy Elimination Expression e at point p redundant iff every path from procedure’s entry to p contains evaluation of e and value(s) of e’s operands do not change between those earlier evaluations and p  Evaluating e at p always produces the same value as those earlier evaluations

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 6 Example m  a + b n  a + b A p  c + d r  c + d B q  a + b r  c + d C

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 7 Redundancy Elimination If the compiler can prove expression redundant Replace redundant evaluation with reference Problem Proving x+y is redundant Eliminate redundant evaluation Can use value numbering

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 8 Value Numbering Key notion Assign a number, V(n), to each expression  V(x+y) = V(j)  iff x+y and j have same value  inputs  Hash on value numbers to make efficient Use numbers to improve the code

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 9 Local Value Numbering The algorithm For each expression e in the block 1 Get value numbers for operands o 1 and o 2 from hash lookup 2 Hash to get value number for e 3 If e already had a value number, replace e with a reference 4 If o 1 & o 2 are constant, evaluate it & use a “load immediate” Local  one block at a time

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 10 Local Value Numbering Example With VNs a 0 3  x y 0 2  b 0 3  x y 0 2 a 1 4  17  c 0 3  x y 0 2 Original Code a 0  x 0 + y 0  b 0  x 0 + y 0 a 1  17  c 0  x 0 + y 0 1. Renaming: Give each value a unique name While complex, the meaning is clear Rewritten a 0 3  x y 0 2  b 0 3  a 0 3 a 1 4  17  c 0 3  a Result: a 0 3 is available rewriting works

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 11 Global Redundancy Elimination u  e + f D E x  e + f F e+f is redundant Find common subexpressions beyond basic blocks, and eliminate unnecessary re-evaluations

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 12 Expression “available” is as follows: Expression defined at point p if value computed at p Expression killed at point p if one or more operands defined at point p e available at p if every path leading to p contains a prior definition of e and e is not killed between definition and p

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 13 Expression “available” visually:

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 14 “Available” Expressions for GCSE GCSE = Global Common Subexpression Elimination Mechanism System of simultaneous equations over the CFG Solve the equations to produce a set for each CFG node Contains names of every expression available on entry Use these sets, AVAIL(n), for redundancy elimination

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 15 “Available” Expressions for GCSE Safety x+y  AVAIL(n) proves earlier value x+y is same Transformation provides name for each value Several schemes for this mapping

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 16 “Available” Expressions for GCSE Profitability Do not add any evaluations Add some copy operations Copies are inexpensive Many copies coalesce away

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 17 Computing Available Expressions For each block b Let AVAIL(b) be the set of expressions available on entry to b AVAIL constructed from local sets Let EXPRKILL(b) be the set of expression killed in b Let DEEXPR(b) be the set of downward exposed expressions  x  DEEXPR(b)  x defined in b & not subsequently killed in b

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 18 Computing Available Expressions Now, AVAIL(b) can be defined as: AVAIL(b) =  p  pred(b) (DEEXPR(p)  (AVAIL(p)  EXPRKILL(p) )) AVAIL(n 0 ) = Ø where preds(b) is the set of b’s predecessors in the CFG This system of simultaneous equations forms a data-flow problem  Solve it with a data-flow algorithm Entry node in CFG is n 0

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 19 Computing Available Expressions ……………… DEEXPR (p) And not later killed Downward exposed expressions Basic Block p AVAIL(b) =  p  pred(b) (DEEXPR(p)  (AVAIL(p)  EXPRKILL(p) ))

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 20 Computing Available Expressions Expressions that Pass through unscathed ……………… AVAIL(p)  EXPRKILL(p) AVAIL(p) EXPRKILL(p) Available upon entry Any expresssions killed Basic Block p AVAIL(b) =  p  pred(b) (DEEXPR(p)  (AVAIL(p)  EXPRKILL(p) ))

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 21 Available Expressions for GCSE

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 22 Available Expressions for GCSE The Big Picture 1.  block b, compute AVAIL(b) 2. Assign unique global names to expressions in AVAIL(b) 3.  block b, local value number b starting with AVAIL(b)

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 23 Compute DEExpr for each Block b assume a block b with operations o 1, o 2, …, o k V AR K ILL  Ø DEE XPR (b)  Ø for i = k to 1 // from last to first insts assume o i is “x  y + z” add x to V AR K ILL if (y  V AR K ILL ) and (z  V AR K ILL ) then add “y + z” to DEE XPR (b) } Compute DEExpr

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 24 Compute ExprKILL for each Block b E XPR K ILL (b)  Ø For each expression e in procedure for each variable v  e if v  V AR K ILL (b) then E XPR K ILL (b)  E XPR K ILL (b)  {e } } Compute ExprKill

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 25 Example v  a + b a  c + d x  e + f DEExpr = { c+d,e+f } VarKill = {v,a,x} ExprKill = {a+b}

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 26 Compute Available Expressions for all blocks b compute DEExpr(b) and EXPRKILL(b) Changed=true while (Changed) Changed=false for all blocks b OldValue = A VAIL (b) A VAIL (b) =  p  pred(b) (DEE XPR (p)  (A VAIL (p)  E XPR K ILL (p) )) if A VAIL (b ) != OldValue Changed=true

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 27 Example: Compute DEEXPR m  a + b n  a + b A p  c + d r  c + d B y  a + b z  c + d G q  a + b r  c + d C e  b + 18 s  a + b u  e + f D e  a + 17 t  c + d u  e + f E v  a + b w  c + d x  e + f F assume a block b with operations o 1, o 2, …, o k VARKILL  Ø DEEXPR(b)  Ø for i = k to 1 // from last to first insts assume o i is “x  y + z” add x to VARKILL if (y  VARKILL) and (z  VARKILL) then add “y + z” to DEEXPR(b)

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 28 Example: Compute EXPRKILL m  a + b n  a + b A p  c + d r  c + d B y  a + b z  c + d G q  a + b r  c + d C e  b + 18 s  a + b u  e + f D e  a + 17 t  c + d u  e + f E v  a + b w  c + d x  e + f F EXPRKILL(b)  Ø For each expression e in procedure for each variable v  e if v  VARKILL(b) then EXPRKILL(b)  EXPRKILL(b)  {e }

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 29 Example AVAIL(A) = Ø AVAIL(B) = {a+b}  (Ø  all) = {a+b} AVAIL(C) = {a+b} AVAIL(D) = {a+b,c+d}  ({a+b}  all) = {a+b,c+d} AVAIL(E) = {a+b,c+d} AVAIL(F) = [{b+18,a+b,e+f}  ({a+b,c+d}  {all - e+f})]  [{a+17,c+d,e+f}  ({a+b,c+d}  {all - e+f})] = {a+b,c+d,e+f} AVAIL(G)= [ {c+d}  ({a+b}  all)]  [{a+b,c+d,e+f}  ({a+b,c+d,e+f}  all)] = {a+b,c+d} m  a + b n  a + b A p  c + d r  c + d B y  a + b z  c + d G q  a + b r  c + d C e  b + 18 s  a + b u  e + f D e  a + 17 t  c + d u  e + f E v  a + b w  c + d x  e + f F  p  pred(b) (DEEXPR(p)  (AVAIL(p)  EXPRKILL(p) ))

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 30 Example m  a + b n  a + b A p  c + d r  c + d B y  a + b z  c + d G q  a + b r  c + d C e  b + 18 s  a + b u  e + f D e  a + 17 t  c + d u  e + f E v  a + b w  c + d x  e + f F A VAIL sets in blue { a+b } { a+b,c+d } { a+b,c+d,e+f } { a+b,c+d } { a+b } 

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 31 Remember Big Picture The Big Picture 1.  block b, compute AVAIL(b) 2. Assign unique global names to expressions in AVAIL(b) 3.  block b, value number b starting with AVAIL(b) We’ve done step 1.

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 32 Global CSE (replacement step) Compute a static mapping from expression to name After analysis & before transformation   b,  e  AVAIL(b), assign e a global name by hashing on e

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 33 Example m  a + b n  a + b A p  c + d r  c + d B y  a + b z  c + d G q  a + b r  c + d C e  b + 18 s  a + b u  e + f D e  a + 17 t  c + d u  e + f E v  a + b w  c + d x  e + f F { a+b } { a+b,c+d } { a+b,c+d,e+f } { a+b,c+d } { a+b }  Assigning unique names to global CSEs a+b  t 1 c+d  t 2 e+f  t 3

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 34 Remember the Big Picture The Big Picture 1.  block b, compute AVAIL(b) 2. Assign unique global names to expressions in AVAIL(b) 3.  block b, value number b starting with AVAIL(b) We’ve done steps 1 & 2.

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 35 Value Numbering To perform replacement, value numbering each block b Initialize hash table with AVAIL(b) Replace an expression in AVAIL(b) means copy from its name At each evaluation of a global name, copy new value to its name Otherwise, value number as in last lecture

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 36 Net Result Catches local redundancies with value numbering Catches nonlocal redundancies because of AVAIL sets Not quite same effect, but close  Local redundancies found by value  Global redundancies found by spelling

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 37 Example m  a + b t 1  m n  t 1 A p  c + d t 2  p r  t 2 B y  t 1 z  t 2 G q  t 1 r  c + d t 2  r C e  b + 18 s  t 1 u  e + f t 3  u D e  a + 17 t  t 2 u  e + f t 3  u E v  t 1 w  t 2 x  t 3 F After replacement & local value numbering

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 38 Example m  a + b t 1  m n  t 1 A p  c + d t 2  p r  t 2 B y  t 1 z  t 2 G q  t 1 r  c + d t 2  r C e  b + 18 s  t 1 u  e + f t 3  u D e  a + 17 t  t 2 u  e + f t 3  u E v  t 1 w  t 2 x  t 3 F In practice, most of these copies will be folded into subsequent uses… u p r r m m m mr m

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 39 Some Copies Serve a Purpose In the example, all the copies coalesce away. Sometimes, the copies are needed. Copies into t 1 create a common name along two paths Makes the replacement possible w  a + bw  a + bx  a + bx  a + b y  a + by  a + b w  a + bt1  ww  a + bt1  w x  a + bt1  xx  a + bt1  x y  t1y  t1  Cannot write “ w or x ”

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 40 Example m  a + b n  a + b A p  c + d r  c + d B y  a + b z  c + d G q  a + b r  c + d C e  b + 18 s  a + b u  e + f D e  a + 17 t  c + d u  e + f E v  a + b w  c + d x  e + f F LVN GRE

U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT 41 Next Time More Data Flow