1 //===- lib/Linker/IRMover.cpp ---------------------------------------------===//
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 #include "llvm/Linker/IRMover.h"
11 #include "LinkDiagnosticInfo.h"
12 #include "llvm/ADT/SetVector.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DebugInfo.h"
17 #include "llvm/IR/DiagnosticPrinter.h"
18 #include "llvm/IR/GVMaterializer.h"
19 #include "llvm/IR/TypeFinder.h"
20 #include "llvm/Transforms/Utils/Cloning.h"
21 using namespace llvm;
22
23 //===----------------------------------------------------------------------===//
24 // TypeMap implementation.
25 //===----------------------------------------------------------------------===//
26
27 namespace {
28 class TypeMapTy : public ValueMapTypeRemapper {
29 /// This is a mapping from a source type to a destination type to use.
30 DenseMap<Type *, Type *> MappedTypes;
31
32 /// When checking to see if two subgraphs are isomorphic, we speculatively
33 /// add types to MappedTypes, but keep track of them here in case we need to
34 /// roll back.
35 SmallVector<Type *, 16> SpeculativeTypes;
36
37 SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes;
38
39 /// This is a list of non-opaque structs in the source module that are mapped
40 /// to an opaque struct in the destination module.
41 SmallVector<StructType *, 16> SrcDefinitionsToResolve;
42
43 /// This is the set of opaque types in the destination modules who are
44 /// getting a body from the source module.
45 SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes;
46
47 public:
TypeMapTy(IRMover::IdentifiedStructTypeSet & DstStructTypesSet)48 TypeMapTy(IRMover::IdentifiedStructTypeSet &DstStructTypesSet)
49 : DstStructTypesSet(DstStructTypesSet) {}
50
51 IRMover::IdentifiedStructTypeSet &DstStructTypesSet;
52 /// Indicate that the specified type in the destination module is conceptually
53 /// equivalent to the specified type in the source module.
54 void addTypeMapping(Type *DstTy, Type *SrcTy);
55
56 /// Produce a body for an opaque type in the dest module from a type
57 /// definition in the source module.
58 void linkDefinedTypeBodies();
59
60 /// Return the mapped type to use for the specified input type from the
61 /// source module.
62 Type *get(Type *SrcTy);
63 Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
64
65 void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
66
get(FunctionType * T)67 FunctionType *get(FunctionType *T) {
68 return cast<FunctionType>(get((Type *)T));
69 }
70
71 private:
remapType(Type * SrcTy)72 Type *remapType(Type *SrcTy) override { return get(SrcTy); }
73
74 bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
75 };
76 }
77
addTypeMapping(Type * DstTy,Type * SrcTy)78 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
79 assert(SpeculativeTypes.empty());
80 assert(SpeculativeDstOpaqueTypes.empty());
81
82 // Check to see if these types are recursively isomorphic and establish a
83 // mapping between them if so.
84 if (!areTypesIsomorphic(DstTy, SrcTy)) {
85 // Oops, they aren't isomorphic. Just discard this request by rolling out
86 // any speculative mappings we've established.
87 for (Type *Ty : SpeculativeTypes)
88 MappedTypes.erase(Ty);
89
90 SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
91 SpeculativeDstOpaqueTypes.size());
92 for (StructType *Ty : SpeculativeDstOpaqueTypes)
93 DstResolvedOpaqueTypes.erase(Ty);
94 } else {
95 for (Type *Ty : SpeculativeTypes)
96 if (auto *STy = dyn_cast<StructType>(Ty))
97 if (STy->hasName())
98 STy->setName("");
99 }
100 SpeculativeTypes.clear();
101 SpeculativeDstOpaqueTypes.clear();
102 }
103
104 /// Recursively walk this pair of types, returning true if they are isomorphic,
105 /// false if they are not.
areTypesIsomorphic(Type * DstTy,Type * SrcTy)106 bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
107 // Two types with differing kinds are clearly not isomorphic.
108 if (DstTy->getTypeID() != SrcTy->getTypeID())
109 return false;
110
111 // If we have an entry in the MappedTypes table, then we have our answer.
112 Type *&Entry = MappedTypes[SrcTy];
113 if (Entry)
114 return Entry == DstTy;
115
116 // Two identical types are clearly isomorphic. Remember this
117 // non-speculatively.
118 if (DstTy == SrcTy) {
119 Entry = DstTy;
120 return true;
121 }
122
123 // Okay, we have two types with identical kinds that we haven't seen before.
124
125 // If this is an opaque struct type, special case it.
126 if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
127 // Mapping an opaque type to any struct, just keep the dest struct.
128 if (SSTy->isOpaque()) {
129 Entry = DstTy;
130 SpeculativeTypes.push_back(SrcTy);
131 return true;
132 }
133
134 // Mapping a non-opaque source type to an opaque dest. If this is the first
135 // type that we're mapping onto this destination type then we succeed. Keep
136 // the dest, but fill it in later. If this is the second (different) type
137 // that we're trying to map onto the same opaque type then we fail.
138 if (cast<StructType>(DstTy)->isOpaque()) {
139 // We can only map one source type onto the opaque destination type.
140 if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
141 return false;
142 SrcDefinitionsToResolve.push_back(SSTy);
143 SpeculativeTypes.push_back(SrcTy);
144 SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
145 Entry = DstTy;
146 return true;
147 }
148 }
149
150 // If the number of subtypes disagree between the two types, then we fail.
151 if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
152 return false;
153
154 // Fail if any of the extra properties (e.g. array size) of the type disagree.
155 if (isa<IntegerType>(DstTy))
156 return false; // bitwidth disagrees.
157 if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
158 if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
159 return false;
160
161 } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
162 if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
163 return false;
164 } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
165 StructType *SSTy = cast<StructType>(SrcTy);
166 if (DSTy->isLiteral() != SSTy->isLiteral() ||
167 DSTy->isPacked() != SSTy->isPacked())
168 return false;
169 } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
170 if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
171 return false;
172 } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
173 if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
174 return false;
175 }
176
177 // Otherwise, we speculate that these two types will line up and recursively
178 // check the subelements.
179 Entry = DstTy;
180 SpeculativeTypes.push_back(SrcTy);
181
182 for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
183 if (!areTypesIsomorphic(DstTy->getContainedType(I),
184 SrcTy->getContainedType(I)))
185 return false;
186
187 // If everything seems to have lined up, then everything is great.
188 return true;
189 }
190
linkDefinedTypeBodies()191 void TypeMapTy::linkDefinedTypeBodies() {
192 SmallVector<Type *, 16> Elements;
193 for (StructType *SrcSTy : SrcDefinitionsToResolve) {
194 StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
195 assert(DstSTy->isOpaque());
196
197 // Map the body of the source type over to a new body for the dest type.
198 Elements.resize(SrcSTy->getNumElements());
199 for (unsigned I = 0, E = Elements.size(); I != E; ++I)
200 Elements[I] = get(SrcSTy->getElementType(I));
201
202 DstSTy->setBody(Elements, SrcSTy->isPacked());
203 DstStructTypesSet.switchToNonOpaque(DstSTy);
204 }
205 SrcDefinitionsToResolve.clear();
206 DstResolvedOpaqueTypes.clear();
207 }
208
finishType(StructType * DTy,StructType * STy,ArrayRef<Type * > ETypes)209 void TypeMapTy::finishType(StructType *DTy, StructType *STy,
210 ArrayRef<Type *> ETypes) {
211 DTy->setBody(ETypes, STy->isPacked());
212
213 // Steal STy's name.
214 if (STy->hasName()) {
215 SmallString<16> TmpName = STy->getName();
216 STy->setName("");
217 DTy->setName(TmpName);
218 }
219
220 DstStructTypesSet.addNonOpaque(DTy);
221 }
222
get(Type * Ty)223 Type *TypeMapTy::get(Type *Ty) {
224 SmallPtrSet<StructType *, 8> Visited;
225 return get(Ty, Visited);
226 }
227
get(Type * Ty,SmallPtrSet<StructType *,8> & Visited)228 Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
229 // If we already have an entry for this type, return it.
230 Type **Entry = &MappedTypes[Ty];
231 if (*Entry)
232 return *Entry;
233
234 // These are types that LLVM itself will unique.
235 bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
236
237 #ifndef NDEBUG
238 if (!IsUniqued) {
239 for (auto &Pair : MappedTypes) {
240 assert(!(Pair.first != Ty && Pair.second == Ty) &&
241 "mapping to a source type");
242 }
243 }
244 #endif
245
246 if (!IsUniqued && !Visited.insert(cast<StructType>(Ty)).second) {
247 StructType *DTy = StructType::create(Ty->getContext());
248 return *Entry = DTy;
249 }
250
251 // If this is not a recursive type, then just map all of the elements and
252 // then rebuild the type from inside out.
253 SmallVector<Type *, 4> ElementTypes;
254
255 // If there are no element types to map, then the type is itself. This is
256 // true for the anonymous {} struct, things like 'float', integers, etc.
257 if (Ty->getNumContainedTypes() == 0 && IsUniqued)
258 return *Entry = Ty;
259
260 // Remap all of the elements, keeping track of whether any of them change.
261 bool AnyChange = false;
262 ElementTypes.resize(Ty->getNumContainedTypes());
263 for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
264 ElementTypes[I] = get(Ty->getContainedType(I), Visited);
265 AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
266 }
267
268 // If we found our type while recursively processing stuff, just use it.
269 Entry = &MappedTypes[Ty];
270 if (*Entry) {
271 if (auto *DTy = dyn_cast<StructType>(*Entry)) {
272 if (DTy->isOpaque()) {
273 auto *STy = cast<StructType>(Ty);
274 finishType(DTy, STy, ElementTypes);
275 }
276 }
277 return *Entry;
278 }
279
280 // If all of the element types mapped directly over and the type is not
281 // a nomed struct, then the type is usable as-is.
282 if (!AnyChange && IsUniqued)
283 return *Entry = Ty;
284
285 // Otherwise, rebuild a modified type.
286 switch (Ty->getTypeID()) {
287 default:
288 llvm_unreachable("unknown derived type to remap");
289 case Type::ArrayTyID:
290 return *Entry = ArrayType::get(ElementTypes[0],
291 cast<ArrayType>(Ty)->getNumElements());
292 case Type::VectorTyID:
293 return *Entry = VectorType::get(ElementTypes[0],
294 cast<VectorType>(Ty)->getNumElements());
295 case Type::PointerTyID:
296 return *Entry = PointerType::get(ElementTypes[0],
297 cast<PointerType>(Ty)->getAddressSpace());
298 case Type::FunctionTyID:
299 return *Entry = FunctionType::get(ElementTypes[0],
300 makeArrayRef(ElementTypes).slice(1),
301 cast<FunctionType>(Ty)->isVarArg());
302 case Type::StructTyID: {
303 auto *STy = cast<StructType>(Ty);
304 bool IsPacked = STy->isPacked();
305 if (IsUniqued)
306 return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
307
308 // If the type is opaque, we can just use it directly.
309 if (STy->isOpaque()) {
310 DstStructTypesSet.addOpaque(STy);
311 return *Entry = Ty;
312 }
313
314 if (StructType *OldT =
315 DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
316 STy->setName("");
317 return *Entry = OldT;
318 }
319
320 if (!AnyChange) {
321 DstStructTypesSet.addNonOpaque(STy);
322 return *Entry = Ty;
323 }
324
325 StructType *DTy = StructType::create(Ty->getContext());
326 finishType(DTy, STy, ElementTypes);
327 return *Entry = DTy;
328 }
329 }
330 }
331
LinkDiagnosticInfo(DiagnosticSeverity Severity,const Twine & Msg)332 LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
333 const Twine &Msg)
334 : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
print(DiagnosticPrinter & DP) const335 void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
336
337 //===----------------------------------------------------------------------===//
338 // IRLinker implementation.
339 //===----------------------------------------------------------------------===//
340
341 namespace {
342 class IRLinker;
343
344 /// Creates prototypes for functions that are lazily linked on the fly. This
345 /// speeds up linking for modules with many/ lazily linked functions of which
346 /// few get used.
347 class GlobalValueMaterializer final : public ValueMaterializer {
348 IRLinker *TheIRLinker;
349
350 public:
GlobalValueMaterializer(IRLinker * TheIRLinker)351 GlobalValueMaterializer(IRLinker *TheIRLinker) : TheIRLinker(TheIRLinker) {}
352 Value *materializeDeclFor(Value *V) override;
353 void materializeInitFor(GlobalValue *New, GlobalValue *Old) override;
354 Metadata *mapTemporaryMetadata(Metadata *MD) override;
355 void replaceTemporaryMetadata(const Metadata *OrigMD,
356 Metadata *NewMD) override;
357 bool isMetadataNeeded(Metadata *MD) override;
358 };
359
360 class LocalValueMaterializer final : public ValueMaterializer {
361 IRLinker *TheIRLinker;
362
363 public:
LocalValueMaterializer(IRLinker * TheIRLinker)364 LocalValueMaterializer(IRLinker *TheIRLinker) : TheIRLinker(TheIRLinker) {}
365 Value *materializeDeclFor(Value *V) override;
366 void materializeInitFor(GlobalValue *New, GlobalValue *Old) override;
367 Metadata *mapTemporaryMetadata(Metadata *MD) override;
368 void replaceTemporaryMetadata(const Metadata *OrigMD,
369 Metadata *NewMD) override;
370 bool isMetadataNeeded(Metadata *MD) override;
371 };
372
373 /// This is responsible for keeping track of the state used for moving data
374 /// from SrcM to DstM.
375 class IRLinker {
376 Module &DstM;
377 Module &SrcM;
378
379 std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor;
380
381 TypeMapTy TypeMap;
382 GlobalValueMaterializer GValMaterializer;
383 LocalValueMaterializer LValMaterializer;
384
385 /// Mapping of values from what they used to be in Src, to what they are now
386 /// in DstM. ValueToValueMapTy is a ValueMap, which involves some overhead
387 /// due to the use of Value handles which the Linker doesn't actually need,
388 /// but this allows us to reuse the ValueMapper code.
389 ValueToValueMapTy ValueMap;
390 ValueToValueMapTy AliasValueMap;
391
392 DenseSet<GlobalValue *> ValuesToLink;
393 std::vector<GlobalValue *> Worklist;
394
maybeAdd(GlobalValue * GV)395 void maybeAdd(GlobalValue *GV) {
396 if (ValuesToLink.insert(GV).second)
397 Worklist.push_back(GV);
398 }
399
400 /// Set to true when all global value body linking is complete (including
401 /// lazy linking). Used to prevent metadata linking from creating new
402 /// references.
403 bool DoneLinkingBodies = false;
404
405 bool HasError = false;
406
407 /// Flag indicating that we are just linking metadata (after function
408 /// importing).
409 bool IsMetadataLinkingPostpass;
410
411 /// Flags to pass to value mapper invocations.
412 RemapFlags ValueMapperFlags = RF_MoveDistinctMDs;
413
414 /// Association between metadata values created during bitcode parsing and
415 /// the value id. Used to correlate temporary metadata created during
416 /// function importing with the final metadata parsed during the subsequent
417 /// metadata linking postpass.
418 DenseMap<const Metadata *, unsigned> MDValueToValIDMap;
419
420 /// Association between metadata value id and temporary metadata that
421 /// remains unmapped after function importing. Saved during function
422 /// importing and consumed during the metadata linking postpass.
423 DenseMap<unsigned, MDNode *> *ValIDToTempMDMap;
424
425 /// Set of subprogram metadata that does not need to be linked into the
426 /// destination module, because the functions were not imported directly
427 /// or via an inlined body in an imported function.
428 SmallPtrSet<const Metadata *, 16> UnneededSubprograms;
429
430 /// Handles cloning of a global values from the source module into
431 /// the destination module, including setting the attributes and visibility.
432 GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition);
433
434 /// Helper method for setting a message and returning an error code.
emitError(const Twine & Message)435 bool emitError(const Twine &Message) {
436 SrcM.getContext().diagnose(LinkDiagnosticInfo(DS_Error, Message));
437 HasError = true;
438 return true;
439 }
440
emitWarning(const Twine & Message)441 void emitWarning(const Twine &Message) {
442 SrcM.getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));
443 }
444
445 /// Check whether we should be linking metadata from the source module.
shouldLinkMetadata()446 bool shouldLinkMetadata() {
447 // ValIDToTempMDMap will be non-null when we are importing or otherwise want
448 // to link metadata lazily, and then when linking the metadata.
449 // We only want to return true for the former case.
450 return ValIDToTempMDMap == nullptr || IsMetadataLinkingPostpass;
451 }
452
453 /// Given a global in the source module, return the global in the
454 /// destination module that is being linked to, if any.
getLinkedToGlobal(const GlobalValue * SrcGV)455 GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
456 // If the source has no name it can't link. If it has local linkage,
457 // there is no name match-up going on.
458 if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
459 return nullptr;
460
461 // Otherwise see if we have a match in the destination module's symtab.
462 GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());
463 if (!DGV)
464 return nullptr;
465
466 // If we found a global with the same name in the dest module, but it has
467 // internal linkage, we are really not doing any linkage here.
468 if (DGV->hasLocalLinkage())
469 return nullptr;
470
471 // Otherwise, we do in fact link to the destination global.
472 return DGV;
473 }
474
475 void computeTypeMapping();
476
477 Constant *linkAppendingVarProto(GlobalVariable *DstGV,
478 const GlobalVariable *SrcGV);
479
480 bool shouldLink(GlobalValue *DGV, GlobalValue &SGV);
481 Constant *linkGlobalValueProto(GlobalValue *GV, bool ForAlias);
482
483 bool linkModuleFlagsMetadata();
484
485 void linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src);
486 bool linkFunctionBody(Function &Dst, Function &Src);
487 void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src);
488 bool linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src);
489
490 /// Functions that take care of cloning a specific global value type
491 /// into the destination module.
492 GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar);
493 Function *copyFunctionProto(const Function *SF);
494 GlobalValue *copyGlobalAliasProto(const GlobalAlias *SGA);
495
496 void linkNamedMDNodes();
497
498 /// Populate the UnneededSubprograms set with the DISubprogram metadata
499 /// from the source module that we don't need to link into the dest module,
500 /// because the functions were not imported directly or via an inlined body
501 /// in an imported function.
502 void findNeededSubprograms(ValueToValueMapTy &ValueMap);
503
504 /// The value mapper leaves nulls in the list of subprograms for any
505 /// in the UnneededSubprograms map. Strip those out after metadata linking.
506 void stripNullSubprograms();
507
508 public:
IRLinker(Module & DstM,IRMover::IdentifiedStructTypeSet & Set,Module & SrcM,ArrayRef<GlobalValue * > ValuesToLink,std::function<void (GlobalValue &,IRMover::ValueAdder)> AddLazyFor,DenseMap<unsigned,MDNode * > * ValIDToTempMDMap=nullptr,bool IsMetadataLinkingPostpass=false)509 IRLinker(Module &DstM, IRMover::IdentifiedStructTypeSet &Set, Module &SrcM,
510 ArrayRef<GlobalValue *> ValuesToLink,
511 std::function<void(GlobalValue &, IRMover::ValueAdder)> AddLazyFor,
512 DenseMap<unsigned, MDNode *> *ValIDToTempMDMap = nullptr,
513 bool IsMetadataLinkingPostpass = false)
514 : DstM(DstM), SrcM(SrcM), AddLazyFor(AddLazyFor), TypeMap(Set),
515 GValMaterializer(this), LValMaterializer(this),
516 IsMetadataLinkingPostpass(IsMetadataLinkingPostpass),
517 ValIDToTempMDMap(ValIDToTempMDMap) {
518 for (GlobalValue *GV : ValuesToLink)
519 maybeAdd(GV);
520
521 // If appropriate, tell the value mapper that it can expect to see
522 // temporary metadata.
523 if (!shouldLinkMetadata())
524 ValueMapperFlags = ValueMapperFlags | RF_HaveUnmaterializedMetadata;
525 }
526
527 bool run();
528 Value *materializeDeclFor(Value *V, bool ForAlias);
529 void materializeInitFor(GlobalValue *New, GlobalValue *Old, bool ForAlias);
530
531 /// Save the mapping between the given temporary metadata and its metadata
532 /// value id. Used to support metadata linking as a postpass for function
533 /// importing.
534 Metadata *mapTemporaryMetadata(Metadata *MD);
535
536 /// Replace any temporary metadata saved for the source metadata's id with
537 /// the new non-temporary metadata. Used when metadata linking as a postpass
538 /// for function importing.
539 void replaceTemporaryMetadata(const Metadata *OrigMD, Metadata *NewMD);
540
541 /// Indicates whether we need to map the given metadata into the destination
542 /// module. Used to prevent linking of metadata only needed by functions not
543 /// linked into the dest module.
544 bool isMetadataNeeded(Metadata *MD);
545 };
546 }
547
548 /// The LLVM SymbolTable class autorenames globals that conflict in the symbol
549 /// table. This is good for all clients except for us. Go through the trouble
550 /// to force this back.
forceRenaming(GlobalValue * GV,StringRef Name)551 static void forceRenaming(GlobalValue *GV, StringRef Name) {
552 // If the global doesn't force its name or if it already has the right name,
553 // there is nothing for us to do.
554 if (GV->hasLocalLinkage() || GV->getName() == Name)
555 return;
556
557 Module *M = GV->getParent();
558
559 // If there is a conflict, rename the conflict.
560 if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
561 GV->takeName(ConflictGV);
562 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
563 assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
564 } else {
565 GV->setName(Name); // Force the name back
566 }
567 }
568
materializeDeclFor(Value * V)569 Value *GlobalValueMaterializer::materializeDeclFor(Value *V) {
570 return TheIRLinker->materializeDeclFor(V, false);
571 }
572
materializeInitFor(GlobalValue * New,GlobalValue * Old)573 void GlobalValueMaterializer::materializeInitFor(GlobalValue *New,
574 GlobalValue *Old) {
575 TheIRLinker->materializeInitFor(New, Old, false);
576 }
577
mapTemporaryMetadata(Metadata * MD)578 Metadata *GlobalValueMaterializer::mapTemporaryMetadata(Metadata *MD) {
579 return TheIRLinker->mapTemporaryMetadata(MD);
580 }
581
replaceTemporaryMetadata(const Metadata * OrigMD,Metadata * NewMD)582 void GlobalValueMaterializer::replaceTemporaryMetadata(const Metadata *OrigMD,
583 Metadata *NewMD) {
584 TheIRLinker->replaceTemporaryMetadata(OrigMD, NewMD);
585 }
586
isMetadataNeeded(Metadata * MD)587 bool GlobalValueMaterializer::isMetadataNeeded(Metadata *MD) {
588 return TheIRLinker->isMetadataNeeded(MD);
589 }
590
materializeDeclFor(Value * V)591 Value *LocalValueMaterializer::materializeDeclFor(Value *V) {
592 return TheIRLinker->materializeDeclFor(V, true);
593 }
594
materializeInitFor(GlobalValue * New,GlobalValue * Old)595 void LocalValueMaterializer::materializeInitFor(GlobalValue *New,
596 GlobalValue *Old) {
597 TheIRLinker->materializeInitFor(New, Old, true);
598 }
599
mapTemporaryMetadata(Metadata * MD)600 Metadata *LocalValueMaterializer::mapTemporaryMetadata(Metadata *MD) {
601 return TheIRLinker->mapTemporaryMetadata(MD);
602 }
603
replaceTemporaryMetadata(const Metadata * OrigMD,Metadata * NewMD)604 void LocalValueMaterializer::replaceTemporaryMetadata(const Metadata *OrigMD,
605 Metadata *NewMD) {
606 TheIRLinker->replaceTemporaryMetadata(OrigMD, NewMD);
607 }
608
isMetadataNeeded(Metadata * MD)609 bool LocalValueMaterializer::isMetadataNeeded(Metadata *MD) {
610 return TheIRLinker->isMetadataNeeded(MD);
611 }
612
materializeDeclFor(Value * V,bool ForAlias)613 Value *IRLinker::materializeDeclFor(Value *V, bool ForAlias) {
614 auto *SGV = dyn_cast<GlobalValue>(V);
615 if (!SGV)
616 return nullptr;
617
618 return linkGlobalValueProto(SGV, ForAlias);
619 }
620
materializeInitFor(GlobalValue * New,GlobalValue * Old,bool ForAlias)621 void IRLinker::materializeInitFor(GlobalValue *New, GlobalValue *Old,
622 bool ForAlias) {
623 // If we already created the body, just return.
624 if (auto *F = dyn_cast<Function>(New)) {
625 if (!F->isDeclaration())
626 return;
627 } else if (auto *V = dyn_cast<GlobalVariable>(New)) {
628 if (V->hasInitializer())
629 return;
630 } else {
631 auto *A = cast<GlobalAlias>(New);
632 if (A->getAliasee())
633 return;
634 }
635
636 if (ForAlias || shouldLink(New, *Old))
637 linkGlobalValueBody(*New, *Old);
638 }
639
mapTemporaryMetadata(Metadata * MD)640 Metadata *IRLinker::mapTemporaryMetadata(Metadata *MD) {
641 if (!ValIDToTempMDMap)
642 return nullptr;
643 // If this temporary metadata has a value id recorded during function
644 // parsing, record that in the ValIDToTempMDMap if one was provided.
645 if (MDValueToValIDMap.count(MD)) {
646 unsigned Idx = MDValueToValIDMap[MD];
647 // Check if we created a temp MD when importing a different function from
648 // this module. If so, reuse it the same temporary metadata, otherwise
649 // add this temporary metadata to the map.
650 if (!ValIDToTempMDMap->count(Idx)) {
651 MDNode *Node = cast<MDNode>(MD);
652 assert(Node->isTemporary());
653 (*ValIDToTempMDMap)[Idx] = Node;
654 }
655 return (*ValIDToTempMDMap)[Idx];
656 }
657 return nullptr;
658 }
659
replaceTemporaryMetadata(const Metadata * OrigMD,Metadata * NewMD)660 void IRLinker::replaceTemporaryMetadata(const Metadata *OrigMD,
661 Metadata *NewMD) {
662 if (!ValIDToTempMDMap)
663 return;
664 #ifndef NDEBUG
665 auto *N = dyn_cast_or_null<MDNode>(NewMD);
666 assert(!N || !N->isTemporary());
667 #endif
668 // If a mapping between metadata value ids and temporary metadata
669 // created during function importing was provided, and the source
670 // metadata has a value id recorded during metadata parsing, replace
671 // the temporary metadata with the final mapped metadata now.
672 if (MDValueToValIDMap.count(OrigMD)) {
673 unsigned Idx = MDValueToValIDMap[OrigMD];
674 // Nothing to do if we didn't need to create a temporary metadata during
675 // function importing.
676 if (!ValIDToTempMDMap->count(Idx))
677 return;
678 MDNode *TempMD = (*ValIDToTempMDMap)[Idx];
679 TempMD->replaceAllUsesWith(NewMD);
680 MDNode::deleteTemporary(TempMD);
681 ValIDToTempMDMap->erase(Idx);
682 }
683 }
684
isMetadataNeeded(Metadata * MD)685 bool IRLinker::isMetadataNeeded(Metadata *MD) {
686 // Currently only DISubprogram metadata is marked as being unneeded.
687 if (UnneededSubprograms.empty())
688 return true;
689 MDNode *Node = dyn_cast<MDNode>(MD);
690 if (!Node)
691 return true;
692 DISubprogram *SP = getDISubprogram(Node);
693 if (!SP)
694 return true;
695 return !UnneededSubprograms.count(SP);
696 }
697
698 /// Loop through the global variables in the src module and merge them into the
699 /// dest module.
copyGlobalVariableProto(const GlobalVariable * SGVar)700 GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
701 // No linking to be performed or linking from the source: simply create an
702 // identical version of the symbol over in the dest module... the
703 // initializer will be filled in later by LinkGlobalInits.
704 GlobalVariable *NewDGV =
705 new GlobalVariable(DstM, TypeMap.get(SGVar->getType()->getElementType()),
706 SGVar->isConstant(), GlobalValue::ExternalLinkage,
707 /*init*/ nullptr, SGVar->getName(),
708 /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
709 SGVar->getType()->getAddressSpace());
710 NewDGV->setAlignment(SGVar->getAlignment());
711 return NewDGV;
712 }
713
714 /// Link the function in the source module into the destination module if
715 /// needed, setting up mapping information.
copyFunctionProto(const Function * SF)716 Function *IRLinker::copyFunctionProto(const Function *SF) {
717 // If there is no linkage to be performed or we are linking from the source,
718 // bring SF over.
719 return Function::Create(TypeMap.get(SF->getFunctionType()),
720 GlobalValue::ExternalLinkage, SF->getName(), &DstM);
721 }
722
723 /// Set up prototypes for any aliases that come over from the source module.
copyGlobalAliasProto(const GlobalAlias * SGA)724 GlobalValue *IRLinker::copyGlobalAliasProto(const GlobalAlias *SGA) {
725 // If there is no linkage to be performed or we're linking from the source,
726 // bring over SGA.
727 auto *Ty = TypeMap.get(SGA->getValueType());
728 return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
729 GlobalValue::ExternalLinkage, SGA->getName(),
730 &DstM);
731 }
732
copyGlobalValueProto(const GlobalValue * SGV,bool ForDefinition)733 GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
734 bool ForDefinition) {
735 GlobalValue *NewGV;
736 if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {
737 NewGV = copyGlobalVariableProto(SGVar);
738 } else if (auto *SF = dyn_cast<Function>(SGV)) {
739 NewGV = copyFunctionProto(SF);
740 } else {
741 if (ForDefinition)
742 NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV));
743 else
744 NewGV = new GlobalVariable(
745 DstM, TypeMap.get(SGV->getType()->getElementType()),
746 /*isConstant*/ false, GlobalValue::ExternalLinkage,
747 /*init*/ nullptr, SGV->getName(),
748 /*insertbefore*/ nullptr, SGV->getThreadLocalMode(),
749 SGV->getType()->getAddressSpace());
750 }
751
752 if (ForDefinition)
753 NewGV->setLinkage(SGV->getLinkage());
754 else if (SGV->hasExternalWeakLinkage() || SGV->hasWeakLinkage() ||
755 SGV->hasLinkOnceLinkage())
756 NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
757
758 NewGV->copyAttributesFrom(SGV);
759 return NewGV;
760 }
761
762 /// Loop over all of the linked values to compute type mappings. For example,
763 /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
764 /// types 'Foo' but one got renamed when the module was loaded into the same
765 /// LLVMContext.
computeTypeMapping()766 void IRLinker::computeTypeMapping() {
767 for (GlobalValue &SGV : SrcM.globals()) {
768 GlobalValue *DGV = getLinkedToGlobal(&SGV);
769 if (!DGV)
770 continue;
771
772 if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
773 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
774 continue;
775 }
776
777 // Unify the element type of appending arrays.
778 ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
779 ArrayType *SAT = cast<ArrayType>(SGV.getType()->getElementType());
780 TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
781 }
782
783 for (GlobalValue &SGV : SrcM)
784 if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
785 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
786
787 for (GlobalValue &SGV : SrcM.aliases())
788 if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
789 TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
790
791 // Incorporate types by name, scanning all the types in the source module.
792 // At this point, the destination module may have a type "%foo = { i32 }" for
793 // example. When the source module got loaded into the same LLVMContext, if
794 // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
795 std::vector<StructType *> Types = SrcM.getIdentifiedStructTypes();
796 for (StructType *ST : Types) {
797 if (!ST->hasName())
798 continue;
799
800 // Check to see if there is a dot in the name followed by a digit.
801 size_t DotPos = ST->getName().rfind('.');
802 if (DotPos == 0 || DotPos == StringRef::npos ||
803 ST->getName().back() == '.' ||
804 !isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1])))
805 continue;
806
807 // Check to see if the destination module has a struct with the prefix name.
808 StructType *DST = DstM.getTypeByName(ST->getName().substr(0, DotPos));
809 if (!DST)
810 continue;
811
812 // Don't use it if this actually came from the source module. They're in
813 // the same LLVMContext after all. Also don't use it unless the type is
814 // actually used in the destination module. This can happen in situations
815 // like this:
816 //
817 // Module A Module B
818 // -------- --------
819 // %Z = type { %A } %B = type { %C.1 }
820 // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* }
821 // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] }
822 // %C = type { i8* } %B.3 = type { %C.1 }
823 //
824 // When we link Module B with Module A, the '%B' in Module B is
825 // used. However, that would then use '%C.1'. But when we process '%C.1',
826 // we prefer to take the '%C' version. So we are then left with both
827 // '%C.1' and '%C' being used for the same types. This leads to some
828 // variables using one type and some using the other.
829 if (TypeMap.DstStructTypesSet.hasType(DST))
830 TypeMap.addTypeMapping(DST, ST);
831 }
832
833 // Now that we have discovered all of the type equivalences, get a body for
834 // any 'opaque' types in the dest module that are now resolved.
835 TypeMap.linkDefinedTypeBodies();
836 }
837
getArrayElements(const Constant * C,SmallVectorImpl<Constant * > & Dest)838 static void getArrayElements(const Constant *C,
839 SmallVectorImpl<Constant *> &Dest) {
840 unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
841
842 for (unsigned i = 0; i != NumElements; ++i)
843 Dest.push_back(C->getAggregateElement(i));
844 }
845
846 /// If there were any appending global variables, link them together now.
847 /// Return true on error.
linkAppendingVarProto(GlobalVariable * DstGV,const GlobalVariable * SrcGV)848 Constant *IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
849 const GlobalVariable *SrcGV) {
850 Type *EltTy = cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()))
851 ->getElementType();
852
853 StringRef Name = SrcGV->getName();
854 bool IsNewStructor = false;
855 bool IsOldStructor = false;
856 if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") {
857 if (cast<StructType>(EltTy)->getNumElements() == 3)
858 IsNewStructor = true;
859 else
860 IsOldStructor = true;
861 }
862
863 PointerType *VoidPtrTy = Type::getInt8Ty(SrcGV->getContext())->getPointerTo();
864 if (IsOldStructor) {
865 auto &ST = *cast<StructType>(EltTy);
866 Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
867 EltTy = StructType::get(SrcGV->getContext(), Tys, false);
868 }
869
870 if (DstGV) {
871 ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
872
873 if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage()) {
874 emitError(
875 "Linking globals named '" + SrcGV->getName() +
876 "': can only link appending global with another appending global!");
877 return nullptr;
878 }
879
880 // Check to see that they two arrays agree on type.
881 if (EltTy != DstTy->getElementType()) {
882 emitError("Appending variables with different element types!");
883 return nullptr;
884 }
885 if (DstGV->isConstant() != SrcGV->isConstant()) {
886 emitError("Appending variables linked with different const'ness!");
887 return nullptr;
888 }
889
890 if (DstGV->getAlignment() != SrcGV->getAlignment()) {
891 emitError(
892 "Appending variables with different alignment need to be linked!");
893 return nullptr;
894 }
895
896 if (DstGV->getVisibility() != SrcGV->getVisibility()) {
897 emitError(
898 "Appending variables with different visibility need to be linked!");
899 return nullptr;
900 }
901
902 if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr()) {
903 emitError(
904 "Appending variables with different unnamed_addr need to be linked!");
905 return nullptr;
906 }
907
908 if (StringRef(DstGV->getSection()) != SrcGV->getSection()) {
909 emitError(
910 "Appending variables with different section name need to be linked!");
911 return nullptr;
912 }
913 }
914
915 SmallVector<Constant *, 16> DstElements;
916 if (DstGV)
917 getArrayElements(DstGV->getInitializer(), DstElements);
918
919 SmallVector<Constant *, 16> SrcElements;
920 getArrayElements(SrcGV->getInitializer(), SrcElements);
921
922 if (IsNewStructor)
923 SrcElements.erase(
924 std::remove_if(SrcElements.begin(), SrcElements.end(),
925 [this](Constant *E) {
926 auto *Key = dyn_cast<GlobalValue>(
927 E->getAggregateElement(2)->stripPointerCasts());
928 if (!Key)
929 return false;
930 GlobalValue *DGV = getLinkedToGlobal(Key);
931 return !shouldLink(DGV, *Key);
932 }),
933 SrcElements.end());
934 uint64_t NewSize = DstElements.size() + SrcElements.size();
935 ArrayType *NewType = ArrayType::get(EltTy, NewSize);
936
937 // Create the new global variable.
938 GlobalVariable *NG = new GlobalVariable(
939 DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
940 /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
941 SrcGV->getType()->getAddressSpace());
942
943 NG->copyAttributesFrom(SrcGV);
944 forceRenaming(NG, SrcGV->getName());
945
946 Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
947
948 // Stop recursion.
949 ValueMap[SrcGV] = Ret;
950
951 for (auto *V : SrcElements) {
952 Constant *NewV;
953 if (IsOldStructor) {
954 auto *S = cast<ConstantStruct>(V);
955 auto *E1 = MapValue(S->getOperand(0), ValueMap, ValueMapperFlags,
956 &TypeMap, &GValMaterializer);
957 auto *E2 = MapValue(S->getOperand(1), ValueMap, ValueMapperFlags,
958 &TypeMap, &GValMaterializer);
959 Value *Null = Constant::getNullValue(VoidPtrTy);
960 NewV =
961 ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null, nullptr);
962 } else {
963 NewV =
964 MapValue(V, ValueMap, ValueMapperFlags, &TypeMap, &GValMaterializer);
965 }
966 DstElements.push_back(NewV);
967 }
968
969 NG->setInitializer(ConstantArray::get(NewType, DstElements));
970
971 // Replace any uses of the two global variables with uses of the new
972 // global.
973 if (DstGV) {
974 DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
975 DstGV->eraseFromParent();
976 }
977
978 return Ret;
979 }
980
useExistingDest(GlobalValue & SGV,GlobalValue * DGV,bool ShouldLink)981 static bool useExistingDest(GlobalValue &SGV, GlobalValue *DGV,
982 bool ShouldLink) {
983 if (!DGV)
984 return false;
985
986 if (SGV.isDeclaration())
987 return true;
988
989 if (DGV->isDeclarationForLinker() && !SGV.isDeclarationForLinker())
990 return false;
991
992 if (ShouldLink)
993 return false;
994
995 return true;
996 }
997
shouldLink(GlobalValue * DGV,GlobalValue & SGV)998 bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) {
999 // Already imported all the values. Just map to the Dest value
1000 // in case it is referenced in the metadata.
1001 if (IsMetadataLinkingPostpass) {
1002 assert(!ValuesToLink.count(&SGV) &&
1003 "Source value unexpectedly requested for link during metadata link");
1004 return false;
1005 }
1006
1007 if (ValuesToLink.count(&SGV))
1008 return true;
1009
1010 if (SGV.hasLocalLinkage())
1011 return true;
1012
1013 if (DGV && !DGV->isDeclaration())
1014 return false;
1015
1016 if (SGV.hasAvailableExternallyLinkage())
1017 return true;
1018
1019 if (DoneLinkingBodies)
1020 return false;
1021
1022 AddLazyFor(SGV, [this](GlobalValue &GV) { maybeAdd(&GV); });
1023 return ValuesToLink.count(&SGV);
1024 }
1025
linkGlobalValueProto(GlobalValue * SGV,bool ForAlias)1026 Constant *IRLinker::linkGlobalValueProto(GlobalValue *SGV, bool ForAlias) {
1027 GlobalValue *DGV = getLinkedToGlobal(SGV);
1028
1029 bool ShouldLink = shouldLink(DGV, *SGV);
1030
1031 // just missing from map
1032 if (ShouldLink) {
1033 auto I = ValueMap.find(SGV);
1034 if (I != ValueMap.end())
1035 return cast<Constant>(I->second);
1036
1037 I = AliasValueMap.find(SGV);
1038 if (I != AliasValueMap.end())
1039 return cast<Constant>(I->second);
1040 }
1041
1042 DGV = nullptr;
1043 if (ShouldLink || !ForAlias)
1044 DGV = getLinkedToGlobal(SGV);
1045
1046 // Handle the ultra special appending linkage case first.
1047 assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage());
1048 if (SGV->hasAppendingLinkage())
1049 return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),
1050 cast<GlobalVariable>(SGV));
1051
1052 GlobalValue *NewGV;
1053 if (useExistingDest(*SGV, DGV, ShouldLink)) {
1054 NewGV = DGV;
1055 } else {
1056 // If we are done linking global value bodies (i.e. we are performing
1057 // metadata linking), don't link in the global value due to this
1058 // reference, simply map it to null.
1059 if (DoneLinkingBodies)
1060 return nullptr;
1061
1062 NewGV = copyGlobalValueProto(SGV, ShouldLink);
1063 if (!ForAlias)
1064 forceRenaming(NewGV, SGV->getName());
1065 }
1066 if (ShouldLink || ForAlias) {
1067 if (const Comdat *SC = SGV->getComdat()) {
1068 if (auto *GO = dyn_cast<GlobalObject>(NewGV)) {
1069 Comdat *DC = DstM.getOrInsertComdat(SC->getName());
1070 DC->setSelectionKind(SC->getSelectionKind());
1071 GO->setComdat(DC);
1072 }
1073 }
1074 }
1075
1076 if (!ShouldLink && ForAlias)
1077 NewGV->setLinkage(GlobalValue::InternalLinkage);
1078
1079 Constant *C = NewGV;
1080 if (DGV)
1081 C = ConstantExpr::getBitCast(NewGV, TypeMap.get(SGV->getType()));
1082
1083 if (DGV && NewGV != DGV) {
1084 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType()));
1085 DGV->eraseFromParent();
1086 }
1087
1088 return C;
1089 }
1090
1091 /// Update the initializers in the Dest module now that all globals that may be
1092 /// referenced are in Dest.
linkGlobalInit(GlobalVariable & Dst,GlobalVariable & Src)1093 void IRLinker::linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src) {
1094 // Figure out what the initializer looks like in the dest module.
1095 Dst.setInitializer(MapValue(Src.getInitializer(), ValueMap, ValueMapperFlags,
1096 &TypeMap, &GValMaterializer));
1097 }
1098
1099 /// Copy the source function over into the dest function and fix up references
1100 /// to values. At this point we know that Dest is an external function, and
1101 /// that Src is not.
linkFunctionBody(Function & Dst,Function & Src)1102 bool IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
1103 assert(Dst.isDeclaration() && !Src.isDeclaration());
1104
1105 // Materialize if needed.
1106 if (std::error_code EC = Src.materialize())
1107 return emitError(EC.message());
1108
1109 if (!shouldLinkMetadata())
1110 // This is only supported for lazy links. Do after materialization of
1111 // a function and before remapping metadata on instructions below
1112 // in RemapInstruction, as the saved mapping is used to handle
1113 // the temporary metadata hanging off instructions.
1114 SrcM.getMaterializer()->saveMDValueList(MDValueToValIDMap, true);
1115
1116 // Link in the prefix data.
1117 if (Src.hasPrefixData())
1118 Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap, ValueMapperFlags,
1119 &TypeMap, &GValMaterializer));
1120
1121 // Link in the prologue data.
1122 if (Src.hasPrologueData())
1123 Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap,
1124 ValueMapperFlags, &TypeMap,
1125 &GValMaterializer));
1126
1127 // Link in the personality function.
1128 if (Src.hasPersonalityFn())
1129 Dst.setPersonalityFn(MapValue(Src.getPersonalityFn(), ValueMap,
1130 ValueMapperFlags, &TypeMap,
1131 &GValMaterializer));
1132
1133 // Go through and convert function arguments over, remembering the mapping.
1134 Function::arg_iterator DI = Dst.arg_begin();
1135 for (Argument &Arg : Src.args()) {
1136 DI->setName(Arg.getName()); // Copy the name over.
1137
1138 // Add a mapping to our mapping.
1139 ValueMap[&Arg] = &*DI;
1140 ++DI;
1141 }
1142
1143 // Copy over the metadata attachments.
1144 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
1145 Src.getAllMetadata(MDs);
1146 for (const auto &I : MDs)
1147 Dst.setMetadata(I.first, MapMetadata(I.second, ValueMap, ValueMapperFlags,
1148 &TypeMap, &GValMaterializer));
1149
1150 // Splice the body of the source function into the dest function.
1151 Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
1152
1153 // At this point, all of the instructions and values of the function are now
1154 // copied over. The only problem is that they are still referencing values in
1155 // the Source function as operands. Loop through all of the operands of the
1156 // functions and patch them up to point to the local versions.
1157 for (BasicBlock &BB : Dst)
1158 for (Instruction &I : BB)
1159 RemapInstruction(&I, ValueMap, RF_IgnoreMissingEntries | ValueMapperFlags,
1160 &TypeMap, &GValMaterializer);
1161
1162 // There is no need to map the arguments anymore.
1163 for (Argument &Arg : Src.args())
1164 ValueMap.erase(&Arg);
1165
1166 return false;
1167 }
1168
linkAliasBody(GlobalAlias & Dst,GlobalAlias & Src)1169 void IRLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
1170 Constant *Aliasee = Src.getAliasee();
1171 Constant *Val = MapValue(Aliasee, AliasValueMap, ValueMapperFlags, &TypeMap,
1172 &LValMaterializer);
1173 Dst.setAliasee(Val);
1174 }
1175
linkGlobalValueBody(GlobalValue & Dst,GlobalValue & Src)1176 bool IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
1177 if (auto *F = dyn_cast<Function>(&Src))
1178 return linkFunctionBody(cast<Function>(Dst), *F);
1179 if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
1180 linkGlobalInit(cast<GlobalVariable>(Dst), *GVar);
1181 return false;
1182 }
1183 linkAliasBody(cast<GlobalAlias>(Dst), cast<GlobalAlias>(Src));
1184 return false;
1185 }
1186
findNeededSubprograms(ValueToValueMapTy & ValueMap)1187 void IRLinker::findNeededSubprograms(ValueToValueMapTy &ValueMap) {
1188 // Track unneeded nodes to make it simpler to handle the case
1189 // where we are checking if an already-mapped SP is needed.
1190 NamedMDNode *CompileUnits = SrcM.getNamedMetadata("llvm.dbg.cu");
1191 if (!CompileUnits)
1192 return;
1193 for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) {
1194 auto *CU = cast<DICompileUnit>(CompileUnits->getOperand(I));
1195 assert(CU && "Expected valid compile unit");
1196 for (auto *Op : CU->getSubprograms()) {
1197 // Unless we were doing function importing and deferred metadata linking,
1198 // any needed SPs should have been mapped as they would be reached
1199 // from the function linked in (either on the function itself for linked
1200 // function bodies, or from DILocation on inlined instructions).
1201 assert(!(ValueMap.MD()[Op] && IsMetadataLinkingPostpass) &&
1202 "DISubprogram shouldn't be mapped yet");
1203 if (!ValueMap.MD()[Op])
1204 UnneededSubprograms.insert(Op);
1205 }
1206 }
1207 if (!IsMetadataLinkingPostpass)
1208 return;
1209 // In the case of metadata linking as a postpass (e.g. for function
1210 // importing), see which DISubprogram MD from the source has an associated
1211 // temporary metadata node, which means the SP was needed by an imported
1212 // function.
1213 for (auto MDI : MDValueToValIDMap) {
1214 const MDNode *Node = dyn_cast<MDNode>(MDI.first);
1215 if (!Node)
1216 continue;
1217 DISubprogram *SP = getDISubprogram(Node);
1218 if (!SP || !ValIDToTempMDMap->count(MDI.second))
1219 continue;
1220 UnneededSubprograms.erase(SP);
1221 }
1222 }
1223
1224 // Squash null subprograms from compile unit subprogram lists.
stripNullSubprograms()1225 void IRLinker::stripNullSubprograms() {
1226 NamedMDNode *CompileUnits = DstM.getNamedMetadata("llvm.dbg.cu");
1227 if (!CompileUnits)
1228 return;
1229 for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) {
1230 auto *CU = cast<DICompileUnit>(CompileUnits->getOperand(I));
1231 assert(CU && "Expected valid compile unit");
1232
1233 SmallVector<Metadata *, 16> NewSPs;
1234 NewSPs.reserve(CU->getSubprograms().size());
1235 bool FoundNull = false;
1236 for (DISubprogram *SP : CU->getSubprograms()) {
1237 if (!SP) {
1238 FoundNull = true;
1239 continue;
1240 }
1241 NewSPs.push_back(SP);
1242 }
1243 if (FoundNull)
1244 CU->replaceSubprograms(MDTuple::get(CU->getContext(), NewSPs));
1245 }
1246 }
1247
1248 /// Insert all of the named MDNodes in Src into the Dest module.
linkNamedMDNodes()1249 void IRLinker::linkNamedMDNodes() {
1250 findNeededSubprograms(ValueMap);
1251 const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
1252 for (const NamedMDNode &NMD : SrcM.named_metadata()) {
1253 // Don't link module flags here. Do them separately.
1254 if (&NMD == SrcModFlags)
1255 continue;
1256 NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
1257 // Add Src elements into Dest node.
1258 for (const MDNode *op : NMD.operands())
1259 DestNMD->addOperand(MapMetadata(
1260 op, ValueMap, ValueMapperFlags | RF_NullMapMissingGlobalValues,
1261 &TypeMap, &GValMaterializer));
1262 }
1263 stripNullSubprograms();
1264 }
1265
1266 /// Merge the linker flags in Src into the Dest module.
linkModuleFlagsMetadata()1267 bool IRLinker::linkModuleFlagsMetadata() {
1268 // If the source module has no module flags, we are done.
1269 const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
1270 if (!SrcModFlags)
1271 return false;
1272
1273 // If the destination module doesn't have module flags yet, then just copy
1274 // over the source module's flags.
1275 NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
1276 if (DstModFlags->getNumOperands() == 0) {
1277 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1278 DstModFlags->addOperand(SrcModFlags->getOperand(I));
1279
1280 return false;
1281 }
1282
1283 // First build a map of the existing module flags and requirements.
1284 DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
1285 SmallSetVector<MDNode *, 16> Requirements;
1286 for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1287 MDNode *Op = DstModFlags->getOperand(I);
1288 ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
1289 MDString *ID = cast<MDString>(Op->getOperand(1));
1290
1291 if (Behavior->getZExtValue() == Module::Require) {
1292 Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1293 } else {
1294 Flags[ID] = std::make_pair(Op, I);
1295 }
1296 }
1297
1298 // Merge in the flags from the source module, and also collect its set of
1299 // requirements.
1300 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1301 MDNode *SrcOp = SrcModFlags->getOperand(I);
1302 ConstantInt *SrcBehavior =
1303 mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
1304 MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1305 MDNode *DstOp;
1306 unsigned DstIndex;
1307 std::tie(DstOp, DstIndex) = Flags.lookup(ID);
1308 unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1309
1310 // If this is a requirement, add it and continue.
1311 if (SrcBehaviorValue == Module::Require) {
1312 // If the destination module does not already have this requirement, add
1313 // it.
1314 if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1315 DstModFlags->addOperand(SrcOp);
1316 }
1317 continue;
1318 }
1319
1320 // If there is no existing flag with this ID, just add it.
1321 if (!DstOp) {
1322 Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
1323 DstModFlags->addOperand(SrcOp);
1324 continue;
1325 }
1326
1327 // Otherwise, perform a merge.
1328 ConstantInt *DstBehavior =
1329 mdconst::extract<ConstantInt>(DstOp->getOperand(0));
1330 unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1331
1332 // If either flag has override behavior, handle it first.
1333 if (DstBehaviorValue == Module::Override) {
1334 // Diagnose inconsistent flags which both have override behavior.
1335 if (SrcBehaviorValue == Module::Override &&
1336 SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1337 emitError("linking module flags '" + ID->getString() +
1338 "': IDs have conflicting override values");
1339 }
1340 continue;
1341 } else if (SrcBehaviorValue == Module::Override) {
1342 // Update the destination flag to that of the source.
1343 DstModFlags->setOperand(DstIndex, SrcOp);
1344 Flags[ID].first = SrcOp;
1345 continue;
1346 }
1347
1348 // Diagnose inconsistent merge behavior types.
1349 if (SrcBehaviorValue != DstBehaviorValue) {
1350 emitError("linking module flags '" + ID->getString() +
1351 "': IDs have conflicting behaviors");
1352 continue;
1353 }
1354
1355 auto replaceDstValue = [&](MDNode *New) {
1356 Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
1357 MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1358 DstModFlags->setOperand(DstIndex, Flag);
1359 Flags[ID].first = Flag;
1360 };
1361
1362 // Perform the merge for standard behavior types.
1363 switch (SrcBehaviorValue) {
1364 case Module::Require:
1365 case Module::Override:
1366 llvm_unreachable("not possible");
1367 case Module::Error: {
1368 // Emit an error if the values differ.
1369 if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1370 emitError("linking module flags '" + ID->getString() +
1371 "': IDs have conflicting values");
1372 }
1373 continue;
1374 }
1375 case Module::Warning: {
1376 // Emit a warning if the values differ.
1377 if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1378 emitWarning("linking module flags '" + ID->getString() +
1379 "': IDs have conflicting values");
1380 }
1381 continue;
1382 }
1383 case Module::Append: {
1384 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1385 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1386 SmallVector<Metadata *, 8> MDs;
1387 MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
1388 MDs.append(DstValue->op_begin(), DstValue->op_end());
1389 MDs.append(SrcValue->op_begin(), SrcValue->op_end());
1390
1391 replaceDstValue(MDNode::get(DstM.getContext(), MDs));
1392 break;
1393 }
1394 case Module::AppendUnique: {
1395 SmallSetVector<Metadata *, 16> Elts;
1396 MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1397 MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1398 Elts.insert(DstValue->op_begin(), DstValue->op_end());
1399 Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
1400
1401 replaceDstValue(MDNode::get(DstM.getContext(),
1402 makeArrayRef(Elts.begin(), Elts.end())));
1403 break;
1404 }
1405 }
1406 }
1407
1408 // Check all of the requirements.
1409 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1410 MDNode *Requirement = Requirements[I];
1411 MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1412 Metadata *ReqValue = Requirement->getOperand(1);
1413
1414 MDNode *Op = Flags[Flag].first;
1415 if (!Op || Op->getOperand(2) != ReqValue) {
1416 emitError("linking module flags '" + Flag->getString() +
1417 "': does not have the required value");
1418 continue;
1419 }
1420 }
1421
1422 return HasError;
1423 }
1424
1425 // This function returns true if the triples match.
triplesMatch(const Triple & T0,const Triple & T1)1426 static bool triplesMatch(const Triple &T0, const Triple &T1) {
1427 // If vendor is apple, ignore the version number.
1428 if (T0.getVendor() == Triple::Apple)
1429 return T0.getArch() == T1.getArch() && T0.getSubArch() == T1.getSubArch() &&
1430 T0.getVendor() == T1.getVendor() && T0.getOS() == T1.getOS();
1431
1432 return T0 == T1;
1433 }
1434
1435 // This function returns the merged triple.
mergeTriples(const Triple & SrcTriple,const Triple & DstTriple)1436 static std::string mergeTriples(const Triple &SrcTriple,
1437 const Triple &DstTriple) {
1438 // If vendor is apple, pick the triple with the larger version number.
1439 if (SrcTriple.getVendor() == Triple::Apple)
1440 if (DstTriple.isOSVersionLT(SrcTriple))
1441 return SrcTriple.str();
1442
1443 return DstTriple.str();
1444 }
1445
run()1446 bool IRLinker::run() {
1447 // Inherit the target data from the source module if the destination module
1448 // doesn't have one already.
1449 if (DstM.getDataLayout().isDefault())
1450 DstM.setDataLayout(SrcM.getDataLayout());
1451
1452 if (SrcM.getDataLayout() != DstM.getDataLayout()) {
1453 emitWarning("Linking two modules of different data layouts: '" +
1454 SrcM.getModuleIdentifier() + "' is '" +
1455 SrcM.getDataLayoutStr() + "' whereas '" +
1456 DstM.getModuleIdentifier() + "' is '" +
1457 DstM.getDataLayoutStr() + "'\n");
1458 }
1459
1460 // Copy the target triple from the source to dest if the dest's is empty.
1461 if (DstM.getTargetTriple().empty() && !SrcM.getTargetTriple().empty())
1462 DstM.setTargetTriple(SrcM.getTargetTriple());
1463
1464 Triple SrcTriple(SrcM.getTargetTriple()), DstTriple(DstM.getTargetTriple());
1465
1466 if (!SrcM.getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
1467 emitWarning("Linking two modules of different target triples: " +
1468 SrcM.getModuleIdentifier() + "' is '" + SrcM.getTargetTriple() +
1469 "' whereas '" + DstM.getModuleIdentifier() + "' is '" +
1470 DstM.getTargetTriple() + "'\n");
1471
1472 DstM.setTargetTriple(mergeTriples(SrcTriple, DstTriple));
1473
1474 // Append the module inline asm string.
1475 if (!SrcM.getModuleInlineAsm().empty()) {
1476 if (DstM.getModuleInlineAsm().empty())
1477 DstM.setModuleInlineAsm(SrcM.getModuleInlineAsm());
1478 else
1479 DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" +
1480 SrcM.getModuleInlineAsm());
1481 }
1482
1483 // Loop over all of the linked values to compute type mappings.
1484 computeTypeMapping();
1485
1486 std::reverse(Worklist.begin(), Worklist.end());
1487 while (!Worklist.empty()) {
1488 GlobalValue *GV = Worklist.back();
1489 Worklist.pop_back();
1490
1491 // Already mapped.
1492 if (ValueMap.find(GV) != ValueMap.end() ||
1493 AliasValueMap.find(GV) != AliasValueMap.end())
1494 continue;
1495
1496 assert(!GV->isDeclaration());
1497 MapValue(GV, ValueMap, ValueMapperFlags, &TypeMap, &GValMaterializer);
1498 if (HasError)
1499 return true;
1500 }
1501
1502 // Note that we are done linking global value bodies. This prevents
1503 // metadata linking from creating new references.
1504 DoneLinkingBodies = true;
1505
1506 // Remap all of the named MDNodes in Src into the DstM module. We do this
1507 // after linking GlobalValues so that MDNodes that reference GlobalValues
1508 // are properly remapped.
1509 if (shouldLinkMetadata()) {
1510 // Even if just linking metadata we should link decls above in case
1511 // any are referenced by metadata. IRLinker::shouldLink ensures that
1512 // we don't actually link anything from source.
1513 if (IsMetadataLinkingPostpass) {
1514 // Ensure metadata materialized
1515 if (SrcM.getMaterializer()->materializeMetadata())
1516 return true;
1517 SrcM.getMaterializer()->saveMDValueList(MDValueToValIDMap, false);
1518 }
1519
1520 linkNamedMDNodes();
1521
1522 if (IsMetadataLinkingPostpass) {
1523 // Handle anything left in the ValIDToTempMDMap, such as metadata nodes
1524 // not reached by the dbg.cu NamedMD (i.e. only reached from
1525 // instructions).
1526 // Walk the MDValueToValIDMap once to find the set of new (imported) MD
1527 // that still has corresponding temporary metadata, and invoke metadata
1528 // mapping on each one.
1529 for (auto MDI : MDValueToValIDMap) {
1530 if (!ValIDToTempMDMap->count(MDI.second))
1531 continue;
1532 MapMetadata(MDI.first, ValueMap, ValueMapperFlags, &TypeMap,
1533 &GValMaterializer);
1534 }
1535 assert(ValIDToTempMDMap->empty());
1536 }
1537
1538 // Merge the module flags into the DstM module.
1539 if (linkModuleFlagsMetadata())
1540 return true;
1541 }
1542
1543 return false;
1544 }
1545
KeyTy(ArrayRef<Type * > E,bool P)1546 IRMover::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
1547 : ETypes(E), IsPacked(P) {}
1548
KeyTy(const StructType * ST)1549 IRMover::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
1550 : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1551
operator ==(const KeyTy & That) const1552 bool IRMover::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
1553 if (IsPacked != That.IsPacked)
1554 return false;
1555 if (ETypes != That.ETypes)
1556 return false;
1557 return true;
1558 }
1559
operator !=(const KeyTy & That) const1560 bool IRMover::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
1561 return !this->operator==(That);
1562 }
1563
getEmptyKey()1564 StructType *IRMover::StructTypeKeyInfo::getEmptyKey() {
1565 return DenseMapInfo<StructType *>::getEmptyKey();
1566 }
1567
getTombstoneKey()1568 StructType *IRMover::StructTypeKeyInfo::getTombstoneKey() {
1569 return DenseMapInfo<StructType *>::getTombstoneKey();
1570 }
1571
getHashValue(const KeyTy & Key)1572 unsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1573 return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
1574 Key.IsPacked);
1575 }
1576
getHashValue(const StructType * ST)1577 unsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1578 return getHashValue(KeyTy(ST));
1579 }
1580
isEqual(const KeyTy & LHS,const StructType * RHS)1581 bool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1582 const StructType *RHS) {
1583 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1584 return false;
1585 return LHS == KeyTy(RHS);
1586 }
1587
isEqual(const StructType * LHS,const StructType * RHS)1588 bool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS,
1589 const StructType *RHS) {
1590 if (RHS == getEmptyKey())
1591 return LHS == getEmptyKey();
1592
1593 if (RHS == getTombstoneKey())
1594 return LHS == getTombstoneKey();
1595
1596 return KeyTy(LHS) == KeyTy(RHS);
1597 }
1598
addNonOpaque(StructType * Ty)1599 void IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
1600 assert(!Ty->isOpaque());
1601 NonOpaqueStructTypes.insert(Ty);
1602 }
1603
switchToNonOpaque(StructType * Ty)1604 void IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
1605 assert(!Ty->isOpaque());
1606 NonOpaqueStructTypes.insert(Ty);
1607 bool Removed = OpaqueStructTypes.erase(Ty);
1608 (void)Removed;
1609 assert(Removed);
1610 }
1611
addOpaque(StructType * Ty)1612 void IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
1613 assert(Ty->isOpaque());
1614 OpaqueStructTypes.insert(Ty);
1615 }
1616
1617 StructType *
findNonOpaque(ArrayRef<Type * > ETypes,bool IsPacked)1618 IRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
1619 bool IsPacked) {
1620 IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
1621 auto I = NonOpaqueStructTypes.find_as(Key);
1622 if (I == NonOpaqueStructTypes.end())
1623 return nullptr;
1624 return *I;
1625 }
1626
hasType(StructType * Ty)1627 bool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) {
1628 if (Ty->isOpaque())
1629 return OpaqueStructTypes.count(Ty);
1630 auto I = NonOpaqueStructTypes.find(Ty);
1631 if (I == NonOpaqueStructTypes.end())
1632 return false;
1633 return *I == Ty;
1634 }
1635
IRMover(Module & M)1636 IRMover::IRMover(Module &M) : Composite(M) {
1637 TypeFinder StructTypes;
1638 StructTypes.run(M, true);
1639 for (StructType *Ty : StructTypes) {
1640 if (Ty->isOpaque())
1641 IdentifiedStructTypes.addOpaque(Ty);
1642 else
1643 IdentifiedStructTypes.addNonOpaque(Ty);
1644 }
1645 }
1646
move(Module & Src,ArrayRef<GlobalValue * > ValuesToLink,std::function<void (GlobalValue &,ValueAdder Add)> AddLazyFor,DenseMap<unsigned,MDNode * > * ValIDToTempMDMap,bool IsMetadataLinkingPostpass)1647 bool IRMover::move(
1648 Module &Src, ArrayRef<GlobalValue *> ValuesToLink,
1649 std::function<void(GlobalValue &, ValueAdder Add)> AddLazyFor,
1650 DenseMap<unsigned, MDNode *> *ValIDToTempMDMap,
1651 bool IsMetadataLinkingPostpass) {
1652 IRLinker TheIRLinker(Composite, IdentifiedStructTypes, Src, ValuesToLink,
1653 AddLazyFor, ValIDToTempMDMap, IsMetadataLinkingPostpass);
1654 bool RetCode = TheIRLinker.run();
1655 Composite.dropTriviallyDeadConstantArrays();
1656 return RetCode;
1657 }
1658