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