Skip to content

Commit

Permalink
Pass a module reference to CloneModule.
Browse files Browse the repository at this point in the history
It can never be null and most callers were already using references or
std::unique_ptr.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@325160 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Feb 14, 2018
1 parent 4fe1287 commit 22b7724
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 53 deletions.
6 changes: 3 additions & 3 deletions include/llvm/Transforms/Utils/Cloning.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ class ReturnInst;

/// Return an exact copy of the specified module
///
std::unique_ptr<Module> CloneModule(const Module *M);
std::unique_ptr<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.
std::unique_ptr<Module>
CloneModule(const Module *M, ValueToValueMapTy &VMap,
CloneModule(const Module &M, ValueToValueMapTy &VMap,
function_ref<bool(const GlobalValue *)> ShouldCloneDefinition);

/// ClonedCodeInfo - This struct can be used to capture information about code
Expand Down
2 changes: 1 addition & 1 deletion lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void splitAndWriteThinLTOBitcode(

ValueToValueMapTy VMap;
std::unique_ptr<Module> MergedM(
CloneModule(&M, VMap, [&](const GlobalValue *GV) -> bool {
CloneModule(M, VMap, [&](const GlobalValue *GV) -> bool {
if (const auto *C = GV->getComdat())
if (MergedMComdats.count(C))
return true;
Expand Down
35 changes: 18 additions & 17 deletions lib/Transforms/Utils/CloneModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,33 @@ static void copyComdat(GlobalObject *Dst, const GlobalObject *Src) {
/// copies of global variables and functions, and making their (initializers and
/// references, respectively) refer to the right globals.
///
std::unique_ptr<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);
}

std::unique_ptr<Module> llvm::CloneModule(const Module *M,
std::unique_ptr<Module> llvm::CloneModule(const Module &M,
ValueToValueMapTy &VMap) {
return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; });
}

std::unique_ptr<Module> llvm::CloneModule(
const Module *M, ValueToValueMapTy &VMap,
const Module &M, ValueToValueMapTy &VMap,
function_ref<bool(const GlobalValue *)> ShouldCloneDefinition) {
// First off, we need to create the new module.
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());
llvm::make_unique<Module>(M.getModuleIdentifier(), M.getContext());
New->setDataLayout(M.getDataLayout());
New->setTargetTriple(M.getTargetTriple());
New->setModuleInlineAsm(M.getModuleInlineAsm());

// Loop over all of the global variables, making corresponding globals in the
// new module. Here we add them to the VMap and to the new Module. We
// don't worry about attributes or initializers, they will come later.
//
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I) {
GlobalVariable *GV = new GlobalVariable(*New,
I->getValueType(),
Expand All @@ -72,15 +72,15 @@ std::unique_ptr<Module> llvm::CloneModule(
}

// Loop over the functions in the module, making external functions as before
for (const Function &I : *M) {
for (const Function &I : M) {
Function *NF = Function::Create(cast<FunctionType>(I.getValueType()),
I.getLinkage(), I.getName(), New.get());
NF->copyAttributesFrom(&I);
VMap[&I] = NF;
}

// Loop over the aliases in the module
for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
I != E; ++I) {
if (!ShouldCloneDefinition(&*I)) {
// An alias cannot act as an external reference, so we need to create
Expand Down Expand Up @@ -114,7 +114,7 @@ std::unique_ptr<Module> llvm::CloneModule(
// have been created, loop through and copy the global variable referrers
// over... We also set the attributes on the global now.
//
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I) {
if (I->isDeclaration())
continue;
Expand All @@ -139,7 +139,7 @@ std::unique_ptr<Module> llvm::CloneModule(

// Similarly, copy over function bodies now...
//
for (const Function &I : *M) {
for (const Function &I : M) {
if (I.isDeclaration())
continue;

Expand Down Expand Up @@ -169,7 +169,7 @@ std::unique_ptr<Module> llvm::CloneModule(
}

// And aliases
for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
I != E; ++I) {
// We already dealt with undefined aliases above.
if (!ShouldCloneDefinition(&*I))
Expand All @@ -180,8 +180,9 @@ std::unique_ptr<Module> llvm::CloneModule(
}

// And named metadata....
for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
E = M->named_metadata_end(); I != E; ++I) {
for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
E = M.named_metadata_end();
I != E; ++I) {
const NamedMDNode &NMD = *I;
NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
Expand All @@ -194,7 +195,7 @@ std::unique_ptr<Module> llvm::CloneModule(
extern "C" {

LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
return wrap(CloneModule(unwrap(M)).release());
return wrap(CloneModule(*unwrap(M)).release());
}

}
2 changes: 1 addition & 1 deletion lib/Transforms/Utils/SplitModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void llvm::SplitModule(
for (unsigned I = 0; I < N; ++I) {
ValueToValueMapTy VMap;
std::unique_ptr<Module> MPart(
CloneModule(M.get(), VMap, [&](const GlobalValue *GV) {
CloneModule(*M, VMap, [&](const GlobalValue *GV) {
if (ClusterIDMap.count(GV))
return (ClusterIDMap[GV] == I);
else
Expand Down
22 changes: 11 additions & 11 deletions tools/bugpoint/CrashDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ bool ReduceCrashingGlobalInitializers::TestGlobalVariables(
std::vector<GlobalVariable *> &GVs) {
// Clone the program to try hacking it apart...
ValueToValueMapTy VMap;
std::unique_ptr<Module> M = CloneModule(BD.getProgram(), VMap);
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);

// Convert list to set for fast lookup...
std::set<GlobalVariable *> GVSet;
Expand Down Expand Up @@ -244,7 +244,7 @@ bool ReduceCrashingFunctions::TestFuncs(std::vector<Function *> &Funcs) {

// Clone the program to try hacking it apart...
ValueToValueMapTy VMap;
Module *M = CloneModule(BD.getProgram(), VMap).release();
Module *M = CloneModule(*BD.getProgram(), VMap).release();

// Convert list to set for fast lookup...
std::set<Function *> Functions;
Expand Down Expand Up @@ -388,7 +388,7 @@ class ReduceCrashingBlocks : public ListReducer<const BasicBlock *> {
bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock *> &BBs) {
// Clone the program to try hacking it apart...
ValueToValueMapTy VMap;
std::unique_ptr<Module> M = CloneModule(BD.getProgram(), VMap);
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);

// Convert list to set for fast lookup...
SmallPtrSet<BasicBlock *, 8> Blocks;
Expand Down Expand Up @@ -507,7 +507,7 @@ bool ReduceCrashingConditionals::TestBlocks(
std::vector<const BasicBlock *> &BBs) {
// Clone the program to try hacking it apart...
ValueToValueMapTy VMap;
std::unique_ptr<Module> M = CloneModule(BD.getProgram(), VMap);
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);

// Convert list to set for fast lookup...
SmallPtrSet<const BasicBlock *, 8> Blocks;
Expand Down Expand Up @@ -611,7 +611,7 @@ class ReduceSimplifyCFG : public ListReducer<const BasicBlock *> {
bool ReduceSimplifyCFG::TestBlocks(std::vector<const BasicBlock *> &BBs) {
// Clone the program to try hacking it apart...
ValueToValueMapTy VMap;
std::unique_ptr<Module> M = CloneModule(BD.getProgram(), VMap);
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);

// Convert list to set for fast lookup...
SmallPtrSet<const BasicBlock *, 8> Blocks;
Expand Down Expand Up @@ -703,7 +703,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).release();
Module *M = CloneModule(*BD.getProgram(), VMap).release();

// Convert list to set for fast lookup...
SmallPtrSet<Instruction *, 32> Instructions;
Expand Down Expand Up @@ -778,7 +778,7 @@ class ReduceCrashingNamedMD : public ListReducer<std::string> {
bool ReduceCrashingNamedMD::TestNamedMDs(std::vector<std::string> &NamedMDs) {

ValueToValueMapTy VMap;
Module *M = CloneModule(BD.getProgram(), VMap).release();
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);
Expand Down Expand Up @@ -858,7 +858,7 @@ bool ReduceCrashingNamedMDOps::TestNamedMDOps(
outs() << " named metadata operands: ";

ValueToValueMapTy VMap;
Module *M = CloneModule(BD.getProgram(), VMap).release();
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.
Expand Down Expand Up @@ -900,7 +900,7 @@ static Error ReduceGlobalInitializers(BugDriver &BD, BugTester TestFn) {

// Now try to reduce the number of global variable initializers in the
// module to something small.
std::unique_ptr<Module> M = CloneModule(OrigM);
std::unique_ptr<Module> M = CloneModule(*OrigM);
bool DeletedInit = false;

for (GlobalVariable &GV : M->globals()) {
Expand Down Expand Up @@ -1120,7 +1120,7 @@ static Error DebugACrash(BugDriver &BD, BugTester TestFn) {

// Attempt to strip debug info metadata.
auto stripMetadata = [&](std::function<bool(Module &)> strip) {
std::unique_ptr<Module> M = CloneModule(BD.getProgram());
std::unique_ptr<Module> M = CloneModule(*BD.getProgram());
strip(*M);
if (TestFn(BD, M.get()))
BD.setNewProgram(M.release());
Expand Down Expand Up @@ -1166,7 +1166,7 @@ static Error DebugACrash(BugDriver &BD, BugTester TestFn) {
// Try to clean up the testcase by running funcresolve and globaldce...
if (!BugpointIsInterrupted) {
outs() << "\n*** Attempting to perform final cleanups: ";
std::unique_ptr<Module> M = CloneModule(BD.getProgram());
std::unique_ptr<Module> M = CloneModule(*BD.getProgram());
M = BD.performFinalCleanups(M.release(), true);

// Find out if the pass still crashes on the cleaned up program...
Expand Down
4 changes: 2 additions & 2 deletions tools/bugpoint/ExtractFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ std::unique_ptr<Module>
BugDriver::deleteInstructionFromProgram(const Instruction *I,
unsigned Simplification) {
// FIXME, use vmap?
std::unique_ptr<Module> Clone = CloneModule(Program);
std::unique_ptr<Module> Clone = CloneModule(*Program);

const BasicBlock *PBB = I->getParent();
const Function *PF = PBB->getParent();
Expand Down Expand Up @@ -318,7 +318,7 @@ llvm::SplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F,
}

ValueToValueMapTy NewVMap;
std::unique_ptr<Module> New = CloneModule(M, NewVMap);
std::unique_ptr<Module> New = CloneModule(*M, NewVMap);

// Remove the Test functions from the Safe module
std::set<Function *> TestFunctions;
Expand Down
24 changes: 12 additions & 12 deletions tools/bugpoint/Miscompilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ static Expected<std::unique_ptr<Module>> testMergedProgram(const BugDriver &BD,
const Module &M2,
bool &Broken) {
// Resulting merge of M1 and M2.
auto Merged = CloneModule(&M1);
if (Linker::linkModules(*Merged, CloneModule(&M2)))
auto Merged = CloneModule(M1);
if (Linker::linkModules(*Merged, CloneModule(M2)))
// TODO: Shouldn't we thread the error up instead of exiting?
exit(1);

Expand Down Expand Up @@ -266,7 +266,7 @@ 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).release();
Module *Clone = CloneModule(*BD.getProgram(), VMap).release();
Module *Orig = BD.swapProgramIn(Clone);

std::vector<Function *> FuncsOnClone;
Expand All @@ -277,7 +277,7 @@ ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function *> &Funcs) {

// Split the module into the two halves of the program we want.
VMap.clear();
std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);
std::unique_ptr<Module> ToNotOptimize = CloneModule(*BD.getProgram(), VMap);
std::unique_ptr<Module> ToOptimize =
SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap);

Expand Down Expand Up @@ -316,7 +316,7 @@ ExtractLoops(BugDriver &BD,
return MadeChange;

ValueToValueMapTy VMap;
std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);
std::unique_ptr<Module> ToNotOptimize = CloneModule(*BD.getProgram(), VMap);
Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize.get(),
MiscompiledFunctions, VMap)
.release();
Expand Down Expand Up @@ -377,8 +377,8 @@ ExtractLoops(BugDriver &BD,
outs() << " Testing after loop extraction:\n";
// Clone modules, the tester function will free them.
std::unique_ptr<Module> TOLEBackup =
CloneModule(ToOptimizeLoopExtracted.get(), VMap);
std::unique_ptr<Module> TNOBackup = CloneModule(ToNotOptimize.get(), VMap);
CloneModule(*ToOptimizeLoopExtracted, VMap);
std::unique_ptr<Module> TNOBackup = CloneModule(*ToNotOptimize, VMap);

for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
MiscompiledFunctions[i] = cast<Function>(VMap[MiscompiledFunctions[i]]);
Expand Down Expand Up @@ -508,7 +508,7 @@ 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).release();
Module *Clone = CloneModule(*BD.getProgram(), VMap).release();
Module *Orig = BD.swapProgramIn(Clone);
std::vector<Function *> FuncsOnClone;
std::vector<BasicBlock *> BBsOnClone;
Expand All @@ -522,7 +522,7 @@ ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock *> &BBs) {
}
VMap.clear();

std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);
std::unique_ptr<Module> ToNotOptimize = CloneModule(*BD.getProgram(), VMap);
std::unique_ptr<Module> ToOptimize =
SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap);

Expand Down Expand Up @@ -577,7 +577,7 @@ ExtractBlocks(BugDriver &BD,
}

ValueToValueMapTy VMap;
Module *ProgClone = CloneModule(BD.getProgram(), VMap).release();
Module *ProgClone = CloneModule(*BD.getProgram(), VMap).release();
Module *ToExtract =
SplitFunctionsOutOfModule(ProgClone, MiscompiledFunctions, VMap)
.release();
Expand Down Expand Up @@ -770,7 +770,7 @@ Error BugDriver::debugMiscompilation() {
// 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).release();
Module *ToNotOptimize = CloneModule(*getProgram(), VMap).release();
Module *ToOptimize =
SplitFunctionsOutOfModule(ToNotOptimize, *MiscompiledFunctions, VMap)
.release();
Expand Down Expand Up @@ -1037,7 +1037,7 @@ Error BugDriver::debugCodeGenerator() {

// Split the module into the two halves of the program we want.
ValueToValueMapTy VMap;
std::unique_ptr<Module> ToNotCodeGen = CloneModule(getProgram(), VMap);
std::unique_ptr<Module> ToNotCodeGen = CloneModule(*getProgram(), VMap);
std::unique_ptr<Module> ToCodeGen =
SplitFunctionsOutOfModule(ToNotCodeGen.get(), *Funcs, VMap);

Expand Down
2 changes: 1 addition & 1 deletion tools/llc/llc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
// in the future.
SmallVector<char, 0> CompileTwiceBuffer;
if (CompileTwice) {
std::unique_ptr<Module> M2(llvm::CloneModule(M.get()));
std::unique_ptr<Module> M2(llvm::CloneModule(*M));
PM.run(*M2);
CompileTwiceBuffer = Buffer;
Buffer.clear();
Expand Down
8 changes: 4 additions & 4 deletions tools/opt/opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,10 +765,10 @@ int main(int argc, char **argv) {
// If requested, run all passes again with the same pass manager to catch
// bugs caused by persistent state in the passes
if (RunTwice) {
std::unique_ptr<Module> M2(CloneModule(M.get()));
Passes.run(*M2);
CompileTwiceBuffer = Buffer;
Buffer.clear();
std::unique_ptr<Module> M2(CloneModule(*M));
Passes.run(*M2);
CompileTwiceBuffer = Buffer;
Buffer.clear();
}

// Now that we have all of the passes ready, run them.
Expand Down
2 changes: 1 addition & 1 deletion unittests/Transforms/Utils/Cloning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ class CloneModule : public ::testing::Test {
DBuilder.finalize();
}

void CreateNewModule() { NewM = llvm::CloneModule(OldM).release(); }
void CreateNewModule() { NewM = llvm::CloneModule(*OldM).release(); }

LLVMContext C;
Module *OldM;
Expand Down

0 comments on commit 22b7724

Please sign in to comment.