Docsity

Lecture Notes on Value Numbering - Introduction to Compilers | CMSC 430, Study notes of Computer Science

Toggle sidebar
bg1
Value numbering
Technique for nding lo cal redundantexpressions
Approach
associate
unique
value number with each
distinct value within block
twovariables havethe samevalue
only
if they
are provably identical
compiler uses hash table to i dentify expressions
producing samevalue numbers
hashingonvalue numbers, not names ( DAGs)
Algorithm
1. given operat ion
op x
1
x
2

x
k
,
1
)
x
k
2. nd value numbers for
x
1
x
2

x
k
,
1
3. construct hash key from
op
andval uenumbers
4. if hash key exists in tabl e, replace
op
with
copy
5. else create value number
v
, assign
v
to
op
and
x
k
CMSC 430
Lecture 18, Page 1
Value numbering example
CODE
Instructions resultValNum isConst
(t1) t1 :=5+i 3 no
(t2) a:=t1 3 no
(t3) b:=3 4 yes
(t4) t4 :=b+6 6 yes deleted
(t5) m:=t4 6 yes
SYMBOLS CONSTANTS
name ValNum isConst ValNum bits
c5 1 yes 1 5
i 2 no 4 3
a 3 no 5 6
c3 4 yes 6 9
b 4 yes
c6 5 yes
m 6 yes
AVAIL
lhsValNum op rhsValNum resultValNum instr
1 + 2 3 (t1)
CMSC 430
Lecture 18, Page 2
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lecture Notes on Value Numbering - Introduction to Compilers | CMSC 430 and more Study notes Computer Science in PDF only on Docsity!

V

alue

n um

b e ring

T

ec

hni

que

for

n di

ng

l o cal

redun

dan

t ex

p ressi

ons

Ap

p roa

ch



asso

c i ate

un

(^) ique

v al

ue

n um

b e r wi

t h

eac

h

di

sti

nct

v al ue

wi

thi

n

b l (^) o c k



t w o

v ari

(^) abl

es

h a v e t h e

sam

e

v al ue

o nly

i f t (^) hey

are

p ro

v ab

l y

i (^) den

ti c al



com

pi

l er

uses

hash

t abl

e to

i den

t i fy

ex

pr

e ssi

on

s

pr

o duci

ng

sam

e

v al

ue

n um

b (^) e r s



hashi

n g

on

v al

ue

n u m

b ers,

not

n am

es

D

A

Gs)

Al

go

ri thm

gi v en

op

(^) erat

ion

op

x 1 x 2  (^)  (^)  (^) x k 1 )

x k

nd

v al ue

n um

b e r s for

x 1 x 2 (^)  (^)  (^)  (^) x k (^) 1

const

r uct

h ash

k ey

f (^) r om

op

an

d

v al

u e n um

b e r s

i f hash

k e y

exi

st

s i n

tabl

(^) e,

repl

ace

op

wi

(^) th

c opy

el se

creat

e v al

ue

n um

b (^) er

v , assi

gn

v

to

op

and

x k

C MSC

430

L ec ture

1 8, (^) P age

(^1)

V alue

n um

b e ring

example

C

OD

E

Instruct

i ons

resul

tV

al

N um

i sConst

(t

t 1

:

i

no

(t

a

:=

t

no

(t

b

:=

y es

(t

t 4

: = b + 6 6 y

es

del

et

(^) ed

(t

m

t 4

y es

SYMBOLS

CONST

A

NTS

nam

e

V al Num

i sConst

V

al

Num

bi

(^) t s

c

y es

i

no

a

no

c

y es

b

y es

c

y es

m

y es

A

V

AIL

l (^) hsV

al Num

op

r hsV

al (^) N

um

resul

tV

al

Num

i nst

r

t1)

C (^) MSC

430

L ec ture

(^1) 8, (^) P age

(^2)

Data-

o w

anal

ysi

s

Da

ta-

o w

an

al ysis



c om

pile-

tim

e

r e ason

i (^) ng

ab

out

t he

r un-tim

e

o w

of

v al u e s i n

the

progr

am



repr

e sen

t fact

s ab

ou

t r un-ti

m

e b eha

vi (^) or



repr

e sen

t e

ec

t of

ex

ecuti

ng

eac

h

b asi

c

bl

o c k



pr

o p aga

te

fac

ts

ar

o u nd

con

t rol

o w

gr

aph

F o r mu

la ted

as

a

set

of

si mu

lta

ne

ous

eq

uati

on

s



set

s at

tac

hed

to

t h e

no

(^) d e s and

edges



l att

i (^) ce

to

desc

r i b e

rel

(^) at

i on

b (^) et

w een

v al (^) ues



usual

ly

r e p rese

n t ed

as

bi

t or

bi

t v e ctor

Lim

ita

tion

s



answ

ers

m

u st

b (^) e

conserv

at

i (^) v e



oft

en

n e (^) ed

t o

app

ro

xi

m

ate

i nform

ati

on



assum

e

al l p ossi

(^) bl

e

paths

can

b e

tak

e n

C MSC

430

L ec ture

1 8, (^) P age

(^3)

Data-

o w

anal

ysi

s

Al

(^) go

ri thm

bui

ld

con

trol

o w

graph

CF

G)

i ni

ti al

(l o c al

) data

gat

h e (^) r i ng

pr

o p aga

te

i nform

ati

on

ar

ound

the

grap

h

p ost

pro

c essi

ng

if

ne

e de

d )

Exam

pl (^) e

con

trol

o w

gra

ph

a

:

1

if

(b)

t hen

c

:=

a +b

els

e

b

:=

1

c

:=

a +b

...

C:

A:

B:

c := a+b

c := a+bb := 1

if (b)a := 1

D:

C (^) MSC

430

L ec ture

(^1) 8, (^) P age

(^4)

Av

ail

able

expre

ssions

example

N

o (^) d e

K I LL

GEN

D C B A

C:

A:

B:

c := a+b

c := a+bb := 1

if (b)a := 1

D:

A V A I L ( (^) A

)

=

A V AIL

(B)

A V AIL

(C)

A V AI

(^) L (D)

C MSC

430

L ec ture

1 8, (^) P age

(^7)

It

erativ

e

al

g orithm

Al

(^) go

ri thm

c hange

t (^) r ue;

whi

l e (c

hange)

c h ange

fal

se;

for

e ac

h

basi

c

bl

o c k

i n

rev

e r se

P

ostOrder:

sol

v e

for

b

i f (ol

d

6

new)

c h ange

true;

end

f or

end

whil

e

Sp

eed

of

so

lu tion



no

de

m

a y

c hange

onl

y

i f som

e pr

ede

cessor

c hanges



try

t o

vi

si t no

de

aft

er

a ll

i (^) ts

predece

ss

o r s



rP

ostOrder

p ropagat

es

inf

(^) or

m

at

i on

q u i c k l y



pr

o gr

am

s usu

al

l y

c on

v erge

af (^) ter

p asse

s



use

bit

v ect

(^) ors

f or

m

ore

eci

ency

C (^) MSC

430

L ec ture

(^1) 8, (^) P age

(^8)

P

o stOrde

r

and

R

ev

erse

P ost

O

r de

r

Step

:

P o stOrder

m

ai c (^) n() ou

n t =

Vi

si t (ro

o t) ;

Visi

(^) t (n)

m

ark

n

as

vi

si t ed

f or

eac

h

successor

s

of

n

not

y e t vi

si te

d

V

i (^) si t( (^) s);

P

ostOrder(n)

coun

t ;

c ou

n t =

coun

t

Step

2 :

rP

o stOrder

for

eac

h

no

(^) de

n

rP

ost

Or

de

r ( n)

Num

N

o (^) d e s

P ostOrder(n)

Depth-

r st

searc

h



r P ostOrder

C MSC

430

L ec ture

1 8, (^) P age

(^9)

Re

ac

hi

(^) ng

De

ni

(^) t i o ns



The

p ro

bl em:

W

hat

are

the

assi

gnm

e n t s ( or

de

ni

(^) ti ons)

of

a

v a r i (^) abl

e

x

that

m

a y

r e ac

h

a

parti

cul

ar

ref

erence

to

x ?



Wh

y

i s th

(^) i (^) s u sefu

l?

Con

stant

p r opa

gati

(^) on:

A

:=

2

A

:=

5

B

:

A

Lo

(^) op

i nva

ri an

(^) t co

d e moti

on:

L:

W : = W + 4 Y

:

20

X : = Y + W if

(...)

goto

L

C (^) MSC

430

L e cture

(^18)

, (^) P a ge

10

Liv

e

V

ar

i (^) abl

es



Sl

i gh

t ly

d i

eren

t , si nce

i nform

ati

on

at

basi

c

bl

o (^) c k

i s based

on

what

happ

ens

l ate

r i n

t (^) he

pr

o gr

am



A

bac

kw

ard

dat

a-

o w

pr

o b l e m

.



LIV

E(b)

i s t h e se

t of

de

nit

i ons

l i (^) v e on

ex

it

from

bl

o c k

b .



KILL(b)

i (^) s

as

b efore.



US

E(b)

i (^) s

the

set

of

l o cal

(^) l y

e xp

ose

(^) d

uses



succ(

b)

i s t (^) he

set

of

basi

c

bl

o (^) c ks

that

are

i m

m

e (^) d i at

e successors

of

b

i n

the

con

trol

o w

graph



Eq

ua

tion

s:

LIV

E(

b ) =

[

x 2 succ

( b ) (USE

x ) [

(LIVE

x )

KILL(

x )) )

Best

c ase

for

LIVE

b)

;

W

orse

case

f or

LIV

E(

(^) b ) =

f

al (^) l de

n itions

g

C MSC

430

L e cture

(^18)

, P a ge

13

What

Do

The

se

H

a v e

I n

Co

mm

on?

A

V

AIL

(^) b ) =

\

x 2 pr ed ( b ) (GEN

x ) [ (^) (A

V

AIL(

x )

KILL(

x ) ))

REA

CH(

b ) =

[

x 2 pr ed ( (^) b ) ( DE

F(

x ) [ (R

E

A

CH

x ) K

I (^) L L(

x )) (^) )

LIV

E(

b ) =

[

x 2 succ

( b ) (USE

(^) x ) [

(LIVE

x )

(^) KILL(

x )) )



Con

uen

ce

Op

e rato

r o r Me

et

F u nc

(^) tion

: unio

n

or

i n t ersect

i on



Be

havi

o r f o r bl o ck:

G

EN

and

KILL



A

d ire

cti

on:

f or

w ard

(con

uence

o v er

pr

e decessors)

or

b a (^) c k w ar

d

(o

v er

successors)



Be

st

c (^) ase

set

val

ue:

>



W

o rst

case

se

t val

ue

: ?

Ge

ner

al

e qu

ati

ons:

IN(b)

^ p 2 pr e d (b)

OUT(

p (^) ) y

OU

T

(b)

G

EN(b)

[

IN

b)

KILL(b)

y

Re

v erse

gr

aph

for

bac

k w a (^) r d

pr

obl

(^) em

C (^) MSC

430

L e cture

(^18)

, P a ge

14

Data-

o w

Analy

sis

F ramew

o rks

Use

sa

me

fr ame

w o rk

fo

r al l d (^) ata

o w

p rob

lem

s



gi v en

l o cal

i nform

ati

on

G

EN

KILL



start

w (^) it h

som

e

i ni

ti al

v alue

(^) s

f or

set

s IN,

OU

T



i te

r at

e

through

no

d e s i n

the

o w

graph,

recom

pute

t r ansfe

r funct

i ons

un

ti (^) l sets

stab

i lize

F ram

ew

o r k

h as

th

ree

com

p (^) one

nts



Dom

a in

of

v al u e (^) s:

L



Op

(^) erat

or

for

com

b i ni

(^) ng

v al ues:

^



A

set

of

transfer

functi

ons

L

!

L ) : F

Usef

ul ne

ss

of

u ni

ed

f ra

mew

o r k



De

nes

a

c ol

le (^) cti

on

of

prop

erti

es

t h at

guar

an

te

e

correct

ness,

c on

v ergenc

e;



Can

descri

b e

sp

ee

d

of

con

v ergence

and

p rec

i si (^) on

of

resul

(^) t

for

a

f (^) am

i l y

of

a n al y si s probl

em

s



Can

r e

use

co

de

to

sol

v e

new

anal

ysi

s p robl

em

s

C MSC

430

L e cture

(^18)

, P a ge

15

Data-

o w

Lat

tice

s

De

ni tion

s

a

lat

tic

e

i s a

set

L

and

a

m

ee

t op

erat

i on

^

suc

h

that

8

a ; (^) b;

(^) c

2

L

(a)

a

^

a

=

a

[i dem

p (^) ot

en

t]

b ) a ^ b = b ^ a

[com

m

utat

(^) i v e]

(c)

a

^

( (^) b ^

c ) =

a (^) ^

b ) (^) ^

(^) c

[asso

(^) ci at

(^) i v e]

^

i m

p oses

a

p art

i (^) al

order

on

L , 8

a ; (^) b

2

L

(a)

a



b ,

a (^) ^

(^) b

=

b

b ) a > b , a  b

and

a

6

b

a

l att

(^) i ce

m

a y

ha

v e

a

b ot

tom

el em

en

t ?

(a)

8

a

2

L ; (^)?

(^) ^

(^) a

=

?

b ) 8 a 2 L ;

(^) a



?

a

l att

(^) i ce

m

a y

ha

v e

a

t op

e le m

e n t >

(a)

8

a

2

L ; (^) >

(^) ^

(^) a

=

a

b ) 8 a 2 L ;

(^) >



a

C (^) MSC

430

L e cture

(^18)

, P a ge

16

It

erativ

e

al

g orithm

Wh

at

ab

(^) ou

t lo ops?



ci rcul

ar

dep

endenci

es

b (^) et

w ee

n

bl

o c k s



can

i nit

i (^) al

i ze

sol

uti

ons,

then

sol

v e

r e p eat

edl

y

Exam

pl (^) e

C

=

A+B

L:

D

=

A+B

A

=

...

if

( ...)

goto

L

T e rmi

n ati

on



goal

i s f or

sol

uti

ons

to

con

v erge

to

a

xe

d

p oint



can

stop

once

sol

uti

ons

stop

c hangi

ng



i s thi

s guaran

tee

d ?

C MSC

430

L e cture

(^18)

, P a ge

19

Mo

not

oni

cit

y



A

f r am

ew

ork

(D,

^ , F)

i (^) s

m

on

ot

one

i

x



y

i m

pl

ie (^) s

f (^) ( x ) 

f (^) ( (^) y )

i .e.,

a

\sm

al

l er

or

eq

u a l"

i nput

to

t he

sam

e

f u nct

i on

w i ll

al

w a y s gi

v e

a

\sm

al

l er

or

e qual

o (^) u t pu

t



Equi

v al

e n t ly , m

onoto

n e i

f (^) ( x (^) ^

(^) y ) 

f (^) ( x ) ^

f (^) ( (^) y )

i .e.,

i f m

erge

input,

t h e n

appl

y

f,

result

i s

\sm

al l er

or

eq

ual

t o

a p pl

y ing

f

i ndi

vi

dual

l y

and

m

ergi

ng

resul

t



In

t ui

ti v el y , m

onotoni

(^) ci t y

m

e (^) an

s \sm

al

l er"

i nput

wi

l l n ot

yi

el d

\l

ar

ge

r "

out

p ut

.



m

onotone

fram

ew

orks

ar

e

guaran

tee

d

t o

con

v erge

and

t (^) erm

i (^) nat

(^) e

(i f l att

i ce

el em

en

ts

can

onl

y

dr

op

i (^) n

i nfo

r m

at

ion

a

n i t e

n um

b er

of

ti m

es)

C (^) MSC

430

L e cture

(^18)

, (^) P a ge

20

Qua

li t y

o f Solution

P ossib

le

sol

uti

on

s



p erfe

ct

sol

uti

on

m

e et

o v er

r e al

pat

h s t ak

en

dur

i ng

pr

ogram

exe

cuti

on



m

eet

o v er-al

l

pat

hs

MOP

m

eet

o v er

p ot

ential

paths

i (^) n

con

trol

o w

graph



m

axi

m

al

x ed-p

(^) oi

n t ( (^) MFP)

sol

ut

(^) i on

from

i te

r at

i v e

fram

ew

or

k

Prop

e rti

(^) es



i n

ge

neral,

MFP



MOP



P

erfe

ct

Sol

uti

on



i n

som

e sense,

MOP

i (^) s

b est

f easi

bl

e

solut

i on



MFP

i s uniq

ue,

r e gar

dl

ess

of

order

of

pr

o p aga

ti (^) on



a

fram

ew

or

k

i (^) s

dis

t r ibut

i v e

i f

f (^) ( x ^ y ) = f

x ) ^

f (^) ( y )



for

a

di

st

r i buti

v e f r am

ew

ork,

MFP

MOP

C MSC

430

L e cture

(^18)

, P a ge

21