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

Generated by: LCOV version 1.13