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

Generated by: LCOV version 1.13