Apex › Systems & OS

How variables get registers (graph coloring)

A compiler invents hundreds of temporaries but the CPU has ~16 registers. Two variables live at the same time interfere and can’t share a register; model that as an interference graph and register allocation becomes graph coloring with K = the register count. Chaitin’s algorithm removes low-degree nodes to a stack (each is guaranteed a color), then pops and colors; if it gets stuck, a variable spills to memory. Verified in node over 600 graphs: the coloring is always proper, and coloring every node means the graph really is K-colorable (brute-force agrees). Slide K and watch variables get registers or spill.

More in Systems & OS