Algorithm Search⌘K

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 kk registers, if each name keeps one register for its whole life?" is exactly as hard as kk-coloring of general graphs, and it is NP-complete for every fixed k≥3k \ge 3. 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 G(P)G(P) has one vertex per name and an edge {x,y}\{x, y\} when yy is live just after an instruction that defines xx, and that instruction is not the copy x = y.

Problem RAk\mathrm{RA}_k (for a fixed integer kk). Input: a program PP. Question: is there a map RR from the names of PP to {0,…,k−1}\{0, \ldots, k-1\} with R(x)≠R(y)R(x) \ne R(y) for every edge {x,y}\{x, y\} of G(P)G(P)? 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 PP.

Theorem.

  1. For every graph HH with nn vertices and mm edges there is a program P(H)P(H), built in time O(n+m)O(n + m), whose interference graph is HH plus one isolated vertex. P(H)P(H) is strict, has no loop and no copy, and at most two names are live at any point of it.
  2. For every fixed k≥3k \ge 3, RAk\mathrm{RA}_k 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 RR. Live variable analysis over the control flow graph of PP takes polynomial time, and so does the walk that lists the edges of G(P)G(P). Checking R(x)≠R(y)R(x) \ne R(y) on each edge is then linear in the number of edges.

The program P(H)P(H). Let H=(V,E)H = (V, E) with V={x1,…,xn}V = \{x_1, \ldots, x_n\}, n≥1n \ge 1. An item is an edge of HH or an isolated vertex of HH. List the items as I1,…,ItI_1, \ldots, I_t; then t≥1t \ge 1. The program has one parameter pp, and one name for each vertex of HH. Its blocks are:

  • For j=1,…,t−1j = 1, \ldots, t - 1, a selector block SjS_j with p = p - 1 and br p N_j T_j, where Nj=Sj+1N_j = S_{j+1} for j<t−1j < t - 1 and Nt−1=TtN_{t-1} = T_t.
  • For an item Ij={x,y}I_j = \{x, y\} that is an edge, a block TjT_j with x = 1, y = 2 and br y R_y R_x.
  • For an item Ij=xI_j = x that is an isolated vertex, a block TjT_j with x = 1 and jump R_x.
  • For each vertex xx, a block RxR_x with ret x.

The entry block is S1S_1, or T1T_1 when t=1t = 1. The program has 2t−1+n2t - 1 + n blocks and O(n+m)O(n + m) instructions. It has no loop, because every edge of its flow graph goes from an SS block to a later SS block or to a TT block, or from a TT block to an RR block. It is strict: the only predecessors of RxR_x are TT blocks that define xx, and pp is a parameter.

The branch conditions are constants inside the TT 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 P(H)P(H) are:

  • at the start of RxR_x: exactly {x}\{x\};
  • in a TT block of an edge {x,y}\{x, y\}: nothing at the start, {x}\{x\} just after x = 1, and {x,y}\{x, y\} just after y = 2;
  • in a TT block of an isolated vertex xx: nothing at the start, and {x}\{x\} just after x = 1;
  • in each selector block: {p}\{p\} at the start and {p}\{p\} just after p = p - 1.

Proof. RxR_x uses xx and has no successor, so its live-in set is {x}\{x\}. A TT block of an edge {x,y}\{x, y\} has the successors RyR_y and RxR_x, so its live-out set is {x,y}\{x, y\}. The branch reads yy, which is in that set already. Going backward, y = 2 removes yy, which leaves {x}\{x\}, and x = 1 removes xx, 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 TT block, and their live-in sets are {p}\{p\} and ∅\emptyset. The branch reads pp, so {p}\{p\} is live just after p = p - 1, and since that instruction reads pp, {p}\{p\} 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. □\square

Lemma 2 (edges). G(P(H))G(P(H)) has the vertex set V∪{p}V \cup \{p\} and the edge set EE.

Proof. Each name of VV is defined in some TT block and read in its RR block, and pp is the parameter, so the vertices are V∪{p}V \cup \{p\}. 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 {p}\{p\}, which gives no edge. In a TT block, x = 1 is followed by {x}\{x\}, which gives no edge. In the TT block of an edge {x,y}\{x, y\}, y = 2 is followed by {x,y}\{x, y\}, which gives exactly the edge {x,y}\{x, y\}. Each edge of EE has its own TT block, so every edge of EE appears, and no other edge appears. □\square

Lemma 1 also shows that at most two names are live at any point. This proves part 1.

NP-hardness. For a fixed k≥3k \ge 3, the problem kk-coloring is NP-complete (k-coloring; for k=3k = 3 this is 3-colorability, and for k>3k > 3 one adds k−3k - 3 new vertices joined to every other vertex). Map a graph HH to P(H)P(H). By Lemma 2, G(P(H))G(P(H)) is HH plus the isolated vertex pp. An isolated vertex can take any color, so G(P(H))G(P(H)) has a kk-coloring if and only if HH has one, and a kk-coloring of G(P(H))G(P(H)) is exactly a map RR as in RAk\mathrm{RA}_k. The map H↦P(H)H \mapsto P(H) takes linear time, so it is a polynomial-time reduction. With membership in NP, this proves part 2. ■\blacksquare

Example. For the triangle u,v,wu, v, w, 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 uu-vv, vv-ww and ww-uu, and pp 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 uu is in r1 in E1 and in r0 in E3, so its live range is split. A script ran both programs on the inputs −1-1 to 33: both return 3 for p=1p = 1, 7 for p=2p = 2 and 4 otherwise.

Consequences

  • No polynomial-time algorithm decides RAk\mathrm{RA}_k for any fixed k≥3k \ge 3 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 MaxLive\mathrm{MaxLive}. A Mycielski graph has no triangle and any chromatic number, and P(H)P(H) has MaxLive≤2\mathrm{MaxLive} \le 2 for every HH.
  • 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 MaxLive\mathrm{MaxLive} 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 MaxLive\mathrm{MaxLive}. They also show that when critical edges cannot be split, deciding whether kk registers suffice stays NP-complete even with live range splitting.
  • In strict SSA form the interference graph is chordal, so χ=ω=MaxLive\chi = \omega = \mathrm{MaxLive} 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 k≥3k \ge 3 is needed. RA1\mathrm{RA}_1 asks whether G(P)G(P) has no edge, and RA2\mathrm{RA}_2 asks whether G(P)G(P) 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 RR block, but no loop.
  • Register pressure is not the cause. The hard instances have MaxLive≤2\mathrm{MaxLive} \le 2, 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 xx: each edge block also sets x=u+vx = u + v, and each return block returns u+xu + x. So xx interferes with every other name, and the program needs k+1k + 1 registers exactly when the graph needs kk colors. The version on this page drops xx 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/TAJDRY/text.yaml