blob: b16a02adbd6c7d240c13f843b8f81978ad919743 [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
15#include "llvm/Transforms/Utils/Cloning.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constant.h"
17#include "llvm/IR/DerivedTypes.h"
18#include "llvm/IR/Module.h"
Dan Gohmana2095032010-08-24 18:50:07 +000019#include "llvm/Transforms/Utils/ValueMapper.h"
Tom Stellard0a4e9a32014-10-01 17:14:57 +000020#include "llvm-c/Core.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Rafael Espindolacab951d2015-12-08 23:57:17 +000023/// This is not as easy as it might seem because we have to worry about making
24/// copies of global variables and functions, and making their (initializers and
25/// references, respectively) refer to the right globals.
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000026///
Rafael Espindolacab951d2015-12-08 23:57:17 +000027std::unique_ptr<Module> llvm::CloneModule(const Module *M) {
Chris Lattner2e266802006-05-17 18:05:35 +000028 // Create the value map that maps things from the old module over to the new
29 // module.
Devang Pateld8dedee2010-06-24 00:00:42 +000030 ValueToValueMapTy VMap;
Devang Patelb8f11de2010-06-23 23:55:51 +000031 return CloneModule(M, VMap);
Chris Lattner2e266802006-05-17 18:05:35 +000032}
33
Rafael Espindolacab951d2015-12-08 23:57:17 +000034std::unique_ptr<Module> llvm::CloneModule(const Module *M,
35 ValueToValueMapTy &VMap) {
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000036 return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; });
37}
38
Rafael Espindolacab951d2015-12-08 23:57:17 +000039std::unique_ptr<Module> llvm::CloneModule(
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000040 const Module *M, ValueToValueMapTy &VMap,
41 std::function<bool(const GlobalValue *)> ShouldCloneDefinition) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000042 // First off, we need to create the new module.
Rafael Espindolacab951d2015-12-08 23:57:17 +000043 std::unique_ptr<Module> New =
44 llvm::make_unique<Module>(M->getModuleIdentifier(), M->getContext());
Reid Spencer3ac38e92007-01-26 08:11:39 +000045 New->setDataLayout(M->getDataLayout());
Chris Lattnerb98282d2006-01-18 21:32:45 +000046 New->setTargetTriple(M->getTargetTriple());
Chris Lattner00fcdfe2006-01-24 04:16:34 +000047 New->setModuleInlineAsm(M->getModuleInlineAsm());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000048
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000049 // Loop over all of the global variables, making corresponding globals in the
Devang Patelb8f11de2010-06-23 23:55:51 +000050 // new module. Here we add them to the VMap and to the new Module. We
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000051 // don't worry about attributes or initializers, they will come later.
52 //
Chris Lattner21d1dde2005-05-09 01:04:34 +000053 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Nick Lewycky03c5fa12008-10-09 06:27:14 +000054 I != E; ++I) {
Owen Andersonb17f3292009-07-08 19:03:57 +000055 GlobalVariable *GV = new GlobalVariable(*New,
Manuel Jacob5f6eaac2016-01-16 20:30:46 +000056 I->getValueType(),
Eli Friedman4d051982011-08-15 21:05:06 +000057 I->isConstant(), I->getLinkage(),
Craig Topperf40110f2014-04-25 05:29:35 +000058 (Constant*) nullptr, I->getName(),
59 (GlobalVariable*) nullptr,
Hans Wennborgcbe34b42012-06-23 11:37:03 +000060 I->getThreadLocalMode(),
Eli Friedman4d051982011-08-15 21:05:06 +000061 I->getType()->getAddressSpace());
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000062 GV->copyAttributesFrom(&*I);
63 VMap[&*I] = GV;
Nick Lewycky03c5fa12008-10-09 06:27:14 +000064 }
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000065
66 // Loop over the functions in the module, making external functions as before
Chris Lattner21d1dde2005-05-09 01:04:34 +000067 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +000068 Function *NF =
Manuel Jacob5f6eaac2016-01-16 20:30:46 +000069 Function::Create(cast<FunctionType>(I->getValueType()),
Rafael Espindolacab951d2015-12-08 23:57:17 +000070 I->getLinkage(), I->getName(), New.get());
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000071 NF->copyAttributesFrom(&*I);
72 VMap[&*I] = NF;
Chris Lattner21d1dde2005-05-09 01:04:34 +000073 }
Chris Lattnere4dbb1a2002-11-20 20:47:41 +000074
Anton Korobeynikov76547342007-07-10 19:07:35 +000075 // Loop over the aliases in the module
76 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
Eli Friedman4d051982011-08-15 21:05:06 +000077 I != E; ++I) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000078 if (!ShouldCloneDefinition(&*I)) {
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000079 // An alias cannot act as an external reference, so we need to create
80 // either a function or a global variable depending on the value type.
81 // FIXME: Once pointee types are gone we can probably pick one or the
82 // other.
83 GlobalValue *GV;
84 if (I->getValueType()->isFunctionTy())
85 GV = Function::Create(cast<FunctionType>(I->getValueType()),
Rafael Espindolacab951d2015-12-08 23:57:17 +000086 GlobalValue::ExternalLinkage, I->getName(),
87 New.get());
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000088 else
89 GV = new GlobalVariable(
90 *New, I->getValueType(), false, GlobalValue::ExternalLinkage,
91 (Constant *)nullptr, I->getName(), (GlobalVariable *)nullptr,
92 I->getThreadLocalMode(), I->getType()->getAddressSpace());
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000093 VMap[&*I] = GV;
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000094 // We do not copy attributes (mainly because copying between different
95 // kinds of globals is forbidden), but this is generally not required for
96 // correctness.
97 continue;
98 }
David Blaikie6614d8d2015-09-14 20:29:26 +000099 auto *GA = GlobalAlias::create(I->getValueType(),
100 I->getType()->getPointerAddressSpace(),
Rafael Espindolacab951d2015-12-08 23:57:17 +0000101 I->getLinkage(), I->getName(), New.get());
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000102 GA->copyAttributesFrom(&*I);
103 VMap[&*I] = GA;
Eli Friedman4d051982011-08-15 21:05:06 +0000104 }
Anton Korobeynikov76547342007-07-10 19:07:35 +0000105
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000106 // Now that all of the things that global variable initializer can refer to
107 // have been created, loop through and copy the global variable referrers
108 // over... We also set the attributes on the global now.
109 //
Chris Lattner21d1dde2005-05-09 01:04:34 +0000110 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
111 I != E; ++I) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000112 GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]);
113 if (!ShouldCloneDefinition(&*I)) {
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000114 // Skip after setting the correct linkage for an external reference.
115 GV->setLinkage(GlobalValue::ExternalLinkage);
116 continue;
117 }
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000118 if (I->hasInitializer())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000119 GV->setInitializer(MapValue(I->getInitializer(), VMap));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000120 }
121
122 // Similarly, copy over function bodies now...
123 //
124 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000125 Function *F = cast<Function>(VMap[&*I]);
126 if (!ShouldCloneDefinition(&*I)) {
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000127 // Skip after setting the correct linkage for an external reference.
128 F->setLinkage(GlobalValue::ExternalLinkage);
129 continue;
130 }
Reid Spencer5301e7c2007-01-30 20:08:39 +0000131 if (!I->isDeclaration()) {
Chris Lattner531f9e92005-03-15 04:54:21 +0000132 Function::arg_iterator DestI = F->arg_begin();
Chris Lattner21d1dde2005-05-09 01:04:34 +0000133 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
134 ++J) {
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000135 DestI->setName(J->getName());
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000136 VMap[&*J] = &*DestI++;
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000137 }
138
Keno Fischer7c7c3e32016-02-13 02:04:29 +0000139 CloneDebugInfoMetadata(F, &*I, VMap);
Chris Lattnerd84dbb32009-08-27 04:02:30 +0000140 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000141 CloneFunctionInto(F, &*I, VMap, /*ModuleLevelChanges=*/true, Returns);
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000142 }
David Majnemercda86882015-06-30 22:14:01 +0000143
144 if (I->hasPersonalityFn())
145 F->setPersonalityFn(MapValue(I->getPersonalityFn(), VMap));
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000146 }
147
Anton Korobeynikov76547342007-07-10 19:07:35 +0000148 // And aliases
149 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
150 I != E; ++I) {
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000151 // We already dealt with undefined aliases above.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000152 if (!ShouldCloneDefinition(&*I))
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000153 continue;
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000154 GlobalAlias *GA = cast<GlobalAlias>(VMap[&*I]);
Rafael Espindola64c1e182014-06-03 02:41:57 +0000155 if (const Constant *C = I->getAliasee())
Michael Kuperstein0bf33ff2014-12-23 08:23:45 +0000156 GA->setAliasee(MapValue(C, VMap));
Anton Korobeynikov76547342007-07-10 19:07:35 +0000157 }
Devang Patele3fbbd12010-06-22 18:52:38 +0000158
159 // And named metadata....
160 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
161 E = M->named_metadata_end(); I != E; ++I) {
162 const NamedMDNode &NMD = *I;
Dan Gohman2637cc12010-07-21 23:38:33 +0000163 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
Devang Patele3fbbd12010-06-22 18:52:38 +0000164 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
Duncan P. N. Exon Smith46d7af52014-12-19 06:06:18 +0000165 NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
Devang Patele3fbbd12010-06-22 18:52:38 +0000166 }
Devang Patele43c6482010-06-22 22:50:42 +0000167
Chris Lattnere4dbb1a2002-11-20 20:47:41 +0000168 return New;
169}
Tom Stellard0a4e9a32014-10-01 17:14:57 +0000170
171extern "C" {
172
173LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
Rafael Espindolacab951d2015-12-08 23:57:17 +0000174 return wrap(CloneModule(unwrap(M)).release());
Tom Stellard0a4e9a32014-10-01 17:14:57 +0000175}
176
177}