Every program you run has been transformed by a compiler into machine code. One of the most critical steps in that transformation is register allocation: deciding which variables get to live in the CPU's tiny set of blazing-fast registers, and which ones get exiled to much slower memory.
A modern CPU might have 16 or 32 general-purpose registers. A typical function, however, may juggle dozens or hundreds of variables. Two variables that are alive at the same time cannot share a register — they would overwrite each other. Variables that are never alive simultaneously, on the other hand, can safely share one.
The compiler's job is to color the interference graph: build a graph where each node is a variable and each edge connects two variables that are alive at the same time, then assign a color (register) to each node so that no two neighbors share a color. If colors (registers) are enough, every variable stays fast. If not, some variables must be spilled — written to and read from memory, which costs precious cycles.
This coloring problem is the engine of every optimizing compiler you have ever used.
Comments
Loading comments...