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/Constant.h"
20 #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 *CloneModule(const Module *M) {
30 // First off, we need to create the new module...
31 Module *New = new Module(M->getModuleIdentifier());
32 New->setEndianness(M->getEndianness());
33 New->setPointerSize(M->getPointerSize());
35 // Copy all of the type symbol table entries over...
36 const SymbolTable &SymTab = M->getSymbolTable();
37 SymbolTable::const_iterator TypeI = SymTab.find(Type::TypeTy);
38 if (TypeI != SymTab.end())
39 for (SymbolTable::VarMap::const_iterator I = TypeI->second.begin(),
40 E = TypeI->second.end(); I != E; ++I)
41 New->addTypeName(I->first, cast<Type>(I->second));
43 // Create the value map that maps things from the old module over to the new
45 std::map<const Value*, Value*> ValueMap;
47 // Loop over all of the global variables, making corresponding globals in the
48 // new module. Here we add them to the ValueMap and to the new Module. We
49 // don't worry about attributes or initializers, they will come later.
51 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
52 ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
53 GlobalValue::ExternalLinkage, 0,
56 // Loop over the functions in the module, making external functions as before
57 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
58 ValueMap[I]=new Function(cast<FunctionType>(I->getType()->getElementType()),
59 GlobalValue::ExternalLinkage, I->getName(), New);
61 // Now that all of the things that global variable initializer can refer to
62 // have been created, loop through and copy the global variable referrers
63 // over... We also set the attributes on the global now.
65 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
66 GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
67 if (I->hasInitializer())
68 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
70 GV->setLinkage(I->getLinkage());
73 // Similarly, copy over function bodies now...
75 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
76 Function *F = cast<Function>(ValueMap[I]);
77 if (!I->isExternal()) {
78 Function::aiterator DestI = F->abegin();
79 for (Function::const_aiterator J = I->abegin(); J != I->aend(); ++J) {
80 DestI->setName(J->getName());
81 ValueMap[J] = DestI++;
84 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
85 CloneFunctionInto(F, I, ValueMap, Returns);
88 F->setLinkage(I->getLinkage());
94 } // End llvm namespace