SSA-based Register Allocation

About

Register allocation is one of the most important optimizations in a modern compiler. This is basically because of increasing gap between memory/cache access time and register access time. To quote Hennessy & Patterson in Computer Architecture: A Quantitative Approach:

Because of the central role that register allocation plays, both in speeding up the code and in making other optimizations useful, it is one of the most important—if not the most important—optimizations.

Graph Coloring

A useful abstraction for register allocation is coloring the so-called interference graph:

A variable corresponds to a node in an undirected graph.

If two variables are alive at the same time, their nodes are connected by an edge.

This reduction has (at least) two inconvenient consequences:

It forces us to use a heuristic to perform register allocation. This heuristic might fail to color the graph with k colors, although χ(G) ≤ k As a result, the heuristic decides to spill some variables to memory. Hence, we inserted costly memory operations although there is a valid register allocation for the program with k registers. One might think that if maximum number of simultaneously live variables (the register pressure) in a program P is n, then we can find an n-coloring of P's interference graph G. However, this not true. In fact, there exist programs with an arbitrarily large gap between the register pressure and the register demand. In terms of graph theory, this is reflected by the inequality χ(G) ≥ ω(G).

ω(G) is the clique number of the graph. That is the size of the largest clique in G that (roughly) corresponds to the register pressure.

χ(G) is the chromatic number χ(G) of the graph, i.e. the minimum number of colors needed for a coloring of G.

Mycielski's construction is one example of how to construct graphs with ω(G)=2 and an arbitrarily high χ(G)

SSA

However, if we require the program to be in SSA-form, a program-representation property widely used in modern compilers, this no longer holds. Around 2005, three research groups independently showed that the interference graphs of SSA-form programs are chordal. Chordal graphs have two properties that make them especially appealing for register allocation:

They are optimally colorable in time O(ω(G) ⋅ |V|). Such a coloring can be computed using a perfect elimination order. Chordal graphs are perfect. This implies ω(H) = χ(H) for every induced subgraph H of G.

Both properties correlate tightly to the program:

The dominance relation of the SSA-form program induces a perfect elimination order on the program's interference graph. As a consequence, the interference graph does not have to be constructed as a data structure. It is sufficient to traverse the control-flow graph of the program in an order that is compatible with dominance and assign registers first-come first-serve. For every clique in the interference graph, there is a program point in the control-flow graph of the program, where all variables in that clique are simultaneously live. As a consequence, one can look at the program and identify all program points where the register pressure is excessive (i.e. larger than the number of available registers = k). By lowering the register pressure at these points to at most k, it is ensured that there is no clique larger than k in the graph. Hence ω(G) ≤ k. Because we have ω(G) = χ(G) for chordal graphs, the graph is colorable with k colors.

Links and Material

Publications of our group

Journal Papers

Optimal Register Allocation for SSA-form Programs in polynomial Time

Hack, S. and Goos, G.

Information Processing Letters, 98 (4): 150–155, 2006. [doi] [bib]

@ARTICLE{HG:2006:RA, author = {Sebastian Hack and Gerhard Goos}, title = {{O}ptimal {R}egister {A}llocation for {SSA}-form {P}rograms in polynomial {T}ime}, journal = {Information Processing Letters}, year = {2006}, volume = {98}, pages = {150--155}, number = {4}, month = {May}, booktitle = {Information Processing Letters}, doi = {10.1016/j.ipl.2006.01.008}, publisher = {Elsevier} }

Conferences

Preference-Guided Register Assignment - CC 2010

Braun, M., Mallon, C. and Hack, S.

Compiler Construction , pages 205–223, Springer, 2010. [doi] [bib]

@CONFERENCE{BMH:2010:Preference, author = {Matthias Braun and Christoph Mallon and Sebastian Hack}, title = {{P}reference-{G}uided {R}egister {A}ssignment}, booktitle = {Compiler Construction}, booktitle_short = {CC}, year = {2010}, isbn = {978-3-642-11969-9}, pages = {205--223}, publisher = {Springer}, location = {Paphos, Cyprus}, volume = {6011}, series = {Lecture Notes In Computer Science}, doi = {10.1007/978-3-642-11970-5}, }

Braun, M., Mallon, C. and Hack, S. , pages 205–223, Springer, 2010. [doi] [bib] Register Spilling and Live-Range Splitting for SSA-Form Programs - CC 2009

Braun, M. and Hack, S.

Compiler Construction , pages 174–189, Springer, 2009. [doi] [bib]

@CONFERENCE{BH:2009:Spill, author = {Matthias Braun and Sebastian Hack}, title = {{R}egister {S}pilling and {L}ive-{R}ange {S}plitting for {SSA}-{F}orm {P}rograms}, booktitle = {Compiler Construction}, booktitle_short = {CC}, year = {2009}, volume = {5501}, series = {Lecture Notes In Computer Science}, pages = {174--189}, publisher = {Springer}, doi = {10.1007/978-3-642-00722-4_13}, location = {York, England} }

Braun, M. and Hack, S. , pages 174–189, Springer, 2009. [doi] [bib] Register Coalescing by Graph Recoloring - PLDI 2008

Hack, S. and Goos, G.

Conference on Programming Language Design and Implementation , pages 227–237, ACM, 2008. [doi] [bib]

@CONFERENCE{HG:2008:Recolor, author = {Sebastian Hack and Gerhard Goos}, title = {{R}egister {C}oalescing by {G}raph {R}ecoloring}, booktitle = {Conference on Programming Language Design and Implementation}, booktitle_short = {PLDI}, year = {2008}, pages = {227--237}, address = {New York, NY, USA}, publisher = {ACM}, doi = {10.1145/1375581.1375610}, isbn = {978-1-59593-860-2}, location = {Tucson, AZ, USA} }

Hack, S. and Goos, G. , pages 227–237, ACM, 2008. [doi] [bib] A Fast Cutting-Plane Algorithm for Optimal Coalescing - CC 2007

(Best Paper Award)

Grund, D. and Hack, S.

Compiler Construction , pages 111–125, Springer, 2007. [doi] [bib]

@CONFERENCE{GH:2007:Coal, author = {Daniel Grund and Sebastian Hack}, title = {A {F}ast {C}utting-{P}lane {A}lgorithm for {O}ptimal {C}oalescing}, booktitle = {Compiler Construction}, booktitle_short = {CC}, year = {2007}, editor = {Shriram Krishnamurthi and Martin Odersky}, volume = {4420}, series = {Lecture Notes In Computer Science}, pages = {111--125}, month = {March}, publisher = {Springer}, award = {Best Paper Award}, doi = {10.1007/978-3-540-71229-9_8}, location = {Braga, Portugal} }

Grund, D. and Hack, S. , pages 111–125, Springer, 2007. [doi] [bib] Register Allocation for Programs in SSA-Form - CC 2006

Hack, S., Grund, D. and Goos, G.

Compiler Construction, pages 247–262, Springer, 2006. [doi] [bib]

@CONFERENCE{HGG:2006:RA_SSA, author = {Sebastian Hack and Daniel Grund and Gerhard Goos}, title = {{R}egister {A}llocation for {P}rograms in {SSA}-{F}orm}, booktitle = {Compiler Construction}, booktitle_short = {CC}, year = {2006}, editor = {Andreas Zeller and Alan Mycroft}, volume = {3923}, series = {Lecture Notes In Computer Science}, pages = {247--262}, month = {March}, publisher = {Springer}, abstract = {As register allocation is one of the most important phases in optimizingcompilers, much work has been done to improve its quality and speed.We present a novel register allocation architecture for programs inSSA-form which simplifies register allocation significantly. We investigatecertain properties of SSA-programs and their interference graphs,showing that they belong to the class of chordal graphs. This leadsto a quadratic-time optimal coloring algorithm and allows for decouplingthe tasks of coloring, spilling and coalescing completely. After presentingheuristic methods for spilling and coalescing, we compare our coalescingheuristic to an optimal method based on integer linear programming.}, doi = {10.1007/11688839_20}, journal = {Lecture Notes In Computer Science} }

Technical Reports

Interference Graphs of Programs in SSA Form

Hack, S.

[pdf] [bib]

@TECHREPORT{Hack:2005:IfgSSA, author = {Sebastian Hack}, title = {{I}nterference {G}raphs of {P}rograms in {SSA} {F}orm}, institution = {Universit{\"a}t Karlsruhe}, year = {2005}, number = {2005-15}, month = {June}, school = {IPD Goos}, webpdf = {http://compilers.cs.uni-saarland.de/papers/ifg_ssa.pdf} }

Hack, S. [pdf] [bib] Towards Register Allocation for Programs in SSA-Form

Hack, S., Grund, D. and Goos, G.

[pdf] [bib]

@TECHREPORT{HGG:2005:RegisterSSA, author = {Sebastian Hack and Daniel Grund and Gerhard Goos}, title = {{T}owards {R}egister {A}llocation for {P}rograms in {SSA}-{F}orm}, institution = {Universit{\"a}t Karlsruhe}, year = {2005}, number = {2005-27}, month = {September}, abstract = {In this technical report, we present an architecture for register allocationon the SSA form. We show, how the properties of SSA-form programsand their interference graphs can be exploited to develop new methodsfor spilling, coloring and coalescing. We present heuristic and optimalsolution methods for these three subtasks.}, webpdf = {http://compilers.cs.uni-saarland.de/papers/ra_ssa.pdf} }

PhD Thesis