2002-11-21 04:47:41 +08:00
|
|
|
//===- CloneModule.cpp - Clone an entire module ---------------------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-21 04:47:41 +08:00
|
|
|
//
|
|
|
|
// This file implements the CloneModule interface which makes a copy of an
|
|
|
|
// entire module.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constant.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2010-08-25 02:50:07 +08:00
|
|
|
#include "llvm/Transforms/Utils/ValueMapper.h"
|
2014-10-02 01:14:57 +08:00
|
|
|
#include "llvm-c/Core.h"
|
2004-01-09 14:12:26 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2017-01-19 04:02:31 +08:00
|
|
|
static void copyComdat(GlobalObject *Dst, const GlobalObject *Src) {
|
|
|
|
const Comdat *SC = Src->getComdat();
|
|
|
|
if (!SC)
|
|
|
|
return;
|
|
|
|
Comdat *DC = Dst->getParent()->getOrInsertComdat(SC->getName());
|
|
|
|
DC->setSelectionKind(SC->getSelectionKind());
|
|
|
|
Dst->setComdat(DC);
|
|
|
|
}
|
|
|
|
|
2015-12-09 07:57:17 +08:00
|
|
|
/// 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.
|
2002-11-21 04:47:41 +08:00
|
|
|
///
|
2015-12-09 07:57:17 +08:00
|
|
|
std::unique_ptr<Module> llvm::CloneModule(const Module *M) {
|
2006-05-18 02:05:35 +08:00
|
|
|
// Create the value map that maps things from the old module over to the new
|
|
|
|
// module.
|
2010-06-24 08:00:42 +08:00
|
|
|
ValueToValueMapTy VMap;
|
2010-06-24 07:55:51 +08:00
|
|
|
return CloneModule(M, VMap);
|
2006-05-18 02:05:35 +08:00
|
|
|
}
|
|
|
|
|
2015-12-09 07:57:17 +08:00
|
|
|
std::unique_ptr<Module> llvm::CloneModule(const Module *M,
|
|
|
|
ValueToValueMapTy &VMap) {
|
2015-08-21 10:48:20 +08:00
|
|
|
return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; });
|
|
|
|
}
|
|
|
|
|
2015-12-09 07:57:17 +08:00
|
|
|
std::unique_ptr<Module> llvm::CloneModule(
|
2015-08-21 10:48:20 +08:00
|
|
|
const Module *M, ValueToValueMapTy &VMap,
|
2016-06-13 00:13:55 +08:00
|
|
|
function_ref<bool(const GlobalValue *)> ShouldCloneDefinition) {
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
// First off, we need to create the new module.
|
2015-12-09 07:57:17 +08:00
|
|
|
std::unique_ptr<Module> New =
|
|
|
|
llvm::make_unique<Module>(M->getModuleIdentifier(), M->getContext());
|
2007-01-26 16:11:39 +08:00
|
|
|
New->setDataLayout(M->getDataLayout());
|
2006-01-19 05:32:45 +08:00
|
|
|
New->setTargetTriple(M->getTargetTriple());
|
2006-01-24 12:16:34 +08:00
|
|
|
New->setModuleInlineAsm(M->getModuleInlineAsm());
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
|
2002-11-21 04:47:41 +08:00
|
|
|
// Loop over all of the global variables, making corresponding globals in the
|
2010-06-24 07:55:51 +08:00
|
|
|
// new module. Here we add them to the VMap and to the new Module. We
|
2002-11-21 04:47:41 +08:00
|
|
|
// don't worry about attributes or initializers, they will come later.
|
|
|
|
//
|
2005-05-09 09:04:34 +08:00
|
|
|
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
|
2008-10-09 14:27:14 +08:00
|
|
|
I != E; ++I) {
|
2009-07-09 03:03:57 +08:00
|
|
|
GlobalVariable *GV = new GlobalVariable(*New,
|
2016-01-17 04:30:46 +08:00
|
|
|
I->getValueType(),
|
2011-08-16 05:05:06 +08:00
|
|
|
I->isConstant(), I->getLinkage(),
|
2014-04-25 13:29:35 +08:00
|
|
|
(Constant*) nullptr, I->getName(),
|
|
|
|
(GlobalVariable*) nullptr,
|
2012-06-23 19:37:03 +08:00
|
|
|
I->getThreadLocalMode(),
|
2011-08-16 05:05:06 +08:00
|
|
|
I->getType()->getAddressSpace());
|
2015-10-13 10:39:05 +08:00
|
|
|
GV->copyAttributesFrom(&*I);
|
|
|
|
VMap[&*I] = GV;
|
2008-10-09 14:27:14 +08:00
|
|
|
}
|
2002-11-21 04:47:41 +08:00
|
|
|
|
|
|
|
// Loop over the functions in the module, making external functions as before
|
2016-06-26 20:28:59 +08:00
|
|
|
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;
|
2005-05-09 09:04:34 +08:00
|
|
|
}
|
2002-11-21 04:47:41 +08:00
|
|
|
|
2007-07-11 03:07:35 +08:00
|
|
|
// Loop over the aliases in the module
|
|
|
|
for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
|
2011-08-16 05:05:06 +08:00
|
|
|
I != E; ++I) {
|
2015-10-13 10:39:05 +08:00
|
|
|
if (!ShouldCloneDefinition(&*I)) {
|
2015-08-21 10:48:20 +08:00
|
|
|
// An alias cannot act as an external reference, so we need to create
|
|
|
|
// either a function or a global variable depending on the value type.
|
|
|
|
// FIXME: Once pointee types are gone we can probably pick one or the
|
|
|
|
// other.
|
|
|
|
GlobalValue *GV;
|
|
|
|
if (I->getValueType()->isFunctionTy())
|
|
|
|
GV = Function::Create(cast<FunctionType>(I->getValueType()),
|
2015-12-09 07:57:17 +08:00
|
|
|
GlobalValue::ExternalLinkage, I->getName(),
|
|
|
|
New.get());
|
2015-08-21 10:48:20 +08:00
|
|
|
else
|
|
|
|
GV = new GlobalVariable(
|
|
|
|
*New, I->getValueType(), false, GlobalValue::ExternalLinkage,
|
|
|
|
(Constant *)nullptr, I->getName(), (GlobalVariable *)nullptr,
|
|
|
|
I->getThreadLocalMode(), I->getType()->getAddressSpace());
|
2015-10-13 10:39:05 +08:00
|
|
|
VMap[&*I] = GV;
|
2015-08-21 10:48:20 +08:00
|
|
|
// We do not copy attributes (mainly because copying between different
|
|
|
|
// kinds of globals is forbidden), but this is generally not required for
|
|
|
|
// correctness.
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-15 04:29:26 +08:00
|
|
|
auto *GA = GlobalAlias::create(I->getValueType(),
|
|
|
|
I->getType()->getPointerAddressSpace(),
|
2015-12-09 07:57:17 +08:00
|
|
|
I->getLinkage(), I->getName(), New.get());
|
2015-10-13 10:39:05 +08:00
|
|
|
GA->copyAttributesFrom(&*I);
|
|
|
|
VMap[&*I] = GA;
|
2011-08-16 05:05:06 +08:00
|
|
|
}
|
2007-07-11 03:07:35 +08:00
|
|
|
|
2002-11-21 04:47:41 +08:00
|
|
|
// Now that all of the things that global variable initializer can refer to
|
|
|
|
// have been created, loop through and copy the global variable referrers
|
|
|
|
// over... We also set the attributes on the global now.
|
|
|
|
//
|
2005-05-09 09:04:34 +08:00
|
|
|
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
|
|
|
|
I != E; ++I) {
|
2016-04-01 04:21:31 +08:00
|
|
|
if (I->isDeclaration())
|
|
|
|
continue;
|
|
|
|
|
2015-10-13 10:39:05 +08:00
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]);
|
|
|
|
if (!ShouldCloneDefinition(&*I)) {
|
2015-08-21 10:48:20 +08:00
|
|
|
// Skip after setting the correct linkage for an external reference.
|
|
|
|
GV->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
continue;
|
|
|
|
}
|
2002-11-21 04:47:41 +08:00
|
|
|
if (I->hasInitializer())
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-10 01:41:24 +08:00
|
|
|
GV->setInitializer(MapValue(I->getInitializer(), VMap));
|
2016-10-26 10:57:33 +08:00
|
|
|
|
|
|
|
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
|
|
|
|
I->getAllMetadata(MDs);
|
|
|
|
for (auto MD : MDs)
|
|
|
|
GV->addMetadata(MD.first, *MapMetadata(MD.second, VMap));
|
2017-01-19 04:02:31 +08:00
|
|
|
|
|
|
|
copyComdat(GV, &*I);
|
2002-11-21 04:47:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Similarly, copy over function bodies now...
|
|
|
|
//
|
2016-06-26 20:28:59 +08:00
|
|
|
for (const Function &I : *M) {
|
|
|
|
if (I.isDeclaration())
|
2016-04-01 04:21:31 +08:00
|
|
|
continue;
|
|
|
|
|
2016-06-26 20:28:59 +08:00
|
|
|
Function *F = cast<Function>(VMap[&I]);
|
|
|
|
if (!ShouldCloneDefinition(&I)) {
|
2015-08-21 10:48:20 +08:00
|
|
|
// Skip after setting the correct linkage for an external reference.
|
|
|
|
F->setLinkage(GlobalValue::ExternalLinkage);
|
2016-03-29 05:37:02 +08:00
|
|
|
// Personality function is not valid on a declaration.
|
|
|
|
F->setPersonalityFn(nullptr);
|
2015-08-21 10:48:20 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-01 04:21:31 +08:00
|
|
|
|
|
|
|
Function::arg_iterator DestI = F->arg_begin();
|
2016-06-26 20:28:59 +08:00
|
|
|
for (Function::const_arg_iterator J = I.arg_begin(); J != I.arg_end();
|
2016-04-01 04:21:31 +08:00
|
|
|
++J) {
|
|
|
|
DestI->setName(J->getName());
|
|
|
|
VMap[&*J] = &*DestI++;
|
2002-11-21 04:47:41 +08:00
|
|
|
}
|
2015-07-01 06:14:01 +08:00
|
|
|
|
2016-04-01 04:21:31 +08:00
|
|
|
SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
|
2016-06-26 20:28:59 +08:00
|
|
|
CloneFunctionInto(F, &I, VMap, /*ModuleLevelChanges=*/true, Returns);
|
2016-04-01 04:21:31 +08:00
|
|
|
|
2016-06-26 20:28:59 +08:00
|
|
|
if (I.hasPersonalityFn())
|
|
|
|
F->setPersonalityFn(MapValue(I.getPersonalityFn(), VMap));
|
2017-01-19 04:02:31 +08:00
|
|
|
|
|
|
|
copyComdat(F, &I);
|
2002-11-21 04:47:41 +08:00
|
|
|
}
|
|
|
|
|
2007-07-11 03:07:35 +08:00
|
|
|
// And aliases
|
|
|
|
for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
|
|
|
|
I != E; ++I) {
|
2015-08-21 10:48:20 +08:00
|
|
|
// We already dealt with undefined aliases above.
|
2015-10-13 10:39:05 +08:00
|
|
|
if (!ShouldCloneDefinition(&*I))
|
2015-08-21 10:48:20 +08:00
|
|
|
continue;
|
2015-10-13 10:39:05 +08:00
|
|
|
GlobalAlias *GA = cast<GlobalAlias>(VMap[&*I]);
|
2014-06-03 10:41:57 +08:00
|
|
|
if (const Constant *C = I->getAliasee())
|
2014-12-23 16:23:45 +08:00
|
|
|
GA->setAliasee(MapValue(C, VMap));
|
2007-07-11 03:07:35 +08:00
|
|
|
}
|
2010-06-23 02:52:38 +08:00
|
|
|
|
|
|
|
// And named metadata....
|
|
|
|
for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
|
|
|
|
E = M->named_metadata_end(); I != E; ++I) {
|
|
|
|
const NamedMDNode &NMD = *I;
|
2010-07-22 07:38:33 +08:00
|
|
|
NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
|
2010-06-23 02:52:38 +08:00
|
|
|
for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
|
2014-12-19 14:06:18 +08:00
|
|
|
NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
|
2010-06-23 02:52:38 +08:00
|
|
|
}
|
2010-06-23 06:50:42 +08:00
|
|
|
|
2002-11-21 04:47:41 +08:00
|
|
|
return New;
|
|
|
|
}
|
2014-10-02 01:14:57 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
|
2015-12-09 07:57:17 +08:00
|
|
|
return wrap(CloneModule(unwrap(M)).release());
|
2014-10-02 01:14:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|