Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Data Flow Analysis Extensions & Foundations.

Similar presentations


Presentation on theme: "Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Data Flow Analysis Extensions & Foundations."— Presentation transcript:

1 Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Data Flow Analysis Extensions & Foundations

2 “Advanced Compiler Techniques” REVIEW  Some Basic Ideas  Reaching Definitions  Available Expressions  Live Variables 2

3 “Advanced Compiler Techniques” REVIEW 3

4 “Advanced Compiler Techniques” REVIEW  Dataflow Analysis Control flow graph IN [ b ], OUT [ b ], transfer functions, join points  Paired analyses and transformations Reaching definitions / constant propagation Available expressions / common sub - expression elimination Live - variable analysis / Reg Alloc & Dead code elimination 4

5 “Advanced Compiler Techniques” Dataflow Analysis  Compile - Time Reasoning About Run - Time Values of Variables or Expressions  At Different Program Points Which assignment statements produced value of variable at this point? Which variables contain values that are no longer used after this program point? What is the range of possible values of variable at this program point? 5

6 Outline  Foundations Data Flow Framework Lattice - Theoretic Formulation Meet - Over - Paths Solution  Extensions Other DFA Methods 6 “Advanced Compiler Techniques”

7 Program Representation  Control Flow Graph Nodes N – statements of program Edges E – flow of control  pred ( n ) = set of all predecessors of n  succ ( n ) = set of all successors of n Start node n 0 Set of final nodes N final  Could also add special nodes ENTRY & EXIT 7

8 Basic Questions of DFA  Under what circumstances is the iterative algorithm used in data - flow analysis correct?  How precise is the solution obtained by the iterative algorithm?  Will the iterative algorithm converge?  What is the meaning of the solution to the equations? 8 “Advanced Compiler Techniques”

9 Basic Idea  Information about program represented using values from algebraic structure called lattice  Analysis produces lattice value for each program point  Two flavors of analysis Forward dataflow analysis Backward dataflow analysis 9

10 Forward Dataflow Analysis  Analysis propagates values forward through control flow graph with flow of control Each node has a transfer function f  Input – value at program point before node  Output – new value at program point after node Values flow from program points after predecessor nodes to program points before successor nodes At join points, values are combined using a merge function  Canonical Example : Reaching Definitions 10 “Advanced Compiler Techniques”

11 Backward Dataflow Analysis  Analysis propagates values backward through control flow graph against flow of control Each node has a transfer function f  Input – value at program point after node  Output – new value at program point before node Values flow from program points before successor nodes to program points after predecessor nodes At split points, values are combined using a merge function  Canonical Example : Live Variables 11 “Advanced Compiler Techniques”

12 DFA Frameworks 12  Generalizes and unifies the DFA examples from previous slide set.  Important components : 1. Direction D : forward or backward. 2. Domain V ( possible values for IN, OUT ). 3. Meet operator ∧ ( effect of path confluence ). 4. Set of possible transfer functions F ( effect of passing through a basic block in the direction D ). “Advanced Compiler Techniques”

13 13 Semilattices V & ∧ form a semilattice if for all x, y, and z in V :  x ∧ x = x ( idempotence ).  x ∧ y = y ∧ x ( commutativity ).  x ∧ ( y ∧ z ) = ( x ∧ y ) ∧ z ( associativity ). Top element ⊤ such that for all x, ⊤∧ x = x. Why? Meet is “ greatest lower bound,” so meet pushes us downward, and ⊤ is above all other elements. Bottom element ⊥ such that for all x, ⊥∧ x = ⊥. Optional and not generally used in the theory. “Advanced Compiler Techniques”

14 14 Example: Semilattice  V = power set of some set.  ∧ = union.  Union is idempotent, commutative, and associative.  What are the top and bottom elements? ⊤ = ∅ ⊥ = U “Advanced Compiler Techniques”

15 Example: Powerset Semilattice 15 {2,3 } { } {1,3}{1,2} {3}{2} {1,2,3} {1} Convention : Draw the meet of elements below both. x y x ∧ y “Advanced Compiler Techniques”

16 Partial Orders  Set P  Partial order ≤ such that  x, y, z  P x ≤ x ( reflexive ) x ≤ y and y ≤ x implies x  y ( asymmetric ) x ≤ y and y ≤ z implies x ≤ z ( transitive )  Can use partial order to define Upper and lower bounds Least upper bound Greatest lower bound 16

17 17 Partial Order for a Semilattice  Say x ≤ y iff x ∧ y = x.  Also, x < y iff x ≤ y and x ≠ y.  ≤ is really a partial order : x ≤ y and y ≤ z imply x ≤ z ( proof in text ). x ≤ y and y ≤ x iff x = y. Proof : If : Idempotence gives us x ≤ x. Only - if : x ∧ y = x and y ∧ x = y ( given ). Thus, x = x ∧ y ( given ) = y ∧ x ( commutativity ) = y ( given ). x ≤ y means there is a path downward from y to x. y x “Advanced Compiler Techniques”

18 Upper Bounds  If S  P then x  P is an upper bound of S if  y  S, y ≤ x x  P is the least upper bound of S if  x is an upper bound of S, and  x ≤ y for all upper bounds y of S  - join, least upper bound ( lub ), supremum, sup   S is the least upper bound of S  x  y is the least upper bound of { x, y } 18

19 “Advanced Compiler Techniques” Lower Bounds  If S  P then x  P is a lower bound of S if  y  S, x ≤ y x  P is the greatest lower bound of S if  x is a lower bound of S, and  y ≤ x for all lower bounds y of S  - meet, greatest lower bound ( glb ), infimum, inf   S is the greatest lower bound of S  x  y is the greatest lower bound of { x, y } 19

20 “Advanced Compiler Techniques” Covering  x  y if x ≤ y and x  y  x is covered by y ( y covers x ) if x  y, and x ≤ z  y implies x  z  Conceptually, y covers x if there are no elements between x and y 20

21 “Advanced Compiler Techniques” Example: Covering  P = { 000, 001, 010, 011, 100, 101, 110, 111} ( standard boolean lattice, also called hypercube )  x ≤ y if ( x bitwise and y ) = x 111 011 101 110 010 001 000 100 Hasse Diagram If y covers x Line from y to x y above x in diagram 21

22 “Advanced Compiler Techniques” Lattices  If x  y and x  y exist for all x, y  P, then P is a lattice.  If  S and  S exist for all S  P, then P is a complete lattice.  All finite lattices are complete  Example of a lattice that is not complete Integers I For any x, y  I, x  y = max ( x, y ), x  y = min ( x, y ) But  I and  I do not exist I  { ,  } is a complete lattice 22

23 “Advanced Compiler Techniques” Lattice Examples  Lattices  Non - lattices 23

24 Partially Ordered Sets and Lattices 24 Partially ordered sets Partial order ≤ is reflexive, transitive, and anti - symmetric A lower bound of x, y is u s. t. u ≤ x and u ≤ y An upper bound of x, y is u s. t. x ≤ u and y ≤ u Lattices Every non - empty finite subset has a greatest lower bound ( glb ) and a least upper bound ( lub ) “Advanced Compiler Techniques”

25 Some Variants of Lattices  A partial order set L is A lattice iff each non - empty finite subset of L has a glb and lub A complete lattice iff each subset of L has a glb and lub A meet semilattice iff each non - empty finite subset of L has a glb A join semilattice iff each non - empty finite subset of L has a lub A bounded lattice iff L is a lattice and has ⊤ and ⊥ elements 25 “Advanced Compiler Techniques”

26 Ascending and Descending Chains  Strictly ascending chain x < y < · · · < z  Strictly descending chain x > y > · · · > z  DCC : Descending Chain Condition All strictly descending chains are finite  ACC : Ascending Chain Condition All strictly ascending chains are finite  If L satisfies ACC and DCC, then L has finite height, and L is complete  A complete lattice need not have finite height 26 “Advanced Compiler Techniques”

27 Variants of Lattices 27 Meet Semilattices with ⊥ element Meet Semilattice s satisfying dcc Join Semilattices satisfyin g acc Join Semilatt ices with ⊤ element Lattices Bounded lattices Complete lattices Complete lattices with dcc and acc “Advanced Compiler Techniques”

28 28 Good News!  Recall the model and RD / AE / LV. RD ’ s : Forward, meet = union, transfer functions based on Gen and Kill. AE ’ s : Forward, meet = intersection, transfer functions based on Gen and Kill. LV ’ s : Backward, meet = union, transfer functions based on Use and Def. “Advanced Compiler Techniques”

29 29 Example: Reaching Definitions  Direction D = forward.  Domain V = set of all sets of definitions in the flow graph.  ∧ = union.  Functions F = all “ gen - kill ” functions of the form f ( x ) = ( x - K ) ∪ G, where K and G are sets of definitions ( members of V ). “Advanced Compiler Techniques”

30 30 Example: RD Satisfies Axioms  Union on a power set forms a semilattice ( idempotent, commutative, associative ).  Identity function : let K = G = ∅.  Composition : A little algebra. f 1 ( x ) = ( x – K 1 ) ∪ G 1 f 2 ( x ) = ( x – K 2 ) ∪ G 2 f 1 ( f 2 ( x )) = ((( x – K 2 ) ∪ G 2 ) – K 1 ) ∪ G 1 = ( x – ( K 1 ∪ K 2 ) ∪ (( G 2 – K 1 ) ∪ G 1 ) G 2, K 2 G 1, K 1 “Advanced Compiler Techniques”

31 31 Example: Partial Order  For RD ’ s, S ≤ T means S ∪ T = S.  Equivalently S ⊇ T. Seems “ backward,” but that ’ s what the definitions give you.  Intuition : ≤ measures “ ignorance.” The more definitions we know about, the less ignorance we have. ⊤ = “ total ignorance.” “Advanced Compiler Techniques”

32 32 Using a DFA Framework  We apply an iterative algorithm to the following information : 1. DFA framework ( D, V, ∧, F ). 2. A flow graph, with an associated function f B in F for each block B.  Important assumption : There are Entry and Exit blocks that do nothing. Entry has no predecessors ; Exit has no successors. Feel free to ignore above important assumption. 3. A boundary value v ENTRY or v EXIT if D = forward or backward, respectively. “Advanced Compiler Techniques”

33 Picture of a Flow Graph 33 Entry Exit The interesting stuff happens here “Advanced Compiler Techniques”

34 34 Iterative Algorithm (Forward) OUT[Entry] = v ENTRY ; for (other blocks B) OUT[B] = ⊤ ; while (changes to any OUT) for (each block B) { IN ( B ) = ∧ predecessors P of B OUT ( P ); OUT ( B ) = f B ( IN ( B )); } “Advanced Compiler Techniques”

35 Iterative Algorithm (Backward)  Same thing – just : 1. Swap IN and OUT everywhere. 2. Replace entry by exit. 35 “Advanced Compiler Techniques”

36 The Concept of Approximation  x approximates y iff x can be used in place of y without causing any problems  Validity of approximation is context specific x may be approximated by y in one context and by z in another Approximating Money  Earnings : 1050 can be safely approximated by 1000  Expenses : 1050 can be safely approximated by 1100 Approximating Time  Expected travel time of 2 hours can be safely approximated by 3 hours  Availability of 3 day ’ s time for study can be safely assumed to be only 2 day ’ s time 36 “Advanced Compiler Techniques”

37 Two Important Objectives in DFA  The discovered data flow information should be Exhaustive. No optimization opportunity should be missed Safe. Optimizations which do not preserve semantics should not be enabled  Conservative approximations of these objectives are allowed  The intended use of data flow information ( ≡ context ) determines validity of approximations 37 “Advanced Compiler Techniques”

38 Context Determines the Validity of Approximations Analy sis Applic ation Safe Approxi mation Exhaust ive Approxi mation 38 Live Variab les Dead Code Elimina tion A dead variable is consider ed live A live variable is consider ed dead May prohibit correct optimizat ion May enable wrong optimization Availa ble Expres sions Common Subexpr ession Elimina tion An availabl e expressi on is consider ed non - availabl e A non - availabl e expressi on is consider ed availabl e Spurious Inclusion Spurious Exclusion “Advanced Compiler Techniques”

39 Partial Order & Approximation  ≤ captures valid approximation for safety x ≤ y ⇒ x is weaker than y The data flow information represented by x can be safely used in place of the data flow information represented by y It may be imprecise, though  ≥ captures valid approximation for exhaustiveness x ≥ y ⇒ x is stronger than y The data flow information represented by x contains every value contained in the data flow information represented by y It may be unsafe, though  We want most exhaustive information which is also safe 39 “Advanced Compiler Techniques”

40 Most Approximate Values in a Complete Lattice  Top. ∀ x ∈ L, x ≤ ⊤ The most exhaustive value Using ⊤ in place of any data flow value will never miss out ( or rule out ) any possible value The consequences may be semantically unsafe, or incorrect  Bottom. ∀ x ∈ L, ⊥ ≤ x The safest value Using ⊥ in place of any data flow value will never be unsafe, or incorrect The consequences may be undefined or useless because this replacement might miss out valid values  Appropriate orientation chosen by design 40 “Advanced Compiler Techniques”

41 Examples 41 “Advanced Compiler Techniques” Available Expressions Analysis Live Variables Analysis ≤ is ⊆, ∧ is ∩≤ is ⊇, ∧ is ∪

42 Partial Order Relation 42 Refle xive x ≤ xx ≤ x Trans itive x ≤ y, y ≤ z ⇒ x ≤ z Anti - symme tric x ≤ y, y ≤ x ⇔ x = y x can be safely used in place of x If x can be safely used in place of y and y can be safely used in place of z, then x can be safely used in place of z If x can be safely used in place of y and y can be safely used in place of x, then x must be same as y “Advanced Compiler Techniques”

43 The Set of Flow Functions  F is the set of functions f : L → L such that F contains an identity function To model “ empty ” statements, i. e. statements which do not influence the data flow information F is closed under composition  Cumulative effect of statements should generate data flow information from the same set For every x ∈ L, there must be a finite set of flow functions { f 1, f 2,... f m } ⊆ F such that x = ∧ 1 ≤i≤m f i ( B i )  Properties of f Monotonicity and Distributivity Loop Closure Boundedness and Separability 43 “Advanced Compiler Techniques”

44 Transfer Function of a Path 44 f1f1 f2f2 f n-1 B... f n -1 (... f 2 ( f 1 ( v ENTRY ))...) “Advanced Compiler Techniques”

45 Monotonicity of Flow Functions  Partial order is preserved : If x can be safely used in place of y then f ( x ) can be safely used in place of f ( y ) ∀ x, y ∈ L x ≤ y ⇒ f ( x ) ≤ f ( y )  Alternative definition ∀ x, y ∈ L f ( x ∧ y ) ≤ f ( x ) ∧ f ( y )  Merging at intermediate points in shared segments of paths is safe ( However, it may lead to imprecision ) 45 “Advanced Compiler Techniques” ≤ ≤

46 Distributivity of Flow Functions  Merging distributes over function application ∀ x, y ∈ L, f ( x ∧ y ) = f ( x ) ∧ f ( y ) 46 “Advanced Compiler Techniques”  Merging at intermediate points in shared segments of paths does not lead to imprecision

47 47 DFA Solutions  MFP ( maximal fixed point ) = result of iterative algorithm.  MOP = “ Meet Over all Paths ” from entry to a given point, of the transfer function along that path applied to v ENTRY.  IDEAL = ideal solution = meet over all executable paths from entry to a point. Question : why might a path not be “ executable ” ? “Advanced Compiler Techniques”

48 48 Maximum Fixed Point  Fixedpoint = solution to the equations used in iteration : IN ( B ) = ∧ predecessors P of B OUT ( P ); OUT ( B ) = f B ( IN ( B ));  Maximum = IN ’ s and OUT ’ s of any other solution are ≤ the result of the iterative algorithm ( MFP ).  Subtle point : “ maximum ” = “ maximum ignorance ” = “ we don ’ t believe any fact that is not justified by the flow graph and the rules.” This is a good thing. “Advanced Compiler Techniques”

49 Example: Reaching Definitions  MFP = solution with maximum “ ignorance.”  That is, the smallest sets of definitions that satisfy the equations.  Makes sense : We need to discover all definitions that really reach.  That ’ s what “ fixedpoint ” gives us. But we want to avoid “ imaginary ” definitions that are not justified.  That ’ s what “ maximum ” gives us. 49 “Advanced Compiler Techniques”

50 Example: RD’s – (2) 50 d: x = 10 e: x = 20 f: y = 30 d e,fe,f e,fe,f Here is the MFP But if we add d at these two points, we still have a solution, just not the maximal ( ignorance ) solution. “Advanced Compiler Techniques”

51 Meet Over Paths (MoP) Assignment  The largest safe approximation of the information reaching a program point along all information flow paths f represents the compositions of flow functions along BI refers to the relevant information from the calling context All execution paths are considered potentially executable by ignoring the results of conditionals  Any Info ( p ) ≤ MoP ( p ) is safe 51 “Advanced Compiler Techniques”

52 Difficulties in computing MoP assignment  In the presence of cycles there are infinite paths If all paths need to be traversed ⇒ Undecidability  Even if a program is acyclic, every conditional multiplies the number of paths by two If all paths need to be traversed ⇒ Intractability 52 “Advanced Compiler Techniques”

53 53 MOP Versus IDEAL  At each block B, MOP [ B ] ≤ IDEAL [ B ].  Why? The meet over many paths is ≤ the meet over a subset.  Example : x ∧ y ∧ z ≤ x ∧ y because ( x ∧ y ∧ z ) ∧ ( x ∧ y ) = x ∧ y ∧ z. Use commutativity, associativity, idempotence. “Advanced Compiler Techniques”

54 54 MOP Versus IDEAL – (2)  Intuition : Anything not ≤ IDEAL is not safe, because there is some executable path whose effect is not accounted for.  Conversely : any solution that is ≤ IDEAL accounts for all executable paths ( and maybe more paths ), and is therefore conservative ( safe ), even if not accurate. “Advanced Compiler Techniques”

55 55 MFP Versus MOP  Is MFP ≤ MOP? If so, then since MOP ≤ IDEAL, we have MFP ≤ IDEAL, and therefore MFP is safe.  Yes, but …  Requires two assumptions about the framework : 1.“ Monotonicity ” 2. Finite height ( no infinite chains... < x 2 < x 1 < x )  Example : OK for power sets of a finite set, and therefore for the frameworks we have seen. “Advanced Compiler Techniques”

56 56 Monotonicity  A framework is monotone if the functions respect ≤. That is :  If x ≤ y, then f ( x ) ≤ f ( y ).  Equivalently : f ( x ∧ y ) ≤ f ( x ) ∧ f ( y ). Proof that these are equivalent is on p. 625 of the text. But we only need the second ; the first justifies the term “ monotonicity.” “Advanced Compiler Techniques”

57 Possible Assignments as Solutions of Data Flow Analysis 57 All possible assignments All safe assignments All fixed point solutions Meet Over Paths Assignment Maximum Fixed Point Least Fixed Point ∀ i, In i = Out i = ⊤ ∀ i, Ini = Outi = ⊥ “Advanced Compiler Techniques”

58 Existence and Computation of the Maximum Fixed Point  For monotonic f : L → L, if all descending chains are finite, then  MFP ( f ) = f k +1 ( ⊤ ) = f k ( ⊤ ) such that f j +1 ( ⊤ ) ≠ f j ( ⊤ ), j < k 58 ⊤ ⊥ f ( ⊤ ) f i ( ⊤ )  ⊤ ≥ f ( ⊤ ) ≥ f 2 ( ⊤ ) ≥ f 3 ( ⊤ ) ≥ f 4 ( ⊤ ) ≥...  Since descending chains are finite, there must exist f k ( ⊤ ) such that f k +1 ( ⊤ ) = f k ( ⊤ ) and f j +1 ( ⊤ ) ≠ f j ( ⊤ ), j < k f k+1 ( ⊤ ) = f k ( ⊤ )  If p is a fixed point of f then p ≤ f k ( ⊤ ) Proof strategy : Induction on i for f i ( ⊤ ) Basis ( i = 0): p ≤ f 0 ( ⊤ ) = ⊤ Inductive Hypothesis : Assume that p ≤ f i ( ⊤ ) Proof : f ( p ) ≤ f ( f i ( ⊤ )) ( f is monotonic ) ⇒ p ≤ f ( f i ( ⊤ )) ( f ( p ) = p ) ⇒ p ≤ f i +1 ( ⊤ )  ⇒ f k +1 ( ⊤ ) is the MFP “Advanced Compiler Techniques”

59 59 Good News!  The frameworks we ’ ve studied so far are all monotone. Easy proof for functions in Gen - Kill form.  And they have finite height. Only a finite number of defs, variables, etc. in any program. “Advanced Compiler Techniques”

60 Two Paths to B That Meet Early 60 Entry B Since f ( x ∧ y ) ≤ f ( x ) ∧ f ( y ), it is as if we added nonexistent paths, but we ’ re safe. f OUT = x OUT = y IN = x ∧ y OUT = f(x ∧ y) In MFP, Values x and y get combined too soon. f(x) f(y) MOP considers paths independen tly and and combines at the last possible moment. OUT = f(x) ∧ f(y) “Advanced Compiler Techniques”

61 ⊤ ⊥ L ⊤ ⊥ L Monotonicity and Distributivity 61 Distribu tive and hence monotoni c Monotoni c but not distribu tive “Advanced Compiler Techniques”

62 Const. Propagation Non-Distribitive  Possible combinations due to merging a = 1 a = 2 b = 1 b = 2 c = a + b = 3 or c = a + b = 2 or c = a + b = 4  Mutually exclusive information  No execution path along which this information holds 62 “Advanced Compiler Techniques”

63 63 Distributive Frameworks  Strictly stronger than monotonicity is the distributivity condition : f ( x ∧ y ) = f ( x ) ∧ f ( y ) “Advanced Compiler Techniques”

64 64 Even More Good News!  All the Gen - Kill frameworks are distributive.  If a framework is distributive, then combining paths early doesn ’ t hurt. MOP = MFP. That is, the iterative algorithm computes a solution that takes into account all and only the physical paths. “Advanced Compiler Techniques”

65 More Way to DFA  Iterative Techniques [ Kildall -1973]  Nested Strongly Connected Regions [ Allen -1969]  Interval Analysis [ Cocke & Allen 1970-1976]  T 1- T 2 Analysis [ Ullman -1973]  Node Listing [ Kennedy -1975]  Path Compression [ Graham - Wegman -1976]  Balanced Path Compression [ Tarjan - 1975]  Graph Grammars [ Farrow, Kennedy, Zucconi - 1976]  High - Level DFA [ Rosen - 1977]  Slotwise Analysis [ Dhamdhere & Rosen 1992] 65 “Advanced Compiler Techniques”

66 Iterative Techniques  Simplest Approach  Don ’ t Converge rapidly in constant propagation 66 “Advanced Compiler Techniques”

67 Nested Strongly Connected Region  Based on loop organization Extending DF in into inner loops Effectively collapsing these loops to single nodes before continuing to the next level  Many optimization such as code motion could be performed in stages using this method  But Not always easy to find NSCR. May used in Register Assignment 67 “Advanced Compiler Techniques”

68 Interval Analysis  Simple way to partition the control flow graph into regions.  By Cocke and Allen.  An Interval in G is defined to be a set of blocks I with following properties : There is a node h ∈ I, called the head of I, which is contained in every control flow from a block outside I to a block within I ; i. e., I is a single - entry region. I is connected.( This property is trivial if G is connected.) I -{ h } is cycle - free ; i. e., all cycles within I must contain h. 68 “Advanced Compiler Techniques”

69 MI Construction 69 “Advanced Compiler Techniques”

70 Interval Partition 70 “Advanced Compiler Techniques”

71 Examples  Flow graph with intervals &  Derived sequence of the CFG 71 “Advanced Compiler Techniques”

72 Non-reducible Graph 72 “Advanced Compiler Techniques”

73 Interval Analysis  Require fewer bit vector operations  Still O ( n 2 ) in worst case  Not that fast in practical implementations  BUT It constructs representation of the program control flow structure which may be used for other optimizations ! 73 “Advanced Compiler Techniques”

74 T1-T2 Analysis  Search of better theoretical results and faster algorithm, Ullman introduced two transformation on program graphs  T 1 collapse a self - loop to a single node  T 2 collapse a sequence of two nodes to a single node  To design an algorithm used balanced trees to perform available expression computation in O ( nlogn ) extended steps. 74 “Advanced Compiler Techniques”

75 Example: Transfer Functions 75 1 3 5 2 4 U S f U,IN[S] = f 1 (f 2 f 3 )* f S,OUT[2] = f 2 (f 3 f 2 )* f U,OUT[2] = f 1 f 2 (f 3 f 2 )* f U,OUT[4] = f 1 f 4 Comment : if that looks like regular expressions, they are. But in the computation, the expressions are evaluated. “Advanced Compiler Techniques”

76 Example: Meet 76 1 3 5 2 4 U V f U,OUT[2] = f 1 f 2 (f 3 f 2 )* f U,OUT[4] = f 1 f 4 f V,IN[5] = f 1 f 4 ∧ f 1 f 2 (f 3 f 2 )* Region U consumes node 5 to make region V. “Advanced Compiler Techniques”

77 Node Listings  Variation of Iterative methods  Based on IR called node listings  By Kennendy 1975 77 “Advanced Compiler Techniques”

78 Node Listings  A node listing for CFG G =( N, E, n 0) Is defined to be a sequence L = ( n 1, n 2, n 3, …n m ) of nodes from N ( nodes may be repeated )  Every simple path in G is a subsequence of l  O ( nlogn ) algorithm to solve forward or backward problems. 78 “Advanced Compiler Techniques”

79 Path Compression  Graham & Wegman  Based on transformations similar to T 1- T 2 79 “Advanced Compiler Techniques”

80 Sample Graham-Wegman Reduction 80 “Advanced Compiler Techniques”

81 GW 2-Pass Live Flow Analysis  O ( nlogn ) Algorithm  Path Compression is very fast 81 “Advanced Compiler Techniques”

82 Balanced Path Compression  Combined Node Listing approach with a stronger form of path compression  Using balanced tree data structure  Very fast algorithm  O ( n α ( n, n )) α is related to a functional inverse of Ackermann ’ s function  For practical use is asymptotically linear !  BUT Too complex May not suitable for backwards problems. 82 “Advanced Compiler Techniques”

83 Graph Grammars  By Farrow Kennedy & Zucconi  Introduced SSFG ( Semi - Structured Flow Graph ) grammar, and studied the class of flow graphs generated by that grammar.  Complexity of O ( n )  Major problem is graph parsing 83 “Advanced Compiler Techniques”

84 SSFG Grammar 84 “Advanced Compiler Techniques”

85 Example SSFG Parse 85 “Advanced Compiler Techniques”

86 Example SSFG Parse 86 “Advanced Compiler Techniques”

87 High-Level DFA  Possible to perform the same analysis on a high - level representation such as the parse tree? – YES ! 87 “Advanced Compiler Techniques” := begin end := := ; := if then else fi := while do od

88 Specifications of HL DFA Grammar := begin end := := ; 88 “Advanced Compiler Techniques” P:= begin S end S:= A S 0 := S 1 ; S 2 # no computations on pass 1 # # pass 2 computations # LIVE(S) := IN(S); LIVEOUT(S) := ∅ ; # pass 1 # IN(S) := IN(A); THRU(S) := THRU(A); # pass 2 # LIVE(A) := IN(A) ∪ (THRU(A) ∩ LIVEOUT(S)); # pass 1 # IN(S 0 ) := IN(S 1 ) ∪ (THRU(S 1 ) ∩ IN(S 2 )); THRU(S 0 ) := THRU(S 1 ) ∩ THRU(S 2 ); # pass 2 # LIVEOUT(S 2 ) :=LIVEOUT(S 0 ); LIVE(S 2 ) := IN(S 2 ) ∪ (THRU(S 2 ) ∩ LIVEOUT(S 2 )); LIVEOUT(S 1 ) :=LIVE(S 2 ); LIVE(S 1 ) := IN(S 1 ) ∪ (THRU(S 1 ) ∩ LIVEOUT(S 1 ));

89 Specifications of HL DFA Grammar := if then else fi := while do od 89 “Advanced Compiler Techniques” S 0 := if C then S 1 else S 2 fi S 0 := while C do S 1 od # pass 1 # IN(S 0 ) := IN(C) ∪ (THRU(C) ∩ (IN(S 1 ) ∪ IN(S 2 ))); THRU(S 0 ) := THRU(C) ∩ (THRU(S 1 ) ∪ THRU(S 2 )); # pass 2 # LIVEOUT(S 1 ) :=LIVEOUT(S 2 ) :=LIVEOUT(S 0 ); LIVE(S 1 ) := IN(S 1 ) ∪ (THRU(S 1 ) ∩ LIVEOUT(S 1 )); LIVE(S 2 ) := IN(S 2 ) ∪ (THRU(S 2 ) ∩ LIVEOUT(S 2 )); LIVEOUT(C) :=LIVE(S 1 ) ∪ LIVE(S 2 ); LIVE(C) := IN(C) ∪ (THRU(C) ∩ LIVEOUT(C)); # pass 1 # IN(S 0 ) := IN(C) ∪ (THRU(C) ∩ IN(S 1 )); THRU(S 0 ) := THRU(C); # pass 2 # LIVEOUT(C) :=LIVEOUT(S 0 ) ∪ IN(S 1 ) ∪ (THRU(S 1 ) ∩ IN(C)); LIVE(C) := IN(C) ∪ (THRU(C) ∩ IN(C)); LIVEOUT(S 1 ) :=LIVE(C); LIVE(S 1 ) := IN(S 1 ) ∪ (THRU(S 1 ) ∩ LIVEOUT(S 1 ));

90 Summary of DFA Methods MethodSpeedSimple?StructureBoth Way?Graph Class IterativeO(n 2 )SimpleNoYesAll IntervalO(n 2 )MiddleYes Reducible Balance TreeO(nlogn)ComplicatedYesNoReducible Path Comp.O(nlogn)MiddleSemiYesReducible Node ListO(nlogn)MiddleNoYesReducible Balance PathO(n α (n,n))ComplicatedNo?Reducible GrammarnMiddleYes L(Grammar) High LevelnSimpleYes Parse Trees 90 “Advanced Compiler Techniques”

91 Next Time  Homework 9.2.1 , 9.2.2 , 9.2.3 , 9.2.11  Partial Redundancy Elimination Dragon Book : § 9.5 91


Download ppt "Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Data Flow Analysis Extensions & Foundations."

Similar presentations


Ads by Google