blob: afebd5bd5d158cd67c347da9370d0b7d66e30cd8 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- CloneModule.cpp - Clone an entire module ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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"
18#include "llvm/TypeSymbolTable.h"
19#include "llvm/Constant.h"
Anton Korobeynikovad7ea242007-11-09 12:27:04 +000020#include "llvm/Transforms/Utils/ValueMapper.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021using namespace llvm;
22
23/// CloneModule - Return an exact copy of the specified module. This is not as
24/// easy as it might seem because we have to worry about making copies of global
25/// variables and functions, and making their (initializers and references,
26/// respectively) refer to the right globals.
27///
28Module *llvm::CloneModule(const Module *M) {
29 // Create the value map that maps things from the old module over to the new
30 // module.
31 DenseMap<const Value*, Value*> ValueMap;
32 return CloneModule(M, ValueMap);
33}
34
35Module *llvm::CloneModule(const Module *M,
36 DenseMap<const Value*, Value*> &ValueMap) {
37 // First off, we need to create the new module...
Owen Anderson25209b42009-07-01 16:58:40 +000038 Module *New = new Module(M->getModuleIdentifier(), M->getContext());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 New->setDataLayout(M->getDataLayout());
40 New->setTargetTriple(M->getTargetTriple());
41 New->setModuleInlineAsm(M->getModuleInlineAsm());
42
43 // Copy all of the type symbol table entries over.
44 const TypeSymbolTable &TST = M->getTypeSymbolTable();
45 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
46 TI != TE; ++TI)
47 New->addTypeName(TI->first, TI->second);
48
49 // Copy all of the dependent libraries over.
50 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
51 New->addLibrary(*I);
52
53 // Loop over all of the global variables, making corresponding globals in the
54 // new module. Here we add them to the ValueMap and to the new Module. We
55 // don't worry about attributes or initializers, they will come later.
56 //
57 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
Nick Lewycky2b8703f2008-10-09 06:27:14 +000058 I != E; ++I) {
Owen Andersone0f136d2009-07-08 01:26:06 +000059 GlobalVariable *GV = new GlobalVariable(M->getContext(),
60 I->getType()->getElementType(),
Nick Lewycky2b8703f2008-10-09 06:27:14 +000061 false,
62 GlobalValue::ExternalLinkage, 0,
63 I->getName(), New);
64 GV->setAlignment(I->getAlignment());
65 ValueMap[I] = GV;
66 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067
68 // Loop over the functions in the module, making external functions as before
69 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
70 Function *NF =
Gabor Greifd6da1d02008-04-06 20:25:17 +000071 Function::Create(cast<FunctionType>(I->getType()->getElementType()),
72 GlobalValue::ExternalLinkage, I->getName(), New);
Duncan Sands0cc90582008-05-26 19:58:59 +000073 NF->copyAttributesFrom(I);
Nick Lewycky2b8703f2008-10-09 06:27:14 +000074 ValueMap[I] = NF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 }
76
77 // Loop over the aliases in the module
78 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
79 I != E; ++I)
80 ValueMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage,
81 I->getName(), NULL, New);
82
83 // Now that all of the things that global variable initializer can refer to
84 // have been created, loop through and copy the global variable referrers
85 // over... We also set the attributes on the global now.
86 //
87 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
88 I != E; ++I) {
89 GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
90 if (I->hasInitializer())
91 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
Owen Andersona09d2342009-07-05 22:41:43 +000092 ValueMap,
93 &M->getContext())));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 GV->setLinkage(I->getLinkage());
95 GV->setThreadLocal(I->isThreadLocal());
96 GV->setConstant(I->isConstant());
97 }
98
99 // Similarly, copy over function bodies now...
100 //
101 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
102 Function *F = cast<Function>(ValueMap[I]);
103 if (!I->isDeclaration()) {
104 Function::arg_iterator DestI = F->arg_begin();
105 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
106 ++J) {
107 DestI->setName(J->getName());
108 ValueMap[J] = DestI++;
109 }
110
111 std::vector<ReturnInst*> Returns; // Ignore returns cloned...
112 CloneFunctionInto(F, I, ValueMap, Returns);
113 }
114
115 F->setLinkage(I->getLinkage());
116 }
117
118 // And aliases
119 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
120 I != E; ++I) {
121 GlobalAlias *GA = cast<GlobalAlias>(ValueMap[I]);
122 GA->setLinkage(I->getLinkage());
123 if (const Constant* C = I->getAliasee())
Owen Andersona09d2342009-07-05 22:41:43 +0000124 GA->setAliasee(cast<Constant>(MapValue(C, ValueMap, &M->getContext())));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 }
126
127 return New;
128}