MIT Foundations of Dataflow Analysis

75
Massachusetts Institute of T MIT 6.035 oun aons o aa ow na yss Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technolo

Transcript of MIT Foundations of Dataflow Analysis

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 1/75

Massachusetts Institute of T

MIT 6.035

oun a ons o a a ow na ys s

Martin RinardLaboratory for Computer Science

Massachusetts Institute of Technolo

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 2/75

Dataflow Anal sis• Compile-Time Reasoning About 

• Run-Time Values of Variables or Expressions

 – Which assignment statements produced value of 

 – Which variables contain values that are no longer 

 – What is the range of possible values of variable at

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 3/75

Pro ram Re resentation • Control Flow Graph

 – Nodes N – statements of program – Ed es 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 N  

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 4/75

• –

Pro ram Points• One program point before each node 

• One program point after each node

• Split point – point with multiple successors

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 5/75

Basic Idea• Information about program represented using 

values from algebraic structure called lattice•

 program point

 – Forward dataflow analysis

 – ac war a a ow ana ys s

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 6/75

Forward Dataflow Anal sis• 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

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 7/75

Backward Dataflow Anal sis• 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

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 8/75

Partial Orders• Set P

• Partial order ≤ such that ∀x,y,z∈P – 

 – x ≤ y and y ≤ x implies x = y (asymmetric)

• Can use partial order to define

 – pper an ower oun s – Least upper bound

 – Greatest lower bound

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 9/75

U er Bounds• If S ⊆ P then

 – x∈P is an upper bound of S if ∀y∈S. y ≤ x – x∈P is the least u er 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}

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 10/75

Lower Bounds• If S ⊆ P then

 – x∈P is a lower bound of S if ∀y∈S. x ≤ y – x∈P is the reatest 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}

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 11/75

Coverin• x<  if x ≤  and x≠ 

• x is covered by y (y covers x) if  – ,

 – x ≤ z < y implies x = z,

elements between x and y

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 12/75

Exam le• 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

011 101  110  • Line from y to x

010 001  100 

• above x in diagram

000 

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 13/75

Lattices• If x ∧  and x ∨  exist for all x, ∈P, 

then P is a lattice.• ,

then P is a complete lattice.

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 14/75

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, y∈I, x ∨ y = max(x,y), x ∧ y = min(x,y) – But ∨ I and ∧ I do not exist

 – ,−  

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 15/75

To and Bottom• Greatest element of P (if it exists) is top 

• Least element of P (if it exists) is bottom (⊥)

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 16/75

Connection Between ≤  ∧ and ∨ • The following 3 properties are equivalent:

 – x ≤ y – x ∨ y = y – x ∧ y = x

• Will prove:

 – x ≤ y implies x ∨ y = y and x ∧ y = x – x ∨ y = y implies x ≤ y – x ∧ y = x implies x ≤ y

• en y trans t v ty, can o ta n – x ∨ y = y implies x ∧ y = x –  =  = 

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 17/75

• =

Connectin Lemma Proofs• Proof of x ≤  im lies x ∨  = 

 – x ≤ y implies y is an upper bound of {x,y}. – An u er bound z of x must satisf  ≤ z. 

 – So y is least upper bound of {x,y} and x ∨ y = y – x ≤ y implies x is a lower bound of {x,y}.

 – , . – So x is greatest lower bound of {x,y} and x ∧ y = x

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 18/75

Connectin Lemma Proofs• Proof of x ∨  =  im lies x ≤ 

 – y is an upper bound of {x,y} implies x ≤ y• = 

 – x is a lower bound of {x,y} implies x ≤ y

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 19/75

Lattices as Al ebraic Structures• Have defined ∨ and ∧ in terms of ≤ • Will now define ≤ in terms of ∨ and ∧ 

 – 

that satisfy associative, commutative, idempotence,

and absorption laws

 – Will define ≤ using ∨ and ∧  – Will show that ≤ is a artial order 

• Intuitive concept of ∨ and ∧ as information

,

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 20/75

=

– =

Al ebraic Pro erties of LatticesAssume arbitrar  o erations ∨ and ∧ such that – (x ∨ y) ∨ z = x ∨ (y ∨ z) (associativity of ∨) – x ∧  ∧ z = x ∧  ∧ z associativit of ∧ 

 – x ∨ y = y ∨ x (commutativity of ∨)

 –  =   – x ∨ x = x (idempotence of ∨)

 – 

 – x ∨ (x ∧ y) = x (absorption of ∨ over ∧)

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 21/75

Connection Between ∧ and ∨

 • x ∨  =  if and onl if x ∧  = x 

• Proof of x ∨ y = y implies x = x ∧ y=

= x ∧ y (by assumption)

= y = y ∨ (y ∧ x) (by absorption)

= y ∨  x ∧ y y commutat v ty= y ∨ x (by assumption)

= x ∨ y (by commutativity)

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 22/75

= = =

Pro erties of ≤• Define x ≤  if x ∨  = • Proof of transitive property. Must show that

  x ∨ z = x ∨ (y ∨ z) (by assumption)

= x ∨ y ∨ z y assoc a v y  

= y ∨ z (by assumption)

= z y assumpt on 

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 23/75

=

Pro erties of ≤• Proof of asymmetry property. Must show that 

x ∨ y = y and y ∨ x = x implies x = y=

= x ∨ y (by commutativity)

• Proof of reflexivity property. Must show that

x ∨ x = xx ∨ x = x (by idempotence)

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 24/75

Pro erties of ≤• Induced o eration ≤ a rees with ori inal

definitions of ∨ and ∧, i.e.,  – x ∨  = su x

 – x ∧ y = inf {x, y}

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 25/75

Proof of x ∨ 

= su x• Consider any u pper bound u for x and y.

• Given x ∨ u = u and y ∨ u = u, must show =   , . .,

u = x ∨ u (by assumption)=  = (x ∨ y) ∨ u (by associativity)

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 26/75

Proof of x ∧ 

= inf x• Consider any lower bound l for x and y.

• Given x ∧ l = l and y ∧ l = l, must show =   , . .,

l = x ∧ l (by assumption)=  = (x ∧ y) ∧ l (by associativity)

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 27/75

Chains• A set S is a chain if ∀x, ∈S. ≤ x or x ≤ • P has no infinite chains if every chain in P is

• P satisfies the ascending chain condition if 

1 2 …

such that xn = xn+1 = …

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 28/75

A lication to Dataflow Anal sis • Dataflow information will be lattice values

 – Transfer functions operate on lattice values – Solution al orithm will enerate increasin 

sequence of values at each program point

 – Ascendin chain condition will ensure termination

• Will use ∨ to combine values at control-flow

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 29/75

Transfer Functions• Transfer function f: P→P for each node in

control flow graph•

information

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 30/75

Transfer FunctionsEach dataflow analysis problem has a set F of 

rans er unc ons :

 – Identity function i∈F

 – F must be closed under composition:

∀f,g∈F. the function h = λx.f(g(x)) ∈F

 – ac ∈ mus e mono one:

x ≤ y implies f(x) ≤ f(y)

 – ome mes a ∈ are s r u ve:f(x ∨ y) = f(x) ∨ f(y)

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 31/75

• = =

Distributivit Im lies Monotonicit• Proof of distributivity implies monotonicity

• Assume f(x ∨ y) = f(x) ∨ f(y)f(y) = f(x ∨ y) (by assumption)

= x ∨  y y s r u v y 

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 32/75

Puttin Pieces To ether  • Forward Dataflow Analysis Framework 

• Simulates execution of program forward with

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 33/75

Forward Dataflow Analysis• Simulates execution of program forward with

flow of control

• For each node n, have – inn – value at program point before n

 – outn – value at program point after n

 – f n – transfer function for n (given inn, computes outn)• Require that solution satisfy

 – ∀n. outn = f n(inn)

 – ∀n ≠ n0. inn = ∨ { outm . m in pred(n) } – inn0 = I

 – Where I summarizes information at start of program

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 34/75

Dataflow E uations• Compiler processes program to obtain a set of  

dataflow equationsout  := f in

inn := ∨ { outm . m in pred(n) }

 program

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 35/75

Worklist Algorithm for Solving

orwar a a ow qua ons  n n

inn0 := I; outn0 := f n0(I)  - 0

while worklist ≠ ∅ do   

inn := ∨ { outm . m in pred(n) }  =

if outn changed then worklist := worklist ∪ succ n  

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 36/75

Correctness Ar ument• Why result satisfies dataflow equations 

• Whenever process a node n, set outn := f n(inn)Al orithm ensures that out  = f in 

• Whenever outm changes, put succ(m) on worklist.

Consider any node n ∈ succ(m). It will eventually come off worklist and algorithm will set

inn := ∨ { outm . m in pred(n) }

to ensure that inn = ∨ { outm . m in pred(n) }• So final solution will satisfy dataflow equations

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 37/75

Termination Ar ument• Why does algorithm terminate? 

• Sequence of values taken on by inn or outn is a. ,

empties and algorithm terminates.

,

algorithm terminates

 – 

 – For lattices without ascending chain property, use

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 38/75

Widenin O erators• Detect lattice values that may be part of infinitely

ascending chain

• Artificially raise value to least upper bound of chain

• Example:

 – Lattice is set of all subsets of integers – Could be used to collect possible values taken on by

variable during execution of program

 – Widening operator might raise all sets of size n or greater to TOP (likely to be useful for loops)

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 39/75

Reachin Definitions• P = powerset of set of all definitions in program (all

subsets of set of definitions in program)

• ∨ = ∪ (order is ⊆)

• ⊥ = ∅ • I = inn0 = ⊥ • F = all functions f of the form f(x) = a∪ (x-b)

 – b is set of definitions that node kills

 – a is set of definitions that node generates

• General pattern for many transfer functions

 – f(x) = GEN ∪ (x-KILL)

D R hi D fi iti

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 40/75

=

Does Reaching Definitions

ramewor a s y roper es • ⊆ satisfies conditions for ≤

 – x ⊆ y and y ⊆ z implies x ⊆ z (transitivity) – x ⊆ y and y ⊆ x implies y = x (asymmetry) – x ⊆ x (idempotence)

• F satisfies transfer function conditions – λx.∅ ∪ (x- ∅) = λx.x∈F (identity)

 – Will show f(x ∪ y) = f(x) ∪ f(y) (distributivity)f(x) ∪ f(y) = (a ∪ (x – b)) ∪ (a ∪ (y – b))

= a ∪ (x – b) ∪ (y – b) = a ∪ ((x ∪ y) – b)

=

D R hi D fi iti

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 41/75

Does Reaching Definitions

ramewor a s y roper es • What about composition?

 – Given f 1(x) = a1 ∪ (x-b1) and f 2(x) = a2 ∪ (x-b2) – Must show f f  x can be ex ressed as a ∪  x - b

f 1(f 2(x)) = a1 ∪ ((a2 ∪ (x-b2)) - b1)

= a1 ∪ ((a2 - b1) ∪ ((x-b2) - b1))= (a1 ∪ (a2 - b1)) ∪ ((x-b2) - b1))

= (a1 ∪ (a2 - b1)) ∪ (x-(b2 ∪  b1))

 – Let a = (a1 ∪ (a2 - b1)) and b = b2 ∪  b1

 – Then f 1(f 2(x)) = a ∪ (x – b)

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 42/75

General ResultAll GEN/KILL transfer function frameworks

satisfy – Identit

 – Distributivity

 – Com osition

Properties

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 43/75

Available Ex ressions

• P = powerset of set of all expressions in program (all subsets of set of expressions)

• ∨ = ∩ (order is ⊇)

• ⊥ = P

• I = in  = ∅ • F = all functions f of the form f(x) = a ∪ (x-b)

 – b is set of ex ressions that node kills 

 – a is set of expressions that node generates

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 44/75

Conce t of Conservatism

• Reachin definitions use ∪ as oin 

 – Optimizations must take into account all definitionsthat reach along ANY path

• Available expressions use ∩ as join

 – ALL paths

 possible executions into account. Structure of 

.

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 45/75

Backward Dataflow Anal sis• Simulates execution of program backward against

the flow of control

• For each node n, have – in   – value at ro ram oint before n

 – outn – value at program point after n – f    – transfer function for n ( iven out   ), com utes in

• Require that solution satisfies

 – ∀n. in  = f out

 – ∀n ∉  Nfinal. outn = ∨ { inm . m in succ(n) } – ∀n ∈  N  = out  = O

 – Where O summarizes information at end of program

Worklist Algorithm for Solving

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 46/75

Worklist Algorithm for Solving

ac war a a ow qua onsfor each n do inn := f n(⊥) 

for each n ∈  Nfinal

do outn

:= O; inn:= f 

n(O)

worklist := N - Nfinalwhile worklist ≠ ∅ do

remove a node n from worklist

outn := ∨ { inm . m in succ(n) }inn := f n(outn)if inn changed then

worklist := worklist ∪  pred(n)

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 47/75

Live Variables

• P = powerset of set of all variables in program 

(all subsets of set of variables in program)• =

• ⊥ = ∅ • =

• F = all functions f of the form f(x) = a ∪ (x-b)

 – b is set of variables that node kills – a is set of variables that node reads

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 48/75

Meanin of Dataflow Results

• Concept of program state s for control-flow graphs

• Program point n where execution located

(n is node that will execute next)

• Values of variables in program

• Each execution generates a trajectory of states: – s0;s1;…;sk ,where each si ∈ST

 – s + generated from s   by executing basic block to

• Update variable values

• Obtain new ro ram oint n 

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 49/75

Relatin States to Anal sis Result 

• Meaning of analysis results is given by an abstraction function AF:ST→P

• Correctness condition: require that for all states s AF(s) ≤ inn

where n is the next statement to execute in state s

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 50/75

Si n Anal sis Exam le 

• Sign analysis - compute sign of each variable v 

• Base Lattice: P = flat lattice on {-,0,+}

- 0 +

BOT

• Actual lattice records a value for each variable

 – -, ,

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 51/75

Inter retation of Lattice Values

• If value of v in lattice is:

 – BOT: no information about sign of v – -: variable v is ne ative

 – 0: variable v is 0

 – + – TOP: v may be positive or negative

 – AF([x1,…,xn]) = [sign(x1), …, sign(xn)]

 – ere s gn x = x = , x > , - x <

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 52/75

O eration ⊗ on Lattice

⊗  BOT - 0 + TOP

BOT BOT - 0 + TOP

- - + 0 - TOP

+ + - 0 + TOP

TOP TOP TOP 0 TOP TOP

f i

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 53/75

Transfer Functions

• If n of the form v = c

 – f n(x) = x[v→+] if c is positive – f x = x v→0 if c is 0

 – f n(x) = x[v→-] if c is negative

= *  1 2 3

 – f n(x) = x[v1→x[v2] ⊗ x[v3]]

• = (uninitialized variables may have any sign)

E l

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 54/75

[ + b +]

[ , , ]

Exam le

a = 1

[a→+][a→+]

 b = -1 b = 1

a→+, →+, -

,

c = a*ba→+, b→TOP,c →TOP

I i i I E l

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 55/75

[ + b +]

Im recision In Exam le 

a = 1Abstraction Imprecision:

[a→+][a→+]

 b = -1 b = 1

a→+, →+, -

,

c = a*bControl Flow Imprecision:

[b→TOP] summarizes results of all executions. In any

execution state s, AF(s)[b]≠TOP

G l S f I i i

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 56/75

bst act o u ct o t ows a o at o

General Sources of Im recision

• Abstraction Imprecision

-, ,

 – Lattice values less precise than execution values – Abstraction function throws awa information

• Control Flow Imprecision

 – One lattice value for all ossible control flow aths  – Analysis result has a single lattice value to summarize results of 

multiple concrete executions

 – Join operation∨ 

moves up in lattice to combine values fromdifferent execution paths

 – ,

Wh H I i i

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 57/75

Wh Have Im recision 

• Make analysis tractable

• Unbounded sets of values in execution – 

• Execution may visit unbounded set of states

 – s rac e y compu ng o ns o eren pa s

Ab t ti F ti

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 58/75

Abstraction Function

• AF(s)[v] = sign of v

 – , , , - , , -

• Establishes meaning of the analysis results – If analysis says variable has a given sign

 – Always has that sign in actual execution

• Correctness condition:

 – ∀ v. AF s v ≤ in v n is node for s 

 – Reflects possibility of imprecision

Ab t ti F ti S d

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 59/75

Abstraction Function Soundness

• Will show

∀ v. AF(s)[v] ≤

 inn[v] (n is node for s)

 produced s

• ase case:

 – ∀ v. inn0[v] = TOP, which implies that

 – ∀ v. AF(s)[v] ≤ TOP

Induction Step

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 60/75

Induction Step• Assume ∀ v. AF(s)[v] ≤ inn[v] for computations of length k 

• Prove for computations of length k+1

• roo :

 – Given s (state), n (node to execute next), and inn

 – Find the node that ust executed , s the revious state , and in p

 – By induction hypothesis ∀ v. AF(s p)[v] ≤ in p[v]

 – 

• If n of the form v = c, then

 – s[v] = c and out  [v] = sign(c), so 

AF(s)[v] = sign(c) = out p [v]≤ inn[v]

 – If x≠v, s[x] = s p [x] and out p [x] = in p[x], so= = p    p    p n  

• Similar reasoning if n of the form v1 = v2*v3

Au mented Execution States

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 61/75

Au mented Execution States

• Abstraction functions for some analyses require 

augmented execution states

 – Reachin definitions: states are au mented with 

definition that created each value

 – Available expressions: states are augmented withexpression for each value

Meet Over Paths Solution

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 62/75

Meet Over Paths Solution

• What solution would be ideal for a forward dataflow

analysis problem?

• Consider a path p = n0, n1, …, nk , n to a node n

(note that for all i ni ∈  pred(ni+1))

• The solution must take this path into account:

f  p (⊥) = (f nk (f nk-1(…f n1(f n0(⊥)) …)) ≤ inn

• So the solution must have the property that

∨ p

. p s a pa o n nn

and ideally

∨{  p (⊥) . p s a pat to n} = nn

Soundness Proof of Analysis

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 63/75

gor m• Property to prove: 

For all paths p to n, f  p (⊥

)≤ 

inn

 – Uses monotonicity of transfer functions

• Lemma:

or st a gor t m pro uces a so ut on suc t at

f n(inn) = outn

if n ∈  pred(m) then outn ≤ inm

Proof

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 64/75

Proof 

• Base case: p is of length 1 

 – Then p = n0 and f  p(⊥

) =⊥ 

= inn0

 – Assume theorem for all paths of length k 

Induction Ste Proof

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 65/75

Induction Ste Proof 

• p = n0, …, nk , n

• Must show f k (f k-1(…f n1(f n0(⊥)) …)) ≤ inn

 – By induction (f - (…f (f (⊥)) …)) ≤ in  

 – Apply f k to both sides, by monotonicity we get

f k (f k-1(…f n1(f n0(⊥

)) …))≤ 

f k (innk ) – By lemma, f k (innk ) = outnk 

 – By lemma, out  ≤ in  

 – By transitivity, f k (f k-1(…f n1(f n0(⊥)) …)) ≤ inn

Distributivit

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 66/75

=

Distributivit

• Distributivity preserves precision

•If framework is distributive, then worklist

 – For all n: p . n

Lack of Distributivit Exam le

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 67/75

Lack of Distributivit Exam le 

• Constant Calculator 

• Flat Lattice on Integers

-1 10- 2 … 

BOT

• Actual lattice records a value for each variable

 – xamp e e emen : a→ , → , c→  

Transfer Functions

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 68/75

Transfer Functions

• If n of the form v = c

 – f n(x) = x[v→c]

• =

 – f n(x) = x[v1→x[v2] + x[v3]]

 – Consider transfer function f for c = a + b

 – a→ , →  ∨  a→ , → = a→ , → , c→  

 – f([a→3, b→2]∨[a→2, b→3]) = f([a→TOP, b→TOP]) =

a→TOP b→TOP c→TOP 

Lack of Distributivit Anomal

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 69/75

Lack of Distributivit Anomal

a = 2

 b = 3

a = 3

 b = 2

[a→3, b→2][a→2, b→3]

[a→TOP, b→TOP]

= +Lack of Distributivity Imprecision:

[a→TOP, b→TOP, c →TOP]

a→ , → , c→ more prec se

What is the meet over all paths solution?

How to Make Anal sis Distributive

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 70/75

{[ 2 b 3 5] [ 3 b 2 5]}

How to Make Anal sis Distributive

• Keep combinations of values on different paths 

= =

 b = 3 b = 2

,,

a→ , → , a→ , →  

c = a+b

a→ , → ,c→ , a→ , → ,c→  

Issues

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 71/75

Issues

• Basically simulating all combinations of values 

in all executions

 – Ex onential blowu

 – Nontermination because of infinite ascending chains

• – Use widening operator to eliminate blowup

 – Loses precision in many cases

Multi le Fixed Points

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 72/75

==

Multi le Fixed Points

• Dataflow analysis generates least fixed point

• May be multiple fixed points

• Available expressions example

a = x +y

0

a = x +

0

==0

1

1

000

==

1

11nop= x+y;

01nop b = x+y;

11

Pessimistic vs. O timistic Anal ses 

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 73/75

• Available expressions is optimistic (for common sub-expression elimination) –  Assumes expressions are available at start of analysis

 –  Analysis eliminates all that are not available –  ,n

 –  Cannot stop analysis early and use current result

• Live variables is pessimistic (for dead code elimination)

 –  Assumes all variables are live at start of analysis

 – Analysis finds variables that are dead

n,

 –  Can stop analysis early and use current result

• Optimism/pessimism depends on intended use

• Formal dataflow setup same for both analyses

Summar 

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 74/75

• Formal dataflow analysis framework 

 – Lattices, partial orders

 – Transfer functions oins and s lits 

 – Dataflow equations and fixed point solutions

 – Abstraction function AF: S→ P – , n

 – Meet over all paths solutions, distributivity

MIT OpenCourseWarehttp://ocw.mit.edu 

7/28/2019 MIT Foundations of Dataflow Analysis

http://slidepdf.com/reader/full/mit-foundations-of-dataflow-analysis 75/75

6.035 Computer Language Engineering

Spring 2010

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.