Register Allocation Based on slides by E. Ernst Register Allocation - - PowerPoint PPT Presentation

register allocation
SMART_READER_LITE
LIVE PREVIEW

Register Allocation Based on slides by E. Ernst Register Allocation - - PowerPoint PPT Presentation

Compilation 2016 Register Allocation Based on slides by E. Ernst Register Allocation Recall: Interference graph Node: temporary Edge: interference (cannot unify end points) Undirected Unification of temporaries:


slide-1
SLIDE 1

Compilation 2016

Register Allocation


 
 Based on slides by E. Ernst

slide-2
SLIDE 2

Register allocation

Register Allocation

Recall: Interference graph

  • Node: temporary
  • Edge: interference (cannot unify end points)
  • Undirected

Unification of temporaries: graph coloring

  • Neighbors ⇒ different colors
  • K-coloring of interference graph = register allocation
  • If no K-coloring exists: spill and repeat

Basic parameter: We have K registers Useful concept: Significant degree: degree(n) ≥ K Convenient words for it: A heavy node (vs light)

2

slide-3
SLIDE 3

Register allocation

Graph Coloring

The basic problem is NP-complete (polynomial to verify, exponential to compute) We are lucky: Good approximation linear! Algorithm:

  • build interference graph G
  • simplify G
  • spill some nodes
  • select colors

3

slide-4
SLIDE 4

Register allocation

Graph Coloring: Build

Build the interference graph Recall how:

  • build data flow graph
  • compute use/def locally, then live-in/live-out via iteration
  • create interference graph node per temp, edge per pair of

temps with overlapping live ranges

4

slide-5
SLIDE 5

Register allocation

Graph Coloring: Simplify

Reducing the graph G, preserving colorability Algorithm:

  • repeat { find light node n; remove n from G; push n }

For each step we have: Graph K-colorable after removal ⇒ also K-colorable before removal Reason: Node n is light, i.e., at most K-1 colors used Stopping: Every node is heavy

5

slide-6
SLIDE 6

Register allocation

Graph Coloring: Spill

Remove one node from the graph G, marking it as a ‘potential spill’ Algorithm:

  • find heavy node n; remove n; mark n ‘spill’; push n

Got here because Simplify stopped

  • If G non-empty: all nodes heavy, proceed, go to Simplify
  • If G empty: go to Select

6

slide-7
SLIDE 7

Register allocation

Graph Coloring: Select

Pop and re-insert all nodes from the stack Algorithm:

  • repeat { n=pop; add n with edges to G; color n }

Cases

  • n was light: reinsert/color always works
  • n was heavy (marked ‘spill’): go ahead and try! ;-)
  • it worked — continue
  • it failed — insert w/o color, continue, noting failure

Stopping: stack empty

7

slide-8
SLIDE 8

Register allocation

Graph Coloring: Start over

Perform spills, if any Algorithm:

  • rewrite program: add load/store of temp at use/def, using

new, short-lived temporaries

Changed program ⇒ recompute all (go to build) Note: entire algorithm typically repeats only 1-2 times

8

slide-9
SLIDE 9

Register allocation

Graph Coloring Example

Example program, similar to 3- address assembly K = 4 Build interference graph

9

live-in: k j g := mem[j+12] h := k - 1 f := g * h e := mem[j+8] m := mem[j+16] b := mem[f] c := e + 8 d := c k := m + 4 j := b live-out: d k j f e m j k h d g b c

slide-10
SLIDE 10

Register allocation

Graph Coloring Example

Simplify:

  • g, h, c, f are light, less than K

neighbors

  • choose g, h, then k for removal

10

live-in: k j g := mem[j+12] h := k - 1 f := g * h e := mem[j+8] m := mem[j+16] b := mem[f] c := e + 8 d := c k := m + 4 j := b live-out: d k j f e m j k h d g b c

slide-11
SLIDE 11

Register allocation

Graph Coloring Example

Simplify:

  • g, h, c, f are light, less than K

neighbors

  • choose g, h, then k for removal

11

live-in: k j g := mem[j+12] h := k - 1 f := g * h e := mem[j+8] m := mem[j+16] b := mem[f] c := e + 8 d := c k := m + 4 j := b live-out: d k j f e m j k h d b c

slide-12
SLIDE 12

Register allocation

Graph Coloring Example

Simplify:

  • g, h, c, f are light, less than K

neighbors

  • choose g, h, then k for removal

12

live-in: k j g := mem[j+12] h := k - 1 f := g * h e := mem[j+8] m := mem[j+16] b := mem[f] c := e + 8 d := c k := m + 4 j := b live-out: d k j f e m j k d b c

slide-13
SLIDE 13

Register allocation

Graph Coloring Example

Simplify:

  • result after removal of g, h, k
  • then continue to produce stack
  • run Select for coloring
  • (no Spill)

13

live-in: k j g := mem[j+12] h := k - 1 f := g * h e := mem[j+8] m := mem[j+16] b := mem[f] c := e + 8 d := c k := m + 4 j := b live-out: d k j f e m j d b c

STACK: m c b f e j d k h g COLORING: 1 3 2 2 4 3 4 1 2 4

At end:

slide-14
SLIDE 14

Register allocation

Graph Coloring Example

Note important property:

  • choice required during Select
  • NP-completeness: it’s hard!

14

live-in: k j g := mem[j+12] h := k - 1 f := g * h e := mem[j+8] m := mem[j+16] b := mem[f] c := e + 8 d := c k := m + 4 j := b live-out: d k j

STACK: m c b f e j d k h g COLORING: 1 3 2 2 4 3 4 1 2 4

At end:

f e m j k h d g b c

slide-15
SLIDE 15

Register allocation

Coalescing

Basic idea: If two nodes do not interfere, they could be the same color (register) Problem: Merging two nodes
 can add a heavy node 
 from two light ones Problem: Making a heavy node
 heavier could prevent it becoming light
 enough during Simplify

15

f e m j k h d g b c

slide-16
SLIDE 16

Register allocation

Coalescing Criteria

Solution: Criterion that ensures
 K-colorability preservation Briggs: ensure merged node
 has <K heavy neighbors George: ensure first node to
 merge has only light exclusive
 neighbors (i.e., not neighbors


  • f second node to merge)

16

f e m j k h d b c f e m j k h d b c

1 2

slide-17
SLIDE 17

Register allocation

Briggs Correctness

Briggs: ensure merged node
 has <K heavy neighbors Let G K-colorable, j,b have K’<K heavy
 neighbors, G’ is G with merged node jb. Then G' is K- colorable Proof: Assume C K-coloring of G, simplify G' to remove all light neighbors of j and b in G, then remove jb (ok: K’ neighbors now). Transfer colors from C to the remaining G’. Reinsert jb with the color of j; reinsert the remaining nodes --- they are all light: removed by Simplify; they can have color from C because their neighbors have at most the same colors (color of b may be gone).

17

f e m j k h d b c

slide-18
SLIDE 18

Register allocation

George Correctness

George: ensure first node to merge 
 has only light exclusive neighbors 
 (i.e., not neighbors of second 
 node to merge) Let G K-colorable, j,b merging, all exclusive neighbors


  • f j light. Let G’ be G with merged node jb. Then G' is

K-colorable Proof: Assume C K-coloring of G. Simplify G' removing all light neighbors of j, then transfer all colors of C to

  • ther nodes than jb; color jb with the color of b (now, all

neighbors of jb are neighbors of b in G, so that color is OK for jb); then reinsert and color all the missing neighbors of j (they are all light: immediately colorable)

18

f e m j k h d b c

1 2

slide-19
SLIDE 19

Register allocation

Graph Coloring with Coalescing

Extend previous algorithm with extra phases Simplify modified Core addition: coalesce Needed: freeze, to give up

19 build simplify coalesce freeze may spill select did spill

slide-20
SLIDE 20

Register allocation

Graph Coloring with Coalescing

Build: as before, but mark end- points of move edges as move related (‘moving’) Simplify: remove light nodes if not move related Coalesce: enforce Briggs or George criterion; repeat until all nodes heavy or moving

20 build simplify coalesce freeze may spill select did spill

slide-21
SLIDE 21

Register allocation

Graph Coloring with Coalescing

Freeze: unmark one low degree moving node, enabling new simplifications Spill: preferring low degree node, select and push Select: pop all, assign colors for each reinsertion

21 build simplify coalesce freeze may spill select did spill

slide-22
SLIDE 22

Register allocation

Graph Coloring with Coalescing

Do spill: change program as before When actual spill occurred, rebuild graph Extra corner case: constrained move, where pair has both move and interference (remove ‘moving’ mark)

22 build simplify coalesce freeze may spill select did spill

slide-23
SLIDE 23

Register allocation

Spilling — déjà vu

23

When rerunning build, we can preserve coalescing nodes created before first spill was discovered Stack frame can grow wildly due to spilled temps May well have disjoint live ranges: Use graph coloring with coalescing! NB: no limit on stack frame size, as if K = ∞, just coalesce aggressively (no criteria)

slide-24
SLIDE 24

Register allocation

Summary

Register allocation builds on interference graph Idea: colored nodes represent choice of registers Graph coloring NP-complete, but linear approx. Algorithm: build simplify spill select start_over Coalescing: merge two non-interfering nodes Problem: creates ‘heavier’ nodes Solution: criteria (Briggs, George) Enhanced algorithm: build simplify coalesce freeze may_spill select did_spill Can use aggressive coalescing on the stack

24