REGISTER ALLOCATION
Graph coloring meets hardware limits
a, b and c are all live (still needed later) after the same instruction, and the machine has 2 registers. What must happen?
Programs use many variables; a CPU has a few registers. Two variables interfere if both are live at the same point, so they cannot share a register. Draw a node per variable and an edge per interference: allocation is colouring that graph with K colours, one per register.
Chaitin’s allocator, with Briggs’s optimistic refinement, removes nodes with fewer than K neighbours (they can always be coloured), then colours them in reverse. When every node has K or more neighbours it is stuck. It pushes a spill candidate anyway, and spills it only if no register is free for it on the way back: store after the definition, load before each use. The split live ranges are short, so the rewritten code usually colours; if not, the allocator repeats.
Chaitin/Briggs graph-colouring allocation
build: liveness backwards; a def interferes with everything live out of it
simplify: while some node has degree < K: remove it, push it
(it is colourable whatever its neighbours get)
spill?: stuck (all degree >= K): push the node with the lowest
spill cost / degree as a candidate, keep simplifying
select: pop each node, give it the lowest register its
coloured neighbours do not hold; none free => actual spill
rewrite: store x after its def, t = load x before each use
(tiny live ranges), then start again from build The invariant strip checks every interference edge. NAIVE runs out of registers and reuses R1 for c while a still lives there. SPILL & RETRY gets stuck on the same triangle, spills b (refs/degree 3/4, the cheapest), and colours the rewritten code with two registers. Here b is a constant, so a real allocator would rematerialise it (re-emit b = 2 before each use) instead of storing and loading it; the lesson spills it to show the general store-and-load case. Drag K to 3 and the spill disappears.
With K = 2 the allocator has to spill. Find the smallest number of registers at which this program needs no spill code at all.
GOAL: no spills, and one register fewer would spill
Implement color(graph, K). Return \{ assignment, spills \}: assignment[v] is a register 0..K-1, and every node is either assigned or in spills. No two neighbours may share a register. Spill only when you must: simplify nodes of degree < K first; when stuck, push the node with the lowest cost[v] / degree(v) (full-graph degree) and keep going; spill only nodes that find no free register on the way back.
Every optimising compiler back end: GCC's IRA, LLVM's greedy allocator (live-range splitting plus spill weights), JIT compilers using linear scan for speed. The symptom of pressure is in the disassembly: stack loads and stores inside a hot loop.
“How does a compiler assign registers?” Describe liveness, the interference graph, simplify/select, and what a spill is; explain why spilling a high-degree, rarely used variable is cheapest, and mention linear scan as the fast alternative in JITs.