Algorithm Search⌘K

Definitioncore

Interference graph

Also called register interference graph.

An undirected graph with one vertex per name of a program and an edge between two names that cannot share a register, because one is live where the other is defined.

The interference graph of a program has one vertex for each name and an edge between two names that must not share a register. Two names get an edge when one of them is live just after an instruction that defines the other. A k-coloring of the graph is the same thing as an assignment of kk registers in which each name keeps one register for its whole life. The graph is the input of the allocators of Chaitin, Briggs and George and Appel; the register allocation problem is larger, because it also allows spills, splits and copies.

Definition

Let PP be a program in the toy IR. Live variable analysis gives, for each instruction ii, the set out(i)\mathrm{out}(i) of names live just after ii. The memory slots mem.x are not register candidates and are left out. A parameter is treated as defined at the entry; an allocator usually makes this explicit with a load p = mem.p in a new entry block.

The interference graph G=(V,E)G = (V, E) is the undirected graph with these vertices and edges:

  • VV is the set of names of PP.
  • For each instruction ii that defines a name xx, and for each y∈out(i)y \in \mathrm{out}(i) with y≠xy \ne x, the edge {x,y}\{x, y\} is in EE, except when ii is a copy x = y.

No other edge is in EE. Two names interfere if they are joined by an edge.

The rule is Chaitin's. It looks at the definitions and not at whole live ranges, for two reasons:

  1. A copy does not make its source and target interfere. Just after x = y, both names hold the same value, so one register can serve both. If one of them is later redefined while the other is still live, that later definition adds the edge.
  2. A dead definition still interferes. If xx is not live after its definition ii, the instruction still writes a register. So xx gets an edge to each name in out(i)\mathrm{out}(i), and the write cannot destroy a live value.

The live range of a name is the set of points where it is live. A weaker test says that two names interfere when their live ranges meet. In a strict program, where every path from the entry to a use of a name passes through a definition of it, two live ranges that meet give an edge in EE unless a copy joins the two names: on a path from the entry to a common point, take the later of the two last definitions; the other name is live just after it. Compared with the weaker test, the rule above adds edges only at dead definitions, and removes edges only at copies.

The copies x = y between two names form a second set of edges, the move edges or affinities. They are not interference edges. A coalescer tries to give the two ends of a move edge the same color, so that the copy disappears (register coalescing problem).

A machine register that an instruction requires (an argument of a call, a result) is a precolored vertex: its color is fixed in advance.

Allocators often build the graph on webs instead of names. A web is a maximal set of definitions and uses of one variable that are linked through def-use chains. In a strict program two webs of one variable are never live at the same point, so they can get different registers. Renaming each web to its own name and then building the graph on names gives the graph on webs.

Size and construction. The graph has n=∣V∣n = |V| vertices and at most n(n−1)/2n(n-1)/2 edges. A backward walk over each block from the live-out set visits each instruction once and adds one edge per live name, so the build takes time proportional to the sum of ∣out(i)∣|\mathrm{out}(i)| over all instructions, plus the liveness analysis. Chaitin kept both a bit matrix, to test an edge in constant time, and adjacency lists, to visit the neighbors of a vertex.

Examples and non-examples

A triangle. In the block

L0:
  a = p + 1
  b = p + 2
  c = a + b
  d = a * c
  e = b * d
  ret e

the sets out(i)\mathrm{out}(i) are {p,a}\{p, a\}, {a,b}\{a, b\}, {a,b,c}\{a, b, c\}, {b,d}\{b, d\} and {e}\{e\}. The edges are pp-aa, aa-bb, aa-cc, bb-cc and bb-dd, and ee has no edge. The triangle a,b,ca, b, c shows that three registers are needed for an assignment without spills.

A copy that does not interfere. In

L0:
  y = x
  z = x + y
  ret z

the set out\mathrm{out} of the copy is {x,y}\{x, y\}, but the copy is from xx, so xx and yy get no edge, even though both are live at the same point. One register serves both, and after renaming the copy becomes r0 = r0, which is deleted. The test by meeting live ranges would put an edge here and waste a register.

The same copy, then a redefinition. If y = y + 1 follows y = x, then xx is live just after that new definition, and the edge xx-yy appears. The exception for copies is safe because each later change of either name is a definition, and the rule checks it.

A dead definition. In

L0:
  a = p + 1
  t = p * 2
  b = a + p
  ret b

the name t is never used, so its live range is empty. The set out\mathrm{out} of t = p * 2 is {a,p}\{a, p\}, so the rule adds tt-aa and tt-pp, and {p,a,t}\{p, a, t\} is a triangle. Under the test by meeting live ranges, t has no edge, it could get the register of a, and the write to t would destroy a.

Not every interference graph is chordal. The loop

L0:
  d = n
  jump L1
L1:
  a = d + 1
  b = a + d
  c = a + 1
  d = b - c
  d = d % c
  br d L1 L2
L2:
  ret d

has the edges aa-bb, bb-cc, cc-dd and dd-aa and no others: a cycle of length 4 with no chord. The copy d = n adds the move edge dd-nn but no interference. This graph is not a chordal graph, but it is 2-colorable. Simplification by degree fails on it with k=2k = 2, which is the example of optimistic coloring.

Every graph is an interference graph. For each graph there is a program whose interference graph is that graph plus one isolated vertex. The construction is on the page register-allocation-np-completeness, and it is the reason why the coloring form of allocation is NP-complete.

References

  1. Gregory J. Chaitin, Marc A. Auslander, Ashok K. Chandra, John Cocke, Martin E. Hopkins and Peter W. Markstein, Register allocation via coloring, Computer Languages 6(1), 1981, 47-57, the interference graph, its edges from the live names at each definition, and the proof that every graph is the interference graph of some program
  2. 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, the build phase, the bit matrix and adjacency lists, and the rule that a copy does not make its source and target interfere
  3. 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 interference graph of a strict SSA program is chordal
  4. Andrew W. Appel with Jens Palsberg, Modern Compiler Implementation in Java, second edition, Cambridge University Press, 2002, Chapter 10 (Liveness Analysis), the section on interference graphs and the special treatment of move instructions
  5. Steven S. Muchnick, Advanced Compiler Design and Implementation, Morgan Kaufmann, 1997, Chapter 16 (Register Allocation), webs, and the construction of the interference graph as an adjacency matrix and adjacency lists
  6. Keith D. Cooper and Linda Torczon, Engineering a Compiler, third edition, Morgan Kaufmann, 2022, Chapter 13 (Register Allocation), live ranges and the interference graph of global allocation

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/04HWFD/text.yaml