CS153: Copilrs Ltur 20: Ristr Alloation I Stpn Con ttps://www.sas.arvar.u/ourss/s153
Pr-lass Puzzl Wat s t iniu nur o olors n to olor a ap o t USA? Evry stat is assin on olor Aant stats ust ivn irnt olors ttps://printal-aps.lospot.o/2011/12/lan-ap-o-unit-stats.tl 2
Pr-lass Puzzl Answr 4 Four-olor tor says 4 Must at last 4: Suppos w a only 3 olors Grn R Pi so olors or CA an OR (R an Grn) NV ust Blu R Blu!!! ID ust R AZ ust Grn Grn UT!!!!!! 3
Announnts Prot 5 out Du Tusay Nov 13 (5 ays) Prot 6 out Du Tusay Nov 20 (12 ays) Prot 7 out Du Tursay Nov 29 (21 ays) Prot 8 will rlas on Tusay Du Saturay D 8 4
Toay Ristr alloation Grap olorin y sipliiation Coalsin 5
Ristr Alloation Sour Co Parsin Front n Elaoration Lowrin Optiization Ba n Co Gnration Tart Co Fro an intriat rprsntation wit unliit nur o tporary /loal varials Assin tporary varials to t (sall) nur o ain ristrs 6
Ristr Alloation Ristr alloation is in nrally an NP-oplt prol Can w alloat all ts n tporaris to ristrs? But w av a uristi tat is linar in prati! Bas on rap olorin Givn a rap, an w assin on o olors to a no su tat onnt nos av irnt olors? Hr, nos ar tp varials, an twn t1 an t2 ans tat t1 an t2 ar liv at t sa ti. Colors ar ristrs. But rap olorin is also NP-oplt! How os tat wor? 7
Colorin y Sipliiation Four pass Buil: onstrut intrrn rap, usin atalow analysis to in or a prora point vars tat ar liv at t sa ti Sipliy: olor as on sipl uristi I rap G as no n wit -1 s, tn G-{n} is -oloral i G is -oloral So rov nos wit r < Spill: i rap as only nos wit r, oos on to potntially spill (i.., tat ay n to sav to sta) Tn ontinu wit Sipliy Slt: wn rap is pty, start rstorin nos in rvrs orr an olor t Wn w nountr a potntial spill no, try olorin it. I w an t, rwrit prora to stor it to sta atr inition an loa or us. Try aain! Buil Sipliy Spill Slt 8
Exapl Fro Appl Intrrn rap {liv-in:, } := *(+12) := - 1 := * := *(+8) := *(+16) := *(+0) := + 8 := := + 4 := {liv-out:,,} 9
Sipliiation (4 ristrs) Coos any no wit r <4 Sta: 10
Sipliiation (4 ristrs) Coos any no wit r <4 Sta: 11
Sipliiation (4 ristrs) Coos any no wit r <4 Sta: 12
Sipliiation (4 ristrs) Coos any no wit r <4 Sta: 13
Sipliiation (4 ristrs) Coos any no wit r <4 Sta: 14
Sipliiation (4 ristrs) Coos any no wit r <4 Sta: 15
Sipliiation (4 ristrs) Coos any no wit r <4 Sta: 16
Sipliiation (4 ristrs) Coos any no wit r <4 Sta: 17
Sipliiation (4 ristrs) Coos any no wit r <4 Sta: 18
Sipliiation (4 ristrs) Coos any no wit r <4 Sta: 19
Slt (4 ristrs) 20 Sta: Color nos in orr o sta =t1 =t2 =t3 =t4 Grap is now pty!
Slt (4 ristrs) := *(+12) := - 1 := * := *(+8) := *(+16) := *(+0) := + 8 := := + 4 := =t1 =t2 =t3 =t4 21
Slt (4 ristrs) $t2 := *(t4+12) $t1 := $t1-1 $t2 := $t2 * $t1 $t3 := *($t4+8) $t1 := *($t4+16) $t2 := *($t2+0) $t3 := $t3 + 8 $t3 := $t3 $t1 := $t1 + 4 $t4 := $t2 So ovs it susquntly siplii... =t1 =t2 =t3 =t4 22
Spillin Tis xapl wor out nily! Always a nos wit r < Lt s try aain, ut now wit only 3 ristrs... 23
Exapl Fro Appl Intrrn rap {liv-in:, } := *(+12) := - 1 := * := *(+8) := *(+16) := *(+0) := + 8 := := + 4 := {liv-out:,,} 24
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: 25
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: 26
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: 27
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: Now w ar stu! No nos wit r <3 Pi a no to potntially spill 28
Wi No to Spill? Want to pi a no (i.., tp varial) tat will a it lily w ll al to olor rap Hi r ( liv at any prora points) Not us/in vry otn (so w on t n to ass sta vry otn) E.., oput spill Uss+s outsi loop + Uss+s in loop 10 priority o no r o no 29
Wi No to Spill? {liv-in:, } := *(+12) := - 1 := * := *(+8) := *(+16) := *(+0) := + 8 := := + 4 := {liv-out:,,} Spill priority = Uss+s outsi loop + Uss+s in loop 10 r o no 30
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: spill? Pi a no wit sall spill priority r to potntially spill 31
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: spill? 32
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: spill? 33
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: spill? 34
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: spill? 35
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: spill? 36
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: spill? 37
Slt (3 ristrs) Sta: spill? Grap is now pty! Color nos in orr o sta =t1 =t2 =t3 38
Slt (3 ristrs) Sta: spill? W ot unluy! In so ass a potntial spill no is still oloral, an t Slt pas an ontinu. But in tis as, w n to rwrit... =t1 =t2 =t3 39
Slt (3 ristrs) Spill {liv-in:, } := *(+12) := - 1 := * := *(+8) := *(+16) := *(+0) := + 8 := := + 4 := {liv-out:,,} {liv-in:, } := *(+12) := - 1 := * := *(+8) := *(+16) := *(+0) := + 8 := *<p+o>:= := + 4 := 2:=*<p+o> {liv-out: 2,,} 40
Buil {liv-in:, } := *(+12) := - 1 := * := *(+8) := *(+16) := *(+0) := + 8 := *<p+o>:= := + 4 := 2:=*<p+o> {liv-out: 2,,} 2 41
Sipliiation (3 ristrs) Coos any no wit r <3 Sta: 2 Tis ti w su an will al to oplt Slt pas sussully! 2 42
Ristr Prssur So optiizations inras liv-rans: Copy propaation Coon su-xprssion liination Loop invariant roval In turn, tat an aus t alloator to spill Copy propaation isn't tat usul anyway: Lt ristr alloator iur out i it an assin t sa ristr to two tps! Tn t opy an o away. An w on't av to worry aout ristr prssur. 43
Coalsin Ristr Alloation I w av x := y an x an y av no in t intrrn rap, w it al to assin t t sa olor. Tis woul translat to ri := ri wi woul tn rov On ia is to optiistially oals nos in t intrrn rap Just ta t s to t union 44
Exapl E.., t ollowin nos oul oals an an {liv-in:, } := *(+12) := - 1 := * := *(+8) := *(+16) := *(+0) := + 8 := := + 4 := {liv-out:,,} 45