LLVM 18.0.0git
MetadataLoader.cpp
Go to the documentation of this file.
1//===- MetadataLoader.cpp - Internal BitcodeReader implementation ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "MetadataLoader.h"
10#include "ValueList.h"
11
12#include "llvm/ADT/APInt.h"
13#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/SetVector.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Twine.h"
30#include "llvm/IR/AutoUpgrade.h"
31#include "llvm/IR/BasicBlock.h"
32#include "llvm/IR/Constants.h"
34#include "llvm/IR/Function.h"
37#include "llvm/IR/Instruction.h"
39#include "llvm/IR/LLVMContext.h"
40#include "llvm/IR/Metadata.h"
41#include "llvm/IR/Module.h"
43#include "llvm/IR/Type.h"
49
50#include <algorithm>
51#include <cassert>
52#include <cstddef>
53#include <cstdint>
54#include <deque>
55#include <iterator>
56#include <limits>
57#include <map>
58#include <optional>
59#include <string>
60#include <tuple>
61#include <type_traits>
62#include <utility>
63#include <vector>
64namespace llvm {
65class Argument;
66}
67
68using namespace llvm;
69
70#define DEBUG_TYPE "bitcode-reader"
71
72STATISTIC(NumMDStringLoaded, "Number of MDStrings loaded");
73STATISTIC(NumMDNodeTemporary, "Number of MDNode::Temporary created");
74STATISTIC(NumMDRecordLoaded, "Number of Metadata records loaded");
75
76/// Flag whether we need to import full type definitions for ThinLTO.
77/// Currently needed for Darwin and LLDB.
79 "import-full-type-definitions", cl::init(false), cl::Hidden,
80 cl::desc("Import full type definitions for ThinLTO."));
81
83 "disable-ondemand-mds-loading", cl::init(false), cl::Hidden,
84 cl::desc("Force disable the lazy-loading on-demand of metadata when "
85 "loading bitcode for importing."));
86
87namespace {
88
89static int64_t unrotateSign(uint64_t U) { return (U & 1) ? ~(U >> 1) : U >> 1; }
90
91class BitcodeReaderMetadataList {
92 /// Array of metadata references.
93 ///
94 /// Don't use std::vector here. Some versions of libc++ copy (instead of
95 /// move) on resize, and TrackingMDRef is very expensive to copy.
97
98 /// The set of indices in MetadataPtrs above of forward references that were
99 /// generated.
101
102 /// The set of indices in MetadataPtrs above of Metadata that need to be
103 /// resolved.
104 SmallDenseSet<unsigned, 1> UnresolvedNodes;
105
106 /// Structures for resolving old type refs.
107 struct {
112 } OldTypeRefs;
113
115
116 /// Maximum number of valid references. Forward references exceeding the
117 /// maximum must be invalid.
118 unsigned RefsUpperBound;
119
120public:
121 BitcodeReaderMetadataList(LLVMContext &C, size_t RefsUpperBound)
122 : Context(C),
123 RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(),
124 RefsUpperBound)) {}
125
126 // vector compatibility methods
127 unsigned size() const { return MetadataPtrs.size(); }
128 void resize(unsigned N) { MetadataPtrs.resize(N); }
129 void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); }
130 void clear() { MetadataPtrs.clear(); }
131 Metadata *back() const { return MetadataPtrs.back(); }
132 void pop_back() { MetadataPtrs.pop_back(); }
133 bool empty() const { return MetadataPtrs.empty(); }
134
135 Metadata *operator[](unsigned i) const {
136 assert(i < MetadataPtrs.size());
137 return MetadataPtrs[i];
138 }
139
140 Metadata *lookup(unsigned I) const {
141 if (I < MetadataPtrs.size())
142 return MetadataPtrs[I];
143 return nullptr;
144 }
145
146 void shrinkTo(unsigned N) {
147 assert(N <= size() && "Invalid shrinkTo request!");
148 assert(ForwardReference.empty() && "Unexpected forward refs");
149 assert(UnresolvedNodes.empty() && "Unexpected unresolved node");
150 MetadataPtrs.resize(N);
151 }
152
153 /// Return the given metadata, creating a replaceable forward reference if
154 /// necessary.
155 Metadata *getMetadataFwdRef(unsigned Idx);
156
157 /// Return the given metadata only if it is fully resolved.
158 ///
159 /// Gives the same result as \a lookup(), unless \a MDNode::isResolved()
160 /// would give \c false.
161 Metadata *getMetadataIfResolved(unsigned Idx);
162
163 MDNode *getMDNodeFwdRefOrNull(unsigned Idx);
164 void assignValue(Metadata *MD, unsigned Idx);
165 void tryToResolveCycles();
166 bool hasFwdRefs() const { return !ForwardReference.empty(); }
167 int getNextFwdRef() {
168 assert(hasFwdRefs());
169 return *ForwardReference.begin();
170 }
171
172 /// Upgrade a type that had an MDString reference.
173 void addTypeRef(MDString &UUID, DICompositeType &CT);
174
175 /// Upgrade a type that had an MDString reference.
176 Metadata *upgradeTypeRef(Metadata *MaybeUUID);
177
178 /// Upgrade a type ref array that may have MDString references.
179 Metadata *upgradeTypeRefArray(Metadata *MaybeTuple);
180
181private:
182 Metadata *resolveTypeRefArray(Metadata *MaybeTuple);
183};
184
185void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) {
186 if (auto *MDN = dyn_cast<MDNode>(MD))
187 if (!MDN->isResolved())
188 UnresolvedNodes.insert(Idx);
189
190 if (Idx == size()) {
191 push_back(MD);
192 return;
193 }
194
195 if (Idx >= size())
196 resize(Idx + 1);
197
198 TrackingMDRef &OldMD = MetadataPtrs[Idx];
199 if (!OldMD) {
200 OldMD.reset(MD);
201 return;
202 }
203
204 // If there was a forward reference to this value, replace it.
205 TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
206 PrevMD->replaceAllUsesWith(MD);
207 ForwardReference.erase(Idx);
208}
209
210Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) {
211 // Bail out for a clearly invalid value.
212 if (Idx >= RefsUpperBound)
213 return nullptr;
214
215 if (Idx >= size())
216 resize(Idx + 1);
217
218 if (Metadata *MD = MetadataPtrs[Idx])
219 return MD;
220
221 // Track forward refs to be resolved later.
222 ForwardReference.insert(Idx);
223
224 // Create and return a placeholder, which will later be RAUW'd.
225 ++NumMDNodeTemporary;
226 Metadata *MD = MDNode::getTemporary(Context, std::nullopt).release();
227 MetadataPtrs[Idx].reset(MD);
228 return MD;
229}
230
231Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) {
232 Metadata *MD = lookup(Idx);
233 if (auto *N = dyn_cast_or_null<MDNode>(MD))
234 if (!N->isResolved())
235 return nullptr;
236 return MD;
237}
238
239MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) {
240 return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx));
241}
242
243void BitcodeReaderMetadataList::tryToResolveCycles() {
244 if (!ForwardReference.empty())
245 // Still forward references... can't resolve cycles.
246 return;
247
248 // Give up on finding a full definition for any forward decls that remain.
249 for (const auto &Ref : OldTypeRefs.FwdDecls)
250 OldTypeRefs.Final.insert(Ref);
251 OldTypeRefs.FwdDecls.clear();
252
253 // Upgrade from old type ref arrays. In strange cases, this could add to
254 // OldTypeRefs.Unknown.
255 for (const auto &Array : OldTypeRefs.Arrays)
256 Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get()));
257 OldTypeRefs.Arrays.clear();
258
259 // Replace old string-based type refs with the resolved node, if possible.
260 // If we haven't seen the node, leave it to the verifier to complain about
261 // the invalid string reference.
262 for (const auto &Ref : OldTypeRefs.Unknown) {
263 if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first))
264 Ref.second->replaceAllUsesWith(CT);
265 else
266 Ref.second->replaceAllUsesWith(Ref.first);
267 }
268 OldTypeRefs.Unknown.clear();
269
270 if (UnresolvedNodes.empty())
271 // Nothing to do.
272 return;
273
274 // Resolve any cycles.
275 for (unsigned I : UnresolvedNodes) {
276 auto &MD = MetadataPtrs[I];
277 auto *N = dyn_cast_or_null<MDNode>(MD);
278 if (!N)
279 continue;
280
281 assert(!N->isTemporary() && "Unexpected forward reference");
282 N->resolveCycles();
283 }
284
285 // Make sure we return early again until there's another unresolved ref.
286 UnresolvedNodes.clear();
287}
288
289void BitcodeReaderMetadataList::addTypeRef(MDString &UUID,
290 DICompositeType &CT) {
291 assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID");
292 if (CT.isForwardDecl())
293 OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT));
294 else
295 OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT));
296}
297
298Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) {
299 auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID);
300 if (LLVM_LIKELY(!UUID))
301 return MaybeUUID;
302
303 if (auto *CT = OldTypeRefs.Final.lookup(UUID))
304 return CT;
305
306 auto &Ref = OldTypeRefs.Unknown[UUID];
307 if (!Ref)
308 Ref = MDNode::getTemporary(Context, std::nullopt);
309 return Ref.get();
310}
311
312Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) {
313 auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
314 if (!Tuple || Tuple->isDistinct())
315 return MaybeTuple;
316
317 // Look through the array immediately if possible.
318 if (!Tuple->isTemporary())
319 return resolveTypeRefArray(Tuple);
320
321 // Create and return a placeholder to use for now. Eventually
322 // resolveTypeRefArrays() will be resolve this forward reference.
323 OldTypeRefs.Arrays.emplace_back(
324 std::piecewise_construct, std::forward_as_tuple(Tuple),
325 std::forward_as_tuple(MDTuple::getTemporary(Context, std::nullopt)));
326 return OldTypeRefs.Arrays.back().second.get();
327}
328
329Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) {
330 auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
331 if (!Tuple || Tuple->isDistinct())
332 return MaybeTuple;
333
334 // Look through the DITypeRefArray, upgrading each DIType *.
336 Ops.reserve(Tuple->getNumOperands());
337 for (Metadata *MD : Tuple->operands())
338 Ops.push_back(upgradeTypeRef(MD));
339
340 return MDTuple::get(Context, Ops);
341}
342
343namespace {
344
345class PlaceholderQueue {
346 // Placeholders would thrash around when moved, so store in a std::deque
347 // instead of some sort of vector.
348 std::deque<DistinctMDOperandPlaceholder> PHs;
349
350public:
351 ~PlaceholderQueue() {
352 assert(empty() &&
353 "PlaceholderQueue hasn't been flushed before being destroyed");
354 }
355 bool empty() const { return PHs.empty(); }
356 DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID);
357 void flush(BitcodeReaderMetadataList &MetadataList);
358
359 /// Return the list of temporaries nodes in the queue, these need to be
360 /// loaded before we can flush the queue.
361 void getTemporaries(BitcodeReaderMetadataList &MetadataList,
362 DenseSet<unsigned> &Temporaries) {
363 for (auto &PH : PHs) {
364 auto ID = PH.getID();
365 auto *MD = MetadataList.lookup(ID);
366 if (!MD) {
367 Temporaries.insert(ID);
368 continue;
369 }
370 auto *N = dyn_cast_or_null<MDNode>(MD);
371 if (N && N->isTemporary())
372 Temporaries.insert(ID);
373 }
374 }
375};
376
377} // end anonymous namespace
378
379DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) {
380 PHs.emplace_back(ID);
381 return PHs.back();
382}
383
384void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) {
385 while (!PHs.empty()) {
386 auto *MD = MetadataList.lookup(PHs.front().getID());
387 assert(MD && "Flushing placeholder on unassigned MD");
388#ifndef NDEBUG
389 if (auto *MDN = dyn_cast<MDNode>(MD))
390 assert(MDN->isResolved() &&
391 "Flushing Placeholder while cycles aren't resolved");
392#endif
393 PHs.front().replaceUseWith(MD);
394 PHs.pop_front();
395 }
396}
397
398} // anonymous namespace
399
400static Error error(const Twine &Message) {
401 return make_error<StringError>(
402 Message, make_error_code(BitcodeError::CorruptedBitcode));
403}
404
406 BitcodeReaderMetadataList MetadataList;
407 BitcodeReaderValueList &ValueList;
408 BitstreamCursor &Stream;
410 Module &TheModule;
411 MetadataLoaderCallbacks Callbacks;
412
413 /// Cursor associated with the lazy-loading of Metadata. This is the easy way
414 /// to keep around the right "context" (Abbrev list) to be able to jump in
415 /// the middle of the metadata block and load any record.
416 BitstreamCursor IndexCursor;
417
418 /// Index that keeps track of MDString values.
419 std::vector<StringRef> MDStringRef;
420
421 /// On-demand loading of a single MDString. Requires the index above to be
422 /// populated.
423 MDString *lazyLoadOneMDString(unsigned Idx);
424
425 /// Index that keeps track of where to find a metadata record in the stream.
426 std::vector<uint64_t> GlobalMetadataBitPosIndex;
427
428 /// Cursor position of the start of the global decl attachments, to enable
429 /// loading using the index built for lazy loading, instead of forward
430 /// references.
431 uint64_t GlobalDeclAttachmentPos = 0;
432
433#ifndef NDEBUG
434 /// Baisic correctness check that we end up parsing all of the global decl
435 /// attachments.
436 unsigned NumGlobalDeclAttachSkipped = 0;
437 unsigned NumGlobalDeclAttachParsed = 0;
438#endif
439
440 /// Load the global decl attachments, using the index built for lazy loading.
441 Expected<bool> loadGlobalDeclAttachments();
442
443 /// Populate the index above to enable lazily loading of metadata, and load
444 /// the named metadata as well as the transitively referenced global
445 /// Metadata.
446 Expected<bool> lazyLoadModuleMetadataBlock();
447
448 /// On-demand loading of a single metadata. Requires the index above to be
449 /// populated.
450 void lazyLoadOneMetadata(unsigned Idx, PlaceholderQueue &Placeholders);
451
452 // Keep mapping of seens pair of old-style CU <-> SP, and update pointers to
453 // point from SP to CU after a block is completly parsed.
454 std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms;
455
456 /// Functions that need to be matched with subprograms when upgrading old
457 /// metadata.
459
460 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
462
463 bool StripTBAA = false;
464 bool HasSeenOldLoopTags = false;
465 bool NeedUpgradeToDIGlobalVariableExpression = false;
466 bool NeedDeclareExpressionUpgrade = false;
467
468 /// Map DILocalScope to the enclosing DISubprogram, if any.
470
471 /// True if metadata is being parsed for a module being ThinLTO imported.
472 bool IsImporting = false;
473
474 Error parseOneMetadata(SmallVectorImpl<uint64_t> &Record, unsigned Code,
475 PlaceholderQueue &Placeholders, StringRef Blob,
476 unsigned &NextMetadataNo);
477 Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob,
478 function_ref<void(StringRef)> CallBack);
479 Error parseGlobalObjectAttachment(GlobalObject &GO,
481 Error parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
482
483 void resolveForwardRefsAndPlaceholders(PlaceholderQueue &Placeholders);
484
485 /// Upgrade old-style CU <-> SP pointers to point from SP to CU.
486 void upgradeCUSubprograms() {
487 for (auto CU_SP : CUSubprograms)
488 if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second))
489 for (auto &Op : SPs->operands())
490 if (auto *SP = dyn_cast_or_null<DISubprogram>(Op))
491 SP->replaceUnit(CU_SP.first);
492 CUSubprograms.clear();
493 }
494
495 /// Upgrade old-style bare DIGlobalVariables to DIGlobalVariableExpressions.
496 void upgradeCUVariables() {
497 if (!NeedUpgradeToDIGlobalVariableExpression)
498 return;
499
500 // Upgrade list of variables attached to the CUs.
501 if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu"))
502 for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) {
503 auto *CU = cast<DICompileUnit>(CUNodes->getOperand(I));
504 if (auto *GVs = dyn_cast_or_null<MDTuple>(CU->getRawGlobalVariables()))
505 for (unsigned I = 0; I < GVs->getNumOperands(); I++)
506 if (auto *GV =
507 dyn_cast_or_null<DIGlobalVariable>(GVs->getOperand(I))) {
510 GVs->replaceOperandWith(I, DGVE);
511 }
512 }
513
514 // Upgrade variables attached to globals.
515 for (auto &GV : TheModule.globals()) {
517 GV.getMetadata(LLVMContext::MD_dbg, MDs);
518 GV.eraseMetadata(LLVMContext::MD_dbg);
519 for (auto *MD : MDs)
520 if (auto *DGV = dyn_cast<DIGlobalVariable>(MD)) {
523 GV.addMetadata(LLVMContext::MD_dbg, *DGVE);
524 } else
525 GV.addMetadata(LLVMContext::MD_dbg, *MD);
526 }
527 }
528
529 DISubprogram *findEnclosingSubprogram(DILocalScope *S) {
530 if (!S)
531 return nullptr;
532 if (auto *SP = ParentSubprogram[S]) {
533 return SP;
534 }
535
536 DILocalScope *InitialScope = S;
538 while (S && !isa<DISubprogram>(S)) {
539 S = dyn_cast_or_null<DILocalScope>(S->getScope());
540 if (Visited.contains(S))
541 break;
542 Visited.insert(S);
543 }
544 ParentSubprogram[InitialScope] = llvm::dyn_cast_or_null<DISubprogram>(S);
545
546 return ParentSubprogram[InitialScope];
547 }
548
549 /// Move local imports from DICompileUnit's 'imports' field to
550 /// DISubprogram's retainedNodes.
551 void upgradeCULocals() {
552 if (NamedMDNode *CUNodes = TheModule.getNamedMetadata("llvm.dbg.cu")) {
553 for (unsigned I = 0, E = CUNodes->getNumOperands(); I != E; ++I) {
554 auto *CU = dyn_cast<DICompileUnit>(CUNodes->getOperand(I));
555 if (!CU)
556 continue;
557
558 if (CU->getRawImportedEntities()) {
559 // Collect a set of imported entities to be moved.
560 SetVector<Metadata *> EntitiesToRemove;
561 for (Metadata *Op : CU->getImportedEntities()->operands()) {
562 auto *IE = cast<DIImportedEntity>(Op);
563 if (dyn_cast_or_null<DILocalScope>(IE->getScope())) {
564 EntitiesToRemove.insert(IE);
565 }
566 }
567
568 if (!EntitiesToRemove.empty()) {
569 // Make a new list of CU's 'imports'.
570 SmallVector<Metadata *> NewImports;
571 for (Metadata *Op : CU->getImportedEntities()->operands()) {
572 if (!EntitiesToRemove.contains(cast<DIImportedEntity>(Op))) {
573 NewImports.push_back(Op);
574 }
575 }
576
577 // Find DISubprogram corresponding to each entity.
578 std::map<DISubprogram *, SmallVector<Metadata *>> SPToEntities;
579 for (auto *I : EntitiesToRemove) {
580 auto *Entity = cast<DIImportedEntity>(I);
581 if (auto *SP = findEnclosingSubprogram(
582 cast<DILocalScope>(Entity->getScope()))) {
583 SPToEntities[SP].push_back(Entity);
584 }
585 }
586
587 // Update DISubprograms' retainedNodes.
588 for (auto I = SPToEntities.begin(); I != SPToEntities.end(); ++I) {
589 auto *SP = I->first;
590 auto RetainedNodes = SP->getRetainedNodes();
591 SmallVector<Metadata *> MDs(RetainedNodes.begin(),
592 RetainedNodes.end());
593 MDs.append(I->second);
594 SP->replaceRetainedNodes(MDNode::get(Context, MDs));
595 }
596
597 // Remove entities with local scope from CU.
598 CU->replaceImportedEntities(MDTuple::get(Context, NewImports));
599 }
600 }
601 }
602 }
603
604 ParentSubprogram.clear();
605 }
606
607 /// Remove a leading DW_OP_deref from DIExpressions in a dbg.declare that
608 /// describes a function argument.
609 void upgradeDeclareExpressions(Function &F) {
610 if (!NeedDeclareExpressionUpgrade)
611 return;
612
613 for (auto &BB : F)
614 for (auto &I : BB)
615 if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
616 if (auto *DIExpr = DDI->getExpression())
617 if (DIExpr->startsWithDeref() &&
618 isa_and_nonnull<Argument>(DDI->getAddress())) {
620 Ops.append(std::next(DIExpr->elements_begin()),
621 DIExpr->elements_end());
622 DDI->setExpression(DIExpression::get(Context, Ops));
623 }
624 }
625
626 /// Upgrade the expression from previous versions.
627 Error upgradeDIExpression(uint64_t FromVersion,
630 auto N = Expr.size();
631 switch (FromVersion) {
632 default:
633 return error("Invalid record");
634 case 0:
635 if (N >= 3 && Expr[N - 3] == dwarf::DW_OP_bit_piece)
636 Expr[N - 3] = dwarf::DW_OP_LLVM_fragment;
637 [[fallthrough]];
638 case 1:
639 // Move DW_OP_deref to the end.
640 if (N && Expr[0] == dwarf::DW_OP_deref) {
641 auto End = Expr.end();
642 if (Expr.size() >= 3 &&
643 *std::prev(End, 3) == dwarf::DW_OP_LLVM_fragment)
644 End = std::prev(End, 3);
645 std::move(std::next(Expr.begin()), End, Expr.begin());
646 *std::prev(End) = dwarf::DW_OP_deref;
647 }
648 NeedDeclareExpressionUpgrade = true;
649 [[fallthrough]];
650 case 2: {
651 // Change DW_OP_plus to DW_OP_plus_uconst.
652 // Change DW_OP_minus to DW_OP_uconst, DW_OP_minus
653 auto SubExpr = ArrayRef<uint64_t>(Expr);
654 while (!SubExpr.empty()) {
655 // Skip past other operators with their operands
656 // for this version of the IR, obtained from
657 // from historic DIExpression::ExprOperand::getSize().
658 size_t HistoricSize;
659 switch (SubExpr.front()) {
660 default:
661 HistoricSize = 1;
662 break;
663 case dwarf::DW_OP_constu:
664 case dwarf::DW_OP_minus:
665 case dwarf::DW_OP_plus:
666 HistoricSize = 2;
667 break;
669 HistoricSize = 3;
670 break;
671 }
672
673 // If the expression is malformed, make sure we don't
674 // copy more elements than we should.
675 HistoricSize = std::min(SubExpr.size(), HistoricSize);
676 ArrayRef<uint64_t> Args = SubExpr.slice(1, HistoricSize - 1);
677
678 switch (SubExpr.front()) {
679 case dwarf::DW_OP_plus:
680 Buffer.push_back(dwarf::DW_OP_plus_uconst);
681 Buffer.append(Args.begin(), Args.end());
682 break;
683 case dwarf::DW_OP_minus:
684 Buffer.push_back(dwarf::DW_OP_constu);
685 Buffer.append(Args.begin(), Args.end());
686 Buffer.push_back(dwarf::DW_OP_minus);
687 break;
688 default:
689 Buffer.push_back(*SubExpr.begin());
690 Buffer.append(Args.begin(), Args.end());
691 break;
692 }
693
694 // Continue with remaining elements.
695 SubExpr = SubExpr.slice(HistoricSize);
696 }
697 Expr = MutableArrayRef<uint64_t>(Buffer);
698 [[fallthrough]];
699 }
700 case 3:
701 // Up-to-date!
702 break;
703 }
704
705 return Error::success();
706 }
707
708 void upgradeDebugInfo() {
709 upgradeCUSubprograms();
710 upgradeCUVariables();
711 upgradeCULocals();
712 }
713
714 void callMDTypeCallback(Metadata **Val, unsigned TypeID);
715
716public:
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) {}
724
725 Error parseMetadata(bool ModuleLevel);
726
727 bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); }
728
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);
741 }
742 return MetadataList.getMetadataFwdRef(ID);
743 }
744
746 return FunctionsWithSPs.lookup(F);
747 }
748
749 bool hasSeenOldLoopTags() const { return HasSeenOldLoopTags; }
750
752 ArrayRef<Instruction *> InstructionList);
753
755
756 void setStripTBAA(bool Value) { StripTBAA = Value; }
757 bool isStrippingTBAA() const { return StripTBAA; }
758
759 unsigned size() const { return MetadataList.size(); }
760 void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); }
761 void upgradeDebugIntrinsics(Function &F) { upgradeDeclareExpressions(F); }
762};
763
765MetadataLoader::MetadataLoaderImpl::lazyLoadModuleMetadataBlock() {
766 IndexCursor = Stream;
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);
778
779 switch (Entry.Kind) {
780 case BitstreamEntry::SubBlock: // Handled for us already.
782 return error("Malformed block");
784 return true;
785 }
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) {
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))
802 ;
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);
809 };
810 if (auto Err = parseMetadataStrings(Record, Blob, IndexNextMDString))
811 return std::move(Err);
812 break;
813 }
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))
822 ;
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(
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);
854 }
855 break;
856 }
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();
866
867 unsigned Code;
868 if (Expected<unsigned> MaybeCode =
869 IndexCursor.readRecord(Entry.ID, Record)) {
870 Code = MaybeCode.get();
872 } else
873 return MaybeCode.takeError();
874
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();
881
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();
890
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);
902 }
903 break;
904 }
906 if (!GlobalDeclAttachmentPos)
907 GlobalDeclAttachmentPos = SavedPos;
908#ifndef NDEBUG
909 NumGlobalDeclAttachSkipped++;
910#endif
911 break;
912 }
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;
955 }
956 break;
957 }
958 }
959 }
960}
961
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.
967Expected<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;
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);
986
987 switch (Entry.Kind) {
988 case BitstreamEntry::SubBlock: // Handled for us already.
990 return error("Malformed block");
992 // Check that we parsed them all.
993 assert(NumGlobalDeclAttachSkipped == NumGlobalDeclAttachParsed);
994 return true;
996 break;
997 }
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;
1007 }
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))
1018 ;
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);
1036 }
1037 }
1038}
1039
1040void MetadataLoader::MetadataLoaderImpl::callMDTypeCallback(Metadata **Val,
1041 unsigned TypeID) {
1042 if (Callbacks.MDType) {
1043 (*Callbacks.MDType)(Val, TypeID, Callbacks.GetTypeByID,
1044 Callbacks.GetContainedTypeID);
1045 }
1046}
1047
1048/// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
1049/// module level metadata.
1051 if (!ModuleLevel && MetadataList.hasFwdRefs())
1052 return error("Invalid metadata: fwd refs into function blocks");
1053
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();
1057
1058 if (Error Err = Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
1059 return Err;
1060
1062 PlaceholderQueue Placeholders;
1063
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() &&
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());
1076
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());
1084
1085 // Reading the named metadata created forward references and/or
1086 // placeholders, that we flush here.
1087 resolveForwardRefsAndPlaceholders(Placeholders);
1088 upgradeDebugInfo();
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();
1099 }
1100 return Error::success();
1101 }
1102 // Couldn't load an index, fallback to loading all the block "old-style".
1103 }
1104
1105 unsigned NextMetadataNo = MetadataList.size();
1106
1107 // Read all the records.
1108 while (true) {
1109 BitstreamEntry Entry;
1110 if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry))
1111 return E;
1112
1113 switch (Entry.Kind) {
1114 case BitstreamEntry::SubBlock: // Handled for us already.
1116 return error("Malformed block");
1118 resolveForwardRefsAndPlaceholders(Placeholders);
1119 upgradeDebugInfo();
1120 return Error::success();
1122 // The interesting case.
1123 break;
1124 }
1125
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();
1137 }
1138}
1139
1140MDString *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;
1147}
1148
1149void 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;
1158 }
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())));
1180}
1181
1182/// Ensure that all forward-references and placeholders are resolved.
1183/// Iteratively lazy-loading metadata on-demand if needed.
1184void 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);
1190
1191 // If we don't have any temporary, or FwdReference, we're done!
1192 if (Temporaries.empty() && !MetadataList.hasFwdRefs())
1193 break;
1194
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();
1200
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);
1205 }
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();
1210
1211 // Finally, everything is in place, we can replace the placeholders operands
1212 // with the final node they refer to.
1213 Placeholders.flush(MetadataList);
1214}
1215
1216Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
1217 SmallVectorImpl<uint64_t> &Record, unsigned Code,
1218 PlaceholderQueue &Placeholders, StringRef Blob, unsigned &NextMetadataNo) {
1219
1220 bool IsDistinct = false;
1221 auto getMD = [&](unsigned ID) -> Metadata * {
1222 if (ID < MDStringRef.size())
1223 return lazyLoadOneMDString(ID);
1224 if (!IsDistinct) {
1225 if (auto *MD = MetadataList.lookup(ID))
1226 return MD;
1227 // If lazy-loading is enabled, we try recursively to load the operand
1228 // instead of creating a temporary.
1229 if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) {
1230 // Create a temporary for the node that is referencing the operand we
1231 // will lazy-load. It is needed before recursing in case there are
1232 // uniquing cycles.
1233 MetadataList.getMetadataFwdRef(NextMetadataNo);
1234 lazyLoadOneMetadata(ID, Placeholders);
1235 return MetadataList.lookup(ID);
1236 }
1237 // Return a temporary.
1238 return MetadataList.getMetadataFwdRef(ID);
1239 }
1240 if (auto *MD = MetadataList.getMetadataIfResolved(ID))
1241 return MD;
1242 return &Placeholders.getPlaceholderOp(ID);
1243 };
1244 auto getMDOrNull = [&](unsigned ID) -> Metadata * {
1245 if (ID)
1246 return getMD(ID - 1);
1247 return nullptr;
1248 };
1249 auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * {
1250 if (ID)
1251 return MetadataList.getMetadataFwdRef(ID - 1);
1252 return nullptr;
1253 };
1254 auto getMDString = [&](unsigned ID) -> MDString * {
1255 // This requires that the ID is not really a forward reference. In
1256 // particular, the MDString must already have been resolved.
1257 auto MDS = getMDOrNull(ID);
1258 return cast_or_null<MDString>(MDS);
1259 };
1260
1261 // Support for old type refs.
1262 auto getDITypeRefOrNull = [&](unsigned ID) {
1263 return MetadataList.upgradeTypeRef(getMDOrNull(ID));
1264 };
1265
1266#define GET_OR_DISTINCT(CLASS, ARGS) \
1267 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
1268
1269 switch (Code) {
1270 default: // Default behavior: ignore.
1271 break;
1272 case bitc::METADATA_NAME: {
1273 // Read name of the named metadata.
1274 SmallString<8> Name(Record.begin(), Record.end());
1275 Record.clear();
1276 if (Error E = Stream.ReadCode().moveInto(Code))
1277 return E;
1278
1279 ++NumMDRecordLoaded;
1280 if (Expected<unsigned> MaybeNextBitCode = Stream.readRecord(Code, Record)) {
1281 if (MaybeNextBitCode.get() != bitc::METADATA_NAMED_NODE)
1282 return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
1283 } else
1284 return MaybeNextBitCode.takeError();
1285
1286 // Read named metadata elements.
1287 unsigned Size = Record.size();
1288 NamedMDNode *NMD = TheModule.getOrInsertNamedMetadata(Name);
1289 for (unsigned i = 0; i != Size; ++i) {
1290 MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
1291 if (!MD)
1292 return error("Invalid named metadata: expect fwd ref to MDNode");
1293 NMD->addOperand(MD);
1294 }
1295 break;
1296 }
1298 // Deprecated, but still needed to read old bitcode files.
1299 // This is a LocalAsMetadata record, the only type of function-local
1300 // metadata.
1301 if (Record.size() % 2 == 1)
1302 return error("Invalid record");
1303
1304 // If this isn't a LocalAsMetadata record, we're dropping it. This used
1305 // to be legal, but there's no upgrade path.
1306 auto dropRecord = [&] {
1307 MetadataList.assignValue(MDNode::get(Context, std::nullopt),
1308 NextMetadataNo);
1309 NextMetadataNo++;
1310 };
1311 if (Record.size() != 2) {
1312 dropRecord();
1313 break;
1314 }
1315
1316 unsigned TyID = Record[0];
1317 Type *Ty = Callbacks.GetTypeByID(TyID);
1318 if (Ty->isMetadataTy() || Ty->isVoidTy()) {
1319 dropRecord();
1320 break;
1321 }
1322
1323 Value *V = ValueList.getValueFwdRef(Record[1], Ty, TyID,
1324 /*ConstExprInsertBB*/ nullptr);
1325 if (!V)
1326 return error("Invalid value reference from old fn metadata");
1327
1328 MetadataList.assignValue(LocalAsMetadata::get(V), NextMetadataNo);
1329 NextMetadataNo++;
1330 break;
1331 }
1333 // Deprecated, but still needed to read old bitcode files.
1334 if (Record.size() % 2 == 1)
1335 return error("Invalid record");
1336
1337 unsigned Size = Record.size();
1339 for (unsigned i = 0; i != Size; i += 2) {
1340 unsigned TyID = Record[i];
1341 Type *Ty = Callbacks.GetTypeByID(TyID);
1342 if (!Ty)
1343 return error("Invalid record");
1344 if (Ty->isMetadataTy())
1345 Elts.push_back(getMD(Record[i + 1]));
1346 else if (!Ty->isVoidTy()) {
1347 Value *V = ValueList.getValueFwdRef(Record[i + 1], Ty, TyID,
1348 /*ConstExprInsertBB*/ nullptr);
1349 if (!V)
1350 return error("Invalid value reference from old metadata");
1352 assert(isa<ConstantAsMetadata>(MD) &&
1353 "Expected non-function-local metadata");
1354 callMDTypeCallback(&MD, TyID);
1355 Elts.push_back(MD);
1356 } else
1357 Elts.push_back(nullptr);
1358 }
1359 MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo);
1360 NextMetadataNo++;
1361 break;
1362 }
1363 case bitc::METADATA_VALUE: {
1364 if (Record.size() != 2)
1365 return error("Invalid record");
1366
1367 unsigned TyID = Record[0];
1368 Type *Ty = Callbacks.GetTypeByID(TyID);
1369 if (Ty->isMetadataTy() || Ty->isVoidTy())
1370 return error("Invalid record");
1371
1372 Value *V = ValueList.getValueFwdRef(Record[1], Ty, TyID,
1373 /*ConstExprInsertBB*/ nullptr);
1374 if (!V)
1375 return error("Invalid value reference from metadata");
1376
1378 callMDTypeCallback(&MD, TyID);
1379 MetadataList.assignValue(MD, NextMetadataNo);
1380 NextMetadataNo++;
1381 break;
1382 }
1384 IsDistinct = true;
1385 [[fallthrough]];
1386 case bitc::METADATA_NODE: {
1388 Elts.reserve(Record.size());
1389 for (unsigned ID : Record)
1390 Elts.push_back(getMDOrNull(ID));
1391 MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
1392 : MDNode::get(Context, Elts),
1393 NextMetadataNo);
1394 NextMetadataNo++;
1395 break;
1396 }
1398 if (Record.size() != 5 && Record.size() != 6)
1399 return error("Invalid record");
1400
1401 IsDistinct = Record[0];
1402 unsigned Line = Record[1];
1403 unsigned Column = Record[2];
1404 Metadata *Scope = getMD(Record[3]);
1405 Metadata *InlinedAt = getMDOrNull(Record[4]);
1406 bool ImplicitCode = Record.size() == 6 && Record[5];
1407 MetadataList.assignValue(
1408 GET_OR_DISTINCT(DILocation, (Context, Line, Column, Scope, InlinedAt,
1409 ImplicitCode)),
1410 NextMetadataNo);
1411 NextMetadataNo++;
1412 break;
1413 }
1415 if (Record.size() < 4)
1416 return error("Invalid record");
1417
1418 IsDistinct = Record[0];
1419 unsigned Tag = Record[1];
1420 unsigned Version = Record[2];
1421
1422 if (Tag >= 1u << 16 || Version != 0)
1423 return error("Invalid record");
1424
1425 auto *Header = getMDString(Record[3]);
1427 for (unsigned I = 4, E = Record.size(); I != E; ++I)
1428 DwarfOps.push_back(getMDOrNull(Record[I]));
1429 MetadataList.assignValue(
1430 GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)),
1431 NextMetadataNo);
1432 NextMetadataNo++;
1433 break;
1434 }
1436 Metadata *Val = nullptr;
1437 // Operand 'count' is interpreted as:
1438 // - Signed integer (version 0)
1439 // - Metadata node (version 1)
1440 // Operand 'lowerBound' is interpreted as:
1441 // - Signed integer (version 0 and 1)
1442 // - Metadata node (version 2)
1443 // Operands 'upperBound' and 'stride' are interpreted as:
1444 // - Metadata node (version 2)
1445 switch (Record[0] >> 1) {
1446 case 0:
1448 (Context, Record[1], unrotateSign(Record[2])));
1449 break;
1450 case 1:
1451 Val = GET_OR_DISTINCT(DISubrange, (Context, getMDOrNull(Record[1]),
1452 unrotateSign(Record[2])));
1453 break;
1454 case 2:
1455 Val = GET_OR_DISTINCT(
1456 DISubrange, (Context, getMDOrNull(Record[1]), getMDOrNull(Record[2]),
1457 getMDOrNull(Record[3]), getMDOrNull(Record[4])));
1458 break;
1459 default:
1460 return error("Invalid record: Unsupported version of DISubrange");
1461 }
1462
1463 MetadataList.assignValue(Val, NextMetadataNo);
1464 IsDistinct = Record[0] & 1;
1465 NextMetadataNo++;
1466 break;
1467 }
1469 Metadata *Val = nullptr;
1471 (Context, getMDOrNull(Record[1]),
1472 getMDOrNull(Record[2]), getMDOrNull(Record[3]),
1473 getMDOrNull(Record[4])));
1474
1475 MetadataList.assignValue(Val, NextMetadataNo);
1476 IsDistinct = Record[0] & 1;
1477 NextMetadataNo++;
1478 break;
1479 }
1481 if (Record.size() < 3)
1482 return error("Invalid record");
1483
1484 IsDistinct = Record[0] & 1;
1485 bool IsUnsigned = Record[0] & 2;
1486 bool IsBigInt = Record[0] & 4;
1487 APInt Value;
1488
1489 if (IsBigInt) {
1490 const uint64_t BitWidth = Record[1];
1491 const size_t NumWords = Record.size() - 3;
1492 Value = readWideAPInt(ArrayRef(&Record[3], NumWords), BitWidth);
1493 } else
1494 Value = APInt(64, unrotateSign(Record[1]), !IsUnsigned);
1495
1496 MetadataList.assignValue(
1498 (Context, Value, IsUnsigned, getMDString(Record[2]))),
1499 NextMetadataNo);
1500 NextMetadataNo++;
1501 break;
1502 }
1504 if (Record.size() < 6 || Record.size() > 7)
1505 return error("Invalid record");
1506
1507 IsDistinct = Record[0];
1508 DINode::DIFlags Flags = (Record.size() > 6)
1509 ? static_cast<DINode::DIFlags>(Record[6])
1510 : DINode::FlagZero;
1511
1512 MetadataList.assignValue(
1514 (Context, Record[1], getMDString(Record[2]), Record[3],
1515 Record[4], Record[5], Flags)),
1516 NextMetadataNo);
1517 NextMetadataNo++;
1518 break;
1519 }
1521 if (Record.size() > 9 || Record.size() < 8)
1522 return error("Invalid record");
1523
1524 IsDistinct = Record[0];
1525 bool SizeIs8 = Record.size() == 8;
1526 // StringLocationExp (i.e. Record[5]) is added at a later time
1527 // than the other fields. The code here enables backward compatibility.
1528 Metadata *StringLocationExp = SizeIs8 ? nullptr : getMDOrNull(Record[5]);
1529 unsigned Offset = SizeIs8 ? 5 : 6;
1530 MetadataList.assignValue(
1532 (Context, Record[1], getMDString(Record[2]),
1533 getMDOrNull(Record[3]), getMDOrNull(Record[4]),
1534 StringLocationExp, Record[Offset], Record[Offset + 1],
1535 Record[Offset + 2])),
1536 NextMetadataNo);
1537 NextMetadataNo++;
1538 break;
1539 }
1541 if (Record.size() < 12 || Record.size() > 14)
1542 return error("Invalid record");
1543
1544 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1545 // that there is no DWARF address space associated with DIDerivedType.
1546 std::optional<unsigned> DWARFAddressSpace;
1547 if (Record.size() > 12 && Record[12])
1548 DWARFAddressSpace = Record[12] - 1;
1549
1550 Metadata *Annotations = nullptr;
1551 if (Record.size() > 13 && Record[13])
1552 Annotations = getMDOrNull(Record[13]);
1553
1554 IsDistinct = Record[0];
1555 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1556 MetadataList.assignValue(
1558 (Context, Record[1], getMDString(Record[2]),
1559 getMDOrNull(Record[3]), Record[4],
1560 getDITypeRefOrNull(Record[5]),
1561 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1562 Record[9], DWARFAddressSpace, Flags,
1563 getDITypeRefOrNull(Record[11]), Annotations)),
1564 NextMetadataNo);
1565 NextMetadataNo++;
1566 break;
1567 }
1569 if (Record.size() < 16 || Record.size() > 22)
1570 return error("Invalid record");
1571
1572 // If we have a UUID and this is not a forward declaration, lookup the
1573 // mapping.
1574 IsDistinct = Record[0] & 0x1;
1575 bool IsNotUsedInTypeRef = Record[0] >= 2;
1576 unsigned Tag = Record[1];
1577 MDString *Name = getMDString(Record[2]);
1578 Metadata *File = getMDOrNull(Record[3]);
1579 unsigned Line = Record[4];
1580 Metadata *Scope = getDITypeRefOrNull(Record[5]);
1581 Metadata *BaseType = nullptr;
1582 uint64_t SizeInBits = Record[7];
1583 if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
1584 return error("Alignment value is too large");
1585 uint32_t AlignInBits = Record[8];
1586 uint64_t OffsetInBits = 0;
1587 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[10]);
1588 Metadata *Elements = nullptr;
1589 unsigned RuntimeLang = Record[12];
1590 Metadata *VTableHolder = nullptr;
1591 Metadata *TemplateParams = nullptr;
1592 Metadata *Discriminator = nullptr;
1593 Metadata *DataLocation = nullptr;
1594 Metadata *Associated = nullptr;
1595 Metadata *Allocated = nullptr;
1596 Metadata *Rank = nullptr;
1597 Metadata *Annotations = nullptr;
1598 auto *Identifier = getMDString(Record[15]);
1599 // If this module is being parsed so that it can be ThinLTO imported
1600 // into another module, composite types only need to be imported
1601 // as type declarations (unless full type definitions requested).
1602 // Create type declarations up front to save memory. Also, buildODRType
1603 // handles the case where this is type ODRed with a definition needed
1604 // by the importing module, in which case the existing definition is
1605 // used.
1606 if (IsImporting && !ImportFullTypeDefinitions && Identifier &&
1607 (Tag == dwarf::DW_TAG_enumeration_type ||
1608 Tag == dwarf::DW_TAG_class_type ||
1609 Tag == dwarf::DW_TAG_structure_type ||
1610 Tag == dwarf::DW_TAG_union_type)) {
1611 Flags = Flags | DINode::FlagFwdDecl;
1612 if (Name) {
1613 // This is a hack around preserving template parameters for simplified
1614 // template names - it should probably be replaced with a
1615 // DICompositeType flag specifying whether template parameters are
1616 // required on declarations of this type.
1617 StringRef NameStr = Name->getString();
1618 if (!NameStr.contains('<') || NameStr.startswith("_STN|"))
1619 TemplateParams = getMDOrNull(Record[14]);
1620 }
1621 } else {
1622 BaseType = getDITypeRefOrNull(Record[6]);
1623 OffsetInBits = Record[9];
1624 Elements = getMDOrNull(Record[11]);
1625 VTableHolder = getDITypeRefOrNull(Record[13]);
1626 TemplateParams = getMDOrNull(Record[14]);
1627 if (Record.size() > 16)
1628 Discriminator = getMDOrNull(Record[16]);
1629 if (Record.size() > 17)
1630 DataLocation = getMDOrNull(Record[17]);
1631 if (Record.size() > 19) {
1632 Associated = getMDOrNull(Record[18]);
1633 Allocated = getMDOrNull(Record[19]);
1634 }
1635 if (Record.size() > 20) {
1636 Rank = getMDOrNull(Record[20]);
1637 }
1638 if (Record.size() > 21) {
1639 Annotations = getMDOrNull(Record[21]);
1640 }
1641 }
1642 DICompositeType *CT = nullptr;
1643 if (Identifier)
1645 Context, *Identifier, Tag, Name, File, Line, Scope, BaseType,
1646 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1647 VTableHolder, TemplateParams, Discriminator, DataLocation, Associated,
1648 Allocated, Rank, Annotations);
1649
1650 // Create a node if we didn't get a lazy ODR type.
1651 if (!CT)
1653 (Context, Tag, Name, File, Line, Scope, BaseType,
1654 SizeInBits, AlignInBits, OffsetInBits, Flags,
1655 Elements, RuntimeLang, VTableHolder, TemplateParams,
1656 Identifier, Discriminator, DataLocation, Associated,
1657 Allocated, Rank, Annotations));
1658 if (!IsNotUsedInTypeRef && Identifier)
1659 MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT));
1660
1661 MetadataList.assignValue(CT, NextMetadataNo);
1662 NextMetadataNo++;
1663 break;
1664 }
1666 if (Record.size() < 3 || Record.size() > 4)
1667 return error("Invalid record");
1668 bool IsOldTypeRefArray = Record[0] < 2;
1669 unsigned CC = (Record.size() > 3) ? Record[3] : 0;
1670
1671 IsDistinct = Record[0] & 0x1;
1672 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[1]);
1673 Metadata *Types = getMDOrNull(Record[2]);
1674 if (LLVM_UNLIKELY(IsOldTypeRefArray))
1675 Types = MetadataList.upgradeTypeRefArray(Types);
1676
1677 MetadataList.assignValue(
1678 GET_OR_DISTINCT(DISubroutineType, (Context, Flags, CC, Types)),
1679 NextMetadataNo);
1680 NextMetadataNo++;
1681 break;
1682 }
1683
1684 case bitc::METADATA_MODULE: {
1685 if (Record.size() < 5 || Record.size() > 9)
1686 return error("Invalid record");
1687
1688 unsigned Offset = Record.size() >= 8 ? 2 : 1;
1689 IsDistinct = Record[0];
1690 MetadataList.assignValue(
1692 DIModule,
1693 (Context, Record.size() >= 8 ? getMDOrNull(Record[1]) : nullptr,
1694 getMDOrNull(Record[0 + Offset]), getMDString(Record[1 + Offset]),
1695 getMDString(Record[2 + Offset]), getMDString(Record[3 + Offset]),
1696 getMDString(Record[4 + Offset]),
1697 Record.size() <= 7 ? 0 : Record[7],
1698 Record.size() <= 8 ? false : Record[8])),
1699 NextMetadataNo);
1700 NextMetadataNo++;
1701 break;
1702 }
1703
1704 case bitc::METADATA_FILE: {
1705 if (Record.size() != 3 && Record.size() != 5 && Record.size() != 6)
1706 return error("Invalid record");
1707
1708 IsDistinct = Record[0];
1709 std::optional<DIFile::ChecksumInfo<MDString *>> Checksum;
1710 // The BitcodeWriter writes null bytes into Record[3:4] when the Checksum
1711 // is not present. This matches up with the old internal representation,
1712 // and the old encoding for CSK_None in the ChecksumKind. The new
1713 // representation reserves the value 0 in the ChecksumKind to continue to
1714 // encode None in a backwards-compatible way.
1715 if (Record.size() > 4 && Record[3] && Record[4])
1716 Checksum.emplace(static_cast<DIFile::ChecksumKind>(Record[3]),
1717 getMDString(Record[4]));
1718 MetadataList.assignValue(
1720 (Context, getMDString(Record[1]),
1721 getMDString(Record[2]), Checksum,
1722 Record.size() > 5 ? getMDString(Record[5]) : nullptr)),
1723 NextMetadataNo);
1724 NextMetadataNo++;
1725 break;
1726 }
1728 if (Record.size() < 14 || Record.size() > 22)
1729 return error("Invalid record");
1730
1731 // Ignore Record[0], which indicates whether this compile unit is
1732 // distinct. It's always distinct.
1733 IsDistinct = true;
1735 Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]),
1736 Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]),
1737 Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]),
1738 getMDOrNull(Record[12]), getMDOrNull(Record[13]),
1739 Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
1740 Record.size() <= 14 ? 0 : Record[14],
1741 Record.size() <= 16 ? true : Record[16],
1742 Record.size() <= 17 ? false : Record[17],
1743 Record.size() <= 18 ? 0 : Record[18],
1744 Record.size() <= 19 ? false : Record[19],
1745 Record.size() <= 20 ? nullptr : getMDString(Record[20]),
1746 Record.size() <= 21 ? nullptr : getMDString(Record[21]));
1747
1748 MetadataList.assignValue(CU, NextMetadataNo);
1749 NextMetadataNo++;
1750
1751 // Move the Upgrade the list of subprograms.
1752 if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11]))
1753 CUSubprograms.push_back({CU, SPs});
1754 break;
1755 }
1757 if (Record.size() < 18 || Record.size() > 21)
1758 return error("Invalid record");
1759
1760 bool HasSPFlags = Record[0] & 4;
1761
1764 if (!HasSPFlags)
1765 Flags = static_cast<DINode::DIFlags>(Record[11 + 2]);
1766 else {
1767 Flags = static_cast<DINode::DIFlags>(Record[11]);
1768 SPFlags = static_cast<DISubprogram::DISPFlags>(Record[9]);
1769 }
1770
1771 // Support for old metadata when
1772 // subprogram specific flags are placed in DIFlags.
1773 const unsigned DIFlagMainSubprogram = 1 << 21;
1774 bool HasOldMainSubprogramFlag = Flags & DIFlagMainSubprogram;
1775 if (HasOldMainSubprogramFlag)
1776 // Remove old DIFlagMainSubprogram from DIFlags.
1777 // Note: This assumes that any future use of bit 21 defaults to it
1778 // being 0.
1779 Flags &= ~static_cast<DINode::DIFlags>(DIFlagMainSubprogram);
1780
1781 if (HasOldMainSubprogramFlag && HasSPFlags)
1782 SPFlags |= DISubprogram::SPFlagMainSubprogram;
1783 else if (!HasSPFlags)
1784 SPFlags = DISubprogram::toSPFlags(
1785 /*IsLocalToUnit=*/Record[7], /*IsDefinition=*/Record[8],
1786 /*IsOptimized=*/Record[14], /*Virtuality=*/Record[11],
1787 /*IsMainSubprogram=*/HasOldMainSubprogramFlag);
1788
1789 // All definitions should be distinct.
1790 IsDistinct = (Record[0] & 1) || (SPFlags & DISubprogram::SPFlagDefinition);
1791 // Version 1 has a Function as Record[15].
1792 // Version 2 has removed Record[15].
1793 // Version 3 has the Unit as Record[15].
1794 // Version 4 added thisAdjustment.
1795 // Version 5 repacked flags into DISPFlags, changing many element numbers.
1796 bool HasUnit = Record[0] & 2;
1797 if (!HasSPFlags && HasUnit && Record.size() < 19)
1798 return error("Invalid record");
1799 if (HasSPFlags && !HasUnit)
1800 return error("Invalid record");
1801 // Accommodate older formats.
1802 bool HasFn = false;
1803 bool HasThisAdj = true;
1804 bool HasThrownTypes = true;
1805 bool HasAnnotations = false;
1806 bool HasTargetFuncName = false;
1807 unsigned OffsetA = 0;
1808 unsigned OffsetB = 0;
1809 if (!HasSPFlags) {
1810 OffsetA = 2;
1811 OffsetB = 2;
1812 if (Record.size() >= 19) {
1813 HasFn = !HasUnit;
1814 OffsetB++;
1815 }
1816 HasThisAdj = Record.size() >= 20;
1817 HasThrownTypes = Record.size() >= 21;
1818 } else {
1819 HasAnnotations = Record.size() >= 19;
1820 HasTargetFuncName = Record.size() >= 20;
1821 }
1822 Metadata *CUorFn = getMDOrNull(Record[12 + OffsetB]);
1825 (Context,
1826 getDITypeRefOrNull(Record[1]), // scope
1827 getMDString(Record[2]), // name
1828 getMDString(Record[3]), // linkageName
1829 getMDOrNull(Record[4]), // file
1830 Record[5], // line
1831 getMDOrNull(Record[6]), // type
1832 Record[7 + OffsetA], // scopeLine
1833 getDITypeRefOrNull(Record[8 + OffsetA]), // containingType
1834 Record[10 + OffsetA], // virtualIndex
1835 HasThisAdj ? Record[16 + OffsetB] : 0, // thisAdjustment
1836 Flags, // flags
1837 SPFlags, // SPFlags
1838 HasUnit ? CUorFn : nullptr, // unit
1839 getMDOrNull(Record[13 + OffsetB]), // templateParams
1840 getMDOrNull(Record[14 + OffsetB]), // declaration
1841 getMDOrNull(Record[15 + OffsetB]), // retainedNodes
1842 HasThrownTypes ? getMDOrNull(Record[17 + OffsetB])
1843 : nullptr, // thrownTypes
1844 HasAnnotations ? getMDOrNull(Record[18 + OffsetB])
1845 : nullptr, // annotations
1846 HasTargetFuncName ? getMDString(Record[19 + OffsetB])
1847 : nullptr // targetFuncName
1848 ));
1849 MetadataList.assignValue(SP, NextMetadataNo);
1850 NextMetadataNo++;
1851
1852 // Upgrade sp->function mapping to function->sp mapping.
1853 if (HasFn) {
1854 if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(CUorFn))
1855 if (auto *F = dyn_cast<Function>(CMD->getValue())) {
1856 if (F->isMaterializable())
1857 // Defer until materialized; unmaterialized functions may not have
1858 // metadata.
1859 FunctionsWithSPs[F] = SP;
1860 else if (!F->empty())
1861 F->setSubprogram(SP);
1862 }
1863 }
1864 break;
1865 }
1867 if (Record.size() != 5)
1868 return error("Invalid record");
1869
1870 IsDistinct = Record[0];
1871 MetadataList.assignValue(
1873 (Context, getMDOrNull(Record[1]),
1874 getMDOrNull(Record[2]), Record[3], Record[4])),
1875 NextMetadataNo);
1876 NextMetadataNo++;
1877 break;
1878 }
1880 if (Record.size() != 4)
1881 return error("Invalid record");
1882
1883 IsDistinct = Record[0];
1884 MetadataList.assignValue(
1886 (Context, getMDOrNull(Record[1]),
1887 getMDOrNull(Record[2]), Record[3])),
1888 NextMetadataNo);
1889 NextMetadataNo++;
1890 break;
1891 }
1893 IsDistinct = Record[0] & 1;
1894 MetadataList.assignValue(
1896 (Context, getMDOrNull(Record[1]),
1897 getMDOrNull(Record[2]), getMDString(Record[3]),
1898 getMDOrNull(Record[4]), Record[5])),
1899 NextMetadataNo);
1900 NextMetadataNo++;
1901 break;
1902 }
1904 // Newer versions of DINamespace dropped file and line.
1905 MDString *Name;
1906 if (Record.size() == 3)
1907 Name = getMDString(Record[2]);
1908 else if (Record.size() == 5)
1909 Name = getMDString(Record[3]);
1910 else
1911 return error("Invalid record");
1912
1913 IsDistinct = Record[0] & 1;
1914 bool ExportSymbols = Record[0] & 2;
1915 MetadataList.assignValue(
1917 (Context, getMDOrNull(Record[1]), Name, ExportSymbols)),
1918 NextMetadataNo);
1919 NextMetadataNo++;
1920 break;
1921 }
1922 case bitc::METADATA_MACRO: {
1923 if (Record.size() != 5)
1924 return error("Invalid record");
1925
1926 IsDistinct = Record[0];
1927 MetadataList.assignValue(
1929 (Context, Record[1], Record[2], getMDString(Record[3]),
1930 getMDString(Record[4]))),
1931 NextMetadataNo);
1932 NextMetadataNo++;
1933 break;
1934 }
1936 if (Record.size() != 5)
1937 return error("Invalid record");
1938
1939 IsDistinct = Record[0];
1940 MetadataList.assignValue(
1942 (Context, Record[1], Record[2], getMDOrNull(Record[3]),
1943 getMDOrNull(Record[4]))),
1944 NextMetadataNo);
1945 NextMetadataNo++;
1946 break;
1947 }
1949 if (Record.size() < 3 || Record.size() > 4)
1950 return error("Invalid record");
1951
1952 IsDistinct = Record[0];
1953 MetadataList.assignValue(
1955 (Context, getMDString(Record[1]),
1956 getDITypeRefOrNull(Record[2]),
1957 (Record.size() == 4) ? getMDOrNull(Record[3])
1958 : getMDOrNull(false))),
1959 NextMetadataNo);
1960 NextMetadataNo++;
1961 break;
1962 }
1964 if (Record.size() < 5 || Record.size() > 6)
1965 return error("Invalid record");
1966
1967 IsDistinct = Record[0];
1968
1969 MetadataList.assignValue(
1972 (Context, Record[1], getMDString(Record[2]),
1973 getDITypeRefOrNull(Record[3]),
1974 (Record.size() == 6) ? getMDOrNull(Record[4]) : getMDOrNull(false),
1975 (Record.size() == 6) ? getMDOrNull(Record[5])
1976 : getMDOrNull(Record[4]))),
1977 NextMetadataNo);
1978 NextMetadataNo++;
1979 break;
1980 }
1982 if (Record.size() < 11 || Record.size() > 13)
1983 return error("Invalid record");
1984
1985 IsDistinct = Record[0] & 1;
1986 unsigned Version = Record[0] >> 1;
1987
1988 if (Version == 2) {
1989 Metadata *Annotations = nullptr;
1990 if (Record.size() > 12)
1991 Annotations = getMDOrNull(Record[12]);
1992
1993 MetadataList.assignValue(
1995 (Context, getMDOrNull(Record[1]),
1996 getMDString(Record[2]), getMDString(Record[3]),
1997 getMDOrNull(Record[4]), Record[5],
1998 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
1999 getMDOrNull(Record[9]), getMDOrNull(Record[10]),
2000 Record[11], Annotations)),
2001 NextMetadataNo);
2002
2003 NextMetadataNo++;
2004 } else if (Version == 1) {
2005 // No upgrade necessary. A null field will be introduced to indicate
2006 // that no parameter information is available.
2007 MetadataList.assignValue(
2010 (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
2011 getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
2012 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
2013 getMDOrNull(Record[10]), nullptr, Record[11], nullptr)),
2014 NextMetadataNo);
2015
2016 NextMetadataNo++;
2017 } else if (Version == 0) {
2018 // Upgrade old metadata, which stored a global variable reference or a
2019 // ConstantInt here.
2020 NeedUpgradeToDIGlobalVariableExpression = true;
2021 Metadata *Expr = getMDOrNull(Record[9]);
2022 uint32_t AlignInBits = 0;
2023 if (Record.size() > 11) {
2024 if (Record[11] > (uint64_t)std::numeric_limits<uint32_t>::max())
2025 return error("Alignment value is too large");
2026 AlignInBits = Record[11];
2027 }
2028 GlobalVariable *Attach = nullptr;
2029 if (auto *CMD = dyn_cast_or_null<ConstantAsMetadata>(Expr)) {
2030 if (auto *GV = dyn_cast<GlobalVariable>(CMD->getValue())) {
2031 Attach = GV;
2032 Expr = nullptr;
2033 } else if (auto *CI = dyn_cast<ConstantInt>(CMD->getValue())) {
2034 Expr = DIExpression::get(Context,
2035 {dwarf::DW_OP_constu, CI->getZExtValue(),
2036 dwarf::DW_OP_stack_value});
2037 } else {
2038 Expr = nullptr;
2039 }
2040 }
2043 (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
2044 getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
2045 getDITypeRefOrNull(Record[6]), Record[7], Record[8],
2046 getMDOrNull(Record[10]), nullptr, AlignInBits, nullptr));
2047
2048 DIGlobalVariableExpression *DGVE = nullptr;
2049 if (Attach || Expr)
2051 Context, DGV, Expr ? Expr : DIExpression::get(Context, {}));
2052 if (Attach)
2053 Attach->addDebugInfo(DGVE);
2054
2055 auto *MDNode = Expr ? cast<Metadata>(DGVE) : cast<Metadata>(DGV);
2056 MetadataList.assignValue(MDNode, NextMetadataNo);
2057 NextMetadataNo++;
2058 } else
2059 return error("Invalid record");
2060
2061 break;
2062 }
2064 if (Record.size() != 1)
2065 return error("Invalid DIAssignID record.");
2066
2067 IsDistinct = Record[0] & 1;
2068 if (!IsDistinct)
2069 return error("Invalid DIAssignID record. Must be distinct");
2070
2071 MetadataList.assignValue(DIAssignID::getDistinct(Context), NextMetadataNo);
2072 NextMetadataNo++;
2073 break;
2074 }
2076 // 10th field is for the obseleted 'inlinedAt:' field.
2077 if (Record.size() < 8 || Record.size() > 10)
2078 return error("Invalid record");
2079
2080 IsDistinct = Record[0] & 1;
2081 bool HasAlignment = Record[0] & 2;
2082 // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
2083 // DW_TAG_arg_variable, if we have alignment flag encoded it means, that
2084 // this is newer version of record which doesn't have artificial tag.
2085 bool HasTag = !HasAlignment && Record.size() > 8;
2086 DINode::DIFlags Flags = static_cast<DINode::DIFlags>(Record[7 + HasTag]);
2087 uint32_t AlignInBits = 0;
2088 Metadata *Annotations = nullptr;
2089 if (HasAlignment) {
2090 if (Record[8] > (uint64_t)std::numeric_limits<uint32_t>::max())
2091 return error("Alignment value is too large");
2092 AlignInBits = Record[8];
2093 if (Record.size() > 9)
2094 Annotations = getMDOrNull(Record[9]);
2095 }
2096
2097 MetadataList.assignValue(
2099 (Context, getMDOrNull(Record[1 + HasTag]),
2100 getMDString(Record[2 + HasTag]),
2101 getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
2102 getDITypeRefOrNull(Record[5 + HasTag]),
2103 Record[6 + HasTag], Flags, AlignInBits, Annotations)),
2104 NextMetadataNo);
2105 NextMetadataNo++;
2106 break;
2107 }
2108 case bitc::METADATA_LABEL: {
2109 if (Record.size() != 5)
2110 return error("Invalid record");
2111
2112 IsDistinct = Record[0] & 1;
2113 MetadataList.assignValue(
2114 GET_OR_DISTINCT(DILabel, (Context, getMDOrNull(Record[1]),
2115 getMDString(Record[2]),
2116 getMDOrNull(Record[3]), Record[4])),
2117 NextMetadataNo);
2118 NextMetadataNo++;
2119 break;
2120 }
2122 if (Record.size() < 1)
2123 return error("Invalid record");
2124
2125 IsDistinct = Record[0] & 1;
2126 uint64_t Version = Record[0] >> 1;
2127 auto Elts = MutableArrayRef<uint64_t>(Record).slice(1);
2128
2130 if (Error Err = upgradeDIExpression(Version, Elts, Buffer))
2131 return Err;
2132
2133 MetadataList.assignValue(GET_OR_DISTINCT(DIExpression, (Context, Elts)),
2134 NextMetadataNo);
2135 NextMetadataNo++;
2136 break;
2137 }
2139 if (Record.size() != 3)
2140 return error("Invalid record");
2141
2142 IsDistinct = Record[0];
2143 Metadata *Expr = getMDOrNull(Record[2]);
2144 if (!Expr)
2145 Expr = DIExpression::get(Context, {});
2146 MetadataList.assignValue(
2148 (Context, getMDOrNull(Record[1]), Expr)),
2149 NextMetadataNo);
2150 NextMetadataNo++;
2151 break;
2152 }
2154 if (Record.size() != 8)
2155 return error("Invalid record");
2156
2157 IsDistinct = Record[0];
2158 MetadataList.assignValue(
2160 (Context, getMDString(Record[1]),
2161 getMDOrNull(Record[2]), Record[3],
2162 getMDString(Record[4]), getMDString(Record[5]),
2163 Record[6], getDITypeRefOrNull(Record[7]))),
2164 NextMetadataNo);
2165 NextMetadataNo++;
2166 break;
2167 }
2169 if (Record.size() < 6 || Record.size() > 8)
2170 return error("Invalid DIImportedEntity record");
2171
2172 IsDistinct = Record[0];
2173 bool HasFile = (Record.size() >= 7);
2174 bool HasElements = (Record.size() >= 8);
2175 MetadataList.assignValue(
2177 (Context, Record[1], getMDOrNull(Record[2]),
2178 getDITypeRefOrNull(Record[3]),
2179 HasFile ? getMDOrNull(Record[6]) : nullptr,
2180 HasFile ? Record[4] : 0, getMDString(Record[5]),
2181 HasElements ? getMDOrNull(Record[7]) : nullptr)),
2182 NextMetadataNo);
2183 NextMetadataNo++;
2184 break;
2185 }
2187 std::string String(Record.begin(), Record.end());
2188
2189 // Test for upgrading !llvm.loop.
2190 HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String);
2191 ++NumMDStringLoaded;
2192 Metadata *MD = MDString::get(Context, String);
2193 MetadataList.assignValue(MD, NextMetadataNo);
2194 NextMetadataNo++;
2195 break;
2196 }
2198 auto CreateNextMDString = [&](StringRef Str) {
2199 ++NumMDStringLoaded;
2200 MetadataList.assignValue(MDString::get(Context, Str), NextMetadataNo);
2201 NextMetadataNo++;
2202 };
2203 if (Error Err = parseMetadataStrings(Record, Blob, CreateNextMDString))
2204 return Err;
2205 break;
2206 }
2208 if (Record.size() % 2 == 0)
2209 return error("Invalid record");
2210 unsigned ValueID = Record[0];
2211 if (ValueID >= ValueList.size())
2212 return error("Invalid record");
2213 if (auto *GO = dyn_cast<GlobalObject>(ValueList[ValueID]))
2214 if (Error Err = parseGlobalObjectAttachment(
2215 *GO, ArrayRef<uint64_t>(Record).slice(1)))
2216 return Err;
2217 break;
2218 }
2219 case bitc::METADATA_KIND: {
2220 // Support older bitcode files that had METADATA_KIND records in a
2221 // block with METADATA_BLOCK_ID.
2222 if (Error Err = parseMetadataKindRecord(Record))
2223 return Err;
2224 break;
2225 }
2228 Elts.reserve(Record.size());
2229 for (uint64_t Elt : Record) {
2230 Metadata *MD = getMD(Elt);
2231 if (isa<MDNode>(MD) && cast<MDNode>(MD)->isTemporary())
2232 return error(
2233 "Invalid record: DIArgList should not contain forward refs");
2234 if (!isa<ValueAsMetadata>(MD))
2235 return error("Invalid record");
2236 Elts.push_back(cast<ValueAsMetadata>(MD));
2237 }
2238
2239 MetadataList.assignValue(DIArgList::get(Context, Elts), NextMetadataNo);
2240 NextMetadataNo++;
2241 break;
2242 }
2243 }
2244 return Error::success();
2245#undef GET_OR_DISTINCT
2246}
2247
2248Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
2250 function_ref<void(StringRef)> CallBack) {
2251 // All the MDStrings in the block are emitted together in a single
2252 // record. The strings are concatenated and stored in a blob along with
2253 // their sizes.
2254 if (Record.size() != 2)
2255 return error("Invalid record: metadata strings layout");
2256
2257 unsigned NumStrings = Record[0];
2258 unsigned StringsOffset = Record[1];
2259 if (!NumStrings)
2260 return error("Invalid record: metadata strings with no strings");
2261 if (StringsOffset > Blob.size())
2262 return error("Invalid record: metadata strings corrupt offset");
2263
2264 StringRef Lengths = Blob.slice(0, StringsOffset);
2265 SimpleBitstreamCursor R(Lengths);
2266
2267 StringRef Strings = Blob.drop_front(StringsOffset);
2268 do {
2269 if (R.AtEndOfStream())
2270 return error("Invalid record: metadata strings bad length");
2271
2272 uint32_t Size;
2273 if (Error E = R.ReadVBR(6).moveInto(Size))
2274 return E;
2275 if (Strings.size() < Size)
2276 return error("Invalid record: metadata strings truncated chars");
2277
2278 CallBack(Strings.slice(0, Size));
2279 Strings = Strings.drop_front(Size);
2280 } while (--NumStrings);
2281
2282 return Error::success();
2283}
2284
2285Error MetadataLoader::MetadataLoaderImpl::parseGlobalObjectAttachment(
2287 assert(Record.size() % 2 == 0);
2288 for (unsigned I = 0, E = Record.size(); I != E; I += 2) {
2289 auto K = MDKindMap.find(Record[I]);
2290 if (K == MDKindMap.end())
2291 return error("Invalid ID");
2292 MDNode *MD =
2293 dyn_cast_or_null<MDNode>(getMetadataFwdRefOrLoad(Record[I + 1]));
2294 if (!MD)
2295 return error("Invalid metadata attachment: expect fwd ref to MDNode");
2296 GO.addMetadata(K->second, *MD);
2297 }
2298 return Error::success();
2299}
2300
2301/// Parse metadata attachments.
2303 Function &F, ArrayRef<Instruction *> InstructionList) {
2304 if (Error Err = Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
2305 return Err;
2306
2308 PlaceholderQueue Placeholders;
2309
2310 while (true) {
2311 BitstreamEntry Entry;
2312 if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry))
2313 return E;
2314
2315 switch (Entry.Kind) {
2316 case BitstreamEntry::SubBlock: // Handled for us already.
2318 return error("Malformed block");
2320 resolveForwardRefsAndPlaceholders(Placeholders);
2321 return Error::success();
2323 // The interesting case.
2324 break;
2325 }
2326
2327 // Read a metadata attachment record.
2328 Record.clear();
2329 ++NumMDRecordLoaded;
2330 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record);
2331 if (!MaybeRecord)
2332 return MaybeRecord.takeError();
2333 switch (MaybeRecord.get()) {
2334 default: // Default behavior: ignore.
2335 break;
2337 unsigned RecordLength = Record.size();
2338 if (Record.empty())
2339 return error("Invalid record");
2340 if (RecordLength % 2 == 0) {
2341 // A function attachment.
2342 if (Error Err = parseGlobalObjectAttachment(F, Record))
2343 return Err;
2344 continue;
2345 }
2346
2347 // An instruction attachment.
2348 Instruction *Inst = InstructionList[Record[0]];
2349 for (unsigned i = 1; i != RecordLength; i = i + 2) {
2350 unsigned Kind = Record[i];
2351 DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind);
2352 if (I == MDKindMap.end())
2353 return error("Invalid ID");
2354 if (I->second == LLVMContext::MD_tbaa && StripTBAA)
2355 continue;
2356
2357 auto Idx = Record[i + 1];
2358 if (Idx < (MDStringRef.size() + GlobalMetadataBitPosIndex.size()) &&
2359 !MetadataList.lookup(Idx)) {
2360 // Load the attachment if it is in the lazy-loadable range and hasn't
2361 // been loaded yet.
2362 lazyLoadOneMetadata(Idx, Placeholders);
2363 resolveForwardRefsAndPlaceholders(Placeholders);
2364 }
2365
2366 Metadata *Node = MetadataList.getMetadataFwdRef(Idx);
2367 if (isa<LocalAsMetadata>(Node))
2368 // Drop the attachment. This used to be legal, but there's no
2369 // upgrade path.
2370 break;
2371 MDNode *MD = dyn_cast_or_null<MDNode>(Node);
2372 if (!MD)
2373 return error("Invalid metadata attachment");
2374
2375 if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop)
2377
2378 if (I->second == LLVMContext::MD_tbaa) {
2379 assert(!MD->isTemporary() && "should load MDs before attachments");
2380 MD = UpgradeTBAANode(*MD);
2381 }
2382 Inst->setMetadata(I->second, MD);
2383 }
2384 break;
2385 }
2386 }
2387 }
2388}
2389
2390/// Parse a single METADATA_KIND record, inserting result in MDKindMap.
2391Error MetadataLoader::MetadataLoaderImpl::parseMetadataKindRecord(
2393 if (Record.size() < 2)
2394 return error("Invalid record");
2395
2396 unsigned Kind = Record[0];
2397 SmallString<8> Name(Record.begin() + 1, Record.end());
2398
2399 unsigned NewKind = TheModule.getMDKindID(Name.str());
2400 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
2401 return error("Conflicting METADATA_KIND records");
2402 return Error::success();
2403}
2404
2405/// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
2407 if (Error Err = Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
2408 return Err;
2409
2411
2412 // Read all the records.
2413 while (true) {
2414 BitstreamEntry Entry;
2415 if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry))
2416 return E;
2417
2418 switch (Entry.Kind) {
2419 case BitstreamEntry::SubBlock: // Handled for us already.
2421 return error("Malformed block");
2423 return Error::success();
2425 // The interesting case.
2426 break;
2427 }
2428
2429 // Read a record.
2430 Record.clear();
2431 ++NumMDRecordLoaded;
2432 Expected<unsigned> MaybeCode = Stream.readRecord(Entry.ID, Record);
2433 if (!MaybeCode)
2434 return MaybeCode.takeError();
2435 switch (MaybeCode.get()) {
2436 default: // Default behavior: ignore.
2437 break;
2438 case bitc::METADATA_KIND: {
2439 if (Error Err = parseMetadataKindRecord(Record))
2440 return Err;
2441 break;
2442 }
2443 }
2444 }
2445}
2446
2448 Pimpl = std::move(RHS.Pimpl);
2449 return *this;
2450}
2452 : Pimpl(std::move(RHS.Pimpl)) {}
2453
2456 BitcodeReaderValueList &ValueList,
2457 bool IsImporting,
2458 MetadataLoaderCallbacks Callbacks)
2459 : Pimpl(std::make_unique<MetadataLoaderImpl>(
2460 Stream, TheModule, ValueList, std::move(Callbacks), IsImporting)) {}
2461
2462Error MetadataLoader::parseMetadata(bool ModuleLevel) {
2463 return Pimpl->parseMetadata(ModuleLevel);
2464}
2465
2466bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); }
2467
2468/// Return the given metadata, creating a replaceable forward reference if
2469/// necessary.
2471 return Pimpl->getMetadataFwdRefOrLoad(Idx);
2472}
2473
2475 return Pimpl->lookupSubprogramForFunction(F);
2476}
2477
2479 Function &F, ArrayRef<Instruction *> InstructionList) {
2480 return Pimpl->parseMetadataAttachment(F, InstructionList);
2481}
2482
2484 return Pimpl->parseMetadataKinds();
2485}
2486
2487void MetadataLoader::setStripTBAA(bool StripTBAA) {
2488 return Pimpl->setStripTBAA(StripTBAA);
2489}
2490
2491bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); }
2492
2493unsigned MetadataLoader::size() const { return Pimpl->size(); }
2494void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); }
2495
2497 return Pimpl->upgradeDebugIntrinsics(F);
2498}
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:222
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:221
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:150
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:469
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
Definition: InlineInfo.cpp:109
#define GET_OR_DISTINCT(CLASS, ARGS)
Definition: LLParser.cpp:4817
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static cl::opt< bool > DisableLazyLoading("disable-ondemand-mds-loading", cl::init(false), cl::Hidden, cl::desc("Force disable the lazy-loading on-demand of metadata when " "loading bitcode for importing."))
static cl::opt< bool > ImportFullTypeDefinitions("import-full-type-definitions", cl::init(false), cl::Hidden, cl::desc("Import full type definitions for ThinLTO."))
Flag whether we need to import full type definitions for ThinLTO.
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
LLVMContext & Context
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallString class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
#define error(X)
std::pair< llvm::MachO::Target, std::string > UUID
Value * RHS
Metadata * getMetadataFwdRefOrLoad(unsigned ID)
Error parseMetadataAttachment(Function &F, ArrayRef< Instruction * > InstructionList)
Parse metadata attachments.
MetadataLoaderImpl(BitstreamCursor &Stream, Module &TheModule, BitcodeReaderValueList &ValueList, MetadataLoaderCallbacks Callbacks, bool IsImporting)
Error parseMetadataKinds()
Parse the metadata kinds out of the METADATA_KIND_BLOCK.
Error parseMetadata(bool ModuleLevel)
Parse a METADATA_BLOCK.
DISubprogram * lookupSubprogramForFunction(Function *F)
Class for arbitrary precision integers.
Definition: APInt.h:76
Annotations lets you mark points and ranges inside source code, for tests:
Definition: Annotations.h:53
This class represents an incoming formal argument to a Function.
Definition: Argument.h:28
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
Error JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
uint64_t GetCurrentBitNo() const
Return the bit # of the bit we are reading.
Expected< BitstreamEntry > advanceSkippingSubblocks(unsigned Flags=0)
This is a convenience function for clients that don't expect any subblocks.
Expected< unsigned > readRecord(unsigned AbbrevID, SmallVectorImpl< uint64_t > &Vals, StringRef *Blob=nullptr)
Expected< unsigned > skipRecord(unsigned AbbrevID)
Read the current record and discard it, returning the code for the record.
Expected< unsigned > ReadCode()
@ AF_DontPopBlockAtEnd
If this flag is used, the advance() method does not automatically pop the block scope when the end of...
static DIAssignID * getDistinct(LLVMContext &Context)
Basic type, like 'int' or 'float'.
static DICompositeType * buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name, Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator, Metadata *DataLocation, Metadata *Associated, Metadata *Allocated, Metadata *Rank, Metadata *Annotations)
Build a DICompositeType with the given ODR identifier.
MDString * getRawIdentifier() const
Enumeration value.
DWARF expression.
ChecksumKind
Which algorithm (e.g.
A pair of DIGlobalVariable and DIExpression.
An imported module (C++ using directive or similar).
A scope for locals.
Debug location.
Represents a module in the programming language, for example, a Clang module, or a Fortran module.
DIFlags
Debug info flags.
DIScope * getScope() const
String type, Fortran CHARACTER(n)
Subprogram description.
static DISPFlags toSPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized, unsigned Virtuality=SPFlagNonvirtual, bool IsMainSubprogram=false)
DISPFlags
Debug info subprogram flags.
Array subrange.
Type array for a subprogram.
bool isForwardDecl() const
This class represents an Operation in the Expression.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:202
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Placeholder metadata for operands of distinct MDNodes.
Definition: Metadata.h:1559
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
Generic tagged DWARF-like metadata node.
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1425
void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
Definition: Metadata.cpp:1740
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1521
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
static LocalAsMetadata * get(Value *Local)
Definition: Metadata.h:445
Metadata node.
Definition: Metadata.h:950
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1424
bool isTemporary() const
Definition: Metadata.h:1134
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1428
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1416
A single uniqued string.
Definition: Metadata.h:611
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:499
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1373
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a temporary node.
Definition: Metadata.h:1393
Helper class that handles loading Metadatas and keeping them available.
MetadataLoader(BitstreamCursor &Stream, Module &TheModule, BitcodeReaderValueList &ValueList, bool IsImporting, MetadataLoaderCallbacks Callbacks)
Metadata * getMetadataFwdRefOrLoad(unsigned Idx)
Return the given metadata, creating a replaceable forward reference if necessary.
void upgradeDebugIntrinsics(Function &F)
Perform bitcode upgrades on llvm.dbg.* calls.
void shrinkTo(unsigned N)
Error parseMetadataKinds()
Parse a METADATA_KIND block for the current module.
void setStripTBAA(bool StripTBAA=true)
Set the mode to strip TBAA metadata on load.
bool isStrippingTBAA()
Return true if the Loader is stripping TBAA metadata.
Error parseMetadataAttachment(Function &F, ArrayRef< Instruction * > InstructionList)
Parse a METADATA_ATTACHMENT block for a function.
DISubprogram * lookupSubprogramForFunction(Function *F)
Return the DISubprogram metadata for a Function if any, null otherwise.
unsigned size() const
MetadataLoader & operator=(MetadataLoader &&)
Root of the metadata hierarchy.
Definition: Metadata.h:61
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
NamedMDNode * getNamedMetadata(const Twine &Name) const
Return the first NamedMDNode in the module with the specified name.
Definition: Module.cpp:251
iterator_range< global_iterator > globals()
Definition: Module.h:667
NamedMDNode * getOrInsertNamedMetadata(StringRef Name)
Return the named MDNode in the module with the specified name.
Definition: Module.cpp:260
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
iterator end() const
Definition: ArrayRef.h:357
iterator begin() const
Definition: ArrayRef.h:356
MutableArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:376
A tuple of MDNodes.
Definition: Metadata.h:1604
void addOperand(MDNode *M)
Definition: Metadata.cpp:1287
A vector that has set insertion semantics.
Definition: SetVector.h:57
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:93
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
Definition: SetVector.h:254
This represents a position within a bitstream.
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition: DenseSet.h:290
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:941
void reserve(size_type N)
Definition: SmallVector.h:667
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:687
void resize(size_type N)
Definition: SmallVector.h:642
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:613
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:688
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:428
bool startswith(StringRef Prefix) const
Definition: StringRef.h:261
Tracking metadata reference.
Definition: TrackingMDRef.h:25
Metadata * get() const
Definition: TrackingMDRef.h:57
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:140
bool isMetadataTy() const
Return true if this is 'metadata'.
Definition: Type.h:222
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:394
LLVM Value Representation.
Definition: Value.h:74
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:185
An efficient, type-erasing, non-owning reference to a callable.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
const uint64_t Version
Definition: InstrProf.h:1021
@ METADATA_NAMESPACE
Definition: LLVMBitCodes.h:348
@ METADATA_COMMON_BLOCK
Definition: LLVMBitCodes.h:368
@ METADATA_STRING_TYPE
Definition: LLVMBitCodes.h:365
@ METADATA_MACRO_FILE
Definition: LLVMBitCodes.h:358
@ METADATA_TEMPLATE_VALUE
Definition: LLVMBitCodes.h:350
@ METADATA_LEXICAL_BLOCK_FILE
Definition: LLVMBitCodes.h:347
@ METADATA_STRING_OLD
Definition: LLVMBitCodes.h:325
@ METADATA_INDEX_OFFSET
Definition: LLVMBitCodes.h:362
@ METADATA_LEXICAL_BLOCK
Definition: LLVMBitCodes.h:346
@ METADATA_SUBPROGRAM
Definition: LLVMBitCodes.h:345
@ METADATA_SUBROUTINE_TYPE
Definition: LLVMBitCodes.h:343
@ METADATA_GLOBAL_DECL_ATTACHMENT
Definition: LLVMBitCodes.h:360
@ METADATA_LOCAL_VAR
Definition: LLVMBitCodes.h:352
@ METADATA_GLOBAL_VAR
Definition: LLVMBitCodes.h:351
@ METADATA_EXPRESSION
Definition: LLVMBitCodes.h:353
@ METADATA_ATTACHMENT
Definition: LLVMBitCodes.h:335
@ METADATA_OBJC_PROPERTY
Definition: LLVMBitCodes.h:354
@ METADATA_NAMED_NODE
Definition: LLVMBitCodes.h:334
@ METADATA_IMPORTED_ENTITY
Definition: LLVMBitCodes.h:355
@ METADATA_GENERIC_SUBRANGE
Definition: LLVMBitCodes.h:369
@ METADATA_ASSIGN_ID
Definition: LLVMBitCodes.h:371
@ METADATA_COMPILE_UNIT
Definition: LLVMBitCodes.h:344
@ METADATA_COMPOSITE_TYPE
Definition: LLVMBitCodes.h:342
@ METADATA_OLD_FN_NODE
Definition: LLVMBitCodes.h:333
@ METADATA_ENUMERATOR
Definition: LLVMBitCodes.h:338
@ METADATA_DERIVED_TYPE
Definition: LLVMBitCodes.h:341
@ METADATA_TEMPLATE_TYPE
Definition: LLVMBitCodes.h:349
@ METADATA_GLOBAL_VAR_EXPR
Definition: LLVMBitCodes.h:361
@ METADATA_BASIC_TYPE
Definition: LLVMBitCodes.h:339
@ METADATA_DISTINCT_NODE
Definition: LLVMBitCodes.h:329
@ METADATA_GENERIC_DEBUG
Definition: LLVMBitCodes.h:336
@ METADATA_KIND_BLOCK_ID
Definition: LLVMBitCodes.h:57
@ METADATA_ATTACHMENT_ID
Definition: LLVMBitCodes.h:46
@ METADATA_BLOCK_ID
Definition: LLVMBitCodes.h:45
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
@ DW_OP_LLVM_fragment
Only used in LLVM metadata.
Definition: Dwarf.h:141
NodeAddr< CodeNode * > Code
Definition: RDFGraph.h:388
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
@ Offset
Definition: DWP.cpp:440
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1685
std::error_code make_error_code(BitcodeError E)
MDNode * upgradeInstructionLoopAttachment(MDNode &N)
Upgrade the loop attachment metadata node.
bool mayBeOldLoopAttachmentTag(StringRef Name)
Check whether a string looks like an old loop attachment tag.
Definition: AutoUpgrade.h:92
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
@ Ref
The access may reference the value stored in memory.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:184
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1854
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition: Casting.h:565
APInt readWideAPInt(ArrayRef< uint64_t > Vals, unsigned TypeBits)
MDNode * UpgradeTBAANode(MDNode &TBAANode)
If the given TBAA tag uses the scalar TBAA format, create a new node corresponding to the upgrade to ...
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
#define N
When advancing through a bitstream cursor, each advance can discover a few different kinds of entries...