LCOV - code coverage report
Current view: top level - lib/Linker - IRMover.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 527 551 95.6 %
Date: 2018-07-13 00:08:38 Functions: 57 59 96.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13