1 //===- CloneModule.cpp - Clone an entire module ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/SymbolTable.h"
19 #include "llvm/TypeSymbolTable.h"
20 #include "llvm/Constant.h"
21 #include "ValueMapper.h"
24 /// CloneModule - Return an exact copy of the specified module. This is not as
25 /// easy as it might seem because we have to worry about making copies of global
26 /// variables and functions, and making their (initializers and references,
27 /// respectively) refer to the right globals.
29 Module *llvm::CloneModule(const Module *M) {
30 // Create the value map that maps things from the old module over to the new
32 DenseMap<const Value*, Value*> ValueMap;
33 return CloneModule(M, ValueMap);
36 Module *llvm::CloneModule(const Module *M,
37 DenseMap<const Value*, Value*> &ValueMap) {
38 // First off, we need to create the new module...
39 Module *New = new Module(M->getModuleIdentifier());
40 New->setDataLayout(M->getDataLayout());
41 New->setTargetTriple(M->getTargetTriple());
42 New->setModuleInlineAsm(M->getModuleInlineAsm());
44 // Copy all of the type symbol table entries over.
45 const TypeSymbolTable &TST = M->getTypeSymbolTable();
46 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
48 New->addTypeName(TI->first, TI->second);
50 // Copy all of the dependent libraries over.
51 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
54 // Loop over all of the global variables, making corresponding globals in the
55 // new module. Here we add them to the ValueMap and to the new Module. We
56 // don't worry about attributes or initializers, they will come later.
58 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
60 ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
61 GlobalValue::ExternalLinkage, 0,
64 // Loop over the functions in the module, making external functions as before
65 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
67 new Function(cast<FunctionType>(I->getType()->getElementType()),
68 GlobalValue::ExternalLinkage, I->getName(), New);
69 NF->setCallingConv(I->getCallingConv());
73 // Now that all of the things that global variable initializer can refer to
74 // have been created, loop through and copy the global variable referrers
75 // over... We also set the attributes on the global now.
77 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
79 GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
80 if (I->hasInitializer())
81 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
83 GV->setLinkage(I->getLinkage());
86 // Similarly, copy over function bodies now...
88 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
89 Function *F = cast<Function>(ValueMap[I]);
90 if (!I->isDeclaration()) {
91 Function::arg_iterator DestI = F->arg_begin();
92 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
94 DestI->setName(J->getName());
95 ValueMap[J] = DestI++;
98 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
99 CloneFunctionInto(F, I, ValueMap, Returns);
102 F->setLinkage(I->getLinkage());