Advanced Compiler Techniques
description
Transcript of Advanced Compiler Techniques
Advanced Compiler Techniques
LIU Xianhua
School of EECS, Peking University
Data Flow AnalysisExtensions & Foundations
“Advanced Compiler Techniques”
REVIEW
• Some Basic Ideas• Reaching Definitions• Available Expressions• Live Variables
2
“Advanced Compiler Techniques”
REVIEW
3
“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
“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
“Advanced Compiler Techniques”
“Advanced Compiler Techniques”
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
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?
“Advanced Compiler Techniques”
“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
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
“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
DFA Frameworks
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
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
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
{2,3 }
{ }
{1,3}{1,2}
{3}{2}
{1,2,3}
{1}
Convention:Draw the meet of elements below both.
xy
x ∧ y
“Advanced Compiler Techniques”
“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
16
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 thereis a path downwardfrom y to x.
y
x
“Advanced Compiler Techniques”
“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}
18
“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}
19
“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
“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
011101
110
010001
000
100
Hasse Diagram• If y covers x
• Line from y to x• y above x in
diagram
21
“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
22
“Advanced Compiler Techniques”
Lattice Examples
• Lattices
• Non-lattices
23
24
Partially Ordered Sets and Lattices
Partially ordered setsPartial order ≤ isreflexive, transitive,and anti-symmetric
A lower bound ofx, y is u s.t. u ≤ xand u ≤ y
An upper bound ofx, y is u s.t. x ≤ uand y ≤ uLattices
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, thenL has finite height, andL is complete
A complete lattice need not have finite height
“Advanced Compiler Techniques”
27
Variants of Lattices
Meet Semilattices
Meet Semilatticeswith ⊥ element
Meet Semilatticessatisfying dcc
Join Semilattices
Join Semilatticessatisfying acc
Join Semilatticeswith ⊤ element
Lattices Bounded lattices
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:1. DFA framework (D, V, ∧, F).2. 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.3. A boundary value vENTRY or vEXIT if D =
forward or backward, respectively.“Advanced Compiler Techniques”
33
Picture of a Flow Graph
Entry
Exit
The interesting
stuff happens here
“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:
1. Swap IN and OUT everywhere.2. Replace entry by exit.
“Advanced Compiler Techniques”
36
The Concept of Approximation x approximates y iffx can be used in place of y without causing any problems Validity of approximation is context specificx 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
Analysis Application Safe Approximati
on
ExhaustiveApproximati
onLive
VariablesDead CodeElimination
A dead variableis considered live
A live variable is considered dead
May prohibit correct optimization
May enable wrong optimization
AvailableExpressions
Common Subexpression
Elimination
An availableexpression isconsidered
non-available
A non-availableexpression isconsideredavailable
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”
Available Expressions Analysis
Live Variables Analysis
≤ is ⊆, ∧ is ∩ ≤ is ⊇, ∧ is ∪
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 yand y can be safely used in place of z,then x can be safely used in place of zIf x can be safely used in place of yand 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 functionTo 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 thatx =∧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 ∈ Lx ≤ y ⇒ f (x) ≤ f (y)
Alternative definition ∀x, y ∈ Lf (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)
“Advanced Compiler Techniques”
Merging at intermediate points in shared segments of paths does not lead to imprecision
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
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
Example: RD’s – (2)
d: x = 10
e: x = 20
f: y = 30
d
e,f
e,f
Here is the MFP
But if we add d at thesetwo points, we still havea solution, just not themaximal (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“Advanced Compiler Techniques”
52
Difficulties in computingMoP 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
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
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
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 . . . < x2 < x1 <
x)• Example: OK for power sets of a finite set, and
therefore for the frameworks we have seen.
“Advanced Compiler Techniques”
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. 625of the text. But we onlyneed the second; the firstjustifies the term“monotonicity.”
“Advanced Compiler Techniques”
57
Possible Assignments asSolutions of Data Flow Analysis
All possible assignments
All safe assignments
All fixed point solutions
Meet Over Paths Assignment
Maximum Fixed Point
Least Fixed Point
∀i , Ini = Outi = ⊤
∀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
⊤
⊥
f ( )⊤f i ( )⊤
⊤ ≥ f (⊤) ≥ f 2(⊤) ≥ f 3(⊤) ≥ f 4(⊤) ≥ . . .
Since descending chains are finite, there must existf k(⊤) such that f k+1(⊤) = f k(⊤) andf 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
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
Entry B
Since f(x ∧ y) ≤ f(x) ∧ f(y), it is as if we addednonexistent paths, but we’re safe.
f
OUT = x
OUT = y
IN = x∧y
OUT = f(x∧y)
In MFP, Values x and yget combined too soon. f(x)
f(y)
MOP considers pathsindependently andand combines at the last possible moment.
OUT = f(x) ∧ f(y)
“Advanced Compiler Techniques”
61
⊤
⊥
L
⊤
⊥
L
Monotonicity and Distributivity
Distributive andhence monotonic
Monotonic butnot distributive
“Advanced Compiler Techniques”
62
Const. Propagation Non-Distribitive Possible combinations due to merginga = 1 a = 2 b = 1 b = 2
c = a + b = 3 orc = a + b = 2 orc = 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
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] T1-T2 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]
“Advanced Compiler Techniques”
66
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
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
MI Construction
“Advanced Compiler Techniques”
70
Interval Partition
“Advanced Compiler Techniques”
71
Examples Flow graph with intervals & Derived sequence of the CFG
“Advanced Compiler Techniques”
72
Non-reducible Graph
“Advanced Compiler Techniques”
73
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
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
1
3
5
24
U
S
fU,IN[S] = f1(f2f3)*
fS,OUT[2] = f2(f3f2)*
fU,OUT[2] = f1f2(f3f2)*f U,OUT[4] = f1f4
Comment: if that looks likeregular expressions, they are.But in the computation, theexpressions are evaluated.
“Advanced Compiler Techniques”
76
Example: Meet
1
3
5
24
U
V
fU,OUT[2] = f1f2(f3f2)*fU,OUT[4] = f1f4
fV,IN[5] = f1f4 ∧ f1f2(f3f2)*Region U consumesnode 5 to makeregion V.
“Advanced Compiler Techniques”
77
Node Listings Variation of Iterative methods Based on IR called node listings By Kennendy 1975
“Advanced Compiler Techniques”
78
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
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
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
SSFG Grammar
“Advanced Compiler Techniques”
85
Example SSFG Parse
“Advanced Compiler Techniques”
86
Example SSFG Parse
“Advanced Compiler Techniques”
87
High-Level DFA Possible to perform the same analysis on
a high-level representation such as the parse tree? – YES!
“Advanced Compiler Techniques”
<program> := begin <statement> end<statement> := <assignment><statement> := <statement>; <statement><statement> := if <condition> then <statement>else<statement>fi<statement> := while <condition> do <statement> od
88
Specifications of HL DFA Grammar<program> := begin <statement> end
<statement> := <assignment>
<statement> := <statement>; <statement>
“Advanced Compiler Techniques”
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));
89
Specifications of HL DFA Grammar<statement> := if <condition> then <statement>else<statement>fi
<statement> := while <condition> do <statement> od
“Advanced Compiler Techniques”
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));
90
Summary of DFA Methods
Method Speed Simple?
Structure
Both Way?
Graph Class
Iterative O(n2) Simple No Yes AllInterval O(n2) Middle Yes Yes ReducibleBalance
TreeO(nlog
n)Complicated
Yes No Reducible
Path Comp.
O(nlogn)
Middle Semi Yes Reducible
Node List O(nlogn)
Middle No Yes Reducible
Balance Path
O(nα(n,n))
Complicated
No ? Reducible
Grammar
n Middle Yes Yes L(Grammar)
High Level
n Simple Yes Yes Parse Trees
“Advanced Compiler Techniques”
“Advanced Compiler Techniques”
Next Time
• Homework– 9.2.1 , 9.2.2 , 9.2.3 , 9.2.11
• Partial Redundancy Elimination– Dragon Book: §9.5
91