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/IR/Constant.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Transforms/Utils/ValueMapper.h"
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.
27 Module *llvm::CloneModule(const Module *M) {
28 // Create the value map that maps things from the old module over to the new
30 ValueToValueMapTy VMap;
31 return CloneModule(M, VMap);
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());
41 // Loop over all of the global variables, making corresponding globals in the
42 // new module. Here we add them to the VMap and to the new Module. We
43 // don't worry about attributes or initializers, they will come later.
45 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
47 GlobalVariable *GV = new GlobalVariable(*New,
48 I->getType()->getElementType(),
49 I->isConstant(), I->getLinkage(),
50 (Constant*) nullptr, I->getName(),
51 (GlobalVariable*) nullptr,
52 I->getThreadLocalMode(),
53 I->getType()->getAddressSpace());
54 GV->copyAttributesFrom(I);
58 // Loop over the functions in the module, making external functions as before
59 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
61 Function::Create(cast<FunctionType>(I->getType()->getElementType()),
62 I->getLinkage(), I->getName(), New);
63 NF->copyAttributesFrom(I);
67 // Loop over the aliases in the module
68 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
70 auto *PTy = cast<PointerType>(I->getType());
72 GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
73 I->getLinkage(), I->getName(), New);
74 GA->copyAttributesFrom(I);
78 // Now that all of the things that global variable initializer can refer to
79 // have been created, loop through and copy the global variable referrers
80 // over... We also set the attributes on the global now.
82 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
84 GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
85 if (I->hasInitializer())
86 GV->setInitializer(MapValue(I->getInitializer(), VMap));
89 // Similarly, copy over function bodies now...
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();
97 DestI->setName(J->getName());
101 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
102 CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
107 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
109 GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
110 if (const Constant *C = I->getAliasee())
111 GA->setAliasee(cast<GlobalObject>(MapValue(C, VMap)));
114 // And named metadata....
115 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
116 E = M->named_metadata_end(); I != E; ++I) {
117 const NamedMDNode &NMD = *I;
118 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
119 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
120 NewNMD->addOperand(MapValue(NMD.getOperand(i), VMap));