Compilers (CS362 2002F)

Class 33: Register Allocation

Back to Liveness Analysis. On to Class Canceleld.

Held Wednesday, November 20, 2002


Today we consider how to assign registers to temporaries.



Register Allocation

A Basic Algorithm

Detour: NP-completeness

Optimistic Graph Coloring

An Example

Here's a graph (a list of edges) to try the algorithm on. We'll try to four-color it. Note that this is a modified version of a graph that I took from Appel's book (a book we no longer use for reasons that the first set of CS362 students will be happy to tell you).

(A,B), (A,F), (A,G), (B,C), (B,D), (B,E), (B,K), (B,M), (C,M), (D,J), (D,K), (D,M), (E,F), (E,J), (E,M), (F,J), (F,M), (G,H), (G,I), (G,J), (G,K), (H,I), (H,J), (J,K), (J,L), (L,M)

(I haven't given the underlying program. I don't think we'll need to rewrite the program, but it may depend on choices we make.)


The Algorithm

function allocReg(program p, regcount k) 
  build the interference graph for p
  build the list of move-pairs 
  create an empty stack
  while p is nonempty
        remove nodes of arity < k not in move pairs,
          pushing them on the stack
        join nodes in any move pairs if safe to do so
      if (we neither simplified nor coalesced)
          pick some move-pair node with arity < k, and
            remove all move-pairs with that node
    until no changes can be made
    if there are nodes of arity >= k
        remove some node of arity >= k, 
          pushing it on the stack
    end if
  end while
    while there are nodes on the stack
      if some color can be assigned to the top node on
        the stack then assign that color
      otherwise, note that the top node must be spilled
    if some node was marked as spilled during the select phase,
      then update the program to do the spilling and call allocReg again.
end allocReg



Thursday, 29 August 2002 [Samuel A. Rebelsky]

Wednesday, 20 November 2002 [Samuel A. Rebelsky]


Back to Liveness Analysis. On to Class Canceleld.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri Dec 6 10:38:35 2002.
The source to the document was last modified on Wed Nov 20 10:20:23 2002.
This document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Glimmer Labs: The Grinnell Laboratory for Interactive Multimedia Experimentation & Research