Algorithm Search⌘K

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 HH: its nodes are one node ϵ\epsilon that defines the parameters, one node φB\varphi_B per block BB with phis, which defines all phis of BB at once, one node per ordinary instruction and one per terminator. The edges go from ϵ\epsilon 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 aa of BB for the predecessor PP is read by the terminator node of PP. Write def(v)\mathrm{def}(v) and use(v)\mathrm{use}(v) for the names that node vv defines and reads. Dominance is taken in HH from ϵ\epsilon.

Strict SSA. Each name xx is defined by exactly one node dxd_x, and dxd_x strictly dominates every node that reads xx.

Liveness and interference. A name xx is live in at vv if some path v=v0,v1,…,vmv = v_0, v_1, \ldots, v_m in HH has x∈use(vm)x \in \mathrm{use}(v_m) and x∉def(vi)x \notin \mathrm{def}(v_i) for i<mi < m. It is live out at vv if it is live in at some successor of vv. Two different names xx and yy interfere if some node vv has x∈def(v)x \in \mathrm{def}(v) and y∈def(v)∪out(v)y \in \mathrm{def}(v) \cup \mathrm{out}(v), where out(v)\mathrm{out}(v) is the live-out set, or the same with xx and yy exchanged. This is the rule of the interference graph page without its exception for copies. Let

MaxLive=max⁡v ∣def(v)∪out(v)∣,\mathrm{MaxLive} = \max_v \, |\mathrm{def}(v) \cup \mathrm{out}(v)|,

the largest number of names that hold a value just after some node.

Theorem. Let PP be a program in strict SSA form, let GG be its interference graph, and let σ\sigma list the names by their defining nodes in a preorder of the dominator tree of HH, with the names of one node in any order. Then:

  1. σ\sigma read backward is a perfect elimination ordering of GG, so GG is chordal.
  2. ω(G)=χ(G)=MaxLive\omega(G) = \chi(G) = \mathrm{MaxLive}: the largest clique, the chromatic number and the register pressure are equal.
  3. Coloring the names in the order σ\sigma, each with the lowest color not used by its colored neighbors, uses exactly MaxLive\mathrm{MaxLive} 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 xx is live in at vv, then dxd_x strictly dominates vv. If x∈def(v)∪out(v)x \in \mathrm{def}(v) \cup \mathrm{out}(v), then dxd_x dominates vv.

Proof. Let v=v0,…,vmv = v_0, \ldots, v_m be the path in the definition, with x∈use(vm)x \in \mathrm{use}(v_m). By strictness dx≠vmd_x \ne v_m, and x∉def(vi)x \notin \mathrm{def}(v_i) for i<mi < m, so no node of the path is dxd_x. If dxd_x did not dominate vv, a path from ϵ\epsilon to vv that avoids dxd_x, followed by this path, would reach vmv_m without passing dxd_x. That is false, because dxd_x dominates vmv_m. So dxd_x dominates vv, and dx≠v=v0d_x \ne v = v_0. For the second claim, x∈def(v)x \in \mathrm{def}(v) means dx=vd_x = v. If x∈out(v)∖def(v)x \in \mathrm{out}(v) \setminus \mathrm{def}(v), then xx is live in at a successor of vv and vv does not define it, so xx is live in at vv. □\square

Lemma 2 (live after a later definition). Let dyd_y strictly dominate a node uu, and let yy be live in at uu. If dyd_y strictly dominates dzd_z and dzd_z dominates uu, then y∈out(dz)y \in \mathrm{out}(d_z).

Proof. Take any path π\pi from ϵ\epsilon to uu. It passes dyd_y; let π′\pi' be the part after the last visit of dyd_y. Suppose π′\pi' does not pass dzd_z. Since dyd_y strictly dominates dzd_z, dzd_z does not dominate dyd_y, so some path from ϵ\epsilon to dyd_y avoids dzd_z. That path followed by π′\pi' reaches uu without dzd_z, which is false. So π′\pi' passes dzd_z, and the part of π′\pi' from dzd_z to uu does not pass dyd_y. Follow it with the path that makes yy live in at uu. The result starts at the successor of dzd_z on π′\pi', reads yy at its end, and has no definition of yy before that read. So yy is live in at that successor, and y∈out(dz)y \in \mathrm{out}(d_z). □\square

Lemma 3 (one point gives a clique). For each node vv, any two different names in def(v)∪out(v)\mathrm{def}(v) \cup \mathrm{out}(v) interfere.

Proof. Let y≠zy \ne z be in the set. If one of them is in def(v)\mathrm{def}(v), the rule of interference applies at vv. Otherwise both are in out(v)∖def(v)\mathrm{out}(v) \setminus \mathrm{def}(v), so both are live in at vv, and by Lemma 1 both dyd_y and dzd_z strictly dominate vv. The dominators of a node lie on one path of the dominator tree, so they are ordered: say dyd_y dominates dzd_z. If dy=dzd_y = d_z, the rule applies at that node. Otherwise Lemma 2 with u=vu = v gives y∈out(dz)y \in \mathrm{out}(d_z), and z∈def(dz)z \in \mathrm{def}(d_z), so the rule applies at dzd_z. □\square

Lemma 4 (earlier neighbors are live at the definition). If xx and yy interfere and yy comes before xx in σ\sigma, then y∈def(dx)∪out(dx)y \in \mathrm{def}(d_x) \cup \mathrm{out}(d_x).

Proof. By the rule, some node vv defines one of them while the other is in def(v)∪out(v)\mathrm{def}(v) \cup \mathrm{out}(v). If vv defines xx, then v=dxv = d_x and we are done. Otherwise v=dyv = d_y and x∈def(dy)∪out(dy)x \in \mathrm{def}(d_y) \cup \mathrm{out}(d_y). If x∈def(dy)x \in \mathrm{def}(d_y), then dx=dyd_x = d_y and y∈def(dx)y \in \mathrm{def}(d_x). If x∈out(dy)∖def(dy)x \in \mathrm{out}(d_y) \setminus \mathrm{def}(d_y), then xx is live in at dyd_y, so by Lemma 1 dxd_x strictly dominates dyd_y. A preorder of the dominator tree lists a strict dominator first, so xx would come before yy in σ\sigma, which is false. □\square

Proof of the theorem. Fix a name xx and let N−(x)N^-(x) be its neighbors that come before it in σ\sigma. By Lemma 4, N−(x)∪{x}⊆def(dx)∪out(dx)N^-(x) \cup \{x\} \subseteq \mathrm{def}(d_x) \cup \mathrm{out}(d_x), and by Lemma 3 that set is a clique. So the neighbors of xx that come after it in σ\sigma 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 def(v)∪out(v)\mathrm{def}(v) \cup \mathrm{out}(v) is a clique, so ω(G)≥MaxLive\omega(G) \ge \mathrm{MaxLive}. The greedy coloring in the order σ\sigma gives xx the lowest color not used by N−(x)N^-(x). Since ∣N−(x)∣≤∣def(dx)∪out(dx)∣−1≤MaxLive−1|N^-(x)| \le |\mathrm{def}(d_x) \cup \mathrm{out}(d_x)| - 1 \le \mathrm{MaxLive} - 1, that color is below MaxLive\mathrm{MaxLive}. With ω(G)≤χ(G)\omega(G) \le \chi(G) for every graph, we get

MaxLive≤ω(G)≤χ(G)≤MaxLive.□\mathrm{MaxLive} \le \omega(G) \le \chi(G) \le \mathrm{MaxLive}. \qquad \square

The live-in sets give nothing larger. The node ϵ\epsilon has no live-in name by Lemma 1, and every other node vv has a predecessor uu with in(v)⊆out(u)\mathrm{in}(v) \subseteq \mathrm{out}(u). So MaxLive\mathrm{MaxLive} 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 MaxLive≤k\mathrm{MaxLive} \le k, the strict SSA program can be colored with kk 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 kk registers, it is enough to lower the pressure to kk 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 aa-bb-cc-dd 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 GG, 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 GG is chordal, with the edge aa-cc. The copy c = a removes that edge, and then aa-bb-cc-dd is a cycle of length 4 with no chord, since bb is dead before dd is defined. The coloring of GG is still a proper coloring of the subgraph, so MaxLive=4\mathrm{MaxLive} = 4 colors (the names a,c,d,ea, c, d, e just after d = a + c) still suffice. The theorem itself needs the full rule.

Strictness is needed. If dxd_x does not dominate a use of xx, 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 yy is live just after x = 1 (along B1, B3, B5), so xx and yy 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 MaxLive\mathrm{MaxLive}, 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

  1. 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
  2. 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
  3. 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)
  4. 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
  5. 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
  6. 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.

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)
WritingW4 complete
  • a review of the text: no review record reviews/1SRBV4/text.yaml