Algorithm Search⌘K

Problemadvanced

Register coalescing

Also called copy coalescing.

Merge the nodes of copy-related values in the interference graph to remove copies, while keeping the graph colorable; NP-complete in general.

Register coalescing removes copy instructions x = y by giving xx and yy the same register: the copy becomes r = r and is deleted. In the interference graph, this means merging the vertices of xx and yy into one vertex, which is possible only if xx and yy do not interfere. Each merge also makes the graph harder to color, because the merged vertex has the neighbors of both. The problem is to remove as many copies as possible, weighted by how often they run, while the graph stays colorable with the kk registers. Bouchez, Darte and Rastello (2007) proved that its main forms are NP-complete.

In plain words

Compilers make many copies of values: when they leave SSA form, when they inline a function, and when they move arguments into the registers a call expects. A copy costs nothing if the value and its copy sit in the same register, because then there is nothing to move. So the compiler tries to put each value and its copy in one register. This is only allowed when the two are never needed at the same time with different contents. But joining two values also joins their conflicts, and too many conflicts can push a value out to memory, which costs more than the copy. The task is to remove as many copies as possible without pushing anything out to memory.

Definition

An affinity graph is an interference graph G=(V,E)G = (V, E) with a set AA of affinity edges, one for each copy x = y between two names, and a weight w(a)≥0w(a) \ge 0 for each a∈Aa \in A, such as the number of times the copy runs. A copy does not make its two names interfere: at a copy x = y, the definition rule of the interference graph leaves out the edge {x,y}\{x, y\}. They interfere only if some other definition of one of them happens while the other is live.

A coalescing is a partition of VV into classes such that no two vertices of one class are adjacent in GG. The coalesced graph GfG_f has the classes as vertices, and two classes are adjacent if some vertex of one is adjacent in GG to some vertex of the other. An affinity is coalesced if its two ends are in one class. A kk-coloring of GfG_f gives a kk-coloring of GG in which the two ends of each coalesced affinity have the same color, so each coalesced copy is deleted.

Problem (coalescing). Given GG, AA, ww and kk, find a coalescing that maximizes the total weight of the coalesced affinities, such that GfG_f satisfies a coloring condition. The condition defines the variant.

Variants

The names follow Bouchez, Darte and Rastello (2007).

  • Aggressive coalescing. No coloring condition: any coalescing is allowed. It is the first step of Chaitin's allocator, which then spills if the coalesced graph is hard to color.
  • Conservative coalescing. GfG_f must stay greedy-kk-colorable, that is, its kk-core must be empty, so that the simplify phase of Chaitin's allocator colors it. A stronger condition, "GfG_f is kk-colorable", is also studied, but even checking it is NP-complete.
  • Incremental conservative coalescing. GG is greedy-kk-colorable and only one affinity {x,y}\{x, y\} is given: can xx and yy be merged so that the graph stays greedy-kk-colorable? This is the question that the Briggs and George tests answer in part.
  • Optimistic coalescing. Start from an aggressive coalescing and undo as few merges as possible, by weight, until the graph is greedy-kk-colorable.
  • Precolored vertices. Some names must be in fixed registers, for example arguments in a calling convention. They are vertices that are already colored, and two precolored vertices of different colors interfere.

Examples

All copies can go. In this loop, after out-of-SSA translation:

L0:
  i = n
  s = 0
  jump L1
L1:
  t = s + i
  j = i - 1
  s = t
  i = j
  br i L1 L2
L2:
  ret s

the edges are ii-ss, ss-jj, jj-tt and tt-ii, and the affinities are ii-nn, ii-jj and ss-tt. The classes {i,j,n}\{i, j, n\} and {s,t}\{s, t\} are a coalescing with one edge between them, so with k=2k = 2 all three copies are deleted. Iterated register coalescing finds it.

Two copies compete. Here bb and cc are both copies of aa:

L0:
  a = p + 1
  b = a
  c = a
  d = b * c
  ret d

The only edge is bb-cc: at c = a, the name bb is live. The affinities are aa-bb and aa-cc. A class cannot hold both bb and cc, so at most one copy can be coalesced. Note that bb and cc hold the same value, so one register would do for all three names; the definition rule does not see values, only names.

Aggressive coalescing causes a spill. In this program, bb is a copy of aa on one path:

L0:
  y = p + 2
  x = p + 1
  br x L1 L2
L1:
  a = x + 1
  a = a + x
  b = a
  jump L3
L2:
  b = y + 1
  b = b + y
  jump L3
L3:
  ret b

The edges are aa-xx, xx-yy, yy-bb and pp-yy. The path a,x,y,ba, x, y, b is 2-colorable, but merging aa and bb makes a triangle with xx and yy. With k=2k = 2, the aggressive coalescing {a,b}\{a, b\} forces a spill, and an allocator with aggressive coalescing spills yy: one store and two loads instead of one copy. The conservative answer is to keep the copy.

Known algorithms

  • Aggressive coalescing (Chaitin's allocator): merge the ends of every copy that do not interfere, then rebuild the graph, and repeat. It removes many copies and can cause spills.
  • Conservative coalescing (conservative coalescing): merge only when the Briggs test or the George test proves that the merge keeps the kk-core empty. Briggs, Cooper and Torczon (1994) ran it before coloring.
  • Iterated register coalescing (George and Appel, 1996): interleave conservative merges with simplify steps, which lower degrees and let more merges pass.
  • Optimistic coalescing (Park and Moon): merge aggressively, and undo merges at select time when a merged vertex finds no color.
  • Biased coloring: no merge at all. When select can choose among several colors, it prefers the color of a copy partner.
  • SSA-based allocation (SSA-based register allocation): color the chordal graph of an SSA program first, after spilling has brought the number of live values down to kk, then change colors to make copy partners equal, which keeps the coloring valid at each step.
AlgorithmTimeSpacePropertiesCode
Iterated register coalescingO(r (l + s n + n^2 (c + 1))) worstO(n + m + s + c) extradeterministicofflineiterative1 language

Bounds

  • Aggressive coalescing is NP-complete, by the reduction below. Bouchez, Darte and Rastello (2007) proved that it stays NP-complete even when the interference graph is chordal or greedy-3-colorable.
  • Conservative coalescing is NP-complete (Bouchez, Darte and Rastello, 2007).
  • Incremental conservative coalescing is NP-complete when the condition is kk-colorability, and polynomial when the graph is chordal. For greedy-kk-colorable graphs, Bouchez, Darte and Rastello (2007) left its complexity open.
  • Optimistic coalescing is NP-complete, even for chordal graphs with k=4k = 4 (Bouchez, Darte and Rastello, 2007).
  • At most ∣A∣|A| copies can be removed, and every affinity whose ends interfere stays.

Reductions

kk-coloring reduces to aggressive coalescing. Given a graph HH with nn vertices and a number kk, build GG from HH by adding a clique of kk new vertices c1,…,ckc_1, \ldots, c_k with no edge to HH. Add an affinity of weight 1 between each vertex vv of HH and each cic_i. Then some coalescing of GG coalesces nn affinities if and only if HH is kk-colorable.

Proof. The cic_i are pairwise adjacent, so they are in different classes, and each vertex vv is in the class of at most one cic_i. So vv gives at most one coalesced affinity, and nn affinities are coalesced exactly when each vv is in the class of some cic_i. If that holds, set the color of vv to ii: two adjacent vertices of HH cannot share a class, so this is a kk-coloring of HH. Conversely, a kk-coloring χ\chi of HH gives the classes {ci}∪χ−1(i)\{c_i\} \cup \chi^{-1}(i), which contain no edge of GG and coalesce all nn affinities. □\square

The construction is polynomial, and a coalescing is a certificate that can be checked in polynomial time, so aggressive coalescing is NP-complete for every fixed k≥3k \ge 3 (three-coloring). The clique c1,…,ckc_1, \ldots, c_k plays the role of kk precolored machine registers.

For the conservative condition "GfG_f is kk-colorable", hardness is immediate: with A=∅A = \emptyset, the question is whether GG is kk-colorable.

Applications

  • Leaving SSA form. Out-of-SSA translation replaces each ϕ\phi-function by copies on the incoming edges. Most of them can be coalesced, so that the copies cost nothing.
  • Live range splitting. Live range splitting adds copies between the pieces of a value. Coalescing removes the copies where the split was not needed.
  • Calling conventions and two-address instructions. Arguments and results must be in fixed registers, and some machines have instructions whose result register must be the same as one operand. The copies that this adds are affinities with precolored vertices.
  • Copy propagation removes some copies before allocation, but not the copies that SSA destruction makes on edges or that the machine requires.

References

  1. Florent Bouchez, Alain Darte and Fabrice Rastello, On the complexity of register coalescing, International Symposium on Code Generation and Optimization (CGO), 2007, 102-114, the definitions of aggressive, conservative, incremental conservative and optimistic coalescing, and their NP-completeness results
  2. Gregory J. Chaitin, Register allocation and spilling via graph coloring, Proceedings of the SIGPLAN '82 Symposium on Compiler Construction, SIGPLAN Notices 17(6), 1982, 98-105, subsumption, the aggressive coalescing of copies before coloring
  3. Preston Briggs, Keith D. Cooper and Linda Torczon, Improvements to graph coloring register allocation, ACM Transactions on Programming Languages and Systems 16(3), 1994, 428-455, conservative coalescing and the problem that aggressive coalescing causes spills
  4. Lal George and Andrew W. Appel, Iterated register coalescing, ACM Transactions on Programming Languages and Systems 18(3), 1996, 300-324, the George test and the interleaving of coalescing with simplification
  5. Andrew W. Appel with Jens Palsberg, Modern Compiler Implementation in Java, second edition, Cambridge University Press, 2002, Chapter 11 (Register Allocation), the section on coalescing and the Briggs and George criteria

Where this leads

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)
WritingW2 basic
  • a contract.yaml