• No results found

Bit Vector Data Flow Frameworks

N/A
N/A
Protected

Academic year: 2022

Share "Bit Vector Data Flow Frameworks"

Copied!
305
0
0

Loading.... (view fulltext now)

Full text

(1)

Bit Vector Data Flow Frameworks

Uday Khedker

Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Jul 2010

(2)

Part 1

About These Slides

(3)

CS 618 Bit Vector Frameworks: About These Slides

Copyright

These slides constitute the lecture notes for CS618 Program Analysis course at IIT Bombay and have been made available as teaching material accompanying the book:

• Uday Khedker, Amitabha Sanyal, and Bageshri Karkare. Data Flow Analysis: Theory and Practice. CRC Press (Taylor and Francis Group). 2009.

Apart from the above book, some slides are based on the material from the following books

• M. S. Hecht. Flow Analysis of Computer Programs. Elsevier North-Holland Inc. 1977.

• F. Nielson, H. R. Nielson, and C. Hankin. Principles of Program Analysis. Springer-Verlag. 1998.

These slides are being made available under GNU FDL v1.2 or later purely for academic or research use.

Jul 2010 IIT Bombay

(4)

CS 618 Bit Vector Frameworks: Outline 2/93

Outline

• Live Variables Analysis

• Program Execution Model and Semantics

• Soundness of Data Flow Analysis

• Available Expressions Analysis

• Anticipable Expressions Analysis

• Reaching Definitions Analysis

• Common Features of Bit Vector Frameworks

• Partial Redundancy Elimination

Jul 2010 IIT Bombay

(5)

Part 2

Live Variables Analysis

(6)

CS 618 Bit Vector Frameworks: Live Variables Analysis 3/93

Defining Live Variables Analysis

A variable v is live at a program point p, if some path from p to program exit contains an r-value oc- currence of v which is not preceded by an l-value occurrence of v .

v = a∗b

a = v +2 End

p

Start

v = a∗b

v = a +2 End

p

Start

v=v+ 2

v = v +2 End

p

Start

Jul 2010 IIT Bombay

(7)

CS 618

Defining Live Variables Analysis

A variable v is live at a program point p, if some path from p to program exit contains an r-value oc- currence of v which is not preceded by an l-value occurrence of v .

v is live at p

v = a∗b

a = v +2 End

p

Start

v = a∗b

v = a +2 End

p

Start

v=v+ 2

v = v +2 End

p

Start

Jul 2010 IIT Bombay

(8)

CS 618 Bit Vector Frameworks: Live Variables Analysis 3/93

Defining Live Variables Analysis

A variable v is live at a program point p, if some path from p to program exit contains an r-value oc- currence of v which is not preceded by an l-value occurrence of v .

v is live at p v is not live at p

v = a∗b

a = v +2 End

p

Start

v = a∗b

v = a +2 End

p

Start

v=v+ 2

v = v +2 End

p

Start

Jul 2010 IIT Bombay

(9)

CS 618

Defining Live Variables Analysis

A variable v is live at a program point p, if some path from p to program exit contains an r-value oc- currence of v which is not preceded by an l-value occurrence of v .

v is live at p v is not live at p v is live at p

v = a∗b

a = v +2 End

p

Start

v = a∗b

v = a +2 End

p

Start

v=v+ 2

v = v +2 End

p

Start

Jul 2010 IIT Bombay

(10)

CS 618 Bit Vector Frameworks: Live Variables Analysis 3/93

Defining Live Variables Analysis

A variable v is live at a program point p, if some path from p to program exit contains an r-value oc- currence of v which is not preceded by an l-value occurrence of v .

Path based specification

v is live at p v is not live at p v is live at p

v = a∗b

a = v +2 End

p

Start

v = a∗b

v = a +2 End

p

Start

v=v+ 2

v = v +2 End

p

Start

Jul 2010 IIT Bombay

(11)

CS 618

Defining Data Flow Analysis for Live Variables Analysis

In

i

Gen

i

, Kill

i

Out

i

In

j

Gen

j

, Kill

j

Out

j

In

k

= Gen

k

∪ (Out

k

Kill

k

) Gen

k

, Kill

k

Out

k

= In

i

In

j

Jul 2010 IIT Bombay

(12)

CS 618 Bit Vector Frameworks: Live Variables Analysis 4/93

Defining Data Flow Analysis for Live Variables Analysis

In

i

Gen

i

, Kill

i

Out

i

In

j

Gen

j

, Kill

j

Out

j

In

k

= Gen

k

∪ (Out

k

Kill

k

) Gen

k

, Kill

k

Out

k

= In

i

In

j

Basic Blocks ≡ Single statements or Maximal groups of sequentially executed statements

Jul 2010 IIT Bombay

(13)

CS 618

Defining Data Flow Analysis for Live Variables Analysis

In

i

Gen

i

, Kill

i

Out

i

In

j

Gen

j

, Kill

j

Out

j

In

k

= Gen

k

∪ (Out

k

Kill

k

) Gen

k

, Kill

k

Out

k

= In

i

In

j

Basic Blocks ≡ Single statements or Maximal groups of sequentially executed statements

Control Transfer

Jul 2010 IIT Bombay

(14)

CS 618 Bit Vector Frameworks: Live Variables Analysis 4/93

Defining Data Flow Analysis for Live Variables Analysis

In

i

Gen

i

, Kill

i

Out

i

In

j

Gen

j

, Kill

j

Out

j

In

k

= Gen

k

∪ (Out

k

Kill

k

) Gen

k

, Kill

k

Out

k

= In

i

In

j

In

i

Gen

i

, Kill

i

Out

i

In

j

Gen

j

, Kill

j

Out

j

In

k

= Gen

k

∪ (Out

k

Kill

k

) Gen

k

, Kill

k

Out

k

= In

i

In

j

Jul 2010 IIT Bombay

(15)

CS 618

Defining Data Flow Analysis for Live Variables Analysis

In

i

Gen

i

, Kill

i

Out

i

In

j

Gen

j

, Kill

j

Out

j

In

k

= Gen

k

∪ (Out

k

Kill

k

) Gen

k

, Kill

k

Out

k

= In

i

In

j

In

i

Gen

i

, Kill

i

Out

i

In

j

Gen

j

, Kill

j

Out

j

In

k

= Gen

k

∪ (Out

k

Kill

k

) Gen

k

, Kill

k

Out

k

= In

i

In

j

Local Data Flow Properties

Jul 2010 IIT Bombay

(16)

CS 618 Bit Vector Frameworks: Live Variables Analysis 5/93

Local Data Flow Properties for Live Variables Analysis

Gen

n

= { v | variable v is used in basic block n and is not preceded by a definition of v } Kill

n

= { v | basic block n contains a definition of v }

Jul 2010 IIT Bombay

(17)

CS 618

Local Data Flow Properties for Live Variables Analysis

Gen

n

= { v | variable v is used in basic block n and is not preceded by a definition of v } Kill

n

= { v | basic block n contains a definition of v } r-value occurrence

Value is only read, e.g. x,y,z in x.sum = y.data + z.data

Jul 2010 IIT Bombay

(18)

CS 618 Bit Vector Frameworks: Live Variables Analysis 5/93

Local Data Flow Properties for Live Variables Analysis

Gen

n

= { v | variable v is used in basic block n and is not preceded by a definition of v } Kill

n

= { v | basic block n contains a definition of v } r-value occurrence

Value is only read, e.g. x,y,z in x.sum = y.data + z.data

l-value occurrence

Value is modified e.g. y in y = x.lptr

Jul 2010 IIT Bombay

(19)

CS 618

Local Data Flow Properties for Live Variables Analysis

Gen

n

= { v | variable v is used in basic block n and is not preceded by a definition of v } Kill

n

= { v | basic block n contains a definition of v } r-value occurrence

Value is only read, e.g. x,y,z in x.sum = y.data + z.data

l-value occurrence

Value is modified e.g. y in y = x.lptr

within n

Jul 2010 IIT Bombay

(20)

CS 618 Bit Vector Frameworks: Live Variables Analysis 5/93

Local Data Flow Properties for Live Variables Analysis

Gen

n

= { v | variable v is used in basic block n and is not preceded by a definition of v } Kill

n

= { v | basic block n contains a definition of v } r-value occurrence

Value is only read, e.g. x,y,z in x.sum = y.data + z.data

l-value occurrence

Value is modified e.g. y in y = x.lptr

within n

anywhere in n

Jul 2010 IIT Bombay

(21)

CS 618

Defining Data Flow Analysis for Live Variables Analysis

In

i

Gen

i

, Kill

i

Out

i

In

j

Gen

j

, Kill

j

Out

j

In

k

= Gen

k

∪ (Out

k

Kill

k

) Gen

k

, Kill

k

Out

k

= In

i

In

j

Jul 2010 IIT Bombay

(22)

CS 618 Bit Vector Frameworks: Live Variables Analysis 6/93

Defining Data Flow Analysis for Live Variables Analysis

In

i

Gen

i

, Kill

i

Out

i

In

j

Gen

j

, Kill

j

Out

j

In

k

= Gen

k

∪ (Out

k

Kill

k

) Gen

k

, Kill

k

Out

k

= In

i

In

j

Global Data Flow Properties

Jul 2010 IIT Bombay

(23)

CS 618

Defining Data Flow Analysis for Live Variables Analysis

In

i

Gen

i

, Kill

i

Out

i

In

j

Gen

j

, Kill

j

Out

j

In

k

= Gen

k

∪ (Out

k

Kill

k

) Gen

k

, Kill

k

Out

k

= In

i

In

j

Global Data Flow Properties Edge based

specifications

Jul 2010 IIT Bombay

(24)

CS 618 Bit Vector Frameworks: Live Variables Analysis 7/93

Data Flow Equations For Live Variables Analysis

In

n

= (Out

n

Kill

n

) ∪ Gen

n

Out

n

=

BI n is End block [

s∈succ(n)

In

s

otherwise

Jul 2010 IIT Bombay

(25)

CS 618

Data Flow Equations For Live Variables Analysis

In

n

= (Out

n

Kill

n

) ∪ Gen

n

Out

n

=

BI n is End block [

s∈succ(n)

In

s

otherwise

In

n

and Out

n

are sets of variables.

Jul 2010 IIT Bombay

(26)

CS 618 Bit Vector Frameworks: Live Variables Analysis 8/93

Data Flow Equations for Our Example

w = x 1

while (x.data < max) 2

x = x.rptr 3 y = x.lptr

4

z = New class of z 5

y = y.lptr 6

z.sum = x.data + y.data 7

In

1

= (Out

1

Kill

1

) ∪ Gen

1

Out

1

= In

2

In

2

= (Out

2

Kill

2

) ∪ Gen

2

Out

2

= In

3

In

4

In

3

= (Out

3

Kill

3

) ∪ Gen

3

Out

3

= In

2

In

4

= (Out

4

Kill

4

) ∪ Gen

4

Out

4

= In

5

In

5

= (Out

5

Kill

5

) ∪ Gen

5

Out

5

= In

6

In

6

= (Out

6

Kill

6

) ∪ Gen

6

Out

6

= In

7

In

7

= (Out

7

Kill

7

) ∪ Gen

7

Out

7

= In

7

Jul 2010 IIT Bombay

(27)

CS 618

Data Flow Equations for Our Example

w = x 1

while (x.data < max) 2

x = x.rptr 3 y = x.lptr

4

z = New class of z 5

y = y.lptr 6

z.sum = x.data + y.data 7

In

1

= (Out

1

Kill

1

) ∪ Gen

1

Out

1

= In

2

In

2

= (Out

2

Kill

2

) ∪ Gen

2

Out

2

= In

3

In

4

In

3

= (Out

3

Kill

3

) ∪ Gen

3

Out

3

= In

2

In

4

= (Out

4

Kill

4

) ∪ Gen

4

Out

4

= In

5

In

5

= (Out

5

Kill

5

) ∪ Gen

5

Out

5

= In

6

In

6

= (Out

6

Kill

6

) ∪ Gen

6

Out

6

= In

7

In

7

= (Out

7

Kill

7

) ∪ Gen

7

Out

7

= In

7

Jul 2010 IIT Bombay

(28)

CS 618 Bit Vector Frameworks: Live Variables Analysis 9/93

Performing Live Variables Analysis

Gen = {x }, Kill ={w } w = x Gen ={x}, Kill =∅

while (x.data < max)

Gen = { x }, Kill ={ x } x = x.rptr Gen = { x }, Kill ={ y }

y = x.lptr Gen = ∅, Kill ={z}

z = New class of z Gen ={ y }, Kill ={ y }

y = y.lptr Gen ={x, y, z}, Kill =∅

z.sum = x.data + y.data

Jul 2010 IIT Bombay

(29)

CS 618

Performing Live Variables Analysis

Gen = {x }, Kill ={w } w = x Gen ={x}, Kill =∅

while (x.data < max)

Gen = { x }, Kill ={ x } x = x.rptr Gen = { x }, Kill ={ y }

y = x.lptr Gen = ∅, Kill ={z}

z = New class of z Gen ={ y }, Kill ={ y }

y = y.lptr Gen ={x, y, z}, Kill =∅

z.sum = x.data + y.data

Gen and Kill need not be mutually exclusive

Jul 2010 IIT Bombay

(30)

CS 618 Bit Vector Frameworks: Live Variables Analysis 9/93

Performing Live Variables Analysis

Gen = {x }, Kill ={w } w = x Gen ={x}, Kill =∅

while (x.data < max)

Gen = { x }, Kill ={ x } x = x.rptr Gen = { x }, Kill ={ y }

y = x.lptr Gen = ∅, Kill ={z}

z = New class of z Gen ={ y }, Kill ={ y }

y = y.lptr Gen ={x, y, z}, Kill =∅

z.sum = x.data + y.data

z is an r-value occurrence and not an l-value occurrence

Jul 2010 IIT Bombay

(31)

CS 618

Performing Live Variables Analysis

Gen = {x }, Kill ={w } w = x Gen ={x}, Kill =∅

while (x.data < max)

Gen = { x }, Kill ={ x } x = x.rptr Gen = { x }, Kill ={ y }

y = x.lptr Gen = ∅, Kill ={z}

z = New class of z Gen ={ y }, Kill ={ y }

y = y.lptr Gen ={x, y, z}, Kill =∅

z.sum = x.data + y.data

x, y , z are considered to be used based purely on local use even if the value of z is not use later. A different analysis called faint vari- ables analysis improves on this.

Jul 2010 IIT Bombay

(32)

CS 618 Bit Vector Frameworks: Live Variables Analysis 9/93

Performing Live Variables Analysis

Gen = {x }, Kill ={w } w = x Gen ={x}, Kill =∅

while (x.data < max)

Gen = { x }, Kill ={ x } x = x.rptr Gen = { x }, Kill ={ y }

y = x.lptr Gen = ∅, Kill ={z}

z = New class of z Gen ={ y }, Kill ={ y }

y = y.lptr Gen ={x, y, z}, Kill =∅

z.sum = x.data + y.data Initialization

Jul 2010 IIT Bombay

(33)

CS 618

Performing Live Variables Analysis

Gen = {x }, Kill ={w } w = x Gen ={x}, Kill =∅

while (x.data < max)

Gen = { x }, Kill ={ x } x = x.rptr Gen = { x }, Kill ={ y }

y = x.lptr Gen = ∅, Kill ={z}

z = New class of z Gen ={ y }, Kill ={ y }

y = y.lptr Gen ={x, y, z}, Kill =∅

z.sum = x.data + y.data

Traversal

Iteration #1 {x, y, z }

{x, y, z } {x, y, z } {x, y, z } {x, y } {x, y}

{x}

∅ {x}

{ x } {x}

{x}

{x}

Ignoring max be- cause we are doing analysis for pointer variables w, x, y, z

Jul 2010 IIT Bombay

(34)

CS 618 Bit Vector Frameworks: Live Variables Analysis 9/93

Performing Live Variables Analysis

Gen = {x }, Kill ={w } w = x Gen ={x}, Kill =∅

while (x.data < max)

Gen = { x }, Kill ={ x } x = x.rptr Gen = { x }, Kill ={ y }

y = x.lptr Gen = ∅, Kill ={z}

z = New class of z Gen ={ y }, Kill ={ y }

y = y.lptr Gen ={x, y, z}, Kill =∅

z.sum = x.data + y.data

Traversal

Iteration #1 {x, y, z }

{x, y, z } {x, y, z } {x, y, z } {x, y } {x, y}

{x}

∅ {x}

{ x } {x}

{x}

{x}

Ignoring max be- cause we are doing analysis for pointer variables w, x, y, z

Iteration #2

∅ {x, y, z } {x, y, z } {x, y, z } {x, y, z }

{x, y}

{x, y}

{x}

{x}

{x}

{ x } {x}

{x}

{x}

Jul 2010 IIT Bombay

(35)

CS 618

Performing Live Variables Analysis

Gen = {x }, Kill ={w } w = x

Gen ={x}, Kill =∅

while (x.data < max)

Gen = {x }, Kill ={x } x = x.rptr Gen = {x}, Kill ={y , z}

y = x.lptr z = New class of z

y = y.lptr z.sum = x.data + y.data

Jul 2010 IIT Bombay

(36)

CS 618 Bit Vector Frameworks: Live Variables Analysis 11/93

Local Data Flow Properties for Live Variables Analysis

In

n

= (Out

n

Kill

n

) ∪ Gen

n

Gen

n

: Use not preceded by definition

Kill

n

: Definition anywhere in a block

Jul 2010 IIT Bombay

(37)

CS 618

Local Data Flow Properties for Live Variables Analysis

In

n

= (Out

n

Kill

n

) ∪ Gen

n

Gen

n

: Use not preceded by definition Upwards exposed use

Kill

n

: Definition anywhere in a block

Stop the effect from being propagated across a block

Jul 2010 IIT Bombay

(38)

CS 618 Bit Vector Frameworks: Live Variables Analysis 12/93

Local Data Flow Properties for Live Variables Analysis

Case Local Information Example Explanation 1 v 6∈ Gen

n

v 6∈ Kill

n

2 vGen

n

v 6∈ Kill

n

3 v 6∈ Gen

n

vKill

n

4 vGen

n

vKill

n

Jul 2010 IIT Bombay

(39)

CS 618

Local Data Flow Properties for Live Variables Analysis

Case Local Information Example Explanation 1 v 6∈ Gen

n

v 6∈ Kill

n

a = b + c

b = cd

liveness of v is unaffected by the basic block

2 vGen

n

v 6∈ Kill

n

a = b + c b = vd

v becomes live before the basic block 3 v 6∈ Gen

n

vKill

n

a = b + c

v = cd

v ceases to be live before the statement 4 vGen

n

vKill

n

a = v + c

v = cd

liveness of v is killed but v becomes live before the statement

Jul 2010 IIT Bombay

(40)

CS 618 Bit Vector Frameworks: Live Variables Analysis 13/93

Using Data Flow Information of Live Variables Analysis

• Used for register allocation.

If variable x is live in a basic block b, it is a potential candidate for register allocation.

Jul 2010 IIT Bombay

(41)

CS 618

Using Data Flow Information of Live Variables Analysis

• Used for register allocation.

If variable x is live in a basic block b, it is a potential candidate for register allocation.

• Used for dead code elimination.

If variable x is not live after an assignment x = . . ., then the assginment is redundant and can be deleted as dead code.

Jul 2010 IIT Bombay

(42)

CS 618 Bit Vector Frameworks: Live Variables Analysis 14/93

Tutorial Problem 1 for Liveness Analysis a = 4

b = 2 c = 3 n = c*2

n1

if (a>n) n2 a = a+1 n3

if (a≥12) n4 t1 = a+b a = t1+c n5

nop n6 F

F T

T

Local Data Flow Information

Gen Kill

n1 ∅ {a, b, c, n}

n2 {a, n}

n3 {a} {a}

n4 {a} ∅

n5 {a, b, c} {a, t 1}

n6 ∅ ∅

Jul 2010 IIT Bombay

(43)

CS 618

Tutorial Problem 1 for Liveness Analysis a = 4

b = 2 c = 3 n = c*2

n1

if (a>n) n2 a = a+1 n3

if (a≥12) n4 t1 = a+b a = t1+c n5

nop n6 F

F T

T

Local Data Flow Information

Gen Kill

n1 ∅ {a, b, c, n}

n2 {a, n}

n3 {a} {a}

n4 {a} ∅

n5 {a, b, c} {a, t 1}

n6 ∅ ∅

Global Data Flow Information Iteration #1 Iteration #2

Out In Out In

n6 ∅ ∅

n5 ∅ {a, b, c}

n4 {a, b, c} {a, b, c}

n3 ∅ {a}

n2 {a, b, c} {a, b, c, n}

n1 {a, b, c, n}

Jul 2010 IIT Bombay

(44)

CS 618 Bit Vector Frameworks: Live Variables Analysis 14/93

Tutorial Problem 1 for Liveness Analysis a = 4

b = 2 c = 3 n = c*2

n1

if (a>n) n2 a = a+1 n3

if (a≥12) n4 t1 = a+b a = t1+c n5

nop n6 F

F T

T

Local Data Flow Information

Gen Kill

n1 ∅ {a, b, c, n}

n2 {a, n}

n3 {a} {a}

n4 {a} ∅

n5 {a, b, c} {a, t 1}

n6 ∅ ∅

Global Data Flow Information Iteration #1 Iteration #2

Out In Out In

n6 ∅ ∅ ∅ ∅

n5 ∅ {a, b, c} ∅ {a, b, c}

n4 {a, b, c} {a, b, c} {a, b, c} {a, b, c}

n3 ∅ {a} {a, b, c} {a, b, c}

n2 {a, b, c} {a, b, c, n} {a, b, c, n} {a, b, c, n}

n1 {a, b, c, n} ∅ {a, b, c, n}

Jul 2010 IIT Bombay

(45)

CS 618

Tutorial Problem 2 for Liveness Analysis: C Program

1 int x, y, z;

2 int exmp(void) 3 { int a, b, c, d;

4 b = 4;

5 a = b + c;

6 d = a * b;

7 if (x < y)

8 b = a -c;

9 else

10 { do

11 { c = b + c;

12 if (y > x)

13 { do

14 { d = a + b;

15 f(b + c);

16 } while(y > x);

17 }

18 else

19 { c = a * b;

20 f(a - b);

21 }

22 g (a + b);

23 } while(z > x);

24 }

25 h(a-c);

26 f(b+c);

27 }

Jul 2010 IIT Bombay

(46)

CS 618 Bit Vector Frameworks: Live Variables Analysis 16/93

Tutorial Problem 2 for Liveness Analysis: Control Flow Graph

n

1

b = 4;

a = b + c;

d = ab; n

1

n

2

b = ac; n

2

n

3

c = b + c; n

3

n

4

c = ab;

f (a − b); n

4

n

5

d = a + b; n

5

n

6

f (b + c); n

6

n

7

g (a + b); n

7

n

8

h(ac);

f (b + c); n

8

V ar = {a, b, c, d}

Jul 2010 IIT Bombay

(47)

CS 618

Tutorial Problem 2 for Liveness Analysis: Control Flow Graph

n

1

b = 4;

a = b + c;

d = ab; n

1

n

2

b = ac; n

2

n

3

c = b + c; n

3

n

4

c = ab;

f (a − b); n

4

n

5

d = a + b; n

5

n

6

f (b + c); n

6

n

7

g (a + b); n

7

n

8

h(ac);

f (b + c); n

8

V ar = {a, b, c, d}

n

5

and n

6

have been artificially separated.

gcc combines them.

Jul 2010 IIT Bombay

(48)

CS 618 Bit Vector Frameworks: Live Variables Analysis 17/93

Solution of the Tutorial Problem

Local Global Information

Block Information Iteration # 1 Iteration # 2 Gen

n

Kill

n

Out

n

In

n

Out

n

In

n

n

8

{a, b, c} ∅ ∅ {a, b, c } ∅ {a, b, c } n

7

{a, b} ∅ {a, b, c } {a, b, c } {a, b, c } {a, b, c } n

6

{b, c } ∅ {a, b, c } {a, b, c } {a, b, c } {a, b, c } n

5

{a, b} {d } {a, b, c } {a, b, c } {a, b, c } {a, b, c } n

4

{a, b} {c} {a, b, c } {a, b} {a, b, c } {a, b}

n

3

{b, c } {c} {a, b, c } {a, b, c } {a, b, c } {a, b, c } n

2

{a, c} {b} {a, b, c } {a, c } {a, b, c } {a, c } n

1

{c } {a, b, d } {a, b, c } {c} {a, b, c } {c}

Jul 2010 IIT Bombay

(49)

CS 618

Tutorial Problems for Liveness Analysis

• Perform analysis with universal set V ar as the initialization at internal nodes.

• Modify the previous program so that some data flow value computed in second iteration differs from the corresponding data flow value computed in the first iteration.

(No structural changes, suggest at least two distinct kinds of modifications)

• Modify the above program so that some data flow value computed in third iteration differs from the corresponding data flow value computed in the second iteration.

Write a C program corresponding to the modified control flow graph

Jul 2010 IIT Bombay

(50)

Part 3

Program Execution Model and

Semantics

(51)

CS 618

Our Language

• Variables v ∈ V ar, expressions e ∈ E xpr and labels l , m ∈ L abel

Expressions compute integer or boolean values

A label is an index that holds the position of a statement in a program

• Labelled three address code statements

• We assume that the programs are type correct

Jul 2010 IIT Bombay

(52)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 20/93

Statements in Our Language

• Assignment l : v = e where l ∈ L abel, v ∈ V ar and e ∈ E xpr

Jul 2010 IIT Bombay

(53)

CS 618

Statements in Our Language

• Assignment l : v = e where l ∈ L abel, v ∈ V ar and e ∈ E xpr

• Expression computation l : e where l ∈ L abel and e ∈ E xpr

(This models use of variables in statements other than assignments)

Jul 2010 IIT Bombay

(54)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 20/93

Statements in Our Language

• Assignment l : v = e where l ∈ L abel, v ∈ V ar and e ∈ E xpr

• Expression computation l : e where l ∈ L abel and e ∈ E xpr

(This models use of variables in statements other than assignments)

• Unconditional jump l : goto m where l , m ∈ L abel

Jul 2010 IIT Bombay

(55)

CS 618

Statements in Our Language

• Assignment l : v = e where l ∈ L abel, v ∈ V ar and e ∈ E xpr

• Expression computation l : e where l ∈ L abel and e ∈ E xpr

(This models use of variables in statements other than assignments)

• Unconditional jump l : goto m where l , m ∈ L abel

• Conditional jump l : if e goto m where l, m ∈ L abel, and e ∈ E xpr

Jul 2010 IIT Bombay

(56)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 20/93

Statements in Our Language

• Assignment l : v = e where l ∈ L abel, v ∈ V ar and e ∈ E xpr

• Expression computation l : e where l ∈ L abel and e ∈ E xpr

(This models use of variables in statements other than assignments)

• Unconditional jump l : goto m where l , m ∈ L abel

• Conditional jump l : if e goto m where l, m ∈ L abel, and e ∈ E xpr

• No operation l : nop

Jul 2010 IIT Bombay

(57)

CS 618

Statements in Our Language

• Assignment l : v = e where l ∈ L abel, v ∈ V ar and e ∈ E xpr

• Expression computation l : e where l ∈ L abel and e ∈ E xpr

(This models use of variables in statements other than assignments)

• Unconditional jump l : goto m where l , m ∈ L abel

• Conditional jump l : if e goto m where l, m ∈ L abel, and e ∈ E xpr

• No operation l : nop

(Other statements such as function calls, returns, heap accesses etc. will be added when required)

Jul 2010 IIT Bombay

(58)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 21/93

Context Free Grammar of Our Language

• program (P), statement (S), label (m)

• expression (E), arithmetic expression (aE), boolean expression (bE)

• binary arithmetic operator (bao), unary arithmetic operator (uao), binary boolean operator (bbo), unary boolean operator (ubo), relational operator (ro)

• arithmetic value (aV), boolean value (bV). variable (v), number (n) P → m : S P | m : S

S → v = E | E | goto m | if E goto m | nop

E → aE | bE

aE → aV bao aV | uao aV | aV

bE → bV bbo bV | ubo bV | aV ro aV | bV aV → v | n

bV → v | T | F

Jul 2010 IIT Bombay

(59)

CS 618

An Example Program int main()

{ int a, b, c, n;

a = 4;

b = 2;

c = 3;

n = c*2;

while (a <= n) {

a = a+1;

}

if (a < 12) a = a+b+c;

}

Jul 2010 IIT Bombay

(60)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 22/93

An Example Program int main()

{ int a, b, c, n;

a = 4;

b = 2;

c = 3;

n = c*2;

while (a <= n) {

a = a+1;

}

if (a < 12) a = a+b+c;

}

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: if (a > n)

goto 8 6: a = a + 1 7: goto 5 8: if (a ≥ 12)

goto 11 9: t1 = a+b 10: a = t1+c 11: nop

Jul 2010 IIT Bombay

(61)

CS 618

An Example Program int main()

{ int a, b, c, n;

a = 4;

b = 2;

c = 3;

n = c*2;

while (a <= n) {

a = a+1;

}

if (a < 12) a = a+b+c;

}

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: if (a > n)

goto 8 6: a = a + 1 7: goto 5 8: if (a ≥ 12)

goto 11 9: t1 = a+b 10: a = t1+c 11: nop

Jul 2010 IIT Bombay

(62)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 22/93

An Example Program int main()

{ int a, b, c, n;

a = 4;

b = 2;

c = 3;

n = c*2;

while (a <= n) {

a = a+1;

}

if (a < 12) a = a+b+c;

}

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: if (a > n)

goto 8 6: a = a + 1 7: goto 5 8: if (a ≥ 12)

goto 11 9: t1 = a+b 10: a = t1+c 11: nop

a = 4 b = 2 c = 3 n = c*2 if (a>n)

a = a + 1 if (a≥12)

t1 = a+b a = t1+c nop

F

F T

T

Jul 2010 IIT Bombay

(63)

CS 618

Labels and Program Points 1: a = 4

2: b = 2 3: c = 3 4: n = c*2

5: if (a > n) goto 8 6: a = a + 1

7: goto 5

8: if (a ≥ 12) goto 11 9: t1 = a+b

10: a = t1+c 11: nop

Jul 2010 IIT Bombay

(64)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 23/93

Labels and Program Points 1: a = 4

2: b = 2 3: c = 3 4: n = c*2

5: if (a > n) goto 8 6: a = a + 1

7: goto 5

8: if (a ≥ 12) goto 11 9: t1 = a+b

10: a = t1+c 11: nop

A label of a statement represents

• the program point just before the execution of the statement

• the program point just after the execution of the previous statement

• both the source and the target of the control transfer edge reaching the statement This is fine if there is no other control transfer to the same program point

Jul 2010 IIT Bombay

(65)

CS 618

Labels and Control Flow 1: a = 4

2: b = 2 3: c = 3 4: n = c*2 5: if (a > n)

goto 8 6: a = a + 1 7: goto 5 8: if (a ≥ 12)

goto 11 9: t1 = a+b 10: a = t1+c 11: nop

a = 4 b = 2 c = 3 n = c*2 if (a>n)

a = a + 1 if (a≥12)

t1 = a+b a = t1+c nop

F

F T

T

Jul 2010 IIT Bombay

(66)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 24/93

Labels and Control Flow 1: a = 4

2: b = 2 3: c = 3 4: n = c*2 5: if (a > n)

goto 8 6: a = a + 1 7: goto 5 8: if (a ≥ 12)

goto 11 9: t1 = a+b 10: a = t1+c 11: nop

a = 4 b = 2 c = 3 n = c*2 if (a>n)

a = a + 1 if (a≥12)

t1 = a+b a = t1+c nop

F

F T

T

• Value of variable a could be different at these two points

• Need to distinguish between them

• Blue edges represent implicit goto across block

• We need to explicate all such implicit gotos

Jul 2010 IIT Bombay

(67)

CS 618

Labels and Control Flow 1: a = 4

2: b = 2 3: c = 3 4: n = c*2 5: if (a > n)

goto 8 6: a = a + 1 7: goto 5 8: if (a ≥ 12)

goto 11 9: t1 = a+b 10: a = t1+c 11: nop

a = 4 b = 2 c = 3 n = c*2 if (a>n)

a = a + 1 if (a≥12)

t1 = a+b a = t1+c nop

F

F T

T

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6 6: if (a > n)

goto 9 7: a = a + 1 8: goto 6 9: if (a ≥ 12)

goto 13 10: t1 = a+b 11: a = t1+c 12: goto 13 13: nop

Jul 2010 IIT Bombay

(68)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 24/93

Labels and Control Flow 1: a = 4

2: b = 2 3: c = 3 4: n = c*2 5: if (a > n)

goto 8 6: a = a + 1 7: goto 5 8: if (a ≥ 12)

goto 11 9: t1 = a+b 10: a = t1+c 11: nop

a = 4 b = 2 c = 3 n = c*2 if (a>n)

a = a + 1 if (a≥12)

t1 = a+b a = t1+c nop

F

F T

T

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6 6: if (a > n)

goto 9 7: a = a + 1 8: goto 6 9: if (a ≥ 12)

goto 13 10: t1 = a+b 11: a = t1+c 12: goto 13 13: nop

Jul 2010 IIT Bombay

(69)

CS 618

Updating Control Flow

• We assume that all implicit gotos across basic blocks are explicited and labels adjusted appropriately

This is required only for the purpose of our reasoning about our analysis

Jul 2010 IIT Bombay

(70)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 26/93

Entities in Our Example Program 1: a = 4

2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

L abel = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}

V ar = {a, b, c , n, t1}

E xpr = {c ∗ 2, a > n, a + 1, a ≥ 12, a + b, t 1 + c}

Jul 2010 IIT Bombay

(71)

CS 618

The Semantics of Our Language

• σ ∈ S tore : V ar 7→ I ∪ B ∪ {⊥}

Jul 2010 IIT Bombay

(72)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 27/93

The Semantics of Our Language

• σ ∈ S tore : V ar 7→ I ∪ B ∪ {⊥}

(σ is V ar 7→ I ∪ B ∪ {⊥} and S tore is the set of σs)

Jul 2010 IIT Bombay

(73)

CS 618

The Semantics of Our Language

• σ ∈ S tore : V ar 7→ I ∪ B ∪ {⊥}

(σ is V ar 7→ I ∪ B ∪ {⊥} and S tore is the set of σs)

• (l , σ) ∈ S tate : L abel 7→ S tore

Jul 2010 IIT Bombay

(74)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 27/93

The Semantics of Our Language

• σ ∈ S tore : V ar 7→ I ∪ B ∪ {⊥}

(σ is V ar 7→ I ∪ B ∪ {⊥} and S tore is the set of σs)

• (l , σ) ∈ S tate : L abel 7→ S tore Q. Why not L abel × S tore?

Jul 2010 IIT Bombay

(75)

CS 618

The Semantics of Our Language

• σ ∈ S tore : V ar 7→ I ∪ B ∪ {⊥}

(σ is V ar 7→ I ∪ B ∪ {⊥} and S tore is the set of σs)

• (l , σ) ∈ S tate : L abel 7→ S tore Q. Why not L abel × S tore?

A. Only one store can be associated with a given label

Jul 2010 IIT Bombay

(76)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 27/93

The Semantics of Our Language

• σ ∈ S tore : V ar 7→ I ∪ B ∪ {⊥}

(σ is V ar 7→ I ∪ B ∪ {⊥} and S tore is the set of σs)

• (l , σ) ∈ S tate : L abel 7→ S tore Q. Why not L abel × S tore?

A. Only one store can be associated with a given label

• Execution of program causes state transitions

Jul 2010 IIT Bombay

(77)

CS 618

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 01,

Variable Value

a

b

c

n

t1

Jul 2010 IIT Bombay

(78)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 28/93

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 02,

Variable Value

a 4

b

c

n

t1

Jul 2010 IIT Bombay

(79)

CS 618

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 03,

Variable Value

a 4

b 2

c

n

t1

Jul 2010 IIT Bombay

(80)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 28/93

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 04,

Variable Value

a 4

b 2

c 3

n

t1

Jul 2010 IIT Bombay

(81)

CS 618

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 05,

Variable Value

a 4

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(82)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 28/93

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 06,

Variable Value

a 4

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(83)

CS 618

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 07,

Variable Value

a 4

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(84)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 28/93

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 08,

Variable Value

a 5

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(85)

CS 618

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 06,

Variable Value

a 5

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(86)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 28/93

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 07,

Variable Value

a 5

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(87)

CS 618

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 08,

Variable Value

a 6

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(88)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 28/93

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 06,

Variable Value

a 6

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(89)

CS 618

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 07,

Variable Value

a 6

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(90)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 28/93

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 08,

Variable Value

a 7

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(91)

CS 618

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 06,

Variable Value

a 7

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(92)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 28/93

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 09,

Variable Value

a 7

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(93)

CS 618

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 10,

Variable Value

a 7

b 2

c 3

n 6

t1

Jul 2010 IIT Bombay

(94)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 28/93

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 11,

Variable Value

a 7

b 2

c 3

n 6

t1 9

Jul 2010 IIT Bombay

(95)

CS 618

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 12,

Variable Value

a 12

b 2

c 3

n 6

t1 9

Jul 2010 IIT Bombay

(96)

CS 618 Bit Vector Frameworks: Program Execution Model and Semantics 28/93

Execution of Our Example Program

1: a = 4 2: b = 2 3: c = 3 4: n = c*2 5: goto 6

6: if (a > n) goto 9 7: a = a + 1

8: goto 6

9: if (a ≥ 12) goto 13 10: t1 = a+b

11: a = t1+c 12: goto 13 13: nop

State (l, σ) =

 13,

Variable Value

a 12

b 2

c 3

n 6

t1 9

Jul 2010 IIT Bombay

References

Related documents

July 09 GDFA: Common Abstractions in Data Flow Analysis 15/37. Common Form of Data

Forward Reaching Definitions Available Expressions Backward Live Variables Anticipable Exressions.. Bidirectional Partial

CS 618 DFA Theory: Solutions of Data Flow Analysis 58/106.

◮ Due to “every control flow path nature”, computation of anticipable and available access paths uses ∩ as the confluence..

• Data flow analysis uses static representation of programs to compute summary information along paths.. CS 618 Interprocedural DFA: Issues in Interprocedural

• in defining other strongly live variables in an assignment statement (this case is different from simple liveness analysis)... Using Data Flow Information of Live

1 July 2012 Introduction to DFA: Live Variables Analysis 6/34.. Local Data Flow Properties for Live

Characterises safety of hoisting Hoist an expression to the entry of a block only if it can. be hoisted out of the block into all