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