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