Docsity

CS-322 Optimization: Data Flow Analysis - Prof. Harry H. Porter, Study notes of Computer Science

Toggle sidebar
bg1
1
CS-322 Optimization, Part 3
© Harry H. Porter, 2006
Global Data_Flow Analysis
Examples:
Reaching Definitions:
Which DEFINITIONs reach which USEs?
LIVE Variable Analysis:
Which variables are live at a given point, P?
Global Sub-Expression Elimination:
Which expressions reach point P
and do not need to be re-computed?
Copy Propagation:
Which copies reach point P?
Can we do copy propagation?
2
CS-322 Optimization, Part 3
© Harry H. Porter, 2006
Terminology
A “point”
between two adjacent statements in a basic block,
or directly before the basic block,
or directly after the basic block.
a := b+c
c := b-d
e := a*c
if a<6 goto...
a := c+1
d := a+d
b := b-5
d := e-f
f := a+d
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e

Partial preview of the text

Download CS-322 Optimization: Data Flow Analysis - Prof. Harry H. Porter and more Study notes Computer Science in PDF only on Docsity!

© Harry H. Porter, 2006

Global Data_Flow Analysis

Examples:

Reaching Definitions:

Which DEFINITIONs reach which USEs?

LIVE Variable Analysis:

Which variables are live at a given point, P?

Global Sub-Expression Elimination:

Which expressions reach point P

and do not need to be re-computed?

Copy Propagation:

Which copies reach point P?

Can we do copy propagation?

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

Terminology

A “point”

between two adjacent statements in a basic block,

or directly before the basic block,

or directly after the basic block.

a := b+c c := b-d e := ac if a<6 goto... a := c+ d := a+d b := b- d := e-f f := a+d*

© Harry H. Porter, 2006

Terminology

A “point”

between two adjacent statements in a basic block,

or directly before the basic block,

or directly after the basic block.

A “path”

is a sequence of points from P 1 to PN such that...

control could flow from P 1 to PN.

The path may traverse several blocks.

a := b+c c := b-d e := ac if a<6 goto... a := c+ d := a+d b := b- d := e-f*

from here f := a+d

to here

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

Reaching Definitions

A “definition” of variable x

A statement that assigns to x (or might assign to x).

Ambiguous Definitions -- Might assign

Unambiguous Definitions -- Will definitely assign

Examples

x := ...;

read (x);

call foo (... x ...)

call foo ()

*p := ...;

y := ...;

Where x is passed by reference,

by copy-restore, or by name

Where the function may

access X as a non-local

Pointer assignment

Aliasing

Unambiguous; will

definitely change x

© Harry H. Porter, 2006

A definition D “reaches” a point P...

if there is a path from D to P along which D is not killed. If “x” is defined at D, then the value given to “x” might be the value of “x” at point P. When D reaches P, it means... The value of “x” might reach P at runtime. D 1 : x := a+b D 2 : y := c+d ... d := ab b := x+y x := c y := e-d y := b+*

B 1
B 2
B 3
B 4

Does D 1 reach P?

Does D 2 reach P?

Point P

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

A definition D “reaches” a point P...

if there is a path from D to P along which D is not killed. If “x” is defined at D, then the value given to “x” might be the value of “x” at point P. When D reaches P, it means... The value of “x” might reach P at runtime. D 1 : x := a+b D 2 : y := c+d ... d := ab b := x+y x := c y := e-d y := b+*

B 1
B 2
B 3
B 4

Does D 1 reach P? YES

Does D 2 reach P? NO

Point P

© Harry H. Porter, 2006

Safe, Conservative Estimates

Will the value of x reach point P?

The runtime value of variables may cause some paths to

It may be the case that... NEVER be taken.

In ALL executions, control ALWAYS passes through B2...

D may get killed in every execution!

The value of “x” may never reach point P!

Nevertheless, we say “D reaches P”.

D 1 : x := a+b D 2 : y := c+d ... d := ab b := x+y x := c y := e-d y := b+*

B 1
B 2
B 3

B 4 Point P

It is undecideable (in general)

to determine statically which paths

will be taken at runtime.

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

USE-DEFINITION Chains (U-D Chains)

For each USE of some variable “x”...

build a list of all the DEFINITIONs of “x”

that reach this USE.

d := x * b c := e - x x := b + c g := a - 1 b := a + x ...

x := a+b

x := c+d

© Harry H. Porter, 2006

If we can deduce that the set of definitions

reaching this point contains

ONLY the assignment D to “x”,

then it is okay to substitute 5 for “x” here

D: x := 5 y := x * 2

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

DEFINITION-USE Chains (D-U Chains)

A variable is USED at statement S if

its value may be required.

For each DEFINITION of a variable...

compute a list of all possible USEs of that variable.

z := a + x ... y := x * 3 ... x := a

z := x + y ... x := x + 1

[ , , ]

x := a + b ...

but not here

© Harry H. Porter, 2006

If we can deduce that the definition D

has NO POSSIBLE USES

then D is “DEAD” (useless code)

and can be eliminated!

... D: x := 5 ... CS-322 Optimization, Part 3 © Harry H. Porter, 2006

The Universe

U = Universe

= the set of all DEFINITIONs in the program / CFG

Number them D 1 , D 2 , D 3 , ...

D 1 : i := m- D 2 : j := n D 3 : a := w D 4 : i := i+ D 5 : j := j- if... D 7 : i := z if... D 6 : a := y

B 1

B 2

B 3

B 4

Example:

© Harry H. Porter, 2006

Representing Sets

We will work with sets.

How to represent?

Each set is represented with a Bit Vector

How to compute set operations?

Set Union

A! B " A’ or B’

Set Intersection

A # B " A’ and B’

Set Difference

A – B " A’ and (not B’)

D 1 D 2 D 3 D 4 D 5 D 6 D 7 0 1 0 1 0 0 1

Example

A = { D 2 , D 4 , D 7 }

A’ =

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

Approach

Figure out what happens in each basic block...

GEN[B] =

-! The set of definitions appearing in block B

which reach the end of B

(without being KILLed before the end of the block)

KILL[B] =

-! The set of definitions KILLed by statements in block B. -! If B contains an unambiguous definition of variable “x”,

then add all definitions of “x” to KILL[B].

(unless the definition D of “x” also occurs in B and

there are no unambiguous definitions between D

and the end of B).

Use this info to do the entire flow graph...

Using DATA FLOW EQUATIONS

In the text: DEDef()

In the text: DefKill()

© Harry H. Porter, 2006

Example of GEN [B]

Consider this Basic Block:

Consider D 5 , a definition of “c”...

Add D 5 to GEN [B].

Consider D 6 , a definition of “x”...

But this is KILLed before the end of the block.

Consider D 7 , a definition of “x”...

Add D 7 to GEN [B].

GEN [B] = {D 5 , D 7 }

D 5 : c := e - x D 6 : x := b + c D 7 : x := a - 4

B

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

Example of KILL [B]

Consider this Basic Block:

Consider D 5 , an unambiguous defintion of “c”...

Add all other definitions of “c” to KILL [B].

(Except, do not add D 5 itself,

since this definition “makes it to the end of the block”.)

Consider D 7 , an unambiguous defintion of “x”...

Add all other definitions of “x” to KILL [B]

(Except, do not add D 7 itself,

since this definition “makes it to the end of the block”.)

D 5 : c := e - x D 6 : x := b + c D 7 : x := a - 4

B

© Harry H. Porter, 2006

Start by setting IN[B] to {} for each basic block.

Then compute OUT[B] from the previous estimate of IN[B].

Finally, propagate OUT[B] to the IN[B’]

for all successor blocks to B.

Repeat, until no more changes.

As the definitions “flow through the graph”,

the IN and OUT sets grow and grow.

The approximation gets closer and closer.

Conservative: May overestimate

how far definitions will reach.

(i.e., the results may be larger than “truly” necessary.)

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

A Recurrence

(a set of simultaneous equations)

P 1

OUT$

P 2

OUT$

P 3

OUT$

B

OUT$
IN$

! f(i)

0<i<N

IN[B] := OUT[P]

P is a predecessor of B OUT[B] := GEN[B]! ( IN[B] - KILL[B])

© Harry H. Porter, 2006

OUT

for each block B do

OUT[B] := GEN[B]

endfor

while change do

for each block B do

IN[B] :=! OUT[P]

P is a predecessor of B

OUT[B] := GEN[B]! (IN[B] - KILL[B])

endfor

endwhile

P 1

OUT$

P 2

OUT$

P 3

OUT$

B

OUT$
IN$
IN[B] := OUT[P]

P is a predecessor of B OUT[B] := GEN[B]! ( IN[B] - KILL[B])

Initialize OUT on the

assumption that

IN[B] = {} for all blocks.

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

OUT

for each block B do

OUT[B] := GEN[B]

endfor

change := true

while change do

change := false

for each block B do

IN[B] :=! OUT[P]

P is a predecessor of B

OLD_OUT := OUT[B]

OUT[B] := GEN[B]! (IN[B] - KILL[B])

if OUT[B] % OLD_OUT then

change := true

endif

endfor

endwhile

P 1

OUT$

P 2

OUT$

P 3

OUT$

B

OUT$
IN$
IN[B] := OUT[P]

P is a predecessor of B OUT[B] := GEN[B]! ( IN[B] - KILL[B])

Initialize OUT on the

assumption that

IN[B] = {} for all blocks.

© Harry H. Porter, 2006

Example

D 1 : i := m- D 2 : j := n D 3 : a := w D 4 : i := i+ D 5 : j := j- if... D 7 : i := z if... D 6 : a := y

B 1

B 2

B 3

B 4

GEN[B 1 ] = { D 1 , D 2 , D 3 }

1 1 1 0 0 0 0 KILL[B 1 ] = { D 4 , D 5 , D 6 , D 7 } 0 0 0 1 1 1 1 GEN[B 2 ] = { D 4 , D 5 } 0 0 0 1 1 0 0 KILL[B 2 ] = { D 1 , D 2 , D 7 } 1 1 0 0 0 0 1 GEN[B 3 ] = { D 6 } 0 0 0 0 0 1 0 KILL[B 3 ] = { D 3 } 0 0 1 0 0 0 0 GEN[B 4 ] = { D 7 } 0 0 0 0 0 0 1 KILL[B 4 ] = { D 1 , D 4 } 1 0 0 1 0 0 0 OUT 111 0000 000 1100 000 0010 000 0001 IN 000 0000 111 0001 000 1100 000 1110 OUT 111 0000 001 1100 000 1110 000 0111

B 1 B 2 B 3 B 4

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

Example

D 1 : i := m- D 2 : j := n D 3 : a := w D 4 : i := i+ D 5 : j := j- if... D 7 : i := z if... D 6 : a := y

B 1

B 2

B 3

B 4

GEN[B 1 ] = { D 1 , D 2 , D 3 }

1 1 1 0 0 0 0 KILL[B 1 ] = { D 4 , D 5 , D 6 , D 7 } 0 0 0 1 1 1 1 GEN[B 2 ] = { D 4 , D 5 } 0 0 0 1 1 0 0 KILL[B 2 ] = { D 1 , D 2 , D 7 } 1 1 0 0 0 0 1 GEN[B 3 ] = { D 6 } 0 0 0 0 0 1 0 KILL[B 3 ] = { D 3 } 0 0 1 0 0 0 0 GEN[B 4 ] = { D 7 } 0 0 0 0 0 0 1 KILL[B 4 ] = { D 1 , D 4 } 1 0 0 1 0 0 0 OUT 111 0000 000 1100 000 0010 000 0001 IN 000 0000 111 0001 000 1100 000 1110 OUT 111 0000 001 1100 000 1110 000 0111 IN 000 0000 111 0111 001 1100 001 1110

B 1 B 2 B 3 B 4

© Harry H. Porter, 2006

Example

D 1 : i := m- D 2 : j := n D 3 : a := w D 4 : i := i+ D 5 : j := j- if... D 7 : i := z if... D 6 : a := y

B 1

B 2

B 3

B 4

GEN[B 1 ] = { D 1 , D 2 , D 3 }

1 1 1 0 0 0 0 KILL[B 1 ] = { D 4 , D 5 , D 6 , D 7 } 0 0 0 1 1 1 1 GEN[B 2 ] = { D 4 , D 5 } 0 0 0 1 1 0 0 KILL[B 2 ] = { D 1 , D 2 , D 7 } 1 1 0 0 0 0 1 GEN[B 3 ] = { D 6 } 0 0 0 0 0 1 0 KILL[B 3 ] = { D 3 } 0 0 1 0 0 0 0 GEN[B 4 ] = { D 7 } 0 0 0 0 0 0 1 KILL[B 4 ] = { D 1 , D 4 } 1 0 0 1 0 0 0 OUT 111 0000 000 1100 000 0010 000 0001 IN 000 0000 111 0001 000 1100 000 1110 OUT 111 0000 001 1100 000 1110 000 0111 IN 000 0000 111 0111 001 1100 001 1110 OUT 111 0000 001 1110 000 1110 001 0111

B 1 B 2 B 3 B 4

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

Example

D 1 : i := m- D 2 : j := n D 3 : a := w D 4 : i := i+ D 5 : j := j- if... D 7 : i := z if... D 6 : a := y

B 1

B 2

B 3

B 4

GEN[B 1 ] = { D 1 , D 2 , D 3 }

1 1 1 0 0 0 0 KILL[B 1 ] = { D 4 , D 5 , D 6 , D 7 } 0 0 0 1 1 1 1 GEN[B 2 ] = { D 4 , D 5 } 0 0 0 1 1 0 0 KILL[B 2 ] = { D 1 , D 2 , D 7 } 1 1 0 0 0 0 1 GEN[B 3 ] = { D 6 } 0 0 0 0 0 1 0 KILL[B 3 ] = { D 3 } 0 0 1 0 0 0 0 GEN[B 4 ] = { D 7 } 0 0 0 0 0 0 1 KILL[B 4 ] = { D 1 , D 4 } 1 0 0 1 0 0 0 OUT 111 0000 000 1100 000 0010 000 0001 IN 000 0000 111 0001 000 1100 000 1110 OUT 111 0000 001 1100 000 1110 000 0111 IN 000 0000 111 0111 001 1100 001 1110 OUT 111 0000 001 1110 000 1110 001 0111 IN 000 0000 111 0111 001 1110 001 1110

B 1 B 2 B 3 B 4

© Harry H. Porter, 2006

This algorithm converges.

OUT[B] never decreases...

Once in OUT[B] a definition stays there.

Eventually, no changes will be made to OUT[B].

An upper bound on the “while” loop?

Number of nodes in the flow graph.

Each iteration propagates reaching definitions.

The “while” loop will converge quickly

...if you select a good order for the nodes in the “for”

loop.

This algorithm is efficient in practice.

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

LIVE Variable Analysis

A similar Data Flow Algorithm

Goal: Compute IN[] and OUT[]

However, it will work backwards!

(i.e., data will flow “upwards”, against the arrow directions)

Given: Compute:

Which

variables

are LIVE

here

Which

variables

are LIVE

here

© Harry H. Porter, 2006

LIVE Variable Analysis

Then:

Compute the OUT set from

all the IN sets of the block’s successors!

CS-322 Optimization, Part 3 © Harry H. Porter, 2006

LIVE Variable Analysis

Then:

Compute the OUT set from

all the IN sets of the block’s successors!

Given Given