- Check if a register is livein before removing it. It may have already been removed.
authorEvan Cheng <evan.cheng@apple.com>
Thu, 24 Apr 2008 09:06:33 +0000 (09:06 +0000)
committerEvan Cheng <evan.cheng@apple.com>
Thu, 24 Apr 2008 09:06:33 +0000 (09:06 +0000)
- Do not iterate over SmallPtrSet, the order of iteration is not deterministic.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50209 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/MachineBasicBlock.h
lib/CodeGen/MachineBasicBlock.cpp
lib/CodeGen/SimpleRegisterCoalescing.cpp

index 66de06dcafde7aa5a505e72bb99496b449c33dc2..4ce695c1f3bb0b1fdbb4953bd4564c2d89575e44 100644 (file)
@@ -176,6 +176,10 @@ public:
   ///
   void removeLiveIn(unsigned Reg);
 
+  /// isLiveIn - Return true if the specified register is in the live in set.
+  ///
+  bool isLiveIn(unsigned Reg) const;
+
   // Iteration support for live in sets.  These sets are kept in sorted
   // order by their register number.
   typedef std::vector<unsigned>::iterator       livein_iterator;
index af91a2fb15c471b499892c3970543f17c82785ff..58ca6efcfe20953d03df8e7398f3b0fb6f21675d 100644 (file)
@@ -205,6 +205,11 @@ void MachineBasicBlock::removeLiveIn(unsigned Reg) {
   LiveIns.erase(I);
 }
 
+bool MachineBasicBlock::isLiveIn(unsigned Reg) const {
+  const_livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
+  return I != livein_end();
+}
+
 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
   MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
   getParent()->getBasicBlockList().splice(NewAfter, BBList, this);
index 6ad8bf54017537230ddbbfcbb1864d7d620bd9cf..3ea35c6eab2e3294a8769bb051571654abef5fcc 100644 (file)
@@ -639,7 +639,8 @@ SimpleRegisterCoalescing::ShortenDeadCopySrcLiveRange(LiveInterval &li,
     // first instruction index starts at > 0 value.
     assert(TargetRegisterInfo::isPhysicalRegister(li.reg));
     // Live-in to the function but dead. Remove it from entry live-in set.
-    mf_->begin()->removeLiveIn(li.reg);
+    if (mf_->begin()->isLiveIn(li.reg))
+      mf_->begin()->removeLiveIn(li.reg);
     const LiveRange *LR = li.getLiveRangeContaining(CopyIdx);
     removeRange(li, LR->start, LR->end, li_, tri_);
     return removeIntervalIfEmpty(li, li_, tri_);
@@ -2002,27 +2003,6 @@ bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction &fn) {
       I->second.print(DOUT, tri_);
       DOUT << "\n";
     }
-
-    // Delete all coalesced copies.
-    for (SmallPtrSet<MachineInstr*,32>::iterator I = JoinedCopies.begin(),
-           E = JoinedCopies.end(); I != E; ++I) {
-      MachineInstr *CopyMI = *I;
-      unsigned SrcReg, DstReg;
-      if (!tii_->isMoveInstr(*CopyMI, SrcReg, DstReg)) {
-        assert((CopyMI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
-                CopyMI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) &&
-               "Unrecognized copy instruction");
-        DstReg = CopyMI->getOperand(0).getReg();
-      }
-      if (CopyMI->registerDefIsDead(DstReg)) {
-        LiveInterval &li = li_->getInterval(DstReg);
-        if (!ShortenDeadCopySrcLiveRange(li, CopyMI))
-          ShortenDeadCopyLiveRange(li, CopyMI);
-      }
-      li_->RemoveMachineInstrFromMaps(*I);
-      (*I)->eraseFromParent();
-      ++numPeep;
-    }
   }
 
   // Perform a final pass over the instructions and compute spill weights
@@ -2034,15 +2014,35 @@ bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction &fn) {
 
     for (MachineBasicBlock::iterator mii = mbb->begin(), mie = mbb->end();
          mii != mie; ) {
-      // if the move will be an identity move delete it
-      unsigned srcReg, dstReg;
-      bool isMove = tii_->isMoveInstr(*mii, srcReg, dstReg);
-      if (isMove && srcReg == dstReg) {
-        if (li_->hasInterval(srcReg)) {
-          LiveInterval &RegInt = li_->getInterval(srcReg);
+      MachineInstr *MI = mii;
+      unsigned SrcReg, DstReg;
+      if (JoinedCopies.count(MI)) {
+        // Delete all coalesced copies.
+        if (!tii_->isMoveInstr(*MI, SrcReg, DstReg)) {
+          assert((MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
+                  MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG) &&
+                 "Unrecognized copy instruction");
+          DstReg = MI->getOperand(0).getReg();
+        }
+        if (MI->registerDefIsDead(DstReg)) {
+          LiveInterval &li = li_->getInterval(DstReg);
+          if (!ShortenDeadCopySrcLiveRange(li, MI))
+            ShortenDeadCopyLiveRange(li, MI);
+        }
+        li_->RemoveMachineInstrFromMaps(MI);
+        mii = mbbi->erase(mii);
+        ++numPeep;
+        continue;
+      }
+
+      // If the move will be an identity move delete it
+      bool isMove = tii_->isMoveInstr(*mii, SrcReg, DstReg);
+      if (isMove && SrcReg == DstReg) {
+        if (li_->hasInterval(SrcReg)) {
+          LiveInterval &RegInt = li_->getInterval(SrcReg);
           // If def of this move instruction is dead, remove its live range
           // from the dstination register's live interval.
-          if (mii->registerDefIsDead(dstReg)) {
+          if (mii->registerDefIsDead(DstReg)) {
             if (!ShortenDeadCopySrcLiveRange(RegInt, mii))
               ShortenDeadCopyLiveRange(RegInt, mii);
           }
@@ -2050,7 +2050,7 @@ bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction &fn) {
         li_->RemoveMachineInstrFromMaps(mii);
         mii = mbbi->erase(mii);
         ++numPeep;
-      } else if (!isMove || !TurnCopyIntoImpDef(mii, mbb, dstReg, srcReg)) {
+      } else if (!isMove || !TurnCopyIntoImpDef(mii, mbb, DstReg, SrcReg)) {
         SmallSet<unsigned, 4> UniqueUses;
         for (unsigned i = 0, e = mii->getNumOperands(); i != e; ++i) {
           const MachineOperand &mop = mii->getOperand(i);