new testcase for PR1286
[oota-llvm.git] / lib / CodeGen / VirtRegMap.cpp
index dfa5e4383d009533a4c220f25947b8ab9a45b13b..3fb84bdd8f74c278c436c60edd13f241a8f47335 100644 (file)
@@ -87,6 +87,9 @@ void VirtRegMap::assignVirt2StackSlot(unsigned virtReg, int frameIndex) {
   assert(MRegisterInfo::isVirtualRegister(virtReg));
   assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
          "attempt to assign stack slot to already spilled register");
+  assert((frameIndex >= 0 ||
+          (frameIndex >= MF.getFrameInfo()->getObjectIndexBegin())) &&
+         "illegal fixed frame index");
   Virt2StackSlotMap[virtReg] = frameIndex;
 }
 
@@ -94,8 +97,14 @@ int VirtRegMap::assignVirtReMatId(unsigned virtReg) {
   assert(MRegisterInfo::isVirtualRegister(virtReg));
   assert(Virt2StackSlotMap[virtReg] == NO_STACK_SLOT &&
          "attempt to assign re-mat id to already spilled register");
+  const MachineInstr *DefMI = getReMaterializedMI(virtReg);
+  int FrameIdx;
+  if (TII.isLoadFromStackSlot((MachineInstr*)DefMI, FrameIdx)) {
+    // Load from stack slot is re-materialize as reload from the stack slot!
+    Virt2StackSlotMap[virtReg] = FrameIdx;
+    return FrameIdx;
+  }
   Virt2StackSlotMap[virtReg] = ReMatId;
-  ++NumReMats;
   return ReMatId++;
 }
 
@@ -316,7 +325,9 @@ public:
       assert(II != SpillSlotsAvailable.end() && "Slot not available!");
       unsigned Val = II->second.first;
       assert((Val >> 1) == PhysReg && "Bidirectional map mismatch!");
-      II->second.second.push_back(Use);
+      // This can be true if there are multiple uses of the same register.
+      if (II->second.second.back() != Use)
+        II->second.second.push_back(Use);
     }
   }
   
@@ -623,7 +634,6 @@ namespace {
 /// register allocator is done with them.  If possible, avoid reloading vregs.
 void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
                               std::vector<MachineInstr*> &ReMatedMIs) {
-
   DOUT << MBB.getBasicBlock()->getName() << ":\n";
 
   // Spills - Keep track of which spilled values are available in physregs so
@@ -654,7 +664,9 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
     const TargetInstrDescriptor *TID = MI.getInstrDescriptor();
 
     // If this instruction is being rematerialized, just remove it!
-    if (TID->Flags & M_REMATERIALIZIBLE) {
+    int FrameIdx;
+    if ((TID->Flags & M_REMATERIALIZIBLE) ||
+        TII->isLoadFromStackSlot(&MI, FrameIdx)) {
       bool Remove = true;
       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
         MachineOperand &MO = MI.getOperand(i);
@@ -754,10 +766,11 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
           // necessary.
           bool WasKill = false;
           if (SSMI) {
-            MachineOperand *MOK = SSMI->findRegisterUseOperand(PhysReg, true);
-            if (MOK) {
-              WasKill = MOK->isKill();
-              MOK->unsetIsKill();
+            int UIdx = SSMI->findRegisterUseOperand(PhysReg, true);
+            if (UIdx != -1) {
+              MachineOperand &MOK = SSMI->getOperand(UIdx);
+              WasKill = MOK.isKill();
+              MOK.unsetIsKill();
             }
           }
           if (ti == -1) {
@@ -840,17 +853,20 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
         // necessary.
         bool WasKill = false;
         if (SSMI) {
-          MachineOperand *MOK = SSMI->findRegisterUseOperand(PhysReg, true);
-          if (MOK) {
-            WasKill = MOK->isKill();
-            MOK->unsetIsKill();
+          int UIdx = SSMI->findRegisterUseOperand(PhysReg, true);
+          if (UIdx != -1) {
+            MachineOperand &MOK = SSMI->getOperand(UIdx);
+            WasKill = MOK.isKill();
+            MOK.unsetIsKill();
           }
         }
         MachineInstr *CopyMI = prior(MII);
         if (WasKill) {
           // Transfer kill to the next use.
-          MachineOperand *MOU = CopyMI->findRegisterUseOperand(PhysReg);
-          MOU->setIsKill();
+          int UIdx = CopyMI->findRegisterUseOperand(PhysReg);
+          assert(UIdx != -1);
+          MachineOperand &MOU = CopyMI->getOperand(UIdx);
+          MOU.setIsKill();
         }
         Spills.addLastUse(PhysReg, CopyMI);
 
@@ -880,10 +896,13 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
       
       PhysRegsUsed[PhysReg] = true;
       ReusedOperands.markClobbered(PhysReg);
-      if (doReMat)
+      if (doReMat) {
         MRI->reMaterialize(MBB, &MI, PhysReg, VRM.getReMaterializedMI(VirtReg));
-      else
+        ++NumReMats;
+      } else {
         MRI->loadRegFromStackSlot(MBB, &MI, PhysReg, StackSlot, RC);
+        ++NumLoads;
+      }
       // This invalidates PhysReg.
       Spills.ClobberPhysReg(PhysReg);
 
@@ -895,7 +914,6 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
       // unless it's a two-address operand.
       if (TID->getOperandConstraint(i, TOI::TIED_TO) == -1)
         MI.getOperand(i).setIsKill();
-      ++NumLoads;
       MI.getOperand(i).setReg(PhysReg);
       DOUT << '\t' << *prior(MII);
     }
@@ -945,18 +963,26 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
               // extended. Remove its kill.
               bool WasKill = false;
               if (SSMI) {
-                MachineOperand *MOK = SSMI->findRegisterUseOperand(InReg, true);
-                if (MOK) {
-                  WasKill = MOK->isKill();
-                  MOK->unsetIsKill();
+                int UIdx = SSMI->findRegisterUseOperand(InReg, true);
+                if (UIdx != -1) {
+                  MachineOperand &MOK = SSMI->getOperand(UIdx);
+                  WasKill = MOK.isKill();
+                  MOK.unsetIsKill();
                 }
               }
               if (NextMII != MBB.end()) {
-                // If NextMII uses InReg (must be the copy?), mark it killed.
-                MachineOperand *MOU = NextMII->findRegisterUseOperand(InReg);
-                if (MOU) {
-                  if (WasKill)
-                    MOU->setIsKill();
+                // If NextMII uses InReg and the use is not a two address
+                // operand, mark it killed.
+                int UIdx = NextMII->findRegisterUseOperand(InReg);
+                if (UIdx != -1) {
+                  MachineOperand &MOU = NextMII->getOperand(UIdx);
+                  if (WasKill) {
+                    const TargetInstrDescriptor *NTID =
+                      NextMII->getInstrDescriptor();
+                    if (UIdx >= NTID->numOperands ||
+                        NTID->getOperandConstraint(UIdx, TOI::TIED_TO) == -1)
+                      MOU.setIsKill();
+                  }
                   Spills.addLastUse(InReg, &(*NextMII));
                 }
               }
@@ -1105,6 +1131,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
           if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
             ++NumDCE;
             DOUT << "Removing now-noop copy: " << MI;
+            Spills.removeLastUse(Src, &MI);
             MBB.erase(&MI);
             VRM.RemoveFromFoldedVirtMap(&MI);
             goto ProcessNextInst;