From: Rafael Espindola Date: Tue, 8 Dec 2015 23:57:17 +0000 (+0000) Subject: Return a std::unique_ptr from CloneModule. NFC. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cab951dd466b9826a80a22496cbe03cdea3f33d2;p=platform%2Fupstream%2Fllvm.git Return a std::unique_ptr from CloneModule. NFC. llvm-svn: 255078 --- diff --git a/llvm/include/llvm/Transforms/Utils/Cloning.h b/llvm/include/llvm/Transforms/Utils/Cloning.h index 2fccbb8..5d5689c 100644 --- a/llvm/include/llvm/Transforms/Utils/Cloning.h +++ b/llvm/include/llvm/Transforms/Utils/Cloning.h @@ -48,16 +48,16 @@ class AllocaInst; class AssumptionCacheTracker; class DominatorTree; -/// CloneModule - Return an exact copy of the specified module +/// Return an exact copy of the specified module /// -Module *CloneModule(const Module *M); -Module *CloneModule(const Module *M, ValueToValueMapTy &VMap); +std::unique_ptr CloneModule(const Module *M); +std::unique_ptr CloneModule(const Module *M, ValueToValueMapTy &VMap); /// Return a copy of the specified module. The ShouldCloneDefinition function /// controls whether a specific GlobalValue's definition is cloned. If the /// function returns false, the module copy will contain an external reference /// in place of the global definition. -Module * +std::unique_ptr CloneModule(const Module *M, ValueToValueMapTy &VMap, std::function ShouldCloneDefinition); diff --git a/llvm/lib/Transforms/Utils/CloneModule.cpp b/llvm/lib/Transforms/Utils/CloneModule.cpp index acb88c2..ab08335 100644 --- a/llvm/lib/Transforms/Utils/CloneModule.cpp +++ b/llvm/lib/Transforms/Utils/CloneModule.cpp @@ -20,27 +20,28 @@ #include "llvm-c/Core.h" using namespace llvm; -/// CloneModule - Return an exact copy of the specified module. This is not as -/// easy as it might seem because we have to worry about making copies of global -/// variables and functions, and making their (initializers and references, -/// respectively) refer to the right globals. +/// This is not as easy as it might seem because we have to worry about making +/// copies of global variables and functions, and making their (initializers and +/// references, respectively) refer to the right globals. /// -Module *llvm::CloneModule(const Module *M) { +std::unique_ptr llvm::CloneModule(const Module *M) { // Create the value map that maps things from the old module over to the new // module. ValueToValueMapTy VMap; return CloneModule(M, VMap); } -Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) { +std::unique_ptr llvm::CloneModule(const Module *M, + ValueToValueMapTy &VMap) { return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; }); } -Module *llvm::CloneModule( +std::unique_ptr llvm::CloneModule( const Module *M, ValueToValueMapTy &VMap, std::function ShouldCloneDefinition) { // First off, we need to create the new module. - Module *New = new Module(M->getModuleIdentifier(), M->getContext()); + std::unique_ptr New = + llvm::make_unique(M->getModuleIdentifier(), M->getContext()); New->setDataLayout(M->getDataLayout()); New->setTargetTriple(M->getTargetTriple()); New->setModuleInlineAsm(M->getModuleInlineAsm()); @@ -65,8 +66,8 @@ Module *llvm::CloneModule( // Loop over the functions in the module, making external functions as before for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { Function *NF = - Function::Create(cast(I->getType()->getElementType()), - I->getLinkage(), I->getName(), New); + Function::Create(cast(I->getType()->getElementType()), + I->getLinkage(), I->getName(), New.get()); NF->copyAttributesFrom(&*I); VMap[&*I] = NF; } @@ -82,7 +83,8 @@ Module *llvm::CloneModule( GlobalValue *GV; if (I->getValueType()->isFunctionTy()) GV = Function::Create(cast(I->getValueType()), - GlobalValue::ExternalLinkage, I->getName(), New); + GlobalValue::ExternalLinkage, I->getName(), + New.get()); else GV = new GlobalVariable( *New, I->getValueType(), false, GlobalValue::ExternalLinkage, @@ -96,7 +98,7 @@ Module *llvm::CloneModule( } auto *GA = GlobalAlias::create(I->getValueType(), I->getType()->getPointerAddressSpace(), - I->getLinkage(), I->getName(), New); + I->getLinkage(), I->getName(), New.get()); GA->copyAttributesFrom(&*I); VMap[&*I] = GA; } @@ -168,7 +170,7 @@ Module *llvm::CloneModule( extern "C" { LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) { - return wrap(CloneModule(unwrap(M))); + return wrap(CloneModule(unwrap(M)).release()); } } diff --git a/llvm/tools/bugpoint/CrashDebugger.cpp b/llvm/tools/bugpoint/CrashDebugger.cpp index 631a584..6cdc43a 100644 --- a/llvm/tools/bugpoint/CrashDebugger.cpp +++ b/llvm/tools/bugpoint/CrashDebugger.cpp @@ -143,7 +143,7 @@ ReduceCrashingGlobalVariables::TestGlobalVariables( std::vector &GVs) { // Clone the program to try hacking it apart... ValueToValueMapTy VMap; - Module *M = CloneModule(BD.getProgram(), VMap); + Module *M = CloneModule(BD.getProgram(), VMap).release(); // Convert list to set for fast lookup... std::set GVSet; @@ -239,7 +239,7 @@ bool ReduceCrashingFunctions::TestFuncs(std::vector &Funcs) { // Clone the program to try hacking it apart... ValueToValueMapTy VMap; - Module *M = CloneModule(BD.getProgram(), VMap); + Module *M = CloneModule(BD.getProgram(), VMap).release(); // Convert list to set for fast lookup... std::set Functions; @@ -346,7 +346,7 @@ namespace { bool ReduceCrashingBlocks::TestBlocks(std::vector &BBs) { // Clone the program to try hacking it apart... ValueToValueMapTy VMap; - Module *M = CloneModule(BD.getProgram(), VMap); + Module *M = CloneModule(BD.getProgram(), VMap).release(); // Convert list to set for fast lookup... SmallPtrSet Blocks; @@ -456,7 +456,7 @@ bool ReduceCrashingInstructions::TestInsts(std::vector &Insts) { // Clone the program to try hacking it apart... ValueToValueMapTy VMap; - Module *M = CloneModule(BD.getProgram(), VMap); + Module *M = CloneModule(BD.getProgram(), VMap).release(); // Convert list to set for fast lookup... SmallPtrSet Instructions; @@ -532,7 +532,7 @@ public: bool ReduceCrashingNamedMD::TestNamedMDs(std::vector &NamedMDs) { ValueToValueMapTy VMap; - Module *M = CloneModule(BD.getProgram(), VMap); + Module *M = CloneModule(BD.getProgram(), VMap).release(); outs() << "Checking for crash with only these named metadata nodes:"; unsigned NumPrint = std::min(NamedMDs.size(), 10); @@ -612,7 +612,7 @@ bool ReduceCrashingNamedMDOps::TestNamedMDOps( outs() << " named metadata operands: "; ValueToValueMapTy VMap; - Module *M = CloneModule(BD.getProgram(), VMap); + Module *M = CloneModule(BD.getProgram(), VMap).release(); // This is a little wasteful. In the future it might be good if we could have // these dropped during cloning. @@ -658,7 +658,7 @@ static bool DebugACrash(BugDriver &BD, BD.getProgram()->global_begin() != BD.getProgram()->global_end()) { // Now try to reduce the number of global variable initializers in the // module to something small. - Module *M = CloneModule(BD.getProgram()); + Module *M = CloneModule(BD.getProgram()).release(); bool DeletedInit = false; for (Module::global_iterator I = M->global_begin(), E = M->global_end(); @@ -840,7 +840,7 @@ ExitLoops: // Try to clean up the testcase by running funcresolve and globaldce... if (!BugpointIsInterrupted) { outs() << "\n*** Attempting to perform final cleanups: "; - Module *M = CloneModule(BD.getProgram()); + Module *M = CloneModule(BD.getProgram()).release(); M = BD.performFinalCleanups(M, true).release(); // Find out if the pass still crashes on the cleaned up program... diff --git a/llvm/tools/bugpoint/ExtractFunction.cpp b/llvm/tools/bugpoint/ExtractFunction.cpp index 7b98cb8..62c3f3e 100644 --- a/llvm/tools/bugpoint/ExtractFunction.cpp +++ b/llvm/tools/bugpoint/ExtractFunction.cpp @@ -86,7 +86,7 @@ std::unique_ptr BugDriver::deleteInstructionFromProgram(const Instruction *I, unsigned Simplification) { // FIXME, use vmap? - Module *Clone = CloneModule(Program); + Module *Clone = CloneModule(Program).release(); const BasicBlock *PBB = I->getParent(); const Function *PF = PBB->getParent(); @@ -323,7 +323,7 @@ llvm::SplitFunctionsOutOfModule(Module *M, } ValueToValueMapTy NewVMap; - Module *New = CloneModule(M, NewVMap); + Module *New = CloneModule(M, NewVMap).release(); // Remove the Test functions from the Safe module std::set TestFunctions; diff --git a/llvm/tools/bugpoint/Miscompilation.cpp b/llvm/tools/bugpoint/Miscompilation.cpp index 0b61b09..db6dd54 100644 --- a/llvm/tools/bugpoint/Miscompilation.cpp +++ b/llvm/tools/bugpoint/Miscompilation.cpp @@ -228,8 +228,8 @@ static Module *TestMergedProgram(const BugDriver &BD, Module *M1, Module *M2, bool &Broken) { // Link the two portions of the program back to together. if (!DeleteInputs) { - M1 = CloneModule(M1); - M2 = CloneModule(M2); + M1 = CloneModule(M1).release(); + M2 = CloneModule(M2).release(); } if (Linker::linkModules(*M1, *M2, diagnosticHandler)) exit(1); @@ -268,7 +268,7 @@ bool ReduceMiscompilingFunctions::TestFuncs(const std::vector &Funcs, // we can conclude that a function triggers the bug when in fact one // needs a larger set of original functions to do so. ValueToValueMapTy VMap; - Module *Clone = CloneModule(BD.getProgram(), VMap); + Module *Clone = CloneModule(BD.getProgram(), VMap).release(); Module *Orig = BD.swapProgramIn(Clone); std::vector FuncsOnClone; @@ -279,7 +279,7 @@ bool ReduceMiscompilingFunctions::TestFuncs(const std::vector &Funcs, // Split the module into the two halves of the program we want. VMap.clear(); - Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); + Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap).release(); Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, FuncsOnClone, VMap); @@ -317,7 +317,7 @@ static bool ExtractLoops(BugDriver &BD, if (BugpointIsInterrupted) return MadeChange; ValueToValueMapTy VMap; - Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); + Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap).release(); Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, MiscompiledFunctions, VMap); @@ -376,8 +376,8 @@ static bool ExtractLoops(BugDriver &BD, outs() << " Testing after loop extraction:\n"; // Clone modules, the tester function will free them. - Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted, VMap); - Module *TNOBackup = CloneModule(ToNotOptimize, VMap); + Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted, VMap).release(); + Module *TNOBackup = CloneModule(ToNotOptimize, VMap).release(); for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) MiscompiledFunctions[i] = cast(VMap[MiscompiledFunctions[i]]); @@ -506,7 +506,7 @@ bool ReduceMiscompiledBlocks::TestFuncs(const std::vector &BBs, // Split the module into the two halves of the program we want. ValueToValueMapTy VMap; - Module *Clone = CloneModule(BD.getProgram(), VMap); + Module *Clone = CloneModule(BD.getProgram(), VMap).release(); Module *Orig = BD.swapProgramIn(Clone); std::vector FuncsOnClone; std::vector BBsOnClone; @@ -520,7 +520,7 @@ bool ReduceMiscompiledBlocks::TestFuncs(const std::vector &BBs, } VMap.clear(); - Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); + Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap).release(); Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, FuncsOnClone, VMap); @@ -581,7 +581,7 @@ static bool ExtractBlocks(BugDriver &BD, } ValueToValueMapTy VMap; - Module *ProgClone = CloneModule(BD.getProgram(), VMap); + Module *ProgClone = CloneModule(BD.getProgram(), VMap).release(); Module *ToExtract = SplitFunctionsOutOfModule(ProgClone, MiscompiledFunctions, VMap); @@ -763,7 +763,7 @@ void BugDriver::debugMiscompilation(std::string *Error) { // Output a bunch of bitcode files for the user... outs() << "Outputting reduced bitcode files which expose the problem:\n"; ValueToValueMapTy VMap; - Module *ToNotOptimize = CloneModule(getProgram(), VMap); + Module *ToNotOptimize = CloneModule(getProgram(), VMap).release(); Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, MiscompiledFunctions, VMap); @@ -1039,7 +1039,7 @@ bool BugDriver::debugCodeGenerator(std::string *Error) { // Split the module into the two halves of the program we want. ValueToValueMapTy VMap; - Module *ToNotCodeGen = CloneModule(getProgram(), VMap); + Module *ToNotCodeGen = CloneModule(getProgram(), VMap).release(); Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs, VMap); // Condition the modules diff --git a/llvm/unittests/Transforms/Utils/Cloning.cpp b/llvm/unittests/Transforms/Utils/Cloning.cpp index e22573f..25e322e 100644 --- a/llvm/unittests/Transforms/Utils/Cloning.cpp +++ b/llvm/unittests/Transforms/Utils/Cloning.cpp @@ -436,7 +436,7 @@ protected: IBuilder.CreateRetVoid(); } - void CreateNewModule() { NewM = llvm::CloneModule(OldM); } + void CreateNewModule() { NewM = llvm::CloneModule(OldM).release(); } LLVMContext C; Module *OldM;