blob: 2a7d1ed586e21e1d5abf0edb611a942356abf3e7 [file] [log] [blame]
Chris Lattner51cbcbf2002-11-20 20:47:41 +00001//===- CloneModule.cpp - Clone an entire module ---------------------------===//
2//
3// This file implements the CloneModule interface which makes a copy of an
4// entire module.
5//
6//===----------------------------------------------------------------------===//
7
8#include "llvm/Transforms/Utils/Cloning.h"
9#include "llvm/Module.h"
10#include "llvm/DerivedTypes.h"
Chris Lattner892310e2003-04-24 17:15:33 +000011#include "llvm/SymbolTable.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000012#include "llvm/Constant.h"
13#include "ValueMapper.h"
14
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
Misha Brukmancf00c4a2003-10-10 17:57:28 +000017/// variables and functions, and making their (initializers and references,
Chris Lattner51cbcbf2002-11-20 20:47:41 +000018/// respectively) refer to the right globals.
19///
20Module *CloneModule(const Module *M) {
21 // First off, we need to create the new module...
Chris Lattner37f59412003-04-22 18:02:26 +000022 Module *New = new Module(M->getModuleIdentifier());
Chris Lattner7899b742003-04-24 15:54:40 +000023 New->setEndianness(M->getEndianness());
24 New->setPointerSize(M->getPointerSize());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000025
Chris Lattner892310e2003-04-24 17:15:33 +000026 // 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));
33
Chris Lattner51cbcbf2002-11-20 20:47:41 +000034 // Create the value map that maps things from the old module over to the new
35 // module.
36 std::map<const Value*, Value*> ValueMap;
37
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.
41 //
42 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattner4ad02e72003-04-16 20:28:45 +000043 ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
44 GlobalValue::ExternalLinkage, 0,
45 I->getName(), New);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000046
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()),
Chris Lattner4ad02e72003-04-16 20:28:45 +000050 GlobalValue::ExternalLinkage, I->getName(), New);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000051
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.
55 //
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(),
60 ValueMap)));
Chris Lattner4ad02e72003-04-16 20:28:45 +000061 GV->setLinkage(I->getLinkage());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000062 }
63
64 // Similarly, copy over function bodies now...
65 //
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++;
73 }
74
75 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
76 CloneFunctionInto(F, I, ValueMap, Returns);
77 }
78
Chris Lattner4ad02e72003-04-16 20:28:45 +000079 F->setLinkage(I->getLinkage());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000080 }
81
82 return New;
83}