1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 10:42:39 +01:00

Return a std::unique_ptr from CloneModule. NFC.

llvm-svn: 255078
This commit is contained in:
Rafael Espindola 2015-12-08 23:57:17 +00:00
parent e384f13917
commit f20bc23b7c
6 changed files with 42 additions and 40 deletions

View File

@ -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<Module> CloneModule(const Module *M);
std::unique_ptr<Module> 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<Module>
CloneModule(const Module *M, ValueToValueMapTy &VMap,
std::function<bool(const GlobalValue *)> ShouldCloneDefinition);

View File

@ -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<Module> 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<Module> llvm::CloneModule(const Module *M,
ValueToValueMapTy &VMap) {
return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; });
}
Module *llvm::CloneModule(
std::unique_ptr<Module> llvm::CloneModule(
const Module *M, ValueToValueMapTy &VMap,
std::function<bool(const GlobalValue *)> ShouldCloneDefinition) {
// First off, we need to create the new module.
Module *New = new Module(M->getModuleIdentifier(), M->getContext());
std::unique_ptr<Module> New =
llvm::make_unique<Module>(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<FunctionType>(I->getType()->getElementType()),
I->getLinkage(), I->getName(), New);
Function::Create(cast<FunctionType>(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<FunctionType>(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());
}
}

View File

@ -143,7 +143,7 @@ ReduceCrashingGlobalVariables::TestGlobalVariables(
std::vector<GlobalVariable*> &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<GlobalVariable*> GVSet;
@ -239,7 +239,7 @@ bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &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<Function*> Functions;
@ -346,7 +346,7 @@ namespace {
bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &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<BasicBlock*, 8> Blocks;
@ -456,7 +456,7 @@ bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*>
&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<Instruction*, 64> Instructions;
@ -532,7 +532,7 @@ public:
bool ReduceCrashingNamedMD::TestNamedMDs(std::vector<std::string> &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<size_t>(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...

View File

@ -86,7 +86,7 @@ std::unique_ptr<Module>
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<Function *> TestFunctions;

View File

@ -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<Function*> &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<Function*> FuncsOnClone;
@ -279,7 +279,7 @@ bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &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<Function>(VMap[MiscompiledFunctions[i]]);
@ -506,7 +506,7 @@ bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &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<Function*> FuncsOnClone;
std::vector<BasicBlock*> BBsOnClone;
@ -520,7 +520,7 @@ bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &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

View File

@ -436,7 +436,7 @@ protected:
IBuilder.CreateRetVoid();
}
void CreateNewModule() { NewM = llvm::CloneModule(OldM); }
void CreateNewModule() { NewM = llvm::CloneModule(OldM).release(); }
LLVMContext C;
Module *OldM;