Lattice-Theoretic Formulation Meet-Over-Paths Solution Monotonicity/ Distributivity

31
Data-Flow Frameworks Lattice-Theoretic Formulation Meet-Over-Paths Solution Monotonicity/Distributivity Jeffrey D. Ullman Stanford University

description

Data-Flow Frameworks. Lattice-Theoretic Formulation Meet-Over-Paths Solution Monotonicity/ Distributivity. Jeffrey D. Ullman Stanford University. Data-Flow Analysis Frameworks. Generalizes and unifies the DFA examples from previous slide set. Important components : - PowerPoint PPT Presentation

Transcript of Lattice-Theoretic Formulation Meet-Over-Paths Solution Monotonicity/ Distributivity

Page 1: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

Data-Flow Frameworks

Lattice-Theoretic FormulationMeet-Over-Paths SolutionMonotonicity/Distributivity

Jeffrey D. UllmanStanford University

Page 2: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

2

Data-Flow Analysis 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).

Page 3: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

3

Gary Kildall This theory was the thesis at U. Wash. of Gary

Kildall. Gary is better known for CP/M, the first real PC

operating system. There is an interesting story.

Google query: [kildall cp/m].

Page 4: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

4

Semilattices V and ∧ form a semilattice if for all x, y, and z

in V:1. x ∧ y = y ∧ x (commutativity).2. x ∧ (y ∧ z) = (x ∧ y) ∧ z (associativity).3. x ∧ x = x (idempotence).4. 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.

5. Bottom element ⊥ such that for all x, ⊥∧ x = ⊥. Optional and not generally used in the theory.

Page 5: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

5

Example: Semilattice V = power set of some set. ∧ = union. Union is idempotent, commutative, and

associative. What are the top and bottom elements?

Page 6: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

6

Example: Powerset Semilattice

{2,3 }

{ }

{1,3}{1,2}

{3}{2}

{1,2,3}

{1}Convention:Draw the meetof elementsbelow both.

xy

x ∧ y

Page 7: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

7

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:

1. x ≤ y and y ≤ z imply x ≤ z (proof in text).2. 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

Page 8: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

8

Axioms for Transfer Functions1. F includes the identity function.

Why needed? Constructions often require introduction of an empty

block.2. F is closed under composition.

Why needed? The concatenation of two blocks is a block. Transfer function for a block can be constructed from

individual statements.

Page 9: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

9

Good News! The problems from the last slide set fit the

model. 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.

Page 10: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

10

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).

Page 11: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

11

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

Page 12: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

12

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.”

Page 13: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

13

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.

Page 14: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

14

Picture of a Flow Graph

Entry

Exit

The interesting

stuff happens

here

Page 15: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

15

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));}

Page 16: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

16

Iterative Algorithm (Backward) Same thing – just:

1. Swap IN and OUT everywhere.2. Replace entry by exit.

Page 17: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

17

What Does the Iterative Algorithm Do? MFP (maximal fixedpoint) = result of iterative

algorithm. MOP = “Meet Over all Paths” (from the 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 the point. Question: why might a path not be “executable”?

Page 18: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

18

Transfer Function of a Path

f1 f2 fn-1

B

. . .

fn-1( . . .f2(f1(vENTRY)). . .)

Page 19: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

19

Maximum Fixedpoint 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.

Page 20: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

20

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.

Page 21: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

21

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.

Page 22: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

22

MOP and IDEAL All solutions are really meets of the result of

starting with vENTRY and following some set of paths (possibly imaginary) to the point in question.

If we don’t include at least the IDEAL paths, we have an error. But try not to include too many more.

Page 23: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

23

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.

Page 24: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

24

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.

Page 25: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

25

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).

Needed to assure convergence of the algorithm. Example: OK for power sets of a finite set, and therefore

for the frameworks we have seen.

Page 26: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

26

MFP Versus MOP – (2) Intuition: If we computed the MOP directly, we

would compose functions along all paths, then take a big meet:

∧all paths P fP(vENTRY) But the MFP (iterative algorithm) alternates

compositions and meets arbitrarily. That’s why we need the “monotonicity” assumption.

Page 27: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

27

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 areequivalent is on p. 625of the text. But we onlyneed the second; the firstjustifies the term“monotonicity.”

Page 28: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

28

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.

Page 29: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

29

Two Paths to B That Meet Early

Entry B

Since f(x ∧ y) ≤ f(x) ∧ f(y), we may have imaginednonexistent paths, but we’re safe.

fOUT = 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)

Page 30: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

30

Distributive Frameworks Strictly stronger than monotonicity is the

distributivity condition:

f(x ∧ y) = f(x) ∧ f(y)

Page 31: Lattice-Theoretic Formulation Meet-Over-Paths   Solution Monotonicity/ Distributivity

31

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.