250 likes | 386 Vues
Register Allocation after Classical SSA Elimination is NP-complete. Fernando M Q Pereira Jens Palsberg. - UCLA - The University of California, Los Angeles. The Core Register Allocation Problem. Instance : a program P and a number K of available registers.
E N D
Register Allocationafter Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles
The 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?
Chaitin’s 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
Register 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?
Polynomial Time RA • Sebastian Hack, Daniel Grund, and Gerhard Goos, Register Allocation for Programs in SSA-form, University of Karlsruhe, Germany, CC, 2006 • CC, 16:30 - 18:00, Friday, Session 4
Polynomial Time RA? 1 2 Polynomial mapping The core Register Allocation Problem. The core Register Allocation Problem. - SSA form - ??? Polynomial Iff P = NP NP-complete Polynomial Time
SSA-Elimination int m(int p1, int p2) { int v11 = p1; int i1 = p2; int v1 = v11; int i = i1; while (i < 10) { int i2 = i + 1; if (v1 > 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 < 10){ 5) i = i+1; 6) if(v1 > 11) 7) break; 8) v1 = i+2; 9) } 10) return v1; 11) } Target program Post-SSA Program SSA-form Program
The Main Conclusion Target program NP-complete register allocation SSA-form Polynomial register allocation Classical SSA elimination We’ve shown that the core register allocation problem is NP-complete. Hack et al CC’06 Code Code
What about Chaitin’s Proof? G G’ C(G) = C(G’) - 1
Circular-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.
d c d1,d2 c1,c2 = Arcs 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 = a+1; 7) d = b+1; 8) } c1 d1 = …; c1 = …; d1 c1 d1 c d c a b d a = c; b = d; c2 = a+1; d2 = b+1; c2 a d2 b c2 d2
d c d1,d2 c1,c2 = Value 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 = a+1; 9) d2 = b+1; 10) d = d2; 11) c = c2; 12) } d d b c a b c2 a d d2 c2 a b d2 a = c; b = d; c2 = a+1; d2 = b+1; b c=c2 c2 d2 d=d2 Post-SSA graph
Color 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. c=c2 d=d2
Can G be colored with K colors? n = variables crossing the loop; K = number of colors a c d b c a d2 b c=c2 d=d2 n K-n
Example 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.
Mapping 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 > C2) break;
A Simple Example int m(int a,int e,int i){ while(i < 100) { i = i + 1; if(e > 10) break; int b = i + 11; if (a > 11) break; int c = i + 12; if(b > 12) break; int d = i + 13; if(c > 13) break; e = i + 14; if(d > 14) break; a = i + 15; } return a; }
K = 3, n = 2 one auxiliary t int m(int a,int e,int t,int i){ while(i < 100) { i = i + 1; if(t > 9) break; if(e > 10) break; int b = i + 11; if (a > 11) break; int c = i + 12; if(b > 12) break; int d = i + 13; if(c > 13) break; e = i + 14; if(d > 14) break; a = i + 15; t = i + 16; } return a; }
a c t i a1,a2 e1,e2 t1,t2 i1,i2 = The Example in SSA-form a1 = …; e1 = …; t1 = …; i1 = …; i2 = i + 1; if(t > 9) break; if(e > 10) break; b = i2 + 11; if(a > 11) break; c = i2 + 12; if(b > 12) break; d = i2 + 13; if(c > 13) break; e2 = i2 + 14; if(d > 14) break; a2 = i2 + 15; t2 = i2 + 16;
After 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 > 10) { int i2 = i + 1; << main loop >> i = i2; a = a2; t = t2; e = e2; } return a; } if(t > 9) break; if(e > 10) break; int b = i2 + 11; if (a > 11) break; int c = i2 + 12; if(b > 12) break; int d = i2 + 13; if(c > 13) break; int e2 = i2 + 14; if(d > 14) break; int a2 = i2 + 15; int t2 = i2 + 16; K + 1 register assignment K coloring
Summary 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 < 100) { int i2 = i + 1; << main loop >> i = i2; a = a2; t = t2; e = e2; } return a; } Simple Post-SSA Program Circular-arc graph Post-SSA graph Register To colors Register Assignment Arcs to arcs
Other 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.
Conclusions • 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.