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 and . Both guarantee that if the graph was -colorable it stays -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 colors and uses simplification, as in Chaitin's allocator, optimistic coloring or iterated register coalescing.
- There is a copy
x = ywhose 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 and low otherwise.
The template
Let and be the vertices of and , not adjacent, and let be the graph after the merge, where the new vertex has the neighbors . A common neighbor of and loses one edge in ; 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 and if BRIGGS-OK passes, or if GEORGE-OK passes in one of the two directions. Each test runs in time with adjacency sets.
Why the Briggs test is safe. The neighbors of with low degree in can be simplified away. Then has fewer than neighbors left, all of high degree, so can be simplified too. What remains is without , and some low vertices, a subgraph of . So a -coloring of gives one of what remains, and the simplify rule extends it back to . For the -core: in a nonempty -core of that contains , the vertex would need neighbors of degree at least , which the test excludes.
Why the George test is safe. The neighbors of that are not neighbors of have low degree, and they keep the same degree in . Simplify them away. Then has exactly the neighbors of , so what remains is without and those vertices, with in the place of . Again it is a subgraph of .
The full proofs, for both -colorability and the empty -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 , the interference graph is the cycle plus a vertex with no edge, and there is a copy i = n. Merging into gives a vertex with the neighbors and , both of degree 2, so the Briggs test fails. The George test, merging into , passes at once: has no neighbor.
A merge that only Briggs allows. Take . Let have the neighbor , and the neighbor , where and each have two more neighbors of their own, so both have degree 3. The merged vertex has the neighbors and , two of high degree, fewer than 3, so the Briggs test passes. The George test fails in both directions: is not a neighbor of and has degree 3, and is not a neighbor of and has degree 3.
A merge that both refuse, rightly. In the path with , merging and makes a triangle. After the merge, and have degree 2, so the Briggs test fails. The George test fails both ways: is not a neighbor of and is not a neighbor of , and both have degree 2. Aggressive coalescing would merge them and force a spill.
A merge that both refuse, wrongly. In the cycle with , merge and . The merged vertex has the neighbors , and and have degree 2 after the merge, so the Briggs test fails. The George test fails both ways, because of and of . But the merged graph has the edges -, -, -, - and -, and it is bipartite with the sides and , 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 , 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 the precolored vertex, needs only the neighbors of the other vertex and a membership check in , 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 -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 and 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 is left out of the edges of . - 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 -core, the tests keep it -colorable when it was, but the allocator may still spill for other reasons.
Exercises
-
Show that the Briggs test with the degrees of instead of is still safe, but refuses more merges.
Answer. For a neighbor of , if is a common neighbor and otherwise. So , and every vertex counted as high with the degrees of is also high with the degrees of . If the test passes with the degrees of , it passes with those of , and the merge is safe. A common neighbor of degree exactly in is high in and low in , so the version with can refuse a merge that the right version allows.
-
In the George test, why can the degrees of be used, not those of ?
Answer. The test looks only at neighbors of that are not neighbors of . Such a loses its neighbor and gains , and it was not adjacent to , so its degree in is the same as in .
-
Let have no neighbor of high degree, and let be any vertex not adjacent to . Which test passes for merging into ?
Answer. The George test, merging into : every neighbor of has low degree. The Briggs test may fail if has or more neighbors of high degree.
-
In the cycle with , 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 and , the merged vertex has three neighbors, and the two that are not common keep degree 2. At distance 3, as for and , 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 and keeps it 2-colorable.
References
- 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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag VQDV7X Edit this page History Report an error Markdown