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