register allocation after classical ssa elimination is np-complete fernando m q pereira jens...
TRANSCRIPT
![Page 1: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/1.jpg)
Register Allocationafter Classical SSA
Elimination is NP-complete
Fernando M Q Pereira
Jens Palsberg
- UCLA -
The University of California, Los Angeles
![Page 2: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/2.jpg)
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?
![Page 3: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/3.jpg)
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
![Page 4: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/4.jpg)
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?
![Page 5: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/5.jpg)
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
![Page 6: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/6.jpg)
Polynomial Time RA?
The coreRegister
AllocationProblem.
The coreRegister
AllocationProblem.
- SSA form -
Polynomial mapping
???
PolynomialTime
NP-complete PolynomialIff P = NP
1 2
![Page 7: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/7.jpg)
SSA-Elimination
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) }
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; }
Target program
SSA-form ProgramPost-SSA Program
![Page 8: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/8.jpg)
Targetprogram
SSA-form
Code Code
We’ve shown thatthe core register
allocation problemis NP-complete.
The Main Conclusion
NP-complete register allocation
Polynomialregister
allocation
Hack et alCC’06
ClassicalSSA
elimination
![Page 9: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/9.jpg)
What about Chaitin’s Proof?
G G’
C(G) = C(G’) - 1
![Page 10: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/10.jpg)
Chaitin’s Proof does not Work for SSA-form Programs
![Page 11: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/11.jpg)
Three Registers are Enough.
![Page 12: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/12.jpg)
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.
![Page 13: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/13.jpg)
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) }
d1 = …;c1 = …;
a = c;b = d;c2 = a+1;d2 = b+1;
dc
d1,d2
c1,c2=
c
b
c
d
b
c2
d2
a
ac2
d1
d1
d
d2
c1
c1
Non-SSA-form SSA-form
![Page 14: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/14.jpg)
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
c2
b
ca
a
c=c2
d2
d
d=d2
d1 = …;c1 = …;
a = c;b = d;c2 = a+1;d2 = b+1;
dc
d1,d2
c1,c2=
c
b
c
d
b
c2
d2
a
ac2
d
d2
Post-SSAprogram
Post-SSA graph
![Page 15: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/15.jpg)
Color Mapping
b
c2
b
ca
a
c=c2
d2
d
d=d2
We want to show that coloringcircular-arc graphs, andpost-SSA graphs is equally hard.
![Page 16: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/16.jpg)
Can G be colored with K colors?
b
c
b
ca
a
c=c2
d2
d
d=d2
n K-n
n = variables crossing the loop; K = number of colors
![Page 17: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/17.jpg)
Example of Post-SSA-Graphn = 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.
![Page 18: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/18.jpg)
Mapping Graphs to Programs
int a = C1 + i;
If(a > C2) break;
• Post-SSA-graph function with loop
• Arc Live range of variable.
• How to avoid compiler optimizations?
![Page 19: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/19.jpg)
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;}
![Page 20: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/20.jpg)
K = 3, n = 2 one auxiliary tint 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;}
![Page 21: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/21.jpg)
The Example in SSA-forma1 = …;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;
acti
a1,a2
e1,e2
t1,t2
i1,i2
=
![Page 22: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/22.jpg)
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
![Page 23: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/23.jpg)
Summary of the Reduction
Circular-arcgraph
Post-SSAgraph
SimplePost-SSAProgram
RegisterAssignment
RegisterTo 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; << main loop >> i = i2; a = a2; t = t2; e = e2; } return a;}
![Page 24: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/24.jpg)
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.
![Page 25: Register Allocation after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles](https://reader030.vdocuments.net/reader030/viewer/2022032701/56649c755503460f94929935/html5/thumbnails/25.jpg)
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.