[MachineSink] Improve the compile time by preserving the dominance information
[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/Optional.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/TypeFinder.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Transforms/Utils/Cloning.h"
26 #include <cctype>
27 #include <tuple>
28 using namespace llvm;
29
30
31 //===----------------------------------------------------------------------===//
32 // TypeMap implementation.
33 //===----------------------------------------------------------------------===//
34
35 namespace {
36   typedef SmallPtrSet<StructType*, 32> TypeSet;
37
38 class TypeMapTy : public ValueMapTypeRemapper {
39   /// MappedTypes - This is a mapping from a source type to a destination type
40   /// to use.
41   DenseMap<Type*, Type*> MappedTypes;
42
43   /// SpeculativeTypes - When checking to see if two subgraphs are isomorphic,
44   /// we speculatively add types to MappedTypes, but keep track of them here in
45   /// case we need to roll back.
46   SmallVector<Type*, 16> SpeculativeTypes;
47
48   /// SrcDefinitionsToResolve - This is a list of non-opaque structs in the
49   /// source module that are mapped to an opaque struct in the destination
50   /// module.
51   SmallVector<StructType*, 16> SrcDefinitionsToResolve;
52
53   /// DstResolvedOpaqueTypes - This is the set of opaque types in the
54   /// destination modules who are getting a body from the source module.
55   SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes;
56
57 public:
58   TypeMapTy(TypeSet &Set) : DstStructTypesSet(Set) {}
59
60   TypeSet &DstStructTypesSet;
61   /// addTypeMapping - Indicate that the specified type in the destination
62   /// module is conceptually equivalent to the specified type in the source
63   /// module.
64   void addTypeMapping(Type *DstTy, Type *SrcTy);
65
66   /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest
67   /// module from a type definition in the source module.
68   void linkDefinedTypeBodies();
69
70   /// get - Return the mapped type to use for the specified input type from the
71   /// source module.
72   Type *get(Type *SrcTy);
73
74   FunctionType *get(FunctionType *T) {return cast<FunctionType>(get((Type*)T));}
75
76   /// dump - Dump out the type map for debugging purposes.
77   void dump() const {
78     for (DenseMap<Type*, Type*>::const_iterator
79            I = MappedTypes.begin(), E = MappedTypes.end(); I != E; ++I) {
80       dbgs() << "TypeMap: ";
81       I->first->dump();
82       dbgs() << " => ";
83       I->second->dump();
84       dbgs() << '\n';
85     }
86   }
87
88 private:
89   Type *getImpl(Type *T);
90   /// remapType - Implement the ValueMapTypeRemapper interface.
91   Type *remapType(Type *SrcTy) override {
92     return get(SrcTy);
93   }
94
95   bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
96 };
97 }
98
99 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
100   Type *&Entry = MappedTypes[SrcTy];
101   if (Entry) return;
102
103   if (DstTy == SrcTy) {
104     Entry = DstTy;
105     return;
106   }
107
108   // Check to see if these types are recursively isomorphic and establish a
109   // mapping between them if so.
110   if (!areTypesIsomorphic(DstTy, SrcTy)) {
111     // Oops, they aren't isomorphic.  Just discard this request by rolling out
112     // any speculative mappings we've established.
113     for (unsigned i = 0, e = SpeculativeTypes.size(); i != e; ++i)
114       MappedTypes.erase(SpeculativeTypes[i]);
115   }
116   SpeculativeTypes.clear();
117 }
118
119 /// areTypesIsomorphic - Recursively walk this pair of types, returning true
120 /// if they are isomorphic, false if they are not.
121 bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
122   // Two types with differing kinds are clearly not isomorphic.
123   if (DstTy->getTypeID() != SrcTy->getTypeID()) return false;
124
125   // If we have an entry in the MappedTypes table, then we have our answer.
126   Type *&Entry = MappedTypes[SrcTy];
127   if (Entry)
128     return Entry == DstTy;
129
130   // Two identical types are clearly isomorphic.  Remember this
131   // non-speculatively.
132   if (DstTy == SrcTy) {
133     Entry = DstTy;
134     return true;
135   }
136
137   // Okay, we have two types with identical kinds that we haven't seen before.
138
139   // If this is an opaque struct type, special case it.
140   if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
141     // Mapping an opaque type to any struct, just keep the dest struct.
142     if (SSTy->isOpaque()) {
143       Entry = DstTy;
144       SpeculativeTypes.push_back(SrcTy);
145       return true;
146     }
147
148     // Mapping a non-opaque source type to an opaque dest.  If this is the first
149     // type that we're mapping onto this destination type then we succeed.  Keep
150     // the dest, but fill it in later.  This doesn't need to be speculative.  If
151     // this is the second (different) type that we're trying to map onto the
152     // same opaque type then we fail.
153     if (cast<StructType>(DstTy)->isOpaque()) {
154       // We can only map one source type onto the opaque destination type.
155       if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)))
156         return false;
157       SrcDefinitionsToResolve.push_back(SSTy);
158       Entry = DstTy;
159       return true;
160     }
161   }
162
163   // If the number of subtypes disagree between the two types, then we fail.
164   if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
165     return false;
166
167   // Fail if any of the extra properties (e.g. array size) of the type disagree.
168   if (isa<IntegerType>(DstTy))
169     return false;  // bitwidth disagrees.
170   if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
171     if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
172       return false;
173
174   } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
175     if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
176       return false;
177   } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
178     StructType *SSTy = cast<StructType>(SrcTy);
179     if (DSTy->isLiteral() != SSTy->isLiteral() ||
180         DSTy->isPacked() != SSTy->isPacked())
181       return false;
182   } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
183     if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
184       return false;
185   } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
186     if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
187       return false;
188   }
189
190   // Otherwise, we speculate that these two types will line up and recursively
191   // check the subelements.
192   Entry = DstTy;
193   SpeculativeTypes.push_back(SrcTy);
194
195   for (unsigned i = 0, e = SrcTy->getNumContainedTypes(); i != e; ++i)
196     if (!areTypesIsomorphic(DstTy->getContainedType(i),
197                             SrcTy->getContainedType(i)))
198       return false;
199
200   // If everything seems to have lined up, then everything is great.
201   return true;
202 }
203
204 /// linkDefinedTypeBodies - Produce a body for an opaque type in the dest
205 /// module from a type definition in the source module.
206 void TypeMapTy::linkDefinedTypeBodies() {
207   SmallVector<Type*, 16> Elements;
208   SmallString<16> TmpName;
209
210   // Note that processing entries in this loop (calling 'get') can add new
211   // entries to the SrcDefinitionsToResolve vector.
212   while (!SrcDefinitionsToResolve.empty()) {
213     StructType *SrcSTy = SrcDefinitionsToResolve.pop_back_val();
214     StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
215
216     // TypeMap is a many-to-one mapping, if there were multiple types that
217     // provide a body for DstSTy then previous iterations of this loop may have
218     // already handled it.  Just ignore this case.
219     if (!DstSTy->isOpaque()) continue;
220     assert(!SrcSTy->isOpaque() && "Not resolving a definition?");
221
222     // Map the body of the source type over to a new body for the dest type.
223     Elements.resize(SrcSTy->getNumElements());
224     for (unsigned i = 0, e = Elements.size(); i != e; ++i)
225       Elements[i] = getImpl(SrcSTy->getElementType(i));
226
227     DstSTy->setBody(Elements, SrcSTy->isPacked());
228
229     // If DstSTy has no name or has a longer name than STy, then viciously steal
230     // STy's name.
231     if (!SrcSTy->hasName()) continue;
232     StringRef SrcName = SrcSTy->getName();
233
234     if (!DstSTy->hasName() || DstSTy->getName().size() > SrcName.size()) {
235       TmpName.insert(TmpName.end(), SrcName.begin(), SrcName.end());
236       SrcSTy->setName("");
237       DstSTy->setName(TmpName.str());
238       TmpName.clear();
239     }
240   }
241
242   DstResolvedOpaqueTypes.clear();
243 }
244
245 /// get - Return the mapped type to use for the specified input type from the
246 /// source module.
247 Type *TypeMapTy::get(Type *Ty) {
248   Type *Result = getImpl(Ty);
249
250   // If this caused a reference to any struct type, resolve it before returning.
251   if (!SrcDefinitionsToResolve.empty())
252     linkDefinedTypeBodies();
253   return Result;
254 }
255
256 /// getImpl - This is the recursive version of get().
257 Type *TypeMapTy::getImpl(Type *Ty) {
258   // If we already have an entry for this type, return it.
259   Type **Entry = &MappedTypes[Ty];
260   if (*Entry) return *Entry;
261
262   // If this is not a named struct type, then just map all of the elements and
263   // then rebuild the type from inside out.
264   if (!isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral()) {
265     // If there are no element types to map, then the type is itself.  This is
266     // true for the anonymous {} struct, things like 'float', integers, etc.
267     if (Ty->getNumContainedTypes() == 0)
268       return *Entry = Ty;
269
270     // Remap all of the elements, keeping track of whether any of them change.
271     bool AnyChange = false;
272     SmallVector<Type*, 4> ElementTypes;
273     ElementTypes.resize(Ty->getNumContainedTypes());
274     for (unsigned i = 0, e = Ty->getNumContainedTypes(); i != e; ++i) {
275       ElementTypes[i] = getImpl(Ty->getContainedType(i));
276       AnyChange |= ElementTypes[i] != Ty->getContainedType(i);
277     }
278
279     // If we found our type while recursively processing stuff, just use it.
280     Entry = &MappedTypes[Ty];
281     if (*Entry) return *Entry;
282
283     // If all of the element types mapped directly over, then the type is usable
284     // as-is.
285     if (!AnyChange)
286       return *Entry = Ty;
287
288     // Otherwise, rebuild a modified type.
289     switch (Ty->getTypeID()) {
290     default: llvm_unreachable("unknown derived type to remap");
291     case Type::ArrayTyID:
292       return *Entry = ArrayType::get(ElementTypes[0],
293                                      cast<ArrayType>(Ty)->getNumElements());
294     case Type::VectorTyID:
295       return *Entry = VectorType::get(ElementTypes[0],
296                                       cast<VectorType>(Ty)->getNumElements());
297     case Type::PointerTyID:
298       return *Entry = PointerType::get(ElementTypes[0],
299                                       cast<PointerType>(Ty)->getAddressSpace());
300     case Type::FunctionTyID:
301       return *Entry = FunctionType::get(ElementTypes[0],
302                                         makeArrayRef(ElementTypes).slice(1),
303                                         cast<FunctionType>(Ty)->isVarArg());
304     case Type::StructTyID:
305       // Note that this is only reached for anonymous structs.
306       return *Entry = StructType::get(Ty->getContext(), ElementTypes,
307                                       cast<StructType>(Ty)->isPacked());
308     }
309   }
310
311   // Otherwise, this is an unmapped named struct.  If the struct can be directly
312   // mapped over, just use it as-is.  This happens in a case when the linked-in
313   // module has something like:
314   //   %T = type {%T*, i32}
315   //   @GV = global %T* null
316   // where T does not exist at all in the destination module.
317   //
318   // The other case we watch for is when the type is not in the destination
319   // module, but that it has to be rebuilt because it refers to something that
320   // is already mapped.  For example, if the destination module has:
321   //  %A = type { i32 }
322   // and the source module has something like
323   //  %A' = type { i32 }
324   //  %B = type { %A'* }
325   //  @GV = global %B* null
326   // then we want to create a new type: "%B = type { %A*}" and have it take the
327   // pristine "%B" name from the source module.
328   //
329   // To determine which case this is, we have to recursively walk the type graph
330   // speculating that we'll be able to reuse it unmodified.  Only if this is
331   // safe would we map the entire thing over.  Because this is an optimization,
332   // and is not required for the prettiness of the linked module, we just skip
333   // it and always rebuild a type here.
334   StructType *STy = cast<StructType>(Ty);
335
336   // If the type is opaque, we can just use it directly.
337   if (STy->isOpaque()) {
338     // A named structure type from src module is used. Add it to the Set of
339     // identified structs in the destination module.
340     DstStructTypesSet.insert(STy);
341     return *Entry = STy;
342   }
343
344   // Otherwise we create a new type and resolve its body later.  This will be
345   // resolved by the top level of get().
346   SrcDefinitionsToResolve.push_back(STy);
347   StructType *DTy = StructType::create(STy->getContext());
348   // A new identified structure type was created. Add it to the set of
349   // identified structs in the destination module.
350   DstStructTypesSet.insert(DTy);
351   DstResolvedOpaqueTypes.insert(DTy);
352   return *Entry = DTy;
353 }
354
355 //===----------------------------------------------------------------------===//
356 // ModuleLinker implementation.
357 //===----------------------------------------------------------------------===//
358
359 namespace {
360   class ModuleLinker;
361
362   /// ValueMaterializerTy - Creates prototypes for functions that are lazily
363   /// linked on the fly. This speeds up linking for modules with many
364   /// lazily linked functions of which few get used.
365   class ValueMaterializerTy : public ValueMaterializer {
366     TypeMapTy &TypeMap;
367     Module *DstM;
368     std::vector<Function*> &LazilyLinkFunctions;
369   public:
370     ValueMaterializerTy(TypeMapTy &TypeMap, Module *DstM,
371                         std::vector<Function*> &LazilyLinkFunctions) :
372       ValueMaterializer(), TypeMap(TypeMap), DstM(DstM),
373       LazilyLinkFunctions(LazilyLinkFunctions) {
374     }
375
376     Value *materializeValueFor(Value *V) override;
377   };
378
379   /// ModuleLinker - This is an implementation class for the LinkModules
380   /// function, which is the entrypoint for this file.
381   class ModuleLinker {
382     Module *DstM, *SrcM;
383
384     TypeMapTy TypeMap;
385     ValueMaterializerTy ValMaterializer;
386
387     /// ValueMap - Mapping of values from what they used to be in Src, to what
388     /// they are now in DstM.  ValueToValueMapTy is a ValueMap, which involves
389     /// some overhead due to the use of Value handles which the Linker doesn't
390     /// actually need, but this allows us to reuse the ValueMapper code.
391     ValueToValueMapTy ValueMap;
392
393     struct AppendingVarInfo {
394       GlobalVariable *NewGV;  // New aggregate global in dest module.
395       Constant *DstInit;      // Old initializer from dest module.
396       Constant *SrcInit;      // Old initializer from src module.
397     };
398
399     std::vector<AppendingVarInfo> AppendingVars;
400
401     unsigned Mode; // Mode to treat source module.
402
403     // Set of items not to link in from source.
404     SmallPtrSet<const Value*, 16> DoNotLinkFromSource;
405
406     // Vector of functions to lazily link in.
407     std::vector<Function*> LazilyLinkFunctions;
408
409     bool SuppressWarnings;
410
411   public:
412     std::string ErrorMsg;
413
414     ModuleLinker(Module *dstM, TypeSet &Set, Module *srcM, unsigned mode,
415                  bool SuppressWarnings=false)
416         : DstM(dstM), SrcM(srcM), TypeMap(Set),
417           ValMaterializer(TypeMap, DstM, LazilyLinkFunctions), Mode(mode),
418           SuppressWarnings(SuppressWarnings) {}
419
420     bool run();
421
422   private:
423     /// emitError - Helper method for setting a message and returning an error
424     /// code.
425     bool emitError(const Twine &Message) {
426       ErrorMsg = Message.str();
427       return true;
428     }
429
430     bool getComdatLeader(Module *M, StringRef ComdatName,
431                          const GlobalVariable *&GVar);
432     bool computeResultingSelectionKind(StringRef ComdatName,
433                                        Comdat::SelectionKind Src,
434                                        Comdat::SelectionKind Dst,
435                                        Comdat::SelectionKind &Result,
436                                        bool &LinkFromSrc);
437     std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
438         ComdatsChosen;
439     bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
440                          bool &LinkFromSrc);
441
442     /// getLinkageResult - This analyzes the two global values and determines
443     /// what the result will look like in the destination module.
444     bool getLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
445                           GlobalValue::LinkageTypes &LT,
446                           GlobalValue::VisibilityTypes &Vis,
447                           bool &LinkFromSrc);
448
449     /// getLinkedToGlobal - Given a global in the source module, return the
450     /// global in the destination module that is being linked to, if any.
451     GlobalValue *getLinkedToGlobal(GlobalValue *SrcGV) {
452       // If the source has no name it can't link.  If it has local linkage,
453       // there is no name match-up going on.
454       if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
455         return nullptr;
456
457       // Otherwise see if we have a match in the destination module's symtab.
458       GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName());
459       if (!DGV) return nullptr;
460
461       // If we found a global with the same name in the dest module, but it has
462       // internal linkage, we are really not doing any linkage here.
463       if (DGV->hasLocalLinkage())
464         return nullptr;
465
466       // Otherwise, we do in fact link to the destination global.
467       return DGV;
468     }
469
470     void computeTypeMapping();
471
472     bool linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV);
473     bool linkGlobalProto(GlobalVariable *SrcGV);
474     bool linkFunctionProto(Function *SrcF);
475     bool linkAliasProto(GlobalAlias *SrcA);
476     bool linkModuleFlagsMetadata();
477
478     void linkAppendingVarInit(const AppendingVarInfo &AVI);
479     void linkGlobalInits();
480     void linkFunctionBody(Function *Dst, Function *Src);
481     void linkAliasBodies();
482     void linkNamedMDNodes();
483   };
484 }
485
486 /// forceRenaming - The LLVM SymbolTable class autorenames globals that conflict
487 /// in the symbol table.  This is good for all clients except for us.  Go
488 /// through the trouble to force this back.
489 static void forceRenaming(GlobalValue *GV, StringRef Name) {
490   // If the global doesn't force its name or if it already has the right name,
491   // there is nothing for us to do.
492   if (GV->hasLocalLinkage() || GV->getName() == Name)
493     return;
494
495   Module *M = GV->getParent();
496
497   // If there is a conflict, rename the conflict.
498   if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
499     GV->takeName(ConflictGV);
500     ConflictGV->setName(Name);    // This will cause ConflictGV to get renamed
501     assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
502   } else {
503     GV->setName(Name);              // Force the name back
504   }
505 }
506
507 /// copyGVAttributes - copy additional attributes (those not needed to construct
508 /// a GlobalValue) from the SrcGV to the DestGV.
509 static void copyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
510   // Use the maximum alignment, rather than just copying the alignment of SrcGV.
511   auto *DestGO = dyn_cast<GlobalObject>(DestGV);
512   unsigned Alignment;
513   if (DestGO)
514     Alignment = std::max(DestGO->getAlignment(), SrcGV->getAlignment());
515
516   DestGV->copyAttributesFrom(SrcGV);
517
518   if (DestGO)
519     DestGO->setAlignment(Alignment);
520
521   forceRenaming(DestGV, SrcGV->getName());
522 }
523
524 static bool isLessConstraining(GlobalValue::VisibilityTypes a,
525                                GlobalValue::VisibilityTypes b) {
526   if (a == GlobalValue::HiddenVisibility)
527     return false;
528   if (b == GlobalValue::HiddenVisibility)
529     return true;
530   if (a == GlobalValue::ProtectedVisibility)
531     return false;
532   if (b == GlobalValue::ProtectedVisibility)
533     return true;
534   return false;
535 }
536
537 Value *ValueMaterializerTy::materializeValueFor(Value *V) {
538   Function *SF = dyn_cast<Function>(V);
539   if (!SF)
540     return nullptr;
541
542   Function *DF = Function::Create(TypeMap.get(SF->getFunctionType()),
543                                   SF->getLinkage(), SF->getName(), DstM);
544   copyGVAttributes(DF, SF);
545
546   LazilyLinkFunctions.push_back(SF);
547   return DF;
548 }
549
550 bool ModuleLinker::getComdatLeader(Module *M, StringRef ComdatName,
551                                    const GlobalVariable *&GVar) {
552   const GlobalValue *GVal = M->getNamedValue(ComdatName);
553   if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
554     GVal = GA->getBaseObject();
555     if (!GVal)
556       // We cannot resolve the size of the aliasee yet.
557       return emitError("Linking COMDATs named '" + ComdatName +
558                        "': COMDAT key involves incomputable alias size.");
559   }
560
561   GVar = dyn_cast_or_null<GlobalVariable>(GVal);
562   if (!GVar)
563     return emitError(
564         "Linking COMDATs named '" + ComdatName +
565         "': GlobalVariable required for data dependent selection!");
566
567   return false;
568 }
569
570 bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
571                                                  Comdat::SelectionKind Src,
572                                                  Comdat::SelectionKind Dst,
573                                                  Comdat::SelectionKind &Result,
574                                                  bool &LinkFromSrc) {
575   // The ability to mix Comdat::SelectionKind::Any with
576   // Comdat::SelectionKind::Largest is a behavior that comes from COFF.
577   bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
578                          Dst == Comdat::SelectionKind::Largest;
579   bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
580                          Src == Comdat::SelectionKind::Largest;
581   if (DstAnyOrLargest && SrcAnyOrLargest) {
582     if (Dst == Comdat::SelectionKind::Largest ||
583         Src == Comdat::SelectionKind::Largest)
584       Result = Comdat::SelectionKind::Largest;
585     else
586       Result = Comdat::SelectionKind::Any;
587   } else if (Src == Dst) {
588     Result = Dst;
589   } else {
590     return emitError("Linking COMDATs named '" + ComdatName +
591                      "': invalid selection kinds!");
592   }
593
594   switch (Result) {
595   case Comdat::SelectionKind::Any:
596     // Go with Dst.
597     LinkFromSrc = false;
598     break;
599   case Comdat::SelectionKind::NoDuplicates:
600     return emitError("Linking COMDATs named '" + ComdatName +
601                      "': noduplicates has been violated!");
602   case Comdat::SelectionKind::ExactMatch:
603   case Comdat::SelectionKind::Largest:
604   case Comdat::SelectionKind::SameSize: {
605     const GlobalVariable *DstGV;
606     const GlobalVariable *SrcGV;
607     if (getComdatLeader(DstM, ComdatName, DstGV) ||
608         getComdatLeader(SrcM, ComdatName, SrcGV))
609       return true;
610
611     const DataLayout *DstDL = DstM->getDataLayout();
612     const DataLayout *SrcDL = SrcM->getDataLayout();
613     if (!DstDL || !SrcDL) {
614       return emitError(
615           "Linking COMDATs named '" + ComdatName +
616           "': can't do size dependent selection without DataLayout!");
617     }
618     uint64_t DstSize =
619         DstDL->getTypeAllocSize(DstGV->getType()->getPointerElementType());
620     uint64_t SrcSize =
621         SrcDL->getTypeAllocSize(SrcGV->getType()->getPointerElementType());
622     if (Result == Comdat::SelectionKind::ExactMatch) {
623       if (SrcGV->getInitializer() != DstGV->getInitializer())
624         return emitError("Linking COMDATs named '" + ComdatName +
625                          "': ExactMatch violated!");
626       LinkFromSrc = false;
627     } else if (Result == Comdat::SelectionKind::Largest) {
628       LinkFromSrc = SrcSize > DstSize;
629     } else if (Result == Comdat::SelectionKind::SameSize) {
630       if (SrcSize != DstSize)
631         return emitError("Linking COMDATs named '" + ComdatName +
632                          "': SameSize violated!");
633       LinkFromSrc = false;
634     } else {
635       llvm_unreachable("unknown selection kind");
636     }
637     break;
638   }
639   }
640
641   return false;
642 }
643
644 bool ModuleLinker::getComdatResult(const Comdat *SrcC,
645                                    Comdat::SelectionKind &Result,
646                                    bool &LinkFromSrc) {
647   Comdat::SelectionKind SSK = SrcC->getSelectionKind();
648   StringRef ComdatName = SrcC->getName();
649   Module::ComdatSymTabType &ComdatSymTab = DstM->getComdatSymbolTable();
650   Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
651
652   if (DstCI == ComdatSymTab.end()) {
653     // Use the comdat if it is only available in one of the modules.
654     LinkFromSrc = true;
655     Result = SSK;
656     return false;
657   }
658
659   const Comdat *DstC = &DstCI->second;
660   Comdat::SelectionKind DSK = DstC->getSelectionKind();
661   return computeResultingSelectionKind(ComdatName, SSK, DSK, Result,
662                                        LinkFromSrc);
663 }
664
665 /// getLinkageResult - This analyzes the two global values and determines what
666 /// the result will look like in the destination module.  In particular, it
667 /// computes the resultant linkage type and visibility, computes whether the
668 /// global in the source should be copied over to the destination (replacing
669 /// the existing one), and computes whether this linkage is an error or not.
670 bool ModuleLinker::getLinkageResult(GlobalValue *Dest, const GlobalValue *Src,
671                                     GlobalValue::LinkageTypes &LT,
672                                     GlobalValue::VisibilityTypes &Vis,
673                                     bool &LinkFromSrc) {
674   assert(Dest && "Must have two globals being queried");
675   assert(!Src->hasLocalLinkage() &&
676          "If Src has internal linkage, Dest shouldn't be set!");
677
678   bool SrcIsDeclaration = Src->isDeclaration() && !Src->isMaterializable();
679   bool DestIsDeclaration = Dest->isDeclaration();
680
681   if (SrcIsDeclaration) {
682     // If Src is external or if both Src & Dest are external..  Just link the
683     // external globals, we aren't adding anything.
684     if (Src->hasDLLImportStorageClass()) {
685       // If one of GVs is marked as DLLImport, result should be dllimport'ed.
686       if (DestIsDeclaration) {
687         LinkFromSrc = true;
688         LT = Src->getLinkage();
689       }
690     } else if (Dest->hasExternalWeakLinkage()) {
691       // If the Dest is weak, use the source linkage.
692       LinkFromSrc = true;
693       LT = Src->getLinkage();
694     } else {
695       LinkFromSrc = false;
696       LT = Dest->getLinkage();
697     }
698   } else if (DestIsDeclaration && !Dest->hasDLLImportStorageClass()) {
699     // If Dest is external but Src is not:
700     LinkFromSrc = true;
701     LT = Src->getLinkage();
702   } else if (Src->isWeakForLinker()) {
703     // At this point we know that Dest has LinkOnce, External*, Weak, Common,
704     // or DLL* linkage.
705     if (Dest->hasExternalWeakLinkage() ||
706         Dest->hasAvailableExternallyLinkage() ||
707         (Dest->hasLinkOnceLinkage() &&
708          (Src->hasWeakLinkage() || Src->hasCommonLinkage()))) {
709       LinkFromSrc = true;
710       LT = Src->getLinkage();
711     } else {
712       LinkFromSrc = false;
713       LT = Dest->getLinkage();
714     }
715   } else if (Dest->isWeakForLinker()) {
716     // At this point we know that Src has External* or DLL* linkage.
717     if (Src->hasExternalWeakLinkage()) {
718       LinkFromSrc = false;
719       LT = Dest->getLinkage();
720     } else {
721       LinkFromSrc = true;
722       LT = GlobalValue::ExternalLinkage;
723     }
724   } else {
725     assert((Dest->hasExternalLinkage()  || Dest->hasExternalWeakLinkage()) &&
726            (Src->hasExternalLinkage()   || Src->hasExternalWeakLinkage()) &&
727            "Unexpected linkage type!");
728     return emitError("Linking globals named '" + Src->getName() +
729                  "': symbol multiply defined!");
730   }
731
732   // Compute the visibility. We follow the rules in the System V Application
733   // Binary Interface.
734   assert(!GlobalValue::isLocalLinkage(LT) &&
735          "Symbols with local linkage should not be merged");
736   Vis = isLessConstraining(Src->getVisibility(), Dest->getVisibility()) ?
737     Dest->getVisibility() : Src->getVisibility();
738   return false;
739 }
740
741 /// computeTypeMapping - Loop over all of the linked values to compute type
742 /// mappings.  For example, if we link "extern Foo *x" and "Foo *x = NULL", then
743 /// we have two struct types 'Foo' but one got renamed when the module was
744 /// loaded into the same LLVMContext.
745 void ModuleLinker::computeTypeMapping() {
746   // Incorporate globals.
747   for (Module::global_iterator I = SrcM->global_begin(),
748        E = SrcM->global_end(); I != E; ++I) {
749     GlobalValue *DGV = getLinkedToGlobal(I);
750     if (!DGV) continue;
751
752     if (!DGV->hasAppendingLinkage() || !I->hasAppendingLinkage()) {
753       TypeMap.addTypeMapping(DGV->getType(), I->getType());
754       continue;
755     }
756
757     // Unify the element type of appending arrays.
758     ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
759     ArrayType *SAT = cast<ArrayType>(I->getType()->getElementType());
760     TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
761   }
762
763   // Incorporate functions.
764   for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I) {
765     if (GlobalValue *DGV = getLinkedToGlobal(I))
766       TypeMap.addTypeMapping(DGV->getType(), I->getType());
767   }
768
769   // Incorporate types by name, scanning all the types in the source module.
770   // At this point, the destination module may have a type "%foo = { i32 }" for
771   // example.  When the source module got loaded into the same LLVMContext, if
772   // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
773   TypeFinder SrcStructTypes;
774   SrcStructTypes.run(*SrcM, true);
775   SmallPtrSet<StructType*, 32> SrcStructTypesSet(SrcStructTypes.begin(),
776                                                  SrcStructTypes.end());
777
778   for (unsigned i = 0, e = SrcStructTypes.size(); i != e; ++i) {
779     StructType *ST = SrcStructTypes[i];
780     if (!ST->hasName()) continue;
781
782     // Check to see if there is a dot in the name followed by a digit.
783     size_t DotPos = ST->getName().rfind('.');
784     if (DotPos == 0 || DotPos == StringRef::npos ||
785         ST->getName().back() == '.' ||
786         !isdigit(static_cast<unsigned char>(ST->getName()[DotPos+1])))
787       continue;
788
789     // Check to see if the destination module has a struct with the prefix name.
790     if (StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos)))
791       // Don't use it if this actually came from the source module. They're in
792       // the same LLVMContext after all. Also don't use it unless the type is
793       // actually used in the destination module. This can happen in situations
794       // like this:
795       //
796       //      Module A                         Module B
797       //      --------                         --------
798       //   %Z = type { %A }                %B = type { %C.1 }
799       //   %A = type { %B.1, [7 x i8] }    %C.1 = type { i8* }
800       //   %B.1 = type { %C }              %A.2 = type { %B.3, [5 x i8] }
801       //   %C = type { i8* }               %B.3 = type { %C.1 }
802       //
803       // When we link Module B with Module A, the '%B' in Module B is
804       // used. However, that would then use '%C.1'. But when we process '%C.1',
805       // we prefer to take the '%C' version. So we are then left with both
806       // '%C.1' and '%C' being used for the same types. This leads to some
807       // variables using one type and some using the other.
808       if (!SrcStructTypesSet.count(DST) && TypeMap.DstStructTypesSet.count(DST))
809         TypeMap.addTypeMapping(DST, ST);
810   }
811
812   // Don't bother incorporating aliases, they aren't generally typed well.
813
814   // Now that we have discovered all of the type equivalences, get a body for
815   // any 'opaque' types in the dest module that are now resolved.
816   TypeMap.linkDefinedTypeBodies();
817 }
818
819 /// linkAppendingVarProto - If there were any appending global variables, link
820 /// them together now.  Return true on error.
821 bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
822                                          GlobalVariable *SrcGV) {
823
824   if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
825     return emitError("Linking globals named '" + SrcGV->getName() +
826            "': can only link appending global with another appending global!");
827
828   ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
829   ArrayType *SrcTy =
830     cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
831   Type *EltTy = DstTy->getElementType();
832
833   // Check to see that they two arrays agree on type.
834   if (EltTy != SrcTy->getElementType())
835     return emitError("Appending variables with different element types!");
836   if (DstGV->isConstant() != SrcGV->isConstant())
837     return emitError("Appending variables linked with different const'ness!");
838
839   if (DstGV->getAlignment() != SrcGV->getAlignment())
840     return emitError(
841              "Appending variables with different alignment need to be linked!");
842
843   if (DstGV->getVisibility() != SrcGV->getVisibility())
844     return emitError(
845             "Appending variables with different visibility need to be linked!");
846
847   if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr())
848     return emitError(
849         "Appending variables with different unnamed_addr need to be linked!");
850
851   if (StringRef(DstGV->getSection()) != SrcGV->getSection())
852     return emitError(
853           "Appending variables with different section name need to be linked!");
854
855   uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements();
856   ArrayType *NewType = ArrayType::get(EltTy, NewSize);
857
858   // Create the new global variable.
859   GlobalVariable *NG =
860     new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(),
861                        DstGV->getLinkage(), /*init*/nullptr, /*name*/"", DstGV,
862                        DstGV->getThreadLocalMode(),
863                        DstGV->getType()->getAddressSpace());
864
865   // Propagate alignment, visibility and section info.
866   copyGVAttributes(NG, DstGV);
867
868   AppendingVarInfo AVI;
869   AVI.NewGV = NG;
870   AVI.DstInit = DstGV->getInitializer();
871   AVI.SrcInit = SrcGV->getInitializer();
872   AppendingVars.push_back(AVI);
873
874   // Replace any uses of the two global variables with uses of the new
875   // global.
876   ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
877
878   DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
879   DstGV->eraseFromParent();
880
881   // Track the source variable so we don't try to link it.
882   DoNotLinkFromSource.insert(SrcGV);
883
884   return false;
885 }
886
887 /// linkGlobalProto - Loop through the global variables in the src module and
888 /// merge them into the dest module.
889 bool ModuleLinker::linkGlobalProto(GlobalVariable *SGV) {
890   GlobalValue *DGV = getLinkedToGlobal(SGV);
891   llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility;
892   bool HasUnnamedAddr = SGV->hasUnnamedAddr();
893
894   bool LinkFromSrc = false;
895   Comdat *DC = nullptr;
896   if (const Comdat *SC = SGV->getComdat()) {
897     Comdat::SelectionKind SK;
898     std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
899     DC = DstM->getOrInsertComdat(SC->getName());
900     DC->setSelectionKind(SK);
901   }
902
903   if (DGV) {
904     if (!DC) {
905       // Concatenation of appending linkage variables is magic and handled later.
906       if (DGV->hasAppendingLinkage() || SGV->hasAppendingLinkage())
907         return linkAppendingVarProto(cast<GlobalVariable>(DGV), SGV);
908
909       // Determine whether linkage of these two globals follows the source
910       // module's definition or the destination module's definition.
911       GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
912       GlobalValue::VisibilityTypes NV;
913       if (getLinkageResult(DGV, SGV, NewLinkage, NV, LinkFromSrc))
914         return true;
915       NewVisibility = NV;
916       HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
917
918       // If we're not linking from the source, then keep the definition that we
919       // have.
920       if (!LinkFromSrc) {
921         // Special case for const propagation.
922         if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV))
923           if (DGVar->isDeclaration() && SGV->isConstant() &&
924               !DGVar->isConstant())
925             DGVar->setConstant(true);
926
927         // Set calculated linkage, visibility and unnamed_addr.
928         DGV->setLinkage(NewLinkage);
929         DGV->setVisibility(*NewVisibility);
930         DGV->setUnnamedAddr(HasUnnamedAddr);
931       }
932     }
933
934     if (!LinkFromSrc) {
935       // Make sure to remember this mapping.
936       ValueMap[SGV] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGV->getType()));
937
938       // Track the source global so that we don't attempt to copy it over when
939       // processing global initializers.
940       DoNotLinkFromSource.insert(SGV);
941
942       return false;
943     }
944   }
945
946   // If the Comdat this variable was inside of wasn't selected, skip it.
947   if (DC && !DGV && !LinkFromSrc) {
948     DoNotLinkFromSource.insert(SGV);
949     return false;
950   }
951
952   // No linking to be performed or linking from the source: simply create an
953   // identical version of the symbol over in the dest module... the
954   // initializer will be filled in later by LinkGlobalInits.
955   GlobalVariable *NewDGV =
956     new GlobalVariable(*DstM, TypeMap.get(SGV->getType()->getElementType()),
957                        SGV->isConstant(), SGV->getLinkage(), /*init*/nullptr,
958                        SGV->getName(), /*insertbefore*/nullptr,
959                        SGV->getThreadLocalMode(),
960                        SGV->getType()->getAddressSpace());
961   // Propagate alignment, visibility and section info.
962   copyGVAttributes(NewDGV, SGV);
963   if (NewVisibility)
964     NewDGV->setVisibility(*NewVisibility);
965   NewDGV->setUnnamedAddr(HasUnnamedAddr);
966
967   if (DC)
968     NewDGV->setComdat(DC);
969
970   if (DGV) {
971     DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV, DGV->getType()));
972     DGV->eraseFromParent();
973   }
974
975   // Make sure to remember this mapping.
976   ValueMap[SGV] = NewDGV;
977   return false;
978 }
979
980 /// linkFunctionProto - Link the function in the source module into the
981 /// destination module if needed, setting up mapping information.
982 bool ModuleLinker::linkFunctionProto(Function *SF) {
983   GlobalValue *DGV = getLinkedToGlobal(SF);
984   llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility;
985   bool HasUnnamedAddr = SF->hasUnnamedAddr();
986
987   bool LinkFromSrc = false;
988   Comdat *DC = nullptr;
989   if (const Comdat *SC = SF->getComdat()) {
990     Comdat::SelectionKind SK;
991     std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
992     DC = DstM->getOrInsertComdat(SC->getName());
993     DC->setSelectionKind(SK);
994   }
995
996   if (DGV) {
997     if (!DC) {
998       GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
999       GlobalValue::VisibilityTypes NV;
1000       if (getLinkageResult(DGV, SF, NewLinkage, NV, LinkFromSrc))
1001         return true;
1002       NewVisibility = NV;
1003       HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
1004
1005       if (!LinkFromSrc) {
1006         // Set calculated linkage
1007         DGV->setLinkage(NewLinkage);
1008         DGV->setVisibility(*NewVisibility);
1009         DGV->setUnnamedAddr(HasUnnamedAddr);
1010       }
1011     }
1012
1013     if (!LinkFromSrc) {
1014       // Make sure to remember this mapping.
1015       ValueMap[SF] = ConstantExpr::getBitCast(DGV, TypeMap.get(SF->getType()));
1016
1017       // Track the function from the source module so we don't attempt to remap
1018       // it.
1019       DoNotLinkFromSource.insert(SF);
1020
1021       return false;
1022     }
1023   }
1024
1025   // If the function is to be lazily linked, don't create it just yet.
1026   // The ValueMaterializerTy will deal with creating it if it's used.
1027   if (!DGV && (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() ||
1028                SF->hasAvailableExternallyLinkage())) {
1029     DoNotLinkFromSource.insert(SF);
1030     return false;
1031   }
1032
1033   // If the Comdat this function was inside of wasn't selected, skip it.
1034   if (DC && !DGV && !LinkFromSrc) {
1035     DoNotLinkFromSource.insert(SF);
1036     return false;
1037   }
1038
1039   // If there is no linkage to be performed or we are linking from the source,
1040   // bring SF over.
1041   Function *NewDF = Function::Create(TypeMap.get(SF->getFunctionType()),
1042                                      SF->getLinkage(), SF->getName(), DstM);
1043   copyGVAttributes(NewDF, SF);
1044   if (NewVisibility)
1045     NewDF->setVisibility(*NewVisibility);
1046   NewDF->setUnnamedAddr(HasUnnamedAddr);
1047
1048   if (DC)
1049     NewDF->setComdat(DC);
1050
1051   if (DGV) {
1052     // Any uses of DF need to change to NewDF, with cast.
1053     DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType()));
1054     DGV->eraseFromParent();
1055   }
1056
1057   ValueMap[SF] = NewDF;
1058   return false;
1059 }
1060
1061 /// LinkAliasProto - Set up prototypes for any aliases that come over from the
1062 /// source module.
1063 bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) {
1064   GlobalValue *DGV = getLinkedToGlobal(SGA);
1065   llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility;
1066   bool HasUnnamedAddr = SGA->hasUnnamedAddr();
1067
1068   bool LinkFromSrc = false;
1069   Comdat *DC = nullptr;
1070   if (const Comdat *SC = SGA->getComdat()) {
1071     Comdat::SelectionKind SK;
1072     std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
1073     DC = DstM->getOrInsertComdat(SC->getName());
1074     DC->setSelectionKind(SK);
1075   }
1076
1077   if (DGV) {
1078     if (!DC) {
1079       GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
1080       GlobalValue::VisibilityTypes NV;
1081       if (getLinkageResult(DGV, SGA, NewLinkage, NV, LinkFromSrc))
1082         return true;
1083       NewVisibility = NV;
1084       HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
1085
1086       if (!LinkFromSrc) {
1087         // Set calculated linkage.
1088         DGV->setLinkage(NewLinkage);
1089         DGV->setVisibility(*NewVisibility);
1090         DGV->setUnnamedAddr(HasUnnamedAddr);
1091       }
1092     }
1093
1094     if (!LinkFromSrc) {
1095       // Make sure to remember this mapping.
1096       ValueMap[SGA] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGA->getType()));
1097
1098       // Track the alias from the source module so we don't attempt to remap it.
1099       DoNotLinkFromSource.insert(SGA);
1100
1101       return false;
1102     }
1103   }
1104
1105   // If the Comdat this alias was inside of wasn't selected, skip it.
1106   if (DC && !DGV && !LinkFromSrc) {
1107     DoNotLinkFromSource.insert(SGA);
1108     return false;
1109   }
1110
1111   // If there is no linkage to be performed or we're linking from the source,
1112   // bring over SGA.
1113   auto *PTy = cast<PointerType>(TypeMap.get(SGA->getType()));
1114   auto *NewDA =
1115       GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1116                           SGA->getLinkage(), SGA->getName(), DstM);
1117   copyGVAttributes(NewDA, SGA);
1118   if (NewVisibility)
1119     NewDA->setVisibility(*NewVisibility);
1120   NewDA->setUnnamedAddr(HasUnnamedAddr);
1121
1122   if (DGV) {
1123     // Any uses of DGV need to change to NewDA, with cast.
1124     DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDA, DGV->getType()));
1125     DGV->eraseFromParent();
1126   }
1127
1128   ValueMap[SGA] = NewDA;
1129   return false;
1130 }
1131
1132 static void getArrayElements(Constant *C, SmallVectorImpl<Constant*> &Dest) {
1133   unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
1134
1135   for (unsigned i = 0; i != NumElements; ++i)
1136     Dest.push_back(C->getAggregateElement(i));
1137 }
1138
1139 void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
1140   // Merge the initializer.
1141   SmallVector<Constant*, 16> Elements;
1142   getArrayElements(AVI.DstInit, Elements);
1143
1144   Constant *SrcInit = MapValue(AVI.SrcInit, ValueMap, RF_None, &TypeMap, &ValMaterializer);
1145   getArrayElements(SrcInit, Elements);
1146
1147   ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
1148   AVI.NewGV->setInitializer(ConstantArray::get(NewType, Elements));
1149 }
1150
1151 /// linkGlobalInits - Update the initializers in the Dest module now that all
1152 /// globals that may be referenced are in Dest.
1153 void ModuleLinker::linkGlobalInits() {
1154   // Loop over all of the globals in the src module, mapping them over as we go
1155   for (Module::const_global_iterator I = SrcM->global_begin(),
1156        E = SrcM->global_end(); I != E; ++I) {
1157
1158     // Only process initialized GV's or ones not already in dest.
1159     if (!I->hasInitializer() || DoNotLinkFromSource.count(I)) continue;
1160
1161     // Grab destination global variable.
1162     GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[I]);
1163     // Figure out what the initializer looks like in the dest module.
1164     DGV->setInitializer(MapValue(I->getInitializer(), ValueMap,
1165                                  RF_None, &TypeMap, &ValMaterializer));
1166   }
1167 }
1168
1169 /// linkFunctionBody - Copy the source function over into the dest function and
1170 /// fix up references to values.  At this point we know that Dest is an external
1171 /// function, and that Src is not.
1172 void ModuleLinker::linkFunctionBody(Function *Dst, Function *Src) {
1173   assert(Src && Dst && Dst->isDeclaration() && !Src->isDeclaration());
1174
1175   // Go through and convert function arguments over, remembering the mapping.
1176   Function::arg_iterator DI = Dst->arg_begin();
1177   for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
1178        I != E; ++I, ++DI) {
1179     DI->setName(I->getName());  // Copy the name over.
1180
1181     // Add a mapping to our mapping.
1182     ValueMap[I] = DI;
1183   }
1184
1185   if (Mode == Linker::DestroySource) {
1186     // Splice the body of the source function into the dest function.
1187     Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList());
1188
1189     // At this point, all of the instructions and values of the function are now
1190     // copied over.  The only problem is that they are still referencing values in
1191     // the Source function as operands.  Loop through all of the operands of the
1192     // functions and patch them up to point to the local versions.
1193     for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB)
1194       for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1195         RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries,
1196                          &TypeMap, &ValMaterializer);
1197
1198   } else {
1199     // Clone the body of the function into the dest function.
1200     SmallVector<ReturnInst*, 8> Returns; // Ignore returns.
1201     CloneFunctionInto(Dst, Src, ValueMap, false, Returns, "", nullptr,
1202                       &TypeMap, &ValMaterializer);
1203   }
1204
1205   // There is no need to map the arguments anymore.
1206   for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
1207        I != E; ++I)
1208     ValueMap.erase(I);
1209
1210 }
1211
1212 /// linkAliasBodies - Insert all of the aliases in Src into the Dest module.
1213 void ModuleLinker::linkAliasBodies() {
1214   for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end();
1215        I != E; ++I) {
1216     if (DoNotLinkFromSource.count(I))
1217       continue;
1218     if (Constant *Aliasee = I->getAliasee()) {
1219       GlobalAlias *DA = cast<GlobalAlias>(ValueMap[I]);
1220       Constant *Val =
1221           MapValue(Aliasee, ValueMap, RF_None, &TypeMap, &ValMaterializer);
1222       DA->setAliasee(Val);
1223     }
1224   }
1225 }
1226
1227 /// linkNamedMDNodes - Insert all of the named MDNodes in Src into the Dest
1228 /// module.
1229 void ModuleLinker::linkNamedMDNodes() {
1230   const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1231   for (Module::const_named_metadata_iterator I = SrcM->named_metadata_begin(),
1232        E = SrcM->named_metadata_end(); I != E; ++I) {
1233     // Don't link module flags here. Do them separately.
1234     if (&*I == SrcModFlags) continue;
1235     NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName());
1236     // Add Src elements into Dest node.
1237     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1238       DestNMD->addOperand(MapValue(I->getOperand(i), ValueMap,
1239                                    RF_None, &TypeMap, &ValMaterializer));
1240   }
1241 }
1242
1243 /// linkModuleFlagsMetadata - Merge the linker flags in Src into the Dest
1244 /// module.
1245 bool ModuleLinker::linkModuleFlagsMetadata() {
1246   // If the source module has no module flags, we are done.
1247   const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1248   if (!SrcModFlags) return false;
1249
1250   // If the destination module doesn't have module flags yet, then just copy
1251   // over the source module's flags.
1252   NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
1253   if (DstModFlags->getNumOperands() == 0) {
1254     for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1255       DstModFlags->addOperand(SrcModFlags->getOperand(I));
1256
1257     return false;
1258   }
1259
1260   // First build a map of the existing module flags and requirements.
1261   DenseMap<MDString*, MDNode*> Flags;
1262   SmallSetVector<MDNode*, 16> Requirements;
1263   for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1264     MDNode *Op = DstModFlags->getOperand(I);
1265     ConstantInt *Behavior = cast<ConstantInt>(Op->getOperand(0));
1266     MDString *ID = cast<MDString>(Op->getOperand(1));
1267
1268     if (Behavior->getZExtValue() == Module::Require) {
1269       Requirements.insert(cast<MDNode>(Op->getOperand(2)));
1270     } else {
1271       Flags[ID] = Op;
1272     }
1273   }
1274
1275   // Merge in the flags from the source module, and also collect its set of
1276   // requirements.
1277   bool HasErr = false;
1278   for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1279     MDNode *SrcOp = SrcModFlags->getOperand(I);
1280     ConstantInt *SrcBehavior = cast<ConstantInt>(SrcOp->getOperand(0));
1281     MDString *ID = cast<MDString>(SrcOp->getOperand(1));
1282     MDNode *DstOp = Flags.lookup(ID);
1283     unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1284
1285     // If this is a requirement, add it and continue.
1286     if (SrcBehaviorValue == Module::Require) {
1287       // If the destination module does not already have this requirement, add
1288       // it.
1289       if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
1290         DstModFlags->addOperand(SrcOp);
1291       }
1292       continue;
1293     }
1294
1295     // If there is no existing flag with this ID, just add it.
1296     if (!DstOp) {
1297       Flags[ID] = SrcOp;
1298       DstModFlags->addOperand(SrcOp);
1299       continue;
1300     }
1301
1302     // Otherwise, perform a merge.
1303     ConstantInt *DstBehavior = cast<ConstantInt>(DstOp->getOperand(0));
1304     unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1305
1306     // If either flag has override behavior, handle it first.
1307     if (DstBehaviorValue == Module::Override) {
1308       // Diagnose inconsistent flags which both have override behavior.
1309       if (SrcBehaviorValue == Module::Override &&
1310           SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1311         HasErr |= emitError("linking module flags '" + ID->getString() +
1312                             "': IDs have conflicting override values");
1313       }
1314       continue;
1315     } else if (SrcBehaviorValue == Module::Override) {
1316       // Update the destination flag to that of the source.
1317       DstOp->replaceOperandWith(0, SrcBehavior);
1318       DstOp->replaceOperandWith(2, SrcOp->getOperand(2));
1319       continue;
1320     }
1321
1322     // Diagnose inconsistent merge behavior types.
1323     if (SrcBehaviorValue != DstBehaviorValue) {
1324       HasErr |= emitError("linking module flags '" + ID->getString() +
1325                           "': IDs have conflicting behaviors");
1326       continue;
1327     }
1328
1329     // Perform the merge for standard behavior types.
1330     switch (SrcBehaviorValue) {
1331     case Module::Require:
1332     case Module::Override: llvm_unreachable("not possible");
1333     case Module::Error: {
1334       // Emit an error if the values differ.
1335       if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1336         HasErr |= emitError("linking module flags '" + ID->getString() +
1337                             "': IDs have conflicting values");
1338       }
1339       continue;
1340     }
1341     case Module::Warning: {
1342       // Emit a warning if the values differ.
1343       if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1344         if (!SuppressWarnings) {
1345           errs() << "WARNING: linking module flags '" << ID->getString()
1346                  << "': IDs have conflicting values";
1347         }
1348       }
1349       continue;
1350     }
1351     case Module::Append: {
1352       MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1353       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1354       unsigned NumOps = DstValue->getNumOperands() + SrcValue->getNumOperands();
1355       Value **VP, **Values = VP = new Value*[NumOps];
1356       for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i, ++VP)
1357         *VP = DstValue->getOperand(i);
1358       for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i, ++VP)
1359         *VP = SrcValue->getOperand(i);
1360       DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
1361                                                ArrayRef<Value*>(Values,
1362                                                                 NumOps)));
1363       delete[] Values;
1364       break;
1365     }
1366     case Module::AppendUnique: {
1367       SmallSetVector<Value*, 16> Elts;
1368       MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
1369       MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
1370       for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i)
1371         Elts.insert(DstValue->getOperand(i));
1372       for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
1373         Elts.insert(SrcValue->getOperand(i));
1374       DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
1375                                                ArrayRef<Value*>(Elts.begin(),
1376                                                                 Elts.end())));
1377       break;
1378     }
1379     }
1380   }
1381
1382   // Check all of the requirements.
1383   for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1384     MDNode *Requirement = Requirements[I];
1385     MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1386     Value *ReqValue = Requirement->getOperand(1);
1387
1388     MDNode *Op = Flags[Flag];
1389     if (!Op || Op->getOperand(2) != ReqValue) {
1390       HasErr |= emitError("linking module flags '" + Flag->getString() +
1391                           "': does not have the required value");
1392       continue;
1393     }
1394   }
1395
1396   return HasErr;
1397 }
1398
1399 bool ModuleLinker::run() {
1400   assert(DstM && "Null destination module");
1401   assert(SrcM && "Null source module");
1402
1403   // Inherit the target data from the source module if the destination module
1404   // doesn't have one already.
1405   if (!DstM->getDataLayout() && SrcM->getDataLayout())
1406     DstM->setDataLayout(SrcM->getDataLayout());
1407
1408   // Copy the target triple from the source to dest if the dest's is empty.
1409   if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1410     DstM->setTargetTriple(SrcM->getTargetTriple());
1411
1412   if (SrcM->getDataLayout() && DstM->getDataLayout() &&
1413       *SrcM->getDataLayout() != *DstM->getDataLayout()) {
1414     if (!SuppressWarnings) {
1415       errs() << "WARNING: Linking two modules of different data layouts: '"
1416              << SrcM->getModuleIdentifier() << "' is '"
1417              << SrcM->getDataLayoutStr() << "' whereas '"
1418              << DstM->getModuleIdentifier() << "' is '"
1419              << DstM->getDataLayoutStr() << "'\n";
1420     }
1421   }
1422   if (!SrcM->getTargetTriple().empty() &&
1423       DstM->getTargetTriple() != SrcM->getTargetTriple()) {
1424     if (!SuppressWarnings) {
1425       errs() << "WARNING: Linking two modules of different target triples: "
1426              << SrcM->getModuleIdentifier() << "' is '"
1427              << SrcM->getTargetTriple() << "' whereas '"
1428              << DstM->getModuleIdentifier() << "' is '"
1429              << DstM->getTargetTriple() << "'\n";
1430     }
1431   }
1432
1433   // Append the module inline asm string.
1434   if (!SrcM->getModuleInlineAsm().empty()) {
1435     if (DstM->getModuleInlineAsm().empty())
1436       DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
1437     else
1438       DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+
1439                                SrcM->getModuleInlineAsm());
1440   }
1441
1442   // Loop over all of the linked values to compute type mappings.
1443   computeTypeMapping();
1444
1445   ComdatsChosen.clear();
1446   for (const StringMapEntry<llvm::Comdat> &SMEC : SrcM->getComdatSymbolTable()) {
1447     const Comdat &C = SMEC.getValue();
1448     if (ComdatsChosen.count(&C))
1449       continue;
1450     Comdat::SelectionKind SK;
1451     bool LinkFromSrc;
1452     if (getComdatResult(&C, SK, LinkFromSrc))
1453       return true;
1454     ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
1455   }
1456
1457   // Insert all of the globals in src into the DstM module... without linking
1458   // initializers (which could refer to functions not yet mapped over).
1459   for (Module::global_iterator I = SrcM->global_begin(),
1460        E = SrcM->global_end(); I != E; ++I)
1461     if (linkGlobalProto(I))
1462       return true;
1463
1464   // Link the functions together between the two modules, without doing function
1465   // bodies... this just adds external function prototypes to the DstM
1466   // function...  We do this so that when we begin processing function bodies,
1467   // all of the global values that may be referenced are available in our
1468   // ValueMap.
1469   for (Module::iterator I = SrcM->begin(), E = SrcM->end(); I != E; ++I)
1470     if (linkFunctionProto(I))
1471       return true;
1472
1473   // If there were any aliases, link them now.
1474   for (Module::alias_iterator I = SrcM->alias_begin(),
1475        E = SrcM->alias_end(); I != E; ++I)
1476     if (linkAliasProto(I))
1477       return true;
1478
1479   for (unsigned i = 0, e = AppendingVars.size(); i != e; ++i)
1480     linkAppendingVarInit(AppendingVars[i]);
1481
1482   // Link in the function bodies that are defined in the source module into
1483   // DstM.
1484   for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) {
1485     // Skip if not linking from source.
1486     if (DoNotLinkFromSource.count(SF)) continue;
1487
1488     Function *DF = cast<Function>(ValueMap[SF]);
1489     if (SF->hasPrefixData()) {
1490       // Link in the prefix data.
1491       DF->setPrefixData(MapValue(
1492           SF->getPrefixData(), ValueMap, RF_None, &TypeMap, &ValMaterializer));
1493     }
1494
1495     // Skip if no body (function is external) or materialize.
1496     if (SF->isDeclaration()) {
1497       if (!SF->isMaterializable())
1498         continue;
1499       if (SF->Materialize(&ErrorMsg))
1500         return true;
1501     }
1502
1503     linkFunctionBody(DF, SF);
1504     SF->Dematerialize();
1505   }
1506
1507   // Resolve all uses of aliases with aliasees.
1508   linkAliasBodies();
1509
1510   // Remap all of the named MDNodes in Src into the DstM module. We do this
1511   // after linking GlobalValues so that MDNodes that reference GlobalValues
1512   // are properly remapped.
1513   linkNamedMDNodes();
1514
1515   // Merge the module flags into the DstM module.
1516   if (linkModuleFlagsMetadata())
1517     return true;
1518
1519   // Update the initializers in the DstM module now that all globals that may
1520   // be referenced are in DstM.
1521   linkGlobalInits();
1522
1523   // Process vector of lazily linked in functions.
1524   bool LinkedInAnyFunctions;
1525   do {
1526     LinkedInAnyFunctions = false;
1527
1528     for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(),
1529         E = LazilyLinkFunctions.end(); I != E; ++I) {
1530       Function *SF = *I;
1531       if (!SF)
1532         continue;
1533
1534       Function *DF = cast<Function>(ValueMap[SF]);
1535       if (SF->hasPrefixData()) {
1536         // Link in the prefix data.
1537         DF->setPrefixData(MapValue(SF->getPrefixData(),
1538                                    ValueMap,
1539                                    RF_None,
1540                                    &TypeMap,
1541                                    &ValMaterializer));
1542       }
1543
1544       // Materialize if necessary.
1545       if (SF->isDeclaration()) {
1546         if (!SF->isMaterializable())
1547           continue;
1548         if (SF->Materialize(&ErrorMsg))
1549           return true;
1550       }
1551
1552       // Erase from vector *before* the function body is linked - linkFunctionBody could
1553       // invalidate I.
1554       LazilyLinkFunctions.erase(I);
1555
1556       // Link in function body.
1557       linkFunctionBody(DF, SF);
1558       SF->Dematerialize();
1559
1560       // Set flag to indicate we may have more functions to lazily link in
1561       // since we linked in a function.
1562       LinkedInAnyFunctions = true;
1563       break;
1564     }
1565   } while (LinkedInAnyFunctions);
1566
1567   // Now that all of the types from the source are used, resolve any structs
1568   // copied over to the dest that didn't exist there.
1569   TypeMap.linkDefinedTypeBodies();
1570
1571   return false;
1572 }
1573
1574 Linker::Linker(Module *M, bool SuppressWarnings)
1575     : Composite(M), SuppressWarnings(SuppressWarnings) {
1576   TypeFinder StructTypes;
1577   StructTypes.run(*M, true);
1578   IdentifiedStructTypes.insert(StructTypes.begin(), StructTypes.end());
1579 }
1580
1581 Linker::~Linker() {
1582 }
1583
1584 void Linker::deleteModule() {
1585   delete Composite;
1586   Composite = nullptr;
1587 }
1588
1589 bool Linker::linkInModule(Module *Src, unsigned Mode, std::string *ErrorMsg) {
1590   ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src, Mode,
1591                          SuppressWarnings);
1592   if (TheLinker.run()) {
1593     if (ErrorMsg)
1594       *ErrorMsg = TheLinker.ErrorMsg;
1595     return true;
1596   }
1597   return false;
1598 }
1599
1600 //===----------------------------------------------------------------------===//
1601 // LinkModules entrypoint.
1602 //===----------------------------------------------------------------------===//
1603
1604 /// LinkModules - This function links two modules together, with the resulting
1605 /// Dest module modified to be the composite of the two input modules.  If an
1606 /// error occurs, true is returned and ErrorMsg (if not null) is set to indicate
1607 /// the problem.  Upon failure, the Dest module could be in a modified state,
1608 /// and shouldn't be relied on to be consistent.
1609 bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode,
1610                          std::string *ErrorMsg) {
1611   Linker L(Dest);
1612   return L.linkInModule(Src, Mode, ErrorMsg);
1613 }
1614
1615 //===----------------------------------------------------------------------===//
1616 // C API.
1617 //===----------------------------------------------------------------------===//
1618
1619 LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
1620                          LLVMLinkerMode Mode, char **OutMessages) {
1621   std::string Messages;
1622   LLVMBool Result = Linker::LinkModules(unwrap(Dest), unwrap(Src),
1623                                         Mode, OutMessages? &Messages : nullptr);
1624   if (OutMessages)
1625     *OutMessages = strdup(Messages.c_str());
1626   return Result;
1627 }