1 / 91

Advanced Compiler Techniques

Advanced Compiler Techniques. Data Flow Analysis Extensions & Foundations. LIU Xianhua School of EECS, Peking University. REVIEW. Some Basic Ideas Reaching Definitions Available Expressions Live Variables. REVIEW. REVIEW. Dataflow Analysis Control flow graph

malaya
Download Presentation

Advanced Compiler Techniques

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. Advanced Compiler Techniques Data Flow Analysis Extensions & Foundations LIU Xianhua School of EECS, Peking University

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

  3. REVIEW “Advanced Compiler Techniques”

  4. 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/RegAlloc & Dead code elimination “Advanced Compiler Techniques”

  5. 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? “Advanced Compiler Techniques”

  6. Outline • Foundations • Data Flow Framework • Lattice-Theoretic Formulation • Meet-Over-Paths Solution • Extensions • Other DFA Methods “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 n0 • Set of final nodes Nfinal • Could also add special nodes ENTRY & EXIT “Advanced Compiler Techniques”

  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? “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 “Advanced Compiler Techniques”

  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 “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 afternode • 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 “Advanced Compiler Techniques”

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

  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). Topelement ⊤ 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. 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:PowersetSemilattice { } {1} {2} {3} Convention: Draw the meet of elements below both. {1,2} {1,3} {2,3 } x y {1,2,3} 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 “Advanced Compiler Techniques”

  17. Partial Order for a Semilattice x ≤ y means there is a path downward from y to x. • 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). 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} “Advanced Compiler Techniques”

  19. LowerBounds • 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} “Advanced Compiler Techniques”

  20. 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 “Advanced Compiler Techniques”

  21. 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 • Hasse Diagram • If y covers x • Line from y to x • y above x in diagram 011 110 101 010 001 100 000 “Advanced Compiler Techniques”

  22. 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 “Advanced Compiler Techniques”

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

  24. Partially Ordered Sets and Lattices 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 latticeiff 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 semilatticeiff each non-empty finite subset of L has a glb • A join semilatticeiff each non-empty finite subset of L has a lub • A bounded lattice iff L is a lattice and has ⊤ and ⊥ elements “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 “Advanced Compiler Techniques”

  27. Variants of Lattices Lattices Bounded lattices Join Semilattices Meet Semilattices Meet Semilattices satisfying dcc Join Semilattices satisfying acc Meet Semilattices with ⊥ element Join Semilattices with ⊤ element Complete lattices Complete lattices with dcc and acc “Advanced Compiler Techniques”

  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. 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. Example: RD Satisfies Axioms • Union on a power set forms a semilattice (idempotent, commutative, associative). • Identity function:let K = G = ∅. • Composition:A little algebra. f1(x) = (x – K1) ∪G1 f2(x) = (x – K2) ∪G2 f1(f2(x)) = (((x – K2) ∪G2) – K1) ∪G1 = (x – (K1∪K2 ) ∪ ((G2– K1) ∪ G1) G2, K2 G1, K1 “Advanced Compiler Techniques”

  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. Using a DFA Framework • We apply an iterative algorithm to the following information: • DFA framework (D, V, ∧, F). • A flow graph, with an associated function fB 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. • A boundary value vENTRY or vEXIT if D = forward or backward, respectively. “Advanced Compiler Techniques”

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

  34. Iterative Algorithm (Forward) OUT[Entry] = vENTRY; 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) = fB(IN(B)); } “Advanced Compiler Techniques”

  35. Iterative Algorithm (Backward) • Same thing – just: • Swap IN and OUT everywhere. • Replace entry by exit. “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 “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 “Advanced Compiler Techniques”

  38. Context Determines theValidity of Approximations May prohibit correct optimization May enable wrong optimization 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 “Advanced Compiler Techniques”

  40. Most Approximate Values in aComplete 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 “Advanced Compiler Techniques”

  41. Examples “Advanced Compiler Techniques”

  42. Partial Order Relation “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 {f1, f2, . . . fm} ⊆ F such that x =∧1≤i≤m fi(Bi) • Properties of f • Monotonicity and Distributivity • Loop Closure Boundedness and Separability “Advanced Compiler Techniques”

  44. Transfer Function of a Path f1 f2 fn-1 . . . B fn-1( . . .f2(f1(vENTRY)). . .) “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) ≤ ≤ “Advanced Compiler Techniques”

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

  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 vENTRY. • 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. MaximumFixed Point • Fixedpoint= solution to the equations used in iteration: IN(B) = ∧predecessors P of B OUT(P); OUT(B) = fB(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. “Advanced Compiler Techniques”

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

More Related