1 //===- MetadataLoader.cpp - Internal BitcodeReader implementation ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "MetadataLoader.h"
10 #include "ValueList.h"
11 
12 #include "llvm/ADT/APInt.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/BitmaskEnum.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/STLFunctionalExtras.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/ADT/ilist_iterator.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/BinaryFormat/Dwarf.h"
26 #include "llvm/Bitcode/BitcodeReader.h"
27 #include "llvm/Bitcode/LLVMBitCodes.h"
28 #include "llvm/Bitstream/BitstreamReader.h"
29 #include "llvm/IR/AutoUpgrade.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/Constants.h"
32 #include "llvm/IR/DebugInfoMetadata.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/GlobalObject.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/Instruction.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/LLVMContext.h"
39 #include "llvm/IR/Metadata.h"
40 #include "llvm/IR/Module.h"
41 #include "llvm/IR/TrackingMDRef.h"
42 #include "llvm/IR/Type.h"
43 #include "llvm/Support/Casting.h"
44 #include "llvm/Support/CommandLine.h"
45 #include "llvm/Support/Compiler.h"
46 #include "llvm/Support/ErrorHandling.h"
47 #include "llvm/Support/type_traits.h"
48 
49 #include <algorithm>
50 #include <cassert>
51 #include <cstddef>
52 #include <cstdint>
53 #include <deque>
54 #include <iterator>
55 #include <limits>
56 #include <optional>
57 #include <string>
58 #include <tuple>
59 #include <type_traits>
60 #include <utility>
61 #include <vector>
62 namespace llvm {
63 class Argument;
64 }
65 
66 using namespace llvm;
67 
68 #define DEBUG_TYPE "bitcode-reader"
69 
70 STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded");
71 STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created");
72 STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded");
73 
74 /// Flag whether we need to import full type definitions for ThinLTO.
75 /// Currently needed for Darwin and LLDB.
76 static cl::opt<bool> ImportFullTypeDefinitions(
77     "import-full-type-definitions", cl::init(false), cl::Hidden,
78     cl::desc("Import full type definitions for ThinLTO."));
79 
80 static cl::opt<bool> DisableLazyLoading(
81     "disable-ondemand-mds-loading", cl::init(false), cl::Hidden,
82     cl::desc("Force disable the lazy-loading on-demand of metadata when "
83              "loading bitcode for importing."));
84 
85 namespace {
86 
87 static int64_t unrotateSign(uint64_t U) { return (U & 1) ? ~(U >> 1) : U >> 1; }
88 
89 class BitcodeReaderMetadataList {
90   /// Array of metadata references.
91   ///
92   /// Don't use std::vector here.  Some versions of libc++ copy (instead of
93   /// move) on resize, and TrackingMDRef is very expensive to copy.
94   SmallVector<TrackingMDRef, 1> MetadataPtrs;
95 
96   /// The set of indices in MetadataPtrs above of forward references that were
97   /// generated.
98   SmallDenseSet<unsigned, 1> ForwardReference;
99 
100   /// The set of indices in MetadataPtrs above of Metadata that need to be
101   /// resolved.
102   SmallDenseSet<unsigned, 1> UnresolvedNodes;
103 
104   /// Structures for resolving old type refs.
105   struct {
106     SmallDenseMap<MDString *, TempMDTuple, 1> Unknown;
107     SmallDenseMap<MDString *, DICompositeType *, 1> Final;
108     SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls;
109     SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays;
110   } OldTypeRefs;
111 
112   LLVMContext &Context;
113 
114   /// Maximum number of valid references. Forward references exceeding the
115   /// maximum must be invalid.
116   unsigned RefsUpperBound;
117 
118 public:
119   BitcodeReaderMetadataList(LLVMContext &C, size_t RefsUpperBound)
120       : Context(C),
121         RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(),
122                                 RefsUpperBound)) {}
123 
124   // vector compatibility methods
125   unsigned size() const { return MetadataPtrs.size(); }
126   void resize(unsigned N) { MetadataPtrs.resize(N); }
127   void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); }
128   void clear() { MetadataPtrs.clear(); }
129   Metadata *back() const { return MetadataPtrs.back(); }
130   void pop_back() { MetadataPtrs.pop_back(); }
131   bool empty() const { return MetadataPtrs.empty(); }
132 
133   Metadata *operator[](unsigned i) const {
134     assert(i < MetadataPtrs.size());
135     return MetadataPtrs[i];
136   }
137 
138   Metadata *lookup(unsigned I) const {
139     if (I < MetadataPtrs.size())
140       return MetadataPtrs[I];
141     return nullptr;
142   }
143 
144   void shrinkTo(unsigned N) {
145     assert(N <= size() && "Invalid shrinkTo request!");
146     assert(ForwardReference.empty() && "Unexpected forward refs");
147     assert(UnresolvedNodes.empty() && "Unexpected unresolved node");
148     MetadataPtrs.resize(N);
149   }
150 
151   /// Return the given metadata, creating a replaceable forward reference if
152   /// necessary.
153   Metadata *getMetadataFwdRef(unsigned Idx);
154 
155   /// Return the given metadata only if it is fully resolved.
156   ///
157   /// Gives the same result as \a lookup(), unless \a MDNode::isResolved()
158   /// would give \c false.
159   Metadata *getMetadataIfResolved(unsigned Idx);
160 
161   MDNode *getMDNodeFwdRefOrNull(unsigned Idx);
162   void assignValue(Metadata *MD, unsigned Idx);
163   void tryToResolveCycles();
164   bool hasFwdRefs() const { return !ForwardReference.empty(); }
165   int getNextFwdRef() {
166     assert(hasFwdRefs());
167     return *ForwardReference.begin();
168   }
169 
170   /// Upgrade a type that had an MDString reference.
171   void addTypeRef(MDString &UUID, DICompositeType &CT);
172 
173   /// Upgrade a type that had an MDString reference.
174   Metadata *upgradeTypeRef(Metadata *MaybeUUID);
175 
176   /// Upgrade a type ref array that may have MDString references.
177   Metadata *upgradeTypeRefArray(Metadata *MaybeTuple);
178 
179 private:
180   Metadata *resolveTypeRefArray(Metadata *MaybeTuple);
181 };
182 
183 void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) {
184   if (auto *MDN = dyn_cast<MDNode>(MD))
185     if (!MDN->isResolved())
186       UnresolvedNodes.insert(Idx);
187 
188   if (Idx == size()) {
189     push_back(MD);
190     return;
191   }
192 
193   if (Idx >= size())
194     resize(Idx + 1);
195 
196   TrackingMDRef &OldMD = MetadataPtrs[Idx];
197   if (!OldMD) {
198     OldMD.reset(MD);
199     return;
200   }
201 
202   // If there was a forward reference to this value, replace it.
203   TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
204   PrevMD->replaceAllUsesWith(MD);
205   ForwardReference.erase(Idx);
206 }
207 
208 Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) {
209   // Bail out for a clearly invalid value.
210   if (Idx >= RefsUpperBound)
211     return nullptr;
212 
213   if (Idx >= size())
214     resize(Idx + 1);
215 
216   if (Metadata *MD = MetadataPtrs[Idx])
217     return MD;
218 
219   // Track forward refs to be resolved later.
220   ForwardReference.insert(Idx);
221 
222   // Create and return a placeholder, which will later be RAUW'd.
223   ++NumMDNodeTemporary;
224   Metadata *MD = MDNode::getTemporary(Context, std::nullopt).release();
225   MetadataPtrs[Idx].reset(MD);
226   return MD;
227 }
228 
229 Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) {
230   Metadata *MD = lookup(Idx);
231   if (auto *N = dyn_cast_or_null<MDNode>(MD))
232     if (!N->isResolved())
233       return nullptr;
234   return MD;
235 }
236 
237 MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) {
238   return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx));
239 }
240 
241 void BitcodeReaderMetadataList::tryToResolveCycles() {
242   if (!ForwardReference.empty())
243     // Still forward references... can't resolve cycles.
244     return;
245 
246   // Give up on finding a full definition for any forward decls that remain.
247   for (const auto &Ref : OldTypeRefs.FwdDecls)
248     OldTypeRefs.Final.insert(Ref);
249   OldTypeRefs.FwdDecls.clear();
250 
251   // Upgrade from old type ref arrays.  In strange cases, this could add to
252   // OldTypeRefs.Unknown.
253   for (const auto &Array : OldTypeRefs.Arrays)
254     Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get()));
255   OldTypeRefs.Arrays.clear();
256 
257   // Replace old string-based type refs with the resolved node, if possible.
258   // If we haven't seen the node, leave it to the verifier to complain about
259   // the invalid string reference.
260   for (const auto &Ref : OldTypeRefs.Unknown) {
261     if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first))
262       Ref.second->replaceAllUsesWith(CT);
263     else
264       Ref.second->replaceAllUsesWith(Ref.first);
265   }
266   OldTypeRefs.Unknown.clear();
267 
268   if (UnresolvedNodes.empty())
269     // Nothing to do.
270     return;
271 
272   // Resolve any cycles.
273   for (unsigned I : UnresolvedNodes) {
274     auto &MD = MetadataPtrs[I];
275     auto *N = dyn_cast_or_null<MDNode>(MD);
276     if (!N)
277       continue;
278 
279     assert(!N->isTemporary() && "Unexpected forward reference");
280     N->resolveCycles();
281   }
282 
283   // Make sure we return early again until there's another unresolved ref.
284   UnresolvedNodes.clear();
285 }
286 
287 void BitcodeReaderMetadataList::addTypeRef(MDString &UUID,
288                                            DICompositeType &CT) {
289   assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID");
290   if (CT.isForwardDecl())
291     OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT));
292   else
293     OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT));
294 }
295 
296 Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) {
297   auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID);
298   if (LLVM_LIKELY(!UUID))
299     return MaybeUUID;
300 
301   if (auto *CT = OldTypeRefs.Final.lookup(UUID))
302     return CT;
303 
304   auto &Ref = OldTypeRefs.Unknown[UUID];
305   if (!Ref)
306     Ref = MDNode::getTemporary(Context, std::nullopt);
307   return Ref.get();
308 }
309 
310 Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) {
311   auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
312   if (!Tuple || Tuple->isDistinct())
313     return MaybeTuple;
314 
315   // Look through the array immediately if possible.
316   if (!Tuple->isTemporary())
317     return resolveTypeRefArray(Tuple);
318 
319   // Create and return a placeholder to use for now.  Eventually
320   // resolveTypeRefArrays() will be resolve this forward reference.
321   OldTypeRefs.Arrays.emplace_back(
322       std::piecewise_construct, std::forward_as_tuple(Tuple),
323       std::forward_as_tuple(MDTuple::getTemporary(Context, std::nullopt)));
324   return OldTypeRefs.Arrays.back().second.get();
325 }
326 
327 Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) {
328   auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
329   if (!Tuple || Tuple->isDistinct())
330     return MaybeTuple;
331 
332   // Look through the DITypeRefArray, upgrading each DIType *.
333   SmallVector<Metadata *, 32> Ops;
334   Ops.reserve(Tuple->getNumOperands());
335   for (Metadata *MD : Tuple->operands())
336     Ops.push_back(upgradeTypeRef(MD));
337 
338   return MDTuple::get(Context, Ops);
339 }
340 
341 namespace {
342 
343 class PlaceholderQueue {
344   // Placeholders would thrash around when moved, so store in a std::deque
345   // instead of some sort of vector.
346   std::deque<DistinctMDOperandPlaceholder> PHs;
347 
348 public:
349   ~PlaceholderQueue() {
350     assert(empty() &&
351            "PlaceholderQueue hasn't been flushed before being destroyed");
352   }
353   bool empty() const { return PHs.empty(); }
354   DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID);
355   void flush(BitcodeReaderMetadataList &MetadataList);
356 
357   /// Return the list of temporaries nodes in the queue, these need to be
358   /// loaded before we can flush the queue.
359   void getTemporaries(BitcodeReaderMetadataList &MetadataList,
360                       DenseSet<unsigned> &Temporaries) {
361     for (auto &PH : PHs) {
362       auto ID = PH.getID();
363       auto *MD = MetadataList.lookup(ID);
364       if (!MD) {
365         Temporaries.insert(ID);
366         continue;
367       }
368       auto *N = dyn_cast_or_null<MDNode>(MD);
369       if (N && N->isTemporary())
370         Temporaries.insert(ID);
371     }
372   }
373 };
374 
375 } // end anonymous namespace
376 
377 DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) {
378   PHs.emplace_back(ID);
379   return PHs.back();
380 }
381 
382 void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) {
383   while (!PHs.empty()) {
384     auto *MD = MetadataList.lookup(PHs.front().getID());
385     assert(MD && "Flushing placeholder on unassigned MD");
386 #ifndef NDEBUG
387     if (auto *MDN = dyn_cast<MDNode>(MD))
388       assert(MDN->isResolved() &&
389              "Flushing Placeholder while cycles aren't resolved");
390 #endif
391     PHs.front().replaceUseWith(MD);
392     PHs.pop_front();
393   }
394 }
395 
396 } // anonymous namespace
397 
398 static Error error(const Twine &Message) {
399   return make_error<StringError>(
400       Message, make_error_code(BitcodeError::CorruptedBitcode));
401 }
402 
403 class MetadataLoader::MetadataLoaderImpl {
404   BitcodeReaderMetadataList MetadataList;
405   BitcodeReaderValueList &ValueList;
406   BitstreamCursor &Stream;
407   LLVMContext &Context;
408   Module &TheModule;
409   MetadataLoaderCallbacks Callbacks;
410 
411   /// Cursor associated with the lazy-loading of Metadata. This is the easy way
412   /// to keep around the right "context" (Abbrev list) to be able to jump in
413   /// the middle of the metadata block and load any record.
414   BitstreamCursor IndexCursor;
415 
416   /// Index that keeps track of MDString values.
417   std::vector<StringRef> MDStringRef;
418 
419   /// On-demand loading of a single MDString. Requires the index above to be
420   /// populated.
421   MDString *lazyLoadOneMDString(unsigned Idx);
422 
423   /// Index that keeps track of where to find a metadata record in the stream.
424   std::vector<uint64_t> GlobalMetadataBitPosIndex;
425 
426   /// Cursor position of the start of the global decl attachments, to enable
427   /// loading using the index built for lazy loading, instead of forward
428   /// references.
429   uint64_t GlobalDeclAttachmentPos = 0;
430 
431 #ifndef NDEBUG
432   /// Baisic correctness check that we end up parsing all of the global decl
433   /// attachments.
434   unsigned NumGlobalDeclAttachSkipped = 0;
435   unsigned NumGlobalDeclAttachParsed = 0;
436 #endif
437 
438   /// Load the global decl attachments, using the index built for lazy loading.
439   Expected<bool> loadGlobalDeclAttachments();
440 
441   /// Populate the index above to enable lazily loading of metadata, and load
442   /// the named metadata as well as the transitively referenced global
443   /// Metadata.
444   Expected<bool> lazyLoadModuleMetadataBlock();
445 
446   /// On-demand loading of a single metadata. Requires the index above to be
447   /// populated.
448   void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders);
449 
450   // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to
451   // point from SP to CU after a block is completly parsed.
452   std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms;
453 
454   /// Functions that need to be matched with subprograms when upgrading old
455   /// metadata.
456   SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
457 
458   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
459   DenseMap<unsigned, unsigned> MDKindMap;
460 
461   bool StripTBAA = false;
462   bool HasSeenOldLoopTags = false;
463   bool NeedUpgradeToDIGlobalVariableExpression = false;
464   bool NeedDeclareExpressionUpgrade = false;
465 
466   /// True if metadata is being parsed for a module being ThinLTO imported.
467   bool IsImporting = false;
468 
469   Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code,
470                          PlaceholderQueue &Placeholders, StringRef Blob,
471                          unsigned &NextMetadataNo);
472   Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob,
473                              function_ref<void(StringRef)> CallBack);
474   Error parseGlobalObjectAttachment(GlobalObject &GO,
475                                     ArrayRef<uint64_t> Record);
476   Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
477 
478   void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders);
479 
480   /// Upgrade old-style CU <-> SP pointers to point from SP to CU.
481   void upgradeCUSubprograms() {
482     for (auto CU_SP : CUSubprograms)
483       if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second))
484         for (auto &Op : SPs->operands())
485           if (auto *SP = dyn_cast_or_null<DISubprogram>(Op))
486             SP->replaceUnit(CU_SP.first);
487     CUSubprograms.clear();
488   }
489 
490   /// Upgrade old-style bare DIGlobalVariables to DIGlobalVariableExpressions.
491   void upgradeCUVariables() {
492     if (!NeedUpgradeToDIGlobalVariableExpression)
493       return;
494 
495     // Upgrade list of variables attached to the CUs.
496     if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu"))
497       for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) {
498         auto *CU = cast<DICompileUnit>(CUNodes->getOperand(I));
499         if (auto *GVs = dyn_cast_or_null<MDTuple>(CU->getRawGlobalVariables()))
500           for (unsigned I = 0; I < GVs->getNumOperands(); I++)
501             if (auto *GV =
502                     dyn_cast_or_null<DIGlobalVariable>(GVs->getOperand(I))) {
503               auto *DGVE = DIGlobalVariableExpression::getDistinct(
504                   Context, GV, DIExpression::get(Context, {}));
505               GVs->replaceOperandWith(I, DGVE);
506             }
507       }
508 
509     // Upgrade variables attached to globals.
510     for (auto &GV : TheModule.globals()) {
511       SmallVector<MDNode *, 1> MDs;
512       GV.getMetadata(LLVMContext::MD_dbg, MDs);
513       GV.eraseMetadata(LLVMContext::MD_dbg);
514       for (auto *MD : MDs)
515         if (auto *DGV = dyn_cast<DIGlobalVariable>(MD)) {
516           auto *DGVE = DIGlobalVariableExpression::getDistinct(
517               Context, DGV, DIExpression::get(Context, {}));
518           GV.addMetadata(LLVMContext::MD_dbg, *DGVE);
519         } else
520           GV.addMetadata(LLVMContext::MD_dbg, *MD);
521     }
522   }
523 
524   /// Remove a leading DW_OP_deref from DIExpressions in a dbg.declare that
525   /// describes a function argument.
526   void upgradeDeclareExpressions(Function &F) {
527     if (!NeedDeclareExpressionUpgrade)
528       return;
529 
530     for (auto &BB : F)
531       for (auto &I : BB)
532         if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
533           if (auto *DIExpr = DDI->getExpression())
534             if (DIExpr->startsWithDeref() &&
535                 isa_and_nonnull<Argument>(DDI->getAddress())) {
536               SmallVector<uint64_t, 8> Ops;
537               Ops.append(std::next(DIExpr->elements_begin()),
538                          DIExpr->elements_end());
539               DDI->setExpression(DIExpression::get(Context, Ops));
540             }
541   }
542 
543   /// Upgrade the expression from previous versions.
544   Error upgradeDIExpression(uint64_t FromVersion,
545                             MutableArrayRef<uint64_t> &Expr,
546                             SmallVectorImpl<uint64_t> &Buffer) {
547     auto N = Expr.size();
548     switch (FromVersion) {
549     default:
550       return error("Invalid record");
551     case 0:
552       if (N >= 3 && Expr[N - 3] == dwarf::DW_OP_bit_piece)
553         Expr[N - 3] = dwarf::DW_OP_LLVM_fragment;
554       [[fallthrough]];
555     case 1:
556       // Move DW_OP_deref to the end.
557       if (N && Expr[0] == dwarf::DW_OP_deref) {
558         auto End = Expr.end();
559         if (Expr.size() >= 3 &&
560             *std::prev(End, 3) == dwarf::DW_OP_LLVM_fragment)
561           End = std::prev(End, 3);
562         std::move(std::next(Expr.begin()), End, Expr.begin());
563         *std::prev(End) = dwarf::DW_OP_deref;
564       }
565       NeedDeclareExpressionUpgrade = true;
566       [[fallthrough]];
567     case 2: {
568       // Change DW_OP_plus to DW_OP_plus_uconst.
569       // Change DW_OP_minus to DW_OP_uconst, DW_OP_minus
570       auto SubExpr = ArrayRef<uint64_t>(Expr);
571       while (!SubExpr.empty()) {
572         // Skip past other operators with their operands
573         // for this version of the IR, obtained from
574         // from historic DIExpression::ExprOperand::getSize().
575         size_t HistoricSize;
576         switch (SubExpr.front()) {
577         default:
578           HistoricSize = 1;
579           break;
580         case dwarf::DW_OP_constu:
581         case dwarf::DW_OP_minus:
582         case dwarf::DW_OP_plus:
583           HistoricSize = 2;
584           break;
585         case dwarf::DW_OP_LLVM_fragment:
586           HistoricSize = 3;
587           break;
588         }
589 
590         // If the expression is malformed, make sure we don't
591         // copy more elements than we should.
592         HistoricSize = std::min(SubExpr.size(), HistoricSize);
593         ArrayRef<uint64_t> Args = SubExpr.slice(1, HistoricSize - 1);
594 
595         switch (SubExpr.front()) {
596         case dwarf::DW_OP_plus:
597           Buffer.push_back(dwarf::DW_OP_plus_uconst);
598           Buffer.append(Args.begin(), Args.end());
599           break;
600         case dwarf::DW_OP_minus:
601           Buffer.push_back(dwarf::DW_OP_constu);
602           Buffer.append(Args.begin(), Args.end());
603           Buffer.push_back(dwarf::DW_OP_minus);
604           break;
605         default:
606           Buffer.push_back(*SubExpr.begin());
607           Buffer.append(Args.begin(), Args.end());
608           break;
609         }
610 
611         // Continue with remaining elements.
612         SubExpr = SubExpr.slice(HistoricSize);
613       }
614       Expr = MutableArrayRef<uint64_t>(Buffer);
615       [[fallthrough]];
616     }
617     case 3:
618       // Up-to-date!
619       break;
620     }
621 
622     return Error::success();
623   }
624 
625   void upgradeDebugInfo() {
626     upgradeCUSubprograms();
627     upgradeCUVariables();
628   }
629 
630   void callMDTypeCallback(Metadata **Val, unsigned TypeID);
631 
632 public:
633   MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule,
634                      BitcodeReaderValueList &ValueList,
635                      MetadataLoaderCallbacks Callbacks, bool IsImporting)
636       : MetadataList(TheModule.getContext(), Stream.SizeInBytes()),
637         ValueList(ValueList), Stream(Stream), Context(TheModule.getContext()),
638         TheModule(TheModule), Callbacks(std::move(Callbacks)),
639         IsImporting(IsImporting) {}
640 
641   Error parseMetadata(bool ModuleLevel);
642 
643   bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); }
644 
645   Metadata *getMetadataFwdRefOrLoad(unsigned ID) {
646     if (ID < MDStringRef.size())
647       return lazyLoadOneMDString(ID);
648     if (auto *MD = MetadataList.lookup(ID))
649       return MD;
650     // If lazy-loading is enabled, we try recursively to load the operand
651     // instead of creating a temporary.
652     if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
653       PlaceholderQueue Placeholders;
654       lazyLoadOneMetadata(ID, Placeholders);
655       resolveForwardRefsAndPlaceholders(Placeholders);
656       return MetadataList.lookup(ID);
657     }
658     return MetadataList.getMetadataFwdRef(ID);
659   }
660 
661   DISubprogram *lookupSubprogramForFunction(Function *F) {
662     return FunctionsWithSPs.lookup(F);
663   }
664 
665   bool hasSeenOldLoopTags() const { return HasSeenOldLoopTags; }
666 
667   Error parseMetadataAttachment(Function &F,
668                                 ArrayRef<Instruction *> InstructionList);
669 
670   Error parseMetadataKinds();
671 
672   void setStripTBAA(bool Value) { StripTBAA = Value; }
673   bool isStrippingTBAA() const { return StripTBAA; }
674 
675   unsigned size() const { return MetadataList.size(); }
676   void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); }
677   void upgradeDebugIntrinsics(Function &F) { upgradeDeclareExpressions(F); }
678 };
679 
680 Expected<bool>
681 MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() {
682   IndexCursor = Stream;
683   SmallVector<uint64_t, 64> Record;
684   GlobalDeclAttachmentPos = 0;
685   // Get the abbrevs, and preload record positions to make them lazy-loadable.
686   while (true) {
687     uint64_t SavedPos = IndexCursor.GetCurrentBitNo();
688     BitstreamEntry Entry;
689     if (Error E =
690             IndexCursor
691                 .advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd)
692                 .moveInto(Entry))
693       return std::move(E);
694 
695     switch (Entry.Kind) {
696     case BitstreamEntry::SubBlock: // Handled for us already.
697     case BitstreamEntry::Error:
698       return error("Malformed block");
699     case BitstreamEntry::EndBlock: {
700       return true;
701     }
702     case BitstreamEntry::Record: {
703       // The interesting case.
704       ++NumMDRecordLoaded;
705       uint64_t CurrentPos = IndexCursor.GetCurrentBitNo();
706       unsigned Code;
707       if (Error E = IndexCursor.skipRecord(Entry.ID).moveInto(Code))
708         return std::move(E);
709       switch (Code) {
710       case bitc::METADATA_STRINGS: {
711         // Rewind and parse the strings.
712         if (Error Err = IndexCursor.JumpToBit(CurrentPos))
713           return std::move(Err);
714         StringRef Blob;
715         Record.clear();
716         if (Expected<unsigned> MaybeRecord =
717                 IndexCursor.readRecord(Entry.ID, Record, &Blob))
718           ;
719         else
720           return MaybeRecord.takeError();
721         unsigned NumStrings = Record[0];
722         MDStringRef.reserve(NumStrings);
723         auto IndexNextMDString = [&](StringRef Str) {
724           MDStringRef.push_back(Str);
725         };
726         if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString))
727           return std::move(Err);
728         break;
729       }
730       case bitc::METADATA_INDEX_OFFSET: {
731         // This is the offset to the index, when we see this we skip all the
732         // records and load only an index to these.
733         if (Error Err = IndexCursor.JumpToBit(CurrentPos))
734           return std::move(Err);
735         Record.clear();
736         if (Expected<unsigned> MaybeRecord =
737                 IndexCursor.readRecord(Entry.ID, Record))
738           ;
739         else
740           return MaybeRecord.takeError();
741         if (Record.size() != 2)
742           return error("Invalid record");
743         auto Offset = Record[0] + (Record[1] << 32);
744         auto BeginPos = IndexCursor.GetCurrentBitNo();
745         if (Error Err = IndexCursor.JumpToBit(BeginPos + Offset))
746           return std::move(Err);
747         Expected<BitstreamEntry> MaybeEntry =
748             IndexCursor.advanceSkippingSubblocks(
749                 BitstreamCursor::AF_DontPopBlockAtEnd);
750         if (!MaybeEntry)
751           return MaybeEntry.takeError();
752         Entry = MaybeEntry.get();
753         assert(Entry.Kind == BitstreamEntry::Record &&
754                "Corrupted bitcode: Expected `Record` when trying to find the "
755                "Metadata index");
756         Record.clear();
757         if (Expected<unsigned> MaybeCode =
758                 IndexCursor.readRecord(Entry.ID, Record))
759           assert(MaybeCode.get() == bitc::METADATA_INDEX &&
760                  "Corrupted bitcode: Expected `METADATA_INDEX` when trying to "
761                  "find the Metadata index");
762         else
763           return MaybeCode.takeError();
764         // Delta unpack
765         auto CurrentValue = BeginPos;
766         GlobalMetadataBitPosIndex.reserve(Record.size());
767         for (auto &Elt : Record) {
768           CurrentValue += Elt;
769           GlobalMetadataBitPosIndex.push_back(CurrentValue);
770         }
771         break;
772       }
773       case bitc::METADATA_INDEX:
774         // We don't expect to get there, the Index is loaded when we encounter
775         // the offset.
776         return error("Corrupted Metadata block");
777       case bitc::METADATA_NAME: {
778         // Named metadata need to be materialized now and aren't deferred.
779         if (Error Err = IndexCursor.JumpToBit(CurrentPos))
780           return std::move(Err);
781         Record.clear();
782 
783         unsigned Code;
784         if (Expected<unsigned> MaybeCode =
785                 IndexCursor.readRecord(Entry.ID, Record)) {
786           Code = MaybeCode.get();
787           assert(Code == bitc::METADATA_NAME);
788         } else
789           return MaybeCode.takeError();
790 
791         // Read name of the named metadata.
792         SmallString<8> Name(Record.begin(), Record.end());
793         if (Expected<unsigned> MaybeCode = IndexCursor.ReadCode())
794           Code = MaybeCode.get();
795         else
796           return MaybeCode.takeError();
797 
798         // Named Metadata comes in two parts, we expect the name to be followed
799         // by the node
800         Record.clear();
801         if (Expected<unsigned> MaybeNextBitCode =
802                 IndexCursor.readRecord(Code, Record))
803           assert(MaybeNextBitCode.get() == bitc::METADATA_NAMED_NODE);
804         else
805           return MaybeNextBitCode.takeError();
806 
807         // Read named metadata elements.
808         unsigned Size = Record.size();
809         NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
810         for (unsigned i = 0; i != Size; ++i) {
811           // FIXME: We could use a placeholder here, however NamedMDNode are
812           // taking MDNode as operand and not using the Metadata infrastructure.
813           // It is acknowledged by 'TODO: Inherit from Metadata' in the
814           // NamedMDNode class definition.
815           MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
816           assert(MD && "Invalid metadata: expect fwd ref to MDNode");
817           NMD->addOperand(MD);
818         }
819         break;
820       }
821       case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
822         if (!GlobalDeclAttachmentPos)
823           GlobalDeclAttachmentPos = SavedPos;
824 #ifndef NDEBUG
825         NumGlobalDeclAttachSkipped++;
826 #endif
827         break;
828       }
829       case bitc::METADATA_KIND:
830       case bitc::METADATA_STRING_OLD:
831       case bitc::METADATA_OLD_FN_NODE:
832       case bitc::METADATA_OLD_NODE:
833       case bitc::METADATA_VALUE:
834       case bitc::METADATA_DISTINCT_NODE:
835       case bitc::METADATA_NODE:
836       case bitc::METADATA_LOCATION:
837       case bitc::METADATA_GENERIC_DEBUG:
838       case bitc::METADATA_SUBRANGE:
839       case bitc::METADATA_ENUMERATOR:
840       case bitc::METADATA_BASIC_TYPE:
841       case bitc::METADATA_STRING_TYPE:
842       case bitc::METADATA_DERIVED_TYPE:
843       case bitc::METADATA_COMPOSITE_TYPE:
844       case bitc::METADATA_SUBROUTINE_TYPE:
845       case bitc::METADATA_MODULE:
846       case bitc::METADATA_FILE:
847       case bitc::METADATA_COMPILE_UNIT:
848       case bitc::METADATA_SUBPROGRAM:
849       case bitc::METADATA_LEXICAL_BLOCK:
850       case bitc::METADATA_LEXICAL_BLOCK_FILE:
851       case bitc::METADATA_NAMESPACE:
852       case bitc::METADATA_COMMON_BLOCK:
853       case bitc::METADATA_MACRO:
854       case bitc::METADATA_MACRO_FILE:
855       case bitc::METADATA_TEMPLATE_TYPE:
856       case bitc::METADATA_TEMPLATE_VALUE:
857       case bitc::METADATA_GLOBAL_VAR:
858       case bitc::METADATA_LOCAL_VAR:
859       case bitc::METADATA_ASSIGN_ID:
860       case bitc::METADATA_LABEL:
861       case bitc::METADATA_EXPRESSION:
862       case bitc::METADATA_OBJC_PROPERTY:
863       case bitc::METADATA_IMPORTED_ENTITY:
864       case bitc::METADATA_GLOBAL_VAR_EXPR:
865       case bitc::METADATA_GENERIC_SUBRANGE:
866         // We don't expect to see any of these, if we see one, give up on
867         // lazy-loading and fallback.
868         MDStringRef.clear();
869         GlobalMetadataBitPosIndex.clear();
870         return false;
871       }
872       break;
873     }
874     }
875   }
876 }
877 
878 // Load the global decl attachments after building the lazy loading index.
879 // We don't load them "lazily" - all global decl attachments must be
880 // parsed since they aren't materialized on demand. However, by delaying
881 // their parsing until after the index is created, we can use the index
882 // instead of creating temporaries.
883 Expected<bool> MetadataLoader::MetadataLoaderImpl::loadGlobalDeclAttachments() {
884   // Nothing to do if we didn't find any of these metadata records.
885   if (!GlobalDeclAttachmentPos)
886     return true;
887   // Use a temporary cursor so that we don't mess up the main Stream cursor or
888   // the lazy loading IndexCursor (which holds the necessary abbrev ids).
889   BitstreamCursor TempCursor = Stream;
890   SmallVector<uint64_t, 64> Record;
891   // Jump to the position before the first global decl attachment, so we can
892   // scan for the first BitstreamEntry record.
893   if (Error Err = TempCursor.JumpToBit(GlobalDeclAttachmentPos))
894     return std::move(Err);
895   while (true) {
896     BitstreamEntry Entry;
897     if (Error E =
898             TempCursor
899                 .advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd)
900                 .moveInto(Entry))
901       return std::move(E);
902 
903     switch (Entry.Kind) {
904     case BitstreamEntry::SubBlock: // Handled for us already.
905     case BitstreamEntry::Error:
906       return error("Malformed block");
907     case BitstreamEntry::EndBlock:
908       // Check that we parsed them all.
909       assert(NumGlobalDeclAttachSkipped == NumGlobalDeclAttachParsed);
910       return true;
911     case BitstreamEntry::Record:
912       break;
913     }
914     uint64_t CurrentPos = TempCursor.GetCurrentBitNo();
915     Expected<unsigned> MaybeCode = TempCursor.skipRecord(Entry.ID);
916     if (!MaybeCode)
917       return MaybeCode.takeError();
918     if (MaybeCode.get() != bitc::METADATA_GLOBAL_DECL_ATTACHMENT) {
919       // Anything other than a global decl attachment signals the end of
920       // these records. Check that we parsed them all.
921       assert(NumGlobalDeclAttachSkipped == NumGlobalDeclAttachParsed);
922       return true;
923     }
924 #ifndef NDEBUG
925     NumGlobalDeclAttachParsed++;
926 #endif
927     // FIXME: we need to do this early because we don't materialize global
928     // value explicitly.
929     if (Error Err = TempCursor.JumpToBit(CurrentPos))
930       return std::move(Err);
931     Record.clear();
932     if (Expected<unsigned> MaybeRecord =
933             TempCursor.readRecord(Entry.ID, Record))
934       ;
935     else
936       return MaybeRecord.takeError();
937     if (Record.size() % 2 == 0)
938       return error("Invalid record");
939     unsigned ValueID = Record[0];
940     if (ValueID >= ValueList.size())
941       return error("Invalid record");
942     if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID])) {
943       // Need to save and restore the current position since
944       // parseGlobalObjectAttachment will resolve all forward references which
945       // would require parsing from locations stored in the index.
946       CurrentPos = TempCursor.GetCurrentBitNo();
947       if (Error Err = parseGlobalObjectAttachment(
948               *GO, ArrayRef<uint64_t>(Record).slice(1)))
949         return std::move(Err);
950       if (Error Err = TempCursor.JumpToBit(CurrentPos))
951         return std::move(Err);
952     }
953   }
954 }
955 
956 void MetadataLoader::MetadataLoaderImpl::callMDTypeCallback(Metadata **Val,
957                                                             unsigned TypeID) {
958   if (Callbacks.MDType) {
959     (*Callbacks.MDType)(Val, TypeID, Callbacks.GetTypeByID,
960                         Callbacks.GetContainedTypeID);
961   }
962 }
963 
964 /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
965 /// module level metadata.
966 Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {
967   if (!ModuleLevel && MetadataList.hasFwdRefs())
968     return error("Invalid metadata: fwd refs into function blocks");
969 
970   // Record the entry position so that we can jump back here and efficiently
971   // skip the whole block in case we lazy-load.
972   auto EntryPos = Stream.GetCurrentBitNo();
973 
974   if (Error Err = Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
975     return Err;
976 
977   SmallVector<uint64_t, 64> Record;
978   PlaceholderQueue Placeholders;
979 
980   // We lazy-load module-level metadata: we build an index for each record, and
981   // then load individual record as needed, starting with the named metadata.
982   if (ModuleLevel && IsImporting && MetadataList.empty() &&
983       !DisableLazyLoading) {
984     auto SuccessOrErr = lazyLoadModuleMetadataBlock();
985     if (!SuccessOrErr)
986       return SuccessOrErr.takeError();
987     if (SuccessOrErr.get()) {
988       // An index was successfully created and we will be able to load metadata
989       // on-demand.
990       MetadataList.resize(MDStringRef.size() +
991                           GlobalMetadataBitPosIndex.size());
992 
993       // Now that we have built the index, load the global decl attachments
994       // that were deferred during that process. This avoids creating
995       // temporaries.
996       SuccessOrErr = loadGlobalDeclAttachments();
997       if (!SuccessOrErr)
998         return SuccessOrErr.takeError();
999       assert(SuccessOrErr.get());
1000 
1001       // Reading the named metadata created forward references and/or
1002       // placeholders, that we flush here.
1003       resolveForwardRefsAndPlaceholders(Placeholders);
1004       upgradeDebugInfo();
1005       // Return at the beginning of the block, since it is easy to skip it
1006       // entirely from there.
1007       Stream.ReadBlockEnd(); // Pop the abbrev block context.
1008       if (Error Err = IndexCursor.JumpToBit(EntryPos))
1009         return Err;
1010       if (Error Err = Stream.SkipBlock()) {
1011         // FIXME this drops the error on the floor, which
1012         // ThinLTO/X86/debuginfo-cu-import.ll relies on.
1013         consumeError(std::move(Err));
1014         return Error::success();
1015       }
1016       return Error::success();
1017     }
1018     // Couldn't load an index, fallback to loading all the block "old-style".
1019   }
1020 
1021   unsigned NextMetadataNo = MetadataList.size();
1022 
1023   // Read all the records.
1024   while (true) {
1025     BitstreamEntry Entry;
1026     if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry))
1027       return E;
1028 
1029     switch (Entry.Kind) {
1030     case BitstreamEntry::SubBlock: // Handled for us already.
1031     case BitstreamEntry::Error:
1032       return error("Malformed block");
1033     case BitstreamEntry::EndBlock:
1034       resolveForwardRefsAndPlaceholders(Placeholders);
1035       upgradeDebugInfo();
1036       return Error::success();
1037     case BitstreamEntry::Record:
1038       // The interesting case.
1039       break;
1040     }
1041 
1042     // Read a record.
1043     Record.clear();
1044     StringRef Blob;
1045     ++NumMDRecordLoaded;
1046     if (Expected<unsigned> MaybeCode =
1047             Stream.readRecord(Entry.ID, Record, &Blob)) {
1048       if (Error Err = parseOneMetadata(Record, MaybeCode.get(), Placeholders,
1049                                        Blob, NextMetadataNo))
1050         return Err;
1051     } else
1052       return MaybeCode.takeError();
1053   }
1054 }
1055 
1056 MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) {
1057   ++NumMDStringLoaded;
1058   if (Metadata *MD = MetadataList.lookup(ID))
1059     return cast<MDString>(MD);
1060   auto MDS = MDString::get(Context, MDStringRef[ID]);
1061   MetadataList.assignValue(MDS, ID);
1062   return MDS;
1063 }
1064 
1065 void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata(
1066     unsigned ID, PlaceholderQueue &Placeholders) {
1067   assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size());
1068   assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString");
1069   // Lookup first if the metadata hasn't already been loaded.
1070   if (auto *MD = MetadataList.lookup(ID)) {
1071     auto *N = cast<MDNode>(MD);
1072     if (!N->isTemporary())
1073       return;
1074   }
1075   SmallVector<uint64_t, 64> Record;
1076   StringRef Blob;
1077   if (Error Err = IndexCursor.JumpToBit(
1078           GlobalMetadataBitPosIndex[ID - MDStringRef.size()]))
1079     report_fatal_error("lazyLoadOneMetadata failed jumping: " +
1080                        Twine(toString(std::move(Err))));
1081   BitstreamEntry Entry;
1082   if (Error E = IndexCursor.advanceSkippingSubblocks().moveInto(Entry))
1083     // FIXME this drops the error on the floor.
1084     report_fatal_error("lazyLoadOneMetadata failed advanceSkippingSubblocks: " +
1085                        Twine(toString(std::move(E))));
1086   ++NumMDRecordLoaded;
1087   if (Expected<unsigned> MaybeCode =
1088           IndexCursor.readRecord(Entry.ID, Record, &Blob)) {
1089     if (Error Err =
1090             parseOneMetadata(Record, MaybeCode.get(), Placeholders, Blob, ID))
1091       report_fatal_error("Can't lazyload MD, parseOneMetadata: " +
1092                          Twine(toString(std::move(Err))));
1093   } else
1094     report_fatal_error("Can't lazyload MD: " +
1095                        Twine(toString(MaybeCode.takeError())));
1096 }
1097 
1098 /// Ensure that all forward-references and placeholders are resolved.
1099 /// Iteratively lazy-loading metadata on-demand if needed.
1100 void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders(
1101     PlaceholderQueue &Placeholders) {
1102   DenseSet<unsigned> Temporaries;
1103   while (true) {
1104     // Populate Temporaries with the placeholders that haven't been loaded yet.
1105     Placeholders.getTemporaries(MetadataList, Temporaries);
1106 
1107     // If we don't have any temporary, or FwdReference, we're done!
1108     if (Temporaries.empty() && !MetadataList.hasFwdRefs())
1109       break;
1110 
1111     // First, load all the temporaries. This can add new placeholders or
1112     // forward references.
1113     for (auto ID : Temporaries)
1114       lazyLoadOneMetadata(ID, Placeholders);
1115     Temporaries.clear();
1116 
1117     // Second, load the forward-references. This can also add new placeholders
1118     // or forward references.
1119     while (MetadataList.hasFwdRefs())
1120       lazyLoadOneMetadata(MetadataList.getNextFwdRef(), Placeholders);
1121   }
1122   // At this point we don't have any forward reference remaining, or temporary
1123   // that haven't been loaded. We can safely drop RAUW support and mark cycles
1124   // as resolved.
1125   MetadataList.tryToResolveCycles();
1126 
1127   // Finally, everything is in place, we can replace the placeholders operands
1128   // with the final node they refer to.
1129   Placeholders.flush(MetadataList);
1130 }
1131 
1132 Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
1133     SmallVectorImpl<uint64_t> &Record, unsigned Code,
1134     PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) {
1135 
1136   bool IsDistinct = false;
1137   auto getMD = [&](unsigned ID) -> Metadata * {
1138     if (ID < MDStringRef.size())
1139       return lazyLoadOneMDString(ID);
1140     if (!IsDistinct) {
1141       if (auto *MD = MetadataList.lookup(ID))
1142         return MD;
1143       // If lazy-loading is enabled, we try recursively to load the operand
1144       // instead of creating a temporary.
1145       if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
1146         // Create a temporary for the node that is referencing the operand we
1147         // will lazy-load. It is needed before recursing in case there are
1148         // uniquing cycles.
1149         MetadataList.getMetadataFwdRef(NextMetadataNo);
1150         lazyLoadOneMetadata(ID, Placeholders);
1151         return MetadataList.lookup(ID);
1152       }
1153       // Return a temporary.
1154       return MetadataList.getMetadataFwdRef(ID);
1155     }
1156     if (auto *MD = MetadataList.getMetadataIfResolved(ID))
1157       return MD;
1158     return &Placeholders.getPlaceholderOp(ID);
1159   };
1160   auto getMDOrNull = [&](unsigned ID) -> Metadata * {
1161     if (ID)
1162       return getMD(ID - 1);
1163     return nullptr;
1164   };
1165   auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * {
1166     if (ID)
1167       return MetadataList.getMetadataFwdRef(ID - 1);
1168     return nullptr;
1169   };
1170   auto getMDString = [&](unsigned ID) -> MDString * {
1171     // This requires that the ID is not really a forward reference.  In
1172     // particular, the MDString must already have been resolved.
1173     auto MDS = getMDOrNull(ID);
1174     return cast_or_null<MDString>(MDS);
1175   };
1176 
1177   // Support for old type refs.
1178   auto getDITypeRefOrNull = [&](unsigned ID) {
1179     return MetadataList.upgradeTypeRef(getMDOrNull(ID));
1180   };
1181 
1182 #define GET_OR_DISTINCT(CLASS, ARGS)                                           \
1183   (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
1184 
1185   switch (Code) {
1186   default: // Default behavior: ignore.
1187     break;
1188   case bitc::METADATA_NAME: {
1189     // Read name of the named metadata.
1190     SmallString<8> Name(Record.begin(), Record.end());
1191     Record.clear();
1192     if (Error E = Stream.ReadCode().moveInto(Code))
1193       return E;
1194 
1195     ++NumMDRecordLoaded;
1196     if (Expected<unsigned> MaybeNextBitCode = Stream.readRecord(Code, Record)) {
1197       if (MaybeNextBitCode.get() != bitc::METADATA_NAMED_NODE)
1198         return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
1199     } else
1200       return MaybeNextBitCode.takeError();
1201 
1202     // Read named metadata elements.
1203     unsigned Size = Record.size();
1204     NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
1205     for (unsigned i = 0; i != Size; ++i) {
1206       MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
1207       if (!MD)
1208         return error("Invalid named metadata: expect fwd ref to MDNode");
1209       NMD->addOperand(MD);
1210     }
1211     break;
1212   }
1213   case bitc::METADATA_OLD_FN_NODE: {
1214     // Deprecated, but still needed to read old bitcode files.
1215     // This is a LocalAsMetadata record, the only type of function-local
1216     // metadata.
1217     if (Record.size() % 2 == 1)
1218       return error("Invalid record");
1219 
1220     // If this isn't a LocalAsMetadata record, we're dropping it.  This used
1221     // to be legal, but there's no upgrade path.
1222     auto dropRecord = [&] {
1223       MetadataList.assignValue(MDNode::get(Context, std::nullopt),
1224                                NextMetadataNo);
1225       NextMetadataNo++;
1226     };
1227     if (Record.size() != 2) {
1228       dropRecord();
1229       break;
1230     }
1231 
1232     unsigned TyID = Record[0];
1233     Type *Ty = Callbacks.GetTypeByID(TyID);
1234     if (Ty->isMetadataTy() || Ty->isVoidTy()) {
1235       dropRecord();
1236       break;
1237     }
1238 
1239     Value *V = ValueList.getValueFwdRef(Record[1], Ty, TyID,
1240                                         /*ConstExprInsertBB*/ nullptr);
1241     if (!V)
1242       return error("Invalid value reference from old fn metadata");
1243 
1244     MetadataList.assignValue(LocalAsMetadata::get(V), NextMetadataNo);
1245     NextMetadataNo++;
1246     break;
1247   }
1248   case bitc::METADATA_OLD_NODE: {
1249     // Deprecated, but still needed to read old bitcode files.
1250     if (Record.size() % 2 == 1)
1251       return error("Invalid record");
1252 
1253     unsigned Size = Record.size();
1254     SmallVector<Metadata *, 8> Elts;
1255     for (unsigned i = 0; i != Size; i += 2) {
1256       unsigned TyID = Record[i];
1257       Type *Ty = Callbacks.GetTypeByID(TyID);
1258       if (!Ty)
1259         return error("Invalid record");
1260       if (Ty->isMetadataTy())
1261         Elts.push_back(getMD(Record[i + 1]));
1262       else if (!Ty->isVoidTy()) {
1263         Value *V = ValueList.getValueFwdRef(Record[i + 1], Ty, TyID,
1264                                             /*ConstExprInsertBB*/ nullptr);
1265         if (!V)
1266           return error("Invalid value reference from old metadata");
1267         Metadata *MD = ValueAsMetadata::get(V);
1268         assert(isa<ConstantAsMetadata>(MD) &&
1269                "Expected non-function-local metadata");
1270         callMDTypeCallback(&MD, TyID);
1271         Elts.push_back(MD);
1272       } else
1273         Elts.push_back(nullptr);
1274     }
1275     MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo);
1276     NextMetadataNo++;
1277     break;
1278   }
1279   case bitc::METADATA_VALUE: {
1280     if (Record.size() != 2)
1281       return error("Invalid record");
1282 
1283     unsigned TyID = Record[0];
1284     Type *Ty = Callbacks.GetTypeByID(TyID);
1285     if (Ty->isMetadataTy() || Ty->isVoidTy())
1286       return error("Invalid record");
1287 
1288     Value *V = ValueList.getValueFwdRef(Record[1], Ty, TyID,
1289                                         /*ConstExprInsertBB*/ nullptr);
1290     if (!V)
1291       return error("Invalid value reference from metadata");
1292 
1293     Metadata *MD = ValueAsMetadata::get(V);
1294     callMDTypeCallback(&MD, TyID);
1295     MetadataList.assignValue(MD, NextMetadataNo);
1296     NextMetadataNo++;
1297     break;
1298   }
1299   case bitc::METADATA_DISTINCT_NODE:
1300     IsDistinct = true;
1301     [[fallthrough]];
1302   case bitc::METADATA_NODE: {
1303     SmallVector<Metadata *, 8> Elts;
1304     Elts.reserve(Record.size());
1305     for (unsigned ID : Record)
1306       Elts.push_back(getMDOrNull(ID));
1307     MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
1308                                         : MDNode::get(Context, Elts),
1309                              NextMetadataNo);
1310     NextMetadataNo++;
1311     break;
1312   }
1313   case bitc::METADATA_LOCATION: {
1314     if (Record.size() != 5 && Record.size() != 6)
1315       return error("Invalid record");
1316 
1317     IsDistinct = Record[0];
1318     unsigned Line = Record[1];
1319     unsigned Column = Record[2];
1320     Metadata *Scope = getMD(Record[3]);
1321     Metadata *InlinedAt = getMDOrNull(Record[4]);
1322     bool ImplicitCode = Record.size() == 6 && Record[5];
1323     MetadataList.assignValue(
1324         GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt,
1325                                      ImplicitCode)),
1326         NextMetadataNo);
1327     NextMetadataNo++;
1328     break;
1329   }
1330   case bitc::METADATA_GENERIC_DEBUG: {
1331     if (Record.size() < 4)
1332       return error("Invalid record");
1333 
1334     IsDistinct = Record[0];
1335     unsigned Tag = Record[1];
1336     unsigned Version = Record[2];
1337 
1338     if (Tag >= 1u << 16 || Version != 0)
1339       return error("Invalid record");
1340 
1341     auto *Header = getMDString(Record[3]);
1342     SmallVector<Metadata *, 8> DwarfOps;
1343     for (unsigned I = 4, E = Record.size(); I != E; ++I)
1344       DwarfOps.push_back(getMDOrNull(Record[I]));
1345     MetadataList.assignValue(
1346         GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)),
1347         NextMetadataNo);
1348     NextMetadataNo++;
1349     break;
1350   }
1351   case bitc::METADATA_SUBRANGE: {
1352     Metadata *Val = nullptr;
1353     // Operand 'count' is interpreted as:
1354     // - Signed integer (version 0)
1355     // - Metadata node  (version 1)
1356     // Operand 'lowerBound' is interpreted as:
1357     // - Signed integer (version 0 and 1)
1358     // - Metadata node  (version 2)
1359     // Operands 'upperBound' and 'stride' are interpreted as:
1360     // - Metadata node  (version 2)
1361     switch (Record[0] >> 1) {
1362     case 0:
1363       Val = GET_OR_DISTINCT(DISubrange,
1364                             (Context, Record[1], unrotateSign(Record[2])));
1365       break;
1366     case 1:
1367       Val = GET_OR_DISTINCT(DISubrange, (Context, getMDOrNull(Record[1]),
1368                                          unrotateSign(Record[2])));
1369       break;
1370     case 2:
1371       Val = GET_OR_DISTINCT(
1372           DISubrange, (Context, getMDOrNull(Record[1]), getMDOrNull(Record[2]),
1373                        getMDOrNull(Record[3]), getMDOrNull(Record[4])));
1374       break;
1375     default:
1376       return error("Invalid record: Unsupported version of DISubrange");
1377     }
1378 
1379     MetadataList.assignValue(Val, NextMetadataNo);
1380     IsDistinct = Record[0] & 1;
1381     NextMetadataNo++;
1382     break;
1383   }
1384   case bitc::METADATA_GENERIC_SUBRANGE: {
1385     Metadata *Val = nullptr;
1386     Val = GET_OR_DISTINCT(DIGenericSubrange,
1387                           (Context, getMDOrNull(Record[1]),
1388                            getMDOrNull(Record[2]), getMDOrNull(Record[3]),
1389                            getMDOrNull(Record[4])));
1390 
1391     MetadataList.assignValue(Val, NextMetadataNo);
1392     IsDistinct = Record[0] & 1;
1393     NextMetadataNo++;
1394     break;
1395   }
1396   case bitc::METADATA_ENUMERATOR: {
1397     if (Record.size() < 3)
1398       return error("Invalid record");
1399 
1400     IsDistinct = Record[0] & 1;
1401     bool IsUnsigned = Record[0] & 2;
1402     bool IsBigInt = Record[0] & 4;
1403     APInt Value;
1404 
1405     if (IsBigInt) {
1406       const uint64_t BitWidth = Record[1];
1407       const size_t NumWords = Record.size() - 3;
1408       Value = readWideAPInt(ArrayRef(&Record[3], NumWords), BitWidth);
1409     } else
1410       Value = APInt(64, unrotateSign(Record[1]), !IsUnsigned);
1411 
1412     MetadataList.assignValue(
1413         GET_OR_DISTINCT(DIEnumerator,
1414                         (Context, Value, IsUnsigned, getMDString(Record[2]))),
1415         NextMetadataNo);
1416     NextMetadataNo++;
1417     break;
1418   }
1419   case bitc::METADATA_BASIC_TYPE: {
1420     if (Record.size() < 6 || Record.size() > 7)
1421       return error("Invalid record");
1422 
1423     IsDistinct = Record[0];
1424     DINode::DIFlags Flags = (Record.size() > 6)
1425                                 ? static_cast<DINode::DIFlags>(Record[6])
1426                                 : DINode::FlagZero;
1427 
1428     MetadataList.assignValue(
1429         GET_OR_DISTINCT(DIBasicType,
1430                         (Context, Record[1], getMDString(Record[2]), Record[3],
1431                          Record[4], Record[5], Flags)),
1432         NextMetadataNo);
1433     NextMetadataNo++;
1434     break;
1435   }
1436   case bitc::METADATA_STRING_TYPE: {
1437     if (Record.size() > 9 || Record.size() < 8)
1438       return error("Invalid record");
1439 
1440     IsDistinct = Record[0];
1441     bool SizeIs8 = Record.size() == 8;
1442     // StringLocationExp (i.e. Record[5]) is added at a later time
1443     // than the other fields. The code here enables backward compatibility.
1444     Metadata *StringLocationExp = SizeIs8 ? nullptr : getMDOrNull(Record[5]);
1445     unsigned Offset = SizeIs8 ? 5 : 6;
1446     MetadataList.assignValue(
1447         GET_OR_DISTINCT(DIStringType,
1448                         (Context, Record[1], getMDString(Record[2]),
1449                          getMDOrNull(Record[3]), getMDOrNull(Record[4]),
1450                          StringLocationExp, Record[Offset], Record[Offset + 1],
1451                          Record[Offset + 2])),
1452         NextMetadataNo);
1453     NextMetadataNo++;
1454     break;
1455   }
1456   case bitc::METADATA_DERIVED_TYPE: {
1457     if (Record.size() < 12 || Record.size() > 14)
1458       return error("Invalid record");
1459 
1460     // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1461     // that there is no DWARF address space associated with DIDerivedType.
1462     std::optional<unsigned> DWARFAddressSpace;
1463     if (Record.size() > 12 && Record[12])
1464       DWARFAddressSpace = Record[12] - 1;
1465 
1466     Metadata *Annotations = nullptr;
1467     if (Record.size() > 13 && Record[13])
1468       Annotations = getMDOrNull(Record[13]);
1469 
1470     IsDistinct = Record[0];
1471     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1472     MetadataList.assignValue(
1473         GET_OR_DISTINCT(DIDerivedType,
1474                         (Context, Record[1], getMDString(Record[2]),
1475                          getMDOrNull(Record[3]), Record[4],
1476                          getDITypeRefOrNull(Record[5]),
1477                          getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1478                          Record[9], DWARFAddressSpace, Flags,
1479                          getDITypeRefOrNull(Record[11]), Annotations)),
1480         NextMetadataNo);
1481     NextMetadataNo++;
1482     break;
1483   }
1484   case bitc::METADATA_COMPOSITE_TYPE: {
1485     if (Record.size() < 16 || Record.size() > 22)
1486       return error("Invalid record");
1487 
1488     // If we have a UUID and this is not a forward declaration, lookup the
1489     // mapping.
1490     IsDistinct = Record[0] & 0x1;
1491     bool IsNotUsedInTypeRef = Record[0] >= 2;
1492     unsigned Tag = Record[1];
1493     MDString *Name = getMDString(Record[2]);
1494     Metadata *File = getMDOrNull(Record[3]);
1495     unsigned Line = Record[4];
1496     Metadata *Scope = getDITypeRefOrNull(Record[5]);
1497     Metadata *BaseType = nullptr;
1498     uint64_t SizeInBits = Record[7];
1499     if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
1500       return error("Alignment value is too large");
1501     uint32_t AlignInBits = Record[8];
1502     uint64_t OffsetInBits = 0;
1503     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1504     Metadata *Elements = nullptr;
1505     unsigned RuntimeLang = Record[12];
1506     Metadata *VTableHolder = nullptr;
1507     Metadata *TemplateParams = nullptr;
1508     Metadata *Discriminator = nullptr;
1509     Metadata *DataLocation = nullptr;
1510     Metadata *Associated = nullptr;
1511     Metadata *Allocated = nullptr;
1512     Metadata *Rank = nullptr;
1513     Metadata *Annotations = nullptr;
1514     auto *Identifier = getMDString(Record[15]);
1515     // If this module is being parsed so that it can be ThinLTO imported
1516     // into another module, composite types only need to be imported
1517     // as type declarations (unless full type definitions requested).
1518     // Create type declarations up front to save memory. Also, buildODRType
1519     // handles the case where this is type ODRed with a definition needed
1520     // by the importing module, in which case the existing definition is
1521     // used.
1522     if (IsImporting && !ImportFullTypeDefinitions && Identifier &&
1523         (Tag == dwarf::DW_TAG_enumeration_type ||
1524          Tag == dwarf::DW_TAG_class_type ||
1525          Tag == dwarf::DW_TAG_structure_type ||
1526          Tag == dwarf::DW_TAG_union_type)) {
1527       Flags = Flags | DINode::FlagFwdDecl;
1528       if (Name) {
1529         // This is a hack around preserving template parameters for simplified
1530         // template names - it should probably be replaced with a
1531         // DICompositeType flag specifying whether template parameters are
1532         // required on declarations of this type.
1533         StringRef NameStr = Name->getString();
1534         if (!NameStr.contains('<') || NameStr.startswith("_STN|"))
1535           TemplateParams = getMDOrNull(Record[14]);
1536       }
1537     } else {
1538       BaseType = getDITypeRefOrNull(Record[6]);
1539       OffsetInBits = Record[9];
1540       Elements = getMDOrNull(Record[11]);
1541       VTableHolder = getDITypeRefOrNull(Record[13]);
1542       TemplateParams = getMDOrNull(Record[14]);
1543       if (Record.size() > 16)
1544         Discriminator = getMDOrNull(Record[16]);
1545       if (Record.size() > 17)
1546         DataLocation = getMDOrNull(Record[17]);
1547       if (Record.size() > 19) {
1548         Associated = getMDOrNull(Record[18]);
1549         Allocated = getMDOrNull(Record[19]);
1550       }
1551       if (Record.size() > 20) {
1552         Rank = getMDOrNull(Record[20]);
1553       }
1554       if (Record.size() > 21) {
1555         Annotations = getMDOrNull(Record[21]);
1556       }
1557     }
1558     DICompositeType *CT = nullptr;
1559     if (Identifier)
1560       CT = DICompositeType::buildODRType(
1561           Context, *Identifier, Tag, Name, File, Line, Scope, BaseType,
1562           SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1563           VTableHolder, TemplateParams, Discriminator, DataLocation, Associated,
1564           Allocated, Rank, Annotations);
1565 
1566     // Create a node if we didn't get a lazy ODR type.
1567     if (!CT)
1568       CT = GET_OR_DISTINCT(DICompositeType,
1569                            (Context, Tag, Name, File, Line, Scope, BaseType,
1570                             SizeInBits, AlignInBits, OffsetInBits, Flags,
1571                             Elements, RuntimeLang, VTableHolder, TemplateParams,
1572                             Identifier, Discriminator, DataLocation, Associated,
1573                             Allocated, Rank, Annotations));
1574     if (!IsNotUsedInTypeRef && Identifier)
1575       MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT));
1576 
1577     MetadataList.assignValue(CT, NextMetadataNo);
1578     NextMetadataNo++;
1579     break;
1580   }
1581   case bitc::METADATA_SUBROUTINE_TYPE: {
1582     if (Record.size() < 3 || Record.size() > 4)
1583       return error("Invalid record");
1584     bool IsOldTypeRefArray = Record[0] < 2;
1585     unsigned CC = (Record.size() > 3) ? Record[3] : 0;
1586 
1587     IsDistinct = Record[0] & 0x1;
1588     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]);
1589     Metadata *Types = getMDOrNull(Record[2]);
1590     if (LLVM_UNLIKELY(IsOldTypeRefArray))
1591       Types = MetadataList.upgradeTypeRefArray(Types);
1592 
1593     MetadataList.assignValue(
1594         GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)),
1595         NextMetadataNo);
1596     NextMetadataNo++;
1597     break;
1598   }
1599 
1600   case bitc::METADATA_MODULE: {
1601     if (Record.size() < 5 || Record.size() > 9)
1602       return error("Invalid record");
1603 
1604     unsigned Offset = Record.size() >= 8 ? 2 : 1;
1605     IsDistinct = Record[0];
1606     MetadataList.assignValue(
1607         GET_OR_DISTINCT(
1608             DIModule,
1609             (Context, Record.size() >= 8 ? getMDOrNull(Record[1]) : nullptr,
1610              getMDOrNull(Record[0 + Offset]), getMDString(Record[1 + Offset]),
1611              getMDString(Record[2 + Offset]), getMDString(Record[3 + Offset]),
1612              getMDString(Record[4 + Offset]),
1613              Record.size() <= 7 ? 0 : Record[7],
1614              Record.size() <= 8 ? false : Record[8])),
1615         NextMetadataNo);
1616     NextMetadataNo++;
1617     break;
1618   }
1619 
1620   case bitc::METADATA_FILE: {
1621     if (Record.size() != 3 && Record.size() != 5 && Record.size() != 6)
1622       return error("Invalid record");
1623 
1624     IsDistinct = Record[0];
1625     std::optional<DIFile::ChecksumInfo<MDString *>> Checksum;
1626     // The BitcodeWriter writes null bytes into Record[3:4] when the Checksum
1627     // is not present. This matches up with the old internal representation,
1628     // and the old encoding for CSK_None in the ChecksumKind. The new
1629     // representation reserves the value 0 in the ChecksumKind to continue to
1630     // encode None in a backwards-compatible way.
1631     if (Record.size() > 4 && Record[3] && Record[4])
1632       Checksum.emplace(static_cast<DIFile::ChecksumKind>(Record[3]),
1633                        getMDString(Record[4]));
1634     MetadataList.assignValue(
1635         GET_OR_DISTINCT(DIFile,
1636                         (Context, getMDString(Record[1]),
1637                          getMDString(Record[2]), Checksum,
1638                          Record.size() > 5 ? getMDString(Record[5]) : nullptr)),
1639         NextMetadataNo);
1640     NextMetadataNo++;
1641     break;
1642   }
1643   case bitc::METADATA_COMPILE_UNIT: {
1644     if (Record.size() < 14 || Record.size() > 22)
1645       return error("Invalid record");
1646 
1647     // Ignore Record[0], which indicates whether this compile unit is
1648     // distinct.  It's always distinct.
1649     IsDistinct = true;
1650     auto *CU = DICompileUnit::getDistinct(
1651         Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]),
1652         Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]),
1653         Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]),
1654         getMDOrNull(Record[12]), getMDOrNull(Record[13]),
1655         Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
1656         Record.size() <= 14 ? 0 : Record[14],
1657         Record.size() <= 16 ? true : Record[16],
1658         Record.size() <= 17 ? false : Record[17],
1659         Record.size() <= 18 ? 0 : Record[18],
1660         Record.size() <= 19 ? false : Record[19],
1661         Record.size() <= 20 ? nullptr : getMDString(Record[20]),
1662         Record.size() <= 21 ? nullptr : getMDString(Record[21]));
1663 
1664     MetadataList.assignValue(CU, NextMetadataNo);
1665     NextMetadataNo++;
1666 
1667     // Move the Upgrade the list of subprograms.
1668     if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11]))
1669       CUSubprograms.push_back({CU, SPs});
1670     break;
1671   }
1672   case bitc::METADATA_SUBPROGRAM: {
1673     if (Record.size() < 18 || Record.size() > 21)
1674       return error("Invalid record");
1675 
1676     bool HasSPFlags = Record[0] & 4;
1677 
1678     DINode::DIFlags Flags;
1679     DISubprogram::DISPFlags SPFlags;
1680     if (!HasSPFlags)
1681       Flags = static_cast<DINode::DIFlags>(Record[11 + 2]);
1682     else {
1683       Flags = static_cast<DINode::DIFlags>(Record[11]);
1684       SPFlags = static_cast<DISubprogram::DISPFlags>(Record[9]);
1685     }
1686 
1687     // Support for old metadata when
1688     // subprogram specific flags are placed in DIFlags.
1689     const unsigned DIFlagMainSubprogram = 1 << 21;
1690     bool HasOldMainSubprogramFlag = Flags & DIFlagMainSubprogram;
1691     if (HasOldMainSubprogramFlag)
1692       // Remove old DIFlagMainSubprogram from DIFlags.
1693       // Note: This assumes that any future use of bit 21 defaults to it
1694       // being 0.
1695       Flags &= ~static_cast<DINode::DIFlags>(DIFlagMainSubprogram);
1696 
1697     if (HasOldMainSubprogramFlag && HasSPFlags)
1698       SPFlags |= DISubprogram::SPFlagMainSubprogram;
1699     else if (!HasSPFlags)
1700       SPFlags = DISubprogram::toSPFlags(
1701           /*IsLocalToUnit=*/Record[7], /*IsDefinition=*/Record[8],
1702           /*IsOptimized=*/Record[14], /*Virtuality=*/Record[11],
1703           /*IsMainSubprogram=*/HasOldMainSubprogramFlag);
1704 
1705     // All definitions should be distinct.
1706     IsDistinct = (Record[0] & 1) || (SPFlags & DISubprogram::SPFlagDefinition);
1707     // Version 1 has a Function as Record[15].
1708     // Version 2 has removed Record[15].
1709     // Version 3 has the Unit as Record[15].
1710     // Version 4 added thisAdjustment.
1711     // Version 5 repacked flags into DISPFlags, changing many element numbers.
1712     bool HasUnit = Record[0] & 2;
1713     if (!HasSPFlags && HasUnit && Record.size() < 19)
1714       return error("Invalid record");
1715     if (HasSPFlags && !HasUnit)
1716       return error("Invalid record");
1717     // Accommodate older formats.
1718     bool HasFn = false;
1719     bool HasThisAdj = true;
1720     bool HasThrownTypes = true;
1721     bool HasAnnotations = false;
1722     bool HasTargetFuncName = false;
1723     unsigned OffsetA = 0;
1724     unsigned OffsetB = 0;
1725     if (!HasSPFlags) {
1726       OffsetA = 2;
1727       OffsetB = 2;
1728       if (Record.size() >= 19) {
1729         HasFn = !HasUnit;
1730         OffsetB++;
1731       }
1732       HasThisAdj = Record.size() >= 20;
1733       HasThrownTypes = Record.size() >= 21;
1734     } else {
1735       HasAnnotations = Record.size() >= 19;
1736       HasTargetFuncName = Record.size() >= 20;
1737     }
1738     Metadata *CUorFn = getMDOrNull(Record[12 + OffsetB]);
1739     DISubprogram *SP = GET_OR_DISTINCT(
1740         DISubprogram,
1741         (Context,
1742          getDITypeRefOrNull(Record[1]),           // scope
1743          getMDString(Record[2]),                  // name
1744          getMDString(Record[3]),                  // linkageName
1745          getMDOrNull(Record[4]),                  // file
1746          Record[5],                               // line
1747          getMDOrNull(Record[6]),                  // type
1748          Record[7 + OffsetA],                     // scopeLine
1749          getDITypeRefOrNull(Record[8 + OffsetA]), // containingType
1750          Record[10 + OffsetA],                    // virtualIndex
1751          HasThisAdj ? Record[16 + OffsetB] : 0,   // thisAdjustment
1752          Flags,                                   // flags
1753          SPFlags,                                 // SPFlags
1754          HasUnit ? CUorFn : nullptr,              // unit
1755          getMDOrNull(Record[13 + OffsetB]),       // templateParams
1756          getMDOrNull(Record[14 + OffsetB]),       // declaration
1757          getMDOrNull(Record[15 + OffsetB]),       // retainedNodes
1758          HasThrownTypes ? getMDOrNull(Record[17 + OffsetB])
1759                         : nullptr, // thrownTypes
1760          HasAnnotations ? getMDOrNull(Record[18 + OffsetB])
1761                         : nullptr, // annotations
1762          HasTargetFuncName ? getMDString(Record[19 + OffsetB])
1763                            : nullptr // targetFuncName
1764          ));
1765     MetadataList.assignValue(SP, NextMetadataNo);
1766     NextMetadataNo++;
1767 
1768     // Upgrade sp->function mapping to function->sp mapping.
1769     if (HasFn) {
1770       if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn))
1771         if (auto *F = dyn_cast<Function>(CMD->getValue())) {
1772           if (F->isMaterializable())
1773             // Defer until materialized; unmaterialized functions may not have
1774             // metadata.
1775             FunctionsWithSPs[F] = SP;
1776           else if (!F->empty())
1777             F->setSubprogram(SP);
1778         }
1779     }
1780     break;
1781   }
1782   case bitc::METADATA_LEXICAL_BLOCK: {
1783     if (Record.size() != 5)
1784       return error("Invalid record");
1785 
1786     IsDistinct = Record[0];
1787     MetadataList.assignValue(
1788         GET_OR_DISTINCT(DILexicalBlock,
1789                         (Context, getMDOrNull(Record[1]),
1790                          getMDOrNull(Record[2]), Record[3], Record[4])),
1791         NextMetadataNo);
1792     NextMetadataNo++;
1793     break;
1794   }
1795   case bitc::METADATA_LEXICAL_BLOCK_FILE: {
1796     if (Record.size() != 4)
1797       return error("Invalid record");
1798 
1799     IsDistinct = Record[0];
1800     MetadataList.assignValue(
1801         GET_OR_DISTINCT(DILexicalBlockFile,
1802                         (Context, getMDOrNull(Record[1]),
1803                          getMDOrNull(Record[2]), Record[3])),
1804         NextMetadataNo);
1805     NextMetadataNo++;
1806     break;
1807   }
1808   case bitc::METADATA_COMMON_BLOCK: {
1809     IsDistinct = Record[0] & 1;
1810     MetadataList.assignValue(
1811         GET_OR_DISTINCT(DICommonBlock,
1812                         (Context, getMDOrNull(Record[1]),
1813                          getMDOrNull(Record[2]), getMDString(Record[3]),
1814                          getMDOrNull(Record[4]), Record[5])),
1815         NextMetadataNo);
1816     NextMetadataNo++;
1817     break;
1818   }
1819   case bitc::METADATA_NAMESPACE: {
1820     // Newer versions of DINamespace dropped file and line.
1821     MDString *Name;
1822     if (Record.size() == 3)
1823       Name = getMDString(Record[2]);
1824     else if (Record.size() == 5)
1825       Name = getMDString(Record[3]);
1826     else
1827       return error("Invalid record");
1828 
1829     IsDistinct = Record[0] & 1;
1830     bool ExportSymbols = Record[0] & 2;
1831     MetadataList.assignValue(
1832         GET_OR_DISTINCT(DINamespace,
1833                         (Context, getMDOrNull(Record[1]), Name, ExportSymbols)),
1834         NextMetadataNo);
1835     NextMetadataNo++;
1836     break;
1837   }
1838   case bitc::METADATA_MACRO: {
1839     if (Record.size() != 5)
1840       return error("Invalid record");
1841 
1842     IsDistinct = Record[0];
1843     MetadataList.assignValue(
1844         GET_OR_DISTINCT(DIMacro,
1845                         (Context, Record[1], Record[2], getMDString(Record[3]),
1846                          getMDString(Record[4]))),
1847         NextMetadataNo);
1848     NextMetadataNo++;
1849     break;
1850   }
1851   case bitc::METADATA_MACRO_FILE: {
1852     if (Record.size() != 5)
1853       return error("Invalid record");
1854 
1855     IsDistinct = Record[0];
1856     MetadataList.assignValue(
1857         GET_OR_DISTINCT(DIMacroFile,
1858                         (Context, Record[1], Record[2], getMDOrNull(Record[3]),
1859                          getMDOrNull(Record[4]))),
1860         NextMetadataNo);
1861     NextMetadataNo++;
1862     break;
1863   }
1864   case bitc::METADATA_TEMPLATE_TYPE: {
1865     if (Record.size() < 3 || Record.size() > 4)
1866       return error("Invalid record");
1867 
1868     IsDistinct = Record[0];
1869     MetadataList.assignValue(
1870         GET_OR_DISTINCT(DITemplateTypeParameter,
1871                         (Context, getMDString(Record[1]),
1872                          getDITypeRefOrNull(Record[2]),
1873                          (Record.size() == 4) ? getMDOrNull(Record[3])
1874                                               : getMDOrNull(false))),
1875         NextMetadataNo);
1876     NextMetadataNo++;
1877     break;
1878   }
1879   case bitc::METADATA_TEMPLATE_VALUE: {
1880     if (Record.size() < 5 || Record.size() > 6)
1881       return error("Invalid record");
1882 
1883     IsDistinct = Record[0];
1884 
1885     MetadataList.assignValue(
1886         GET_OR_DISTINCT(
1887             DITemplateValueParameter,
1888             (Context, Record[1], getMDString(Record[2]),
1889              getDITypeRefOrNull(Record[3]),
1890              (Record.size() == 6) ? getMDOrNull(Record[4]) : getMDOrNull(false),
1891              (Record.size() == 6) ? getMDOrNull(Record[5])
1892                                   : getMDOrNull(Record[4]))),
1893         NextMetadataNo);
1894     NextMetadataNo++;
1895     break;
1896   }
1897   case bitc::METADATA_GLOBAL_VAR: {
1898     if (Record.size() < 11 || Record.size() > 13)
1899       return error("Invalid record");
1900 
1901     IsDistinct = Record[0] & 1;
1902     unsigned Version = Record[0] >> 1;
1903 
1904     if (Version == 2) {
1905       Metadata *Annotations = nullptr;
1906       if (Record.size() > 12)
1907         Annotations = getMDOrNull(Record[12]);
1908 
1909       MetadataList.assignValue(
1910           GET_OR_DISTINCT(DIGlobalVariable,
1911                           (Context, getMDOrNull(Record[1]),
1912                            getMDString(Record[2]), getMDString(Record[3]),
1913                            getMDOrNull(Record[4]), Record[5],
1914                            getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1915                            getMDOrNull(Record[9]), getMDOrNull(Record[10]),
1916                            Record[11], Annotations)),
1917           NextMetadataNo);
1918 
1919       NextMetadataNo++;
1920     } else if (Version == 1) {
1921       // No upgrade necessary. A null field will be introduced to indicate
1922       // that no parameter information is available.
1923       MetadataList.assignValue(
1924           GET_OR_DISTINCT(
1925               DIGlobalVariable,
1926               (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
1927                getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
1928                getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1929                getMDOrNull(Record[10]), nullptr, Record[11], nullptr)),
1930           NextMetadataNo);
1931 
1932       NextMetadataNo++;
1933     } else if (Version == 0) {
1934       // Upgrade old metadata, which stored a global variable reference or a
1935       // ConstantInt here.
1936       NeedUpgradeToDIGlobalVariableExpression = true;
1937       Metadata *Expr = getMDOrNull(Record[9]);
1938       uint32_t AlignInBits = 0;
1939       if (Record.size() > 11) {
1940         if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max())
1941           return error("Alignment value is too large");
1942         AlignInBits = Record[11];
1943       }
1944       GlobalVariable *Attach = nullptr;
1945       if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) {
1946         if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) {
1947           Attach = GV;
1948           Expr = nullptr;
1949         } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) {
1950           Expr = DIExpression::get(Context,
1951                                    {dwarf::DW_OP_constu, CI->getZExtValue(),
1952                                     dwarf::DW_OP_stack_value});
1953         } else {
1954           Expr = nullptr;
1955         }
1956       }
1957       DIGlobalVariable *DGV = GET_OR_DISTINCT(
1958           DIGlobalVariable,
1959           (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
1960            getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
1961            getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1962            getMDOrNull(Record[10]), nullptr, AlignInBits, nullptr));
1963 
1964       DIGlobalVariableExpression *DGVE = nullptr;
1965       if (Attach || Expr)
1966         DGVE = DIGlobalVariableExpression::getDistinct(
1967             Context, DGV, Expr ? Expr : DIExpression::get(Context, {}));
1968       if (Attach)
1969         Attach->addDebugInfo(DGVE);
1970 
1971       auto *MDNode = Expr ? cast<Metadata>(DGVE) : cast<Metadata>(DGV);
1972       MetadataList.assignValue(MDNode, NextMetadataNo);
1973       NextMetadataNo++;
1974     } else
1975       return error("Invalid record");
1976 
1977     break;
1978   }
1979   case bitc::METADATA_ASSIGN_ID: {
1980     if (Record.size() != 1)
1981       return error("Invalid DIAssignID record.");
1982 
1983     IsDistinct = Record[0] & 1;
1984     if (!IsDistinct)
1985       return error("Invalid DIAssignID record. Must be distinct");
1986 
1987     MetadataList.assignValue(DIAssignID::getDistinct(Context), NextMetadataNo);
1988     NextMetadataNo++;
1989     break;
1990   }
1991   case bitc::METADATA_LOCAL_VAR: {
1992     // 10th field is for the obseleted 'inlinedAt:' field.
1993     if (Record.size() < 8 || Record.size() > 10)
1994       return error("Invalid record");
1995 
1996     IsDistinct = Record[0] & 1;
1997     bool HasAlignment = Record[0] & 2;
1998     // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
1999     // DW_TAG_arg_variable, if we have alignment flag encoded it means, that
2000     // this is newer version of record which doesn't have artificial tag.
2001     bool HasTag = !HasAlignment && Record.size() > 8;
2002     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]);
2003     uint32_t AlignInBits = 0;
2004     Metadata *Annotations = nullptr;
2005     if (HasAlignment) {
2006       if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
2007         return error("Alignment value is too large");
2008       AlignInBits = Record[8];
2009       if (Record.size() > 9)
2010         Annotations = getMDOrNull(Record[9]);
2011     }
2012 
2013     MetadataList.assignValue(
2014         GET_OR_DISTINCT(DILocalVariable,
2015                         (Context, getMDOrNull(Record[1 + HasTag]),
2016                          getMDString(Record[2 + HasTag]),
2017                          getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
2018                          getDITypeRefOrNull(Record[5 + HasTag]),
2019                          Record[6 + HasTag], Flags, AlignInBits, Annotations)),
2020         NextMetadataNo);
2021     NextMetadataNo++;
2022     break;
2023   }
2024   case bitc::METADATA_LABEL: {
2025     if (Record.size() != 5)
2026       return error("Invalid record");
2027 
2028     IsDistinct = Record[0] & 1;
2029     MetadataList.assignValue(
2030         GET_OR_DISTINCT(DILabel, (Context, getMDOrNull(Record[1]),
2031                                   getMDString(Record[2]),
2032                                   getMDOrNull(Record[3]), Record[4])),
2033         NextMetadataNo);
2034     NextMetadataNo++;
2035     break;
2036   }
2037   case bitc::METADATA_EXPRESSION: {
2038     if (Record.size() < 1)
2039       return error("Invalid record");
2040 
2041     IsDistinct = Record[0] & 1;
2042     uint64_t Version = Record[0] >> 1;
2043     auto Elts = MutableArrayRef<uint64_t>(Record).slice(1);
2044 
2045     SmallVector<uint64_t, 6> Buffer;
2046     if (Error Err = upgradeDIExpression(Version, Elts, Buffer))
2047       return Err;
2048 
2049     MetadataList.assignValue(GET_OR_DISTINCT(DIExpression, (Context, Elts)),
2050                              NextMetadataNo);
2051     NextMetadataNo++;
2052     break;
2053   }
2054   case bitc::METADATA_GLOBAL_VAR_EXPR: {
2055     if (Record.size() != 3)
2056       return error("Invalid record");
2057 
2058     IsDistinct = Record[0];
2059     Metadata *Expr = getMDOrNull(Record[2]);
2060     if (!Expr)
2061       Expr = DIExpression::get(Context, {});
2062     MetadataList.assignValue(
2063         GET_OR_DISTINCT(DIGlobalVariableExpression,
2064                         (Context, getMDOrNull(Record[1]), Expr)),
2065         NextMetadataNo);
2066     NextMetadataNo++;
2067     break;
2068   }
2069   case bitc::METADATA_OBJC_PROPERTY: {
2070     if (Record.size() != 8)
2071       return error("Invalid record");
2072 
2073     IsDistinct = Record[0];
2074     MetadataList.assignValue(
2075         GET_OR_DISTINCT(DIObjCProperty,
2076                         (Context, getMDString(Record[1]),
2077                          getMDOrNull(Record[2]), Record[3],
2078                          getMDString(Record[4]), getMDString(Record[5]),
2079                          Record[6], getDITypeRefOrNull(Record[7]))),
2080         NextMetadataNo);
2081     NextMetadataNo++;
2082     break;
2083   }
2084   case bitc::METADATA_IMPORTED_ENTITY: {
2085     if (Record.size() < 6 || Record.size() > 8)
2086       return error("Invalid DIImportedEntity record");
2087 
2088     IsDistinct = Record[0];
2089     bool HasFile = (Record.size() >= 7);
2090     bool HasElements = (Record.size() >= 8);
2091     MetadataList.assignValue(
2092         GET_OR_DISTINCT(DIImportedEntity,
2093                         (Context, Record[1], getMDOrNull(Record[2]),
2094                          getDITypeRefOrNull(Record[3]),
2095                          HasFile ? getMDOrNull(Record[6]) : nullptr,
2096                          HasFile ? Record[4] : 0, getMDString(Record[5]),
2097                          HasElements ? getMDOrNull(Record[7]) : nullptr)),
2098         NextMetadataNo);
2099     NextMetadataNo++;
2100     break;
2101   }
2102   case bitc::METADATA_STRING_OLD: {
2103     std::string String(Record.begin(), Record.end());
2104 
2105     // Test for upgrading !llvm.loop.
2106     HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String);
2107     ++NumMDStringLoaded;
2108     Metadata *MD = MDString::get(Context, String);
2109     MetadataList.assignValue(MD, NextMetadataNo);
2110     NextMetadataNo++;
2111     break;
2112   }
2113   case bitc::METADATA_STRINGS: {
2114     auto CreateNextMDString = [&](StringRef Str) {
2115       ++NumMDStringLoaded;
2116       MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo);
2117       NextMetadataNo++;
2118     };
2119     if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString))
2120       return Err;
2121     break;
2122   }
2123   case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
2124     if (Record.size() % 2 == 0)
2125       return error("Invalid record");
2126     unsigned ValueID = Record[0];
2127     if (ValueID >= ValueList.size())
2128       return error("Invalid record");
2129     if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
2130       if (Error Err = parseGlobalObjectAttachment(
2131               *GO, ArrayRef<uint64_t>(Record).slice(1)))
2132         return Err;
2133     break;
2134   }
2135   case bitc::METADATA_KIND: {
2136     // Support older bitcode files that had METADATA_KIND records in a
2137     // block with METADATA_BLOCK_ID.
2138     if (Error Err = parseMetadataKindRecord(Record))
2139       return Err;
2140     break;
2141   }
2142   case bitc::METADATA_ARG_LIST: {
2143     SmallVector<ValueAsMetadata *, 4> Elts;
2144     Elts.reserve(Record.size());
2145     for (uint64_t Elt : Record) {
2146       Metadata *MD = getMD(Elt);
2147       if (isa<MDNode>(MD) && cast<MDNode>(MD)->isTemporary())
2148         return error(
2149             "Invalid record: DIArgList should not contain forward refs");
2150       if (!isa<ValueAsMetadata>(MD))
2151         return error("Invalid record");
2152       Elts.push_back(cast<ValueAsMetadata>(MD));
2153     }
2154 
2155     MetadataList.assignValue(DIArgList::get(Context, Elts), NextMetadataNo);
2156     NextMetadataNo++;
2157     break;
2158   }
2159   }
2160   return Error::success();
2161 #undef GET_OR_DISTINCT
2162 }
2163 
2164 Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
2165     ArrayRef<uint64_t> Record, StringRef Blob,
2166     function_ref<void(StringRef)> CallBack) {
2167   // All the MDStrings in the block are emitted together in a single
2168   // record.  The strings are concatenated and stored in a blob along with
2169   // their sizes.
2170   if (Record.size() != 2)
2171     return error("Invalid record: metadata strings layout");
2172 
2173   unsigned NumStrings = Record[0];
2174   unsigned StringsOffset = Record[1];
2175   if (!NumStrings)
2176     return error("Invalid record: metadata strings with no strings");
2177   if (StringsOffset > Blob.size())
2178     return error("Invalid record: metadata strings corrupt offset");
2179 
2180   StringRef Lengths = Blob.slice(0, StringsOffset);
2181   SimpleBitstreamCursor R(Lengths);
2182 
2183   StringRef Strings = Blob.drop_front(StringsOffset);
2184   do {
2185     if (R.AtEndOfStream())
2186       return error("Invalid record: metadata strings bad length");
2187 
2188     uint32_t Size;
2189     if (Error E = R.ReadVBR(6).moveInto(Size))
2190       return E;
2191     if (Strings.size() < Size)
2192       return error("Invalid record: metadata strings truncated chars");
2193 
2194     CallBack(Strings.slice(0, Size));
2195     Strings = Strings.drop_front(Size);
2196   } while (--NumStrings);
2197 
2198   return Error::success();
2199 }
2200 
2201 Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment(
2202     GlobalObject &GO, ArrayRef<uint64_t> Record) {
2203   assert(Record.size() % 2 == 0);
2204   for (unsigned I = 0, E = Record.size(); I != E; I += 2) {
2205     auto K = MDKindMap.find(Record[I]);
2206     if (K == MDKindMap.end())
2207       return error("Invalid ID");
2208     MDNode *MD =
2209         dyn_cast_or_null<MDNode>(getMetadataFwdRefOrLoad(Record[I + 1]));
2210     if (!MD)
2211       return error("Invalid metadata attachment: expect fwd ref to MDNode");
2212     GO.addMetadata(K->second, *MD);
2213   }
2214   return Error::success();
2215 }
2216 
2217 /// Parse metadata attachments.
2218 Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment(
2219     Function &F, ArrayRef<Instruction *> InstructionList) {
2220   if (Error Err = Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
2221     return Err;
2222 
2223   SmallVector<uint64_t, 64> Record;
2224   PlaceholderQueue Placeholders;
2225 
2226   while (true) {
2227     BitstreamEntry Entry;
2228     if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry))
2229       return E;
2230 
2231     switch (Entry.Kind) {
2232     case BitstreamEntry::SubBlock: // Handled for us already.
2233     case BitstreamEntry::Error:
2234       return error("Malformed block");
2235     case BitstreamEntry::EndBlock:
2236       resolveForwardRefsAndPlaceholders(Placeholders);
2237       return Error::success();
2238     case BitstreamEntry::Record:
2239       // The interesting case.
2240       break;
2241     }
2242 
2243     // Read a metadata attachment record.
2244     Record.clear();
2245     ++NumMDRecordLoaded;
2246     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2247     if (!MaybeRecord)
2248       return MaybeRecord.takeError();
2249     switch (MaybeRecord.get()) {
2250     default: // Default behavior: ignore.
2251       break;
2252     case bitc::METADATA_ATTACHMENT: {
2253       unsigned RecordLength = Record.size();
2254       if (Record.empty())
2255         return error("Invalid record");
2256       if (RecordLength % 2 == 0) {
2257         // A function attachment.
2258         if (Error Err = parseGlobalObjectAttachment(F, Record))
2259           return Err;
2260         continue;
2261       }
2262 
2263       // An instruction attachment.
2264       Instruction *Inst = InstructionList[Record[0]];
2265       for (unsigned i = 1; i != RecordLength; i = i + 2) {
2266         unsigned Kind = Record[i];
2267         DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind);
2268         if (I == MDKindMap.end())
2269           return error("Invalid ID");
2270         if (I->second == LLVMContext::MD_tbaa && StripTBAA)
2271           continue;
2272 
2273         auto Idx = Record[i + 1];
2274         if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) &&
2275             !MetadataList.lookup(Idx)) {
2276           // Load the attachment if it is in the lazy-loadable range and hasn't
2277           // been loaded yet.
2278           lazyLoadOneMetadata(Idx, Placeholders);
2279           resolveForwardRefsAndPlaceholders(Placeholders);
2280         }
2281 
2282         Metadata *Node = MetadataList.getMetadataFwdRef(Idx);
2283         if (isa<LocalAsMetadata>(Node))
2284           // Drop the attachment.  This used to be legal, but there's no
2285           // upgrade path.
2286           break;
2287         MDNode *MD = dyn_cast_or_null<MDNode>(Node);
2288         if (!MD)
2289           return error("Invalid metadata attachment");
2290 
2291         if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop)
2292           MD = upgradeInstructionLoopAttachment(*MD);
2293 
2294         if (I->second == LLVMContext::MD_tbaa) {
2295           assert(!MD->isTemporary() && "should load MDs before attachments");
2296           MD = UpgradeTBAANode(*MD);
2297         }
2298         Inst->setMetadata(I->second, MD);
2299       }
2300       break;
2301     }
2302     }
2303   }
2304 }
2305 
2306 /// Parse a single METADATA_KIND record, inserting result in MDKindMap.
2307 Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord(
2308     SmallVectorImpl<uint64_t> &Record) {
2309   if (Record.size() < 2)
2310     return error("Invalid record");
2311 
2312   unsigned Kind = Record[0];
2313   SmallString<8> Name(Record.begin() + 1, Record.end());
2314 
2315   unsigned NewKind = TheModule.getMDKindID(Name.str());
2316   if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
2317     return error("Conflicting METADATA_KIND records");
2318   return Error::success();
2319 }
2320 
2321 /// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
2322 Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() {
2323   if (Error Err = Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
2324     return Err;
2325 
2326   SmallVector<uint64_t, 64> Record;
2327 
2328   // Read all the records.
2329   while (true) {
2330     BitstreamEntry Entry;
2331     if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry))
2332       return E;
2333 
2334     switch (Entry.Kind) {
2335     case BitstreamEntry::SubBlock: // Handled for us already.
2336     case BitstreamEntry::Error:
2337       return error("Malformed block");
2338     case BitstreamEntry::EndBlock:
2339       return Error::success();
2340     case BitstreamEntry::Record:
2341       // The interesting case.
2342       break;
2343     }
2344 
2345     // Read a record.
2346     Record.clear();
2347     ++NumMDRecordLoaded;
2348     Expected<unsigned> MaybeCode = Stream.readRecord(Entry.ID, Record);
2349     if (!MaybeCode)
2350       return MaybeCode.takeError();
2351     switch (MaybeCode.get()) {
2352     default: // Default behavior: ignore.
2353       break;
2354     case bitc::METADATA_KIND: {
2355       if (Error Err = parseMetadataKindRecord(Record))
2356         return Err;
2357       break;
2358     }
2359     }
2360   }
2361 }
2362 
2363 MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) {
2364   Pimpl = std::move(RHS.Pimpl);
2365   return *this;
2366 }
2367 MetadataLoader::MetadataLoader(MetadataLoader &&RHS)
2368     : Pimpl(std::move(RHS.Pimpl)) {}
2369 
2370 MetadataLoader::~MetadataLoader() = default;
2371 MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule,
2372                                BitcodeReaderValueList &ValueList,
2373                                bool IsImporting,
2374                                MetadataLoaderCallbacks Callbacks)
2375     : Pimpl(std::make_unique<MetadataLoaderImpl>(
2376           Stream, TheModule, ValueList, std::move(Callbacks), IsImporting)) {}
2377 
2378 Error MetadataLoader::parseMetadata(bool ModuleLevel) {
2379   return Pimpl->parseMetadata(ModuleLevel);
2380 }
2381 
2382 bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); }
2383 
2384 /// Return the given metadata, creating a replaceable forward reference if
2385 /// necessary.
2386 Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) {
2387   return Pimpl->getMetadataFwdRefOrLoad(Idx);
2388 }
2389 
2390 DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) {
2391   return Pimpl->lookupSubprogramForFunction(F);
2392 }
2393 
2394 Error MetadataLoader::parseMetadataAttachment(
2395     Function &F, ArrayRef<Instruction *> InstructionList) {
2396   return Pimpl->parseMetadataAttachment(F, InstructionList);
2397 }
2398 
2399 Error MetadataLoader::parseMetadataKinds() {
2400   return Pimpl->parseMetadataKinds();
2401 }
2402 
2403 void MetadataLoader::setStripTBAA(bool StripTBAA) {
2404   return Pimpl->setStripTBAA(StripTBAA);
2405 }
2406 
2407 bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); }
2408 
2409 unsigned MetadataLoader::size() const { return Pimpl->size(); }
2410 void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); }
2411 
2412 void MetadataLoader::upgradeDebugIntrinsics(Function &F) {
2413   return Pimpl->upgradeDebugIntrinsics(F);
2414 }
2415