1 //===- CloneModule.cpp - Clone an entire module ---------------------------===//
3 // This file implements the CloneModule interface which makes a copy of an
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Transforms/Utils/Cloning.h"
9 #include "llvm/Module.h"
10 #include "llvm/DerivedTypes.h"
11 #include "llvm/SymbolTable.h"
12 #include "llvm/Constant.h"
13 #include "ValueMapper.h"
15 /// CloneModule - Return an exact copy of the specified module. This is not as
16 /// easy as it might seem because we have to worry about making copies of global
17 /// variables and functions, and making their (initializers and references,
18 /// respectively) refer to the right globals.
20 Module *CloneModule(const Module *M) {
21 // First off, we need to create the new module...
22 Module *New = new Module(M->getModuleIdentifier());
23 New->setEndianness(M->getEndianness());
24 New->setPointerSize(M->getPointerSize());
26 // Copy all of the type symbol table entries over...
27 const SymbolTable &SymTab = M->getSymbolTable();
28 SymbolTable::const_iterator TypeI = SymTab.find(Type::TypeTy);
29 if (TypeI != SymTab.end())
30 for (SymbolTable::VarMap::const_iterator I = TypeI->second.begin(),
31 E = TypeI->second.end(); I != E; ++I)
32 New->addTypeName(I->first, cast<Type>(I->second));
34 // Create the value map that maps things from the old module over to the new
36 std::map<const Value*, Value*> ValueMap;
38 // Loop over all of the global variables, making corresponding globals in the
39 // new module. Here we add them to the ValueMap and to the new Module. We
40 // don't worry about attributes or initializers, they will come later.
42 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
43 ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
44 GlobalValue::ExternalLinkage, 0,
47 // Loop over the functions in the module, making external functions as before
48 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
49 ValueMap[I]=new Function(cast<FunctionType>(I->getType()->getElementType()),
50 GlobalValue::ExternalLinkage, I->getName(), New);
52 // Now that all of the things that global variable initializer can refer to
53 // have been created, loop through and copy the global variable referrers
54 // over... We also set the attributes on the global now.
56 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
57 GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
58 if (I->hasInitializer())
59 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
61 GV->setLinkage(I->getLinkage());
64 // Similarly, copy over function bodies now...
66 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
67 Function *F = cast<Function>(ValueMap[I]);
68 if (!I->isExternal()) {
69 Function::aiterator DestI = F->abegin();
70 for (Function::const_aiterator J = I->abegin(); J != I->aend(); ++J) {
71 DestI->setName(J->getName());
72 ValueMap[J] = DestI++;
75 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
76 CloneFunctionInto(F, I, ValueMap, Returns);
79 F->setLinkage(I->getLinkage());