21
1 CS 201 Compiler Construction Lecture 4 Data Flow Framework

CS 201 Compiler Construction

  • Upload
    oihane

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

CS 201 Compiler Construction. Lecture 4 Data Flow Framework. Data Flow Framework. The various problems considered have things in common: Transfer functions Confluence Operator Direction of Propagation - PowerPoint PPT Presentation

Citation preview

Page 1: CS 201 Compiler Construction

1

CS 201Compiler Construction

Lecture 4Data Flow Framework

Page 2: CS 201 Compiler Construction

2

Data Flow Framework

The various problems considered have things in common:– Transfer functions

– Confluence Operator

– Direction of Propagation

These problems can be treated in a unified way data flow framework is an algebraic structure used to encode and solve data flow problems.

Page 3: CS 201 Compiler Construction

3

Monotone Data Flow Framework

Components of the framework:1.Information Set: L2.Effect of joining paths: ∧ (meet operator)3.Effect of basic blocks: fn (monotone transfer func.)4.Iterative Solution: can be shown to terminate

(L,∧) is a semilattice st ∨ a,b,c εL1.a ∧ a = a (idempotent)2.a ∧ b = b ∧ a (commutative)3.a ∧ (b ∧ c) = (a ∧ b) ∧ c (assocative)

Bottom Element st ∨ a ε L, a ∧ = Top Element Τ st ∨ a ε L, a ∧ Τ = aIf top & bottom elements exist, they are unique.

Page 4: CS 201 Compiler Construction

Contd..

Relation ≤ is a partial order on L

a ≤ b ≅ a ∧ b = a

Can similarly define <, >, ≥ relations

A semilattice is bounded iff ∨ a εL there exists a constant ca st length of chain beginning at a is at most ca.

4

Max ca

Page 5: CS 201 Compiler Construction

5

Monotonic Functions

Effect of each basic block is modeled by a transfer function f: L L. Function f must be monotonic.

A total function f: LL is monotonic iff ∨ a,b ε L f(a∧b) ≤ f(a) ∧ f(b)

Distributive function: f(a∧b) = f(a) ∧ f(b)

For monotonic functions: a ≤ b => f(a) ≤ f(b)

Page 6: CS 201 Compiler Construction

6

Contd..

For monotonic functions: a ≤ b => f(a) ≤ f(b)

Proof:

f(a∧b) ≤ f(a) ∧ f(b) Defn. of Monotonicityf(a∧b)∧f(a)∧f(b) = f(a∧b) Defn. of ≤:(a∧b=a)f(a)∧f(a)∧f(b)= f(a) Given a ≤ b: a ∧ b = af(a) ∧ f(b) = f(a) f(a)∧f(a) = f(a) idempotencef(a) ≤ f(b) Defn. of ≤

Page 7: CS 201 Compiler Construction

7

Fixpoint

A fixpoint of a monotonic function f: L L is a valuea ε L such that f(a) = a

Τ > f (Τ) > f ( f (Τ) ) > f ( f ( f (Τ) ) ) ……..

There exists t such that f ( ft (Τ) ) = ft (Τ)

ft (Τ) is the greatest fixpoint of f.

Page 8: CS 201 Compiler Construction

8

Monotone Function SpaceA monotone function space for a semilattice is a set F of monotonic functions which:

1.Contains the identity function (id)-- basic blocks may not modify information

2.Is closed under function composition-- to model the effects of paths

3.For each a ε L, there exists fεF st f( ) = a-- to model gen functions

A distributive function space is a monotone function space in which all functions are distributive.

Page 9: CS 201 Compiler Construction

9

A Monotone Data Flow System

A monotone data flow system is a tuple < L, ∧, F, G, FM >

1.(L,∧) is a bounded semilattice with Τ & 2.F is the monotone function space3.G = (N, E, s) is the program flow graph4.FM: N F is a total function that associates a function from F with each basic block.

Page 10: CS 201 Compiler Construction

10

Meet Over All Paths SolutionMeet over all paths solution (MOP) of a data flow system – MOP: N L

MOP(s) = NULL (NULL is the element in L which represents “no information”)

Ffπ is composition of functions from nodes along path π excluding node n. n1n2n3….nk-1nk fnk o fnk-1 o….o fn2 o fn1

Page 11: CS 201 Compiler Construction

11

MOP SolutionFinding MOP solution is undecidable, i.e. there does not exist a general algorithm that computes MOP solution for all monotone data flow systems.

Let X: N L denote a total function that associates nodes with lattice elements.X is conservative or safe iff ∨n εN, X(n) ≤ MOP(n)

Iterative algorithm computes conservative approximation of MOP. For distributive data flow systems, it computes solution that is identical to MOP solution.

Page 12: CS 201 Compiler Construction

12

Iterative Algorithm

Page 13: CS 201 Compiler Construction

13

Reaching Definitions

Page 14: CS 201 Compiler Construction

14

Contd…

Page 15: CS 201 Compiler Construction

15

Dominators

Page 16: CS 201 Compiler Construction

16

Constant Propagation

f (X)={(a,2),(b,3),(c,5)}f (Y)={(a,3),(b,2),(c,5)}f (X) ∧ f (Y) = {(a,not-const), (b, not-const),

(c,5)}

X ∧ Y = {(a,not-const),(b,not-const),(c,undef)}f (X∧ Y) = {(a,not-const),(b,not-const),(c,not-

const)}

f (X ∧ Y) ≤ f(X) ∧ f(Y)

Page 17: CS 201 Compiler Construction

17

Sample ProblemsData Flow Framework

Page 18: CS 201 Compiler Construction

18

Data Flow Framework

For each of the following problems given below provide the following:

• Lattice values• Meet operator• Top and bottom elements• The partial order relation, including its

pictorial representation• The transfer function• Data flow equations.

Page 19: CS 201 Compiler Construction

19

1. Reachable uses -- for each definition identify the set of uses reachable by the definition. This information is used for computing def-use chains.

2. Reaching uses -- given a definition of variable x, identify the set of uses of x that are encountered prior to reaching the definition and there is no other definitions of x that intervene the use and the definition. This information is used for computing antidependences.

Page 20: CS 201 Compiler Construction

20

3. Classify Variable Values -- classify the value of each program variable at each program point into one of the following categories: (a) the value is a unique constant -- you must also identify this constant value; (b) the value is one-of-many constants – you do not have to compute the identities of these constants as part of your solution; and (c) the value is not-a-constant, that is, it is neither a unique constant nor a one-of-many constants. This is a generalization of constant propagation.

Page 21: CS 201 Compiler Construction

21

4. Postdominators -- postdominator set of a node is the set of nodes that are encountered along all paths from the node to the end node of the control flow graph. This information is used for computing control dependence.