1 / 71

Data-Flow Analysis (Chapter 8)

Data-Flow Analysis (Chapter 8). Mooly Sagiv. Outline. What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts: Lattices, Flow-Functions, and Fixed Points Taxonomy of Data-Flow Problems and Solutions Iterative Data-Flow Analysis

liam
Télécharger la présentation

Data-Flow Analysis (Chapter 8)

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. Data-Flow Analysis(Chapter 8) Mooly Sagiv

  2. Outline • What is Data-Flow Analysis? • Structure of an optimizing compiler • An example: Reaching Definitions • Basic Concepts: Lattices, Flow-Functions, and Fixed Points • Taxonomy of Data-Flow Problems and Solutions • Iterative Data-Flow Analysis • Structural Data-Flow Analysis • DU-Chains and SSA

  3. Data-Flow Analysis • Input: A control flow graph • Output:A control flow graph with “global” information at every basic blockExamples • Constant expressions: x+y*z • Live variables

  4. Compiler Structure String of characters Scanner tokens Parser Symbol table and access routines OS Interface AST Semantic analyzer IR Code Generator Object code

  5. Optimizing Compiler Structure String of characters Front-End IR Control Flow Analysis CFG Data Flow Analysis CFG+information Program Transformations Object code IR instruction selection

  6. An Example Reaching Definitions • A definition --- an assignment to variable • An assignment d reaches a basic block if there exists an execution path to the basic block in which the value assigned at d is still active at the basic block

  7. Running Example unsigned int fib(unsigned int m) {unsigned int f0=0, f1=1, f2, i; if (m <= 1) { return m; } else { for (i=2, i <=m, i++) { f2=f0+f1; f0=f1; f1 =f2;} return f2; } } 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m

  8. entry 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m   2,3 2,3, 5,8,9, 10, 11 2,3, 5, 8,9, 10, 11 2,3, 5, 8,9, 10, 11 2,3, 5,8,9, 10, 11 2,3 exit

  9. Difficulties in Data-Flow Analysis • Input-dependent information • Undecidability of program analysis • Reachability of basic blocks • Arithmetic • ...

  10. 1 int g(int m, int i) 2 int f(int n) 3 { int i=0; 4 if (n == 1) i = 2 5 while (n > 0) { 6 j = i+1; 7 n = g(n, i); 8 } 9 return j 10 }

  11. Conservative data-flow analysis • Every piece of data-flow information is sound • Every enabled optimization is correct • A superset of the execution sequences is considered • In the reaching definition example a superset of the reaching definitions is computed

  12. 1 int g(int m, int i) 2 int f(int n) 3 { int i=0; 4 if (n == 1) i = 2 5 while (n > 0) { 6 j = i+1; 7 n = g(n, i); 8 } 9 return j 10 }

  13. Iterative Computation of Reaching Definitions • Optimistically assume that at every block no definition is reached • Every basic block “generates” new definitions and “preserves” other definitions • No definition reaches ENTRY • Accumulate reaching definitions along different paths • Iteratively compute more and more definitions at every basic block • The process must terminate • The final solution is unique and conservative

  14. Iterative Computation of Reaching Definitions RCin(ENTRY) =  RCin(B) = B’  Pred(B) RCout(B’) RCout(B) = GEN(B)  (RCin(B)PRSV(B))

  15. entry 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m   2,3 2,3, 5,8,9, 10, 11 2,3, 5, 8,9, 10, 11 2,3, 5, 8,9, 10, 11 2,3, 5,8,9, 10, 11 2,3 exit

  16. Iterative Computation of Reaching Definitions Using Bit-Vectors • Represent every definition with a bit • PRSV and GEN are bit-vectors RCin(ENTRY) = <000...0> RCin(B) = B’  Pred(B) RCout(B’) RCout(B) = GEN(B)  (RCin(B) PRSV(B))

  17. entry 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m   2,3 2,3, 5,8,9, 10, 11 2,3, 5, 8,9, 10, 11 2,3, 5, 8,9, 10, 11 2,3, 5,8,9, 10, 11 2,3 exit

  18. Complete Join-Lattices • A set L of “data-flow” information • A partial order  on the elements of L • x  y  x “covers” less states than y x is more precise than y •  is the minimum element • height of a lattice  length of maximal strictly increasing chain x1x2...  xk • A “join” confluence operator : LLL • x  xy, y  xy • x  z, y  z  xy  z • Examples: Powersets, Bit-Vectors, ICP

  19. Properties of Lattices •   x = x   = x • x  x = x (reflexivity) • x  y = y  x (commutativity) • (x  y )  z = x  (y  z) (associativity)

  20. Functions on Lattices • Models effects of basic blocks • A monotonic function f: L  L x  y  f(x)  f(y) • A distributive function f: L  L f(x)  f(y) = f(x  y) • A fixed point of a function f, f(x) = x • For a monotonic function f the effective height of L w.r.t. f, the longest increasing chainf()f2()=f(f())...  fk() = lfp(f)

  21. The Join (Meet) Over All Paths • A data-flow solution which is precise under the assumption that every control flow path is executable • For a path [B1, B2, ... Bn] Fp = FBn ... FB2FB1 • The JOP at a block BJOP(B) =  P  Path(B) Fp(Init) • For distributive Fp compute JOP • Otherwise, find X(B) JOP(B)

  22. entry w > 0? N Y u 1 v 2 u 2 v 1 w u+v exit

  23. Dimensions for Data-Flow Problems • The information provided • “ralational” Vs. independent attributes • The type of lattice and functions usedpowersets, ICP, ..., unbounded heights • The direction of information flowforward, backward, bidirectional

  24. Example Data-Flow Problems • Reaching Definitions • Available Expressions • Live Variables • Upward Exposed Uses • Copy-Propagation Analysis • Constant-Propagation Analysis • Partial-Redundency Analysis

  25. entry z > 1? N Y x 1 z > y x 2 N Y z x-3 y x+1 exit

  26. Data-Flow Analysis Algorithms • Allen’s strongly connected regions • Kildall’s iterative algorithm • Ullman’s T1-T2 analysis • Kennedy’s node-listing algorithm • Farrow, Kennedy, and Zuconi’s graph grammar approach • Rosen’s high-level approach • structural analysis • slotwise analysis

  27. Iterative Data-Flow Analysis in(ENTRY) = Init In(B) = B’  Pred(B) Out(B’) Out(B) = FB(In(B))

  28. Iterative Data-Flow Algorithm Input:aflow graph G=(N,E,r) An init value Init A montonic function FB for every B in N Output:For every N in(N) Initializatio: in(Entry) := Init; for each node B in N-{Entry} do in(B) := WL := N - {Entry} Iteration: while WL != {} do Select and remove an B from WL out := FB(in(B)) For all B’ in succ(B) such that in(B’) != in(B’) out do in(B’):= in(B’)  out WL := WL {B’}

  29. Post-ordering Input:aflow graph G=(N,E,r) Output:a depth-first spanning tree (N,T) andordering Post ofN Method:T := Ø; for each node n in N do mark n unvisited; i := 1; call DFS(r) Using:procedure DFS(n) is mark n visited; for each n ® s in E doif s is not visited then add the edge n ® s to T; call DFS(s); Post(n) := i; i := i + 1;

  30. entry 8 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m 7 6 5 3 4 1 2 exit

  31. entry 1 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m {} {} 2 {2, 3} 3 {2, 3, 5} 4 {2, 3, 5} 6 {2, 3, 5} 5 8 {2, 3} 7 exit

  32. entry 1 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m {} {} 2 {2, 3} 3 {2, 3, 5, 8, 9, 10} 4 {2, 3, 5} 6 {2, 3, 5} 5 8 {2, 3} 7 exit

  33. entry 1 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m 2 {2, 3} 3 {2, 3, 5, 8, 9, 10} 4 6 {2, 3, 5, 8, 9, 10} {2, 3, 5, 8, 9, 10} 5 8 {2, 3} 7 exit

  34. entry 1 1: receive m(val) 2: f0  0 3: f1  1 4: if m <= 1 goto L3 5: i  2 6: L1: if i <=m goto L2 7: return f2 8: L2: f2  f0 + f1 9: f0  f1 10: f1  f2 11: i  i + 1 12: goto L1 13: L3: return m 2 {2, 3} 3 {2, 3, 5, 8, 9, 10} 4 6 {2, 3, 5, 8, 9, 10} {2, 3, 5, 8, 9, 10} 5 {2, 3, 5, 8, 9, 10} 8 {2, 3} 7 exit

  35. Iterative Backward Data-Flow Analysis Out(Exit) = Init Out(B) = B’  Succ(B) In(B’) In(B) = FB(Out(B))

  36. Lattices of Flow Functions • For a lattice L LF are the montonic functions from L to L f LF  x  y f(x)  f(y) • LF is a lattice with the order f  g for all z: f(z) g(z) • F(z)  • (x F y)(z) x(z) y(z) • LF is closed under composition (f g)(z) f(g(z)) • f0id, fn f fn-1 • f*(z)limn  (id  f)n (z)

  37. Structural Data-Flow Analysis • Phase 1: Compute “the effect” of every program construct in a bottom-up fashion on the tree of control flow constructs(control-tree) • Phase 2: Propagates the data-flow value in a top-down fashion into basic blocks

  38. if Fif/Y Fif/N then if-then Fif-then Fthen Bottom-Up Phase(if-then) Fif-then=(F then° Fif/Y)  Fif/N

  39. if Fif Fif then if-then Fif-then Fthen Bottom-Up Phase(Simplified if-then) Fif-then=(F then° Fif)  Fif

  40. if FG1, P1 FG1, P1 then if-then FG, P FG2, P2 Bottom-Up PhaseReaching Definitions(Simplified if-then) (F G2, P2° FG1, P1)  FG1, P1 F (G1P2)G2, P1  P2 FG1, P1 F G1G2, P1

  41. if Fif/Y Fif/N then if-then Fif-then Fthen Top-Down Phase(if-then) in(if-then)=in(if) in(then) = Fif/Y(in(if))

  42. if Fif Fif then if-then Fif-then Fthen Top-Down Phase(Simplified if-then) in(if-then)=in(if) in(then) = Fif (in(if))

  43. if FG1, P1 FG1, P1 then if-then FG,P FG2, P2 Top-Down PhaseReaching Definitions(Simplified if-then) in(if) = in(if-then) in(then) = FG1, P1 (in(if))

  44. if Fif/Y Fif/N if-then-else then else Fif-then-else Fthen Felse Bottom-Up Phase(if-then-else) Fif-then-else=(F then° Fif/Y)  (F else° Fif/N)

  45. if Fif Fif if-then-else then else Fif-then-else Fthen Felse Bottom-Up Phase(simplified if-then-else) Fif-then-else=(F then° Fif)  (F else° Fif)= =(F then F else )° Fif

  46. if FG0, P0 FG0, P0 if-then-else then else FG,P FG1,P1 FG2, P2 Bottom-Up Phase Reaching Definitions(simplified if-then-else) (F G1, P1 F G2, P2 )° FG0, P0 F G1G2, P1P2, ° FG0, P0 F(G0 (P1P2)) G1G2, P0  (P1P2)

  47. if Fif/Y Fif/N if-then-else then else Fif-then-else Fthen Felse Top-Down Phase(if-then-else) in(if)=in(if-then-else) in(then)= Fif/Y (in(if)) in(else)= Fif/N (in(if))

  48. if Fif Fif if-then-else then else Fif-then-else Fthen Felse Top-Down Phase(Simplified if-then-else) in(if)=in(if-then-else) in(then)= Fif (in(if)) in(else)= Fif (in(if))

  49. if FG0, P0 FG0, P0 if-then-else then else FG,P FG1,P1 FG2, P2 Top-Down-Up Phase Reaching Definitions(simplified if-then-else) in(if)=in(if-then-else) in(then)= FG0, P0 (in(if)) in(else)= FG0, P0 (in(if))

  50. while Fwhile/Y while-loop body Fwhile-loop Fbody Bottom-Up Phase(while) Fwhile-loop=Fwhile/N °(F body° Fwhile/Y)* Fwhile/N

More Related