data-flow analysis - information sciences...

41
Spring 2016 CSCI 565 - Compiler Design Pedro Diniz [email protected] Data-Flow Analysis Live-Variable Analysis Copyright 2016, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies of these materials for their personal use.

Upload: others

Post on 24-Jan-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

Data-Flow AnalysisLive-Variable Analysis

Copyright 2016, Pedro C. Diniz, all rights reserved.Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies of these materials for their personal use.

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

2

Live-Variable Analysis• What is Live-Variable Analysis?

– For each Variable x where is the last program point p where the a specific value of x is used.

– In other words, for x and program point p determine if the value of x at pcan still be used along some path starting at p.

• If so, x is live at p

• If not x is dead at p

– Must take Control-Flow into account : a Data-Flow Problem !!!

• Applications:– Register Allocation: If a variable is dead at a given point p

• Can reuse its storage, i.e, the register it occupies if any;

• If its value as been modified must save the value to storage unless it is not live on exit of the procedure or loop

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

3

Live-Variable Analysis: Illustration• At point p0 the x variable is live:

– There is a path to p1 where value at p0 is used

– Beyond px towards p2 the value of x is no longer needed and is dead

… = x

… = xx = …

p0

p1

p2

px

• Need to observe for each variable and for each program point:– Where is the last program point beyond which the value is not used

– Trace back from uses to definitions and observe the first definition (backwards) that reaches that use.

– That definition kills all uses backwards of it.

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

4

Data-Flow Analysis Formulation

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

OUT(B) = ∪ IN(s)

• Variable is live at a point p if its value is used along at least one Path– A use of x prior to any definition in basic block means x must be alive– A definition of x in B prior to any subsequent use means previous uses must

be dead

• Gen Set: Set of Variables Used in B– Upward Exposed Reads of B

• Kill Set: Set of Variables Defined in B

S a successor of B

OUT set

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

5

Data-Flow Analysis Formulation

• Initialize IN(B) to Empty Set• Compute Gen/Use and Kill/Def for each Basic Block

– Tracing backwards from end of block to beginning of block– Initialize Last Instruction’s Out(i) to Empty– Use IN(i) = use(i) ∪ (OUT(i) - def(i))

• Iteratively Apply Relations to Basic Block Until Convergence– OUT(B) = ∪ IN(s)– IN(B) = Use(B) ∪ (OUT(B) - Def(B))

• Given OUT(B) use relations at instruction level to determine the live variables after each instruction

S a successor of B

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

6

i = 0b = 0x = p

if(i < p) goto L1

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

7

Use & Def Functions for a Basic Block

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

Out = { }

In = Use ∪ (Out - Def)

In(i) = Use(i) ∪ (Out(i) - Def(i))

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

8

Use & Def Functions for a Basic Block

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

Out = { }

In = {a,b} ∪ ({} - {}) = {a,b}

In(i) = Use(i) ∪ (Out (i) - Def(i))

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

9

Use & Def Functions for a Basic Block

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

Out = { }

Out = {a,b}

In = Use ∪ (Out - Def)

In(i) = Use(i) ∪ (Out(i) - Def(i))

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

10

Use & Def Functions for a Basic Block

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

Out = { }

Out = {a,b}

In = {t} ∪ ({a,b} - {b})

In(i) = Use(i) ∪ (Out(i) - Def(i))

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

11

Use & Def Functions for a Basic Block

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

Out = { }

Out = {a,b}

Out = {a,t}

In(i) = Use(i) ∪ (Out(i) - Def(i))

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

12

Use & Def Functions for a Basic Block

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

Out = { }

Out = {a,b}

Out = {a,t}

In = Use ∪ (Out - Def)

In(i) = Use(i) ∪ (Out(i) - Def(i))

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

13

Use & Def Functions for a Basic Block

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

Out = { }

Out = {a,b}

Out = {a,t}

In = {a} ∪ ({a,t} - {t})

In(i) = Use(i) ∪ (Out(i) - Def(i))

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

14

Use & Def Functions for a Basic Block

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

Out = { }

Out = {a,b}

Out = {a,t}

In = {a}

In(i) = Use(i) ∪ (Out(i) - Def(i))

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

15

Use & Def Functions for a Basic Block

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

OutUse = { }

OutUse = {a,b}

OutUse = {a,t}

InUse = {a}

InUse(i) = Use(i) ∪ (OutUse(i) - Def(i))

Use

(B) =

{a}

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

16

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

OutDef = { }

InDef(i) = Def(i) ∪ OutDef(i)

Use & Def Functions for a Basic Block

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

17

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

OutDef = { }

InDef(i) = Def(i) ∪ OutDef(i)

InDef = Def ∪ OutDef = { }

Use & Def Functions for a Basic Block

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

18

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

OutDef = { }

InDef(i) = Def(i) ∪ OutDef(i)

OutDef = { }

InDef = Def ∪ OutDef = {b}

Use & Def Functions for a Basic Block

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

19

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

OutDef = { }

InDef(i) = Def(i) ∪ OutDef(i)

OutDef = { }

OutDef = {b}

InDef = Def ∪ OutDef = {t} ∪ {b}

Use & Def Functions for a Basic Block

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

20

t = a +1

b = t

if (a = b) goto L2use = { a, b }def = { }

use = { t }def = { b }

use = { a }def = { t }

OutDef = { }

InDef(i) = Def(i) ∪ OutDef(i)

OutDef = { }

OutDef = {b}

InDef = {t, b}D

ef(B

) = {t

,b}

Use & Def Functions for a Basic Block

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

21

• Can be Accomplished by a Forward Scanning of the Block– Keep Track of Which Variables are Read before they are written thus

computing the Upwards Exposed Reads (UpExp) or Use Function– Track Variables that are Written or Killed (VarKill) or Def Function

// Assume instruction in format “x ← y op z”for i ← 1 to Num Instructions in B do

if (instr(i) is leader of B) thenb ← Number(B);UpExp(b) ←∅;VarKill(b) ←∅;

if y ∉ VarKill(b) thenUpExp(b) ← UpExp(b) ∪ {y}

if z ∉ VarKill(b) thenUpExp(b) ← UpExp(b) ∪ {z}

VarKill(b) ← VarKill(b) ∪ {x}

Use & Def Functions for a Basic Block

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

22

i = 0b = 0x = p

if(i < p) goto L1

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

23

i = 0b = 0x = p

if(i < p) goto L1

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

24

i = 0b = 0x = p

if(i < p) goto L1

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { }

Out = { }

Out = { }

In = { } In = { }

In = { }Out = { }

Out = { }

In = { }

Out = { }

Out = { }In = { }

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

25

i = 0b = 0x = p

if(i < p) goto L1

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { }

Out = { }

In = { } In = { }

In = { }Out = { }

Out = { }

In = { }

Out = { }

Out = { }In = { }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

26

i = 0b = 0x = p

if(i < p) goto L1

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { }

Out = { }

In = { } In = { }

In = { }Out = { b }

Out = { i, p, b}

In = { }

Out = { }

Out = { }In = { }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

27

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { }

Out = { }

In = { } In = { }

In = { }Out = { b }

Out = { i, p, b}

In = { p }

Out = { }

Out = { }In = { }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

28

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { }

Out = { i }

In = { } In = { }

In = { }Out = { b }

Out = { i, p, b}

In = { p }

Out = { }

Out = { i }In = { i }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

29

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { }

Out = { i }

In = { x, i } In = { }

In = { }Out = { b }

Out = { i, p, b}

In = { p }

Out = { }

Out = { i }In = { i }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

30

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { }

Out = { i }

In = { x, i } In = { x, i }

In = { }Out = { b }

Out = { i, p, b}

In = { p }

Out = { }

Out = { i }In = { i }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

31

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { }

Out = { i }

In = { x, i } In = { x, i }

In = { a, i, x }Out = { b }

Out = { i, p, b}

In = { p }

Out = { x, i }

Out = { i }In = { i }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

32

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { a, i, x }

Out = { i }

In = { x, a } In = { x, a }

In = { a, i, x }Out = { b }

Out = { i, p, b}

In = { p }

Out = { x, a }

Out = { i }In = { a, i, x }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

33

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x + 1 a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { a, i, x }

Out = { a,i,x }

In = { i,x } In = { i,x }

In = { a, i, x }Out = { b }

Out = { i, p, b}

In = { p }

Out = { x, a }

Out = { a,i,x }In = { a, i, x }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

34

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { a, , x }

Out = { a,i,x }

In = { i,x } In = { i,x }

In = { a,i,x }Out = { b }

Out = { i, p, b}

In = { p }

Out = { i,x }

Out = { a,i,x }In = { a, i, x }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

a = x + 1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

35

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { a,i,x }

Out = { a,i,x }

In = { i,x } In = { i,x }

In = { a,i,x }Out = { b }

Out = { i, p, b}

In = { p }

Out = { i,x }

Out = { a,i,x }In = { a, i, x }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

a = x + 1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

36

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { a,i,x }

Out = { a,i,x }

In = { i,x } In = { i,x }

In = { a,i,x }Out = { b }

Out = { i, p, b}

In = { p }

Out = { i,x }

Out = { a,i,x }In = { a, i, x }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

a = x + 1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

37

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { a,i,x }

Out = { a,i,x }

In = { i,x } In = { i,x }

In = { a,i,x }Out = { b }

Out = { i, p, b}

In = { p }

Out = { i,x }

Out = { a,i,x }In = { a, i, x }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

a = x + 1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

38

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { a,i,x }

Out = { a,i,x }

In = { i,x } In = { i,x }

In = { a,i,x }Out = { a,b,i,x }

Out = { a,b,i,p,x }

In = { p }

Out = { i,x }

Out = { a,i,x }In = { a, i, x }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

if(i < p) goto L1

a = x + 1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

39

i = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x - 1

i = i + 1goto L3

b = b + 1x = 0

use = { a }def = { t, b }

use = { x }def = { a }

use = { x }def = { a }

use = { i }def = { i }

use = { b }def = { x, b }

use = { i, p }def = { }

use = { p }def = { i, b, x }

Out = { }

In = { b }

Out = { a,i,x }

Out = { a,i,x }

In = { i,x } In = { i,x }

In = { a,i,x }Out = { a,b,i,x }

Out = { a,b,i,p,x }

In = { a, p }

Out = { i,x }

Out = { a,i,x }In = { a, i, x }

OUT(B) = ∪ IN(s)

IN(B) = Use(B) ∪ (OUT(B) - Def(B))

S a successor of B

a = x + 1

if(i < p) goto L1

Example

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

40

Examplei = 0b = 0x = p

t = a +1b = tif (a = b) goto L2

a = x - 1

i = i + 1goto L3

b = b + 1x = 0

Out = { }

Out = { a,i,x }

Out = { a,i,x }

Out = { a,b,i,x }

Out = { a,b,i,p,x }

Out = { i,x }

Out = { a,i,x }a = x + 1

if(i < p) goto L1

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

41

Summary• What is Live-Variable Analysis?

– Backward Data-Flow Analysis Problem

– Upwards Exposed (Gen) - Computed in a Forward Pass

• Most Significant Application– Register Allocation