Docsity

Data Flow Analysis - Lecture Notes | CS 6463, Study notes of Computer Science

Partial preview of the text

Download Data Flow Analysis - Lecture Notes | CS 6463 and more Study notes Computer Science in PDF only on Docsity!

Dataflow analysis

Theory and Applications

Control-flow graph

 Graphical representation of runtime control-flow paths

 Nodes of graph: basic blocks (straight-line computations)

 Edges of graph: flows of control

 Useful for collecting information about computation

 Detect loops, remove redundant computations, register

allocation, instruction scheduling…

 Alternative CFG: Each node contains a single statement

i = 0

while (i < 50) {

t1 = b * 2;

a = a + t1;

i = i + 1;

if I < 50

t1 := b * 2;

a := a + t1;

i = i + 1;

i =0;

Building control-flow graphs

 Identify all the basic blocks

 Create a flow graph node for each basic block

 For each basic block B

 If B1 ends with a jump to a statement that starts basic block B2,

create an edge from B1 to B

 If B1 does not end with an unconditional jump, create an edge from

B1 to the basic block that immediately follows B1 in the original

evaluation order

……

i := 0

s0: if i < 50 goto s

goto s

s1: t1 := b * 2

a := a + t

goto s

S2: …

S0: if i < 50 goto s

goto s

s1: t1 := b * 2

a := a + t

goto s

S2: ……

i :=

Example Dataflow

Live variable analysis

 A data-flow analysis problem

 A variable v is live at CFG point p iff there is a path from

p to a use of v along which v is not redefined

 At any CFG point p, what variables are alive?

 Live variable analysis can be used in

 Global register allocation

 Dead variables no longer need to be in registers

 Useless-store elimination

 Dead variable don’t need to be stored back to memory

 Uninitialized variable detection

 No variable should be alive at program entry point

Computing live variables

 Domain

 All variables inside a function

 For each basic block n, let

 UEVar(n)

vars used before defined

 VarKill(n)

vars defined (killed by n)

Goal: evaluate vars alive on

entry to and exit from n

LiveOut(n)=∪m∈succ(n)LiveIn(m)

LiveIn(m)=UEVar(m) ∪

(LiveOut(m)-VarKill(m))

LiveOut(n)= ∪ m∈succ(n)

(UEVar(m) ∪

(LiveOut(m)-VarKill(m))

m:=a+b

n:=a+b

p:=c+d

r:=c+d

q:=a+b

r:=c+d

e:=b+

s:=a+b

u:=e+f

e:=a+

t:=c+d

u:=e+f

v:=a+b

w:=c+d

m:=a+b

n:=c+d

A

B

C

D

E

F

G

Algorithm: computing live variables

 For each basic block n, let

 UEVar(n)=variables used before any definition in n

 VarKill(n)=variables defined (modified) in n (killed by n)

Goal: evaluate names of variables alive on exit from n

 LiveOut(n)= ∪ (UEVar(m) ∪ (LiveOut(m) - VarKill(m))

m∈succ(n)

for each basic block bi

compute UEVar(bi) and VarKill(bi)

LiveOut(bi) := ∅

for (changed := true; changed; )

changed = false

for each basic block bi

old = LiveOut(bi)

LiveOut(bi)= ∪ (UEVar(m) ∪ (LiveOut(m) - VarKill(m))

if (LiveOut(bi) != old) changed := true

m∈succ(bi)

Another Example

Available Expressions Analysis

 The aim of the Available Expressions Analysis is to

determine

 For each program point, which expressions must have already

been computed, and not later modified, on all paths to the

program point.

 Example

[x:= a+b ]1;

[y:=a*b]2;

while [y> a+b ]3 {

[a:=a+1]4;

[x:= a+b ]

[x:= a+b]1;

[y:=a*b]2;

while [y> x ]3 {

[a:=a+1]4;

[x:= a+b]

Optimized code:

Available Expression Analysis

 Domain of analysis

 All expressions within a

function

 For each basic block n, let

 DEexp(n)

Exps evaluated without any

operand redefined

 ExpKill(n)

Exps whose operands are

redefined (exps killed by n)

Goal: evaluate exps available

on all paths entering n

AvailIn(n)=∩m∈pred(n)AvailOut(m)

AvailOut(m) = DEexp(m)∪

(AvailIn(m)-ExpKill(m))

==>

AvailIn(n)= ∩ m∈pred(n)

(DEexp(m) ∪

(AvailIn(m)-ExpKill(m))

m:=a+b

b:=c+d

p:=a+b

e:=c+d

q:=a+b

e:=c+d

e:=b+

s:=a+b

a:=e+f

e:=a+

t:=c+d

b:=e+f

w:=a+b

X:=e+f

y:=a+b

c:=c+d

A

B

C

D

E

F

G

Solution

Available Expression Analysis

m:=a+b

b:=c+d

p:=a+b

e:=c+d

q:=a+b

e:=c+d

e:=b+

s:=a+b

a:=e+f

e:=a+

t:=c+d

b:=e+f

w:=a+b

X:=e+f

y:=a+b

c:=c+d

A

B

C

D

E

F

G

1,2 2 12345 1, G

12345 2, ∅

1, F

2,4,5 1,3,4 12345 1, E

3,4 1,4,5 12345 1, D

1,2 4 12345 2 C

1,2 4 12345 2 B

2 1, A

DEexp ExpKil Avail Avail

Domain: a+b(1), c+d(2),

b+18(3),e+f(4), a+17(5)

Iterative dataflow algorithm

 Iterative evaluation of result

sets until a fixed point is

reached

 Does the algorithm always

terminate?

 If the result sets are

bounded and grow

monotonically, then yes;

Otherwise, no.

 Fixed-point solution is

independent of evaluation

order

 What answer does the

algorithm compute?

 Unique fixed-point solution

 The meet-over-all-paths

solution

 How long does it take the

algorithm to terminate?

 Depends on traversing order

of basic blocks

for each basic block bi

compute Gen(bi) and Kill(bi)

Result(bi) := ∅ or Domain

for (changed := true; changed; )

changed = false

for each basic block bi

old = Result(bi)

Result(bi)=

∩ or ∪

[m∈pred(bi) or succ(bi)]

(Gen(m) ∪ (Result(m)-Kill(m))

if (Result(bi) != old)

changed := true

The Overall Pattern

 Each data-flow analysis takes the form

Input(n) := ∅ if n is program entry/exit

:= Λ m∈Flow(n) Result(m) otherwise

Result(n) = ƒn (Input(n))

 where Λ is ∩ or ∪ (may vs. must analysis)

 May analysis: detect properties satisfied by at least one path (∪)

 Must analysis: detect properties satisfied by all paths(∩)

 Flow(n) is either pred(n) or succ(n) (forward vs. backward flow)

 Forward flow: data flow forward along control-flow edges.

 Input(n) is data entering n, Result is data exiting n

 Input(n) is ∅ if n is program entry

 Backward flow: data flow backward along control-flow edges.

 Input(n) is data exiting n, Result is data entering n

 Input(n) is ∅ if n is program exit

 Function ƒn is the transfer function associated with each block n

The Mathematical Foundation of

Dataflow Analysis

 Mathematical formulation of dataflow analysis

 The property space L is used to represent the data flow domain

information

 The combination operator Λ: P(L) → L is used to combine

information from different paths

 A set P is an ordered set if a partial order ≤ can be

defined s.t. ∀x,y,z∈P

 x ≤ x (reflexive)

 If x ≤ y and y ≤ x, then x = y (asymmetric)

 If x ≤ y and y ≤ z implies x ≤ z (transitive)

 Example: Power(L) with ⊆ define the partial order

Lattices

 An ordered set (L, ≤, V, Λ) is a lattice

 If x Λ y and x V y exist for all x,y∈L

 An lattice (L,≤, Λ) is a complete lattice if

 Each subset Y ⊆ L has a least upper bound and a greatest lower bound

 LeastUpperBound(Y) = Vm∈Y m

 GreatestLowerBound(Y) = Λ m∈Y m

 All finite lattices are complete

 Example lattice that is not complete: the set of all integers I

 For any x, y∈I, x Λ y = min(x,y), x V y = max(x,y)

 But LeastUpperBound(I) does not exist

 I ∪ {+∞,−∞} is a complete lattice

 Each complete lattice has

 A top element: the least element

 A bottom element: the greatest element

Chains

 A set S is a chain if ∀x,y∈S. y ≤ x or x ≤ y

 A set S has no infinite chains if every chain in S is finite

 A set S satisfies the finite ascending chain condition if

 For all sequences x

1

≤ x

2

≤ …, there exists n such that

 x

n

= x

n+

 That is, all chains in S have an finite upper bound

 A complete lattice L satisfies the finite ascending chain condition if

each ascending chain of L eventually stabilizes

 If l1≤ l2 ≤ l3 ≤ … , then there is an upper bound ln = ln+1=ln+2…

 This means starting from an arbitrary element e ∈ L, one can only

increase e by a finite number of times before reaching an upper bound