Rgistr Allotion Xio Ji April 25 th, 2012 1
Outlin Introution Grph oloring Linr sn 2
Introution Wnt to rpl tmporry vrils with som fix st of rgistrs 3
Introution Wnt to rpl tmporry vrils with som fix st of rgistrs W will jug this phs y Co rviw Limiting # of xut instrutions,.g. no grtr thn 1 million Compr: only 160,000 for my optimiz 8-qun 4
Grph oloring First: n to know whih vrils r liv ftr h instrution Two simultnously liv vrils nnot llot to th sm rgistr 5
Grph oloring For vry no n in CFG, w hv out[n] St of tmporris liv out of n Two vrils intrfr if Control Flow Grph oth initilly liv (i.. funtion rgumnts), or oth ppr in out[n] for ny n, or on is fin n th othr is in out[n] x = - whr x is & is liv intrfr How to ssign rgistrs to vrils? 6
Intrfrn grph Nos of th grph = vrils Egs onnt vrils tht intrfr with on nothr Nos will ssign olor orrsponing to th rgistr ssign to th vril Two olors n t nxt to on nothr in th grph 7
Intrfrn grph Instrutions Liv vrs = + 2 = * = + 1 rturn * 8
Intrfrn grph Instrutions Liv vrs = + 2 = * = + 1 rturn *, 9
Intrfrn grph Instrutions Liv vrs = + 2 = * = + 1 rturn *,, 10
Intrfrn grph Instrutions = + 2 = * = + 1 rturn * Liv vrs,,, 11
Intrfrn grph Instrutions = + 2 = * = + 1 rturn * Liv vrs,,, 12
Intrfrn grph olor rgistr Instrutions = + 2 = * = + 1 rturn * Liv vrs,,, $t1 $t2 13
Intrfrn grph olor rgistr Instrutions = + 2 = * = + 1 rturn * Liv vrs,,, $t1 $t2 14
Grph oloring Qustions: Cn w ffiintly fin oloring of th grph whnvr possil? Cn w ffiintly fin th optimum oloring of th grph? Wht o w o whn thr rn t nough olors (rgistrs) to olor th grph? 15
Coloring grph Kmp s lgorithm [1879] for fining K- oloring of grph Stp 1 (simplify): fin no with t most K-1 gs n ut it out of th grph. (Rmmr this no on stk for ltr stgs.) 16
Coloring grph On oloring is foun for th simplr grph, w n lwys olor th no w sv on th stk Stp 2 (olor): whn th simplifi sugrph hs n olor, k th no on th top of th stk n ssign it olor not tkn y on of th jnt nos 17
Coloring olor rgistr $t1 $t2 stk: 18
Coloring olor rgistr $t1 $t2 stk: 19
Coloring olor rgistr $t1 $t2 stk: 20
Coloring olor rgistr $t1 $t2 stk: 21
Coloring olor rgistr $t1 $t2 stk: 22
Coloring olor rgistr $t1 $t2 stk: 23
Coloring olor rgistr $t1 $t2 stk: 24
Coloring olor rgistr $t1 $t2 stk: 25
Coloring olor rgistr $t1 $t2 stk: 26
Coloring olor rgistr $t1 $t2 stk: 27
Coloring olor rgistr $t1 $t2 stk: 28
Filur If th grph nnot olor, it will vntully simplifi to grph in whih vry no hs t lst K nighors Somtims, th grph is still K-olorl! Fining K-oloring in ll situtions is n NP-omplt prolm W will hv to pproximt to mk rgistr llotors fst nough 29
Coloring olor rgistr $t1 $t2 stk: 30
Coloring olor rgistr $t1 $t2 stk: ll nos hv 2 nighours! 31
Coloring olor rgistr $t1 $t2 stk: 32
Coloring olor rgistr $t1 $t2 stk: 33
Coloring olor rgistr $t1 $t2 stk: 34
Coloring olor rgistr $t1 $t2 stk: 35
Coloring olor rgistr $t1 $t2 stk: 36
Coloring olor rgistr $t1 $t2 stk: 37
Coloring olor rgistr $t1 $t2 stk: W got luky! 38
Coloring olor rgistr $t1 $t2 Som grphs n t olor in K olors: stk: 39
Coloring olor rgistr $t1 $t2 Som grphs n t olor in K olors: stk: 40
Coloring olor rgistr $t1 $t2 Som grphs n t olor in K olors: stk: 41
Coloring olor rgistr $t1 $t2 Som grphs n t olor in K olors: stk: no olors lft for! 42
Spilling Stp 3 (spilling): on ll nos hv K or mor nighors, pik no for spilling Storg on th stk Thr r mny huristis tht n us to pik no not in n innr loop 43
Spilling o W n to gnrt xtr instrutions to lo vrils from stk n stor thm Ths instrutions us rgistrs thmslvs. Wht to o? Stupi pproh: lwys kp xtr rgistrs hny for shuffling t in n out: wht wst! Bttr pproh:? Dit rgistrs 44
Spilling o W n to gnrt xtr instrutions to lo vrils from stk n stor thm Ths instrutions us rgistrs thmslvs. Wht to o? Stupi pproh: lwys kp xtr rgistrs hny for shuffling t in n out: wht wst! Bttr pproh: rwrit o introuing nw tmporry; rrun livnss nlysis n rgistr llotion 45
Rwriting o Consir: t1, t1, t2 Suppos t2 is slt for spilling n ssign to stk lotion 24($fp) Introu nw tmporry t3 for just this instrution n rwrit: l t3, 24($fp) t1, t1, t3 Avntg: t3 hs vry short liv rng n is muh lss likly to intrfr. Rrun th lgorithm; fwr vrils will spill 46
Prolor Nos S Tigr ook for mor tils Som vrils r pr-ssign to rgistrs Frm pointr Argumnts ($0, $1, $2, $3) Funtion ll fins (trshs) llr-sv rgistrs Trt ths rgistrs s spil tmporris; for ginning, thm to th grph with thir olors 47
Prolor Nos S Tigr ook for mor tils Cn t simplify grph y rmoving prolor no Prolor nos r th strting point of th oloring pross On simplifi own to olor nos strt ing k th othr nos s for 48
Optimizing Movs Co gnrtion prous lot of xtr mov instrutions mov t1, t2 If w n ssign t1 n t2 to th sm rgistr, w o not hv to xut th mov I: if t1 n t2 r not onnt in th intrfrn grph, w ols into singl vril 49
Colsing Prolm: olsing n inrs th numr of intrfrn gs n mk grph unolorl ols t1 t2 t1/t2 Solution 1 (Briggs): voi rtion of high-gr (>= K) nos Solution 2 (Gorg): n ols with if vry nighour t of : lry intrfrs with, or hs low-gr (< K) 50
Simplify & Cols Stp 1 (simplify): simplify s muh s possil without rmoving nos tht r th sour or stintion of mov (mov-rlt nos) Stp 2 (ols): ols mov-rlt nos provi low-gr no rsults Stp 3 (frz): if nithr stps 1 or 2 pply, frz mov instrution: rgistrs involv r mrk not mov-rlt n try stp 1 gin 51
Ovrll Algorithm Simplify, frz n ols Livnss Mrk possil spills Color & tt tul spills Rwrit o to implmnt tul spills 52
Vritions My r pprs for mor vritions 53
Qustions? 54
Linr sn Givn th liv rngs of vrils in funtion, th lgorithm sns ll th liv rngs in singl pss, lloting rgistrs to vrils in gry fshion. M. Poltto, V. Srkr. Linr sn rgistr llotion. 1999. 55
56
57
58
Exmpl Initilly, tiv is mpty # of vill rgistrs is R = 2 59
Exmpl Stp 1: tiv =< A > # of vill rgistrs is R = 2 60
Exmpl Stp 2: tiv =< A, B > # of vill rgistrs is R = 2 61
Exmpl Stp 3: 3 liv intrvls ovrlp # of vill rgistrs is R = 2 62
Exmpl # of vill rgistrs is R = 2 Stp 3: 3 liv intrvls ovrlp spills C, whos intrvl ns furthst wy from th urrnt point tiv =< A, B > 63
Exmpl Stp 4: A xpirs tiv =< A, B, D > # of vill rgistrs is R = 2 64
Exmpl Stp 5: B xpirs tiv =< B, D, E > # of vill rgistrs is R = 2 65
Exmpl In th n, C is th only vril not llot to rgistr. # of vill rgistrs is R = 2 66
Exmpl In th n, C is th only vril not llot to rgistr. # of vill rgistrs is R = 2 Othrwis, oth on of A n B n on of D n E woul hv n spill to mmory. 67
Qustions? 68
Conlusion # of slis Grph oloring: ~50 Linr sn: ~10 69
Conlusion # of slis Grph oloring: ~50 Linr sn: ~10 Linr sn is muh mor simplr! Only out 10% slowr thn prftly implmnt grph oloring lgorithm An your o my not tht prft 70
Aknowlgmnts Grph oloring slis r pt from Rgistr Allotion y Dvi Wlkr Linr sn psuo-o n xmpl r pt from Linr Sn Rgistr Allotion y M. Poltto n V. Srkr. 71