register allocation graph
TRANSCRIPT
-
8/9/2019 Register Allocation Graph
1/31
Register allocation problems(via graph colouring)
by: Navdeep singh
-
8/9/2019 Register Allocation Graph
2/31
Lecture Outline
• Memory Hierarchy Management
• Register Allocation
– Register interference graph
– Graph coloring heuristics
– Spilling
-
8/9/2019 Register Allocation Graph
3/31
-
8/9/2019 Register Allocation Graph
4/31
Current Trends to manage memory hierarchy
• .ache and register si/es are gro#ing slo#ly
• "rocessor speed improves faster than memory speed anddis$ speed– 0he cost of a cache miss is gro#ing– 0he #idening gap is bridged #ith more caches
• (t is very important to:
– Manage registers properly– Manage caches properly
• .ompilers are good at managing registers
-
8/9/2019 Register Allocation Graph
5/31
The Register Allocation ProblemThe Register Allocation Problem
• Recall that intermediate code uses as many temporariesas necessary– 0his complicates final translation to assembly– 1ut simplifies code generation and optimi/ation
– 0ypical intermediate code uses too many temporaries
• 0he register allocation problem:– Re#rite the intermediate code intermediate code to use e!er temporaries than
there are machine registers
– Method"Method" assign more temporaries to a register• 1ut #ithout changing the program behavior
-
8/9/2019 Register Allocation Graph
6/31
-
8/9/2019 Register Allocation Graph
7/31
%asic Register Allocation &dea
• 0he value in a dead temporary is not neededfor the rest of the computation– A dead temporary can be reused
• %asic rule%asic rule:
– 0emporaries t and t % can share the same
register if at any point in the program atmost one of t or t % is live 7
-
8/9/2019 Register Allocation Graph
8/31
Algorithm to minimi'e the number oregisters" Part &
• .ompute live variables for each point:
a :2 b 3 cd :2 4a
e :2 d 3 f
f :2 % 8 eb :2 d 3 e
e :2 e 4
b :2 f 3 c
9b
9c'e
9b
9c'f 9c'f
9b'c'e'f
9c'd'e'f
9b'c'f
9c'd'f9a'c'f
a
f
e
d
c
b
{a,b} not
included in
any subset
-
8/9/2019 Register Allocation Graph
9/31
The Register &ntererence raphThe Register &ntererence raph
• 0#o temporaries that are live simultaneouslycannot be allocated in the same register same register
• ;e construct an undirected graph– A node for each temporary
– An edge bet#een t and t% if they are livesimultaneously at some point in the program
• 0his is the register interference graph &R(G+– 0#o temporaries can be allocated to the same
register if there is no edge connecting them
-
8/9/2019 Register Allocation Graph
10/31
Register &ntererence raph #$ample
• !g!' b and c cannot be in the same register
• >!g!' b and d can be in the same register
a :2 b 3 cd :2 4a
e :2 d 3 f
f :2 % 8 eb :2 d 3 e
e :2 e 4
b :2 f 3 c
-
8/9/2019 Register Allocation Graph
11/31
Register &ntererence raphRegister &ntererence raph Properties
! (t e=tracts e=actly the information neededto characteri/e legal register assignments
%! (t gives a global &i!e!' over the entire flo#graph+ picture of the register re?uirements
,! After R(G construction the registerallocation algorithm is architectureindependent
-
8/9/2019 Register Allocation Graph
12/31
raph Coloring *einitionsraph Coloring *einitions
• A coloring of a graph is an assignment ofcolors to nodes' such that nodes connected by
an edge have different colors
• A graph is $4colorable if it has a coloring #ith$ colors
-
8/9/2019 Register Allocation Graph
13/31
Register Allocation Through raph Coloring
• (n our problem' colors + registers colors + registers – ;e need to assign colors ®isters+ to graph nodes
&temporaries+
• @et $ 2 number of machine registers
• (f the R(G is $4colorable then there is aregister assignment that uses no more than $registers
Register Interference Graph
-
8/9/2019 Register Allocation Graph
14/31
raph Coloring #$ample
• .onsider the e=ample R(G
a
f
e
d
c
b
• 0here is no coloring #ith less than - colors
• 0here are -4colorings of this graph
r-
r
r%
r,
r%
r,
-
8/9/2019 Register Allocation Graph
15/31
raph Coloring #$ample
• nder this coloring the code becomes:
r% :2 r, 3 r-r, :2 4r%
r% :2 r, 3 r
r :2 % 8 r%r, :2 r, 3 r%r% :2 r% 4
r, :2 r 3 r-
-
8/9/2019 Register Allocation Graph
16/31
Computing raph ColoringsComputing raph Colorings
• 0he remaining problem is to compute acoloring for the interference graph
• 1ut:
! 0his problem is very hard &N"4hard+! No efficientalgorithms are $no#n!
%! A coloring might not e=ist for a given number orregisters
• 0he solution to &+ is to use heuristics• ;eBll consider later the other problem
-
8/9/2019 Register Allocation Graph
17/31
raph Coloring Heuristic
• *bservation:– "ic$ a node t #ith fe#er than $ neighbors in R(G– >liminate t and its edges from R(G– (f the resulting graph has a $4coloring then so does
the original graph
• ;hy:
– @et c'C'cn be the colors assigned to the neighbors oft in the reduced graph– Since n D $ #e can pic$ some color for t that is
different from those of its neighbors
-
8/9/2019 Register Allocation Graph
18/31
raph Coloring Heuristic
• 0he follo#ing #or$s #ell in practice:– "ic$ a node t #ith fe#er than $ neighbors
– "ut t on a stac$ and remove it from the R(G
– Repeat until the graph has one node
• 0hen start assigning colors to nodes on thestac$ &starting #ith the last node added+– At each step pic$ a color different from those
assigned to already colored neighbors
-
8/9/2019 Register Allocation Graph
19/31
raph Coloring #$ample (,)raph Coloring #$ample (,)
• Remove a and then d
a
f
e
d
c
b
• Start #ith the R(G and #ith $ 2 -:
Stac$: 9
-
8/9/2019 Register Allocation Graph
20/31
raph Coloring #$ample (-)
• No# all nodes have fe#er than - neighborsand can be removed: c' b' e' f
f
e c
b
Stac$: 9d' a
-
8/9/2019 Register Allocation Graph
21/31
raph Coloring #$ample (-)
• Start assigning colors to: f' e' b' c' d' a
b
a
e c r-
fr
r%
r,
r%
r,
d
-
8/9/2019 Register Allocation Graph
22/31
.hat i the Heuristic /ails0.hat i the Heuristic /ails0
• ;hat if during simplification #e get to a state #here allnodes have $ or more neighbors E
• >=ample: try to find a ,4coloring of the R(G:
a
f
e
d
c
b
-
8/9/2019 Register Allocation Graph
23/31
.hat i the Heuristic /ails0
• Remove a and get stuc$ &as sho#n belo#+
f
e
d
c
b
• "ic$ a node as a candidate for spilling– A spilled temporary 1lives2 in memory
• Assume that f is pic$ed as a candidate
-
8/9/2019 Register Allocation Graph
24/31
.hat i the Heuristic /ails0
• Remove f and continue the simplification– Simplification no# succeeds: b' d' e' c
e
d
c
b
-
8/9/2019 Register Allocation Graph
25/31
.hat i the Heuristic /ails0
• *n the assignment phase #e get to the point#hen #e have to assign a color to f
• ;e hope that among the - neighbors of f #euse less than , colors ⇒ optimistic coloring
f
e
d
c
b r,
rr%
r,
E
ll3 illi
-
8/9/2019 Register Allocation Graph
26/31
3pilling3pilling
• Since optimistic coloring failed #e must spill temporaryf
• ;e must allocate a memory location as the home of f– 0ypically this is in the current stac$ frame– .all this address fa
• 1efore each operation that uses f' insert f :2 load fa
• After each operation that defines f' insert store f' fa
-
8/9/2019 Register Allocation Graph
27/31
3pilling #$ample
• 0his is the ne# code after spilling f
a :2 b 3 cd :2 4af :2 load fae :2 d 3 f
f :2 % 8 estore f' fa
b :2 d 3 e
e :2 e 4
f :2 load fab :2 f 3 c
-
8/9/2019 Register Allocation Graph
28/31
Recomputing Liveness &normationRecomputing Liveness &normation
• 0he ne# liveness information after spilling:
a :2 b 3 cd :2 4af :2 load fae :2 d 3 f
f :2 % 8 estore f' fa
b :2 d 3 e
e :2 e 4
f :2 load fab :2 f 3 c
9b
9c'e
9b
9c'f9c'f
9b'c'e'f
9c'd'e'f
9b'c'f
9c'd'f9a'c'f
9c'd'f
9c'f
9c'f
-
8/9/2019 Register Allocation Graph
29/31
Recomputing Liveness &normation
• 0he ne# liveness information is almost as before
• f is live only– 1et#een a f :2 load fa and the ne=t instruction
– 1et#een a store f' fa and the preceding instructiom!
• Spilling reduces the live range of f
• And thus reduces its interferences
• ;hich result in fe#er neighbors in R(G for f
-
8/9/2019 Register Allocation Graph
30/31
Recompute R& Ater 3pilling
• 0he only changes are in removing some of the edges
of the spilled node
• (n our case f still interferes only #ith c and d
• And the resulting R(G is ,4colorable
a
f
e
d
c
b
-
8/9/2019 Register Allocation Graph
31/31