Fix bugs handling ESP in alloca references
[oota-llvm.git] / lib / CodeGen / RegAllocLocal.cpp
index 57f3c836e5513a182c4d5f92c97d3233c0f8edbe..1b84b30f57f0970c5c02f017f663f69850d2f607 100644 (file)
@@ -232,30 +232,26 @@ void RA::removePhysReg(unsigned PhysReg) {
 ///
 void RA::spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
                       unsigned VirtReg, unsigned PhysReg) {
-
-  DEBUG(std::cerr << "  Spilling register " << RegInfo->getName(PhysReg));
-  if (VirtReg == 0) {
-    DEBUG(std::cerr << " which corresponds to no vreg, "
-                    << "must be spurious physreg: ignoring (WARNING)\n");
-  } else {
-    // FIXME: move this into the conditional??
+  if (!VirtReg && DisableKill) return;
+  assert(VirtReg && "Spilling a physical register is illegal!"
+         " Must not have appropriate kill for the register or use exists beyond"
+         " the intended one.");
+  DEBUG(std::cerr << "  Spilling register " << RegInfo->getName(PhysReg);
+        std::cerr << " containing %reg" << VirtReg;
+        if (!isVirtRegModified(VirtReg))
+        std::cerr << " which has not been modified, so no store necessary!");
+
+  // Otherwise, there is a virtual register corresponding to this physical
+  // register.  We only need to spill it into its stack slot if it has been
+  // modified.
+  if (isVirtRegModified(VirtReg)) {
     const TargetRegisterClass *RC = MF->getSSARegMap()->getRegClass(VirtReg);
     int FrameIndex = getStackSpaceFor(VirtReg, RC);
-
-    DEBUG(std::cerr << " containing %reg" << VirtReg;
-          if (!isVirtRegModified(VirtReg))
-           std::cerr << " which has not been modified, so no store necessary!");
-
-    // Otherwise, there is a virtual register corresponding to this physical
-    // register.  We only need to spill it into its stack slot if it has been
-    // modified.
-    if (isVirtRegModified(VirtReg)) {
-      DEBUG(std::cerr << " to stack slot #" << FrameIndex);
-      RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
-      ++NumSpilled;   // Update statistics
-    }
-    Virt2PhysRegMap.erase(VirtReg);   // VirtReg no longer available
+    DEBUG(std::cerr << " to stack slot #" << FrameIndex);
+    RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIndex, RC);
+    ++NumSpilled;   // Update statistics
   }
+  Virt2PhysRegMap.erase(VirtReg);   // VirtReg no longer available
 
   DEBUG(std::cerr << "\n");
   removePhysReg(PhysReg);
@@ -512,12 +508,7 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
         if (PhysReg) {
           DEBUG(std::cerr << "  Last use of " << RegInfo->getName(PhysReg)
                       << "[%reg" << VirtReg <<"], removing it from live set\n");
-          // If the physical register was used, but there was no definition of
-          // the physical register (we are reading garbage), Live Variables will
-          // tell us that this is the last use of the register even though we
-          // don't know of anything in the register.  No need to remove it.
-          if (VirtReg != PhysReg || PhysRegsUsed.count(PhysReg))
-            removePhysReg(PhysReg);
+          removePhysReg(PhysReg);
         }
       }
     }
@@ -616,8 +607,10 @@ void RA::AllocateBasicBlock(MachineBasicBlock &MBB) {
 
   // Spill all physical registers holding virtual registers now.
   while (!PhysRegsUsed.empty())
-    spillVirtReg(MBB, I, PhysRegsUsed.begin()->second,
-                 PhysRegsUsed.begin()->first);
+    if (unsigned VirtReg = PhysRegsUsed.begin()->second)
+      spillVirtReg(MBB, I, VirtReg, PhysRegsUsed.begin()->first);
+    else
+      removePhysReg(PhysRegsUsed.begin()->first);
 
   for (std::map<unsigned, unsigned>::iterator I = Virt2PhysRegMap.begin(),
          E = Virt2PhysRegMap.end(); I != E; ++I)