Title: Register Allocation after Classical SSA Elimination is NP-complete
1Register Allocationafter Classical SSA
Elimination is NP-complete
- Fernando M Q Pereira
- Jens Palsberg
- UCLA - The University of California, Los Angeles
2The Core Register Allocation Problem
- Instance a program P and a number K of available
registers. - Problem can each of the temporaries of P be
mapped to one of the K registers such that
temporary variables with interfering live ranges
are assigned to different registers?
3Chaitins Proof
- CHAITIN, G., AUSLANDER, M., CHANDRA, A., COCKE,
J., HOPKINS, M., AND MARKSTEIN, P.
Register allocation via coloring. Computer
Languages 6, 1, 47--57, 1981. - Graph Coloring ? Register Allocation
4Register Allocation in SSA-form
- Instance a program P, in SSA form, and a number
K of available registers. - Problem can each of the temporaries of P be
mapped to one of the K registers such that
temporary variables with interfering live ranges
are assigned to different registers?
5Polynomial Time RA
- Sebastian Hack, Daniel Grund, and Gerhard Goos,
Register Allocation for Programs in SSA-form,
University of Karlsruhe, Germany, CC, 2006 - CC, 1630 - 1800, Friday, Session 4
6Polynomial Time RA?
1
2
Polynomial mapping
The core Register Allocation Problem.
The core Register Allocation Problem. - SSA form -
???
Polynomial Iff P NP
Polynomial Time
NP-complete
7SSA-Elimination
int m(int p1, int p2) int v11 p1 int
i1 p2 int v1 v11 int i i1
while (i lt 10) int i2 i 1
if (v1 gt 11) break int v12
i2 2 v1 v12 i i2
return v1
1) int m(int p1,int p2) 2) int v1 p1 3)
int i p2 4) while(i lt 10) 5) i
i1 6) if(v1 gt 11) 7)
break 8) v1 i2 9) 10)
return v1 11)
Target program
Post-SSA Program
SSA-form Program
8The Main Conclusion
Target program
NP-complete register allocation
SSA-form
Polynomial register allocation
Classical SSA elimination
Weve shown that the core register allocation
problem is NP-complete.
Hack et al CC06
Code
Code
9What about Chaitins Proof?
G
G
C(G) C(G) - 1
10Chaitins Proof does not Work for SSA-form
Programs
11Three Registers are Enough.
12Circular-Arc Graphs
- Theorem to find a minimal coloring for a
circular-arc graph is NP-complete. - Garey, Johnson and Stockmeyer. Some simplified
NP-complete problems, ACM Symposium on Theory of
Computing, 47-63, 1974.
13Arcs and Loops
Non-SSA-form
SSA-form
1) int d 2) int c 3) while ( ) 4)
int a c 5) int b d 6) c a1 7)
d b1 8)
c1
d1 c1
d1
c1
d1
c
d
c
a
b
d
a c b d c2 a1 d2 b1
c2
a
d2
b
c2
d2
14Value Related Live Ranges
d1 c1
Post-SSA program
a
c
c
1) int d1 2) int c1 3) d d1 4) c
c1 5) while ( ) 6) int a c 7)
int b d 8) c2 a1 9) d2 b1 10)
d d2 11) c c2 12)
d
d
b
c
a
c2
b
a
d
d2
c2
a
b
d2
a c b d c2 a1 d2 b1
b
cc2
c2
dd2
d2
Post-SSA graph
15Color Mapping
a
c
d
b
c2
a
d2
b
We want to show that coloring circular-arc
graphs, and post-SSA graphs is equally hard.
cc2
dd2
16Can G be colored with K colors?
n variables crossing the loop K number of
colors
a
c
d
b
c
a
d2
b
cc2
dd2
n
K-n
17Example of Post-SSA-Graph
n 2 K 3
- Circular-arc graph has K coloring iff
post-SSA-graph has K coloring. - It is possible to map post-SSA-graphs to post-SSA
programs.
18Mapping Graphs to Programs
- Post-SSA-graph ? function with loop
- Arc ? Live range of variable.
- How to avoid compiler optimizations?
int a C1 i
If(a gt C2) break
19A Simple Example
int m(int a,int e,int i) while(i lt 100)
i i 1 if(e gt 10) break int b i
11 if (a gt 11) break int c i 12
if(b gt 12) break int d i 13 if(c gt
13) break e i 14 if(d gt 14) break
a i 15 return a
20K 3, n 2 ? one auxiliary t
int m(int a,int e,int t,int i) while(i lt 100)
i i 1 if(t gt 9) break if(e gt
10) break int b i 11 if (a gt 11)
break int c i 12 if(b gt 12) break
int d i 13 if(c gt 13) break e
i 14 if(d gt 14) break a i 15
t i 16 return a
21The Example in SSA-form
a1 e1 t1 i1
i2 i 1 if(t gt 9) break if(e gt 10) break b
i2 11 if(a gt 11) break c i2 12 if(b gt
12) break d i2 13 if(c gt 13) break e2 i2
14 if(d gt 14) break a2 i2 15 t2 i2
16
22After SSA-elimination
int m(int a1, int e1, int t1, int i1) int a
a1 int e e1 int t t1 int i i1
while(i gt 10) int i2 i 1 ltlt main
loop gtgt i i2 a a2 t t2 e
e2 return a
if(t gt 9) break if(e gt 10) break int b i2
11 if (a gt 11) break int c i2 12 if(b gt
12) break int d i2 13 if(c gt 13) break int
e2 i2 14 if(d gt 14) break int a2 i2
15 int t2 i2 16
K 1 register assignment ? K coloring
23Summary of the Reduction
int m(int a1,int e1, int t1,int i1) int a
a1 int e e1 int t t1 int i i1
while(i lt 100) int i2 i 1 ltlt main
loop gtgt i i2 a a2 t t2 e
e2 return a
Simple Post-SSA Program
Circular-arc graph
Post-SSA graph
Register Assignment
Register To colors
Arcs to arcs
24Other NP-completeness Proofs
- Ravi Sethi, Complete register allocation
problems, 73. - NP-complete for strait line code with
rescheduling. - Farach and Liberatore, On Local Register
Allocation, 98. - which register to spill is NP-complete.
- Bodlaender and Gustedt, Linear Register
Allocation for a Fixed Number of Registers, 98. - SSA-form programs have chordal interference
graphs three different proofs Hack et al, Brisk
et al, Bouchez, 05. - Polynomial register allocation Hack 06.
25Conclusions
- Register allocation is NP-complete if classical
algorithms are used to eliminate ?-functions. - Proof is independent on the order in which copy
instructions are inserted. - Post-SSA programs can be built with a simple
if-then-else statement.