Handle cases when joining live intervals of two virtual registers.
[oota-llvm.git] / lib / CodeGen / LiveIntervalAnalysis.cpp
index 70a791b090b4082ac54abe84a892e7ecc1fd9765..ce2e4290b164636711f24daa164ee4f8e3ce3e96 100644 (file)
@@ -145,7 +145,8 @@ bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
         // the dstination register's live interval.
         if (MO->isDead()) {
           unsigned MoveIdx = getDefIndex(getInstructionIndex(mii));
-          RegInt.removeRange(*RegInt.FindLiveRangeContaining(MoveIdx));
+          LiveInterval::iterator MLR = RegInt.FindLiveRangeContaining(MoveIdx);
+          RegInt.removeRange(MLR->start, MoveIdx+1);
           if (RegInt.empty())
             removeInterval(RegRep);
         }
@@ -657,14 +658,15 @@ void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
 }
 
 void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
+                                         unsigned MIIdx,
                                          LiveInterval &interval) {
   DOUT << "\t\tlivein register: "; DEBUG(printRegName(interval.reg));
 
   // Look for kills, if it reaches a def before it's killed, then it shouldn't
   // be considered a livein.
   MachineBasicBlock::iterator mi = MBB->begin();
-  unsigned baseIndex = 0;
-  unsigned start = 0;
+  unsigned baseIndex = MIIdx;
+  unsigned start = baseIndex;
   unsigned end = start;
   while (mi != MBB->end()) {
     if (lv_->KillsRegister(mi, interval.reg)) {
@@ -689,8 +691,8 @@ exit:
   assert(start < end && "did not find end of interval?");
 
   LiveRange LR(start, end, interval.getNextValue(~0U, 0));
-  interval.addRange(LR);
   DOUT << " +" << LR << '\n';
+  interval.addRange(LR);
 }
 
 /// computeIntervals - computes the live intervals for virtual
@@ -714,9 +716,9 @@ void LiveIntervals::computeIntervals() {
       // Create intervals for live-ins to this BB first.
       for (MachineBasicBlock::const_livein_iterator LI = MBB->livein_begin(),
              LE = MBB->livein_end(); LI != LE; ++LI) {
-        handleLiveInRegister(MBB, getOrCreateInterval(*LI));
+        handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*LI));
         for (const unsigned* AS = mri_->getAliasSet(*LI); *AS; ++AS)
-          handleLiveInRegister(MBB, getOrCreateInterval(*AS));
+          handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS));
       }
     }
     
@@ -957,7 +959,29 @@ bool LiveIntervals::JoinCopy(MachineInstr *CopyMI,
 
   DOUT << "\n\t\tJoined.  Result = "; DestInt.print(DOUT, mri_);
   DOUT << "\n";
-  
+
+  // Live range has been lengthened due to colaescing, eliminate the
+  // unnecessary kills at the end of the source live ranges.
+  LiveVariables::VarInfo& svi = lv_->getVarInfo(repSrcReg);
+  for (unsigned i = 0, e = svi.Kills.size(); i != e; ++i) {
+    MachineInstr *Kill = svi.Kills[i];
+    if (Kill == CopyMI || isRemoved(Kill))
+      continue;
+    if (DestInt.liveAt(getInstructionIndex(Kill) + InstrSlots::NUM))
+      unsetRegisterKill(Kill, repSrcReg);
+  }
+  if (MRegisterInfo::isVirtualRegister(repDstReg)) {
+    // If both are virtual registers...
+    LiveVariables::VarInfo& dvi = lv_->getVarInfo(repDstReg);
+    for (unsigned i = 0, e = dvi.Kills.size(); i != e; ++i) {
+      MachineInstr *Kill = dvi.Kills[i];
+      if (Kill == CopyMI || isRemoved(Kill))
+        continue;
+      if (DestInt.liveAt(getInstructionIndex(Kill) + InstrSlots::NUM))
+        unsetRegisterKill(Kill, repDstReg);
+    }
+  }
+
   // If the intervals were swapped by Join, swap them back so that the register
   // mapping (in the r2i map) is correct.
   if (Swapped) SrcInt.swap(DestInt);
@@ -1450,10 +1474,10 @@ bool LiveIntervals::differingRegisterClasses(unsigned RegA,
 /// reg between indexes Start and End.
 bool
 LiveIntervals::hasRegisterUse(unsigned Reg, unsigned Start, unsigned End) {
-  for (unsigned Index = Start+InstrSlots::NUM; Index != End;
+  for (unsigned Index = Start+InstrSlots::NUM; Index < End;
        Index += InstrSlots::NUM) {
     // Skip deleted instructions
-    while (Index != End && !getInstructionFromIndex(Index))
+    while (Index < End && !getInstructionFromIndex(Index))
       Index += InstrSlots::NUM;
     if (Index >= End) break;
 
@@ -1469,6 +1493,17 @@ LiveIntervals::hasRegisterUse(unsigned Reg, unsigned Start, unsigned End) {
   return false;
 }
 
+/// unsetRegisterKill - Unset IsKill property of all uses of specific register
+/// of the specific instruction.
+void LiveIntervals::unsetRegisterKill(MachineInstr *MI, unsigned Reg) {
+  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
+    MachineOperand &MO = MI->getOperand(i);
+    if (MO.isReg() && MO.isUse() && MO.isKill() && MO.getReg() &&
+        mri_->regsOverlap(rep(MO.getReg()), Reg))
+      MO.unsetIsKill();
+  }
+}
+
 LiveInterval LiveIntervals::createInterval(unsigned reg) {
   float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
                        HUGE_VALF : 0.0F;