A Simpl Co Gnrtor Co Gnrtion Chptr 8 II Gnrt o for singl si lok How to us rgistrs? In most mhin rhitturs, som or ll of th oprnsmust in rgistrs Rgistrs mk goo tmporris Hol vlus tht r omput in on si lok n us in othr loks Oftn us with run tim storg mngmnt 1 2 Rgistr n Arss Dsriptors For h vill rgistr, rgistr sriptor (RD) kps trk of th vrs whos urrnt vlu in tht rgistr Initilly mpty For h vr, n rss sriptor (AD) kps trk of th lotions whr th urrnt vlu of th vr n foun Lotion n rgistr, mmory rss, t. Co gnrtion Algorithm For thr rss instrution,.g. x=yz Us gtrg(x=yz) to slt rgistrs R x, R y, R z for x, y, z If y is not in R y, issu n instrution LD R y, y Similrly for x Issu th instrution ADD R R R Issu th instrution ADD R x, R y, R z Copy sttmnt x=y If y is not lry in rgistr, gnrt LD R y, y Ajust RD for R y so it inlus x Chng AD for x so its only lotion is R y Ening th si lok If x is us t othr loks, issu ST x, R x 3 4 Mnging Rgistr n Arss Dsriptors For LD R, x Chng RD for R so it hols only x Chng AD for x y ing R s n itionl lotion For ST x, R Chng AD for x to inlu its own mmory lotion For ADD R x, R y, R z Chng RD for R x so it hols only x Chng AD for x so its only lotion is R x Rmov R x from th AD of ny vr othr thn x Exmpl t = u = v = t u = = v u t, u, v r tmp vrs, whil,,, r glol Assum rgistrs r nough Rus rgistrs whnvr possil 5 6 1
t = u = Exmpl LD R1, ; LD R2, ; SUB R2, R1, R2 R1 R2 R3 t u v t,r1 R2 LD R3, ; SUB R1, R1, R3 u t,r3 R2 R1 v = t u = ADD R3, R2, R1 u t v R2 R1 R3 LD R2, u, v R2,R2 R1 R3 7 8 Funtion gtrg = v u ADD R1, R3, R1 v R2 R1 R3 ST, R2; ST, R1 v,r2,r1 R3 Consir piking R y for y in x = y z If y in rgistr, o nothing If y not in rgistr n thr is mpty on, hoos it s R y Lt v on of th vr in R W r OK if v is somwhr sis R W r OK if v is x W r OK if v is not us ltr Spill: ST v, R 9 10 Pphol Optimiztion Exm sliing winow n rpl instrution squn with shortr or fstr squn Runnt instrution limintion Flow of ontrol of optimiztion Algri simplifitions Us mhin iioms Eliminting Runnt Los n Stors LD, R0 ST R0, 11 12 2
Eliminting Unrhl Co Flow of Control Optimiztions if ug==1 goto L1 goto L2 L1: print ugging info if ug!=1 goto L2 L1: print ugging info goto L1 goto L2 L1: goto L2 L1: goto L2 if < goto L1 L1: goto L2 if < goto L2 L1: goto L2 13 Chptr 6: Intrmit Co 14 Gnrtion Rgistr Allotion n Assignmnt Exmpl Usg Counts An pproximt formul for th nfit from lloting rgistr for x loksbinl us ( x, B ) 2* liv ( x, B ) us(x,b) is th numr of tims x is us in B prior to ny finition of x liv(x,b) is 1 if x is liv on xit from B n is ssign vlu in B, 0 othrwis 15 16 Instrution Sltion y Tr Rwriting Instrution sltion n lrg omintionl tsk Evn th vlution orr is givn n rgistr llotion hs n on [i]=1 Tr Trnsltion Shms in in Rsp i = Rsp M C 1 LD R0, # ADD R0, R0, SP ADD R0, R0,i(SP) LD R1, INC R1 ST *R0, R1 17 18 3
Optiml Co Gnrtion for Exprssions W n hoos rgistrs optimlly If si lok onsists of singl xprssion, or It is suffiint to gnrt o for lok on xprssion t tim Ershov Numrs Assign th nos of n xprssion tr numr tht tlls how mny rgistrs n Ll lf 1 Th ll of n intrior no with on hil is th ll of its hil Th ll of n intrior no with two hilrn th lrgr on if th lls r iffrnt On plus th ll if th lls r th sm 19 20 ( )*() Ershov Numrs = = = * = Gnrting Co From Ll Exprssion Tr Rursiv lgorithm strting t th root Ll k mns k rgistrs will us R, R 1,, R k 1, whr >=1 is s To gnrt mhin o for no with ll k n two hilrn with qul lls Rursivly gnrt o for th right hil, using s 1: R 1, R 2,, Rsult in R k 1 Rursivly gnrt o for th right hil, using s : R, R 1,, Rsult in R k 2 Gnrt instrution OP R k 1, R k 2,R k 1 21 22 Gnrting Co From Ll Exprssion Tr To gnrt mhin o for no with ll k n two hilrn with unqul lls Rursivly gnrt o for th hil with ll k, using s : R, R 1 1,, Rsult in R k1 k 1 Rursivly gnrt o for th hil with ll m, using s : R, R 1,, Rsult in R m 1 Gnrt instrution OP R k 1, R m 1,R k 1 For lf x, if s is gnrt LD R, x LD R3, LD R2, ADD R3, R2, R3 LD R2, MUL R3, R2, R3 Ershov Numrs LD R2, LD R1, SUB R2, R1, R2 ADD R3, R2, R3 23 24 4
Insuffiint Supply of Rgistrs Input: ll tr n numr r of rgistrs For no N with t lst on hil ll r or grtr rursivly gnrt o for th ig hil with =1. Th rsult will ppr in R r Gnrt mhin instrution ST t k, R r If th littl hil hs ll r or grtr, =1. If th ll is j<r, thn =r j. Th rsult in R r Gnrt th instrution LD R r 1, t k Gnrt OP R r, R r, R r 1 or OP R r, R r 1, R r 25 Insuffiint Supply of Rgistrs LD R2, LD R1, ADD R2, R1, R2 LD R1, MUL R2, R1, R2 ST, R2 26 LD R2, LD R1, SUB R2, R1, R2 LD R1, ADD R2, R2, R1 5