1 / 41

Optimizing Compilers CISC 673 Spring 2011 Data flow analysis

Optimizing Compilers CISC 673 Spring 2011 Data flow analysis. John Cavazos University of Delaware. 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.

Download Presentation

Optimizing Compilers CISC 673 Spring 2011 Data flow analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Optimizing CompilersCISC 673Spring 2011Data flow analysis John Cavazos University of Delaware

  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

  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

  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

  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

  6. A m  a + b n  a + b q  a + b r  c + d B p  c + d r  c + d Example C

  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

  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

  9. Local Value Numbering Local  one block at a time • The algorithm • For each expression e in the block • Get value numbers for operands o1 and o2 from hash lookup • Hash <operator,VN(o1),VN(o2)> to get value number for e • If e already had a value number, replace e with a reference • If o1 & o2 are constant, evaluate it & use a “load immediate”

  10. Original Code a0 x0 + y0  b0  x0 + y0 a1  17  c0  x0 + y0 Rewritten a03 x01 + y02  b03  a03 a14  17  c03  a03 • 1. Renaming: • Give each value a unique name • While complex, the meaning is clear • 2. Result: • a03 is available • rewriting works Local Value Numbering Example With VNs a03 x01 + y02  b03  x01 + y02 a14  17  c03  x01 + y02

  11. D u  e + f E u  e + f F x  e + f e+f is redundant Global Redundancy Elimination Find common subexpressions beyond basic blocks, and eliminate unnecessary re-evaluations

  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

  13. Expression “available” visually:

  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

  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

  16. Copies are inexpensive • Many copies coalesce away “Available” Expressions for GCSE • Profitability • Do not add any evaluations • Add some copy operations

  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 killedin b • Let DEEXPR(b) be the set of downward exposed expressions • x  DEEXPR(b)  x defined in b & not subsequently killed in b

  18. Computing Available Expressions • Now, AVAIL(b) can be defined as: • AVAIL(b) = ppred(b)(DEEXPR(p) (AVAIL(p)  EXPRKILL(p) )) • AVAIL(n0) = Ø • 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 n0

  19. Computing Available Expressions AVAIL(b) = ppred(b)(DEEXPR(p) (AVAIL(p)  EXPRKILL(p) )) Basic Block p … … … DEEXPR (p) Downward exposed expressions And not later killed

  20. Computing Available Expressions AVAIL(b) = ppred(b)(DEEXPR(p) (AVAIL(p)  EXPRKILL(p) )) Available upon entry AVAIL(p) Basic Block p … … … EXPRKILL(p) Any expresssions killed AVAIL(p)  EXPRKILL(p) Expressions that Pass through unscathed

  21. Available Expressions for GCSE

  22. Available Expressions for GCSE The Big Picture  block b, compute AVAIL(b) Assign unique global names to expressions in AVAIL(b)  block b, local value number b starting with AVAIL(b)

  23. } Compute DEExpr Compute DEExpr for each Block b assume a block b with operations o1, o2, …, ok VARKILL Ø DEEXPR(b)  Ø for i = k to 1 // from last to first insts assume oi is “x  y + z” add x to VARKILL if (y  VARKILL) and (z  VARKILL) then add “y + z” to DEEXPR(b)

  24. } Compute ExprKill Compute ExprKILL for each Block b EXPRKILL(b)  Ø For each expression e in procedure for each variable v  e if v  VARKILL(b) then EXPRKILL(b)  EXPRKILL(b)  {e}

  25. Example v  a + b a  c + d x  e + f DEExpr = {c+d,e+f } VarKill = {v,a,x} ExprKill = {a+b}

  26. Compute Available Expressions for all blocks b compute DEExpr(b) and EXPRKILL(b) Changed=truewhile (Changed) Changed=false for all blocks b OldValue = AVAIL(b) AVAIL(b) = ppred(b)(DEEXPR(p) (AVAIL(p)  EXPRKILL(p) )) if AVAIL(b) != OldValue Changed=true

  27. C q  a + b r  c + d A m  a + b n  a + b B p  c + d r  c + d D e  b + 18 s  a + b u  e + f E e  a + 17 t  c + d u  e + f F v  a + b w  c + d x  e + f y  a + b z  c + d G Example: Compute DEEXPR assume a block b with operations o1, o2, …, ok VARKILL  Ø DEEXPR(b)  Ø for i = k to 1 // from last to first insts assume oi is “x  y + z” add x to VARKILL if (y  VARKILL) and (z  VARKILL) then add “y + z” to DEEXPR(b)

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

  29. A m  a + b n  a + b C q  a + b r  c + d B p  c + d r  c + d D e  b + 18 s  a + b u  e + f E e  a + 17 t  c + d u  e + f F v  a + b w  c + d x  e + f G y  a + b z  c + d Example ppred(b)(DEEXPR(p) (AVAIL(p)  EXPRKILL(p) )) 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}

  30. A m  a + b n  a + b C q  a + b r  c + d B p  c + d r  c + d D e  b + 18 s  a + b u  e + f E e  a + 17 t  c + d u  e + f F v  a + b w  c + d x  e + f G y  a + b z  c + d Example  AVAILsets in blue { a+b } { a+b } { a+b,c+d } { a+b,c+d } { a+b,c+d,e+f } { a+b,c+d }

  31. Remember Big Picture The Big Picture  block b, compute AVAIL(b) Assign unique global names to expressions in AVAIL(b)  block b, value number b starting with AVAIL(b) We’ve done step 1.

  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

  33. A m  a + b n  a + b C q  a + b r  c + d Assigning unique names to global CSEs a+b  t1 c+d  t2 e+f  t3 B p  c + d r  c + d D e  b + 18 s  a + b u  e + f E e  a + 17 t  c + d u  e + f F v  a + b w  c + d x  e + f G y  a + b z  c + d Example  { a+b } { a+b } { a+b,c+d } { a+b,c+d } { a+b,c+d,e+f } { a+b,c+d }

  34. Remember the Big Picture The Big Picture  block b, compute AVAIL(b) Assign unique global names to expressions in AVAIL(b)  block b, value number b starting with AVAIL(b) We’ve done steps 1 & 2.

  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

  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

  37. A m  a + b t1 m n  t1 B C p  c + d t2  p r  t2 q  t1 r  c + d t2  r D E e  b + 18 s  t1 u  e + f t3 u e  a + 17 t  t2 u  e + f t3 u F v  t1 w  t2 x  t3 G y  t1 z  t2 Example After replacement & local value numbering

  38. A m  a + b t1 m n  t1 m m r m m m p B C p  c + d t2  p r  t2 q  t1 r  c + d t2  r D E e  b + 18 s  t1 u  e + f t3 u e  a + 17 t  t2 u  e + f t3 u F v  t1 w  t2 x  t3 r u r G y  t1 z  t2 Example In practice, most of these copies will be folded into subsequent uses…

  39. wa+b t1w xa+b t1x wa+b xa+b ya+b Cannot write “w or x” yt1 Some Copies Serve a Purpose • In the example, all the copies coalesce away. • Sometimes, the copies are needed. • Copies into t1 create a common name along two paths • Makes the replacement possible 

  40. A m  a + b n  a + b C q  a + b r  c + d B p  c + d r  c + d D e  b + 18 s  a + b u  e + f E e  a + 17 t  c + d u  e + f F v  a + b w  c + d x  e + f G y  a + b z  c + d Example LVN GRE LVN GRE GRE GRE GRE GRE GRE GRE

  41. Next Time • More Data Flow

More Related