0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===- CloneModule.cpp - Clone an entire module ---------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 // The LLVM Compiler Infrastructure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // This file is distributed under the University of Illinois Open Source
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // License. See LICENSE.TXT for details.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 // This file implements the CloneModule interface which makes a copy of an
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 // entire module.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14
|
121
|
15 #include "llvm-c/Core.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 #include "llvm/IR/Constant.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 #include "llvm/IR/DerivedTypes.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 #include "llvm/IR/Module.h"
|
121
|
19 #include "llvm/Transforms/Utils/Cloning.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 #include "llvm/Transforms/Utils/ValueMapper.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22
|
121
|
23 static void copyComdat(GlobalObject *Dst, const GlobalObject *Src) {
|
|
24 const Comdat *SC = Src->getComdat();
|
|
25 if (!SC)
|
|
26 return;
|
|
27 Comdat *DC = Dst->getParent()->getOrInsertComdat(SC->getName());
|
|
28 DC->setSelectionKind(SC->getSelectionKind());
|
|
29 Dst->setComdat(DC);
|
|
30 }
|
|
31
|
100
|
32 /// This is not as easy as it might seem because we have to worry about making
|
|
33 /// copies of global variables and functions, and making their (initializers and
|
|
34 /// references, respectively) refer to the right globals.
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 ///
|
100
|
36 std::unique_ptr<Module> llvm::CloneModule(const Module *M) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 // Create the value map that maps things from the old module over to the new
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 // module.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 ValueToValueMapTy VMap;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 return CloneModule(M, VMap);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42
|
100
|
43 std::unique_ptr<Module> llvm::CloneModule(const Module *M,
|
|
44 ValueToValueMapTy &VMap) {
|
95
|
45 return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; });
|
|
46 }
|
|
47
|
100
|
48 std::unique_ptr<Module> llvm::CloneModule(
|
95
|
49 const Module *M, ValueToValueMapTy &VMap,
|
120
|
50 function_ref<bool(const GlobalValue *)> ShouldCloneDefinition) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 // First off, we need to create the new module.
|
100
|
52 std::unique_ptr<Module> New =
|
|
53 llvm::make_unique<Module>(M->getModuleIdentifier(), M->getContext());
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 New->setDataLayout(M->getDataLayout());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 New->setTargetTriple(M->getTargetTriple());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 New->setModuleInlineAsm(M->getModuleInlineAsm());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 // Loop over all of the global variables, making corresponding globals in the
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 // new module. Here we add them to the VMap and to the new Module. We
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 // don't worry about attributes or initializers, they will come later.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63 I != E; ++I) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 GlobalVariable *GV = new GlobalVariable(*New,
|
100
|
65 I->getValueType(),
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66 I->isConstant(), I->getLinkage(),
|
77
|
67 (Constant*) nullptr, I->getName(),
|
|
68 (GlobalVariable*) nullptr,
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 I->getThreadLocalMode(),
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 I->getType()->getAddressSpace());
|
95
|
71 GV->copyAttributesFrom(&*I);
|
|
72 VMap[&*I] = GV;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
74
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75 // Loop over the functions in the module, making external functions as before
|
120
|
76 for (const Function &I : *M) {
|
|
77 Function *NF = Function::Create(cast<FunctionType>(I.getValueType()),
|
|
78 I.getLinkage(), I.getName(), New.get());
|
|
79 NF->copyAttributesFrom(&I);
|
|
80 VMap[&I] = NF;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
81 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 // Loop over the aliases in the module
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 I != E; ++I) {
|
95
|
86 if (!ShouldCloneDefinition(&*I)) {
|
|
87 // An alias cannot act as an external reference, so we need to create
|
|
88 // either a function or a global variable depending on the value type.
|
|
89 // FIXME: Once pointee types are gone we can probably pick one or the
|
|
90 // other.
|
|
91 GlobalValue *GV;
|
|
92 if (I->getValueType()->isFunctionTy())
|
|
93 GV = Function::Create(cast<FunctionType>(I->getValueType()),
|
100
|
94 GlobalValue::ExternalLinkage, I->getName(),
|
|
95 New.get());
|
95
|
96 else
|
|
97 GV = new GlobalVariable(
|
|
98 *New, I->getValueType(), false, GlobalValue::ExternalLinkage,
|
121
|
99 nullptr, I->getName(), nullptr,
|
95
|
100 I->getThreadLocalMode(), I->getType()->getAddressSpace());
|
|
101 VMap[&*I] = GV;
|
|
102 // We do not copy attributes (mainly because copying between different
|
|
103 // kinds of globals is forbidden), but this is generally not required for
|
|
104 // correctness.
|
|
105 continue;
|
|
106 }
|
|
107 auto *GA = GlobalAlias::create(I->getValueType(),
|
|
108 I->getType()->getPointerAddressSpace(),
|
100
|
109 I->getLinkage(), I->getName(), New.get());
|
95
|
110 GA->copyAttributesFrom(&*I);
|
|
111 VMap[&*I] = GA;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
112 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
113
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
114 // Now that all of the things that global variable initializer can refer to
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
115 // have been created, loop through and copy the global variable referrers
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
116 // over... We also set the attributes on the global now.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
117 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
118 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
119 I != E; ++I) {
|
120
|
120 if (I->isDeclaration())
|
|
121 continue;
|
|
122
|
95
|
123 GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]);
|
|
124 if (!ShouldCloneDefinition(&*I)) {
|
|
125 // Skip after setting the correct linkage for an external reference.
|
|
126 GV->setLinkage(GlobalValue::ExternalLinkage);
|
|
127 continue;
|
|
128 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
129 if (I->hasInitializer())
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
130 GV->setInitializer(MapValue(I->getInitializer(), VMap));
|
120
|
131
|
|
132 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
|
|
133 I->getAllMetadata(MDs);
|
|
134 for (auto MD : MDs)
|
121
|
135 GV->addMetadata(MD.first,
|
|
136 *MapMetadata(MD.second, VMap, RF_MoveDistinctMDs));
|
|
137
|
|
138 copyComdat(GV, &*I);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
139 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
140
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
141 // Similarly, copy over function bodies now...
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
142 //
|
120
|
143 for (const Function &I : *M) {
|
|
144 if (I.isDeclaration())
|
|
145 continue;
|
|
146
|
|
147 Function *F = cast<Function>(VMap[&I]);
|
|
148 if (!ShouldCloneDefinition(&I)) {
|
95
|
149 // Skip after setting the correct linkage for an external reference.
|
|
150 F->setLinkage(GlobalValue::ExternalLinkage);
|
120
|
151 // Personality function is not valid on a declaration.
|
|
152 F->setPersonalityFn(nullptr);
|
95
|
153 continue;
|
|
154 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
155
|
120
|
156 Function::arg_iterator DestI = F->arg_begin();
|
|
157 for (Function::const_arg_iterator J = I.arg_begin(); J != I.arg_end();
|
|
158 ++J) {
|
|
159 DestI->setName(J->getName());
|
|
160 VMap[&*J] = &*DestI++;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
161 }
|
95
|
162
|
120
|
163 SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
|
|
164 CloneFunctionInto(F, &I, VMap, /*ModuleLevelChanges=*/true, Returns);
|
|
165
|
|
166 if (I.hasPersonalityFn())
|
|
167 F->setPersonalityFn(MapValue(I.getPersonalityFn(), VMap));
|
121
|
168
|
|
169 copyComdat(F, &I);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
170 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
171
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
172 // And aliases
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
173 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
174 I != E; ++I) {
|
95
|
175 // We already dealt with undefined aliases above.
|
|
176 if (!ShouldCloneDefinition(&*I))
|
|
177 continue;
|
|
178 GlobalAlias *GA = cast<GlobalAlias>(VMap[&*I]);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
179 if (const Constant *C = I->getAliasee())
|
83
|
180 GA->setAliasee(MapValue(C, VMap));
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
181 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
182
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
183 // And named metadata....
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
184 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
185 E = M->named_metadata_end(); I != E; ++I) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
186 const NamedMDNode &NMD = *I;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
187 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
188 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
|
83
|
189 NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
190 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
191
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
192 return New;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
193 }
|
83
|
194
|
|
195 extern "C" {
|
|
196
|
|
197 LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
|
100
|
198 return wrap(CloneModule(unwrap(M)).release());
|
83
|
199 }
|
|
200
|
|
201 }
|