2002-11-20 20:47:41 +00:00
|
|
|
//===- CloneModule.cpp - Clone an entire module ---------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constant.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2017-06-06 11:49:48 +00:00
|
|
|
#include "llvm/Transforms/Utils/Cloning.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
|
|
|
|
2017-01-18 20:02:31 +00: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-08 23:57:17 +00: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-20 20:47:41 +00:00
|
|
|
///
|
2018-02-14 19:50:40 +00:00
|
|
|
std::unique_ptr<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
|
|
|
}
|
|
|
|
|
2018-02-14 19:50:40 +00:00
|
|
|
std::unique_ptr<Module> llvm::CloneModule(const Module &M,
|
2015-12-08 23:57:17 +00:00
|
|
|
ValueToValueMapTy &VMap) {
|
2015-08-21 02:48:20 +00:00
|
|
|
return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; });
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:57:17 +00:00
|
|
|
std::unique_ptr<Module> llvm::CloneModule(
|
2018-02-14 19:50:40 +00:00
|
|
|
const Module &M, ValueToValueMapTy &VMap,
|
2016-06-12 16:13:55 +00: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-09 17:41:24 +00:00
|
|
|
// First off, we need to create the new module.
|
2015-12-08 23:57:17 +00:00
|
|
|
std::unique_ptr<Module> New =
|
2018-02-14 19:50:40 +00:00
|
|
|
llvm::make_unique<Module>(M.getModuleIdentifier(), M.getContext());
|
[DebugInfo][OPT] Fixing a couple of DI duplication bugs of CloneModule
As demonstrated by the regression tests added in this patch, the
following cases are valid cases:
1. A Function with no DISubprogram attached, but various debug info
related to its instructions, coming, for instance, from an inlined
function, also defined somewhere else in the same module;
2. ... or coming exclusively from the functions inlined and eliminated
from the module entirely.
The ValueMap shared between CloneFunctionInto calls within CloneModule
needs to contain identity mappings for all of the DISubprogram's to
prevent them from being duplicated by MapMetadata / RemapInstruction
calls, this is achieved via DebugInfoFinder collecting all the
DISubprogram's. However, CloneFunctionInto was missing calls into
DebugInfoFinder for functions w/o DISubprogram's attached, but still
referring DISubprogram's from within (case 1). This patch fixes that.
The fix above, however, exposes another issue: if a module contains a
DISubprogram referenced only indirectly from other debug info
metadata, but not attached to any Function defined within the module
(case 2), cloning such a module causes a DICompileUnit duplication: it
will be moved in indirecty via a DISubprogram by DebugInfoFinder first
(because of the first bug fix described above), without being
self-mapped within the shared ValueMap, and then will be copied during
named metadata cloning. So this patch makes sure DebugInfoFinder
visits DICompileUnit's referenced from DISubprogram's as it goes w/o
re-processing llvm.dbg.cu list over and over again for every function
cloned, and makes sure that CloneFunctionInto self-maps
DICompileUnit's referenced from the entire function, not just its own
DISubprogram attached that may also be missing.
The most convenient way of tesing CloneModule I found is to rely on
CloneModule call from `opt -run-twice`, instead of writing tedious
unit tests. That feature has a couple of properties that makes it hard
to use for this purpose though:
1. CloneModule doesn't copy source filename, making `opt -run-twice`
report it as a difference.
2. `opt -run-twice` does the second run on the original module, not
its clone, making the result of cloning completely invisible in opt's
actual output with and without `-run-twice` both, which directly
contradicts `opt -run-twice`s own error message.
This patch fixes this as well.
Reviewed By: aprantl
Reviewers: loladiro, GorNishanov, espindola, echristo, dexonsmith
Subscribers: vsk, debug-info, JDevlieghere, llvm-commits
Differential Revision: https://reviews.llvm.org/D45593
llvm-svn: 330069
2018-04-13 21:22:24 +00:00
|
|
|
New->setSourceFileName(M.getSourceFileName());
|
2018-02-14 19:50:40 +00:00
|
|
|
New->setDataLayout(M.getDataLayout());
|
|
|
|
New->setTargetTriple(M.getTargetTriple());
|
|
|
|
New->setModuleInlineAsm(M.getModuleInlineAsm());
|
|
|
|
|
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.
|
|
|
|
//
|
2018-02-14 19:50:40 +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) {
|
2018-07-30 19:41:25 +00:00
|
|
|
GlobalVariable *GV = new GlobalVariable(*New,
|
2016-01-16 20:30:46 +00:00
|
|
|
I->getValueType(),
|
2011-08-15 21:05:06 +00:00
|
|
|
I->isConstant(), I->getLinkage(),
|
2014-04-25 05:29:35 +00:00
|
|
|
(Constant*) nullptr, I->getName(),
|
|
|
|
(GlobalVariable*) nullptr,
|
2012-06-23 11:37:03 +00:00
|
|
|
I->getThreadLocalMode(),
|
2011-08-15 21:05:06 +00:00
|
|
|
I->getType()->getAddressSpace());
|
2015-10-13 02:39:05 +00:00
|
|
|
GV->copyAttributesFrom(&*I);
|
|
|
|
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
|
2018-02-14 19:50:40 +00:00
|
|
|
for (const Function &I : M) {
|
2018-08-23 09:25:17 +00:00
|
|
|
Function *NF =
|
|
|
|
Function::Create(cast<FunctionType>(I.getValueType()), I.getLinkage(),
|
|
|
|
I.getAddressSpace(), I.getName(), New.get());
|
2016-06-26 12:28:59 +00:00
|
|
|
NF->copyAttributesFrom(&I);
|
|
|
|
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
|
2018-02-14 19:50:40 +00:00
|
|
|
for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
|
2011-08-15 21:05:06 +00:00
|
|
|
I != E; ++I) {
|
2015-10-13 02:39:05 +00:00
|
|
|
if (!ShouldCloneDefinition(&*I)) {
|
2015-08-21 02:48:20 +00: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()),
|
2018-08-23 09:25:17 +00:00
|
|
|
GlobalValue::ExternalLinkage,
|
|
|
|
I->getAddressSpace(), I->getName(), New.get());
|
2015-08-21 02:48:20 +00:00
|
|
|
else
|
|
|
|
GV = new GlobalVariable(
|
|
|
|
*New, I->getValueType(), false, GlobalValue::ExternalLinkage,
|
2017-05-11 08:53:00 +00:00
|
|
|
nullptr, I->getName(), nullptr,
|
2015-08-21 02:48:20 +00:00
|
|
|
I->getThreadLocalMode(), I->getType()->getAddressSpace());
|
2015-10-13 02:39:05 +00:00
|
|
|
VMap[&*I] = GV;
|
2015-08-21 02:48:20 +00: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-14 20:29:26 +00:00
|
|
|
auto *GA = GlobalAlias::create(I->getValueType(),
|
|
|
|
I->getType()->getPointerAddressSpace(),
|
2015-12-08 23:57:17 +00:00
|
|
|
I->getLinkage(), I->getName(), New.get());
|
2015-10-13 02:39:05 +00:00
|
|
|
GA->copyAttributesFrom(&*I);
|
|
|
|
VMap[&*I] = GA;
|
2011-08-15 21:05:06 +00:00
|
|
|
}
|
2018-07-30 19:41:25 +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.
|
|
|
|
//
|
2018-02-14 19:50:40 +00:00
|
|
|
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
|
2005-05-09 01:04:34 +00:00
|
|
|
I != E; ++I) {
|
2016-03-31 20:21:31 +00:00
|
|
|
if (I->isDeclaration())
|
|
|
|
continue;
|
|
|
|
|
2015-10-13 02:39:05 +00:00
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]);
|
|
|
|
if (!ShouldCloneDefinition(&*I)) {
|
2015-08-21 02:48:20 +00:00
|
|
|
// Skip after setting the correct linkage for an external reference.
|
|
|
|
GV->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
continue;
|
|
|
|
}
|
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.
llvm-svn: 134829
2011-07-09 17:41:24 +00:00
|
|
|
GV->setInitializer(MapValue(I->getInitializer(), VMap));
|
2016-10-26 02:57:33 +00:00
|
|
|
|
|
|
|
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
|
|
|
|
I->getAllMetadata(MDs);
|
|
|
|
for (auto MD : MDs)
|
[Cloning] Move distinct GlobalVariable debug info metadata in CloneModule
Duplicating the distinct Subprogram and CU metadata nodes seems like the incorrect thing to do in CloneModule for GlobalVariable debug info. As it results in the scope of the GlobalVariable DI no longer being consistent with the rest of the module, and the new CU is absent from llvm.dbg.cu.
Fixed by adding RF_MoveDistinctMDs to MapMetadata flags for GlobalVariables.
Current unit test IR after clone:
```
@gv = global i32 1, comdat($comdat), !dbg !0, !type !5
define private void @f() comdat($comdat) personality void ()* @persfn !dbg !14 {
!llvm.dbg.cu = !{!10}
!0 = !DIGlobalVariableExpression(var: !1)
!1 = distinct !DIGlobalVariable(name: "gv", linkageName: "gv", scope: !2, file: !3, line: 1, type: !9, isLocal: false, isDefinition: true)
!2 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !3, line: 4, type: !4, isLocal: true, isDefinition: true, scopeLine: 3, isOptimized: false, unit: !6, variables: !5)
!3 = !DIFile(filename: "filename.c", directory: "/file/dir/")
!4 = !DISubroutineType(types: !5)
!5 = !{}
!6 = distinct !DICompileUnit(language: DW_LANG_C99, file: !7, producer: "CloneModule", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !5, globals: !8)
!7 = !DIFile(filename: "filename.c", directory: "/file/dir")
!8 = !{!0}
!9 = !DIBasicType(tag: DW_TAG_unspecified_type, name: "decltype(nullptr)")
!10 = distinct !DICompileUnit(language: DW_LANG_C99, file: !7, producer: "CloneModule", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !5, globals: !11)
!11 = !{!12}
!12 = !DIGlobalVariableExpression(var: !13)
!13 = distinct !DIGlobalVariable(name: "gv", linkageName: "gv", scope: !14, file: !3, line: 1, type: !9, isLocal: false, isDefinition: true)
!14 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !3, line: 4, type: !4, isLocal: true, isDefinition: true, scopeLine: 3, isOptimized: false, unit: !10, variables: !5)
```
Patched IR after clone:
```
@gv = global i32 1, comdat($comdat), !dbg !0, !type !5
define private void @f() comdat($comdat) personality void ()* @persfn !dbg !2 {
!llvm.dbg.cu = !{!6}
!0 = !DIGlobalVariableExpression(var: !1)
!1 = distinct !DIGlobalVariable(name: "gv", linkageName: "gv", scope: !2, file: !3, line: 1, type: !9, isLocal: false, isDefinition: true)
!2 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !3, line: 4, type: !4, isLocal: true, isDefinition: true, scopeLine: 3, isOptimized: false, unit: !6, variables: !5)
!3 = !DIFile(filename: "filename.c", directory: "/file/dir/")
!4 = !DISubroutineType(types: !5)
!5 = !{}
!6 = distinct !DICompileUnit(language: DW_LANG_C99, file: !7, producer: "CloneModule", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !5, globals: !8)
!7 = !DIFile(filename: "filename.c", directory: "/file/dir")
!8 = !{!0}
!9 = !DIBasicType(tag: DW_TAG_unspecified_type, name: "decltype(nullptr)")
```
Reviewers: aprantl, probinson, dblaikie, echristo, loladiro
Reviewed By: aprantl
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D36082
llvm-svn: 309928
2017-08-03 09:23:03 +00:00
|
|
|
GV->addMetadata(MD.first,
|
|
|
|
*MapMetadata(MD.second, VMap, RF_MoveDistinctMDs));
|
2017-01-18 20:02:31 +00:00
|
|
|
|
|
|
|
copyComdat(GV, &*I);
|
2002-11-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Similarly, copy over function bodies now...
|
|
|
|
//
|
2018-02-14 19:50:40 +00:00
|
|
|
for (const Function &I : M) {
|
2016-06-26 12:28:59 +00:00
|
|
|
if (I.isDeclaration())
|
2016-03-31 20:21:31 +00:00
|
|
|
continue;
|
|
|
|
|
2016-06-26 12:28:59 +00:00
|
|
|
Function *F = cast<Function>(VMap[&I]);
|
|
|
|
if (!ShouldCloneDefinition(&I)) {
|
2015-08-21 02:48:20 +00:00
|
|
|
// Skip after setting the correct linkage for an external reference.
|
|
|
|
F->setLinkage(GlobalValue::ExternalLinkage);
|
2016-03-28 21:37:02 +00:00
|
|
|
// Personality function is not valid on a declaration.
|
|
|
|
F->setPersonalityFn(nullptr);
|
2015-08-21 02:48:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-03-31 20:21:31 +00:00
|
|
|
|
|
|
|
Function::arg_iterator DestI = F->arg_begin();
|
2016-06-26 12:28:59 +00:00
|
|
|
for (Function::const_arg_iterator J = I.arg_begin(); J != I.arg_end();
|
2016-03-31 20:21:31 +00:00
|
|
|
++J) {
|
|
|
|
DestI->setName(J->getName());
|
|
|
|
VMap[&*J] = &*DestI++;
|
2002-11-20 20:47:41 +00:00
|
|
|
}
|
2015-06-30 22:14:01 +00:00
|
|
|
|
2016-03-31 20:21:31 +00:00
|
|
|
SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
|
2016-06-26 12:28:59 +00:00
|
|
|
CloneFunctionInto(F, &I, VMap, /*ModuleLevelChanges=*/true, Returns);
|
2016-03-31 20:21:31 +00:00
|
|
|
|
2016-06-26 12:28:59 +00:00
|
|
|
if (I.hasPersonalityFn())
|
|
|
|
F->setPersonalityFn(MapValue(I.getPersonalityFn(), VMap));
|
2017-01-18 20:02:31 +00:00
|
|
|
|
|
|
|
copyComdat(F, &I);
|
2002-11-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
2007-07-10 19:07:35 +00:00
|
|
|
// And aliases
|
2018-02-14 19:50:40 +00:00
|
|
|
for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
|
2007-07-10 19:07:35 +00:00
|
|
|
I != E; ++I) {
|
2015-08-21 02:48:20 +00:00
|
|
|
// We already dealt with undefined aliases above.
|
2015-10-13 02:39:05 +00:00
|
|
|
if (!ShouldCloneDefinition(&*I))
|
2015-08-21 02:48:20 +00:00
|
|
|
continue;
|
2015-10-13 02:39:05 +00:00
|
|
|
GlobalAlias *GA = cast<GlobalAlias>(VMap[&*I]);
|
2014-06-03 02:41:57 +00:00
|
|
|
if (const Constant *C = I->getAliasee())
|
2014-12-23 08:23:45 +00:00
|
|
|
GA->setAliasee(MapValue(C, VMap));
|
2007-07-10 19:07:35 +00:00
|
|
|
}
|
2010-06-22 18:52:38 +00:00
|
|
|
|
|
|
|
// And named metadata....
|
2018-02-14 19:50:40 +00:00
|
|
|
for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
|
|
|
|
E = M.named_metadata_end();
|
|
|
|
I != E; ++I) {
|
2010-06-22 18:52:38 +00:00
|
|
|
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)
|
2014-12-19 06:06:18 +00:00
|
|
|
NewNMD->addOperand(MapMetadata(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;
|
|
|
|
}
|
2014-10-01 17:14:57 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
|
2018-02-14 19:50:40 +00:00
|
|
|
return wrap(CloneModule(*unwrap(M)).release());
|
2014-10-01 17:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|