Correct free-ing of temp register.

Bug 11199874.
The card mark was potentially using a register freed just before. Make the
free-ing of temps strongly correspond to their allocation.

Change-Id: I3d1e8c923b7fd8b3666e841d3ff9a46e6eb58318
diff --git a/compiler/dex/quick/arm/int_arm.cc b/compiler/dex/quick/arm/int_arm.cc
index b1772fd..75d3738 100644
--- a/compiler/dex/quick/arm/int_arm.cc
+++ b/compiler/dex/quick/arm/int_arm.cc
@@ -884,12 +884,14 @@
   }
 
   int reg_ptr;
+  bool allocated_reg_ptr_temp = false;
   if (constant_index) {
     reg_ptr = rl_array.low_reg;
   } else if (IsTemp(rl_array.low_reg)) {
     Clobber(rl_array.low_reg);
     reg_ptr = rl_array.low_reg;
   } else {
+    allocated_reg_ptr_temp = true;
     reg_ptr = AllocTemp();
   }
 
@@ -940,7 +942,7 @@
     StoreBaseIndexed(reg_ptr, rl_index.low_reg, rl_src.low_reg,
                      scale, size);
   }
-  if (!constant_index) {
+  if (allocated_reg_ptr_temp) {
     FreeTemp(reg_ptr);
   }
   if (card_mark) {
diff --git a/compiler/dex/quick/mips/int_mips.cc b/compiler/dex/quick/mips/int_mips.cc
index 218ed48..7fefd33 100644
--- a/compiler/dex/quick/mips/int_mips.cc
+++ b/compiler/dex/quick/mips/int_mips.cc
@@ -498,12 +498,14 @@
   rl_array = LoadValue(rl_array, kCoreReg);
   rl_index = LoadValue(rl_index, kCoreReg);
   int reg_ptr = INVALID_REG;
+  bool allocated_reg_ptr_temp = false;
   if (IsTemp(rl_array.low_reg)) {
     Clobber(rl_array.low_reg);
     reg_ptr = rl_array.low_reg;
   } else {
     reg_ptr = AllocTemp();
     OpRegCopy(reg_ptr, rl_array.low_reg);
+    allocated_reg_ptr_temp = true;
   }
 
   /* null object? */
@@ -538,8 +540,6 @@
     }
 
     StoreBaseDispWide(reg_ptr, 0, rl_src.low_reg, rl_src.high_reg);
-
-    FreeTemp(reg_ptr);
   } else {
     rl_src = LoadValue(rl_src, reg_class);
     if (needs_range_check) {
@@ -549,6 +549,9 @@
     StoreBaseIndexed(reg_ptr, rl_index.low_reg, rl_src.low_reg,
                      scale, size);
   }
+  if (allocated_reg_ptr_temp) {
+    FreeTemp(reg_ptr);
+  }
   if (card_mark) {
     MarkGCCard(rl_src.low_reg, rl_array.low_reg);
   }
diff --git a/compiler/dex/quick/x86/int_x86.cc b/compiler/dex/quick/x86/int_x86.cc
index 14f5348..a9f2c59 100644
--- a/compiler/dex/quick/x86/int_x86.cc
+++ b/compiler/dex/quick/x86/int_x86.cc
@@ -503,7 +503,8 @@
                          rl_src.high_reg, size, INVALID_SREG);
   }
   if (card_mark) {
-    FreeTemp(rl_index.low_reg);  // Ensure there are 2 free regs for card mark.
+    // Free rl_index if its a temp. Ensures there are 2 free regs for card mark.
+    FreeTemp(rl_index.low_reg);
     MarkGCCard(rl_src.low_reg, rl_array.low_reg);
   }
 }