Register allocation fixes.

This fixes the calculator button problem, but let's leave optimization
off until we've passed more tests.

Change-Id: I79b71687a1651e83f16037dead768c43f55d65da
diff --git a/src/compiler/codegen/RallocUtil.cc b/src/compiler/codegen/RallocUtil.cc
index 1b0fb90..3fe3b3a 100644
--- a/src/compiler/codegen/RallocUtil.cc
+++ b/src/compiler/codegen/RallocUtil.cc
@@ -367,13 +367,11 @@
 {
     RegisterInfo* p = cUnit->regPool->FPRegs;
     int numRegs = cUnit->regPool->numFPRegs;
-    int next = cUnit->regPool->nextFPReg;
-    int i;
+    /* Start looking at an even reg */
+    int next = cUnit->regPool->nextFPReg & ~0x1;
 
-    for (i=0; i < numRegs; i+=2) {
-        /* Cleanup - not all targets need aligned regs */
-        if (next & 1)
-            next++;
+    // First try to avoid allocating live registers
+    for (int i=0; i < numRegs; i+=2) {
         if (next >= numRegs)
             next = 0;
         if ((p[next].isTemp && !p[next].inUse && !p[next].live) &&
@@ -384,13 +382,18 @@
             p[next+1].inUse = true;
             DCHECK_EQ((p[next].reg+1), p[next+1].reg);
             DCHECK_EQ((p[next].reg & 0x1), 0);
-            cUnit->regPool->nextFPReg += 2;
+            cUnit->regPool->nextFPReg = next + 2;
+            if (cUnit->regPool->nextFPReg >= numRegs) {
+                cUnit->regPool->nextFPReg = 0;
+            }
             return p[next].reg;
         }
         next += 2;
     }
-    next = cUnit->regPool->nextFPReg;
-    for (i=0; i < numRegs; i+=2) {
+    next = cUnit->regPool->nextFPReg & ~0x1;
+
+    // No choice - find a pair and kill it.
+    for (int i=0; i < numRegs; i+=2) {
         if (next >= numRegs)
             next = 0;
         if (p[next].isTemp && !p[next].inUse && p[next+1].isTemp &&
@@ -401,7 +404,10 @@
             p[next+1].inUse = true;
             DCHECK_EQ((p[next].reg+1), p[next+1].reg);
             DCHECK_EQ((p[next].reg & 0x1), 0);
-            cUnit->regPool->nextFPReg += 2;
+            cUnit->regPool->nextFPReg = next + 2;
+            if (cUnit->regPool->nextFPReg >= numRegs) {
+                cUnit->regPool->nextFPReg = 0;
+            }
             return p[next].reg;
         }
         next += 2;