2002-11-20 20:47:41 +00:00
|
|
|
//===- CloneModule.cpp - Clone an entire module ---------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-20 20:47:41 +00:00
|
|
|
//
|
|
|
|
// This file implements the CloneModule interface which makes a copy of an
|
|
|
|
// entire module.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Constant.h"
|
2010-08-24 18:50:07 +00:00
|
|
|
#include "llvm/Transforms/Utils/ValueMapper.h"
|
2004-01-09 06:12:26 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-11-20 20:47:41 +00:00
|
|
|
/// 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
|
2003-10-10 17:57:28 +00:00
|
|
|
/// variables and functions, and making their (initializers and references,
|
2002-11-20 20:47:41 +00:00
|
|
|
/// respectively) refer to the right globals.
|
|
|
|
///
|
2004-01-09 06:12:26 +00:00
|
|
|
Module *llvm::CloneModule(const Module *M) {
|
2006-05-17 18:05:35 +00:00
|
|
|
// Create the value map that maps things from the old module over to the new
|
|
|
|
// module.
|
2010-06-24 00:00:42 +00:00
|
|
|
ValueToValueMapTy VMap;
|
2010-06-23 23:55:51 +00:00
|
|
|
return CloneModule(M, VMap);
|
2006-05-17 18:05:35 +00:00
|
|
|
}
|
|
|
|
|
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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) {
|
|
|
|
// First off, we need to create the new module.
|
2009-07-01 16:58:40 +00:00
|
|
|
Module *New = new Module(M->getModuleIdentifier(), M->getContext());
|
2007-01-26 08:11:39 +00:00
|
|
|
New->setDataLayout(M->getDataLayout());
|
2006-01-18 21:32:45 +00:00
|
|
|
New->setTargetTriple(M->getTargetTriple());
|
2006-01-24 04:16:34 +00: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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
|
2006-01-18 21:32:45 +00:00
|
|
|
// Copy all of the dependent libraries over.
|
|
|
|
for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
|
|
|
|
New->addLibrary(*I);
|
2003-04-24 17:15:33 +00:00
|
|
|
|
2002-11-20 20:47:41 +00:00
|
|
|
// Loop over all of the global variables, making corresponding globals in the
|
2010-06-23 23:55:51 +00:00
|
|
|
// new module. Here we add them to the VMap and to the new Module. We
|
2002-11-20 20:47:41 +00:00
|
|
|
// don't worry about attributes or initializers, they will come later.
|
|
|
|
//
|
2005-05-09 01:04:34 +00:00
|
|
|
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
|
2008-10-09 06:27:14 +00:00
|
|
|
I != E; ++I) {
|
2009-07-08 19:03:57 +00:00
|
|
|
GlobalVariable *GV = new GlobalVariable(*New,
|
2009-07-08 01:26:06 +00:00
|
|
|
I->getType()->getElementType(),
|
2011-08-15 21:05:06 +00:00
|
|
|
I->isConstant(), I->getLinkage(),
|
|
|
|
(Constant*) 0, I->getName(),
|
|
|
|
(GlobalVariable*) 0,
|
2012-06-23 11:37:03 +00:00
|
|
|
I->getThreadLocalMode(),
|
2011-08-15 21:05:06 +00:00
|
|
|
I->getType()->getAddressSpace());
|
|
|
|
GV->copyAttributesFrom(I);
|
2010-06-23 23:55:51 +00:00
|
|
|
VMap[I] = GV;
|
2008-10-09 06:27:14 +00:00
|
|
|
}
|
2002-11-20 20:47:41 +00:00
|
|
|
|
|
|
|
// Loop over the functions in the module, making external functions as before
|
2005-05-09 01:04:34 +00:00
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
2005-07-27 06:12:32 +00:00
|
|
|
Function *NF =
|
2008-04-06 20:25:17 +00:00
|
|
|
Function::Create(cast<FunctionType>(I->getType()->getElementType()),
|
2011-08-15 21:05:06 +00:00
|
|
|
I->getLinkage(), I->getName(), New);
|
2008-05-26 19:58:59 +00:00
|
|
|
NF->copyAttributesFrom(I);
|
2010-06-23 23:55:51 +00:00
|
|
|
VMap[I] = NF;
|
2005-05-09 01:04:34 +00:00
|
|
|
}
|
2002-11-20 20:47:41 +00:00
|
|
|
|
2007-07-10 19:07:35 +00:00
|
|
|
// Loop over the aliases in the module
|
|
|
|
for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
|
2011-08-15 21:05:06 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
GlobalAlias *GA = new GlobalAlias(I->getType(), I->getLinkage(),
|
|
|
|
I->getName(), NULL, New);
|
|
|
|
GA->copyAttributesFrom(I);
|
|
|
|
VMap[I] = GA;
|
|
|
|
}
|
2007-07-10 19:07:35 +00:00
|
|
|
|
2002-11-20 20:47:41 +00: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 01:04:34 +00:00
|
|
|
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
|
|
|
|
I != E; ++I) {
|
2010-06-23 23:55:51 +00:00
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
|
2002-11-20 20:47:41 +00: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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
GV->setInitializer(MapValue(I->getInitializer(), VMap));
|
2002-11-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Similarly, copy over function bodies now...
|
|
|
|
//
|
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
2010-06-23 23:55:51 +00:00
|
|
|
Function *F = cast<Function>(VMap[I]);
|
2007-01-30 20:08:39 +00:00
|
|
|
if (!I->isDeclaration()) {
|
2005-03-15 04:54:21 +00:00
|
|
|
Function::arg_iterator DestI = F->arg_begin();
|
2005-05-09 01:04:34 +00:00
|
|
|
for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
|
|
|
|
++J) {
|
2002-11-20 20:47:41 +00:00
|
|
|
DestI->setName(J->getName());
|
2010-06-23 23:55:51 +00:00
|
|
|
VMap[J] = DestI++;
|
2002-11-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
2009-08-27 04:02:30 +00:00
|
|
|
SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
|
2010-08-26 15:41:53 +00:00
|
|
|
CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
|
2002-11-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-10 19:07:35 +00:00
|
|
|
// And aliases
|
|
|
|
for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
|
|
|
|
I != E; ++I) {
|
2010-06-23 23:55:51 +00:00
|
|
|
GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
|
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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
if (const Constant *C = I->getAliasee())
|
|
|
|
GA->setAliasee(MapValue(C, VMap));
|
2007-07-10 19:07:35 +00:00
|
|
|
}
|
2010-06-22 18:52:38 +00: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-21 23:38:33 +00:00
|
|
|
NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
|
2010-06-22 18:52:38 +00:00
|
|
|
for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
|
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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@134829 91177308-0d34-0410-b5e6-96231b3b80d8
2011-07-09 17:41:24 +00:00
|
|
|
NewNMD->addOperand(MapValue(NMD.getOperand(i), VMap));
|
2010-06-22 18:52:38 +00:00
|
|
|
}
|
2010-06-22 22:50:42 +00:00
|
|
|
|
2002-11-20 20:47:41 +00:00
|
|
|
return New;
|
|
|
|
}
|