by Tatsuyuki Ishi
Liveness
0
1
2
3
Free registers
r0,r1,r2
r1,r2
r2
r1
r0
r1
r2
r1
Liveness
0
1
2
3
Free registers
r0,r1
r1
r0
r1
Liveness
0
1
2
3
Evicted
r1
r1
r0
r1
Evict variable 0:
Variable 0 is now spilled - better!
v1 = ...
v2 = ...
v3 = add v1, v2
v4 = add v2, v3
...
one definition for each variable
Potentially multiple uses
SSA Graph colorable in quadratic time!
(num_variables × num_regs)
How: Just do the Linear Scan
But: RA is NP-complete
There is no contradiction:
the optimality only applies to basic blocks
Hack, Sebastian. “Register allocation for programs in SSA form.” International Conference on Compiler Construction (2006).
v1 = add ..., ...
if A
v2 = add ..., ...
v3 = add v2, v2
v4 = if A then v1 else v3
r1
r1
r2
Problem: v1 and v3 does not match
→ need to insert moves!
(To fix this, we need heuristics approaches to do lookahead / propagate constraints from future instructions)
Liveness
0
1
2
3
r0
Colombet, Quentin et al. “Graph-coloring and treescan register allocation using repairing.” 2011 Proceedings of the 14th International Conference on Compilers, Architectures and Synthesis for Embedded Systems (CASES) (2011): 45-54.
Liveness
0
1
2
3
r0
r0
r1
Colombet, Quentin et al. “Graph-coloring and treescan register allocation using repairing.” 2011 Proceedings of the 14th International Conference on Compilers, Architectures and Synthesis for Embedded Systems (CASES) (2011): 45-54.
Liveness
0
1
2
3
r0
r0
r1
r2
Colombet, Quentin et al. “Graph-coloring and treescan register allocation using repairing.” 2011 Proceedings of the 14th International Conference on Compilers, Architectures and Synthesis for Embedded Systems (CASES) (2011): 45-54.
SSA graph chromacity
=
max-clique
=
max-live
(Also true for chordal graphs!)
We know the number of variables
that need to be spilled.
MIN algorithm: Spill largest next-use distance
z = ...
while x > 0:
y = ...
x = y - ...
w = z + ...
Braun, Matthias and Sebastian Hack. “Register Spilling and Live-Range Splitting for SSA-Form Programs.” CC (2009).
// Spill x, y or z?
Spill z.
x and y are used in loop and have larger next-use distance than z
Why SSA RA?
Why not SSA RA?