Vous êtes sur la page 1sur 10

UNIT 8

Code Optimization and Generation


Code Generation Issues
Instructon Seecton
Can be a smpe transaton of three address nstructon to target
code
x = y + z becomes w $t0, y
w $t1, z
add $t2, $t0, $t1
sw $t2, x
Concepts of code generaton, regster aocaton and
optmzaton are ntroduced
Agorthm for generatng code for arthmetc expressons usng
a mnma number of regsters
Target Code Addresses
Four areas of memory: Code, Statc, Heap and Stack
Can use one statc base ocaton for Code and Statc varabe area
procedures have a ocaton (offset) of the code n ths area
goba varabes aocated n the statc area aso gven offsets here
Other program varabes, oca varabes and forma
parameters, are gven offset ocatons wth regard to a stack
actvaton record ponter
Basic Blocks and Flow Graphs
Representaton of ntermedate code as a graph
nodes of the graph are basc bocks, where the fow of contro can
ony enter at the frst nstructon and eave through the ast
edges ndcate whch bocks can foow other bocks, representng
the |umps n the code
Usefu for dscussng code generaton
Defnng basc bocks
separate sequence of TAC (three address code) nto basc bocks by
dentfyng the frst nstructon as a eader:
very frst nstructon s a eader
any nstructon that s the target of a |ump s a eader
any nstructon foowng a |ump s a eader
!ample o" Basic #locks
psuedo code to ntaze a 10 by 10
array to be the dentty matrx,
for from 1 to 10 do
for | from 1 to 10 do
a|, || = 0.0
for from 1 to 10 do
a|, || = 1.0
Three address code, assumng
a s the startng address of the
array n row-ma|or form and
that each eement takes 8 bytes
each:
1. = 1 eader
2. | = 1 eader
3. t1 = 10 * eader
4. t2 = t1 + |
5. t3 = 8 * t2
6. t4 = t3 88
7. a|t4| = 0.0
8. | = | + 1
9. f | <= 10 goto 3.
10. = + 1 eader
11. f <= 10 goto 2.
12. = 1 eader
13. t5 = 1 eader
14. t6 = 88 * t5
15. a|t6| = 1.0
16. = + 1
17. f <= 10 goto 13.
Graph $epresentation
Ne!t Use and %i&eness
Use of a varabe:
f nstructon assgns a vaue to x, nstructon | has x as an operand,
and contro can fow aong a path from to | wth no ntervenng
assgnments to x, then | uses
Lve varabes:
for each nstructon x = y + z, determne for x, y and z whch
nstructon next uses that varabe
These propertes can be determned by makng a backward
pass over a basc bock and recordng the nformaton n the
symbo tabe
'imple code generation
Generates code consderng |ust one basc bock, but
ntroduces the deas of regster aocaton
assume that we keep nformaton about regsters
regster descrptor keeps track of whch varabe names have a
current vaue n that regster
address descrptor for each program varabe keeps track of where
the current vaue of the varabe can be found
GetRegster functon gets an approprate regster for any
operand of the TAC
For the nstructon x = y + z
ca GetRegster for each of the operands
f y s not aready n ts regster: w Ry, y
f z s not aready n ts regster: w Rz, z
gve the nstructon add Rx, Ry, Rz
'imple generation updates descriptors
At end of basc bock, gnore TAC temporares (not ve)
for each program varabe x, f current vaue of x s not n memory,
ssue a sw nstructon
Updatng descrptors:
For the nstructon w Ry, y, change regster descr. for Ry to ony
hod y
add Ry to address descrptor of y as a ocaton
for the nstructon sw x, Rx, change the address descr of x to ncude
memory address of x
for each operaton add Rx, Ry, Rz
regster descr Rx hods ony x
address descr of x has ony Rx (not any memory ocaton)
remove Rx from address descr of any other varabe
Get$egister "unction
Pck a regster Ry for any varabe y that s an operand
f y s aready n a regster, pck t (no oad nstructon needed)
f y s not n a regster, but one s free, pck that regster (and oad t)
f y s not n a regster but there s not regster free, consder
canddate
regsters R
any regster wth a varabe v, whose descrptor says ts current
vaue s n memory aready
any regster wth the varabe x, the resut of the nstructon, and x
s not aso one of the operands (x w be rewrtten anyway)
any regster wth a varabe v that s not used ater
otherwse, generate a store nstructon sw R, v to "sp" v
repeat these steps for other varabes n the regster, and pck a
regster wth the fewest number of stores
Pck a regster Rx for the varabe x that s the resut
n addton to above: any regster hodng ony x
f y s not used ater, use Ry to hod the resut Rx (e.g. x = y )
(eephole Optimization
Another strategy s to generate nave code and then mprove
the quaty of the target code by smpe optmzatons
sdng wndow of nstructons (peephoe) to examne
Redundant oads and stores
Emnatng unreachabe code
exampe: emnate |umps over |umps
Fow of contro optmzatons anayze |ump sequences
Agebrac smpfcaton and reducton n strength
$egister Allocation and Assignment
Goba regster aocaton tres to keep frequenty used
varabes n regsters across basc bock boundares
assgn regsters to most actve vaues of nner oops
Usage counts approxmate formua for the beneft to be
obtaned from aocatng a regster x wthn oop L s:
S
(bocks B n L) use(x, B) + 2 * ve(x,B)
aso assumes that oops teratons are a arge number and that the
dfference n number of tmes a bock s executed s neggbe
choose regster aocatons that maxmze benefts over the varabe
set
after aocatng nner oops, go on to smar counts for outer
oops.
Optimization Concepts
Optmzaton seeks to mprove the tme or space used by the
generated code wthout changng the behavor of the
program
Comper area where most research s currenty beng
conducted
Many comper optmzaton probems are NP
approxmatons and heurstcs used nstead
"soup" approach keep addng functonates
Many modern compers have extensve optmzaton whch
may be qute tme and space-consumng themseves
optons to turn off optmzaton for better compng
Glo#al Optimization Framework
Extend the deas of the program as a DAG of basc bocks to do
goba
data-fow anayss
prevousy dscussed optmzatons wthn basc bocks as part
of code
generaton
ook at exampes n Stanford optmzaton handout
constant fodng, constand and copy propagaton,
Functon preservng transformatons can mprove the program
wthout
changng what t computes
Emnatng common subexpressons
Copy propogaton
Dead-code emnaton
Loop optmzatons
Code moton for oop-nvarant code
Inducton varabes and reducton n strength
Prmary at procedure eve, cross-procedure data-fow s more
dffcut
)ata "or data*"low anal+sis
The comper needs to coect nformaton about the
program as a whoe, prmary about varabe defntons
Data-fow equatons are set up. A typca equaton may
have the form:
out|S| = gen|S| + (n|S| k|S|)
"the nformaton at the end of statement S s ether
generated wthn S or enters at the begnnng and s not
ked durng S"
for dfferent data-fow probems may sove n the drecton of
contro fow to fnd out|S| n terms of n|S|, or the probem may
requre to backwards to fnd n|S| n terms of out|S|
)e"initions "or Flow Graphs
A pont s between two statement or basc bocks
A path connects ponts
A node d s a domnator node over a node n f every path n the
fow graph to n aso goes through d
A oop s a set of nodes wth a snge entry node, caed the
oop header, that domnates a other nodes n the oop
the oop must aso have a path to terate the oop, .e. go back
to the header
$eaching )e"initions
A defnton of a varabe x s a statement that assgns, or may assgn,
a vaue to x = expr and Read(x) are unambguous assgnments
ambguous assgnments ncude passng x as a reference parameter
to a procedure or assgnng to a ponter that coud refer to x
A defnton d reaches a pont p f there s a path from the defnton d
such that d s not "ked" aong the path
A defnton d s ked f ts varabe x has another defnton
,uations "or reaching de"initions
Based on the structure of statements, we can defne the equatons
for reachng defntons
Exampe: S s an assgnment statement (def d): d: a = b + c
gen|S| = {d}, k|S| = Def(a) {d},
out|S| = gen|S| + (n|S| k|S|)
Exampe: S s 2 statement sequence: S1; S2
gen|S| = gen|S2| + (gen|S1| k|S2|)
k|S| = k|S2| + (k|S1| gen|S2|)
n|S1| = n|S|, n|S2| = out|S1|, out|S| = out|S2|
Exampe: S has the body of a oop: whe . { S1 }
gen|S| = gen|S1|
k|S| = k|S1|
n|S1| = n|S| + gen|S1|
out|S| = out|S1|
Use*)e"inition Chains
Reachng defnton nformaton s often stored as use defntons
chans, aso known as ud-chans.
for each use of a varabe, there s a st of a the defntons of the
varabe that reach that use
Agorthms to sove the data-fow anayss may be dffcut f there are
break and contnue statements aowed
approaches ncude
teratve agorthms: start wth sets at emtpy and terate (ether
forward or backward) addng defntons unt convergence
nterva anayss
Smar technques are used to defne "avaabe expressons"
an expresson "x + y" s avaabe at pont p f every path from
the nta node to p evauates x + y, and after the ast such
evauaton, there are no subsequent assgnments to x or y
%i&e -aria#le anal+sis
For a varabe x and a pont p n the program, we want to know f x
can be used aong some path startng from p.
If so, x s "ve" at p; otherwse x s "dead" at p can be used n
regster aocaton
Defne n|B| to be the set of varabes ve at the pont mmedatey
before bock B and out|B| to be the same at the end of the bock. Aso
def|B| s the set of varabes defned n B pror to any use of that
varabe and use|B| s the set of varabes whose vaues may be used n
B pror to any defnton
n|B| = use|B| + (out|B| def|B|)
out|B| = unon over a successors S: n|S|
)e"inition*Use Chains
A cacuaton done n the same manner as ve-varabe anayss s
defnton-use channg, (du-chans).
A varabe s used n a statement f ts vaue s requred.
b and c (but not a) are used n both a = b+ c and a|b| = c.
The du-chan computes for a pont p, the set of uses s of a
varabe x, such that there s a pont from p to s that does not
redefne x
limination o" Glo#al Common 'u#e!pressions
Gven a fow graph wth avaabe expresson nformaton,
computes a revsed fow graph emnatng common subexpressons
For every statement of the form x = y + z such that y + z s
avaabe at the begnnng of the bock, search to fnd the
evauatons of y+z n prevous bock (say w = y + z) and repace
wth
u = y + z
w = u
Then repace a x = y + z n ths bock wth x = u
Conservatve agorthm doesnt fnd a possbe common
subexpressons
It doesnt aways mprove code, but ater optmzatons may
Cop+ (ropogation
Varous optmzatons produce copy statements of the form s: x =
y
Determne a the paces where ths defnton of x s used and
substtute y for x n a those paces
every use of x must meet condtons
statement s must be the ony defnton of x reachng u .e. the
ud-chan for use u conssts ony of s
on every path from s to u, there are no assgnments to y ths
requres settng up an addtona data-fow probem computng the
use of copy statements
)etection o" %oop*In&ariant Computations
Use ud-chans to detect computatons whose vaue does not
change as ong as contro stays wthn the oop
Consder an assgnment x = y + z n a oop where a possbe
defntons of y and z are outsde the oop, then y + z s oop-
nvarant
Suppose there s another statement v = x + w, where w s ony
defned outsde the oop, the ths statement s aso oopnvarant
the du-chan for x = y + z w te us there the vaue of x can be
used, so we need ony check those uses of x for operands w that are
aso defned outsde the oop.
Code .otion
Once we have found nvarant statements, then we want to
perform code moton to move such statements outsde the oop, f
possbe
Before the header bock of the oop, we ntroduce a "preheader"
bock wth oop-nvarant assgnments
Condtons for movng statement s: x = y + z
bock contanng s domnates a ext nodes of the oop
there are no other statements n the oop that assgn to x
no use of x n the oop s reached by any defnton of x other than
s
Ths s conservatve, there are other code moton strateges
limination o" induction &aria#les
A varabe x s caed an nducton varabe of a oop L f every tme
that the varabe changes vaues, t s ncremented or decremented
by the same constant each tme around the oop
e.g. n a for oop, the ndex varabe I
We frst ook for basc nducton varabes, those whose ony
assgnments are (+ or - ) a constant
requres reachng defnton nformaton and oop-nvarant
nformaton
We next ook for addtona nducton varabes | that are defned
ony n L and are a near functon of a basc nducton varabe
Modfy the computatons of the addtona nducton varabes to
use + or constants, nstead of mutpcatons
)i""iculties "or Code Optimization
Deang wth aases: two or more expressons denote the
same memory address
Aases can be ntroduced both by ponters and by procedure
cas
anayss of ponter assgnments
data-fow anayss of "changed" varabes across procedure cas

Vous aimerez peut-être aussi