Move the ConstantInt uniquing table into LLVMContextImpl.  This exposed a number of issues in
our current context-passing stuff, which is also fixed here


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76089 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/IPO/DeadArgumentElimination.cpp b/lib/Transforms/IPO/DeadArgumentElimination.cpp
index 2e91391..e86f619 100644
--- a/lib/Transforms/IPO/DeadArgumentElimination.cpp
+++ b/lib/Transforms/IPO/DeadArgumentElimination.cpp
@@ -908,6 +908,7 @@
 
 bool DAE::runOnModule(Module &M) {
   bool Changed = false;
+  Context = &M.getContext();
 
   // First pass: Do a simple check to see if any functions can have their "..."
   // removed.  We can do this if they never call va_start.  This loop cannot be
diff --git a/lib/Transforms/IPO/DeadTypeElimination.cpp b/lib/Transforms/IPO/DeadTypeElimination.cpp
index 85aed2b..f202403 100644
--- a/lib/Transforms/IPO/DeadTypeElimination.cpp
+++ b/lib/Transforms/IPO/DeadTypeElimination.cpp
@@ -77,6 +77,7 @@
 //
 bool DTE::runOnModule(Module &M) {
   bool Changed = false;
+  Context = &M.getContext();
 
   TypeSymbolTable &ST = M.getTypeSymbolTable();
   std::set<const Type *> UsedTypes = getAnalysis<FindUsedTypes>().getTypes();
diff --git a/lib/Transforms/IPO/ExtractGV.cpp b/lib/Transforms/IPO/ExtractGV.cpp
index b7c5ca3..fa3a055 100644
--- a/lib/Transforms/IPO/ExtractGV.cpp
+++ b/lib/Transforms/IPO/ExtractGV.cpp
@@ -44,6 +44,8 @@
         return false;  // Nothing to extract
       }
       
+      Context = &M.getContext();
+      
       if (deleteStuff)
         return deleteGV();
       M.setModuleInlineAsm("");
diff --git a/lib/Transforms/IPO/GlobalDCE.cpp b/lib/Transforms/IPO/GlobalDCE.cpp
index 9c652b9..9ecc494 100644
--- a/lib/Transforms/IPO/GlobalDCE.cpp
+++ b/lib/Transforms/IPO/GlobalDCE.cpp
@@ -58,6 +58,8 @@
 
 bool GlobalDCE::runOnModule(Module &M) {
   bool Changed = false;
+  Context = &M.getContext();
+  
   // Loop over the module, adding globals which are obviously necessary.
   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
     Changed |= RemoveUnusedGlobalValue(*I);
diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp
index 398f78a..8b3cf48 100644
--- a/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/lib/Transforms/IPO/GlobalOpt.cpp
@@ -2476,6 +2476,7 @@
 
 bool GlobalOpt::runOnModule(Module &M) {
   bool Changed = false;
+  Context = &M.getContext();
   
   // Try to find the llvm.globalctors list.
   GlobalVariable *GlobalCtors = FindGlobalCtors(M);
diff --git a/lib/Transforms/IPO/IPConstantPropagation.cpp b/lib/Transforms/IPO/IPConstantPropagation.cpp
index e27792a..30834e2 100644
--- a/lib/Transforms/IPO/IPConstantPropagation.cpp
+++ b/lib/Transforms/IPO/IPConstantPropagation.cpp
@@ -56,6 +56,8 @@
   bool Changed = false;
   bool LocalChange = true;
 
+  Context = &M.getContext();
+
   // FIXME: instead of using smart algorithms, we just iterate until we stop
   // making changes.
   while (LocalChange) {
diff --git a/lib/Transforms/IPO/IndMemRemoval.cpp b/lib/Transforms/IPO/IndMemRemoval.cpp
index 2086a16..7b9b0cc 100644
--- a/lib/Transforms/IPO/IndMemRemoval.cpp
+++ b/lib/Transforms/IPO/IndMemRemoval.cpp
@@ -44,6 +44,8 @@
 X("indmemrem","Indirect Malloc and Free Removal");
 
 bool IndMemRemPass::runOnModule(Module &M) {
+  Context = &M.getContext();
+  
   // In theory, all direct calls of malloc and free should be promoted
   // to intrinsics.  Therefore, this goes through and finds where the
   // address of free or malloc are taken and replaces those with bounce
diff --git a/lib/Transforms/IPO/Internalize.cpp b/lib/Transforms/IPO/Internalize.cpp
index 5093ae9..9bc9172 100644
--- a/lib/Transforms/IPO/Internalize.cpp
+++ b/lib/Transforms/IPO/Internalize.cpp
@@ -101,6 +101,8 @@
 bool InternalizePass::runOnModule(Module &M) {
   CallGraph *CG = getAnalysisIfAvailable<CallGraph>();
   CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
+  
+  Context = &M.getContext();
 
   if (ExternalNames.empty()) {
     // Return if we're not in 'all but main' mode and have no external api
diff --git a/lib/Transforms/IPO/LowerSetJmp.cpp b/lib/Transforms/IPO/LowerSetJmp.cpp
index dfdf5b6..bfa2bd9 100644
--- a/lib/Transforms/IPO/LowerSetJmp.cpp
+++ b/lib/Transforms/IPO/LowerSetJmp.cpp
@@ -134,6 +134,8 @@
 bool LowerSetJmp::runOnModule(Module& M) {
   bool Changed = false;
 
+  Context = &M.getContext();
+
   // These are what the functions are called.
   Function* SetJmp = M.getFunction("llvm.setjmp");
   Function* LongJmp = M.getFunction("llvm.longjmp");
diff --git a/lib/Transforms/IPO/MergeFunctions.cpp b/lib/Transforms/IPO/MergeFunctions.cpp
index 9cc4daa..e93a087 100644
--- a/lib/Transforms/IPO/MergeFunctions.cpp
+++ b/lib/Transforms/IPO/MergeFunctions.cpp
@@ -615,6 +615,8 @@
 bool MergeFunctions::runOnModule(Module &M) {
   bool Changed = false;
 
+  Context = &M.getContext();
+
   std::map<unsigned long, std::vector<Function *> > FnMap;
 
   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
diff --git a/lib/Transforms/IPO/PartialInlining.cpp b/lib/Transforms/IPO/PartialInlining.cpp
index 73ec9c1..a6e090b 100644
--- a/lib/Transforms/IPO/PartialInlining.cpp
+++ b/lib/Transforms/IPO/PartialInlining.cpp
@@ -141,6 +141,8 @@
 }
 
 bool PartialInliner::runOnModule(Module& M) {
+  Context = &M.getContext();
+  
   std::vector<Function*> worklist;
   worklist.reserve(M.size());
   for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
diff --git a/lib/Transforms/IPO/PartialSpecialization.cpp b/lib/Transforms/IPO/PartialSpecialization.cpp
index 0e1fdb9..3b2ce14 100644
--- a/lib/Transforms/IPO/PartialSpecialization.cpp
+++ b/lib/Transforms/IPO/PartialSpecialization.cpp
@@ -108,6 +108,8 @@
 
 
 bool PartSpec::runOnModule(Module &M) {
+  Context = &M.getContext();
+  
   bool Changed = false;
   for (Module::iterator I = M.begin(); I != M.end(); ++I) {
     Function &F = *I;
diff --git a/lib/Transforms/IPO/RaiseAllocations.cpp b/lib/Transforms/IPO/RaiseAllocations.cpp
index 6c32050..8d2a9cb 100644
--- a/lib/Transforms/IPO/RaiseAllocations.cpp
+++ b/lib/Transforms/IPO/RaiseAllocations.cpp
@@ -70,6 +70,7 @@
 // function into the appropriate instruction.
 //
 void RaiseAllocations::doInitialization(Module &M) {
+  Context = &M.getContext();
 
   // Get Malloc and free prototypes if they exist!
   MallocFunc = M.getFunction("malloc");
diff --git a/lib/Transforms/IPO/StripDeadPrototypes.cpp b/lib/Transforms/IPO/StripDeadPrototypes.cpp
index a94d78e..2a17791 100644
--- a/lib/Transforms/IPO/StripDeadPrototypes.cpp
+++ b/lib/Transforms/IPO/StripDeadPrototypes.cpp
@@ -42,6 +42,7 @@
 
 bool StripDeadPrototypesPass::runOnModule(Module &M) {
   bool MadeChange = false;
+  Context = &M.getContext();
   
   // Erase dead function prototypes.
   for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
diff --git a/lib/Transforms/IPO/StripSymbols.cpp b/lib/Transforms/IPO/StripSymbols.cpp
index 323d180..82a0952 100644
--- a/lib/Transforms/IPO/StripSymbols.cpp
+++ b/lib/Transforms/IPO/StripSymbols.cpp
@@ -374,6 +374,7 @@
 }
 
 bool StripSymbols::runOnModule(Module &M) {
+  Context = &M.getContext();
   bool Changed = false;
   Changed |= StripDebugInfo(M);
   if (!OnlyDebugInfo)
diff --git a/lib/Transforms/Scalar/PredicateSimplifier.cpp b/lib/Transforms/Scalar/PredicateSimplifier.cpp
index 50d6063..51de4f7 100644
--- a/lib/Transforms/Scalar/PredicateSimplifier.cpp
+++ b/lib/Transforms/Scalar/PredicateSimplifier.cpp
@@ -905,6 +905,7 @@
   class VISIBILITY_HIDDEN ValueRanges {
     ValueNumbering &VN;
     TargetData *TD;
+    LLVMContext *Context;
 
     class VISIBILITY_HIDDEN ScopedRange {
       typedef std::vector<std::pair<DomTreeDFS::Node *, ConstantRange> >
@@ -1025,7 +1026,8 @@
 
   public:
 
-    ValueRanges(ValueNumbering &VN, TargetData *TD) : VN(VN), TD(TD) {}
+    ValueRanges(ValueNumbering &VN, TargetData *TD, LLVMContext *C) :
+      VN(VN), TD(TD), Context(C) {}
 
 #ifndef NDEBUG
     virtual ~ValueRanges() {}
@@ -1167,7 +1169,7 @@
         Value *V = VN.value(n); // XXX: redesign worklist.
         const Type *Ty = V->getType();
         if (Ty->isInteger()) {
-          addToWorklist(V, ConstantInt::get(*I), ICmpInst::ICMP_EQ, VRP);
+          addToWorklist(V, Context->getConstantInt(*I), ICmpInst::ICMP_EQ, VRP);
           return;
         } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
           assert(*I == 0 && "Pointer is null but not zero?");
@@ -1678,7 +1680,8 @@
         Top(DTDFS->getNodeForBlock(TopInst->getParent())),
         TopBB(TopInst->getParent()),
         TopInst(TopInst),
-        modified(modified)
+        modified(modified),
+        Context(TopInst->getParent()->getContext())
     {
       assert(Top && "VRPSolver created for unreachable basic block.");
       assert(Top->getBlock() == TopInst->getParent() && "Context mismatch.");
@@ -1779,7 +1782,8 @@
 
             if (ConstantInt *CI = dyn_cast<ConstantInt>(Canonical)) {
               if (ConstantInt *Arg = dyn_cast<ConstantInt>(LHS)) {
-                add(RHS, ConstantInt::get(CI->getValue() ^ Arg->getValue()),
+                add(RHS,
+                    Context->getConstantInt(CI->getValue() ^ Arg->getValue()),
                     ICmpInst::ICMP_EQ, NewContext);
               }
             }
@@ -2404,7 +2408,7 @@
     DomTreeDFS::Node *Root = DTDFS->getRootNode();
     VN = new ValueNumbering(DTDFS);
     IG = new InequalityGraph(*VN, Root);
-    VR = new ValueRanges(*VN, TD);
+    VR = new ValueRanges(*VN, TD, Context);
     WorkList.push_back(Root);
 
     do {
@@ -2526,21 +2530,23 @@
 
   void PredicateSimplifier::Forwards::visitSExtInst(SExtInst &SI) {
     VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &SI);
+    LLVMContext *Context = SI.getParent()->getContext();
     uint32_t SrcBitWidth = cast<IntegerType>(SI.getSrcTy())->getBitWidth();
     uint32_t DstBitWidth = cast<IntegerType>(SI.getDestTy())->getBitWidth();
     APInt Min(APInt::getHighBitsSet(DstBitWidth, DstBitWidth-SrcBitWidth+1));
     APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth-1));
-    VRP.add(ConstantInt::get(Min), &SI, ICmpInst::ICMP_SLE);
-    VRP.add(ConstantInt::get(Max), &SI, ICmpInst::ICMP_SGE);
+    VRP.add(Context->getConstantInt(Min), &SI, ICmpInst::ICMP_SLE);
+    VRP.add(Context->getConstantInt(Max), &SI, ICmpInst::ICMP_SGE);
     VRP.solve();
   }
 
   void PredicateSimplifier::Forwards::visitZExtInst(ZExtInst &ZI) {
     VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &ZI);
+    LLVMContext *Context = ZI.getParent()->getContext();
     uint32_t SrcBitWidth = cast<IntegerType>(ZI.getSrcTy())->getBitWidth();
     uint32_t DstBitWidth = cast<IntegerType>(ZI.getDestTy())->getBitWidth();
     APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth));
-    VRP.add(ConstantInt::get(Max), &ZI, ICmpInst::ICMP_UGE);
+    VRP.add(Context->getConstantInt(Max), &ZI, ICmpInst::ICMP_UGE);
     VRP.solve();
   }
 
@@ -2629,6 +2635,8 @@
 
     Pred = IC.getPredicate();
 
+    LLVMContext *Context = IC.getParent()->getContext();
+
     if (ConstantInt *Op1 = dyn_cast<ConstantInt>(IC.getOperand(1))) {
       ConstantInt *NextVal = 0;
       switch (Pred) {
@@ -2636,12 +2644,12 @@
         case ICmpInst::ICMP_SLT:
         case ICmpInst::ICMP_ULT:
           if (Op1->getValue() != 0)
-            NextVal = ConstantInt::get(Op1->getValue()-1);
+            NextVal = Context->getConstantInt(Op1->getValue()-1);
          break;
         case ICmpInst::ICMP_SGT:
         case ICmpInst::ICMP_UGT:
           if (!Op1->getValue().isAllOnesValue())
-            NextVal = ConstantInt::get(Op1->getValue()+1);
+            NextVal = Context->getConstantInt(Op1->getValue()+1);
          break;
       }
 
diff --git a/lib/Transforms/Scalar/SCCP.cpp b/lib/Transforms/Scalar/SCCP.cpp
index 6ada288..7fa1a08 100644
--- a/lib/Transforms/Scalar/SCCP.cpp
+++ b/lib/Transforms/Scalar/SCCP.cpp
@@ -1662,7 +1662,10 @@
 }
 
 bool IPSCCP::runOnModule(Module &M) {
+  Context = &M.getContext();
+  
   SCCPSolver Solver;
+  Solver.setContext(Context);
 
   // Loop over all functions, marking arguments to those with their addresses
   // taken or that are external as overdefined.
diff --git a/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/lib/Transforms/Scalar/SimplifyLibCalls.cpp
index 8a28ca2..f285406 100644
--- a/lib/Transforms/Scalar/SimplifyLibCalls.cpp
+++ b/lib/Transforms/Scalar/SimplifyLibCalls.cpp
@@ -1730,6 +1730,8 @@
 /// doInitialization - Add attributes to well-known functions.
 ///
 bool SimplifyLibCalls::doInitialization(Module &M) {
+  Context = &M.getContext();
+  
   Modified = false;
   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
     Function &F = *I;
diff --git a/lib/Transforms/Utils/LowerInvoke.cpp b/lib/Transforms/Utils/LowerInvoke.cpp
index fae3edf..1417f10 100644
--- a/lib/Transforms/Utils/LowerInvoke.cpp
+++ b/lib/Transforms/Utils/LowerInvoke.cpp
@@ -115,6 +115,8 @@
 // doInitialization - Make sure that there is a prototype for abort in the
 // current module.
 bool LowerInvoke::doInitialization(Module &M) {
+  Context = &M.getContext();
+  
   const Type *VoidPtrTy = Context->getPointerTypeUnqual(Type::Int8Ty);
   AbortMessage = 0;
   if (ExpensiveEHSupport) {