8e0904a858bca2f481b215b013f9b0a66472c3e4
[oota-llvm.git] / lib / Linker / LinkModules.cpp
1 //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
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 // This file implements the LLVM module linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Linker/Linker.h"
15 #include "llvm-c/Linker.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/TypeFinder.h"
25 #include "llvm/Transforms/Utils/Cloning.h"
26 using namespace llvm;
27
28 //===----------------------------------------------------------------------===//
29 // TypeMap implementation.
30 //===----------------------------------------------------------------------===//
31
32 namespace {
33 class TypeMapTy : public ValueMapTypeRemapper {
34   /// This is a mapping from a source type to a destination type to use.
35   DenseMap<Type *, Type *> MappedTypes;
36
37   /// When checking to see if two subgraphs are isomorphic, we speculatively
38   /// add types to MappedTypes, but keep track of them here in case we need to
39   /// roll back.
40   SmallVector<Type *, 16> SpeculativeTypes;
41
42   SmallVector<StructType *, 16> SpeculativeDstOpaqueTypes;
43
44   /// This is a list of non-opaque structs in the source module that are mapped
45   /// to an opaque struct in the destination module.
46   SmallVector<StructType *, 16> SrcDefinitionsToResolve;
47
48   /// This is the set of opaque types in the destination modules who are
49   /// getting a body from the source module.
50   SmallPtrSet<StructType *, 16> DstResolvedOpaqueTypes;
51
52 public:
53   TypeMapTy(Linker::IdentifiedStructTypeSet &DstStructTypesSet)
54       : DstStructTypesSet(DstStructTypesSet) {}
55
56   Linker::IdentifiedStructTypeSet &DstStructTypesSet;
57   /// Indicate that the specified type in the destination module is conceptually
58   /// equivalent to the specified type in the source module.
59   void addTypeMapping(Type *DstTy, Type *SrcTy);
60
61   /// Produce a body for an opaque type in the dest module from a type
62   /// definition in the source module.
63   void linkDefinedTypeBodies();
64
65   /// Return the mapped type to use for the specified input type from the
66   /// source module.
67   Type *get(Type *SrcTy);
68   Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
69
70   void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
71
72   FunctionType *get(FunctionType *T) {
73     return cast<FunctionType>(get((Type *)T));
74   }
75
76   /// Dump out the type map for debugging purposes.
77   void dump() const {
78     for (auto &Pair : MappedTypes) {
79       dbgs() << "TypeMap: ";
80       Pair.first->print(dbgs());
81       dbgs() << " => ";
82       Pair.second->print(dbgs());
83       dbgs() << '\n';
84     }
85   }
86
87 private:
88   Type *remapType(Type *SrcTy) override { return get(SrcTy); }
89
90   bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
91 };
92 }
93
94 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
95   assert(SpeculativeTypes.empty());
96   assert(SpeculativeDstOpaqueTypes.empty());
97
98   // Check to see if these types are recursively isomorphic and establish a
99   // mapping between them if so.
100   if (!areTypesIsomorphic(DstTy, SrcTy)) {
101     // Oops, they aren't isomorphic.  Just discard this request by rolling out
102     // any speculative mappings we've established.
103     for (Type *Ty : SpeculativeTypes)
104       MappedTypes.erase(Ty);
105
106     SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
107                                    SpeculativeDstOpaqueTypes.size());
108     for (StructType *Ty : SpeculativeDstOpaqueTypes)
109       DstResolvedOpaqueTypes.erase(Ty);
110   } else {
111     for (Type *Ty : SpeculativeTypes)
112       if (auto *STy = dyn_cast<StructType>(Ty))
113         if (STy->hasName())
114           STy->setName("");
115   }
116   SpeculativeTypes.clear();
117   SpeculativeDstOpaqueTypes.clear();
118 }
119
120 /// Recursively walk this pair of types, returning true if they are isomorphic,
121 /// false if they are not.
122 bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
123   // Two types with differing kinds are clearly not isomorphic.
124   if (DstTy->getTypeID() != SrcTy->getTypeID())
125     return false;
126
127   // If we have an entry in the MappedTypes table, then we have our answer.
128   Type *&Entry = MappedTypes[SrcTy];
129   if (Entry)
130     return Entry == DstTy;
131
132   // Two identical types are clearly isomorphic.  Remember this
133   // non-speculatively.
134   if (DstTy == SrcTy) {
135     Entry = DstTy;
136     return true;
137   }
138
139   // Okay, we have two types with identical kinds that we haven't seen before.
140
141   // If this is an opaque struct type, special case it.
142   if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
143     // Mapping an opaque type to any struct, just keep the dest struct.
144     if (SSTy->isOpaque()) {
145       Entry = DstTy;
146       SpeculativeTypes.push_back(SrcTy);
147       return true;
148     }
149
150     // Mapping a non-opaque source type to an opaque dest.  If this is the first
151     // type that we're mapping onto this destination type then we succeed.  Keep
152     // the dest, but fill it in later. If this is the second (different) type
153     // that we're trying to map onto the same opaque type then we fail.
154     if (cast<StructType>(DstTy)->isOpaque()) {
155       // We can only map one source type onto the opaque destination type.
156       if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
157         return false;
158       SrcDefinitionsToResolve.push_back(SSTy);
159       SpeculativeTypes.push_back(SrcTy);
160       SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
161       Entry = DstTy;
162       return true;
163     }
164   }
165
166   // If the number of subtypes disagree between the two types, then we fail.
167   if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
168     return false;
169
170   // Fail if any of the extra properties (e.g. array size) of the type disagree.
171   if (isa<IntegerType>(DstTy))
172     return false; // bitwidth disagrees.
173   if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
174     if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
175       return false;
176
177   } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
178     if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
179       return false;
180   } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
181     StructType *SSTy = cast<StructType>(SrcTy);
182     if (DSTy->isLiteral() != SSTy->isLiteral() ||
183         DSTy->isPacked() != SSTy->isPacked())
184       return false;
185   } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
186     if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
187       return false;
188   } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
189     if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
190       return false;
191   }
192
193   // Otherwise, we speculate that these two types will line up and recursively
194   // check the subelements.
195   Entry = DstTy;
196   SpeculativeTypes.push_back(SrcTy);
197
198   for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
199     if (!areTypesIsomorphic(DstTy->getContainedType(I),
200                             SrcTy->getContainedType(I)))
201       return false;
202
203   // If everything seems to have lined up, then everything is great.
204   return true;
205 }
206
207 void TypeMapTy::linkDefinedTypeBodies() {
208   SmallVector<Type *, 16> Elements;
209   for (StructType *SrcSTy : SrcDefinitionsToResolve) {
210     StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
211     assert(DstSTy->isOpaque());
212
213     // Map the body of the source type over to a new body for the dest type.
214     Elements.resize(SrcSTy->getNumElements());
215     for (unsigned I = 0, E = Elements.size(); I != E; ++I)
216       Elements[I] = get(SrcSTy->getElementType(I));
217
218     DstSTy->setBody(Elements, SrcSTy->isPacked());
219     DstStructTypesSet.switchToNonOpaque(DstSTy);
220   }
221   SrcDefinitionsToResolve.clear();
222   DstResolvedOpaqueTypes.clear();
223 }
224
225 void TypeMapTy::finishType(StructType *DTy, StructType *STy,
226                            ArrayRef<Type *> ETypes) {
227   DTy->setBody(ETypes, STy->isPacked());
228
229   // Steal STy's name.
230   if (STy->hasName()) {
231     SmallString<16> TmpName = STy->getName();
232     STy->setName("");
233     DTy->setName(TmpName);
234   }
235
236   DstStructTypesSet.addNonOpaque(DTy);
237 }
238
239 Type *TypeMapTy::get(Type *Ty) {
240   SmallPtrSet<StructType *, 8> Visited;
241   return get(Ty, Visited);
242 }
243
244 Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
245   // If we already have an entry for this type, return it.
246   Type **Entry = &MappedTypes[Ty];
247   if (*Entry)
248     return *Entry;
249
250   // These are types that LLVM itself will unique.
251   bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
252
253 #ifndef NDEBUG
254   if (!IsUniqued) {
255     for (auto &Pair : MappedTypes) {
256       assert(!(Pair.first != Ty && Pair.second == Ty) &&
257              "mapping to a source type");
258     }
259   }
260 #endif
261
262   if (!IsUniqued && !Visited.insert(cast<StructType>(Ty)).second) {
263     StructType *DTy = StructType::create(Ty->getContext());
264     return *Entry = DTy;
265   }
266
267   // If this is not a recursive type, then just map all of the elements and
268   // then rebuild the type from inside out.
269   SmallVector<Type *, 4> ElementTypes;
270
271   // If there are no element types to map, then the type is itself.  This is
272   // true for the anonymous {} struct, things like 'float', integers, etc.
273   if (Ty->getNumContainedTypes() == 0 && IsUniqued)
274     return *Entry = Ty;
275
276   // Remap all of the elements, keeping track of whether any of them change.
277   bool AnyChange = false;
278   ElementTypes.resize(Ty->getNumContainedTypes());
279   for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
280     ElementTypes[I] = get(Ty->getContainedType(I), Visited);
281     AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
282   }
283
284   // If we found our type while recursively processing stuff, just use it.
285   Entry = &MappedTypes[Ty];
286   if (*Entry) {
287     if (auto *DTy = dyn_cast<StructType>(*Entry)) {
288       if (DTy->isOpaque()) {
289         auto *STy = cast<StructType>(Ty);
290         finishType(DTy, STy, ElementTypes);
291       }
292     }
293     return *Entry;
294   }
295
296   // If all of the element types mapped directly over and the type is not
297   // a nomed struct, then the type is usable as-is.
298   if (!AnyChange && IsUniqued)
299     return *Entry = Ty;
300
301   // Otherwise, rebuild a modified type.
302   switch (Ty->getTypeID()) {
303   default:
304     llvm_unreachable("unknown derived type to remap");
305   case Type::ArrayTyID:
306     return *Entry = ArrayType::get(ElementTypes[0],
307                                    cast<ArrayType>(Ty)->getNumElements());
308   case Type::VectorTyID:
309     return *Entry = VectorType::get(ElementTypes[0],
310                                     cast<VectorType>(Ty)->getNumElements());
311   case Type::PointerTyID:
312     return *Entry = PointerType::get(ElementTypes[0],
313                                      cast<PointerType>(Ty)->getAddressSpace());
314   case Type::FunctionTyID:
315     return *Entry = FunctionType::get(ElementTypes[0],
316                                       makeArrayRef(ElementTypes).slice(1),
317                                       cast<FunctionType>(Ty)->isVarArg());
318   case Type::StructTyID: {
319     auto *STy = cast<StructType>(Ty);
320     bool IsPacked = STy->isPacked();
321     if (IsUniqued)
322       return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
323
324     // If the type is opaque, we can just use it directly.
325     if (STy->isOpaque()) {
326       DstStructTypesSet.addOpaque(STy);
327       return *Entry = Ty;
328     }
329
330     if (StructType *OldT =
331             DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
332       STy->setName("");
333       return *Entry = OldT;
334     }
335
336     if (!AnyChange) {
337       DstStructTypesSet.addNonOpaque(STy);
338       return *Entry = Ty;
339     }
340
341     StructType *DTy = StructType::create(Ty->getContext());
342     finishType(DTy, STy, ElementTypes);
343     return *Entry = DTy;
344   }
345   }
346 }
347
348 //===----------------------------------------------------------------------===//
349 // ModuleLinker implementation.
350 //===----------------------------------------------------------------------===//
351
352 namespace {
353 class ModuleLinker;
354
355 /// Creates prototypes for functions that are lazily linked on the fly. This
356 /// speeds up linking for modules with many/ lazily linked functions of which
357 /// few get used.
358 class ValueMaterializerTy final : public ValueMaterializer {
359   ModuleLinker *ModLinker;
360
361 public:
362   ValueMaterializerTy(ModuleLinker *ModLinker) : ModLinker(ModLinker) {}
363
364   Value *materializeDeclFor(Value *V) override;
365   void materializeInitFor(GlobalValue *New, GlobalValue *Old) override;
366 };
367
368 class LinkDiagnosticInfo : public DiagnosticInfo {
369   const Twine &Msg;
370
371 public:
372   LinkDiagnosticInfo(DiagnosticSeverity Severity, const Twine &Msg);
373   void print(DiagnosticPrinter &DP) const override;
374 };
375 LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
376                                        const Twine &Msg)
377     : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
378 void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
379
380 /// This is an implementation class for the LinkModules function, which is the
381 /// entrypoint for this file.
382 class ModuleLinker {
383   Module &DstM;
384   Module &SrcM;
385
386   TypeMapTy TypeMap;
387   ValueMaterializerTy ValMaterializer;
388
389   /// Mapping of values from what they used to be in Src, to what they are now
390   /// in DstM.  ValueToValueMapTy is a ValueMap, which involves some overhead
391   /// due to the use of Value handles which the Linker doesn't actually need,
392   /// but this allows us to reuse the ValueMapper code.
393   ValueToValueMapTy ValueMap;
394
395   SetVector<GlobalValue *> ValuesToLink;
396
397   DiagnosticHandlerFunction DiagnosticHandler;
398
399   /// For symbol clashes, prefer those from Src.
400   unsigned Flags;
401
402   /// Function index passed into ModuleLinker for using in function
403   /// importing/exporting handling.
404   const FunctionInfoIndex *ImportIndex;
405
406   /// Function to import from source module, all other functions are
407   /// imported as declarations instead of definitions.
408   DenseSet<const GlobalValue *> *ImportFunction;
409
410   /// Set to true if the given FunctionInfoIndex contains any functions
411   /// from this source module, in which case we must conservatively assume
412   /// that any of its functions may be imported into another module
413   /// as part of a different backend compilation process.
414   bool HasExportedFunctions = false;
415
416   /// Set to true when all global value body linking is complete (including
417   /// lazy linking). Used to prevent metadata linking from creating new
418   /// references.
419   bool DoneLinkingBodies = false;
420
421   bool HasError = false;
422
423 public:
424   ModuleLinker(Module &DstM, Linker::IdentifiedStructTypeSet &Set, Module &SrcM,
425                DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags,
426                const FunctionInfoIndex *Index = nullptr,
427                DenseSet<const GlobalValue *> *FunctionsToImport = nullptr)
428       : DstM(DstM), SrcM(SrcM), TypeMap(Set), ValMaterializer(this),
429         DiagnosticHandler(DiagnosticHandler), Flags(Flags), ImportIndex(Index),
430         ImportFunction(FunctionsToImport) {
431     assert((ImportIndex || !ImportFunction) &&
432            "Expect a FunctionInfoIndex when importing");
433     // If we have a FunctionInfoIndex but no function to import,
434     // then this is the primary module being compiled in a ThinLTO
435     // backend compilation, and we need to see if it has functions that
436     // may be exported to another backend compilation.
437     if (ImportIndex && !ImportFunction)
438       HasExportedFunctions = ImportIndex->hasExportedFunctions(SrcM);
439   }
440
441   bool run();
442   Value *materializeDeclFor(Value *V);
443   void materializeInitFor(GlobalValue *New, GlobalValue *Old);
444
445 private:
446   bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; }
447   bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; }
448   bool shouldInternalizeLinkedSymbols() {
449     return Flags & Linker::InternalizeLinkedSymbols;
450   }
451
452   /// Handles cloning of a global values from the source module into
453   /// the destination module, including setting the attributes and visibility.
454   GlobalValue *copyGlobalValueProto(const GlobalValue *SGV,
455                                     const GlobalValue *DGV, bool ForDefinition);
456
457   /// Check if we should promote the given local value to global scope.
458   bool doPromoteLocalToGlobal(const GlobalValue *SGV);
459
460   bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
461                             const GlobalValue &Src);
462
463   /// Helper method for setting a message and returning an error code.
464   bool emitError(const Twine &Message) {
465     DiagnosticHandler(LinkDiagnosticInfo(DS_Error, Message));
466     HasError = true;
467     return true;
468   }
469
470   void emitWarning(const Twine &Message) {
471     DiagnosticHandler(LinkDiagnosticInfo(DS_Warning, Message));
472   }
473
474   bool getComdatLeader(Module &M, StringRef ComdatName,
475                        const GlobalVariable *&GVar);
476   bool computeResultingSelectionKind(StringRef ComdatName,
477                                      Comdat::SelectionKind Src,
478                                      Comdat::SelectionKind Dst,
479                                      Comdat::SelectionKind &Result,
480                                      bool &LinkFromSrc);
481   std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
482       ComdatsChosen;
483   bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
484                        bool &LinkFromSrc);
485   // Keep track of the global value members of each comdat in source.
486   DenseMap<const Comdat *, std::vector<GlobalValue *>> ComdatMembers;
487
488   /// Given a global in the source module, return the global in the
489   /// destination module that is being linked to, if any.
490   GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
491     // If the source has no name it can't link.  If it has local linkage,
492     // there is no name match-up going on.
493     if (!SrcGV->hasName() || GlobalValue::isLocalLinkage(getLinkage(SrcGV)))
494       return nullptr;
495
496     // Otherwise see if we have a match in the destination module's symtab.
497     GlobalValue *DGV = DstM.getNamedValue(getName(SrcGV));
498     if (!DGV)
499       return nullptr;
500
501     // If we found a global with the same name in the dest module, but it has
502     // internal linkage, we are really not doing any linkage here.
503     if (DGV->hasLocalLinkage())
504       return nullptr;
505
506     // Otherwise, we do in fact link to the destination global.
507     return DGV;
508   }
509
510   void computeTypeMapping();
511
512   bool linkIfNeeded(GlobalValue &GV);
513   Constant *linkAppendingVarProto(GlobalVariable *DstGV,
514                                   const GlobalVariable *SrcGV);
515
516   Constant *linkGlobalValueProto(GlobalValue *GV);
517   bool linkModuleFlagsMetadata();
518
519   void linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src);
520   bool linkFunctionBody(Function &Dst, Function &Src);
521   void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src);
522   bool linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src);
523
524   /// Functions that take care of cloning a specific global value type
525   /// into the destination module.
526   GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar);
527   Function *copyFunctionProto(const Function *SF);
528   GlobalValue *copyGlobalAliasProto(const GlobalAlias *SGA);
529
530   /// Helper methods to check if we are importing from or potentially
531   /// exporting from the current source module.
532   bool isPerformingImport() { return ImportFunction != nullptr; }
533   bool isModuleExporting() { return HasExportedFunctions; }
534
535   /// If we are importing from the source module, checks if we should
536   /// import SGV as a definition, otherwise import as a declaration.
537   bool doImportAsDefinition(const GlobalValue *SGV);
538
539   /// Get the name for SGV that should be used in the linked destination
540   /// module. Specifically, this handles the case where we need to rename
541   /// a local that is being promoted to global scope.
542   std::string getName(const GlobalValue *SGV);
543
544   /// Get the new linkage for SGV that should be used in the linked destination
545   /// module. Specifically, for ThinLTO importing or exporting it may need
546   /// to be adjusted.
547   GlobalValue::LinkageTypes getLinkage(const GlobalValue *SGV);
548
549   /// Copies the necessary global value attributes and name from the source
550   /// to the newly cloned global value.
551   void copyGVAttributes(GlobalValue *NewGV, const GlobalValue *SrcGV);
552
553   /// Updates the visibility for the new global cloned from the source
554   /// and, if applicable, linked with an existing destination global.
555   /// Handles visibility change required for promoted locals.
556   void setVisibility(GlobalValue *NewGV, const GlobalValue *SGV,
557                      const GlobalValue *DGV = nullptr);
558
559   void linkNamedMDNodes();
560 };
561 }
562
563 /// The LLVM SymbolTable class autorenames globals that conflict in the symbol
564 /// table. This is good for all clients except for us. Go through the trouble
565 /// to force this back.
566 static void forceRenaming(GlobalValue *GV, StringRef Name) {
567   // If the global doesn't force its name or if it already has the right name,
568   // there is nothing for us to do.
569   // Note that any required local to global promotion should already be done,
570   // so promoted locals will not skip this handling as their linkage is no
571   // longer local.
572   if (GV->hasLocalLinkage() || GV->getName() == Name)
573     return;
574
575   Module *M = GV->getParent();
576
577   // If there is a conflict, rename the conflict.
578   if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
579     GV->takeName(ConflictGV);
580     ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
581     assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
582   } else {
583     GV->setName(Name); // Force the name back
584   }
585 }
586
587 /// copy additional attributes (those not needed to construct a GlobalValue)
588 /// from the SrcGV to the DestGV.
589 void ModuleLinker::copyGVAttributes(GlobalValue *NewGV,
590                                     const GlobalValue *SrcGV) {
591   NewGV->copyAttributesFrom(SrcGV);
592   forceRenaming(NewGV, getName(SrcGV));
593 }
594
595 bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) {
596   if (!isPerformingImport())
597     return false;
598   auto *GA = dyn_cast<GlobalAlias>(SGV);
599   if (GA) {
600     if (GA->hasWeakAnyLinkage())
601       return false;
602     const GlobalObject *GO = GA->getBaseObject();
603     if (!GO->hasLinkOnceODRLinkage())
604       return false;
605     return doImportAsDefinition(GO);
606   }
607   // Always import GlobalVariable definitions, except for the special
608   // case of WeakAny which are imported as ExternalWeak declarations
609   // (see comments in ModuleLinker::getLinkage). The linkage changes
610   // described in ModuleLinker::getLinkage ensure the correct behavior (e.g.
611   // global variables with external linkage are transformed to
612   // available_externally definitions, which are ultimately turned into
613   // declarations after the EliminateAvailableExternally pass).
614   if (isa<GlobalVariable>(SGV) && !SGV->isDeclaration() &&
615       !SGV->hasWeakAnyLinkage())
616     return true;
617   // Only import the function requested for importing.
618   auto *SF = dyn_cast<Function>(SGV);
619   if (SF && ImportFunction->count(SF))
620     return true;
621   // Otherwise no.
622   return false;
623 }
624
625 bool ModuleLinker::doPromoteLocalToGlobal(const GlobalValue *SGV) {
626   assert(SGV->hasLocalLinkage());
627   // Both the imported references and the original local variable must
628   // be promoted.
629   if (!isPerformingImport() && !isModuleExporting())
630     return false;
631
632   // Local const variables never need to be promoted unless they are address
633   // taken. The imported uses can simply use the clone created in this module.
634   // For now we are conservative in determining which variables are not
635   // address taken by checking the unnamed addr flag. To be more aggressive,
636   // the address taken information must be checked earlier during parsing
637   // of the module and recorded in the function index for use when importing
638   // from that module.
639   auto *GVar = dyn_cast<GlobalVariable>(SGV);
640   if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr())
641     return false;
642
643   // Eventually we only need to promote functions in the exporting module that
644   // are referenced by a potentially exported function (i.e. one that is in the
645   // function index).
646   return true;
647 }
648
649 std::string ModuleLinker::getName(const GlobalValue *SGV) {
650   // For locals that must be promoted to global scope, ensure that
651   // the promoted name uniquely identifies the copy in the original module,
652   // using the ID assigned during combined index creation. When importing,
653   // we rename all locals (not just those that are promoted) in order to
654   // avoid naming conflicts between locals imported from different modules.
655   if (SGV->hasLocalLinkage() &&
656       (doPromoteLocalToGlobal(SGV) || isPerformingImport()))
657     return FunctionInfoIndex::getGlobalNameForLocal(
658         SGV->getName(),
659         ImportIndex->getModuleId(SGV->getParent()->getModuleIdentifier()));
660   return SGV->getName();
661 }
662
663 GlobalValue::LinkageTypes ModuleLinker::getLinkage(const GlobalValue *SGV) {
664   // Any local variable that is referenced by an exported function needs
665   // to be promoted to global scope. Since we don't currently know which
666   // functions reference which local variables/functions, we must treat
667   // all as potentially exported if this module is exporting anything.
668   if (isModuleExporting()) {
669     if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
670       return GlobalValue::ExternalLinkage;
671     return SGV->getLinkage();
672   }
673
674   // Otherwise, if we aren't importing, no linkage change is needed.
675   if (!isPerformingImport())
676     return SGV->getLinkage();
677
678   switch (SGV->getLinkage()) {
679   case GlobalValue::ExternalLinkage:
680     // External defnitions are converted to available_externally
681     // definitions upon import, so that they are available for inlining
682     // and/or optimization, but are turned into declarations later
683     // during the EliminateAvailableExternally pass.
684     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
685       return GlobalValue::AvailableExternallyLinkage;
686     // An imported external declaration stays external.
687     return SGV->getLinkage();
688
689   case GlobalValue::AvailableExternallyLinkage:
690     // An imported available_externally definition converts
691     // to external if imported as a declaration.
692     if (!doImportAsDefinition(SGV))
693       return GlobalValue::ExternalLinkage;
694     // An imported available_externally declaration stays that way.
695     return SGV->getLinkage();
696
697   case GlobalValue::LinkOnceAnyLinkage:
698   case GlobalValue::LinkOnceODRLinkage:
699     // These both stay the same when importing the definition.
700     // The ThinLTO pass will eventually force-import their definitions.
701     return SGV->getLinkage();
702
703   case GlobalValue::WeakAnyLinkage:
704     // Can't import weak_any definitions correctly, or we might change the
705     // program semantics, since the linker will pick the first weak_any
706     // definition and importing would change the order they are seen by the
707     // linker. The module linking caller needs to enforce this.
708     assert(!doImportAsDefinition(SGV));
709     // If imported as a declaration, it becomes external_weak.
710     return GlobalValue::ExternalWeakLinkage;
711
712   case GlobalValue::WeakODRLinkage:
713     // For weak_odr linkage, there is a guarantee that all copies will be
714     // equivalent, so the issue described above for weak_any does not exist,
715     // and the definition can be imported. It can be treated similarly
716     // to an imported externally visible global value.
717     if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
718       return GlobalValue::AvailableExternallyLinkage;
719     else
720       return GlobalValue::ExternalLinkage;
721
722   case GlobalValue::AppendingLinkage:
723     // It would be incorrect to import an appending linkage variable,
724     // since it would cause global constructors/destructors to be
725     // executed multiple times. This should have already been handled
726     // by linkIfNeeded, and we will assert in shouldLinkFromSource
727     // if we try to import, so we simply return AppendingLinkage here
728     // as this helper is called more widely in getLinkedToGlobal.
729     return GlobalValue::AppendingLinkage;
730
731   case GlobalValue::InternalLinkage:
732   case GlobalValue::PrivateLinkage:
733     // If we are promoting the local to global scope, it is handled
734     // similarly to a normal externally visible global.
735     if (doPromoteLocalToGlobal(SGV)) {
736       if (doImportAsDefinition(SGV) && !dyn_cast<GlobalAlias>(SGV))
737         return GlobalValue::AvailableExternallyLinkage;
738       else
739         return GlobalValue::ExternalLinkage;
740     }
741     // A non-promoted imported local definition stays local.
742     // The ThinLTO pass will eventually force-import their definitions.
743     return SGV->getLinkage();
744
745   case GlobalValue::ExternalWeakLinkage:
746     // External weak doesn't apply to definitions, must be a declaration.
747     assert(!doImportAsDefinition(SGV));
748     // Linkage stays external_weak.
749     return SGV->getLinkage();
750
751   case GlobalValue::CommonLinkage:
752     // Linkage stays common on definitions.
753     // The ThinLTO pass will eventually force-import their definitions.
754     return SGV->getLinkage();
755   }
756
757   llvm_unreachable("unknown linkage type");
758 }
759
760 /// Loop through the global variables in the src module and merge them into the
761 /// dest module.
762 GlobalVariable *
763 ModuleLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
764   // No linking to be performed or linking from the source: simply create an
765   // identical version of the symbol over in the dest module... the
766   // initializer will be filled in later by LinkGlobalInits.
767   GlobalVariable *NewDGV =
768       new GlobalVariable(DstM, TypeMap.get(SGVar->getType()->getElementType()),
769                          SGVar->isConstant(), GlobalValue::ExternalLinkage,
770                          /*init*/ nullptr, getName(SGVar),
771                          /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
772                          SGVar->getType()->getAddressSpace());
773
774   return NewDGV;
775 }
776
777 /// Link the function in the source module into the destination module if
778 /// needed, setting up mapping information.
779 Function *ModuleLinker::copyFunctionProto(const Function *SF) {
780   // If there is no linkage to be performed or we are linking from the source,
781   // bring SF over.
782   return Function::Create(TypeMap.get(SF->getFunctionType()),
783                           GlobalValue::ExternalLinkage, getName(SF), &DstM);
784 }
785
786 /// Set up prototypes for any aliases that come over from the source module.
787 GlobalValue *ModuleLinker::copyGlobalAliasProto(const GlobalAlias *SGA) {
788   // If there is no linkage to be performed or we're linking from the source,
789   // bring over SGA.
790   auto *Ty = TypeMap.get(SGA->getValueType());
791   return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
792                              GlobalValue::ExternalLinkage, getName(SGA), &DstM);
793 }
794
795 static GlobalValue::VisibilityTypes
796 getMinVisibility(GlobalValue::VisibilityTypes A,
797                  GlobalValue::VisibilityTypes B) {
798   if (A == GlobalValue::HiddenVisibility || B == GlobalValue::HiddenVisibility)
799     return GlobalValue::HiddenVisibility;
800   if (A == GlobalValue::ProtectedVisibility ||
801       B == GlobalValue::ProtectedVisibility)
802     return GlobalValue::ProtectedVisibility;
803   return GlobalValue::DefaultVisibility;
804 }
805
806 void ModuleLinker::setVisibility(GlobalValue *NewGV, const GlobalValue *SGV,
807                                  const GlobalValue *DGV) {
808   GlobalValue::VisibilityTypes Visibility = SGV->getVisibility();
809   if (DGV)
810     Visibility = getMinVisibility(DGV->getVisibility(), Visibility);
811   // For promoted locals, mark them hidden so that they can later be
812   // stripped from the symbol table to reduce bloat.
813   if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
814     Visibility = GlobalValue::HiddenVisibility;
815   NewGV->setVisibility(Visibility);
816 }
817
818 GlobalValue *ModuleLinker::copyGlobalValueProto(const GlobalValue *SGV,
819                                                 const GlobalValue *DGV,
820                                                 bool ForDefinition) {
821   GlobalValue *NewGV;
822   if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) {
823     NewGV = copyGlobalVariableProto(SGVar);
824   } else if (auto *SF = dyn_cast<Function>(SGV)) {
825     NewGV = copyFunctionProto(SF);
826   } else {
827     if (ForDefinition)
828       NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV));
829     else
830       NewGV = new GlobalVariable(
831           DstM, TypeMap.get(SGV->getType()->getElementType()),
832           /*isConstant*/ false, GlobalValue::ExternalLinkage,
833           /*init*/ nullptr, getName(SGV),
834           /*insertbefore*/ nullptr, SGV->getThreadLocalMode(),
835           SGV->getType()->getAddressSpace());
836   }
837
838   if (ForDefinition)
839     NewGV->setLinkage(getLinkage(SGV));
840   else if (SGV->hasAvailableExternallyLinkage() || SGV->hasWeakLinkage() ||
841            SGV->hasLinkOnceLinkage())
842     NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
843
844   copyGVAttributes(NewGV, SGV);
845   setVisibility(NewGV, SGV, DGV);
846   return NewGV;
847 }
848
849 Value *ValueMaterializerTy::materializeDeclFor(Value *V) {
850   return ModLinker->materializeDeclFor(V);
851 }
852
853 Value *ModuleLinker::materializeDeclFor(Value *V) {
854   auto *SGV = dyn_cast<GlobalValue>(V);
855   if (!SGV)
856     return nullptr;
857
858   return linkGlobalValueProto(SGV);
859 }
860
861 void ValueMaterializerTy::materializeInitFor(GlobalValue *New,
862                                              GlobalValue *Old) {
863   return ModLinker->materializeInitFor(New, Old);
864 }
865
866 static bool shouldLazyLink(const GlobalValue &GV) {
867   return GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
868          GV.hasAvailableExternallyLinkage();
869 }
870
871 void ModuleLinker::materializeInitFor(GlobalValue *New, GlobalValue *Old) {
872   if (auto *F = dyn_cast<Function>(New)) {
873     if (!F->isDeclaration())
874       return;
875   } else if (auto *V = dyn_cast<GlobalVariable>(New)) {
876     if (V->hasInitializer())
877       return;
878   } else {
879     auto *A = cast<GlobalAlias>(New);
880     if (A->getAliasee())
881       return;
882   }
883
884   if (Old->isDeclaration())
885     return;
886
887   if (isPerformingImport() && !doImportAsDefinition(Old))
888     return;
889
890   if (!ValuesToLink.count(Old) && !shouldLazyLink(*Old))
891     return;
892
893   linkGlobalValueBody(*New, *Old);
894 }
895
896 bool ModuleLinker::getComdatLeader(Module &M, StringRef ComdatName,
897                                    const GlobalVariable *&GVar) {
898   const GlobalValue *GVal = M.getNamedValue(ComdatName);
899   if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
900     GVal = GA->getBaseObject();
901     if (!GVal)
902       // We cannot resolve the size of the aliasee yet.
903       return emitError("Linking COMDATs named '" + ComdatName +
904                        "': COMDAT key involves incomputable alias size.");
905   }
906
907   GVar = dyn_cast_or_null<GlobalVariable>(GVal);
908   if (!GVar)
909     return emitError(
910         "Linking COMDATs named '" + ComdatName +
911         "': GlobalVariable required for data dependent selection!");
912
913   return false;
914 }
915
916 bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
917                                                  Comdat::SelectionKind Src,
918                                                  Comdat::SelectionKind Dst,
919                                                  Comdat::SelectionKind &Result,
920                                                  bool &LinkFromSrc) {
921   // The ability to mix Comdat::SelectionKind::Any with
922   // Comdat::SelectionKind::Largest is a behavior that comes from COFF.
923   bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
924                          Dst == Comdat::SelectionKind::Largest;
925   bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
926                          Src == Comdat::SelectionKind::Largest;
927   if (DstAnyOrLargest && SrcAnyOrLargest) {
928     if (Dst == Comdat::SelectionKind::Largest ||
929         Src == Comdat::SelectionKind::Largest)
930       Result = Comdat::SelectionKind::Largest;
931     else
932       Result = Comdat::SelectionKind::Any;
933   } else if (Src == Dst) {
934     Result = Dst;
935   } else {
936     return emitError("Linking COMDATs named '" + ComdatName +
937                      "': invalid selection kinds!");
938   }
939
940   switch (Result) {
941   case Comdat::SelectionKind::Any:
942     // Go with Dst.
943     LinkFromSrc = false;
944     break;
945   case Comdat::SelectionKind::NoDuplicates:
946     return emitError("Linking COMDATs named '" + ComdatName +
947                      "': noduplicates has been violated!");
948   case Comdat::SelectionKind::ExactMatch:
949   case Comdat::SelectionKind::Largest:
950   case Comdat::SelectionKind::SameSize: {
951     const GlobalVariable *DstGV;
952     const GlobalVariable *SrcGV;
953     if (getComdatLeader(DstM, ComdatName, DstGV) ||
954         getComdatLeader(SrcM, ComdatName, SrcGV))
955       return true;
956
957     const DataLayout &DstDL = DstM.getDataLayout();
958     const DataLayout &SrcDL = SrcM.getDataLayout();
959     uint64_t DstSize =
960         DstDL.getTypeAllocSize(DstGV->getType()->getPointerElementType());
961     uint64_t SrcSize =
962         SrcDL.getTypeAllocSize(SrcGV->getType()->getPointerElementType());
963     if (Result == Comdat::SelectionKind::ExactMatch) {
964       if (SrcGV->getInitializer() != DstGV->getInitializer())
965         return emitError("Linking COMDATs named '" + ComdatName +
966                          "': ExactMatch violated!");
967       LinkFromSrc = false;
968     } else if (Result == Comdat::SelectionKind::Largest) {
969       LinkFromSrc = SrcSize > DstSize;
970     } else if (Result == Comdat::SelectionKind::SameSize) {
971       if (SrcSize != DstSize)
972         return emitError("Linking COMDATs named '" + ComdatName +
973                          "': SameSize violated!");
974       LinkFromSrc = false;
975     } else {
976       llvm_unreachable("unknown selection kind");
977     }
978     break;
979   }
980   }
981
982   return false;
983 }
984
985 bool ModuleLinker::getComdatResult(const Comdat *SrcC,
986                                    Comdat::SelectionKind &Result,
987                                    bool &LinkFromSrc) {
988   Comdat::SelectionKind SSK = SrcC->getSelectionKind();
989   StringRef ComdatName = SrcC->getName();
990   Module::ComdatSymTabType &ComdatSymTab = DstM.getComdatSymbolTable();
991   Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
992
993   if (DstCI == ComdatSymTab.end()) {
994     // Use the comdat if it is only available in one of the modules.
995     LinkFromSrc = true;
996     Result = SSK;
997     return false;
998   }
999
1000   const Comdat *DstC = &DstCI->second;
1001   Comdat::SelectionKind DSK = DstC->getSelectionKind();
1002   return computeResultingSelectionKind(ComdatName, SSK, DSK, Result,
1003                                        LinkFromSrc);
1004 }
1005
1006 bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
1007                                         const GlobalValue &Dest,
1008                                         const GlobalValue &Src) {
1009   // Should we unconditionally use the Src?
1010   if (shouldOverrideFromSrc()) {
1011     LinkFromSrc = true;
1012     return false;
1013   }
1014
1015   // We always have to add Src if it has appending linkage.
1016   if (Src.hasAppendingLinkage()) {
1017     // Should have prevented importing for appending linkage in linkIfNeeded.
1018     assert(!isPerformingImport());
1019     LinkFromSrc = true;
1020     return false;
1021   }
1022
1023   bool SrcIsDeclaration = Src.isDeclarationForLinker();
1024   bool DestIsDeclaration = Dest.isDeclarationForLinker();
1025
1026   if (isPerformingImport()) {
1027     if (isa<Function>(&Src)) {
1028       // For functions, LinkFromSrc iff this is the function requested
1029       // for importing. For variables, decide below normally.
1030       LinkFromSrc = ImportFunction->count(&Src);
1031       return false;
1032     }
1033
1034     // Check if this is an alias with an already existing definition
1035     // in Dest, which must have come from a prior importing pass from
1036     // the same Src module. Unlike imported function and variable
1037     // definitions, which are imported as available_externally and are
1038     // not definitions for the linker, that is not a valid linkage for
1039     // imported aliases which must be definitions. Simply use the existing
1040     // Dest copy.
1041     if (isa<GlobalAlias>(&Src) && !DestIsDeclaration) {
1042       assert(isa<GlobalAlias>(&Dest));
1043       LinkFromSrc = false;
1044       return false;
1045     }
1046   }
1047
1048   if (SrcIsDeclaration) {
1049     // If Src is external or if both Src & Dest are external..  Just link the
1050     // external globals, we aren't adding anything.
1051     if (Src.hasDLLImportStorageClass()) {
1052       // If one of GVs is marked as DLLImport, result should be dllimport'ed.
1053       LinkFromSrc = DestIsDeclaration;
1054       return false;
1055     }
1056     // If the Dest is weak, use the source linkage.
1057     LinkFromSrc = Dest.hasExternalWeakLinkage();
1058     return false;
1059   }
1060
1061   if (DestIsDeclaration) {
1062     // If Dest is external but Src is not:
1063     LinkFromSrc = true;
1064     return false;
1065   }
1066
1067   if (Src.hasCommonLinkage()) {
1068     if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
1069       LinkFromSrc = true;
1070       return false;
1071     }
1072
1073     if (!Dest.hasCommonLinkage()) {
1074       LinkFromSrc = false;
1075       return false;
1076     }
1077
1078     const DataLayout &DL = Dest.getParent()->getDataLayout();
1079     uint64_t DestSize = DL.getTypeAllocSize(Dest.getType()->getElementType());
1080     uint64_t SrcSize = DL.getTypeAllocSize(Src.getType()->getElementType());
1081     LinkFromSrc = SrcSize > DestSize;
1082     return false;
1083   }
1084
1085   if (Src.isWeakForLinker()) {
1086     assert(!Dest.hasExternalWeakLinkage());
1087     assert(!Dest.hasAvailableExternallyLinkage());
1088
1089     if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
1090       LinkFromSrc = true;
1091       return false;
1092     }
1093
1094     LinkFromSrc = false;
1095     return false;
1096   }
1097
1098   if (Dest.isWeakForLinker()) {
1099     assert(Src.hasExternalLinkage());
1100     LinkFromSrc = true;
1101     return false;
1102   }
1103
1104   assert(!Src.hasExternalWeakLinkage());
1105   assert(!Dest.hasExternalWeakLinkage());
1106   assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
1107          "Unexpected linkage type!");
1108   return emitError("Linking globals named '" + Src.getName() +
1109                    "': symbol multiply defined!");
1110 }
1111
1112 /// Loop over all of the linked values to compute type mappings.  For example,
1113 /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
1114 /// types 'Foo' but one got renamed when the module was loaded into the same
1115 /// LLVMContext.
1116 void ModuleLinker::computeTypeMapping() {
1117   for (GlobalValue &SGV : SrcM.globals()) {
1118     GlobalValue *DGV = getLinkedToGlobal(&SGV);
1119     if (!DGV)
1120       continue;
1121
1122     if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
1123       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
1124       continue;
1125     }
1126
1127     // Unify the element type of appending arrays.
1128     ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
1129     ArrayType *SAT = cast<ArrayType>(SGV.getType()->getElementType());
1130     TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
1131   }
1132
1133   for (GlobalValue &SGV : SrcM) {
1134     if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
1135       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
1136   }
1137
1138   for (GlobalValue &SGV : SrcM.aliases()) {
1139     if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
1140       TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
1141   }
1142
1143   // Incorporate types by name, scanning all the types in the source module.
1144   // At this point, the destination module may have a type "%foo = { i32 }" for
1145   // example.  When the source module got loaded into the same LLVMContext, if
1146   // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
1147   std::vector<StructType *> Types = SrcM.getIdentifiedStructTypes();
1148   for (StructType *ST : Types) {
1149     if (!ST->hasName())
1150       continue;
1151
1152     // Check to see if there is a dot in the name followed by a digit.
1153     size_t DotPos = ST->getName().rfind('.');
1154     if (DotPos == 0 || DotPos == StringRef::npos ||
1155         ST->getName().back() == '.' ||
1156         !isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1])))
1157       continue;
1158
1159     // Check to see if the destination module has a struct with the prefix name.
1160     StructType *DST = DstM.getTypeByName(ST->getName().substr(0, DotPos));
1161     if (!DST)
1162       continue;
1163
1164     // Don't use it if this actually came from the source module. They're in
1165     // the same LLVMContext after all. Also don't use it unless the type is
1166     // actually used in the destination module. This can happen in situations
1167     // like this:
1168     //
1169     //      Module A                         Module B
1170     //      --------                         --------
1171     //   %Z = type { %A }                %B = type { %C.1 }
1172     //   %A = type { %B.1, [7 x i8] }    %C.1 = type { i8* }
1173     //   %B.1 = type { %C }              %A.2 = type { %B.3, [5 x i8] }
1174     //   %C = type { i8* }               %B.3 = type { %C.1 }
1175     //
1176     // When we link Module B with Module A, the '%B' in Module B is
1177     // used. However, that would then use '%C.1'. But when we process '%C.1',
1178     // we prefer to take the '%C' version. So we are then left with both
1179     // '%C.1' and '%C' being used for the same types. This leads to some
1180     // variables using one type and some using the other.
1181     if (TypeMap.DstStructTypesSet.hasType(DST))
1182       TypeMap.addTypeMapping(DST, ST);
1183   }
1184
1185   // Now that we have discovered all of the type equivalences, get a body for
1186   // any 'opaque' types in the dest module that are now resolved.
1187   TypeMap.linkDefinedTypeBodies();
1188 }
1189
1190 static void getArrayElements(const Constant *C,
1191                              SmallVectorImpl<Constant *> &Dest) {
1192   unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
1193
1194   for (unsigned i = 0; i != NumElements; ++i)
1195     Dest.push_back(C->getAggregateElement(i));
1196 }
1197
1198 /// If there were any appending global variables, link them together now.
1199 /// Return true on error.
1200 Constant *ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
1201                                               const GlobalVariable *SrcGV) {
1202   Type *EltTy = cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()))
1203                     ->getElementType();
1204
1205   StringRef Name = SrcGV->getName();
1206   bool IsNewStructor = false;
1207   bool IsOldStructor = false;
1208   if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") {
1209     if (cast<StructType>(EltTy)->getNumElements() == 3)
1210       IsNewStructor = true;
1211     else
1212       IsOldStructor = true;
1213   }
1214
1215   PointerType *VoidPtrTy = Type::getInt8Ty(SrcGV->getContext())->getPointerTo();
1216   if (IsOldStructor) {
1217     auto &ST = *cast<StructType>(EltTy);
1218     Type *Tys[3] = {ST.getElementType(0), ST.getElementType(1), VoidPtrTy};
1219     EltTy = StructType::get(SrcGV->getContext(), Tys, false);
1220   }
1221
1222   if (DstGV) {
1223     ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
1224
1225     if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage()) {
1226       emitError(
1227           "Linking globals named '" + SrcGV->getName() +
1228           "': can only link appending global with another appending global!");
1229       return nullptr;
1230     }
1231
1232     // Check to see that they two arrays agree on type.
1233     if (EltTy != DstTy->getElementType()) {
1234       emitError("Appending variables with different element types!");
1235       return nullptr;
1236     }
1237     if (DstGV->isConstant() != SrcGV->isConstant()) {
1238       emitError("Appending variables linked with different const'ness!");
1239       return nullptr;
1240     }
1241
1242     if (DstGV->getAlignment() != SrcGV->getAlignment()) {
1243       emitError(
1244           "Appending variables with different alignment need to be linked!");
1245       return nullptr;
1246     }
1247
1248     if (DstGV->getVisibility() != SrcGV->getVisibility()) {
1249       emitError(
1250           "Appending variables with different visibility need to be linked!");
1251       return nullptr;
1252     }
1253
1254     if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr()) {
1255       emitError(
1256           "Appending variables with different unnamed_addr need to be linked!");
1257       return nullptr;
1258     }
1259
1260     if (StringRef(DstGV->getSection()) != SrcGV->getSection()) {
1261       emitError(
1262           "Appending variables with different section name need to be linked!");
1263       return nullptr;
1264     }
1265   }
1266
1267   SmallVector<Constant *, 16> DstElements;
1268   if (DstGV)
1269     getArrayElements(DstGV->getInitializer(), DstElements);
1270
1271   SmallVector<Constant *, 16> SrcElements;
1272   getArrayElements(SrcGV->getInitializer(), SrcElements);
1273
1274   if (IsNewStructor)
1275     SrcElements.erase(
1276         std::remove_if(SrcElements.begin(), SrcElements.end(),
1277                        [this](Constant *E) {
1278                          auto *Key = dyn_cast<GlobalValue>(
1279                              E->getAggregateElement(2)->stripPointerCasts());
1280                          return Key && !ValuesToLink.count(Key) &&
1281                                 !shouldLazyLink(*Key);
1282                        }),
1283         SrcElements.end());
1284   uint64_t NewSize = DstElements.size() + SrcElements.size();
1285   ArrayType *NewType = ArrayType::get(EltTy, NewSize);
1286
1287   // Create the new global variable.
1288   GlobalVariable *NG = new GlobalVariable(
1289       DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
1290       /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
1291       SrcGV->getType()->getAddressSpace());
1292
1293   // Propagate alignment, visibility and section info.
1294   copyGVAttributes(NG, SrcGV);
1295
1296   Constant *Ret = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
1297
1298   // Stop recursion.
1299   ValueMap[SrcGV] = Ret;
1300
1301   for (auto *V : SrcElements) {
1302     Constant *NewV;
1303     if (IsOldStructor) {
1304       auto *S = cast<ConstantStruct>(V);
1305       auto *E1 = MapValue(S->getOperand(0), ValueMap, RF_MoveDistinctMDs,
1306                           &TypeMap, &ValMaterializer);
1307       auto *E2 = MapValue(S->getOperand(1), ValueMap, RF_MoveDistinctMDs,
1308                           &TypeMap, &ValMaterializer);
1309       Value *Null = Constant::getNullValue(VoidPtrTy);
1310       NewV =
1311           ConstantStruct::get(cast<StructType>(EltTy), E1, E2, Null, nullptr);
1312     } else {
1313       NewV =
1314           MapValue(V, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
1315     }
1316     DstElements.push_back(NewV);
1317   }
1318
1319   NG->setInitializer(ConstantArray::get(NewType, DstElements));
1320
1321   // Replace any uses of the two global variables with uses of the new
1322   // global.
1323   if (DstGV) {
1324     DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
1325     DstGV->eraseFromParent();
1326   }
1327
1328   return Ret;
1329 }
1330
1331 Constant *ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
1332   GlobalValue *DGV = getLinkedToGlobal(SGV);
1333
1334   // Handle the ultra special appending linkage case first.
1335   assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage());
1336   if (SGV->hasAppendingLinkage()) {
1337     // Should have prevented importing for appending linkage in linkIfNeeded.
1338     assert(!isPerformingImport());
1339     return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV),
1340                                  cast<GlobalVariable>(SGV));
1341   }
1342
1343   bool LinkFromSrc = true;
1344   Comdat *C = nullptr;
1345   bool HasUnnamedAddr = SGV->hasUnnamedAddr();
1346
1347   if (isPerformingImport() && !doImportAsDefinition(SGV)) {
1348     LinkFromSrc = false;
1349   } else if (const Comdat *SC = SGV->getComdat()) {
1350     Comdat::SelectionKind SK;
1351     std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
1352     C = DstM.getOrInsertComdat(SC->getName());
1353     C->setSelectionKind(SK);
1354     if (SGV->hasLocalLinkage())
1355       LinkFromSrc = true;
1356   } else if (DGV) {
1357     if (shouldLinkFromSource(LinkFromSrc, *DGV, *SGV))
1358       return nullptr;
1359   }
1360
1361   if (DGV)
1362     HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
1363
1364   GlobalValue *NewGV;
1365   if (!LinkFromSrc && DGV) {
1366     NewGV = DGV;
1367     // When linking from source we setVisibility from copyGlobalValueProto.
1368     setVisibility(NewGV, SGV, DGV);
1369   } else {
1370     // If we are done linking global value bodies (i.e. we are performing
1371     // metadata linking), don't link in the global value due to this
1372     // reference, simply map it to null.
1373     if (DoneLinkingBodies)
1374       return nullptr;
1375
1376     NewGV = copyGlobalValueProto(SGV, DGV, LinkFromSrc);
1377   }
1378
1379   NewGV->setUnnamedAddr(HasUnnamedAddr);
1380
1381   if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
1382     if (C && LinkFromSrc)
1383       NewGO->setComdat(C);
1384
1385     if (DGV && DGV->hasCommonLinkage() && SGV->hasCommonLinkage())
1386       NewGO->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment()));
1387   }
1388
1389   if (auto *NewGVar = dyn_cast<GlobalVariable>(NewGV)) {
1390     auto *DGVar = dyn_cast_or_null<GlobalVariable>(DGV);
1391     auto *SGVar = dyn_cast<GlobalVariable>(SGV);
1392     if (DGVar && SGVar && DGVar->isDeclaration() && SGVar->isDeclaration() &&
1393         (!DGVar->isConstant() || !SGVar->isConstant()))
1394       NewGVar->setConstant(false);
1395   }
1396
1397   if (NewGV != DGV && DGV) {
1398     DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType()));
1399     DGV->eraseFromParent();
1400   }
1401
1402   return ConstantExpr::getBitCast(NewGV, TypeMap.get(SGV->getType()));
1403 }
1404
1405 /// Update the initializers in the Dest module now that all globals that may be
1406 /// referenced are in Dest.
1407 void ModuleLinker::linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src) {
1408   // Figure out what the initializer looks like in the dest module.
1409   Dst.setInitializer(MapValue(Src.getInitializer(), ValueMap,
1410                               RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
1411 }
1412
1413 /// Copy the source function over into the dest function and fix up references
1414 /// to values. At this point we know that Dest is an external function, and
1415 /// that Src is not.
1416 bool ModuleLinker::linkFunctionBody(Function &Dst, Function &Src) {
1417   assert(Dst.isDeclaration() && !Src.isDeclaration());
1418
1419   // Materialize if needed.
1420   if (std::error_code EC = Src.materialize())
1421     return emitError(EC.message());
1422
1423   // Link in the prefix data.
1424   if (Src.hasPrefixData())
1425     Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap,
1426                                RF_MoveDistinctMDs, &TypeMap, &ValMaterializer));
1427
1428   // Link in the prologue data.
1429   if (Src.hasPrologueData())
1430     Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap,
1431                                  RF_MoveDistinctMDs, &TypeMap,
1432                                  &ValMaterializer));
1433
1434   // Link in the personality function.
1435   if (Src.hasPersonalityFn())
1436     Dst.setPersonalityFn(MapValue(Src.getPersonalityFn(), ValueMap,
1437                                   RF_MoveDistinctMDs, &TypeMap,
1438                                   &ValMaterializer));
1439
1440   // Go through and convert function arguments over, remembering the mapping.
1441   Function::arg_iterator DI = Dst.arg_begin();
1442   for (Argument &Arg : Src.args()) {
1443     DI->setName(Arg.getName()); // Copy the name over.
1444
1445     // Add a mapping to our mapping.
1446     ValueMap[&Arg] = &*DI;
1447     ++DI;
1448   }
1449
1450   // Copy over the metadata attachments.
1451   SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
1452   Src.getAllMetadata(MDs);
1453   for (const auto &I : MDs)
1454     Dst.setMetadata(I.first, MapMetadata(I.second, ValueMap, RF_MoveDistinctMDs,
1455                                          &TypeMap, &ValMaterializer));
1456
1457   // Splice the body of the source function into the dest function.
1458   Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
1459
1460   // At this point, all of the instructions and values of the function are now
1461   // copied over.  The only problem is that they are still referencing values in
1462   // the Source function as operands.  Loop through all of the operands of the
1463   // functions and patch them up to point to the local versions.
1464   for (BasicBlock &BB : Dst)
1465     for (Instruction &I : BB)
1466       RemapInstruction(&I, ValueMap,
1467                        RF_IgnoreMissingEntries | RF_MoveDistinctMDs, &TypeMap,
1468                        &ValMaterializer);
1469
1470   // There is no need to map the arguments anymore.
1471   for (Argument &Arg : Src.args())
1472     ValueMap.erase(&Arg);
1473
1474   Src.dematerialize();
1475   return false;
1476 }
1477
1478 void ModuleLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
1479   Constant *Aliasee = Src.getAliasee();
1480   Constant *Val = MapValue(Aliasee, ValueMap, RF_MoveDistinctMDs, &TypeMap,
1481                            &ValMaterializer);
1482   Dst.setAliasee(Val);
1483 }
1484
1485 bool ModuleLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
1486   if (const Comdat *SC = Src.getComdat()) {
1487     // To ensure that we don't generate an incomplete comdat group,
1488     // we must materialize and map in any other members that are not
1489     // yet materialized in Dst, which also ensures their definitions
1490     // are linked in. Otherwise, linkonce and other lazy linked GVs will
1491     // not be materialized if they aren't referenced.
1492     for (auto *SGV : ComdatMembers[SC]) {
1493       auto *DGV = cast_or_null<GlobalValue>(ValueMap.lookup(SGV));
1494       if (DGV && !DGV->isDeclaration())
1495         continue;
1496       MapValue(SGV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
1497     }
1498   }
1499   if (shouldInternalizeLinkedSymbols())
1500     if (auto *DGV = dyn_cast<GlobalValue>(&Dst))
1501       DGV->setLinkage(GlobalValue::InternalLinkage);
1502   if (auto *F = dyn_cast<Function>(&Src))
1503     return linkFunctionBody(cast<Function>(Dst), *F);
1504   if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
1505     linkGlobalInit(cast<GlobalVariable>(Dst), *GVar);
1506     return false;
1507   }
1508   linkAliasBody(cast<GlobalAlias>(Dst), cast<GlobalAlias>(Src));
1509   return false;
1510 }
1511
1512 /// Insert all of the named MDNodes in Src into the Dest module.
1513 void ModuleLinker::linkNamedMDNodes() {
1514   const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
1515   for (const NamedMDNode &NMD : SrcM.named_metadata()) {
1516     // Don't link module flags here. Do them separately.
1517     if (&NMD == SrcModFlags)
1518       continue;
1519     NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
1520     // Add Src elements into Dest node.
1521     for (const MDNode *op : NMD.operands())
1522       DestNMD->addOperand(MapMetadata(
1523           op, ValueMap, RF_MoveDistinctMDs | RF_NullMapMissingGlobalValues,
1524           &TypeMap, &ValMaterializer));
1525   }
1526 }
1527
1528 /// Merge the linker flags in Src into the Dest module.
1529 bool ModuleLinker::linkModuleFlagsMetadata() {
1530   // If the source module has no module flags, we are done.
1531   const NamedMDNode *SrcModFlags = SrcM.getModuleFlagsMetadata();
1532   if (!SrcModFlags)
1533     return false;
1534
1535   // If the destination module doesn't have module flags yet, then just copy
1536   // over the source module's flags.
1537   NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
1538   if (DstModFlags->getNumOperands() == 0) {
1539     for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1540       DstModFlags->addOperand(SrcModFlags->getOperand(I));
1541
1542     return false;
1543   }
1544
1545   // First build a map of the existing module flags and requirements.
1546   DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
1547   SmallSetVector<MDNode *, 16> Requirements;
1548   for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1549     MDNode *Op = DstModFlags->getOperand(I);
1550     ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
1551     MDString *ID = cast<MDString>(Op->getOperand(1));
1552
1553     if (Behavior->getZExtValue() == Module::Require) {
1554       Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1555     } else {
1556       Flags[ID] = std::make_pair(Op, I);
1557     }
1558   }
1559
1560   // Merge in the flags from the source module, and also collect its set of
1561   // requirements.
1562   bool HasErr = false;
1563   for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1564     MDNode *SrcOp = SrcModFlags->getOperand(I);
1565     ConstantInt *SrcBehavior =
1566         mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
1567     MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1568     MDNode *DstOp;
1569     unsigned DstIndex;
1570     std::tie(DstOp, DstIndex) = Flags.lookup(ID);
1571     unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1572
1573     // If this is a requirement, add it and continue.
1574     if (SrcBehaviorValue == Module::Require) {
1575       // If the destination module does not already have this requirement, add
1576       // it.
1577       if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1578         DstModFlags->addOperand(SrcOp);
1579       }
1580       continue;
1581     }
1582
1583     // If there is no existing flag with this ID, just add it.
1584     if (!DstOp) {
1585       Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
1586       DstModFlags->addOperand(SrcOp);
1587       continue;
1588     }
1589
1590     // Otherwise, perform a merge.
1591     ConstantInt *DstBehavior =
1592         mdconst::extract<ConstantInt>(DstOp->getOperand(0));
1593     unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1594
1595     // If either flag has override behavior, handle it first.
1596     if (DstBehaviorValue == Module::Override) {
1597       // Diagnose inconsistent flags which both have override behavior.
1598       if (SrcBehaviorValue == Module::Override &&
1599           SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1600         HasErr |= emitError("linking module flags '" + ID->getString() +
1601                             "': IDs have conflicting override values");
1602       }
1603       continue;
1604     } else if (SrcBehaviorValue == Module::Override) {
1605       // Update the destination flag to that of the source.
1606       DstModFlags->setOperand(DstIndex, SrcOp);
1607       Flags[ID].first = SrcOp;
1608       continue;
1609     }
1610
1611     // Diagnose inconsistent merge behavior types.
1612     if (SrcBehaviorValue != DstBehaviorValue) {
1613       HasErr |= emitError("linking module flags '" + ID->getString() +
1614                           "': IDs have conflicting behaviors");
1615       continue;
1616     }
1617
1618     auto replaceDstValue = [&](MDNode *New) {
1619       Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
1620       MDNode *Flag = MDNode::get(DstM.getContext(), FlagOps);
1621       DstModFlags->setOperand(DstIndex, Flag);
1622       Flags[ID].first = Flag;
1623     };
1624
1625     // Perform the merge for standard behavior types.
1626     switch (SrcBehaviorValue) {
1627     case Module::Require:
1628     case Module::Override:
1629       llvm_unreachable("not possible");
1630     case Module::Error: {
1631       // Emit an error if the values differ.
1632       if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1633         HasErr |= emitError("linking module flags '" + ID->getString() +
1634                             "': IDs have conflicting values");
1635       }
1636       continue;
1637     }
1638     case Module::Warning: {
1639       // Emit a warning if the values differ.
1640       if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1641         emitWarning("linking module flags '" + ID->getString() +
1642                     "': IDs have conflicting values");
1643       }
1644       continue;
1645     }
1646     case Module::Append: {
1647       MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1648       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1649       SmallVector<Metadata *, 8> MDs;
1650       MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
1651       MDs.append(DstValue->op_begin(), DstValue->op_end());
1652       MDs.append(SrcValue->op_begin(), SrcValue->op_end());
1653
1654       replaceDstValue(MDNode::get(DstM.getContext(), MDs));
1655       break;
1656     }
1657     case Module::AppendUnique: {
1658       SmallSetVector<Metadata *, 16> Elts;
1659       MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1660       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1661       Elts.insert(DstValue->op_begin(), DstValue->op_end());
1662       Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
1663
1664       replaceDstValue(MDNode::get(DstM.getContext(),
1665                                   makeArrayRef(Elts.begin(), Elts.end())));
1666       break;
1667     }
1668     }
1669   }
1670
1671   // Check all of the requirements.
1672   for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1673     MDNode *Requirement = Requirements[I];
1674     MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1675     Metadata *ReqValue = Requirement->getOperand(1);
1676
1677     MDNode *Op = Flags[Flag].first;
1678     if (!Op || Op->getOperand(2) != ReqValue) {
1679       HasErr |= emitError("linking module flags '" + Flag->getString() +
1680                           "': does not have the required value");
1681       continue;
1682     }
1683   }
1684
1685   return HasErr;
1686 }
1687
1688 // This function returns true if the triples match.
1689 static bool triplesMatch(const Triple &T0, const Triple &T1) {
1690   // If vendor is apple, ignore the version number.
1691   if (T0.getVendor() == Triple::Apple)
1692     return T0.getArch() == T1.getArch() && T0.getSubArch() == T1.getSubArch() &&
1693            T0.getVendor() == T1.getVendor() && T0.getOS() == T1.getOS();
1694
1695   return T0 == T1;
1696 }
1697
1698 // This function returns the merged triple.
1699 static std::string mergeTriples(const Triple &SrcTriple,
1700                                 const Triple &DstTriple) {
1701   // If vendor is apple, pick the triple with the larger version number.
1702   if (SrcTriple.getVendor() == Triple::Apple)
1703     if (DstTriple.isOSVersionLT(SrcTriple))
1704       return SrcTriple.str();
1705
1706   return DstTriple.str();
1707 }
1708
1709 bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
1710   GlobalValue *DGV = getLinkedToGlobal(&GV);
1711
1712   if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration()))
1713     return false;
1714
1715   if (DGV && !GV.hasLocalLinkage() && !GV.hasAppendingLinkage()) {
1716     auto *DGVar = dyn_cast<GlobalVariable>(DGV);
1717     auto *SGVar = dyn_cast<GlobalVariable>(&GV);
1718     if (DGVar && SGVar) {
1719       if (DGVar->isDeclaration() && SGVar->isDeclaration() &&
1720           (!DGVar->isConstant() || !SGVar->isConstant())) {
1721         DGVar->setConstant(false);
1722         SGVar->setConstant(false);
1723       }
1724       if (DGVar->hasCommonLinkage() && SGVar->hasCommonLinkage()) {
1725         unsigned Align = std::max(DGVar->getAlignment(), SGVar->getAlignment());
1726         SGVar->setAlignment(Align);
1727         DGVar->setAlignment(Align);
1728       }
1729     }
1730
1731     GlobalValue::VisibilityTypes Visibility =
1732         getMinVisibility(DGV->getVisibility(), GV.getVisibility());
1733     DGV->setVisibility(Visibility);
1734     GV.setVisibility(Visibility);
1735
1736     bool HasUnnamedAddr = GV.hasUnnamedAddr() && DGV->hasUnnamedAddr();
1737     DGV->setUnnamedAddr(HasUnnamedAddr);
1738     GV.setUnnamedAddr(HasUnnamedAddr);
1739   }
1740
1741   // Don't want to append to global_ctors list, for example, when we
1742   // are importing for ThinLTO, otherwise the global ctors and dtors
1743   // get executed multiple times for local variables (the latter causing
1744   // double frees).
1745   if (GV.hasAppendingLinkage() && isPerformingImport())
1746     return false;
1747
1748   if (isPerformingImport() && !doImportAsDefinition(&GV))
1749     return false;
1750
1751   if (!DGV && !shouldOverrideFromSrc() &&
1752       (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
1753        GV.hasAvailableExternallyLinkage()))
1754     return false;
1755
1756   if (GV.isDeclaration())
1757     return false;
1758
1759   if (const Comdat *SC = GV.getComdat()) {
1760     bool LinkFromSrc;
1761     Comdat::SelectionKind SK;
1762     std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
1763     if (LinkFromSrc)
1764       ValuesToLink.insert(&GV);
1765     return false;
1766   }
1767
1768   bool LinkFromSrc = true;
1769   if (DGV && shouldLinkFromSource(LinkFromSrc, *DGV, GV))
1770     return true;
1771   if (LinkFromSrc)
1772     ValuesToLink.insert(&GV);
1773   return false;
1774 }
1775
1776 bool ModuleLinker::run() {
1777   // Inherit the target data from the source module if the destination module
1778   // doesn't have one already.
1779   if (DstM.getDataLayout().isDefault())
1780     DstM.setDataLayout(SrcM.getDataLayout());
1781
1782   if (SrcM.getDataLayout() != DstM.getDataLayout()) {
1783     emitWarning("Linking two modules of different data layouts: '" +
1784                 SrcM.getModuleIdentifier() + "' is '" +
1785                 SrcM.getDataLayoutStr() + "' whereas '" +
1786                 DstM.getModuleIdentifier() + "' is '" +
1787                 DstM.getDataLayoutStr() + "'\n");
1788   }
1789
1790   // Copy the target triple from the source to dest if the dest's is empty.
1791   if (DstM.getTargetTriple().empty() && !SrcM.getTargetTriple().empty())
1792     DstM.setTargetTriple(SrcM.getTargetTriple());
1793
1794   Triple SrcTriple(SrcM.getTargetTriple()), DstTriple(DstM.getTargetTriple());
1795
1796   if (!SrcM.getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
1797     emitWarning("Linking two modules of different target triples: " +
1798                 SrcM.getModuleIdentifier() + "' is '" + SrcM.getTargetTriple() +
1799                 "' whereas '" + DstM.getModuleIdentifier() + "' is '" +
1800                 DstM.getTargetTriple() + "'\n");
1801
1802   DstM.setTargetTriple(mergeTriples(SrcTriple, DstTriple));
1803
1804   // Append the module inline asm string.
1805   if (!SrcM.getModuleInlineAsm().empty()) {
1806     if (DstM.getModuleInlineAsm().empty())
1807       DstM.setModuleInlineAsm(SrcM.getModuleInlineAsm());
1808     else
1809       DstM.setModuleInlineAsm(DstM.getModuleInlineAsm() + "\n" +
1810                               SrcM.getModuleInlineAsm());
1811   }
1812
1813   // Loop over all of the linked values to compute type mappings.
1814   computeTypeMapping();
1815
1816   ComdatsChosen.clear();
1817   for (const auto &SMEC : SrcM.getComdatSymbolTable()) {
1818     const Comdat &C = SMEC.getValue();
1819     if (ComdatsChosen.count(&C))
1820       continue;
1821     Comdat::SelectionKind SK;
1822     bool LinkFromSrc;
1823     if (getComdatResult(&C, SK, LinkFromSrc))
1824       return true;
1825     ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
1826   }
1827
1828   for (GlobalVariable &GV : SrcM.globals())
1829     if (const Comdat *SC = GV.getComdat())
1830       ComdatMembers[SC].push_back(&GV);
1831
1832   for (Function &SF : SrcM)
1833     if (const Comdat *SC = SF.getComdat())
1834       ComdatMembers[SC].push_back(&SF);
1835
1836   for (GlobalAlias &GA : SrcM.aliases())
1837     if (const Comdat *SC = GA.getComdat())
1838       ComdatMembers[SC].push_back(&GA);
1839
1840   // Insert all of the globals in src into the DstM module... without linking
1841   // initializers (which could refer to functions not yet mapped over).
1842   for (GlobalVariable &GV : SrcM.globals())
1843     if (linkIfNeeded(GV))
1844       return true;
1845
1846   for (Function &SF : SrcM)
1847     if (linkIfNeeded(SF))
1848       return true;
1849
1850   for (GlobalAlias &GA : SrcM.aliases())
1851     if (linkIfNeeded(GA))
1852       return true;
1853
1854   for (GlobalValue *GV : ValuesToLink) {
1855     MapValue(GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
1856     if (HasError)
1857       return true;
1858   }
1859
1860   // Note that we are done linking global value bodies. This prevents
1861   // metadata linking from creating new references.
1862   DoneLinkingBodies = true;
1863
1864   // Remap all of the named MDNodes in Src into the DstM module. We do this
1865   // after linking GlobalValues so that MDNodes that reference GlobalValues
1866   // are properly remapped.
1867   linkNamedMDNodes();
1868
1869   // Merge the module flags into the DstM module.
1870   if (linkModuleFlagsMetadata())
1871     return true;
1872
1873   return false;
1874 }
1875
1876 Linker::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
1877     : ETypes(E), IsPacked(P) {}
1878
1879 Linker::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
1880     : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1881
1882 bool Linker::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
1883   if (IsPacked != That.IsPacked)
1884     return false;
1885   if (ETypes != That.ETypes)
1886     return false;
1887   return true;
1888 }
1889
1890 bool Linker::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
1891   return !this->operator==(That);
1892 }
1893
1894 StructType *Linker::StructTypeKeyInfo::getEmptyKey() {
1895   return DenseMapInfo<StructType *>::getEmptyKey();
1896 }
1897
1898 StructType *Linker::StructTypeKeyInfo::getTombstoneKey() {
1899   return DenseMapInfo<StructType *>::getTombstoneKey();
1900 }
1901
1902 unsigned Linker::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1903   return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
1904                       Key.IsPacked);
1905 }
1906
1907 unsigned Linker::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1908   return getHashValue(KeyTy(ST));
1909 }
1910
1911 bool Linker::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1912                                         const StructType *RHS) {
1913   if (RHS == getEmptyKey() || RHS == getTombstoneKey())
1914     return false;
1915   return LHS == KeyTy(RHS);
1916 }
1917
1918 bool Linker::StructTypeKeyInfo::isEqual(const StructType *LHS,
1919                                         const StructType *RHS) {
1920   if (RHS == getEmptyKey())
1921     return LHS == getEmptyKey();
1922
1923   if (RHS == getTombstoneKey())
1924     return LHS == getTombstoneKey();
1925
1926   return KeyTy(LHS) == KeyTy(RHS);
1927 }
1928
1929 void Linker::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
1930   assert(!Ty->isOpaque());
1931   NonOpaqueStructTypes.insert(Ty);
1932 }
1933
1934 void Linker::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
1935   assert(!Ty->isOpaque());
1936   NonOpaqueStructTypes.insert(Ty);
1937   bool Removed = OpaqueStructTypes.erase(Ty);
1938   (void)Removed;
1939   assert(Removed);
1940 }
1941
1942 void Linker::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
1943   assert(Ty->isOpaque());
1944   OpaqueStructTypes.insert(Ty);
1945 }
1946
1947 StructType *
1948 Linker::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
1949                                                bool IsPacked) {
1950   Linker::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
1951   auto I = NonOpaqueStructTypes.find_as(Key);
1952   if (I == NonOpaqueStructTypes.end())
1953     return nullptr;
1954   return *I;
1955 }
1956
1957 bool Linker::IdentifiedStructTypeSet::hasType(StructType *Ty) {
1958   if (Ty->isOpaque())
1959     return OpaqueStructTypes.count(Ty);
1960   auto I = NonOpaqueStructTypes.find(Ty);
1961   if (I == NonOpaqueStructTypes.end())
1962     return false;
1963   return *I == Ty;
1964 }
1965
1966 Linker::Linker(Module &M, DiagnosticHandlerFunction DiagnosticHandler)
1967     : Composite(M), DiagnosticHandler(DiagnosticHandler) {
1968   TypeFinder StructTypes;
1969   StructTypes.run(M, true);
1970   for (StructType *Ty : StructTypes) {
1971     if (Ty->isOpaque())
1972       IdentifiedStructTypes.addOpaque(Ty);
1973     else
1974       IdentifiedStructTypes.addNonOpaque(Ty);
1975   }
1976 }
1977
1978 bool Linker::linkInModule(Module &Src, unsigned Flags,
1979                           const FunctionInfoIndex *Index,
1980                           DenseSet<const GlobalValue *> *FunctionsToImport) {
1981   ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src,
1982                          DiagnosticHandler, Flags, Index, FunctionsToImport);
1983   bool RetCode = TheLinker.run();
1984   Composite.dropTriviallyDeadConstantArrays();
1985   return RetCode;
1986 }
1987
1988 //===----------------------------------------------------------------------===//
1989 // LinkModules entrypoint.
1990 //===----------------------------------------------------------------------===//
1991
1992 /// This function links two modules together, with the resulting Dest module
1993 /// modified to be the composite of the two input modules. If an error occurs,
1994 /// true is returned and ErrorMsg (if not null) is set to indicate the problem.
1995 /// Upon failure, the Dest module could be in a modified state, and shouldn't be
1996 /// relied on to be consistent.
1997 bool Linker::linkModules(Module &Dest, Module &Src,
1998                          DiagnosticHandlerFunction DiagnosticHandler,
1999                          unsigned Flags) {
2000   Linker L(Dest, DiagnosticHandler);
2001   return L.linkInModule(Src, Flags);
2002 }
2003
2004 std::unique_ptr<Module>
2005 llvm::renameModuleForThinLTO(std::unique_ptr<Module> &M,
2006                              const FunctionInfoIndex *Index,
2007                              DiagnosticHandlerFunction DiagnosticHandler) {
2008   std::unique_ptr<llvm::Module> RenamedModule(
2009       new llvm::Module(M->getModuleIdentifier(), M->getContext()));
2010   Linker L(*RenamedModule.get(), DiagnosticHandler);
2011   if (L.linkInModule(*M.get(), llvm::Linker::Flags::None, Index))
2012     return nullptr;
2013   return RenamedModule;
2014 }
2015
2016 //===----------------------------------------------------------------------===//
2017 // C API.
2018 //===----------------------------------------------------------------------===//
2019
2020 LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
2021                          LLVMLinkerMode Unused, char **OutMessages) {
2022   Module *D = unwrap(Dest);
2023   std::string Message;
2024   raw_string_ostream Stream(Message);
2025   DiagnosticPrinterRawOStream DP(Stream);
2026
2027   LLVMBool Result = Linker::linkModules(
2028       *D, *unwrap(Src), [&](const DiagnosticInfo &DI) { DI.print(DP); });
2029
2030   if (OutMessages && Result) {
2031     Stream.flush();
2032     *OutMessages = strdup(Message.c_str());
2033   }
2034   return Result;
2035 }