# Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles.

## Presentation on theme: "Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles."— Presentation transcript:

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. 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? The core Register Allocation Problem. The core Register Allocation Problem. - SSA form - Polynomial mapping ??? Polynomial Time NP-complete Polynomial Iff P = NP 12

SSA-Elimination 1) int m(int p 1,int p 2 ){ 2) int v 1 = p 1 ; 3) int i = p 2 ; 4) while(i < 10){ 5) i = i+1; 6) if(v 1 > 11) 7) break; 8) v 1 = i+2; 9) } 10) return v 1 ; 11) } int m(int p 1, int p 2 ) { int v 11 = p 1 ; int i 1 = p 2 ; int v 1 = v 11 ; int i = i 1 ; while (i < 10) { int i 2 = i + 1; if (v 1 > 11) break; int v 12 = i 2 + 2; v 1 = v 12; i = i 2 ; } return v 1 ; } Target program SSA-form Program Post-SSA Program

Target program SSA-form Code We’ve shown that the core register allocation problem is NP-complete. The Main Conclusion NP-complete register allocation Polynomial register allocation Hack et al CC’06 Classical SSA elimination

What about Chaitin’s Proof? G G’ C(G) = C(G’) - 1

Chaitin’s Proof does not Work for SSA-form Programs

Three Registers are Enough.

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.

Arcs and Loops 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) } d 1 = … ; c 1 = … ; a = c; b = d; c 2 = a+1; d 2 = b+1; dcdc d 1,d 2 c 1,c 2 = c b c d b c2c2 d2d2 a a c2c2 d1d1 d1d1 d d2d2 c1c1 c1c1 Non-SSA-formSSA-form

Value Related Live Ranges 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) } b c2c2 b c a a c=c 2 d2d2 d d=d 2 d 1 = … ; c 1 = … ; a = c; b = d; c 2 = a+1; d 2 = b+1; dcdc d 1,d 2 c 1,c 2 = c b c d b c2c2 d2d2 a a c2c2 d d2d2 Post-SSA program Post-SSA graph

Color Mapping b c2c2 b c a a c=c 2 d2d2 d d=d 2 We want to show that coloring circular-arc graphs, and post-SSA graphs is equally hard.

Can G be colored with K colors? b c b c a a c=c 2 d2d2 d d=d 2 nK-n n = variables crossing the loop; K = number of colors

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 int a = C 1 + i; If(a > C 2 ) break; Post-SSA-graph  function with loop Arc  Live range of variable. How to avoid compiler optimizations?

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; }

The Example in SSA-form a 1 = … ; e 1 = … ; t 1 = … ; i 1 = … ; i 2 = i + 1; if(t > 9) break; if(e > 10) break; b = i 2 + 11; if(a > 11) break; c = i 2 + 12; if(b > 12) break; d = i 2 + 13; if(c > 13) break; e 2 = i 2 + 14; if(d > 14) break; a 2 = i 2 + 15; t 2 = i 2 + 16; actiacti a 1,a 2 e 1,e 2 t 1,t 2 i 1,i 2 =

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; > 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 Circular-arc graph Post-SSA graph Simple Post-SSA Program Register Assignment Register To colors Arcs to arcs 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; > i = i2; a = a2; t = t2; e = e2; } return a; }

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.

Download ppt "Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles."

Similar presentations