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