Make it explicit that ExecutionEngine takes ownership of the modules.

llvm-svn: 215967
diff --git a/llvm/unittests/ExecutionEngine/ExecutionEngineTest.cpp b/llvm/unittests/ExecutionEngine/ExecutionEngineTest.cpp
index f23745c..06a176f 100644
--- a/llvm/unittests/ExecutionEngine/ExecutionEngineTest.cpp
+++ b/llvm/unittests/ExecutionEngine/ExecutionEngineTest.cpp
@@ -20,9 +20,10 @@
 
 class ExecutionEngineTest : public testing::Test {
 protected:
-  ExecutionEngineTest()
-    : M(new Module("<main>", getGlobalContext())), Error(""),
-      Engine(EngineBuilder(M).setErrorStr(&Error).create()) {
+  ExecutionEngineTest() {
+    auto Owner = make_unique<Module>("<main>", getGlobalContext());
+    M = Owner.get();
+    Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create());
   }
 
   virtual void SetUp() {
@@ -35,9 +36,9 @@
                               GlobalValue::ExternalLinkage, nullptr, Name);
   }
 
-  Module *const M;
   std::string Error;
-  const std::unique_ptr<ExecutionEngine> Engine;
+  Module *M;  // Owned by ExecutionEngine.
+  std::unique_ptr<ExecutionEngine> Engine;
 };
 
 TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
diff --git a/llvm/unittests/ExecutionEngine/JIT/JITEventListenerTest.cpp b/llvm/unittests/ExecutionEngine/JIT/JITEventListenerTest.cpp
index 175b9fb..4b0862e 100644
--- a/llvm/unittests/ExecutionEngine/JIT/JITEventListenerTest.cpp
+++ b/llvm/unittests/ExecutionEngine/JIT/JITEventListenerTest.cpp
@@ -60,15 +60,16 @@
 
 class JITEventListenerTest : public testing::Test {
  protected:
-  JITEventListenerTest()
-      : M(new Module("module", getGlobalContext())),
-        EE(EngineBuilder(M)
-           .setEngineKind(EngineKind::JIT)
-           .create()) {
-  }
+   JITEventListenerTest() {
+     auto Owner = make_unique<Module>("module", getGlobalContext());
+     M = Owner.get();
+     EE.reset(EngineBuilder(std::move(Owner))
+                  .setEngineKind(EngineKind::JIT)
+                  .create());
+   }
 
   Module *M;
-  const std::unique_ptr<ExecutionEngine> EE;
+  std::unique_ptr<ExecutionEngine> EE;
 };
 
 // Tests on SystemZ disabled as we're running the old JIT
diff --git a/llvm/unittests/ExecutionEngine/JIT/JITTest.cpp b/llvm/unittests/ExecutionEngine/JIT/JITTest.cpp
index 817d207..6110cb5 100644
--- a/llvm/unittests/ExecutionEngine/JIT/JITTest.cpp
+++ b/llvm/unittests/ExecutionEngine/JIT/JITTest.cpp
@@ -184,15 +184,18 @@
   }
 
   virtual void SetUp() {
-    M = new Module("<main>", Context);
+    std::unique_ptr<Module> Owner = make_unique<Module>("<main>", Context);
+    M = Owner.get();
     RJMM = createMemoryManager();
     RJMM->setPoisonMemory(true);
     std::string Error;
     TargetOptions Options;
-    TheJIT.reset(EngineBuilder(M).setEngineKind(EngineKind::JIT)
-                 .setJITMemoryManager(RJMM)
-                 .setErrorStr(&Error)
-                 .setTargetOptions(Options).create());
+    TheJIT.reset(EngineBuilder(std::move(Owner))
+                     .setEngineKind(EngineKind::JIT)
+                     .setJITMemoryManager(RJMM)
+                     .setErrorStr(&Error)
+                     .setTargetOptions(Options)
+                     .create());
     ASSERT_TRUE(TheJIT.get() != nullptr) << Error;
   }
 
@@ -213,14 +216,15 @@
 // stays alive after that.
 TEST(JIT, GlobalInFunction) {
   LLVMContext context;
-  Module *M = new Module("<main>", context);
+  std::unique_ptr<Module> Owner = make_unique<Module>("<main>", context);
+  Module *M = Owner.get();
 
   JITMemoryManager *MemMgr = JITMemoryManager::CreateDefaultMemManager();
   // Tell the memory manager to poison freed memory so that accessing freed
   // memory is more easily tested.
   MemMgr->setPoisonMemory(true);
   std::string Error;
-  std::unique_ptr<ExecutionEngine> JIT(EngineBuilder(M)
+  std::unique_ptr<ExecutionEngine> JIT(EngineBuilder(std::move(Owner))
                                            .setEngineKind(EngineKind::JIT)
                                            .setErrorStr(&Error)
                                            .setJITMemoryManager(MemMgr)
@@ -638,9 +642,10 @@
     delete BitcodeBuffer;
     return nullptr;
   }
-  M = ModuleOrErr.get();
+  std::unique_ptr<Module> Owner(ModuleOrErr.get());
+  M = Owner.get();
   std::string errMsg;
-  ExecutionEngine *TheJIT = EngineBuilder(M)
+  ExecutionEngine *TheJIT = EngineBuilder(std::move(Owner))
     .setEngineKind(EngineKind::JIT)
     .setErrorStr(&errMsg)
     .create();
diff --git a/llvm/unittests/ExecutionEngine/JIT/MultiJITTest.cpp b/llvm/unittests/ExecutionEngine/JIT/MultiJITTest.cpp
index f530e0d..73acc6b 100644
--- a/llvm/unittests/ExecutionEngine/JIT/MultiJITTest.cpp
+++ b/llvm/unittests/ExecutionEngine/JIT/MultiJITTest.cpp
@@ -24,20 +24,20 @@
 #if !defined(__arm__) && !defined(__powerpc__) && !defined(__s390__) \
                       && !defined(__aarch64__)
 
-bool LoadAssemblyInto(Module *M, const char *assembly) {
+std::unique_ptr<Module> loadAssembly(LLVMContext &Context,
+                                     const char *Assembly) {
   SMDiagnostic Error;
-  bool success =
-    nullptr != ParseAssemblyString(assembly, M, Error, M->getContext());
+  std::unique_ptr<Module> Ret(
+      ParseAssemblyString(Assembly, nullptr, Error, Context));
   std::string errMsg;
   raw_string_ostream os(errMsg);
   Error.print("", os);
-  EXPECT_TRUE(success) << os.str();
-  return success;
+  EXPECT_TRUE((bool)Ret) << os.str();
+  return std::move(Ret);
 }
 
-void createModule1(LLVMContext &Context1, Module *&M1, Function *&FooF1) {
-  M1 = new Module("test1", Context1);
-  LoadAssemblyInto(M1,
+std::unique_ptr<Module> createModule1(LLVMContext &Context1, Function *&FooF1) {
+  std::unique_ptr<Module> Ret =  loadAssembly(Context1,
                    "define i32 @add1(i32 %ArgX1) { "
                    "entry: "
                    "  %addresult = add i32 1, %ArgX1 "
@@ -49,12 +49,12 @@
                    "  %add1 = call i32 @add1(i32 10) "
                    "  ret i32 %add1 "
                    "} ");
-  FooF1 = M1->getFunction("foo1");
+  FooF1 = Ret->getFunction("foo1");
+  return std::move(Ret);
 }
 
-void createModule2(LLVMContext &Context2, Module *&M2, Function *&FooF2) {
-  M2 = new Module("test2", Context2);
-  LoadAssemblyInto(M2,
+std::unique_ptr<Module> createModule2(LLVMContext &Context2, Function *&FooF2) {
+  std::unique_ptr<Module> Ret = loadAssembly(Context2,
                    "define i32 @add2(i32 %ArgX2) { "
                    "entry: "
                    "  %addresult = add i32 2, %ArgX2 "
@@ -66,24 +66,23 @@
                    "  %add2 = call i32 @add2(i32 10) "
                    "  ret i32 %add2 "
                    "} ");
-  FooF2 = M2->getFunction("foo2");
+  FooF2 = Ret->getFunction("foo2");
+  return std::move(Ret);
 }
 
 TEST(MultiJitTest, EagerMode) {
   LLVMContext Context1;
-  Module *M1 = nullptr;
   Function *FooF1 = nullptr;
-  createModule1(Context1, M1, FooF1);
+  std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
 
   LLVMContext Context2;
-  Module *M2 = nullptr;
   Function *FooF2 = nullptr;
-  createModule2(Context2, M2, FooF2);
+  std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
 
   // Now we create the JIT in eager mode
-  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(M1).create());
+  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
   EE1->DisableLazyCompilation(true);
-  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
   EE2->DisableLazyCompilation(true);
 
   // Call the `foo' function with no arguments:
@@ -101,19 +100,17 @@
 
 TEST(MultiJitTest, LazyMode) {
   LLVMContext Context1;
-  Module *M1 = nullptr;
   Function *FooF1 = nullptr;
-  createModule1(Context1, M1, FooF1);
+  std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
 
   LLVMContext Context2;
-  Module *M2 = nullptr;
   Function *FooF2 = nullptr;
-  createModule2(Context2, M2, FooF2);
+  std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
 
   // Now we create the JIT in lazy mode
-  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(M1).create());
+  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
   EE1->DisableLazyCompilation(false);
-  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
   EE2->DisableLazyCompilation(false);
 
   // Call the `foo' function with no arguments:
@@ -135,18 +132,16 @@
 
 TEST(MultiJitTest, JitPool) {
   LLVMContext Context1;
-  Module *M1 = nullptr;
   Function *FooF1 = nullptr;
-  createModule1(Context1, M1, FooF1);
+  std::unique_ptr<Module> M1 = createModule1(Context1, FooF1);
 
   LLVMContext Context2;
-  Module *M2 = nullptr;
   Function *FooF2 = nullptr;
-  createModule2(Context2, M2, FooF2);
+  std::unique_ptr<Module> M2 = createModule2(Context2, FooF2);
 
   // Now we create two JITs
-  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(M1).create());
-  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(M2).create());
+  std::unique_ptr<ExecutionEngine> EE1(EngineBuilder(std::move(M1)).create());
+  std::unique_ptr<ExecutionEngine> EE2(EngineBuilder(std::move(M2)).create());
 
   Function *F1 = EE1->FindFunctionNamed("foo1");
   void *foo1 = EE1->getPointerToFunction(F1);
diff --git a/llvm/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp b/llvm/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp
index c5ca36e4..b0d1bb3 100644
--- a/llvm/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp
+++ b/llvm/unittests/ExecutionEngine/MCJIT/MCJITMultipleModuleTest.cpp
@@ -94,8 +94,8 @@
   Function *FA, *FB;
   createTwoModuleCase(A, FA, B, FB);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
   checkAdd(ptr);
@@ -114,8 +114,8 @@
   Function *FA, *FB;
   createTwoModuleCase(A, FA, B, FB);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
   TheJIT->finalizeObject();
@@ -135,8 +135,8 @@
   Function *FA, *FB;
   createTwoModuleExternCase(A, FA, B, FB);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
   TheJIT->finalizeObject();
@@ -156,8 +156,8 @@
   Function *FA, *FB;
   createTwoModuleExternCase(A, FA, B, FB);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
   checkAdd(ptr);
@@ -177,8 +177,8 @@
   createTwoModuleExternCase(A, FA1, B, FB);
   FA2 = insertSimpleCallFunction<int32_t(int32_t, int32_t)>(A.get(), FA1);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FB->getName().str());
   TheJIT->finalizeObject();
@@ -213,8 +213,8 @@
   FB = startFunction<int32_t(void)>(B.get(), "FB");
   endFunctionWithRet(FB, Builder.CreateLoad(GVB));
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t FBPtr = TheJIT->getFunctionAddress(FB->getName().str());
   TheJIT->finalizeObject();
@@ -241,9 +241,9 @@
   Function *FA, *FB, *FC;
   createThreeModuleCase(A, FA, B, FB, C, FC);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
-  TheJIT->addModule(C.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
+  TheJIT->addModule(std::move(C));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FC->getName().str());
   checkAdd(ptr);
@@ -266,9 +266,9 @@
   Function *FA, *FB, *FC;
   createThreeModuleCase(A, FA, B, FB, C, FC);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
-  TheJIT->addModule(C.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
+  TheJIT->addModule(std::move(C));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
   checkAdd(ptr);
@@ -291,9 +291,9 @@
   Function *FA, *FB, *FC;
   createThreeModuleChainedCallsCase(A, FA, B, FB, C, FC);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
-  TheJIT->addModule(C.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
+  TheJIT->addModule(std::move(C));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FC->getName().str());
   checkAdd(ptr);
@@ -316,9 +316,9 @@
   Function *FA, *FB, *FC;
   createThreeModuleChainedCallsCase(A, FA, B, FB, C, FC);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
-  TheJIT->addModule(C.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
+  TheJIT->addModule(std::move(C));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
   checkAdd(ptr);
@@ -341,8 +341,8 @@
   Function *FA, *FB1, *FB2;
   createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FA->getName().str());
   checkAccumulate(ptr);
@@ -362,8 +362,8 @@
   Function *FA, *FB1, *FB2;
   createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FB1->getName().str());
   checkAccumulate(ptr);
@@ -383,8 +383,8 @@
   Function *FA, *FB1, *FB2;
   createCrossModuleRecursiveCase(A, FA, B, FB1, FB2);
 
-  createJIT(A.release());
-  TheJIT->addModule(B.release());
+  createJIT(std::move(A));
+  TheJIT->addModule(std::move(B));
 
   uint64_t ptr = TheJIT->getFunctionAddress(FB1->getName().str());
   checkAccumulate(ptr);
diff --git a/llvm/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp b/llvm/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp
index 4c3b5cf..5eebddb 100644
--- a/llvm/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp
+++ b/llvm/unittests/ExecutionEngine/MCJIT/MCJITObjectCacheTest.cpp
@@ -114,7 +114,7 @@
 TEST_F(MCJITObjectCacheTest, SetNullObjectCache) {
   SKIP_UNSUPPORTED_PLATFORM;
 
-  createJIT(M.release());
+  createJIT(std::move(M));
 
   TheJIT->setObjectCache(nullptr);
 
@@ -130,7 +130,7 @@
   // Save a copy of the module pointer before handing it off to MCJIT.
   const Module * SavedModulePointer = M.get();
 
-  createJIT(M.release());
+  createJIT(std::move(M));
 
   TheJIT->setObjectCache(Cache.get());
 
@@ -157,7 +157,7 @@
   std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
 
   // Compile this module with an MCJIT engine
-  createJIT(M.release());
+  createJIT(std::move(M));
   TheJIT->setObjectCache(Cache.get());
   TheJIT->finalizeObject();
 
@@ -174,7 +174,7 @@
   const Module * SecondModulePointer = M.get();
 
   // Create a new MCJIT instance to load this module then execute it.
-  createJIT(M.release());
+  createJIT(std::move(M));
   TheJIT->setObjectCache(Cache.get());
   compileAndRun();
 
@@ -191,7 +191,7 @@
   std::unique_ptr<TestObjectCache> Cache(new TestObjectCache);
 
   // Compile this module with an MCJIT engine
-  createJIT(M.release());
+  createJIT(std::move(M));
   TheJIT->setObjectCache(Cache.get());
   TheJIT->finalizeObject();
 
@@ -209,7 +209,7 @@
   const Module * SecondModulePointer = M.get();
 
   // Create a new MCJIT instance to load this module then execute it.
-  createJIT(M.release());
+  createJIT(std::move(M));
   TheJIT->setObjectCache(Cache.get());
 
   // Verify that our object cache does not contain the module yet.
diff --git a/llvm/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp b/llvm/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp
index c37c1d1..15e0efd 100644
--- a/llvm/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp
+++ b/llvm/unittests/ExecutionEngine/MCJIT/MCJITTest.cpp
@@ -49,7 +49,7 @@
 
   int initialValue = 5;
   GlobalValue *Global = insertGlobalInt32(M.get(), "test_global", initialValue);
-  createJIT(M.release());
+  createJIT(std::move(M));
   void *globalPtr =  TheJIT->getPointerToGlobal(Global);
   EXPECT_TRUE(nullptr != globalPtr)
     << "Unable to get pointer to global value from JIT";
@@ -62,7 +62,7 @@
   SKIP_UNSUPPORTED_PLATFORM;
 
   Function *F = insertAddFunction(M.get());
-  createJIT(M.release());
+  createJIT(std::move(M));
   uint64_t addPtr = TheJIT->getFunctionAddress(F->getName().str());
   EXPECT_TRUE(0 != addPtr)
     << "Unable to get pointer to function from JIT";
@@ -83,7 +83,7 @@
 
   int rc = 6;
   Function *Main = insertMainFunction(M.get(), 6);
-  createJIT(M.release());
+  createJIT(std::move(M));
   uint64_t ptr = TheJIT->getFunctionAddress(Main->getName().str());
   EXPECT_TRUE(0 != ptr)
     << "Unable to get pointer to main() from JIT";
@@ -104,7 +104,7 @@
   Value *ReadGlobal = Builder.CreateLoad(GV);
   endFunctionWithRet(ReturnGlobal, ReadGlobal);
 
-  createJIT(M.release());
+  createJIT(std::move(M));
   uint64_t rgvPtr = TheJIT->getFunctionAddress(ReturnGlobal->getName().str());
   EXPECT_TRUE(0 != rgvPtr);
 
@@ -175,7 +175,7 @@
     Inner = Outer;
   }
 
-  createJIT(M.release());
+  createJIT(std::move(M));
   uint64_t ptr = TheJIT->getFunctionAddress(Outer->getName().str());
   EXPECT_TRUE(0 != ptr)
     << "Unable to get pointer to outer function from JIT";
diff --git a/llvm/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h b/llvm/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h
index 82d7f35..1a36579 100644
--- a/llvm/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h
+++ b/llvm/unittests/ExecutionEngine/MCJIT/MCJITTestBase.h
@@ -307,13 +307,13 @@
     UnsupportedEnvironments.push_back(Triple::Cygnus);
   }
 
-  void createJIT(Module *M) {
+  void createJIT(std::unique_ptr<Module> M) {
 
     // Due to the EngineBuilder constructor, it is required to have a Module
     // in order to construct an ExecutionEngine (i.e. MCJIT)
     assert(M != 0 && "a non-null Module must be provided to create MCJIT");
 
-    EngineBuilder EB(M);
+    EngineBuilder EB(std::move(M));
     std::string Error;
     TheJIT.reset(EB.setEngineKind(EngineKind::JIT)
                  .setUseMCJIT(true) /* can this be folded into the EngineKind enum? */