Add TargetInstrInfo::isSafeToMoveRegisterClassDefs. It returns true if it's safe...
[oota-llvm.git] / lib / CodeGen / VirtRegMap.cpp
index f03db748973e18c6dfe6914069534f63dc257ad2..9cb580b9f0ab7385edef9c9226305a70f45c8cd5 100644 (file)
@@ -56,8 +56,8 @@ static cl::opt<SpillerName>
 SpillerOpt("spiller",
            cl::desc("Spiller to use: (default: local)"),
            cl::Prefix,
-           cl::values(clEnumVal(simple, "  simple spiller"),
-                      clEnumVal(local,  "  local spiller"),
+           cl::values(clEnumVal(simple, "simple spiller"),
+                      clEnumVal(local,  "local spiller"),
                       clEnumValEnd),
            cl::init(local));
 
@@ -143,7 +143,7 @@ int VirtRegMap::getEmergencySpillSlot(const TargetRegisterClass *RC) {
     LowSpillSlot = SS;
   if (HighSpillSlot == NO_STACK_SLOT || SS > HighSpillSlot)
     HighSpillSlot = SS;
-  I->second = SS;
+  EmergencySpillSlots[RC] = SS;
   return SS;
 }
 
@@ -635,7 +635,8 @@ static bool InvalidateRegDef(MachineBasicBlock::iterator I,
 /// marked kill, then it must be due to register reuse. Transfer the kill info
 /// over.
 static void UpdateKills(MachineInstr &MI, BitVector &RegKills,
-                        std::vector<MachineOperand*> &KillOps) {
+                        std::vector<MachineOperand*> &KillOps,
+                        const TargetRegisterInfo* TRI) {
   const TargetInstrDesc &TID = MI.getDesc();
   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
     MachineOperand &MO = MI.getOperand(i);
@@ -669,6 +670,11 @@ static void UpdateKills(MachineInstr &MI, BitVector &RegKills,
     unsigned Reg = MO.getReg();
     RegKills.reset(Reg);
     KillOps[Reg] = NULL;
+    // It also defines (or partially define) aliases.
+    for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
+      RegKills.reset(*AS);
+      KillOps[*AS] = NULL;
+    }
   }
 }
 
@@ -839,7 +845,7 @@ namespace {
             
             Spills.addAvailable(NewOp.StackSlotOrReMat, MI, NewPhysReg);
             --MII;
-            UpdateKills(*MII, RegKills, KillOps);
+            UpdateKills(*MII, RegKills, KillOps, TRI);
             DOUT << '\t' << *MII;
             
             DOUT << "Reuse undone!\n";
@@ -1283,7 +1289,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
         }
         // This invalidates Phys.
         Spills.ClobberPhysReg(Phys);
-        UpdateKills(*prior(MII), RegKills, KillOps);
+        UpdateKills(*prior(MII), RegKills, KillOps, TRI);
         DOUT << '\t' << *prior(MII);
       }
     }
@@ -1336,6 +1342,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
     }
 
     // Process all of the spilled uses and all non spilled reg references.
+    SmallVector<int, 2> PotentialDeadStoreSlots;
     for (unsigned j = 0, e = VirtUseOps.size(); j != e; ++j) {
       unsigned i = VirtUseOps[j];
       MachineOperand &MO = MI.getOperand(i);
@@ -1353,7 +1360,8 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
         unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys;
         MI.getOperand(i).setReg(RReg);
         if (VRM.isImplicitlyDefined(VirtReg))
-          BuildMI(MBB, &MI, TII->get(TargetInstrInfo::IMPLICIT_DEF), RReg);
+          BuildMI(MBB, &MI, MI.getDebugLoc(),
+                  TII->get(TargetInstrInfo::IMPLICIT_DEF), RReg);
         continue;
       }
       
@@ -1440,17 +1448,14 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
 
           if (MI.getOperand(i).isKill() &&
               ReuseSlot <= VirtRegMap::MAX_STACK_SLOT) {
-            // This was the last use and the spilled value is still available
-            // for reuse. That means the spill was unnecessary!
-            MachineInstr* DeadStore = MaybeDeadStores[ReuseSlot];
-            if (DeadStore) {
-              DOUT << "Removed dead store:\t" << *DeadStore;
-              InvalidateKills(*DeadStore, RegKills, KillOps);
-              VRM.RemoveMachineInstrFromMaps(DeadStore);
-              MBB.erase(DeadStore);
-              MaybeDeadStores[ReuseSlot] = NULL;
-              ++NumDSE;
-            }
+
+            // The store of this spilled value is potentially dead, but we
+            // won't know for certain until we've confirmed that the re-use
+            // above is valid, which means waiting until the other operands
+            // are processed. For now we just track the spill slot, we'll
+            // remove it after the other operands are processed if valid.
+
+            PotentialDeadStoreSlots.push_back(ReuseSlot);
           }
           continue;
         }  // CanReuse
@@ -1500,7 +1505,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
         TII->copyRegToReg(MBB, &MI, DesignatedReg, PhysReg, RC, RC);
 
         MachineInstr *CopyMI = prior(MII);
-        UpdateKills(*CopyMI, RegKills, KillOps);
+        UpdateKills(*CopyMI, RegKills, KillOps, TRI);
 
         // This invalidates DesignatedReg.
         Spills.ClobberPhysReg(DesignatedReg);
@@ -1550,10 +1555,27 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
         MI.getOperand(i).setIsKill();
       unsigned RReg = SubIdx ? TRI->getSubReg(PhysReg, SubIdx) : PhysReg;
       MI.getOperand(i).setReg(RReg);
-      UpdateKills(*prior(MII), RegKills, KillOps);
+      UpdateKills(*prior(MII), RegKills, KillOps, TRI);
       DOUT << '\t' << *prior(MII);
     }
 
+    // Ok - now we can remove stores that have been confirmed dead.
+    for (unsigned j = 0, e = PotentialDeadStoreSlots.size(); j != e; ++j) {
+      // This was the last use and the spilled value is still available
+      // for reuse. That means the spill was unnecessary!
+      int PDSSlot = PotentialDeadStoreSlots[j];
+      MachineInstr* DeadStore = MaybeDeadStores[PDSSlot];
+      if (DeadStore) {
+        DOUT << "Removed dead store:\t" << *DeadStore;
+        InvalidateKills(*DeadStore, RegKills, KillOps);
+        VRM.RemoveMachineInstrFromMaps(DeadStore);
+        MBB.erase(DeadStore);
+        MaybeDeadStores[PDSSlot] = NULL;
+        ++NumDSE;
+      }
+    }
+
+
     DOUT << '\t' << MI;
 
 
@@ -1650,7 +1672,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
             // super-register is needed below.
             if (KillOpnd && !KillOpnd->getSubReg() &&
                 TII->unfoldMemoryOperand(MF, &MI, PhysReg, false, true,NewMIs)){
-              MBB.insert(MII, NewMIs[0]);
+             MBB.insert(MII, NewMIs[0]);
               NewStore = NewMIs[1];
               MBB.insert(MII, NewStore);
               VRM.addSpillSlotUse(SS, NewStore);
@@ -1732,15 +1754,17 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
       if (!TargetRegisterInfo::isVirtualRegister(VirtReg)) {
         // Check to see if this is a noop copy.  If so, eliminate the
         // instruction before considering the dest reg to be changed.
-        unsigned Src, Dst;
-        if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
+        unsigned Src, Dst, SrcSR, DstSR;
+        if (TII->isMoveInstr(MI, Src, Dst, SrcSR, DstSR) && Src == Dst) {
           ++NumDCE;
           DOUT << "Removing now-noop copy: " << MI;
           SmallVector<unsigned, 2> KillRegs;
           InvalidateKills(MI, RegKills, KillOps, &KillRegs);
           if (MO.isDead() && !KillRegs.empty()) {
-            // Source register or an implicit super-register use is killed.
-            assert(KillRegs[0] == Dst || TRI->isSubRegister(KillRegs[0], Dst));
+            // Source register or an implicit super/sub-register use is killed.
+            assert(KillRegs[0] == Dst ||
+                   TRI->isSubRegister(KillRegs[0], Dst) ||
+                   TRI->isSuperRegister(KillRegs[0], Dst));
             // Last def is now dead.
             TransferDeadness(&MBB, Dist, Src, RegKills, KillOps);
           }
@@ -1817,15 +1841,15 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
         // Check to see if this is a noop copy.  If so, eliminate the
         // instruction before considering the dest reg to be changed.
         {
-          unsigned Src, Dst;
-          if (TII->isMoveInstr(MI, Src, Dst) && Src == Dst) {
+          unsigned Src, Dst, SrcSR, DstSR;
+          if (TII->isMoveInstr(MI, Src, Dst, SrcSR, DstSR) && Src == Dst) {
             ++NumDCE;
             DOUT << "Removing now-noop copy: " << MI;
             InvalidateKills(MI, RegKills, KillOps);
             VRM.RemoveMachineInstrFromMaps(&MI);
             MBB.erase(&MI);
             Erased = true;
-            UpdateKills(*LastStore, RegKills, KillOps);
+            UpdateKills(*LastStore, RegKills, KillOps, TRI);
             goto ProcessNextInst;
           }
         }
@@ -1835,7 +1859,7 @@ void LocalSpiller::RewriteMBB(MachineBasicBlock &MBB, VirtRegMap &VRM) {
     DistanceMap.insert(std::make_pair(&MI, Dist++));
     if (!Erased && !BackTracked) {
       for (MachineBasicBlock::iterator II = &MI; II != NextMII; ++II)
-        UpdateKills(*II, RegKills, KillOps);
+        UpdateKills(*II, RegKills, KillOps, TRI);
     }
     MII = NextMII;
   }