LLVM: lib/Transforms/Utils/CloneModule.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
19using namespace llvm;
20
21namespace llvm {
23}
24
26 const Comdat *SC = Src->getComdat();
27 if (!SC)
28 return;
29 Comdat *DC = Dst->getParent()->getOrInsertComdat(SC->getName());
31 Dst->setComdat(DC);
32}
33
34
35
36
37
38
39
46
51
55
56 assert(M.isMaterialized() && "Module must be materialized before cloning!");
57
58
59 std::unique_ptr New =
60 std::make_unique(M.getModuleIdentifier(), M.getContext());
61 New->setSourceFileName(M.getSourceFileName());
62 New->setDataLayout(M.getDataLayout());
63 New->setTargetTriple(M.getTargetTriple());
64 New->setModuleInlineAsm(M.getModuleInlineAsm());
65
66
67
68
69
72 *New, I.getValueType(), I.isConstant(), I.getLinkage(),
74 I.getThreadLocalMode(), I.getType()->getAddressSpace());
76 VMap[&I] = NewGV;
77 }
78
79
83 I.getAddressSpace(), I.getName(), New.get());
85 VMap[&I] = NF;
86 }
87
88
90 if (!ShouldCloneDefinition(&I)) {
91
92
93
94
96 if (I.getValueType()->isFunctionTy())
99 I.getName(), New.get());
100 else
103 I.getName(), nullptr, I.getThreadLocalMode(),
104 I.getType()->getAddressSpace());
105 VMap[&I] = GV;
106
107
108
109 continue;
110 }
112 I.getType()->getPointerAddressSpace(),
113 I.getLinkage(), I.getName(), New.get());
114 GA->copyAttributesFrom(&I);
115 VMap[&I] = GA;
116 }
117
119
120 auto *GI =
122 I.getLinkage(), I.getName(), nullptr, New.get());
123 GI->copyAttributesFrom(&I);
124 VMap[&I] = GI;
125 }
126
127
128
131
132 if (I.isDeclaration()) {
133
134
136 I.getAllMetadata(MDs);
137 for (auto MD : MDs)
138 F->addMetadata(MD.first, *MapMetadata(MD.second, VMap));
139 continue;
140 }
141
142 if (!ShouldCloneDefinition(&I)) {
143
145
146 F->setPersonalityFn(nullptr);
147 continue;
148 }
149
151 for (const Argument &J : I.args()) {
152 DestI->setName(J.getName());
153 VMap[&J] = &*DestI++;
154 }
155
158 Returns);
159
160 if (I.hasPersonalityFn())
161 F->setPersonalityFn(MapValue(I.getPersonalityFn(), VMap));
162
164 }
165
166
168
169 if (!ShouldCloneDefinition(&I))
170 continue;
172 if (const Constant *C = I.getAliasee())
174 }
175
180 }
181
182
183 for (const NamedMDNode &NMD : M.named_metadata()) {
184 NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
185 for (const MDNode *N : NMD.operands())
187 }
188
189
190
191
192
195
197 G.getAllMetadata(MDs);
198 for (auto MD : MDs)
200
201 if (G.isDeclaration())
202 continue;
203
204 if (!ShouldCloneDefinition(&G)) {
205
207 continue;
208 }
209 if (G.hasInitializer())
211
213 }
214
215 return New;
216}
217
218extern "C" {
219
223
224}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void copyComdat(GlobalObject *Dst, const GlobalObject *Src)
Definition CloneModule.cpp:25
Module.h This file contains the declarations for the Module class.
This class represents an incoming formal argument to a Function.
LLVM_ABI StringRef getName() const
void setSelectionKind(SelectionKind Val)
SelectionKind getSelectionKind() const
This is an important base class in LLVM.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
LLVM_ABI void setAliasee(Constant *Aliasee)
These methods retrieve and set alias target.
static LLVM_ABI GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
static LLVM_ABI GlobalIFunc * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Resolver, Module *Parent)
If a parent module is specified, the ifunc is automatically inserted into the end of the specified mo...
void setResolver(Constant *Resolver)
These methods retrieve and set ifunc resolver function.
LLVM_ABI void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
void setLinkage(LinkageTypes LT)
@ ExternalLinkage
Externally visible function.
LLVM_ABI void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
LLVM_ABI void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
A Module instance is used to store all the information related to an LLVM module.
LLVM_ABI void addOperand(MDNode *M)
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
An efficient, type-erasing, non-owning reference to a callable.
LLVMModuleRef LLVMCloneModule(LLVMModuleRef M)
Return an exact copy of the specified module.
Definition CloneModule.cpp:220
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
Attribute unwrap(LLVMAttributeRef Attr)
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
LLVM_ABI void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, CloneFunctionChangeType Changes, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=nullptr, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Clone OldFunc into NewFunc, transforming the old arguments into references to VMap values.
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataPredicate *IdentityMD=nullptr)
Look up or compute a value in the value map.
LLVMAttributeRef wrap(Attribute Attr)
LLVM_ABI std::unique_ptr< Module > CloneModule(const Module &M)
Return an exact copy of the specified module.
Definition CloneModule.cpp:40
Metadata * MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataPredicate *IdentityMD=nullptr)
Lookup or compute a mapping for a piece of metadata.