LCOV - code coverage report
Current view: top level - lib/Bitcode/Reader - MetadataLoader.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 845 965 87.6 %
Date: 2017-09-14 15:23:50 Functions: 54 56 96.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- MetadataLoader.cpp - Internal BitcodeReader implementation ---------===//
       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 "MetadataLoader.h"
      11             : #include "ValueList.h"
      12             : 
      13             : #include "llvm/ADT/APFloat.h"
      14             : #include "llvm/ADT/APInt.h"
      15             : #include "llvm/ADT/ArrayRef.h"
      16             : #include "llvm/ADT/DenseMap.h"
      17             : #include "llvm/ADT/DenseSet.h"
      18             : #include "llvm/ADT/None.h"
      19             : #include "llvm/ADT/STLExtras.h"
      20             : #include "llvm/ADT/SmallString.h"
      21             : #include "llvm/ADT/SmallVector.h"
      22             : #include "llvm/ADT/Statistic.h"
      23             : #include "llvm/ADT/StringRef.h"
      24             : #include "llvm/ADT/Triple.h"
      25             : #include "llvm/ADT/Twine.h"
      26             : #include "llvm/Bitcode/BitcodeReader.h"
      27             : #include "llvm/Bitcode/BitstreamReader.h"
      28             : #include "llvm/Bitcode/LLVMBitCodes.h"
      29             : #include "llvm/IR/Argument.h"
      30             : #include "llvm/IR/Attributes.h"
      31             : #include "llvm/IR/AutoUpgrade.h"
      32             : #include "llvm/IR/BasicBlock.h"
      33             : #include "llvm/IR/CallSite.h"
      34             : #include "llvm/IR/CallingConv.h"
      35             : #include "llvm/IR/Comdat.h"
      36             : #include "llvm/IR/Constant.h"
      37             : #include "llvm/IR/Constants.h"
      38             : #include "llvm/IR/DebugInfo.h"
      39             : #include "llvm/IR/DebugInfoMetadata.h"
      40             : #include "llvm/IR/DebugLoc.h"
      41             : #include "llvm/IR/DerivedTypes.h"
      42             : #include "llvm/IR/DiagnosticInfo.h"
      43             : #include "llvm/IR/DiagnosticPrinter.h"
      44             : #include "llvm/IR/Function.h"
      45             : #include "llvm/IR/GVMaterializer.h"
      46             : #include "llvm/IR/GlobalAlias.h"
      47             : #include "llvm/IR/GlobalIFunc.h"
      48             : #include "llvm/IR/GlobalIndirectSymbol.h"
      49             : #include "llvm/IR/GlobalObject.h"
      50             : #include "llvm/IR/GlobalValue.h"
      51             : #include "llvm/IR/GlobalVariable.h"
      52             : #include "llvm/IR/InlineAsm.h"
      53             : #include "llvm/IR/InstrTypes.h"
      54             : #include "llvm/IR/Instruction.h"
      55             : #include "llvm/IR/Instructions.h"
      56             : #include "llvm/IR/IntrinsicInst.h"
      57             : #include "llvm/IR/Intrinsics.h"
      58             : #include "llvm/IR/LLVMContext.h"
      59             : #include "llvm/IR/Module.h"
      60             : #include "llvm/IR/ModuleSummaryIndex.h"
      61             : #include "llvm/IR/OperandTraits.h"
      62             : #include "llvm/IR/Operator.h"
      63             : #include "llvm/IR/TrackingMDRef.h"
      64             : #include "llvm/IR/Type.h"
      65             : #include "llvm/IR/ValueHandle.h"
      66             : #include "llvm/Support/AtomicOrdering.h"
      67             : #include "llvm/Support/Casting.h"
      68             : #include "llvm/Support/CommandLine.h"
      69             : #include "llvm/Support/Compiler.h"
      70             : #include "llvm/Support/Debug.h"
      71             : #include "llvm/Support/Error.h"
      72             : #include "llvm/Support/ErrorHandling.h"
      73             : #include "llvm/Support/ManagedStatic.h"
      74             : #include "llvm/Support/MemoryBuffer.h"
      75             : #include "llvm/Support/raw_ostream.h"
      76             : #include <algorithm>
      77             : #include <cassert>
      78             : #include <cstddef>
      79             : #include <cstdint>
      80             : #include <deque>
      81             : #include <limits>
      82             : #include <map>
      83             : #include <memory>
      84             : #include <string>
      85             : #include <system_error>
      86             : #include <tuple>
      87             : #include <utility>
      88             : #include <vector>
      89             : 
      90             : using namespace llvm;
      91             : 
      92             : #define DEBUG_TYPE "bitcode-reader"
      93             : 
      94             : STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded");
      95             : STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created");
      96             : STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded");
      97             : 
      98             : /// Flag whether we need to import full type definitions for ThinLTO.
      99             : /// Currently needed for Darwin and LLDB.
     100       72330 : static cl::opt<bool> ImportFullTypeDefinitions(
     101      216990 :     "import-full-type-definitions", cl::init(false), cl::Hidden,
     102      289320 :     cl::desc("Import full type definitions for ThinLTO."));
     103             : 
     104       72330 : static cl::opt<bool> DisableLazyLoading(
     105      216990 :     "disable-ondemand-mds-loading", cl::init(false), cl::Hidden,
     106      216990 :     cl::desc("Force disable the lazy-loading on-demand of metadata when "
     107      216990 :              "loading bitcode for importing."));
     108             : 
     109             : namespace {
     110             : 
     111          55 : static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
     112             : 
     113       11492 : class BitcodeReaderMetadataList {
     114             :   /// Array of metadata references.
     115             :   ///
     116             :   /// Don't use std::vector here.  Some versions of libc++ copy (instead of
     117             :   /// move) on resize, and TrackingMDRef is very expensive to copy.
     118             :   SmallVector<TrackingMDRef, 1> MetadataPtrs;
     119             : 
     120             :   /// The set of indices in MetadataPtrs above of forward references that were
     121             :   /// generated.
     122             :   SmallDenseSet<unsigned, 1> ForwardReference;
     123             : 
     124             :   /// The set of indices in MetadataPtrs above of Metadata that need to be
     125             :   /// resolved.
     126             :   SmallDenseSet<unsigned, 1> UnresolvedNodes;
     127             : 
     128             :   /// Structures for resolving old type refs.
     129       25888 :   struct {
     130             :     SmallDenseMap<MDString *, TempMDTuple, 1> Unknown;
     131             :     SmallDenseMap<MDString *, DICompositeType *, 1> Final;
     132             :     SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls;
     133             :     SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays;
     134             :   } OldTypeRefs;
     135             : 
     136             :   LLVMContext &Context;
     137             : 
     138             : public:
     139       11519 :   BitcodeReaderMetadataList(LLVMContext &C) : Context(C) {}
     140             : 
     141             :   // vector compatibility methods
     142       75616 :   unsigned size() const { return MetadataPtrs.size(); }
     143         283 :   void resize(unsigned N) { MetadataPtrs.resize(N); }
     144       13911 :   void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); }
     145             :   void clear() { MetadataPtrs.clear(); }
     146             :   Metadata *back() const { return MetadataPtrs.back(); }
     147             :   void pop_back() { MetadataPtrs.pop_back(); }
     148          10 :   bool empty() const { return MetadataPtrs.empty(); }
     149             : 
     150             :   Metadata *operator[](unsigned i) const {
     151             :     assert(i < MetadataPtrs.size());
     152             :     return MetadataPtrs[i];
     153             :   }
     154             : 
     155             :   Metadata *lookup(unsigned I) const {
     156       64370 :     if (I < MetadataPtrs.size())
     157       92940 :       return MetadataPtrs[I];
     158             :     return nullptr;
     159             :   }
     160             : 
     161             :   void shrinkTo(unsigned N) {
     162             :     assert(N <= size() && "Invalid shrinkTo request!");
     163             :     assert(ForwardReference.empty() && "Unexpected forward refs");
     164             :     assert(UnresolvedNodes.empty() && "Unexpected unresolved node");
     165        7075 :     MetadataPtrs.resize(N);
     166             :   }
     167             : 
     168             :   /// Return the given metadata, creating a replaceable forward reference if
     169             :   /// necessary.
     170             :   Metadata *getMetadataFwdRef(unsigned Idx);
     171             : 
     172             :   /// Return the the given metadata only if it is fully resolved.
     173             :   ///
     174             :   /// Gives the same result as \a lookup(), unless \a MDNode::isResolved()
     175             :   /// would give \c false.
     176             :   Metadata *getMetadataIfResolved(unsigned Idx);
     177             : 
     178             :   MDNode *getMDNodeFwdRefOrNull(unsigned Idx);
     179             :   void assignValue(Metadata *MD, unsigned Idx);
     180             :   void tryToResolveCycles();
     181       31668 :   bool hasFwdRefs() const { return !ForwardReference.empty(); }
     182             :   int getNextFwdRef() {
     183             :     assert(hasFwdRefs());
     184          12 :     return *ForwardReference.begin();
     185             :   }
     186             : 
     187             :   /// Upgrade a type that had an MDString reference.
     188             :   void addTypeRef(MDString &UUID, DICompositeType &CT);
     189             : 
     190             :   /// Upgrade a type that had an MDString reference.
     191             :   Metadata *upgradeTypeRef(Metadata *MaybeUUID);
     192             : 
     193             :   /// Upgrade a type ref array that may have MDString references.
     194             :   Metadata *upgradeTypeRefArray(Metadata *MaybeTuple);
     195             : 
     196             : private:
     197             :   Metadata *resolveTypeRefArray(Metadata *MaybeTuple);
     198             : };
     199             : 
     200       14961 : void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) {
     201        7077 :   if (auto *MDN = dyn_cast<MDNode>(MD))
     202        1356 :     if (!MDN->isResolved())
     203        2712 :       UnresolvedNodes.insert(Idx);
     204             : 
     205       14961 :   if (Idx == size()) {
     206             :     push_back(MD);
     207             :     return;
     208             :   }
     209             : 
     210        1050 :   if (Idx >= size())
     211           0 :     resize(Idx + 1);
     212             : 
     213        2100 :   TrackingMDRef &OldMD = MetadataPtrs[Idx];
     214        1050 :   if (!OldMD) {
     215         178 :     OldMD.reset(MD);
     216         178 :     return;
     217             :   }
     218             : 
     219             :   // If there was a forward reference to this value, replace it.
     220        3488 :   TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
     221        1744 :   PrevMD->replaceAllUsesWith(MD);
     222        1744 :   ForwardReference.erase(Idx);
     223             : }
     224             : 
     225       13639 : Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) {
     226       13639 :   if (Idx >= size())
     227         281 :     resize(Idx + 1);
     228             : 
     229       40917 :   if (Metadata *MD = MetadataPtrs[Idx])
     230             :     return MD;
     231             : 
     232             :   // Track forward refs to be resolved later.
     233        1744 :   ForwardReference.insert(Idx);
     234             : 
     235             :   // Create and return a placeholder, which will later be RAUW'd.
     236         872 :   ++NumMDNodeTemporary;
     237        3488 :   Metadata *MD = MDNode::getTemporary(Context, None).release();
     238        1744 :   MetadataPtrs[Idx].reset(MD);
     239         872 :   return MD;
     240             : }
     241             : 
     242        2478 : Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) {
     243        1550 :   Metadata *MD = lookup(Idx);
     244        1823 :   if (auto *N = dyn_cast_or_null<MDNode>(MD))
     245             :     if (!N->isResolved())
     246             :       return nullptr;
     247             :   return MD;
     248             : }
     249             : 
     250             : MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) {
     251       23474 :   return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx));
     252             : }
     253             : 
     254        1365 : void BitcodeReaderMetadataList::tryToResolveCycles() {
     255        2730 :   if (!ForwardReference.empty())
     256             :     // Still forward references... can't resolve cycles.
     257             :     return;
     258             : 
     259             :   // Give up on finding a full definition for any forward decls that remain.
     260        4095 :   for (const auto &Ref : OldTypeRefs.FwdDecls)
     261           0 :     OldTypeRefs.Final.insert(Ref);
     262        1365 :   OldTypeRefs.FwdDecls.clear();
     263             : 
     264             :   // Upgrade from old type ref arrays.  In strange cases, this could add to
     265             :   // OldTypeRefs.Unknown.
     266        4095 :   for (const auto &Array : OldTypeRefs.Arrays)
     267           0 :     Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get()));
     268        2730 :   OldTypeRefs.Arrays.clear();
     269             : 
     270             :   // Replace old string-based type refs with the resolved node, if possible.
     271             :   // If we haven't seen the node, leave it to the verifier to complain about
     272             :   // the invalid string reference.
     273        4097 :   for (const auto &Ref : OldTypeRefs.Unknown) {
     274           4 :     if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first))
     275           4 :       Ref.second->replaceAllUsesWith(CT);
     276             :     else
     277           0 :       Ref.second->replaceAllUsesWith(Ref.first);
     278             :   }
     279        1365 :   OldTypeRefs.Unknown.clear();
     280             : 
     281        2730 :   if (UnresolvedNodes.empty())
     282             :     // Nothing to do.
     283             :     return;
     284             : 
     285             :   // Resolve any cycles.
     286        4188 :   for (unsigned I : UnresolvedNodes) {
     287        2712 :     auto &MD = MetadataPtrs[I];
     288        1356 :     auto *N = dyn_cast_or_null<MDNode>(MD);
     289           0 :     if (!N)
     290           0 :       continue;
     291             : 
     292             :     assert(!N->isTemporary() && "Unexpected forward reference");
     293        1356 :     N->resolveCycles();
     294             :   }
     295             : 
     296             :   // Make sure we return early again until there's another unresolved ref.
     297          24 :   UnresolvedNodes.clear();
     298             : }
     299             : 
     300           6 : void BitcodeReaderMetadataList::addTypeRef(MDString &UUID,
     301             :                                            DICompositeType &CT) {
     302             :   assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID");
     303          12 :   if (CT.isForwardDecl())
     304           0 :     OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT));
     305             :   else
     306          18 :     OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT));
     307           6 : }
     308             : 
     309        1702 : Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) {
     310        1702 :   auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID);
     311        1702 :   if (LLVM_LIKELY(!UUID))
     312             :     return MaybeUUID;
     313             : 
     314          68 :   if (auto *CT = OldTypeRefs.Final.lookup(UUID))
     315             :     return CT;
     316             : 
     317           4 :   auto &Ref = OldTypeRefs.Unknown[UUID];
     318           2 :   if (!Ref)
     319           4 :     Ref = MDNode::getTemporary(Context, None);
     320           2 :   return Ref.get();
     321             : }
     322             : 
     323           4 : Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) {
     324           4 :   auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
     325           4 :   if (!Tuple || Tuple->isDistinct())
     326             :     return MaybeTuple;
     327             : 
     328             :   // Look through the array immediately if possible.
     329           4 :   if (!Tuple->isTemporary())
     330           4 :     return resolveTypeRefArray(Tuple);
     331             : 
     332             :   // Create and return a placeholder to use for now.  Eventually
     333             :   // resolveTypeRefArrays() will be resolve this forward reference.
     334           0 :   OldTypeRefs.Arrays.emplace_back(
     335           0 :       std::piecewise_construct, std::forward_as_tuple(Tuple),
     336           0 :       std::forward_as_tuple(MDTuple::getTemporary(Context, None)));
     337           0 :   return OldTypeRefs.Arrays.back().second.get();
     338             : }
     339             : 
     340           4 : Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) {
     341           4 :   auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
     342           4 :   if (!Tuple || Tuple->isDistinct())
     343             :     return MaybeTuple;
     344             : 
     345             :   // Look through the DITypeRefArray, upgrading each DITypeRef.
     346           4 :   SmallVector<Metadata *, 32> Ops;
     347           4 :   Ops.reserve(Tuple->getNumOperands());
     348          16 :   for (Metadata *MD : Tuple->operands())
     349           4 :     Ops.push_back(upgradeTypeRef(MD));
     350             : 
     351           8 :   return MDTuple::get(Context, Ops);
     352             : }
     353             : 
     354             : namespace {
     355             : 
     356        2728 : class PlaceholderQueue {
     357             :   // Placeholders would thrash around when moved, so store in a std::deque
     358             :   // instead of some sort of vector.
     359             :   std::deque<DistinctMDOperandPlaceholder> PHs;
     360             : 
     361             : public:
     362        1364 :   ~PlaceholderQueue() {
     363             :     assert(empty() && "PlaceholderQueue hasn't been flushed before being destroyed");
     364             :   }
     365             :   bool empty() { return PHs.empty(); }
     366             :   DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID);
     367             :   void flush(BitcodeReaderMetadataList &MetadataList);
     368             : 
     369             :   /// Return the list of temporaries nodes in the queue, these need to be
     370             :   /// loaded before we can flush the queue.
     371        1368 :   void getTemporaries(BitcodeReaderMetadataList &MetadataList,
     372             :                       DenseSet<unsigned> &Temporaries) {
     373        5066 :     for (auto &PH : PHs) {
     374         962 :       auto ID = PH.getID();
     375        1924 :       auto *MD = MetadataList.lookup(ID);
     376         972 :       if (!MD) {
     377          20 :         Temporaries.insert(ID);
     378          10 :         continue;
     379             :       }
     380         952 :       auto *N = dyn_cast_or_null<MDNode>(MD);
     381         952 :       if (N && N->isTemporary())
     382           0 :         Temporaries.insert(ID);
     383             :     }
     384        1368 :   }
     385             : };
     386             : 
     387             : } // end anonymous namespace
     388             : 
     389             : DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) {
     390         944 :   PHs.emplace_back(ID);
     391        1888 :   return PHs.back();
     392             : }
     393             : 
     394        1365 : void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) {
     395        5562 :   while (!PHs.empty()) {
     396        2832 :     auto *MD = MetadataList.lookup(PHs.front().getID());
     397             :     assert(MD && "Flushing placeholder on unassigned MD");
     398             : #ifndef NDEBUG
     399             :     if (auto *MDN = dyn_cast<MDNode>(MD))
     400             :       assert(MDN->isResolved() &&
     401             :              "Flushing Placeholder while cycles aren't resolved");
     402             : #endif
     403        1888 :     PHs.front().replaceUseWith(MD);
     404         944 :     PHs.pop_front();
     405             :   }
     406        1365 : }
     407             : 
     408             : } // anonynous namespace
     409             : 
     410           0 : static Error error(const Twine &Message) {
     411             :   return make_error<StringError>(
     412           0 :       Message, make_error_code(BitcodeError::CorruptedBitcode));
     413             : }
     414             : 
     415       22984 : class MetadataLoader::MetadataLoaderImpl {
     416             :   BitcodeReaderMetadataList MetadataList;
     417             :   BitcodeReaderValueList &ValueList;
     418             :   BitstreamCursor &Stream;
     419             :   LLVMContext &Context;
     420             :   Module &TheModule;
     421             :   std::function<Type *(unsigned)> getTypeByID;
     422             : 
     423             :   /// Cursor associated with the lazy-loading of Metadata. This is the easy way
     424             :   /// to keep around the right "context" (Abbrev list) to be able to jump in
     425             :   /// the middle of the metadata block and load any record.
     426             :   BitstreamCursor IndexCursor;
     427             : 
     428             :   /// Index that keeps track of MDString values.
     429             :   std::vector<StringRef> MDStringRef;
     430             : 
     431             :   /// On-demand loading of a single MDString. Requires the index above to be
     432             :   /// populated.
     433             :   MDString *lazyLoadOneMDString(unsigned Idx);
     434             : 
     435             :   /// Index that keeps track of where to find a metadata record in the stream.
     436             :   std::vector<uint64_t> GlobalMetadataBitPosIndex;
     437             : 
     438             :   /// Populate the index above to enable lazily loading of metadata, and load
     439             :   /// the named metadata as well as the transitively referenced global
     440             :   /// Metadata.
     441             :   Expected<bool> lazyLoadModuleMetadataBlock();
     442             : 
     443             :   /// On-demand loading of a single metadata. Requires the index above to be
     444             :   /// populated.
     445             :   void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders);
     446             : 
     447             :   // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to
     448             :   // point from SP to CU after a block is completly parsed.
     449             :   std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms;
     450             : 
     451             :   /// Functions that need to be matched with subprograms when upgrading old
     452             :   /// metadata.
     453             :   SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
     454             : 
     455             :   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
     456             :   DenseMap<unsigned, unsigned> MDKindMap;
     457             : 
     458             :   bool StripTBAA = false;
     459             :   bool HasSeenOldLoopTags = false;
     460             :   bool NeedUpgradeToDIGlobalVariableExpression = false;
     461             :   bool NeedDeclareExpressionUpgrade = false;
     462             : 
     463             :   /// True if metadata is being parsed for a module being ThinLTO imported.
     464             :   bool IsImporting = false;
     465             : 
     466             :   Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code,
     467             :                          PlaceholderQueue &Placeholders, StringRef Blob,
     468             :                          unsigned &NextMetadataNo);
     469             :   Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob,
     470             :                              function_ref<void(StringRef)> CallBack);
     471             :   Error parseGlobalObjectAttachment(GlobalObject &GO,
     472             :                                     ArrayRef<uint64_t> Record);
     473             :   Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
     474             : 
     475             :   void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders);
     476             : 
     477             :   /// Upgrade old-style CU <-> SP pointers to point from SP to CU.
     478        1055 :   void upgradeCUSubprograms() {
     479        4225 :     for (auto CU_SP : CUSubprograms)
     480          10 :       if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second))
     481          14 :         for (auto &Op : SPs->operands())
     482           4 :           if (auto *SP = dyn_cast_or_null<DISubprogram>(Op))
     483           4 :             SP->replaceUnit(CU_SP.first);
     484        2110 :     CUSubprograms.clear();
     485        1055 :   }
     486             : 
     487             :   /// Upgrade old-style bare DIGlobalVariables to DIGlobalVariableExpressions.
     488        1055 :   void upgradeCUVariables() {
     489        1055 :     if (!NeedUpgradeToDIGlobalVariableExpression)
     490             :       return;
     491             : 
     492             :     // Upgrade list of variables attached to the CUs.
     493          22 :     if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu"))
     494          18 :       for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) {
     495          18 :         auto *CU = cast<DICompileUnit>(CUNodes->getOperand(I));
     496           9 :         if (auto *GVs = dyn_cast_or_null<MDTuple>(CU->getRawGlobalVariables()))
     497          35 :           for (unsigned I = 0; I < GVs->getNumOperands(); I++)
     498             :             if (auto *GV =
     499          31 :                     dyn_cast_or_null<DIGlobalVariable>(GVs->getOperand(I))) {
     500           5 :               auto *DGVE = DIGlobalVariableExpression::getDistinct(
     501          15 :                   Context, GV, DIExpression::get(Context, {}));
     502           5 :               GVs->replaceOperandWith(I, DGVE);
     503             :             }
     504             :       }
     505             : 
     506             :     // Upgrade variables attached to globals.
     507          34 :     for (auto &GV : TheModule.globals()) {
     508          24 :       SmallVector<MDNode *, 1> MDs;
     509          12 :       GV.getMetadata(LLVMContext::MD_dbg, MDs);
     510          12 :       GV.eraseMetadata(LLVMContext::MD_dbg);
     511          48 :       for (auto *MD : MDs)
     512           3 :         if (auto *DGV = dyn_cast_or_null<DIGlobalVariable>(MD)) {
     513           3 :           auto *DGVE = DIGlobalVariableExpression::getDistinct(
     514           9 :               Context, DGV, DIExpression::get(Context, {}));
     515           3 :           GV.addMetadata(LLVMContext::MD_dbg, *DGVE);
     516             :         } else
     517           9 :           GV.addMetadata(LLVMContext::MD_dbg, *MD);
     518             :     }
     519             :   }
     520             : 
     521             :   /// Remove a leading DW_OP_deref from DIExpressions in a dbg.declare that
     522             :   /// describes a function argument.
     523       31178 :   void upgradeDeclareExpressions(Function &F) {
     524       31178 :     if (!NeedDeclareExpressionUpgrade)
     525             :       return;
     526             : 
     527          21 :     for (auto &BB : F)
     528          30 :       for (auto &I : BB)
     529           7 :         if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
     530          14 :           if (auto *DIExpr = DDI->getExpression())
     531           1 :             if (DIExpr->startsWithDeref() &&
     532           2 :                 dyn_cast_or_null<Argument>(DDI->getAddress())) {
     533           2 :               SmallVector<uint64_t, 8> Ops;
     534           3 :               Ops.append(std::next(DIExpr->elements_begin()),
     535             :                          DIExpr->elements_end());
     536           2 :               auto *E = DIExpression::get(Context, Ops);
     537           2 :               DDI->setOperand(2, MetadataAsValue::get(Context, E));
     538             :             }
     539             :   }
     540             : 
     541             :   /// Upgrade the expression from previous versions.
     542          94 :   Error upgradeDIExpression(uint64_t FromVersion,
     543             :                             MutableArrayRef<uint64_t> &Expr,
     544             :                             SmallVectorImpl<uint64_t> &Buffer) {
     545          94 :     auto N = Expr.size();
     546          94 :     switch (FromVersion) {
     547           0 :     default:
     548           0 :       return error("Invalid record");
     549           3 :     case 0:
     550           4 :       if (N >= 3 && Expr[N - 3] == dwarf::DW_OP_bit_piece)
     551           2 :         Expr[N - 3] = dwarf::DW_OP_LLVM_fragment;
     552             :       LLVM_FALLTHROUGH;
     553             :     case 1:
     554             :       // Move DW_OP_deref to the end.
     555          23 :       if (N && Expr[0] == dwarf::DW_OP_deref) {
     556           4 :         auto End = Expr.end();
     557           6 :         if (Expr.size() >= 3 &&
     558           2 :             *std::prev(End, 3) == dwarf::DW_OP_LLVM_fragment)
     559           1 :           End = std::prev(End, 3);
     560          16 :         std::move(std::next(Expr.begin()), End, Expr.begin());
     561           4 :         *std::prev(End) = dwarf::DW_OP_deref;
     562             :       }
     563          13 :       NeedDeclareExpressionUpgrade = true;
     564             :       LLVM_FALLTHROUGH;
     565          14 :     case 2: {
     566             :       // Change DW_OP_plus to DW_OP_plus_uconst.
     567             :       // Change DW_OP_minus to DW_OP_uconst, DW_OP_minus
     568          14 :       auto SubExpr = ArrayRef<uint64_t>(Expr);
     569          48 :       while (!SubExpr.empty()) {
     570             :         // Skip past other operators with their operands
     571             :         // for this version of the IR, obtained from
     572             :         // from historic DIExpression::ExprOperand::getSize().
     573             :         size_t HistoricSize;
     574          17 :         switch (SubExpr.front()) {
     575           7 :         default:
     576           7 :           HistoricSize = 1;
     577           7 :           break;
     578           8 :         case dwarf::DW_OP_constu:
     579             :         case dwarf::DW_OP_minus:
     580             :         case dwarf::DW_OP_plus:
     581           8 :           HistoricSize = 2;
     582           8 :           break;
     583           2 :         case dwarf::DW_OP_LLVM_fragment:
     584           2 :           HistoricSize = 3;
     585           2 :           break;
     586             :         }
     587             : 
     588             :         // If the expression is malformed, make sure we don't
     589             :         // copy more elements than we should.
     590          34 :         HistoricSize = std::min(SubExpr.size(), HistoricSize);
     591          34 :         ArrayRef<uint64_t> Args = SubExpr.slice(1, HistoricSize-1);
     592             : 
     593          17 :         switch (SubExpr.front()) {
     594           5 :         case dwarf::DW_OP_plus:
     595           5 :           Buffer.push_back(dwarf::DW_OP_plus_uconst);
     596           5 :           Buffer.append(Args.begin(), Args.end());
     597           5 :           break;
     598           1 :         case dwarf::DW_OP_minus:
     599           1 :           Buffer.push_back(dwarf::DW_OP_constu);
     600           1 :           Buffer.append(Args.begin(), Args.end());
     601           1 :           Buffer.push_back(dwarf::DW_OP_minus);
     602           1 :           break;
     603          11 :         default:
     604          11 :           Buffer.push_back(*SubExpr.begin());
     605          11 :           Buffer.append(Args.begin(), Args.end());
     606          11 :           break;
     607             :         }
     608             : 
     609             :         // Continue with remaining elements.
     610          34 :         SubExpr = SubExpr.slice(HistoricSize);
     611             :       }
     612          14 :       Expr = MutableArrayRef<uint64_t>(Buffer);
     613             :       LLVM_FALLTHROUGH;
     614             :     }
     615             :     case 3:
     616             :       // Up-to-date!
     617             :       break;
     618             :     }
     619             : 
     620         282 :     return Error::success();
     621             :   }
     622             : 
     623             :   void upgradeDebugInfo() {
     624        1055 :     upgradeCUSubprograms();
     625        1055 :     upgradeCUVariables();
     626             :   }
     627             : 
     628             : public:
     629        2880 :   MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule,
     630             :                      BitcodeReaderValueList &ValueList,
     631             :                      std::function<Type *(unsigned)> getTypeByID,
     632             :                      bool IsImporting)
     633        2880 :       : MetadataList(TheModule.getContext()), ValueList(ValueList),
     634        2880 :         Stream(Stream), Context(TheModule.getContext()), TheModule(TheModule),
     635       25920 :         getTypeByID(std::move(getTypeByID)), IsImporting(IsImporting) {}
     636             : 
     637             :   Error parseMetadata(bool ModuleLevel);
     638             : 
     639       28360 :   bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); }
     640             : 
     641        9632 :   Metadata *getMetadataFwdRefOrLoad(unsigned ID) {
     642       19264 :     if (ID < MDStringRef.size())
     643           0 :       return lazyLoadOneMDString(ID);
     644       19264 :     if (auto *MD = MetadataList.lookup(ID))
     645             :       return MD;
     646             :     // If lazy-loading is enabled, we try recursively to load the operand
     647             :     // instead of creating a temporary.
     648           0 :     if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
     649           0 :       PlaceholderQueue Placeholders;
     650           0 :       lazyLoadOneMetadata(ID, Placeholders);
     651           0 :       resolveForwardRefsAndPlaceholders(Placeholders);
     652           0 :       return MetadataList.lookup(ID);
     653             :     }
     654           0 :     return MetadataList.getMetadataFwdRef(ID);
     655             :   }
     656             : 
     657             :   MDNode *getMDNodeFwdRefOrNull(unsigned Idx) {
     658       16112 :     return MetadataList.getMDNodeFwdRefOrNull(Idx);
     659             :   }
     660             : 
     661             :   DISubprogram *lookupSubprogramForFunction(Function *F) {
     662       14150 :     return FunctionsWithSPs.lookup(F);
     663             :   }
     664             : 
     665             :   bool hasSeenOldLoopTags() { return HasSeenOldLoopTags; }
     666             : 
     667             :   Error parseMetadataAttachment(
     668             :       Function &F, const SmallVectorImpl<Instruction *> &InstructionList);
     669             : 
     670             :   Error parseMetadataKinds();
     671             : 
     672           2 :   void setStripTBAA(bool Value) { StripTBAA = Value; }
     673             :   bool isStrippingTBAA() { return StripTBAA; }
     674             : 
     675       14210 :   unsigned size() const { return MetadataList.size(); }
     676       14150 :   void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); }
     677       31178 :   void upgradeDebugIntrinsics(Function &F) { upgradeDeclareExpressions(F); }
     678             : };
     679             : 
     680             : Expected<bool>
     681          10 : MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() {
     682          10 :   IndexCursor = Stream;
     683          10 :   SmallVector<uint64_t, 64> Record;
     684             :   // Get the abbrevs, and preload record positions to make them lazy-loadable.
     685             :   while (true) {
     686             :     BitstreamEntry Entry = IndexCursor.advanceSkippingSubblocks(
     687          25 :         BitstreamCursor::AF_DontPopBlockAtEnd);
     688          25 :     switch (Entry.Kind) {
     689           0 :     case BitstreamEntry::SubBlock: // Handled for us already.
     690             :     case BitstreamEntry::Error:
     691           0 :       return error("Malformed block");
     692           2 :     case BitstreamEntry::EndBlock: {
     693           4 :       return true;
     694             :     }
     695          23 :     case BitstreamEntry::Record: {
     696             :       // The interesting case.
     697          23 :       ++NumMDRecordLoaded;
     698          46 :       uint64_t CurrentPos = IndexCursor.GetCurrentBitNo();
     699          23 :       auto Code = IndexCursor.skipRecord(Entry.ID);
     700          23 :       switch (Code) {
     701          10 :       case bitc::METADATA_STRINGS: {
     702             :         // Rewind and parse the strings.
     703          20 :         IndexCursor.JumpToBit(CurrentPos);
     704          10 :         StringRef Blob;
     705          10 :         Record.clear();
     706          10 :         IndexCursor.readRecord(Entry.ID, Record, &Blob);
     707          10 :         unsigned NumStrings = Record[0];
     708          10 :         MDStringRef.reserve(NumStrings);
     709             :         auto IndexNextMDString = [&](StringRef Str) {
     710          54 :           MDStringRef.push_back(Str);
     711          10 :         };
     712          50 :         if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString))
     713           0 :           return std::move(Err);
     714          10 :         break;
     715             :       }
     716           2 :       case bitc::METADATA_INDEX_OFFSET: {
     717             :         // This is the offset to the index, when we see this we skip all the
     718             :         // records and load only an index to these.
     719           4 :         IndexCursor.JumpToBit(CurrentPos);
     720           2 :         Record.clear();
     721           2 :         IndexCursor.readRecord(Entry.ID, Record);
     722           2 :         if (Record.size() != 2)
     723           0 :           return error("Invalid record");
     724           4 :         auto Offset = Record[0] + (Record[1] << 32);
     725           4 :         auto BeginPos = IndexCursor.GetCurrentBitNo();
     726           4 :         IndexCursor.JumpToBit(BeginPos + Offset);
     727           2 :         Entry = IndexCursor.advanceSkippingSubblocks(
     728             :             BitstreamCursor::AF_DontPopBlockAtEnd);
     729             :         assert(Entry.Kind == BitstreamEntry::Record &&
     730             :                "Corrupted bitcode: Expected `Record` when trying to find the "
     731             :                "Metadata index");
     732           2 :         Record.clear();
     733           2 :         auto Code = IndexCursor.readRecord(Entry.ID, Record);
     734             :         (void)Code;
     735             :         assert(Code == bitc::METADATA_INDEX && "Corrupted bitcode: Expected "
     736             :                                                "`METADATA_INDEX` when trying "
     737             :                                                "to find the Metadata index");
     738             : 
     739             :         // Delta unpack
     740           2 :         auto CurrentValue = BeginPos;
     741           4 :         GlobalMetadataBitPosIndex.reserve(Record.size());
     742          45 :         for (auto &Elt : Record) {
     743          39 :           CurrentValue += Elt;
     744          39 :           GlobalMetadataBitPosIndex.push_back(CurrentValue);
     745             :         }
     746           2 :         break;
     747             :       }
     748           0 :       case bitc::METADATA_INDEX:
     749             :         // We don't expect to get there, the Index is loaded when we encounter
     750             :         // the offset.
     751           0 :         return error("Corrupted Metadata block");
     752           3 :       case bitc::METADATA_NAME: {
     753             :         // Named metadata need to be materialized now and aren't deferred.
     754           6 :         IndexCursor.JumpToBit(CurrentPos);
     755           3 :         Record.clear();
     756           3 :         unsigned Code = IndexCursor.readRecord(Entry.ID, Record);
     757             :         assert(Code == bitc::METADATA_NAME);
     758             : 
     759             :         // Read name of the named metadata.
     760          12 :         SmallString<8> Name(Record.begin(), Record.end());
     761           6 :         Code = IndexCursor.ReadCode();
     762             : 
     763             :         // Named Metadata comes in two parts, we expect the name to be followed
     764             :         // by the node
     765           3 :         Record.clear();
     766           3 :         unsigned NextBitCode = IndexCursor.readRecord(Code, Record);
     767             :         assert(NextBitCode == bitc::METADATA_NAMED_NODE);
     768             :         (void)NextBitCode;
     769             : 
     770             :         // Read named metadata elements.
     771           3 :         unsigned Size = Record.size();
     772           6 :         NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
     773           7 :         for (unsigned i = 0; i != Size; ++i) {
     774             :           // FIXME: We could use a placeholder here, however NamedMDNode are
     775             :           // taking MDNode as operand and not using the Metadata infrastructure.
     776             :           // It is acknowledged by 'TODO: Inherit from Metadata' in the
     777             :           // NamedMDNode class definition.
     778          12 :           MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
     779             :           assert(MD && "Invalid record");
     780           4 :           NMD->addOperand(MD);
     781             :         }
     782             :         break;
     783             :       }
     784           0 :       case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
     785             :         // FIXME: we need to do this early because we don't materialize global
     786             :         // value explicitly.
     787           0 :         IndexCursor.JumpToBit(CurrentPos);
     788           0 :         Record.clear();
     789           0 :         IndexCursor.readRecord(Entry.ID, Record);
     790           0 :         if (Record.size() % 2 == 0)
     791           0 :           return error("Invalid record");
     792           0 :         unsigned ValueID = Record[0];
     793           0 :         if (ValueID >= ValueList.size())
     794           0 :           return error("Invalid record");
     795           0 :         if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
     796           0 :           if (Error Err = parseGlobalObjectAttachment(
     797           0 :                   *GO, ArrayRef<uint64_t>(Record).slice(1)))
     798           0 :             return std::move(Err);
     799             :         break;
     800             :       }
     801           8 :       case bitc::METADATA_KIND:
     802             :       case bitc::METADATA_STRING_OLD:
     803             :       case bitc::METADATA_OLD_FN_NODE:
     804             :       case bitc::METADATA_OLD_NODE:
     805             :       case bitc::METADATA_VALUE:
     806             :       case bitc::METADATA_DISTINCT_NODE:
     807             :       case bitc::METADATA_NODE:
     808             :       case bitc::METADATA_LOCATION:
     809             :       case bitc::METADATA_GENERIC_DEBUG:
     810             :       case bitc::METADATA_SUBRANGE:
     811             :       case bitc::METADATA_ENUMERATOR:
     812             :       case bitc::METADATA_BASIC_TYPE:
     813             :       case bitc::METADATA_DERIVED_TYPE:
     814             :       case bitc::METADATA_COMPOSITE_TYPE:
     815             :       case bitc::METADATA_SUBROUTINE_TYPE:
     816             :       case bitc::METADATA_MODULE:
     817             :       case bitc::METADATA_FILE:
     818             :       case bitc::METADATA_COMPILE_UNIT:
     819             :       case bitc::METADATA_SUBPROGRAM:
     820             :       case bitc::METADATA_LEXICAL_BLOCK:
     821             :       case bitc::METADATA_LEXICAL_BLOCK_FILE:
     822             :       case bitc::METADATA_NAMESPACE:
     823             :       case bitc::METADATA_MACRO:
     824             :       case bitc::METADATA_MACRO_FILE:
     825             :       case bitc::METADATA_TEMPLATE_TYPE:
     826             :       case bitc::METADATA_TEMPLATE_VALUE:
     827             :       case bitc::METADATA_GLOBAL_VAR:
     828             :       case bitc::METADATA_LOCAL_VAR:
     829             :       case bitc::METADATA_EXPRESSION:
     830             :       case bitc::METADATA_OBJC_PROPERTY:
     831             :       case bitc::METADATA_IMPORTED_ENTITY:
     832             :       case bitc::METADATA_GLOBAL_VAR_EXPR:
     833             :         // We don't expect to see any of these, if we see one, give up on
     834             :         // lazy-loading and fallback.
     835          16 :         MDStringRef.clear();
     836          16 :         GlobalMetadataBitPosIndex.clear();
     837          16 :         return false;
     838             :       }
     839             :       break;
     840             :     }
     841             :     }
     842          15 :   }
     843             : }
     844             : 
     845             : /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
     846             : /// module level metadata.
     847        1055 : Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {
     848        1336 :   if (!ModuleLevel && MetadataList.hasFwdRefs())
     849           0 :     return error("Invalid metadata: fwd refs into function blocks");
     850             : 
     851             :   // Record the entry position so that we can jump back here and efficiently
     852             :   // skip the whole block in case we lazy-load.
     853        2110 :   auto EntryPos = Stream.GetCurrentBitNo();
     854             : 
     855        1055 :   if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
     856           0 :     return error("Invalid record");
     857             : 
     858        1055 :   SmallVector<uint64_t, 64> Record;
     859        2110 :   PlaceholderQueue Placeholders;
     860             : 
     861             :   // We lazy-load module-level metadata: we build an index for each record, and
     862             :   // then load individual record as needed, starting with the named metadata.
     863        1075 :   if (ModuleLevel && IsImporting && MetadataList.empty() &&
     864          10 :       !DisableLazyLoading) {
     865          18 :     auto SuccessOrErr = lazyLoadModuleMetadataBlock();
     866          10 :     if (!SuccessOrErr)
     867           2 :       return SuccessOrErr.takeError();
     868          10 :     if (SuccessOrErr.get()) {
     869             :       // An index was successfully created and we will be able to load metadata
     870             :       // on-demand.
     871           8 :       MetadataList.resize(MDStringRef.size() +
     872           2 :                           GlobalMetadataBitPosIndex.size());
     873             : 
     874             :       // Reading the named metadata created forward references and/or
     875             :       // placeholders, that we flush here.
     876           2 :       resolveForwardRefsAndPlaceholders(Placeholders);
     877           2 :       upgradeDebugInfo();
     878             :       // Return at the beginning of the block, since it is easy to skip it
     879             :       // entirely from there.
     880           2 :       Stream.ReadBlockEnd(); // Pop the abbrev block context.
     881           4 :       Stream.JumpToBit(EntryPos);
     882           2 :       if (Stream.SkipBlock())
     883           0 :         return error("Invalid record");
     884           6 :       return Error::success();
     885             :     }
     886             :     // Couldn't load an index, fallback to loading all the block "old-style".
     887             :   }
     888             : 
     889        2106 :   unsigned NextMetadataNo = MetadataList.size();
     890             : 
     891             :   // Read all the records.
     892             :   while (true) {
     893       15380 :     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
     894             : 
     895       15380 :     switch (Entry.Kind) {
     896           0 :     case BitstreamEntry::SubBlock: // Handled for us already.
     897             :     case BitstreamEntry::Error:
     898           0 :       return error("Malformed block");
     899        1053 :     case BitstreamEntry::EndBlock:
     900        1053 :       resolveForwardRefsAndPlaceholders(Placeholders);
     901        1053 :       upgradeDebugInfo();
     902        3159 :       return Error::success();
     903             :     case BitstreamEntry::Record:
     904             :       // The interesting case.
     905             :       break;
     906             :     }
     907             : 
     908             :     // Read a record.
     909       14327 :     Record.clear();
     910       14327 :     StringRef Blob;
     911       14327 :     ++NumMDRecordLoaded;
     912       14327 :     unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
     913       14327 :     if (Error Err =
     914       42981 :             parseOneMetadata(Record, Code, Placeholders, Blob, NextMetadataNo))
     915           0 :       return Err;
     916             :   }
     917             : }
     918             : 
     919          23 : MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) {
     920          23 :   ++NumMDStringLoaded;
     921          46 :   if (Metadata *MD = MetadataList.lookup(ID))
     922             :     return cast<MDString>(MD);
     923          44 :   auto MDS = MDString::get(Context, MDStringRef[ID]);
     924          22 :   MetadataList.assignValue(MDS, ID);
     925          22 :   return MDS;
     926             : }
     927             : 
     928          40 : void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata(
     929             :     unsigned ID, PlaceholderQueue &Placeholders) {
     930             :   assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size());
     931             :   assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString");
     932             :   // Lookup first if the metadata hasn't already been loaded.
     933          80 :   if (auto *MD = MetadataList.lookup(ID)) {
     934           5 :     auto *N = dyn_cast_or_null<MDNode>(MD);
     935           5 :     if (!N->isTemporary())
     936           1 :       return;
     937             :   }
     938          78 :   SmallVector<uint64_t, 64> Record;
     939          39 :   StringRef Blob;
     940         156 :   IndexCursor.JumpToBit(GlobalMetadataBitPosIndex[ID - MDStringRef.size()]);
     941          39 :   auto Entry = IndexCursor.advanceSkippingSubblocks();
     942          39 :   ++NumMDRecordLoaded;
     943          39 :   unsigned Code = IndexCursor.readRecord(Entry.ID, Record, &Blob);
     944         117 :   if (Error Err = parseOneMetadata(Record, Code, Placeholders, Blob, ID))
     945           0 :     report_fatal_error("Can't lazyload MD");
     946             : }
     947             : 
     948             : /// Ensure that all forward-references and placeholders are resolved.
     949             : /// Iteratively lazy-loading metadata on-demand if needed.
     950        1365 : void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders(
     951             :     PlaceholderQueue &Placeholders) {
     952        1365 :   DenseSet<unsigned> Temporaries;
     953             :   while (1) {
     954             :     // Populate Temporaries with the placeholders that haven't been loaded yet.
     955        1368 :     Placeholders.getTemporaries(MetadataList, Temporaries);
     956             : 
     957             :     // If we don't have any temporary, or FwdReference, we're done!
     958        2734 :     if (Temporaries.empty() && !MetadataList.hasFwdRefs())
     959             :       break;
     960             : 
     961             :     // First, load all the temporaries. This can add new placeholders or
     962             :     // forward references.
     963          45 :     for (auto ID : Temporaries)
     964          10 :       lazyLoadOneMetadata(ID, Placeholders);
     965             :     Temporaries.clear();
     966             : 
     967             :     // Second, load the forward-references. This can also add new placeholders
     968             :     // or forward references.
     969          18 :     while (MetadataList.hasFwdRefs())
     970           8 :       lazyLoadOneMetadata(MetadataList.getNextFwdRef(), Placeholders);
     971             :   }
     972             :   // At this point we don't have any forward reference remaining, or temporary
     973             :   // that haven't been loaded. We can safely drop RAUW support and mark cycles
     974             :   // as resolved.
     975        1365 :   MetadataList.tryToResolveCycles();
     976             : 
     977             :   // Finally, everything is in place, we can replace the placeholders operands
     978             :   // with the final node they refer to.
     979        1365 :   Placeholders.flush(MetadataList);
     980        1365 : }
     981             : 
     982       14366 : Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
     983             :     SmallVectorImpl<uint64_t> &Record, unsigned Code,
     984             :     PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) {
     985             : 
     986       14366 :   bool IsDistinct = false;
     987       20580 :   auto getMD = [&](unsigned ID) -> Metadata * {
     988       41210 :     if (ID < MDStringRef.size())
     989          23 :       return lazyLoadOneMDString(ID);
     990       20557 :     if (!IsDistinct) {
     991       35881 :       if (auto *MD = MetadataList.lookup(ID))
     992             :         return MD;
     993             :       // If lazy-loading is enabled, we try recursively to load the operand
     994             :       // instead of creating a temporary.
     995        1756 :       if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
     996             :         // Create a temporary for the node that is referencing the operand we
     997             :         // will lazy-load. It is needed before recursing in case there are
     998             :         // uniquing cycles.
     999          25 :         MetadataList.getMetadataFwdRef(NextMetadataNo);
    1000         969 :         lazyLoadOneMetadata(ID, Placeholders);
    1001          25 :         return MetadataList.lookup(ID);
    1002             :       }
    1003             :       // Return a temporary.
    1004         853 :       return MetadataList.getMetadataFwdRef(ID);
    1005             :     }
    1006        2478 :     if (auto *MD = MetadataList.getMetadataIfResolved(ID))
    1007             :       return MD;
    1008         944 :     return &Placeholders.getPlaceholderOp(ID);
    1009       14366 :   };
    1010       15469 :   auto getMDOrNull = [&](unsigned ID) -> Metadata * {
    1011       20914 :     if (ID)
    1012       14941 :       return getMD(ID - 1);
    1013             :     return nullptr;
    1014       14366 :   };
    1015             :   auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * {
    1016         320 :     if (ID)
    1017           5 :       return MetadataList.getMetadataFwdRef(ID - 1);
    1018             :     return nullptr;
    1019       14366 :   };
    1020        3747 :   auto getMDString = [&](unsigned ID) -> MDString * {
    1021             :     // This requires that the ID is not really a forward reference.  In
    1022             :     // particular, the MDString must already have been resolved.
    1023        5807 :     auto MDS = getMDOrNull(ID);
    1024        3747 :     return cast_or_null<MDString>(MDS);
    1025       14366 :   };
    1026             : 
    1027             :   // Support for old type refs.
    1028        1698 :   auto getDITypeRefOrNull = [&](unsigned ID) {
    1029        3396 :     return MetadataList.upgradeTypeRef(getMDOrNull(ID));
    1030       16064 :   };
    1031             : 
    1032             : #define GET_OR_DISTINCT(CLASS, ARGS)                                           \
    1033             :   (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
    1034             : 
    1035       14366 :   switch (Code) {
    1036             :   default: // Default behavior: ignore.
    1037             :     break;
    1038        1327 :   case bitc::METADATA_NAME: {
    1039             :     // Read name of the named metadata.
    1040        5308 :     SmallString<8> Name(Record.begin(), Record.end());
    1041        1327 :     Record.clear();
    1042        2654 :     Code = Stream.ReadCode();
    1043             : 
    1044        1327 :     ++NumMDRecordLoaded;
    1045        1327 :     unsigned NextBitCode = Stream.readRecord(Code, Record);
    1046        1327 :     if (NextBitCode != bitc::METADATA_NAMED_NODE)
    1047           0 :       return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
    1048             : 
    1049             :     // Read named metadata elements.
    1050        2654 :     unsigned Size = Record.size();
    1051        2654 :     NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
    1052        4704 :     for (unsigned i = 0; i != Size; ++i) {
    1053       10131 :       MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
    1054             :       if (!MD)
    1055           0 :         return error("Invalid record");
    1056        3377 :       NMD->addOperand(MD);
    1057             :     }
    1058        1327 :     break;
    1059             :   }
    1060         598 :   case bitc::METADATA_OLD_FN_NODE: {
    1061             :     // FIXME: Remove in 4.0.
    1062             :     // This is a LocalAsMetadata record, the only type of function-local
    1063             :     // metadata.
    1064        1196 :     if (Record.size() % 2 == 1)
    1065           0 :       return error("Invalid record");
    1066             : 
    1067             :     // If this isn't a LocalAsMetadata record, we're dropping it.  This used
    1068             :     // to be legal, but there's no upgrade path.
    1069           2 :     auto dropRecord = [&] {
    1070           4 :       MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo);
    1071           2 :       NextMetadataNo++;
    1072         600 :     };
    1073        1196 :     if (Record.size() != 2) {
    1074           1 :       dropRecord();
    1075         599 :       break;
    1076             :     }
    1077             : 
    1078        1791 :     Type *Ty = getTypeByID(Record[0]);
    1079        1193 :     if (Ty->isMetadataTy() || Ty->isVoidTy()) {
    1080           1 :       dropRecord();
    1081           1 :       break;
    1082             :     }
    1083             : 
    1084        1192 :     MetadataList.assignValue(
    1085        1788 :         LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
    1086             :         NextMetadataNo);
    1087         596 :     NextMetadataNo++;
    1088         596 :     break;
    1089             :   }
    1090        1346 :   case bitc::METADATA_OLD_NODE: {
    1091             :     // FIXME: Remove in 4.0.
    1092        2692 :     if (Record.size() % 2 == 1)
    1093           0 :       return error("Invalid record");
    1094             : 
    1095        2692 :     unsigned Size = Record.size();
    1096        1346 :     SmallVector<Metadata *, 8> Elts;
    1097       11830 :     for (unsigned i = 0; i != Size; i += 2) {
    1098       31452 :       Type *Ty = getTypeByID(Record[i]);
    1099       10484 :       if (!Ty)
    1100           0 :         return error("Invalid record");
    1101       10484 :       if (Ty->isMetadataTy())
    1102       11206 :         Elts.push_back(getMD(Record[i + 1]));
    1103        4881 :       else if (!Ty->isVoidTy()) {
    1104             :         auto *MD =
    1105        9248 :             ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
    1106             :         assert(isa<ConstantAsMetadata>(MD) &&
    1107             :                "Expected non-function-local metadata");
    1108        4624 :         Elts.push_back(MD);
    1109             :       } else
    1110         257 :         Elts.push_back(nullptr);
    1111             :     }
    1112        4038 :     MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo);
    1113        1346 :     NextMetadataNo++;
    1114        1346 :     break;
    1115             :   }
    1116        3308 :   case bitc::METADATA_VALUE: {
    1117        6616 :     if (Record.size() != 2)
    1118           0 :       return error("Invalid record");
    1119             : 
    1120        9924 :     Type *Ty = getTypeByID(Record[0]);
    1121        6616 :     if (Ty->isMetadataTy() || Ty->isVoidTy())
    1122           0 :       return error("Invalid record");
    1123             : 
    1124        6616 :     MetadataList.assignValue(
    1125        6616 :         ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
    1126             :         NextMetadataNo);
    1127        3308 :     NextMetadataNo++;
    1128        3308 :     break;
    1129             :   }
    1130         222 :   case bitc::METADATA_DISTINCT_NODE:
    1131         222 :     IsDistinct = true;
    1132             :     LLVM_FALLTHROUGH;
    1133        3426 :   case bitc::METADATA_NODE: {
    1134        6852 :     SmallVector<Metadata *, 8> Elts;
    1135        6852 :     Elts.reserve(Record.size());
    1136       20059 :     for (unsigned ID : Record)
    1137        9781 :       Elts.push_back(getMDOrNull(ID));
    1138        7074 :     MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
    1139        3204 :                                         : MDNode::get(Context, Elts),
    1140             :                              NextMetadataNo);
    1141        3426 :     NextMetadataNo++;
    1142             :     break;
    1143             :   }
    1144          36 :   case bitc::METADATA_LOCATION: {
    1145          72 :     if (Record.size() != 5)
    1146           0 :       return error("Invalid record");
    1147             : 
    1148          72 :     IsDistinct = Record[0];
    1149          72 :     unsigned Line = Record[1];
    1150          72 :     unsigned Column = Record[2];
    1151          72 :     Metadata *Scope = getMD(Record[3]);
    1152          72 :     Metadata *InlinedAt = getMDOrNull(Record[4]);
    1153          72 :     MetadataList.assignValue(
    1154          36 :         GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt)),
    1155             :         NextMetadataNo);
    1156          36 :     NextMetadataNo++;
    1157          36 :     break;
    1158             :   }
    1159          20 :   case bitc::METADATA_GENERIC_DEBUG: {
    1160          40 :     if (Record.size() < 4)
    1161           0 :       return error("Invalid record");
    1162             : 
    1163          40 :     IsDistinct = Record[0];
    1164          40 :     unsigned Tag = Record[1];
    1165          40 :     unsigned Version = Record[2];
    1166             : 
    1167          20 :     if (Tag >= 1u << 16 || Version != 0)
    1168           0 :       return error("Invalid record");
    1169             : 
    1170          40 :     auto *Header = getMDString(Record[3]);
    1171          40 :     SmallVector<Metadata *, 8> DwarfOps;
    1172          55 :     for (unsigned I = 4, E = Record.size(); I != E; ++I)
    1173          30 :       DwarfOps.push_back(getMDOrNull(Record[I]));
    1174          40 :     MetadataList.assignValue(
    1175          20 :         GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)),
    1176             :         NextMetadataNo);
    1177          20 :     NextMetadataNo++;
    1178             :     break;
    1179             :   }
    1180          37 :   case bitc::METADATA_SUBRANGE: {
    1181          74 :     if (Record.size() != 3)
    1182           0 :       return error("Invalid record");
    1183             : 
    1184          74 :     IsDistinct = Record[0];
    1185          74 :     MetadataList.assignValue(
    1186         148 :         GET_OR_DISTINCT(DISubrange,
    1187             :                         (Context, Record[1], unrotateSign(Record[2]))),
    1188             :         NextMetadataNo);
    1189          37 :     NextMetadataNo++;
    1190          37 :     break;
    1191             :   }
    1192          18 :   case bitc::METADATA_ENUMERATOR: {
    1193          36 :     if (Record.size() != 3)
    1194           0 :       return error("Invalid record");
    1195             : 
    1196          36 :     IsDistinct = Record[0];
    1197          36 :     MetadataList.assignValue(
    1198          72 :         GET_OR_DISTINCT(DIEnumerator, (Context, unrotateSign(Record[1]),
    1199             :                                        getMDString(Record[2]))),
    1200             :         NextMetadataNo);
    1201          18 :     NextMetadataNo++;
    1202          18 :     break;
    1203             :   }
    1204         141 :   case bitc::METADATA_BASIC_TYPE: {
    1205         282 :     if (Record.size() != 6)
    1206           0 :       return error("Invalid record");
    1207             : 
    1208         282 :     IsDistinct = Record[0];
    1209         282 :     MetadataList.assignValue(
    1210         846 :         GET_OR_DISTINCT(DIBasicType,
    1211             :                         (Context, Record[1], getMDString(Record[2]), Record[3],
    1212             :                          Record[4], Record[5])),
    1213             :         NextMetadataNo);
    1214         141 :     NextMetadataNo++;
    1215         141 :     break;
    1216             :   }
    1217         111 :   case bitc::METADATA_DERIVED_TYPE: {
    1218         333 :     if (Record.size() < 12 || Record.size() > 13)
    1219           0 :       return error("Invalid record");
    1220             : 
    1221             :     // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
    1222             :     // that there is no DWARF address space associated with DIDerivedType.
    1223         222 :     Optional<unsigned> DWARFAddressSpace;
    1224         428 :     if (Record.size() > 12 && Record[12])
    1225          18 :       DWARFAddressSpace = Record[12] - 1;
    1226             : 
    1227         222 :     IsDistinct = Record[0];
    1228         222 :     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
    1229         222 :     MetadataList.assignValue(
    1230        1332 :         GET_OR_DISTINCT(DIDerivedType,
    1231             :                         (Context, Record[1], getMDString(Record[2]),
    1232             :                          getMDOrNull(Record[3]), Record[4],
    1233             :                          getDITypeRefOrNull(Record[5]),
    1234             :                          getDITypeRefOrNull(Record[6]), Record[7], Record[8],
    1235             :                          Record[9], DWARFAddressSpace, Flags,
    1236             :                          getDITypeRefOrNull(Record[11]))),
    1237             :         NextMetadataNo);
    1238         111 :     NextMetadataNo++;
    1239             :     break;
    1240             :   }
    1241         183 :   case bitc::METADATA_COMPOSITE_TYPE: {
    1242         366 :     if (Record.size() != 16)
    1243           0 :       return error("Invalid record");
    1244             : 
    1245             :     // If we have a UUID and this is not a forward declaration, lookup the
    1246             :     // mapping.
    1247         366 :     IsDistinct = Record[0] & 0x1;
    1248         366 :     bool IsNotUsedInTypeRef = Record[0] >= 2;
    1249         366 :     unsigned Tag = Record[1];
    1250         366 :     MDString *Name = getMDString(Record[2]);
    1251         366 :     Metadata *File = getMDOrNull(Record[3]);
    1252         366 :     unsigned Line = Record[4];
    1253         366 :     Metadata *Scope = getDITypeRefOrNull(Record[5]);
    1254         183 :     Metadata *BaseType = nullptr;
    1255         366 :     uint64_t SizeInBits = Record[7];
    1256         366 :     if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
    1257           0 :       return error("Alignment value is too large");
    1258         366 :     uint32_t AlignInBits = Record[8];
    1259         183 :     uint64_t OffsetInBits = 0;
    1260         366 :     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
    1261         183 :     Metadata *Elements = nullptr;
    1262         366 :     unsigned RuntimeLang = Record[12];
    1263         183 :     Metadata *VTableHolder = nullptr;
    1264         183 :     Metadata *TemplateParams = nullptr;
    1265         366 :     auto *Identifier = getMDString(Record[15]);
    1266             :     // If this module is being parsed so that it can be ThinLTO imported
    1267             :     // into another module, composite types only need to be imported
    1268             :     // as type declarations (unless full type definitions requested).
    1269             :     // Create type declarations up front to save memory. Also, buildODRType
    1270             :     // handles the case where this is type ODRed with a definition needed
    1271             :     // by the importing module, in which case the existing definition is
    1272             :     // used.
    1273         217 :     if (IsImporting && !ImportFullTypeDefinitions && Identifier &&
    1274          26 :         (Tag == dwarf::DW_TAG_enumeration_type ||
    1275          13 :          Tag == dwarf::DW_TAG_class_type ||
    1276          10 :          Tag == dwarf::DW_TAG_structure_type ||
    1277           5 :          Tag == dwarf::DW_TAG_union_type)) {
    1278          13 :       Flags = Flags | DINode::FlagFwdDecl;
    1279             :     } else {
    1280         340 :       BaseType = getDITypeRefOrNull(Record[6]);
    1281         340 :       OffsetInBits = Record[9];
    1282         340 :       Elements = getMDOrNull(Record[11]);
    1283         340 :       VTableHolder = getDITypeRefOrNull(Record[13]);
    1284         340 :       TemplateParams = getMDOrNull(Record[14]);
    1285             :     }
    1286         183 :     DICompositeType *CT = nullptr;
    1287         183 :     if (Identifier)
    1288         126 :       CT = DICompositeType::buildODRType(
    1289             :           Context, *Identifier, Tag, Name, File, Line, Scope, BaseType,
    1290             :           SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
    1291             :           VTableHolder, TemplateParams);
    1292             : 
    1293             :     // Create a node if we didn't get a lazy ODR type.
    1294         126 :     if (!CT)
    1295         268 :       CT = GET_OR_DISTINCT(DICompositeType,
    1296             :                            (Context, Tag, Name, File, Line, Scope, BaseType,
    1297             :                             SizeInBits, AlignInBits, OffsetInBits, Flags,
    1298             :                             Elements, RuntimeLang, VTableHolder, TemplateParams,
    1299             :                             Identifier));
    1300         183 :     if (!IsNotUsedInTypeRef && Identifier)
    1301          12 :       MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT));
    1302             : 
    1303         183 :     MetadataList.assignValue(CT, NextMetadataNo);
    1304         183 :     NextMetadataNo++;
    1305         183 :     break;
    1306             :   }
    1307         193 :   case bitc::METADATA_SUBROUTINE_TYPE: {
    1308         579 :     if (Record.size() < 3 || Record.size() > 4)
    1309           0 :       return error("Invalid record");
    1310         386 :     bool IsOldTypeRefArray = Record[0] < 2;
    1311         760 :     unsigned CC = (Record.size() > 3) ? Record[3] : 0;
    1312             : 
    1313         386 :     IsDistinct = Record[0] & 0x1;
    1314         386 :     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]);
    1315         386 :     Metadata *Types = getMDOrNull(Record[2]);
    1316         193 :     if (LLVM_UNLIKELY(IsOldTypeRefArray))
    1317           4 :       Types = MetadataList.upgradeTypeRefArray(Types);
    1318             : 
    1319         386 :     MetadataList.assignValue(
    1320         193 :         GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)),
    1321             :         NextMetadataNo);
    1322         193 :     NextMetadataNo++;
    1323         193 :     break;
    1324             :   }
    1325             : 
    1326          12 :   case bitc::METADATA_MODULE: {
    1327          24 :     if (Record.size() != 6)
    1328           0 :       return error("Invalid record");
    1329             : 
    1330          24 :     IsDistinct = Record[0];
    1331          24 :     MetadataList.assignValue(
    1332          72 :         GET_OR_DISTINCT(DIModule,
    1333             :                         (Context, getMDOrNull(Record[1]),
    1334             :                          getMDString(Record[2]), getMDString(Record[3]),
    1335             :                          getMDString(Record[4]), getMDString(Record[5]))),
    1336             :         NextMetadataNo);
    1337          12 :     NextMetadataNo++;
    1338          12 :     break;
    1339             :   }
    1340             : 
    1341         371 :   case bitc::METADATA_FILE: {
    1342        1096 :     if (Record.size() != 3 && Record.size() != 5)
    1343           0 :       return error("Invalid record");
    1344             : 
    1345         742 :     IsDistinct = Record[0];
    1346         742 :     MetadataList.assignValue(
    1347        2563 :         GET_OR_DISTINCT(
    1348             :             DIFile,
    1349             :             (Context, getMDString(Record[1]), getMDString(Record[2]),
    1350             :              Record.size() == 3 ? DIFile::CSK_None
    1351             :                                 : static_cast<DIFile::ChecksumKind>(Record[3]),
    1352             :              Record.size() == 3 ? nullptr : getMDString(Record[4]))),
    1353             :         NextMetadataNo);
    1354         371 :     NextMetadataNo++;
    1355         371 :     break;
    1356             :   }
    1357         320 :   case bitc::METADATA_COMPILE_UNIT: {
    1358         960 :     if (Record.size() < 14 || Record.size() > 19)
    1359           0 :       return error("Invalid record");
    1360             : 
    1361             :     // Ignore Record[0], which indicates whether this compile unit is
    1362             :     // distinct.  It's always distinct.
    1363         320 :     IsDistinct = true;
    1364        7300 :     auto *CU = DICompileUnit::getDistinct(
    1365        1920 :         Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]),
    1366        2560 :         Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]),
    1367        1920 :         Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]),
    1368        1280 :         getMDOrNull(Record[12]), getMDOrNull(Record[13]),
    1369        1268 :         Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
    1370        1278 :         Record.size() <= 14 ? 0 : Record[14],
    1371        1258 :         Record.size() <= 16 ? true : Record[16],
    1372        1246 :         Record.size() <= 17 ? false : Record[17],
    1373        2162 :         Record.size() <= 18 ? false : Record[18]);
    1374             : 
    1375         320 :     MetadataList.assignValue(CU, NextMetadataNo);
    1376         320 :     NextMetadataNo++;
    1377             : 
    1378             :     // Move the Upgrade the list of subprograms.
    1379         645 :     if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11]))
    1380          15 :       CUSubprograms.push_back({CU, SPs});
    1381             :     break;
    1382             :   }
    1383         292 :   case bitc::METADATA_SUBPROGRAM: {
    1384         876 :     if (Record.size() < 18 || Record.size() > 21)
    1385           0 :       return error("Invalid record");
    1386             : 
    1387         292 :     IsDistinct =
    1388         698 :         (Record[0] & 1) || Record[8]; // All definitions should be distinct.
    1389             :     // Version 1 has a Function as Record[15].
    1390             :     // Version 2 has removed Record[15].
    1391             :     // Version 3 has the Unit as Record[15].
    1392             :     // Version 4 added thisAdjustment.
    1393         584 :     bool HasUnit = Record[0] >= 2;
    1394         574 :     if (HasUnit && Record.size() < 19)
    1395           0 :       return error("Invalid record");
    1396         584 :     Metadata *CUorFn = getMDOrNull(Record[15]);
    1397         876 :     unsigned Offset = Record.size() >= 19 ? 1 : 0;
    1398         292 :     bool HasFn = Offset && !HasUnit;
    1399         584 :     bool HasThisAdj = Record.size() >= 20;
    1400         584 :     bool HasThrownTypes = Record.size() >= 21;
    1401        6105 :     DISubprogram *SP = GET_OR_DISTINCT(
    1402             :         DISubprogram,
    1403             :         (Context,
    1404             :          getDITypeRefOrNull(Record[1]),                     // scope
    1405             :          getMDString(Record[2]),                            // name
    1406             :          getMDString(Record[3]),                            // linkageName
    1407             :          getMDOrNull(Record[4]),                            // file
    1408             :          Record[5],                                         // line
    1409             :          getMDOrNull(Record[6]),                            // type
    1410             :          Record[7],                                         // isLocal
    1411             :          Record[8],                                         // isDefinition
    1412             :          Record[9],                                         // scopeLine
    1413             :          getDITypeRefOrNull(Record[10]),                    // containingType
    1414             :          Record[11],                                        // virtuality
    1415             :          Record[12],                                        // virtualIndex
    1416             :          HasThisAdj ? Record[19] : 0,                       // thisAdjustment
    1417             :          static_cast<DINode::DIFlags>(Record[13]),          // flags
    1418             :          Record[14],                                        // isOptimized
    1419             :          HasUnit ? CUorFn : nullptr,                        // unit
    1420             :          getMDOrNull(Record[15 + Offset]),                  // templateParams
    1421             :          getMDOrNull(Record[16 + Offset]),                  // declaration
    1422             :          getMDOrNull(Record[17 + Offset]),                  // variables
    1423             :          HasThrownTypes ? getMDOrNull(Record[20]) : nullptr // thrownTypes
    1424             :          ));
    1425         292 :     MetadataList.assignValue(SP, NextMetadataNo);
    1426         292 :     NextMetadataNo++;
    1427             : 
    1428             :     // Upgrade sp->function mapping to function->sp mapping.
    1429         292 :     if (HasFn) {
    1430           2 :       if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn))
    1431           4 :         if (auto *F = dyn_cast<Function>(CMD->getValue())) {
    1432           4 :           if (F->isMaterializable())
    1433             :             // Defer until materialized; unmaterialized functions may not have
    1434             :             // metadata.
    1435           4 :             FunctionsWithSPs[F] = SP;
    1436           0 :           else if (!F->empty())
    1437           0 :             F->setSubprogram(SP);
    1438             :         }
    1439             :     }
    1440             :     break;
    1441             :   }
    1442          86 :   case bitc::METADATA_LEXICAL_BLOCK: {
    1443         172 :     if (Record.size() != 5)
    1444           0 :       return error("Invalid record");
    1445             : 
    1446         172 :     IsDistinct = Record[0];
    1447         172 :     MetadataList.assignValue(
    1448         430 :         GET_OR_DISTINCT(DILexicalBlock,
    1449             :                         (Context, getMDOrNull(Record[1]),
    1450             :                          getMDOrNull(Record[2]), Record[3], Record[4])),
    1451             :         NextMetadataNo);
    1452          86 :     NextMetadataNo++;
    1453          86 :     break;
    1454             :   }
    1455          42 :   case bitc::METADATA_LEXICAL_BLOCK_FILE: {
    1456          84 :     if (Record.size() != 4)
    1457           0 :       return error("Invalid record");
    1458             : 
    1459          84 :     IsDistinct = Record[0];
    1460          84 :     MetadataList.assignValue(
    1461         168 :         GET_OR_DISTINCT(DILexicalBlockFile,
    1462             :                         (Context, getMDOrNull(Record[1]),
    1463             :                          getMDOrNull(Record[2]), Record[3])),
    1464             :         NextMetadataNo);
    1465          42 :     NextMetadataNo++;
    1466          42 :     break;
    1467             :   }
    1468          34 :   case bitc::METADATA_NAMESPACE: {
    1469             :     // Newer versions of DINamespace dropped file and line.
    1470             :     MDString *Name;
    1471          68 :     if (Record.size() == 3)
    1472          66 :       Name = getMDString(Record[2]);
    1473           2 :     else if (Record.size() == 5)
    1474           2 :       Name = getMDString(Record[3]);
    1475             :     else
    1476           0 :       return error("Invalid record");
    1477             : 
    1478          68 :     IsDistinct = Record[0] & 1;
    1479          68 :     bool ExportSymbols = Record[0] & 2;
    1480          68 :     MetadataList.assignValue(
    1481          68 :         GET_OR_DISTINCT(DINamespace,
    1482             :                         (Context, getMDOrNull(Record[1]), Name, ExportSymbols)),
    1483             :         NextMetadataNo);
    1484          34 :     NextMetadataNo++;
    1485          34 :     break;
    1486             :   }
    1487          13 :   case bitc::METADATA_MACRO: {
    1488          26 :     if (Record.size() != 5)
    1489           0 :       return error("Invalid record");
    1490             : 
    1491          26 :     IsDistinct = Record[0];
    1492          26 :     MetadataList.assignValue(
    1493          65 :         GET_OR_DISTINCT(DIMacro,
    1494             :                         (Context, Record[1], Record[2], getMDString(Record[3]),
    1495             :                          getMDString(Record[4]))),
    1496             :         NextMetadataNo);
    1497          13 :     NextMetadataNo++;
    1498          13 :     break;
    1499             :   }
    1500          19 :   case bitc::METADATA_MACRO_FILE: {
    1501          38 :     if (Record.size() != 5)
    1502           0 :       return error("Invalid record");
    1503             : 
    1504          38 :     IsDistinct = Record[0];
    1505          38 :     MetadataList.assignValue(
    1506          95 :         GET_OR_DISTINCT(DIMacroFile,
    1507             :                         (Context, Record[1], Record[2], getMDOrNull(Record[3]),
    1508             :                          getMDOrNull(Record[4]))),
    1509             :         NextMetadataNo);
    1510          19 :     NextMetadataNo++;
    1511          19 :     break;
    1512             :   }
    1513          24 :   case bitc::METADATA_TEMPLATE_TYPE: {
    1514          48 :     if (Record.size() != 3)
    1515           0 :       return error("Invalid record");
    1516             : 
    1517          48 :     IsDistinct = Record[0];
    1518          96 :     MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter,
    1519             :                                              (Context, getMDString(Record[1]),
    1520             :                                               getDITypeRefOrNull(Record[2]))),
    1521             :                              NextMetadataNo);
    1522          24 :     NextMetadataNo++;
    1523          24 :     break;
    1524             :   }
    1525          25 :   case bitc::METADATA_TEMPLATE_VALUE: {
    1526          50 :     if (Record.size() != 5)
    1527           0 :       return error("Invalid record");
    1528             : 
    1529          50 :     IsDistinct = Record[0];
    1530          50 :     MetadataList.assignValue(
    1531         125 :         GET_OR_DISTINCT(DITemplateValueParameter,
    1532             :                         (Context, Record[1], getMDString(Record[2]),
    1533             :                          getDITypeRefOrNull(Record[3]),
    1534             :                          getMDOrNull(Record[4]))),
    1535             :         NextMetadataNo);
    1536          25 :     NextMetadataNo++;
    1537          25 :     break;
    1538             :   }
    1539          49 :   case bitc::METADATA_GLOBAL_VAR: {
    1540         147 :     if (Record.size() < 11 || Record.size() > 12)
    1541           0 :       return error("Invalid record");
    1542             : 
    1543          98 :     IsDistinct = Record[0] & 1;
    1544          98 :     unsigned Version = Record[0] >> 1;
    1545             : 
    1546          49 :     if (Version == 1) {
    1547          72 :       MetadataList.assignValue(
    1548         396 :           GET_OR_DISTINCT(DIGlobalVariable,
    1549             :                           (Context, getMDOrNull(Record[1]),
    1550             :                            getMDString(Record[2]), getMDString(Record[3]),
    1551             :                            getMDOrNull(Record[4]), Record[5],
    1552             :                            getDITypeRefOrNull(Record[6]), Record[7], Record[8],
    1553             :                            getMDOrNull(Record[10]), Record[11])),
    1554             :           NextMetadataNo);
    1555          36 :       NextMetadataNo++;
    1556          13 :     } else if (Version == 0) {
    1557             :       // Upgrade old metadata, which stored a global variable reference or a
    1558             :       // ConstantInt here.
    1559          13 :       NeedUpgradeToDIGlobalVariableExpression = true;
    1560          26 :       Metadata *Expr = getMDOrNull(Record[9]);
    1561          13 :       uint32_t AlignInBits = 0;
    1562          26 :       if (Record.size() > 11) {
    1563          14 :         if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max())
    1564           0 :           return error("Alignment value is too large");
    1565          14 :         AlignInBits = Record[11];
    1566             :       }
    1567          13 :       GlobalVariable *Attach = nullptr;
    1568           5 :       if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) {
    1569           5 :         if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) {
    1570             :           Attach = GV;
    1571             :           Expr = nullptr;
    1572           2 :         } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) {
    1573           4 :           Expr = DIExpression::get(Context,
    1574           1 :                                    {dwarf::DW_OP_constu, CI->getZExtValue(),
    1575             :                                     dwarf::DW_OP_stack_value});
    1576             :         } else {
    1577             :           Expr = nullptr;
    1578             :         }
    1579             :       }
    1580         143 :       DIGlobalVariable *DGV = GET_OR_DISTINCT(
    1581             :           DIGlobalVariable,
    1582             :           (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
    1583             :            getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
    1584             :            getDITypeRefOrNull(Record[6]), Record[7], Record[8],
    1585             :            getMDOrNull(Record[10]), AlignInBits));
    1586             : 
    1587          13 :       DIGlobalVariableExpression *DGVE = nullptr;
    1588          13 :       if (Attach || Expr)
    1589          24 :         DGVE = DIGlobalVariableExpression::getDistinct(
    1590           4 :             Context, DGV, Expr ? Expr : DIExpression::get(Context, {}));
    1591          13 :       if (Attach)
    1592           4 :         Attach->addDebugInfo(DGVE);
    1593             : 
    1594          20 :       auto *MDNode = Expr ? cast<Metadata>(DGVE) : cast<Metadata>(DGV);
    1595          13 :       MetadataList.assignValue(MDNode, NextMetadataNo);
    1596          13 :       NextMetadataNo++;
    1597             :     } else
    1598           0 :       return error("Invalid record");
    1599             : 
    1600             :     break;
    1601             :   }
    1602         111 :   case bitc::METADATA_LOCAL_VAR: {
    1603             :     // 10th field is for the obseleted 'inlinedAt:' field.
    1604         333 :     if (Record.size() < 8 || Record.size() > 10)
    1605           0 :       return error("Invalid record");
    1606             : 
    1607         222 :     IsDistinct = Record[0] & 1;
    1608         222 :     bool HasAlignment = Record[0] & 2;
    1609             :     // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
    1610             :     // DW_TAG_arg_variable, if we have alignment flag encoded it means, that
    1611             :     // this is newer version of record which doesn't have artificial tag.
    1612         125 :     bool HasTag = !HasAlignment && Record.size() > 8;
    1613         222 :     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]);
    1614         111 :     uint32_t AlignInBits = 0;
    1615         111 :     if (HasAlignment) {
    1616         208 :       if (Record[8 + HasTag] > (uint64_t)std::numeric_limits<uint32_t>::max())
    1617           0 :         return error("Alignment value is too large");
    1618         208 :       AlignInBits = Record[8 + HasTag];
    1619             :     }
    1620         222 :     MetadataList.assignValue(
    1621         777 :         GET_OR_DISTINCT(DILocalVariable,
    1622             :                         (Context, getMDOrNull(Record[1 + HasTag]),
    1623             :                          getMDString(Record[2 + HasTag]),
    1624             :                          getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
    1625             :                          getDITypeRefOrNull(Record[5 + HasTag]),
    1626             :                          Record[6 + HasTag], Flags, AlignInBits)),
    1627             :         NextMetadataNo);
    1628         111 :     NextMetadataNo++;
    1629         111 :     break;
    1630             :   }
    1631          94 :   case bitc::METADATA_EXPRESSION: {
    1632         188 :     if (Record.size() < 1)
    1633           0 :       return error("Invalid record");
    1634             : 
    1635         188 :     IsDistinct = Record[0] & 1;
    1636         188 :     uint64_t Version = Record[0] >> 1;
    1637         188 :     auto Elts = MutableArrayRef<uint64_t>(Record).slice(1);
    1638             : 
    1639          94 :     SmallVector<uint64_t, 6> Buffer;
    1640         282 :     if (Error Err = upgradeDIExpression(Version, Elts, Buffer))
    1641           0 :       return Err;
    1642             : 
    1643         188 :     MetadataList.assignValue(
    1644          94 :         GET_OR_DISTINCT(DIExpression, (Context, Elts)), NextMetadataNo);
    1645          94 :     NextMetadataNo++;
    1646          94 :     break;
    1647             :   }
    1648          23 :   case bitc::METADATA_GLOBAL_VAR_EXPR: {
    1649          46 :     if (Record.size() != 3)
    1650           0 :       return error("Invalid record");
    1651             : 
    1652          46 :     IsDistinct = Record[0];
    1653          46 :     Metadata *Expr = getMDOrNull(Record[2]);
    1654          23 :     if (!Expr)
    1655           0 :       Expr = DIExpression::get(Context, {});
    1656          46 :     MetadataList.assignValue(
    1657          46 :         GET_OR_DISTINCT(DIGlobalVariableExpression,
    1658             :                         (Context, getMDOrNull(Record[1]), Expr)),
    1659             :         NextMetadataNo);
    1660          23 :     NextMetadataNo++;
    1661          23 :     break;
    1662             :   }
    1663          18 :   case bitc::METADATA_OBJC_PROPERTY: {
    1664          36 :     if (Record.size() != 8)
    1665           0 :       return error("Invalid record");
    1666             : 
    1667          36 :     IsDistinct = Record[0];
    1668          36 :     MetadataList.assignValue(
    1669         144 :         GET_OR_DISTINCT(DIObjCProperty,
    1670             :                         (Context, getMDString(Record[1]),
    1671             :                          getMDOrNull(Record[2]), Record[3],
    1672             :                          getMDString(Record[4]), getMDString(Record[5]),
    1673             :                          Record[6], getDITypeRefOrNull(Record[7]))),
    1674             :         NextMetadataNo);
    1675          18 :     NextMetadataNo++;
    1676          18 :     break;
    1677             :   }
    1678          31 :   case bitc::METADATA_IMPORTED_ENTITY: {
    1679          87 :     if (Record.size() != 6 && Record.size() != 7)
    1680           0 :       return error("Invalid record");
    1681             : 
    1682          62 :     IsDistinct = Record[0];
    1683          62 :     bool HasFile = (Record.size() == 7);
    1684          62 :     MetadataList.assignValue(
    1685         205 :         GET_OR_DISTINCT(DIImportedEntity,
    1686             :                         (Context, Record[1], getMDOrNull(Record[2]),
    1687             :                          getDITypeRefOrNull(Record[3]),
    1688             :                          HasFile ? getMDOrNull(Record[6]) : nullptr,
    1689             :                          HasFile ? Record[4] : 0, getMDString(Record[5]))),
    1690             :         NextMetadataNo);
    1691          31 :     NextMetadataNo++;
    1692          31 :     break;
    1693             :   }
    1694             :   case bitc::METADATA_STRING_OLD: {
    1695        1918 :     std::string String(Record.begin(), Record.end());
    1696             : 
    1697             :     // Test for upgrading !llvm.loop.
    1698         548 :     HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String);
    1699         274 :     ++NumMDStringLoaded;
    1700         274 :     Metadata *MD = MDString::get(Context, String);
    1701         274 :     MetadataList.assignValue(MD, NextMetadataNo);
    1702         274 :     NextMetadataNo++;
    1703             :     break;
    1704             :   }
    1705         808 :   case bitc::METADATA_STRINGS: {
    1706        3684 :     auto CreateNextMDString = [&](StringRef Str) {
    1707        3684 :       ++NumMDStringLoaded;
    1708        3684 :       MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo);
    1709        3684 :       NextMetadataNo++;
    1710        4492 :     };
    1711        4040 :     if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString))
    1712           0 :       return Err;
    1713         808 :     break;
    1714             :   }
    1715          56 :   case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
    1716         112 :     if (Record.size() % 2 == 0)
    1717           0 :       return error("Invalid record");
    1718         112 :     unsigned ValueID = Record[0];
    1719         112 :     if (ValueID >= ValueList.size())
    1720           0 :       return error("Invalid record");
    1721         168 :     if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
    1722          56 :       if (Error Err = parseGlobalObjectAttachment(
    1723         280 :               *GO, ArrayRef<uint64_t>(Record).slice(1)))
    1724           0 :         return Err;
    1725             :     break;
    1726             :   }
    1727         804 :   case bitc::METADATA_KIND: {
    1728             :     // Support older bitcode files that had METADATA_KIND records in a
    1729             :     // block with METADATA_BLOCK_ID.
    1730        2412 :     if (Error Err = parseMetadataKindRecord(Record))
    1731           0 :       return Err;
    1732             :     break;
    1733             :   }
    1734             :   }
    1735       43098 :   return Error::success();
    1736             : #undef GET_OR_DISTINCT
    1737             : }
    1738             : 
    1739         818 : Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
    1740             :     ArrayRef<uint64_t> Record, StringRef Blob,
    1741             :     function_ref<void(StringRef)> CallBack) {
    1742             :   // All the MDStrings in the block are emitted together in a single
    1743             :   // record.  The strings are concatenated and stored in a blob along with
    1744             :   // their sizes.
    1745         818 :   if (Record.size() != 2)
    1746           0 :     return error("Invalid record: metadata strings layout");
    1747             : 
    1748         818 :   unsigned NumStrings = Record[0];
    1749         818 :   unsigned StringsOffset = Record[1];
    1750         818 :   if (!NumStrings)
    1751           0 :     return error("Invalid record: metadata strings with no strings");
    1752        1636 :   if (StringsOffset > Blob.size())
    1753           0 :     return error("Invalid record: metadata strings corrupt offset");
    1754             : 
    1755        1636 :   StringRef Lengths = Blob.slice(0, StringsOffset);
    1756         818 :   SimpleBitstreamCursor R(Lengths);
    1757             : 
    1758        1636 :   StringRef Strings = Blob.drop_front(StringsOffset);
    1759             :   do {
    1760           0 :     if (R.AtEndOfStream())
    1761           0 :       return error("Invalid record: metadata strings bad length");
    1762             : 
    1763        3738 :     unsigned Size = R.ReadVBR(6);
    1764        3738 :     if (Strings.size() < Size)
    1765           0 :       return error("Invalid record: metadata strings truncated chars");
    1766             : 
    1767       11214 :     CallBack(Strings.slice(0, Size));
    1768        7476 :     Strings = Strings.drop_front(Size);
    1769        3738 :   } while (--NumStrings);
    1770             : 
    1771        2454 :   return Error::success();
    1772             : }
    1773             : 
    1774         248 : Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment(
    1775             :     GlobalObject &GO, ArrayRef<uint64_t> Record) {
    1776             :   assert(Record.size() % 2 == 0);
    1777         548 :   for (unsigned I = 0, E = Record.size(); I != E; I += 2) {
    1778         600 :     auto K = MDKindMap.find(Record[I]);
    1779         900 :     if (K == MDKindMap.end())
    1780           0 :       return error("Invalid ID");
    1781         900 :     MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[I + 1]);
    1782             :     if (!MD)
    1783           0 :       return error("Invalid metadata attachment");
    1784         300 :     GO.addMetadata(K->second, *MD);
    1785             :   }
    1786         744 :   return Error::success();
    1787             : }
    1788             : 
    1789             : /// Parse metadata attachments.
    1790         309 : Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment(
    1791             :     Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
    1792         309 :   if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
    1793           0 :     return error("Invalid record");
    1794             : 
    1795         309 :   SmallVector<uint64_t, 64> Record;
    1796         309 :   PlaceholderQueue Placeholders;
    1797             : 
    1798             :   while (true) {
    1799        1442 :     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
    1800             : 
    1801        1442 :     switch (Entry.Kind) {
    1802           0 :     case BitstreamEntry::SubBlock: // Handled for us already.
    1803             :     case BitstreamEntry::Error:
    1804           0 :       return error("Malformed block");
    1805         309 :     case BitstreamEntry::EndBlock:
    1806         309 :       resolveForwardRefsAndPlaceholders(Placeholders);
    1807         927 :       return Error::success();
    1808             :     case BitstreamEntry::Record:
    1809             :       // The interesting case.
    1810             :       break;
    1811             :     }
    1812             : 
    1813             :     // Read a metadata attachment record.
    1814        1133 :     Record.clear();
    1815        1133 :     ++NumMDRecordLoaded;
    1816        1133 :     switch (Stream.readRecord(Entry.ID, Record)) {
    1817             :     default: // Default behavior: ignore.
    1818             :       break;
    1819        1133 :     case bitc::METADATA_ATTACHMENT: {
    1820        1133 :       unsigned RecordLength = Record.size();
    1821        1133 :       if (Record.empty())
    1822           0 :         return error("Invalid record");
    1823        1133 :       if (RecordLength % 2 == 0) {
    1824             :         // A function attachment.
    1825         576 :         if (Error Err = parseGlobalObjectAttachment(F, Record))
    1826           0 :           return Err;
    1827         192 :         continue;
    1828             :       }
    1829             : 
    1830             :       // An instruction attachment.
    1831        1882 :       Instruction *Inst = InstructionList[Record[0]];
    1832        1970 :       for (unsigned i = 1; i != RecordLength; i = i + 2) {
    1833        2060 :         unsigned Kind = Record[i];
    1834        1030 :         DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind);
    1835        3090 :         if (I == MDKindMap.end())
    1836           0 :           return error("Invalid ID");
    1837        1030 :         if (I->second == LLVMContext::MD_tbaa && StripTBAA)
    1838          11 :           continue;
    1839             : 
    1840        2038 :         auto Idx = Record[i + 1];
    1841        3059 :         if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) &&
    1842           4 :             !MetadataList.lookup(Idx)) {
    1843             :           // Load the attachment if it is in the lazy-loadable range and hasn't
    1844             :           // been loaded yet.
    1845           1 :           lazyLoadOneMetadata(Idx, Placeholders);
    1846           1 :           resolveForwardRefsAndPlaceholders(Placeholders);
    1847             :         }
    1848             : 
    1849        1019 :         Metadata *Node = MetadataList.getMetadataFwdRef(Idx);
    1850        2038 :         if (isa<LocalAsMetadata>(Node))
    1851             :           // Drop the attachment.  This used to be legal, but there's no
    1852             :           // upgrade path.
    1853             :           break;
    1854        2036 :         MDNode *MD = dyn_cast_or_null<MDNode>(Node);
    1855             :         if (!MD)
    1856           0 :           return error("Invalid metadata attachment");
    1857             : 
    1858        1018 :         if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop)
    1859           2 :           MD = upgradeInstructionLoopAttachment(*MD);
    1860             : 
    1861        1018 :         if (I->second == LLVMContext::MD_tbaa) {
    1862             :           assert(!MD->isTemporary() && "should load MDs before attachments");
    1863         393 :           MD = UpgradeTBAANode(*MD);
    1864             :         }
    1865        1018 :         Inst->setMetadata(I->second, MD);
    1866             :       }
    1867             :       break;
    1868             :     }
    1869             :     }
    1870             :   }
    1871             : }
    1872             : 
    1873             : /// Parse a single METADATA_KIND record, inserting result in MDKindMap.
    1874       64283 : Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord(
    1875             :     SmallVectorImpl<uint64_t> &Record) {
    1876      128566 :   if (Record.size() < 2)
    1877           0 :     return error("Invalid record");
    1878             : 
    1879      128566 :   unsigned Kind = Record[0];
    1880      128566 :   SmallString<8> Name(Record.begin() + 1, Record.end());
    1881             : 
    1882      128566 :   unsigned NewKind = TheModule.getMDKindID(Name.str());
    1883      192849 :   if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
    1884           0 :     return error("Conflicting METADATA_KIND records");
    1885      192849 :   return Error::success();
    1886             : }
    1887             : 
    1888             : /// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
    1889        2756 : Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() {
    1890        2756 :   if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
    1891           0 :     return error("Invalid record");
    1892             : 
    1893             :   SmallVector<uint64_t, 64> Record;
    1894             : 
    1895             :   // Read all the records.
    1896             :   while (true) {
    1897       66235 :     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
    1898             : 
    1899       66235 :     switch (Entry.Kind) {
    1900           0 :     case BitstreamEntry::SubBlock: // Handled for us already.
    1901             :     case BitstreamEntry::Error:
    1902           0 :       return error("Malformed block");
    1903             :     case BitstreamEntry::EndBlock:
    1904        8268 :       return Error::success();
    1905             :     case BitstreamEntry::Record:
    1906             :       // The interesting case.
    1907             :       break;
    1908             :     }
    1909             : 
    1910             :     // Read a record.
    1911       63479 :     Record.clear();
    1912       63479 :     ++NumMDRecordLoaded;
    1913       63479 :     unsigned Code = Stream.readRecord(Entry.ID, Record);
    1914       63479 :     switch (Code) {
    1915             :     default: // Default behavior: ignore.
    1916             :       break;
    1917       63479 :     case bitc::METADATA_KIND: {
    1918      190437 :       if (Error Err = parseMetadataKindRecord(Record))
    1919           0 :         return Err;
    1920             :       break;
    1921             :     }
    1922             :     }
    1923       63479 :   }
    1924             : }
    1925             : 
    1926           0 : MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) {
    1927           0 :   Pimpl = std::move(RHS.Pimpl);
    1928           0 :   return *this;
    1929             : }
    1930        2880 : MetadataLoader::MetadataLoader(MetadataLoader &&RHS)
    1931        5760 :     : Pimpl(std::move(RHS.Pimpl)) {}
    1932             : 
    1933             : MetadataLoader::~MetadataLoader() = default;
    1934        2879 : MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule,
    1935             :                                BitcodeReaderValueList &ValueList,
    1936             :                                bool IsImporting,
    1937        2879 :                                std::function<Type *(unsigned)> getTypeByID)
    1938             :     : Pimpl(llvm::make_unique<MetadataLoaderImpl>(
    1939        2879 :           Stream, TheModule, ValueList, std::move(getTypeByID), IsImporting)) {}
    1940             : 
    1941        1055 : Error MetadataLoader::parseMetadata(bool ModuleLevel) {
    1942        2110 :   return Pimpl->parseMetadata(ModuleLevel);
    1943             : }
    1944             : 
    1945       42540 : bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); }
    1946             : 
    1947             : /// Return the given metadata, creating a replaceable forward reference if
    1948             : /// necessary.
    1949        9632 : Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) {
    1950       19264 :   return Pimpl->getMetadataFwdRefOrLoad(Idx);
    1951             : }
    1952             : 
    1953        8056 : MDNode *MetadataLoader::getMDNodeFwdRefOrNull(unsigned Idx) {
    1954       24168 :   return Pimpl->getMDNodeFwdRefOrNull(Idx);
    1955             : }
    1956             : 
    1957        7075 : DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) {
    1958       21225 :   return Pimpl->lookupSubprogramForFunction(F);
    1959             : }
    1960             : 
    1961         309 : Error MetadataLoader::parseMetadataAttachment(
    1962             :     Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
    1963         618 :   return Pimpl->parseMetadataAttachment(F, InstructionList);
    1964             : }
    1965             : 
    1966        2756 : Error MetadataLoader::parseMetadataKinds() {
    1967        5512 :   return Pimpl->parseMetadataKinds();
    1968             : }
    1969             : 
    1970           2 : void MetadataLoader::setStripTBAA(bool StripTBAA) {
    1971           6 :   return Pimpl->setStripTBAA(StripTBAA);
    1972             : }
    1973             : 
    1974       14150 : bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); }
    1975             : 
    1976       21315 : unsigned MetadataLoader::size() const { return Pimpl->size(); }
    1977       21225 : void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); }
    1978             : 
    1979       31178 : void MetadataLoader::upgradeDebugIntrinsics(Function &F) {
    1980       93534 :   return Pimpl->upgradeDebugIntrinsics(F);
    1981      216990 : }

Generated by: LCOV version 1.13