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