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