np hard
1981 United States NP-complete
09

Register Allocation via Graph Coloring

Gregory Chaitin, 1981 — optimal register allocation reduces to graph coloring, which is NP-complete. Even the compiler is solving an NP-hard problem on every build.

Gregory Chaitin, at IBM Research Yorktown Heights, formulated register allocation as a graph coloring problem and built a compiler that used the reduction. Each variable in a program becomes a node in an interference graph; two nodes are connected if their variables are alive at the same time and therefore cannot share a register. Coloring the graph with k colors corresponds to assigning each variable to one of k registers. Graph k-coloring is NP-complete for k ≥ 3 (Karp 1972), so register allocation is NP-complete. Modern compilers use heuristics — Chaitin's graph coloring approach, linear scan, or SSA-based variants — none of which is guaranteed optimal. Every time gcc, clang, or rustc compiles your code, it is approximating an NP-complete problem. The hardness is invisible because we have lived with the heuristic for forty years.

Chaitin, G. J. (1982). Register Allocation and Spilling via Graph Coloring. Proceedings of the 1982 SIGPLAN Symposium on Compiler Construction, 98–105. Source →