1 //===- lib/Linker/IRMover.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Linker/IRMover.h"
10 #include "LinkDiagnosticInfo.h"
11 #include "llvm/ADT/SetVector.h"
12 #include "llvm/ADT/SmallPtrSet.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/IR/AutoUpgrade.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DebugInfoMetadata.h"
18 #include "llvm/IR/DiagnosticPrinter.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/GVMaterializer.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/IR/Intrinsics.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/PseudoProbe.h"
25 #include "llvm/IR/TypeFinder.h"
26 #include "llvm/Object/ModuleSymbolTable.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Transforms/Utils/ValueMapper.h"
30 #include <utility>
31 using namespace llvm;
32 
33 //===----------------------------------------------------------------------===//
34 // TypeMap implementation.
35 //===----------------------------------------------------------------------===//
36 
37 namespace {
38 class TypeMapTy : public ValueMapTypeRemapper {
39   /// This is a mapping from a source type to a destination type to use.
40   DenseMap<Type *, Type *> MappedTypes;
41 
42   /// When checking to see if two subgraphs are isomorphic, we speculatively
43   /// add types to MappedTypes, but keep track of them here in case we need to
44   /// roll back.
45   SmallVector<Type *, 16> SpeculativeTypes;
46 
47   SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes;
48 
49   /// This is a list of non-opaque structs in the source module that are mapped
50   /// to an opaque struct in the destination module.
51   SmallVector<StructType *, 16> SrcDefinitionsToResolve;
52 
53   /// This is the set of opaque types in the destination modules who are
54   /// getting a body from the source module.
55   SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes;
56 
57 public:
58   TypeMapTy(IRMover::IdentifiedStructTypeSet &DstStructTypesSet)
59       : DstStructTypesSet(DstStructTypesSet) {}
60 
61   IRMover::IdentifiedStructTypeSet &DstStructTypesSet;
62   /// Indicate that the specified type in the destination module is conceptually
63   /// equivalent to the specified type in the source module.
64   void addTypeMapping(Type *DstTy, Type *SrcTy);
65 
66   /// Produce a body for an opaque type in the dest module from a type
67   /// definition in the source module.
68   void linkDefinedTypeBodies();
69 
70   /// Return the mapped type to use for the specified input type from the
71   /// source module.
72   Type *get(Type *SrcTy);
73   Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
74 
75   void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
76 
77   FunctionType *get(FunctionType *T) {
78     return cast<FunctionType>(get((Type *)T));
79   }
80 
81 private:
82   Type *remapType(Type *SrcTy) override { return get(SrcTy); }
83 
84   bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
85 };
86 }
87 
88 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
89   assert(SpeculativeTypes.empty());
90   assert(SpeculativeDstOpaqueTypes.empty());
91 
92   // Check to see if these types are recursively isomorphic and establish a
93   // mapping between them if so.
94   if (!areTypesIsomorphic(DstTy, SrcTy)) {
95     // Oops, they aren't isomorphic.  Just discard this request by rolling out
96     // any speculative mappings we've established.
97     for (Type *Ty : SpeculativeTypes)
98       MappedTypes.erase(Ty);
99 
100     SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
101                                    SpeculativeDstOpaqueTypes.size());
102     for (StructType *Ty : SpeculativeDstOpaqueTypes)
103       DstResolvedOpaqueTypes.erase(Ty);
104   } else {
105     // SrcTy and DstTy are recursively ismorphic. We clear names of SrcTy
106     // and all its descendants to lower amount of renaming in LLVM context
107     // Renaming occurs because we load all source modules to the same context
108     // and declaration with existing name gets renamed (i.e Foo -> Foo.42).
109     // As a result we may get several different types in the destination
110     // module, which are in fact the same.
111     for (Type *Ty : SpeculativeTypes)
112       if (auto *STy = dyn_cast<StructType>(Ty))
113         if (STy->hasName())
114           STy->setName("");
115   }
116   SpeculativeTypes.clear();
117   SpeculativeDstOpaqueTypes.clear();
118 }
119 
120 /// Recursively walk this pair of types, returning true if they are isomorphic,
121 /// false if they are not.
122 bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
123   // Two types with differing kinds are clearly not isomorphic.
124   if (DstTy->getTypeID() != SrcTy->getTypeID())
125     return false;
126 
127   // If we have an entry in the MappedTypes table, then we have our answer.
128   Type *&Entry = MappedTypes[SrcTy];
129   if (Entry)
130     return Entry == DstTy;
131 
132   // Two identical types are clearly isomorphic.  Remember this
133   // non-speculatively.
134   if (DstTy == SrcTy) {
135     Entry = DstTy;
136     return true;
137   }
138 
139   // Okay, we have two types with identical kinds that we haven't seen before.
140 
141   // If this is an opaque struct type, special case it.
142   if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
143     // Mapping an opaque type to any struct, just keep the dest struct.
144     if (SSTy->isOpaque()) {
145       Entry = DstTy;
146       SpeculativeTypes.push_back(SrcTy);
147       return true;
148     }
149 
150     // Mapping a non-opaque source type to an opaque dest.  If this is the first
151     // type that we're mapping onto this destination type then we succeed.  Keep
152     // the dest, but fill it in later. If this is the second (different) type
153     // that we're trying to map onto the same opaque type then we fail.
154     if (cast<StructType>(DstTy)->isOpaque()) {
155       // We can only map one source type onto the opaque destination type.
156       if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
157         return false;
158       SrcDefinitionsToResolve.push_back(SSTy);
159       SpeculativeTypes.push_back(SrcTy);
160       SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
161       Entry = DstTy;
162       return true;
163     }
164   }
165 
166   // If the number of subtypes disagree between the two types, then we fail.
167   if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
168     return false;
169 
170   // Fail if any of the extra properties (e.g. array size) of the type disagree.
171   if (isa<IntegerType>(DstTy))
172     return false; // bitwidth disagrees.
173   if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
174     if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
175       return false;
176   } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
177     if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
178       return false;
179   } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
180     StructType *SSTy = cast<StructType>(SrcTy);
181     if (DSTy->isLiteral() != SSTy->isLiteral() ||
182         DSTy->isPacked() != SSTy->isPacked())
183       return false;
184   } else if (auto *DArrTy = dyn_cast<ArrayType>(DstTy)) {
185     if (DArrTy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
186       return false;
187   } else if (auto *DVecTy = dyn_cast<VectorType>(DstTy)) {
188     if (DVecTy->getElementCount() != cast<VectorType>(SrcTy)->getElementCount())
189       return false;
190   }
191 
192   // Otherwise, we speculate that these two types will line up and recursively
193   // check the subelements.
194   Entry = DstTy;
195   SpeculativeTypes.push_back(SrcTy);
196 
197   for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
198     if (!areTypesIsomorphic(DstTy->getContainedType(I),
199                             SrcTy->getContainedType(I)))
200       return false;
201 
202   // If everything seems to have lined up, then everything is great.
203   return true;
204 }
205 
206 void TypeMapTy::linkDefinedTypeBodies() {
207   SmallVector<Type *, 16> Elements;
208   for (StructType *SrcSTy : SrcDefinitionsToResolve) {
209     StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
210     assert(DstSTy->isOpaque());
211 
212     // Map the body of the source type over to a new body for the dest type.
213     Elements.resize(SrcSTy->getNumElements());
214     for (unsigned I = 0, E = Elements.size(); I != E; ++I)
215       Elements[I] = get(SrcSTy->getElementType(I));
216 
217     DstSTy->setBody(Elements, SrcSTy->isPacked());
218     DstStructTypesSet.switchToNonOpaque(DstSTy);
219   }
220   SrcDefinitionsToResolve.clear();
221   DstResolvedOpaqueTypes.clear();
222 }
223 
224 void TypeMapTy::finishType(StructType *DTy, StructType *STy,
225                            ArrayRef<Type *> ETypes) {
226   DTy->setBody(ETypes, STy->isPacked());
227 
228   // Steal STy's name.
229   if (STy->hasName()) {
230     SmallString<16> TmpName = STy->getName();
231     STy->setName("");
232     DTy->setName(TmpName);
233   }
234 
235   DstStructTypesSet.addNonOpaque(DTy);
236 }
237 
238 Type *TypeMapTy::get(Type *Ty) {
239   SmallPtrSet<StructType *, 8> Visited;
240   return get(Ty, Visited);
241 }
242 
243 Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
244   // If we already have an entry for this type, return it.
245   Type **Entry = &MappedTypes[Ty];
246   if (*Entry)
247     return *Entry;
248 
249   // These are types that LLVM itself will unique.
250   bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
251 
252   if (!IsUniqued) {
253 #ifndef NDEBUG
254     for (auto &Pair : MappedTypes) {
255       assert(!(Pair.first != Ty && Pair.second == Ty) &&
256              "mapping to a source type");
257     }
258 #endif
259 
260     if (!Visited.insert(cast<StructType>(Ty)).second) {
261       StructType *DTy = StructType::create(Ty->getContext());
262       return *Entry = DTy;
263     }
264   }
265 
266   // If this is not a recursive type, then just map all of the elements and
267   // then rebuild the type from inside out.
268   SmallVector<Type *, 4> ElementTypes;
269 
270   // If there are no element types to map, then the type is itself.  This is
271   // true for the anonymous {} struct, things like 'float', integers, etc.
272   if (Ty->getNumContainedTypes() == 0 && IsUniqued)
273     return *Entry = Ty;
274 
275   // Remap all of the elements, keeping track of whether any of them change.
276   bool AnyChange = false;
277   ElementTypes.resize(Ty->getNumContainedTypes());
278   for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
279     ElementTypes[I] = get(Ty->getContainedType(I), Visited);
280     AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
281   }
282 
283   // If we found our type while recursively processing stuff, just use it.
284   Entry = &MappedTypes[Ty];
285   if (*Entry) {
286     if (auto *DTy = dyn_cast<StructType>(*Entry)) {
287       if (DTy->isOpaque()) {
288         auto *STy = cast<StructType>(Ty);
289         finishType(DTy, STy, ElementTypes);
290       }
291     }
292     return *Entry;
293   }
294 
295   // If all of the element types mapped directly over and the type is not
296   // a named struct, then the type is usable as-is.
297   if (!AnyChange && IsUniqued)
298     return *Entry = Ty;
299 
300   // Otherwise, rebuild a modified type.
301   switch (Ty->getTypeID()) {
302   default:
303     llvm_unreachable("unknown derived type to remap");
304   case Type::ArrayTyID:
305     return *Entry = ArrayType::get(ElementTypes[0],
306                                    cast<ArrayType>(Ty)->getNumElements());
307   case Type::ScalableVectorTyID:
308   case Type::FixedVectorTyID:
309     return *Entry = VectorType::get(ElementTypes[0],
310                                     cast<VectorType>(Ty)->getElementCount());
311   case Type::PointerTyID:
312     return *Entry = PointerType::get(ElementTypes[0],
313                                      cast<PointerType>(Ty)->getAddressSpace());
314   case Type::FunctionTyID:
315     return *Entry = FunctionType::get(ElementTypes[0],
316                                       makeArrayRef(ElementTypes).slice(1),
317                                       cast<FunctionType>(Ty)->isVarArg());
318   case Type::StructTyID: {
319     auto *STy = cast<StructType>(Ty);
320     bool IsPacked = STy->isPacked();
321     if (IsUniqued)
322       return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
323 
324     // If the type is opaque, we can just use it directly.
325     if (STy->isOpaque()) {
326       DstStructTypesSet.addOpaque(STy);
327       return *Entry = Ty;
328     }
329 
330     if (StructType *OldT =
331             DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
332       STy->setName("");
333       return *Entry = OldT;
334     }
335 
336     if (!AnyChange) {
337       DstStructTypesSet.addNonOpaque(STy);
338       return *Entry = Ty;
339     }
340 
341     StructType *DTy = StructType::create(Ty->getContext());
342     finishType(DTy, STy, ElementTypes);
343     return *Entry = DTy;
344   }
345   }
346 }
347 
348 LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
349                                        const Twine &Msg)
350     : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
351 void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
352 
353 //===----------------------------------------------------------------------===//
354 // IRLinker implementation.
355 //===----------------------------------------------------------------------===//
356 
357 namespace {
358 class IRLinker;
359 
360 /// Creates prototypes for functions that are lazily linked on the fly. This
361 /// speeds up linking for modules with many/ lazily linked functions of which
362 /// few get used.
363 class GlobalValueMaterializer final : public ValueMaterializer {
364   IRLinker &TheIRLinker;
365 
366 public:
367   GlobalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
368   Value *materialize(Value *V) override;
369 };
370 
371 class LocalValueMaterializer final : public ValueMaterializer {
372   IRLinker &TheIRLinker;
373 
374 public:
375   LocalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {}
376   Value *materialize(Value *V) override;
377 };
378 
379 /// Type of the Metadata map in \a ValueToValueMapTy.
380 typedef DenseMap<const Metadata *, TrackingMDRef> MDMapT;
381 
382 /// This is responsible for keeping track of the state used for moving data
383 /// from SrcM to DstM.
384 class IRLinker {
385   Module &DstM;
386   std::unique_ptr<Module> SrcM;
387 
388   /// See IRMover::move().
389   IRMover::LazyCallback AddLazyFor;
390 
391   TypeMapTy TypeMap;
392   GlobalValueMaterializer GValMaterializer;
393   LocalValueMaterializer LValMaterializer;
394 
395   /// A metadata map that's shared between IRLinker instances.
396   MDMapT &SharedMDs;
397 
398   /// Mapping of values from what they used to be in Src, to what they are now
399   /// in DstM.  ValueToValueMapTy is a ValueMap, which involves some overhead
400   /// due to the use of Value handles which the Linker doesn't actually need,
401   /// but this allows us to reuse the ValueMapper code.
402   ValueToValueMapTy ValueMap;
403   ValueToValueMapTy IndirectSymbolValueMap;
404 
405   DenseSet<GlobalValue *> ValuesToLink;
406   std::vector<GlobalValue *> Worklist;
407   std::vector<std::pair<GlobalValue *, Value*>> RAUWWorklist;
408 
409   void maybeAdd(GlobalValue *GV) {
410     if (ValuesToLink.insert(GV).second)
411       Worklist.push_back(GV);
412   }
413 
414   /// Whether we are importing globals for ThinLTO, as opposed to linking the
415   /// source module. If this flag is set, it means that we can rely on some
416   /// other object file to define any non-GlobalValue entities defined by the
417   /// source module. This currently causes us to not link retained types in
418   /// debug info metadata and module inline asm.
419   bool IsPerformingImport;
420 
421   /// Set to true when all global value body linking is complete (including
422   /// lazy linking). Used to prevent metadata linking from creating new
423   /// references.
424   bool DoneLinkingBodies = false;
425 
426   /// The Error encountered during materialization. We use an Optional here to
427   /// avoid needing to manage an unconsumed success value.
428   Optional<Error> FoundError;
429   void setError(Error E) {
430     if (E)
431       FoundError = std::move(E);
432   }
433 
434   /// Most of the errors produced by this module are inconvertible StringErrors.
435   /// This convenience function lets us return one of those more easily.
436   Error stringErr(const Twine &T) {
437     return make_error<StringError>(T, inconvertibleErrorCode());
438   }
439 
440   /// Entry point for mapping values and alternate context for mapping aliases.
441   ValueMapper Mapper;
442   unsigned IndirectSymbolMCID;
443 
444   /// Handles cloning of a global values from the source module into
445   /// the destination module, including setting the attributes and visibility.
446   GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition);
447 
448   void emitWarning(const Twine &Message) {
449     SrcM->getContext().diagnose(LinkDiagnosticInfo(DS_Warning, Message));
450   }
451 
452   /// Given a global in the source module, return the global in the
453   /// destination module that is being linked to, if any.
454   GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
455     // If the source has no name it can't link.  If it has local linkage,
456     // there is no name match-up going on.
457     if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
458       return nullptr;
459 
460     // Otherwise see if we have a match in the destination module's symtab.
461     GlobalValue *DGV = DstM.getNamedValue(SrcGV->getName());
462     if (!DGV)
463       return nullptr;
464 
465     // If we found a global with the same name in the dest module, but it has
466     // internal linkage, we are really not doing any linkage here.
467     if (DGV->hasLocalLinkage())
468       return nullptr;
469 
470     // If we found an intrinsic declaration with mismatching prototypes, we
471     // probably had a nameclash. Don't use that version.
472     if (auto *FDGV = dyn_cast<Function>(DGV))
473       if (FDGV->isIntrinsic())
474         if (const auto *FSrcGV = dyn_cast<Function>(SrcGV))
475           if (FDGV->getFunctionType() != TypeMap.get(FSrcGV->getFunctionType()))
476             return nullptr;
477 
478     // Otherwise, we do in fact link to the destination global.
479     return DGV;
480   }
481 
482   void computeTypeMapping();
483 
484   Expected<Constant *> linkAppendingVarProto(GlobalVariable *DstGV,
485                                              const GlobalVariable *SrcGV);
486 
487   /// Given the GlobaValue \p SGV in the source module, and the matching
488   /// GlobalValue \p DGV (if any), return true if the linker will pull \p SGV
489   /// into the destination module.
490   ///
491   /// Note this code may call the client-provided \p AddLazyFor.
492   bool shouldLink(GlobalValue *DGV, GlobalValue &SGV);
493   Expected<Constant *> linkGlobalValueProto(GlobalValue *GV,
494                                             bool ForIndirectSymbol);
495 
496   Error linkModuleFlagsMetadata();
497 
498   void linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src);
499   Error linkFunctionBody(Function &Dst, Function &Src);
500   void linkAliasAliasee(GlobalAlias &Dst, GlobalAlias &Src);
501   void linkIFuncResolver(GlobalIFunc &Dst, GlobalIFunc &Src);
502   Error linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src);
503 
504   /// Replace all types in the source AttributeList with the
505   /// corresponding destination type.
506   AttributeList mapAttributeTypes(LLVMContext &C, AttributeList Attrs);
507 
508   /// Functions that take care of cloning a specific global value type
509   /// into the destination module.
510   GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar);
511   Function *copyFunctionProto(const Function *SF);
512   GlobalValue *copyIndirectSymbolProto(const GlobalValue *SGV);
513 
514   /// Perform "replace all uses with" operations. These work items need to be
515   /// performed as part of materialization, but we postpone them to happen after
516   /// materialization is done. The materializer called by ValueMapper is not
517   /// expected to delete constants, as ValueMapper is holding pointers to some
518   /// of them, but constant destruction may be indirectly triggered by RAUW.
519   /// Hence, the need to move this out of the materialization call chain.
520   void flushRAUWWorklist();
521 
522   /// When importing for ThinLTO, prevent importing of types listed on
523   /// the DICompileUnit that we don't need a copy of in the importing
524   /// module.
525   void prepareCompileUnitsForImport();
526   void linkNamedMDNodes();
527 
528 public:
529   IRLinker(Module &DstM, MDMapT &SharedMDs,
530            IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr<Module> SrcM,
531            ArrayRef<GlobalValue *> ValuesToLink,
532            IRMover::LazyCallback AddLazyFor, bool IsPerformingImport)
533       : DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)),
534         TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this),
535         SharedMDs(SharedMDs), IsPerformingImport(IsPerformingImport),
536         Mapper(ValueMap, RF_ReuseAndMutateDistinctMDs | RF_IgnoreMissingLocals,
537                &TypeMap, &GValMaterializer),
538         IndirectSymbolMCID(Mapper.registerAlternateMappingContext(
539             IndirectSymbolValueMap, &LValMaterializer)) {
540     ValueMap.getMDMap() = std::move(SharedMDs);
541     for (GlobalValue *GV : ValuesToLink)
542       maybeAdd(GV);
543     if (IsPerformingImport)
544       prepareCompileUnitsForImport();
545   }
546   ~IRLinker() { SharedMDs = std::move(*ValueMap.getMDMap()); }
547 
548   Error run();
549   Value *materialize(Value *V, bool ForIndirectSymbol);
550 };
551 }
552 
553 /// The LLVM SymbolTable class autorenames globals that conflict in the symbol
554 /// table. This is good for all clients except for us. Go through the trouble
555 /// to force this back.
556 static void forceRenaming(GlobalValue *GV, StringRef Name) {
557   // If the global doesn't force its name or if it already has the right name,
558   // there is nothing for us to do.
559   if (GV->hasLocalLinkage() || GV->getName() == Name)
560     return;
561 
562   Module *M = GV->getParent();
563 
564   // If there is a conflict, rename the conflict.
565   if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
566     GV->takeName(ConflictGV);
567     ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
568     assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
569   } else {
570     GV->setName(Name); // Force the name back
571   }
572 }
573 
574 Value *GlobalValueMaterializer::materialize(Value *SGV) {
575   return TheIRLinker.materialize(SGV, false);
576 }
577 
578 Value *LocalValueMaterializer::materialize(Value *SGV) {
579   return TheIRLinker.materialize(SGV, true);
580 }
581 
582 Value *IRLinker::materialize(Value *V, bool ForIndirectSymbol) {
583   auto *SGV = dyn_cast<GlobalValue>(V);
584   if (!SGV)
585     return nullptr;
586 
587   // When linking a global from other modules than source & dest, skip
588   // materializing it because it would be mapped later when its containing
589   // module is linked. Linking it now would potentially pull in many types that
590   // may not be mapped properly.
591   if (SGV->getParent() != &DstM && SGV->getParent() != SrcM.get())
592     return nullptr;
593 
594   Expected<Constant *> NewProto = linkGlobalValueProto(SGV, ForIndirectSymbol);
595   if (!NewProto) {
596     setError(NewProto.takeError());
597     return nullptr;
598   }
599   if (!*NewProto)
600     return nullptr;
601 
602   GlobalValue *New = dyn_cast<GlobalValue>(*NewProto);
603   if (!New)
604     return *NewProto;
605 
606   // If we already created the body, just return.
607   if (auto *F = dyn_cast<Function>(New)) {
608     if (!F->isDeclaration())
609       return New;
610   } else if (auto *V = dyn_cast<GlobalVariable>(New)) {
611     if (V->hasInitializer() || V->hasAppendingLinkage())
612       return New;
613   } else if (auto *GA = dyn_cast<GlobalAlias>(New)) {
614     if (GA->getAliasee())
615       return New;
616   } else if (auto *GI = dyn_cast<GlobalIFunc>(New)) {
617     if (GI->getResolver())
618       return New;
619   } else {
620     llvm_unreachable("Invalid GlobalValue type");
621   }
622 
623   // If the global is being linked for an indirect symbol, it may have already
624   // been scheduled to satisfy a regular symbol. Similarly, a global being linked
625   // for a regular symbol may have already been scheduled for an indirect
626   // symbol. Check for these cases by looking in the other value map and
627   // confirming the same value has been scheduled.  If there is an entry in the
628   // ValueMap but the value is different, it means that the value already had a
629   // definition in the destination module (linkonce for instance), but we need a
630   // new definition for the indirect symbol ("New" will be different).
631   if ((ForIndirectSymbol && ValueMap.lookup(SGV) == New) ||
632       (!ForIndirectSymbol && IndirectSymbolValueMap.lookup(SGV) == New))
633     return New;
634 
635   if (ForIndirectSymbol || shouldLink(New, *SGV))
636     setError(linkGlobalValueBody(*New, *SGV));
637 
638   return New;
639 }
640 
641 /// Loop through the global variables in the src module and merge them into the
642 /// dest module.
643 GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
644   // No linking to be performed or linking from the source: simply create an
645   // identical version of the symbol over in the dest module... the
646   // initializer will be filled in later by LinkGlobalInits.
647   GlobalVariable *NewDGV =
648       new GlobalVariable(DstM, TypeMap.get(SGVar->getValueType()),
649                          SGVar->isConstant(), GlobalValue::ExternalLinkage,
650                          /*init*/ nullptr, SGVar->getName(),
651                          /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
652                          SGVar->getAddressSpace());
653   NewDGV->setAlignment(SGVar->getAlign());
654   NewDGV->copyAttributesFrom(SGVar);
655   return NewDGV;
656 }
657 
658 AttributeList IRLinker::mapAttributeTypes(LLVMContext &C, AttributeList Attrs) {
659   for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
660     for (int AttrIdx = Attribute::FirstTypeAttr;
661          AttrIdx <= Attribute::LastTypeAttr; AttrIdx++) {
662       Attribute::AttrKind TypedAttr = (Attribute::AttrKind)AttrIdx;
663       if (Attrs.hasAttributeAtIndex(i, TypedAttr)) {
664         if (Type *Ty =
665                 Attrs.getAttributeAtIndex(i, TypedAttr).getValueAsType()) {
666           Attrs = Attrs.replaceAttributeTypeAtIndex(C, i, TypedAttr,
667                                                     TypeMap.get(Ty));
668           break;
669         }
670       }
671     }
672   }
673   return Attrs;
674 }
675 
676 /// Link the function in the source module into the destination module if
677 /// needed, setting up mapping information.
678 Function *IRLinker::copyFunctionProto(const Function *SF) {
679   // If there is no linkage to be performed or we are linking from the source,
680   // bring SF over.
681   auto *F = Function::Create(TypeMap.get(SF->getFunctionType()),
682                              GlobalValue::ExternalLinkage,
683                              SF->getAddressSpace(), SF->getName(), &DstM);
684   F->copyAttributesFrom(SF);
685   F->setAttributes(mapAttributeTypes(F->getContext(), F->getAttributes()));
686   return F;
687 }
688 
689 /// Set up prototypes for any indirect symbols that come over from the source
690 /// module.
691 GlobalValue *IRLinker::copyIndirectSymbolProto(const GlobalValue *SGV) {
692   // If there is no linkage to be performed or we're linking from the source,
693   // bring over SGA.
694   auto *Ty = TypeMap.get(SGV->getValueType());
695 
696   if (auto *GA = dyn_cast<GlobalAlias>(SGV)) {
697     auto *DGA = GlobalAlias::create(Ty, SGV->getAddressSpace(),
698                                     GlobalValue::ExternalLinkage,
699                                     SGV->getName(), &DstM);
700     DGA->copyAttributesFrom(GA);
701     return DGA;
702   }
703 
704   if (auto *GI = dyn_cast<GlobalIFunc>(SGV)) {
705     auto *DGI = GlobalIFunc::create(Ty, SGV->getAddressSpace(),
706                                     GlobalValue::ExternalLinkage,
707                                     SGV->getName(), nullptr, &DstM);
708     DGI->copyAttributesFrom(GI);
709     return DGI;
710   }
711 
712   llvm_unreachable("Invalid source global value type");
713 }
714 
715 GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
716                                             bool ForDefinition) {
717   GlobalValue *NewGV;
718   if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {
719     NewGV = copyGlobalVariableProto(SGVar);
720   } else if (auto *SF = dyn_cast<Function>(SGV)) {
721     NewGV = copyFunctionProto(SF);
722   } else {
723     if (ForDefinition)
724       NewGV = copyIndirectSymbolProto(SGV);
725     else if (SGV->getValueType()->isFunctionTy())
726       NewGV =
727           Function::Create(cast<FunctionType>(TypeMap.get(SGV->getValueType())),
728                            GlobalValue::ExternalLinkage, SGV->getAddressSpace(),
729                            SGV->getName(), &DstM);
730     else
731       NewGV =
732           new GlobalVariable(DstM, TypeMap.get(SGV->getValueType()),
733                              /*isConstant*/ false, GlobalValue::ExternalLinkage,
734                              /*init*/ nullptr, SGV->getName(),
735                              /*insertbefore*/ nullptr,
736                              SGV->getThreadLocalMode(), SGV->getAddressSpace());
737   }
738 
739   if (ForDefinition)
740     NewGV->setLinkage(SGV->getLinkage());
741   else if (SGV->hasExternalWeakLinkage())
742     NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
743 
744   if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
745     // Metadata for global variables and function declarations is copied eagerly.
746     if (isa<GlobalVariable>(SGV) || SGV->isDeclaration())
747       NewGO->copyMetadata(cast<GlobalObject>(SGV), 0);
748   }
749 
750   // Remove these copied constants in case this stays a declaration, since
751   // they point to the source module. If the def is linked the values will
752   // be mapped in during linkFunctionBody.
753   if (auto *NewF = dyn_cast<Function>(NewGV)) {
754     NewF->setPersonalityFn(nullptr);
755     NewF->setPrefixData(nullptr);
756     NewF->setPrologueData(nullptr);
757   }
758 
759   return NewGV;
760 }
761 
762 static StringRef getTypeNamePrefix(StringRef Name) {
763   size_t DotPos = Name.rfind('.');
764   return (DotPos == 0 || DotPos == StringRef::npos || Name.back() == '.' ||
765           !isdigit(static_cast<unsigned char>(Name[DotPos + 1])))
766              ? Name
767              : Name.substr(0, DotPos);
768 }
769 
770 /// Loop over all of the linked values to compute type mappings.  For example,
771 /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
772 /// types 'Foo' but one got renamed when the module was loaded into the same
773 /// LLVMContext.
774 void IRLinker::computeTypeMapping() {
775   for (GlobalValue &SGV : SrcM->globals()) {
776     GlobalValue *DGV = getLinkedToGlobal(&SGV);
777     if (!DGV)
778       continue;
779 
780     if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
781       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
782       continue;
783     }
784 
785     // Unify the element type of appending arrays.
786     ArrayType *DAT = cast<ArrayType>(DGV->getValueType());
787     ArrayType *SAT = cast<ArrayType>(SGV.getValueType());
788     TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
789   }
790 
791   for (GlobalValue &SGV : *SrcM)
792     if (GlobalValue *DGV = getLinkedToGlobal(&SGV)) {
793       if (DGV->getType() == SGV.getType()) {
794         // If the types of DGV and SGV are the same, it means that DGV is from
795         // the source module and got added to DstM from a shared metadata.  We
796         // shouldn't map this type to itself in case the type's components get
797         // remapped to a new type from DstM (for instance, during the loop over
798         // SrcM->getIdentifiedStructTypes() below).
799         continue;
800       }
801 
802       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
803     }
804 
805   for (GlobalValue &SGV : SrcM->aliases())
806     if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
807       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
808 
809   // Incorporate types by name, scanning all the types in the source module.
810   // At this point, the destination module may have a type "%foo = { i32 }" for
811   // example.  When the source module got loaded into the same LLVMContext, if
812   // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
813   std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
814   for (StructType *ST : Types) {
815     if (!ST->hasName())
816       continue;
817 
818     if (TypeMap.DstStructTypesSet.hasType(ST)) {
819       // This is actually a type from the destination module.
820       // getIdentifiedStructTypes() can have found it by walking debug info
821       // metadata nodes, some of which get linked by name when ODR Type Uniquing
822       // is enabled on the Context, from the source to the destination module.
823       continue;
824     }
825 
826     auto STTypePrefix = getTypeNamePrefix(ST->getName());
827     if (STTypePrefix.size() == ST->getName().size())
828       continue;
829 
830     // Check to see if the destination module has a struct with the prefix name.
831     StructType *DST = StructType::getTypeByName(ST->getContext(), STTypePrefix);
832     if (!DST)
833       continue;
834 
835     // Don't use it if this actually came from the source module. They're in
836     // the same LLVMContext after all. Also don't use it unless the type is
837     // actually used in the destination module. This can happen in situations
838     // like this:
839     //
840     //      Module A                         Module B
841     //      --------                         --------
842     //   %Z = type { %A }                %B = type { %C.1 }
843     //   %A = type { %B.1, [7 x i8] }    %C.1 = type { i8* }
844     //   %B.1 = type { %C }              %A.2 = type { %B.3, [5 x i8] }
845     //   %C = type { i8* }               %B.3 = type { %C.1 }
846     //
847     // When we link Module B with Module A, the '%B' in Module B is
848     // used. However, that would then use '%C.1'. But when we process '%C.1',
849     // we prefer to take the '%C' version. So we are then left with both
850     // '%C.1' and '%C' being used for the same types. This leads to some
851     // variables using one type and some using the other.
852     if (TypeMap.DstStructTypesSet.hasType(DST))
853       TypeMap.addTypeMapping(DST, ST);
854   }
855 
856   // Now that we have discovered all of the type equivalences, get a body for
857   // any 'opaque' types in the dest module that are now resolved.
858   TypeMap.linkDefinedTypeBodies();
859 }
860 
861 static void getArrayElements(const Constant *C,
862                              SmallVectorImpl<Constant *> &Dest) {
863   unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
864 
865   for (unsigned i = 0; i != NumElements; ++i)
866     Dest.push_back(C->getAggregateElement(i));
867 }
868 
869 /// If there were any appending global variables, link them together now.
870 Expected<Constant *>
871 IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
872                                 const GlobalVariable *SrcGV) {
873   // Check that both variables have compatible properties.
874   if (DstGV && !DstGV->isDeclaration() && !SrcGV->isDeclaration()) {
875     if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
876       return stringErr(
877           "Linking globals named '" + SrcGV->getName() +
878           "': can only link appending global with another appending "
879           "global!");
880 
881     if (DstGV->isConstant() != SrcGV->isConstant())
882       return stringErr("Appending variables linked with different const'ness!");
883 
884     if (DstGV->getAlign() != SrcGV->getAlign())
885       return stringErr(
886           "Appending variables with different alignment need to be linked!");
887 
888     if (DstGV->getVisibility() != SrcGV->getVisibility())
889       return stringErr(
890           "Appending variables with different visibility need to be linked!");
891 
892     if (DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr())
893       return stringErr(
894           "Appending variables with different unnamed_addr need to be linked!");
895 
896     if (DstGV->getSection() != SrcGV->getSection())
897       return stringErr(
898           "Appending variables with different section name need to be linked!");
899   }
900 
901   // Do not need to do anything if source is a declaration.
902   if (SrcGV->isDeclaration())
903     return DstGV;
904 
905   Type *EltTy = cast<ArrayType>(TypeMap.get(SrcGV->getValueType()))
906                     ->getElementType();
907 
908   // FIXME: This upgrade is done during linking to support the C API.  Once the
909   // old form is deprecated, we should move this upgrade to
910   // llvm::UpgradeGlobalVariable() and simplify the logic here and in
911   // Mapper::mapAppendingVariable() in ValueMapper.cpp.
912   StringRef Name = SrcGV->getName();
913   bool IsNewStructor = false;
914   bool IsOldStructor = false;
915   if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") {
916     if (cast<StructType>(EltTy)->getNumElements() == 3)
917       IsNewStructor = true;
918     else
919       IsOldStructor = true;
920   }
921 
922   PointerType *VoidPtrTy = Type::getInt8Ty(SrcGV->getContext())->getPointerTo();
923   if (IsOldStructor) {
924     auto &ST = *cast<StructType>(EltTy);
925     Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
926     EltTy = StructType::get(SrcGV->getContext(), Tys, false);
927   }
928 
929   uint64_t DstNumElements = 0;
930   if (DstGV && !DstGV->isDeclaration()) {
931     ArrayType *DstTy = cast<ArrayType>(DstGV->getValueType());
932     DstNumElements = DstTy->getNumElements();
933 
934     // Check to see that they two arrays agree on type.
935     if (EltTy != DstTy->getElementType())
936       return stringErr("Appending variables with different element types!");
937   }
938 
939   SmallVector<Constant *, 16> SrcElements;
940   getArrayElements(SrcGV->getInitializer(), SrcElements);
941 
942   if (IsNewStructor) {
943     erase_if(SrcElements, [this](Constant *E) {
944       auto *Key =
945           dyn_cast<GlobalValue>(E->getAggregateElement(2)->stripPointerCasts());
946       if (!Key)
947         return false;
948       GlobalValue *DGV = getLinkedToGlobal(Key);
949       return !shouldLink(DGV, *Key);
950     });
951   }
952   uint64_t NewSize = DstNumElements + SrcElements.size();
953   ArrayType *NewType = ArrayType::get(EltTy, NewSize);
954 
955   // Create the new global variable.
956   GlobalVariable *NG = new GlobalVariable(
957       DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
958       /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
959       SrcGV->getAddressSpace());
960 
961   NG->copyAttributesFrom(SrcGV);
962   forceRenaming(NG, SrcGV->getName());
963 
964   Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
965 
966   Mapper.scheduleMapAppendingVariable(
967       *NG,
968       (DstGV && !DstGV->isDeclaration()) ? DstGV->getInitializer() : nullptr,
969       IsOldStructor, SrcElements);
970 
971   // Replace any uses of the two global variables with uses of the new
972   // global.
973   if (DstGV) {
974     RAUWWorklist.push_back(
975         std::make_pair(DstGV, ConstantExpr::getBitCast(NG, DstGV->getType())));
976   }
977 
978   return Ret;
979 }
980 
981 bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) {
982   if (ValuesToLink.count(&SGV) || SGV.hasLocalLinkage())
983     return true;
984 
985   if (DGV && !DGV->isDeclarationForLinker())
986     return false;
987 
988   if (SGV.isDeclaration() || DoneLinkingBodies)
989     return false;
990 
991   // Callback to the client to give a chance to lazily add the Global to the
992   // list of value to link.
993   bool LazilyAdded = false;
994   if (AddLazyFor)
995     AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) {
996       maybeAdd(&GV);
997       LazilyAdded = true;
998     });
999   return LazilyAdded;
1000 }
1001 
1002 Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV,
1003                                                     bool ForIndirectSymbol) {
1004   GlobalValue *DGV = getLinkedToGlobal(SGV);
1005 
1006   bool ShouldLink = shouldLink(DGV, *SGV);
1007 
1008   // just missing from map
1009   if (ShouldLink) {
1010     auto I = ValueMap.find(SGV);
1011     if (I != ValueMap.end())
1012       return cast<Constant>(I->second);
1013 
1014     I = IndirectSymbolValueMap.find(SGV);
1015     if (I != IndirectSymbolValueMap.end())
1016       return cast<Constant>(I->second);
1017   }
1018 
1019   if (!ShouldLink && ForIndirectSymbol)
1020     DGV = nullptr;
1021 
1022   // Handle the ultra special appending linkage case first.
1023   if (SGV->hasAppendingLinkage() || (DGV && DGV->hasAppendingLinkage()))
1024     return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),
1025                                  cast<GlobalVariable>(SGV));
1026 
1027   bool NeedsRenaming = false;
1028   GlobalValue *NewGV;
1029   if (DGV && !ShouldLink) {
1030     NewGV = DGV;
1031   } else {
1032     // If we are done linking global value bodies (i.e. we are performing
1033     // metadata linking), don't link in the global value due to this
1034     // reference, simply map it to null.
1035     if (DoneLinkingBodies)
1036       return nullptr;
1037 
1038     NewGV = copyGlobalValueProto(SGV, ShouldLink || ForIndirectSymbol);
1039     if (ShouldLink || !ForIndirectSymbol)
1040       NeedsRenaming = true;
1041   }
1042 
1043   // Overloaded intrinsics have overloaded types names as part of their
1044   // names. If we renamed overloaded types we should rename the intrinsic
1045   // as well.
1046   if (Function *F = dyn_cast<Function>(NewGV))
1047     if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) {
1048       NewGV->eraseFromParent();
1049       NewGV = *Remangled;
1050       NeedsRenaming = false;
1051     }
1052 
1053   if (NeedsRenaming)
1054     forceRenaming(NewGV, SGV->getName());
1055 
1056   if (ShouldLink || ForIndirectSymbol) {
1057     if (const Comdat *SC = SGV->getComdat()) {
1058       if (auto *GO = dyn_cast<GlobalObject>(NewGV)) {
1059         Comdat *DC = DstM.getOrInsertComdat(SC->getName());
1060         DC->setSelectionKind(SC->getSelectionKind());
1061         GO->setComdat(DC);
1062       }
1063     }
1064   }
1065 
1066   if (!ShouldLink && ForIndirectSymbol)
1067     NewGV->setLinkage(GlobalValue::InternalLinkage);
1068 
1069   Constant *C = NewGV;
1070   // Only create a bitcast if necessary. In particular, with
1071   // DebugTypeODRUniquing we may reach metadata in the destination module
1072   // containing a GV from the source module, in which case SGV will be
1073   // the same as DGV and NewGV, and TypeMap.get() will assert since it
1074   // assumes it is being invoked on a type in the source module.
1075   if (DGV && NewGV != SGV) {
1076     C = ConstantExpr::getPointerBitCastOrAddrSpaceCast(
1077       NewGV, TypeMap.get(SGV->getType()));
1078   }
1079 
1080   if (DGV && NewGV != DGV) {
1081     // Schedule "replace all uses with" to happen after materializing is
1082     // done. It is not safe to do it now, since ValueMapper may be holding
1083     // pointers to constants that will get deleted if RAUW runs.
1084     RAUWWorklist.push_back(std::make_pair(
1085         DGV,
1086         ConstantExpr::getPointerBitCastOrAddrSpaceCast(NewGV, DGV->getType())));
1087   }
1088 
1089   return C;
1090 }
1091 
1092 /// Update the initializers in the Dest module now that all globals that may be
1093 /// referenced are in Dest.
1094 void IRLinker::linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src) {
1095   // Figure out what the initializer looks like in the dest module.
1096   Mapper.scheduleMapGlobalInitializer(Dst, *Src.getInitializer());
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.
1102 Error IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
1103   assert(Dst.isDeclaration() && !Src.isDeclaration());
1104 
1105   // Materialize if needed.
1106   if (Error Err = Src.materialize())
1107     return Err;
1108 
1109   // Link in the operands without remapping.
1110   if (Src.hasPrefixData())
1111     Dst.setPrefixData(Src.getPrefixData());
1112   if (Src.hasPrologueData())
1113     Dst.setPrologueData(Src.getPrologueData());
1114   if (Src.hasPersonalityFn())
1115     Dst.setPersonalityFn(Src.getPersonalityFn());
1116 
1117   // Copy over the metadata attachments without remapping.
1118   Dst.copyMetadata(&Src, 0);
1119 
1120   // Steal arguments and splice the body of Src into Dst.
1121   Dst.stealArgumentListFrom(Src);
1122   Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
1123 
1124   // Everything has been moved over.  Remap it.
1125   Mapper.scheduleRemapFunction(Dst);
1126   return Error::success();
1127 }
1128 
1129 void IRLinker::linkAliasAliasee(GlobalAlias &Dst, GlobalAlias &Src) {
1130   Mapper.scheduleMapGlobalAlias(Dst, *Src.getAliasee(), IndirectSymbolMCID);
1131 }
1132 
1133 void IRLinker::linkIFuncResolver(GlobalIFunc &Dst, GlobalIFunc &Src) {
1134   Mapper.scheduleMapGlobalIFunc(Dst, *Src.getResolver(), IndirectSymbolMCID);
1135 }
1136 
1137 Error IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
1138   if (auto *F = dyn_cast<Function>(&Src))
1139     return linkFunctionBody(cast<Function>(Dst), *F);
1140   if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
1141     linkGlobalVariable(cast<GlobalVariable>(Dst), *GVar);
1142     return Error::success();
1143   }
1144   if (auto *GA = dyn_cast<GlobalAlias>(&Src)) {
1145     linkAliasAliasee(cast<GlobalAlias>(Dst), *GA);
1146     return Error::success();
1147   }
1148   linkIFuncResolver(cast<GlobalIFunc>(Dst), cast<GlobalIFunc>(Src));
1149   return Error::success();
1150 }
1151 
1152 void IRLinker::flushRAUWWorklist() {
1153   for (const auto &Elem : RAUWWorklist) {
1154     GlobalValue *Old;
1155     Value *New;
1156     std::tie(Old, New) = Elem;
1157 
1158     Old->replaceAllUsesWith(New);
1159     Old->eraseFromParent();
1160   }
1161   RAUWWorklist.clear();
1162 }
1163 
1164 void IRLinker::prepareCompileUnitsForImport() {
1165   NamedMDNode *SrcCompileUnits = SrcM->getNamedMetadata("llvm.dbg.cu");
1166   if (!SrcCompileUnits)
1167     return;
1168   // When importing for ThinLTO, prevent importing of types listed on
1169   // the DICompileUnit that we don't need a copy of in the importing
1170   // module. They will be emitted by the originating module.
1171   for (unsigned I = 0, E = SrcCompileUnits->getNumOperands(); I != E; ++I) {
1172     auto *CU = cast<DICompileUnit>(SrcCompileUnits->getOperand(I));
1173     assert(CU && "Expected valid compile unit");
1174     // Enums, macros, and retained types don't need to be listed on the
1175     // imported DICompileUnit. This means they will only be imported
1176     // if reached from the mapped IR.
1177     CU->replaceEnumTypes(nullptr);
1178     CU->replaceMacros(nullptr);
1179     CU->replaceRetainedTypes(nullptr);
1180 
1181     // The original definition (or at least its debug info - if the variable is
1182     // internalized and optimized away) will remain in the source module, so
1183     // there's no need to import them.
1184     // If LLVM ever does more advanced optimizations on global variables
1185     // (removing/localizing write operations, for instance) that can track
1186     // through debug info, this decision may need to be revisited - but do so
1187     // with care when it comes to debug info size. Emitting small CUs containing
1188     // only a few imported entities into every destination module may be very
1189     // size inefficient.
1190     CU->replaceGlobalVariables(nullptr);
1191 
1192     // Imported entities only need to be mapped in if they have local
1193     // scope, as those might correspond to an imported entity inside a
1194     // function being imported (any locally scoped imported entities that
1195     // don't end up referenced by an imported function will not be emitted
1196     // into the object). Imported entities not in a local scope
1197     // (e.g. on the namespace) only need to be emitted by the originating
1198     // module. Create a list of the locally scoped imported entities, and
1199     // replace the source CUs imported entity list with the new list, so
1200     // only those are mapped in.
1201     // FIXME: Locally-scoped imported entities could be moved to the
1202     // functions they are local to instead of listing them on the CU, and
1203     // we would naturally only link in those needed by function importing.
1204     SmallVector<TrackingMDNodeRef, 4> AllImportedModules;
1205     bool ReplaceImportedEntities = false;
1206     for (auto *IE : CU->getImportedEntities()) {
1207       DIScope *Scope = IE->getScope();
1208       assert(Scope && "Invalid Scope encoding!");
1209       if (isa<DILocalScope>(Scope))
1210         AllImportedModules.emplace_back(IE);
1211       else
1212         ReplaceImportedEntities = true;
1213     }
1214     if (ReplaceImportedEntities) {
1215       if (!AllImportedModules.empty())
1216         CU->replaceImportedEntities(MDTuple::get(
1217             CU->getContext(),
1218             SmallVector<Metadata *, 16>(AllImportedModules.begin(),
1219                                         AllImportedModules.end())));
1220       else
1221         // If there were no local scope imported entities, we can map
1222         // the whole list to nullptr.
1223         CU->replaceImportedEntities(nullptr);
1224     }
1225   }
1226 }
1227 
1228 /// Insert all of the named MDNodes in Src into the Dest module.
1229 void IRLinker::linkNamedMDNodes() {
1230   const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1231   for (const NamedMDNode &NMD : SrcM->named_metadata()) {
1232     // Don't link module flags here. Do them separately.
1233     if (&NMD == SrcModFlags)
1234       continue;
1235     // Don't import pseudo probe descriptors here for thinLTO. They will be
1236     // emitted by the originating module.
1237     if (IsPerformingImport && NMD.getName() == PseudoProbeDescMetadataName) {
1238       if (!DstM.getNamedMetadata(NMD.getName()))
1239         emitWarning("Pseudo-probe ignored: source module '" +
1240                     SrcM->getModuleIdentifier() +
1241                     "' is compiled with -fpseudo-probe-for-profiling while "
1242                     "destination module '" +
1243                     DstM.getModuleIdentifier() + "' is not\n");
1244       continue;
1245     }
1246     NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
1247     // Add Src elements into Dest node.
1248     for (const MDNode *Op : NMD.operands())
1249       DestNMD->addOperand(Mapper.mapMDNode(*Op));
1250   }
1251 }
1252 
1253 /// Merge the linker flags in Src into the Dest module.
1254 Error IRLinker::linkModuleFlagsMetadata() {
1255   // If the source module has no module flags, we are done.
1256   const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1257   if (!SrcModFlags)
1258     return Error::success();
1259 
1260   // Check for module flag for updates before do anything.
1261   UpgradeModuleFlags(*SrcM);
1262 
1263   // If the destination module doesn't have module flags yet, then just copy
1264   // over the source module's flags.
1265   NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
1266   if (DstModFlags->getNumOperands() == 0) {
1267     for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1268       DstModFlags->addOperand(SrcModFlags->getOperand(I));
1269 
1270     return Error::success();
1271   }
1272 
1273   // First build a map of the existing module flags and requirements.
1274   DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
1275   SmallSetVector<MDNode *, 16> Requirements;
1276   SmallVector<unsigned, 0> Mins;
1277   DenseSet<MDString *> SeenMin;
1278   for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1279     MDNode *Op = DstModFlags->getOperand(I);
1280     uint64_t Behavior =
1281         mdconst::extract<ConstantInt>(Op->getOperand(0))->getZExtValue();
1282     MDString *ID = cast<MDString>(Op->getOperand(1));
1283 
1284     if (Behavior == Module::Require) {
1285       Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1286     } else {
1287       if (Behavior == Module::Min)
1288         Mins.push_back(I);
1289       Flags[ID] = std::make_pair(Op, I);
1290     }
1291   }
1292 
1293   // Merge in the flags from the source module, and also collect its set of
1294   // requirements.
1295   for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1296     MDNode *SrcOp = SrcModFlags->getOperand(I);
1297     ConstantInt *SrcBehavior =
1298         mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
1299     MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1300     MDNode *DstOp;
1301     unsigned DstIndex;
1302     std::tie(DstOp, DstIndex) = Flags.lookup(ID);
1303     unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1304     SeenMin.insert(ID);
1305 
1306     // If this is a requirement, add it and continue.
1307     if (SrcBehaviorValue == Module::Require) {
1308       // If the destination module does not already have this requirement, add
1309       // it.
1310       if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1311         DstModFlags->addOperand(SrcOp);
1312       }
1313       continue;
1314     }
1315 
1316     // If there is no existing flag with this ID, just add it.
1317     if (!DstOp) {
1318       if (SrcBehaviorValue == Module::Min) {
1319         Mins.push_back(DstModFlags->getNumOperands());
1320         SeenMin.erase(ID);
1321       }
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     auto overrideDstValue = [&]() {
1333       DstModFlags->setOperand(DstIndex, SrcOp);
1334       Flags[ID].first = SrcOp;
1335     };
1336 
1337     // If either flag has override behavior, handle it first.
1338     if (DstBehaviorValue == Module::Override) {
1339       // Diagnose inconsistent flags which both have override behavior.
1340       if (SrcBehaviorValue == Module::Override &&
1341           SrcOp->getOperand(2) != DstOp->getOperand(2))
1342         return stringErr("linking module flags '" + ID->getString() +
1343                          "': IDs have conflicting override values in '" +
1344                          SrcM->getModuleIdentifier() + "' and '" +
1345                          DstM.getModuleIdentifier() + "'");
1346       continue;
1347     } else if (SrcBehaviorValue == Module::Override) {
1348       // Update the destination flag to that of the source.
1349       overrideDstValue();
1350       continue;
1351     }
1352 
1353     // Diagnose inconsistent merge behavior types.
1354     if (SrcBehaviorValue != DstBehaviorValue) {
1355       bool MinAndWarn = (SrcBehaviorValue == Module::Min &&
1356                          DstBehaviorValue == Module::Warning) ||
1357                         (DstBehaviorValue == Module::Min &&
1358                          SrcBehaviorValue == Module::Warning);
1359       bool MaxAndWarn = (SrcBehaviorValue == Module::Max &&
1360                          DstBehaviorValue == Module::Warning) ||
1361                         (DstBehaviorValue == Module::Max &&
1362                          SrcBehaviorValue == Module::Warning);
1363       if (!(MaxAndWarn || MinAndWarn))
1364         return stringErr("linking module flags '" + ID->getString() +
1365                          "': IDs have conflicting behaviors in '" +
1366                          SrcM->getModuleIdentifier() + "' and '" +
1367                          DstM.getModuleIdentifier() + "'");
1368     }
1369 
1370     auto ensureDistinctOp = [&](MDNode *DstValue) {
1371       assert(isa<MDTuple>(DstValue) &&
1372              "Expected MDTuple when appending module flags");
1373       if (DstValue->isDistinct())
1374         return dyn_cast<MDTuple>(DstValue);
1375       ArrayRef<MDOperand> DstOperands = DstValue->operands();
1376       MDTuple *New = MDTuple::getDistinct(
1377           DstM.getContext(),
1378           SmallVector<Metadata *, 4>(DstOperands.begin(), DstOperands.end()));
1379       Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
1380       MDNode *Flag = MDTuple::getDistinct(DstM.getContext(), FlagOps);
1381       DstModFlags->setOperand(DstIndex, Flag);
1382       Flags[ID].first = Flag;
1383       return New;
1384     };
1385 
1386     // Emit a warning if the values differ and either source or destination
1387     // request Warning behavior.
1388     if ((DstBehaviorValue == Module::Warning ||
1389          SrcBehaviorValue == Module::Warning) &&
1390         SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1391       std::string Str;
1392       raw_string_ostream(Str)
1393           << "linking module flags '" << ID->getString()
1394           << "': IDs have conflicting values ('" << *SrcOp->getOperand(2)
1395           << "' from " << SrcM->getModuleIdentifier() << " with '"
1396           << *DstOp->getOperand(2) << "' from " << DstM.getModuleIdentifier()
1397           << ')';
1398       emitWarning(Str);
1399     }
1400 
1401     // Choose the minimum if either source or destination request Min behavior.
1402     if (DstBehaviorValue == Module::Min || SrcBehaviorValue == Module::Min) {
1403       ConstantInt *DstValue =
1404           mdconst::extract<ConstantInt>(DstOp->getOperand(2));
1405       ConstantInt *SrcValue =
1406           mdconst::extract<ConstantInt>(SrcOp->getOperand(2));
1407 
1408       // The resulting flag should have a Min behavior, and contain the minimum
1409       // value from between the source and destination values.
1410       Metadata *FlagOps[] = {
1411           (DstBehaviorValue != Module::Min ? SrcOp : DstOp)->getOperand(0), ID,
1412           (SrcValue->getZExtValue() < DstValue->getZExtValue() ? SrcOp : DstOp)
1413               ->getOperand(2)};
1414       MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1415       DstModFlags->setOperand(DstIndex, Flag);
1416       Flags[ID].first = Flag;
1417       continue;
1418     }
1419 
1420     // Choose the maximum if either source or destination request Max behavior.
1421     if (DstBehaviorValue == Module::Max || SrcBehaviorValue == Module::Max) {
1422       ConstantInt *DstValue =
1423           mdconst::extract<ConstantInt>(DstOp->getOperand(2));
1424       ConstantInt *SrcValue =
1425           mdconst::extract<ConstantInt>(SrcOp->getOperand(2));
1426 
1427       // The resulting flag should have a Max behavior, and contain the maximum
1428       // value from between the source and destination values.
1429       Metadata *FlagOps[] = {
1430           (DstBehaviorValue != Module::Max ? SrcOp : DstOp)->getOperand(0), ID,
1431           (SrcValue->getZExtValue() > DstValue->getZExtValue() ? SrcOp : DstOp)
1432               ->getOperand(2)};
1433       MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1434       DstModFlags->setOperand(DstIndex, Flag);
1435       Flags[ID].first = Flag;
1436       continue;
1437     }
1438 
1439     // Perform the merge for standard behavior types.
1440     switch (SrcBehaviorValue) {
1441     case Module::Require:
1442     case Module::Override:
1443       llvm_unreachable("not possible");
1444     case Module::Error: {
1445       // Emit an error if the values differ.
1446       if (SrcOp->getOperand(2) != DstOp->getOperand(2))
1447         return stringErr("linking module flags '" + ID->getString() +
1448                          "': IDs have conflicting values in '" +
1449                          SrcM->getModuleIdentifier() + "' and '" +
1450                          DstM.getModuleIdentifier() + "'");
1451       continue;
1452     }
1453     case Module::Warning: {
1454       break;
1455     }
1456     case Module::Max: {
1457       break;
1458     }
1459     case Module::Append: {
1460       MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(DstOp->getOperand(2)));
1461       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1462       for (const auto &O : SrcValue->operands())
1463         DstValue->push_back(O);
1464       break;
1465     }
1466     case Module::AppendUnique: {
1467       SmallSetVector<Metadata *, 16> Elts;
1468       MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(DstOp->getOperand(2)));
1469       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1470       Elts.insert(DstValue->op_begin(), DstValue->op_end());
1471       Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
1472       for (auto I = DstValue->getNumOperands(); I < Elts.size(); I++)
1473         DstValue->push_back(Elts[I]);
1474       break;
1475     }
1476     }
1477 
1478   }
1479 
1480   // For the Min behavior, set the value to 0 if either module does not have the
1481   // flag.
1482   for (auto Idx : Mins) {
1483     MDNode *Op = DstModFlags->getOperand(Idx);
1484     MDString *ID = cast<MDString>(Op->getOperand(1));
1485     if (!SeenMin.count(ID)) {
1486       ConstantInt *V = mdconst::extract<ConstantInt>(Op->getOperand(2));
1487       Metadata *FlagOps[] = {
1488           Op->getOperand(0), ID,
1489           ConstantAsMetadata::get(ConstantInt::get(V->getType(), 0))};
1490       DstModFlags->setOperand(Idx, MDNode::get(DstM.getContext(), FlagOps));
1491     }
1492   }
1493 
1494   // Check all of the requirements.
1495   for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1496     MDNode *Requirement = Requirements[I];
1497     MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1498     Metadata *ReqValue = Requirement->getOperand(1);
1499 
1500     MDNode *Op = Flags[Flag].first;
1501     if (!Op || Op->getOperand(2) != ReqValue)
1502       return stringErr("linking module flags '" + Flag->getString() +
1503                        "': does not have the required value");
1504   }
1505   return Error::success();
1506 }
1507 
1508 /// Return InlineAsm adjusted with target-specific directives if required.
1509 /// For ARM and Thumb, we have to add directives to select the appropriate ISA
1510 /// to support mixing module-level inline assembly from ARM and Thumb modules.
1511 static std::string adjustInlineAsm(const std::string &InlineAsm,
1512                                    const Triple &Triple) {
1513   if (Triple.getArch() == Triple::thumb || Triple.getArch() == Triple::thumbeb)
1514     return ".text\n.balign 2\n.thumb\n" + InlineAsm;
1515   if (Triple.getArch() == Triple::arm || Triple.getArch() == Triple::armeb)
1516     return ".text\n.balign 4\n.arm\n" + InlineAsm;
1517   return InlineAsm;
1518 }
1519 
1520 Error IRLinker::run() {
1521   // Ensure metadata materialized before value mapping.
1522   if (SrcM->getMaterializer())
1523     if (Error Err = SrcM->getMaterializer()->materializeMetadata())
1524       return Err;
1525 
1526   // Inherit the target data from the source module if the destination module
1527   // doesn't have one already.
1528   if (DstM.getDataLayout().isDefault())
1529     DstM.setDataLayout(SrcM->getDataLayout());
1530 
1531   // Copy the target triple from the source to dest if the dest's is empty.
1532   if (DstM.getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1533     DstM.setTargetTriple(SrcM->getTargetTriple());
1534 
1535   Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM.getTargetTriple());
1536 
1537   // During CUDA compilation we have to link with the bitcode supplied with
1538   // CUDA. libdevice bitcode either has no data layout set (pre-CUDA-11), or has
1539   // the layout that is different from the one used by LLVM/clang (it does not
1540   // include i128). Issuing a warning is not very helpful as there's not much
1541   // the user can do about it.
1542   bool EnableDLWarning = true;
1543   bool EnableTripleWarning = true;
1544   if (SrcTriple.isNVPTX() && DstTriple.isNVPTX()) {
1545     std::string ModuleId = SrcM->getModuleIdentifier();
1546     StringRef FileName = llvm::sys::path::filename(ModuleId);
1547     bool SrcIsLibDevice =
1548         FileName.startswith("libdevice") && FileName.endswith(".10.bc");
1549     bool SrcHasLibDeviceDL =
1550         (SrcM->getDataLayoutStr().empty() ||
1551          SrcM->getDataLayoutStr() == "e-i64:64-v16:16-v32:32-n16:32:64");
1552     // libdevice bitcode uses nvptx64-nvidia-gpulibs or just
1553     // 'nvptx-unknown-unknown' triple (before CUDA-10.x) and is compatible with
1554     // all NVPTX variants.
1555     bool SrcHasLibDeviceTriple = (SrcTriple.getVendor() == Triple::NVIDIA &&
1556                                   SrcTriple.getOSName() == "gpulibs") ||
1557                                  (SrcTriple.getVendorName() == "unknown" &&
1558                                   SrcTriple.getOSName() == "unknown");
1559     EnableTripleWarning = !(SrcIsLibDevice && SrcHasLibDeviceTriple);
1560     EnableDLWarning = !(SrcIsLibDevice && SrcHasLibDeviceDL);
1561   }
1562 
1563   if (EnableDLWarning && (SrcM->getDataLayout() != DstM.getDataLayout())) {
1564     emitWarning("Linking two modules of different data layouts: '" +
1565                 SrcM->getModuleIdentifier() + "' is '" +
1566                 SrcM->getDataLayoutStr() + "' whereas '" +
1567                 DstM.getModuleIdentifier() + "' is '" +
1568                 DstM.getDataLayoutStr() + "'\n");
1569   }
1570 
1571   if (EnableTripleWarning && !SrcM->getTargetTriple().empty() &&
1572       !SrcTriple.isCompatibleWith(DstTriple))
1573     emitWarning("Linking two modules of different target triples: '" +
1574                 SrcM->getModuleIdentifier() + "' is '" +
1575                 SrcM->getTargetTriple() + "' whereas '" +
1576                 DstM.getModuleIdentifier() + "' is '" + DstM.getTargetTriple() +
1577                 "'\n");
1578 
1579   DstM.setTargetTriple(SrcTriple.merge(DstTriple));
1580 
1581   // Loop over all of the linked values to compute type mappings.
1582   computeTypeMapping();
1583 
1584   std::reverse(Worklist.begin(), Worklist.end());
1585   while (!Worklist.empty()) {
1586     GlobalValue *GV = Worklist.back();
1587     Worklist.pop_back();
1588 
1589     // Already mapped.
1590     if (ValueMap.find(GV) != ValueMap.end() ||
1591         IndirectSymbolValueMap.find(GV) != IndirectSymbolValueMap.end())
1592       continue;
1593 
1594     assert(!GV->isDeclaration());
1595     Mapper.mapValue(*GV);
1596     if (FoundError)
1597       return std::move(*FoundError);
1598     flushRAUWWorklist();
1599   }
1600 
1601   // Note that we are done linking global value bodies. This prevents
1602   // metadata linking from creating new references.
1603   DoneLinkingBodies = true;
1604   Mapper.addFlags(RF_NullMapMissingGlobalValues);
1605 
1606   // Remap all of the named MDNodes in Src into the DstM module. We do this
1607   // after linking GlobalValues so that MDNodes that reference GlobalValues
1608   // are properly remapped.
1609   linkNamedMDNodes();
1610 
1611   if (!IsPerformingImport && !SrcM->getModuleInlineAsm().empty()) {
1612     // Append the module inline asm string.
1613     DstM.appendModuleInlineAsm(adjustInlineAsm(SrcM->getModuleInlineAsm(),
1614                                                SrcTriple));
1615   } else if (IsPerformingImport) {
1616     // Import any symver directives for symbols in DstM.
1617     ModuleSymbolTable::CollectAsmSymvers(*SrcM,
1618                                          [&](StringRef Name, StringRef Alias) {
1619       if (DstM.getNamedValue(Name)) {
1620         SmallString<256> S(".symver ");
1621         S += Name;
1622         S += ", ";
1623         S += Alias;
1624         DstM.appendModuleInlineAsm(S);
1625       }
1626     });
1627   }
1628 
1629   // Reorder the globals just added to the destination module to match their
1630   // original order in the source module.
1631   Module::GlobalListType &Globals = DstM.getGlobalList();
1632   for (GlobalVariable &GV : SrcM->globals()) {
1633     if (GV.hasAppendingLinkage())
1634       continue;
1635     Value *NewValue = Mapper.mapValue(GV);
1636     if (NewValue) {
1637       auto *NewGV = dyn_cast<GlobalVariable>(NewValue->stripPointerCasts());
1638       if (NewGV)
1639         Globals.splice(Globals.end(), Globals, NewGV->getIterator());
1640     }
1641   }
1642 
1643   // Merge the module flags into the DstM module.
1644   return linkModuleFlagsMetadata();
1645 }
1646 
1647 IRMover::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
1648     : ETypes(E), IsPacked(P) {}
1649 
1650 IRMover::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
1651     : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1652 
1653 bool IRMover::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
1654   return IsPacked == That.IsPacked && ETypes == That.ETypes;
1655 }
1656 
1657 bool IRMover::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
1658   return !this->operator==(That);
1659 }
1660 
1661 StructType *IRMover::StructTypeKeyInfo::getEmptyKey() {
1662   return DenseMapInfo<StructType *>::getEmptyKey();
1663 }
1664 
1665 StructType *IRMover::StructTypeKeyInfo::getTombstoneKey() {
1666   return DenseMapInfo<StructType *>::getTombstoneKey();
1667 }
1668 
1669 unsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1670   return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
1671                       Key.IsPacked);
1672 }
1673 
1674 unsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1675   return getHashValue(KeyTy(ST));
1676 }
1677 
1678 bool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1679                                          const StructType *RHS) {
1680   if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1681     return false;
1682   return LHS == KeyTy(RHS);
1683 }
1684 
1685 bool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS,
1686                                          const StructType *RHS) {
1687   if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1688     return LHS == RHS;
1689   return KeyTy(LHS) == KeyTy(RHS);
1690 }
1691 
1692 void IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
1693   assert(!Ty->isOpaque());
1694   NonOpaqueStructTypes.insert(Ty);
1695 }
1696 
1697 void IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
1698   assert(!Ty->isOpaque());
1699   NonOpaqueStructTypes.insert(Ty);
1700   bool Removed = OpaqueStructTypes.erase(Ty);
1701   (void)Removed;
1702   assert(Removed);
1703 }
1704 
1705 void IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
1706   assert(Ty->isOpaque());
1707   OpaqueStructTypes.insert(Ty);
1708 }
1709 
1710 StructType *
1711 IRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
1712                                                 bool IsPacked) {
1713   IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
1714   auto I = NonOpaqueStructTypes.find_as(Key);
1715   return I == NonOpaqueStructTypes.end() ? nullptr : *I;
1716 }
1717 
1718 bool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) {
1719   if (Ty->isOpaque())
1720     return OpaqueStructTypes.count(Ty);
1721   auto I = NonOpaqueStructTypes.find(Ty);
1722   return I == NonOpaqueStructTypes.end() ? false : *I == Ty;
1723 }
1724 
1725 IRMover::IRMover(Module &M) : Composite(M) {
1726   TypeFinder StructTypes;
1727   StructTypes.run(M, /* OnlyNamed */ false);
1728   for (StructType *Ty : StructTypes) {
1729     if (Ty->isOpaque())
1730       IdentifiedStructTypes.addOpaque(Ty);
1731     else
1732       IdentifiedStructTypes.addNonOpaque(Ty);
1733   }
1734   // Self-map metadatas in the destination module. This is needed when
1735   // DebugTypeODRUniquing is enabled on the LLVMContext, since metadata in the
1736   // destination module may be reached from the source module.
1737   for (auto *MD : StructTypes.getVisitedMetadata()) {
1738     SharedMDs[MD].reset(const_cast<MDNode *>(MD));
1739   }
1740 }
1741 
1742 Error IRMover::move(std::unique_ptr<Module> Src,
1743                     ArrayRef<GlobalValue *> ValuesToLink,
1744                     LazyCallback AddLazyFor, bool IsPerformingImport) {
1745   IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes,
1746                        std::move(Src), ValuesToLink, std::move(AddLazyFor),
1747                        IsPerformingImport);
1748   Error E = TheIRLinker.run();
1749   Composite.dropTriviallyDeadConstantArrays();
1750   return E;
1751 }
1752