Professional Documents
Culture Documents
ratlon - - - - - - - - - - - - - - - 427),
Solution. Given expression is Now [et us apply the above discussed algorithm, which gives following Da~,...
a= (J' + q) ((r + s) - f) with heuristic ordering.
Following table will generates thc_~n:truc~n---~- _ _ _ _ _ _ __
Let us understand how effective it is to generate code using DAG instead of using t,:=c-d
a three address code sequence. t 3:=e+t2
t 1:=a+b
8.12.1 Rearranging Order
t,:=t,+t,
The order of three address code affects st of the ob;ect code being generated.
then we get an improved code as
In the sense that by changing the order whl computations are done we can
obtain the object code with minimum cost. MOVc, RO
SUB d , RO
For example :
MOVe, R1
ti7=a+b ADD RO, Rl
t2:=c--d MOVa, RO
tf=e-rt2 ADDb, RO
t4·=t;---t3 ADD Rl, RO
For the expression (a+b)+(e+(c--d)) MOV RO, t 4
A DAG can be constructed for the above sequence as follows.
8.12.2 Heuristic Ordering
The heurbhc l)rder~ al~t)ritlun is as fd!t)WS..
1) Oht.un Jll the intt-ri..,r n,x!~ Cl'n.-ider tht'::'e interior nodes as unlisted
'
interior n,X1t'S.
I ~) whilt' (unli:,tt,i i.ntt'rk,r ""le; re-main)
-1) p1d;. \t}' ,\I\ unh::-tt,i m,k I\, ,~ h,'-.'<.' 'f'lrent::- h.1n.• ~"t!n listed.
4) hst n;
Fig. 8.11 DAG fc,r (atb) i (ot(c d))
••
11 hi$ unlltl\t'd pprmt AND is not leaf
l ~345681
As now onl 8 IS remammg from the anJisted inferkJr nacfe •
!ll1lnt' \'ffl nprt•ssmn
Hence the resulting list JS I 2 4 5 6 8 Then the order of c:1emnni;P1111ii1111•.i
wn in lhl' foll \\-Ing Fill 8 12.
bv mersmg this list We get the order of !?Valuation as IJ & 5 4 3 2
that we have to perform the computations at these nodes m the pa anfet.
Is .= d e
~ =a-t,
l.i=½'ts
t =~,-"
I - t6 • !.i
I ~•~
0
{e Fg 115
A~,:lrith:n
'\ e " fi.~-t ..il.."'---us::' the imr~~rtant n,ltaboru used in this alsonthm
1 The function Gen_code n) i:, u:-cd that produce" the code to c, aluatc a node
labelled as n n,
:.. Fa;- the ref-Ster allocabon a stack 1s U!'ed called 'Rc~Stack'. InitiaU) this
stad co.,taJ..-.s all the a,·ailat,le rcpstcrs RO, Rl, ... Rk :such that the reg1ster RO
1• en the top of the stack
3. The Ge.,.,__code n) e,-aluatcs n m the register which is on the top ot the stack
4 Another stack is used to ,tore the temporaries called 'Temp_stad. it contains Fig 8 16
all the temporanes TO, Tl .Tk ha, m~ TO on the top of the stack
5 The function s\vap(Reg_!:-itack) is used to swap top two registers on the stack.
6 The furu:bon Pnnt is used to concatenate U1l' argum.:-nts m the order in which
they ar amung The 1s used to indicate the concatenation. In this function
the non-quot<.>d arguments nl"ed to be .:-valuated first. Case 4.
7 ThL' function GeJLoode 1s a funcbon that generates the code. This function then,
of vanous ca Let u cons1d<!r th,•se cases.
~
:Prtm( MOV I 1,,amit 11 ', • tOJJ(Reg Sta It))
Ben the node n i na1111:cl h) «in 1d1 nlih r
11111/J/ ·n J ll m, a11 lo cJ
ii~t
li) If no precedence relation holds between the terminal on top
of the stack and the
current input.
(ii) If a handle has been found, but there is no production
with this handle as a right
side.
: Objectiv e Type Questio ns ode Opt imi zati o~- ---- -..-- --
1. Any description error can be repaired by
(i) Insertion alone
(iii) Insertion and deletion alone
(ii) Deletion alone
(iv) Replaceme nt alone
2. _ _ _ _ sequence of characters that does not match the
and Gen era tion
pattern of any token:
(i) Lexical Error (ii) Syntax Error INSIDE THIS CHAPTER
(iii) Semantic Error (iv) Parsing Error 13.1. Introductio n to Code Optimizatio n 13.2. Optimizatio
n Strategy 13.3. Loop Optimizati on
3. Scope and declaration errors are _ _ _ _ errors. 13.4. Global Data-Flow Analysis 13.5. Elimination of Induction
Variables 13.6. Elimination of Common
Subexpress ion 13.7. Loop Unrolling 13.8. Introductio
(i) Run time (ii) Compile Time n to Code Generation 13.9. Machine
Architecture currently in Use 13.10. Major :ssues of Code
(iii) Both (i) and (ii) Generation 13.11. A Machine Model and
(iv) None Code Generation 13.12. A Simple Code Generator 13.13.
Code Generation From DAG 13.14. Register
4. _ _ _ _ are errors committed by programme rs during Allocation 13.15. Dynamic Programmin g (DP) 13.16.
coding process. Peephole Optimizatio n
(i) Lexical Error (ii) Syntax Error
(iii) Semantic Em;,r (iv) Parsing Error 13.1. INTRODUCTION TO CODE OPTIMIZATION
s. _ _ _ are errors take placed during declaration and scoping.
Due to the cost factor of the software, now-a-da ys emph::>.sis
(i) Lexical Error (ii) Syntax Error - has shifted from the
construct ion of efficient programs to that of more natural
(iii) Semantic Error (iv) Parsing Error or more 'conveni ent'
programs .
( Answer s J The process of optimizat ion is aimed at improvin g the execution
efficienc y of a
program. Optimiza tion aims mainly at rearrang ing the computa
1. (iii) 2. (i) 3. (ii) 4. (ii) 5. (iii) tion in a prograin
so as to gain the advantag e of execution speed, without changing
the meaning of
..
a program .
□□□
"So finally we can say that code optimizati on refers to
the technique used by the
compiler to improve the execution efficiency of the generated
object code_..
I
Optimiza tion may be machine dependen t or may be machine
independ ent_
Machine- independ ent optimizat ion can be performe d independ
ently of che tan?:et
machine for which the compiler is generatin g the code. On
the other hand. machi~e -
dependen t optimizat ion requires knowledg e of target machine
architect ure. ,ve should
know the target machine architectu re so that better utilizatio
n of registers and other
architectu re characteri stics may be possible.
A good optimizer has to expJoit :
(i) Cha~acter is~cs of the ~arget machine in tenns of
the addressin g structure
and mstrucuo n repertoire .
(ii) Character istics of the particular program in tc-rms
of progr.un structure 3lld
nature of computat ion involved.
Optimiza tion may be local and global :
l_n case of loc~1 I ~pti'.niznt ion. Wl' ~-onsider ,)nly sm:tll porti'-")n
of prognun c d
ln this type of op1tm1znt ton 011ly r<:stnl'k'd mn,,\mt 1.lf an:thsis
•
required b 0
e:
• ut gain of
.
405
~ 406--------- - - - Fundamentals a/ Comp/le~ Design~ J- Code Optimization and Generation - - - - - - - - - - - - - - - - - - - - ~ ~
optimization is restricted in thi:- type of optinlll:ahon. On the other hand in global • represent control flow relationships between basic blocks using a O
optimization the scope is not restricted to short code sequences. Thus optimization
flow graph.
oflarge program sections-mcluding entire loops can be performed. While this requires
For example :
considerable analysis of the program structure, its gains are also higher than those
of local optimirntion L1 : if x > y goto '-o
t1 =X+ 1
1.3.2. OPTIMIZATION STRATEGY x= t1
l0 y= 0
Some common steps in optimization may be as follows :
goto l 1
• Examine the program to find out certain properties of interest, it is said to
can be represent as follows :
be Data Slow Analysis.
• Use this mfonnation to change the code in a way that improves performance.
("code optimization"). Examples of optimization include:
(a) Code Motion out of Loops. If a computation inside a loop produces the
same result for all iterations (that is computing the base address of a local array), it
may be possible to move the computation outside the loop. Complete process, we
\,ill study under loop optimization.
(b) Common Sub-expression Flimination. If the same expression is computed
in many places (that is array address computations; result of macro expansion),
compute it once and reuse the result.
(c) Copy Propagation. If we have an intermediate code "copy" instructio
'x: y'. replace subsequent uses of xbyy (where possible).
(d) Dead Code Elimination. Delete instruction whose results are not used.
Fig. 13.1.
13.3. LOOP OPTIMIZATION
. I. Representation of Three-address Code in to B .
Loop optimization is most valuable machine-independent optimization becaus JS a sequence of consecutive instruction such that . asJc Bloclts. .-\ basic block
a program's inner loop are good candidate for improvement. I. Control enters at the beginnino- ·
The execution efficiency of a loop can be optimized by moving out some cod 2. Control leaves at the end; and,,,.
which is loop-invarienL Such code would now be executed just once prior to loo
3. c_ontro! cannot halt or branch except ac the end.
entry, and not repeatedly during execution of the loop. For example, in
IdentificatJon of Basic Block can done b"· foll .
while (i<Jl Step J D • • seeps ·
{ . etemune the set of Leaders th . th01!1.'ID!?
..:.:. - .
block : · ar JS e uut IDStroction o f ' ~ basic
a=X*2-Z*2;
(a) The :"'st ins_truction of the function is a header.
(b) Any mstruction that is the t~et f b .
The evaluation of (x * 2 - z * 2) can be moved out of the loop provided it i and unconditional goto) .,,. o a tr:lllch is a header(~ of '-"'C!Ddi.tiooal
invariant. This kind of code movement also called Frequency Reductio (c) Any instmction immedi.11eh folio\\ in,.. a ( • .
transformation. a branch is n header. - .:- .. ondition:il orunC'Qaditioo.d sroto)
The major tasks in loop optimization are : Step 2. For ea ..·h ht'ader. Hs t>as1 ..• t>1 ·k .. . -. -
(i) Detecting loops in a program. upro, but not indudi,1g. the fit''\t h 'Jd • o.: ..-o~.sJ.sts N tt.self md all instruction
L' . < < r (or end of function).
(it) Deducing the feasibility of moving certain code out of lhe loop. .--or e'\nmplt• lc>t us <-',)n,-i..kr th<" f)ll .
Up '"' ' l l)\\1n,_g pn,gr.uu h':lgme.nt •
13.3.1. Analysis of Program for Loop Optimization
(
For program analy~i1; and loop oplimizalwn, it is u~uully neccssury, lo !,.mi lntP= l;
control now relation~hip~ be1ween cJiffcrenl pieceR of wcJc for (/ • 2 ; /, • , ; /++)
For this we: p•p • I
• gmup three-addre~" 1r1Nlruc111,n "' 11, Um1k UlockH. r•turn (p).
)
I- Code Optimization and Generation
- Fundamentals of Compiler Design
1I Step 2. There is a d" - - - - - - - - - - - - - 409 -I
42 6 uiree-nddress-code for above code fragment · f II (a) There is a (cond:;tocteld edge from block B, to block 82 if
\- so\'',,.- . lS as O OWS : na or unconditional) ju
of B, to the first instruction of B . or
f h
mp rom t e last instruction
(l)P::::l. 2
(2) ; == 2 (b) B
d 2 immediately
. f 0 IJ ows B in
· the ' textual order of the program and B
I 1
=x
(3) if i
(4) p = p * i
< goto (8)
foll:::. oes not end in an unconditional jump. •
example th e control flow graph for the basic blocks in Fig. 13.3 is as
(5) 11 = i + l
a,
(6) i = t\
(7) goto (3)
(8) goto caller program Bz
Here header statement are :
(i) statement 1, because it is first instruction.
(ii) instruction 3, since it is target of goto. a,
(iii) instruction 4, since it follows a conditional goto.
(iv) instruction 8 , since it is target of conditional goto statement.
a.
Now it is simple to find the basic blocks :
• Block B, has instructions 1 and 2. Fig. 13.3.
• Block B 2 has instruction 3. 3. Identifying the Dominators. A node a is a dominator of node b if every path
• Block B has instructions from 4 to 7 . from the initial node of the flow graph to b goes through a.
3
Dommators (dom) relationship have the following properties :
• Block B 4 has instruction 8.
(i) There are reflexive; that is every node is domioatorS of itself.
(ii) Transitivity holds that is, if a dom b and b dom c this implies a dom c.
a, : p = 1;
i=2 For example for the following given flow graph :
Fig. 13.2-
I- 4 1 0 - - - - - - - - - - - - from the initial node _of G .l f d es whose beads dominates their tails.
Dommators tree can be shown as follows : (ii) The back edges consist on Y o e g
Let us consider the followfog flow graph :
Fig. 13.5.
4. Back Edges Identification. AIL those edges are called Back Edges, whose
heads dominate their tails.
13.3.2. Loop Detection
Now after previous analysis, it is to detect a loop in a flow graph. A loop is a
Fig. 13.6.
cycle in the flow graph that satisfies two properties :
l. A loop must have a single entry point, called the "header". This entry point Here 4 ➔ 3, 7 ➔ 4, 8 ➔ 3 9 ➔ I and 10 7 are back edges., whose heads
dominates their tail. ' -
dominates all nodes in the loop, it makes possible to move all of the loop
invariant computations to a unique place, called a "preheader", which is a . Now let us remove the back edges from the .
block/node placed outside the loop, just infrout of the header. gives us reduced flow graph. flow graph shown m Fig. 13.6, it
2. There must be at least one way to iterate the loop, that is one path back to Now new flow graph is as follows :
the header.
For example consider the flow graph given in Fig. 13.4. In which
4 dom 7, 7 dom 10, 3 dom 4 , 3 dom 8 and 1 dom 9.
Now it is easy to detect the loop. "If a given back edge is a ➔ d, we define the
natural loop of tte edge to be d plus the set of nodes that can reach a without going
through d. Node d is the header of the loop".
So clearly the natural loop of the edge 10 ➔ 7 consists of nodes 7, 8 and l 0,
since 8 and 10 are all those nodes that can reach 10 without going through 7. The
natural loop of 9 ➔ I is the entire flow graph. Fig. 13.7. A non-n-ducible flow graph
13.3.4. Pre-Headers
13.3.3. Reducible Flow Graph
_ It is as~umed that code optimization ·operations can be easy ly performed by To more statements "bet:
~rehender. The preheader haso:U~e header node" we de.fine a ne\lo header
~smg red~~•~I~ now graph. A flow graph G is reducible if we can partition the edges __,.,
) the header as su.:ce-s.sor• all the ..-...ges
header through prehead called
mto two d1 sJomt groups, often called the forward edges a nd back edges with the • er. ~ enters in
following two properties : '
I.
ig. 3.8 :-howi- a flow o .
c,raph w1th loop : entals of compller Design l I- Code Opt/m Iration and Generation
fla1: • true
{hlle lfla1) do
fla1., false
~or each block 8 do
}.
Preheader Now let us consider an ex
flow graph : ample to clear the concept. Consider the following
Fig. 13.9.
. I
I- 414 - - - - - - -- -
- - - - - - - Fun dam I- Code Optimization and Generation
Step 2. Now let us apply
the itrati ve meth od to solve
ental s a/ Camp i/er Desig
n -I Here In [BJ beco mes to
Out [BJ, so we mus ~ slop
't ratio n here
the data- flow equa tions . I e <
lntia lly ln [B] = <I> and out . f U e Defi nHi on Cha ins •
[B] = gen [B] Step 3. Com putm g o . s - :
So : . n as "Us e-de finit ion cha ins" or "ud- chai
Block In It is easy to store the info ns",
Out whic h are lisls, for each rma u? fall the defi nitio n that reac
B, use of a van able , o h that use.
<I> ll, 21
B~ ,t, l3, 4) We can calc ulate ud-c hain
8.1
as follo ~s : ded b no unam bigi ous
<I> (S) (i) If a use of varia ble defi nitio n of
n. a in bloc k B isfpr o_ce h e~
<I> l6. 7) a then the ud-c hain for thal use o a is t e s of defi nitio n in In[B J
that are
B, <I> (8, 9)
B• <I> (10, 11)
d~fin ition of a.
(ii) If the use of a in bloc . . h
ll1D k B is proc eede d by its
a cont ains only the defin ition defi nitio n, t en the ud chai n of
-
l $,,
lntlol ly, we take In [BJ prio r to this use of a .
for every block that is
robe gen (BJ, ond we comp to be an empt y set, and
we take out [BJ For exam ple, cons ider flow
ute In new [BJ. If it is differ grap h is Fig. 13.1 0.
.:-ut [BJ and more to ne,ct ent from In [BJ, we comp
ute a new The use of a in defin ition . .
Iterat ion, accor ding to
algor ithm discu ssed in 4, bloc k B is prec eded
previ ous sectio n. defin ition of a. Hen ce ud-c 2 by defi nitio n 3, whic h
nft<' r first itcrn tion In hain for this use of a cont IS the
nnd out valu es are : use of b in B is not prec ains only defi njtio n 3.
eded by any defin ition of But the
Uln~k 2 b in B 2 , so ud-c haio for
In Out of b will be { 13, whic h is this use
u, the onJy defin ition of bin
<I> Step 4. Dete ction of Loo In[B ]J.
n, { l , 2 , 6 , 7}
ll, 21
(1,2, 3 . 4 , 6,71
p-In vari ant Com puta tion
s :
11 , By the use of ud-c bain
(3 , 4 , 8, 9}
(3, s. 91
, we can dete ct the loop
Supp ose a := b + c js at a -inv arian t.
"·n,
B,
{3. 4 , SJ
(SJ
l3. 4 , s. 6 , 71
(8 . 9}
and c are outs ide the loop
posi tion in the loop whe
(incl udin g the spec ial case
re alll poss ible defi nitio
n of b
( 6, 7} (7, 10. 11} then the calcu latio n of b whe re b and/ or c are cons
+ c will be the sam e each tant ),
.\fl:cr seco nd itera tion ln and inva rian t in the loop . tjme enco unte red, so jt
Out value s are : is loop -
Block
Step 5. Cod e Mot io'l (Mo
In ving loop -inv aria nt code
Out mov e loop -jnva riant outs ide out o-f the loop ) : We
the loop if follo wing cond can
<I> (1 , 2} (a) The bloc k in whic ition s are met :
l 1, 2, 3, 4 , s. 6 . 7 J h a loop inva riant state men
{l . 2,3 , 4 , 6,7] - of all exist s of the loop , t occu rs shou ld be a dom inat or
ll , 2, 3, 4 , 6, 7 . 8, 9 ] or the nam e assig ned to
{l , 2 , 3 . 5 , 6 . 7 . 9} used outsi de the loop . the bloc k shou ld not be
( l. 2 , 3 , 4 , 5, 6 , 7 , 9 ] .
{ l , 3 , 4, S. 6 , 7}
{3. 5 , 9] (b) We c3:nn ot mov e
{3 , 8 , 9} a loop -inva riant state men
(3 , 4, s, 6 . 7}
{3, 4. s, 7 . 10, l1 J there ts anot her state men t assig ned to a into preh
t in the loop that assig n eade r if'
Afte r third itera tion In and For exam ple, cons ider the to a.
out valu" s are as follo ws : follo wing flow grap h ,
Block In
Out
<I> {1 . 2)
(1 , 2, 3 , 4 , 5 , 6 , 7}
lJ , 2, 3, 4. 6 , 71
{l, 2 , 3 , 4 , 5 , 6, 7, 8 , 9]
{l , 2 , 3, 5 , 6, 7 , 9]
l 1 , 2 , 3 . 4 , 5. 6, 7, 9J
ll, 3, 4 , s. 6 , 71
{1 , 2 , 3 , 5 , 6 , 7. 9)
{1 , 2. 3 , 6, 8 . 9}
{l, 3, 4, 5 , 6 , 71 {1, 3, 4 , s. 7, 10, 11}
Afte r Four th Itera tion In and
Out value s are as follo ws
:
Block In
Out
<I> {l,2}
( 1, 2 , 3 , 4 , s. 6,'7)
{l. 2, 3, 4, 6, 7}
{l , 2, 3 , 4, 6 , 7 , 8 , 9]
(1,2, 3,5 , 6,7,9 }
{1 , 2, 3 , 4 , 5, 6, 7. 9}
(1 , 3 , 4, 5 , 6, 7}
(1 , 2 , 3 . 5, 6 , 7, 9} {l , 2, 3, 6 , 8, 9}
{1.3 ,4,5 , 6,7} { 1, 3, 4 , 5, 7, 10, 11}
Fig. 13.11 .
Fundamentals of c I
flcre a =4in B cannot be moved outside .
the I . .
ompller Design -I
rrunator
.
2
o f other block of loop · si·nce movmg . . to th
it
oop msplte of that B 2 1s • f- Code Optimlzatfon and Genercrtion
We note that the purpose of_I_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 417.J
fie meanmg ·of the program Because •i f a _- 4 1s . e preheader will chang e
moved Of
,alue that "v1ll be assigned to •v in B 5 Wl. 11 b e 3 1f . the out · the loop, then th e purpose of T, is to step through th JS to count from 1 to. 20 in the loop • while th e
e arrays four bytes at
fo b /
.,.
Tb~; 4
execution path is B1 - B2 - . ur ytes word. At the assignment T __ a tune, since we are assuming
83 - B~ - B2 - B4 - B5 whereas for same exec ti
assign~ toy m B • u on path, the original program time through the beginning of loop. T t ~ ~ on the values I. 2, ...• 20 each
5 after ~ach _assignment to T, . That is both ,'
and ; ;evaJues 4, _8, •.. , ~O immediately
(i) The move is illegal if g_ is used in the loo and . such ,denttfiers are called induction variables" ' form anthmatJc progression.
of g_ other than the statement to be moved. p g_ is reached by any definillon
As the relationship T, = 4 * I surely holds aft th .
For example. consider the following flow-graph. not ~banged elsewhere in the loop, it follows the:~assignment to T,. and Ti is
relat10nsbip T, = 4 * I - 4 must hold. a r stalemenc I := / + I the
X= T+5
Fig. 13.12. Above optimization is possible only when. none of the state=ts in.c..·•rvenina
between the evaluation of B .,_. C and its use in the computation of X changes th;
Here a= 2 (from Bi) cannot be moved out since use of a in B 4 is also reached
value of B ** C. -
• the
bY definition
Note that, if all 1 in Bi-
a=above discussed condition are met then we can move out loop- 13.6.1. Elimination of Local Common Subexpression
in',;aria:nt code, from the loop. Local common subexpression can be eliminating by Dag Constroetion-
_ _ _ Let us define a function node (id), ..,.inch return the most recentl,; crear.ed node
U.S. ELIMINATION OF INDUCTION VARIABL~ associated with its id. For every three address statement.>:=; OP::.:,:·= OP y. or:,:=
We can decrease total number of instruction and §eedup the loop by elimJnaung
y in the block, we have co follow the following steps :
the induction variable. . · ent within Step 1. If node (y) is undefined. create a leaf labelled y. and let node (...·) be this
"'Induction variables of a loop are th0 ~e names w:ose : : : : ~ ~ :value does node. If node (z) is undefined. create a leaf labelli:d ::. and let tbar le:n be node(;:). If
the loop are of the form I = I± C, where C is a constan or a the statement is of the form .T = OP y or x = y. then if node C...·) is tmdefu:.ed. creares a
not change within the loop." leaf labelled y, and lee node (y) be the node.
~
T := T4 [T1 ] •
5
T6 := T3 • Ts Let us consu.lcr an e-...;1111ple to make the c-,n.:ept -:1--.i.r. ~-onsxder the roUowmg
PROD := PROD+ T,
/:= /+ 1
1he block of code
(I) S 1 ·= -1 • I
If / S 20 goto 82
Fig. 13.13.
l--414---- . . ation and Generation D for complete code. as fo
I- code opt1m1z we can find the ag
I- 418
(2) 5 2 := addr (A) - 4
(3) S 3 := S 2 [S 1]
(4) S4 := 4 * I 5
(5) s,:= addr (B) - 4
(6) S 6 := S 5 [S4 )
(7) S1 := S3 * S6 S9, J 20
(8) 5 8 := PROD + S 7
(9) PROD := S• PRODO
(I 0) S9 := I + 1
(11) I= S 9
(12) if/<=20got0{1) Fig. 13.14(d).
Now Jet us construct the Dag for above code step-by-step :
1. Very first instruction ( 1) is s, := 4 * I, we will create leaves n,. and n 2 lab_elled
From the Fig. 13.14(d), it is clear that PROD O indicates the initial value of ·
4 an / then a node n labelled * and we also attach an idenufier S, to 1t : PROD and / 0 indicates the initial value of I. S 1 and S 4 are same when their value are
3
0
computed, same value is assigned to S 8 and PROD, so we can eliminate these common
subexpression by selecting one of the attached identifiers (one that is needed outside
the loop). Here we assume that none of the tempories is needed outside the block.
Finally block will be :
(I) S, := 4 * I
(2) S 2 := addr (A) - 4
Fig. 13.14(a)
(3) SJ := S 2 [S,J
(4) S 5 := addr (B) - 4
2. Now for S 2 := add (A) - 4 in same manner.
(5) S 6 := S 5 [S,]
S2 s, (6) S 7 := SJ * S6
(7) PROD := PROD + s7
(8) I:= I+ I
n, (9) if I~ 20 goto (1)
addr(A) 13. 7. LOOP UNROLLING
Fig. 13.14(b). If number of iterations are constant then we
3. Figure 13.14(c) represents the Dag for S 3 := S 2 [S 1] of the loop to reduce the required number of test::no::~u~~sthe replicating the body
For example, consider the following loop: p called loop unco1Jing.
= [] I= 1
while (I <= 100)
{
S2 x{I) =O;
I:= I+ 1;
~
1;
I: .. I .... -•lack I rr,nl(c "' mldr"••ing
(l,J A ~mu~umc• etc, mr,dc• lo •u - --
xCIJ = O;
I:= I+ 1
(q M,,. . "''"'""'"'re
indc~u;i.C<:tal purp11scg i ;•e~• (usually "~"'~""'"'"'
16 or I ys, records,42'
lists,
(d) 'l'wo-add;:~. , cg1S1er•, for cxampl::~-,
l11L·n rJ1e number of times this t est will
INTRODUCT ION TO C
(<) ~Ore""'"· '""'""'""''""'"A ""'°" m,, ""=~"' Im
,1.3.S. need to be petformed
. . ,inablc len th . + /J -A, where A
I< ;, the las<h ODE GENER woll be 50 (/) Instruct ions :,it~n•_truction. • B may be complex
dependent p,o=s p ase of compile, d . ATION ?) Widely differen •ide effect•. for ex
h) Control i t execution tim ample aulo-incrc
;;,ie. We mus< be ve,; c"''"i of machine "~~~c"',tioo, is mac hi"'
to generate goods so without knowing tJ~st!n process. Code 2. RISC (R mplemented by micr cs for instruclJOns. mcnt instructions.
selection since a
rnugh algolithm ,c,ent algolithm may bMe "." aboot code g =to,e, ii isd iffico lt characteristics. educed Instruction ;:;ogam.
Let us see pie< . e tw,ce as fast as co':i:''""o" algo,i•hm (a) Simply addressin omputer) : RISC• have the .
:ures view of code . generated by an (b) Many re . g modes usual! _ followmg
(c) All regis~~::e:, at least 32. y only mvolvmg registers.
Intermediate generallon phase :
(d) Three-address :;teneral purpose.
code
(e) Fixed length instruru~tJons but only involvin
I
(f) One result p . ctJon (32 bits). g registers that is r =r +
Code
!
'-
optimizer 1
I
(g) Bae, iostroC::o:";:;'ctioo ooly. , ' ,,
( h ) Control is hard w·ire_,,
:s arather
similar length
than . of time to execute.
/,/F- --- , 13. 10. MAJOR ISSUES O nucroprngrammed.
• loStrnCtion selection
: Register Utilization (Allocation)
Fig. 13.15. 1 Storage allocation.
. Memory
symbol-tab~e Management Duri
A_ good generation must do following things :
enterie~ have to be.ma
mapped•• m,=ction addre=· p
pedng code generation process references
macOWP......,..,.,....,-m::
~
(i) Produce correct code
_(ii) Make use of machine architecture Mapping names in the source ro
pa,s I (Floo, ,od) and p= 2(co.l'e :.=:.,;;""""
of da<a - - m
(iii) Run efficiently. Local vanables (local to functions .•
• Input to code generator : ,ctioarioo reconl while glootl -,,ri;obt~:::. ~ : . -.. . - mm,
(i)
(ii)
•
Intermediate code program
Symbol table
Output of code generatur : is target program
(b) Some instn1ctions tthat is MUL- DIV) may oeed pairs~~ md taB
-
-
.
=-
(i) ,Assembly language program se,eral ?cles to complete ltbat is.:an·t~tb-.--.se~forome£purposes).
(ii) Absolute machine-language program. tc) Ad~ssmg mode tFC-relati,-e vs.. absolute). sp,111 (soort VS- Iong).
(iii) Relocatable rnachine-Janguage prograrn - is regular lhal is UDifanD (all
Instruction sde.:tion is ··easy·· if ;nsauctiOll set
:1,3.9. ""ACHINE ARCHITEcTURE CURRENTLY Iii USE ttipl< oddl<s<<' ,UI s<,ok ,i...1< ,,,,...,,, "°" ,,.,..,,_ (""' of - .. -,
aefo« considering oth<' i,sues, we should took at the two main types of machine
1
operation)_
l cisC
a,rchitectu«' (Comple•
curren<IY in Instruction
use Set computer), c1sc, "" designed w,< . b;'~
· · f n·gh Jevel Janguages in mind and wou ld 1hen,fo1e seem, th<' "
3) picl. up unlistt'l.i n,-.Je
~
.in
4) list n,; n. r,irents have been
;,nple,nent.auon
candid• o for
te as u,rge< • com
- .
pile<' . ,yp,cal . .,cs.
characte"" "'° .. . \\h,..~
j
possible, a node immediately follows the evaluation of its left-most operand . /6 =q+r
Algorithm for Ordering Nodes of Dag : This algorithm lists the nodes of a t5 = t6 + s
Dag such that the node's reverse listing res•Jlts in the computation order. 14 = p + t6
Begin
while there exists an unlisted interior node g.Q t3 = 13. + t5
lz = I.: - t5
!e1ect an unlisted node n whose parent have been listed
tl = tz - f3
!~i~ tnere exists a left-most child m of n that
not a leaf do
has no parent listed anclm is
13.2.4. REGISTER ALLOCATION ,.1
listm
m=n
Registers are a finite resource. Sources and targets for many instractions
modern RISC like architectures. Code generation assmnes an unbounded number of
registe~s to simplify matters. In register allocation we map anbounded number to
00
j
order = reverse of the order of listing of nodes the firute set. Key to this is knowing whether a value within a register is still needed.
End
. "Register allocation is the process of assigning a large number of target prognu;.n
Let us see an example, consider the given Dag in Fig. 13.16. vanables onto a small number of CPU re!!isters.. -
It is the process of compiler back end as foll~ws:
En'Ols
Fig. 13.16.