graph coloring register allocation
Download
Skip this Video
Download Presentation
Graph-Coloring Register Allocation

Loading in 2 Seconds...

play fullscreen
1 / 64

Graph-Coloring Register Allocation - PowerPoint PPT Presentation


  • 80 Views
  • Uploaded on

Graph-Coloring Register Allocation. CS153: Compilers Greg Morrisett. Last Time:. Dataflow analysis on CFG's Find iterative solution to equations: Available Expressions (forwards): Din[L] = Dout[L 1 ]  …  Dout[L n ] where pred[L] = {L 1 ,…,L n }

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Graph-Coloring Register Allocation' - urbana


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
graph coloring register allocation

Graph-ColoringRegister Allocation

CS153: Compilers

Greg Morrisett

last time
Last Time:

Dataflow analysis on CFG's

Find iterative solution to equations:

  • Available Expressions (forwards):
    • Din[L] = Dout[L1]  …  Dout[Ln] where pred[L] = {L1,…,Ln}
    • Dout[L] = (Din[L] - Kill[L])  Gen[L]
  • live variable sets (backwards):
    • LiveIn[L] = Gen[L]  (LiveOut[L] - Kill[L])
    • LiveOut[L] = LiveIn[L1]  …  LiveIn[Ln] where succ[L] = {L1,…,Ln}
register allocation
Register Allocation

Goal is to assign each temp to one of k registers.

(The general problem is NP-complete.)

So we use a heuristic that turns out to be poly-time for the particular situation we are in.

  • Build interference graph G
    • G(x,y)=true if x & y are live at same point.
  • Simplify the graph G
    • If x has degree < k, push x and simplify G-{x}
    • if no such x, then we need to spill some temp.
  • Once graph is empty, start popping temps and assigning them registers.
    • Always have a free register since sub-graph G-{x} can't have >= k interfering temps.
example from book
Example from book

{live-in: j, k}

g := *(j+12)

h := k - 1

f := g * h

e := *(j+8)

m := *(j+16)

b := *(f+0)

c := e + 8

d := c

k := m + 4

j := b

{live-out: d,j,k}

f

e

b

m

j

k

d

c

h

g

simplification 4 regs
Simplification (4 regs)

Stack:

f

e

b

m

j

k

d

c

h

g

simplification
Simplification

Stack:

g

f

e

b

m

j

k

d

c

h

g

simplification1
Simplification

Stack:

g

f

e

b

m

j

k

d

c

h

simplification2
Simplification

Stack:

g

h

f

e

b

m

j

k

d

c

h

simplification3
Simplification

Stack:

g

h

f

e

b

m

j

k

d

c

simplification4
Simplification

Stack:

g

h

k

f

e

b

m

j

k

d

c

simplification5
Simplification

Stack:

g

h

k

f

e

b

m

j

d

c

simplification6
Simplification

Stack:

g

h

k

d

f

e

b

m

j

d

c

simplification7
Simplification

Stack:

g

h

k

d

f

e

b

m

j

c

simplification8
Simplification

Stack:

g

h

k

d

j

f

e

b

m

j

c

simplification9
Simplification

Stack:

g

h

k

d

j

e

f

e

b

m

c

simplification10
Simplification

Stack:

g

h

k

d

j

e

f

f

b

m

c

simplification11
Simplification

Stack:

g

h

k

d

j

e

f

b

b

m

c

simplification12
Simplification

Stack:

g

h

k

d

j

e

f

b

c

m

c

simplification13
Simplification

Stack:

g

h

k

d

j

e

f

b

c

m

select
Select:

Stack:

g

h

k

d

j

e

f

b

c

m

f

e

b

m

j

k

d

c

h

g

select1
Select:

Stack:

g

h

k

d

j

e

f

b

c

f

e

b

m

j

k

d

c

h

g

select2
Select:

Stack:

g

h

k

d

j

e

f

b

f

e

b

m

j

k

d

c

h

g

select3
Select:

Stack:

g

h

k

d

j

e

f

f

e

b

m

j

k

d

c

h

g

select4
Select:

Stack:

g

h

k

d

j

e

f

e

b

m

j

k

d

c

h

g

select5
Select:

Stack:

g

h

k

d

j

f

e

b

m

j

k

d

c

h

g

select6
Select:

Stack:

g

h

k

d

f

e

b

m

j

k

d

c

h

g

select7
Select:

Stack:

g

h

k

f

e

b

m

j

k

d

c

h

g

select8
Select:

Stack:

g

h

f

e

b

m

j

k

d

c

h

g

select9
Select:

Stack:

g

f

e

b

m

j

k

d

c

h

g

use coloring to codegen
Use coloring to codegen:

g := *(j+12)

h := k - 1

f := g * h

e := *(j+8)

m := *(j+16)

b := *(f+0)

c := e + 8

d := c

k := m + 4

j := b

f

e

b

m

j

k

d

c

h

g

t3

t4

t2

t1

use coloring to codegen1
Use coloring to codegen:

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

f

e

b

m

j

k

d

c

h

g

Notice that we can simplify

away moves such as this one…

spilling
Spilling…
  • Suppose all of the nodes in the graph have degree >= k.
  • Pick one of the nodes to spill.
    • Picking a high-degree temp will make it more likely that we can color the rest of the graph.
    • Picking a temp that is used infrequently will likely generate better code.
      • e.g., spilling a register used in a loop when we could spill one accessed outside the loop is a bad idea…
  • Rewrite the code:
    • after definition of temp, write it into memory.
    • before use of temp, load it into another temp.
try it with 3 registers
Try it with 3 registers…

{live-in: j, k}

g := *(j+12)

h := k - 1

f := g * h

e := *(j+8)

m := *(j+16)

b := *(f+0)

c := e + 8

d := c

k := m + 4

j := b

{live-out: d,j,k}

f

e

b

m

j

k

d

c

h

g

simplify
Simplify:

Stack:

h

f

e

b

m

j

k

d

c

h

g

simplify1
Simplify:

Stack:

h

c

f

e

b

m

j

k

d

c

g

simplify2
Simplify:

Stack:

h

c

g

f

e

b

m

j

k

d

g

simplify3
Simplify:

Stack:

h

c

g

f

e

b

m

j

k

d

3 regs
3 Regs

Stack:

h

c

g

f

e

b

m

j

k

d

We're stuck…

3 regs1
3 Regs

{live-in: j, k}

g := *(j+12)

h := k - 1

f := g * h

e := *(j+8)

m := *(j+16)

b := *(f+0)

c := e + 8

d := c

k := m + 4

j := b

{live-out: d,j,k}

f

e

b

m

j

k

d

Don't want to spill j, it's used a lot.

Don't want to spill f or k, they have relatively low degree.

So let's pick m…

rewrite
Rewrite:

{live-in: j, k}

g := *(j+12)

h := k - 1

f := g * h

e := *(j+8)

m := *(j+16)

*(fp+) := m

b := *(f+0)

c := e + 8

d := c

m2 := *(fp+)

k := m2 + 4

j := b

{live-out: d,j,k}

Eliminated this chunkof code from m's live range…

new interference graph
New Interference Graph

{live-in: j, k}

g := *(j+12)

h := k - 1

f := g * h

e := *(j+8)

m := *(j+16)

*(fp+) := m

b := *(f+0)

c := e + 8

d := c

m2 := *(fp+)

k := m2 + 4

j := b

{live-out: d,j,k}

f

e

b

m

j

k

d

c

h

g

m2

simplify4
Simplify:

Stack:

m2

c

f

e

b

m

j

k

d

c

h

g

m2

simplify5
Simplify:

Stack:

m2

c

h

f

e

b

m

j

k

d

h

g

back to simplification
Back to simplification…

Stack:

m2

c

h

m

f

e

b

m

j

k

d

g

back to simplification1
Back to simplification…

Stack:

m2

c

h

m

f

f

e

b

j

k

d

g

back to simplification2
Back to simplification…

Stack:

m2

c

h

m

f

g

e

b

j

k

d

g

back to simplification3
Back to simplification…

Stack:

m2

c

h

m

f

g

e

e

b

j

k

d

back to simplification4
Back to simplification…

Stack:

m2

c

h

m

f

g

e

j

b

j

k

d

back to simplification5
Back to simplification…

Stack:

m2

c

h

m

f

g

e

j

k

b

k

d

back to simplification6
Back to simplification…

Stack:

m2

c

h

m

f

g

e

j

k

d

b

d

back to simplification7
Back to simplification…

Stack:

m2

c

h

m

f

g

e

j

k

d

b

b

then color
Then Color

Stack:

m2

c

h

m

f

g

e

j

k

d

b

f

e

b

m

j

k

d

c

h

g

m2

then color1
Then Color

Stack:

m2

c

h

m

f

g

e

j

k

d

f

e

b

m

j

k

d

c

h

g

m2

then color2
Then Color

Stack:

m2

c

h

m

f

g

e

j

k

f

e

b

m

j

k

d

c

h

g

m2

then color3
Then Color

Stack:

m2

c

h

m

f

g

e

j

f

e

b

m

j

k

d

c

h

g

m2

then color4
Then Color

Stack:

m2

c

h

m

f

g

e

f

e

b

m

j

k

d

c

h

g

m2

then color5
Then Color

Stack:

m2

c

h

m

f

g

f

e

b

m

j

k

d

c

h

g

m2

then color6
Then Color

Stack:

m2

c

h

m

f

f

e

b

m

j

k

d

c

h

g

m2

then color7
Then Color

Stack:

m2

c

h

m

f

e

b

m

j

k

d

c

h

g

m2

then color8
Then Color

Stack:

m2

c

h

f

e

b

m

j

k

d

c

h

g

m2

then color9
Then Color

Stack:

m2

c

f

e

b

m

j

k

d

c

h

g

m2

then color10
Then Color

Stack:

m2

f

e

b

m

j

k

d

c

h

g

m2

register pressure
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.
next time
Next Time:
  • How to do coalescing register allocation.
  • An optimistic spilling strategy.
  • Some real-world issues:
    • caller/callee-saves registers
    • fixed resources (e.g., mflo, mfhi)
    • allocation of stack slots
ad