Deleted Added
full compact
CloneModule.cpp (210299) CloneModule.cpp (212904)
1//===- CloneModule.cpp - Clone an entire module ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
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"
1//===- CloneModule.cpp - Clone an entire module ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
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"
20#include "ValueMapper.h"
20#include "llvm/Transforms/Utils/ValueMapper.h"
21using 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) {

--- 55 unchanged lines hidden (view full) ---

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>(VMap[I]);
90 if (I->hasInitializer())
91 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
21using 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) {

--- 55 unchanged lines hidden (view full) ---

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>(VMap[I]);
90 if (I->hasInitializer())
91 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
92 VMap)));
92 VMap,
93 true)));
93 GV->setLinkage(I->getLinkage());
94 GV->setThreadLocal(I->isThreadLocal());
95 GV->setConstant(I->isConstant());
96 }
97
98 // Similarly, copy over function bodies now...
99 //
100 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
101 Function *F = cast<Function>(VMap[I]);
102 if (!I->isDeclaration()) {
103 Function::arg_iterator DestI = F->arg_begin();
104 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
105 ++J) {
106 DestI->setName(J->getName());
107 VMap[J] = DestI++;
108 }
109
110 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
94 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>(VMap[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 VMap[J] = DestI++;
109 }
110
111 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
111 CloneFunctionInto(F, I, VMap, Returns);
112 CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
112 }
113
114 F->setLinkage(I->getLinkage());
115 }
116
117 // And aliases
118 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
119 I != E; ++I) {
120 GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
121 GA->setLinkage(I->getLinkage());
122 if (const Constant* C = I->getAliasee())
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>(VMap[I]);
122 GA->setLinkage(I->getLinkage());
123 if (const Constant* C = I->getAliasee())
123 GA->setAliasee(cast(MapValue(C, VMap)));
124 GA->setAliasee(cast<Constant>(MapValue(C, VMap, true)));
124 }
125
126 // And named metadata....
127 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
128 E = M->named_metadata_end(); I != E; ++I) {
129 const NamedMDNode &NMD = *I;
125 }
126
127 // And named metadata....
128 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
129 E = M->named_metadata_end(); I != E; ++I) {
130 const NamedMDNode &NMD = *I;
130 SmallVector<MDNode*, 4> MDs;
131 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
131 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
132 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
132 MDs.push_back(cast<MDNode>(MapValue(NMD.getOperand(i), VMap)));
133 NamedMDNode::Create(New->getContext(), NMD.getName(),
134 MDs.data(), MDs.size(), New);
133 NewNMD->addOperand(cast<MDNode>(MapValue(NMD.getOperand(i), VMap, true)));
135 }
136
134 }
135
137 // Update metadata attach with instructions.
138 for (Module::iterator MI = New->begin(), ME = New->end(); MI != ME; ++MI)
139 for (Function::iterator FI = MI->begin(), FE = MI->end();
140 FI != FE; ++FI)
141 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
142 BI != BE; ++BI) {
143 SmallVector<std::pair<unsigned, MDNode *>, 4 > MDs;
144 BI->getAllMetadata(MDs);
145 for (SmallVector<std::pair<unsigned, MDNode *>, 4>::iterator
146 MDI = MDs.begin(), MDE = MDs.end(); MDI != MDE; ++MDI) {
147 Value *MappedValue = MapValue(MDI->second, VMap);
148 if (MDI->second != MappedValue && MappedValue)
149 BI->setMetadata(MDI->first, cast<MDNode>(MappedValue));
150 }
151 }
152 return New;
153}
136 return New;
137}