Correctly clone FlaggedNodes.
[oota-llvm.git] / lib / CodeGen / SimpleRegisterCoalescing.cpp
index 03b27aca18d8bc69aec2d93b67d67fd64a01287d..65c8a5b99b1245fe1edd5654a7cf89d6b615ed02 100644 (file)
@@ -310,11 +310,26 @@ bool SimpleRegisterCoalescing::RemoveCopyByCommutingDef(LiveInterval &IntA,
   unsigned OpIdx = NewMI->findRegisterUseOperandIdx(IntA.reg, false);
   NewMI->getOperand(OpIdx).setIsKill();
 
-  // Update uses of IntA of the specific Val# with IntB.
   bool BHasPHIKill = BValNo->hasPHIKill;
   SmallVector<VNInfo*, 4> BDeadValNos;
   SmallVector<unsigned, 4> BKills;
   std::map<unsigned, unsigned> BExtend;
+
+  // If ALR and BLR overlaps and end of BLR extends beyond end of ALR, e.g.
+  // A = or A, B
+  // ...
+  // B = A
+  // ...
+  // C = A<kill>
+  // ...
+  //   = B
+  //
+  // then do not add kills of A to the newly created B interval.
+  bool Extended = BLR->end > ALR->end && ALR->end != ALR->start;
+  if (Extended)
+    BExtend[ALR->end] = BLR->end;
+
+  // Update uses of IntA of the specific Val# with IntB.
   for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(IntA.reg),
          UE = mri_->use_end(); UI != UE;) {
     MachineOperand &UseMO = UI.getOperand();
@@ -329,8 +344,12 @@ bool SimpleRegisterCoalescing::RemoveCopyByCommutingDef(LiveInterval &IntA,
     UseMO.setReg(NewReg);
     if (UseMI == CopyMI)
       continue;
-    if (UseMO.isKill())
-      BKills.push_back(li_->getUseIndex(UseIdx)+1);
+    if (UseMO.isKill()) {
+      if (Extended)
+        UseMO.setIsKill(false);
+      else
+        BKills.push_back(li_->getUseIndex(UseIdx)+1);
+    }
     unsigned SrcReg, DstReg;
     if (!tii_->isMoveInstr(*UseMI, SrcReg, DstReg))
       continue;
@@ -347,9 +366,8 @@ bool SimpleRegisterCoalescing::RemoveCopyByCommutingDef(LiveInterval &IntA,
       JoinedCopies.insert(UseMI);
       // If this is a kill but it's going to be removed, the last use
       // of the same val# is the new kill.
-      if (UseMO.isKill()) {
+      if (UseMO.isKill())
         BKills.pop_back();
-      }
     }
   }
 
@@ -451,6 +469,53 @@ SimpleRegisterCoalescing::UpdateRegDefsUses(unsigned SrcReg, unsigned DstReg,
   }
 }
 
+/// RemoveUnnecessaryKills - Remove kill markers that are no longer accurate
+/// due to live range lengthening as the result of coalescing.
+void SimpleRegisterCoalescing::RemoveUnnecessaryKills(unsigned Reg,
+                                                      LiveInterval &LI) {
+  for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
+         UE = mri_->use_end(); UI != UE; ++UI) {
+    MachineOperand &UseMO = UI.getOperand();
+    if (UseMO.isKill()) {
+      MachineInstr *UseMI = UseMO.getParent();
+      unsigned SReg, DReg;
+      if (!tii_->isMoveInstr(*UseMI, SReg, DReg))
+        continue;
+      unsigned UseIdx = li_->getUseIndex(li_->getInstructionIndex(UseMI));
+      if (JoinedCopies.count(UseMI))
+        continue;
+      LiveInterval::const_iterator UI = LI.FindLiveRangeContaining(UseIdx);
+      assert(UI != LI.end());
+      if (!LI.isKill(UI->valno, UseIdx+1))
+        UseMO.setIsKill(false);
+    }
+  }
+}
+
+/// ShortenDeadCopyLiveRange - Shorten a live range as it's artificially
+/// extended by a dead copy. Mark the last use (if any) of the val# as kill
+/// as ends the live range there. If there isn't another use, then this
+/// live range is dead.
+void SimpleRegisterCoalescing::ShortenDeadCopyLiveRange(LiveInterval &li,
+                                                        MachineInstr *CopyMI) {
+  unsigned CopyIdx = li_->getInstructionIndex(CopyMI);
+  LiveInterval::iterator MLR =
+    li.FindLiveRangeContaining(li_->getDefIndex(CopyIdx));
+  unsigned RemoveStart = MLR->start;
+  unsigned RemoveEnd = MLR->end;
+  unsigned LastUseIdx;
+  MachineOperand *LastUse = lastRegisterUse(RemoveStart, CopyIdx, li.reg,
+                                            LastUseIdx);
+  if (LastUse) {
+    // Shorten the liveinterval to the end of last use.
+    LastUse->setIsKill();
+    RemoveStart = li_->getDefIndex(LastUseIdx);
+  }
+  li.removeRange(RemoveStart, RemoveEnd, true);
+  if (li.empty())
+    li_->removeInterval(li.reg);
+}
+
 /// JoinCopy - Attempt to join intervals corresponding to SrcReg/DstReg,
 /// which are the src/dst of the copy instruction CopyMI.  This returns true
 /// if the copy was successfully coalesced away. If it is not currently
@@ -599,23 +664,22 @@ bool SimpleRegisterCoalescing::JoinCopy(CopyRec &TheCopy, bool &Again) {
       SrcInt.FindLiveRangeContaining(li_->getUseIndex(CopyIdx));
     RemoveStart = SrcStart = SrcLR->start;
     RemoveEnd   = SrcEnd   = SrcLR->end;
-    // The instruction which defines the src is only truly dead if there are
-    // no intermediate uses and there isn't a use beyond the copy.
-    // FIXME: find the last use, mark is kill and shorten the live range.
     if (SrcEnd > li_->getDefIndex(CopyIdx)) {
+      // If there are other uses of SrcReg beyond the copy, there is nothing to do.
       isDead = false;
     } else {
       unsigned LastUseIdx;
       MachineOperand *LastUse =
         lastRegisterUse(SrcStart, CopyIdx, SrcReg, LastUseIdx);
       if (LastUse) {
-        // Shorten the liveinterval to the end of last use.
+        // There are uses before the copy, just shorten the live range to the end
+        // of last use.
         LastUse->setIsKill();
         isDead = false;
         isShorten = true;
         RemoveStart = li_->getDefIndex(LastUseIdx);
-        RemoveEnd = SrcEnd;
       } else {
+        // This live range is truly dead. Remove it.
         MachineInstr *SrcMI = li_->getInstructionFromIndex(SrcStart);
         if (SrcMI && SrcMI->modifiesRegister(SrcReg, tri_))
           // A dead def should have a single cycle interval.
@@ -780,6 +844,12 @@ bool SimpleRegisterCoalescing::JoinCopy(CopyRec &TheCopy, bool &Again) {
   // Remember to delete the copy instruction.
   JoinedCopies.insert(CopyMI);
 
+  // Some live range has been lengthened due to colaescing, eliminate the
+  // unnecessary kills.
+  RemoveUnnecessaryKills(SrcReg, *ResDstInt);
+  if (TargetRegisterInfo::isVirtualRegister(DstReg))
+    RemoveUnnecessaryKills(DstReg, *ResDstInt);
+
   // SrcReg is guarateed to be the register whose live interval that is
   // being merged.
   li_->removeInterval(SrcReg);
@@ -1458,8 +1528,6 @@ SimpleRegisterCoalescing::lastRegisterUse(unsigned Start, unsigned End,
 }
 
 
-/// RemoveUnnecessaryKills - Remove kill markers that are no longer accurate
-/// due to live range lengthening as the result of coalescing.
 void SimpleRegisterCoalescing::printRegName(unsigned reg) const {
   if (TargetRegisterInfo::isPhysicalRegister(reg))
     cerr << tri_->getName(reg);
@@ -1531,16 +1599,10 @@ bool SimpleRegisterCoalescing::runOnMachineFunction(MachineFunction &fn) {
       if (tii_->isMoveInstr(*mii, srcReg, dstReg) && srcReg == dstReg) {
         // remove from def list
         LiveInterval &RegInt = li_->getOrCreateInterval(srcReg);
-        MachineOperand *MO = mii->findRegisterDefOperand(dstReg, false);
         // If def of this move instruction is dead, remove its live range from
         // the dstination register's live interval.
-        if (MO->isDead()) {
-          unsigned MoveIdx = li_->getDefIndex(li_->getInstructionIndex(mii));
-          LiveInterval::iterator MLR = RegInt.FindLiveRangeContaining(MoveIdx);
-          RegInt.removeRange(MLR->start, MoveIdx+1, true);
-          if (RegInt.empty())
-            li_->removeInterval(srcReg);
-        }
+        if (mii->registerDefIsDead(dstReg))
+          ShortenDeadCopyLiveRange(RegInt, mii);
         li_->RemoveMachineInstrFromMaps(mii);
         mii = mbbi->erase(mii);
         ++numPeep;