slide11 opt
DESCRIPTION
Principles of Code OptimizationTRANSCRIPT
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(1)
For
mal
Lang
uage
san
dC
ompi
lers
Lect
ure
XI
Prin
cipl
esof
Cod
eO
ptim
izat
ion
Ale
ssan
dro
Art
ale
Fac
ulty
ofC
ompu
ter
Sci
ence
Fre
eU
nive
rsity
ofB
olza
no
PO
SB
uild
ing
Roo
m:
2.03
arta
le@
inf.u
nibz
.it
http
://w
ww
.inf.u
nibz
.it/
ar
tale
/
2014
/201
5F
irstS
emes
ter
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(2)
Sum
mar
yof
Lect
ure
XI
C
ode
Opt
imiz
atio
n
B
asic
Blo
cks
and
Flo
wG
raph
s
S
ourc
esof
Opt
imiz
atio
n
1.C
omm
onS
ubex
pres
sion
Elim
inat
ion
2.C
opy
Pro
paga
tion
3.D
ead-
Cod
eE
limin
atio
n
4.C
onst
antF
oldi
ng
5.Lo
opO
ptim
izat
ion
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(3)
Cod
eO
ptim
izat
ion:
Intr
o
In
term
edia
teC
ode
unde
rgoe
sva
rious
tran
sfor
mat
ions
cal
led
Opt
imiz
a-
tions
tom
ake
the
resu
lting
code
runn
ing
fast
eran
dta
king
less
spac
e.
O
ptim
izat
ionn
ever
guar
ante
esth
atth
ere
sulti
ngco
deis
the
best
poss
ible
.
W
ew
illco
nsid
eron
lyM
achi
ne-I
ndep
ende
ntO
ptim
izat
ionsi.e.
,the
ydo
nt
take
into
cons
ider
atio
nan
ypr
oper
tyof
the
targ
etm
achi
ne.
T
hete
chni
ques
used
are
aco
mbi
natio
nof
Con
trol-F
low
and
Dat
a-F
low
anal
ysis
.
C
ontro
l-Flo
wA
naly
sis.
Iden
ties
loop
sin
the
ow
grap
hof
apr
ogra
m
sinc
esu
chlo
ops
are
usua
llygo
odca
ndid
ates
for
impr
ovem
ent.
D
ata-
Flo
wA
naly
sis.
Col
lect
sin
form
atio
nab
out
the
way
varia
bles
are
used
ina
prog
ram
.
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(4)
Crit
eria
for
Cod
e-Im
prov
ing
Tran
sfor
mat
ions
T
hebe
sttr
ansf
orm
atio
nsar
eth
ose
that
yiel
dth
em
ost
bene
t
for
the
leas
t
effo
rt.
1.A
tran
sfor
mat
ion
mus
tpre
serv
eth
em
eani
ngof
apr
ogra
m.
Its
bette
rto
mis
san
oppo
rtun
ityto
appl
ya
tran
sfor
mat
ion
rath
erth
anri
skch
angi
ng
wha
tthe
prog
ram
does
.
2.A
tran
sfor
mat
ion
mus
t,on
the
aver
age,
spee
dup
apr
ogra
mby
a
mea
sura
ble
amou
nt.
3.A
void
code
-opt
imiz
atio
nfo
rpr
ogra
ms
that
run
occa
sion
ally
ordu
ring
debu
ggin
g.
4.R
emem
ber!
Dra
mat
icim
prov
emen
tsar
eus
ually
obta
ined
byim
prov
ing
the
sour
ceco
de:
The
prog
ram
mer
isal
way
sre
spon
sibl
ein
nd
ing
the
best
poss
ible
data
stru
ctur
esan
dal
gorit
hms
for
solv
ing
ap
robl
em.
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(5)
Qui
ckso
rt:
An
Exa
mpl
eP
rogr
am
W
ew
illus
eth
eso
rtin
gpr
ogra
mQui
ckso
rtto
illus
trat
eth
eef
fect
sof
the
vario
usop
timiz
atio
nte
chni
ques
.
void
quic
ksor
t(m
,n)
int
m,n
;f
int
i,j,v
,x;
if(n
=j)
brea
k;x
=a[
i];a[
i]=
a[j];
a[j]
=x;
g x=
a[i];
a[i]
=a[
n];
a[n]
=x;
/*
frag
men
tend
she
re */
quic
ksor
t(m
,j);
quic
ksor
t(i+
1,n)
;g
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(6)
Qui
ckso
rt:
An
Exa
mpl
eP
rogr
am(C
ont.)
T
hefo
llow
ing
isth
eth
ree-
addr
ess
code
for
afr
agm
ento
fQui
ckso
rt.
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(7)
Sum
mar
y
C
ode
Opt
imiz
atio
n
B
asic
Blo
cks
and
Flo
wG
raph
s
S
ourc
esof
Opt
imiz
atio
n
1.C
omm
onS
ubex
pres
sion
Elim
inat
ion
2.C
opy
Pro
paga
tion
3.D
ead-
Cod
eE
limin
atio
n
4.C
onst
antF
oldi
ng
5.Lo
opO
ptim
izat
ion
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(8)
Bas
icB
lock
san
dF
low
Gra
phs
T
heM
achi
ne-I
ndep
ende
ntC
ode-
Opt
imiz
atio
nph
ase
cons
ist
sof
cont
rol-
ow
and
data
-ow
anal
ysis
follo
wed
byth
eap
plic
atio
nof
tran
sfor
mat
ions
.
D
urin
gco
ntro
l-ow
anal
ysis
,a
prog
ram
isre
pres
ente
das
aF
low
Gra
ph
whe
re:
N
odes
repr
esen
t Bas
icB
lock
s:S
eque
nce
ofco
nsec
utiv
est
atem
ents
inw
hich
ow
-of-
cont
role
nter
sat
the
begi
nnin
gan
dle
aves
at
the
end
with
outh
alto
rbr
anch
es;
E
dges
repr
esen
tthe
ow
ofco
ntro
l.
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(9)
Flo
wG
raph
:A
nE
xam
ple
F
low
grap
hfo
rth
eth
ree-
addr
ess
code
frag
men
tfor
quic
ksor
t.E
achB
iis
aba
sic
bloc
k.
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(10)
The
Prin
cipa
lSou
rces
ofO
ptim
izat
ion
W
ein
divi
duat
eno
wth
eba
sic
tran
sfor
mat
ions
asth
ere
sult
of
data
-ow
anal
ysis
.
W
edi
stin
guis
hloc
altr
ansf
orm
atio
nsin
volv
ing
only
stat
emen
tsin
asi
ngle
basi
cbl
ock
from
glob
altr
ansf
orm
atio
ns.
A
basi
cbl
ockc
ompu
tes
ase
tofe
xpre
ssio
ns:
Anu
mbe
rof
tran
sfor
mat
ions
can
beap
plie
dto
aba
sic
bloc
kw
ithou
tcha
ngin
gth
eex
pres
sion
sco
mpu
ted
byth
ebl
ock.
1.C
omm
onS
ubex
pres
sion
sel
imin
atio
n;
2.C
opy
Pro
paga
tion;
3.D
ead-
Cod
eel
imin
atio
n;
4.C
onst
antF
oldi
ng.
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(11)
Sum
mar
y
C
ode
Opt
imiz
atio
n
B
asic
Blo
cks
and
Flo
wG
raph
s
S
ourc
esof
Opt
imiz
atio
n
1.C
omm
onS
ubex
pres
sion
Elim
inat
ion
2.C
opy
Pro
paga
tion
3.D
ead-
Cod
eE
limin
atio
n
4.C
onst
antF
oldi
ng
5.Lo
opO
ptim
izat
ion
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(12)
Com
mon
Sub
expr
essi
ons
Elim
inat
ion
F
requ
ently
apr
ogra
mw
illin
clud
eca
lcul
atio
nsof
the
sam
ev
alue
.
D
eni
tion.
An
occu
rren
ceof
anex
pres
sionE
isca
lled
aCom
mon
Sub
expr
essi
onif
Ew
aspr
evio
usly
com
pute
d,an
dth
eva
lues
ofva
riabl
esin
Eha
veno
chan
ged
sinc
eth
epr
evio
usco
mpu
tatio
n.
C
omm
onS
ubex
pres
sion
Elim
inat
ion:
Ass
ignm
ents
tote
mpo
rary
varia
bles
invo
lvin
gco
mm
onsu
bexp
ress
ions
can
beel
imin
ated
.
E
xam
ple.
Ass
ignm
ents
tobo
tht7
and
t 10
inbl
ock
B5
have
com
mon
sube
xpre
ssio
nsan
dca
nbe
elim
inat
ed.
B5
istr
ansf
orm
edas
:
t 6:=
4
ix
:=a
[t6]
t 8:=
4
jt 9
:=a
[t8]
a[ t
6]
:=t 9
a[t
8]
:=x
goto
B2
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(13)
Com
mon
Sub
expr
essi
ons
Elim
inat
ion
(Con
t.)
E
xam
ple
(Con
t.)A
fter
loca
lelim
inat
ion,
B5
still
eval
uate
s4
ian
d4
j
whi
char
e glo
balc
omm
onsu
bexp
ress
ions
.
4
j
isev
alua
ted
inB3
byt 4
.T
hen,
the
stat
emen
ts
t 8:=
4
j;t 9
:=a
[t8];
a[t
8]
:=x
can
bere
plac
edby
t 9:=
a[ t
4];
a[t
4]
:=x
N
ow,a
[t4]
isal
soa
com
mon
sube
xpre
ssio
n,co
mpu
ted
inB
3by
t 5.
The
n,
the
stat
emen
ts
t 9:=
a[t
4];
a[t
6]
:=t 9
can
bere
plac
edby
a[t
6]
:=t 5
.
A
nalo
gous
ly,t
heva
lue
ofxis
the
sam
eas
the
valu
eas
sign
edto
t 3in
bloc
k
B3;w
hile
t 6ca
nbe
elim
inat
edan
dre
plac
edbyt 2
.
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(14)
Com
mon
Sub
expr
essi
ons
Elim
inat
ion
(Con
t.)
E
xam
ple.
The
follo
win
go
wgr
aph
show
sth
ere
sult
ofel
imin
atin
gbo
thlo
cala
ndgl
obal
com
mon
sube
xpre
ssio
nsfr
omba
sic
bloc
ksB
5an
dB
6.
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(15)
DA
Gs
for
Det
erm
inin
gC
omm
onS
ubex
pres
sion
s
To
indi
vidu
ate
com
mon
sube
xpre
ssio
nsw
ere
pres
enta
basi
cb
lock
asa
DA
G
show
ing
how
expr
essi
ons
are
re-u
sed
ina
bloc
k.
A
DA
Gfo
ra
Bas
icB
lock
has
the
follo
win
gla
bels
and
node
s:
1.Le
aves
cont
ain
uniq
ueid
enti
ers,
eith
erva
riabl
ena
mes
orco
nsta
nts.
2.In
terio
rno
des
cont
ain
anop
erat
orsy
mbo
l.
3.N
odes
can
optio
nally
beas
soci
ated
toa
list
ofva
riabl
esr
epre
sent
ing
thos
eva
riabl
esha
ving
the
valu
eco
mpu
ted
atth
eno
de.
-
Fre
eU
nive
rsity
ofB
olza
noF
orm
alLa
ngua
ges
and
Com
pile
rs.
Lect
ure
XI,
2014
/201
5A
.Art
ale
(16)
DA
Gs
for
Blo
cks:
An
Exa
mpl
e
T
hefo
llow
ing
show
sbo
tha
thre
e-ad
dres
sco
deof
aba
sic
bloc
kan
dits
asso
ciat
edD
AG
.(1
)t 1
:=4
i
(2)
t 2:=
a[t
1]
(3)
t 3:=
4
i
(4)
t 4:=
b[t 3
]
(5)
t 5:=
t 2
t 4(6
)t 6
:=pr
od+
t 5(7
)pr
od:=
t 6(8
)t 7
:=i
+1
(9)
i:=
t 7(1
0)if
i