Download presentation
Presentation is loading. Please wait.
1
Advanced Compiler Techniques
Data Flow Analysis Extensions & Foundations LIU Xianhua School of EECS, Peking University
2
“Advanced Compiler Techniques”
REVIEW Some Basic Ideas Reaching Definitions Available Expressions Live Variables “Advanced Compiler Techniques”
3
“Advanced Compiler Techniques”
REVIEW “Advanced Compiler Techniques”
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/register allocation & dead code elimination “Advanced Compiler Techniques”
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? “Advanced Compiler Techniques”
6
“Advanced Compiler Techniques”
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
“Advanced Compiler Techniques”
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
“Advanced Compiler Techniques”
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 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 “Advanced Compiler Techniques”
12
“Advanced Compiler Techniques”
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
“Advanced Compiler Techniques”
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
“Advanced Compiler Techniques”
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
{ } Convention: Draw the meet of elements below both. x y x ∧ y {1} {2} {3} {1,2} {1,3} {2,3 } {1,2,3} “Advanced Compiler Techniques”
16
“Advanced Compiler Techniques”
Partial Orders Set P Partial order ≤ such that x,y,zP 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
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 x∧z = x∧y∧z = x∧y = x,thus x ≤ z x ≤ y and y ≤ x iff x = y. 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
“Advanced Compiler Techniques”
Upper Bounds If S P then xP is an upper bound of S if yS, y ≤ x xP 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
“Advanced Compiler Techniques”
Lower Bounds If S P then xP is a lower bound of S if yS, x ≤ y xP 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
“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 “Advanced Compiler Techniques”
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 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
“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, yI, 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
“Advanced Compiler Techniques”
Lattice Examples Lattices Non-lattices “Advanced Compiler Techniques”
24
Partially Ordered Sets and Lattices
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 “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
“Advanced Compiler Techniques”
Variants of Lattices Lattices Bounded lattices Meet Semilattices Join 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
“Advanced Compiler Techniques”
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
“Advanced Compiler Techniques”
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
“Advanced Compiler Techniques”
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 the Validity of Approximations
May prohibit correct optimization May enable wrong optimization Analysis Application Safe Approximation Exhaustive Approximation Live Variables Dead Code Elimination A dead variable is considered live A live variable is considered dead Available Expressions Common Subexpression Elimination An available expression is considered non-available A non-available available 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 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 “Advanced Compiler Techniques”
41
Examples Available Expressions Analysis Live Variables Analysis
≤ is ⊆, ∧ is ∩ ≤ is ⊇, ∧ is ∪ “Advanced Compiler Techniques”
42
Partial Order Relation
Reflexive x ≤ x Transitive x ≤ y, y ≤ z ⇒ x ≤ z Anti- symmetric 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 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 {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
. . . 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 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
“Advanced Compiler Techniques”
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
“Advanced Compiler Techniques”
Maximum Fixed 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
“Advanced Compiler Techniques”
Example: RD’s – (2) d: x = 10 d e,f Here is the MFP e: x = 20 But if we add d at these two points, we still have a solution, just not the maximal (ignorance) solution. f: y = 30 “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 “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 “Advanced Compiler Techniques”
53
“Advanced Compiler Techniques”
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
“Advanced Compiler Techniques”
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
“Advanced Compiler Techniques”
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: “Monotonicity” Finite height (no infinite chains < x2 < x1 < x) Example: OK for power sets of a finite set, and therefore for the frameworks we have seen. “Advanced Compiler Techniques”
56
“Advanced Compiler Techniques”
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
∀i , Ini = Outi = ⊤ All possible assignments Meet Over Paths Assignment All safe assignments Maximum Fixed Point Least Fixed Point ∀i , Ini = Outi = ⊥ All fixed point solutions “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 ⊤ ≥ f (⊤) ≥ f 2(⊤) ≥ f 3(⊤) ≥ f 4(⊤) ≥ . . . ⊤ f (⊤) 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 i (⊤) 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 ⊥
59
“Advanced Compiler Techniques”
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
f(x) f(y) MOP considers paths independently and and combines at the last possible moment. OUT = f(x) ∧ f(y) f OUT = x OUT = y IN = x∧y OUT = f(x∧y) In MFP, Values x and y get combined too soon. B Entry Since f(x ∧ y) ≤ f(x) ∧ f(y), it is as if we added nonexistent paths, but we’re safe. “Advanced Compiler Techniques”
61
Monotonicity and Distributivity
Distributive and hence monotonic ⊤ ⊥ L ⊤ ⊥ L Monotonic but not distributive “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 “Advanced Compiler Techniques”
63
Distributive Frameworks
Strictly stronger than monotonicity is the distributivity condition: f(x ∧ y) = f(x) ∧ f(y) “Advanced Compiler Techniques”
64
“Advanced Compiler Techniques”
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
“Advanced Compiler Techniques”
More Way to DFA Iterative Techniques[Kildall-1973] Nested Strongly Connected Regions [Allen-1969] Interval Analysis [Cocke & Allen ] T1-T2 Analysis [Ullman-1973] Node Listing [Kennedy-1975] Path Compression [Graham-Wegman -1976] Balanced Path Compression [Tarjan ] Graph Grammars[Farrow, Kennedy, Zucconi ] High-Level DFA [Rosen ] Slotwise Analysis [Dhamdhere & Rosen 1992] “Advanced Compiler Techniques”
66
“Advanced Compiler Techniques”
Iterative Techniques Simplest Approach Don’t Converge rapidly in constant propagation “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 “Advanced Compiler Techniques”
68
“Advanced Compiler Techniques”
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. “Advanced Compiler Techniques”
69
“Advanced Compiler Techniques”
MI Construction “Advanced Compiler Techniques”
70
“Advanced Compiler Techniques”
Interval Partition “Advanced Compiler Techniques”
71
“Advanced Compiler Techniques”
Examples Flow graph with intervals & Derived sequence of the CFG “Advanced Compiler Techniques”
72
“Advanced Compiler Techniques”
Non-reducible Graph “Advanced Compiler Techniques”
73
“Advanced Compiler Techniques”
Interval Analysis Require fewer bit vector operations Still O(n2) 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! “Advanced Compiler Techniques”
74
“Advanced Compiler Techniques”
T1-T2 Analysis Search of better theoretical results and faster algorithm, Ullman introduced two transformation on program graphs T1 collapse a self-loop to a single node T2 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. “Advanced Compiler Techniques”
75
Example: Transfer Functions
fU,IN[S] = f1(f2f3)* Comment: if that looks like regular expressions, they are. But in the computation, the expressions are evaluated. U S 1 2 4 fS,OUT[2] = f2(f3f2)* 3 f U,OUT[4] = f1f4 fU,OUT[2] = f1f2(f3f2)* 5 “Advanced Compiler Techniques”
76
“Advanced Compiler Techniques”
Example: Meet Region U consumes node 5 to make region V. fV,IN[5] = f1f4 ∧ f1f2(f3f2)* V U 1 2 4 3 fU,OUT[4] = f1f4 fU,OUT[2] = f1f2(f3f2)* 5 “Advanced Compiler Techniques”
77
“Advanced Compiler Techniques”
Node Listings Variation of Iterative methods Based on IR called node listings By Kennendy 1975 “Advanced Compiler Techniques”
78
“Advanced Compiler Techniques”
Node Listings A node listing for CFG G=(N, E , n0) Is defined to be a sequence L = (n1, n2, n3,…nm) 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. “Advanced Compiler Techniques”
79
“Advanced Compiler Techniques”
Path Compression Graham & Wegman Based on transformations similar to T1-T2 “Advanced Compiler Techniques”
80
Sample Graham-Wegman Reduction
“Advanced Compiler Techniques”
81
GW 2-Pass Live Flow Analysis
O(nlogn) Algorithm Path Compression is very fast “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. “Advanced Compiler Techniques”
83
“Advanced Compiler Techniques”
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 “Advanced Compiler Techniques”
84
“Advanced Compiler Techniques”
SSFG Grammar “Advanced Compiler Techniques”
85
“Advanced Compiler Techniques”
Example SSFG Parse “Advanced Compiler Techniques”
86
“Advanced Compiler Techniques”
Example SSFG Parse “Advanced Compiler Techniques”
87
“Advanced Compiler Techniques”
High-Level DFA Possible to perform the same analysis on a high-level representation such as the parse tree? – YES! <program> := begin <statement> end <statement> := <assignment> <statement> := <statement>; <statement> <statement> := if <condition> then <statement>else<statement>fi <statement> := while <condition> do <statement> od “Advanced Compiler Techniques”
88
Specifications of HL DFA Grammar
<program> := begin <statement> end <statement> := <assignment> <statement> := <statement>; <statement> P := begin S end S := A S0 := S1 ; S2 # 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(S0) := IN(S1)∪(THRU(S1)∩IN(S2)); THRU(S0) := THRU(S1)∩ THRU(S2); # pass 2 # LIVEOUT(S2) :=LIVEOUT(S0); LIVE(S2) := IN(S2)∪(THRU(S2)∩LIVEOUT(S2)); LIVEOUT(S1) :=LIVE(S2); LIVE(S1) := IN(S1)∪(THRU(S1)∩LIVEOUT(S1)); “Advanced Compiler Techniques”
89
Specifications of HL DFA Grammar
<statement> := if <condition> then <statement>else<statement>fi <statement> := while <condition> do <statement> od S0 := if C then S1 else S2 fi S0 := while C do S1 od # pass 1 # IN(S0) := IN(C)∪(THRU(C)∩(IN(S1) ∪IN(S2))); THRU(S0) := THRU(C)∩(THRU(S1) ∪ THRU(S2)); # pass 2 # LIVEOUT(S1) :=LIVEOUT(S2) :=LIVEOUT(S0); LIVE(S1) := IN(S1)∪(THRU(S1)∩LIVEOUT(S1)); LIVE(S2) := IN(S2)∪(THRU(S2)∩LIVEOUT(S2)); LIVEOUT(C) :=LIVE(S1) ∪ LIVE(S2); LIVE(C) := IN(C)∪(THRU(C)∩LIVEOUT(C)); # pass 1 # IN(S0) := IN(C)∪(THRU(C)∩IN(S1)); THRU(S0) := THRU(C); # pass 2 # LIVEOUT(C) :=LIVEOUT(S0)∪IN(S1)∪(THRU(S1)∩IN(C)); LIVE(C) := IN(C)∪(THRU(C)∩IN(C)); LIVEOUT(S1) :=LIVE(C); LIVE(S1) := IN(S1)∪(THRU(S1)∩LIVEOUT(S1)); “Advanced Compiler Techniques”
90
“Advanced Compiler Techniques”
Summary of DFA Methods Method Speed Simple? Structure Both Way? Graph Class Iterative O(n2) Simple No Yes All Interval Middle Reducible Balance Tree O(nlogn) Complicated Path Comp. Semi Node List Balance Path O(nα(n,n)) ? Grammar n L(Grammar) High Level Parse Trees “Advanced Compiler Techniques”
91
“Advanced Compiler Techniques”
Next Time Homework 9.2.1, 9.2.2, 9.2.3, Partial Redundancy Elimination Dragon Book: §9.5 “Advanced Compiler Techniques”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.