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