Remove every uses of getGlobalContext() in LLVM (but the C API)

At the same time, fixes InstructionsTest::CastInst unittest: yes
you can leave the IR in an invalid state and exit when you don't
destroy the context (like the global one), no longer now.

This is the first part of http://reviews.llvm.org/D19094

From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 266379
diff --git a/llvm/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp b/llvm/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp
index 38b60ea..ac84703 100644
--- a/llvm/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp
@@ -17,7 +17,8 @@
 namespace {
 
 TEST(IndirectionUtilsTest, MakeStub) {
-  ModuleBuilder MB(getGlobalContext(), "x86_64-apple-macosx10.10", "");
+  LLVMContext Context;
+  ModuleBuilder MB(Context, "x86_64-apple-macosx10.10", "");
   Function *F = MB.createFunctionDecl<void(DummyStruct, DummyStruct)>("");
   SmallVector<AttributeSet, 4> Attrs;
   Attrs.push_back(
diff --git a/llvm/unittests/ExecutionEngine/Orc/ObjectLinkingLayerTest.cpp b/llvm/unittests/ExecutionEngine/Orc/ObjectLinkingLayerTest.cpp
index a733bd5..89adb66 100644
--- a/llvm/unittests/ExecutionEngine/Orc/ObjectLinkingLayerTest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/ObjectLinkingLayerTest.cpp
@@ -25,6 +25,7 @@
 
 class ObjectLinkingLayerExecutionTest : public testing::Test,
                                         public OrcExecutionTest {
+
 };
 
 class SectionMemoryManagerWrapper : public SectionMemoryManager {
@@ -64,9 +65,10 @@
 
   ObjectLinkingLayer<> ObjLayer;
 
-  auto M = llvm::make_unique<Module>("", getGlobalContext());
+  LLVMContext Context;
+  auto M = llvm::make_unique<Module>("", Context);
   M->setTargetTriple("x86_64-unknown-linux-gnu");
-  Type *Int32Ty = IntegerType::get(getGlobalContext(), 32);
+  Type *Int32Ty = IntegerType::get(Context, 32);
   GlobalVariable *GV =
     new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
                          ConstantInt::get(Int32Ty, 42), "foo");
@@ -131,14 +133,13 @@
   // instance (for Module 1) which is unsafe, as it will prevent relocation of
   // Module 2.
 
-  ModuleBuilder MB1(getGlobalContext(), "", "dummy");
+  ModuleBuilder MB1(Context, "", "dummy");
   {
     MB1.getModule()->setDataLayout(TM->createDataLayout());
     Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("bar");
-    BasicBlock *BarEntry = BasicBlock::Create(getGlobalContext(), "entry",
-                                              BarImpl);
+    BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
     IRBuilder<> Builder(BarEntry);
-    IntegerType *Int32Ty = IntegerType::get(getGlobalContext(), 32);
+    IntegerType *Int32Ty = IntegerType::get(Context, 32);
     Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42);
     Builder.CreateRet(FourtyTwo);
   }
@@ -147,13 +148,12 @@
   std::vector<object::ObjectFile*> Obj1Set;
   Obj1Set.push_back(Obj1.getBinary());
 
-  ModuleBuilder MB2(getGlobalContext(), "", "dummy");
+  ModuleBuilder MB2(Context, "", "dummy");
   {
     MB2.getModule()->setDataLayout(TM->createDataLayout());
     Function *BarDecl = MB2.createFunctionDecl<int32_t(void)>("bar");
     Function *FooImpl = MB2.createFunctionDecl<int32_t(void)>("foo");
-    BasicBlock *FooEntry = BasicBlock::Create(getGlobalContext(), "entry",
-                                              FooImpl);
+    BasicBlock *FooEntry = BasicBlock::Create(Context, "entry", FooImpl);
     IRBuilder<> Builder(FooEntry);
     Builder.CreateRet(Builder.CreateCall(BarDecl));
   }
@@ -203,14 +203,13 @@
   // RuntimeDyld::MemoryManager::needsToReserveAllocationSpace hook, which is
   // called once per object before any sections are allocated.
 
-  ModuleBuilder MB1(getGlobalContext(), "", "dummy");
+  ModuleBuilder MB1(Context, "", "dummy");
   {
     MB1.getModule()->setDataLayout(TM->createDataLayout());
     Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("foo");
-    BasicBlock *BarEntry = BasicBlock::Create(getGlobalContext(), "entry",
-                                              BarImpl);
+    BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
     IRBuilder<> Builder(BarEntry);
-    IntegerType *Int32Ty = IntegerType::get(getGlobalContext(), 32);
+    IntegerType *Int32Ty = IntegerType::get(Context, 32);
     Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42);
     Builder.CreateRet(FourtyTwo);
   }
@@ -219,14 +218,13 @@
   std::vector<object::ObjectFile*> Obj1Set;
   Obj1Set.push_back(Obj1.getBinary());
 
-  ModuleBuilder MB2(getGlobalContext(), "", "dummy");
+  ModuleBuilder MB2(Context, "", "dummy");
   {
     MB2.getModule()->setDataLayout(TM->createDataLayout());
     Function *BarImpl = MB2.createFunctionDecl<int32_t(void)>("bar");
-    BasicBlock *BarEntry = BasicBlock::Create(getGlobalContext(), "entry",
-                                              BarImpl);
+    BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
     IRBuilder<> Builder(BarEntry);
-    IntegerType *Int32Ty = IntegerType::get(getGlobalContext(), 32);
+    IntegerType *Int32Ty = IntegerType::get(Context, 32);
     Value *Seven = ConstantInt::getSigned(Int32Ty, 7);
     Builder.CreateRet(Seven);
   }
diff --git a/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp b/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp
index 776d269..305325b 100644
--- a/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp
@@ -25,11 +25,11 @@
 class OrcCAPIExecutionTest : public testing::Test, public OrcExecutionTest {
 protected:
   std::unique_ptr<Module> createTestModule(const Triple &TT) {
-    ModuleBuilder MB(getGlobalContext(), TT.str(), "");
+    ModuleBuilder MB(Context, TT.str(), "");
     Function *TestFunc = MB.createFunctionDecl<int()>("testFunc");
     Function *Main = MB.createFunctionDecl<int(int, char*[])>("main");
 
-    Main->getBasicBlockList().push_back(BasicBlock::Create(getGlobalContext()));
+    Main->getBasicBlockList().push_back(BasicBlock::Create(Context));
     IRBuilder<> B(&Main->back());
     Value* Result = B.CreateCall(TestFunc);
     B.CreateRet(Result);
diff --git a/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h b/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h
index e8fab56..fe3da88 100644
--- a/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h
+++ b/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h
@@ -54,6 +54,7 @@
   };
 
 protected:
+  LLVMContext Context;
   std::unique_ptr<TargetMachine> TM;
 private:
   static bool NativeTargetInitialized;