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