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