Register Allocation for SSA-Form Programs
Register Allocation for SSA-Form Programs
Register Allocation for SSA-Form Programs
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Register</strong> <strong>Allocation</strong> <strong>for</strong> <strong>SSA</strong>-<strong>Form</strong> <strong>Programs</strong><br />
Sebastian Hack Daniel Grund<br />
(hack|daniel)@ipd.info.uni-karlsruhe.de<br />
Institut für Programmstrukturen und Datenorganisation<br />
Universität Karlsruhe<br />
31.01.2006<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 1 / 47
Overview<br />
1 Preliminaries<br />
<strong>Register</strong> <strong>Allocation</strong><br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 2 / 47
<strong>Register</strong> <strong>Allocation</strong><br />
<strong>Register</strong> <strong>Allocation</strong> is the task of mapping the program’s variables to<br />
processor registers<br />
Issues to be covered:<br />
Spilling Put variables into memory if there are not enough<br />
registers<br />
Coalescing Eliminate unneccessary copies in the program<br />
Often reduced to graph coloring<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 3 / 47
Interference Graphs<br />
Two variables are live at the same label ⇒ they interfere<br />
Each variable has a node in the interference graph (IG)<br />
Whenever two variables interfere, there is an edge between the<br />
corresponding nodes<br />
(a, b) = start<br />
if b < a<br />
c = a − b c = 0<br />
return c<br />
c<br />
a b<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 4 / 47
Interference Graphs<br />
Two variables are live at the same label ⇒ they interfere<br />
Each variable has a node in the interference graph (IG)<br />
Whenever two variables interfere, there is an edge between the<br />
corresponding nodes<br />
(a, b) = start<br />
if b < a<br />
c = a − b c = 0<br />
return c<br />
c<br />
a b<br />
Coloring gives register allocation<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 4 / 47
Chaitin/Briggs <strong>Register</strong> Allocator<br />
Spill<br />
Build Coalesce Color<br />
not k-colorable<br />
Every undirected graph can occur as an interference graph<br />
Determining chromatic number is N P-complete<br />
Color using heuristic ⇒ Iteration necessary<br />
Spilling is focused on the graph<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 5 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
d e<br />
a b<br />
c<br />
elimination order<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
d e<br />
a b<br />
c<br />
elimination order<br />
d,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
d e<br />
a b<br />
c<br />
elimination order<br />
d, e,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
d e<br />
a b<br />
c<br />
elimination order<br />
d, e, c,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
d e<br />
a b<br />
c<br />
elimination order<br />
d, e, c, a,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
d e<br />
a b<br />
c<br />
elimination order<br />
d, e, c, a, b<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
Re-insert the nodes in reverse order<br />
Assign each node the next possible color<br />
d e<br />
a b<br />
c<br />
elimination order<br />
d, e, c, a, b<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
Re-insert the nodes in reverse order<br />
Assign each node the next possible color<br />
d e<br />
a b<br />
c<br />
elimination order<br />
d, e, c, a,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
Re-insert the nodes in reverse order<br />
Assign each node the next possible color<br />
d e<br />
a b<br />
c<br />
elimination order<br />
d, e, c,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
Re-insert the nodes in reverse order<br />
Assign each node the next possible color<br />
d e<br />
a b<br />
c<br />
elimination order<br />
d, e,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
Re-insert the nodes in reverse order<br />
Assign each node the next possible color<br />
d e<br />
a b<br />
c<br />
elimination order<br />
d,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
Re-insert the nodes in reverse order<br />
Assign each node the next possible color<br />
d e<br />
a b<br />
c<br />
elimination order<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Coloring<br />
Subsequently remove the nodes from the graph<br />
Re-insert the nodes in reverse order<br />
Assign each node the next possible color<br />
d e<br />
a b<br />
c<br />
elimination order<br />
Theorem<br />
For each graph there is an elimination order leading to an optimal coloring<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 6 / 47
Perfect Elimination Orders<br />
Suppose all (not yet eliminated) neighbors of a node n <strong>for</strong>m a clique<br />
d e<br />
a b<br />
c<br />
elimination order<br />
a, c, d, e, b<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 7 / 47
Perfect Elimination Orders<br />
Suppose all (not yet eliminated) neighbors of a node n <strong>for</strong>m a clique<br />
d e<br />
a b<br />
c<br />
elimination order<br />
a, c, d, e,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 7 / 47
Perfect Elimination Orders<br />
Suppose all (not yet eliminated) neighbors of a node n <strong>for</strong>m a clique<br />
d e<br />
a b<br />
c<br />
elimination order<br />
a, c, d,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 7 / 47
Perfect Elimination Orders<br />
Suppose all (not yet eliminated) neighbors of a node n <strong>for</strong>m a clique<br />
d e<br />
a b<br />
c<br />
elimination order<br />
a, c,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 7 / 47
Perfect Elimination Orders<br />
Suppose all (not yet eliminated) neighbors of a node n <strong>for</strong>m a clique<br />
d e<br />
a b<br />
c<br />
elimination order<br />
a,<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 7 / 47
Perfect Elimination Orders<br />
Suppose all (not yet eliminated) neighbors of a node n <strong>for</strong>m a clique<br />
d e<br />
a b<br />
c<br />
elimination order<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 7 / 47
Perfect Elimination Orders<br />
Suppose all (not yet eliminated) neighbors of a node n <strong>for</strong>m a clique<br />
d e<br />
a b<br />
c<br />
elimination order<br />
Theorem<br />
A PEO allows <strong>for</strong> an optimal coloring in polynomial time<br />
The number of colors is bound by the size of the largest clique<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 7 / 47
Perfect Elimination Orders<br />
Not every graph has a PEO, e.g.<br />
The graphs which have PEOs are called chordal<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 8 / 47
Perfect Elimination Orders<br />
Not every graph has a PEO, e.g.<br />
The graphs which have PEOs are called chordal<br />
Main result<br />
The dominance relation in <strong>SSA</strong>-<strong>for</strong>m programs induces a PEO in the<br />
interference graph of the program<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 8 / 47
Overview<br />
1 Preliminaries<br />
<strong>Register</strong> <strong>Allocation</strong><br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 9 / 47
<strong>SSA</strong>-<strong>Form</strong><br />
Each variable has exactly one definition<br />
⇒ Identity of variables and dynamic constants (values)<br />
non-<strong>SSA</strong><br />
(a, b) = start<br />
if b < a<br />
c = a − b c = 0<br />
return c<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 10 / 47
<strong>SSA</strong>-<strong>Form</strong><br />
Each variable has exactly one definition<br />
⇒ Identity of variables and dynamic constants (values)<br />
φ-operations select values dependent on control flow<br />
non-<strong>SSA</strong><br />
(a, b) = start<br />
if b < a<br />
c = a − b c = 0<br />
return c<br />
<strong>SSA</strong><br />
(a, b) = start<br />
if b < a<br />
c1 = a − b c2 = 0<br />
c3 = φ(c1, c2)<br />
return c3<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 10 / 47
Dominance<br />
Crucial <strong>for</strong> <strong>SSA</strong>-<strong>for</strong>m programs is the concept of dominance:<br />
Definition<br />
ℓ1 dominates ℓ2 if each path from start to ℓ2 goes through ℓ1<br />
(a, b) = start<br />
if b < a<br />
c1 = a − b c2 = 0<br />
c3 = φ(c1, c2)<br />
return c3<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 11 / 47
Dominance<br />
Crucial <strong>for</strong> <strong>SSA</strong>-<strong>for</strong>m programs is the concept of dominance:<br />
Definition<br />
ℓ1 dominates ℓ2 if each path from start to ℓ2 goes through ℓ1<br />
<br />
(a, b) = start<br />
<br />
if b < a<br />
c1 = a − b c2 = 0<br />
<br />
c3 = φ(c1, c2)<br />
return c3<br />
<br />
Each node has a unique<br />
immediate dominator<br />
Thus, dominance induces a tree<br />
on the control flow graph<br />
Thus, dominance is also a<br />
partial order<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 11 / 47
Liveness and Dominance<br />
Lemma (Budimlić, PLDI ’02)<br />
Each label where a value v is live is dominated by Dv<br />
v = . . .<br />
· · · = v<br />
start<br />
ℓ : . . .<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 12 / 47
Liveness and Dominance<br />
Lemma (Budimlić, PLDI ’02)<br />
Each label where a value v is live is dominated by Dv<br />
v = . . .<br />
· · · = v<br />
start<br />
ℓ : . . .<br />
Proof by contradiction<br />
Assume ℓ is not dominated by Dv<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 12 / 47
Liveness and Dominance<br />
Lemma (Budimlić, PLDI ’02)<br />
Each label where a value v is live is dominated by Dv<br />
v = . . .<br />
· · · = v<br />
start<br />
ℓ : . . .<br />
Proof by contradiction<br />
Assume ℓ is not dominated by Dv<br />
Then there’s a path from start to<br />
some usage of v not containing the<br />
definition of v<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 12 / 47
Liveness and Dominance<br />
Lemma (Budimlić, PLDI ’02)<br />
Each label where a value v is live is dominated by Dv<br />
v = . . .<br />
· · · = v<br />
start<br />
ℓ : . . .<br />
Proof by contradiction<br />
Assume ℓ is not dominated by Dv<br />
Then there’s a path from start to<br />
some usage of v not containing the<br />
definition of v<br />
This cannot be since each value<br />
must have been defined be<strong>for</strong>e it is<br />
used<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 12 / 47
Intuition<br />
Consider a set of intervals in N:<br />
a<br />
b<br />
c<br />
0 1 2 3 4 5 6<br />
Each interval corresponds to a lifetime of a variable<br />
⇒ a node in the interference graph<br />
Iff two intervals overlap, we draw an edge between then nodes<br />
a<br />
b<br />
c<br />
Can we make a cycle by drawing an edge from a to e?<br />
d<br />
d<br />
e<br />
e<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 13 / 47
Intuition<br />
Consider a set of intervals in N:<br />
a<br />
b<br />
c<br />
0 1 2 3 4 5 6<br />
Each interval corresponds to a lifetime of a variable<br />
⇒ a node in the interference graph<br />
Iff two intervals overlap, we draw an edge between then nodes<br />
a<br />
b<br />
c<br />
Can we make a cycle by drawing an edge from a to e?<br />
Only by letting a “start again” at 5<br />
d<br />
d<br />
e<br />
e<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 13 / 47
Interference and Dominance I<br />
Assume v, w interfere, i.e. they are live at some label ℓ<br />
Then, Dv ℓ and Dw ℓ<br />
Since dominance is a tree, either Dv Dw or Dw Dv<br />
{, }<br />
v w<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 14 / 47
Interference and Dominance I<br />
Assume v, w interfere, i.e. they are live at some label ℓ<br />
Then, Dv ℓ and Dw ℓ<br />
Since dominance is a tree, either Dv Dw or Dw Dv<br />
Consequence<br />
{, }<br />
v w<br />
Each edge in the interference graph has a direction according to dominance<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 14 / 47
Interference and Dominance II<br />
Assume<br />
<br />
v w<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 15 / 47
Interference and Dominance II<br />
Assume<br />
<br />
v w<br />
Then, v is live at Dw<br />
v = . . .<br />
· · · = v, w<br />
start<br />
w = . . .<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 15 / 47
Interference and Dominance II<br />
Consider three nodes u, v, w in the IG:<br />
v<br />
u<br />
<br />
???<br />
<br />
w<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 16 / 47
Interference and Dominance II<br />
Consider three nodes u, v, w in the IG:<br />
v<br />
u is live at Dv<br />
w is live at Dv<br />
u<br />
<br />
???<br />
<br />
w<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 16 / 47
Interference and Dominance II<br />
Consider three nodes u, v, w in the IG:<br />
v<br />
u is live at Dv<br />
w is live at Dv<br />
Thus, they interfere<br />
Conclusion<br />
All values<br />
interfering with v<br />
whose definitions dominate the one of v<br />
are members of the same clique<br />
u<br />
<br />
<br />
w<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 16 / 47
Dominance and PEOs<br />
Be<strong>for</strong>e a value v can be added to a PEO, all values whose definitions<br />
are dominated by Dv must be added<br />
Thus, the post order of a dominance tree walk defines a PEO<br />
IGs of <strong>SSA</strong>-<strong>for</strong>m programs can be colored in O(|V | · |E|)<br />
Ideally without constructing the graph itself<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 17 / 47
Overview<br />
1 Preliminaries<br />
<strong>Register</strong> <strong>Allocation</strong><br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 18 / 47
Spilling<br />
Theorem<br />
For each clique in the IG there is a label in the program where all nodes in<br />
the clique are live.<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 19 / 47
Spilling<br />
Theorem<br />
For each clique in the IG there is a label in the program where all nodes in<br />
the clique are live.<br />
b c<br />
<br />
a<br />
<br />
<br />
<br />
<br />
Dominance induces a chain inside the clique<br />
⇒ There is a “greatest” value d<br />
All others are live at Dd<br />
d<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 19 / 47
Spilling<br />
Consequences<br />
The chromatic number of the IG is exactly determined by the size of<br />
the live sets of the labels<br />
Lowering the number of values live at each label to k makes the IG<br />
k-colorable<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 20 / 47
Spilling<br />
Chordal graphs are perfect<br />
Thus, ω(H) = χ(H) holds <strong>for</strong> each induced subgraph<br />
<strong>Register</strong> pressure is a precise measure <strong>for</strong> the number of registers<br />
needed<br />
We know in advance where values must be spilled<br />
⇒ All labels where the pressure is larger than k<br />
Spilling can be done be<strong>for</strong>e coloring and<br />
coloring will always succeed afterwards<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 21 / 47
Spilling<br />
Chordal graphs are perfect<br />
Thus, ω(H) = χ(H) holds <strong>for</strong> each induced subgraph<br />
<strong>Register</strong> pressure is a precise measure <strong>for</strong> the number of registers<br />
needed<br />
We know in advance where values must be spilled<br />
⇒ All labels where the pressure is larger than k<br />
Spilling can be done be<strong>for</strong>e coloring and<br />
coloring will always succeed afterwards<br />
Conclusion<br />
No iteration as in Chaitin/Briggs-allocators<br />
Interference graph has to be built only once (if at all)<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 21 / 47
Overview<br />
1 Preliminaries<br />
<strong>Register</strong> <strong>Allocation</strong><br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 22 / 47
Getting out of <strong>SSA</strong><br />
We now have a k-coloring of the <strong>SSA</strong> interference graph<br />
Can we turn it into a valid register allocation using k registers <strong>for</strong> the<br />
corresponding non-<strong>SSA</strong> program?<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 23 / 47
Getting out of <strong>SSA</strong><br />
We now have a k-coloring of the <strong>SSA</strong> interference graph<br />
Can we turn it into a valid register allocation using k registers <strong>for</strong> the<br />
corresponding non-<strong>SSA</strong> program?<br />
Central question<br />
How to handle φ-functions?<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 23 / 47
φ-Functions<br />
All φ-functions in a basic block<br />
y1 ← φ(x11, . . . , xn1)<br />
.<br />
ym ← φ(x1m, . . . , xnm)<br />
execute simultaneously be<strong>for</strong>e all other instructions in that block<br />
Arriving from the i-th edge, the φ-functions work as a bulk copy<br />
(y1, . . . , ym) ← (xi1, . . . , xim)<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 24 / 47
φ-Functions<br />
Consider following example<br />
a3 ← φ(a1, a2)<br />
b3 ← φ(b1, b2)<br />
c3 ← φ(c1, c2)<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 25 / 47
φ-Functions<br />
Consider following example<br />
a3 ← φ(a1, a2)<br />
b3 ← φ(b1, b2)<br />
c3 ← φ(c1, c2)<br />
The φs represent register permutations on the control flow edges<br />
On edge 1 On edge 2<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 25 / 47
Permutations<br />
A permutation can be implemented copies with one auxiliary register<br />
copy =<br />
copy =<br />
copy =<br />
copy =<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 26 / 47
Permutations<br />
A permutation can be implemented copies with one auxiliary register<br />
copy =<br />
copy =<br />
copy =<br />
copy =<br />
Permutations can be implemented by a series of transpositions<br />
(i.e. swaps)<br />
= ◦<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 26 / 47
Permutations<br />
A permutation can be implemented copies with one auxiliary register<br />
copy =<br />
copy =<br />
copy =<br />
copy =<br />
Permutations can be implemented by a series of transpositions<br />
(i.e. swaps)<br />
= ◦<br />
A transposition can be implemented by three xors without a third<br />
register<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 26 / 47
Permutations II<br />
We can replace φ-operations with code requiring no additional register<br />
A <strong>SSA</strong> register allocation can be turned into a non-<strong>SSA</strong> one without<br />
needing additional registers<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 27 / 47
Overview<br />
1 Preliminaries<br />
<strong>Register</strong> <strong>Allocation</strong><br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 28 / 47
Coalescing<br />
Minimize number of instructions to be inserted <strong>for</strong> all φs<br />
Current practice: Merging nodes in the IG to avoid copies<br />
Renders the graph unchordal<br />
Lose in<strong>for</strong>mation on the chromatic number<br />
When done aggressively, introduce spills in favor of (eliminated) copies<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 29 / 47
Coalescing<br />
Modeling the Problem<br />
Given: A minimal coloring of the IG<br />
c ← a + 1<br />
d ← b + c<br />
x<br />
y<br />
a ← 1<br />
b ← 2<br />
<br />
c g<br />
← Φ<br />
b e<br />
e ← 5<br />
f ← 2 ∗ b<br />
g ← a + e<br />
<br />
d<br />
c<br />
2<br />
b<br />
2<br />
y<br />
x<br />
a<br />
1<br />
1<br />
e<br />
f<br />
g<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 30 / 47
Coalescing<br />
Modeling the Problem<br />
Given: A minimal coloring of the IG<br />
Find a feasible coloring with minimal costs<br />
Costs are a weighted sum over all equal-color edges<br />
Unused colors may be used<br />
Structure of graph and program must not be changed<br />
c ← a + 1<br />
d ← b + c<br />
x<br />
y<br />
a ← 1<br />
b ← 2<br />
<br />
c g<br />
← Φ<br />
b e<br />
e ← 5<br />
f ← 2 ∗ b<br />
g ← a + e<br />
<br />
d<br />
c<br />
0<br />
b<br />
0<br />
y<br />
x<br />
a<br />
1<br />
0<br />
e<br />
f<br />
g<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 30 / 47
Coalescing<br />
<strong>Form</strong>al Statement<br />
Find a k-coloring C of the IG which assigns as many φ-operands and<br />
results the same color.<br />
where<br />
min<br />
C<br />
costs (C, y ← φ(x1, . . . , xn)) =<br />
<br />
costs(C, φ)<br />
φ<br />
n<br />
i=1<br />
<br />
0 if C(y) = C(xi)<br />
wyxi else<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 31 / 47
Coalescing<br />
Solution Strategies<br />
Complexity<br />
Problem is NP-complete in number of φs (TODO Rastello)<br />
Algorithms<br />
A greedy heuristic<br />
An optimal method using ILP (integer linear programming)<br />
Compare solution quality of heuristic vs. ILP<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 32 / 47
Coalescing<br />
Heuristic<br />
Idea: Swap colors to achieve more equal-colored pairs<br />
Problem: Not decidable locally if swap is possible<br />
There<strong>for</strong>e<br />
Consider each φ seperately<br />
Try to give φ-operands and result the same color<br />
Resolve color clashes recursively through the graph<br />
On failure mark the conflict locally and repeat<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 33 / 47
Coalescing<br />
Heuristic<br />
Example r = φ(a, b, c, d, e) with cutout of the IG<br />
a b c d e<br />
r<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 34 / 47
Coalescing<br />
Heuristic<br />
Example r = φ(a, b, c, d, e) with conflict graph<br />
a b c d<br />
Conflict graph represents (in-)compatibilities<br />
Initially it is a part of the IG<br />
Maximum stable set is a largest compatible subset of nodes<br />
r<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 34 / 47
Coalescing<br />
Heuristic<br />
Example r = φ(a, b, c, d, e) with conflict graph<br />
a b c d<br />
Conflict graph represents (in-)compatibilities<br />
Initially it is a part of the IG<br />
Maximum stable set is a largest compatible subset of nodes<br />
Additional edges <strong>for</strong> not resolvable global conflicts:<br />
<strong>Register</strong> constraints<br />
Conflict with prior optimizations of other φ-functions<br />
r<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 34 / 47
Coalescing<br />
Heuristic<br />
Example r = φ(a, b, c, d, e) with conflict graph<br />
a b c d<br />
Conflict graph represents (in-)compatibilities<br />
Initially it is a part of the IG<br />
Maximum stable set is a largest compatible subset of nodes<br />
Additional edges <strong>for</strong> not resolvable global conflicts:<br />
<strong>Register</strong> constraints<br />
Conflict with prior optimizations of other φ-functions<br />
Interaction with another node in same set<br />
r<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 34 / 47
Coalescing<br />
Optimal solution<br />
How good is the heuristic?<br />
Comparison with non-<strong>SSA</strong> allocators is complicated<br />
Circumstances are too different<br />
Compare to optimal solution<br />
How to obtain optimal solutions?<br />
Backtracking? complex, laborious<br />
Reduction to ILP!<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 35 / 47
Coalescing<br />
<strong>Form</strong>alization as ILP<br />
Binary variables represent states/decisions<br />
Coloring: xic = 1 ⇔ node i has color c<br />
Optimality: yij = 1 ⇔ node i and j have different colors<br />
min f = <br />
where<br />
we · yij<br />
e∈Q<br />
<br />
xic = 1 vi ∈ V<br />
c<br />
xic + xjc ≤ 1 [vi, vj] ∈ E<br />
yij ≥ xic − xjc [vi, vj] ∈ Q<br />
yij, xic ∈ {0, 1}<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 36 / 47
Coalescing<br />
ILP Example with 3 nodes and 3 colors<br />
1 2 3<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 37 / 47
Coalescing<br />
ILP Example with 3 nodes and 3 colors<br />
1 2 3<br />
min ???<br />
where x11 + x12 + x13 = 1<br />
x21 + x22 + x23 = 1 coloring<br />
x31 + x32 + x33 = 1<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 37 / 47
Coalescing<br />
ILP Example with 3 nodes and 3 colors<br />
1 2 3<br />
min ???<br />
where x11 + x12 + x13 = 1<br />
x21 + x22 + x23 = 1 coloring<br />
x31 + x32 + x33 = 1<br />
≤ 1<br />
x11 + x21<br />
x12 + x22 ≤ 1 interference<br />
x13 + x23<br />
≤ 1<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 37 / 47
Coalescing<br />
ILP Example with 3 nodes and 3 colors<br />
1 2 3<br />
min ???<br />
where x11 + x12 + x13 = 1<br />
x21 + x22 + x23 = 1 coloring<br />
x31 + x32 + x33 = 1<br />
≤ 1<br />
x11 + x21<br />
x12 + x22 ≤ 1 interference<br />
x13 + x23<br />
≤ 1<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 37 / 47
Coalescing<br />
ILP Example with 3 nodes and 3 colors<br />
w<br />
1 2 3<br />
min w · y23<br />
where x11 + x12 + x13 = 1<br />
x21 + x22 + x23 = 1 coloring<br />
x31 + x32 + x33 = 1<br />
≤ 1<br />
x11 + x21<br />
x12 + x22 ≤ 1 interference<br />
x13 + x23<br />
≤ 1<br />
y23<br />
≥ x21 − x31<br />
y23 ≥ x22 − x32 equal-coloring<br />
y23<br />
≥ x23 − x33<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 37 / 47
Coalescing<br />
ILP Example with 3 nodes and 3 colors<br />
w<br />
1 2 3<br />
min w · y23<br />
where x11 + x12 + x13 = 1<br />
x21 + x22 + x23 = 1 coloring<br />
x31 + x32 + x33 = 1<br />
≤ 1<br />
x11 + x21<br />
x12 + x22 ≤ 1 interference<br />
x13 + x23<br />
≤ 1<br />
y23<br />
≥ x21 − x31<br />
y23 ≥ x22 − x32 equal-coloring<br />
y23<br />
≥ x23 − x33<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 37 / 47
Coalescing<br />
Improving the ILP Runtime<br />
Clique inequalities<br />
Replace O(n 2 ) inequalities xic + xjc ≤ 1<br />
with one:<br />
n<br />
xic ≤ 1<br />
i=1<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 38 / 47
Coalescing<br />
Improving the ILP Runtime<br />
Clique inequalities<br />
Path inequalities<br />
a b c<br />
d<br />
e<br />
Use incompatibility of interference- and<br />
equal-color-edges<br />
yad + ycd ≥ 1<br />
yad + yde + yec ≥ 1<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 38 / 47
Coalescing<br />
Improving the ILP Runtime<br />
Clique inequalities<br />
Path inequalities<br />
Clique-Path inequalities<br />
d<br />
a b c<br />
Multiple use of same argument,<br />
⎛<br />
here ⎝ a<br />
⎞ ⎛ ⎞<br />
d e<br />
b ⎠ = Φ ⎝ d f ⎠<br />
c<br />
d g<br />
results in yad + ybd + ycd ≥ 2<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 38 / 47
Quality of the Coalescing Heuristic<br />
Applied to SPEC 2000<br />
Costs<br />
1000<br />
400<br />
300<br />
200<br />
100<br />
0<br />
8 16 32<br />
<strong>Register</strong>s<br />
Initial<br />
Heuristic<br />
ILP<br />
<strong>Register</strong>s 8 16 32<br />
Non-Opt 6.7% 3.7% 1.3%<br />
Initial 394592 342842 213544<br />
Heuristic 60114 63506 46060<br />
ILP 42738 57010 43479<br />
Elim 95.0% 97.7% 98.4%<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 39 / 47
Runtime of the Heuristic with 8 (16 / 32) <strong>Register</strong>s<br />
ms<br />
1000<br />
100<br />
10<br />
1<br />
0 250 500 750 1000 1250 1500 1750 2000 2250 2500<br />
99% of all Problems in less than 68 (196 / 170) ms.<br />
on average 4 (14 / 21) ms.<br />
|Q|<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 40 / 47
Coalescing - Influence of <strong>Register</strong>s<br />
Costs<br />
1000<br />
350<br />
300<br />
250<br />
200<br />
150<br />
100<br />
50<br />
0<br />
4 6 8 10 12 14 16 24 32 40 48 56 64 128256<br />
ILP Heur Initial #<strong>Register</strong>s<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 41 / 47
Overview<br />
1 Preliminaries<br />
<strong>Register</strong> <strong>Allocation</strong><br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 42 / 47
Conclusions<br />
<strong>SSA</strong> vs. non-<strong>SSA</strong><br />
<strong>SSA</strong>-Construction introduces copies<br />
(φ-operations are copies along edges)<br />
These copies “blow up” the interference graph<br />
Nodes are replaced by stable sets<br />
b<br />
c<br />
a<br />
d<br />
e<br />
⇒<br />
Breaks cycles in the interference graph<br />
The interference graph becomes chordal<br />
b<br />
c<br />
a<br />
d<br />
e1<br />
e2<br />
e3<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 43 / 47
Conclusions<br />
Non-<strong>SSA</strong> vs. <strong>SSA</strong><br />
<strong>SSA</strong>-Destruction coalesces copies aggressively<br />
without considering the number of available registers<br />
Stable sets merged into nodes<br />
b<br />
c<br />
a<br />
d<br />
Possibly creating cycles<br />
e1<br />
e2<br />
e3<br />
Possibly increasing the chromatic number of the graph<br />
⇒<br />
b<br />
c<br />
a<br />
d<br />
e<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 44 / 47
Conclusions<br />
Chordality of <strong>SSA</strong> IGs allows <strong>for</strong> decoupling spilling and coalescing<br />
<strong>Register</strong> pressure is a precise measure <strong>for</strong> the number of registers<br />
needed<br />
We know in advance where values must be spilled<br />
⇒ All labels where the pressure is larger than k<br />
Spilling can be done be<strong>for</strong>e coloring and<br />
coloring will always succeed afterwards<br />
Coalescing re-expressed by introducing a cost function on colorings<br />
and finding a preferably good coloring<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 45 / 47
Conclusions<br />
Chordality of <strong>SSA</strong> IGs allows <strong>for</strong> decoupling spilling and coalescing<br />
<strong>Register</strong> pressure is a precise measure <strong>for</strong> the number of registers<br />
needed<br />
We know in advance where values must be spilled<br />
⇒ All labels where the pressure is larger than k<br />
Spilling can be done be<strong>for</strong>e coloring and<br />
coloring will always succeed afterwards<br />
Coalescing re-expressed by introducing a cost function on colorings<br />
and finding a preferably good coloring<br />
Architecture without iteration<br />
Spill Color Coalesce <strong>SSA</strong>-Destruction<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 45 / 47
Thank you very much!<br />
Universität<br />
Karlsruhe<br />
Sebastian Hack (Universität Karlsruhe) <strong>SSA</strong> <strong>Register</strong> <strong>Allocation</strong> 31.01.2006 46 / 47