REGISTER ALLOCATION

Graph coloring meets hardware limits

L218 MINCOMPILERS
PREDICT

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.

▶ SIMULATION
CODE · round 1 · {live out}
01 a = 1
02 b = 2
03 c = a + b
04 d = b + c
05 e = c + d
06 return e
INTERFERENCE · K = 2
not built
REGISTERS
R1: —
R2: —
spilled: none
program loaded
spill code0 stores · 0 loads
✓ no two interfering variables share a register
SCHEDULE
01[ALLOC]program: a chain of short live ranges
02[ANALYSIS]liveness: walk backwards, record what is live out of each line
03[ANALYSIS]build the interference graph
04[ALLOC]simplify: push nodes with degree < K
05[ALLOC]stuck? push the lowest cost/degree node as a spill candidate, keep simplifying
06[ALLOC]select: pop, give each the lowest free register
07[CHECK]verify: no two interfering variables share a register
Start of chain a—b—c—d scenario. 7 steps.
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.

TUNE IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

registers are a colouring of the interference graph. Low-degree nodes are free: set them aside. When nothing is low-degree, a variable goes to memory, and the spill shrinks its live range to a few instructions. The rule that never bends: two interfering variables never share a register.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: SSA FORM →