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