360 likes | 603 Views
Foundations of Data-Flow Analysis. Basic Questions. Under what circumstances is the iterative algorithm used in the data-flow analysis correct ? How precise is the solution obtained by the iterative algorithm? Will the iterative algorithm converge ?
E N D
Basic Questions • Under what circumstances is the iterative algorithm used in the 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?
Data-Flow Analysis Framework • A direction of the data flow D, which is either forwards or backwards • A semilattice, which includes a domain of values V and a meet operator • A family F of transfer functions from V to V. This family must include functions suitable for the boundary conditions, which are constant transfer functions for the special nodes ENTRY and EXIT in any control flow graph
Example: Reaching Definitions • The direction: forwards • The domain of values: the set of subsets of the set of all definitions in the program • The meet operator: set union • The family of transfer functions: the set of transfer functions for various statements
Semilattices • A semilattice is a set V and a binary meet operator such that for all x, y, and z in V: • xx = x (meetis idempotent) • xy = y x(meetis commutative) • x(yz) = (x y)z(meetis associative) • A semilattice has a top element, denoted 丅, such that for all x in V, 丅 x = x • Optionally, a semilattice may have a bottom element, denoted , such that for all x in V, x =
Example: Reaching Definitions • The domain of values is the set of all subsets of the universal set U, or the power set of U, denoted 2U • The meet operator is the set union • The set union is idempotent, commutative, and associative • The top element is the empty set • The bottom element is the universal set U
Partial Orders • A relation is a partial order on a set V if for all x, y, and z in V: • xx (the partial orderis reflexive) • Ifxyandy x, thenx = y (the partial orderis antisymmetric) • Ifxyandy z, thenxz(the partial orderis transitive) • The pair (V, ) is called a poset, or partially ordered set • We definex<yif and only ifxyandx y
The Partial Order for a Semilattice • It is useful to define a partial order for a semilattice (V, ). For all x and y in V, we define xyif and only ifxy = x • is reflexive: xx = xxx • is antisymmetric:xy xy = x, yx yx = y,x = (xy) = (yx) = y • is transitive:xy xy = x, yz yz = y,(xz) = ((xy)z) = (x(yz )) = (xy) =x xz
Example: Reaching Definitions • The relation is the set inclusion xy = xxy • This says that sets larger in size is smaller in the partial order • The set inclusion is reflexive, antisymmetric, and transitive
Greatest Lower Bounds • A greatest lower bound (or glb) of domain elements x and y is an element g such that • gx, • gy, and • If z is any element such that zx and zy, then zg
Meet and Greatest Lower Bound • The meet of x and y is the greatest lower bound of x and y • Let g = xy • gx:gx= (xy)x= x(y x)= x(x y)= (xx)y = x y = g • gy • zx and zy zgzg= z(x y)= (zx)y= zy = z
{d1} {d2} {d3} {d1, d2} {d2, d3} {d1, d3} {d1, d2, d3} Lattice Diagrams 丅
Product Lattices • The product lattice for lattices (A, A) and (B, B) is defined as follows: • The domain of the product lattice is A B • The meet for the product lattice: (a, b) (a’, b’) = (a A a’, b B b’) • The partial order for the product lattice: (a, b) (a’, b’) iff a A a’ and b B b’ • This definition can be extended to the product of any number of lattices
Example 丅 ({},{},{}) ({d1},{},{}) ({},{d2},{}) ({},{},{d3}) ({d1},{d2},{}) ({d1},{},{d3}) ({},{d2},{d3}) ({d1}, {d2}, {d3})
Height of a Semilattice • An ascending chain in a poset (V, ) is a sequence x1 < x2 < … < xn • The height of a semilattice is the largest number of < relations in any ascending chain • An iterative data flow analysis algorithm is convergent if the corresponding semilattice has finite height • A lattice consisting of a finite set of values will have a finite height • It is also possible for a lattice with an infinite number of values to have a finite height
Transfer Functions • The family of transfer functions F: V V in a data-flow framework has the following properties: • F has an identity function I, such that I(x) = x for all x in V • F is closed under composition; that is, for any two functions f and g in F, the function h defined by h(x) = g(f(x)) is in F
Example: Reaching Definitions • The identity function: gen[B] = kill[B] = • Closure under composition:f1(x) = G1 (x - K1),f2(x) = G2 (x - K2),f2(f1(x)) = G2 ((G1 (x - K1)) - K2) = (G2 (G1 - K2 )) (x - (K1 K2)). Let G = G2 (G1 - K2 ) and K = K1 K2.f(x) = f2(f1(x)) = G (x - K).
Monotone Frameworks • A framework (D, F, V, ) is monotone ifx y implies f(x) f(y),for all x andy in V, and f in F • Equivalently, a framework (D, F, V, ) is monotone iff(xy) f(x) f(y), for all x andy in V, and f in F
Proof of Equivalence () xy xandxy y f(xy) f(x) andf(xy) f(y) f(x) f(y) is the glb of f(x) andf(y) f(xy) f(x) f(y) () xy xy= x f(xy) = f(x) f(x) f(y) f(y) f(x) f(y)
Distributive Frameworks • A framework (D, F, V, ) is distributive iff(xy) =f(x) f(y)for all x andy in V, and f in F • Distributivity implies monotonicity
Example: Reaching Definitions Lety and z be sets of definitions, and f(x) = G (x - K) ThenG ((yz) - K) = (G (y- K)) (G (z- K))
The Iterative Algorithm for General Frameworks: Input • A control flow graph, with specially labeled ENTRY and EXIT nodes, • A direction of the data flow D, • A set of values V, • A meet operator , • A set of functions F, where fB in F is the transfer function for basic block B, and • A constant value vENTRY or vEXIT in V, representing the boundary condition for forward and backward frameworks, respectively
The Iterative Algorithm for General Frameworks: Output • Values in V for IN[B] and OUT[B] for each basic block B in the control flow graph
The Iterative Algorithm for General Frameworks: Forward OUT[ENTRY] =vENTRY; for (each basic block B other thanENTRY) OUT[B] := 丅; while (changes to any OUT occur) for (each basic block B other thanENTRY) { IN[B] := p pred(B)OUT[p]; OUT[B] := fB(IN[B]); }
The Iterative Algorithm for General Frameworks: Backward IN[EXIT] =vEXIT; for (each basic block B other thanEXIT) IN[B] := 丅; while (changes to any IN occur) for (each basic block B other thanEXIT) { OUT[B] := s succ(B)IN[s]; IN[B] := fB(OUT[B]); }
Properties of the Iterative Algorithm • If the algorithm converges, the result is a solution to the data-flow equations • If the framework is monotone, then the solution found is the maximum fixedpoint (MFP) of the data-flow equations. The maximum fixedpoint is a solution with the property that in any other solution, the value of IN[B] and OUT[B] are the corresponding values of MFP • If the semilattice of the framework is monotone and finite height, then the algorithm is guaranteed to converge
The Ideal Solution • Consider any pathP = ENTRY B1 … Bk-1 Bk • The transfer function for P isfP = fBk-1(fBk-2 ( … (fB1) … )) • The ideal solution isIDEAL[B] = Ppossible paths from ENTRY to BfP(vENTRY) • Any answer that is greater than IDEAL is incorrect • Any value smaller than or equal to IDEAL is conservative, i.e., safe
The Meet-Over-Paths Solution • Finding all possible paths is undecidable • The meet-over-paths solution is MOP[B] = P paths from ENTRY to BfP(vENTRY) • The paths considered in the MOP solution is a superset of all the paths that are possibly executed • MOP[B] IDEAL[B]
MFP Solution versus MOP Solution • The iterative algorithm visits basic blocks, not necessarily in the order of execution • At each confluence point, the algorithm applies the meet operator to the data-flow values obtained so far. Some of these values used were introduced artificially in the initialization process, not representing the result of any execution from the beginning of the program
Early Meet over Paths ENTRY B1 B2 B3 B4 MOP[B4] = ((f B3 f B1) (f B3 f B2))(vENTRY) IN[B4] = f B3((f B1 (vENTRY)f B2 (vENTRY)))
Comparison of Solutions • Using the iterative algorithm, we have IN[B] MOP[B]for monotone frameworks and IN[B] =MOP[B]for distributive frameworks • MFP MOP IDEAL