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