CS153: Lecture 20: Register Allocation I

Stephen Chong https://www.seas.harvard.edu/courses/cs153 Pre-class Puzzle

•What’s the minimum number of colors needed to color a map of the USA?

•Every state is assigned one color •Adjacent states must be given different colors Stephen Chong, Harvard University https://printable-maps.blogspot.com/2011/12/blank-map-of-united-states.html 2 Pre-class Puzzle Answer

•4 •Four-color theorem says ≤4 •Must be at least 4: Green •Suppose we had only 3 colors Red •Pick some colors for CA and OR Blue (Red and Green) !!! •NV must be Blue Red •ID must be Red Green •AZ must be Green •UT!!!!!! Stephen Chong, Harvard University 3 Announcements

•Project 5 out •Due Tuesday Nov 13 (5 days) •Project 6 out •Due Tuesday Nov 20 (12 days) •Project 7 out •Due Thursday Nov 29 (21 days) •Project 8 will be released on Tuesday •Due Saturday Dec 8

Stephen Chong, Harvard University 4 Today

•Register allocation • by simplification •Coalescing

Stephen Chong, Harvard University 5 Register Allocation

Source Code

Parsing

Front end Elaboration

Lowering Back end

Optimization

Code Generation

Target Code

•From an intermediate representation with unlimited number of “temporary”/local variables •Assign temporary variables to the (small) number of machine registers Stephen Chong, Harvard University 6 Register Allocation

•Register allocation is in generally an NP-complete problem •Can we allocate all these n temporaries to k registers? •But we have a heuristic that is linear in practice! •Based on graph coloring • Given a graph, can we assign one of k colors to each node such that connected nodes have different colors? •Here, nodes are temp variables, an edge between t1 and t2 means that t1 and t2 are live at the same time. Colors are registers. •But graph coloring is also NP-complete! How does that work?

Stephen Chong, Harvard University 7 Coloring by Simplification

•Four phases •Build: construct interference graph, using dataflow analysis to find for each program point vars that are live at the same time •Simplify: color based on simple heuristic •If graph G has node n with k-1 edges, then G-{n} is k-colorable iff G is k-colorable •So remove nodes with degree

Build Simplify Spill Select

Stephen Chong, Harvard University 8 Example

From Appel Interference graph f {live-in: j, k} g := *(j+12) h := k - 1 e f := g * h j m e := *(j+8) k b m := *(j+16) b := *(f+0) h c := e + 8 d := c d c k := m + 4 g j := b {live-out: d,j,k}

Stephen Chong, Harvard University 9 Simplification (4 registers)

Choose any node with degree <4 Stack: f g

e j m k b

h d c g

Stephen Chong, Harvard University 10 Simplification (4 registers)

Choose any node with degree <4 Stack: f g h e j m k b

h d c

Stephen Chong, Harvard University 11 Simplification (4 registers)

Choose any node with degree <4 Stack: f g h k e j m k b

d c

Stephen Chong, Harvard University 12 Simplification (4 registers)

Choose any node with degree <4 Stack: f g h k e d j m b

d c

Stephen Chong, Harvard University 13 Simplification (4 registers)

Choose any node with degree <4 Stack: f g h k e d j m b j

c

Stephen Chong, Harvard University 14 Simplification (4 registers)

Choose any node with degree <4 Stack: f g h k e d m b j e c

Stephen Chong, Harvard University 15 Simplification (4 registers)

Choose any node with degree <4 Stack: f g h k d m b j e f c

Stephen Chong, Harvard University 16 Simplification (4 registers)

Choose any node with degree <4 Stack: g h k d m b j e f c b

Stephen Chong, Harvard University 17 Simplification (4 registers)

Choose any node with degree <4 Stack: g h k d m j e f c b c

Stephen Chong, Harvard University 18 Simplification (4 registers)

Choose any node with degree <4 Stack: g h k d m j e f b c m Stephen Chong, Harvard University 19 Select (4 registers)

Graph is now empty! Stack: Color nodes in order of stack f g h k e d j m k b j e h f d c b g c m =t1 =t2 =t3 =t4 Stephen Chong, Harvard University 20 Select (4 registers)

f g := *(j+12) h := k - 1 f := g * h e e := *(j+8) j m m := *(j+16) k b b := *(f+0) c := e + 8 d := c h k := m + 4 d c j := b g

=t1 =t2 =t3 =t4

Stephen Chong, Harvard University 21 Select (4 registers)

f $t2 := *(t4+12) $t1 := $t1 - 1 $t2 := $t2 * $t1 e $t3 := *($t4+8) j m $t1 := *($t4+16) k b $t2 := *($t2+0) $t3 := $t3 + 8 $t3 := $t3 h $t1 := $t1 + 4 d c $t4 := $t2 g

Some moves might subsequently be simplified... =t1 =t2 =t3 =t4

Stephen Chong, Harvard University 22 Spilling

•This example worked out nicely! •Always had nodes with degree

Stephen Chong, Harvard University 23 Example

From Appel Interference graph f {live-in: j, k} g := *(j+12) h := k - 1 e f := g * h j m e := *(j+8) k b m := *(j+16) b := *(f+0) h c := e + 8 d := c d c k := m + 4 g j := b {live-out: d,j,k}

Stephen Chong, Harvard University 24 Simplification (3 registers)

Choose any node with degree <3 Stack: f h

e j m k b

h d c g

Stephen Chong, Harvard University 25 Simplification (3 registers)

Choose any node with degree <3 Stack: f h c e j m k b

d c g

Stephen Chong, Harvard University 26 Simplification (3 registers)

Choose any node with degree <3 Stack: f h c g e j m k b

d g

Stephen Chong, Harvard University 27 Simplification (3 registers)

Choose any node with degree <3 Stack: f h c g e j m k b

Now we are stuck! No nodes with degree <3 Pick a node to potentially spill d

Stephen Chong, Harvard University 28 Which Node to Spill?

•Want to pick a node (i.e., temp variable) f that will make it likely we’ll be able to e k color graph m •High degree (≈ live at j k b many program points) •Not used/defined very often (so we don’t need to access stack d very often) Uses+defs Uses+defs •E.g., compute spill outside loop + in loop ×10 priority of node degree of node Stephen Chong, Harvard University 29 Which Node to Spill?

{live-in: j, k} g := *(j+12) f h := k - 1 f := g * h e := *(j+8) e m := *(j+16) m b := *(f+0) j c := e + 8 k b d := c k := m + 4 j := b {live-out: d,j,k} d Uses+defs Uses+defs outside loop + in loop ×10 Spill priority = degree of node Stephen Chong, Harvard University 30 Simplification (3 registers)

Choose any node with degree <3 Stack: f h c g e d spill? j m k b

d

Pick a node with small spill priority degree to potentially spill

Stephen Chong, Harvard University 31 Simplification (3 registers)

Choose any node with degree <3 Stack: f h c g e d spill? j m k k b

Stephen Chong, Harvard University 32 Simplification (3 registers)

Choose any node with degree <3 Stack: f h c g e d spill? j m k b j

Stephen Chong, Harvard University 33 Simplification (3 registers)

Choose any node with degree <3 Stack: f h c g e d spill? m k b j b

Stephen Chong, Harvard University 34 Simplification (3 registers)

Choose any node with degree <3 Stack: f h c g e d spill? m k j b e

Stephen Chong, Harvard University 35 Simplification (3 registers)

Choose any node with degree <3 Stack: f h c g d spill? m k j b e f

Stephen Chong, Harvard University 36 Simplification (3 registers)

Choose any node with degree <3 Stack: h c g d spill? m k j b e f m

Stephen Chong, Harvard University 37 Select (3 registers)

Graph is now empty! Stack: Color nodes in order of stack f h c g e d spill? j m k k b j h b e d c g f m =t1 =t2 =t3

Stephen Chong, Harvard University 38 Select (3 registers)

Stack: f h c g e d spill? j m k b We got unlucky! h In some cases a potential spill node is still colorable, and the d c Select phase can continue. g

But in this case, we need to rewrite... =t1 =t2 =t3

Stephen Chong, Harvard University 39 Select (3 registers)

•Spill d {live-in: j, k} {live-in: j, k} g := *(j+12) g := *(j+12) h := k - 1 h := k - 1 f := g * h f := g * h e := *(j+8) e := *(j+8) m := *(j+16) m := *(j+16) b := *(f+0) b := *(f+0) c := e + 8 c := e + 8 d := c d := c *:=d k := m + 4 k := m + 4 j := b j := b {live-out: d,j,k} d2:=* {live-out: d2,j,k} Stephen Chong, Harvard University 40 Build

{live-in: j, k} f g := *(j+12) h := k - 1 f := g * h e e := *(j+8) j m m := *(j+16) k b b := *(f+0) c := e + 8 h d := c d *:=d c k := m + 4 g d2 j := b d2:=* {live-out: d2,j,k} Stephen Chong, Harvard University 41 Simplification (3 registers)

Choose any node with degree <3 Stack: f h c g e d j m d2 k b k h b d m c g d2 e f This time we succeed and j will be able to complete Select phase successfully! Stephen Chong, Harvard University 42 Register Pressure

•Some optimizations increase live-ranges: •Copy propagation •Common sub-expression elimination •Loop invariant removal •In turn, that can cause the allocator to spill •Copy propagation isn't that useful anyway: •Let register allocator figure out if it can assign the same register to two temps! •Then the copy can go away. •And we don't have to worry about register pressure. Stephen Chong, Harvard University 43 Coalescing Register Allocation

•If we have “x := y” and x and y have no edge in the interference graph, we might be able to assign them the same color. •This would translate to “ri := ri” which would then be removed •One idea is to optimistically coalesce nodes in the interference graph •Just take the edges to be the union

Stephen Chong, Harvard University 44 Example

•E.g., the following nodes could be coalesced •d and c f •j and b {live-in: j, k} g := *(j+12) e h := k - 1 f := g * h j m e := *(j+8) k b m := *(j+16) b := *(f+0) c := e + 8 h d := c k := m + 4 d c j := b g {live-out: d,j,k} Stephen Chong, Harvard University 45