Fix PR number.
[oota-llvm.git] / lib / CodeGen / VirtRegMap.cpp
index 6fcbcf24fcb67e2ff71b6b7c609cce1488f9ef60..9ae38ac9013904845d2b0877e55fc6d1871561a9 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++;
 }
 
@@ -165,7 +174,6 @@ bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) {
   DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
   const TargetMachine &TM = MF.getTarget();
   const MRegisterInfo &MRI = *TM.getRegisterInfo();
-  bool *PhysRegsUsed = MF.getUsedPhysregs();
 
   // LoadedRegs - Keep track of which vregs are loaded, so that we only load
   // each vreg once (in the case where a spilled vreg is used by multiple
@@ -205,10 +213,10 @@ bool SimpleSpiller::runOnMachineFunction(MachineFunction &MF, VirtRegMap &VRM) {
                 ++NumStores;
               }
             }
-            PhysRegsUsed[PhysReg] = true;
+            MF.setPhysRegUsed(PhysReg);
             MI.getOperand(i).setReg(PhysReg);
           } else {
-            PhysRegsUsed[MO.getReg()] = true;
+            MF.setPhysRegUsed(MO.getReg());
           }
       }
 
@@ -316,7 +324,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 +633,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
@@ -638,8 +647,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
   // same stack slot, the original store is deleted.
   std::map<int, MachineInstr*> MaybeDeadStores;
 
-  bool *PhysRegsUsed = MBB.getParent()->getUsedPhysregs();
-
+  MachineFunction &MF = *MBB.getParent();
   for (MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
        MII != E; ) {
     MachineInstr &MI = *MII;
@@ -654,7 +662,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);
@@ -676,7 +686,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
     const unsigned *ImpDef = TID->ImplicitDefs;
     if (ImpDef) {
       for ( ; *ImpDef; ++ImpDef) {
-        PhysRegsUsed[*ImpDef] = true;
+        MF.setPhysRegUsed(*ImpDef);
         ReusedOperands.markClobbered(*ImpDef);
         Spills.ClobberPhysReg(*ImpDef);
       }
@@ -691,7 +701,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
       if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
         // Ignore physregs for spilling, but remember that it is used by this
         // function.
-        PhysRegsUsed[MO.getReg()] = true;
+        MF.setPhysRegUsed(MO.getReg());
         ReusedOperands.markClobbered(MO.getReg());
         continue;
       }
@@ -703,7 +713,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
       if (!VRM.hasStackSlot(VirtReg)) {
         // This virtual register was assigned a physreg!
         unsigned Phys = VRM.getPhys(VirtReg);
-        PhysRegsUsed[Phys] = true;
+        MF.setPhysRegUsed(Phys);
         if (MO.isDef())
           ReusedOperands.markClobbered(Phys);
         MI.getOperand(i).setReg(Phys);
@@ -754,7 +764,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
           // necessary.
           bool WasKill = false;
           if (SSMI) {
-            int UIdx = SSMI->findRegisterUseOperand(PhysReg, true);
+            int UIdx = SSMI->findRegisterUseOperandIdx(PhysReg, true);
             if (UIdx != -1) {
               MachineOperand &MOK = SSMI->getOperand(UIdx);
               WasKill = MOK.isKill();
@@ -830,10 +840,8 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
           continue;
         }
         
-        const TargetRegisterClass* RC =
-          MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
-
-        PhysRegsUsed[DesignatedReg] = true;
+        const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(VirtReg);
+        MF.setPhysRegUsed(DesignatedReg);
         ReusedOperands.markClobbered(DesignatedReg);
         MRI->copyRegToReg(MBB, &MI, DesignatedReg, PhysReg, RC);
 
@@ -841,7 +849,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
         // necessary.
         bool WasKill = false;
         if (SSMI) {
-          int UIdx = SSMI->findRegisterUseOperand(PhysReg, true);
+          int UIdx = SSMI->findRegisterUseOperandIdx(PhysReg, true);
           if (UIdx != -1) {
             MachineOperand &MOK = SSMI->getOperand(UIdx);
             WasKill = MOK.isKill();
@@ -851,7 +859,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
         MachineInstr *CopyMI = prior(MII);
         if (WasKill) {
           // Transfer kill to the next use.
-          int UIdx = CopyMI->findRegisterUseOperand(PhysReg);
+          int UIdx = CopyMI->findRegisterUseOperandIdx(PhysReg);
           assert(UIdx != -1);
           MachineOperand &MOU = CopyMI->getOperand(UIdx);
           MOU.setIsKill();
@@ -871,8 +879,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
       // Otherwise, reload it and remember that we have it.
       PhysReg = VRM.getPhys(VirtReg);
       assert(PhysReg && "Must map virtreg to physreg!");
-      const TargetRegisterClass* RC =
-        MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
+      const TargetRegisterClass* RC = MF.getSSARegMap()->getRegClass(VirtReg);
 
       // Note that, if we reused a register for a previous operand, the
       // register we want to reload into might not actually be
@@ -882,12 +889,15 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
         PhysReg = ReusedOperands.GetRegForReload(PhysReg, &MI, 
                                                  Spills, MaybeDeadStores);
       
-      PhysRegsUsed[PhysReg] = true;
+      MF.setPhysRegUsed(PhysReg);
       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);
 
@@ -899,7 +909,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);
     }
@@ -933,7 +942,6 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
             MachineInstr *SSMI = NULL;
             if (unsigned InReg = Spills.getSpillSlotPhysReg(SS, SSMI)) {
               DOUT << "Promoted Load To Copy: " << MI;
-              MachineFunction &MF = *MBB.getParent();
               if (DestReg != InReg) {
                 MRI->copyRegToReg(MBB, &MI, DestReg, InReg,
                                   MF.getSSARegMap()->getRegClass(VirtReg));
@@ -949,7 +957,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
               // extended. Remove its kill.
               bool WasKill = false;
               if (SSMI) {
-                int UIdx = SSMI->findRegisterUseOperand(InReg, true);
+                int UIdx = SSMI->findRegisterUseOperandIdx(InReg, true);
                 if (UIdx != -1) {
                   MachineOperand &MOK = SSMI->getOperand(UIdx);
                   WasKill = MOK.isKill();
@@ -959,13 +967,14 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
               if (NextMII != MBB.end()) {
                 // If NextMII uses InReg and the use is not a two address
                 // operand, mark it killed.
-                int UIdx = NextMII->findRegisterUseOperand(InReg);
+                int UIdx = NextMII->findRegisterUseOperandIdx(InReg);
                 if (UIdx != -1) {
                   MachineOperand &MOU = NextMII->getOperand(UIdx);
                   if (WasKill) {
                     const TargetInstrDescriptor *NTID =
                       NextMII->getInstrDescriptor();
-                    if (NTID->getOperandConstraint(UIdx, TOI::TIED_TO) == -1)
+                    if (UIdx >= NTID->numOperands ||
+                        NTID->getOperandConstraint(UIdx, TOI::TIED_TO) == -1)
                       MOU.setIsKill();
                   }
                   Spills.addLastUse(InReg, &(*NextMII));
@@ -1066,8 +1075,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
 
         // The only vregs left are stack slot definitions.
         int StackSlot = VRM.getStackSlot(VirtReg);
-        const TargetRegisterClass *RC =
-          MBB.getParent()->getSSARegMap()->getRegClass(VirtReg);
+        const TargetRegisterClass *RC = MF.getSSARegMap()->getRegClass(VirtReg);
 
         // If this def is part of a two-address operand, make sure to execute
         // the store from the correct physical register.
@@ -1085,7 +1093,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM,
           }
         }
 
-        PhysRegsUsed[PhysReg] = true;
+        MF.setPhysRegUsed(PhysReg);
         ReusedOperands.markClobbered(PhysReg);
         MRI->storeRegToStackSlot(MBB, next(MII), PhysReg, StackSlot, RC);
         DOUT << "Store:\t" << *next(MII);
@@ -1116,6 +1124,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;