Get rid of the Pass+Context magic.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76702 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Utils/AddrModeMatcher.cpp b/lib/Transforms/Utils/AddrModeMatcher.cpp
index 36091e6..21c84ab 100644
--- a/lib/Transforms/Utils/AddrModeMatcher.cpp
+++ b/lib/Transforms/Utils/AddrModeMatcher.cpp
@@ -97,7 +97,7 @@
   ConstantInt *CI = 0; Value *AddLHS = 0;
   if (isa<Instruction>(ScaleReg) &&  // not a constant expr.
       match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)),
-            *MemoryInst->getParent()->getContext())) {
+            MemoryInst->getContext())) {
     TestAddrMode.ScaledReg = AddLHS;
     TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale;
       
diff --git a/lib/Transforms/Utils/BasicBlockUtils.cpp b/lib/Transforms/Utils/BasicBlockUtils.cpp
index b8663de..c4b3474 100644
--- a/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -51,7 +51,7 @@
     // contained within it must dominate their uses, that all uses will
     // eventually be removed (they are themselves dead).
     if (!I.use_empty())
-      I.replaceAllUsesWith(BB->getContext()->getUndef(I.getType()));
+      I.replaceAllUsesWith(BB->getContext().getUndef(I.getType()));
     BB->getInstList().pop_back();
   }
   
@@ -71,7 +71,7 @@
     if (PN->getIncomingValue(0) != PN)
       PN->replaceAllUsesWith(PN->getIncomingValue(0));
     else
-      PN->replaceAllUsesWith(BB->getContext()->getUndef(PN->getType()));
+      PN->replaceAllUsesWith(BB->getContext().getUndef(PN->getType()));
     PN->eraseFromParent();
   }
 }
@@ -252,7 +252,7 @@
 
       // Create a value to return... if the function doesn't return null...
       if (BB->getParent()->getReturnType() != Type::VoidTy)
-        RetVal = TI->getParent()->getContext()->getNullValue(
+        RetVal = TI->getContext().getNullValue(
                    BB->getParent()->getReturnType());
 
       // Create the return...
@@ -387,7 +387,7 @@
   if (NumPreds == 0) {
     // Insert dummy values as the incoming value.
     for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
-      cast<PHINode>(I)->addIncoming(BB->getContext()->getUndef(I->getType()), 
+      cast<PHINode>(I)->addIncoming(BB->getContext().getUndef(I->getType()), 
                                     NewBB);
     return NewBB;
   }
@@ -618,7 +618,7 @@
   if (I != I->getParent()->begin()) {
     BasicBlock::iterator BBI = I;  --BBI;
     if (DbgStopPointInst *DSPI = dyn_cast<DbgStopPointInst>(BBI)) {
-      CallInst *newDSPI = DSPI->clone(*I->getParent()->getContext());
+      CallInst *newDSPI = DSPI->clone(I->getContext());
       newDSPI->insertBefore(InsertPos);
     }
   }
diff --git a/lib/Transforms/Utils/CloneFunction.cpp b/lib/Transforms/Utils/CloneFunction.cpp
index f05c5dc..dae39b7 100644
--- a/lib/Transforms/Utils/CloneFunction.cpp
+++ b/lib/Transforms/Utils/CloneFunction.cpp
@@ -43,7 +43,7 @@
   // Loop over all instructions, and copy them over.
   for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
        II != IE; ++II) {
-    Instruction *NewInst = II->clone(*BB->getContext());
+    Instruction *NewInst = II->clone(BB->getContext());
     if (II->hasName())
       NewInst->setName(II->getName()+NameSuffix);
     NewBB->getInstList().push_back(NewInst);
@@ -152,7 +152,7 @@
 
   // Create a new function type...
   FunctionType *FTy =
-     F->getContext()->getFunctionType(F->getFunctionType()->getReturnType(),
+     F->getContext().getFunctionType(F->getFunctionType()->getReturnType(),
                                     ArgTypes, F->getFunctionType()->isVarArg());
 
   // Create the new function...
@@ -249,7 +249,7 @@
         continue;
     }
       
-    Instruction *NewInst = II->clone(*BB->getContext());
+    Instruction *NewInst = II->clone(BB->getContext());
     if (II->hasName())
       NewInst->setName(II->getName()+NameSuffix);
     NewBB->getInstList().push_back(NewInst);
@@ -297,7 +297,7 @@
   }
   
   if (!TerminatorDone) {
-    Instruction *NewInst = OldTI->clone(*BB->getContext());
+    Instruction *NewInst = OldTI->clone(BB->getContext());
     if (OldTI->hasName())
       NewInst->setName(OldTI->getName()+NameSuffix);
     NewBB->getInstList().push_back(NewInst);
@@ -325,7 +325,7 @@
 /// mapping its operands through ValueMap if they are available.
 Constant *PruningFunctionCloner::
 ConstantFoldMappedInstruction(const Instruction *I) {
-  LLVMContext *Context = I->getParent()->getContext();
+  LLVMContext &Context = I->getContext();
   
   SmallVector<Constant*, 8> Ops;
   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
@@ -367,7 +367,7 @@
                                      ClonedCodeInfo *CodeInfo,
                                      const TargetData *TD) {
   assert(NameSuffix && "NameSuffix cannot be null!");
-  LLVMContext *Context = OldFunc->getContext();
+  LLVMContext &Context = OldFunc->getContext();
   
 #ifndef NDEBUG
   for (Function::const_arg_iterator II = OldFunc->arg_begin(), 
@@ -490,7 +490,7 @@
       BasicBlock::iterator I = NewBB->begin();
       BasicBlock::const_iterator OldI = OldBB->begin();
       while ((PN = dyn_cast<PHINode>(I++))) {
-        Value *NV = OldFunc->getContext()->getUndef(PN->getType());
+        Value *NV = OldFunc->getContext().getUndef(PN->getType());
         PN->replaceAllUsesWith(NV);
         assert(ValueMap[OldI] == PN && "ValueMap mismatch");
         ValueMap[OldI] = NV;
diff --git a/lib/Transforms/Utils/CloneModule.cpp b/lib/Transforms/Utils/CloneModule.cpp
index bb5f3e6..13b2d46 100644
--- a/lib/Transforms/Utils/CloneModule.cpp
+++ b/lib/Transforms/Utils/CloneModule.cpp
@@ -90,7 +90,7 @@
     if (I->hasInitializer())
       GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
                                                  ValueMap,
-                                                 &M->getContext())));
+                                                 M->getContext())));
     GV->setLinkage(I->getLinkage());
     GV->setThreadLocal(I->isThreadLocal());
     GV->setConstant(I->isConstant());
@@ -121,7 +121,7 @@
     GlobalAlias *GA = cast<GlobalAlias>(ValueMap[I]);
     GA->setLinkage(I->getLinkage());
     if (const Constant* C = I->getAliasee())
-      GA->setAliasee(cast<Constant>(MapValue(C, ValueMap, &M->getContext())));
+      GA->setAliasee(cast<Constant>(MapValue(C, ValueMap, M->getContext())));
   }
   
   return New;
diff --git a/lib/Transforms/Utils/CodeExtractor.cpp b/lib/Transforms/Utils/CodeExtractor.cpp
index be02560..a019399 100644
--- a/lib/Transforms/Utils/CodeExtractor.cpp
+++ b/lib/Transforms/Utils/CodeExtractor.cpp
@@ -239,7 +239,7 @@
   DOUT << "inputs: " << inputs.size() << "\n";
   DOUT << "outputs: " << outputs.size() << "\n";
 
-  LLVMContext *Context = header->getContext();
+  LLVMContext &Context = header->getContext();
 
   // This function returns unsigned, outputs will go back by reference.
   switch (NumExitBlocks) {
@@ -267,7 +267,7 @@
       paramTy.push_back((*I)->getType());
     else
       paramTy.push_back(
-         header->getContext()->getPointerTypeUnqual((*I)->getType()));
+         header->getContext().getPointerTypeUnqual((*I)->getType()));
   }
 
   DOUT << "Function type: " << *RetTy << " f(";
@@ -278,12 +278,12 @@
 
   if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
     PointerType *StructPtr =
-           Context->getPointerTypeUnqual(Context->getStructType(paramTy));
+           Context.getPointerTypeUnqual(Context.getStructType(paramTy));
     paramTy.clear();
     paramTy.push_back(StructPtr);
   }
   const FunctionType *funcType =
-                  Context->getFunctionType(RetTy, paramTy, false);
+                  Context.getFunctionType(RetTy, paramTy, false);
 
   // Create the new function
   Function *newFunction = Function::Create(funcType,
@@ -305,8 +305,8 @@
     Value *RewriteVal;
     if (AggregateArgs) {
       Value *Idx[2];
-      Idx[0] = Context->getNullValue(Type::Int32Ty);
-      Idx[1] = Context->getConstantInt(Type::Int32Ty, i);
+      Idx[0] = Context.getNullValue(Type::Int32Ty);
+      Idx[1] = Context.getConstantInt(Type::Int32Ty, i);
       std::string GEPname = "gep_" + inputs[i]->getName();
       TerminatorInst *TI = newFunction->begin()->getTerminator();
       GetElementPtrInst *GEP = GetElementPtrInst::Create(AI, Idx, Idx+2, 
@@ -353,7 +353,7 @@
 void CodeExtractor::
 emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
                            Values &inputs, Values &outputs) {
-  LLVMContext *Context = codeReplacer->getContext();
+  LLVMContext &Context = codeReplacer->getContext();
   
   // Emit a call to the new function, passing in: *pointer to struct (if
   // aggregating parameters), or plan inputs and allocated memory for outputs
@@ -387,7 +387,7 @@
       ArgTypes.push_back((*v)->getType());
 
     // Allocate a struct at the beginning of this function
-    Type *StructArgTy = Context->getStructType(ArgTypes);
+    Type *StructArgTy = Context.getStructType(ArgTypes);
     Struct =
       new AllocaInst(StructArgTy, 0, "structArg",
                      codeReplacer->getParent()->begin()->begin());
@@ -395,8 +395,8 @@
 
     for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
       Value *Idx[2];
-      Idx[0] = Context->getNullValue(Type::Int32Ty);
-      Idx[1] = Context->getConstantInt(Type::Int32Ty, i);
+      Idx[0] = Context.getNullValue(Type::Int32Ty);
+      Idx[1] = Context.getConstantInt(Type::Int32Ty, i);
       GetElementPtrInst *GEP =
         GetElementPtrInst::Create(Struct, Idx, Idx + 2,
                                   "gep_" + StructValues[i]->getName());
@@ -421,8 +421,8 @@
     Value *Output = 0;
     if (AggregateArgs) {
       Value *Idx[2];
-      Idx[0] = Context->getNullValue(Type::Int32Ty);
-      Idx[1] = Context->getConstantInt(Type::Int32Ty, FirstOut + i);
+      Idx[0] = Context.getNullValue(Type::Int32Ty);
+      Idx[1] = Context.getConstantInt(Type::Int32Ty, FirstOut + i);
       GetElementPtrInst *GEP
         = GetElementPtrInst::Create(Struct, Idx, Idx + 2,
                                     "gep_reload_" + outputs[i]->getName());
@@ -443,7 +443,7 @@
 
   // Now we can emit a switch statement using the call as a value.
   SwitchInst *TheSwitch =
-      SwitchInst::Create(Context->getNullValue(Type::Int16Ty),
+      SwitchInst::Create(Context.getNullValue(Type::Int16Ty),
                          codeReplacer, 0, codeReplacer);
 
   // Since there may be multiple exits from the original region, make the new
@@ -474,17 +474,17 @@
           case 0:
           case 1: break;  // No value needed.
           case 2:         // Conditional branch, return a bool
-            brVal = Context->getConstantInt(Type::Int1Ty, !SuccNum);
+            brVal = Context.getConstantInt(Type::Int1Ty, !SuccNum);
             break;
           default:
-            brVal = Context->getConstantInt(Type::Int16Ty, SuccNum);
+            brVal = Context.getConstantInt(Type::Int16Ty, SuccNum);
             break;
           }
 
           ReturnInst *NTRet = ReturnInst::Create(brVal, NewTarget);
 
           // Update the switch instruction.
-          TheSwitch->addCase(Context->getConstantInt(Type::Int16Ty, SuccNum),
+          TheSwitch->addCase(Context.getConstantInt(Type::Int16Ty, SuccNum),
                              OldTarget);
 
           // Restore values just before we exit
@@ -522,8 +522,8 @@
             if (DominatesDef) {
               if (AggregateArgs) {
                 Value *Idx[2];
-                Idx[0] = Context->getNullValue(Type::Int32Ty);
-                Idx[1] = Context->getConstantInt(Type::Int32Ty,FirstOut+out);
+                Idx[0] = Context.getNullValue(Type::Int32Ty);
+                Idx[1] = Context.getConstantInt(Type::Int32Ty,FirstOut+out);
                 GetElementPtrInst *GEP =
                   GetElementPtrInst::Create(OAI, Idx, Idx + 2,
                                             "gep_" + outputs[out]->getName(),
@@ -560,7 +560,7 @@
     } else {
       // Otherwise we must have code extracted an unwind or something, just
       // return whatever we want.
-      ReturnInst::Create(Context->getNullValue(OldFnRetTy), TheSwitch);
+      ReturnInst::Create(Context.getNullValue(OldFnRetTy), TheSwitch);
     }
 
     TheSwitch->eraseFromParent();
diff --git a/lib/Transforms/Utils/InlineFunction.cpp b/lib/Transforms/Utils/InlineFunction.cpp
index e1b493c..ae0b5ee 100644
--- a/lib/Transforms/Utils/InlineFunction.cpp
+++ b/lib/Transforms/Utils/InlineFunction.cpp
@@ -239,7 +239,7 @@
 //
 bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) {
   Instruction *TheCall = CS.getInstruction();
-  LLVMContext *Context = TheCall->getParent()->getContext();
+  LLVMContext &Context = TheCall->getContext();
   assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
          "Instruction not in function!");
 
@@ -304,7 +304,7 @@
       if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal) &&
           !CalledFunc->onlyReadsMemory()) {
         const Type *AggTy = cast<PointerType>(I->getType())->getElementType();
-        const Type *VoidPtrTy = Context->getPointerTypeUnqual(Type::Int8Ty);
+        const Type *VoidPtrTy = Context.getPointerTypeUnqual(Type::Int8Ty);
 
         // Create the alloca.  If we have TargetData, use nice alignment.
         unsigned Align = 1;
@@ -322,16 +322,16 @@
 
         Value *Size;
         if (TD == 0)
-          Size = Context->getConstantExprSizeOf(AggTy);
+          Size = Context.getConstantExprSizeOf(AggTy);
         else
-          Size = Context->getConstantInt(Type::Int64Ty,
+          Size = Context.getConstantInt(Type::Int64Ty,
                                          TD->getTypeStoreSize(AggTy));
 
         // Always generate a memcpy of alignment 1 here because we don't know
         // the alignment of the src pointer.  Other optimizations can infer
         // better alignment.
         Value *CallArgs[] = {
-          DestCast, SrcCast, Size, Context->getConstantInt(Type::Int32Ty, 1)
+          DestCast, SrcCast, Size, Context.getConstantInt(Type::Int32Ty, 1)
         };
         CallInst *TheMemCpy =
           CallInst::Create(MemCpyFn, CallArgs, CallArgs+4, "", TheCall);
@@ -362,7 +362,7 @@
              BE = TheCall->getParent()->end(); BI != BE; ++BI) {
         if (DbgStopPointInst *DSPI = dyn_cast<DbgStopPointInst>(BI)) {
           if (DbgRegionEndInst *NewDREI = 
-              dyn_cast<DbgRegionEndInst>(DREI->clone(*Context)))
+              dyn_cast<DbgRegionEndInst>(DREI->clone(Context)))
             NewDREI->insertAfter(DSPI);
           break;
         }
@@ -521,7 +521,7 @@
     if (!TheCall->use_empty()) {
       ReturnInst *R = Returns[0];
       if (TheCall == R->getReturnValue())
-        TheCall->replaceAllUsesWith(Context->getUndef(TheCall->getType()));
+        TheCall->replaceAllUsesWith(Context.getUndef(TheCall->getType()));
       else
         TheCall->replaceAllUsesWith(R->getReturnValue());
     }
@@ -614,7 +614,7 @@
     // using the return value of the call with the computed value.
     if (!TheCall->use_empty()) {
       if (TheCall == Returns[0]->getReturnValue())
-        TheCall->replaceAllUsesWith(Context->getUndef(TheCall->getType()));
+        TheCall->replaceAllUsesWith(Context.getUndef(TheCall->getType()));
       else
         TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
     }
@@ -634,7 +634,7 @@
   } else if (!TheCall->use_empty()) {
     // No returns, but something is using the return value of the call.  Just
     // nuke the result.
-    TheCall->replaceAllUsesWith(Context->getUndef(TheCall->getType()));
+    TheCall->replaceAllUsesWith(Context.getUndef(TheCall->getType()));
   }
 
   // Since we are now done with the Call/Invoke, we can delete it.
diff --git a/lib/Transforms/Utils/LCSSA.cpp b/lib/Transforms/Utils/LCSSA.cpp
index 1c31dc9..1b72849 100644
--- a/lib/Transforms/Utils/LCSSA.cpp
+++ b/lib/Transforms/Utils/LCSSA.cpp
@@ -243,7 +243,7 @@
                                DenseMap<DomTreeNode*, Value*> &Phis) {
   // If there is no dominator info for this BB, it is unreachable.
   if (BB == 0)
-    return Context->getUndef(OrigInst->getType());
+    return OrigInst->getContext().getUndef(OrigInst->getType());
                                  
   // If we have already computed this value, return the previously computed val.
   if (Phis.count(BB)) return Phis[BB];
diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp
index 0bb9614..e9be0e2 100644
--- a/lib/Transforms/Utils/Local.cpp
+++ b/lib/Transforms/Utils/Local.cpp
@@ -263,7 +263,7 @@
 /// too, recursively.
 void
 llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) {
-  LLVMContext *Context = PN->getParent()->getContext();
+  LLVMContext &Context = PN->getContext();
   
   // We can remove a PHI if it is on a cycle in the def-use graph
   // where each node in the cycle has degree one, i.e. only one use,
@@ -281,7 +281,7 @@
     if (PHINode *JP = dyn_cast<PHINode>(J))
       if (!PHIs.insert(cast<PHINode>(JP))) {
         // Break the cycle and delete the PHI and its operands.
-        JP->replaceAllUsesWith(Context->getUndef(JP->getType()));
+        JP->replaceAllUsesWith(Context.getUndef(JP->getType()));
         RecursivelyDeleteTriviallyDeadInstructions(JP);
         break;
       }
@@ -301,7 +301,7 @@
   while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
     Value *NewVal = PN->getIncomingValue(0);
     // Replace self referencing PHI with undef, it must be dead.
-    if (NewVal == PN) NewVal = DestBB->getContext()->getUndef(PN->getType());
+    if (NewVal == PN) NewVal = DestBB->getContext().getUndef(PN->getType());
     PN->replaceAllUsesWith(NewVal);
     PN->eraseFromParent();
   }
diff --git a/lib/Transforms/Utils/LoopSimplify.cpp b/lib/Transforms/Utils/LoopSimplify.cpp
index 81fa4f9..045b428 100644
--- a/lib/Transforms/Utils/LoopSimplify.cpp
+++ b/lib/Transforms/Utils/LoopSimplify.cpp
@@ -166,7 +166,7 @@
     // Delete the dead terminator.
     if (AA) AA->deleteValue(TI);
     if (!TI->use_empty())
-      TI->replaceAllUsesWith(Context->getUndef(TI->getType()));
+      TI->replaceAllUsesWith(F.getContext().getUndef(TI->getType()));
     TI->eraseFromParent();
     Changed |= true;
   }
diff --git a/lib/Transforms/Utils/LowerAllocations.cpp b/lib/Transforms/Utils/LowerAllocations.cpp
index d7ca313..8f61d88 100644
--- a/lib/Transforms/Utils/LowerAllocations.cpp
+++ b/lib/Transforms/Utils/LowerAllocations.cpp
@@ -87,13 +87,10 @@
 // This function is always successful.
 //
 bool LowerAllocations::doInitialization(Module &M) {
-  // Ensure context initialization.
-  BasicBlockPass::doInitialization(M);
-
-  const Type *BPTy = Context->getPointerTypeUnqual(Type::Int8Ty);
+  const Type *BPTy = M.getContext().getPointerTypeUnqual(Type::Int8Ty);
   // Prototype malloc as "char* malloc(...)", because we don't know in
   // doInitialization whether size_t is int or long.
-  FunctionType *FT = Context->getFunctionType(BPTy, true);
+  FunctionType *FT = M.getContext().getFunctionType(BPTy, true);
   MallocFunc = M.getOrInsertFunction("malloc", FT);
   FreeFunc = M.getOrInsertFunction("free"  , Type::VoidTy, BPTy, (Type *)0);
   return true;
@@ -106,6 +103,8 @@
   bool Changed = false;
   assert(MallocFunc && FreeFunc && "Pass not initialized!");
 
+  LLVMContext &Context = BB.getContext();
+
   BasicBlock::InstListType &BBIL = BB.getInstList();
 
   const TargetData &TD = getAnalysis<TargetData>();
@@ -119,12 +118,12 @@
       // malloc(type) becomes i8 *malloc(size)
       Value *MallocArg;
       if (LowerMallocArgToInteger)
-        MallocArg = Context->getConstantInt(Type::Int64Ty,
+        MallocArg = Context.getConstantInt(Type::Int64Ty,
                                      TD.getTypeAllocSize(AllocTy));
       else
-        MallocArg = Context->getConstantExprSizeOf(AllocTy);
+        MallocArg = Context.getConstantExprSizeOf(AllocTy);
       MallocArg =
-           Context->getConstantExprTruncOrBitCast(cast<Constant>(MallocArg), 
+           Context.getConstantExprTruncOrBitCast(cast<Constant>(MallocArg), 
                                                   IntPtrTy);
 
       if (MI->isArrayAllocation()) {
@@ -133,8 +132,8 @@
           MallocArg = MI->getOperand(0);         // Operand * 1 = Operand
         } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
           CO =
-              Context->getConstantExprIntegerCast(CO, IntPtrTy, false /*ZExt*/);
-          MallocArg = Context->getConstantExprMul(CO, 
+              Context.getConstantExprIntegerCast(CO, IntPtrTy, false /*ZExt*/);
+          MallocArg = Context.getConstantExprMul(CO, 
                                                   cast<Constant>(MallocArg));
         } else {
           Value *Scale = MI->getOperand(0);
@@ -157,7 +156,7 @@
       if (MCall->getType() != Type::VoidTy)
         MCast = new BitCastInst(MCall, MI->getType(), "", I);
       else
-        MCast = Context->getNullValue(MI->getType());
+        MCast = Context.getNullValue(MI->getType());
 
       // Replace all uses of the old malloc inst with the cast inst
       MI->replaceAllUsesWith(MCast);
@@ -167,7 +166,7 @@
     } else if (FreeInst *FI = dyn_cast<FreeInst>(I)) {
       Value *PtrCast = 
         new BitCastInst(FI->getOperand(0),
-                        Context->getPointerTypeUnqual(Type::Int8Ty), "", I);
+                        Context.getPointerTypeUnqual(Type::Int8Ty), "", I);
 
       // Insert a call to the free function...
       CallInst::Create(FreeFunc, PtrCast, "", I)->setTailCall();
diff --git a/lib/Transforms/Utils/LowerInvoke.cpp b/lib/Transforms/Utils/LowerInvoke.cpp
index 1417f10..9f20f39 100644
--- a/lib/Transforms/Utils/LowerInvoke.cpp
+++ b/lib/Transforms/Utils/LowerInvoke.cpp
@@ -115,35 +115,35 @@
 // doInitialization - Make sure that there is a prototype for abort in the
 // current module.
 bool LowerInvoke::doInitialization(Module &M) {
-  Context = &M.getContext();
+  LLVMContext &Context = M.getContext();
   
-  const Type *VoidPtrTy = Context->getPointerTypeUnqual(Type::Int8Ty);
+  const Type *VoidPtrTy = Context.getPointerTypeUnqual(Type::Int8Ty);
   AbortMessage = 0;
   if (ExpensiveEHSupport) {
     // Insert a type for the linked list of jump buffers.
     unsigned JBSize = TLI ? TLI->getJumpBufSize() : 0;
     JBSize = JBSize ? JBSize : 200;
-    const Type *JmpBufTy = Context->getArrayType(VoidPtrTy, JBSize);
+    const Type *JmpBufTy = Context.getArrayType(VoidPtrTy, JBSize);
 
     { // The type is recursive, so use a type holder.
       std::vector<const Type*> Elements;
       Elements.push_back(JmpBufTy);
-      OpaqueType *OT = Context->getOpaqueType();
-      Elements.push_back(Context->getPointerTypeUnqual(OT));
-      PATypeHolder JBLType(Context->getStructType(Elements));
+      OpaqueType *OT = Context.getOpaqueType();
+      Elements.push_back(Context.getPointerTypeUnqual(OT));
+      PATypeHolder JBLType(Context.getStructType(Elements));
       OT->refineAbstractTypeTo(JBLType.get());  // Complete the cycle.
       JBLinkTy = JBLType.get();
       M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy);
     }
 
-    const Type *PtrJBList = Context->getPointerTypeUnqual(JBLinkTy);
+    const Type *PtrJBList = Context.getPointerTypeUnqual(JBLinkTy);
 
     // Now that we've done that, insert the jmpbuf list head global, unless it
     // already exists.
     if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList))) {
       JBListHead = new GlobalVariable(M, PtrJBList, false,
                                       GlobalValue::LinkOnceAnyLinkage,
-                                      Context->getNullValue(PtrJBList),
+                                      Context.getNullValue(PtrJBList),
                                       "llvm.sjljeh.jblist");
     }
 
@@ -177,30 +177,32 @@
 }
 
 void LowerInvoke::createAbortMessage(Module *M) {
+  LLVMContext &Context = M->getContext();
+
   if (ExpensiveEHSupport) {
     // The abort message for expensive EH support tells the user that the
     // program 'unwound' without an 'invoke' instruction.
     Constant *Msg =
-      Context->getConstantArray("ERROR: Exception thrown, but not caught!\n");
+      Context.getConstantArray("ERROR: Exception thrown, but not caught!\n");
     AbortMessageLength = Msg->getNumOperands()-1;  // don't include \0
 
     GlobalVariable *MsgGV = new GlobalVariable(*M, Msg->getType(), true,
                                                GlobalValue::InternalLinkage,
                                                Msg, "abortmsg");
-    std::vector<Constant*> GEPIdx(2, Context->getNullValue(Type::Int32Ty));
-    AbortMessage = Context->getConstantExprGetElementPtr(MsgGV, &GEPIdx[0], 2);
+    std::vector<Constant*> GEPIdx(2, Context.getNullValue(Type::Int32Ty));
+    AbortMessage = Context.getConstantExprGetElementPtr(MsgGV, &GEPIdx[0], 2);
   } else {
     // The abort message for cheap EH support tells the user that EH is not
     // enabled.
     Constant *Msg =
-      Context->getConstantArray("Exception handler needed, but not enabled."      
+      Context.getConstantArray("Exception handler needed, but not enabled."      
                         "Recompile program with -enable-correct-eh-support.\n");
     AbortMessageLength = Msg->getNumOperands()-1;  // don't include \0
 
     GlobalVariable *MsgGV = new GlobalVariable(*M, Msg->getType(), true,
                                                GlobalValue::InternalLinkage,
                                                Msg, "abortmsg");
-    std::vector<Constant*> GEPIdx(2, Context->getNullValue(Type::Int32Ty));
+    std::vector<Constant*> GEPIdx(2, Context.getNullValue(Type::Int32Ty));
     AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
   }
 }
@@ -221,6 +223,7 @@
 }
 
 bool LowerInvoke::insertCheapEHSupport(Function &F) {
+  LLVMContext &Context = F.getContext();
   bool Changed = false;
   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
     if (InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) {
@@ -253,7 +256,7 @@
       // Insert a return instruction.  This really should be a "barrier", as it
       // is unreachable.
       ReturnInst::Create(F.getReturnType() == Type::VoidTy ? 0 :
-                         Context->getNullValue(F.getReturnType()), UI);
+                         Context.getNullValue(F.getReturnType()), UI);
 
       // Remove the unwind instruction now.
       BB->getInstList().erase(UI);
@@ -268,7 +271,8 @@
 void LowerInvoke::rewriteExpensiveInvoke(InvokeInst *II, unsigned InvokeNo,
                                          AllocaInst *InvokeNum,
                                          SwitchInst *CatchSwitch) {
-  ConstantInt *InvokeNoC = Context->getConstantInt(Type::Int32Ty, InvokeNo);
+  LLVMContext &Context = II->getContext();
+  ConstantInt *InvokeNoC = Context.getConstantInt(Type::Int32Ty, InvokeNo);
 
   // If the unwind edge has phi nodes, split the edge.
   if (isa<PHINode>(II->getUnwindDest()->begin())) {
@@ -287,7 +291,7 @@
 
   BasicBlock::iterator NI = II->getNormalDest()->getFirstNonPHI();
   // nonvolatile.
-  new StoreInst(Context->getNullValue(Type::Int32Ty), InvokeNum, false, NI);
+  new StoreInst(Context.getNullValue(Type::Int32Ty), InvokeNum, false, NI);
 
   // Add a switch case to our unwind block.
   CatchSwitch->addCase(InvokeNoC, II->getUnwindDest());
@@ -429,6 +433,8 @@
   std::vector<UnwindInst*> Unwinds;
   std::vector<InvokeInst*> Invokes;
 
+  LLVMContext &Context = F.getContext();
+
   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
     if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
       // Remember all return instructions in case we insert an invoke into this
@@ -476,8 +482,8 @@
                      "jblink", F.begin()->begin());
 
     std::vector<Value*> Idx;
-    Idx.push_back(Context->getNullValue(Type::Int32Ty));
-    Idx.push_back(Context->getConstantInt(Type::Int32Ty, 1));
+    Idx.push_back(Context.getNullValue(Type::Int32Ty));
+    Idx.push_back(Context.getConstantInt(Type::Int32Ty, 1));
     OldJmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx.begin(), Idx.end(),
                                              "OldBuf",
                                               EntryBB->getTerminator());
@@ -498,7 +504,7 @@
     // executing.  For normal calls it contains zero.
     AllocaInst *InvokeNum = new AllocaInst(Type::Int32Ty, 0,
                                            "invokenum",EntryBB->begin());
-    new StoreInst(Context->getConstantInt(Type::Int32Ty, 0), InvokeNum, true,
+    new StoreInst(Context.getConstantInt(Type::Int32Ty, 0), InvokeNum, true,
                   EntryBB->getTerminator());
 
     // Insert a load in the Catch block, and a switch on its value.  By default,
@@ -517,7 +523,7 @@
     BasicBlock *ContBlock = EntryBB->splitBasicBlock(EntryBB->getTerminator(),
                                                      "setjmp.cont");
 
-    Idx[1] = Context->getConstantInt(Type::Int32Ty, 0);
+    Idx[1] = Context.getConstantInt(Type::Int32Ty, 0);
     Value *JmpBufPtr = GetElementPtrInst::Create(JmpBuf, Idx.begin(), Idx.end(),
                                                  "TheJmpBuf",
                                                  EntryBB->getTerminator());
@@ -529,7 +535,7 @@
     // Compare the return value to zero.
     Value *IsNormal = new ICmpInst(EntryBB->getTerminator(),
                                    ICmpInst::ICMP_EQ, SJRet,
-                                   Context->getNullValue(SJRet->getType()),
+                                   Context.getNullValue(SJRet->getType()),
                                    "notunwind");
     // Nuke the uncond branch.
     EntryBB->getTerminator()->eraseFromParent();
@@ -563,20 +569,20 @@
 
   // Load the JBList, if it's null, then there was no catch!
   Value *NotNull = new ICmpInst(*UnwindHandler, ICmpInst::ICMP_NE, BufPtr,
-                                Context->getNullValue(BufPtr->getType()),
+                                Context.getNullValue(BufPtr->getType()),
                                 "notnull");
   BranchInst::Create(UnwindBlock, TermBlock, NotNull, UnwindHandler);
 
   // Create the block to do the longjmp.
   // Get a pointer to the jmpbuf and longjmp.
   std::vector<Value*> Idx;
-  Idx.push_back(Context->getNullValue(Type::Int32Ty));
-  Idx.push_back(Context->getConstantInt(Type::Int32Ty, 0));
+  Idx.push_back(Context.getNullValue(Type::Int32Ty));
+  Idx.push_back(Context.getConstantInt(Type::Int32Ty, 0));
   Idx[0] = GetElementPtrInst::Create(BufPtr, Idx.begin(), Idx.end(), "JmpBuf",
                                      UnwindBlock);
   Idx[0] = new BitCastInst(Idx[0], PointerType::getUnqual(Type::Int8Ty),
                            "tmp", UnwindBlock);
-  Idx[1] = Context->getConstantInt(Type::Int32Ty, 1);
+  Idx[1] = Context.getConstantInt(Type::Int32Ty, 1);
   CallInst::Create(LongJmpFn, Idx.begin(), Idx.end(), "", UnwindBlock);
   new UnreachableInst(UnwindBlock);
 
diff --git a/lib/Transforms/Utils/LowerSwitch.cpp b/lib/Transforms/Utils/LowerSwitch.cpp
index 2a7124c..b412147 100644
--- a/lib/Transforms/Utils/LowerSwitch.cpp
+++ b/lib/Transforms/Utils/LowerSwitch.cpp
@@ -162,7 +162,7 @@
   Function::iterator FI = OrigBlock;
   F->getBasicBlockList().insert(++FI, NewNode);
 
-  ICmpInst* Comp = new ICmpInst(*Default->getContext(), ICmpInst::ICMP_SLT,
+  ICmpInst* Comp = new ICmpInst(Default->getContext(), ICmpInst::ICMP_SLT,
                                 Val, Pivot.Low, "Pivot");
   NewNode->getInstList().push_back(Comp);
   BranchInst::Create(LBranch, RBranch, Comp, NewNode);
@@ -180,6 +180,7 @@
                                       BasicBlock* Default)
 {
   Function* F = OrigBlock->getParent();
+  LLVMContext &Context = F->getContext();
   BasicBlock* NewLeaf = BasicBlock::Create("LeafBlock");
   Function::iterator FI = OrigBlock;
   F->getBasicBlockList().insert(++FI, NewLeaf);
@@ -202,11 +203,11 @@
                           "SwitchLeaf");      
     } else {
       // Emit V-Lo <=u Hi-Lo
-      Constant* NegLo = Context->getConstantExprNeg(Leaf.Low);
+      Constant* NegLo = Context.getConstantExprNeg(Leaf.Low);
       Instruction* Add = BinaryOperator::CreateAdd(Val, NegLo,
                                                    Val->getName()+".off",
                                                    NewLeaf);
-      Constant *UpperBound = Context->getConstantExprAdd(NegLo, Leaf.High);
+      Constant *UpperBound = Context.getConstantExprAdd(NegLo, Leaf.High);
       Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Add, UpperBound,
                           "SwitchLeaf");
     }
diff --git a/lib/Transforms/Utils/Mem2Reg.cpp b/lib/Transforms/Utils/Mem2Reg.cpp
index 3110299..5df0832 100644
--- a/lib/Transforms/Utils/Mem2Reg.cpp
+++ b/lib/Transforms/Utils/Mem2Reg.cpp
@@ -75,7 +75,7 @@
 
     if (Allocas.empty()) break;
 
-    PromoteMemToReg(Allocas, DT, DF, Context);
+    PromoteMemToReg(Allocas, DT, DF, F.getContext());
     NumPromoted += Allocas.size();
     Changed = true;
   }
diff --git a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
index a05170e..bad7e4c 100644
--- a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
+++ b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
@@ -183,7 +183,7 @@
     ///
     AliasSetTracker *AST;
     
-    LLVMContext *Context;
+    LLVMContext &Context;
 
     /// AllocaLookup - Reverse mapping of Allocas.
     ///
@@ -216,7 +216,7 @@
   public:
     PromoteMem2Reg(const std::vector<AllocaInst*> &A, DominatorTree &dt,
                    DominanceFrontier &df, AliasSetTracker *ast,
-                   LLVMContext *C)
+                   LLVMContext &C)
       : Allocas(A), DT(dt), DF(df), AST(ast), Context(C) {}
 
     void run();
@@ -449,7 +449,7 @@
   //
   RenamePassData::ValVector Values(Allocas.size());
   for (unsigned i = 0, e = Allocas.size(); i != e; ++i)
-    Values[i] = Context->getUndef(Allocas[i]->getAllocatedType());
+    Values[i] = Context.getUndef(Allocas[i]->getAllocatedType());
 
   // Walks all basic blocks in the function performing the SSA rename algorithm
   // and inserting the phi nodes we marked as necessary
@@ -476,7 +476,7 @@
     // Just delete the users now.
     //
     if (!A->use_empty())
-      A->replaceAllUsesWith(Context->getUndef(A->getType()));
+      A->replaceAllUsesWith(Context.getUndef(A->getType()));
     if (AST) AST->deleteValue(A);
     A->eraseFromParent();
   }
@@ -562,7 +562,7 @@
     BasicBlock::iterator BBI = BB->begin();
     while ((SomePHI = dyn_cast<PHINode>(BBI++)) &&
            SomePHI->getNumIncomingValues() == NumBadPreds) {
-      Value *UndefVal = Context->getUndef(SomePHI->getType());
+      Value *UndefVal = Context.getUndef(SomePHI->getType());
       for (unsigned pred = 0, e = Preds.size(); pred != e; ++pred)
         SomePHI->addIncoming(UndefVal, Preds[pred]);
     }
@@ -808,7 +808,7 @@
   if (StoresByIndex.empty()) {
     for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;) 
       if (LoadInst *LI = dyn_cast<LoadInst>(*UI++)) {
-        LI->replaceAllUsesWith(Context->getUndef(LI->getType()));
+        LI->replaceAllUsesWith(Context.getUndef(LI->getType()));
         if (AST && isa<PointerType>(LI->getType()))
           AST->deleteValue(LI);
         LBI.deleteValue(LI);
@@ -999,7 +999,7 @@
 ///
 void llvm::PromoteMemToReg(const std::vector<AllocaInst*> &Allocas,
                            DominatorTree &DT, DominanceFrontier &DF,
-                           LLVMContext *Context, AliasSetTracker *AST) {
+                           LLVMContext &Context, AliasSetTracker *AST) {
   // If there is nothing to do, bail out...
   if (Allocas.empty()) return;
 
diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp
index 3b6b77f..e155c29 100644
--- a/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -927,7 +927,7 @@
     return true;
 
   // Okay, it is safe to hoist the terminator.
-  Instruction *NT = I1->clone(*BB1->getContext());
+  Instruction *NT = I1->clone(BB1->getContext());
   BIParent->getInstList().insert(BI, NT);
   if (NT->getType() != Type::VoidTy) {
     I1->replaceAllUsesWith(NT);
@@ -1167,7 +1167,7 @@
 /// ultimate destination.
 static bool FoldCondBranchOnPHI(BranchInst *BI) {
   BasicBlock *BB = BI->getParent();
-  LLVMContext *Context = BB->getContext();
+  LLVMContext &Context = BB->getContext();
   PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
   // NOTE: we currently cannot transform this case if the PHI node is used
   // outside of the block.
@@ -1220,7 +1220,7 @@
           TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB);
         } else {
           // Clone the instruction.
-          Instruction *N = BBI->clone(*Context);
+          Instruction *N = BBI->clone(Context);
           if (BBI->hasName()) N->setName(BBI->getName()+".c");
           
           // Update operands due to translation.
@@ -1265,7 +1265,7 @@
 /// FoldTwoEntryPHINode - Given a BB that starts with the specified two-entry
 /// PHI node, see if we can eliminate it.
 static bool FoldTwoEntryPHINode(PHINode *PN) {
-  LLVMContext *Context = PN->getParent()->getContext();
+  LLVMContext &Context = PN->getParent()->getContext();
   
   // Ok, this is a two entry PHI node.  Check to see if this is a simple "if
   // statement", which has a very simple dominance structure.  Basically, we
@@ -1304,7 +1304,7 @@
       if (PN->getIncomingValue(0) != PN)
         PN->replaceAllUsesWith(PN->getIncomingValue(0));
       else
-        PN->replaceAllUsesWith(Context->getUndef(PN->getType()));
+        PN->replaceAllUsesWith(Context.getUndef(PN->getType()));
     } else if (!DominatesMergePoint(PN->getIncomingValue(0), BB,
                                     &AggressiveInsts) ||
                !DominatesMergePoint(PN->getIncomingValue(1), BB,
@@ -1559,7 +1559,7 @@
     // If we need to invert the condition in the pred block to match, do so now.
     if (InvertPredCond) {
       Value *NewCond =
-        BinaryOperator::CreateNot(*BI->getParent()->getContext(), 
+        BinaryOperator::CreateNot(BI->getParent()->getContext(), 
                                   PBI->getCondition(), 
                                   PBI->getCondition()->getName()+".not", PBI);
       PBI->setCondition(NewCond);
@@ -1571,7 +1571,7 @@
     
     // Clone Cond into the predecessor basic block, and or/and the
     // two conditions together.
-    Instruction *New = Cond->clone(*BB->getContext());
+    Instruction *New = Cond->clone(BB->getContext());
     PredBlock->getInstList().insert(PBI, New);
     New->takeName(Cond);
     Cond->setName(New->getName()+".old");
@@ -1599,7 +1599,7 @@
 static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
   assert(PBI->isConditional() && BI->isConditional());
   BasicBlock *BB = BI->getParent();
-  LLVMContext *Context = BB->getContext();
+  LLVMContext &Context = BB->getContext();
   
   // If this block ends with a branch instruction, and if there is a
   // predecessor that ends on a branch of the same condition, make 
@@ -1611,7 +1611,7 @@
     if (BB->getSinglePredecessor()) {
       // Turn this into a branch on constant.
       bool CondIsTrue = PBI->getSuccessor(0) == BB;
-      BI->setCondition(Context->getConstantInt(Type::Int1Ty, CondIsTrue));
+      BI->setCondition(Context.getConstantInt(Type::Int1Ty, CondIsTrue));
       return true;  // Nuke the branch on constant.
     }
     
@@ -1631,7 +1631,7 @@
             PBI->getCondition() == BI->getCondition() &&
             PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
           bool CondIsTrue = PBI->getSuccessor(0) == BB;
-          NewPN->addIncoming(Context->getConstantInt(Type::Int1Ty, 
+          NewPN->addIncoming(Context.getConstantInt(Type::Int1Ty, 
                                               CondIsTrue), *PI);
         } else {
           NewPN->addIncoming(BI->getCondition(), *PI);
@@ -1716,12 +1716,12 @@
   // Make sure we get to CommonDest on True&True directions.
   Value *PBICond = PBI->getCondition();
   if (PBIOp)
-    PBICond = BinaryOperator::CreateNot(*Context, PBICond,
+    PBICond = BinaryOperator::CreateNot(Context, PBICond,
                                         PBICond->getName()+".not",
                                         PBI);
   Value *BICond = BI->getCondition();
   if (BIOp)
-    BICond = BinaryOperator::CreateNot(*Context, BICond,
+    BICond = BinaryOperator::CreateNot(Context, BICond,
                                        BICond->getName()+".not",
                                        PBI);
   // Merge the conditions.
@@ -1831,7 +1831,7 @@
                << "INTO UNCOND BRANCH PRED: " << *Pred;
           Instruction *UncondBranch = Pred->getTerminator();
           // Clone the return and add it to the end of the predecessor.
-          Instruction *NewRet = RI->clone(*BB->getContext());
+          Instruction *NewRet = RI->clone(BB->getContext());
           Pred->getInstList().push_back(NewRet);
 
           BasicBlock::iterator BBI = RI;
diff --git a/lib/Transforms/Utils/ValueMapper.cpp b/lib/Transforms/Utils/ValueMapper.cpp
index f0b24c1..0dc012e 100644
--- a/lib/Transforms/Utils/ValueMapper.cpp
+++ b/lib/Transforms/Utils/ValueMapper.cpp
@@ -23,7 +23,7 @@
 #include "llvm/Support/ErrorHandling.h"
 using namespace llvm;
 
-Value *llvm::MapValue(const Value *V, ValueMapTy &VM, LLVMContext *Context) {
+Value *llvm::MapValue(const Value *V, ValueMapTy &VM, LLVMContext &Context) {
   Value *&VMSlot = VM[V];
   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
   
@@ -55,7 +55,7 @@
           Values.push_back(cast<Constant>(MV));
           for (++i; i != e; ++i)
             Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
-          return VM[V] = Context->getConstantArray(CA->getType(), Values);
+          return VM[V] = Context.getConstantArray(CA->getType(), Values);
         }
       }
       return VM[V] = C;
@@ -75,7 +75,7 @@
           Values.push_back(cast<Constant>(MV));
           for (++i; i != e; ++i)
             Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
-          return VM[V] = Context->getConstantStruct(CS->getType(), Values);
+          return VM[V] = Context.getConstantStruct(CS->getType(), Values);
         }
       }
       return VM[V] = C;
@@ -100,7 +100,7 @@
           Values.push_back(cast<Constant>(MV));
           for (++i; i != e; ++i)
             Values.push_back(cast<Constant>(MapValue(*i, VM, Context)));
-          return VM[V] = Context->getConstantVector(Values);
+          return VM[V] = Context.getConstantVector(Values);
         }
       }
       return VM[V] = C;
@@ -121,7 +121,7 @@
           Values.push_back(MV);
           for (++i; i != e; ++i)
             Values.push_back(MapValue(*i, VM, Context));
-          return VM[V] = Context->getMDNode(Values.data(), Values.size());
+          return VM[V] = Context.getMDNode(Values.data(), Values.size());
         }
       }
       return VM[V] = C;