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;
diff --git a/src/compiler/codegen/arm/ArmRallocUtil.cc b/src/compiler/codegen/arm/ArmRallocUtil.cc
index 4af3d07..81c721d 100644
--- a/src/compiler/codegen/arm/ArmRallocUtil.cc
+++ b/src/compiler/codegen/arm/ArmRallocUtil.cc
@@ -169,6 +169,10 @@
         // Promote fpRegs
         for (int i = 0; (fpRegs[i].count > 0) && (i < numRegs); i++) {
             if (cUnit->promotionMap[fpRegs[i].sReg].fpLocation != kLocPhysReg) {
+                if (fpRegs[i].sReg >= cUnit->numRegs) {
+                    // don't promote arg regs
+                    continue;
+                }
                 int reg = oatAllocPreservedFPReg(cUnit, fpRegs[i].sReg,
                     fpRegs[i].doubleStart);
                 if (reg < 0) {
@@ -181,6 +185,10 @@
         for (int i = 0; (coreRegs[i].count > 0) && i < numRegs; i++) {
             if (cUnit->promotionMap[coreRegs[i].sReg].coreLocation !=
                     kLocPhysReg) {
+                if (coreRegs[i].sReg >= cUnit->numRegs) {
+                    // don't promote arg regs
+                    continue;
+                }
                 int reg = oatAllocPreservedCoreReg(cUnit, coreRegs[i].sReg);
                 if (reg < 0) {
                    break;  // No more left