Algorithm Search⌘K

Techniqueadvanced

Conservative coalescing

Also called Briggs test, George test.

Coalesces two nodes only when a degree test proves that the merged graph stays as colorable as before.

Conservative coalescing merges the two names of a copy x = y in the interference graph only when a cheap local test proves that the merge cannot make the graph harder to color. The two tests in use are the Briggs test, from Briggs, Cooper and Torczon (1994), and the George test, from George and Appel (1996). Both look only at the degrees of the neighbors of xx and yy. Both guarantee that if the graph was kk-colorable it stays kk-colorable, and that if Chaitin's simplify phase could color it, it still can. They are sufficient conditions: a safe merge can fail both tests. The problem they help with, register coalescing, is NP-complete.

In plain words

When one value is a copy of another, putting both in the same register removes the copy. But the joined value now clashes with everything that either value clashed with, and that can force some value out to memory. A safe rule looks at the neighbors of the joined value. A neighbor with few clashes is never a problem, because it can always find a free register at the end. So the first rule says: join only if few neighbors have many clashes. The second rule says: join the copy into the original only if each neighbor of the copy is already a neighbor of the original or has few clashes. Either rule makes sure that the join never forces a value out to memory.

When it applies

  • The allocator works on an interference graph with kk colors and uses simplification, as in Chaitin's allocator, optimistic coloring or iterated register coalescing.
  • There is a copy x = y whose names do not interfere. A copy by itself never makes its two names interfere; they interfere only if one of them is defined, by an instruction other than this copy, while the other is live.
  • The test is applied to the current graph, with the current degrees. A degree is high if it is at least kk and low otherwise.

The template

Let uu and vv be the vertices of xx and yy, not adjacent, and let G′G' be the graph after the merge, where the new vertex ww has the neighbors N(u)∪N(v)N(u) \cup N(v). A common neighbor of uu and vv loses one edge in G′G'; every other vertex keeps its degree.

BRIGGS-OK(G, u, v, k)
  H <- the vertices t in N(u) + N(v) with degree(t) - [t in N(u) and t in N(v)] >= k
  return |H| < k

GEORGE-OK(G, u, v, k)          // merge v into u
  return every t in N(v) is in N(u) or has degree(t) < k

An allocator merges uu and vv if BRIGGS-OK passes, or if GEORGE-OK passes in one of the two directions. Each test runs in O(deg⁡u+deg⁡v)O(\deg u + \deg v) time with adjacency sets.

Why the Briggs test is safe. The neighbors of ww with low degree in G′G' can be simplified away. Then ww has fewer than kk neighbors left, all of high degree, so ww can be simplified too. What remains is GG without uu, vv and some low vertices, a subgraph of GG. So a kk-coloring of GG gives one of what remains, and the simplify rule extends it back to G′G'. For the kk-core: in a nonempty kk-core of G′G' that contains ww, the vertex ww would need kk neighbors of degree at least kk, which the test excludes.

Why the George test is safe. The neighbors of vv that are not neighbors of uu have low degree, and they keep the same degree in G′G'. Simplify them away. Then ww has exactly the neighbors of uu, so what remains is GG without vv and those vertices, with ww in the place of uu. Again it is a subgraph of GG.

The full proofs, for both kk-colorability and the empty kk-core, are Lemmas 1 and 2 of the proof file of iterated register coalescing.

Classic examples

A merge that only George allows. In the summing loop of the iterated coalescing page, with k=2k = 2, the interference graph is the cycle i,s,j,ti, s, j, t plus a vertex nn with no edge, and there is a copy i = n. Merging nn into ii gives a vertex with the neighbors ss and tt, both of degree 2, so the Briggs test fails. The George test, merging nn into ii, passes at once: nn has no neighbor.

A merge that only Briggs allows. Take k=3k = 3. Let uu have the neighbor aa, and vv the neighbor bb, where aa and bb each have two more neighbors of their own, so both have degree 3. The merged vertex has the neighbors aa and bb, two of high degree, fewer than 3, so the Briggs test passes. The George test fails in both directions: bb is not a neighbor of uu and has degree 3, and aa is not a neighbor of vv and has degree 3.

A merge that both refuse, rightly. In the path a,x,y,ba, x, y, b with k=2k = 2, merging aa and bb makes a triangle. After the merge, xx and yy have degree 2, so the Briggs test fails. The George test fails both ways: xx is not a neighbor of bb and yy is not a neighbor of aa, and both have degree 2. Aggressive coalescing would merge them and force a spill.

A merge that both refuse, wrongly. In the cycle 0,1,2,3,4,50, 1, 2, 3, 4, 5 with k=2k = 2, merge 00 and 22. The merged vertex has the neighbors 1,3,51, 3, 5, and 33 and 55 have degree 2 after the merge, so the Briggs test fails. The George test fails both ways, because of 33 and of 55. But the merged graph has the edges ww-11, ww-33, ww-55, 33-44 and 44-55, and it is bipartite with the sides {w,4}\{w, 4\} and {1,3,5}\{1, 3, 5\}, so it is still 2-colorable.

Variations

  • Before coloring. Briggs, Cooper and Torczon (1994) applied the Briggs test in the build and coalesce loop before simplification.
  • Interleaved with simplify. George and Appel (1996) test copies between simplify steps. A failed copy is tried again when a degree drops to k−1k - 1, because simplification lowers degrees and turns more tests into passes (iterated register coalescing).
  • Precolored vertices. A machine register that is fixed by a calling convention interferes with a very large set of names. The George test, with uu the precolored vertex, needs only the neighbors of the other vertex and a membership check in N(u)N(u), so allocators use it in that case.
  • Either test. Since both tests are safe, an allocator can accept a merge if either one passes.
  • Exact incremental test. One can ask exactly whether a merge keeps the kk-core empty, by running simplification on the merged graph. This costs time linear in the graph for each copy, instead of the degrees of two vertices.

Pitfalls

  • Degrees after the merge. The Briggs test counts degrees in the merged graph. A common neighbor of uu and vv loses one edge, and using its old degree makes the test refuse merges it could allow.
  • A copy is not an interference. If the build phase adds an edge between the two names of every copy, no copy can ever be coalesced. At the copy x = y, the source yy is left out of the edges of xx.
  • Safe is not optimal. The tests are sufficient conditions, and the order of the copies changes which merges pass. Finding the best conservative coalescing is NP-complete.
  • Aggressive is not safe. Merging every pair of non-interfering copy ends can turn a 2-colorable graph into a triangle.
  • The guarantee is about this graph. If the graph already has a nonempty kk-core, the tests keep it kk-colorable when it was, but the allocator may still spill for other reasons.

Exercises

  1. Show that the Briggs test with the degrees of GG instead of G′G' is still safe, but refuses more merges.

    Answer. For a neighbor tt of ww, deg⁡G′(t)=deg⁡G(t)−1\deg_{G'}(t) = \deg_G(t) - 1 if tt is a common neighbor and deg⁡G(t)\deg_G(t) otherwise. So deg⁡G′(t)≤deg⁡G(t)\deg_{G'}(t) \le \deg_G(t), and every vertex counted as high with the degrees of G′G' is also high with the degrees of GG. If the test passes with the degrees of GG, it passes with those of G′G', and the merge is safe. A common neighbor of degree exactly kk in GG is high in GG and low in G′G', so the version with GG can refuse a merge that the right version allows.

  2. In the George test, why can the degrees of GG be used, not those of G′G'?

    Answer. The test looks only at neighbors tt of vv that are not neighbors of uu. Such a tt loses its neighbor vv and gains ww, and it was not adjacent to uu, so its degree in G′G' is the same as in GG.

  3. Let uu have no neighbor of high degree, and let vv be any vertex not adjacent to uu. Which test passes for merging uu into vv?

    Answer. The George test, merging uu into vv: every neighbor of uu has low degree. The Briggs test may fail if vv has kk or more neighbors of high degree.

  4. In the cycle 0,1,2,3,4,50, 1, 2, 3, 4, 5 with k=2k = 2, is there a merge of two non-adjacent vertices that the Briggs test allows?

    Answer. No. Two non-adjacent vertices are at distance 2 or 3. At distance 2, as for 00 and 22, the merged vertex has three neighbors, and the two that are not common keep degree 2. At distance 3, as for 00 and 33, the merged vertex has four neighbors, each of degree 2 after the merge. In both cases at least 2 neighbors have degree at least 2. So the Briggs test refuses every merge in this cycle, although the merge of 00 and 22 keeps it 2-colorable.

References

  1. 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 with the test on the number of neighbors of significant degree
  2. Lal George and Andrew W. Appel, Iterated register coalescing, ACM Transactions on Programming Languages and Systems 18(3), 1996, 300-324, the George test, its use for precolored nodes, and the proofs that both tests are safe
  3. 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
  4. 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 NP-completeness of conservative coalescing and the analysis of the incremental question that the two tests answer in part

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
  • three classic examples