*** empty log message ***


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2777 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/DataStructure/DataStructure.cpp b/lib/Analysis/DataStructure/DataStructure.cpp
index 248ed91..8d53b41 100644
--- a/lib/Analysis/DataStructure/DataStructure.cpp
+++ b/lib/Analysis/DataStructure/DataStructure.cpp
@@ -6,7 +6,6 @@
 
 #include "llvm/Analysis/DataStructure.h"
 #include "llvm/Module.h"
-#include "llvm/Function.h"
 #include <fstream>
 #include <algorithm>
 
@@ -42,9 +41,9 @@
     timeval TV1, TV2;
     gettimeofday(&TV1, 0);
     for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
-      if (!(*I)->isExternal()) {
-        getDSGraph(*I);
-        getClosedDSGraph(*I);
+      if (!I->isExternal() && I->getName() == "main") {
+        //getDSGraph(*I);
+        getClosedDSGraph(I);
       }
     gettimeofday(&TV2, 0);
     cerr << "Analysis took "
@@ -53,9 +52,9 @@
   }
 
   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
-    if (!(*I)->isExternal()) {
+    if (!I->isExternal()) {
 
-      string Filename = "ds." + (*I)->getName() + ".dot";
+      string Filename = "ds." + I->getName() + ".dot";
       O << "Writing '" << Filename << "'...";
       ofstream F(Filename.c_str());
       if (F.good()) {
@@ -65,8 +64,8 @@
           << "\tsize=\"10,7.5\";\n"
           << "\trotate=\"90\";\n";
 
-        getDSGraph(*I).printFunction(F, "Local");
-        getClosedDSGraph(*I).printFunction(F, "Closed");
+        getDSGraph(I).printFunction(F, "Local");
+        getClosedDSGraph(I).printFunction(F, "Closed");
 
         F << "}\n";
       } else {
@@ -74,8 +73,8 @@
       }
 
       if (Time) 
-        O << " [" << getDSGraph(*I).getGraphSize() << ", "
-          << getClosedDSGraph(*I).getGraphSize() << "]\n";
+        O << " [" << getDSGraph(I).getGraphSize() << ", "
+          << getClosedDSGraph(I).getGraphSize() << "]\n";
       else
         O << "\n";
     }
diff --git a/lib/Analysis/DataStructure/FunctionRepBuilder.cpp b/lib/Analysis/DataStructure/FunctionRepBuilder.cpp
index 1566ca8..1282d7a 100644
--- a/lib/Analysis/DataStructure/FunctionRepBuilder.cpp
+++ b/lib/Analysis/DataStructure/FunctionRepBuilder.cpp
@@ -68,12 +68,12 @@
 // node if the call returns a pointer value.  Check to see if the call node
 // uses any global variables...
 //
-void InitVisitor::visitCallInst(CallInst *CI) {
-  CallDSNode *C = new CallDSNode(CI);
+void InitVisitor::visitCallInst(CallInst &CI) {
+  CallDSNode *C = new CallDSNode(&CI);
   Rep->CallNodes.push_back(C);
-  Rep->CallMap[CI] = C;
+  Rep->CallMap[&CI] = C;
       
-  if (PointerType *PT = dyn_cast<PointerType>(CI->getType())) {
+  if (const PointerType *PT = dyn_cast<PointerType>(CI.getType())) {
     // Create a critical shadow node to represent the memory object that the
     // return value points to...
     ShadowDSNode *Shad = new ShadowDSNode(PT->getElementType(),
@@ -86,17 +86,17 @@
     C->getLink(0).add(Shad);
 
     // The call instruction returns a pointer to the shadow block...
-    Rep->ValueMap[CI].add(Shad, CI);
+    Rep->ValueMap[&CI].add(Shad, &CI);
     
     // If the call returns a value with pointer type, add all of the users
     // of the call instruction to the work list...
-    Rep->addAllUsesToWorkList(CI);
+    Rep->addAllUsesToWorkList(&CI);
   }
 
   // Loop over all of the operands of the call instruction (except the first
   // one), to look for global variable references...
   //
-  for_each(CI->op_begin(), CI->op_end(),
+  for_each(CI.op_begin(), CI.op_end(),
            bind_obj(this, &InitVisitor::visitOperand));
 }
 
@@ -105,22 +105,22 @@
 // allocation instructions do not take pointer arguments, they cannot refer to
 // global vars...
 //
-void InitVisitor::visitAllocationInst(AllocationInst *AI) {
-  AllocDSNode *N = new AllocDSNode(AI);
+void InitVisitor::visitAllocationInst(AllocationInst &AI) {
+  AllocDSNode *N = new AllocDSNode(&AI);
   Rep->AllocNodes.push_back(N);
   
-  Rep->ValueMap[AI].add(N, AI);
+  Rep->ValueMap[&AI].add(N, &AI);
   
   // Add all of the users of the malloc instruction to the work list...
-  Rep->addAllUsesToWorkList(AI);
+  Rep->addAllUsesToWorkList(&AI);
 }
 
 
 // Visit all other instruction types.  Here we just scan, looking for uses of
 // global variables...
 //
-void InitVisitor::visitInstruction(Instruction *I) {
-  for_each(I->op_begin(), I->op_end(),
+void InitVisitor::visitInstruction(Instruction &I) {
+  for_each(I.op_begin(), I.op_end(),
            bind_obj(this, &InitVisitor::visitOperand));
 }
 
@@ -150,20 +150,18 @@
   // Add all of the arguments to the method to the graph and add all users to
   // the worklists...
   //
-  for (Function::ArgumentListType::iterator I = Func->getArgumentList().begin(),
-         E = Func->getArgumentList().end(); I != E; ++I) {
-    Value *Arg = (Value*)(*I);
+  for (Function::aiterator I = Func->abegin(), E = Func->aend(); I != E; ++I) {
     // Only process arguments that are of pointer type...
-    if (PointerType *PT = dyn_cast<PointerType>(Arg->getType())) {
+    if (const PointerType *PT = dyn_cast<PointerType>(I->getType())) {
       // Add a shadow value for it to represent what it is pointing to and add
       // this to the value map...
       ShadowDSNode *Shad = new ShadowDSNode(PT->getElementType(),
                                             Func->getParent());
       ShadowNodes.push_back(Shad);
-      ValueMap[Arg].add(PointerVal(Shad), Arg);
+      ValueMap[I].add(PointerVal(Shad), I);
       
       // Make sure that all users of the argument are processed...
-      addAllUsesToWorkList(Arg);
+      addAllUsesToWorkList(I);
     }
   }
 
@@ -179,15 +177,15 @@
 
 
 PointerVal FunctionRepBuilder::getIndexedPointerDest(const PointerVal &InP,
-                                                     const MemAccessInst *MAI) {
+                                                     const MemAccessInst &MAI) {
   unsigned Index = InP.Index;
-  const Type *SrcTy = MAI->getPointerOperand()->getType();
+  const Type *SrcTy = MAI.getPointerOperand()->getType();
 
-  for (MemAccessInst::const_op_iterator I = MAI->idx_begin(),
-         E = MAI->idx_end(); I != E; ++I)
+  for (MemAccessInst::const_op_iterator I = MAI.idx_begin(),
+         E = MAI.idx_end(); I != E; ++I)
     if ((*I)->getType() == Type::UByteTy) {     // Look for struct indices...
-      StructType *STy = cast<StructType>(SrcTy);
-      unsigned StructIdx = cast<ConstantUInt>(*I)->getValue();
+      const StructType *STy = cast<StructType>(SrcTy);
+      unsigned StructIdx = cast<ConstantUInt>(I->get())->getValue();
       for (unsigned i = 0; i != StructIdx; ++i)
         Index += countPointerFields(STy->getContainedType(i));
 
@@ -211,11 +209,11 @@
 // changing.  This means that the set of possible values for the GEP
 // needs to be expanded.
 //
-void FunctionRepBuilder::visitGetElementPtrInst(GetElementPtrInst *GEP) {
-  PointerValSet &GEPPVS = ValueMap[GEP];   // PointerValSet to expand
+void FunctionRepBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) {
+  PointerValSet &GEPPVS = ValueMap[&GEP];   // PointerValSet to expand
       
   // Get the input pointer val set...
-  const PointerValSet &SrcPVS = ValueMap[GEP->getOperand(0)];
+  const PointerValSet &SrcPVS = ValueMap[GEP.getOperand(0)];
       
   bool Changed = false;  // Process each input value... propogating it.
   for (unsigned i = 0, e = SrcPVS.size(); i != e; ++i) {
@@ -230,20 +228,20 @@
   // If our current value set changed, notify all of the users of our
   // value.
   //
-  if (Changed) addAllUsesToWorkList(GEP);        
+  if (Changed) addAllUsesToWorkList(&GEP);        
 }
 
-void FunctionRepBuilder::visitReturnInst(ReturnInst *RI) {
-  RetNode.add(ValueMap[RI->getOperand(0)]);
+void FunctionRepBuilder::visitReturnInst(ReturnInst &RI) {
+  RetNode.add(ValueMap[RI.getOperand(0)]);
 }
 
-void FunctionRepBuilder::visitLoadInst(LoadInst *LI) {
+void FunctionRepBuilder::visitLoadInst(LoadInst &LI) {
   // Only loads that return pointers are interesting...
-  const PointerType *DestTy = dyn_cast<PointerType>(LI->getType());
+  const PointerType *DestTy = dyn_cast<PointerType>(LI.getType());
   if (DestTy == 0) return;
 
-  const PointerValSet &SrcPVS = ValueMap[LI->getOperand(0)];        
-  PointerValSet &LIPVS = ValueMap[LI];
+  const PointerValSet &SrcPVS = ValueMap[LI.getOperand(0)];        
+  PointerValSet &LIPVS = ValueMap[&LI];
 
   bool Changed = false;
   for (unsigned si = 0, se = SrcPVS.size(); si != se; ++si) {
@@ -264,18 +262,18 @@
     }
   }
 
-  if (Changed) addAllUsesToWorkList(LI);
+  if (Changed) addAllUsesToWorkList(&LI);
 }
 
-void FunctionRepBuilder::visitStoreInst(StoreInst *SI) {
+void FunctionRepBuilder::visitStoreInst(StoreInst &SI) {
   // The only stores that are interesting are stores the store pointers
   // into data structures...
   //
-  if (!isa<PointerType>(SI->getOperand(0)->getType())) return;
-  if (!ValueMap.count(SI->getOperand(0))) return;  // Src scalar has no values!
+  if (!isa<PointerType>(SI.getOperand(0)->getType())) return;
+  if (!ValueMap.count(SI.getOperand(0))) return;  // Src scalar has no values!
         
-  const PointerValSet &SrcPVS = ValueMap[SI->getOperand(0)];
-  const PointerValSet &PtrPVS = ValueMap[SI->getOperand(1)];
+  const PointerValSet &SrcPVS = ValueMap[SI.getOperand(0)];
+  const PointerValSet &PtrPVS = ValueMap[SI.getOperand(1)];
 
   for (unsigned si = 0, se = SrcPVS.size(); si != se; ++si) {
     const PointerVal &SrcPtr = SrcPVS[si];
@@ -301,24 +299,24 @@
   }
 }
 
-void FunctionRepBuilder::visitCallInst(CallInst *CI) {
-  CallDSNode *DSN = CallMap[CI];
+void FunctionRepBuilder::visitCallInst(CallInst &CI) {
+  CallDSNode *DSN = CallMap[&CI];
   unsigned PtrNum = 0;
-  for (unsigned i = 0, e = CI->getNumOperands(); i != e; ++i)
-    if (isa<PointerType>(CI->getOperand(i)->getType()))
-      DSN->addArgValue(PtrNum++, ValueMap[CI->getOperand(i)]);
+  for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
+    if (isa<PointerType>(CI.getOperand(i)->getType()))
+      DSN->addArgValue(PtrNum++, ValueMap[CI.getOperand(i)]);
 }
 
-void FunctionRepBuilder::visitPHINode(PHINode *PN) {
-  assert(isa<PointerType>(PN->getType()) && "Should only update ptr phis");
+void FunctionRepBuilder::visitPHINode(PHINode &PN) {
+  assert(isa<PointerType>(PN.getType()) && "Should only update ptr phis");
 
-  PointerValSet &PN_PVS = ValueMap[PN];
+  PointerValSet &PN_PVS = ValueMap[&PN];
   bool Changed = false;
-  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
-    Changed |= PN_PVS.add(ValueMap[PN->getIncomingValue(i)],
-                          PN->getIncomingValue(i));
+  for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
+    Changed |= PN_PVS.add(ValueMap[PN.getIncomingValue(i)],
+                          PN.getIncomingValue(i));
 
-  if (Changed) addAllUsesToWorkList(PN);
+  if (Changed) addAllUsesToWorkList(&PN);
 }
 
 
diff --git a/lib/Analysis/DataStructure/FunctionRepBuilder.h b/lib/Analysis/DataStructure/FunctionRepBuilder.h
index e45ea41..9eedf02 100644
--- a/lib/Analysis/DataStructure/FunctionRepBuilder.h
+++ b/lib/Analysis/DataStructure/FunctionRepBuilder.h
@@ -27,9 +27,9 @@
 public:
   InitVisitor(FunctionRepBuilder *R, Function *F) : Rep(R), Func(F) {}
 
-  void visitCallInst(CallInst *CI);
-  void visitAllocationInst(AllocationInst *AI);
-  void visitInstruction(Instruction *I);
+  void visitCallInst(CallInst &CI);
+  void visitAllocationInst(AllocationInst &AI);
+  void visitInstruction(Instruction &I);
 
   // visitOperand - If the specified instruction operand is a global value, add
   // a node for it...
@@ -90,7 +90,7 @@
   const map<Value*, PointerValSet> &getValueMap() const { return ValueMap; }
 private:
   static PointerVal getIndexedPointerDest(const PointerVal &InP,
-                                          const MemAccessInst *MAI);
+                                          const MemAccessInst &MAI);
 
   void initializeWorkList(Function *Func);
   void processWorkList() {
@@ -101,7 +101,7 @@
       cerr << "Processing worklist inst: " << I;
 #endif
     
-      visit(I);  // Dispatch to a visitXXX function based on instruction type...
+      visit(*I); // Dispatch to a visitXXX function based on instruction type...
 #ifdef DEBUG_DATA_STRUCTURE_CONSTRUCTION
       if (I->hasName() && ValueMap.count(I)) {
         cerr << "Inst %" << I->getName() << " value is:\n";
@@ -117,18 +117,16 @@
   // Allow the visitor base class to invoke these methods...
   friend class InstVisitor<FunctionRepBuilder>;
 
-  void visitGetElementPtrInst(GetElementPtrInst *GEP);
-  void visitReturnInst(ReturnInst *RI);
-  void visitLoadInst(LoadInst *LI);
-  void visitStoreInst(StoreInst *SI);
-  void visitCallInst(CallInst *CI);
-  void visitPHINode(PHINode *PN);
-  void visitSetCondInst(SetCondInst *SCI) {}  // SetEQ & friends are ignored
-  void visitFreeInst(FreeInst *FI) {}         // Ignore free instructions
-  void visitInstruction(Instruction *I) {
-    std::cerr << "\n\n\nUNKNOWN INSTRUCTION type: ";
-    I->dump();
-    std::cerr << "\n\n\n";
+  void visitGetElementPtrInst(GetElementPtrInst &GEP);
+  void visitReturnInst(ReturnInst &RI);
+  void visitLoadInst(LoadInst &LI);
+  void visitStoreInst(StoreInst &SI);
+  void visitCallInst(CallInst &CI);
+  void visitPHINode(PHINode &PN);
+  void visitSetCondInst(SetCondInst &SCI) {}  // SetEQ & friends are ignored
+  void visitFreeInst(FreeInst &FI) {}         // Ignore free instructions
+  void visitInstruction(Instruction &I) {
+    std::cerr << "\n\n\nUNKNOWN INSTRUCTION type: " << I << "\n\n\n";
     assert(0 && "Cannot proceed");
   }
 };
diff --git a/lib/Analysis/DataStructure/NodeImpl.cpp b/lib/Analysis/DataStructure/NodeImpl.cpp
index 76b82ba..bd279b6 100644
--- a/lib/Analysis/DataStructure/NodeImpl.cpp
+++ b/lib/Analysis/DataStructure/NodeImpl.cpp
@@ -8,7 +8,6 @@
 #include "llvm/Assembly/Writer.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
-#include "llvm/BasicBlock.h"
 #include "llvm/iMemory.h"
 #include "llvm/iOther.h"
 #include "Support/STLExtras.h"
@@ -18,6 +17,7 @@
 bool AllocDSNode::isEquivalentTo(DSNode *Node) const {
   if (AllocDSNode *N = dyn_cast<AllocDSNode>(Node))
     return getType() == Node->getType();
+  //&& isAllocaNode() == N->isAllocaNode();
   return false;
 }
 
@@ -423,12 +423,11 @@
 
   // Convert over the arguments...
   Function *OF = DSG.getFunction();
-  for (Function::ArgumentListType::iterator I = OF->getArgumentList().begin(),
-         E = OF->getArgumentList().end(); I != E; ++I)
-    if (isa<PointerType>(((Value*)*I)->getType())) {
+  for (Function::aiterator I = OF->abegin(), E = OF->aend(); I != E; ++I)
+    if (isa<PointerType>(I->getType())) {
       PointerValSet ArgPVS;
-      assert(DSG.getValueMap().find((Value*)*I) != DSG.getValueMap().end());
-      MapPVS(ArgPVS, DSG.getValueMap().find((Value*)*I)->second, NodeMap);
+      assert(DSG.getValueMap().find(I) != DSG.getValueMap().end());
+      MapPVS(ArgPVS, DSG.getValueMap().find(I)->second, NodeMap);
       assert(!ArgPVS.empty() && "Argument has no links!");
       Args.push_back(ArgPVS);
     }
diff --git a/lib/Analysis/LiveVar/BBLiveVar.cpp b/lib/Analysis/LiveVar/BBLiveVar.cpp
index 7d735b7..eb58671 100644
--- a/lib/Analysis/LiveVar/BBLiveVar.cpp
+++ b/lib/Analysis/LiveVar/BBLiveVar.cpp
@@ -18,23 +18,23 @@
 
 static AnnotationID AID(AnnotationManager::getID("Analysis::BBLiveVar"));
 
-BBLiveVar *BBLiveVar::CreateOnBB(const BasicBlock *BB, unsigned POID) {
+BBLiveVar *BBLiveVar::CreateOnBB(const BasicBlock &BB, unsigned POID) {
   BBLiveVar *Result = new BBLiveVar(BB, POID);
-  BB->addAnnotation(Result);
+  BB.addAnnotation(Result);
   return Result;
 }
 
-BBLiveVar *BBLiveVar::GetFromBB(const BasicBlock *BB) {
-  return (BBLiveVar*)BB->getAnnotation(AID);
+BBLiveVar *BBLiveVar::GetFromBB(const BasicBlock &BB) {
+  return (BBLiveVar*)BB.getAnnotation(AID);
 }
 
-void BBLiveVar::RemoveFromBB(const BasicBlock *BB) {
-  bool Deleted = BB->deleteAnnotation(AID);
+void BBLiveVar::RemoveFromBB(const BasicBlock &BB) {
+  bool Deleted = BB.deleteAnnotation(AID);
   assert(Deleted && "BBLiveVar annotation did not exist!");
 }
 
 
-BBLiveVar::BBLiveVar(const BasicBlock *bb, unsigned id)
+BBLiveVar::BBLiveVar(const BasicBlock &bb, unsigned id)
   : Annotation(AID), BB(bb), POID(id) {
   InSetChanged = OutSetChanged = false;
 
@@ -50,7 +50,7 @@
 
 void BBLiveVar::calcDefUseSets() {
   // get the iterator for machine instructions
-  const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
+  const MachineCodeForBasicBlock &MIVec = BB.getMachineInstrVec();
 
   // iterate over all the machine instructions in BB
   for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
@@ -129,7 +129,7 @@
 //-----------------------------------------------------------------------------
 // To add an operand which is a def
 //-----------------------------------------------------------------------------
-void  BBLiveVar::addDef(const Value *Op) {
+void BBLiveVar::addDef(const Value *Op) {
   DefSet.insert(Op);     // operand is a def - so add to def set
   InSet.erase(Op);       // this definition kills any later uses
   InSetChanged = true; 
@@ -211,9 +211,9 @@
   //
   bool needAnotherIt = false;  
 
-  for (pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
+  for (pred_const_iterator PI = pred_begin(&BB), PE = pred_end(&BB);
        PI != PE ; ++PI) {
-    BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(*PI);
+    BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(**PI);
 
     // do set union
     if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {  
diff --git a/lib/Analysis/LiveVar/BBLiveVar.h b/lib/Analysis/LiveVar/BBLiveVar.h
index 79cf7dc..0eed337 100644
--- a/lib/Analysis/LiveVar/BBLiveVar.h
+++ b/lib/Analysis/LiveVar/BBLiveVar.h
@@ -24,7 +24,7 @@
 extern LiveVarDebugLevel_t DEBUG_LV;
 
 class BBLiveVar : public Annotation {
-  const BasicBlock *BB;         // pointer to BasicBlock
+  const BasicBlock &BB;         // pointer to BasicBlock
   unsigned POID;                // Post-Order ID
 
   ValueSet DefSet;              // Def set (with no preceding uses) for LV analysis
@@ -49,12 +49,12 @@
 
   void calcDefUseSets();         // calculates the Def & Use sets for this BB
 
-  BBLiveVar(const BasicBlock *BB, unsigned POID);
+  BBLiveVar(const BasicBlock &BB, unsigned POID);
   ~BBLiveVar() {}                // make dtor private
  public:
-  static BBLiveVar *CreateOnBB(const BasicBlock *BB, unsigned POID);
-  static BBLiveVar *GetFromBB(const BasicBlock *BB);
-  static void RemoveFromBB(const BasicBlock *BB);
+  static BBLiveVar *CreateOnBB(const BasicBlock &BB, unsigned POID);
+  static BBLiveVar *GetFromBB(const BasicBlock &BB);
+  static void RemoveFromBB(const BasicBlock &BB);
 
   inline bool isInSetChanged() const  { return InSetChanged; }    
   inline bool isOutSetChanged() const { return OutSetChanged; }
diff --git a/lib/Analysis/LiveVar/FunctionLiveVarInfo.cpp b/lib/Analysis/LiveVar/FunctionLiveVarInfo.cpp
index 1105640..ab0a761 100644
--- a/lib/Analysis/LiveVar/FunctionLiveVarInfo.cpp
+++ b/lib/Analysis/LiveVar/FunctionLiveVarInfo.cpp
@@ -33,12 +33,12 @@
 
 // gets OutSet of a BB
 const ValueSet &FunctionLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
-  return BBLiveVar::GetFromBB(BB)->getOutSet();
+  return BBLiveVar::GetFromBB(*BB)->getOutSet();
 }
 
 // gets InSet of a BB
 const ValueSet &FunctionLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
-  return BBLiveVar::GetFromBB(BB)->getInSet();
+  return BBLiveVar::GetFromBB(*BB)->getInSet();
 }
 
 
@@ -46,15 +46,15 @@
 // Performs live var analysis for a function
 //-----------------------------------------------------------------------------
 
-bool FunctionLiveVarInfo::runOnFunction(Function *Meth) {
-  M = Meth;
+bool FunctionLiveVarInfo::runOnFunction(Function &F) {
+  M = &F;
   if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
 
   // create and initialize all the BBLiveVars of the CFG
-  constructBBs(Meth);
+  constructBBs(M);
 
   unsigned int iter=0;
-  while (doSingleBackwardPass(Meth, iter++))
+  while (doSingleBackwardPass(M, iter++))
     ; // Iterate until we are done.
   
   if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
@@ -71,7 +71,7 @@
   
   for(po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
       BBI != BBE; ++BBI, ++POId) { 
-    const BasicBlock *BB = *BBI;        // get the current BB 
+    const BasicBlock &BB = **BBI;        // get the current BB 
 
     if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
 
@@ -105,7 +105,7 @@
   bool NeedAnotherIteration = false;
   for (po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
        BBI != BBE; ++BBI) {
-    BBLiveVar *LVBB = BBLiveVar::GetFromBB(*BBI);
+    BBLiveVar *LVBB = BBLiveVar::GetFromBB(**BBI);
     assert(LVBB && "BasicBlock information not set for block!");
 
     if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
diff --git a/lib/Analysis/LiveVar/ValueSet.cpp b/lib/Analysis/LiveVar/ValueSet.cpp
index 82fccb7..1914f03 100644
--- a/lib/Analysis/LiveVar/ValueSet.cpp
+++ b/lib/Analysis/LiveVar/ValueSet.cpp
@@ -6,13 +6,13 @@
 #include <iostream>
 
 std::ostream &operator<<(std::ostream &O, RAV V) { // func to print a Value 
-  const Value *v = V.V;
-  if (v->hasName())
-    return O << (void*)v << "(" << v->getName() << ") ";
+  const Value &v = V.V;
+  if (v.hasName())
+    return O << (void*)&v << "(" << v.getName() << ") ";
   else if (isa<Constant>(v))
-    return O << (void*)v << "(" << v << ") ";
+    return O << (void*)&v << "(" << v << ") ";
   else
-    return O << (void*)v << " ";
+    return O << (void*)&v << " ";
 }
 
 void printSet(const ValueSet &S) {
diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp
index 7d43047..fb83e02 100644
--- a/lib/Linker/LinkModules.cpp
+++ b/lib/Linker/LinkModules.cpp
@@ -96,20 +96,20 @@
   }
 
   // Check to see if it's a constant that we are interesting in transforming...
-  if (Constant *CPV = dyn_cast<Constant>(In)) {
+  if (const Constant *CPV = dyn_cast<Constant>(In)) {
     if (!isa<DerivedType>(CPV->getType()))
-      return CPV;              // Simple constants stay identical...
+      return const_cast<Constant*>(CPV);   // Simple constants stay identical...
 
     Constant *Result = 0;
 
-    if (ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
+    if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
       const std::vector<Use> &Ops = CPA->getValues();
       std::vector<Constant*> Operands(Ops.size());
       for (unsigned i = 0; i < Ops.size(); ++i)
         Operands[i] = 
           cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
       Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
-    } else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
+    } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
       const std::vector<Use> &Ops = CPS->getValues();
       std::vector<Constant*> Operands(Ops.size());
       for (unsigned i = 0; i < Ops.size(); ++i)
@@ -117,8 +117,9 @@
           cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
       Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
     } else if (isa<ConstantPointerNull>(CPV)) {
-      Result = CPV;
-    } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
+      Result = const_cast<Constant*>(CPV);
+    } else if (const ConstantPointerRef *CPR =
+                      dyn_cast<ConstantPointerRef>(CPV)) {
       Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
       Result = ConstantPointerRef::get(cast<GlobalValue>(V));
     } else {
@@ -126,7 +127,7 @@
     }
 
     // Cache the mapping in our local map structure...
-    LocalMap.insert(std::make_pair(In, CPV));
+    LocalMap.insert(std::make_pair(In, const_cast<Constant*>(CPV)));
     return Result;
   }
 
@@ -158,7 +159,7 @@
   // Loop over all of the globals in the src module, mapping them over as we go
   //
   for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
-    const GlobalVariable *SGV = *I;
+    const GlobalVariable *SGV = I;
     Value *V;
 
     // If the global variable has a name, and that name is already in use in the
@@ -211,7 +212,7 @@
   // Loop over all of the globals in the src module, mapping them over as we go
   //
   for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
-    const GlobalVariable *SGV = *I;
+    const GlobalVariable *SGV = I;
 
     if (SGV->hasInitializer()) {      // Only process initialized GV's
       // Figure out what the initializer looks like in the dest module...
@@ -249,41 +250,41 @@
   // go
   //
   for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
-    const Function *SM = *I;   // SrcFunction
+    const Function *SF = I;   // SrcFunction
     Value *V;
 
     // If the function has a name, and that name is already in use in the Dest
     // module, make sure that the name is a compatible function...
     //
-    if (SM->hasExternalLinkage() && SM->hasName() &&
-	(V = ST->lookup(SM->getType(), SM->getName())) &&
+    if (SF->hasExternalLinkage() && SF->hasName() &&
+	(V = ST->lookup(SF->getType(), SF->getName())) &&
 	cast<Function>(V)->hasExternalLinkage()) {
       // The same named thing is a Function, because the only two things
       // that may be in a module level symbol table are Global Vars and
       // Functions, and they both have distinct, nonoverlapping, possible types.
       // 
-      Function *DM = cast<Function>(V);   // DestFunction
+      Function *DF = cast<Function>(V);   // DestFunction
 
       // Check to make sure the function is not defined in both modules...
-      if (!SM->isExternal() && !DM->isExternal())
+      if (!SF->isExternal() && !DF->isExternal())
         return Error(Err, "Function '" + 
-                     SM->getFunctionType()->getDescription() + "':\"" + 
-                     SM->getName() + "\" - Function is already defined!");
+                     SF->getFunctionType()->getDescription() + "':\"" + 
+                     SF->getName() + "\" - Function is already defined!");
 
       // Otherwise, just remember this mapping...
-      ValueMap.insert(std::make_pair(SM, DM));
+      ValueMap.insert(std::make_pair(SF, DF));
     } else {
       // Function does not already exist, simply insert an external function
-      // signature identical to SM into the dest module...
-      Function *DM = new Function(SM->getFunctionType(),
-                                  SM->hasInternalLinkage(),
-                                  SM->getName());
+      // signature identical to SF into the dest module...
+      Function *DF = new Function(SF->getFunctionType(),
+                                  SF->hasInternalLinkage(),
+                                  SF->getName());
 
       // Add the function signature to the dest module...
-      Dest->getFunctionList().push_back(DM);
+      Dest->getFunctionList().push_back(DF);
 
       // ... and remember this mapping...
-      ValueMap.insert(std::make_pair(SM, DM));
+      ValueMap.insert(std::make_pair(SF, DF));
     }
   }
   return false;
@@ -300,27 +301,22 @@
   map<const Value*, Value*> LocalMap;   // Map for function local values
 
   // Go through and convert function arguments over...
-  for (Function::ArgumentListType::const_iterator 
-         I = Src->getArgumentList().begin(),
-         E = Src->getArgumentList().end(); I != E; ++I) {
-    const Argument *SMA = *I;
-
+  for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
+       I != E; ++I) {
     // Create the new function argument and add to the dest function...
-    Argument *DMA = new Argument(SMA->getType(), SMA->getName());
-    Dest->getArgumentList().push_back(DMA);
+    Argument *DFA = new Argument(I->getType(), I->getName());
+    Dest->getArgumentList().push_back(DFA);
 
     // Add a mapping to our local map
-    LocalMap.insert(std::make_pair(SMA, DMA));
+    LocalMap.insert(std::make_pair(I, DFA));
   }
 
   // Loop over all of the basic blocks, copying the instructions over...
   //
   for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
-    const BasicBlock *SBB = *I;
-
     // Create new basic block and add to mapping and the Dest function...
-    BasicBlock *DBB = new BasicBlock(SBB->getName(), Dest);
-    LocalMap.insert(std::make_pair(SBB, DBB));
+    BasicBlock *DBB = new BasicBlock(I->getName(), Dest);
+    LocalMap.insert(std::make_pair(I, DBB));
 
     // Loop over all of the instructions in the src basic block, copying them
     // over.  Note that this is broken in a strict sense because the cloned
@@ -328,13 +324,12 @@
     // the remapped values.  In our case, however, we will not get caught and 
     // so we can delay patching the values up until later...
     //
-    for (BasicBlock::const_iterator II = SBB->begin(), IE = SBB->end(); 
+    for (BasicBlock::const_iterator II = I->begin(), IE = I->end(); 
          II != IE; ++II) {
-      const Instruction *SI = *II;
-      Instruction *DI = SI->clone();
-      DI->setName(SI->getName());
+      Instruction *DI = II->clone();
+      DI->setName(II->getName());
       DBB->getInstList().push_back(DI);
-      LocalMap.insert(std::make_pair(SI, DI));
+      LocalMap.insert(std::make_pair(II, DI));
     }
   }
 
@@ -343,17 +338,11 @@
   // the Source function as operands.  Loop through all of the operands of the
   // functions and patch them up to point to the local versions...
   //
-  for (Function::iterator BI = Dest->begin(), BE = Dest->end();
-       BI != BE; ++BI) {
-    BasicBlock *BB = *BI;
-    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
-      Instruction *Inst = *I;
-      
-      for (Instruction::op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
+  for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
+    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
+      for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
            OI != OE; ++OI)
         *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
-    }
-  }
 
   return false;
 }
@@ -370,20 +359,19 @@
   // Loop over all of the functions in the src module, mapping them over as we
   // go
   //
-  for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
-    const Function *SM = *I;                  // Source Function
-    if (!SM->isExternal()) {                  // No body if function is external
-      Function *DM = cast<Function>(ValueMap[SM]); // Destination function
+  for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){
+    if (!SF->isExternal()) {                  // No body if function is external
+      Function *DF = cast<Function>(ValueMap[SF]); // Destination function
 
-      // DM not external SM external?
-      if (!DM->isExternal()) {
+      // DF not external SF external?
+      if (!DF->isExternal()) {
         if (Err)
-          *Err = "Function '" + (SM->hasName() ? SM->getName() : string("")) +
+          *Err = "Function '" + (SF->hasName() ? SF->getName() : string("")) +
                  "' body multiply defined!";
         return true;
       }
 
-      if (LinkFunctionBody(DM, SM, ValueMap, Err)) return true;
+      if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;
     }
   }
   return false;
diff --git a/lib/Target/SparcV9/LiveVar/BBLiveVar.cpp b/lib/Target/SparcV9/LiveVar/BBLiveVar.cpp
index 7d735b7..eb58671 100644
--- a/lib/Target/SparcV9/LiveVar/BBLiveVar.cpp
+++ b/lib/Target/SparcV9/LiveVar/BBLiveVar.cpp
@@ -18,23 +18,23 @@
 
 static AnnotationID AID(AnnotationManager::getID("Analysis::BBLiveVar"));
 
-BBLiveVar *BBLiveVar::CreateOnBB(const BasicBlock *BB, unsigned POID) {
+BBLiveVar *BBLiveVar::CreateOnBB(const BasicBlock &BB, unsigned POID) {
   BBLiveVar *Result = new BBLiveVar(BB, POID);
-  BB->addAnnotation(Result);
+  BB.addAnnotation(Result);
   return Result;
 }
 
-BBLiveVar *BBLiveVar::GetFromBB(const BasicBlock *BB) {
-  return (BBLiveVar*)BB->getAnnotation(AID);
+BBLiveVar *BBLiveVar::GetFromBB(const BasicBlock &BB) {
+  return (BBLiveVar*)BB.getAnnotation(AID);
 }
 
-void BBLiveVar::RemoveFromBB(const BasicBlock *BB) {
-  bool Deleted = BB->deleteAnnotation(AID);
+void BBLiveVar::RemoveFromBB(const BasicBlock &BB) {
+  bool Deleted = BB.deleteAnnotation(AID);
   assert(Deleted && "BBLiveVar annotation did not exist!");
 }
 
 
-BBLiveVar::BBLiveVar(const BasicBlock *bb, unsigned id)
+BBLiveVar::BBLiveVar(const BasicBlock &bb, unsigned id)
   : Annotation(AID), BB(bb), POID(id) {
   InSetChanged = OutSetChanged = false;
 
@@ -50,7 +50,7 @@
 
 void BBLiveVar::calcDefUseSets() {
   // get the iterator for machine instructions
-  const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
+  const MachineCodeForBasicBlock &MIVec = BB.getMachineInstrVec();
 
   // iterate over all the machine instructions in BB
   for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
@@ -129,7 +129,7 @@
 //-----------------------------------------------------------------------------
 // To add an operand which is a def
 //-----------------------------------------------------------------------------
-void  BBLiveVar::addDef(const Value *Op) {
+void BBLiveVar::addDef(const Value *Op) {
   DefSet.insert(Op);     // operand is a def - so add to def set
   InSet.erase(Op);       // this definition kills any later uses
   InSetChanged = true; 
@@ -211,9 +211,9 @@
   //
   bool needAnotherIt = false;  
 
-  for (pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
+  for (pred_const_iterator PI = pred_begin(&BB), PE = pred_end(&BB);
        PI != PE ; ++PI) {
-    BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(*PI);
+    BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(**PI);
 
     // do set union
     if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {  
diff --git a/lib/Target/SparcV9/LiveVar/BBLiveVar.h b/lib/Target/SparcV9/LiveVar/BBLiveVar.h
index 79cf7dc..0eed337 100644
--- a/lib/Target/SparcV9/LiveVar/BBLiveVar.h
+++ b/lib/Target/SparcV9/LiveVar/BBLiveVar.h
@@ -24,7 +24,7 @@
 extern LiveVarDebugLevel_t DEBUG_LV;
 
 class BBLiveVar : public Annotation {
-  const BasicBlock *BB;         // pointer to BasicBlock
+  const BasicBlock &BB;         // pointer to BasicBlock
   unsigned POID;                // Post-Order ID
 
   ValueSet DefSet;              // Def set (with no preceding uses) for LV analysis
@@ -49,12 +49,12 @@
 
   void calcDefUseSets();         // calculates the Def & Use sets for this BB
 
-  BBLiveVar(const BasicBlock *BB, unsigned POID);
+  BBLiveVar(const BasicBlock &BB, unsigned POID);
   ~BBLiveVar() {}                // make dtor private
  public:
-  static BBLiveVar *CreateOnBB(const BasicBlock *BB, unsigned POID);
-  static BBLiveVar *GetFromBB(const BasicBlock *BB);
-  static void RemoveFromBB(const BasicBlock *BB);
+  static BBLiveVar *CreateOnBB(const BasicBlock &BB, unsigned POID);
+  static BBLiveVar *GetFromBB(const BasicBlock &BB);
+  static void RemoveFromBB(const BasicBlock &BB);
 
   inline bool isInSetChanged() const  { return InSetChanged; }    
   inline bool isOutSetChanged() const { return OutSetChanged; }
diff --git a/lib/Target/SparcV9/LiveVar/FunctionLiveVarInfo.cpp b/lib/Target/SparcV9/LiveVar/FunctionLiveVarInfo.cpp
index 1105640..ab0a761 100644
--- a/lib/Target/SparcV9/LiveVar/FunctionLiveVarInfo.cpp
+++ b/lib/Target/SparcV9/LiveVar/FunctionLiveVarInfo.cpp
@@ -33,12 +33,12 @@
 
 // gets OutSet of a BB
 const ValueSet &FunctionLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
-  return BBLiveVar::GetFromBB(BB)->getOutSet();
+  return BBLiveVar::GetFromBB(*BB)->getOutSet();
 }
 
 // gets InSet of a BB
 const ValueSet &FunctionLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
-  return BBLiveVar::GetFromBB(BB)->getInSet();
+  return BBLiveVar::GetFromBB(*BB)->getInSet();
 }
 
 
@@ -46,15 +46,15 @@
 // Performs live var analysis for a function
 //-----------------------------------------------------------------------------
 
-bool FunctionLiveVarInfo::runOnFunction(Function *Meth) {
-  M = Meth;
+bool FunctionLiveVarInfo::runOnFunction(Function &F) {
+  M = &F;
   if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
 
   // create and initialize all the BBLiveVars of the CFG
-  constructBBs(Meth);
+  constructBBs(M);
 
   unsigned int iter=0;
-  while (doSingleBackwardPass(Meth, iter++))
+  while (doSingleBackwardPass(M, iter++))
     ; // Iterate until we are done.
   
   if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
@@ -71,7 +71,7 @@
   
   for(po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
       BBI != BBE; ++BBI, ++POId) { 
-    const BasicBlock *BB = *BBI;        // get the current BB 
+    const BasicBlock &BB = **BBI;        // get the current BB 
 
     if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
 
@@ -105,7 +105,7 @@
   bool NeedAnotherIteration = false;
   for (po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
        BBI != BBE; ++BBI) {
-    BBLiveVar *LVBB = BBLiveVar::GetFromBB(*BBI);
+    BBLiveVar *LVBB = BBLiveVar::GetFromBB(**BBI);
     assert(LVBB && "BasicBlock information not set for block!");
 
     if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
diff --git a/lib/Target/SparcV9/LiveVar/ValueSet.cpp b/lib/Target/SparcV9/LiveVar/ValueSet.cpp
index 82fccb7..1914f03 100644
--- a/lib/Target/SparcV9/LiveVar/ValueSet.cpp
+++ b/lib/Target/SparcV9/LiveVar/ValueSet.cpp
@@ -6,13 +6,13 @@
 #include <iostream>
 
 std::ostream &operator<<(std::ostream &O, RAV V) { // func to print a Value 
-  const Value *v = V.V;
-  if (v->hasName())
-    return O << (void*)v << "(" << v->getName() << ") ";
+  const Value &v = V.V;
+  if (v.hasName())
+    return O << (void*)&v << "(" << v.getName() << ") ";
   else if (isa<Constant>(v))
-    return O << (void*)v << "(" << v << ") ";
+    return O << (void*)&v << "(" << v << ") ";
   else
-    return O << (void*)v << " ";
+    return O << (void*)&v << " ";
 }
 
 void printSet(const ValueSet &S) {
diff --git a/lib/Transforms/Utils/BasicBlockUtils.cpp b/lib/Transforms/Utils/BasicBlockUtils.cpp
index 89078a1..aea0b94 100644
--- a/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -15,19 +15,18 @@
 //
 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
                           BasicBlock::iterator &BI, Value *V) {
-  Instruction *I = *BI;
+  Instruction &I = *BI;
   // Replaces all of the uses of the instruction with uses of the value
-  I->replaceAllUsesWith(V);
+  I.replaceAllUsesWith(V);
 
-  // Remove the unneccesary instruction now...
-  BIL.remove(BI);
+  std::string OldName = I.getName();
+  
+  // Delete the unneccesary instruction now...
+  BI = BIL.erase(BI);
 
   // Make sure to propogate a name if there is one already...
-  if (I->hasName() && !V->hasName())
-    V->setName(I->getName(), BIL.getParent()->getSymbolTable());
-
-  // Remove the dead instruction now...
-  delete I;
+  if (OldName.size() && !V->hasName())
+    V->setName(OldName, BIL.getParent()->getSymbolTable());
 }
 
 
@@ -41,13 +40,13 @@
          "ReplaceInstWithInst: Instruction already inserted into basic block!");
 
   // Insert the new instruction into the basic block...
-  BI = BIL.insert(BI, I)+1;  // Increment BI to point to instruction to delete
+  BasicBlock::iterator New = BIL.insert(BI, I);
 
   // Replace all uses of the old instruction, and delete it.
   ReplaceInstWithValue(BIL, BI, I);
 
   // Move BI back to point to the newly inserted instruction
-  --BI;
+  BI = New;
 }
 
 // ReplaceInstWithInst - Replace the instruction specified by From with the
@@ -56,24 +55,6 @@
 // for the instruction.
 //
 void ReplaceInstWithInst(Instruction *From, Instruction *To) {
-  BasicBlock *BB = From->getParent();
-  BasicBlock::InstListType &BIL = BB->getInstList();
-  BasicBlock::iterator BI = find(BIL.begin(), BIL.end(), From);
-  assert(BI != BIL.end() && "Inst not in it's parents BB!");
-  ReplaceInstWithInst(BIL, BI, To);
+  BasicBlock::iterator BI(From);
+  ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
 }
-
-// InsertInstBeforeInst - Insert 'NewInst' into the basic block that 'Existing'
-// is already in, and put it right before 'Existing'.  This instruction should
-// only be used when there is no iterator to Existing already around.  The 
-// returned iterator points to the new instruction.
-//
-BasicBlock::iterator InsertInstBeforeInst(Instruction *NewInst,
-                                          Instruction *Existing) {
-  BasicBlock *BB = Existing->getParent();
-  BasicBlock::InstListType &BIL = BB->getInstList();
-  BasicBlock::iterator BI = find(BIL.begin(), BIL.end(), Existing);
-  assert(BI != BIL.end() && "Inst not in it's parents BB!");
-  return BIL.insert(BI, NewInst);
-}
-
diff --git a/lib/Transforms/Utils/CloneFunction.cpp b/lib/Transforms/Utils/CloneFunction.cpp
index 6b1b24a..6bf7553 100644
--- a/lib/Transforms/Utils/CloneFunction.cpp
+++ b/lib/Transforms/Utils/CloneFunction.cpp
@@ -36,10 +36,9 @@
 //
 void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
                        const std::vector<Value*> &ArgMap) {
-  assert(OldFunc->getArgumentList().empty() ||
-         !NewFunc->getArgumentList().empty() &&
+  assert(OldFunc->aempty() || !NewFunc->aempty() &&
          "Synthesization of arguments is not implemented yet!");
-  assert(OldFunc->getArgumentList().size() == ArgMap.size() &&
+  assert(OldFunc->asize() == ArgMap.size() &&
          "Improper number of argument values to map specified!");
   
   // Keep a mapping between the original function's values and the new
@@ -49,8 +48,10 @@
   std::map<const Value *, Value*> ValueMap;
 
   // Add all of the function arguments to the mapping...
-  for (unsigned i = 0, e = ArgMap.size(); i != e; ++i)
-    ValueMap[(Value*)OldFunc->getArgumentList()[i]] = ArgMap[i];
+  unsigned i = 0;
+  for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
+       I != E; ++I, ++i)
+    ValueMap[I] = ArgMap[i];
 
 
   // Loop over all of the basic blocks in the function, cloning them as
@@ -58,33 +59,32 @@
   //
   for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
        BI != BE; ++BI) {
-    const BasicBlock *BB = *BI;
-    assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
+    const BasicBlock &BB = *BI;
+    assert(BB.getTerminator() && "BasicBlock doesn't have terminator!?!?");
     
     // Create a new basic block to copy instructions into!
-    BasicBlock *CBB = new BasicBlock(BB->getName(), NewFunc);
-    ValueMap[BB] = CBB;                       // Add basic block mapping.
+    BasicBlock *CBB = new BasicBlock(BB.getName(), NewFunc);
+    ValueMap[&BB] = CBB;                       // Add basic block mapping.
 
     // Loop over all instructions copying them over...
-    for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
+    for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
          II != IE; ++II) {
-      Instruction *NewInst = (*II)->clone();
-      NewInst->setName((*II)->getName());       // Name is not cloned...
+      Instruction *NewInst = II->clone();
+      NewInst->setName(II->getName());       // Name is not cloned...
       CBB->getInstList().push_back(NewInst);
-      ValueMap[*II] = NewInst;                  // Add instruction map to value.
+      ValueMap[II] = NewInst;                // Add instruction map to value.
     }
   }
 
   // Loop over all of the instructions in the function, fixing up operand 
   // references as we go.  This uses ValueMap to do all the hard work.
   //
-  for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
-       BI != BE; ++BI) {
-    const BasicBlock *BB = *BI;
+  for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
+       BB != BE; ++BB) {
     BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
     
     // Loop over all instructions, fixing each one as we find it...
-    for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
-      RemapInstruction(*II, ValueMap);
+    for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
+      RemapInstruction(II, ValueMap);
   }
 }
diff --git a/lib/Transforms/Utils/Linker.cpp b/lib/Transforms/Utils/Linker.cpp
index 7d43047..fb83e02 100644
--- a/lib/Transforms/Utils/Linker.cpp
+++ b/lib/Transforms/Utils/Linker.cpp
@@ -96,20 +96,20 @@
   }
 
   // Check to see if it's a constant that we are interesting in transforming...
-  if (Constant *CPV = dyn_cast<Constant>(In)) {
+  if (const Constant *CPV = dyn_cast<Constant>(In)) {
     if (!isa<DerivedType>(CPV->getType()))
-      return CPV;              // Simple constants stay identical...
+      return const_cast<Constant*>(CPV);   // Simple constants stay identical...
 
     Constant *Result = 0;
 
-    if (ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
+    if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
       const std::vector<Use> &Ops = CPA->getValues();
       std::vector<Constant*> Operands(Ops.size());
       for (unsigned i = 0; i < Ops.size(); ++i)
         Operands[i] = 
           cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
       Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
-    } else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
+    } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
       const std::vector<Use> &Ops = CPS->getValues();
       std::vector<Constant*> Operands(Ops.size());
       for (unsigned i = 0; i < Ops.size(); ++i)
@@ -117,8 +117,9 @@
           cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
       Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
     } else if (isa<ConstantPointerNull>(CPV)) {
-      Result = CPV;
-    } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
+      Result = const_cast<Constant*>(CPV);
+    } else if (const ConstantPointerRef *CPR =
+                      dyn_cast<ConstantPointerRef>(CPV)) {
       Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
       Result = ConstantPointerRef::get(cast<GlobalValue>(V));
     } else {
@@ -126,7 +127,7 @@
     }
 
     // Cache the mapping in our local map structure...
-    LocalMap.insert(std::make_pair(In, CPV));
+    LocalMap.insert(std::make_pair(In, const_cast<Constant*>(CPV)));
     return Result;
   }
 
@@ -158,7 +159,7 @@
   // Loop over all of the globals in the src module, mapping them over as we go
   //
   for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
-    const GlobalVariable *SGV = *I;
+    const GlobalVariable *SGV = I;
     Value *V;
 
     // If the global variable has a name, and that name is already in use in the
@@ -211,7 +212,7 @@
   // Loop over all of the globals in the src module, mapping them over as we go
   //
   for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
-    const GlobalVariable *SGV = *I;
+    const GlobalVariable *SGV = I;
 
     if (SGV->hasInitializer()) {      // Only process initialized GV's
       // Figure out what the initializer looks like in the dest module...
@@ -249,41 +250,41 @@
   // go
   //
   for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
-    const Function *SM = *I;   // SrcFunction
+    const Function *SF = I;   // SrcFunction
     Value *V;
 
     // If the function has a name, and that name is already in use in the Dest
     // module, make sure that the name is a compatible function...
     //
-    if (SM->hasExternalLinkage() && SM->hasName() &&
-	(V = ST->lookup(SM->getType(), SM->getName())) &&
+    if (SF->hasExternalLinkage() && SF->hasName() &&
+	(V = ST->lookup(SF->getType(), SF->getName())) &&
 	cast<Function>(V)->hasExternalLinkage()) {
       // The same named thing is a Function, because the only two things
       // that may be in a module level symbol table are Global Vars and
       // Functions, and they both have distinct, nonoverlapping, possible types.
       // 
-      Function *DM = cast<Function>(V);   // DestFunction
+      Function *DF = cast<Function>(V);   // DestFunction
 
       // Check to make sure the function is not defined in both modules...
-      if (!SM->isExternal() && !DM->isExternal())
+      if (!SF->isExternal() && !DF->isExternal())
         return Error(Err, "Function '" + 
-                     SM->getFunctionType()->getDescription() + "':\"" + 
-                     SM->getName() + "\" - Function is already defined!");
+                     SF->getFunctionType()->getDescription() + "':\"" + 
+                     SF->getName() + "\" - Function is already defined!");
 
       // Otherwise, just remember this mapping...
-      ValueMap.insert(std::make_pair(SM, DM));
+      ValueMap.insert(std::make_pair(SF, DF));
     } else {
       // Function does not already exist, simply insert an external function
-      // signature identical to SM into the dest module...
-      Function *DM = new Function(SM->getFunctionType(),
-                                  SM->hasInternalLinkage(),
-                                  SM->getName());
+      // signature identical to SF into the dest module...
+      Function *DF = new Function(SF->getFunctionType(),
+                                  SF->hasInternalLinkage(),
+                                  SF->getName());
 
       // Add the function signature to the dest module...
-      Dest->getFunctionList().push_back(DM);
+      Dest->getFunctionList().push_back(DF);
 
       // ... and remember this mapping...
-      ValueMap.insert(std::make_pair(SM, DM));
+      ValueMap.insert(std::make_pair(SF, DF));
     }
   }
   return false;
@@ -300,27 +301,22 @@
   map<const Value*, Value*> LocalMap;   // Map for function local values
 
   // Go through and convert function arguments over...
-  for (Function::ArgumentListType::const_iterator 
-         I = Src->getArgumentList().begin(),
-         E = Src->getArgumentList().end(); I != E; ++I) {
-    const Argument *SMA = *I;
-
+  for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
+       I != E; ++I) {
     // Create the new function argument and add to the dest function...
-    Argument *DMA = new Argument(SMA->getType(), SMA->getName());
-    Dest->getArgumentList().push_back(DMA);
+    Argument *DFA = new Argument(I->getType(), I->getName());
+    Dest->getArgumentList().push_back(DFA);
 
     // Add a mapping to our local map
-    LocalMap.insert(std::make_pair(SMA, DMA));
+    LocalMap.insert(std::make_pair(I, DFA));
   }
 
   // Loop over all of the basic blocks, copying the instructions over...
   //
   for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
-    const BasicBlock *SBB = *I;
-
     // Create new basic block and add to mapping and the Dest function...
-    BasicBlock *DBB = new BasicBlock(SBB->getName(), Dest);
-    LocalMap.insert(std::make_pair(SBB, DBB));
+    BasicBlock *DBB = new BasicBlock(I->getName(), Dest);
+    LocalMap.insert(std::make_pair(I, DBB));
 
     // Loop over all of the instructions in the src basic block, copying them
     // over.  Note that this is broken in a strict sense because the cloned
@@ -328,13 +324,12 @@
     // the remapped values.  In our case, however, we will not get caught and 
     // so we can delay patching the values up until later...
     //
-    for (BasicBlock::const_iterator II = SBB->begin(), IE = SBB->end(); 
+    for (BasicBlock::const_iterator II = I->begin(), IE = I->end(); 
          II != IE; ++II) {
-      const Instruction *SI = *II;
-      Instruction *DI = SI->clone();
-      DI->setName(SI->getName());
+      Instruction *DI = II->clone();
+      DI->setName(II->getName());
       DBB->getInstList().push_back(DI);
-      LocalMap.insert(std::make_pair(SI, DI));
+      LocalMap.insert(std::make_pair(II, DI));
     }
   }
 
@@ -343,17 +338,11 @@
   // the Source function as operands.  Loop through all of the operands of the
   // functions and patch them up to point to the local versions...
   //
-  for (Function::iterator BI = Dest->begin(), BE = Dest->end();
-       BI != BE; ++BI) {
-    BasicBlock *BB = *BI;
-    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
-      Instruction *Inst = *I;
-      
-      for (Instruction::op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
+  for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
+    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
+      for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
            OI != OE; ++OI)
         *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
-    }
-  }
 
   return false;
 }
@@ -370,20 +359,19 @@
   // Loop over all of the functions in the src module, mapping them over as we
   // go
   //
-  for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
-    const Function *SM = *I;                  // Source Function
-    if (!SM->isExternal()) {                  // No body if function is external
-      Function *DM = cast<Function>(ValueMap[SM]); // Destination function
+  for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){
+    if (!SF->isExternal()) {                  // No body if function is external
+      Function *DF = cast<Function>(ValueMap[SF]); // Destination function
 
-      // DM not external SM external?
-      if (!DM->isExternal()) {
+      // DF not external SF external?
+      if (!DF->isExternal()) {
         if (Err)
-          *Err = "Function '" + (SM->hasName() ? SM->getName() : string("")) +
+          *Err = "Function '" + (SF->hasName() ? SF->getName() : string("")) +
                  "' body multiply defined!";
         return true;
       }
 
-      if (LinkFunctionBody(DM, SM, ValueMap, Err)) return true;
+      if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;
     }
   }
   return false;
diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp
index cc152c6..10028ba 100644
--- a/lib/Transforms/Utils/Local.cpp
+++ b/lib/Transforms/Utils/Local.cpp
@@ -17,13 +17,12 @@
 // them together...
 //
 bool doConstantPropogation(BasicBlock::iterator &II) {
-  Instruction *Inst = *II;
-  if (Constant *C = ConstantFoldInstruction(Inst)) {
+  if (Constant *C = ConstantFoldInstruction(II)) {
     // Replaces all of the uses of a variable with uses of the constant.
-    Inst->replaceAllUsesWith(C);
+    II->replaceAllUsesWith(C);
     
     // Remove the instruction from the basic block...
-    delete Inst->getParent()->getInstList().remove(II);
+    II = II->getParent()->getInstList().erase(II);
     return true;
   }
 
@@ -102,9 +101,8 @@
 //
 bool dceInstruction(BasicBlock::iterator &BBI) {
   // Look for un"used" definitions...
-  Instruction *I = *BBI;
-  if (isInstructionTriviallyDead(I)) {
-    delete I->getParent()->getInstList().remove(BBI);   // Bye bye
+  if (isInstructionTriviallyDead(BBI)) {
+    BBI = BBI->getParent()->getInstList().erase(BBI);   // Bye bye
     return true;
   }
   return false;
diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp
index 00a02dc..426d18e 100644
--- a/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -46,7 +46,7 @@
 
   // Loop over all of the PHI nodes in the successor BB
   for (BasicBlock::iterator I = Succ->begin();
-       PHINode *PN = dyn_cast<PHINode>(*I); ++I) {
+       PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
     Value *OldVal = PN->removeIncomingValue(BB);
     assert(OldVal && "No entry in PHI for Pred BB!");
 
@@ -69,13 +69,12 @@
 //
 // WARNING:  The entry node of a function may not be simplified.
 //
-bool SimplifyCFG(Function::iterator &BBIt) {
-  BasicBlock *BB = *BBIt;
+bool SimplifyCFG(BasicBlock *BB) {
   Function *M = BB->getParent();
 
   assert(BB && BB->getParent() && "Block not embedded in function!");
   assert(BB->getTerminator() && "Degenerate basic block encountered!");
-  assert(BB->getParent()->front() != BB && "Can't Simplify entry block!");
+  assert(&BB->getParent()->front() != BB && "Can't Simplify entry block!");
 
 
   // Remove basic blocks that have no predecessors... which are unreachable.
@@ -89,20 +88,20 @@
 	     std::bind2nd(std::mem_fun(&BasicBlock::removePredecessor), BB));
 
     while (!BB->empty()) {
-      Instruction *I = BB->back();
+      Instruction &I = BB->back();
       // If this instruction is used, replace uses with an arbitrary
       // constant value.  Because control flow can't get here, we don't care
       // what we replace the value with.  Note that since this block is 
       // unreachable, and all values contained within it must dominate their
       // uses, that all uses will eventually be removed.
-      if (!I->use_empty()) 
+      if (!I.use_empty()) 
         // Make all users of this instruction reference the constant instead
-        I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
+        I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
       
       // Remove the instruction from the basic block
-      delete BB->getInstList().pop_back();
+      BB->getInstList().pop_back();
     }
-    delete M->getBasicBlocks().remove(BBIt);
+    M->getBasicBlockList().erase(BB);
     return true;
   }
 
@@ -110,7 +109,7 @@
   // successor.  If so, replace block references with successor.
   succ_iterator SI(succ_begin(BB));
   if (SI != succ_end(BB) && ++SI == succ_end(BB)) {  // One succ?
-    if (BB->front()->isTerminator()) {   // Terminator is the only instruction!
+    if (BB->front().isTerminator()) {   // Terminator is the only instruction!
       BasicBlock *Succ = *succ_begin(BB); // There is exactly one successor
      
       if (Succ != BB) {   // Arg, don't hurt infinite loops!
@@ -125,11 +124,13 @@
           //cerr << "Killing Trivial BB: \n" << BB;
 
           BB->replaceAllUsesWith(Succ);
-          BB = M->getBasicBlocks().remove(BBIt);
+          std::string OldName = BB->getName();
+
+          // Delete the old basic block...
+          M->getBasicBlockList().erase(BB);
 	
-          if (BB->hasName() && !Succ->hasName())  // Transfer name if we can
-            Succ->setName(BB->getName());
-          delete BB;                              // Delete basic block
+          if (!OldName.empty() && !Succ->hasName())  // Transfer name if we can
+            Succ->setName(OldName);
           
           //cerr << "Function after removal: \n" << M;
           return true;
@@ -168,28 +169,24 @@
       TerminatorInst *Term = OnlyPred->getTerminator();
 
       // Delete the unconditional branch from the predecessor...
-      BasicBlock::iterator DI = OnlyPred->end();
-      delete OnlyPred->getInstList().remove(--DI);       // Destroy branch
+      OnlyPred->getInstList().pop_back();
       
       // Move all definitions in the succecessor to the predecessor...
-      std::vector<Instruction*> Insts(BB->begin(), BB->end());
-      BB->getInstList().remove(BB->begin(), BB->end());
-      OnlyPred->getInstList().insert(OnlyPred->end(),
-                                     Insts.begin(), Insts.end());
-      
-      // Remove basic block from the function... and advance iterator to the
-      // next valid block...
-      M->getBasicBlocks().remove(BBIt);
-
+      OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
+                                     
       // Make all PHI nodes that refered to BB now refer to Pred as their
       // source...
       BB->replaceAllUsesWith(OnlyPred);
-      
+
+      std::string OldName = BB->getName();
+
+      // Erase basic block from the function... 
+      M->getBasicBlockList().erase(BB);
+
       // Inherit predecessors name if it exists...
-      if (BB->hasName() && !OnlyPred->hasName())
-        OnlyPred->setName(BB->getName());
+      if (!OldName.empty() && !OnlyPred->hasName())
+        OnlyPred->setName(OldName);
       
-      delete BB; // You ARE the weakest link... goodbye
       return true;
     }
   }
diff --git a/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp b/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp
index 64ae2e3..9a65fce 100644
--- a/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp
+++ b/lib/Transforms/Utils/UnifyFunctionExitNodes.cpp
@@ -24,14 +24,14 @@
 //
 // If there are no return stmts in the Function, a null pointer is returned.
 //
-bool UnifyFunctionExitNodes::runOnFunction(Function *M) {
+bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
   // Loop over all of the blocks in a function, tracking all of the blocks that
   // return.
   //
   vector<BasicBlock*> ReturningBlocks;
-  for(Function::iterator I = M->begin(), E = M->end(); I != E; ++I)
-    if (isa<ReturnInst>((*I)->getTerminator()))
-      ReturningBlocks.push_back(*I);
+  for(Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
+    if (isa<ReturnInst>(I->getTerminator()))
+      ReturningBlocks.push_back(I);
 
   if (ReturningBlocks.empty()) {
     ExitNode = 0;
@@ -45,11 +45,11 @@
   // node (if the function returns a value), and convert all of the return 
   // instructions into unconditional branches.
   //
-  BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", M);
+  BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", &F);
 
-  if (M->getReturnType() != Type::VoidTy) {
+  if (F.getReturnType() != Type::VoidTy) {
     // If the function doesn't return void... add a PHI node to the block...
-    PHINode *PN = new PHINode(M->getReturnType(), "UnifiedRetVal");
+    PHINode *PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
     NewRetBlock->getInstList().push_back(PN);
 
     // Add an incoming element to the PHI node for every return instruction that
@@ -70,7 +70,7 @@
   //
   for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(), 
                                      E = ReturningBlocks.end(); I != E; ++I) {
-    delete (*I)->getInstList().pop_back();  // Remove the return insn
+    (*I)->getInstList().pop_back();  // Remove the return insn
     (*I)->getInstList().push_back(new BranchInst(NewRetBlock));
   }
   ExitNode = NewRetBlock;
diff --git a/lib/VMCore/Linker.cpp b/lib/VMCore/Linker.cpp
index 7d43047..fb83e02 100644
--- a/lib/VMCore/Linker.cpp
+++ b/lib/VMCore/Linker.cpp
@@ -96,20 +96,20 @@
   }
 
   // Check to see if it's a constant that we are interesting in transforming...
-  if (Constant *CPV = dyn_cast<Constant>(In)) {
+  if (const Constant *CPV = dyn_cast<Constant>(In)) {
     if (!isa<DerivedType>(CPV->getType()))
-      return CPV;              // Simple constants stay identical...
+      return const_cast<Constant*>(CPV);   // Simple constants stay identical...
 
     Constant *Result = 0;
 
-    if (ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
+    if (const ConstantArray *CPA = dyn_cast<ConstantArray>(CPV)) {
       const std::vector<Use> &Ops = CPA->getValues();
       std::vector<Constant*> Operands(Ops.size());
       for (unsigned i = 0; i < Ops.size(); ++i)
         Operands[i] = 
           cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
       Result = ConstantArray::get(cast<ArrayType>(CPA->getType()), Operands);
-    } else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
+    } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CPV)) {
       const std::vector<Use> &Ops = CPS->getValues();
       std::vector<Constant*> Operands(Ops.size());
       for (unsigned i = 0; i < Ops.size(); ++i)
@@ -117,8 +117,9 @@
           cast<Constant>(RemapOperand(Ops[i], LocalMap, GlobalMap));
       Result = ConstantStruct::get(cast<StructType>(CPS->getType()), Operands);
     } else if (isa<ConstantPointerNull>(CPV)) {
-      Result = CPV;
-    } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
+      Result = const_cast<Constant*>(CPV);
+    } else if (const ConstantPointerRef *CPR =
+                      dyn_cast<ConstantPointerRef>(CPV)) {
       Value *V = RemapOperand(CPR->getValue(), LocalMap, GlobalMap);
       Result = ConstantPointerRef::get(cast<GlobalValue>(V));
     } else {
@@ -126,7 +127,7 @@
     }
 
     // Cache the mapping in our local map structure...
-    LocalMap.insert(std::make_pair(In, CPV));
+    LocalMap.insert(std::make_pair(In, const_cast<Constant*>(CPV)));
     return Result;
   }
 
@@ -158,7 +159,7 @@
   // Loop over all of the globals in the src module, mapping them over as we go
   //
   for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
-    const GlobalVariable *SGV = *I;
+    const GlobalVariable *SGV = I;
     Value *V;
 
     // If the global variable has a name, and that name is already in use in the
@@ -211,7 +212,7 @@
   // Loop over all of the globals in the src module, mapping them over as we go
   //
   for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
-    const GlobalVariable *SGV = *I;
+    const GlobalVariable *SGV = I;
 
     if (SGV->hasInitializer()) {      // Only process initialized GV's
       // Figure out what the initializer looks like in the dest module...
@@ -249,41 +250,41 @@
   // go
   //
   for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
-    const Function *SM = *I;   // SrcFunction
+    const Function *SF = I;   // SrcFunction
     Value *V;
 
     // If the function has a name, and that name is already in use in the Dest
     // module, make sure that the name is a compatible function...
     //
-    if (SM->hasExternalLinkage() && SM->hasName() &&
-	(V = ST->lookup(SM->getType(), SM->getName())) &&
+    if (SF->hasExternalLinkage() && SF->hasName() &&
+	(V = ST->lookup(SF->getType(), SF->getName())) &&
 	cast<Function>(V)->hasExternalLinkage()) {
       // The same named thing is a Function, because the only two things
       // that may be in a module level symbol table are Global Vars and
       // Functions, and they both have distinct, nonoverlapping, possible types.
       // 
-      Function *DM = cast<Function>(V);   // DestFunction
+      Function *DF = cast<Function>(V);   // DestFunction
 
       // Check to make sure the function is not defined in both modules...
-      if (!SM->isExternal() && !DM->isExternal())
+      if (!SF->isExternal() && !DF->isExternal())
         return Error(Err, "Function '" + 
-                     SM->getFunctionType()->getDescription() + "':\"" + 
-                     SM->getName() + "\" - Function is already defined!");
+                     SF->getFunctionType()->getDescription() + "':\"" + 
+                     SF->getName() + "\" - Function is already defined!");
 
       // Otherwise, just remember this mapping...
-      ValueMap.insert(std::make_pair(SM, DM));
+      ValueMap.insert(std::make_pair(SF, DF));
     } else {
       // Function does not already exist, simply insert an external function
-      // signature identical to SM into the dest module...
-      Function *DM = new Function(SM->getFunctionType(),
-                                  SM->hasInternalLinkage(),
-                                  SM->getName());
+      // signature identical to SF into the dest module...
+      Function *DF = new Function(SF->getFunctionType(),
+                                  SF->hasInternalLinkage(),
+                                  SF->getName());
 
       // Add the function signature to the dest module...
-      Dest->getFunctionList().push_back(DM);
+      Dest->getFunctionList().push_back(DF);
 
       // ... and remember this mapping...
-      ValueMap.insert(std::make_pair(SM, DM));
+      ValueMap.insert(std::make_pair(SF, DF));
     }
   }
   return false;
@@ -300,27 +301,22 @@
   map<const Value*, Value*> LocalMap;   // Map for function local values
 
   // Go through and convert function arguments over...
-  for (Function::ArgumentListType::const_iterator 
-         I = Src->getArgumentList().begin(),
-         E = Src->getArgumentList().end(); I != E; ++I) {
-    const Argument *SMA = *I;
-
+  for (Function::const_aiterator I = Src->abegin(), E = Src->aend();
+       I != E; ++I) {
     // Create the new function argument and add to the dest function...
-    Argument *DMA = new Argument(SMA->getType(), SMA->getName());
-    Dest->getArgumentList().push_back(DMA);
+    Argument *DFA = new Argument(I->getType(), I->getName());
+    Dest->getArgumentList().push_back(DFA);
 
     // Add a mapping to our local map
-    LocalMap.insert(std::make_pair(SMA, DMA));
+    LocalMap.insert(std::make_pair(I, DFA));
   }
 
   // Loop over all of the basic blocks, copying the instructions over...
   //
   for (Function::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
-    const BasicBlock *SBB = *I;
-
     // Create new basic block and add to mapping and the Dest function...
-    BasicBlock *DBB = new BasicBlock(SBB->getName(), Dest);
-    LocalMap.insert(std::make_pair(SBB, DBB));
+    BasicBlock *DBB = new BasicBlock(I->getName(), Dest);
+    LocalMap.insert(std::make_pair(I, DBB));
 
     // Loop over all of the instructions in the src basic block, copying them
     // over.  Note that this is broken in a strict sense because the cloned
@@ -328,13 +324,12 @@
     // the remapped values.  In our case, however, we will not get caught and 
     // so we can delay patching the values up until later...
     //
-    for (BasicBlock::const_iterator II = SBB->begin(), IE = SBB->end(); 
+    for (BasicBlock::const_iterator II = I->begin(), IE = I->end(); 
          II != IE; ++II) {
-      const Instruction *SI = *II;
-      Instruction *DI = SI->clone();
-      DI->setName(SI->getName());
+      Instruction *DI = II->clone();
+      DI->setName(II->getName());
       DBB->getInstList().push_back(DI);
-      LocalMap.insert(std::make_pair(SI, DI));
+      LocalMap.insert(std::make_pair(II, DI));
     }
   }
 
@@ -343,17 +338,11 @@
   // the Source function as operands.  Loop through all of the operands of the
   // functions and patch them up to point to the local versions...
   //
-  for (Function::iterator BI = Dest->begin(), BE = Dest->end();
-       BI != BE; ++BI) {
-    BasicBlock *BB = *BI;
-    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
-      Instruction *Inst = *I;
-      
-      for (Instruction::op_iterator OI = Inst->op_begin(), OE = Inst->op_end();
+  for (Function::iterator BB = Dest->begin(), BE = Dest->end(); BB != BE; ++BB)
+    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
+      for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
            OI != OE; ++OI)
         *OI = RemapOperand(*OI, LocalMap, &GlobalMap);
-    }
-  }
 
   return false;
 }
@@ -370,20 +359,19 @@
   // Loop over all of the functions in the src module, mapping them over as we
   // go
   //
-  for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
-    const Function *SM = *I;                  // Source Function
-    if (!SM->isExternal()) {                  // No body if function is external
-      Function *DM = cast<Function>(ValueMap[SM]); // Destination function
+  for (Module::const_iterator SF = Src->begin(), E = Src->end(); SF != E; ++SF){
+    if (!SF->isExternal()) {                  // No body if function is external
+      Function *DF = cast<Function>(ValueMap[SF]); // Destination function
 
-      // DM not external SM external?
-      if (!DM->isExternal()) {
+      // DF not external SF external?
+      if (!DF->isExternal()) {
         if (Err)
-          *Err = "Function '" + (SM->hasName() ? SM->getName() : string("")) +
+          *Err = "Function '" + (SF->hasName() ? SF->getName() : string("")) +
                  "' body multiply defined!";
         return true;
       }
 
-      if (LinkFunctionBody(DM, SM, ValueMap, Err)) return true;
+      if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;
     }
   }
   return false;