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