Date post: | 05-Jul-2018 |
Category: |
Documents |
Upload: | awadhesh786 |
View: | 215 times |
Download: | 0 times |
of 234
8/16/2019 History 55
1/234
Register Allocation
8/16/2019 History 55
2/234
Announcements● Programming Project 4 due Saturday, August 18 at 11:30AM
● OH all t is !ee"#● As" $uestions %ia email&● As" $uestions %ia Pia''a&● No late submissions #
8/16/2019 History 55
3/234
Please e%aluate t is course on A(ess#
)our *eed+ac" really ma"es a di**erence#
8/16/2019 History 55
4/234
ere e Are
-e(ical Analysis
.R /eneration
Synta( Analysis
Semantic Analysis
.R O timi'ation
ode /eneration
O timi'ation
Sourceode
MachineCode
8/16/2019 History 55
5/234
ere e Are
.R /eneration
.R O timi'ation
ode /eneration
O timi'ation
Sourceode
MachineCode
Fan-TAC-stic!
-e(ical Analysis
Synta( Analysis
.R /eneration
Semantic Analysis
Fan-TAC-stic!
8/16/2019 History 55
6/234
ere e Are
-e(ical Analysis
Semantic Analysis
Synta( Analysis
.R O timi'ation
.R /eneration
ode /eneration
O timi'ation
Sourceode
MachineCode
8/16/2019 History 55
7/234
ode /eneration at a /lance● At t is oint, !e a%e o timi'ed .R code t at needs
to +e con%erted into t e target language 2e#g#assem+ly, mac ine code #
● /oal o* t is stage:● oose t e a ro riate mac ine instructions *or eac .R
instruction#● i%%y u *inite mac ine resources 2registers, cac es,
etc#●
.m lement lo!5le%el details o* t e runtime en%ironment#● Mac ine5s eci*ic o timi'ations are o*ten done ere,
t oug some are treated as art o* a *inalo timi'ation ase#
8/16/2019 History 55
8/234
O%er%ie!● Register Allocation 26oday
● Ho! to assign %aria+les to *initely manyregisters7
● at to do ! en it can t +e done7● Ho! to do so e**icienty7
● Garbage Collection 2Monday● Ho! to detect reclaima+le memory7● Ho! to reclaim memory e**iciently7
8/16/2019 History 55
9/234
Memory 6radeo**s● 6 ere is an enormous tradeo** +et!een speed and
size in memory#● SRAM is *ast +ut %ery e( ensi%e:
● an "ee u !it rocessor s eeds in t e /H'#● As o* 900 , cost is ;10
8/16/2019 History 55
10/234
6 e Memory Hierarc y● Idea : 6ry to get t e +est o* all !orlds +y
using multi le ty es o* memory#
8/16/2019 History 55
11/234
6 e Memory Hierarc y● Idea : 6ry to get t e +est o* all !orlds +y
using multi le ty es o* memory#
Registers
L1 Cache
L2 Cache
Main Memory Hard Disk
Net ork
8/16/2019 History 55
12/234
6 e Memory Hierarc y● Idea : 6ry to get t e +est o* all !orlds +y
using multi le ty es o* memory#
L1 Cache
L2 Cache
Main Memory Hard Disk
Net ork
9>?= 5 8@=
1?@= ?4@=
1M= 5 4M=
4/= 9>?/=>00/=B
H!G"
Registers
8/16/2019 History 55
13/234
6 e Memory Hierarc y● Idea : 6ry to get t e +est o* all !orlds +y
using multi le ty es o* memory#
L1 Cache
L2 Cache
Main Memory Hard Disk
Net ork
9>?= 5 8@=
1?@= ?4@=
1M= 5 4M=
4/= 9>?/=>00/=B
H!G"
0#9> 1ns
1ns >ns
>ns 9>ns
9>ns 100ns3 10ms
10 9000ms
Registers
8/16/2019 History 55
14/234
6 e allenges o* ode /eneration
● Almost all rogramming languages e( ose acoarse %ie! o* t e memory ierarc y:
● All %aria+les li%e in Cmemory#D●
is" and net!or" e( licitly andled se arately#● 2.nteresting e(ce tion: Stan*ord s #e$uoia
rogramming language● allenges in code generation:
● Position o+jects in a !ay t at ta"es ma(imumad%antage o* t e memory ierarc y#
● o so !it out ints *rom t e rogrammer#
8/16/2019 History 55
15/234
Registers● Most mac ines a%e a set o* registers ,
dedicated memory locations t at● can +e accessed $uic"ly,●
can a%e com utations er*ormed on t em, and● e(ist in small $uantity#
● Esing registers intelligently is a critical ste inany com iler#
● A good register allocator can generate code orderso* magnitude +etter t an a +ad register allocator#
8/16/2019 History 55
16/234
Register Allocation● .n 6A , t ere are an unlimited num+er o* %aria+les#● On a ysical mac ine t ere are a small num+er o*
registers:● (8? as *our general5 ur ose registers and a num+er o*
s eciali'ed registers#● M.PS as t!enty5*our general5 ur ose registers and
eig t s ecial5 ur ose registers#● Register allocation is t e rocess o* assigning
%aria+les to registers and managing data trans*erin and out o* registers#
8/16/2019 History 55
17/234
allenges in Register Allocation
● Registers are scarce #● O*ten su+stantially more .R %aria+les t an registers#● Feed to *ind a !ay to reuse registers ! ene%er ossi+le#
● Registers are com%licated #● (8?: Gac register made o* se%eral smaller registers can t
use a register and its constituent registers at t e sametime#
● (8?: ertain instructions must store t eir results in
s eci*ic registers can t store %alues t ere i* you !ant touse t ose instructions#● M.PS: Some registers reser%ed *or t e assem+ler or
o erating system#● Most arc itectures: Some registers must +e reser%ed
across *unction calls#
8/16/2019 History 55
18/234
/oals *or 6oday● .ntroduce register allocation *or a M.PS5
style mac ine:● Some num+er o* indi%isi+le, general5 ur ose
registers#● G( lore t ree algorit ms *or register
allocation:●
FaI%e 2CnoD register allocation#● -inear scan register allocation#● /ra 5coloring register allocation#
8/16/2019 History 55
19/234
An .nitial Register Allocator● Idea : Store e%ery %alue in main memory,
loading %alues only ! en t ey re needed#● 6o generate a code t at er*orms a
com utation:● /enerate load instructions to ull t e %alues
*rom main memory into registers#● /enerate code to er*orm t e com utation
on t e registers#● /enerate store instructions to store t e
result +ac" into main memory#
8/16/2019 History 55
20/234
Our Register Allocator .n Action
8/16/2019 History 55
21/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
8/16/2019 History 55
22/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
8/16/2019 History 55
23/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
8/16/2019 History 55
24/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
lw $t0, -12(fp)
8/16/2019 History 55
25/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
lw $t0, -12(fp)lw $t1, -16(fp)
8/16/2019 History 55
26/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1
8/16/2019 History 55
27/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
8/16/2019 History 55
28/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
8/16/2019 History 55
29/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)
8/16/2019 History 55
30/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
8/16/2019 History 55
31/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
8/16/2019 History 55
32/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
lw $t0, -8(fp)
8/16/2019 History 55
33/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
lw $t0, -8(fp)lw $t1, -20(fp)
8/16/2019 History 55
34/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
lw $t0, -8(fp)lw $t1, -20(fp)add $t2, $t0, $t1
8/16/2019 History 55
35/234
Our Register Allocator .n Action
a = b + c;d = a;c = a + d;
lw $t0, -12(fp)lw $t1, -16(fp)add $t2, $t0, $t1sw $t2, -8(fp)
lw $t0, -8(fp)sw $t0, -20(fp)
lw $t0, -8(fp)lw $t1, -20(fp)add $t2, $t0, $t1sw $t2, -16(fp)
&aram N
###
&aram 1
#tored '%
#tored ra
a b
c
d
'% ( )
'% ( *N
+++
'% ( *
'% , *
'% , -
'% , 12
'% , 1.
'% , 2)
8/16/2019 History 55
36/234
Analysis o* our Allocator●
isad%antage: Gross ine''iciency #● .ssues unnecessary loads and stores +y t e do'en#● astes s ace on %alues t at could +e stored urely in registers#● Gasily an order o* magnitude or t!o slo!er t an necessary#● Enacce ta+le in any roduction com iler#
● Ad%antage: #im%licity #● an translate eac iece o* .R directly to assem+ly as !e go#● Fe%er need to !orry a+out running out o* registers#● Fe%er need to !orry a+out *unction calls or s ecial5 ur ose
registers#● /ood i* you just needed to get a rototy e com iler u and
running#
8/16/2019 History 55
37/234
=uilding a =etter Allocator● Goal : 6ry to old as many %aria+les in
registers as ossi+le#● Reduces memory reads
● e !ill need to address t ese $uestions:● ic registers do !e ut %aria+les in7● at do !e do ! en !e run out o* registers7
8/16/2019 History 55
38/234
Register onsistency
a = b + c
d = a d = b
e = d
8/16/2019 History 55
39/234
Register onsistency● At eac rogram oint, eac %aria+le
must +e in t e same location#● oes not mean t at eac %aria+le is al!ays
stored in t e same location&● At eac rogram oint, eac register
olds at most one li%e %aria+le#● an assign se%eral %aria+les t e same
register i* no t!o o* t em e%er !ill +e readtoget er#
8/16/2019 History 55
40/234
-i%e Ranges and -i%e .nter%als● Recall: A %aria+le is li/e at a articular rogram
oint i* its %alue may +e read later +e*ore it is!ritten#
● an *ind t is using glo+al li%eness analysis#● 6 e li/e range *or a %aria+le is t e set o* rogram
oints at ! ic t at %aria+le is li%e#● 6 e li/e inter/al *or a %aria+le is t e smallest
su+range o* t e .R code containing all a %aria+le s
li%e ranges#● A ro erty o* t e .R code, not t e J/#● -ess recise t an li%e ranges, +ut sim ler to !or" !it #
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
41/234
g
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
42/234
g
e = d + a f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
43/234
ge = d + a
f = b + c
f = f + b
d = e + f d = e – f
g = d
e = d + a f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
44/234
ge = d + a
f = b + c
f = f + b
d = e + f d = e – f
g = d
{ g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
45/234
ge = d + a
f = b + c
f = f + b
d = e + f d = e – f
{ d }g = d
{ g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1:
g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
46/234
e = d + a
f = b + c
f = f + b
d = e + f{ d }
d = e – f
{ d }g = d
{ g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
47/234
e = d + a
f = b + c
f = f + b
{ e, f }
d = e + f{ d }
d = e – f
{ d }g = d
{ g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
48/234
e = d + a
f = b + c
f = f + b
{ e, f }
d = e + f{ d }
d = e – f{ d }
{ d }g = d { g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
49/234
e = d + a
f = b + c
f = f + b
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
50/234
e = d + a
f = b + c
f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
51/234
e = d + a
f = b + c
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
52/234
e = d + a
f = b + c{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
53/234
e = d + a
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
54/234
e = d + a{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als
8/16/2019 History 55
55/234
{ a, b, c, d }e = d + a
{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als{ b d }
8/16/2019 History 55
56/234
{ a, b, c, d }e = d + a
{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als{ b d }
8/16/2019 History 55
57/234
{ a, b, c, d }e = d + a
{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
a
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als{ b d }
8/16/2019 History 55
58/234
{ a, b, c, d }e = d + a
{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
a b b
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als{ a b c d }b
b
b
8/16/2019 History 55
59/234
{ a, b, c, d }e = d + a
{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
a bc b b
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als{ a b c d }b
b
b b
d
8/16/2019 History 55
60/234
{ a, b, c, d }e = d + a
{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
a bc b b bd
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als{ a b c d }b
b
b b
d
8/16/2019 History 55
61/234
{ a, b, c, d }e = d + a
{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
a bc b b bd
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als{ a b c d }a b
cb
b b
eb
d
8/16/2019 History 55
62/234
{ a, b, c, d }e = d + a
{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
a bc b b be bd
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als{ a, b, c, d }a b
cb
b b
eb
d b
f
8/16/2019 History 55
63/234
{ a, b, c, d }e = d + a
{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
a bc b b be bd bf
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
-i%e Ranges and -i%e .nter%als{ a, b, c, d }a b
cb
b b
eb
d b
gb
f
8/16/2019 History 55
64/234
{ a, b, c, d }e = d + a
{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }
d = e + f{ d }
{ e, f }
d = e – f{ d }
{ d }g = d { g }
a bc b b be bd bg bf
e = d + a
f = b + c
f = f + b
IfZ e Goto _L0
d = e + f
d = e - f
Goto _L1;
_L0:
_L1: g = d
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
65/234
g
●
/i%en t e li%e inter%als *or all t e %aria+les in t e rogram, !e canallocate registers using a sim legreedy algorit m#
● .dea: 6rac" ! ic registers are *reeat eac oint#
● en a li%e inter%al +egins, gi%et at %aria+le a *ree register#
● en a li%e inter%al ends, t eregister is once again *ree#
●
e can t al!ays *it e%eryt ing intoa register !e ll see ! at do to in aminute#
a bc b b e bd bg bf
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
66/234
ga bc b b be bd bg bf
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
67/234
ga bc b b be bd bg bf
R ) R 1 R 2R ) R 1 R 20ree Registers
R 2R
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
68/234
a bc b b be bd bg bf
R ) R 1 R 2R ) R 1 R 20ree Registers
R 2R
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
69/234
R )a bc b b be bd bg bf
R ) R 1 R 2R 1 R 20ree Registers
R 2R
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
70/234
R 1R )a bc b b be bd bg bf
R ) R 1 R 2R 20ree Registers
R 2R
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
71/234
R 2R 1R )a bc b b be bd bg bf
R ) R 1 R 20ree Registers
R 2R
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
72/234
R R 2R 1R )a bc b b be bd bg bf
R ) R 1 R 20ree Registers
R 2
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
73/234
R ) R R 2R 1R )a bc b b be bd bg bf
R 1 R 20ree Registers
R 2
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
74/234
R )R ) R R 2R 1a bc b b be bd bg bf
R 1 R 20ree Registers
R 2
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
75/234
R 2R )R ) R R 2R 1a bc b b be bd bg bf
R 10ree Registers
R 2
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
76/234
R 2R 2R )R ) R R 1a bc b b be bd bg bf
R 10ree Registers
R 2
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
77/234
R 2R ) R 1 R 2R 2R ) R R 1
a bc b b be bd bg bf 0ree Registers
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
78/234
R ) R 2R ) R 1 R 2R 2 R R 1
a bc b b be bd bg bf 0ree Registers
Register Allocation !it -i%e .nter%als
8/16/2019 History 55
79/234
R ) R 2R ) R 1 R 2R 2 R R 1
a bc b b be bd bg bf 0ree Registers
Anot er G(am le
8/16/2019 History 55
80/234
Anot er G(am le
8/16/2019 History 55
81/234
b b ba bg be bfc bd
Anot er G(am le
b
f
d 0 R i
8/16/2019 History 55
82/234
R ) R 1 R 2
b b ba bg be bfc bd
R ) R 1 R 2
0ree Registers
Anot er G(am leb
bb
b
b
b
fb
d
0 R i
8/16/2019 History 55
83/234
R )R )
b b ba bg be bfc bd
R 1 R 2R 1 R 2
0ree Registers
Anot er G(am leb
bb
b
b
b
fb
d
0 R i t
8/16/2019 History 55
84/234
R )R )
b b ba bg be bfc bd
R 1 R 2R 1 R 2
0ree Registers
Anot er G(am leb
bb
b
gb
b
fb
d
0ree Registers
8/16/2019 History 55
85/234
R 1R )R )
b b ba bg be bfc bd
R 1 R 2R 2
0ree Registers
Anot er G(am leb
bb
a b
gb
e b
fc b
d 0ree Registers
8/16/2019 History 55
86/234
R 2R 1R )R )
b b ba bg be bfc bd
R 1 R 2
0ree Registers
Anot er G(am leb
bb
a b
gb
e b
fc b
d 0ree Registers
8/16/2019 History 55
87/234
R 2R 1R )R )
b b ba bg be bfc bd
R 1 R 2
0ree Registers
What do we donow?
Register S illing
8/16/2019 History 55
88/234
●
.* a register cannot +e *ound *or a %aria+lev
, !emay need to s%ill a %aria+le#● en a %aria+le is s illed, it is stored in memory
rat er t an a register#●
en !e need a register *or t e s illed %aria+le:● G%ict some e(isting register to memory#● -oad t e %aria+le into t e register#● en done, !rite t e register +ac" to memory and
reload t e register !it its original %alue#● S illing is slo!, +ut sometimes necessary#
Anot er G(am leb
bb
a b
gb
e b
fc b
d 0ree Registers
8/16/2019 History 55
89/234
R 2R 1R )R )
b b ba bg be bfc bd
R 1 R 2
0ree Registers
What do we donow?
Anot er G(am leb
bb
a b
gb
e b
fc b
d 0ree Registers
8/16/2019 History 55
90/234
R ) R 2R 1R )
b b ba bg be bfc bd
R 1 R 2
0ree Registers
Anot er G(am leb
bb
a b
gb
e b
fc b
d 0ree Registers
8/16/2019 History 55
91/234
R )R ) R 2R 1
b b ba bg be bfc bd
R 1 R 2
0ree Registers
Anot er G(am le b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
92/234
R 2R )R ) R 2R 1
ba ge fc d
R 1
0ree Registers
Anot er G(am le b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
93/234
R 2R 2R )R ) R 1
gd
R 1
g
Anot er G(am le b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
94/234
R ) R 2R 2R ) R 1
g
R 1
g
Anot er G(am le b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
95/234
R )R ) R 2R 2R 1
g
R 1
g
Anot er G(am le b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
96/234
R 1R )R ) R 2R 2R 1
g g
Anot er G(am le b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
97/234
R 1R 1R )R ) R 2R 2
Anot er G(am le b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
98/234
R 1R 1R )R ) R 2R 2
-inear Scan Register Allocation
8/16/2019 History 55
99/234
● 6 is algorit m is called linear scan register allocation and is a com arati%ely ne! algorit m#
● Ad%antages:● Kery e**icient 2a*ter com uting li%e inter%als, runs in linear
time● Produces good code in many instances#● Allocation ste !or"s in one ass can generate code during
iteration#● O*ten used in L.6 com ilers li"e La%a HotS ot#
● isad%antages:● .m recise due to use o* li%e inter/als rat er t an li%e ranges #● Ot er tec ni$ues "no!n to +e su erior in many cases#
orrectness Proo* S"etc
8/16/2019 History 55
100/234
●
Fo register olds t!o li%e %aria+les atonce:● -i%e inter%als are conser%ati%e
a ro(imations o* li%e ranges#● Fo t!o %aria+les !it o%erla ing li%e
ranges laced in t e same register#● At eac rogram oint, e%ery %aria+le is
in t e same location:● All %aria+les assigned a uni$ue location#
Second5 ance =in Pac"ing
A i% % i * li 5 #
8/16/2019 History 55
101/234
● A more aggressi%e %ersion o* linear5scan#● Eses li%e ranges instead o* li%e inter/als #● .* a %aria+le must +e s illed, don t s ill all uses
o* it#● A later li%e range mig t still *it into a register#
● Re$uires a *inal data5*lo! analysis to con*irm %aria+les are assigned consistent locations#
● See C uality and S eed in -inear5scan Register AllocationD +y 6rau+, Hollo!ay, and Smit #
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d
0ree Registers
8/16/2019 History 55
102/234
R )
R )
R 1
R 2
R 1
R 2
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d
0ree Registers
8/16/2019 History 55
103/234
R )
R )
R 1
R 2
R 1
R 2
R
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d R R R
R R0ree Registers
8/16/2019 History 55
104/234
R )
R )
R 1
R 2
R 1
R 2
RR
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d R R R
R0ree Registers
8/16/2019 History 55
105/234
R 1
R )
R )
R 1
R 2
R 2
RRR
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d R R R
0ree Registers
8/16/2019 History 55
106/234
R 2
R 1
R )
R )
R 1
R 2
RRR
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d R R R
0ree Registers
8/16/2019 History 55
107/234
R 2
R 1
R )
R )
R 1
R 2
R RR
R
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d R R
0ree Registers
8/16/2019 History 55
108/234
R )
R 2
R 1
R )
R 1
R 2
R
R RR
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d R R
0ree Registers
8/16/2019 History 55
109/234
R )
R )
R 2
R 1
R 1
R 2
RR
RR
R R
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
110/234
R 2
R 1
R 1
R )
R )
R 2
R
RR
RR
R
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
111/234
R 2
R 2
R 1
R 1
R )
R )
R R
RRR
R
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
112/234
R 1
R 2
R 2
R 1
R )
R )
R
R R
RR
R
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
113/234
R 1
R 1
R 2
R 2
R )
R )
R
R R
RR
R
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
114/234
R 1
R 1
R 2
R 2
R )
R )
R
R R
R R
R
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
115/234
11 22))
R
R R
R R
R
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
116/234
11 22))
R
R R
R R
R
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
117/234
11 22))
R ) R 1
R 1 R 2
R 2R )
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
118/234
) 11 22)
R )
R ) R 1
R 1 R 2
R 2
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
119/234
)) 11 22
R )
R ) R 1
R 1 R 2
R 2
Second5 ance =in Pac"ing b
b b
a b
g b
e b
fc b
d 0ree Registers
8/16/2019 History 55
120/234
))
An Gntirely i**erent A roac
8/16/2019 History 55
121/234
An Gntirely i**erent A roac{ a, b, c, d }
e = d + a{ b, c, e }
8/16/2019 History 55
122/234
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f
{ d }
{ e, f }d = e – f
{ d }
{ d }g = d { g }
An Gntirely i**erent A roac{ a, b, c, d }
e = d + a{ b, c, e }
8/16/2019 History 55
123/234
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f
{ d }
{ e, f }d = e – f
{ d }
{ d }g = d { g }
What can we infer fromall these variables being
live at this point?
An Gntirely i**erent A roac{ a, b, c, d }
e = d + a{ b, c, e }
8/16/2019 History 55
124/234
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f
{ d }
{ e, f }d = e – f
{ d }
{ d }g = d { g }
8/16/2019 History 55
125/234
An Gntirely i**erent A roac{ a, b, c, d }
e = d + a{ b, c, e }
8/16/2019 History 55
126/234
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f
{ d }
{ e, f }d = e – f
{ d }
{ d }g = d { g }
a
b c
d
e
f
g
An Gntirely i**erent A roac{ a, b, c, d }
e = d + a{ b, c, e }
8/16/2019 History 55
127/234
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f
{ d }
{ e, f }d = e – f
{ d }
{ d }g = d { g }
a
b c
d
e
f
g
8/16/2019 History 55
128/234
An Gntirely i**erent A roac{ a, b, c, d }
e = d + a{ b, c, e }
{ b }
R ) R 1 R 2
R ) R 1 R 2Registers
R
8/16/2019 History 55
129/234
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f
{ d }
{ e, f }d = e – f
{ d }
{ d }g = d { g }
a
b c
d
e
f
g
An Gntirely i**erent A roac{ a, b, c, d }e = d + a
{ b, c, e }
{ b c e }
R ) R 1 R 2
R ) R 1 R 2Registers
R
8/16/2019 History 55
130/234
{ b, c, e }f = b + c
{ b, e, f}
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f
{ d }
{ e, f }d = e – f
{ d }
{ d }g = d { g }
a
b c
d
e
f
g
6 e Register .nter*erence /ra
● 6 e register inter'erence gra%h 2R./ o*t l5*l ! i di t d
8/16/2019 History 55
131/234
a control5*lo! gra is an undirected gra! ere
● Gac node is a %aria+le#●
6 ere is an edge +et!een t!o %aria+les t atare li%e at t e same rogram oint#● Per*orm register allocation +y assigning
eac %aria+le a di**erent register *rom all o*
its neig +ors#● 6 ere s just one catc ###
6 e One atc● 6 is ro+lem is e$ui%alent to gra%h,
l i g ! i i N& h d i* t
8/16/2019 History 55
132/234
coloring , ! ic is N&,hard i* t ere areat least t ree registers#
● Fo good olynomial5time algorit ms 2or
e%en good a ro(imations& are "no!n*or t is ro+lem#
● e a%e to +e content !it a euristic
t at is good enoug *or R./s t at arise inractice#
6 e One atc to 6 e One atc
8/16/2019 History 55
133/234
6 e One atc to 6 e One atc
8/16/2019 History 55
134/234
.* you can *igure out a !ay to assignregisters to ar+itrary R./s, you %e just
ro%en & N N& and !ill get a 13)))3)))check *rom t e lay Mat ematics
.nstitute#
6 e One atc to 6 e One atc
8/16/2019 History 55
135/234
.* you can *igure out a !ay to assignregisters to ar+itrary R./s, you %e just
ro%en & N N& and !ill get a 13)))3)))check *rom t e lay Mat ematics
.nstitute#
8/16/2019 History 55
136/234
=attling N& 5Hardness
aitin s Algorit m● .ntuition:
●
Su ose !e are trying to k5color a gra and *ind a node!it * ! t k dg #
8/16/2019 History 55
137/234
Su ose !e are trying to k5color a gra and ind a node!it *e!er t an k edges#● .* !e delete t is node *rom t e gra and color ! at
remains, !e can *ind a color *or t is node i* !e add it +ac"in#
● Reason: it *e!er t an k neig +ors, some color must +ele*t o%er#
● Algorit m:● Jind a node !it *e!er t an k outgoing edges#●
Remo%e it *rom t e gra #● Recursi%ely color t e rest o* t e gra #● Add t e node +ac" in#● Assign it a %alid color#
aitin s Algorit m
8/16/2019 History 55
138/234
aitin s Algorit m
b c
8/16/2019 History 55
139/234
a
b c
d e
g f
aitin s Algorit m
b c
8/16/2019 History 55
140/234
a
b c
d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
aitin s Algorit m
b c
8/16/2019 History 55
141/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
aitin s Algorit m
b c
8/16/2019 History 55
142/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c
aitin s Algorit m
b c
8/16/2019 History 55
143/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c
8/16/2019 History 55
144/234
aitin s Algorit m
b c
8/16/2019 History 55
145/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
aitin s Algorit m
b c
8/16/2019 History 55
146/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
aitin s Algorit m
b c
8/16/2019 History 55
147/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
aitin s Algorit m
b c
8/16/2019 History 55
148/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
d
aitin s Algorit m
b c
8/16/2019 History 55
149/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
d
aitin s Algorit m
b c
8/16/2019 History 55
150/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
d e
aitin s Algorit m
b c
8/16/2019 History 55
151/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
d ea
aitin s Algorit m
b c
g
8/16/2019 History 55
152/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
d ea
8/16/2019 History 55
153/234
aitin s Algorit m
b c
g
8/16/2019 History 55
154/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
d ea
aitin s Algorit m
b c
g
8/16/2019 History 55
155/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
d ea
aitin s Algorit m
b c
8/16/2019 History 55
156/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
d ea
aitin s Algorit m
b c
a
8/16/2019 History 55
157/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
d ea
aitin s Algorit m
b c
8/16/2019 History 55
158/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
d e
aitin s Algorit m
b c
8/16/2019 History 55
159/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers
R
c b
d e
8/16/2019 History 55
160/234
aitin s Algorit m
b c
8/16/2019 History 55
161/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers R
c b
d
aitin s Algorit m
b c
8/16/2019 History 55
162/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers R
c b
aitin s Algorit m
b c
8/16/2019 History 55
163/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers R
c b
8/16/2019 History 55
164/234
aitin s Algorit m
b c
8/16/2019 History 55
165/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers R
c
aitin s Algorit m
b c
8/16/2019 History 55
166/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers R
aitin s Algorit m
b c
8/16/2019 History 55
167/234
a d e
g f
R ) R 1 R 2R ) R 1 R 2Registers R
One Pro+lem● at i* !e can t *ind a node !it *e!er
t an k neig +ors7● oose and remo%e an ar+itrary node
8/16/2019 History 55
168/234
oose and remo%e an ar+itrary node,mar"ing it Ctrou+lesome#D
●
Ese euristics to c oose ! ic one#● en adding node +ac" in, it may +e
ossi+le to *ind a %alid color#●
Ot er!ise, !e a%e to s ill t at node#
aitin s Algorit m Reloaded
a
b c
d
8/16/2019 History 55
169/234
a d
e
f
g
R ) R 1 R 2R ) R 1 R 2Registers
aitin s Algorit m Reloaded
a
b c
d
8/16/2019 History 55
170/234
a d
e
f
R ) R 1 R 2R ) R 1 R 2Registers
gg
aitin s Algorit m Reloaded
a
b c
d
8/16/2019 History 55
171/234
f
a d
e
R ) R 1 R 2R ) R 1 R 2Registers
gg f
aitin s Algorit m Reloaded
a
b c
d
8/16/2019 History 55
172/234
e
f
a d
R ) R 1 R 2R ) R 1 R 2Registers
gg f
e
d
aitin s Algorit m Reloaded
a
b c
8/16/2019 History 55
173/234
d
e
f
a
R ) R 1 R 2R ) R 1 R 2Registers
gg f
ed
c
d
aitin s Algorit m Reloaded
a
b
c
8/16/2019 History 55
174/234
d
e
f
a
R ) R 1 R 2R ) R 1 R 2Registers
gg f
ed
c
d
aitin s Algorit m Reloaded
a
b
c b
8/16/2019 History 55
175/234
d
e
f
a
R ) R 1 R 2R ) R 1 R 2Registers
gg f
ed
c
d
aitin s Algorit m Reloaded
a
b
c b
a
8/16/2019 History 55
176/234
d
e
f
a
R ) R 1 R 2R ) R 1 R 2Registers
gg f
ed
c
d
aitin s Algorit m Reloaded
a
b
dc
b
8/16/2019 History 55
177/234
e
f
R ) R 1 R 2R ) R 1 R 2Registers
gg f
ed
c
d
aitin s Algorit m Reloaded
a
b
dc
b
8/16/2019 History 55
178/234
e
f
R ) R 1 R 2R ) R 1 R 2Registers
gg f
ed
c
d
aitin s Algorit m Reloaded
a
b
dc
8/16/2019 History 55
179/234
e
f
R ) R 1 R 2R ) R 1 R 2Registers
gg f
ed
c
d
aitin s Algorit m Reloaded
a
b
dc
8/16/2019 History 55
180/234
e
f
R ) R 1 R 2R ) R 1 R 2Registers
gg f
ed
d
aitin s Algorit m Reloaded
a
b
d
c
8/16/2019 History 55
181/234
e
f
R ) R 1 R 2R ) R 1 R 2Registers
gg f
ed
d
aitin s Algorit m Reloaded
a
b
d
c
8/16/2019 History 55
182/234
e
f
R ) R 1 R 2R ) R 1 R 2Registers
gg f
ed
d
aitin s Algorit m Reloaded
a
b
c
8/16/2019 History 55
183/234
e
f
R ) R 1 R 2R ) R 1 R 2Registers
gg f
e
d
aitin s Algorit m Reloaded
a
b
c
8/16/2019 History 55
184/234
e
f
R ) R 1 R 2R ) R 1 R 2Registers
gg f
e
d
aitin s Algorit m Reloaded
a
b
d
c
( spilled )
8/16/2019 History 55
185/234
e
f
R ) R 1 R 2R ) R 1 R 2Registers
gg f
e
d
aitin s Algorit m Reloaded
a
b
d
c
( spilled )
8/16/2019 History 55
186/234
f
R ) R 1 R 2R ) R 1 R 2Registers
gg fe
d
aitin s Algorit m Reloaded
a
b
d
c
( spilled )
8/16/2019 History 55
187/234
f
R ) R 1 R 2R ) R 1 R 2Registers
gg fe
d
aitin s Algorit m Reloaded
a
b
d
c
( spilled )
8/16/2019 History 55
188/234
f
R ) R 1 R 2R ) R 1 R 2Registers
gg e
d
aitin s Algorit m Reloaded
a
b
d
c
( spilled )
8/16/2019 History 55
189/234
f
R ) R 1 R 2R ) R 1 R 2Registers
gg e
d
aitin s Algorit m Reloaded
a
b
d
c
( spilled )
8/16/2019 History 55
190/234
f
R ) R 1 R 2R ) R 1 R 2Registers
g e
d
aitin s Algorit m Reloaded
a
b
d
c
( spilled )
8/16/2019 History 55
191/234
f
R ) R 1 R 2R ) R 1 R 2Registers
g e
A Smarter Algorit m{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
R ) R 1 R 2
R ) R 1 R 2Registers
d a
b
d
c
( spilled )
8/16/2019 History 55
192/234
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f
{ d }
{ e, f }d = e – f
{ d }
{ d }g = d { g }
f
g e
8/16/2019 History 55
193/234
A Smarter Algorit m{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
R ) R 1 R 2
R ) R 1 R 2Registers
d a
b
d
c
( spilled )
8/16/2019 History 55
194/234
{ b, e, f }f = f + b{ e, f }
{ e, f }d = e + f
{ d }
{ e, f }d = e – f
{ d }
{ d }g = d{ g }
f
g e
A Smarter Algorit m{ a, b, c, d }
e = d + a{ b, c, e }
{ b, c, e }f = b + c
{ b, e, f}
R ) R 1 R 2
R ) R 1 R 2Registers
d a
b
d
c
d
d ( spilled )
8/16/2019 History 55
195/234
{ b, e, f }f = f + b
{ e, f }
{ e, f }d = e + f
{ d }
{ e, f }d = e – f
{ d }
{ d }g = d{ g }
e
g d
f
Anot er G(am le
8/16/2019 History 55
196/234
Anot er G(am le
a
b c
d
8/16/2019 History 55
197/234
a d
ef
Anot er G(am le
a
b c
d
8/16/2019 History 55
198/234
a d
ef
R ) R 1 R 2R ) R 1 R 2Registers
a
Anot er G(am le
b c
d
8/16/2019 History 55
199/234
a d
ef
R ) R 1 R 2R ) R 1 R 2Registers
a
c
a
Anot er G(am le
b
d
8/16/2019 History 55
200/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
c
a
Anot er G(am le
b
d
8/16/2019 History 55
201/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
b
c
a
Anot er G(am le
b
d
be
8/16/2019 History 55
202/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
b
c
a
Anot er G(am le
b
d
bef
8/16/2019 History 55
203/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
b
c
a
Anot er G(am le
b
d
befd
8/16/2019 History 55
204/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
b
c
a
Anot er G(am le
b
d
bef
8/16/2019 History 55
205/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
b
c
a
Anot er G(am le
b
d
bef
8/16/2019 History 55
206/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
b
c
a
Anot er G(am le
b
d
be
8/16/2019 History 55
207/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
b
c
a
Anot er G(am le
b
d
be
8/16/2019 History 55
208/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
b
c
a
Anot er G(am le
b
d
b
8/16/2019 History 55
209/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
b
c
a
Anot er G(am le
b
d
b
8/16/2019 History 55
210/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
b
c
a
Anot er G(am le
d
b
8/16/2019 History 55
211/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
c
a
Anot er G(am le
d
b
8/16/2019 History 55
212/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
c
a
Anot er G(am le
d
b
8/16/2019 History 55
213/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
a
c
a
Anot er G(am le
d
b
8/16/2019 History 55
214/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
a
c
a
Anot er G(am le
d
b
8/16/2019 History 55
215/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
c
a
Anot er G(am le
d
b
8/16/2019 History 55
216/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
c
a
Anot er G(am le
b
d
befd
8/16/2019 History 55
217/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
c
a
Anot er G(am le
b
d
bef
8/16/2019 History 55
218/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
c
a
Anot er G(am le
b
d
bef
8/16/2019 History 55
219/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
c
a
Anot er G(am le
b
d
be
8/16/2019 History 55
220/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
c
a
Anot er G(am le
b
d
be
8/16/2019 History 55
221/234
ef
R ) R 1 R 2R ) R 1 R 2Registers
ac
c
a
Anot er G(am le
b
d
b
8/16/2019 History 55
222/234
f
R ) R 1 R 2R ) R 1 R 2Registers
ace
c
a
Anot er G(am le
b
d
b
8/16/2019 History 55
223/234
f
R ) R 1 R 2R ) R 1 R 2Registers
ace
c
a
Anot er G(am le
b
d
8/16/2019 History 55
224/234
f
R ) R 1 R 2R ) R 1 R 2Registers
ac
e
8/16/2019 History 55
225/234
c
a
Anot er G(am le
b
d
8/16/2019 History 55
226/234
f
R ) R 1 R 2R ) R 1 R 2Registers
ae
8/16/2019 History 55
227/234
c
a
Anot er G(am le
b
d
(spilled)
8/16/2019 History 55
228/234
f
R ) R 1 R 2R ) R 1 R 2Registers
e
c
a
Anot er G(am le
b
d
(spilled)
(spilled)
8/16/2019 History 55
229/234
f
R ) R 1 R 2R ) R 1 R 2Registers
e
8/16/2019 History 55
230/234
orrectness Proo* S"etc● Fo t!o %aria+les li%e at some oint are
assigned t e same register#● Jorced +y gra coloring#
● At any rogram oint eac %aria+le isal!ays in one location#
8/16/2019 History 55
231/234
y● Automatic i* !e assign eac %aria+le one
register#● Re$uires a *e! tric"s i* !e se arate +y use
case#
.m ro%ements to t e Algorit m● oose ! at to s ill intelligently#
●
Ese euristics 2least5commonly used,greatest im ro%ement, etc# to determine! at to s ill#
● Handle s illing intelligently#
8/16/2019 History 55
232/234
● en s illing a %aria+le, recom ute t e R./+ased on t e s ill and use a ne! coloring to*ind a register#
Summary o* Register Allocation● ritical ste in all o timi'ing com ilers#●
6 e linear scan algorit m uses li/einter/als to greedily assign %aria+les toregisters#
● O*ten used in L.6 com ilers due to e**iciency#
8/16/2019 History 55
233/234
● Chaitin4s algorithm uses t e registerinter'erence gra%h 2+ased on li/e rangesand gra%h coloring to assign registers#
● 6 e +asis *or t e tec ni$ue used in / #
Fe(t 6ime● Garbage Collection
●
Re*erence ounting● Mar"5and5S!ee● Sto 5and5 o y● .ncremental ollectors
8/16/2019 History 55
234/234
.ncremental ollectors