blob: 21a8675f07b9595229e4e1e87021890ad836b241 [file] [log] [blame]
Chris Lattner51cbcbf2002-11-20 20:47:41 +00001//===- CloneModule.cpp - Clone an entire module ---------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner51cbcbf2002-11-20 20:47:41 +00009//
10// This file implements the CloneModule interface which makes a copy of an
11// entire module.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Transforms/Utils/Cloning.h"
16#include "llvm/Module.h"
17#include "llvm/DerivedTypes.h"
Chris Lattner892310e2003-04-24 17:15:33 +000018#include "llvm/SymbolTable.h"
Chris Lattner51cbcbf2002-11-20 20:47:41 +000019#include "llvm/Constant.h"
20#include "ValueMapper.h"
21
Brian Gaeked0fde302003-11-11 22:41:34 +000022namespace llvm {
23
Chris Lattner51cbcbf2002-11-20 20:47:41 +000024/// 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
Misha Brukmancf00c4a2003-10-10 17:57:28 +000026/// variables and functions, and making their (initializers and references,
Chris Lattner51cbcbf2002-11-20 20:47:41 +000027/// respectively) refer to the right globals.
28///
29Module *CloneModule(const Module *M) {
30 // First off, we need to create the new module...
Chris Lattner37f59412003-04-22 18:02:26 +000031 Module *New = new Module(M->getModuleIdentifier());
Chris Lattner7899b742003-04-24 15:54:40 +000032 New->setEndianness(M->getEndianness());
33 New->setPointerSize(M->getPointerSize());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000034
Chris Lattner892310e2003-04-24 17:15:33 +000035 // 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));
42
Chris Lattner51cbcbf2002-11-20 20:47:41 +000043 // Create the value map that maps things from the old module over to the new
44 // module.
45 std::map<const Value*, Value*> ValueMap;
46
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.
50 //
51 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
Chris Lattner4ad02e72003-04-16 20:28:45 +000052 ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
53 GlobalValue::ExternalLinkage, 0,
54 I->getName(), New);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000055
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()),
Chris Lattner4ad02e72003-04-16 20:28:45 +000059 GlobalValue::ExternalLinkage, I->getName(), New);
Chris Lattner51cbcbf2002-11-20 20:47:41 +000060
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.
64 //
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(),
69 ValueMap)));
Chris Lattner4ad02e72003-04-16 20:28:45 +000070 GV->setLinkage(I->getLinkage());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000071 }
72
73 // Similarly, copy over function bodies now...
74 //
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++;
82 }
83
84 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
85 CloneFunctionInto(F, I, ValueMap, Returns);
86 }
87
Chris Lattner4ad02e72003-04-16 20:28:45 +000088 F->setLinkage(I->getLinkage());
Chris Lattner51cbcbf2002-11-20 20:47:41 +000089 }
90
91 return New;
92}
Brian Gaeked0fde302003-11-11 22:41:34 +000093
94} // End llvm namespace