Embed
Email

Introduction to Data-flow analysis Data-flow Analysis Liveness

Document Sample
Introduction to Data-flow analysis Data-flow Analysis Liveness
Introduction to Data-flow analysis Data-flow Analysis



Last Time Idea

– Implementing a Mark and Sweep GC – Data-flow analysis derives information about the dynamic

behavior of a program by only examining the static code

Today

– Control flow graph construction Example

– Liveness analysis – How many registers do we need

1 a := 0

for the program on the right?

2 L1: b := a + 1

– Easy bound: the number of

variables used (3) 3 c := c + b

– Better answer is found by 4 a := b * 2

considering the dynamic 5 if a < 9 goto L1

requirements of the program 6 return c









CS553 Lecture Introduction to Data-flow Analysis 2 CS553 Lecture Introduction to Data-flow Analysis 3









Liveness Analysis Liveness by Example



Definition What is the live range of b?

– A variable is live at a particular point in the program if its value at that – Variable b is read in statement 4, 1 a = 0

point will be used in the future (dead, otherwise). so b is live on the (3 → 4) edge

∴ To compute liveness at a given point, we need to look into the future – Since statement 3 does not assign 2 b = a + 1

into b, b is also live on the (2→

Motivation: Register Allocation 3) edge 3 c = c + b

– A program contains an unbounded number of variables – Statement 2 assigns b, so any

value of b on the (1→2) and (5→ a = b * 2

– Must execute on a machine with a bounded number of registers 4

2) edges are not needed, so b is

– Two variables can use the same register if they are never in use at the same

dead along these edges

time (i.e, never simultaneously live). 5 a<9

∴ Register allocation uses liveness information No Yes

b’s live range is (2→3→4) 6 return c









CS553 Lecture Introduction to Data-flow Analysis 4 CS553 Lecture Introduction to Data-flow Analysis 5









1

Liveness by Example (cont) Control Flow Graphs (CFGs)



Live range of a Definition

– a is live from (1→2) and again from 1 a = 0 – A CFG is a graph whose nodes represent program statements and

(4→5→2) whose directed edges represent control flow

– a is dead from (2→3→4) 2 b = a + 1

Example 1 a = 0

Live range of b 3 c = c + b 1 a := 0 2 b = a + 1

– b is live from (2→3→4) 2 L1: b := a + 1

4 a = b * 2 3 c := c + b 3 c = c + b

Live range of c

4 a := b * 2

– c is live from 5 a<9

5 if a < 9 goto L1 4 a = b * 2

(entry→1→2→3→4→5→2, 5→6) No Yes

6 return c

6 return c 5 a<9

No Yes

Variables a and b are never simultaneously live, so they can share a register

6 return c



CS553 Lecture Introduction to Data-flow Analysis 6 CS553 Lecture Introduction to Data-flow Analysis 7









Terminology Uses and Defs



Flow Graph Terms Def (or definition) a = 0

– A CFG node has out-edges that lead to successor nodes and in-edges that – An assignment of a value to a variable

come from predecessor nodes – def_node[v] = set of CFG nodes that define variable v

– pred[n] is the set of all predecessors of node n – def[n] = set of variables that are defined at node n

succ[n] is the set of all successors of node n 1 a = 0

a < 9?

2 b = a + 1 Use

Examples – A read of a variable’s value

– Out-edges of node 5: (5→6) and (5→2) v live

3 c = c + b – use_node[v] = set of CFG nodes that use variable v

– succ[5] = {2,6} – use[n] = set of variables that are used at node n

– pred[5] = {4} 4 a = b * 2 ∉ def_node[v]

– pred[2] = {1,5}

More precise definition of liveness

5 a<9 – A variable v is live on a CFG edge if ∈ use_node[v]

No Yes (1) ∃ a directed path from that edge to a use of v (node in use_node[v]), and

6 return c (2) that path does not go through any def of v (no nodes in def_node[v])

CS553 Lecture Introduction to Data-flow Analysis 8 CS553 Lecture Introduction to Data-flow Analysis 9









2

program points

The Flow of Liveness Liveness at Nodes

edges

Data-flow We have liveness on edges just before computation

– Liveness of variables is a property that flows – How do we talk about a = 0

just after computation

through the edges of the CFG liveness at nodes?

1 a := 0

Direction of Flow Two More Definitions

– Liveness flows backwards through the CFG,

2 b := a + 1 – A variable is live-out at a node if it is live on any of that node’s out-

because the behavior at future nodes edges

3 c := c + b n

determines liveness at a given node live-out

out-edges

4 a := b * 2

– Consider a

– Consider b 5 a < 9? – A variable is live-in at a node if it is live on any of that node’s in-edges

– Later, we’ll see other properties No Yes

in-edges

that flow forward 6 return c

n live-in







CS553 Lecture Introduction to Data-flow Analysis 10 CS553 Lecture Introduction to Data-flow Analysis 11









Computing Liveness Solving the Data-flow Equations

Rules for computing liveness

Algorithm

(1) Generate liveness: live-in

n use

If a variable is in use[n], for each node n in CFG

it is live-in at node n in[n] = ∅; out[n] = ∅ initialize solutions

(2) Push liveness across edges: pred[n] repeat

live-out live-out live-out

If a variable is live-in at a node n for each node n in CFG

then it is live-out at all nodes in pred[n] n live-in in’[n] = in[n] save current results

(3) Push liveness across nodes: out’[n] = out[n]

If a variable is live-out at node n and not in def[n] live-in in[n] = use[n] ∪ (out[n] – def[n])

n solve data-flow equations

then the variable is also live-in at n live-out out[n] = ∪ in[s]

s ∈ succ[n]

Data-flow equations until in’[n]=in[n] and out’[n]=out[n] for all n test for convergence

(1) in[n] = use[n] ∪ (out[n] – def[n]) (3)



out[n] = ∪ in[s] (2) This is iterative data-flow analysis (for liveness analysis)

s ∈ succ[n]



CS553 Lecture Introduction to Data-flow Analysis 12 CS553 Lecture Introduction to Data-flow Analysis 13









3

Example Liveness in the MiniJava compiler

1st 2nd 3rd 4th 5th 6th 7th

node use def in out in out in out in out in out in out in out

# 1 a := 0

1 a a a ac c ac c ac c ac

2 a b a a bc ac bc ac bc ac bc ac bc ac bc 2 b := a + 1

3 bc c bc bc b bc b bc b bc b bc bc bc bc

4 b a b b a b a b ac bc ac bc ac bc ac 3 c := c + b

5 a a a a ac ac ac ac ac ac ac ac ac ac ac

6 c c c c c c c c 4 a := b * 2



Data-flow Equations for Liveness 5 a < 9?

No Yes

in[n] = use[n] ∪ (out[n] – def[n])

6 return c

out[n] = ∪ in[s]

s ∈ succ[n]









CS553 Lecture Introduction to Data-flow Analysis 14 CS553 Lecture Introduction to Data-flow Analysis 15









Concepts Next Time



Liveness Reading

– Used in register allocation – Ch. 8.4, 9.2-9.25, intro to data-flow analysis

– Generating liveness – Ch 8.8, register allocation

– Flow and direction

– Data-flow equations and analysis Lecture

– Register allocation

Control flow graphs

– Predecessors and successors Suggested Exercises

– For last week:

Defs and uses – 7.4.1, what would heap look like (draw pointers as arrows) with a

singly-linked free list? how would the best-fit algorithm work?

– 7.5.2, how does type safety or lack thereof affect GC?

– This week:

– 8.4.1, 9.2.1, 9.2.3, 8.8.1

CS553 Lecture Introduction to Data-flow Analysis 16 CS553 Lecture Introduction to Data-flow Analysis 17









4


Related docs
Other docs by rebeccaGerritY
INTRODUCTION TO POLITICS Fall Semester 2009
Views: 2  |  Downloads: 0
Contract of Submission
Views: 9  |  Downloads: 1
Introduction to Hedge Funds
Views: 33  |  Downloads: 2
Annual Report of Caucus Activities
Views: 3  |  Downloads: 0
Introduction to the Earth
Views: 8  |  Downloads: 0
Feedback report on consultation
Views: 6  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!