Avoid unnecessary string construction during asm printing.
[oota-llvm.git] / lib / CodeGen / LiveIntervalAnalysis.cpp
index 94952d681251b6315246812a4199943e8ee4b7ed..7b0eaeb341b9e2baea4ae556cd0a921ad38bb0f0 100644 (file)
@@ -73,12 +73,11 @@ void LiveIntervals::releaseMemory() {
   // Release VNInfo memroy regions after all VNInfo objects are dtor'd.
   VNInfoAllocator.Reset();
   for (unsigned i = 0, e = ClonedMIs.size(); i != e; ++i)
-    delete ClonedMIs[i];
+    mf_->DeleteMachineInstr(ClonedMIs[i]);
 }
 
 void LiveIntervals::computeNumbering() {
   Index2MiMap OldI2MI = i2miMap_;
-  std::vector<IdxMBBPair> OldI2MBB = Idx2MBBMap;
   
   Idx2MBBMap.clear();
   MBB2IdxMap.clear();
@@ -94,22 +93,19 @@ void LiveIntervals::computeNumbering() {
        MBB != E; ++MBB) {
     unsigned StartIdx = MIIndex;
 
-    // Insert an empty slot at the beginning of each block.
-    MIIndex += InstrSlots::NUM;
-    i2miMap_.push_back(0);
-    
     for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
          I != E; ++I) {
       bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
       assert(inserted && "multiple MachineInstr -> index mappings");
       i2miMap_.push_back(I);
       MIIndex += InstrSlots::NUM;
-      
-      // Insert an empty slot after every instruction.
+    }
+    
+    if (StartIdx == MIIndex) {
+      // Empty MBB
       MIIndex += InstrSlots::NUM;
       i2miMap_.push_back(0);
     }
-    
     // Set the MBB2IdxMap entry for this MBB.
     MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
     Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
@@ -117,82 +113,90 @@ void LiveIntervals::computeNumbering() {
   std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
   
   if (!OldI2MI.empty())
-    for (iterator OI = begin(), OE = end(); OI != OE; ++OI)
-      for (LiveInterval::iterator LI = OI->second.begin(),
-           LE = OI->second.end(); LI != LE; ++LI) {
+    for (iterator I = begin(), E = end(); I != E; ++I)
+      for (LiveInterval::iterator LI = I->second.begin(), LE = I->second.end();
+           LI != LE; ++LI) {
         
         // Remap the start index of the live range to the corresponding new
         // number, or our best guess at what it _should_ correspond to if the
         // original instruction has been erased.  This is either the following
         // instruction or its predecessor.
-        unsigned index = LI->start / InstrSlots::NUM;
         unsigned offset = LI->start % InstrSlots::NUM;
-        if (offset == InstrSlots::LOAD) {
-          std::vector<IdxMBBPair>::const_iterator I =
-                  std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), index);
-          // Take the pair containing the index
-          std::vector<IdxMBBPair>::const_iterator J =
-                    ((I != OldI2MBB.end() && I->first > index) ||
-                    (I == OldI2MBB.end() && OldI2MBB.size()>0)) ? (I-1): I;
+        if (OldI2MI[LI->start / InstrSlots::NUM])
+          LI->start = mi2iMap_[OldI2MI[LI->start / InstrSlots::NUM]] + offset;
+        else {
+          unsigned i = 0;
+          MachineInstr* newInstr = 0;
+          do {
+            newInstr = OldI2MI[LI->start / InstrSlots::NUM + i];
+            i++;
+          } while (!newInstr);
           
-          LI->start = getMBBStartIdx(J->second);
-        } else {
-          LI->start = mi2iMap_[OldI2MI[index]] + offset;
+          if (mi2iMap_[newInstr] ==
+              MBB2IdxMap[newInstr->getParent()->getNumber()].first)
+            LI->start = mi2iMap_[newInstr];
+          else
+            LI->start = mi2iMap_[newInstr] - InstrSlots::NUM + offset;
         }
         
         // Remap the ending index in the same way that we remapped the start,
         // except for the final step where we always map to the immediately
         // following instruction.
-        index = LI->end / InstrSlots::NUM;
-        offset  = LI->end % InstrSlots::NUM;
-        if (offset == InstrSlots::STORE) {
-          std::vector<IdxMBBPair>::const_iterator I =
-                  std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), index);
-          // Take the pair containing the index
-          std::vector<IdxMBBPair>::const_iterator J =
-                    ((I != OldI2MBB.end() && I->first > index) ||
-                    (I == OldI2MBB.end() && OldI2MBB.size()>0)) ? (I-1): I;
-          
-          LI->start = getMBBEndIdx(J->second);
+        if (LI->end / InstrSlots::NUM < OldI2MI.size()) {
+          offset = LI->end % InstrSlots::NUM;
+          if (OldI2MI[LI->end / InstrSlots::NUM])
+            LI->end = mi2iMap_[OldI2MI[LI->end / InstrSlots::NUM]] + offset;
+          else {
+            unsigned i = 0;
+            MachineInstr* newInstr = 0;
+            do {
+              newInstr = OldI2MI[LI->end / InstrSlots::NUM + i];
+              i++;
+            } while (!newInstr);
+            
+            LI->end = mi2iMap_[newInstr];
+          }
         } else {
-          LI->end = mi2iMap_[OldI2MI[index]] + offset;
+          LI->end = i2miMap_.size() * InstrSlots::NUM;
         }
         
         // Remap the VNInfo def index, which works the same as the
         // start indices above.
         VNInfo* vni = LI->valno;
-        index = vni->def / InstrSlots::NUM;
         offset = vni->def % InstrSlots::NUM;
-        if (offset == InstrSlots::LOAD) {
-          std::vector<IdxMBBPair>::const_iterator I =
-                  std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), index);
-          // Take the pair containing the index
-          std::vector<IdxMBBPair>::const_iterator J =
-                    ((I != OldI2MBB.end() && I->first > index) ||
-                    (I == OldI2MBB.end() && OldI2MBB.size()>0)) ? (I-1): I;
-          
-          vni->def = getMBBStartIdx(J->second);
+        if (OldI2MI[vni->def / InstrSlots::NUM])
+          vni->def = mi2iMap_[OldI2MI[vni->def / InstrSlots::NUM]] + offset;
+        else {
+          unsigned i = 0;
+          MachineInstr* newInstr = 0;
+          do {
+            newInstr = OldI2MI[vni->def / InstrSlots::NUM + i];
+            i++;
+          } while (!newInstr);
           
-        } else {
-          vni->def = mi2iMap_[OldI2MI[index]] + offset;
+          if (mi2iMap_[newInstr] ==
+              MBB2IdxMap[newInstr->getParent()->getNumber()].first)
+            vni->def = mi2iMap_[newInstr];
+          else
+            vni->def = mi2iMap_[newInstr] - InstrSlots::NUM + offset;
         }
         
         // Remap the VNInfo kill indices, which works the same as
         // the end indices above.
         for (size_t i = 0; i < vni->kills.size(); ++i) {
-          index = vni->kills[i] / InstrSlots::NUM;
           offset = vni->kills[i] % InstrSlots::NUM;
-          if (OldI2MI[vni->kills[i] / InstrSlots::NUM]) {
-            std::vector<IdxMBBPair>::const_iterator I =
-                    std::lower_bound(OldI2MBB.begin(), OldI2MBB.end(), index);
-            // Take the pair containing the index
-            std::vector<IdxMBBPair>::const_iterator J =
-                      ((I != OldI2MBB.end() && I->first > index) ||
-                      (I == OldI2MBB.end() && OldI2MBB.size()>0)) ? (I-1): I;
-
-            vni->kills[i] = getMBBEndIdx(J->second);
-          } else {
-            vni->kills[i] = mi2iMap_[OldI2MI[index]] + offset;
+          if (OldI2MI[vni->kills[i] / InstrSlots::NUM])
+            vni->kills[i] = mi2iMap_[OldI2MI[vni->kills[i] / InstrSlots::NUM]] +
+                            offset;
+          else {
+            unsigned e = 0;
+            MachineInstr* newInstr = 0;
+            do {
+              newInstr = OldI2MI[vni->kills[i] / InstrSlots::NUM + e];
+              e++;
+            } while (!newInstr);
+            
+            vni->kills[i] = mi2iMap_[newInstr];
           }
         }
       }
@@ -293,7 +297,7 @@ void LiveIntervals::printRegName(unsigned reg) const {
 
 void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
                                              MachineBasicBlock::iterator mi,
-                                             unsigned MIIdx,
+                                             unsigned MIIdx, MachineOperand& MO,
                                              LiveInterval &interval) {
   DOUT << "\t\tregister: "; DEBUG(printRegName(interval.reg));
   LiveVariables::VarInfo& vi = lv_->getVarInfo(interval.reg);
@@ -350,7 +354,9 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
     // of the defining block, potentially live across some blocks, then is
     // live into some number of blocks, but gets killed.  Start by adding a
     // range that goes from this definition to the end of the defining block.
-    LiveRange NewLR(defIndex, getMBBEndIdx(mbb), ValNo);
+    LiveRange NewLR(defIndex,
+                    getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
+                    ValNo);
     DOUT << " +" << NewLR;
     interval.addRange(NewLR);
 
@@ -422,7 +428,7 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
 
       // If this redefinition is dead, we need to add a dummy unit live
       // range covering the def slot.
-      if (mi->registerDefIsDead(interval.reg, tri_))
+      if (MO.isDead())
         interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
 
       DOUT << " RESULT: ";
@@ -470,7 +476,7 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
         CopyMI = mi;
       ValNo = interval.getNextValue(defIndex, CopyMI, VNInfoAllocator);
       
-      unsigned killIndex = getMBBEndIdx(mbb) + 1;
+      unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
       LiveRange LR(defIndex, killIndex, ValNo);
       interval.addRange(LR);
       interval.addKill(ValNo, killIndex);
@@ -485,6 +491,7 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
 void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
                                               MachineBasicBlock::iterator mi,
                                               unsigned MIIdx,
+                                              MachineOperand& MO,
                                               LiveInterval &interval,
                                               MachineInstr *CopyMI) {
   // A physical register cannot be live across basic block, so its
@@ -498,7 +505,7 @@ void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
   // If it is not used after definition, it is considered dead at
   // the instruction defining it. Hence its interval is:
   // [defSlot(def), defSlot(def)+1)
-  if (mi->registerDefIsDead(interval.reg, tri_)) {
+  if (MO.isDead()) {
     DOUT << " dead";
     end = getDefIndex(start) + 1;
     goto exit;
@@ -507,10 +514,8 @@ void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
   // If it is not dead on definition, it must be killed by a
   // subsequent instruction. Hence its interval is:
   // [defSlot(def), useSlot(kill)+1)
-  baseIndex += InstrSlots::NUM;
   while (++mi != MBB->end()) {
-    while (getInstructionFromIndex(baseIndex) == 0)
-      baseIndex += InstrSlots::NUM;
+    baseIndex += InstrSlots::NUM;
     if (mi->killsRegister(interval.reg, tri_)) {
       DOUT << " killed";
       end = getUseIndex(baseIndex) + 1;
@@ -524,8 +529,6 @@ void LiveIntervals::handlePhysicalRegisterDef(MachineBasicBlock *MBB,
       end = getDefIndex(start) + 1;
       goto exit;
     }
-    
-    baseIndex += InstrSlots::NUM;
   }
   
   // The only case we should have a dead physreg here without a killing or
@@ -550,23 +553,26 @@ exit:
 void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
                                       MachineBasicBlock::iterator MI,
                                       unsigned MIIdx,
-                                      unsigned reg) {
-  if (TargetRegisterInfo::isVirtualRegister(reg))
-    handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
-  else if (allocatableRegs_[reg]) {
+                                      MachineOperand& MO) {
+  if (TargetRegisterInfo::isVirtualRegister(MO.getReg()))
+    handleVirtualRegisterDef(MBB, MI, MIIdx, MO, 
+                             getOrCreateInterval(MO.getReg()));
+  else if (allocatableRegs_[MO.getReg()]) {
     MachineInstr *CopyMI = NULL;
     unsigned SrcReg, DstReg;
     if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG ||
         MI->getOpcode() == TargetInstrInfo::INSERT_SUBREG ||
         tii_->isMoveInstr(*MI, SrcReg, DstReg))
       CopyMI = MI;
-    handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), CopyMI);
+    handlePhysicalRegisterDef(MBB, MI, MIIdx, MO, 
+                              getOrCreateInterval(MO.getReg()), CopyMI);
     // Def of a register also defines its sub-registers.
-    for (const unsigned* AS = tri_->getSubRegisters(reg); *AS; ++AS)
+    for (const unsigned* AS = tri_->getSubRegisters(MO.getReg()); *AS; ++AS)
       // If MI also modifies the sub-register explicitly, avoid processing it
       // more than once. Do not pass in TRI here so it checks for exact match.
       if (!MI->modifiesRegister(*AS))
-        handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(*AS), 0);
+        handlePhysicalRegisterDef(MBB, MI, MIIdx, MO, 
+                                  getOrCreateInterval(*AS), 0);
   }
 }
 
@@ -597,8 +603,6 @@ void LiveIntervals::handleLiveInRegister(MachineBasicBlock *MBB,
     }
 
     baseIndex += InstrSlots::NUM;
-    while (getInstructionFromIndex(baseIndex) == 0)
-      baseIndex += InstrSlots::NUM;
     ++mi;
   }
 
@@ -630,12 +634,6 @@ void LiveIntervals::computeIntervals() {
        << ((Value*)mf_->getFunction())->getName() << '\n';
   // Track the index of the current machine instr.
   unsigned MIIndex = 0;
-  
-  // Skip over empty initial indices.
-  while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
-         getInstructionFromIndex(MIIndex) == 0)
-    MIIndex += InstrSlots::NUM;
-  
   for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
        MBBI != E; ++MBBI) {
     MachineBasicBlock *MBB = MBBI;
@@ -662,16 +660,13 @@ void LiveIntervals::computeIntervals() {
         MachineOperand &MO = MI->getOperand(i);
         // handle register defs - build intervals
         if (MO.isRegister() && MO.getReg() && MO.isDef())
-          handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
+          handleRegisterDef(MBB, MI, MIIndex, MO);
       }
       
       MIIndex += InstrSlots::NUM;
-      
-      // Skip over empty indices.
-      while (MIIndex / InstrSlots::NUM < i2miMap_.size() &&
-             getInstructionFromIndex(MIIndex) == 0)
-        MIIndex += InstrSlots::NUM;
     }
+    
+    if (MBB->begin() == miEnd) MIIndex += InstrSlots::NUM; // Empty MBB
   }
 }
 
@@ -889,10 +884,6 @@ bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI,
 
     // Attempt to fold the memory reference into the instruction. If
     // we can do this, we don't need to insert spill code.
-    if (lv_)
-      lv_->instructionChanged(MI, fmi);
-    else
-      fmi->copyKillDeadInfo(MI, tri_);
     MachineBasicBlock &MBB = *MI->getParent();
     if (isSS && !mf_->getFrameInfo()->isImmutableObjectIndex(Slot))
       vrm.virtFolded(Reg, MI, fmi, (VirtRegMap::ModRef)MRInfo);
@@ -988,7 +979,6 @@ rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
   unsigned loopDepth = loopInfo->getLoopDepth(MBB);
   bool CanFold = false;
  RestartInstruction:
-  bool isMem = MI->getDesc().mayLoad() || MI->getDesc().mayStore();
   for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
     MachineOperand& mop = MI->getOperand(i);
     if (!mop.isRegister())
@@ -1056,7 +1046,7 @@ rewriteInstructionForSpills(const LiveInterval &li, const VNInfo *VNI,
     }
 
     // Update stack slot spill weight if we are splitting.
-    float Weight = getSpillWeight(HasDef, HasUse, isMem, loopDepth);
+    float Weight = getSpillWeight(HasDef, HasUse, loopDepth);
       if (!TrySplit)
       SSWeight += Weight;
 
@@ -1249,7 +1239,6 @@ rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
     bool MIHasUse = rwi.HasUse;
     bool MIHasDef = rwi.HasDef;
     MachineInstr *MI = rwi.MI;
-    bool isMem = MI->getDesc().mayLoad() || MI->getDesc().mayStore();
     // If MI def and/or use the same register multiple times, then there
     // are multiple entries.
     unsigned NumUses = MIHasUse;
@@ -1397,7 +1386,7 @@ rewriteInstructionsForSpills(const LiveInterval &li, bool TrySplit,
 
     // Update spill weight.
     unsigned loopDepth = loopInfo->getLoopDepth(MBB);
-    nI.weight += getSpillWeight(HasDef, HasUse, isMem, loopDepth);
+    nI.weight += getSpillWeight(HasDef, HasUse, loopDepth);
   }
 
   if (NewVReg && TrySplit && AllCanFold) {
@@ -1471,10 +1460,6 @@ std::vector<LiveInterval*> LiveIntervals::
 addIntervalsForSpills(const LiveInterval &li,
                       const MachineLoopInfo *loopInfo, VirtRegMap &vrm,
                       float &SSWeight) {
-  // Since this is called after the analysis is done we don't know if
-  // LiveVariables is available
-  lv_ = getAnalysisToUpdate<LiveVariables>();
-
   assert(li.weight != HUGE_VALF &&
          "attempt to spill already spilled interval!");
 
@@ -1577,7 +1562,7 @@ addIntervalsForSpills(const LiveInterval &li,
       ReMatOrigDefs[VN] = ReMatDefMI;
       // Original def may be modified so we have to make a copy here. vrm must
       // delete these!
-      ReMatDefs[VN] = ReMatDefMI = ReMatDefMI->clone();
+      ReMatDefs[VN] = ReMatDefMI = mf_->CloneMachineInstr(ReMatDefMI);
 
       bool CanDelete = true;
       if (VNI->hasPHIKill) {
@@ -1639,7 +1624,6 @@ addIntervalsForSpills(const LiveInterval &li,
         LiveInterval &nI = getOrCreateInterval(VReg);
         bool isReMat = vrm.isReMaterialized(VReg);
         MachineInstr *MI = getInstructionFromIndex(index);
-        bool isMem = MI->getDesc().mayLoad() || MI->getDesc().mayStore();
         bool CanFold = false;
         bool FoundUse = false;
         Ops.clear();
@@ -1692,7 +1676,7 @@ addIntervalsForSpills(const LiveInterval &li,
 
         // Update spill slot weight.
         if (!isReMat)
-          SSWeight += getSpillWeight(true, false, isMem, loopDepth);
+          SSWeight += getSpillWeight(true, false, loopDepth);
       }
       Id = SpillMBBs.find_next(Id);
     }
@@ -1712,7 +1696,6 @@ addIntervalsForSpills(const LiveInterval &li,
       LiveInterval &nI = getOrCreateInterval(VReg);
       bool isReMat = vrm.isReMaterialized(VReg);
       MachineInstr *MI = getInstructionFromIndex(index);
-      bool isMem = MI->getDesc().mayLoad() || MI->getDesc().mayStore();
       bool CanFold = false;
       Ops.clear();
       if (restores[i].canFold) {
@@ -1766,7 +1749,7 @@ addIntervalsForSpills(const LiveInterval &li,
 
       // Update spill slot weight.
       if (!isReMat)
-        SSWeight += getSpillWeight(false, true, isMem, loopDepth);
+        SSWeight += getSpillWeight(false, true, loopDepth);
     }
     Id = RestoreMBBs.find_next(Id);
   }