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