shithub: mc

Download patch

ref: 7b0c6c77845e01963e6eabd94bbc91c68eed6bc3
parent: b474f4b5e52fdabfec503d8666aefe35bdbdb7f1
author: Ori Bernstein <[email protected]>
date: Wed Jan 30 09:03:19 EST 2013

Work on moving to callee save convention.

    This breaks things badly; We have a register allocator bug.

--- a/6/isel.c
+++ b/6/isel.c
@@ -781,7 +781,7 @@
     Loc *rsp;
     Loc *rbp;
     Loc *stksz;
-    //size_t i;
+    size_t i;
 
     rsp = locphysreg(Rrsp);
     rbp = locphysreg(Rrbp);
@@ -790,13 +790,11 @@
     g(s, Ipush, rbp, NULL);
     g(s, Imov, rsp, rbp, NULL);
     g(s, Isub, stksz, rsp, NULL);
-#if 0
     /* save registers */
     for (i = 0; i < sizeof(savedregs)/sizeof(savedregs[0]); i++) {
         s->calleesave[i] = locreg(ModeQ);
         g(s, Imov, locphysreg(savedregs[i]), s->calleesave[i], NULL);
     }
-#endif
 
     s->stksz = stksz; /* need to update if we spill */
 }
@@ -805,7 +803,7 @@
 {
     Loc *rsp, *rbp;
     Loc *ret;
-    //size_t i;
+    size_t i;
 
     rsp = locphysreg(Rrsp);
     rbp = locphysreg(Rrbp);
@@ -813,12 +811,10 @@
         ret = loc(s, s->ret);
         g(s, Imov, ret, coreg(Rax, ret->mode), NULL);
     }
-#if 0
     /* restore registers */
     for (i = 0; i < Nsaved; i++)
         g(s, Imov, s->calleesave[i], locphysreg(savedregs[i]), NULL);
     /* leave function */
-#endif
     g(s, Imov, rbp, rsp, NULL);
     g(s, Ipop, rbp, NULL);
     g(s, Iret, NULL);
--- a/6/ra.c
+++ b/6/ra.c
@@ -18,6 +18,7 @@
     int r[Maxarg + 1]; /* list of registers used implicitly by instruction */
 };
 
+void wlprint(FILE *fd, char *name, Loc **wl, size_t nwl);
 static void printedge(FILE *fd, char *msg, size_t a, size_t b);
 
 /* tables of uses/defs by instruction */
@@ -329,6 +330,16 @@
     s->degree = zalloc(maxregid * sizeof(int));
     s->rmoves = zalloc(maxregid * sizeof(Loc **));
     s->nrmoves = zalloc(maxregid * sizeof(size_t));
+
+    /* Initialize the degrees of prepainted registers */
+    bsput(s->prepainted, 0);
+    s->degree[0] = 1<<16;
+    for (i = 0; i < maxregid; i++) {
+        if (locmap[i]->reg.colour) {
+            bsput(s->prepainted, i);
+            s->degree[i] = 1<<16;
+        }
+    }
 }
 
 static void build(Isel *s)
@@ -393,7 +404,6 @@
                 goto next;
         if (bshas(s->coalesced, r))
             goto next;
-        assert(r < maxregid);
         *m = r;
         return 1;
 next:
@@ -515,9 +525,12 @@
         return;
     if (s->degree[u] >= K)
         return;
-    for (i = 0; i < s->nwlfreeze; i++)
-        if (s->wlfreeze[i]->reg.id == u)
+    for (i = 0; i < s->nwlfreeze; i++) {
+        if (s->wlfreeze[i]->reg.id == u) {
             ldel(&s->wlfreeze, &s->nwlfreeze, i);
+            break;
+        }
+    }
     lappend(&s->wlsimp, &s->nwlsimp, locmap[u]);
 }
 
@@ -562,7 +575,7 @@
         return 1;
 
     /* if it is, are the adjacent nodes ok to combine with this? */
-    for (t = 0; adjiter(s, u, &t); t++)
+    for (t = 0; adjiter(s, v, &t); t++)
         if (!ok(s, t, u))
             return 0;
     return 1;
@@ -617,6 +630,9 @@
         addedge(s, t, u);
         decdegree(s, t);
     }
+    printf("Degree ");
+    locprint(stdout, locmap[u], 'x');
+    printf(" = %d\n", s->degree[u]);
     if (s->degree[u] >= K && wlhas(s->wlfreeze, s->nwlfreeze, u, &idx)) {
         ldel(&s->wlfreeze, &s->nwlfreeze, idx);
         lappend(&s->wlspill, &s->nwlspill, locmap[u]);
@@ -718,7 +734,6 @@
     /* FIXME: pick a better heuristic for spilling */
     m = NULL;
     for (i = 0; i < s->nwlspill; i++) {
-        printf("Trying to spill %zd\n", s->wlspill[i]->reg.id);
         if (!bshas(s->shouldspill, s->wlspill[i]->reg.id))
             continue;
         m = s->wlspill[i];
@@ -990,19 +1005,14 @@
 
 void regalloc(Isel *s)
 {
-    size_t i;
     int spilled;
+    size_t i;
 
     s->prepainted = mkbs();
     s->shouldspill = mkbs();
     s->neverspill = mkbs();
-#if 0
     for (i = 0; i < Nsaved; i++)
         bsput(s->shouldspill, s->calleesave[i]->reg.id);
-#endif
-    for (i = 0; i < maxregid; i++)
-        if (locmap[i]->reg.colour)
-            bsput(s->prepainted, i);
     do {
         setup(s);
         liveness(s);
@@ -1029,6 +1039,21 @@
     bsfree(s->neverspill);
 }
 
+void wlprint(FILE *fd, char *name, Loc **wl, size_t nwl)
+{
+    size_t i;
+    char *sep;
+
+    sep = "";
+    fprintf(fd, "%s = [", name);
+    for (i = 0; i < nwl; i++) {
+        fprintf(fd, "%s", sep);
+        locprint(fd, wl[i], 'x');
+        sep = ",";
+    }
+    fprintf(fd, "]\n");
+}
+
 static void setprint(FILE *fd, Bitset *s)
 {
     char *sep;
@@ -1087,11 +1112,18 @@
     char *sep;
     Asmbb *bb;
 
-    fprintf(fd, "IGRAPH ----- \n");
-    for (i = 0; i < maxregid; i++) {
-        for (j = i; j < maxregid; j++) {
-            if (gbhasedge(s, i, j))
-                printedge(stdout, "", i, j);
+    fprintf(fd, "WORKLISTS -- \n");
+    wlprint(stdout, "spill", s->wlspill, s->nwlspill);
+    wlprint(stdout, "simp", s->wlsimp, s->nwlsimp);
+    wlprint(stdout, "freeze", s->wlfreeze, s->nwlfreeze);
+    /* noisy to dump this all the time; only dump for higher debug levels */
+    if (debugopt['r'] > 2) {
+        fprintf(fd, "IGRAPH ----- \n");
+        for (i = 0; i < maxregid; i++) {
+            for (j = i; j < maxregid; j++) {
+                if (gbhasedge(s, i, j))
+                    printedge(stdout, "", i, j);
+            }
         }
     }
     fprintf(fd, "ASM -------- \n");