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 and the same register: the copy becomes r = r and is deleted. In the interference graph, this means merging the vertices of and into one vertex, which is possible only if and 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 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 with a set of affinity edges, one for each copy x = y between two names, and a weight for each , 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 . They interfere only if some other definition of one of them happens while the other is live.
A coalescing is a partition of into classes such that no two vertices of one class are adjacent in . The coalesced graph has the classes as vertices, and two classes are adjacent if some vertex of one is adjacent in to some vertex of the other. An affinity is coalesced if its two ends are in one class. A -coloring of gives a -coloring of in which the two ends of each coalesced affinity have the same color, so each coalesced copy is deleted.
Problem (coalescing). Given , , and , find a coalescing that maximizes the total weight of the coalesced affinities, such that 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. must stay greedy--colorable, that is, its -core must be empty, so that the simplify phase of Chaitin's allocator colors it. A stronger condition, " is -colorable", is also studied, but even checking it is NP-complete.
- Incremental conservative coalescing. is greedy--colorable and only one affinity is given: can and be merged so that the graph stays greedy--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--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 -, -, - and -, and the affinities are -, - and -. The classes and are a coalescing with one edge between them, so with all three copies are deleted. Iterated register coalescing finds it.
Two copies compete. Here and are both copies of :
L0:
a = p + 1
b = a
c = a
d = b * c
ret d
The only edge is -: at c = a, the name is live. The affinities are - and -. A class cannot hold both and , so at most one copy can be coalesced. Note that and 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, is a copy of 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 -, -, - and -. The path is 2-colorable, but merging and makes a triangle with and . With , the aggressive coalescing forces a spill, and an allocator with aggressive coalescing spills : 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 -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 , then change colors to make copy partners equal, which keeps the coloring valid at each step.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Iterated register coalescing | O(r (l + s n + n^2 (c + 1))) worst | O(n + m + s + c) extra | deterministicofflineiterative | 1 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 -colorability, and polynomial when the graph is chordal. For greedy--colorable graphs, Bouchez, Darte and Rastello (2007) left its complexity open.
- Optimistic coalescing is NP-complete, even for chordal graphs with (Bouchez, Darte and Rastello, 2007).
- At most copies can be removed, and every affinity whose ends interfere stays.
Reductions
-coloring reduces to aggressive coalescing. Given a graph with vertices and a number , build from by adding a clique of new vertices with no edge to . Add an affinity of weight 1 between each vertex of and each . Then some coalescing of coalesces affinities if and only if is -colorable.
Proof. The are pairwise adjacent, so they are in different classes, and each vertex is in the class of at most one . So gives at most one coalesced affinity, and affinities are coalesced exactly when each is in the class of some . If that holds, set the color of to : two adjacent vertices of cannot share a class, so this is a -coloring of . Conversely, a -coloring of gives the classes , which contain no edge of and coalesce all affinities.
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 (three-coloring). The clique plays the role of precolored machine registers.
For the conservative condition " is -colorable", hardness is immediate: with , the question is whether is -colorable.
Applications
- Leaving SSA form. Out-of-SSA translation replaces each -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
- 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
- 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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag A8A6R0 Edit this page History Report an error Markdown