Theoremadvanced
Chordality of SSA interference graphs
Also called Hack-Grund-Goos theorem.
The interference graph of a strict SSA program is chordal, so it is colored optimally in polynomial time and its clique size gives the bound.
The chordality theorem for SSA says that the interference graph of a program in strict SSA form is a chordal graph. The definitions of the names, listed in an order that respects dominance and read backward, form a perfect elimination ordering. So the graph is colored with the fewest colors by a greedy pass, and that number equals the largest number of names live at one point. Coloring an arbitrary interference graph is NP-hard, so SSA form moves the hard part of register allocation out of coloring and into spilling and copies. The result was found in 2005 by three groups independently, and it is the basis of SSA-based register allocation.
Statement
The program model. A program is a control flow graph with an entry block. Each block holds phis, then ordinary instructions, then a terminator. We work on the item graph : its nodes are one node that defines the parameters, one node per block with phis, which defines all phis of at once, one node per ordinary instruction and one per terminator. The edges go from to the first node of the entry block, from each node to the next one in its block, and from the terminator of a block to the first node of each successor block. A phi operand of for the predecessor is read by the terminator node of . Write and for the names that node defines and reads. Dominance is taken in from .
Strict SSA. Each name is defined by exactly one node , and strictly dominates every node that reads .
Liveness and interference. A name is live in at if some path in has and for . It is live out at if it is live in at some successor of . Two different names and interfere if some node has and , where is the live-out set, or the same with and exchanged. This is the rule of the interference graph page without its exception for copies. Let
the largest number of names that hold a value just after some node.
Theorem. Let be a program in strict SSA form, let be its interference graph, and let list the names by their defining nodes in a preorder of the dominator tree of , with the names of one node in any order. Then:
- read backward is a perfect elimination ordering of , so is chordal.
- : the largest clique, the chromatic number and the register pressure are equal.
- Coloring the names in the order , each with the lowest color not used by its colored neighbors, uses exactly colors.
In plain words
In SSA form each value is written in one place, and that place comes before every read of the value on every path. So if two values are alive at the same moment, the one written first was already alive when the second was written. Now walk through the program in the order the dominator tree gives, and give each value a register when it is written. The values that already have a register and clash with the new one are all alive at the point where it is written, and they all clash with each other. So the new value has as many clashing values as there are values alive at that point, minus itself. That means the number of registers needed is exactly the largest number of values alive at once, and a simple greedy pass finds such an assignment.
Proof
Lemma 1 (a live name is dominated by its definition). If is live in at , then strictly dominates . If , then dominates .
Proof. Let be the path in the definition, with . By strictness , and for , so no node of the path is . If did not dominate , a path from to that avoids , followed by this path, would reach without passing . That is false, because dominates . So dominates , and . For the second claim, means . If , then is live in at a successor of and does not define it, so is live in at .
Lemma 2 (live after a later definition). Let strictly dominate a node , and let be live in at . If strictly dominates and dominates , then .
Proof. Take any path from to . It passes ; let be the part after the last visit of . Suppose does not pass . Since strictly dominates , does not dominate , so some path from to avoids . That path followed by reaches without , which is false. So passes , and the part of from to does not pass . Follow it with the path that makes live in at . The result starts at the successor of on , reads at its end, and has no definition of before that read. So is live in at that successor, and .
Lemma 3 (one point gives a clique). For each node , any two different names in interfere.
Proof. Let be in the set. If one of them is in , the rule of interference applies at . Otherwise both are in , so both are live in at , and by Lemma 1 both and strictly dominate . The dominators of a node lie on one path of the dominator tree, so they are ordered: say dominates . If , the rule applies at that node. Otherwise Lemma 2 with gives , and , so the rule applies at .
Lemma 4 (earlier neighbors are live at the definition). If and interfere and comes before in , then .
Proof. By the rule, some node defines one of them while the other is in . If defines , then and we are done. Otherwise and . If , then and . If , then is live in at , so by Lemma 1 strictly dominates . A preorder of the dominator tree lists a strict dominator first, so would come before in , which is false.
Proof of the theorem. Fix a name and let be its neighbors that come before it in . By Lemma 4, , and by Lemma 3 that set is a clique. So the neighbors of that come after it in read backward form a clique, which is the definition of a perfect elimination ordering. A graph with a perfect elimination ordering is chordal (Fulkerson and Gross). This is part 1.
For parts 2 and 3, Lemma 3 says that each set is a clique, so . The greedy coloring in the order gives the lowest color not used by . Since , that color is below . With for every graph, we get
The live-in sets give nothing larger. The node has no live-in name by Lemma 1, and every other node has a predecessor with . So is also the largest number of names live at any point between two nodes, when a name that is defined and never read counts at the point just after its definition.
Consequences
- Allocation is easy once the pressure is low. If , the strict SSA program can be colored with registers, and the greedy pass of part 3 finds the coloring in time linear in the size of the graph, or without the graph by walking the dominator tree. This is the coloring phase of SSA-based register allocation.
- Spilling and coloring come apart. To make a strict SSA program fit into registers, it is enough to lower the pressure to at every point. No coloring step can then fail, so there is no loop of spill and color as in Chaitin's allocator. Choosing the cheapest spills is still NP-hard (Bouchez, Darte and Rastello, 2007), and so is the best coalescing.
- Clique and coloring by known methods. Every chordal graph is perfect, and chordal graph coloring along any perfect elimination ordering is optimal. The dominance order gives such an ordering for free, with no maximum cardinality search.
- Not NP-complete under SSA. The reduction behind register allocation NP-completeness builds programs whose interference graphs are arbitrary. Those programs are not in strict SSA form, and the theorem shows they cannot be.
Tightness
SSA destruction breaks it. The theorem needs one definition per name. After out-of-SSA translation replaces the phis by copies, a name has several definitions, and the graph can be any graph. The loop on the interference graph page has a chordless cycle --- of length 4. Pereira and Palsberg (2006) proved that coloring the interference graph after the classical replacement of phis by copies is NP-complete. So an allocator that uses the theorem must color before it leaves SSA form, and then implement the phis with moves between registers.
The copy exception breaks it. The interference rule of the interference graph page skips the source of a copy. That gives a subgraph of , and a subgraph of a chordal graph need not be chordal. In this straight-line strict SSA program
a = p + 1
b = a + 1
c = a
e = b + c
d = a + c
f = d + a
g = f + c
h = g + e
ret h
the full graph is chordal, with the edge -. The copy c = a removes that edge, and then --- is a cycle of length 4 with no chord, since is dead before is defined. The coloring of is still a proper coloring of the subgraph, so colors (the names just after d = a + c) still suffice. The theorem itself needs the full rule.
Strictness is needed. If does not dominate a use of , Lemma 1 fails, and two interfering names need not have ordered definitions. In
entry:
br p B1 B2
B1:
x = 1
jump B3
B2:
y = 2
jump B3
B3:
br p B4 B5
B4:
ret x
B5:
ret y
each name has one definition, but ret x is not dominated by x = 1. The name is live just after x = 1 (along B1, B3, B5), so and interfere, and neither definition dominates the other. The proof of Lemma 4 no longer applies. The usual fix, a definition such as y = 0 at the entry, makes the program strict.
The bound is exact. Parts 2 and 3 are equalities: a strict SSA program never needs fewer registers than , and never more when no name is spilled.
History
Three groups found the theorem in 2005, each on its own. Brisk, Dabiri, Macbeth and Sarrafzadeh presented it at IWLS 2005 for register sharing in high-level synthesis. Hack, Grund and Goos at Karlsruhe wrote a technical report in 2005 and published the result at CC 2006, with a full allocator built on it; Hack and Goos gave a short constructive proof in Information Processing Letters in 2006. Bouchez, Darte, Guillon and Rastello proved it again and studied what it means for the NP-completeness proof of Chaitin and others, in a paper at LCPC 2006 (published 2007). In the same period Pereira and Palsberg (2005) observed that most interference graphs of Java methods are chordal even without SSA, and (2006) showed that the property is lost again after the classical SSA elimination.
References
- 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 lemma that the definition of a name live at a point dominates that point, the lemma that the definitions of two interfering names are ordered by dominance, and the theorem that the interference graph of a strict SSA program is chordal
- Sebastian Hack and Gerhard Goos, Optimal register allocation for SSA-form programs in polynomial time, Information Processing Letters 98(4), 2006, 150-155, the constructive proof that the definitions in a dominance order give a perfect elimination order, and the coloring with the register pressure as the number of colors
- Philip Brisk, Foad Dabiri, Jamie Macbeth and Majid Sarrafzadeh, Polynomial time graph coloring register allocation, 14th International Workshop on Logic and Synthesis (IWLS 2005), June 2005, 447-454, the independent proof that the interference graphs of SSA programs are chordal, in the setting of high-level synthesis (the link is the workshop program)
- 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, LCPC 2006, LNCS 4382, Springer, 2007, 283-298, the third independent proof of chordality under strict SSA, and what it means for the complexity of spilling and coalescing
- Fernando Magno Quintão Pereira and Jens Palsberg, Register allocation after classical SSA elimination is NP-complete, FoSSaCS 2006, LNCS 3921, Springer, 2006, the proof that coloring the interference graph after the classical replacement of phis by copies is NP-complete
- Fernando Magno Quintão Pereira and Jens Palsberg, Register allocation via coloring of chordal graphs, APLAS 2005, LNCS 3780, Springer, 2005, 315-329, the observation that most interference graphs of Java methods are chordal, and an allocator built on that
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 | W4 complete |
|
Tag 1SRBV4 Edit this page History Report an error Markdown