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