blob: c7d68bab817003a275fd620350aa5f59a07d77c7 [file] [log] [blame]
Chris Lattnere4dbb1a2002-11-20 20:47:41 +00001//===- CloneModule.cpp - Clone an entire module ---------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnere4dbb1a2002-11-20 20:47:41 +00009//
10// This file implements the CloneModule interface which makes a copy of an
11// entire module.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Constant.h"
16#include "llvm/IR/DerivedTypes.h"
17#include "llvm/IR/Module.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/Transforms/Utils/Cloning.h"
Dan Gohmana2095032010-08-24 18:50:07 +000019#include "llvm/Transforms/Utils/ValueMapper.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000020using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000021
Peter Collingbourne10e3b122017-01-18 20:02:31 +000022static void copyComdat(GlobalObject *Dst, const GlobalObject *Src) {
23 const Comdat *SC = Src->getComdat();
24 if (!SC)
25 return;
26 Comdat *DC = Dst->getParent()->getOrInsertComdat(SC->getName());
27 DC->setSelectionKind(SC->getSelectionKind());
28 Dst->setComdat(DC);
29}
30
Rafael Espindolacab951d2015-12-08 23:57:17 +000031/// This is not as easy as it might seem because we have to worry about making
32/// copies of global variables and functions, and making their (initializers and
33/// references, respectively) refer to the right globals.
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000034///
Rafael Espindola71867532018-02-14 19:50:40 +000035std::unique_ptr<Module> llvm::CloneModule(const Module &M) {
Chris Lattner2e266802006-05-17 18:05:35 +000036 // Create the value map that maps things from the old module over to the new
37 // module.
Devang Pateld8dedee2010-06-24 00:00:42 +000038 ValueToValueMapTy VMap;
Devang Patelb8f11de2010-06-23 23:55:51 +000039 return CloneModule(M, VMap);
Chris Lattner2e266802006-05-17 18:05:35 +000040}
41
Rafael Espindola71867532018-02-14 19:50:40 +000042std::unique_ptr<Module> llvm::CloneModule(const Module &M,
Rafael Espindolacab951d2015-12-08 23:57:17 +000043 ValueToValueMapTy &VMap) {
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000044 return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; });
45}
46
Rafael Espindolacab951d2015-12-08 23:57:17 +000047std::unique_ptr<Module> llvm::CloneModule(
Rafael Espindola71867532018-02-14 19:50:40 +000048 const Module &M, ValueToValueMapTy &VMap,
Benjamin Kramerd3f4c052016-06-12 16:13:55 +000049 function_ref<bool(const GlobalValue *)> ShouldCloneDefinition) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000050 // First off, we need to create the new module.
Rafael Espindolacab951d2015-12-08 23:57:17 +000051 std::unique_ptr<Module> New =
Rafael Espindola71867532018-02-14 19:50:40 +000052 llvm::make_unique<Module>(M.getModuleIdentifier(), M.getContext());
Roman Tereshind769eb32018-04-13 21:22:24 +000053 New->setSourceFileName(M.getSourceFileName());
Rafael Espindola71867532018-02-14 19:50:40 +000054 New->setDataLayout(M.getDataLayout());
55 New->setTargetTriple(M.getTargetTriple());
56 New->setModuleInlineAsm(M.getModuleInlineAsm());
57
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000058 // Loop over all of the global variables, making corresponding globals in the
Devang Patelb8f11de2010-06-23 23:55:51 +000059 // new module. Here we add them to the VMap and to the new Module. We
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000060 // don't worry about attributes or initializers, they will come later.
61 //
Rafael Espindola71867532018-02-14 19:50:40 +000062 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Nick Lewycky03c5fa12008-10-09 06:27:14 +000063 I != E; ++I) {
Fangrui Songf78650a2018-07-30 19:41:25 +000064 GlobalVariable *GV = new GlobalVariable(*New,
Manuel Jacob5f6eaac2016-01-16 20:30:46 +000065 I->getValueType(),
Eli Friedman4d051982011-08-15 21:05:06 +000066 I->isConstant(), I->getLinkage(),
Craig Topperf40110f2014-04-25 05:29:35 +000067 (Constant*) nullptr, I->getName(),
68 (GlobalVariable*) nullptr,
Hans Wennborgcbe34b42012-06-23 11:37:03 +000069 I->getThreadLocalMode(),
Eli Friedman4d051982011-08-15 21:05:06 +000070 I->getType()->getAddressSpace());
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000071 GV->copyAttributesFrom(&*I);
72 VMap[&*I] = GV;
Nick Lewycky03c5fa12008-10-09 06:27:14 +000073 }
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000074
75 // Loop over the functions in the module, making external functions as before
Rafael Espindola71867532018-02-14 19:50:40 +000076 for (const Function &I : M) {
Benjamin Kramer135f7352016-06-26 12:28:59 +000077 Function *NF = Function::Create(cast<FunctionType>(I.getValueType()),
78 I.getLinkage(), I.getName(), New.get());
79 NF->copyAttributesFrom(&I);
80 VMap[&I] = NF;
Chris Lattner21d1dde2005-05-09 01:04:34 +000081 }
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000082
Anton Korobeynikov76547342007-07-10 19:07:35 +000083 // Loop over the aliases in the module
Rafael Espindola71867532018-02-14 19:50:40 +000084 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Eli Friedman4d051982011-08-15 21:05:06 +000085 I != E; ++I) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000086 if (!ShouldCloneDefinition(&*I)) {
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000087 // An alias cannot act as an external reference, so we need to create
88 // either a function or a global variable depending on the value type.
89 // FIXME: Once pointee types are gone we can probably pick one or the
90 // other.
91 GlobalValue *GV;
92 if (I->getValueType()->isFunctionTy())
93 GV = Function::Create(cast<FunctionType>(I->getValueType()),
Rafael Espindolacab951d2015-12-08 23:57:17 +000094 GlobalValue::ExternalLinkage, I->getName(),
95 New.get());
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000096 else
97 GV = new GlobalVariable(
98 *New, I->getValueType(), false, GlobalValue::ExternalLinkage,
Serge Gueltonf4dc59b2017-05-11 08:53:00 +000099 nullptr, I->getName(), nullptr,
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000100 I->getThreadLocalMode(), I->getType()->getAddressSpace());
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000101 VMap[&*I] = GV;
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000102 // We do not copy attributes (mainly because copying between different
103 // kinds of globals is forbidden), but this is generally not required for
104 // correctness.
105 continue;
106 }
David Blaikie6614d8d2015-09-14 20:29:26 +0000107 auto *GA = GlobalAlias::create(I->getValueType(),
108 I->getType()->getPointerAddressSpace(),
Rafael Espindolacab951d2015-12-08 23:57:17 +0000109 I->getLinkage(), I->getName(), New.get());
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000110 GA->copyAttributesFrom(&*I);
111 VMap[&*I] = GA;
Eli Friedman4d051982011-08-15 21:05:06 +0000112 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000113
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000114 // Now that all of the things that global variable initializer can refer to
115 // have been created, loop through and copy the global variable referrers
116 // over... We also set the attributes on the global now.
117 //
Rafael Espindola71867532018-02-14 19:50:40 +0000118 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Chris Lattner21d1dde2005-05-09 01:04:34 +0000119 I != E; ++I) {
Evgeniy Stepanova614ab72016-03-31 20:21:31 +0000120 if (I->isDeclaration())
121 continue;
122
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000123 GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]);
124 if (!ShouldCloneDefinition(&*I)) {
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000125 // Skip after setting the correct linkage for an external reference.
126 GV->setLinkage(GlobalValue::ExternalLinkage);
127 continue;
128 }
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000129 if (I->hasInitializer())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000130 GV->setInitializer(MapValue(I->getInitializer(), VMap));
Peter Collingbourne7b7bac32016-10-26 02:57:33 +0000131
132 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
133 I->getAllMetadata(MDs);
134 for (auto MD : MDs)
Ewan Crawforde18490c2017-08-03 09:23:03 +0000135 GV->addMetadata(MD.first,
136 *MapMetadata(MD.second, VMap, RF_MoveDistinctMDs));
Peter Collingbourne10e3b122017-01-18 20:02:31 +0000137
138 copyComdat(GV, &*I);
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000139 }
140
141 // Similarly, copy over function bodies now...
142 //
Rafael Espindola71867532018-02-14 19:50:40 +0000143 for (const Function &I : M) {
Benjamin Kramer135f7352016-06-26 12:28:59 +0000144 if (I.isDeclaration())
Evgeniy Stepanova614ab72016-03-31 20:21:31 +0000145 continue;
146
Benjamin Kramer135f7352016-06-26 12:28:59 +0000147 Function *F = cast<Function>(VMap[&I]);
148 if (!ShouldCloneDefinition(&I)) {
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000149 // Skip after setting the correct linkage for an external reference.
150 F->setLinkage(GlobalValue::ExternalLinkage);
Evgeniy Stepanovf575b262016-03-28 21:37:02 +0000151 // Personality function is not valid on a declaration.
152 F->setPersonalityFn(nullptr);
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000153 continue;
154 }
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000155
Evgeniy Stepanova614ab72016-03-31 20:21:31 +0000156 Function::arg_iterator DestI = F->arg_begin();
Benjamin Kramer135f7352016-06-26 12:28:59 +0000157 for (Function::const_arg_iterator J = I.arg_begin(); J != I.arg_end();
Evgeniy Stepanova614ab72016-03-31 20:21:31 +0000158 ++J) {
159 DestI->setName(J->getName());
160 VMap[&*J] = &*DestI++;
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000161 }
David Majnemercda86882015-06-30 22:14:01 +0000162
Evgeniy Stepanova614ab72016-03-31 20:21:31 +0000163 SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000164 CloneFunctionInto(F, &I, VMap, /*ModuleLevelChanges=*/true, Returns);
Evgeniy Stepanova614ab72016-03-31 20:21:31 +0000165
Benjamin Kramer135f7352016-06-26 12:28:59 +0000166 if (I.hasPersonalityFn())
167 F->setPersonalityFn(MapValue(I.getPersonalityFn(), VMap));
Peter Collingbourne10e3b122017-01-18 20:02:31 +0000168
169 copyComdat(F, &I);
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000170 }
171
Anton Korobeynikov76547342007-07-10 19:07:35 +0000172 // And aliases
Rafael Espindola71867532018-02-14 19:50:40 +0000173 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Anton Korobeynikov76547342007-07-10 19:07:35 +0000174 I != E; ++I) {
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000175 // We already dealt with undefined aliases above.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000176 if (!ShouldCloneDefinition(&*I))
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000177 continue;
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000178 GlobalAlias *GA = cast<GlobalAlias>(VMap[&*I]);
Rafael Espindola64c1e182014-06-03 02:41:57 +0000179 if (const Constant *C = I->getAliasee())
Michael Kuperstein0bf33ff2014-12-23 08:23:45 +0000180 GA->setAliasee(MapValue(C, VMap));
Anton Korobeynikov76547342007-07-10 19:07:35 +0000181 }
Devang Patele3fbbd12010-06-22 18:52:38 +0000182
183 // And named metadata....
Rafael Espindola71867532018-02-14 19:50:40 +0000184 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
185 E = M.named_metadata_end();
186 I != E; ++I) {
Devang Patele3fbbd12010-06-22 18:52:38 +0000187 const NamedMDNode &NMD = *I;
Dan Gohman2637cc12010-07-21 23:38:33 +0000188 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
Devang Patele3fbbd12010-06-22 18:52:38 +0000189 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000190 NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
Devang Patele3fbbd12010-06-22 18:52:38 +0000191 }
Devang Patele43c6482010-06-22 22:50:42 +0000192
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000193 return New;
194}
Tom Stellard0a4e9a32014-10-01 17:14:57 +0000195
196extern "C" {
197
198LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
Rafael Espindola71867532018-02-14 19:50:40 +0000199 return wrap(CloneModule(*unwrap(M)).release());
Tom Stellard0a4e9a32014-10-01 17:14:57 +0000200}
201
202}