1 //===- CloneModule.cpp - Clone an entire module ---------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the CloneModule interface which makes a copy of an 11 // entire module. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/IR/Constant.h" 16 #include "llvm/IR/DerivedTypes.h" 17 #include "llvm/IR/Module.h" 18 #include "llvm/Transforms/Utils/Cloning.h" 19 #include "llvm/Transforms/Utils/ValueMapper.h" 20 using namespace llvm; 21 22 static 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 31 /// 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. 34 /// 35 std::unique_ptr<Module> llvm::CloneModule(const Module *M) { 36 // Create the value map that maps things from the old module over to the new 37 // module. 38 ValueToValueMapTy VMap; 39 return CloneModule(M, VMap); 40 } 41 42 std::unique_ptr<Module> llvm::CloneModule(const Module *M, 43 ValueToValueMapTy &VMap) { 44 return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; }); 45 } 46 47 std::unique_ptr<Module> llvm::CloneModule( 48 const Module *M, ValueToValueMapTy &VMap, 49 function_ref<bool(const GlobalValue *)> ShouldCloneDefinition) { 50 // First off, we need to create the new module. 51 std::unique_ptr<Module> New = 52 llvm::make_unique<Module>(M->getModuleIdentifier(), M->getContext()); 53 New->setDataLayout(M->getDataLayout()); 54 New->setTargetTriple(M->getTargetTriple()); 55 New->setModuleInlineAsm(M->getModuleInlineAsm()); 56 57 // Loop over all of the global variables, making corresponding globals in the 58 // new module. Here we add them to the VMap and to the new Module. We 59 // don't worry about attributes or initializers, they will come later. 60 // 61 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); 62 I != E; ++I) { 63 GlobalVariable *GV = new GlobalVariable(*New, 64 I->getValueType(), 65 I->isConstant(), I->getLinkage(), 66 (Constant*) nullptr, I->getName(), 67 (GlobalVariable*) nullptr, 68 I->getThreadLocalMode(), 69 I->getType()->getAddressSpace()); 70 GV->copyAttributesFrom(&*I); 71 VMap[&*I] = GV; 72 } 73 74 // Loop over the functions in the module, making external functions as before 75 for (const Function &I : *M) { 76 Function *NF = Function::Create(cast<FunctionType>(I.getValueType()), 77 I.getLinkage(), I.getName(), New.get()); 78 NF->copyAttributesFrom(&I); 79 VMap[&I] = NF; 80 } 81 82 // Loop over the aliases in the module 83 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); 84 I != E; ++I) { 85 if (!ShouldCloneDefinition(&*I)) { 86 // An alias cannot act as an external reference, so we need to create 87 // either a function or a global variable depending on the value type. 88 // FIXME: Once pointee types are gone we can probably pick one or the 89 // other. 90 GlobalValue *GV; 91 if (I->getValueType()->isFunctionTy()) 92 GV = Function::Create(cast<FunctionType>(I->getValueType()), 93 GlobalValue::ExternalLinkage, I->getName(), 94 New.get()); 95 else 96 GV = new GlobalVariable( 97 *New, I->getValueType(), false, GlobalValue::ExternalLinkage, 98 nullptr, I->getName(), nullptr, 99 I->getThreadLocalMode(), I->getType()->getAddressSpace()); 100 VMap[&*I] = GV; 101 // We do not copy attributes (mainly because copying between different 102 // kinds of globals is forbidden), but this is generally not required for 103 // correctness. 104 continue; 105 } 106 auto *GA = GlobalAlias::create(I->getValueType(), 107 I->getType()->getPointerAddressSpace(), 108 I->getLinkage(), I->getName(), New.get()); 109 GA->copyAttributesFrom(&*I); 110 VMap[&*I] = GA; 111 } 112 113 // Now that all of the things that global variable initializer can refer to 114 // have been created, loop through and copy the global variable referrers 115 // over... We also set the attributes on the global now. 116 // 117 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); 118 I != E; ++I) { 119 if (I->isDeclaration()) 120 continue; 121 122 GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]); 123 if (!ShouldCloneDefinition(&*I)) { 124 // Skip after setting the correct linkage for an external reference. 125 GV->setLinkage(GlobalValue::ExternalLinkage); 126 continue; 127 } 128 if (I->hasInitializer()) 129 GV->setInitializer(MapValue(I->getInitializer(), VMap)); 130 131 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs; 132 I->getAllMetadata(MDs); 133 for (auto MD : MDs) 134 GV->addMetadata(MD.first, 135 *MapMetadata(MD.second, VMap, RF_MoveDistinctMDs)); 136 137 copyComdat(GV, &*I); 138 } 139 140 // Similarly, copy over function bodies now... 141 // 142 for (const Function &I : *M) { 143 if (I.isDeclaration()) 144 continue; 145 146 Function *F = cast<Function>(VMap[&I]); 147 if (!ShouldCloneDefinition(&I)) { 148 // Skip after setting the correct linkage for an external reference. 149 F->setLinkage(GlobalValue::ExternalLinkage); 150 // Personality function is not valid on a declaration. 151 F->setPersonalityFn(nullptr); 152 continue; 153 } 154 155 Function::arg_iterator DestI = F->arg_begin(); 156 for (Function::const_arg_iterator J = I.arg_begin(); J != I.arg_end(); 157 ++J) { 158 DestI->setName(J->getName()); 159 VMap[&*J] = &*DestI++; 160 } 161 162 SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned. 163 CloneFunctionInto(F, &I, VMap, /*ModuleLevelChanges=*/true, Returns); 164 165 if (I.hasPersonalityFn()) 166 F->setPersonalityFn(MapValue(I.getPersonalityFn(), VMap)); 167 168 copyComdat(F, &I); 169 } 170 171 // And aliases 172 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); 173 I != E; ++I) { 174 // We already dealt with undefined aliases above. 175 if (!ShouldCloneDefinition(&*I)) 176 continue; 177 GlobalAlias *GA = cast<GlobalAlias>(VMap[&*I]); 178 if (const Constant *C = I->getAliasee()) 179 GA->setAliasee(MapValue(C, VMap)); 180 } 181 182 // And named metadata.... 183 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), 184 E = M->named_metadata_end(); I != E; ++I) { 185 const NamedMDNode &NMD = *I; 186 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName()); 187 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) 188 NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap)); 189 } 190 191 return New; 192 } 193 194 extern "C" { 195 196 LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) { 197 return wrap(CloneModule(unwrap(M)).release()); 198 } 199 200 } 201