Pool-allocation for MachineInstrs, MachineBasicBlocks, and
MachineMemOperands. The pools are owned by MachineFunctions.

This drastically reduces the number of calls to malloc/free made
during the "Emit" phase of scheduling, as well as later phases
in CodeGen. Combined with other changes, this speeds up the
"instruction selection" phase of CodeGen by 10% in some cases.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53212 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/BranchFolding.cpp b/lib/CodeGen/BranchFolding.cpp
index 87a77bb..f6b3d9c 100644
--- a/lib/CodeGen/BranchFolding.cpp
+++ b/lib/CodeGen/BranchFolding.cpp
@@ -127,7 +127,7 @@
   }
   
   // Remove the block.
-  MF->getBasicBlockList().erase(MBB);
+  MF->erase(MBB);
 }
 
 /// OptimizeImpDefsBlock - If a basic block is just a bunch of implicit_def
@@ -375,10 +375,12 @@
 /// iterator.  This returns the new MBB.
 MachineBasicBlock *BranchFolder::SplitMBBAt(MachineBasicBlock &CurMBB,
                                             MachineBasicBlock::iterator BBI1) {
+  MachineFunction &MF = *CurMBB.getParent();
+
   // Create the fall-through block.
   MachineFunction::iterator MBBI = &CurMBB;
-  MachineBasicBlock *NewMBB = new MachineBasicBlock(CurMBB.getBasicBlock());
-  CurMBB.getParent()->getBasicBlockList().insert(++MBBI, NewMBB);
+  MachineBasicBlock *NewMBB =MF.CreateMachineBasicBlock(CurMBB.getBasicBlock());
+  CurMBB.getParent()->insert(++MBBI, NewMBB);
 
   // Move all the successors of this block to the specified block.
   NewMBB->transferSuccessors(&CurMBB);
diff --git a/lib/CodeGen/IfConversion.cpp b/lib/CodeGen/IfConversion.cpp
index 67182ba..b59674c 100644
--- a/lib/CodeGen/IfConversion.cpp
+++ b/lib/CodeGen/IfConversion.cpp
@@ -1134,6 +1134,8 @@
 void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
                                         std::vector<MachineOperand> &Cond,
                                         bool IgnoreBr) {
+  MachineFunction &MF = *ToBBI.BB->getParent();
+
   for (MachineBasicBlock::iterator I = FromBBI.BB->begin(),
          E = FromBBI.BB->end(); I != E; ++I) {
     const TargetInstrDesc &TID = I->getDesc();
@@ -1142,7 +1144,7 @@
     if (IgnoreBr && !isPredicated && TID.isBranch())
       break;
 
-    MachineInstr *MI = I->clone();
+    MachineInstr *MI = MF.CloneMachineInstr(I);
     ToBBI.BB->insert(ToBBI.BB->end(), MI);
     ToBBI.NonPredSize++;
 
diff --git a/lib/CodeGen/LiveIntervalAnalysis.cpp b/lib/CodeGen/LiveIntervalAnalysis.cpp
index 1d161cf..7b0eaeb 100644
--- a/lib/CodeGen/LiveIntervalAnalysis.cpp
+++ b/lib/CodeGen/LiveIntervalAnalysis.cpp
@@ -73,7 +73,7 @@
   // 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() {
@@ -1562,7 +1562,7 @@
       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) {
diff --git a/lib/CodeGen/MachineBasicBlock.cpp b/lib/CodeGen/MachineBasicBlock.cpp
index 01aaba5..31e6ea8 100644
--- a/lib/CodeGen/MachineBasicBlock.cpp
+++ b/lib/CodeGen/MachineBasicBlock.cpp
@@ -18,12 +18,12 @@
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetInstrDesc.h"
 #include "llvm/Target/TargetMachine.h"
-#include "llvm/Support/LeakDetector.h"
 #include <algorithm>
 using namespace llvm;
 
-MachineBasicBlock::~MachineBasicBlock() {
-  LeakDetector::removeGarbageObject(this);
+MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb)
+  : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false) {
+  Insts.getTraits().Parent = this;
 }
 
 std::ostream& llvm::operator<<(std::ostream &OS, const MachineBasicBlock &MBB) {
@@ -38,98 +38,83 @@
 /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
 /// gets the next available unique MBB number. If it is removed from a
 /// MachineFunction, it goes back to being #-1.
-void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
-  assert(N->getParent() == 0 && "machine instruction already in a basic block");
-  N->setParent(Parent);
-  N->Number = Parent->addToMBBNumbering(N);
+void alist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
+  MachineFunction &MF = *N->getParent();
+  N->Number = MF.addToMBBNumbering(N);
 
   // Make sure the instructions have their operands in the reginfo lists.
-  MachineRegisterInfo &RegInfo = Parent->getRegInfo();
+  MachineRegisterInfo &RegInfo = MF.getRegInfo();
   for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I)
     I->AddRegOperandsToUseLists(RegInfo);
-    
-  LeakDetector::removeGarbageObject(N);
 }
 
-void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
-  assert(N->getParent() != 0 && "machine instruction not in a basic block");
+void alist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
   N->getParent()->removeFromMBBNumbering(N->Number);
   N->Number = -1;
-  N->setParent(0);
-  
-  // Make sure the instructions have their operands removed from the reginfo
-  // lists.
-  for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I)
-    I->RemoveRegOperandsFromUseLists();
-  
-  LeakDetector::addGarbageObject(N);
 }
 
 
-MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
-  MachineInstr* dummy = new MachineInstr();
-  LeakDetector::removeGarbageObject(dummy);
-  return dummy;
-}
-
 /// addNodeToList (MI) - When we add an instruction to a basic block
 /// list, we update its parent pointer and add its operands from reg use/def
 /// lists if appropriate.
-void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
+void alist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
   assert(N->getParent() == 0 && "machine instruction already in a basic block");
-  N->setParent(parent);
-  LeakDetector::removeGarbageObject(N);
+  N->setParent(Parent);
   
-  // If the block is in a function, add the instruction's register operands to
-  // their corresponding use/def lists.
-  if (MachineFunction *MF = parent->getParent())
-    N->AddRegOperandsToUseLists(MF->getRegInfo());
+  // Add the instruction's register operands to their corresponding
+  // use/def lists.
+  MachineFunction *MF = Parent->getParent();
+  N->AddRegOperandsToUseLists(MF->getRegInfo());
 }
 
 /// removeNodeFromList (MI) - When we remove an instruction from a basic block
 /// list, we update its parent pointer and remove its operands from reg use/def
 /// lists if appropriate.
-void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
+void alist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
   assert(N->getParent() != 0 && "machine instruction not in a basic block");
-  // If this block is in a function, remove from the use/def lists.
-  if (parent->getParent() != 0)
-    N->RemoveRegOperandsFromUseLists();
+
+  // Remove from the use/def lists.
+  N->RemoveRegOperandsFromUseLists();
   
   N->setParent(0);
-  LeakDetector::addGarbageObject(N);
 }
 
 /// transferNodesFromList (MI) - When moving a range of instructions from one
 /// MBB list to another, we need to update the parent pointers and the use/def
 /// lists.
-void ilist_traits<MachineInstr>::transferNodesFromList(
-      iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList,
-      ilist_iterator<MachineInstr> first,
-      ilist_iterator<MachineInstr> last) {
+void alist_traits<MachineInstr>::transferNodesFromList(
+      alist_traits<MachineInstr>& fromList,
+      MachineBasicBlock::iterator first,
+      MachineBasicBlock::iterator last) {
   // Splice within the same MBB -> no change.
-  if (parent == fromList.parent) return;
+  if (Parent == fromList.Parent) return;
 
   // If splicing between two blocks within the same function, just update the
   // parent pointers.
-  if (parent->getParent() == fromList.parent->getParent()) {
+  if (Parent->getParent() == fromList.Parent->getParent()) {
     for (; first != last; ++first)
-      first->setParent(parent);
+      first->setParent(Parent);
     return;
   }
   
   // Otherwise, we have to update the parent and the use/def lists.  The common
   // case when this occurs is if we're splicing from a block in a MF to a block
   // that is not in an MF.
-  bool HasOldMF = fromList.parent->getParent() != 0;
-  MachineFunction *NewMF = parent->getParent();
+  bool HasOldMF = fromList.Parent->getParent() != 0;
+  MachineFunction *NewMF = Parent->getParent();
   
   for (; first != last; ++first) {
     if (HasOldMF) first->RemoveRegOperandsFromUseLists();
-    first->setParent(parent);
+    first->setParent(Parent);
     if (NewMF) first->AddRegOperandsToUseLists(NewMF->getRegInfo());
   }
 }
 
+void alist_traits<MachineInstr>::deleteNode(MachineInstr* MI) {
+  assert(!MI->getParent() && "MI is still in a block!");
+  Parent->getParent()->DeleteMachineInstr(MI);
+}
+
 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
   iterator I = end();
   while (I != begin() && (--I)->getDesc().isTerminator())
@@ -211,14 +196,12 @@
 }
 
 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
-  MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
-  getParent()->getBasicBlockList().splice(NewAfter, BBList, this);
+  getParent()->splice(NewAfter, this);
 }
 
 void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
-  MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
   MachineFunction::iterator BBI = NewBefore;
-  getParent()->getBasicBlockList().splice(++BBI, BBList, this);
+  getParent()->splice(++BBI, this);
 }
 
 
@@ -271,6 +254,23 @@
   return I != Successors.end();
 }
 
+/// removeFromParent - This method unlinks 'this' from the containing function,
+/// and returns it, but does not delete it.
+MachineBasicBlock *MachineBasicBlock::removeFromParent() {
+  assert(getParent() && "Not embedded in a function!");
+  getParent()->remove(this);
+  return this;
+}
+
+
+/// eraseFromParent - This method unlinks 'this' from the containing function,
+/// and deletes it.
+void MachineBasicBlock::eraseFromParent() {
+  assert(getParent() && "Not embedded in a function!");
+  getParent()->erase(this);
+}
+
+
 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
 /// 'Old', change the code and CFG so that it branches to 'New' instead.
 void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
@@ -309,7 +309,7 @@
   bool MadeChange = false;
   bool AddedFallThrough = false;
 
-  MachineBasicBlock *FallThru = getNext();
+  MachineFunction::iterator FallThru = next(MachineFunction::iterator(this));
   
   // If this block ends with a conditional branch that falls through to its
   // successor, set DestB as the successor.
diff --git a/lib/CodeGen/MachineFunction.cpp b/lib/CodeGen/MachineFunction.cpp
index a950125..787f389 100644
--- a/lib/CodeGen/MachineFunction.cpp
+++ b/lib/CodeGen/MachineFunction.cpp
@@ -29,7 +29,6 @@
 #include "llvm/Instructions.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/GraphWriter.h"
-#include "llvm/Support/LeakDetector.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Config/config.h"
 #include <fstream>
@@ -104,30 +103,20 @@
 // MachineFunction implementation
 //===---------------------------------------------------------------------===//
 
-MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
-  MachineBasicBlock* dummy = new MachineBasicBlock();
-  LeakDetector::removeGarbageObject(dummy);
-  return dummy;
-}
-
-void ilist_traits<MachineBasicBlock>::transferNodesFromList(
-            iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
-            ilist_iterator<MachineBasicBlock> first,
-            ilist_iterator<MachineBasicBlock> last) {
-  // If splicing withing the same function, no change.
-  if (Parent == toList.Parent) return;
-  
-  for (; first != last; ++first)
-    first->setParent(toList.Parent);
+void alist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
+  MBB->getParent()->DeleteMachineBasicBlock(MBB);
 }
 
 MachineFunction::MachineFunction(const Function *F,
                                  const TargetMachine &TM)
   : Annotation(MF_AID), Fn(F), Target(TM) {
-  RegInfo = new MachineRegisterInfo(*TM.getRegisterInfo());
+  RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
+                MachineRegisterInfo(*TM.getRegisterInfo());
   MFInfo = 0;
-  FrameInfo = new MachineFrameInfo(*TM.getFrameInfo());
-  ConstantPool = new MachineConstantPool(TM.getTargetData());
+  FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
+                  MachineFrameInfo(*TM.getFrameInfo());
+  ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
+                     MachineConstantPool(TM.getTargetData());
   
   // Set up jump table.
   const TargetData &TD = *TM.getTargetData();
@@ -135,18 +124,22 @@
   unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
   unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
                              : TD.getPointerABIAlignment();
-  JumpTableInfo = new MachineJumpTableInfo(EntrySize, Alignment);
-  
-  BasicBlocks.Parent = this;
+  JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
+                      MachineJumpTableInfo(EntrySize, Alignment);
 }
 
 MachineFunction::~MachineFunction() {
   BasicBlocks.clear();
-  delete RegInfo;
-  delete MFInfo;
-  delete FrameInfo;
-  delete ConstantPool;
-  delete JumpTableInfo;
+  InstructionRecycler.clear(Allocator);
+  BasicBlockRecycler.clear(Allocator);
+  MemOperandRecycler.clear(Allocator);
+  RegInfo->~MachineRegisterInfo();        Allocator.Deallocate(RegInfo);
+  if (MFInfo) {
+    MFInfo->~MachineFunctionInfo();       Allocator.Deallocate(MFInfo);
+  }
+  FrameInfo->~MachineFrameInfo();         Allocator.Deallocate(FrameInfo);
+  ConstantPool->~MachineConstantPool();   Allocator.Deallocate(ConstantPool);
+  JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
 }
 
 
@@ -192,20 +185,88 @@
   MBBNumbering.resize(BlockNo);
 }
 
+/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
+/// of `new MachineInstr'.
+///
+MachineInstr *
+MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID, bool NoImp) {
+  return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
+             MachineInstr(TID, NoImp);
+}
 
-void MachineFunction::dump() const { print(*cerr.stream()); }
+/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
+/// 'Orig' instruction, identical in all ways except the the instruction
+/// has no parent, prev, or next.
+///
+MachineInstr *
+MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
+  return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
+             MachineInstr(*this, *Orig);
+}
+
+/// DeleteMachineInstr - Delete the given MachineInstr.
+///
+void
+MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
+  // Clear the instructions memoperands. This must be done manually because
+  // the instruction's parent pointer is now null, so it can't properly
+  // deallocate them on its own.
+  MI->clearMemOperands(*this);
+
+  MI->~MachineInstr();
+  InstructionRecycler.Deallocate(Allocator, MI);
+}
+
+/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
+/// instead of `new MachineBasicBlock'.
+///
+MachineBasicBlock *
+MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
+  return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
+             MachineBasicBlock(*this, bb);
+}
+
+/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
+///
+void
+MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
+  assert(MBB->getParent() == this && "MBB parent mismatch!");
+  MBB->~MachineBasicBlock();
+  BasicBlockRecycler.Deallocate(Allocator, MBB);
+}
+
+/// CreateMachineMemOperand - Allocate a new MachineMemOperand. Use this
+/// instead of `new MachineMemOperand'.
+///
+MachineMemOperand *
+MachineFunction::CreateMachineMemOperand(const MachineMemOperand &MMO) {
+  return new (MemOperandRecycler.Allocate<MachineMemOperand>(Allocator))
+             MachineMemOperand(MMO);
+}
+
+/// DeleteMachineMemOperand - Delete the given MachineMemOperand.
+///
+void
+MachineFunction::DeleteMachineMemOperand(MachineMemOperand *MO) {
+  MO->~MachineMemOperand();
+  MemOperandRecycler.Deallocate(Allocator, MO);
+}
+
+void MachineFunction::dump() const {
+  print(*cerr.stream());
+}
 
 void MachineFunction::print(std::ostream &OS) const {
   OS << "# Machine code for " << Fn->getName () << "():\n";
 
   // Print Frame Information
-  getFrameInfo()->print(*this, OS);
+  FrameInfo->print(*this, OS);
   
   // Print JumpTable Information
-  getJumpTableInfo()->print(OS);
+  JumpTableInfo->print(OS);
 
   // Print Constant Pool
-  getConstantPool()->print(OS);
+  ConstantPool->print(OS);
   
   const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
   
diff --git a/lib/CodeGen/MachineInstr.cpp b/lib/CodeGen/MachineInstr.cpp
index 3025af9..da8101f 100644
--- a/lib/CodeGen/MachineInstr.cpp
+++ b/lib/CodeGen/MachineInstr.cpp
@@ -22,7 +22,6 @@
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetInstrDesc.h"
 #include "llvm/Target/TargetRegisterInfo.h"
-#include "llvm/Support/LeakDetector.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/Streams.h"
 #include <ostream>
@@ -257,8 +256,6 @@
 /// TID NULL and no operands.
 MachineInstr::MachineInstr()
   : TID(0), NumImplicitOps(0), Parent(0) {
-  // Make sure that we get added to a machine basicblock
-  LeakDetector::addGarbageObject(this);
 }
 
 void MachineInstr::addImplicitDefUseOperands() {
@@ -285,8 +282,6 @@
   Operands.reserve(NumImplicitOps + TID->getNumOperands());
   if (!NoImp)
     addImplicitDefUseOperands();
-  // Make sure that we get added to a machine basicblock
-  LeakDetector::addGarbageObject(this);
 }
 
 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
@@ -304,18 +299,15 @@
       NumImplicitOps++;
   Operands.reserve(NumImplicitOps + TID->getNumOperands());
   addImplicitDefUseOperands();
-  // Make sure that we get added to a machine basicblock
-  LeakDetector::addGarbageObject(this);
   MBB->push_back(this);  // Add instruction to end of basic block!
 }
 
 /// MachineInstr ctor - Copies MachineInstr arg exactly
 ///
-MachineInstr::MachineInstr(const MachineInstr &MI) {
+MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) {
   TID = &MI.getDesc();
   NumImplicitOps = MI.NumImplicitOps;
   Operands.reserve(MI.getNumOperands());
-  MemOperands = MI.MemOperands;
 
   // Add operands
   for (unsigned i = 0; i != MI.getNumOperands(); ++i) {
@@ -323,15 +315,18 @@
     Operands.back().ParentMI = this;
   }
 
-  // Set parent, next, and prev to null
+  // Add memory operands.
+  for (alist<MachineMemOperand>::const_iterator i = MI.memoperands_begin(),
+       j = MI.memoperands_end(); i != j; ++i)
+    addMemOperand(MF, *i);
+
+  // Set parent to null.
   Parent = 0;
-  Prev = 0;
-  Next = 0;
 }
 
-
 MachineInstr::~MachineInstr() {
-  LeakDetector::removeGarbageObject(this);
+  assert(MemOperands.empty() &&
+         "MachineInstr being deleted with live memoperands!");
 #ifndef NDEBUG
   for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
     assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
@@ -499,6 +494,19 @@
   }
 }
 
+/// addMemOperand - Add a MachineMemOperand to the machine instruction,
+/// referencing arbitrary storage.
+void MachineInstr::addMemOperand(MachineFunction &MF,
+                                 const MachineMemOperand &MO) {
+  MemOperands.push_back(MF.CreateMachineMemOperand(MO));
+}
+
+/// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands.
+void MachineInstr::clearMemOperands(MachineFunction &MF) {
+  while (!MemOperands.empty())
+    MF.DeleteMachineMemOperand(MemOperands.remove(MemOperands.begin()));
+}
+
 
 /// removeFromParent - This method unlinks 'this' from the containing basic
 /// block, and returns it, but does not delete it.
@@ -509,6 +517,14 @@
 }
 
 
+/// eraseFromParent - This method unlinks 'this' from the containing basic
+/// block, and deletes it.
+void MachineInstr::eraseFromParent() {
+  assert(getParent() && "Not embedded in a basic block!");
+  getParent()->erase(this);
+}
+
+
 /// OperandComplete - Return true if it's illegal to add a new operand
 ///
 bool MachineInstr::OperandsComplete() const {
@@ -710,10 +726,11 @@
     getOperand(i).print(OS, TM);
   }
 
-  if (getNumMemOperands() > 0) {
+  if (!memoperands_empty()) {
     OS << ", Mem:";
-    for (unsigned i = 0; i < getNumMemOperands(); i++) {
-      const MachineMemOperand &MRO = getMemOperand(i);
+    for (alist<MachineMemOperand>::const_iterator i = memoperands_begin(),
+         e = memoperands_end(); i != e; ++i) {
+      const MachineMemOperand &MRO = *i;
       const Value *V = MRO.getValue();
 
       assert((MRO.isLoad() || MRO.isStore()) &&
diff --git a/lib/CodeGen/PHIElimination.cpp b/lib/CodeGen/PHIElimination.cpp
index 6dbc3dc..ceba842 100644
--- a/lib/CodeGen/PHIElimination.cpp
+++ b/lib/CodeGen/PHIElimination.cpp
@@ -354,7 +354,7 @@
   }
     
   // Really delete the PHI instruction now!
-  delete MPhi;
+  MF.DeleteMachineInstr(MPhi);
   ++NumAtomic;
 }
 
diff --git a/lib/CodeGen/SelectionDAG/ScheduleDAG.cpp b/lib/CodeGen/SelectionDAG/ScheduleDAG.cpp
index 10a5e8c..d33f3d2 100644
--- a/lib/CodeGen/SelectionDAG/ScheduleDAG.cpp
+++ b/lib/CodeGen/SelectionDAG/ScheduleDAG.cpp
@@ -670,7 +670,7 @@
 }
 
 void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MachineMemOperand &MO) {
-  MI->addMemOperand(MO);
+  MI->addMemOperand(*MF, MO);
 }
 
 /// getSubRegisterRegClass - Returns the register class of specified register
@@ -726,7 +726,7 @@
     unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
 
     // Create the extract_subreg machine instruction.
-    MachineInstr *MI = BuildMI(TII->get(TargetInstrInfo::EXTRACT_SUBREG));
+    MachineInstr *MI = BuildMI(*MF, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
 
     // Figure out the register class to create for the destreg.
     unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
@@ -772,7 +772,7 @@
     }
     
     // Create the insert_subreg or subreg_to_reg machine instruction.
-    MachineInstr *MI = BuildMI(TII->get(Opc));
+    MachineInstr *MI = BuildMI(*MF, TII->get(Opc));
     MI->addOperand(MachineOperand::CreateReg(VRBase, true));
     
     // If creating a subreg_to_reg, then the first input operand
@@ -829,7 +829,7 @@
 #endif
 
     // Create the new machine instruction.
-    MachineInstr *MI = BuildMI(II);
+    MachineInstr *MI = BuildMI(*MF, II);
     
     // Add result register values for things that are defined by this
     // instruction.
@@ -853,7 +853,7 @@
       else {
         DOUT << "Sched: COMMUTED TO: " << *NewMI;
         if (MI != NewMI) {
-          delete MI;
+          MF->DeleteMachineInstr(MI);
           MI = NewMI;
         }
         ++NumCommutes;
@@ -928,7 +928,7 @@
       --NumOps;  // Ignore the flag operand.
       
     // Create the inline asm machine instruction.
-    MachineInstr *MI = BuildMI(TII->get(TargetInstrInfo::INLINEASM));
+    MachineInstr *MI = BuildMI(*MF, TII->get(TargetInstrInfo::INLINEASM));
 
     // Add the asm string as an external symbol operand.
     const char *AsmStr =
diff --git a/lib/CodeGen/TargetInstrInfoImpl.cpp b/lib/CodeGen/TargetInstrInfoImpl.cpp
index ff9c129..4e7fec3 100644
--- a/lib/CodeGen/TargetInstrInfoImpl.cpp
+++ b/lib/CodeGen/TargetInstrInfoImpl.cpp
@@ -40,7 +40,9 @@
     // Create a new instruction.
     unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
     bool Reg0IsDead = MI->getOperand(0).isDead();
-    return BuildMI(MI->getDesc()).addReg(Reg0, true, false, false, Reg0IsDead)
+    MachineFunction &MF = *MI->getParent()->getParent();
+    return BuildMI(MF, MI->getDesc())
+      .addReg(Reg0, true, false, false, Reg0IsDead)
       .addReg(Reg2, false, false, Reg2IsKill)
       .addReg(Reg1, false, false, Reg1IsKill);
   }
@@ -104,7 +106,7 @@
                                         MachineBasicBlock::iterator I,
                                         unsigned DestReg,
                                         const MachineInstr *Orig) const {
-  MachineInstr *MI = Orig->clone();
+  MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
   MI->getOperand(0).setReg(DestReg);
   MBB.insert(I, MI);
 }
diff --git a/lib/CodeGen/VirtRegMap.cpp b/lib/CodeGen/VirtRegMap.cpp
index 30ed107..7696d55 100644
--- a/lib/CodeGen/VirtRegMap.cpp
+++ b/lib/CodeGen/VirtRegMap.cpp
@@ -604,7 +604,7 @@
     return false;
 
   bool FoundUse = false, Done = false;
-  MachineBasicBlock::iterator E = NewDef;
+  MachineBasicBlock::iterator E = &NewDef;
   ++I; ++E;
   for (; !Done && I != E; ++I) {
     MachineInstr *NMI = I;
@@ -973,7 +973,7 @@
         MBB.erase(&MI);
         return true;
       }
-      delete NewMI;
+      MF.DeleteMachineInstr(NewMI);
     }
   }
   return false;
@@ -1032,7 +1032,8 @@
     SmallVector<unsigned, 2> Ops;
     Ops.push_back(NewDstIdx);
     MachineInstr *FoldedMI = TII->foldMemoryOperand(MF, CommutedMI, Ops, SS);
-    delete CommutedMI;  // Not needed since foldMemoryOperand returns new MI.
+    // Not needed since foldMemoryOperand returns new MI.
+    MF.DeleteMachineInstr(CommutedMI);
     if (!FoldedMI)
       return false;
 
@@ -1040,7 +1041,7 @@
     VRM.virtFolded(VirtReg, FoldedMI, VirtRegMap::isRef);
     // Insert new def MI and spill MI.
     const TargetRegisterClass* RC = MF.getRegInfo().getRegClass(VirtReg);
-    TII->storeRegToStackSlot(MBB, MI, NewReg, true, SS, RC);
+    TII->storeRegToStackSlot(MBB, &MI, NewReg, true, SS, RC);
     MII = prior(MII);
     MachineInstr *StoreMI = MII;
     VRM.addSpillSlotUse(SS, StoreMI);
@@ -1341,7 +1342,7 @@
         unsigned RReg = SubIdx ? TRI->getSubReg(Phys, SubIdx) : Phys;
         MI.getOperand(i).setReg(RReg);
         if (VRM.isImplicitlyDefined(VirtReg))
-          BuildMI(MBB, MI, TII->get(TargetInstrInfo::IMPLICIT_DEF), RReg);
+          BuildMI(MBB, &MI, TII->get(TargetInstrInfo::IMPLICIT_DEF), RReg);
         continue;
       }
       
@@ -1814,7 +1815,7 @@
   ProcessNextInst:
     DistanceMap.insert(std::make_pair(&MI, Dist++));
     if (!Erased && !BackTracked) {
-      for (MachineBasicBlock::iterator II = MI; II != NextMII; ++II)
+      for (MachineBasicBlock::iterator II = &MI; II != NextMII; ++II)
         UpdateKills(*II, RegKills, KillOps);
     }
     MII = NextMII;