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