Explaining graph coloring register allocation with example
Graph coloring register allocation assigns variables to a limited set of registers by modeling variables as nodes in an interference graph, where edges show overlapping (simultaneous) live ranges. If this graph can be colored with k colors (registers), each variable gets a register without conflicts. For example, variables x, y, and z whose live ranges overlap form a triangle graph; if only two registers (k=2) are available, the graph isn’t 2-colorable, forcing a spill. Spilling means moving a variable's value to memory, inserting loads and stores to reduce register pressure, then rebuilding the graph and recoloring. This approach uses liveness analysis to build the interference graph and heuristics like Chaitin’s algorithm to simplify coloring and decide spills.
A simple way to think about register allocation in a compiler back–end is that you have a set of temporaries produced by your intermediate code and a fixed number (k) of physical registers. Two temporaries can share a register if their lifetimes don’t overlap, but if they are simultaneously “live” then they must be kept in different registers. Graph colouring gives you a convenient formalism for this:
- Run a liveness analysis over the program to determine, for each program point, which temporaries are live.
- Build an interference graph with one vertex per temporary; add an edge (u!!-!!v) if (u) and (v) are ever live at the same point.
- A proper colouring of this graph with (k) colours corresponds to an assignment of temporaries to (k) physical registers such that no two interfering temporaries get the same register.
For example, consider the following straight–line code fragment:
1: x = load …
2: y = load …
3: z = load …
4: t1 = x + y
5: t2 = t1 + z
6: return t2
Working backwards, the liveness sets look like
after 6: { } before 6: {t2}
after 5: {t2} before 5: {t1,z}
after 4: {t1,z} before 4: {x,y,z}
after 3: {x,y,z} before 3: {x,y}
…
At the program point just before line 4 all of x, y and z are live, so they all interfere. The interference graph has three nodes (x,y,z) with edges between every pair; (t1) and (t2) each interfere with the value they are computed from, but not with each other:
x———y
\ /
z t1——t2
\ /
y (edge from t1 to z, etc.)
If your target machine has (k=3) general–purpose registers, this graph can be 3‑coloured and you can give each of (x,y,z) a different register. If (k=2), the triangle subgraph on (x,y,z) is not 2‑colourable, so a 2‑register allocation is impossible.
Practical allocators deal with this by “spilling” one or more temporaries to memory to reduce the register pressure. Spilling means choosing a vertex (e.g. (z)), assigning it to a stack slot instead of a register, and rewriting the code to insert loads before its uses and stores after its definitions:
3: z = load …
…
5: t2 = t1 + (load [z_spill])
This shortens (z)’s live range and removes its interference edges; when you rebuild and colour the graph with the new live ranges, it may now be (k)-colourable. Most graph–colouring allocators, such as Chaitin–Briggs, perform this simplification/spill/rewrite loop: simplify the graph by removing low–degree vertices, select a spill candidate when you cannot simplify further, rewrite, and then try to colour again.