Theoremcore
NP-completeness of register allocation
Also called Chaitin's NP-completeness theorem.
Every graph is the interference graph of some program, so deciding whether a program fits in k registers, each name keeping one register, is NP-complete for every fixed k >= 3.
The theorem of Chaitin, Auslander, Chandra, Cocke, Hopkins and Markstein (1981) says that every graph is the interference graph of some program. So the question "can this program run in registers, if each name keeps one register for its whole life?" is exactly as hard as -coloring of general graphs, and it is NP-complete for every fixed . The theorem is about this coloring form only. It says nothing about the full register allocation problem, where a name can move between registers: the hard programs of the proof need only two registers once one copy can be placed on a split edge.
Statement
A program is in the toy IR with const, copy and op instructions. Its interference graph has one vertex per name and an edge when is live just after an instruction that defines , and that instruction is not the copy x = y.
Problem (for a fixed integer ). Input: a program . Question: is there a map from the names of to with for every edge of ? Such a map renames each name to one register, and the proof file of Chaitin's allocator shows that the renamed program has the same outcome as .
Theorem.
- For every graph with vertices and edges there is a program , built in time , whose interference graph is plus one isolated vertex. is strict, has no loop and no copy, and at most two names are live at any point of it.
- For every fixed , is NP-complete.
In plain words
Two values that are needed at the same time cannot share a register. Draw a dot for each value and a line between two values that clash. Giving registers is then coloring the dots so that no line joins two dots of one color. Coloring a drawing with three colors is a famous hard problem. Chaitin and his colleagues showed that every drawing of dots and lines comes from some program. So if a fast method could always give registers to programs, it could also color every drawing, and no fast method for that is known. The programs in the proof are strange, though. They never need more than two values at once. They are hard only because the rules say that each value must stay in one register. If a value may move to another register on the way, two registers are enough.
Proof
Membership in NP. A certificate is the map . Live variable analysis over the control flow graph of takes polynomial time, and so does the walk that lists the edges of . Checking on each edge is then linear in the number of edges.
The program . Let with , . An item is an edge of or an isolated vertex of . List the items as ; then . The program has one parameter , and one name for each vertex of . Its blocks are:
- For , a selector block with
p = p - 1andbr p N_j T_j, where for and . - For an item that is an edge, a block with
x = 1,y = 2andbr y R_y R_x. - For an item that is an isolated vertex, a block with
x = 1andjump R_x. - For each vertex , a block with
ret x.
The entry block is , or when . The program has blocks and instructions. It has no loop, because every edge of its flow graph goes from an block to a later block or to a block, or from a block to an block. It is strict: the only predecessors of are blocks that define , and is a parameter.
The branch conditions are constants inside the blocks, so a run takes only one of the two exits. This does not matter: liveness is defined by the paths of the flow graph, not by the values.
Lemma 1 (live sets). The live names of are:
- at the start of : exactly ;
- in a block of an edge : nothing at the start, just after
x = 1, and just aftery = 2; - in a block of an isolated vertex : nothing at the start, and just after
x = 1; - in each selector block: at the start and just after
p = p - 1.
Proof. uses and has no successor, so its live-in set is . A block of an edge has the successors and , so its live-out set is . The branch reads , which is in that set already. Going backward, y = 2 removes , which leaves , and x = 1 removes , which leaves the empty set. The case of an isolated vertex is the same with one successor. Every successor of a selector block is a selector block or a block, and their live-in sets are and . The branch reads , so is live just after p = p - 1, and since that instruction reads , is live at the start. The live sets are a fixed point of the backward equations, and no smaller sets satisfy them, because each name in them is read later on some path with no definition before the read.
Lemma 2 (edges). has the vertex set and the edge set .
Proof. Each name of is defined in some block and read in its block, and is the parameter, so the vertices are . We go through every definition and the set of names live just after it, from Lemma 1. In a selector block, p = p - 1 is followed by , which gives no edge. In a block, x = 1 is followed by , which gives no edge. In the block of an edge , y = 2 is followed by , which gives exactly the edge . Each edge of has its own block, so every edge of appears, and no other edge appears.
Lemma 1 also shows that at most two names are live at any point. This proves part 1.
NP-hardness. For a fixed , the problem -coloring is NP-complete (k-coloring; for this is 3-colorability, and for one adds new vertices joined to every other vertex). Map a graph to . By Lemma 2, is plus the isolated vertex . An isolated vertex can take any color, so has a -coloring if and only if has one, and a -coloring of is exactly a map as in . The map takes linear time, so it is a polynomial-time reduction. With membership in NP, this proves part 2.
Example. For the triangle , a program of the same shape, with other constants and block names, is this one, with parameter p:
S1:
p = p - 1
br p S2 E1
S2:
p = p - 1
br p E2 E3
E1:
v = 3
u = 0
br u Ru Rv
E2:
w = 5
v = 4
br v Rv Rw
E3:
u = 7
w = 0
br w Rw Ru
Ru:
ret u
Rv:
ret v
Rw:
ret w
The interference edges are -, - and -, and is isolated. No map to two registers exists. But at most two names are live at any point, and the edge from E3 to Ru is a critical edge (E3 has two successors and Ru has two predecessors). Split it with a new block X that holds one copy, and two registers suffice:
S1:
r0 = r0 - 1
br r0 S2 E1
S2:
r0 = r0 - 1
br r0 E2 E3
E1:
r0 = 3
r1 = 0
br r1 Ru Rv
E2:
r1 = 5
r0 = 4
br r0 Rv Rw
E3:
r0 = 7
r1 = 0
br r1 Rw X
X:
r1 = r0
jump Ru
Ru:
ret r1
Rv:
ret r0
Rw:
ret r1
Here is in r1 in E1 and in r0 in E3, so its live range is split. A script ran both programs on the inputs to : both return 3 for , 7 for and 4 otherwise.
Consequences
- No polynomial-time algorithm decides for any fixed unless P = NP. This is why Chaitin's allocator, optimistic coloring and iterated register coalescing are heuristics that may spill a name although a coloring exists.
- The chromatic number of an interference graph is not bounded by any function of . A Mycielski graph has no triangle and any chromatic number, and has for every .
- The hardness comes from the rule that a name keeps one register. Bouchez, Darte, Guillon and Rastello (LCPC 2006) show that a strict program fits in registers without spills when critical edges can be split and moves and swaps can be inserted, so with these freedoms the question is decided by computing . They also show that when critical edges cannot be split, deciding whether registers suffice stays NP-complete even with live range splitting.
- In strict SSA form the interference graph is chordal, so and an optimal coloring takes polynomial time (Hack, Grund and Goos 2006; see SSA chordal interference theorem). Pereira and Palsberg (FoSSaCS 2006) observe that the programs of Chaitin et al., once converted to SSA form and back, can be colored with three colors, and they show that the coloring form becomes NP-complete again for programs after the classical elimination of SSA form, by a reduction from coloring circular-arc graphs.
Tightness
- The bound is needed. asks whether has no edge, and asks whether is bipartite (bipartiteness testing). Both take linear time in the size of the graph.
- Branches and joins are needed. In one basic block with no copies, each live range is an interval of instruction positions, and a dead definition is a single point. Then the graph is an interval graph: its chromatic number equals the size of its largest clique, and a greedy sweep in the order of the definitions finds an optimal coloring (interval partitioning). The construction above needs a join at each block, but no loop.
- Register pressure is not the cause. The hard instances have , the smallest value that allows an edge at all.
- The result is about one form only. With live range splitting on split critical edges it fails, as the example above shows.
History
Chaitin, Auslander, Chandra, Cocke, Hopkins and Markstein described global register allocation by graph coloring for the experimental PL.8 compiler at IBM in 1981. Their paper also built, for any graph, a program with that interference graph. The program, as redrawn by Bouchez et al., has one extra name : each edge block also sets , and each return block returns . So interferes with every other name, and the program needs registers exactly when the graph needs colors. The version on this page drops and gives an isolated vertex instead.
For twenty-five years the result was often read as "register allocation is NP-complete". Around 2005 and 2006, several groups found that SSA programs have chordal interference graphs (Hack, Grund and Goos; Bouchez, Darte, Guillon and Rastello; Brisk, Dabiri, Macbeth and Sarrafzadeh). Bouchez, Darte, Guillon and Rastello then asked what the reduction of Chaitin et al. really proves, and located the hardness in the ban on splitting, in critical edges, in spilling and in coalescing.
References
- Gregory J. Chaitin, Marc A. Auslander, Ashok K. Chandra, John Cocke, Martin E. Hopkins and Peter W. Markstein, Register allocation via coloring, Computer Languages 6(1), 1981, 47-57, the construction of a program for any given graph, so that coloring the interference graph is as hard as coloring a general graph
- Florent Bouchez, Alain Darte, Christophe Guillon and Fabrice Rastello, Register allocation: what does the NP-completeness proof of Chaitin et al. really prove? Or revisiting register allocation: why and how, Languages and Compilers for Parallel Computing (LCPC 2006), LNCS 4382, Springer, 2007, the program of Chaitin et al. redrawn with its extra name x, the role of critical edges, MaxLive registers suffice when critical edges can be split and moves and swaps are allowed, and hardness with splitting when critical edges cannot be split
- Fernando Magno Quintão Pereira and Jens Palsberg, Register allocation after classical SSA elimination is NP-complete, Foundations of Software Science and Computation Structures (FoSSaCS 2006), LNCS 3921, Springer, 2006, the programs of Chaitin et al. can be colored with three colors after conversion to SSA form and back (the example of the cycle of length 4), and the reduction from circular-arc graph coloring for programs after SSA elimination
- Sebastian Hack, Daniel Grund and Gerhard Goos, Register allocation for programs in SSA-form, Compiler Construction (CC 2006), LNCS 3923, Springer, 2006, 247-262, the interference graph of a strict SSA program is chordal and can be colored in polynomial time
- Michael R. Garey and David S. Johnson, Computers and Intractability, A Guide to the Theory of NP-Completeness, W. H. Freeman, 1979, Appendix A1.1, problem GT4 (Graph K-Colorability), NP-complete for every fixed K >= 3
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag TAJDRY Edit this page History Report an error Markdown