+ All Categories
Home > Documents > Data Flow Analysis (contd…)

Data Flow Analysis (contd…)

Date post: 26-Oct-2021
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
97
Program Analysis https://www.cse.iitb.ac.in/~karkare/cs618/ Amey Karkare Dept of Computer Science and Engg IIT Kanpur Visiting IIT Bombay [email protected] [email protected] Data Flow Analysis (contd…)
Transcript
Page 1: Data Flow Analysis (contd…)

Program Analysis https://www.cse.iitb.ac.in/~karkare/cs618/

Amey Karkare Dept of Computer Science and Engg

IIT Kanpur Visiting IIT Bombay

[email protected] [email protected]

Data Flow Analysis (contd…)

Page 2: Data Flow Analysis (contd…)

Available Expressions

2

Page 3: Data Flow Analysis (contd…)

Available Expressions

• Expression e is available at a point p:

2

Page 4: Data Flow Analysis (contd…)

Available Expressions

• Expression e is available at a point p: – Every path from entry to p has at least one

evaluation of e

2

Page 5: Data Flow Analysis (contd…)

Available Expressions

• Expression e is available at a point p: – Every path from entry to p has at least one

evaluation of e – There is no assignment to any component

variable of e after last evaluation of e prior to p

2

Page 6: Data Flow Analysis (contd…)

Available Expressions

• Expression e is available at a point p: – Every path from entry to p has at least one

evaluation of e – There is no assignment to any component

variable of e after last evaluation of e prior to p

• Expression e is generated by its evaluation

2

Page 7: Data Flow Analysis (contd…)

Available Expressions

• Expression e is available at a point p: – Every path from entry to p has at least one

evaluation of e – There is no assignment to any component

variable of e after last evaluation of e prior to p

• Expression e is generated by its evaluation • Expression e is killed by assignment to its

component variables 2

Page 8: Data Flow Analysis (contd…)

Available Expr Analysis

3

d: x = y+z s1

Page 9: Data Flow Analysis (contd…)

Available Expr Analysis

gen(s1) = {y+z}

3

d: x = y+z s1

Page 10: Data Flow Analysis (contd…)

Available Expr Analysis

gen(s1) = {y+z}

kill(s1) = Ex // Ex : set of all expressions having x as a component

3

d: x = y+z s1

Page 11: Data Flow Analysis (contd…)

Available Expr Analysis

gen(s1) = {y+z}

kill(s1) = Ex // Ex : set of all expressions having x as a component

out(s1) = in(s1) - kill(s1) U gen(s1)

3

d: x = y+z s1

Page 12: Data Flow Analysis (contd…)

Available Expr Analysis

gen(s1) = {y+z}

kill(s1) = Ex // Ex : set of all expressions having x as a component

out(s1) = in(s1) - kill(s1) U gen(s1) THIS MAY NOT WORK IN GENERAL! WHY?

3

d: x = y+z s1

Page 13: Data Flow Analysis (contd…)

Available Expr Analysis

4

d: x = x+z s1

Page 14: Data Flow Analysis (contd…)

Available Expr Analysis

INCORRECT FORMULATION

4

d: x = x+z s1

Page 15: Data Flow Analysis (contd…)

Available Expr Analysis

INCORRECT FORMULATION out(s1) = in(s1) - kill(s1) U gen(s1)

4

d: x = x+z s1

Page 16: Data Flow Analysis (contd…)

Available Expr Analysis

INCORRECT FORMULATION out(s1) = in(s1) - kill(s1) U gen(s1) gen(s1) = {x+z}

4

d: x = x+z s1

Page 17: Data Flow Analysis (contd…)

Available Expr Analysis

INCORRECT FORMULATION out(s1) = in(s1) - kill(s1) U gen(s1) gen(s1) = {x+z}

kill(s1) = Ex // Ex : set of all expressions having x as a component

4

d: x = x+z s1

Page 18: Data Flow Analysis (contd…)

Available Expr Analysis

INCORRECT FORMULATION out(s1) = in(s1) - kill(s1) U gen(s1) gen(s1) = {x+z} kill(s1) = Ex // Ex : set of all expressions having x as

a component

5

d: x = x+z s1

Page 19: Data Flow Analysis (contd…)

Available Expr Analysis

6

lhs = rhs s1

Page 20: Data Flow Analysis (contd…)

Available Expr Analysis

CORRECT FORMULATION

6

lhs = rhs s1

Page 21: Data Flow Analysis (contd…)

Available Expr Analysis

CORRECT FORMULATION out(s1) = in(s1) - kill(s1) U gen(s1)

6

lhs = rhs s1

Page 22: Data Flow Analysis (contd…)

Available Expr Analysis

CORRECT FORMULATION out(s1) = in(s1) - kill(s1) U gen(s1) gen(s1) = { rhs | lhs is not part of rhs }

6

lhs = rhs s1

Page 23: Data Flow Analysis (contd…)

Available Expr Analysis

CORRECT FORMULATION out(s1) = in(s1) - kill(s1) U gen(s1) gen(s1) = { rhs | lhs is not part of rhs }

kill(s1) = Elhs // Elhs : set of all expressions having lhs as a component

6

lhs = rhs s1

Page 24: Data Flow Analysis (contd…)

Available Expr Analysis

7

S S1

S2

Page 25: Data Flow Analysis (contd…)

Available Expr Analysis

gen(s) = gen(s2) U (gen(s1) – kill(s2))

7

S S1

S2

Page 26: Data Flow Analysis (contd…)

Available Expr Analysis

gen(s) = gen(s2) U (gen(s1) – kill(s2)) kill(s) = kill(s2) U (kill(s1) – gen(s2))

7

S S1

S2

Page 27: Data Flow Analysis (contd…)

Available Expr Analysis

gen(s) = gen(s2) U (gen(s1) – kill(s2)) kill(s) = kill(s2) U (kill(s1) – gen(s2))

in(s1) = in(s)

7

S S1

S2

Page 28: Data Flow Analysis (contd…)

Available Expr Analysis

gen(s) = gen(s2) U (gen(s1) – kill(s2)) kill(s) = kill(s2) U (kill(s1) – gen(s2))

in(s1) = in(s) in(s2) = out(s1)

7

S S1

S2

Page 29: Data Flow Analysis (contd…)

Available Expr Analysis

gen(s) = gen(s2) U (gen(s1) – kill(s2)) kill(s) = kill(s2) U (kill(s1) – gen(s2))

in(s1) = in(s) in(s2) = out(s1) out(s) = out(s2)

7

S S1

S2

Page 30: Data Flow Analysis (contd…)

S

Analysis of Structured Programs

8

S1 S2

Page 31: Data Flow Analysis (contd…)

S

Analysis of Structured Programs

gen(s) = gen(s1) ∩ gen(s2)

8

S1 S2

Page 32: Data Flow Analysis (contd…)

S

Analysis of Structured Programs

gen(s) = gen(s1) ∩ gen(s2) kill(s) = kill(s1) U kill(s2)

8

S1 S2

Page 33: Data Flow Analysis (contd…)

S

Analysis of Structured Programs

gen(s) = gen(s1) ∩ gen(s2) kill(s) = kill(s1) U kill(s2)

in(s1) = in(s2) = in(s)

8

S1 S2

Page 34: Data Flow Analysis (contd…)

S

Analysis of Structured Programs

gen(s) = gen(s1) ∩ gen(s2) kill(s) = kill(s1) U kill(s2)

in(s1) = in(s2) = in(s) out(s) = out(s1) ∩ out(s2)

8

S1 S2

Page 35: Data Flow Analysis (contd…)

S

Available Expr Analysis

9

S1

Page 36: Data Flow Analysis (contd…)

S

Available Expr Analysis

gen(s) = gen(s1)

9

S1

Page 37: Data Flow Analysis (contd…)

S

Available Expr Analysis

gen(s) = gen(s1) kill(s) = kill(s1)

9

S1

Page 38: Data Flow Analysis (contd…)

S

Available Expr Analysis

gen(s) = gen(s1) kill(s) = kill(s1)

in(s1) = in(s) ∩ gen(s1)

9

S1

Page 39: Data Flow Analysis (contd…)

S

Available Expr Analysis

gen(s) = gen(s1) kill(s) = kill(s1)

in(s1) = in(s) ∩ gen(s1)

9

S1

z = x*y; do {} while(…); // is x*y available // here?

Page 40: Data Flow Analysis (contd…)

S

Available Expr Analysis

gen(s) = gen(s1) kill(s) = kill(s1)

in(s1) = in(s) ∩ gen(s1)

9

S1

gen out(s1)

z = x*y; do {} while(…); // is x*y available // here?

Page 41: Data Flow Analysis (contd…)

S

Available Expr Analysis

gen(s) = gen(s1) kill(s) = kill(s1)

in(s1) = in(s) ∩ gen(s1) out(s) = out(s1)

9

S1

gen out(s1)

z = x*y; do {} while(…); // is x*y available // here?

Page 42: Data Flow Analysis (contd…)

S

Again: Conservative Analysis

10

S1 S2

Page 43: Data Flow Analysis (contd…)

S

Again: Conservative Analysis

• Assumption: All paths are feasible.

10

S1 S2

Page 44: Data Flow Analysis (contd…)

S

Again: Conservative Analysis

• Assumption: All paths are feasible. – Consider: if (true) s1; else s2 – s2 is never executed

10

S1 S2

Page 45: Data Flow Analysis (contd…)

S

Again: Conservative Analysis

• Assumption: All paths are feasible. – Consider: if (true) s1; else s2 – s2 is never executed gen(s) = gen(s1) ⊇ gen(s1) ∩ gen(s2) kill(s) = kill(s1) ⊆ kill(s1) ∪ kill(s2)

10

S1 S2

Page 46: Data Flow Analysis (contd…)

S

Again: Conservative Analysis

• Thus: true gen (s) ⊇ analysis gen(s) true kill (s) ⊆ analysis kill(s)

• True is what is computed at run time • This is SAFE estimate

– prevents optimization – but no wrong optimization 11

S1 S2

Page 47: Data Flow Analysis (contd…)

Available Expressions

12

Page 48: Data Flow Analysis (contd…)

Available Expressions

• Expr e is available at the start of a block

12

Page 49: Data Flow Analysis (contd…)

Available Expressions

• Expr e is available at the start of a block –It is available at the end of all

predecessors

12

Page 50: Data Flow Analysis (contd…)

Available Expressions

• Expr e is available at the start of a block –It is available at the end of all

predecessors

in(B) = ∩ P is pred of B out(P)

12

Page 51: Data Flow Analysis (contd…)

Available Expressions

• Expr e is available at the start of a block –It is available at the end of all

predecessors

in(B) = ∩ P is pred of B out(P) • Expr e is available at the end of a block

– either it is generated by the block – or it is available at the start of the block and

not killed by the block

out(B) = in(B) –kill(B) ∪ gen(B) 12

Page 52: Data Flow Analysis (contd…)

Available Expressions

• Kill & gen known for each block.

13

Page 53: Data Flow Analysis (contd…)

Available Expressions

• Kill & gen known for each block. • A program with N blocks has 2N

equations with 2N unknowns –solution is possible. – iterative approach (on next slide)

13

Page 54: Data Flow Analysis (contd…)

14

Page 55: Data Flow Analysis (contd…)

for each block B { out(B) = U; // U = “universal” set of all exprs }

14

Page 56: Data Flow Analysis (contd…)

for each block B { out(B) = U; // U = “universal” set of all exprs } out(Entry) = ϕ; // remember reaching defs?

14

Page 57: Data Flow Analysis (contd…)

for each block B { out(B) = U; // U = “universal” set of all exprs } out(Entry) = ϕ; // remember reaching defs? change = true; while (change) { }

14

Page 58: Data Flow Analysis (contd…)

for each block B { out(B) = U; // U = “universal” set of all exprs } out(Entry) = ϕ; // remember reaching defs? change = true; while (change) { change = false; }

14

Page 59: Data Flow Analysis (contd…)

for each block B { out(B) = U; // U = “universal” set of all exprs } out(Entry) = ϕ; // remember reaching defs? change = true; while (change) { change = false; for each block B other than Entry { } }

14

Page 60: Data Flow Analysis (contd…)

for each block B { out(B) = U; // U = “universal” set of all exprs } out(Entry) = ϕ; // remember reaching defs? change = true; while (change) { change = false; for each block B other than Entry { in(B) = ∩P is pred of B out(P); oldOut = out(B); out(B) = in(B) - kill(B) ∪ gen(B); } }

14

Page 61: Data Flow Analysis (contd…)

for each block B { out(B) = U; // U = “universal” set of all exprs } out(Entry) = ϕ; // remember reaching defs? change = true; while (change) { change = false; for each block B other than Entry { in(B) = ∩P is pred of B out(P); oldOut = out(B); out(B) = in(B) - kill(B) ∪ gen(B); if (oldOut != out(B)) then { change = true; } } }

14

Page 62: Data Flow Analysis (contd…)

Some Issues

• What is the set of all expressions? • How to compute it efficiently? • Why Entry block is initialized differently?

15

Page 63: Data Flow Analysis (contd…)

Available Expressions

16

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

B1

B2

B3

B4

U = {a*b, c+d} We are not interested in other expressions/variables

Page 64: Data Flow Analysis (contd…)

Available Expressions

16

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

B1

B2

B3

B4

BLOCK GEN KILL

B1 {a*b, c+d} {}

B2 {c+d} {a*b}

B3 {a*b} {}

B4 {a*b} {c+d}

U = {a*b, c+d} We are not interested in other expressions/variables

Page 65: Data Flow Analysis (contd…)

Available Expressions

17

B1

B2

B3

B4

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

Page 66: Data Flow Analysis (contd…)

B1 B2 B3 B4

init In

Out

pass1 In

Out

pass2 In

Out

pass3 In

Out

Available Expressions

17

B1

B2

B3

B4

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

Page 67: Data Flow Analysis (contd…)

B1 B2 B3 B4

init In - - - -

Out U U U U

pass1 In

Out

pass2 In

Out

pass3 In

Out

Available Expressions

18

B1

B2

B3

B4

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

Page 68: Data Flow Analysis (contd…)

B1 B2 B3 B4

init In - - - -

Out U U U U

pass1 In ϕ

Out {a*b,c+d}

pass2 In

Out

pass3 In

Out

Available Expressions

19

B1

B2

B3

B4

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

Page 69: Data Flow Analysis (contd…)

B1 B2 B3 B4

init In - - - -

Out U U U U

pass1 In ϕ

{a*b,c+d}

Out {a*b,c+d} {c+d}

pass2 In

Out

pass3 In

Out

Available Expressions

20

B1

B2

B3

B4

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

Page 70: Data Flow Analysis (contd…)

B1 B2 B3 B4

init In - - - -

Out U U U U

pass1 In ϕ

{a*b,c+d}

{c+d}

Out {a*b,c+d} {c+d}

{a*b, c+d}

pass2 In

Out

pass3 In

Out

Available Expressions

21

B1

B2

B3

B4

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

Page 71: Data Flow Analysis (contd…)

B1 B2 B3 B4

init In - - - -

Out U U U U

pass1 In ϕ

{a*b,c+d}

{c+d} {c+d}

Out {a*b,c+d} {c+d}

{a*b, c+d}

{a*b}

pass2 In

Out

pass3 In

Out

Available Expressions

22

B1

B2

B3

B4

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

Page 72: Data Flow Analysis (contd…)

B1 B2 B3 B4

init In - - - -

Out U U U U

pass1 In ϕ

{a*b,c+d}

{c+d} {c+d}

Out {a*b,c+d} {c+d}

{a*b, c+d}

{a*b}

pass2 In ϕ {a*b} {c+d} {c+d}

Out {a*b,c+d} {c+d} {a*b, c+d}

{a*b}

pass3 In

Out

Available Expressions

23

B1

B2

B3

B4

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

Page 73: Data Flow Analysis (contd…)

B1 B2 B3 B4

init In - - - -

Out U U U U

pass1 In ϕ

{a*b,c+d}

{c+d} {c+d}

Out {a*b,c+d} {c+d}

{a*b, c+d}

{a*b}

pass2 In ϕ {a*b} {c+d} {c+d}

Out {a*b,c+d} {c+d} {a*b, c+d}

{a*b}

pass3 In ϕ {a*b} {c+d} {c+d}

Out {a*b,c+d} {c+d} {a*b, c+d}

{a*b}

Available Expressions

24

B1

B2

B3

B4

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

Page 74: Data Flow Analysis (contd…)

B1 B2 B3 B4

init In - - - -

Out 11 11 11 11

pass1 In 00

11 01 01

Out 11 01 11 10

pass2 In 00 10 01 01

Out 11 01 11 10

pass3 In 00 10 01 01

Out 11 01 11 10

Available Expressions: Bitvectors

25

a*b c+d bits for each expression

B1

B2

B3

B4

ENTRY

…= a*b …= c+d

… = c+d a = a*b

… = a*b

c = a*b

EXIT

Page 75: Data Flow Analysis (contd…)

Available Expressions: Bitvectors

in(B) = ∩ P is pred of B out(P) out(B) = in(B) –kill(B) ∪ gen(B)

• With bit vectors, in(B) = ∧ P is pred of B out(P)

out(B) = (in(B) ∧ ¬kill(B)) ∨ gen(B) • Bitwise ∧, ∨, ¬ operations.

Page 76: Data Flow Analysis (contd…)

Available Expressions: Application

27

Page 77: Data Flow Analysis (contd…)

Available Expressions: Application

• Common subexpression elimination in a block B – Expression e available at the entry of B – e is also computed at a point p in B – Components of e are not modified from entry

of B to p

27

Page 78: Data Flow Analysis (contd…)

Available Expressions: Application

• Common subexpression elimination in a block B – Expression e available at the entry of B – e is also computed at a point p in B – Components of e are not modified from entry

of B to p

• e is “upward exposed” in B

27

Page 79: Data Flow Analysis (contd…)

Available Expressions: Application

• Common subexpression elimination in a block B – Expression e available at the entry of B – e is also computed at a point p in B – Components of e are not modified from entry

of B to p

• e is “upward exposed” in B • Expressions generated in B are “downward

exposed”

27

Page 80: Data Flow Analysis (contd…)

Recap: Summary of Reaching Definitions

gen = { dx | dx in B defines variable x and is not followed by another definition of x in B} kill = { dx | block contains some definition of x } in(B) = ∪P is pred of B out(P) out(B) = in(B) –kill(B) ∪ gen(B) meet ( Λ ) operator is ∪ Initialization: out(Bentry) = Entry Info = ϕ out(B) = ϕ

28

Page 81: Data Flow Analysis (contd…)

Summary of Available Expressions

gen = downward exposed expressions kill = { ex | block contains some definition of x } in(B) = ∩ P is pred of B out(P) out(B) = in(B) –kill(B) ∪ gen(B) meet ( Λ ) operator is ∩ Initialization: out(Bentry) = Entry Info = ϕ out(B) = U

29

Page 82: Data Flow Analysis (contd…)

Comparing Reaching Definition and Available Expressions Analysis

• Class Discussion about –Similarities –Differences

30

Page 83: Data Flow Analysis (contd…)

Summary of Available Expressions

31

Page 84: Data Flow Analysis (contd…)

Summary of Available Expressions

• What if we Initialize: out(Bentry) = Entry Info = ϕ out(B) = ϕ

31

Page 85: Data Flow Analysis (contd…)

Summary of Available Expressions

• What if we Initialize: out(Bentry) = Entry Info = ϕ out(B) = ϕ

• We might miss some expressions that are available

31

Page 86: Data Flow Analysis (contd…)

Summary of Available Expressions

• What if we Initialize: out(Bentry) = Entry Info = ϕ out(B) = ϕ

• We might miss some expressions that are available

• Loose on opportunity to optimize!

31

Page 87: Data Flow Analysis (contd…)

32

What are the expressions available at B2 when out(B) initialized with i) U ii) ϕ

ENTRY

…= a*b …= c+d

…= a*b …= c+d

B1

B2

Page 88: Data Flow Analysis (contd…)

Live Variable Analysis

• A variable x is live at a point p if –There is a point p’ along some path in

the flow graph starting at p to the EXIT –Value of x could be used at p’ –There is no definition of x between p

and p’ along this path • Otherwise x is dead at p

33

Page 89: Data Flow Analysis (contd…)

Live Variable Analysis: Gen

• gen(B) • Set of variables whose values may

be used in B prior to any definition • Also called “use(B)” • “upward exposed” use of a

variable is generated by B

Page 90: Data Flow Analysis (contd…)

Live Variable Analysis: Kill

• kill(B) • Set of variables defined in B prior

to any use • Also called “def(B)” • “upward exposed” definition of a

variable kills its liveness in B

Page 91: Data Flow Analysis (contd…)

Live Variable Analysis

out(B) = ∪ S is succ of B in(S) in(B) = out(B) –kill(B) ∪ gen(B)

Alt: in(B) = out(B) – def(B) ∪ use(B) • With bit vectors,

out(B) = ∨ S is succ of B in(S) in(B) = (out(B) ∧ ¬kill(B)) ∨ gen(B)

• Bitwise ∧, ∨, ¬ operations.

Page 92: Data Flow Analysis (contd…)

Very Busy Expressions

37

Page 93: Data Flow Analysis (contd…)

Very Busy Expressions

• Expression e is very busy at a point p

37

Page 94: Data Flow Analysis (contd…)

Very Busy Expressions

• Expression e is very busy at a point p –Every path from p to exit has at least

one evaluation of e

37

Page 95: Data Flow Analysis (contd…)

Very Busy Expressions

• Expression e is very busy at a point p –Every path from p to exit has at least

one evaluation of e –There is no assignment to any

component variable of e before first evaluation of e following p

37

Page 96: Data Flow Analysis (contd…)

Very Busy Expressions

• Expression e is very busy at a point p –Every path from p to exit has at least

one evaluation of e –There is no assignment to any

component variable of e before first evaluation of e following p

• Also called Anticipable expression

37

Page 97: Data Flow Analysis (contd…)

Very Busy Expression

• Practice Assignment –Set the data flow equations for

Very Busy Expression –Hint: Available Expression

Analysis


Recommended