Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 1 | //===- CloneModule.cpp - Clone an entire module ---------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 9 | // |
| 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 Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Constant.h" |
| 17 | #include "llvm/IR/DerivedTypes.h" |
| 18 | #include "llvm/IR/Module.h" |
Dan Gohman | a209503 | 2010-08-24 18:50:07 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Tom Stellard | 0a4e9a3 | 2014-10-01 17:14:57 +0000 | [diff] [blame] | 20 | #include "llvm-c/Core.h" |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 23 | /// 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 Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 26 | /// |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 27 | std::unique_ptr<Module> llvm::CloneModule(const Module *M) { |
Chris Lattner | 2e26680 | 2006-05-17 18:05:35 +0000 | [diff] [blame] | 28 | // Create the value map that maps things from the old module over to the new |
| 29 | // module. |
Devang Patel | d8dedee | 2010-06-24 00:00:42 +0000 | [diff] [blame] | 30 | ValueToValueMapTy VMap; |
Devang Patel | b8f11de | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 31 | return CloneModule(M, VMap); |
Chris Lattner | 2e26680 | 2006-05-17 18:05:35 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 34 | std::unique_ptr<Module> llvm::CloneModule(const Module *M, |
| 35 | ValueToValueMapTy &VMap) { |
Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 36 | return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; }); |
| 37 | } |
| 38 | |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 39 | std::unique_ptr<Module> llvm::CloneModule( |
Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 40 | const Module *M, ValueToValueMapTy &VMap, |
| 41 | std::function<bool(const GlobalValue *)> ShouldCloneDefinition) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 42 | // First off, we need to create the new module. |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 43 | std::unique_ptr<Module> New = |
| 44 | llvm::make_unique<Module>(M->getModuleIdentifier(), M->getContext()); |
Reid Spencer | 3ac38e9 | 2007-01-26 08:11:39 +0000 | [diff] [blame] | 45 | New->setDataLayout(M->getDataLayout()); |
Chris Lattner | b98282d | 2006-01-18 21:32:45 +0000 | [diff] [blame] | 46 | New->setTargetTriple(M->getTargetTriple()); |
Chris Lattner | 00fcdfe | 2006-01-24 04:16:34 +0000 | [diff] [blame] | 47 | New->setModuleInlineAsm(M->getModuleInlineAsm()); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 48 | |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 49 | // Loop over all of the global variables, making corresponding globals in the |
Devang Patel | b8f11de | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 50 | // new module. Here we add them to the VMap and to the new Module. We |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 51 | // don't worry about attributes or initializers, they will come later. |
| 52 | // |
Chris Lattner | 21d1dde | 2005-05-09 01:04:34 +0000 | [diff] [blame] | 53 | for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); |
Nick Lewycky | 03c5fa1 | 2008-10-09 06:27:14 +0000 | [diff] [blame] | 54 | I != E; ++I) { |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 55 | GlobalVariable *GV = new GlobalVariable(*New, |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 56 | I->getValueType(), |
Eli Friedman | 4d05198 | 2011-08-15 21:05:06 +0000 | [diff] [blame] | 57 | I->isConstant(), I->getLinkage(), |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 58 | (Constant*) nullptr, I->getName(), |
| 59 | (GlobalVariable*) nullptr, |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 60 | I->getThreadLocalMode(), |
Eli Friedman | 4d05198 | 2011-08-15 21:05:06 +0000 | [diff] [blame] | 61 | I->getType()->getAddressSpace()); |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 62 | GV->copyAttributesFrom(&*I); |
| 63 | VMap[&*I] = GV; |
Nick Lewycky | 03c5fa1 | 2008-10-09 06:27:14 +0000 | [diff] [blame] | 64 | } |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 65 | |
| 66 | // Loop over the functions in the module, making external functions as before |
Chris Lattner | 21d1dde | 2005-05-09 01:04:34 +0000 | [diff] [blame] | 67 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 68 | Function *NF = |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 69 | Function::Create(cast<FunctionType>(I->getValueType()), |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 70 | I->getLinkage(), I->getName(), New.get()); |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 71 | NF->copyAttributesFrom(&*I); |
| 72 | VMap[&*I] = NF; |
Chris Lattner | 21d1dde | 2005-05-09 01:04:34 +0000 | [diff] [blame] | 73 | } |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 74 | |
Anton Korobeynikov | 7654734 | 2007-07-10 19:07:35 +0000 | [diff] [blame] | 75 | // Loop over the aliases in the module |
| 76 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
Eli Friedman | 4d05198 | 2011-08-15 21:05:06 +0000 | [diff] [blame] | 77 | I != E; ++I) { |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 78 | if (!ShouldCloneDefinition(&*I)) { |
Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 79 | // 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 Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 86 | GlobalValue::ExternalLinkage, I->getName(), |
| 87 | New.get()); |
Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 88 | 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 Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 93 | VMap[&*I] = GV; |
Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 94 | // 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 Blaikie | 6614d8d | 2015-09-14 20:29:26 +0000 | [diff] [blame] | 99 | auto *GA = GlobalAlias::create(I->getValueType(), |
| 100 | I->getType()->getPointerAddressSpace(), |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 101 | I->getLinkage(), I->getName(), New.get()); |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 102 | GA->copyAttributesFrom(&*I); |
| 103 | VMap[&*I] = GA; |
Eli Friedman | 4d05198 | 2011-08-15 21:05:06 +0000 | [diff] [blame] | 104 | } |
Anton Korobeynikov | 7654734 | 2007-07-10 19:07:35 +0000 | [diff] [blame] | 105 | |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 106 | // 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 Lattner | 21d1dde | 2005-05-09 01:04:34 +0000 | [diff] [blame] | 110 | for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); |
| 111 | I != E; ++I) { |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 112 | GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]); |
| 113 | if (!ShouldCloneDefinition(&*I)) { |
Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 114 | // Skip after setting the correct linkage for an external reference. |
| 115 | GV->setLinkage(GlobalValue::ExternalLinkage); |
| 116 | continue; |
| 117 | } |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 118 | if (I->hasInitializer()) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 119 | GV->setInitializer(MapValue(I->getInitializer(), VMap)); |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 120 | } |
| 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 Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 125 | Function *F = cast<Function>(VMap[&*I]); |
| 126 | if (!ShouldCloneDefinition(&*I)) { |
Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 127 | // Skip after setting the correct linkage for an external reference. |
| 128 | F->setLinkage(GlobalValue::ExternalLinkage); |
| 129 | continue; |
| 130 | } |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 131 | if (!I->isDeclaration()) { |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 132 | Function::arg_iterator DestI = F->arg_begin(); |
Chris Lattner | 21d1dde | 2005-05-09 01:04:34 +0000 | [diff] [blame] | 133 | for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end(); |
| 134 | ++J) { |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 135 | DestI->setName(J->getName()); |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 136 | VMap[&*J] = &*DestI++; |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Keno Fischer | 7c7c3e3 | 2016-02-13 02:04:29 +0000 | [diff] [blame] | 139 | CloneDebugInfoMetadata(F, &*I, VMap); |
Chris Lattner | d84dbb3 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 140 | SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned. |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 141 | CloneFunctionInto(F, &*I, VMap, /*ModuleLevelChanges=*/true, Returns); |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 142 | } |
David Majnemer | cda8688 | 2015-06-30 22:14:01 +0000 | [diff] [blame] | 143 | |
| 144 | if (I->hasPersonalityFn()) |
| 145 | F->setPersonalityFn(MapValue(I->getPersonalityFn(), VMap)); |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Anton Korobeynikov | 7654734 | 2007-07-10 19:07:35 +0000 | [diff] [blame] | 148 | // And aliases |
| 149 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 150 | I != E; ++I) { |
Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 151 | // We already dealt with undefined aliases above. |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 152 | if (!ShouldCloneDefinition(&*I)) |
Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 153 | continue; |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 154 | GlobalAlias *GA = cast<GlobalAlias>(VMap[&*I]); |
Rafael Espindola | 64c1e18 | 2014-06-03 02:41:57 +0000 | [diff] [blame] | 155 | if (const Constant *C = I->getAliasee()) |
Michael Kuperstein | 0bf33ff | 2014-12-23 08:23:45 +0000 | [diff] [blame] | 156 | GA->setAliasee(MapValue(C, VMap)); |
Anton Korobeynikov | 7654734 | 2007-07-10 19:07:35 +0000 | [diff] [blame] | 157 | } |
Devang Patel | e3fbbd1 | 2010-06-22 18:52:38 +0000 | [diff] [blame] | 158 | |
| 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 Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 163 | NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName()); |
Devang Patel | e3fbbd1 | 2010-06-22 18:52:38 +0000 | [diff] [blame] | 164 | for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) |
Duncan P. N. Exon Smith | 46d7af5 | 2014-12-19 06:06:18 +0000 | [diff] [blame] | 165 | NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap)); |
Devang Patel | e3fbbd1 | 2010-06-22 18:52:38 +0000 | [diff] [blame] | 166 | } |
Devang Patel | e43c648 | 2010-06-22 22:50:42 +0000 | [diff] [blame] | 167 | |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 168 | return New; |
| 169 | } |
Tom Stellard | 0a4e9a3 | 2014-10-01 17:14:57 +0000 | [diff] [blame] | 170 | |
| 171 | extern "C" { |
| 172 | |
| 173 | LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) { |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 174 | return wrap(CloneModule(unwrap(M)).release()); |
Tom Stellard | 0a4e9a3 | 2014-10-01 17:14:57 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | } |