Low-Lvl Issus Last ltur Intrproural analysis Toay Start low-lvl issus Rgistr alloation Latr Mor rgistr alloation Instrution shuling CS553 Ltur Rgistr Alloation I 2 Rgistr Alloation Prolm Assign an unoun numr of symoli rgistrs to a fix numr of arhittural rgistrs (whih might gt rnam y th harwar to som numr of physial rgistrs) Simultanously liv ata must assign to iffrnt arhittural rgistrs Goal Minimiz ovrha of assing ata Mmory oprations (loas & stors) Rgistr movs CS553 Ltur Rgistr Alloation I 3 1
Sop of Rgistr Alloation Exprssion Loal Loop Gloal Intrproural CS553 Ltur Rgistr Alloation I 4 Granularity of Alloation What is alloat to rgistrs? Varials Liv rangs/ws (i.., u-hains with ommon uss) Valus (i.., finitions; sam as varials with SSA & opy propagation) t 1 : x := 5 2 t 2 : y := x 3 t 3 : x := y+1 4 1 t 6 :... x... t 4 :... x... t 5 : x := 3 Varials: 2 (x & y) Liv Rangs/W: 3 (t 1 t 2,t 4 ; t 2 t 3 ; t 3,t 5 t 6 ) Valus: 4 (t 1, t 2, t 3, t 5, φ (t 3,t 5 )) What ar th traoffs? Eah alloation unit is givn a symoli rgistr nam (.g., s1, s2, t.) CS553 Ltur Rgistr Alloation I 5 2
Gloal Rgistr Alloation y Graph Coloring Ia [Cok 71], First alloator [Chaitin 81] 1. Construt intrfrn graph G=(N,E) Rprsnts notion of simultanously liv Nos ar units of alloation (.g., varials, liv rangs/ws) g (n 1,n 2 ) E if n 1 an n 2 ar simultanously liv Symmtri (not rflxiv nor transitiv) 2. Fin k-oloring of G (for k rgistrs) Ajant nos an t hav sam olor 3. Alloat th sam rgistr to all alloation units of th sam olor Ajant nos must alloat to istint rgistrs s2 s1 s3 CS553 Ltur Rgistr Alloation I 6 Intrfrn Graph Exampl (Varials) a :=... :=... :=...... a... :=... a...... a :=......... a :=............... :=...... a............... :=... CS553 Ltur Rgistr Alloation I 7 3
Intrfrn Graph Exampl (Ws) :=... :=... :=......... :=... Consir ws (u-hains w/ ommon uss) insta of varials...... :=......... :=............... :=..................... :=... CS553 Ltur Rgistr Alloation I 8 Computing th Intrfrn Graph Us rsults of liv varial analysis for ah symoli-rgistr s i o for ah symoli-rgistr s j (j < i) o for ah f {finitions of s i } o if (s j is liv at f) thn E E (s i,s j ) CS553 Ltur Rgistr Alloation I 9 4
Coalsing Mov instrutions Co gnration an prou unnssary mov instrutions mov t1, t2 If w an assign t1 an t2 to th sam rgistr, w an liminat th mov Ia If t1 an t2 ar not onnt in th intrfrn graph, oals thm into a singl varial Prolm Coalsing an inras th numr of gs an mak a graph unoloral Limit oalsing to avoi unoloral graphs oals t1 t2 t1 t2 CS553 Ltur Rgistr Alloation I 10 Alloating Rgistrs Using th Intrfrn Graph K-oloring Color graph nos using up to k olors Ajant nos must hav iffrnt olors Alloating to k rgistrs fining a k-oloring of th intrfrn graph Ajant nos must alloat to istint rgistrs But... Optimal graph oloring is NP-omplt Rgistr alloation is NP-omplt, too (must approximat) What if w an t k-olor a graph? (must spill) CS553 Ltur Rgistr Alloation I 11 5
Spilling If w an t fin a k-oloring of th intrfrn graph Spill varials (nos) until th graph is oloral Choosing varials to spill Choos last frquntly ass varials Brak tis y hoosing nos with th most onflits in th intrfrn graph Ys, ths ar huristis! CS553 Ltur Rgistr Alloation I 12 Wight Intrfrn Graph Goal Wight(s) =! f ( r) f(r) is xution frquny of r " rfrns r of s Stati approximation Us som rasonal shm to rank varials On possiility Wight(s) = 1 Nos aftr ranh: ½ wight of ranh Nos in loop: 10 wight of nos outsi loop CS553 Ltur Rgistr Alloation I 13 6
Simpl Gry Algorithm for Rgistr Alloation for ah n N o { slt n in rasing orr of wight } if n an olor thn o it { rsrv a rgistr for n } ls Rmov n (an its gs) from graph { alloat n to stak (spill) } CS553 Ltur Rgistr Alloation I 14 Exampl Attmpt to 3-olor this graph (,, ) Wight orr: What if you us a iffrnt wighting? CS553 Ltur Rgistr Alloation I 15 7
Exampl Attmpt to 2-olor this graph (, ) Wight orr: a a CS553 Ltur Rgistr Alloation I 16 Improvmnt #1: Simplifiation Phas [Chaitin 81] Ia Nos with < k nighors ar guarant oloral Rmov thm from th graph first Rus th gr of th rmaining nos Must spill only whn all rmaining nos hav gr k CS553 Ltur Rgistr Alloation I 17 8
Algorithm [Chaitin81] whil intrfrn graph not mpty o whil a no n with < k nighors o Rmov n from th graph simplify Push n on a stak if any nos rmain in th graph thn { lok with >= k gs } Pik a no n to spill { lowst spill-ost or } spill A n to spill st { highst gr } Rmov n from th graph if spill st not mpty thn Insrt spill o for all spill nos { stor aftr f; loa for us } Ronstrut intrfrn graph & start ovr whil stak not mpty o Pop no n from stak olor Alloat n to a rgistr CS553 Ltur Rgistr Alloation I 18 Mor on Spilling Chaitin s algorithm rstarts th whol pross on spill Nssary, aus spill o (loas/stors) uss rgistrs Okay, aus it usually only happns a oupl tims Altrnativ Rsrv 2-3 rgistrs for spilling Don t n to start ovr But hav fwr rgistrs to work with CS553 Ltur Rgistr Alloation I 19 9
Exampl Attmpt to 3-olor this graph (,, ) Stak: Wight orr: CS553 Ltur Rgistr Alloation I 20 Exampl Attmpt to 2-olor this graph (, ) Spill St: Stak: Wight orr: Many nos rmain unolor vn though w oul larly o ttr CS553 Ltur Rgistr Alloation I 21 10
Th Prolm: Worst Cas Assumptions Is th following graph 2-oloral? s1 s4 s2 s3 Clarly 2-oloral But Chaitin s algorithm las to an immiat lok an spill Th algorithm assums th worst as, namly, that all nighors will assign a iffrnt olor CS553 Ltur Rgistr Alloation I 22 Improvmnt #2: Optimisti Spilling [Briggs 89] s1 s4 s2 s3 Ia Som nighors might gt th sam olor Nos with k nighors might oloral Bloking os not imply that spilling is nssary Push lok nos on stak (rathr than pla in spill st) Chk oloraility upon popping th stak, whn mor information is availal Dfr ision CS553 Ltur Rgistr Alloation I 23 11
Algorithm [Briggs t al. 89] whil intrfrn graph not mpty o whil a no n with < k nighors o Rmov n from th graph simplify Push n on a stak if any nos rmain in th graph thn { lok with >= k gs } Pik a no n to spill { lowst spill-ost/highst gr } Push n on stak fr ision Rmov n from th graph whil stak not mpty o Pop no n from stak mak ision if n is oloral thn Alloat n to a rgistr ls Insrt spill o for n { Stor aftr f; loa for us } Ronstrut intrfrn graph & start ovr CS553 Ltur Rgistr Alloation I 24 Exampl Attmpt to 2-olor this graph (, ) Stak: * * * * * lok no Wight orr: CS553 Ltur Rgistr Alloation I 25 12
Improvmnt #3: Liv Rang Splitting [Chow & Hnnssy 84] Ia Start with varials as our alloation unit Whn a varial an t alloat, split it into multipl surangs for sparat alloation Sltiv spilling: put som surangs in rgistrs, som in mmory Insrt mmory oprations at ounaris Why is this a goo ia? CS553 Ltur Rgistr Alloation I 26 Improvmnt #4: Rmatrialization [Chaitin 82]&[Briggs 84] Ia Sltivly r-omput valus rathr than loaing from mmory Rvrs CSE Easy as Valu an omput in singl instrution, an All oprans ar availal Exampls Constants Arsss of gloal varials Arsss of loal varials (on stak) CS553 Ltur Rgistr Alloation I 27 13
Nxt Tim Ltur Mor rgistr alloation Alloation aross prour alls CS553 Ltur Rgistr Alloation I 28 14