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

Generated by: LCOV version 1.13