]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
Merge llvm, clang, compiler-rt, libc++, lld and lldb release_40 branch
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Bitcode / Reader / MetadataLoader.cpp
1 //===- MetadataLoader.cpp - Internal BitcodeReader implementation ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "MetadataLoader.h"
11 #include "ValueList.h"
12
13 #include "llvm/ADT/APFloat.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Bitcode/BitcodeReader.h"
27 #include "llvm/Bitcode/BitstreamReader.h"
28 #include "llvm/Bitcode/LLVMBitCodes.h"
29 #include "llvm/IR/Argument.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/AutoUpgrade.h"
32 #include "llvm/IR/BasicBlock.h"
33 #include "llvm/IR/CallSite.h"
34 #include "llvm/IR/CallingConv.h"
35 #include "llvm/IR/Comdat.h"
36 #include "llvm/IR/Constant.h"
37 #include "llvm/IR/Constants.h"
38 #include "llvm/IR/DebugInfo.h"
39 #include "llvm/IR/DebugInfoMetadata.h"
40 #include "llvm/IR/DebugLoc.h"
41 #include "llvm/IR/DerivedTypes.h"
42 #include "llvm/IR/DiagnosticInfo.h"
43 #include "llvm/IR/DiagnosticPrinter.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/GVMaterializer.h"
46 #include "llvm/IR/GlobalAlias.h"
47 #include "llvm/IR/GlobalIFunc.h"
48 #include "llvm/IR/GlobalIndirectSymbol.h"
49 #include "llvm/IR/GlobalObject.h"
50 #include "llvm/IR/GlobalValue.h"
51 #include "llvm/IR/GlobalVariable.h"
52 #include "llvm/IR/InlineAsm.h"
53 #include "llvm/IR/InstrTypes.h"
54 #include "llvm/IR/Instruction.h"
55 #include "llvm/IR/Instructions.h"
56 #include "llvm/IR/Intrinsics.h"
57 #include "llvm/IR/LLVMContext.h"
58 #include "llvm/IR/Module.h"
59 #include "llvm/IR/ModuleSummaryIndex.h"
60 #include "llvm/IR/OperandTraits.h"
61 #include "llvm/IR/Operator.h"
62 #include "llvm/IR/TrackingMDRef.h"
63 #include "llvm/IR/Type.h"
64 #include "llvm/IR/ValueHandle.h"
65 #include "llvm/Support/AtomicOrdering.h"
66 #include "llvm/Support/Casting.h"
67 #include "llvm/Support/CommandLine.h"
68 #include "llvm/Support/Compiler.h"
69 #include "llvm/Support/Debug.h"
70 #include "llvm/Support/Error.h"
71 #include "llvm/Support/ErrorHandling.h"
72 #include "llvm/Support/ManagedStatic.h"
73 #include "llvm/Support/MemoryBuffer.h"
74 #include "llvm/Support/raw_ostream.h"
75 #include <algorithm>
76 #include <cassert>
77 #include <cstddef>
78 #include <cstdint>
79 #include <deque>
80 #include <limits>
81 #include <map>
82 #include <memory>
83 #include <string>
84 #include <system_error>
85 #include <tuple>
86 #include <utility>
87 #include <vector>
88
89 using namespace llvm;
90
91 #define DEBUG_TYPE "bitcode-reader"
92
93 STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded");
94 STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created");
95 STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded");
96
97 /// Flag whether we need to import full type definitions for ThinLTO.
98 /// Currently needed for Darwin and LLDB.
99 static cl::opt<bool> ImportFullTypeDefinitions(
100     "import-full-type-definitions", cl::init(false), cl::Hidden,
101     cl::desc("Import full type definitions for ThinLTO."));
102
103 static cl::opt<bool> DisableLazyLoading(
104     "disable-ondemand-mds-loading", cl::init(false), cl::Hidden,
105     cl::desc("Force disable the lazy-loading on-demand of metadata when "
106              "loading bitcode for importing."));
107
108 namespace {
109
110 static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
111
112 class BitcodeReaderMetadataList {
113   /// Array of metadata references.
114   ///
115   /// Don't use std::vector here.  Some versions of libc++ copy (instead of
116   /// move) on resize, and TrackingMDRef is very expensive to copy.
117   SmallVector<TrackingMDRef, 1> MetadataPtrs;
118
119   /// The set of indices in MetadataPtrs above of forward references that were
120   /// generated.
121   SmallDenseSet<unsigned, 1> ForwardReference;
122
123   /// The set of indices in MetadataPtrs above of Metadata that need to be
124   /// resolved.
125   SmallDenseSet<unsigned, 1> UnresolvedNodes;
126
127   /// Structures for resolving old type refs.
128   struct {
129     SmallDenseMap<MDString *, TempMDTuple, 1> Unknown;
130     SmallDenseMap<MDString *, DICompositeType *, 1> Final;
131     SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls;
132     SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays;
133   } OldTypeRefs;
134
135   LLVMContext &Context;
136
137 public:
138   BitcodeReaderMetadataList(LLVMContext &C) : Context(C) {}
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 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   if (Idx >= size())
226     resize(Idx + 1);
227
228   if (Metadata *MD = MetadataPtrs[Idx])
229     return MD;
230
231   // Track forward refs to be resolved later.
232   ForwardReference.insert(Idx);
233
234   // Create and return a placeholder, which will later be RAUW'd.
235   ++NumMDNodeTemporary;
236   Metadata *MD = MDNode::getTemporary(Context, None).release();
237   MetadataPtrs[Idx].reset(MD);
238   return MD;
239 }
240
241 Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) {
242   Metadata *MD = lookup(Idx);
243   if (auto *N = dyn_cast_or_null<MDNode>(MD))
244     if (!N->isResolved())
245       return nullptr;
246   return MD;
247 }
248
249 MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) {
250   return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx));
251 }
252
253 void BitcodeReaderMetadataList::tryToResolveCycles() {
254   if (!ForwardReference.empty())
255     // Still forward references... can't resolve cycles.
256     return;
257
258   // Give up on finding a full definition for any forward decls that remain.
259   for (const auto &Ref : OldTypeRefs.FwdDecls)
260     OldTypeRefs.Final.insert(Ref);
261   OldTypeRefs.FwdDecls.clear();
262
263   // Upgrade from old type ref arrays.  In strange cases, this could add to
264   // OldTypeRefs.Unknown.
265   for (const auto &Array : OldTypeRefs.Arrays)
266     Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get()));
267   OldTypeRefs.Arrays.clear();
268
269   // Replace old string-based type refs with the resolved node, if possible.
270   // If we haven't seen the node, leave it to the verifier to complain about
271   // the invalid string reference.
272   for (const auto &Ref : OldTypeRefs.Unknown) {
273     if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first))
274       Ref.second->replaceAllUsesWith(CT);
275     else
276       Ref.second->replaceAllUsesWith(Ref.first);
277   }
278   OldTypeRefs.Unknown.clear();
279
280   if (UnresolvedNodes.empty())
281     // Nothing to do.
282     return;
283
284   // Resolve any cycles.
285   for (unsigned I : UnresolvedNodes) {
286     auto &MD = MetadataPtrs[I];
287     auto *N = dyn_cast_or_null<MDNode>(MD);
288     if (!N)
289       continue;
290
291     assert(!N->isTemporary() && "Unexpected forward reference");
292     N->resolveCycles();
293   }
294
295   // Make sure we return early again until there's another unresolved ref.
296   UnresolvedNodes.clear();
297 }
298
299 void BitcodeReaderMetadataList::addTypeRef(MDString &UUID,
300                                            DICompositeType &CT) {
301   assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID");
302   if (CT.isForwardDecl())
303     OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT));
304   else
305     OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT));
306 }
307
308 Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) {
309   auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID);
310   if (LLVM_LIKELY(!UUID))
311     return MaybeUUID;
312
313   if (auto *CT = OldTypeRefs.Final.lookup(UUID))
314     return CT;
315
316   auto &Ref = OldTypeRefs.Unknown[UUID];
317   if (!Ref)
318     Ref = MDNode::getTemporary(Context, None);
319   return Ref.get();
320 }
321
322 Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) {
323   auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
324   if (!Tuple || Tuple->isDistinct())
325     return MaybeTuple;
326
327   // Look through the array immediately if possible.
328   if (!Tuple->isTemporary())
329     return resolveTypeRefArray(Tuple);
330
331   // Create and return a placeholder to use for now.  Eventually
332   // resolveTypeRefArrays() will be resolve this forward reference.
333   OldTypeRefs.Arrays.emplace_back(
334       std::piecewise_construct, std::forward_as_tuple(Tuple),
335       std::forward_as_tuple(MDTuple::getTemporary(Context, None)));
336   return OldTypeRefs.Arrays.back().second.get();
337 }
338
339 Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) {
340   auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
341   if (!Tuple || Tuple->isDistinct())
342     return MaybeTuple;
343
344   // Look through the DITypeRefArray, upgrading each DITypeRef.
345   SmallVector<Metadata *, 32> Ops;
346   Ops.reserve(Tuple->getNumOperands());
347   for (Metadata *MD : Tuple->operands())
348     Ops.push_back(upgradeTypeRef(MD));
349
350   return MDTuple::get(Context, Ops);
351 }
352
353 namespace {
354
355 class PlaceholderQueue {
356   // Placeholders would thrash around when moved, so store in a std::deque
357   // instead of some sort of vector.
358   std::deque<DistinctMDOperandPlaceholder> PHs;
359
360 public:
361   bool empty() { return PHs.empty(); }
362   DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID);
363   void flush(BitcodeReaderMetadataList &MetadataList);
364
365   /// Return the list of temporaries nodes in the queue, these need to be
366   /// loaded before we can flush the queue.
367   void getTemporaries(BitcodeReaderMetadataList &MetadataList,
368                       DenseSet<unsigned> &Temporaries) {
369     for (auto &PH : PHs) {
370       auto ID = PH.getID();
371       auto *MD = MetadataList.lookup(ID);
372       if (!MD) {
373         Temporaries.insert(ID);
374         continue;
375       }
376       auto *N = dyn_cast_or_null<MDNode>(MD);
377       if (N && N->isTemporary())
378         Temporaries.insert(ID);
379     }
380   }
381 };
382
383 } // end anonymous namespace
384
385 DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) {
386   PHs.emplace_back(ID);
387   return PHs.back();
388 }
389
390 void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) {
391   while (!PHs.empty()) {
392     auto *MD = MetadataList.lookup(PHs.front().getID());
393     assert(MD && "Flushing placeholder on unassigned MD");
394 #ifndef NDEBUG
395     if (auto *MDN = dyn_cast<MDNode>(MD))
396       assert(MDN->isResolved() &&
397              "Flushing Placeholder while cycles aren't resolved");
398 #endif
399     PHs.front().replaceUseWith(MD);
400     PHs.pop_front();
401   }
402 }
403
404 } // anonynous namespace
405
406 class MetadataLoader::MetadataLoaderImpl {
407   BitcodeReaderMetadataList MetadataList;
408   BitcodeReaderValueList &ValueList;
409   BitstreamCursor &Stream;
410   LLVMContext &Context;
411   Module &TheModule;
412   std::function<Type *(unsigned)> getTypeByID;
413
414   /// Cursor associated with the lazy-loading of Metadata. This is the easy way
415   /// to keep around the right "context" (Abbrev list) to be able to jump in
416   /// the middle of the metadata block and load any record.
417   BitstreamCursor IndexCursor;
418
419   /// Index that keeps track of MDString values.
420   std::vector<StringRef> MDStringRef;
421
422   /// On-demand loading of a single MDString. Requires the index above to be
423   /// populated.
424   MDString *lazyLoadOneMDString(unsigned Idx);
425
426   /// Index that keeps track of where to find a metadata record in the stream.
427   std::vector<uint64_t> GlobalMetadataBitPosIndex;
428
429   /// Populate the index above to enable lazily loading of metadata, and load
430   /// the named metadata as well as the transitively referenced global
431   /// Metadata.
432   Expected<bool> lazyLoadModuleMetadataBlock();
433
434   /// On-demand loading of a single metadata. Requires the index above to be
435   /// populated.
436   void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders);
437
438   // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to
439   // point from SP to CU after a block is completly parsed.
440   std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms;
441
442   /// Functions that need to be matched with subprograms when upgrading old
443   /// metadata.
444   SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
445
446   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
447   DenseMap<unsigned, unsigned> MDKindMap;
448
449   bool StripTBAA = false;
450   bool HasSeenOldLoopTags = false;
451
452   /// True if metadata is being parsed for a module being ThinLTO imported.
453   bool IsImporting = false;
454
455   Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code,
456                          PlaceholderQueue &Placeholders, StringRef Blob,
457                          unsigned &NextMetadataNo);
458   Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob,
459                              std::function<void(StringRef)> CallBack);
460   Error parseGlobalObjectAttachment(GlobalObject &GO,
461                                     ArrayRef<uint64_t> Record);
462   Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
463
464   void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders);
465
466   /// Upgrade old-style CU <-> SP pointers to point from SP to CU.
467   void upgradeCUSubprograms() {
468     for (auto CU_SP : CUSubprograms)
469       if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second))
470         for (auto &Op : SPs->operands())
471           if (auto *SP = dyn_cast_or_null<MDNode>(Op))
472             SP->replaceOperandWith(7, CU_SP.first);
473     CUSubprograms.clear();
474   }
475
476 public:
477   MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule,
478                      BitcodeReaderValueList &ValueList,
479                      std::function<Type *(unsigned)> getTypeByID,
480                      bool IsImporting)
481       : MetadataList(TheModule.getContext()), ValueList(ValueList),
482         Stream(Stream), Context(TheModule.getContext()), TheModule(TheModule),
483         getTypeByID(getTypeByID), IsImporting(IsImporting) {}
484
485   Error parseMetadata(bool ModuleLevel);
486
487   bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); }
488
489   Metadata *getMetadataFwdRefOrLoad(unsigned ID) {
490     if (ID < MDStringRef.size())
491       return lazyLoadOneMDString(ID);
492     if (auto *MD = MetadataList.lookup(ID))
493       return MD;
494     // If lazy-loading is enabled, we try recursively to load the operand
495     // instead of creating a temporary.
496     if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
497       PlaceholderQueue Placeholders;
498       lazyLoadOneMetadata(ID, Placeholders);
499       resolveForwardRefsAndPlaceholders(Placeholders);
500       return MetadataList.lookup(ID);
501     }
502     return MetadataList.getMetadataFwdRef(ID);
503   }
504
505   MDNode *getMDNodeFwdRefOrNull(unsigned Idx) {
506     return MetadataList.getMDNodeFwdRefOrNull(Idx);
507   }
508
509   DISubprogram *lookupSubprogramForFunction(Function *F) {
510     return FunctionsWithSPs.lookup(F);
511   }
512
513   bool hasSeenOldLoopTags() { return HasSeenOldLoopTags; }
514
515   Error parseMetadataAttachment(
516       Function &F, const SmallVectorImpl<Instruction *> &InstructionList);
517
518   Error parseMetadataKinds();
519
520   void setStripTBAA(bool Value) { StripTBAA = Value; }
521   bool isStrippingTBAA() { return StripTBAA; }
522
523   unsigned size() const { return MetadataList.size(); }
524   void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); }
525 };
526
527 Error error(const Twine &Message) {
528   return make_error<StringError>(
529       Message, make_error_code(BitcodeError::CorruptedBitcode));
530 }
531
532 Expected<bool>
533 MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() {
534   IndexCursor = Stream;
535   SmallVector<uint64_t, 64> Record;
536   // Get the abbrevs, and preload record positions to make them lazy-loadable.
537   while (true) {
538     BitstreamEntry Entry = IndexCursor.advanceSkippingSubblocks(
539         BitstreamCursor::AF_DontPopBlockAtEnd);
540     switch (Entry.Kind) {
541     case BitstreamEntry::SubBlock: // Handled for us already.
542     case BitstreamEntry::Error:
543       return error("Malformed block");
544     case BitstreamEntry::EndBlock: {
545       return true;
546     }
547     case BitstreamEntry::Record: {
548       // The interesting case.
549       ++NumMDRecordLoaded;
550       uint64_t CurrentPos = IndexCursor.GetCurrentBitNo();
551       auto Code = IndexCursor.skipRecord(Entry.ID);
552       switch (Code) {
553       case bitc::METADATA_STRINGS: {
554         // Rewind and parse the strings.
555         IndexCursor.JumpToBit(CurrentPos);
556         StringRef Blob;
557         Record.clear();
558         IndexCursor.readRecord(Entry.ID, Record, &Blob);
559         unsigned NumStrings = Record[0];
560         MDStringRef.reserve(NumStrings);
561         auto IndexNextMDString = [&](StringRef Str) {
562           MDStringRef.push_back(Str);
563         };
564         if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString))
565           return std::move(Err);
566         break;
567       }
568       case bitc::METADATA_INDEX_OFFSET: {
569         // This is the offset to the index, when we see this we skip all the
570         // records and load only an index to these.
571         IndexCursor.JumpToBit(CurrentPos);
572         Record.clear();
573         IndexCursor.readRecord(Entry.ID, Record);
574         if (Record.size() != 2)
575           return error("Invalid record");
576         auto Offset = Record[0] + (Record[1] << 32);
577         auto BeginPos = IndexCursor.GetCurrentBitNo();
578         IndexCursor.JumpToBit(BeginPos + Offset);
579         Entry = IndexCursor.advanceSkippingSubblocks(
580             BitstreamCursor::AF_DontPopBlockAtEnd);
581         assert(Entry.Kind == BitstreamEntry::Record &&
582                "Corrupted bitcode: Expected `Record` when trying to find the "
583                "Metadata index");
584         Record.clear();
585         auto Code = IndexCursor.readRecord(Entry.ID, Record);
586         (void)Code;
587         assert(Code == bitc::METADATA_INDEX && "Corrupted bitcode: Expected "
588                                                "`METADATA_INDEX` when trying "
589                                                "to find the Metadata index");
590
591         // Delta unpack
592         auto CurrentValue = BeginPos;
593         GlobalMetadataBitPosIndex.reserve(Record.size());
594         for (auto &Elt : Record) {
595           CurrentValue += Elt;
596           GlobalMetadataBitPosIndex.push_back(CurrentValue);
597         }
598         break;
599       }
600       case bitc::METADATA_INDEX:
601         // We don't expect to get there, the Index is loaded when we encounter
602         // the offset.
603         return error("Corrupted Metadata block");
604       case bitc::METADATA_NAME: {
605         // Named metadata need to be materialized now and aren't deferred.
606         IndexCursor.JumpToBit(CurrentPos);
607         Record.clear();
608         unsigned Code = IndexCursor.readRecord(Entry.ID, Record);
609         assert(Code == bitc::METADATA_NAME);
610
611         // Read name of the named metadata.
612         SmallString<8> Name(Record.begin(), Record.end());
613         Code = IndexCursor.ReadCode();
614
615         // Named Metadata comes in two parts, we expect the name to be followed
616         // by the node
617         Record.clear();
618         unsigned NextBitCode = IndexCursor.readRecord(Code, Record);
619         assert(NextBitCode == bitc::METADATA_NAMED_NODE);
620         (void)NextBitCode;
621
622         // Read named metadata elements.
623         unsigned Size = Record.size();
624         NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
625         for (unsigned i = 0; i != Size; ++i) {
626           // FIXME: We could use a placeholder here, however NamedMDNode are
627           // taking MDNode as operand and not using the Metadata infrastructure.
628           // It is acknowledged by 'TODO: Inherit from Metadata' in the
629           // NamedMDNode class definition.
630           MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
631           assert(MD && "Invalid record");
632           NMD->addOperand(MD);
633         }
634         break;
635       }
636       case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
637         // FIXME: we need to do this early because we don't materialize global
638         // value explicitly.
639         IndexCursor.JumpToBit(CurrentPos);
640         Record.clear();
641         IndexCursor.readRecord(Entry.ID, Record);
642         if (Record.size() % 2 == 0)
643           return error("Invalid record");
644         unsigned ValueID = Record[0];
645         if (ValueID >= ValueList.size())
646           return error("Invalid record");
647         if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
648           if (Error Err = parseGlobalObjectAttachment(
649                   *GO, ArrayRef<uint64_t>(Record).slice(1)))
650             return std::move(Err);
651         break;
652       }
653       case bitc::METADATA_KIND:
654       case bitc::METADATA_STRING_OLD:
655       case bitc::METADATA_OLD_FN_NODE:
656       case bitc::METADATA_OLD_NODE:
657       case bitc::METADATA_VALUE:
658       case bitc::METADATA_DISTINCT_NODE:
659       case bitc::METADATA_NODE:
660       case bitc::METADATA_LOCATION:
661       case bitc::METADATA_GENERIC_DEBUG:
662       case bitc::METADATA_SUBRANGE:
663       case bitc::METADATA_ENUMERATOR:
664       case bitc::METADATA_BASIC_TYPE:
665       case bitc::METADATA_DERIVED_TYPE:
666       case bitc::METADATA_COMPOSITE_TYPE:
667       case bitc::METADATA_SUBROUTINE_TYPE:
668       case bitc::METADATA_MODULE:
669       case bitc::METADATA_FILE:
670       case bitc::METADATA_COMPILE_UNIT:
671       case bitc::METADATA_SUBPROGRAM:
672       case bitc::METADATA_LEXICAL_BLOCK:
673       case bitc::METADATA_LEXICAL_BLOCK_FILE:
674       case bitc::METADATA_NAMESPACE:
675       case bitc::METADATA_MACRO:
676       case bitc::METADATA_MACRO_FILE:
677       case bitc::METADATA_TEMPLATE_TYPE:
678       case bitc::METADATA_TEMPLATE_VALUE:
679       case bitc::METADATA_GLOBAL_VAR:
680       case bitc::METADATA_LOCAL_VAR:
681       case bitc::METADATA_EXPRESSION:
682       case bitc::METADATA_OBJC_PROPERTY:
683       case bitc::METADATA_IMPORTED_ENTITY:
684       case bitc::METADATA_GLOBAL_VAR_EXPR:
685         // We don't expect to see any of these, if we see one, give up on
686         // lazy-loading and fallback.
687         MDStringRef.clear();
688         GlobalMetadataBitPosIndex.clear();
689         return false;
690       }
691       break;
692     }
693     }
694   }
695 }
696
697 /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
698 /// module level metadata.
699 Error MetadataLoader::MetadataLoaderImpl::parseMetadata(bool ModuleLevel) {
700   if (!ModuleLevel && MetadataList.hasFwdRefs())
701     return error("Invalid metadata: fwd refs into function blocks");
702
703   // Record the entry position so that we can jump back here and efficiently
704   // skip the whole block in case we lazy-load.
705   auto EntryPos = Stream.GetCurrentBitNo();
706
707   if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
708     return error("Invalid record");
709
710   SmallVector<uint64_t, 64> Record;
711   PlaceholderQueue Placeholders;
712
713   // We lazy-load module-level metadata: we build an index for each record, and
714   // then load individual record as needed, starting with the named metadata.
715   if (ModuleLevel && IsImporting && MetadataList.empty() &&
716       !DisableLazyLoading) {
717     auto SuccessOrErr = lazyLoadModuleMetadataBlock();
718     if (!SuccessOrErr)
719       return SuccessOrErr.takeError();
720     if (SuccessOrErr.get()) {
721       // An index was successfully created and we will be able to load metadata
722       // on-demand.
723       MetadataList.resize(MDStringRef.size() +
724                           GlobalMetadataBitPosIndex.size());
725
726       // Reading the named metadata created forward references and/or
727       // placeholders, that we flush here.
728       resolveForwardRefsAndPlaceholders(Placeholders);
729       upgradeCUSubprograms();
730       // Return at the beginning of the block, since it is easy to skip it
731       // entirely from there.
732       Stream.ReadBlockEnd(); // Pop the abbrev block context.
733       Stream.JumpToBit(EntryPos);
734       if (Stream.SkipBlock())
735         return error("Invalid record");
736       return Error::success();
737     }
738     // Couldn't load an index, fallback to loading all the block "old-style".
739   }
740
741   unsigned NextMetadataNo = MetadataList.size();
742
743   // Read all the records.
744   while (true) {
745     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
746
747     switch (Entry.Kind) {
748     case BitstreamEntry::SubBlock: // Handled for us already.
749     case BitstreamEntry::Error:
750       return error("Malformed block");
751     case BitstreamEntry::EndBlock:
752       resolveForwardRefsAndPlaceholders(Placeholders);
753       upgradeCUSubprograms();
754       return Error::success();
755     case BitstreamEntry::Record:
756       // The interesting case.
757       break;
758     }
759
760     // Read a record.
761     Record.clear();
762     StringRef Blob;
763     ++NumMDRecordLoaded;
764     unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
765     if (Error Err =
766             parseOneMetadata(Record, Code, Placeholders, Blob, NextMetadataNo))
767       return Err;
768   }
769 }
770
771 MDString *MetadataLoader::MetadataLoaderImpl::lazyLoadOneMDString(unsigned ID) {
772   ++NumMDStringLoaded;
773   if (Metadata *MD = MetadataList.lookup(ID))
774     return cast<MDString>(MD);
775   auto MDS = MDString::get(Context, MDStringRef[ID]);
776   MetadataList.assignValue(MDS, ID);
777   return MDS;
778 }
779
780 void MetadataLoader::MetadataLoaderImpl::lazyLoadOneMetadata(
781     unsigned ID, PlaceholderQueue &Placeholders) {
782   assert(ID < (MDStringRef.size()) + GlobalMetadataBitPosIndex.size());
783   assert(ID >= MDStringRef.size() && "Unexpected lazy-loading of MDString");
784   // Lookup first if the metadata hasn't already been loaded.
785   if (auto *MD = MetadataList.lookup(ID)) {
786     auto *N = dyn_cast_or_null<MDNode>(MD);
787     if (!N->isTemporary())
788       return;
789   }
790   SmallVector<uint64_t, 64> Record;
791   StringRef Blob;
792   IndexCursor.JumpToBit(GlobalMetadataBitPosIndex[ID - MDStringRef.size()]);
793   auto Entry = IndexCursor.advanceSkippingSubblocks();
794   ++NumMDRecordLoaded;
795   unsigned Code = IndexCursor.readRecord(Entry.ID, Record, &Blob);
796   if (Error Err = parseOneMetadata(Record, Code, Placeholders, Blob, ID))
797     report_fatal_error("Can't lazyload MD");
798 }
799
800 /// Ensure that all forward-references and placeholders are resolved.
801 /// Iteratively lazy-loading metadata on-demand if needed.
802 void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders(
803     PlaceholderQueue &Placeholders) {
804   DenseSet<unsigned> Temporaries;
805   while (1) {
806     // Populate Temporaries with the placeholders that haven't been loaded yet.
807     Placeholders.getTemporaries(MetadataList, Temporaries);
808
809     // If we don't have any temporary, or FwdReference, we're done!
810     if (Temporaries.empty() && !MetadataList.hasFwdRefs())
811       break;
812
813     // First, load all the temporaries. This can add new placeholders or
814     // forward references.
815     for (auto ID : Temporaries)
816       lazyLoadOneMetadata(ID, Placeholders);
817     Temporaries.clear();
818
819     // Second, load the forward-references. This can also add new placeholders
820     // or forward references.
821     while (MetadataList.hasFwdRefs())
822       lazyLoadOneMetadata(MetadataList.getNextFwdRef(), Placeholders);
823   }
824   // At this point we don't have any forward reference remaining, or temporary
825   // that haven't been loaded. We can safely drop RAUW support and mark cycles
826   // as resolved.
827   MetadataList.tryToResolveCycles();
828
829   // Finally, everything is in place, we can replace the placeholders operands
830   // with the final node they refer to.
831   Placeholders.flush(MetadataList);
832 }
833
834 Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
835     SmallVectorImpl<uint64_t> &Record, unsigned Code,
836     PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) {
837
838   bool IsDistinct = false;
839   auto getMD = [&](unsigned ID) -> Metadata * {
840     if (ID < MDStringRef.size())
841       return lazyLoadOneMDString(ID);
842     if (!IsDistinct) {
843       if (auto *MD = MetadataList.lookup(ID))
844         return MD;
845       // If lazy-loading is enabled, we try recursively to load the operand
846       // instead of creating a temporary.
847       if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
848         // Create a temporary for the node that is referencing the operand we
849         // will lazy-load. It is needed before recursing in case there are
850         // uniquing cycles.
851         MetadataList.getMetadataFwdRef(NextMetadataNo);
852         lazyLoadOneMetadata(ID, Placeholders);
853         return MetadataList.lookup(ID);
854       }
855       // Return a temporary.
856       return MetadataList.getMetadataFwdRef(ID);
857     }
858     if (auto *MD = MetadataList.getMetadataIfResolved(ID))
859       return MD;
860     return &Placeholders.getPlaceholderOp(ID);
861   };
862   auto getMDOrNull = [&](unsigned ID) -> Metadata * {
863     if (ID)
864       return getMD(ID - 1);
865     return nullptr;
866   };
867   auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * {
868     if (ID)
869       return MetadataList.getMetadataFwdRef(ID - 1);
870     return nullptr;
871   };
872   auto getMDString = [&](unsigned ID) -> MDString * {
873     // This requires that the ID is not really a forward reference.  In
874     // particular, the MDString must already have been resolved.
875     auto MDS = getMDOrNull(ID);
876     return cast_or_null<MDString>(MDS);
877   };
878
879   // Support for old type refs.
880   auto getDITypeRefOrNull = [&](unsigned ID) {
881     return MetadataList.upgradeTypeRef(getMDOrNull(ID));
882   };
883
884 #define GET_OR_DISTINCT(CLASS, ARGS)                                           \
885   (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
886
887   switch (Code) {
888   default: // Default behavior: ignore.
889     break;
890   case bitc::METADATA_NAME: {
891     // Read name of the named metadata.
892     SmallString<8> Name(Record.begin(), Record.end());
893     Record.clear();
894     Code = Stream.ReadCode();
895
896     ++NumMDRecordLoaded;
897     unsigned NextBitCode = Stream.readRecord(Code, Record);
898     if (NextBitCode != bitc::METADATA_NAMED_NODE)
899       return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
900
901     // Read named metadata elements.
902     unsigned Size = Record.size();
903     NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
904     for (unsigned i = 0; i != Size; ++i) {
905       MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
906       if (!MD)
907         return error("Invalid record");
908       NMD->addOperand(MD);
909     }
910     break;
911   }
912   case bitc::METADATA_OLD_FN_NODE: {
913     // FIXME: Remove in 4.0.
914     // This is a LocalAsMetadata record, the only type of function-local
915     // metadata.
916     if (Record.size() % 2 == 1)
917       return error("Invalid record");
918
919     // If this isn't a LocalAsMetadata record, we're dropping it.  This used
920     // to be legal, but there's no upgrade path.
921     auto dropRecord = [&] {
922       MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo);
923       NextMetadataNo++;
924     };
925     if (Record.size() != 2) {
926       dropRecord();
927       break;
928     }
929
930     Type *Ty = getTypeByID(Record[0]);
931     if (Ty->isMetadataTy() || Ty->isVoidTy()) {
932       dropRecord();
933       break;
934     }
935
936     MetadataList.assignValue(
937         LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
938         NextMetadataNo);
939     NextMetadataNo++;
940     break;
941   }
942   case bitc::METADATA_OLD_NODE: {
943     // FIXME: Remove in 4.0.
944     if (Record.size() % 2 == 1)
945       return error("Invalid record");
946
947     unsigned Size = Record.size();
948     SmallVector<Metadata *, 8> Elts;
949     for (unsigned i = 0; i != Size; i += 2) {
950       Type *Ty = getTypeByID(Record[i]);
951       if (!Ty)
952         return error("Invalid record");
953       if (Ty->isMetadataTy())
954         Elts.push_back(getMD(Record[i + 1]));
955       else if (!Ty->isVoidTy()) {
956         auto *MD =
957             ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
958         assert(isa<ConstantAsMetadata>(MD) &&
959                "Expected non-function-local metadata");
960         Elts.push_back(MD);
961       } else
962         Elts.push_back(nullptr);
963     }
964     MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo);
965     NextMetadataNo++;
966     break;
967   }
968   case bitc::METADATA_VALUE: {
969     if (Record.size() != 2)
970       return error("Invalid record");
971
972     Type *Ty = getTypeByID(Record[0]);
973     if (Ty->isMetadataTy() || Ty->isVoidTy())
974       return error("Invalid record");
975
976     MetadataList.assignValue(
977         ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
978         NextMetadataNo);
979     NextMetadataNo++;
980     break;
981   }
982   case bitc::METADATA_DISTINCT_NODE:
983     IsDistinct = true;
984     LLVM_FALLTHROUGH;
985   case bitc::METADATA_NODE: {
986     SmallVector<Metadata *, 8> Elts;
987     Elts.reserve(Record.size());
988     for (unsigned ID : Record)
989       Elts.push_back(getMDOrNull(ID));
990     MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
991                                         : MDNode::get(Context, Elts),
992                              NextMetadataNo);
993     NextMetadataNo++;
994     break;
995   }
996   case bitc::METADATA_LOCATION: {
997     if (Record.size() != 5)
998       return error("Invalid record");
999
1000     IsDistinct = Record[0];
1001     unsigned Line = Record[1];
1002     unsigned Column = Record[2];
1003     Metadata *Scope = getMD(Record[3]);
1004     Metadata *InlinedAt = getMDOrNull(Record[4]);
1005     MetadataList.assignValue(
1006         GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt)),
1007         NextMetadataNo);
1008     NextMetadataNo++;
1009     break;
1010   }
1011   case bitc::METADATA_GENERIC_DEBUG: {
1012     if (Record.size() < 4)
1013       return error("Invalid record");
1014
1015     IsDistinct = Record[0];
1016     unsigned Tag = Record[1];
1017     unsigned Version = Record[2];
1018
1019     if (Tag >= 1u << 16 || Version != 0)
1020       return error("Invalid record");
1021
1022     auto *Header = getMDString(Record[3]);
1023     SmallVector<Metadata *, 8> DwarfOps;
1024     for (unsigned I = 4, E = Record.size(); I != E; ++I)
1025       DwarfOps.push_back(getMDOrNull(Record[I]));
1026     MetadataList.assignValue(
1027         GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)),
1028         NextMetadataNo);
1029     NextMetadataNo++;
1030     break;
1031   }
1032   case bitc::METADATA_SUBRANGE: {
1033     if (Record.size() != 3)
1034       return error("Invalid record");
1035
1036     IsDistinct = Record[0];
1037     MetadataList.assignValue(
1038         GET_OR_DISTINCT(DISubrange,
1039                         (Context, Record[1], unrotateSign(Record[2]))),
1040         NextMetadataNo);
1041     NextMetadataNo++;
1042     break;
1043   }
1044   case bitc::METADATA_ENUMERATOR: {
1045     if (Record.size() != 3)
1046       return error("Invalid record");
1047
1048     IsDistinct = Record[0];
1049     MetadataList.assignValue(
1050         GET_OR_DISTINCT(DIEnumerator, (Context, unrotateSign(Record[1]),
1051                                        getMDString(Record[2]))),
1052         NextMetadataNo);
1053     NextMetadataNo++;
1054     break;
1055   }
1056   case bitc::METADATA_BASIC_TYPE: {
1057     if (Record.size() != 6)
1058       return error("Invalid record");
1059
1060     IsDistinct = Record[0];
1061     MetadataList.assignValue(
1062         GET_OR_DISTINCT(DIBasicType,
1063                         (Context, Record[1], getMDString(Record[2]), Record[3],
1064                          Record[4], Record[5])),
1065         NextMetadataNo);
1066     NextMetadataNo++;
1067     break;
1068   }
1069   case bitc::METADATA_DERIVED_TYPE: {
1070     if (Record.size() != 12)
1071       return error("Invalid record");
1072
1073     IsDistinct = Record[0];
1074     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1075     MetadataList.assignValue(
1076         GET_OR_DISTINCT(DIDerivedType,
1077                         (Context, Record[1], getMDString(Record[2]),
1078                          getMDOrNull(Record[3]), Record[4],
1079                          getDITypeRefOrNull(Record[5]),
1080                          getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1081                          Record[9], Flags, getDITypeRefOrNull(Record[11]))),
1082         NextMetadataNo);
1083     NextMetadataNo++;
1084     break;
1085   }
1086   case bitc::METADATA_COMPOSITE_TYPE: {
1087     if (Record.size() != 16)
1088       return error("Invalid record");
1089
1090     // If we have a UUID and this is not a forward declaration, lookup the
1091     // mapping.
1092     IsDistinct = Record[0] & 0x1;
1093     bool IsNotUsedInTypeRef = Record[0] >= 2;
1094     unsigned Tag = Record[1];
1095     MDString *Name = getMDString(Record[2]);
1096     Metadata *File = getMDOrNull(Record[3]);
1097     unsigned Line = Record[4];
1098     Metadata *Scope = getDITypeRefOrNull(Record[5]);
1099     Metadata *BaseType = nullptr;
1100     uint64_t SizeInBits = Record[7];
1101     if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
1102       return error("Alignment value is too large");
1103     uint32_t AlignInBits = Record[8];
1104     uint64_t OffsetInBits = 0;
1105     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1106     Metadata *Elements = nullptr;
1107     unsigned RuntimeLang = Record[12];
1108     Metadata *VTableHolder = nullptr;
1109     Metadata *TemplateParams = nullptr;
1110     auto *Identifier = getMDString(Record[15]);
1111     // If this module is being parsed so that it can be ThinLTO imported
1112     // into another module, composite types only need to be imported
1113     // as type declarations (unless full type definitions requested).
1114     // Create type declarations up front to save memory. Also, buildODRType
1115     // handles the case where this is type ODRed with a definition needed
1116     // by the importing module, in which case the existing definition is
1117     // used.
1118     if (IsImporting && !ImportFullTypeDefinitions && Identifier &&
1119         (Tag == dwarf::DW_TAG_enumeration_type ||
1120          Tag == dwarf::DW_TAG_class_type ||
1121          Tag == dwarf::DW_TAG_structure_type ||
1122          Tag == dwarf::DW_TAG_union_type)) {
1123       Flags = Flags | DINode::FlagFwdDecl;
1124     } else {
1125       BaseType = getDITypeRefOrNull(Record[6]);
1126       OffsetInBits = Record[9];
1127       Elements = getMDOrNull(Record[11]);
1128       VTableHolder = getDITypeRefOrNull(Record[13]);
1129       TemplateParams = getMDOrNull(Record[14]);
1130     }
1131     DICompositeType *CT = nullptr;
1132     if (Identifier)
1133       CT = DICompositeType::buildODRType(
1134           Context, *Identifier, Tag, Name, File, Line, Scope, BaseType,
1135           SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1136           VTableHolder, TemplateParams);
1137
1138     // Create a node if we didn't get a lazy ODR type.
1139     if (!CT)
1140       CT = GET_OR_DISTINCT(DICompositeType,
1141                            (Context, Tag, Name, File, Line, Scope, BaseType,
1142                             SizeInBits, AlignInBits, OffsetInBits, Flags,
1143                             Elements, RuntimeLang, VTableHolder, TemplateParams,
1144                             Identifier));
1145     if (!IsNotUsedInTypeRef && Identifier)
1146       MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT));
1147
1148     MetadataList.assignValue(CT, NextMetadataNo);
1149     NextMetadataNo++;
1150     break;
1151   }
1152   case bitc::METADATA_SUBROUTINE_TYPE: {
1153     if (Record.size() < 3 || Record.size() > 4)
1154       return error("Invalid record");
1155     bool IsOldTypeRefArray = Record[0] < 2;
1156     unsigned CC = (Record.size() > 3) ? Record[3] : 0;
1157
1158     IsDistinct = Record[0] & 0x1;
1159     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]);
1160     Metadata *Types = getMDOrNull(Record[2]);
1161     if (LLVM_UNLIKELY(IsOldTypeRefArray))
1162       Types = MetadataList.upgradeTypeRefArray(Types);
1163
1164     MetadataList.assignValue(
1165         GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)),
1166         NextMetadataNo);
1167     NextMetadataNo++;
1168     break;
1169   }
1170
1171   case bitc::METADATA_MODULE: {
1172     if (Record.size() != 6)
1173       return error("Invalid record");
1174
1175     IsDistinct = Record[0];
1176     MetadataList.assignValue(
1177         GET_OR_DISTINCT(DIModule,
1178                         (Context, getMDOrNull(Record[1]),
1179                          getMDString(Record[2]), getMDString(Record[3]),
1180                          getMDString(Record[4]), getMDString(Record[5]))),
1181         NextMetadataNo);
1182     NextMetadataNo++;
1183     break;
1184   }
1185
1186   case bitc::METADATA_FILE: {
1187     if (Record.size() != 3 && Record.size() != 5)
1188       return error("Invalid record");
1189
1190     IsDistinct = Record[0];
1191     MetadataList.assignValue(
1192         GET_OR_DISTINCT(
1193             DIFile,
1194             (Context, getMDString(Record[1]), getMDString(Record[2]),
1195              Record.size() == 3 ? DIFile::CSK_None
1196                                 : static_cast<DIFile::ChecksumKind>(Record[3]),
1197              Record.size() == 3 ? nullptr : getMDString(Record[4]))),
1198         NextMetadataNo);
1199     NextMetadataNo++;
1200     break;
1201   }
1202   case bitc::METADATA_COMPILE_UNIT: {
1203     if (Record.size() < 14 || Record.size() > 17)
1204       return error("Invalid record");
1205
1206     // Ignore Record[0], which indicates whether this compile unit is
1207     // distinct.  It's always distinct.
1208     IsDistinct = true;
1209     auto *CU = DICompileUnit::getDistinct(
1210         Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]),
1211         Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]),
1212         Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]),
1213         getMDOrNull(Record[12]), getMDOrNull(Record[13]),
1214         Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
1215         Record.size() <= 14 ? 0 : Record[14],
1216         Record.size() <= 16 ? true : Record[16]);
1217
1218     MetadataList.assignValue(CU, NextMetadataNo);
1219     NextMetadataNo++;
1220
1221     // Move the Upgrade the list of subprograms.
1222     if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11]))
1223       CUSubprograms.push_back({CU, SPs});
1224     break;
1225   }
1226   case bitc::METADATA_SUBPROGRAM: {
1227     if (Record.size() < 18 || Record.size() > 20)
1228       return error("Invalid record");
1229
1230     IsDistinct =
1231         (Record[0] & 1) || Record[8]; // All definitions should be distinct.
1232     // Version 1 has a Function as Record[15].
1233     // Version 2 has removed Record[15].
1234     // Version 3 has the Unit as Record[15].
1235     // Version 4 added thisAdjustment.
1236     bool HasUnit = Record[0] >= 2;
1237     if (HasUnit && Record.size() < 19)
1238       return error("Invalid record");
1239     Metadata *CUorFn = getMDOrNull(Record[15]);
1240     unsigned Offset = Record.size() >= 19 ? 1 : 0;
1241     bool HasFn = Offset && !HasUnit;
1242     bool HasThisAdj = Record.size() >= 20;
1243     DISubprogram *SP = GET_OR_DISTINCT(
1244         DISubprogram, (Context,
1245                        getDITypeRefOrNull(Record[1]),          // scope
1246                        getMDString(Record[2]),                 // name
1247                        getMDString(Record[3]),                 // linkageName
1248                        getMDOrNull(Record[4]),                 // file
1249                        Record[5],                              // line
1250                        getMDOrNull(Record[6]),                 // type
1251                        Record[7],                              // isLocal
1252                        Record[8],                              // isDefinition
1253                        Record[9],                              // scopeLine
1254                        getDITypeRefOrNull(Record[10]),         // containingType
1255                        Record[11],                             // virtuality
1256                        Record[12],                             // virtualIndex
1257                        HasThisAdj ? Record[19] : 0,            // thisAdjustment
1258                        static_cast<DINode::DIFlags>(Record[13] // flags
1259                                                     ),
1260                        Record[14],                       // isOptimized
1261                        HasUnit ? CUorFn : nullptr,       // unit
1262                        getMDOrNull(Record[15 + Offset]), // templateParams
1263                        getMDOrNull(Record[16 + Offset]), // declaration
1264                        getMDOrNull(Record[17 + Offset])  // variables
1265                        ));
1266     MetadataList.assignValue(SP, NextMetadataNo);
1267     NextMetadataNo++;
1268
1269     // Upgrade sp->function mapping to function->sp mapping.
1270     if (HasFn) {
1271       if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn))
1272         if (auto *F = dyn_cast<Function>(CMD->getValue())) {
1273           if (F->isMaterializable())
1274             // Defer until materialized; unmaterialized functions may not have
1275             // metadata.
1276             FunctionsWithSPs[F] = SP;
1277           else if (!F->empty())
1278             F->setSubprogram(SP);
1279         }
1280     }
1281     break;
1282   }
1283   case bitc::METADATA_LEXICAL_BLOCK: {
1284     if (Record.size() != 5)
1285       return error("Invalid record");
1286
1287     IsDistinct = Record[0];
1288     MetadataList.assignValue(
1289         GET_OR_DISTINCT(DILexicalBlock,
1290                         (Context, getMDOrNull(Record[1]),
1291                          getMDOrNull(Record[2]), Record[3], Record[4])),
1292         NextMetadataNo);
1293     NextMetadataNo++;
1294     break;
1295   }
1296   case bitc::METADATA_LEXICAL_BLOCK_FILE: {
1297     if (Record.size() != 4)
1298       return error("Invalid record");
1299
1300     IsDistinct = Record[0];
1301     MetadataList.assignValue(
1302         GET_OR_DISTINCT(DILexicalBlockFile,
1303                         (Context, getMDOrNull(Record[1]),
1304                          getMDOrNull(Record[2]), Record[3])),
1305         NextMetadataNo);
1306     NextMetadataNo++;
1307     break;
1308   }
1309   case bitc::METADATA_NAMESPACE: {
1310     if (Record.size() != 5)
1311       return error("Invalid record");
1312
1313     IsDistinct = Record[0] & 1;
1314     bool ExportSymbols = Record[0] & 2;
1315     MetadataList.assignValue(
1316         GET_OR_DISTINCT(DINamespace,
1317                         (Context, getMDOrNull(Record[1]),
1318                          getMDOrNull(Record[2]), getMDString(Record[3]),
1319                          Record[4], ExportSymbols)),
1320         NextMetadataNo);
1321     NextMetadataNo++;
1322     break;
1323   }
1324   case bitc::METADATA_MACRO: {
1325     if (Record.size() != 5)
1326       return error("Invalid record");
1327
1328     IsDistinct = Record[0];
1329     MetadataList.assignValue(
1330         GET_OR_DISTINCT(DIMacro,
1331                         (Context, Record[1], Record[2], getMDString(Record[3]),
1332                          getMDString(Record[4]))),
1333         NextMetadataNo);
1334     NextMetadataNo++;
1335     break;
1336   }
1337   case bitc::METADATA_MACRO_FILE: {
1338     if (Record.size() != 5)
1339       return error("Invalid record");
1340
1341     IsDistinct = Record[0];
1342     MetadataList.assignValue(
1343         GET_OR_DISTINCT(DIMacroFile,
1344                         (Context, Record[1], Record[2], getMDOrNull(Record[3]),
1345                          getMDOrNull(Record[4]))),
1346         NextMetadataNo);
1347     NextMetadataNo++;
1348     break;
1349   }
1350   case bitc::METADATA_TEMPLATE_TYPE: {
1351     if (Record.size() != 3)
1352       return error("Invalid record");
1353
1354     IsDistinct = Record[0];
1355     MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter,
1356                                              (Context, getMDString(Record[1]),
1357                                               getDITypeRefOrNull(Record[2]))),
1358                              NextMetadataNo);
1359     NextMetadataNo++;
1360     break;
1361   }
1362   case bitc::METADATA_TEMPLATE_VALUE: {
1363     if (Record.size() != 5)
1364       return error("Invalid record");
1365
1366     IsDistinct = Record[0];
1367     MetadataList.assignValue(
1368         GET_OR_DISTINCT(DITemplateValueParameter,
1369                         (Context, Record[1], getMDString(Record[2]),
1370                          getDITypeRefOrNull(Record[3]),
1371                          getMDOrNull(Record[4]))),
1372         NextMetadataNo);
1373     NextMetadataNo++;
1374     break;
1375   }
1376   case bitc::METADATA_GLOBAL_VAR: {
1377     if (Record.size() < 11 || Record.size() > 12)
1378       return error("Invalid record");
1379
1380     IsDistinct = Record[0] & 1;
1381     unsigned Version = Record[0] >> 1;
1382
1383     if (Version == 1) {
1384       MetadataList.assignValue(
1385           GET_OR_DISTINCT(DIGlobalVariable,
1386                           (Context, getMDOrNull(Record[1]),
1387                            getMDString(Record[2]), getMDString(Record[3]),
1388                            getMDOrNull(Record[4]), Record[5],
1389                            getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1390                            getMDOrNull(Record[10]), Record[11])),
1391           NextMetadataNo);
1392       NextMetadataNo++;
1393     } else if (Version == 0) {
1394       // Upgrade old metadata, which stored a global variable reference or a
1395       // ConstantInt here.
1396       Metadata *Expr = getMDOrNull(Record[9]);
1397       uint32_t AlignInBits = 0;
1398       if (Record.size() > 11) {
1399         if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max())
1400           return error("Alignment value is too large");
1401         AlignInBits = Record[11];
1402       }
1403       GlobalVariable *Attach = nullptr;
1404       if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) {
1405         if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) {
1406           Attach = GV;
1407           Expr = nullptr;
1408         } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) {
1409           Expr = DIExpression::get(Context,
1410                                    {dwarf::DW_OP_constu, CI->getZExtValue(),
1411                                     dwarf::DW_OP_stack_value});
1412         } else {
1413           Expr = nullptr;
1414         }
1415       }
1416       DIGlobalVariable *DGV = GET_OR_DISTINCT(
1417           DIGlobalVariable,
1418           (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
1419            getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
1420            getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1421            getMDOrNull(Record[10]), AlignInBits));
1422
1423       auto *DGVE = DIGlobalVariableExpression::getDistinct(Context, DGV, Expr);
1424       MetadataList.assignValue(DGVE, NextMetadataNo);
1425       NextMetadataNo++;
1426       if (Attach)
1427         Attach->addDebugInfo(DGVE);
1428     } else
1429       return error("Invalid record");
1430
1431     break;
1432   }
1433   case bitc::METADATA_LOCAL_VAR: {
1434     // 10th field is for the obseleted 'inlinedAt:' field.
1435     if (Record.size() < 8 || Record.size() > 10)
1436       return error("Invalid record");
1437
1438     IsDistinct = Record[0] & 1;
1439     bool HasAlignment = Record[0] & 2;
1440     // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
1441     // DW_TAG_arg_variable, if we have alignment flag encoded it means, that
1442     // this is newer version of record which doesn't have artifical tag.
1443     bool HasTag = !HasAlignment && Record.size() > 8;
1444     DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]);
1445     uint32_t AlignInBits = 0;
1446     if (HasAlignment) {
1447       if (Record[8 + HasTag] > (uint64_t)std::numeric_limits<uint32_t>::max())
1448         return error("Alignment value is too large");
1449       AlignInBits = Record[8 + HasTag];
1450     }
1451     MetadataList.assignValue(
1452         GET_OR_DISTINCT(DILocalVariable,
1453                         (Context, getMDOrNull(Record[1 + HasTag]),
1454                          getMDString(Record[2 + HasTag]),
1455                          getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
1456                          getDITypeRefOrNull(Record[5 + HasTag]),
1457                          Record[6 + HasTag], Flags, AlignInBits)),
1458         NextMetadataNo);
1459     NextMetadataNo++;
1460     break;
1461   }
1462   case bitc::METADATA_EXPRESSION: {
1463     if (Record.size() < 1)
1464       return error("Invalid record");
1465
1466     IsDistinct = Record[0] & 1;
1467     bool HasOpFragment = Record[0] & 2;
1468     auto Elts = MutableArrayRef<uint64_t>(Record).slice(1);
1469     if (!HasOpFragment)
1470       if (unsigned N = Elts.size())
1471         if (N >= 3 && Elts[N - 3] == dwarf::DW_OP_bit_piece)
1472           Elts[N - 3] = dwarf::DW_OP_LLVM_fragment;
1473
1474     MetadataList.assignValue(
1475         GET_OR_DISTINCT(DIExpression, (Context, makeArrayRef(Record).slice(1))),
1476         NextMetadataNo);
1477     NextMetadataNo++;
1478     break;
1479   }
1480   case bitc::METADATA_GLOBAL_VAR_EXPR: {
1481     if (Record.size() != 3)
1482       return error("Invalid record");
1483
1484     IsDistinct = Record[0];
1485     MetadataList.assignValue(GET_OR_DISTINCT(DIGlobalVariableExpression,
1486                                              (Context, getMDOrNull(Record[1]),
1487                                               getMDOrNull(Record[2]))),
1488                              NextMetadataNo);
1489     NextMetadataNo++;
1490     break;
1491   }
1492   case bitc::METADATA_OBJC_PROPERTY: {
1493     if (Record.size() != 8)
1494       return error("Invalid record");
1495
1496     IsDistinct = Record[0];
1497     MetadataList.assignValue(
1498         GET_OR_DISTINCT(DIObjCProperty,
1499                         (Context, getMDString(Record[1]),
1500                          getMDOrNull(Record[2]), Record[3],
1501                          getMDString(Record[4]), getMDString(Record[5]),
1502                          Record[6], getDITypeRefOrNull(Record[7]))),
1503         NextMetadataNo);
1504     NextMetadataNo++;
1505     break;
1506   }
1507   case bitc::METADATA_IMPORTED_ENTITY: {
1508     if (Record.size() != 6)
1509       return error("Invalid record");
1510
1511     IsDistinct = Record[0];
1512     MetadataList.assignValue(
1513         GET_OR_DISTINCT(DIImportedEntity,
1514                         (Context, Record[1], getMDOrNull(Record[2]),
1515                          getDITypeRefOrNull(Record[3]), Record[4],
1516                          getMDString(Record[5]))),
1517         NextMetadataNo);
1518     NextMetadataNo++;
1519     break;
1520   }
1521   case bitc::METADATA_STRING_OLD: {
1522     std::string String(Record.begin(), Record.end());
1523
1524     // Test for upgrading !llvm.loop.
1525     HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String);
1526     ++NumMDStringLoaded;
1527     Metadata *MD = MDString::get(Context, String);
1528     MetadataList.assignValue(MD, NextMetadataNo);
1529     NextMetadataNo++;
1530     break;
1531   }
1532   case bitc::METADATA_STRINGS: {
1533     auto CreateNextMDString = [&](StringRef Str) {
1534       ++NumMDStringLoaded;
1535       MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo);
1536       NextMetadataNo++;
1537     };
1538     if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString))
1539       return Err;
1540     break;
1541   }
1542   case bitc::METADATA_GLOBAL_DECL_ATTACHMENT: {
1543     if (Record.size() % 2 == 0)
1544       return error("Invalid record");
1545     unsigned ValueID = Record[0];
1546     if (ValueID >= ValueList.size())
1547       return error("Invalid record");
1548     if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
1549       if (Error Err = parseGlobalObjectAttachment(
1550               *GO, ArrayRef<uint64_t>(Record).slice(1)))
1551         return Err;
1552     break;
1553   }
1554   case bitc::METADATA_KIND: {
1555     // Support older bitcode files that had METADATA_KIND records in a
1556     // block with METADATA_BLOCK_ID.
1557     if (Error Err = parseMetadataKindRecord(Record))
1558       return Err;
1559     break;
1560   }
1561   }
1562   return Error::success();
1563 #undef GET_OR_DISTINCT
1564 }
1565
1566 Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
1567     ArrayRef<uint64_t> Record, StringRef Blob,
1568     std::function<void(StringRef)> CallBack) {
1569   // All the MDStrings in the block are emitted together in a single
1570   // record.  The strings are concatenated and stored in a blob along with
1571   // their sizes.
1572   if (Record.size() != 2)
1573     return error("Invalid record: metadata strings layout");
1574
1575   unsigned NumStrings = Record[0];
1576   unsigned StringsOffset = Record[1];
1577   if (!NumStrings)
1578     return error("Invalid record: metadata strings with no strings");
1579   if (StringsOffset > Blob.size())
1580     return error("Invalid record: metadata strings corrupt offset");
1581
1582   StringRef Lengths = Blob.slice(0, StringsOffset);
1583   SimpleBitstreamCursor R(Lengths);
1584
1585   StringRef Strings = Blob.drop_front(StringsOffset);
1586   do {
1587     if (R.AtEndOfStream())
1588       return error("Invalid record: metadata strings bad length");
1589
1590     unsigned Size = R.ReadVBR(6);
1591     if (Strings.size() < Size)
1592       return error("Invalid record: metadata strings truncated chars");
1593
1594     CallBack(Strings.slice(0, Size));
1595     Strings = Strings.drop_front(Size);
1596   } while (--NumStrings);
1597
1598   return Error::success();
1599 }
1600
1601 Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment(
1602     GlobalObject &GO, ArrayRef<uint64_t> Record) {
1603   assert(Record.size() % 2 == 0);
1604   for (unsigned I = 0, E = Record.size(); I != E; I += 2) {
1605     auto K = MDKindMap.find(Record[I]);
1606     if (K == MDKindMap.end())
1607       return error("Invalid ID");
1608     MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[I + 1]);
1609     if (!MD)
1610       return error("Invalid metadata attachment");
1611     GO.addMetadata(K->second, *MD);
1612   }
1613   return Error::success();
1614 }
1615
1616 /// Parse metadata attachments.
1617 Error MetadataLoader::MetadataLoaderImpl::parseMetadataAttachment(
1618     Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
1619   if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
1620     return error("Invalid record");
1621
1622   SmallVector<uint64_t, 64> Record;
1623   PlaceholderQueue Placeholders;
1624
1625   while (true) {
1626     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1627
1628     switch (Entry.Kind) {
1629     case BitstreamEntry::SubBlock: // Handled for us already.
1630     case BitstreamEntry::Error:
1631       return error("Malformed block");
1632     case BitstreamEntry::EndBlock:
1633       resolveForwardRefsAndPlaceholders(Placeholders);
1634       return Error::success();
1635     case BitstreamEntry::Record:
1636       // The interesting case.
1637       break;
1638     }
1639
1640     // Read a metadata attachment record.
1641     Record.clear();
1642     ++NumMDRecordLoaded;
1643     switch (Stream.readRecord(Entry.ID, Record)) {
1644     default: // Default behavior: ignore.
1645       break;
1646     case bitc::METADATA_ATTACHMENT: {
1647       unsigned RecordLength = Record.size();
1648       if (Record.empty())
1649         return error("Invalid record");
1650       if (RecordLength % 2 == 0) {
1651         // A function attachment.
1652         if (Error Err = parseGlobalObjectAttachment(F, Record))
1653           return Err;
1654         continue;
1655       }
1656
1657       // An instruction attachment.
1658       Instruction *Inst = InstructionList[Record[0]];
1659       for (unsigned i = 1; i != RecordLength; i = i + 2) {
1660         unsigned Kind = Record[i];
1661         DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind);
1662         if (I == MDKindMap.end())
1663           return error("Invalid ID");
1664         if (I->second == LLVMContext::MD_tbaa && StripTBAA)
1665           continue;
1666
1667         auto Idx = Record[i + 1];
1668         if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) &&
1669             !MetadataList.lookup(Idx)) {
1670           // Load the attachment if it is in the lazy-loadable range and hasn't
1671           // been loaded yet.
1672           lazyLoadOneMetadata(Idx, Placeholders);
1673           resolveForwardRefsAndPlaceholders(Placeholders);
1674         }
1675
1676         Metadata *Node = MetadataList.getMetadataFwdRef(Idx);
1677         if (isa<LocalAsMetadata>(Node))
1678           // Drop the attachment.  This used to be legal, but there's no
1679           // upgrade path.
1680           break;
1681         MDNode *MD = dyn_cast_or_null<MDNode>(Node);
1682         if (!MD)
1683           return error("Invalid metadata attachment");
1684
1685         if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop)
1686           MD = upgradeInstructionLoopAttachment(*MD);
1687
1688         if (I->second == LLVMContext::MD_tbaa) {
1689           assert(!MD->isTemporary() && "should load MDs before attachments");
1690           MD = UpgradeTBAANode(*MD);
1691         }
1692         Inst->setMetadata(I->second, MD);
1693       }
1694       break;
1695     }
1696     }
1697   }
1698 }
1699
1700 /// Parse a single METADATA_KIND record, inserting result in MDKindMap.
1701 Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord(
1702     SmallVectorImpl<uint64_t> &Record) {
1703   if (Record.size() < 2)
1704     return error("Invalid record");
1705
1706   unsigned Kind = Record[0];
1707   SmallString<8> Name(Record.begin() + 1, Record.end());
1708
1709   unsigned NewKind = TheModule.getMDKindID(Name.str());
1710   if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
1711     return error("Conflicting METADATA_KIND records");
1712   return Error::success();
1713 }
1714
1715 /// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
1716 Error MetadataLoader::MetadataLoaderImpl::parseMetadataKinds() {
1717   if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
1718     return error("Invalid record");
1719
1720   SmallVector<uint64_t, 64> Record;
1721
1722   // Read all the records.
1723   while (true) {
1724     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1725
1726     switch (Entry.Kind) {
1727     case BitstreamEntry::SubBlock: // Handled for us already.
1728     case BitstreamEntry::Error:
1729       return error("Malformed block");
1730     case BitstreamEntry::EndBlock:
1731       return Error::success();
1732     case BitstreamEntry::Record:
1733       // The interesting case.
1734       break;
1735     }
1736
1737     // Read a record.
1738     Record.clear();
1739     ++NumMDRecordLoaded;
1740     unsigned Code = Stream.readRecord(Entry.ID, Record);
1741     switch (Code) {
1742     default: // Default behavior: ignore.
1743       break;
1744     case bitc::METADATA_KIND: {
1745       if (Error Err = parseMetadataKindRecord(Record))
1746         return Err;
1747       break;
1748     }
1749     }
1750   }
1751 }
1752
1753 MetadataLoader &MetadataLoader::operator=(MetadataLoader &&RHS) {
1754   Pimpl = std::move(RHS.Pimpl);
1755   return *this;
1756 }
1757 MetadataLoader::MetadataLoader(MetadataLoader &&RHS)
1758     : Pimpl(std::move(RHS.Pimpl)) {}
1759
1760 MetadataLoader::~MetadataLoader() = default;
1761 MetadataLoader::MetadataLoader(BitstreamCursor &Stream, Module &TheModule,
1762                                BitcodeReaderValueList &ValueList,
1763                                bool IsImporting,
1764                                std::function<Type *(unsigned)> getTypeByID)
1765     : Pimpl(llvm::make_unique<MetadataLoaderImpl>(Stream, TheModule, ValueList,
1766                                                   getTypeByID, IsImporting)) {}
1767
1768 Error MetadataLoader::parseMetadata(bool ModuleLevel) {
1769   return Pimpl->parseMetadata(ModuleLevel);
1770 }
1771
1772 bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); }
1773
1774 /// Return the given metadata, creating a replaceable forward reference if
1775 /// necessary.
1776 Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) {
1777   return Pimpl->getMetadataFwdRefOrLoad(Idx);
1778 }
1779
1780 MDNode *MetadataLoader::getMDNodeFwdRefOrNull(unsigned Idx) {
1781   return Pimpl->getMDNodeFwdRefOrNull(Idx);
1782 }
1783
1784 DISubprogram *MetadataLoader::lookupSubprogramForFunction(Function *F) {
1785   return Pimpl->lookupSubprogramForFunction(F);
1786 }
1787
1788 Error MetadataLoader::parseMetadataAttachment(
1789     Function &F, const SmallVectorImpl<Instruction *> &InstructionList) {
1790   return Pimpl->parseMetadataAttachment(F, InstructionList);
1791 }
1792
1793 Error MetadataLoader::parseMetadataKinds() {
1794   return Pimpl->parseMetadataKinds();
1795 }
1796
1797 void MetadataLoader::setStripTBAA(bool StripTBAA) {
1798   return Pimpl->setStripTBAA(StripTBAA);
1799 }
1800
1801 bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); }
1802
1803 unsigned MetadataLoader::size() const { return Pimpl->size(); }
1804 void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); }