Docsity

Scalar Optimization: Dataflow Analysis and Live Variables, Slides of Computer Science

Partial preview of the text

Download Scalar Optimization: Dataflow Analysis and Live Variables and more Slides Computer Science in PDF only on Docsity!

Scalar Optimisation Part 2

Michael O’Boyle

January 2013

M. O’Boyle

Scalar Optimisation

1

Course Structure

•^

L1 Introduction and Recap

-^

4-5 lectures on classical optimisation–

2 lectures on scalar optimisation

Last lecture on redundant expressions

Today look at dataflow framework and SSA

-^

4-5 lectures on high level approaches

-^

4-5 lectures on adaptive compilation M. O’Boyle

Scalar Optimisation

3

AVAIL() set calculation

m = a+bn = a+b

p = c + dr = c + d

q = a+br = c+d

e = b + 18s = a + bu = e + f

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

v = a + bw = c + dx = e + f

A y = a + bz = c + d

B^

C

D^

E

F

G

L

L

S

S

S

DD

DG

G

M. O’Boyle

Scalar Optimisation

4

Node

A

B

C

D

E

F

G

pred

-^

A

A

C

C

D,E

B,F

DEExpr

a+b

c+d

a+b

b+

a+

a+b

a+b

c+d

a+b

c+d

c+d

c+d

e+f

e+f

e+f

Kill

e+f

e+f

Calculate Avail(b) for each Basic Block b starting at block A

AV AIL

(A

AV AIL

(B

DEExpr

(A

)^

AV AIL

(A

)^

N OT KILLED

(A

a

b

U

a

b

AV AIL

(G

DEExpr

(B

)^

AV AIL

(B

)^

N OT KILLED

(B

(DEExpr

(F

AV AIL

(F

N OT KILLED

(F

M. O’Boyle

Scalar Optimisation

6

Another example: Dataflow analysis for live variables

•^

A variable

v

is live at a point

p

if there is a path from

p

to a use of

v

along

which

v

is not redefined.

•^

Useful

to

eliminate

stores

of

variables

no

longer

needed

-^

useless

store

elimination

-^

Useful for detecting uninitialised variables

-^

Essential for global register allocation

-^

Determines whether a variable MAY be read after this BB and is therefore acandidate to be put in a register M. O’Boyle

Scalar Optimisation

January 2013

7

Equations for live vars

LiveOut

(b

p∈

succ

(b

U EV ar

(p

)^

LiveOut

(p

)^

N otKilledV ar

(p

U EV ar

(p

)^

upwardly exposed variables used in p before redefinition

N otKilledV ar

(p

)^

var not defined in this block p

•^

Similar to AVAIL

-^

Depends on successors not predecessors backward vs forward

-^

AVAIL is an all paths problem (

) LiveOut any path (

•^

Can also be solved using iterative algorithm. (How long/terminate?) M. O’Boyle

Scalar Optimisation

January 2013

9

Solution:

B

B

B

B

B

B

B

B

UEVar

-^ -^ -^ -^ -^ -^ -^

a,b,c,d,i

NVarKill

a,b,c,d,y,z

b,d,i,y,z

a,i,y,z

b,c,i,y,z

a,b,c,i,y,z

a,b,d,i,y,z

a,c,d,i,y,z

a,b,c,d

Reverse Post order

Iter

B

B

B

B

B

B

B

B

-^

-^

-^

-^

-^

-^

-^

-^

-^

a,b,c,d,i

-^

-^

-^

a,b,c,d,i

-^

a,i

a,b,c,d,i

-^

a,c,d,i

a,c,d,i

a,b,c,d,i

i

i^

a,i

a,b,c,d,i

a,c,d,i

a,c,d,i

a,c,d,i

a,b,c,d,i

i

i^

a,c,i

a,b,c,d,i

a,c,d,i

a,c,d,i

a,c,d,i

a,b,c,d,i

i

i^

a,c,i

a,b,c,d,i

a,c,d,i

a,c,d,i

a,c,d,i

a,b,c,d,i

i

5 iterations to fixed point. Is this the quickest solution? M. O’Boyle

Scalar Optimisation

10

Solution 2

Post order

Iter

B

B

B

B

B

B

B

B

-^

-^

-^

-^

-^

-^

-^

i^

a,c,i

a,b,c,d,i

a,c,d,i

a,c,d,i

a,c,d,i

a,b,c,d,i

i^

a,c,i

a,b,c,d,i

a,c,d,i

a,c,d,i

a,c,d,i

a,b,c,d,i

i

i^

a,c,i

a,b,c,d,i

a,c,d,i

a,c,d,i

a,c,d,i

a,b,c,d,i

i

•^

What is the best order?

-^

Question: why does all this work? M. O’Boyle

Scalar Optimisation

12

Semi-lattice

•^

Choose a semi-lattice to represent the facts

-^

Attach a meaning to each

a

L

. Each a distinct set of facts

•^

For each node (basic block)

n

in the CFG, associate a function

f

n

L

L

•^

It models the behaviour of the code belonging to

n

•^

Avail: Semilatice is

E

,^ ∧

E

the set of all expressions,

is

M. O’Boyle

Scalar Optimisation

January 2013

13

Example of LiveOut lattice

(^1 )

a^

e

d

c

b

b^

c^

d^

e^

c^

d^

b e

c d

c e

d e

a b c

b^

a b e

a c

a c e

a d

c^

bc

a b

c d e

e

d cb a^

a b

c

b

a^

a^

a^

a^

b

d a^

d^

e^

d b^

e^

b d e

c d e

b c d e

a c d e

a b d e

M. O’Boyle

Scalar Optimisation

15

Iterative data flow

•^

If f is monotone and the semi-lattice bounded then the round robin algorithmterminates and finds a least fixed point

-^

Given certain technical constraints on f, there is a unique fixed point and

order

of evaluation does not matter

-^

Pick an order that converges quickly

-^

A lot of theory about this. Given certain conditions then a round-robin post-order alg will finish in

d

(G

passes where d(G)is the loop connectedness

•^

Most dataflow fits this. Means runs in linear time. Muchnick for more detailsfor more in depth explanation. M. O’Boyle

Scalar Optimisation

January 2013

16

Other dataflow analysis

•^

Reaching definitions :

Find all places where a variable was defined and not

killed subsequently

-^

Very Busy Expressions: An expression is evaluated on all paths leaving a block- used for code hoisting

-^

Constant Propagation. Shows that a variable

v

has the same value at point

p

regardless of control-flow. Allows specialisation.

-^

Uses a very small lattice and terminates quickly.

Easy to express using SSA

form M. O’Boyle

Scalar Optimisation

January 2013

18

Example SSA

i = 1

b =c =d=

d =

d=

c=

b =

i = i+ B B

B

B

B

B

B

B

a3 =

a1= a4 = (a2,a3) y = a4+b

(a0,a4)

a2=

M. O’Boyle

Scalar Optimisation

19

Algorithms using SSA

•^

Many dataflow algorithms are considerably simplified using SSA

-^

Value numbering. Each value has a unique name allowing value numbering oncomplex control-flow

-^

Constants

(n

p∈

pred

(n

F)

(p Constants

(p

•^

Small lattice

-maxint .. +maxint

}^

•^

Meet operator:

x

x,

x

, c

i^

c

j^

c

if ci

i^

c

elsej

•^

F

p^

depends on the operations in block. Optimistic algorithm

M. O’Boyle

Scalar Optimisation