1 //===- CloneModule.cpp - Clone an entire module ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the CloneModule interface which makes a copy of an
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/Utils/Cloning.h"
16 #include "llvm/Module.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/TypeSymbolTable.h"
19 #include "llvm/Constant.h"
20 #include "ValueMapper.h"
23 /// CloneModule - Return an exact copy of the specified module. This is not as
24 /// easy as it might seem because we have to worry about making copies of global
25 /// variables and functions, and making their (initializers and references,
26 /// respectively) refer to the right globals.
28 Module *llvm::CloneModule(const Module *M) {
29 // Create the value map that maps things from the old module over to the new
31 ValueToValueMapTy VMap;
32 return CloneModule(M, VMap);
35 Module *llvm::CloneModule(const Module *M,
36 ValueToValueMapTy &VMap) {
37 // First off, we need to create the new module...
38 Module *New = new Module(M->getModuleIdentifier(), M->getContext());
39 New->setDataLayout(M->getDataLayout());
40 New->setTargetTriple(M->getTargetTriple());
41 New->setModuleInlineAsm(M->getModuleInlineAsm());
43 // Copy all of the type symbol table entries over.
44 const TypeSymbolTable &TST = M->getTypeSymbolTable();
45 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
47 New->addTypeName(TI->first, TI->second);
49 // Copy all of the dependent libraries over.
50 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
53 // Loop over all of the global variables, making corresponding globals in the
54 // new module. Here we add them to the VMap and to the new Module. We
55 // don't worry about attributes or initializers, they will come later.
57 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
59 GlobalVariable *GV = new GlobalVariable(*New,
60 I->getType()->getElementType(),
62 GlobalValue::ExternalLinkage, 0,
64 GV->setAlignment(I->getAlignment());
68 // Loop over the functions in the module, making external functions as before
69 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
71 Function::Create(cast<FunctionType>(I->getType()->getElementType()),
72 GlobalValue::ExternalLinkage, I->getName(), New);
73 NF->copyAttributesFrom(I);
77 // Loop over the aliases in the module
78 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
80 VMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage,
81 I->getName(), NULL, New);
83 // Now that all of the things that global variable initializer can refer to
84 // have been created, loop through and copy the global variable referrers
85 // over... We also set the attributes on the global now.
87 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
89 GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
90 if (I->hasInitializer())
91 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
93 GV->setLinkage(I->getLinkage());
94 GV->setThreadLocal(I->isThreadLocal());
95 GV->setConstant(I->isConstant());
98 // Similarly, copy over function bodies now...
100 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
101 Function *F = cast<Function>(VMap[I]);
102 if (!I->isDeclaration()) {
103 Function::arg_iterator DestI = F->arg_begin();
104 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
106 DestI->setName(J->getName());
110 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
111 CloneFunctionInto(F, I, VMap, Returns);
114 F->setLinkage(I->getLinkage());
118 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
120 GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
121 GA->setLinkage(I->getLinkage());
122 if (const Constant* C = I->getAliasee())
123 GA->setAliasee(cast<Constant>(MapValue(C, VMap)));
126 // And named metadata....
127 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
128 E = M->named_metadata_end(); I != E; ++I) {
129 const NamedMDNode &NMD = *I;
130 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
131 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
132 NewNMD->addOperand(cast<MDNode>(MapValue(NMD.getOperand(i), VMap)));
135 // Update metadata attach with instructions.
136 for (Module::iterator MI = New->begin(), ME = New->end(); MI != ME; ++MI)
137 for (Function::iterator FI = MI->begin(), FE = MI->end();
139 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
141 SmallVector<std::pair<unsigned, MDNode *>, 4 > MDs;
142 BI->getAllMetadata(MDs);
143 for (SmallVector<std::pair<unsigned, MDNode *>, 4>::iterator
144 MDI = MDs.begin(), MDE = MDs.end(); MDI != MDE; ++MDI) {
145 Value *MappedValue = MapValue(MDI->second, VMap);
146 if (MDI->second != MappedValue && MappedValue)
147 BI->setMetadata(MDI->first, cast<MDNode>(MappedValue));