Typo.
[oota-llvm.git] / lib / CodeGen / LiveIntervalAnalysis.cpp
index bd8137730b299cdc5a168767972384f6c4636583..ca6c04d8f75712a3f274820991418d83cea94ed1 100644 (file)
 #include <cmath>
 using namespace llvm;
 
+namespace {
+  // Hidden options for help debugging.
+  cl::opt<bool> DisableReMat("disable-rematerialization", 
+                              cl::init(false), cl::Hidden);
+}
+
 STATISTIC(numIntervals, "Number of original intervals");
 STATISTIC(numIntervalsAfter, "Number of intervals after coalescing");
 STATISTIC(numFolded   , "Number of loads/stores folded into instructions");
@@ -56,13 +62,32 @@ void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
 }
 
 void LiveIntervals::releaseMemory() {
+  Idx2MBBMap.clear();
   mi2iMap_.clear();
   i2miMap_.clear();
   r2iMap_.clear();
+  // 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];
 }
 
+namespace llvm {
+  inline bool operator<(unsigned V, const IdxMBBPair &IM) {
+    return V < IM.first;
+  }
+
+  inline bool operator<(const IdxMBBPair &IM, unsigned V) {
+    return IM.first < V;
+  }
+
+  struct Idx2MBBCompare {
+    bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
+      return LHS.first < RHS.first;
+    }
+  };
+}
+
 /// runOnMachineFunction - Register allocate the whole function
 ///
 bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
@@ -92,7 +117,9 @@ bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
 
     // Set the MBB2IdxMap entry for this MBB.
     MBB2IdxMap[MBB->getNumber()] = std::make_pair(StartIdx, MIIndex - 1);
+    Idx2MBBMap.push_back(std::make_pair(StartIdx, MBB));
   }
+  std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());
 
   computeIntervals();
 
@@ -128,79 +155,13 @@ void LiveIntervals::print(std::ostream &O, const Module* ) const {
   }
 }
 
-// Not called?
-/// CreateNewLiveInterval - Create a new live interval with the given live
-/// ranges. The new live interval will have an infinite spill weight.
-LiveInterval&
-LiveIntervals::CreateNewLiveInterval(const LiveInterval *LI,
-                                     const std::vector<LiveRange> &LRs) {
-  const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(LI->reg);
-
-  // Create a new virtual register for the spill interval.
-  unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(RC);
-
-  // Replace the old virtual registers in the machine operands with the shiny
-  // new one.
-  for (std::vector<LiveRange>::const_iterator
-         I = LRs.begin(), E = LRs.end(); I != E; ++I) {
-    unsigned Index = getBaseIndex(I->start);
-    unsigned End = getBaseIndex(I->end - 1) + InstrSlots::NUM;
-
-    for (; Index != End; Index += InstrSlots::NUM) {
-      // Skip deleted instructions
-      while (Index != End && !getInstructionFromIndex(Index))
-        Index += InstrSlots::NUM;
-
-      if (Index == End) break;
-
-      MachineInstr *MI = getInstructionFromIndex(Index);
-
-      for (unsigned J = 0, e = MI->getNumOperands(); J != e; ++J) {
-        MachineOperand &MOp = MI->getOperand(J);
-        if (MOp.isRegister() && MOp.getReg() == LI->reg)
-          MOp.setReg(NewVReg);
-      }
-    }
-  }
-
-  LiveInterval &NewLI = getOrCreateInterval(NewVReg);
-
-  // The spill weight is now infinity as it cannot be spilled again
-  NewLI.weight = float(HUGE_VAL);
-
-  for (std::vector<LiveRange>::const_iterator
-         I = LRs.begin(), E = LRs.end(); I != E; ++I) {
-    DOUT << "  Adding live range " << *I << " to new interval\n";
-    NewLI.addRange(*I);
-  }
-            
-  DOUT << "Created new live interval " << NewLI << "\n";
-  return NewLI;
-}
-
-/// isReDefinedByTwoAddr - Returns true if the Reg re-definition is due to
-/// two addr elimination.
-static bool isReDefinedByTwoAddr(MachineInstr *MI, unsigned Reg,
-                                const TargetInstrInfo *TII) {
-  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
-    MachineOperand &MO1 = MI->getOperand(i);
-    if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) {
-      for (unsigned j = i+1; j < e; ++j) {
-        MachineOperand &MO2 = MI->getOperand(j);
-        if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg &&
-            MI->getInstrDescriptor()->
-            getOperandConstraint(j, TOI::TIED_TO) == (int)i)
-          return true;
-      }
-    }
-  }
-  return false;
-}
-
 /// isReMaterializable - Returns true if the definition MI of the specified
 /// val# of the specified interval is re-materializable.
-bool LiveIntervals::isReMaterializable(const LiveInterval &li, unsigned ValNum,
-                                       MachineInstr *MI) {
+bool LiveIntervals::isReMaterializable(const LiveInterval &li,
+                                       const VNInfo *ValNo, MachineInstr *MI) {
+  if (DisableReMat)
+    return false;
+
   if (tii_->isTriviallyReMaterializable(MI))
     return true;
 
@@ -211,24 +172,33 @@ bool LiveIntervals::isReMaterializable(const LiveInterval &li, unsigned ValNum,
 
   // This is a load from fixed stack slot. It can be rematerialized unless it's
   // re-defined by a two-address instruction.
-  for (unsigned i = 0, e = li.getNumValNums(); i != e; ++i) {
-    if (i == ValNum)
+  for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
+       i != e; ++i) {
+    const VNInfo *VNI = *i;
+    if (VNI == ValNo)
       continue;
-    unsigned DefIdx = li.getDefForValNum(i);
+    unsigned DefIdx = VNI->def;
     if (DefIdx == ~1U)
       continue; // Dead val#.
     MachineInstr *DefMI = (DefIdx == ~0u)
       ? NULL : getInstructionFromIndex(DefIdx);
-    if (DefMI && isReDefinedByTwoAddr(DefMI, li.reg, tii_))
+    if (DefMI && DefMI->isRegReDefinedByTwoAddr(li.reg))
       return false;
   }
   return true;
 }
 
+/// tryFoldMemoryOperand - Attempts to fold either a spill / restore from
+/// slot / to reg or any rematerialized load into ith operand of specified
+/// MI. If it is successul, MI is updated with the newly created MI and
+/// returns true.
 bool LiveIntervals::tryFoldMemoryOperand(MachineInstr* &MI, VirtRegMap &vrm,
+                                         MachineInstr *DefMI,
                                          unsigned index, unsigned i,
-                                         int slot, unsigned reg) {
-  MachineInstr *fmi = mri_->foldMemoryOperand(MI, i, slot);
+                                         bool isSS, int slot, unsigned reg) {
+  MachineInstr *fmi = isSS
+    ? mri_->foldMemoryOperand(MI, i, slot)
+    : mri_->foldMemoryOperand(MI, i, DefMI);
   if (fmi) {
     // Attempt to fold the memory reference into the instruction. If
     // we can do this, we don't need to insert spill code.
@@ -261,7 +231,8 @@ addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) {
   li.print(DOUT, mri_);
   DOUT << '\n';
 
-  const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(li.reg);
+  SSARegMap *RegMap = mf_->getSSARegMap();
+  const TargetRegisterClass* rc = RegMap->getRegClass(li.reg);
 
   unsigned NumValNums = li.getNumValNums();
   SmallVector<MachineInstr*, 4> ReMatDefs;
@@ -274,25 +245,27 @@ addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) {
   unsigned slot = VirtRegMap::MAX_STACK_SLOT;
 
   bool NeedStackSlot = false;
-  for (unsigned i = 0; i != NumValNums; ++i) {
-    unsigned DefIdx = li.getDefForValNum(i);
+  for (LiveInterval::const_vni_iterator i = li.vni_begin(), e = li.vni_end();
+       i != e; ++i) {
+    const VNInfo *VNI = *i;
+    unsigned VN = VNI->id;
+    unsigned DefIdx = VNI->def;
     if (DefIdx == ~1U)
       continue; // Dead val#.
     // Is the def for the val# rematerializable?
     MachineInstr *DefMI = (DefIdx == ~0u)
       ? NULL : getInstructionFromIndex(DefIdx);
-    if (DefMI && isReMaterializable(li, i, DefMI)) {
+    if (DefMI && isReMaterializable(li, VNI, DefMI)) {
       // Remember how to remat the def of this val#.
-      ReMatOrigDefs[i] = DefMI;
+      ReMatOrigDefs[VN] = DefMI;
       // Original def may be modified so we have to make a copy here. vrm must
       // delete these!
-      ReMatDefs[i] = DefMI = DefMI->clone();
+      ReMatDefs[VN] = DefMI = DefMI->clone();
       vrm.setVirtIsReMaterialized(reg, DefMI);
 
       bool CanDelete = true;
-      const SmallVector<unsigned, 4> &kills = li.getKillsForValNum(i);
-      for (unsigned j = 0, ee = kills.size(); j != ee; ++j) {
-        unsigned KillIdx = kills[j];
+      for (unsigned j = 0, ee = VNI->kills.size(); j != ee; ++j) {
+        unsigned KillIdx = VNI->kills[j];
         MachineInstr *KillMI = (KillIdx & 1)
           ? NULL : getInstructionFromIndex(KillIdx);
         // Kill is a phi node, not all of its uses can be rematerialized.
@@ -307,7 +280,7 @@ addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) {
       }
 
       if (CanDelete)
-        ReMatDelete.set(i);
+        ReMatDelete.set(VN);
     } else {
       // Need a stack slot if there is any live range where uses cannot be
       // rematerialized.
@@ -321,12 +294,14 @@ addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) {
 
   for (LiveInterval::Ranges::const_iterator
          I = li.ranges.begin(), E = li.ranges.end(); I != E; ++I) {
-    MachineInstr *DefMI = ReMatDefs[I->ValId];
-    MachineInstr *OrigDefMI = ReMatOrigDefs[I->ValId];
+    MachineInstr *DefMI = ReMatDefs[I->valno->id];
+    MachineInstr *OrigDefMI = ReMatOrigDefs[I->valno->id];
     bool DefIsReMat = DefMI != NULL;
-    bool CanDelete = ReMatDelete[I->ValId];
+    bool CanDelete = ReMatDelete[I->valno->id];
     int LdSlot = 0;
     bool isLoadSS = DefIsReMat && tii_->isLoadFromStackSlot(DefMI, LdSlot);
+    bool isLoad = isLoadSS ||
+      (DefIsReMat && (DefMI->getInstrDescriptor()->Flags & M_LOAD_FLAG));
     unsigned index = getBaseIndex(I->start);
     unsigned end = getBaseIndex(I->end-1) + InstrSlots::NUM;
     for (; index != end; index += InstrSlots::NUM) {
@@ -340,111 +315,126 @@ addIntervalsForSpills(const LiveInterval &li, VirtRegMap &vrm, unsigned reg) {
     RestartInstruction:
       for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
         MachineOperand& mop = MI->getOperand(i);
-        if (mop.isRegister() && mop.getReg() == li.reg) {
-          if (DefIsReMat) {
-            // If this is the rematerializable definition MI itself and
-            // all of its uses are rematerialized, simply delete it.
-            if (MI == OrigDefMI) {
-              if (CanDelete) {
-                RemoveMachineInstrFromMaps(MI);
-                MI->eraseFromParent();
-                break;
-              } else if (tryFoldMemoryOperand(MI, vrm, index, i, slot, li.reg))
-                // Folding the load/store can completely change the instruction
-                // in unpredictable ways, rescan it from the beginning.
-                goto RestartInstruction;
-            } else if (isLoadSS &&
-                       tryFoldMemoryOperand(MI, vrm, index, i, LdSlot, li.reg)){
-              // FIXME: Other rematerializable loads can be folded as well.
-              // Folding the load/store can completely change the
-              // instruction in unpredictable ways, rescan it from
-              // the beginning.
-              goto RestartInstruction;
-            }
-          } else {
-            if (tryFoldMemoryOperand(MI, vrm, index, i, slot, li.reg))
-              // Folding the load/store can completely change the instruction in
-              // unpredictable ways, rescan it from the beginning.
-              goto RestartInstruction;
+        if (!mop.isRegister())
+          continue;
+        unsigned Reg = mop.getReg();
+        if (Reg == 0 || MRegisterInfo::isPhysicalRegister(Reg))
+          continue;
+        bool isSubReg = RegMap->isSubRegister(Reg);
+        unsigned SubIdx = 0;
+        if (isSubReg) {
+          SubIdx = RegMap->getSubRegisterIndex(Reg);
+          Reg = RegMap->getSuperRegister(Reg);
+        }
+        if (Reg != li.reg)
+          continue;
+
+        bool TryFold = !DefIsReMat;
+        bool FoldSS = true;
+        int FoldSlot = slot;
+        if (DefIsReMat) {
+          // If this is the rematerializable definition MI itself and
+          // all of its uses are rematerialized, simply delete it.
+          if (MI == OrigDefMI && CanDelete) {
+            RemoveMachineInstrFromMaps(MI);
+            MI->eraseFromParent();
+            break;
           }
 
-          // Create a new virtual register for the spill interval.
-          unsigned NewVReg = mf_->getSSARegMap()->createVirtualRegister(rc);
+          // If def for this use can't be rematerialized, then try folding.
+          TryFold = !OrigDefMI || (OrigDefMI && (MI == OrigDefMI || isLoad));
+          if (isLoad) {
+            // Try fold loads (from stack slot, constant pool, etc.) into uses.
+            FoldSS = isLoadSS;
+            FoldSlot = LdSlot;
+          }
+        }
+
+        // FIXME: fold subreg use
+        if (!isSubReg && TryFold &&
+            tryFoldMemoryOperand(MI, vrm, DefMI, index, i, FoldSS, FoldSlot, Reg))
+          // Folding the load/store can completely change the instruction in
+          // unpredictable ways, rescan it from the beginning.
+          goto RestartInstruction;
+
+        // Create a new virtual register for the spill interval.
+        unsigned NewVReg = RegMap->createVirtualRegister(rc);
+        if (isSubReg)
+          RegMap->setIsSubRegister(NewVReg, NewVReg, SubIdx);
             
-          // Scan all of the operands of this instruction rewriting operands
-          // to use NewVReg instead of li.reg as appropriate.  We do this for
-          // two reasons:
-          //
-          //   1. If the instr reads the same spilled vreg multiple times, we
-          //      want to reuse the NewVReg.
-          //   2. If the instr is a two-addr instruction, we are required to
-          //      keep the src/dst regs pinned.
-          //
-          // Keep track of whether we replace a use and/or def so that we can
-          // create the spill interval with the appropriate range. 
-          mop.setReg(NewVReg);
+        // Scan all of the operands of this instruction rewriting operands
+        // to use NewVReg instead of li.reg as appropriate.  We do this for
+        // two reasons:
+        //
+        //   1. If the instr reads the same spilled vreg multiple times, we
+        //      want to reuse the NewVReg.
+        //   2. If the instr is a two-addr instruction, we are required to
+        //      keep the src/dst regs pinned.
+        //
+        // Keep track of whether we replace a use and/or def so that we can
+        // create the spill interval with the appropriate range. 
+        mop.setReg(NewVReg);
             
-          bool HasUse = mop.isUse();
-          bool HasDef = mop.isDef();
-          for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
-            if (MI->getOperand(j).isReg() &&
-                MI->getOperand(j).getReg() == li.reg) {
-              MI->getOperand(j).setReg(NewVReg);
-              HasUse |= MI->getOperand(j).isUse();
-              HasDef |= MI->getOperand(j).isDef();
-            }
+        bool HasUse = mop.isUse();
+        bool HasDef = mop.isDef();
+        for (unsigned j = i+1, e = MI->getNumOperands(); j != e; ++j) {
+          if (MI->getOperand(j).isRegister() &&
+              MI->getOperand(j).getReg() == li.reg) {
+            MI->getOperand(j).setReg(NewVReg);
+            HasUse |= MI->getOperand(j).isUse();
+            HasDef |= MI->getOperand(j).isDef();
           }
+        }
 
-          vrm.grow();
-          if (DefIsReMat) {
-            vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/);
-            if (ReMatIds[I->ValId] == VirtRegMap::MAX_STACK_SLOT) {
-              // Each valnum may have its own remat id.
-              ReMatIds[I->ValId] = vrm.assignVirtReMatId(NewVReg);
-            } else {
-              vrm.assignVirtReMatId(NewVReg, ReMatIds[I->ValId]);
-            }
-            if (!CanDelete || (HasUse && HasDef)) {
-              // If this is a two-addr instruction then its use operands are
-              // rematerializable but its def is not. It should be assigned a
-              // stack slot.
-              vrm.assignVirt2StackSlot(NewVReg, slot);
-            }
+        vrm.grow();
+        if (DefIsReMat) {
+          vrm.setVirtIsReMaterialized(NewVReg, DefMI/*, CanDelete*/);
+          if (ReMatIds[I->valno->id] == VirtRegMap::MAX_STACK_SLOT) {
+            // Each valnum may have its own remat id.
+            ReMatIds[I->valno->id] = vrm.assignVirtReMatId(NewVReg);
           } else {
+            vrm.assignVirtReMatId(NewVReg, ReMatIds[I->valno->id]);
+          }
+          if (!CanDelete || (HasUse && HasDef)) {
+            // If this is a two-addr instruction then its use operands are
+            // rematerializable but its def is not. It should be assigned a
+            // stack slot.
             vrm.assignVirt2StackSlot(NewVReg, slot);
           }
+        } else {
+          vrm.assignVirt2StackSlot(NewVReg, slot);
+        }
 
-          // create a new register interval for this spill / remat.
-          LiveInterval &nI = getOrCreateInterval(NewVReg);
-          assert(nI.empty());
+        // create a new register interval for this spill / remat.
+        LiveInterval &nI = getOrCreateInterval(NewVReg);
+        assert(nI.empty());
 
-          // the spill weight is now infinity as it
-          // cannot be spilled again
-          nI.weight = HUGE_VALF;
+        // the spill weight is now infinity as it
+        // cannot be spilled again
+        nI.weight = HUGE_VALF;
 
-          if (HasUse) {
-            LiveRange LR(getLoadIndex(index), getUseIndex(index),
-                         nI.getNextValue(~0U, 0));
-            DOUT << " +" << LR;
-            nI.addRange(LR);
-          }
-          if (HasDef) {
-            LiveRange LR(getDefIndex(index), getStoreIndex(index),
-                         nI.getNextValue(~0U, 0));
-            DOUT << " +" << LR;
-            nI.addRange(LR);
-          }
+        if (HasUse) {
+          LiveRange LR(getLoadIndex(index), getUseIndex(index)+1,
+                       nI.getNextValue(~0U, 0, VNInfoAllocator));
+          DOUT << " +" << LR;
+          nI.addRange(LR);
+        }
+        if (HasDef) {
+          LiveRange LR(getDefIndex(index), getStoreIndex(index),
+                       nI.getNextValue(~0U, 0, VNInfoAllocator));
+          DOUT << " +" << LR;
+          nI.addRange(LR);
+        }
             
-          added.push_back(&nI);
+        added.push_back(&nI);
 
-          // update live variables if it is available
-          if (lv_)
-            lv_->addVirtualRegisterKilled(NewVReg, MI);
+        // update live variables if it is available
+        if (lv_)
+          lv_->addVirtualRegisterKilled(NewVReg, MI);
             
-          DOUT << "\t\t\t\tadded new interval: ";
-          nI.print(DOUT, mri_);
-          DOUT << '\n';
-        }
+        DOUT << "\t\t\t\tadded new interval: ";
+        nI.print(DOUT, mri_);
+        DOUT << '\n';
       }
     }
   }
@@ -473,15 +463,17 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
   if (interval.empty()) {
     // Get the Idx of the defining instructions.
     unsigned defIndex = getDefIndex(MIIdx);
-    unsigned ValNum;
+    VNInfo *ValNo;
     unsigned SrcReg, DstReg;
-    if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
-      ValNum = interval.getNextValue(defIndex, 0);
+    if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
+      ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
+    else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
+      ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
+                                    VNInfoAllocator);
     else
-      ValNum = interval.getNextValue(defIndex, SrcReg);
-    
-    assert(ValNum == 0 && "First value in interval is not 0?");
-    ValNum = 0;  // Clue in the optimizer.
+      ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
+
+    assert(ValNo->id == 0 && "First value in interval is not 0?");
 
     // Loop over all of the blocks that the vreg is defined in.  There are
     // two cases we have to handle here.  The most common case is a vreg
@@ -500,10 +492,10 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
       if (killIdx > defIndex) {
         assert(vi.AliveBlocks.none() &&
                "Shouldn't be alive across any blocks!");
-        LiveRange LR(defIndex, killIdx, ValNum);
+        LiveRange LR(defIndex, killIdx, ValNo);
         interval.addRange(LR);
         DOUT << " +" << LR << "\n";
-        interval.addKillForValNum(ValNum, killIdx);
+        interval.addKill(ValNo, killIdx);
         return;
       }
     }
@@ -514,7 +506,7 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
     // range that goes from this definition to the end of the defining block.
     LiveRange NewLR(defIndex,
                     getInstructionIndex(&mbb->back()) + InstrSlots::NUM,
-                    ValNum);
+                    ValNo);
     DOUT << " +" << NewLR;
     interval.addRange(NewLR);
 
@@ -527,7 +519,7 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
         if (!MBB->empty()) {
           LiveRange LR(getMBBStartIdx(i),
                        getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
-                       ValNum);
+                       ValNo);
           interval.addRange(LR);
           DOUT << " +" << LR;
         }
@@ -540,9 +532,9 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
       MachineInstr *Kill = vi.Kills[i];
       unsigned killIdx = getUseIndex(getInstructionIndex(Kill))+1;
       LiveRange LR(getMBBStartIdx(Kill->getParent()),
-                   killIdx, ValNum);
+                   killIdx, ValNo);
       interval.addRange(LR);
-      interval.addKillForValNum(ValNum, killIdx);
+      interval.addKill(ValNo, killIdx);
       DOUT << " +" << LR;
     }
 
@@ -551,7 +543,7 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
     // must be due to phi elimination or two addr elimination.  If this is
     // the result of two address elimination, then the vreg is one of the
     // def-and-use register operand.
-    if (isReDefinedByTwoAddr(mi, interval.reg, tii_)) {
+    if (mi->isRegReDefinedByTwoAddr(interval.reg)) {
       // If this is a two-address definition, then we have already processed
       // the live range.  The only problem is that we didn't realize there
       // are actually two values in the live interval.  Because of this we
@@ -561,6 +553,7 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
       unsigned RedefIndex = getDefIndex(MIIdx);
 
       const LiveRange *OldLR = interval.getLiveRangeContaining(RedefIndex-1);
+      VNInfo *OldValNo = OldLR->valno;
       unsigned OldEnd = OldLR->end;
 
       // Delete the initial value, which should be short and continuous,
@@ -573,24 +566,24 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
 
       // The new value number (#1) is defined by the instruction we claimed
       // defined value #0.
-      unsigned ValNo = interval.getNextValue(0, 0);
-      interval.copyValNumInfo(ValNo, 0);
+      VNInfo *ValNo = interval.getNextValue(0, 0, VNInfoAllocator);
+      interval.copyValNumInfo(ValNo, OldValNo);
       
       // Value#0 is now defined by the 2-addr instruction.
-      interval.setDefForValNum(0, RedefIndex);
-      interval.setSrcRegForValNum(0, 0);
+      OldValNo->def = RedefIndex;
+      OldValNo->reg = 0;
       
       // Add the new live interval which replaces the range for the input copy.
       LiveRange LR(DefIndex, RedefIndex, ValNo);
       DOUT << " replace range with " << LR;
       interval.addRange(LR);
-      interval.addKillForValNum(ValNo, RedefIndex);
-      interval.removeKillForValNum(ValNo, RedefIndex, OldEnd);
+      interval.addKill(ValNo, RedefIndex);
+      interval.removeKills(ValNo, RedefIndex, OldEnd);
 
       // If this redefinition is dead, we need to add a dummy unit live
       // range covering the def slot.
       if (lv_->RegisterDefIsDead(mi, interval.reg))
-        interval.addRange(LiveRange(RedefIndex, RedefIndex+1, 0));
+        interval.addRange(LiveRange(RedefIndex, RedefIndex+1, OldValNo));
 
       DOUT << " RESULT: ";
       interval.print(DOUT, mri_);
@@ -604,21 +597,22 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
                "PHI elimination vreg should have one kill, the PHI itself!");
 
         // Remove the old range that we now know has an incorrect number.
+        VNInfo *VNI = interval.getValNumInfo(0);
         MachineInstr *Killer = vi.Kills[0];
         unsigned Start = getMBBStartIdx(Killer->getParent());
         unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
         DOUT << " Removing [" << Start << "," << End << "] from: ";
         interval.print(DOUT, mri_); DOUT << "\n";
         interval.removeRange(Start, End);
-        interval.addKillForValNum(0, Start-1); // odd # means phi node
+        interval.addKill(VNI, Start+1); // odd # means phi node
         DOUT << " RESULT: "; interval.print(DOUT, mri_);
 
         // Replace the interval with one of a NEW value number.  Note that this
         // value number isn't actually defined by an instruction, weird huh? :)
-        LiveRange LR(Start, End, interval.getNextValue(~0, 0));
+        LiveRange LR(Start, End, interval.getNextValue(~0, 0, VNInfoAllocator));
         DOUT << " replace range with " << LR;
         interval.addRange(LR);
-        interval.addKillForValNum(LR.ValId, End);
+        interval.addKill(LR.valno, End);
         DOUT << " RESULT: "; interval.print(DOUT, mri_);
       }
 
@@ -627,17 +621,20 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
       // rest of the live range.
       unsigned defIndex = getDefIndex(MIIdx);
       
-      unsigned ValNum;
+      VNInfo *ValNo;
       unsigned SrcReg, DstReg;
-      if (!tii_->isMoveInstr(*mi, SrcReg, DstReg))
-        ValNum = interval.getNextValue(defIndex, 0);
+      if (tii_->isMoveInstr(*mi, SrcReg, DstReg))
+        ValNo = interval.getNextValue(defIndex, SrcReg, VNInfoAllocator);
+      else if (mi->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
+        ValNo = interval.getNextValue(defIndex, mi->getOperand(1).getReg(),
+                                      VNInfoAllocator);
       else
-        ValNum = interval.getNextValue(defIndex, SrcReg);
+        ValNo = interval.getNextValue(defIndex, 0, VNInfoAllocator);
       
       unsigned killIndex = getInstructionIndex(&mbb->back()) + InstrSlots::NUM;
-      LiveRange LR(defIndex, killIndex, ValNum);
+      LiveRange LR(defIndex, killIndex, ValNo);
       interval.addRange(LR);
-      interval.addKillForValNum(ValNum, killIndex-1); // odd # means phi node
+      interval.addKill(ValNo, killIndex-1); // odd # means phi node
       DOUT << " +" << LR;
     }
   }
@@ -698,11 +695,11 @@ exit:
 
   // Already exists? Extend old live interval.
   LiveInterval::iterator OldLR = interval.FindLiveRangeContaining(start);
-  unsigned Id = (OldLR != interval.end())
-    ? OldLR->ValId : interval.getNextValue(start, SrcReg);
-  LiveRange LR(start, end, Id);
+  VNInfo *ValNo = (OldLR != interval.end())
+    ? OldLR->valno : interval.getNextValue(start, SrcReg, VNInfoAllocator);
+  LiveRange LR(start, end, ValNo);
   interval.addRange(LR);
-  interval.addKillForValNum(LR.ValId, end);
+  interval.addKill(LR.valno, end);
   DOUT << " +" << LR << '\n';
 }
 
@@ -714,7 +711,9 @@ void LiveIntervals::handleRegisterDef(MachineBasicBlock *MBB,
     handleVirtualRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg));
   else if (allocatableRegs_[reg]) {
     unsigned SrcReg, DstReg;
-    if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
+    if (MI->getOpcode() == TargetInstrInfo::EXTRACT_SUBREG)
+      SrcReg = MI->getOperand(1).getReg();
+    else if (!tii_->isMoveInstr(*MI, SrcReg, DstReg))
       SrcReg = 0;
     handlePhysicalRegisterDef(MBB, MI, MIIdx, getOrCreateInterval(reg), SrcReg);
     // Def of a register also defines its sub-registers.
@@ -767,9 +766,9 @@ exit:
     }
   }
 
-  LiveRange LR(start, end, interval.getNextValue(start, 0));
+  LiveRange LR(start, end, interval.getNextValue(start, 0, VNInfoAllocator));
   interval.addRange(LR);
-  interval.addKillForValNum(LR.ValId, end);
+  interval.addKill(LR.valno, end);
   DOUT << " +" << LR << '\n';
 }
 
@@ -790,17 +789,15 @@ void LiveIntervals::computeIntervals() {
 
     MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
 
-    if (MBB->livein_begin() != MBB->livein_end()) {
-      // 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, MIIndex, getOrCreateInterval(*LI));
-        // Multiple live-ins can alias the same register.
-        for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
-          if (!hasInterval(*AS))
-            handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
-                                 true);
-      }
+    // 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, MIIndex, getOrCreateInterval(*LI));
+      // Multiple live-ins can alias the same register.
+      for (const unsigned* AS = mri_->getSubRegisters(*LI); *AS; ++AS)
+        if (!hasInterval(*AS))
+          handleLiveInRegister(MBB, MIIndex, getOrCreateInterval(*AS),
+                               true);
     }
     
     for (; MI != miEnd; ++MI) {
@@ -819,6 +816,23 @@ void LiveIntervals::computeIntervals() {
   }
 }
 
+bool LiveIntervals::findLiveInMBBs(const LiveRange &LR,
+                              SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
+  std::vector<IdxMBBPair>::const_iterator I =
+    std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);
+
+  bool ResVal = false;
+  while (I != Idx2MBBMap.end()) {
+    if (LR.end <= I->first)
+      break;
+    MBBs.push_back(I->second);
+    ResVal = true;
+    ++I;
+  }
+  return ResVal;
+}
+
+
 LiveInterval LiveIntervals::createInterval(unsigned reg) {
   float Weight = MRegisterInfo::isPhysicalRegister(reg) ?
                        HUGE_VALF : 0.0F;