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/Transforms/Utils/Cloning.h" 16 #include "llvm/Module.h" 17 #include "llvm/DerivedTypes.h" 18 #include "llvm/Constant.h" 19 #include "llvm/Transforms/Utils/ValueMapper.h" 20 using namespace llvm; 21 22 /// CloneModule - Return an exact copy of the specified module. This is not as 23 /// easy as it might seem because we have to worry about making copies of global 24 /// variables and functions, and making their (initializers and references, 25 /// respectively) refer to the right globals. 26 /// 27 Module *llvm::CloneModule(const Module *M) { 28 // Create the value map that maps things from the old module over to the new 29 // module. 30 ValueToValueMapTy VMap; 31 return CloneModule(M, VMap); 32 } 33 34 Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) { 35 // First off, we need to create the new module. 36 Module *New = new Module(M->getModuleIdentifier(), M->getContext()); 37 New->setDataLayout(M->getDataLayout()); 38 New->setTargetTriple(M->getTargetTriple()); 39 New->setModuleInlineAsm(M->getModuleInlineAsm()); 40 41 // Copy all of the dependent libraries over. 42 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I) 43 New->addLibrary(*I); 44 45 // Loop over all of the global variables, making corresponding globals in the 46 // new module. Here we add them to the VMap and to the new Module. We 47 // don't worry about attributes or initializers, they will come later. 48 // 49 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); 50 I != E; ++I) { 51 GlobalVariable *GV = new GlobalVariable(*New, 52 I->getType()->getElementType(), 53 false, 54 GlobalValue::ExternalLinkage, 0, 55 I->getName()); 56 GV->setAlignment(I->getAlignment()); 57 VMap[I] = GV; 58 } 59 60 // Loop over the functions in the module, making external functions as before 61 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { 62 Function *NF = 63 Function::Create(cast<FunctionType>(I->getType()->getElementType()), 64 GlobalValue::ExternalLinkage, I->getName(), New); 65 NF->copyAttributesFrom(I); 66 VMap[I] = NF; 67 } 68 69 // Loop over the aliases in the module 70 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); 71 I != E; ++I) 72 VMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage, 73 I->getName(), NULL, New); 74 75 // Now that all of the things that global variable initializer can refer to 76 // have been created, loop through and copy the global variable referrers 77 // over... We also set the attributes on the global now. 78 // 79 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); 80 I != E; ++I) { 81 GlobalVariable *GV = cast<GlobalVariable>(VMap[I]); 82 if (I->hasInitializer()) 83 GV->setInitializer(MapValue(I->getInitializer(), VMap)); 84 GV->setLinkage(I->getLinkage()); 85 GV->setThreadLocal(I->isThreadLocal()); 86 GV->setConstant(I->isConstant()); 87 } 88 89 // Similarly, copy over function bodies now... 90 // 91 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { 92 Function *F = cast<Function>(VMap[I]); 93 if (!I->isDeclaration()) { 94 Function::arg_iterator DestI = F->arg_begin(); 95 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end(); 96 ++J) { 97 DestI->setName(J->getName()); 98 VMap[J] = DestI++; 99 } 100 101 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned. 102 CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns); 103 } 104 105 F->setLinkage(I->getLinkage()); 106 } 107 108 // And aliases 109 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); 110 I != E; ++I) { 111 GlobalAlias *GA = cast<GlobalAlias>(VMap[I]); 112 GA->setLinkage(I->getLinkage()); 113 if (const Constant *C = I->getAliasee()) 114 GA->setAliasee(MapValue(C, VMap)); 115 } 116 117 // And named metadata.... 118 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), 119 E = M->named_metadata_end(); I != E; ++I) { 120 const NamedMDNode &NMD = *I; 121 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName()); 122 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) 123 NewNMD->addOperand(MapValue(NMD.getOperand(i), VMap)); 124 } 125 126 return New; 127 } 128