Graph oloring Kmp s algorithm rmovs nods with < K dgs Fin This stp is alld simpliia1on Simpliia9on ithr nds with an mpty graph or a graph suh that ah nod has K dgs Now w hav to do somthing Eithr try out all possibl K-olorings Graph surgry 22
Spilling Givn a graph that has bn simpliid (but is not mpty) Pik a nod and rmov this nod and all its dgs rom th graph Th liv rang rprsntd by this nod is not alloatd a rgistr It is spilld th hom loa1on is in mmory W disuss latr how to pik a nod 23
Graph oloring, rvisd Rmov a nod i it has K-1 or wr nighbors Push on a stak whn rmoving Rmov un1l all nods hav K nighbors or th graph is mpty (All nods hav K nighbors): Pik a nod and rmov it with all its dgs Con1nu simpliia1on Can t on1nu as all nods hav K nighbors: Pik a nod and rmov it (Graph is mpty): Color graph Pop nod rom stak Assign olor 24
Spilld liv rangs A spilld liv rang rsids in mmory Crat tmporary, usually stord in th a1va1on rord What should w do with a spilld liv rang whn gnra9ng od? v1 = a + b = v1 + d v2 = b * 2 v1 v2 v3 a b d b, ar spilld 25
Spilld liv rangs Targt mahin (x86) rquirs that at last on oprand rsids in a rgistr Th othr on an by supplid by mmory Spilld liv rang oprand in mmory v1 = a + b : onstraint that b must b in mmory OUCH Now th rgistr alloator dtrmins instru1on sl1on Must run rgistr alloa1on prior to instru1on sl1on 26
Phas oupling Cod sl9on Rgistr alloa9on Cod shduling Cod sl9on dpnds on od shduling Cod shduling dpnds on rgistr alloa9on Rgistr alloa9on dpnds on od sl9on Clos oupling o dirnt od gnrator phass 27
Spilld liv rangs Targt mahin (x86) rquirs that at last on oprand rsids in a rgistr Th othr on an by supplid by mmory Spilld liv rang oprand in mmory v1 = a + b : onstraint that b must b in mmory OUCH Now th rgistr alloator dtrmins instru1on sl1on Must run rgistr alloa1on prior to instru1on sl1on And what i a is spilld as wll? Sam problm or RISC mahin: All oprands must b in a rgistr 28
Spilld liv rangs Cod gnrator may nd a rgistr or a spilld liv rang ( or or two liv rangs, or or ds9na9on i ds9na9on liv rang is spilld) Op9on 1: spar rgistrs Cod gnrator kps spar rgistrs that ar not alloatd by rgistr alloator 1 rgistr nough on IA32, 2 on RISC mahin Dpnds not all rgistrs may b ratd qual Rgistr alloator inds (K-2)-oloring or (K-1)-oloring!) Mayb OK on a RISC with 32 or 64 rgistrs 29
Mor graph surgry Whn spilling a nod, introdu a nw tmporary, rwrit th IR and start ovr Exampl v1 = a + b with b spilld. Introdu a tmporary tmp101, stord at (say) bp+40 Rwrit to tmp101 = * (bp + 40) v1 = a + tmp101 *(bp+40): shorthand or load tmporary 30
Tmporary liv rangs Liv rang o tmporaris is vry small Just on instru1on Graph should b asir to olor Tmporary has smallr numbr o dgs than spilld liv rang A dirnt tmporary is usd or ah us o th spilld variabl Rbuild intrrn graph and start ovr And i th graph s1ll annot b K-olord: Pik anothr nod or spilling As long as numbr o rgistrs > numbr o (asm) oprands th pross trminats with a lgal K-oloring 31
Exampl Considr an intrrn graph with 5 variabls v3 v1 v5 v2 v4 v1 v2 v3 v4 v5 32
Exampl with 3 rgistrs v3 v4 is rmovd by simpliia9on All rmaining nods 3 dgs v1 v5 Lt v5 b spilld v2 v4 33
Intrrn graph ronstru9on Introdu9on o tmporaris adds nods to intrrn graph v3 v1 t4 t1 v2 v4 t6 v1 v2 v3 v4 t1 t6 t2 t3 t5 34
Mor graph surgry A (bar?) approah is to split th liv rang v1 v2 v3 v4 v5 v1 v2 v3 v4 v5-1 v5-4 35
A nw intrrn graph v5-2 v3 v1 v5-3 v5-1 v2 v4 v5-4 v1 v2 v3 v4 v5-1 v5-4 36
Splibng Splibng rdus numbr o instru9ons that ar ndd to load (stor) tmporary variabls Variabls that ar spilld to mmory Whih liv rangs to split? Whr to split thm? 37
Nd to disuss spilling disions bor splibng 38
Graph oloring, rvisd Rmov a nod i it has K-1 or wr nighbors Push on a stak whn rmoving Rmov un1l all nods hav K nighbors or th graph is mpty (All nods hav K nighbors): Pik a nod and rmov it with all its dgs Con1nu simpliia1on Can t on1nu as all nods hav K nighbors: Pik a nod and rmov it (Graph is mpty): Color graph Pop nod rom stak Assign olor 39
Piking th spill vi9m A numbr o huris9s hav bn trid. Pik a nod at random (Chai9n, 1982) Pik nod with lowst spill ost s9mat (Chow, 1983) How do w s1mat spill ost Pik nod with lowst us ount 40
Es9ma9ng spill ost Nd to s9mat how on a basi blok is xutd Us proil rom past xu9on o program Input dpndnt? Us proil o urrnt xu9on Can b don in JIT (Just-in-1m ompilr) Guss: past prdits th utur Guss by rul-o-tn: loops xut 10 9ms 41
Es9ma9ng spill ost 42
Es9ma9ng spill ost 10 100 1000 10000 100 1000 10 100 43
Extnsions Spill ost s9mat an b xtndd to idn9y splibng andidats Don t orgt: intrrn graph rbuilt ar ah spill (split) dision Rquirs omputa1on o liv rangs! 44
Graph oloring Som spills may not vn b nssary. 45
Exampl 2 rgistrs Color Rgistr ax bx a b Stak: d
Exampl 2 rgistrs Color Rgistr ax bx a b Stak: d
Exampl 2 rgistrs Color Rgistr ax bx a b Stak: d
Exampl 2 rgistrs Color Rgistr ax bx a b Stak: d
Exampl 2 rgistrs Color Rgistr ax bx a b Stak: d
Exampl 2 rgistrs Color Rgistr ax bx a b Stak: d d
Exampl 2 rgistrs Color Rgistr ax bx a Stak: b d b d
Exampl 2 rgistrs Color Rgistr ax bx a b d Stak: a b d
Exampl 2 rgistrs Color Rgistr ax bx a Stak: b d b d
Exampl 2 rgistrs Color Rgistr ax bx a b Stak: d d
Exampl 2 rgistrs Color Rgistr ax bx a b Stak: d
Exampl 2 rgistrs Color Rgistr ax bx a b Stak: d
Exampl 2 rgistrs Color Rgistr ax bx a b Stak: d
Exampl 2 rgistrs Color Rgistr ax bx a b Stak: d
Exampl Although ah nod (ar rmoving, ) has 2 dgs, w ind a 2-oloring. Can w xploit this insight in th rgistr alloator? 60
Coalsing Cod ontains on a numbr o opy assignmnts Dspit opy propaga1on Liv rang spliing may introdu nw opis Som1ms spliing is too aggrssiv Gnral problm: two variabls v1, v2 onntd by opy No dg btwn nods or v1, v2 in intrrn graph v2 v1 61
W an oals ths liv rangs Rmovs th nd to hav a opy assignmnt May mak liv hardr or rgistr alloator as ombind nod (v1/v2) may hav not b rmovd by simpliia1on v1/v2 Huris1s to did whn to oals 62
Mahin aturs Som instru9ons work with spii rgistrs mul on x86: rads ax, dins ax and dx Must mak sur oprands ar in ths rgistrs Othr rgistrs not allowd Pr-olor ths oprands Assurs that oprand is assignd to this rgistr Color nod or oprand in intrrn graph Pr-olord nods ar not rmovd during simpliia1on Coloring starts whn all othr nods ar rmovd 63
Commnt Many itra9ons may b ndd Various huris1s rat many op1ons Major stps Livnss analysis, intrrn graph onstru1on Coloring Simpliia1on Spill/split disions Rwrit od Atual oloring Good rsults in many ass Som ompilrs produ mul1pl olor assignmnts and thn pik th bst What ould b wrong? 64