Reducibility
Glossary Contents
← Ch XI
Chapter XII Chromatic Number
Ch XIII →
  1. 34 — Color the map
  2. 35 — DSATUR in Java
  3. 36 — Register allocation
36

Register allocation

When javac (or any compiler) decides which variable lives in which CPU register, it is graph-coloring the interference graph.

Two program variables interfere if their lifetimes overlap — both are alive at the same instruction. Build the interference graph with a vertex per variable and edges between interfering pairs. Coloring this graph with k colors corresponds to assigning the variables to k registers (same color = same register). Modern CPUs have ~16 general-purpose integer registers; if χ(G) ≤ 16, no spilling is needed. Chaitin's allocator (1981) was the first major use of graph coloring in compilers and remains the model. Variants ship in GCC, LLVM, V8, and the JVM's HotSpot. The same problem also runs exam timetabling (each exam a vertex, conflict edge if a student takes both, color = time slot), frequency assignment (radio towers, color = channel), and Sudoku (cells as vertices, color = digit, with extra "all-different" constraints).

In plain terms

A compiler decides which variable in your code goes into which CPU register. Two variables that are used at the same time can't share a register. That's exactly graph coloring — every variable is a region of the map, and two overlapping ones need different colors.

Scan to come back to page 36

End of Chapter XII
Chapter XIII Clique Cover →
← 35 DSATUR in Java
3/3 · 36/63
37 Cliques as colors →