optimizing compilers cisc 673 spring 2009 data flow analysis

37
UNIVERSITY NIVERSITY OF OF D DELAWARE ELAWARE C COMPUTER & OMPUTER & INFORMATION NFORMATION SCIENCES CIENCES DEPARTMENT EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Data flow analysis John Cavazos University of Delaware

Upload: dennis-delacruz

Post on 03-Jan-2016

42 views

Category:

Documents


2 download

DESCRIPTION

Optimizing Compilers CISC 673 Spring 2009 Data flow analysis. John Cavazos University of Delaware. Data flow analysis. Solving set of equations posed over graph representation (e.g., CFG) Based on any or all paths through program “Any path” or “All path” problems. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT

Optimizing CompilersCISC 673

Spring 2009Data flow analysis

John CavazosUniversity of Delaware

Page 2: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 2

Data flow analysis

Solving set of equations posed over graph representation (e.g., CFG)

Based on any or all paths through program “Any path” or “All path” problems

Page 3: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 3

Includes Infeasible Pathsa = 1;if (a == 0) { a = 1;}if (a == 0) { a = 2;}

Infeasible paths never actually taken by program, regardless of input

Undecidable to distinguish from feasible

Page 4: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 4

Data Flow-Based Optimizations

Dead variable elimination a = 3; print a; x = 12; halt ) a = 3; print

a; halt Copy propagation

x = y; … use of x ) …use of y Partial redundancy

a = 3*c + d; b = 3*c ) b = 3*c; a=b+d Constant propagation

a = 3; b = 2; c = a+b ) a = 3; b = 2; c = 5

Page 5: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 5

Example: Redundancy Elimination

• Expression e at point p redundant iff every path from procedure’s entry to p contains evaluation of e and value(s) of e’s operands do not change between those earlier evaluations and p

Evaluating e at p always produces the same value as those earlier evaluations

Page 6: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 6

Example

m a + bn a + b

A

p c + dr c + d

B q a + br c + d

C

Page 7: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 7

Redundancy Elimination

If the compiler can prove expression redundant

• Replace redundant evaluation with reference

Problem • Proving x+y is redundant• Eliminate redundant evaluation

Can use value numbering

Page 8: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 8

Value Numbering

Key notion

• Assign a number, V(n), to each expression V(x+y) = V(j)

iff x+y and j have same value inputs Hash value numbers to make efficient

• Use numbers to improve the code

Page 9: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 9

Local Value Numbering

The algorithm

For each expression e in the block

1 Get value numbers for operands o1 and o2 from hash lookup

2 Hash <operator,VN(o1),VN(o2)> to get value number for e

3 If e already had a value number, replace e with a reference

4 If o1 & o2 are constant, evaluate it & use a “load immediate”

Local one block at a time

Page 10: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 10

Local Value Numbering Example

With VNs

a03 x0

1 + y02

b03 x0

1 + y02

a14 17

c03 x0

1 + y02

Original Code

a0 x0 + y0

b0 x0 + y0

a1 17

c0 x0 + y0

1. Renaming:

• Give each value a unique name

• While complex, the meaning is clear

Rewritten

a03 x0

1 + y02

b03 a0

3

a14 17

c03 a0

3

2. Result:

• a03 is available

• rewriting works

Page 11: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 11

Global Redundancy Elimination

u e + fD u e + fE

x e + fFe+f is

redundant

Find common subexpressions whose range spans basic blocks, and eliminate unnecessary re-evaluations

Page 12: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 12

Expression “available” is as follows:

• Expression defined at point p if value computed at p

• Expression killed at point p if one or more operands defined at point p

• e available at p if every path leading to p contains a prior definition of e and e is not killed between definition and p

Page 13: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 13

“Available” Expressions for GCSE

Mechanism• System of simultaneous equations over the CFG• Solve the equations to produce a set for each CFG

node• Contains names of every expression available

on entry• Use these sets, AVAIL(n), for redundancy

elimination

Safety• x+y AVAIL(n) proves earlier value x+y is same• Transformation provides name for each value• Several schemes for this mapping

Page 14: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 14

“Available” Expressions for GCSE

Profitability• Don’t add any evaluations• Add some copy operations

•Copies are inexpensive

•Many copies coalesce away

Page 15: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 15

Computing Available Expressions

For each block b

• Let AVAIL(b) be the set of expressions available on entry to b

AVAIL constructed from local sets

• Let EXPRKILL(b) be the set of expression killed in b

• Let DEEXPR(b) be the set of downward exposed expressions x DEEXPR(b) x defined in b & not subsequently

killed in b

Page 16: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 16

Computing Available Expressions

Now, AVAIL(b) can be defined as:

AVAIL(b) = ppred(b) (DEEXPR(p) (AVAIL(p) EXPRKILL(p) ))

AVAIL(n0) = Ø

where preds(b) is the set of b’s predecessors in the CFG

This system of simultaneous equations forms a data-flow problem Solve it with a data-flow algorithm

Entry node in CFG is n0

Page 17: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 17

Computing Available Expressions

………

DEEXPR (p)

And not later killed

Downward exposed

expressions

Basic Block p

AVAIL(b) = ppred(b) (DEEXPR(p) (AVAIL(p) EXPRKILL(p) ))

Page 18: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 18

Computing Available Expressions

Expressions that Pass through

unscathed

………

AVAIL(p) EXPRKILL(p)

AVAIL(p)

EXPRKILL(p)

Available upon entry

Any expresssions killed

Basic Block p

AVAIL(b) = ppred(b) (DEEXPR(p) (AVAIL(p) EXPRKILL(p) ))

Page 19: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 19

Available Expressions for GCSE

The Big Picture

1. block b, compute AVAIL(b)2. Assign unique global names to expressions in AVAIL(b)3. block b, value number b starting with AVAIL(b)

Page 20: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 20

Compute Local Sets for each Block b

assume a block b with operations o1, o2, …, ok

VARKILL ØDEEXPR(b) Ø

for i = k to 1 // from last to first instsassume oi is “x y + z”add x to VARKILL if (y VARKILL) and (z VARKILL) then

add “y + z” to DEEXPR(b)

EXPRKILL(b) Ø

For each expression e in procedurefor each variable v e

if v VARKILL(b) then EXPRKILL(b) EXPRKILL(b) {e }

} Compute DEExpr

}Compute ExprKill

Page 21: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 21

Example

v a + ba c + dx e + f

DEExpr = {c+d,e+f }

VarKill = {y,a,x}

ExprKill = {a+b}

Page 22: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 22

Compute Available Expressions

for all blocks b

compute DEExpr(b) and EXPRKILL(b)

Changed=truewhile (Changed)

Changed=false for all blocks b

OldValue = AVAIL(b)

AVAIL(b) = ppred(b) (DEEXPR(p) (AVAIL(p) EXPRKILL(p) ))

if AVAIL(b ) != OldValue Changed=true

Page 23: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 23

Example: Compute DEEXPRm a + bn a + b

A

p c + dr c + d

B

y a + bz c + dG

q a + br c + d

C

e b + 18s a + bu e + f

D

e a + 17t c + du e + f

E

v a + bw c + dx e + f

F

assume a block b with operations o1, o2, …, ok

VARKILL ØDEEXPR(b) Ø

for i = k to 1 // from last to first instsassume oi is “x y + z”add x to VARKILL if (y VARKILL) and (z VARKILL)

thenadd “y + z” to DEEXPR(b)

=

Page 24: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 24

Example: Compute EXPRKILLm a + bn a + b

A

p c + dr c + d

B

y a + bz c + dG

q a + br c + d

C

e b + 18s a + bu e + f

D

e a + 17t c + du e + f

E

v a + bw c + dx e + f

F

EXPRKILL(b) Ø

For each expression e in procedurefor each variable v e

if v VARKILL(b) then EXPRKILL(b) EXPRKILL(b)

{e }

Page 25: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 25

ExampleAVAIL(A) = ØAVAIL(B) = {a+b} (Ø all)

= {a+b}AVAIL(C) = {a+b}AVAIL(D) = {a+b,c+d} ({a+b} all)

= {a+b,c+d} AVAIL(E) = {a+b,c+d}AVAIL(F) = [{b+18,a+b,e+f}

({a+b,c+d} {all - e+f})]

[{a+17,c+d,e+f} ({a+b,c+d} {all -

e+f})]= {a+b,c+d,e+f}

AVAIL(G)= [ {c+d} ({a+b} all)]

[{a+b,c+d,e+f} ({a+b,c+d,e+f}

all)]= {a+b,c+d}

m a + bn a + b

A

p c + dr c + d

B

y a + bz c + d

G

q a + br c + d

C

e b + 18s a + bu e + f

D e a + 17t c + du e + f

E

v a + bw c + dx e + f

F

A B C D E F GDEEXPR a+b c+d a+b,c+db+18,a+b,e+fa+17,c+d,e+fa+b,c+d,e+fa+b,c+dEXPRK ILL { } { } { } e+f e+f { } { }

ppred(b) (DEEXPR(p) (AVAIL(p) EXPRKILL(p) ))

Page 26: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 26

Example

m a + bn a + b

A

p c + dr c + d

B

y a + bz c + d

G

q a + br c + d

C

e b + 18s a + bu e + f

D e a + 17t c + du e + f

E

v a + bw c + dx e + f

F

AVAIL sets in blue

{ a+b }

{ a+b,c+d }

{ a+b,c+d }

{ a+b,c+d,e+f }

{ a+b,c+d }

{ a+b }

Page 27: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 27

Remember Big PictureThe Big Picture

1. block b, compute AVAIL(b)

2. Assign unique global names to expressions in AVAIL(b)

3. block b, value number b starting with AVAIL(b)

We’ve done step 1.

Page 28: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 28

Global CSE (replacement step)

Compute a static mapping from expression to name

• After analysis & before transformation b, e AVAIL(b), assign e a global

name by hashing on e

Page 29: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 29

Example

m a + bn a + b

A

p c + dr c + d

B

y a + bz c + d

G

q a + br c + d

C

e b + 18s a + bu e + f

D e a + 17t c + du e + f

E

v a + bw c + dx e + f

F

{ a+b }

{ a+b,c+d }

{ a+b,c+d }

{ a+b,c+d,e+f }

{ a+b,c+d }

{ a+b }

Assigning unique names to global

CSEsa+b t1

c+d t2

e+f t3

Page 30: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 30

Remember the Big Picture

The Big Picture

1. block b, compute AVAIL(b)

2. Assign unique global names to expressions in AVAIL(b)

3. block b, value number b starting with AVAIL(b)

We’ve done steps 1 & 2.

Page 31: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 31

Value Numbering

To perform replacement, value numbering each block b

• Initialize hash table with AVAIL(b)

• Replace an expression in AVAIL(b) means copy from its name

• At each evaluation of a global name, copy new value to its name

• Otherwise, value number as in last lecture

Page 32: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 32

Net Result

• Catches local redundancies with value numbering

• Catches nonlocal redundancies because of AVAIL sets

• Not quite same effect, but close Local redundancies found by value Global redundancies found by spelling

Page 33: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 33

Examplem a + b t1 m n t1

A

p c + d t2 p r t2

B

y t1

z t2

G

q t1 r c + d t2 r

C

e b + 18 s t1

u e + f t3 u

D e a + 17 t t2

u e + f t3 u

E

v t1

w t2

x t3

F

After replacement &

local value numbering

Page 34: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 34

Examplem a + b t1 m n t1

A

p c + d t2 p r t2

B

y t1

z t2

G

q t1 r c + d t2 r

C

e b + 18 s t1

u e + f t3 u

D e a + 17 t t2

u e + f t3 u

E

v t1

w t2

x t3

F

In practice, most of these copies will be folded into subsequent uses…

u

p

r

r

m

m

m

m r

m

Page 35: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 35

Some Copies Serve a PurposeIn the example, all the copies coalesce away.

Sometimes, the copies are needed.

• Copies into t1 create a common name along two paths

• Makes the replacement possible

w a + b x a + b

y a + b

w a + bt1 w

x a + bt1 x

y t1

Cannot write

“w or x”

Page 36: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 36

Examplem a + bn a + b

A

p c + dr c + d

B

y a + bz c + d

G

q a + br c + d

C

e b + 18s a + bu e + f

D e a + 17t c + du e + f

E

v a + bw c + dx e + f

F

LVN

LVN

GRE

GRE

GRE

GRE

GRE

GRE

GRE

GRE

Page 37: Optimizing Compilers CISC 673 Spring 2009 Data flow analysis

UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 37

Next Time

More Data Flow