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: 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 ) Register allocation 2
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 Register allocation 3
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 Register allocation 4
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 Register allocation 5
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 Register allocation 6
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 Register allocation 7
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 Register allocation 8
Graph Coloring Example � Example program, similar to 3- live-in : k j g := mem[j+12] address assembly h := k - 1 f := g * h � K = 4 e := mem[j+8] m := mem[j+16] � Build interference graph b := mem[f] c := e + 8 d := c f k := m + 4 j := b live-out : d k j e j k b m d h c g Register allocation 9
Graph Coloring Example � Simplify: live-in : k j g := mem[j+12] • g, h, c, f are light , less than K h := k - 1 f := g * h neighbors e := mem[j+8] • choose g, h, then k for removal m := mem[j+16] b := mem[f] c := e + 8 d := c f k := m + 4 j := b live-out : d k j e j k b m d h c g Register allocation 10
Graph Coloring Example � Simplify: live-in : k j g := mem[j+12] • g, h, c, f are light , less than K h := k - 1 f := g * h neighbors e := mem[j+8] • choose g, h, then k for removal m := mem[j+16] b := mem[f] c := e + 8 d := c f k := m + 4 j := b live-out : d k j e j k b m d h c Register allocation 11
Graph Coloring Example � Simplify: live-in : k j g := mem[j+12] • g, h, c, f are light , less than K h := k - 1 f := g * h neighbors e := mem[j+8] • choose g, h, then k for removal m := mem[j+16] b := mem[f] c := e + 8 d := c f k := m + 4 j := b live-out : d k j e j k b m d c Register allocation 12
Graph Coloring Example � Simplify: live-in : k j g := mem[j+12] • result after removal of g, h, k h := k - 1 f := g * h • then continue to produce stack e := mem[j+8] m := mem[j+16] • run Select for coloring b := mem[f] • (no Spill) c := e + 8 d := c f k := m + 4 j := b live-out : d k j e At end: j b m STACK: d m c b f e j d k h g c COLORING: 1 3 2 2 4 3 4 1 2 4 Register allocation 13
Graph Coloring Example � Note important property: live-in : k j g := mem[j+12] • choice required during Select h := k - 1 f := g * h • NP-completeness: it’s hard! e := mem[j+8] m := mem[j+16] b := mem[f] c := e + 8 d := c f k := m + 4 j := b live-out : d k j e At end: j k b m STACK: d m c b f e j d k h g h c COLORING: g 1 3 2 2 4 3 4 1 2 4 Register allocation 14
Coalescing � Basic idea: If two nodes do not interfere, they could be the same color (register) f � Problem: Merging two nodes e can add a heavy node j k b m from two light ones d h c g � Problem: Making a heavy node heavier could prevent it becoming light enough during Simplify Register allocation 15
Coalescing Criteria � Solution: Criterion that ensures K -colorability preservation f e � Briggs: ensure merged node k j b m has < K heavy neighbors d h c � George: ensure first node to f merge has only light exclusive e neighbors (i.e., not neighbors 2 j k of second node to merge) b m 1 d h c Register allocation 16
Briggs Correctness � Briggs: ensure merged node f has < K heavy neighbors e j k b m d � Let G K -colorable, j,b have K’<K heavy c h 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). Register allocation 17
George Correctness � George: ensure first node to merge f e has only light exclusive neighbors 2 (i.e., not neighbors of second j k b m 1 d node to merge) c h � Let G K -colorable, j,b merging, all exclusive neighbors of j light. Let G’ be G with merged node jb. T hen 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 other 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) Register allocation 18
Graph Coloring with Coalescing � Extend previous algorithm with build extra phases simplify � Simplify modified coalesce � Core addition: coalesce freeze � Needed: freeze, to give up may spill select did spill Register allocation 19
Graph Coloring with Coalescing � Build: as before, but mark end- build points of move edges as move related (‘moving’) simplify � Simplify: remove light nodes if not coalesce move related freeze � Coalesce : enforce Briggs or may spill George criterion; repeat until all nodes heavy or moving select did spill Register allocation 20
Graph Coloring with Coalescing � Freeze: unmark one low degree build moving node, enabling new simplifications simplify � Spill: preferring low degree node, coalesce select and push freeze � Select: pop all, assign colors for may spill each reinsertion select did spill Register allocation 21
Graph Coloring with Coalescing � Do spill: change program as before build � When actual spill occurred, rebuild simplify graph coalesce freeze � Extra corner case: constrained move , where pair has both move may spill and interference (remove ‘moving’ mark) select did spill Register allocation 22
Spilling — déjà vu � 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) Register allocation 23
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 Register allocation 24
Recommend
More recommend