1 //===- BitcodeReader.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 "llvm/Bitcode/BitcodeReader.h" 10 #include "MetadataLoader.h" 11 #include "ValueList.h" 12 #include "llvm/ADT/APFloat.h" 13 #include "llvm/ADT/APInt.h" 14 #include "llvm/ADT/ArrayRef.h" 15 #include "llvm/ADT/DenseMap.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/ADT/Twine.h" 21 #include "llvm/Bitcode/BitcodeCommon.h" 22 #include "llvm/Bitcode/LLVMBitCodes.h" 23 #include "llvm/Bitstream/BitstreamReader.h" 24 #include "llvm/Config/llvm-config.h" 25 #include "llvm/IR/Argument.h" 26 #include "llvm/IR/AttributeMask.h" 27 #include "llvm/IR/Attributes.h" 28 #include "llvm/IR/AutoUpgrade.h" 29 #include "llvm/IR/BasicBlock.h" 30 #include "llvm/IR/CallingConv.h" 31 #include "llvm/IR/Comdat.h" 32 #include "llvm/IR/Constant.h" 33 #include "llvm/IR/Constants.h" 34 #include "llvm/IR/DataLayout.h" 35 #include "llvm/IR/DebugInfo.h" 36 #include "llvm/IR/DebugInfoMetadata.h" 37 #include "llvm/IR/DebugLoc.h" 38 #include "llvm/IR/DerivedTypes.h" 39 #include "llvm/IR/Function.h" 40 #include "llvm/IR/GVMaterializer.h" 41 #include "llvm/IR/GetElementPtrTypeIterator.h" 42 #include "llvm/IR/GlobalAlias.h" 43 #include "llvm/IR/GlobalIFunc.h" 44 #include "llvm/IR/GlobalObject.h" 45 #include "llvm/IR/GlobalValue.h" 46 #include "llvm/IR/GlobalVariable.h" 47 #include "llvm/IR/InlineAsm.h" 48 #include "llvm/IR/InstIterator.h" 49 #include "llvm/IR/InstrTypes.h" 50 #include "llvm/IR/Instruction.h" 51 #include "llvm/IR/Instructions.h" 52 #include "llvm/IR/Intrinsics.h" 53 #include "llvm/IR/IntrinsicsAArch64.h" 54 #include "llvm/IR/IntrinsicsARM.h" 55 #include "llvm/IR/LLVMContext.h" 56 #include "llvm/IR/Metadata.h" 57 #include "llvm/IR/Module.h" 58 #include "llvm/IR/ModuleSummaryIndex.h" 59 #include "llvm/IR/Operator.h" 60 #include "llvm/IR/Type.h" 61 #include "llvm/IR/Value.h" 62 #include "llvm/IR/Verifier.h" 63 #include "llvm/Support/AtomicOrdering.h" 64 #include "llvm/Support/Casting.h" 65 #include "llvm/Support/CommandLine.h" 66 #include "llvm/Support/Compiler.h" 67 #include "llvm/Support/Debug.h" 68 #include "llvm/Support/Error.h" 69 #include "llvm/Support/ErrorHandling.h" 70 #include "llvm/Support/ErrorOr.h" 71 #include "llvm/Support/MathExtras.h" 72 #include "llvm/Support/MemoryBuffer.h" 73 #include "llvm/Support/ModRef.h" 74 #include "llvm/Support/raw_ostream.h" 75 #include "llvm/TargetParser/Triple.h" 76 #include <algorithm> 77 #include <cassert> 78 #include <cstddef> 79 #include <cstdint> 80 #include <deque> 81 #include <map> 82 #include <memory> 83 #include <optional> 84 #include <set> 85 #include <string> 86 #include <system_error> 87 #include <tuple> 88 #include <utility> 89 #include <vector> 90 91 using namespace llvm; 92 93 static cl::opt<bool> PrintSummaryGUIDs( 94 "print-summary-global-ids", cl::init(false), cl::Hidden, 95 cl::desc( 96 "Print the global id for each value when reading the module summary")); 97 98 static cl::opt<bool> ExpandConstantExprs( 99 "expand-constant-exprs", cl::Hidden, 100 cl::desc( 101 "Expand constant expressions to instructions for testing purposes")); 102 103 namespace { 104 105 enum { 106 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex 107 }; 108 109 } // end anonymous namespace 110 111 static Error error(const Twine &Message) { 112 return make_error<StringError>( 113 Message, make_error_code(BitcodeError::CorruptedBitcode)); 114 } 115 116 static Error hasInvalidBitcodeHeader(BitstreamCursor &Stream) { 117 if (!Stream.canSkipToPos(4)) 118 return createStringError(std::errc::illegal_byte_sequence, 119 "file too small to contain bitcode header"); 120 for (unsigned C : {'B', 'C'}) 121 if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 122 if (Res.get() != C) 123 return createStringError(std::errc::illegal_byte_sequence, 124 "file doesn't start with bitcode header"); 125 } else 126 return Res.takeError(); 127 for (unsigned C : {0x0, 0xC, 0xE, 0xD}) 128 if (Expected<SimpleBitstreamCursor::word_t> Res = Stream.Read(4)) { 129 if (Res.get() != C) 130 return createStringError(std::errc::illegal_byte_sequence, 131 "file doesn't start with bitcode header"); 132 } else 133 return Res.takeError(); 134 return Error::success(); 135 } 136 137 static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) { 138 const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart(); 139 const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize(); 140 141 if (Buffer.getBufferSize() & 3) 142 return error("Invalid bitcode signature"); 143 144 // If we have a wrapper header, parse it and ignore the non-bc file contents. 145 // The magic number is 0x0B17C0DE stored in little endian. 146 if (isBitcodeWrapper(BufPtr, BufEnd)) 147 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) 148 return error("Invalid bitcode wrapper header"); 149 150 BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd)); 151 if (Error Err = hasInvalidBitcodeHeader(Stream)) 152 return std::move(Err); 153 154 return std::move(Stream); 155 } 156 157 /// Convert a string from a record into an std::string, return true on failure. 158 template <typename StrTy> 159 static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, 160 StrTy &Result) { 161 if (Idx > Record.size()) 162 return true; 163 164 Result.append(Record.begin() + Idx, Record.end()); 165 return false; 166 } 167 168 // Strip all the TBAA attachment for the module. 169 static void stripTBAA(Module *M) { 170 for (auto &F : *M) { 171 if (F.isMaterializable()) 172 continue; 173 for (auto &I : instructions(F)) 174 I.setMetadata(LLVMContext::MD_tbaa, nullptr); 175 } 176 } 177 178 /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the 179 /// "epoch" encoded in the bitcode, and return the producer name if any. 180 static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) { 181 if (Error Err = Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID)) 182 return std::move(Err); 183 184 // Read all the records. 185 SmallVector<uint64_t, 64> Record; 186 187 std::string ProducerIdentification; 188 189 while (true) { 190 BitstreamEntry Entry; 191 if (Error E = Stream.advance().moveInto(Entry)) 192 return std::move(E); 193 194 switch (Entry.Kind) { 195 default: 196 case BitstreamEntry::Error: 197 return error("Malformed block"); 198 case BitstreamEntry::EndBlock: 199 return ProducerIdentification; 200 case BitstreamEntry::Record: 201 // The interesting case. 202 break; 203 } 204 205 // Read a record. 206 Record.clear(); 207 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 208 if (!MaybeBitCode) 209 return MaybeBitCode.takeError(); 210 switch (MaybeBitCode.get()) { 211 default: // Default behavior: reject 212 return error("Invalid value"); 213 case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N] 214 convertToString(Record, 0, ProducerIdentification); 215 break; 216 case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#] 217 unsigned epoch = (unsigned)Record[0]; 218 if (epoch != bitc::BITCODE_CURRENT_EPOCH) { 219 return error( 220 Twine("Incompatible epoch: Bitcode '") + Twine(epoch) + 221 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'"); 222 } 223 } 224 } 225 } 226 } 227 228 static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) { 229 // We expect a number of well-defined blocks, though we don't necessarily 230 // need to understand them all. 231 while (true) { 232 if (Stream.AtEndOfStream()) 233 return ""; 234 235 BitstreamEntry Entry; 236 if (Error E = Stream.advance().moveInto(Entry)) 237 return std::move(E); 238 239 switch (Entry.Kind) { 240 case BitstreamEntry::EndBlock: 241 case BitstreamEntry::Error: 242 return error("Malformed block"); 243 244 case BitstreamEntry::SubBlock: 245 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) 246 return readIdentificationBlock(Stream); 247 248 // Ignore other sub-blocks. 249 if (Error Err = Stream.SkipBlock()) 250 return std::move(Err); 251 continue; 252 case BitstreamEntry::Record: 253 if (Error E = Stream.skipRecord(Entry.ID).takeError()) 254 return std::move(E); 255 continue; 256 } 257 } 258 } 259 260 static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) { 261 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 262 return std::move(Err); 263 264 SmallVector<uint64_t, 64> Record; 265 // Read all the records for this module. 266 267 while (true) { 268 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 269 if (!MaybeEntry) 270 return MaybeEntry.takeError(); 271 BitstreamEntry Entry = MaybeEntry.get(); 272 273 switch (Entry.Kind) { 274 case BitstreamEntry::SubBlock: // Handled for us already. 275 case BitstreamEntry::Error: 276 return error("Malformed block"); 277 case BitstreamEntry::EndBlock: 278 return false; 279 case BitstreamEntry::Record: 280 // The interesting case. 281 break; 282 } 283 284 // Read a record. 285 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 286 if (!MaybeRecord) 287 return MaybeRecord.takeError(); 288 switch (MaybeRecord.get()) { 289 default: 290 break; // Default behavior, ignore unknown content. 291 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 292 std::string S; 293 if (convertToString(Record, 0, S)) 294 return error("Invalid section name record"); 295 // Check for the i386 and other (x86_64, ARM) conventions 296 if (S.find("__DATA,__objc_catlist") != std::string::npos || 297 S.find("__OBJC,__category") != std::string::npos) 298 return true; 299 break; 300 } 301 } 302 Record.clear(); 303 } 304 llvm_unreachable("Exit infinite loop"); 305 } 306 307 static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) { 308 // We expect a number of well-defined blocks, though we don't necessarily 309 // need to understand them all. 310 while (true) { 311 BitstreamEntry Entry; 312 if (Error E = Stream.advance().moveInto(Entry)) 313 return std::move(E); 314 315 switch (Entry.Kind) { 316 case BitstreamEntry::Error: 317 return error("Malformed block"); 318 case BitstreamEntry::EndBlock: 319 return false; 320 321 case BitstreamEntry::SubBlock: 322 if (Entry.ID == bitc::MODULE_BLOCK_ID) 323 return hasObjCCategoryInModule(Stream); 324 325 // Ignore other sub-blocks. 326 if (Error Err = Stream.SkipBlock()) 327 return std::move(Err); 328 continue; 329 330 case BitstreamEntry::Record: 331 if (Error E = Stream.skipRecord(Entry.ID).takeError()) 332 return std::move(E); 333 continue; 334 } 335 } 336 } 337 338 static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) { 339 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 340 return std::move(Err); 341 342 SmallVector<uint64_t, 64> Record; 343 344 std::string Triple; 345 346 // Read all the records for this module. 347 while (true) { 348 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 349 if (!MaybeEntry) 350 return MaybeEntry.takeError(); 351 BitstreamEntry Entry = MaybeEntry.get(); 352 353 switch (Entry.Kind) { 354 case BitstreamEntry::SubBlock: // Handled for us already. 355 case BitstreamEntry::Error: 356 return error("Malformed block"); 357 case BitstreamEntry::EndBlock: 358 return Triple; 359 case BitstreamEntry::Record: 360 // The interesting case. 361 break; 362 } 363 364 // Read a record. 365 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 366 if (!MaybeRecord) 367 return MaybeRecord.takeError(); 368 switch (MaybeRecord.get()) { 369 default: break; // Default behavior, ignore unknown content. 370 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 371 std::string S; 372 if (convertToString(Record, 0, S)) 373 return error("Invalid triple record"); 374 Triple = S; 375 break; 376 } 377 } 378 Record.clear(); 379 } 380 llvm_unreachable("Exit infinite loop"); 381 } 382 383 static Expected<std::string> readTriple(BitstreamCursor &Stream) { 384 // We expect a number of well-defined blocks, though we don't necessarily 385 // need to understand them all. 386 while (true) { 387 Expected<BitstreamEntry> MaybeEntry = Stream.advance(); 388 if (!MaybeEntry) 389 return MaybeEntry.takeError(); 390 BitstreamEntry Entry = MaybeEntry.get(); 391 392 switch (Entry.Kind) { 393 case BitstreamEntry::Error: 394 return error("Malformed block"); 395 case BitstreamEntry::EndBlock: 396 return ""; 397 398 case BitstreamEntry::SubBlock: 399 if (Entry.ID == bitc::MODULE_BLOCK_ID) 400 return readModuleTriple(Stream); 401 402 // Ignore other sub-blocks. 403 if (Error Err = Stream.SkipBlock()) 404 return std::move(Err); 405 continue; 406 407 case BitstreamEntry::Record: 408 if (llvm::Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID)) 409 continue; 410 else 411 return Skipped.takeError(); 412 } 413 } 414 } 415 416 namespace { 417 418 class BitcodeReaderBase { 419 protected: 420 BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab) 421 : Stream(std::move(Stream)), Strtab(Strtab) { 422 this->Stream.setBlockInfo(&BlockInfo); 423 } 424 425 BitstreamBlockInfo BlockInfo; 426 BitstreamCursor Stream; 427 StringRef Strtab; 428 429 /// In version 2 of the bitcode we store names of global values and comdats in 430 /// a string table rather than in the VST. 431 bool UseStrtab = false; 432 433 Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record); 434 435 /// If this module uses a string table, pop the reference to the string table 436 /// and return the referenced string and the rest of the record. Otherwise 437 /// just return the record itself. 438 std::pair<StringRef, ArrayRef<uint64_t>> 439 readNameFromStrtab(ArrayRef<uint64_t> Record); 440 441 Error readBlockInfo(); 442 443 // Contains an arbitrary and optional string identifying the bitcode producer 444 std::string ProducerIdentification; 445 446 Error error(const Twine &Message); 447 }; 448 449 } // end anonymous namespace 450 451 Error BitcodeReaderBase::error(const Twine &Message) { 452 std::string FullMsg = Message.str(); 453 if (!ProducerIdentification.empty()) 454 FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + 455 LLVM_VERSION_STRING "')"; 456 return ::error(FullMsg); 457 } 458 459 Expected<unsigned> 460 BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) { 461 if (Record.empty()) 462 return error("Invalid version record"); 463 unsigned ModuleVersion = Record[0]; 464 if (ModuleVersion > 2) 465 return error("Invalid value"); 466 UseStrtab = ModuleVersion >= 2; 467 return ModuleVersion; 468 } 469 470 std::pair<StringRef, ArrayRef<uint64_t>> 471 BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) { 472 if (!UseStrtab) 473 return {"", Record}; 474 // Invalid reference. Let the caller complain about the record being empty. 475 if (Record[0] + Record[1] > Strtab.size()) 476 return {"", {}}; 477 return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)}; 478 } 479 480 namespace { 481 482 /// This represents a constant expression or constant aggregate using a custom 483 /// structure internal to the bitcode reader. Later, this structure will be 484 /// expanded by materializeValue() either into a constant expression/aggregate, 485 /// or into an instruction sequence at the point of use. This allows us to 486 /// upgrade bitcode using constant expressions even if this kind of constant 487 /// expression is no longer supported. 488 class BitcodeConstant final : public Value, 489 TrailingObjects<BitcodeConstant, unsigned> { 490 friend TrailingObjects; 491 492 // Value subclass ID: Pick largest possible value to avoid any clashes. 493 static constexpr uint8_t SubclassID = 255; 494 495 public: 496 // Opcodes used for non-expressions. This includes constant aggregates 497 // (struct, array, vector) that might need expansion, as well as non-leaf 498 // constants that don't need expansion (no_cfi, dso_local, blockaddress), 499 // but still go through BitcodeConstant to avoid different uselist orders 500 // between the two cases. 501 static constexpr uint8_t ConstantStructOpcode = 255; 502 static constexpr uint8_t ConstantArrayOpcode = 254; 503 static constexpr uint8_t ConstantVectorOpcode = 253; 504 static constexpr uint8_t NoCFIOpcode = 252; 505 static constexpr uint8_t DSOLocalEquivalentOpcode = 251; 506 static constexpr uint8_t BlockAddressOpcode = 250; 507 static constexpr uint8_t FirstSpecialOpcode = BlockAddressOpcode; 508 509 // Separate struct to make passing different number of parameters to 510 // BitcodeConstant::create() more convenient. 511 struct ExtraInfo { 512 uint8_t Opcode; 513 uint8_t Flags; 514 unsigned Extra; 515 Type *SrcElemTy; 516 517 ExtraInfo(uint8_t Opcode, uint8_t Flags = 0, unsigned Extra = 0, 518 Type *SrcElemTy = nullptr) 519 : Opcode(Opcode), Flags(Flags), Extra(Extra), SrcElemTy(SrcElemTy) {} 520 }; 521 522 uint8_t Opcode; 523 uint8_t Flags; 524 unsigned NumOperands; 525 unsigned Extra; // GEP inrange index or blockaddress BB id. 526 Type *SrcElemTy; // GEP source element type. 527 528 private: 529 BitcodeConstant(Type *Ty, const ExtraInfo &Info, ArrayRef<unsigned> OpIDs) 530 : Value(Ty, SubclassID), Opcode(Info.Opcode), Flags(Info.Flags), 531 NumOperands(OpIDs.size()), Extra(Info.Extra), 532 SrcElemTy(Info.SrcElemTy) { 533 std::uninitialized_copy(OpIDs.begin(), OpIDs.end(), 534 getTrailingObjects<unsigned>()); 535 } 536 537 BitcodeConstant &operator=(const BitcodeConstant &) = delete; 538 539 public: 540 static BitcodeConstant *create(BumpPtrAllocator &A, Type *Ty, 541 const ExtraInfo &Info, 542 ArrayRef<unsigned> OpIDs) { 543 void *Mem = A.Allocate(totalSizeToAlloc<unsigned>(OpIDs.size()), 544 alignof(BitcodeConstant)); 545 return new (Mem) BitcodeConstant(Ty, Info, OpIDs); 546 } 547 548 static bool classof(const Value *V) { return V->getValueID() == SubclassID; } 549 550 ArrayRef<unsigned> getOperandIDs() const { 551 return ArrayRef(getTrailingObjects<unsigned>(), NumOperands); 552 } 553 554 std::optional<unsigned> getInRangeIndex() const { 555 assert(Opcode == Instruction::GetElementPtr); 556 if (Extra == (unsigned)-1) 557 return std::nullopt; 558 return Extra; 559 } 560 561 const char *getOpcodeName() const { 562 return Instruction::getOpcodeName(Opcode); 563 } 564 }; 565 566 class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { 567 LLVMContext &Context; 568 Module *TheModule = nullptr; 569 // Next offset to start scanning for lazy parsing of function bodies. 570 uint64_t NextUnreadBit = 0; 571 // Last function offset found in the VST. 572 uint64_t LastFunctionBlockBit = 0; 573 bool SeenValueSymbolTable = false; 574 uint64_t VSTOffset = 0; 575 576 std::vector<std::string> SectionTable; 577 std::vector<std::string> GCTable; 578 579 std::vector<Type *> TypeList; 580 /// Track type IDs of contained types. Order is the same as the contained 581 /// types of a Type*. This is used during upgrades of typed pointer IR in 582 /// opaque pointer mode. 583 DenseMap<unsigned, SmallVector<unsigned, 1>> ContainedTypeIDs; 584 /// In some cases, we need to create a type ID for a type that was not 585 /// explicitly encoded in the bitcode, or we don't know about at the current 586 /// point. For example, a global may explicitly encode the value type ID, but 587 /// not have a type ID for the pointer to value type, for which we create a 588 /// virtual type ID instead. This map stores the new type ID that was created 589 /// for the given pair of Type and contained type ID. 590 DenseMap<std::pair<Type *, unsigned>, unsigned> VirtualTypeIDs; 591 DenseMap<Function *, unsigned> FunctionTypeIDs; 592 /// Allocator for BitcodeConstants. This should come before ValueList, 593 /// because the ValueList might hold ValueHandles to these constants, so 594 /// ValueList must be destroyed before Alloc. 595 BumpPtrAllocator Alloc; 596 BitcodeReaderValueList ValueList; 597 std::optional<MetadataLoader> MDLoader; 598 std::vector<Comdat *> ComdatList; 599 DenseSet<GlobalObject *> ImplicitComdatObjects; 600 SmallVector<Instruction *, 64> InstructionList; 601 602 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits; 603 std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInits; 604 605 struct FunctionOperandInfo { 606 Function *F; 607 unsigned PersonalityFn; 608 unsigned Prefix; 609 unsigned Prologue; 610 }; 611 std::vector<FunctionOperandInfo> FunctionOperands; 612 613 /// The set of attributes by index. Index zero in the file is for null, and 614 /// is thus not represented here. As such all indices are off by one. 615 std::vector<AttributeList> MAttributes; 616 617 /// The set of attribute groups. 618 std::map<unsigned, AttributeList> MAttributeGroups; 619 620 /// While parsing a function body, this is a list of the basic blocks for the 621 /// function. 622 std::vector<BasicBlock*> FunctionBBs; 623 624 // When reading the module header, this list is populated with functions that 625 // have bodies later in the file. 626 std::vector<Function*> FunctionsWithBodies; 627 628 // When intrinsic functions are encountered which require upgrading they are 629 // stored here with their replacement function. 630 using UpdatedIntrinsicMap = DenseMap<Function *, Function *>; 631 UpdatedIntrinsicMap UpgradedIntrinsics; 632 633 // Several operations happen after the module header has been read, but 634 // before function bodies are processed. This keeps track of whether 635 // we've done this yet. 636 bool SeenFirstFunctionBody = false; 637 638 /// When function bodies are initially scanned, this map contains info about 639 /// where to find deferred function body in the stream. 640 DenseMap<Function*, uint64_t> DeferredFunctionInfo; 641 642 /// When Metadata block is initially scanned when parsing the module, we may 643 /// choose to defer parsing of the metadata. This vector contains info about 644 /// which Metadata blocks are deferred. 645 std::vector<uint64_t> DeferredMetadataInfo; 646 647 /// These are basic blocks forward-referenced by block addresses. They are 648 /// inserted lazily into functions when they're loaded. The basic block ID is 649 /// its index into the vector. 650 DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; 651 std::deque<Function *> BasicBlockFwdRefQueue; 652 653 /// These are Functions that contain BlockAddresses which refer a different 654 /// Function. When parsing the different Function, queue Functions that refer 655 /// to the different Function. Those Functions must be materialized in order 656 /// to resolve their BlockAddress constants before the different Function 657 /// gets moved into another Module. 658 std::vector<Function *> BackwardRefFunctions; 659 660 /// Indicates that we are using a new encoding for instruction operands where 661 /// most operands in the current FUNCTION_BLOCK are encoded relative to the 662 /// instruction number, for a more compact encoding. Some instruction 663 /// operands are not relative to the instruction ID: basic block numbers, and 664 /// types. Once the old style function blocks have been phased out, we would 665 /// not need this flag. 666 bool UseRelativeIDs = false; 667 668 /// True if all functions will be materialized, negating the need to process 669 /// (e.g.) blockaddress forward references. 670 bool WillMaterializeAllForwardRefs = false; 671 672 bool StripDebugInfo = false; 673 TBAAVerifier TBAAVerifyHelper; 674 675 std::vector<std::string> BundleTags; 676 SmallVector<SyncScope::ID, 8> SSIDs; 677 678 std::optional<ValueTypeCallbackTy> ValueTypeCallback; 679 680 public: 681 BitcodeReader(BitstreamCursor Stream, StringRef Strtab, 682 StringRef ProducerIdentification, LLVMContext &Context); 683 684 Error materializeForwardReferencedFunctions(); 685 686 Error materialize(GlobalValue *GV) override; 687 Error materializeModule() override; 688 std::vector<StructType *> getIdentifiedStructTypes() const override; 689 690 /// Main interface to parsing a bitcode buffer. 691 /// \returns true if an error occurred. 692 Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, 693 bool IsImporting, ParserCallbacks Callbacks = {}); 694 695 static uint64_t decodeSignRotatedValue(uint64_t V); 696 697 /// Materialize any deferred Metadata block. 698 Error materializeMetadata() override; 699 700 void setStripDebugInfo() override; 701 702 private: 703 std::vector<StructType *> IdentifiedStructTypes; 704 StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); 705 StructType *createIdentifiedStructType(LLVMContext &Context); 706 707 static constexpr unsigned InvalidTypeID = ~0u; 708 709 Type *getTypeByID(unsigned ID); 710 Type *getPtrElementTypeByID(unsigned ID); 711 unsigned getContainedTypeID(unsigned ID, unsigned Idx = 0); 712 unsigned getVirtualTypeID(Type *Ty, ArrayRef<unsigned> ContainedTypeIDs = {}); 713 714 void callValueTypeCallback(Value *F, unsigned TypeID); 715 Expected<Value *> materializeValue(unsigned ValID, BasicBlock *InsertBB); 716 Expected<Constant *> getValueForInitializer(unsigned ID); 717 718 Value *getFnValueByID(unsigned ID, Type *Ty, unsigned TyID, 719 BasicBlock *ConstExprInsertBB) { 720 if (Ty && Ty->isMetadataTy()) 721 return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); 722 return ValueList.getValueFwdRef(ID, Ty, TyID, ConstExprInsertBB); 723 } 724 725 Metadata *getFnMetadataByID(unsigned ID) { 726 return MDLoader->getMetadataFwdRefOrLoad(ID); 727 } 728 729 BasicBlock *getBasicBlock(unsigned ID) const { 730 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID 731 return FunctionBBs[ID]; 732 } 733 734 AttributeList getAttributes(unsigned i) const { 735 if (i-1 < MAttributes.size()) 736 return MAttributes[i-1]; 737 return AttributeList(); 738 } 739 740 /// Read a value/type pair out of the specified record from slot 'Slot'. 741 /// Increment Slot past the number of slots used in the record. Return true on 742 /// failure. 743 bool getValueTypePair(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 744 unsigned InstNum, Value *&ResVal, unsigned &TypeID, 745 BasicBlock *ConstExprInsertBB) { 746 if (Slot == Record.size()) return true; 747 unsigned ValNo = (unsigned)Record[Slot++]; 748 // Adjust the ValNo, if it was encoded relative to the InstNum. 749 if (UseRelativeIDs) 750 ValNo = InstNum - ValNo; 751 if (ValNo < InstNum) { 752 // If this is not a forward reference, just return the value we already 753 // have. 754 TypeID = ValueList.getTypeID(ValNo); 755 ResVal = getFnValueByID(ValNo, nullptr, TypeID, ConstExprInsertBB); 756 assert((!ResVal || ResVal->getType() == getTypeByID(TypeID)) && 757 "Incorrect type ID stored for value"); 758 return ResVal == nullptr; 759 } 760 if (Slot == Record.size()) 761 return true; 762 763 TypeID = (unsigned)Record[Slot++]; 764 ResVal = getFnValueByID(ValNo, getTypeByID(TypeID), TypeID, 765 ConstExprInsertBB); 766 return ResVal == nullptr; 767 } 768 769 /// Read a value out of the specified record from slot 'Slot'. Increment Slot 770 /// past the number of slots used by the value in the record. Return true if 771 /// there is an error. 772 bool popValue(const SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 773 unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal, 774 BasicBlock *ConstExprInsertBB) { 775 if (getValue(Record, Slot, InstNum, Ty, TyID, ResVal, ConstExprInsertBB)) 776 return true; 777 // All values currently take a single record slot. 778 ++Slot; 779 return false; 780 } 781 782 /// Like popValue, but does not increment the Slot number. 783 bool getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, 784 unsigned InstNum, Type *Ty, unsigned TyID, Value *&ResVal, 785 BasicBlock *ConstExprInsertBB) { 786 ResVal = getValue(Record, Slot, InstNum, Ty, TyID, ConstExprInsertBB); 787 return ResVal == nullptr; 788 } 789 790 /// Version of getValue that returns ResVal directly, or 0 if there is an 791 /// error. 792 Value *getValue(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, 793 unsigned InstNum, Type *Ty, unsigned TyID, 794 BasicBlock *ConstExprInsertBB) { 795 if (Slot == Record.size()) return nullptr; 796 unsigned ValNo = (unsigned)Record[Slot]; 797 // Adjust the ValNo, if it was encoded relative to the InstNum. 798 if (UseRelativeIDs) 799 ValNo = InstNum - ValNo; 800 return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB); 801 } 802 803 /// Like getValue, but decodes signed VBRs. 804 Value *getValueSigned(const SmallVectorImpl<uint64_t> &Record, unsigned Slot, 805 unsigned InstNum, Type *Ty, unsigned TyID, 806 BasicBlock *ConstExprInsertBB) { 807 if (Slot == Record.size()) return nullptr; 808 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); 809 // Adjust the ValNo, if it was encoded relative to the InstNum. 810 if (UseRelativeIDs) 811 ValNo = InstNum - ValNo; 812 return getFnValueByID(ValNo, Ty, TyID, ConstExprInsertBB); 813 } 814 815 Expected<ConstantRange> readConstantRange(ArrayRef<uint64_t> Record, 816 unsigned &OpNum) { 817 if (Record.size() - OpNum < 3) 818 return error("Too few records for range"); 819 unsigned BitWidth = Record[OpNum++]; 820 if (BitWidth > 64) { 821 unsigned LowerActiveWords = Record[OpNum]; 822 unsigned UpperActiveWords = Record[OpNum++] >> 32; 823 if (Record.size() - OpNum < LowerActiveWords + UpperActiveWords) 824 return error("Too few records for range"); 825 APInt Lower = 826 readWideAPInt(ArrayRef(&Record[OpNum], LowerActiveWords), BitWidth); 827 OpNum += LowerActiveWords; 828 APInt Upper = 829 readWideAPInt(ArrayRef(&Record[OpNum], UpperActiveWords), BitWidth); 830 OpNum += UpperActiveWords; 831 return ConstantRange(Lower, Upper); 832 } else { 833 int64_t Start = BitcodeReader::decodeSignRotatedValue(Record[OpNum++]); 834 int64_t End = BitcodeReader::decodeSignRotatedValue(Record[OpNum++]); 835 return ConstantRange(APInt(BitWidth, Start), APInt(BitWidth, End)); 836 } 837 } 838 839 /// Upgrades old-style typeless byval/sret/inalloca attributes by adding the 840 /// corresponding argument's pointee type. Also upgrades intrinsics that now 841 /// require an elementtype attribute. 842 Error propagateAttributeTypes(CallBase *CB, ArrayRef<unsigned> ArgsTys); 843 844 /// Converts alignment exponent (i.e. power of two (or zero)) to the 845 /// corresponding alignment to use. If alignment is too large, returns 846 /// a corresponding error code. 847 Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment); 848 Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); 849 Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false, 850 ParserCallbacks Callbacks = {}); 851 852 Error parseComdatRecord(ArrayRef<uint64_t> Record); 853 Error parseGlobalVarRecord(ArrayRef<uint64_t> Record); 854 Error parseFunctionRecord(ArrayRef<uint64_t> Record); 855 Error parseGlobalIndirectSymbolRecord(unsigned BitCode, 856 ArrayRef<uint64_t> Record); 857 858 Error parseAttributeBlock(); 859 Error parseAttributeGroupBlock(); 860 Error parseTypeTable(); 861 Error parseTypeTableBody(); 862 Error parseOperandBundleTags(); 863 Error parseSyncScopeNames(); 864 865 Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record, 866 unsigned NameIndex, Triple &TT); 867 void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F, 868 ArrayRef<uint64_t> Record); 869 Error parseValueSymbolTable(uint64_t Offset = 0); 870 Error parseGlobalValueSymbolTable(); 871 Error parseConstants(); 872 Error rememberAndSkipFunctionBodies(); 873 Error rememberAndSkipFunctionBody(); 874 /// Save the positions of the Metadata blocks and skip parsing the blocks. 875 Error rememberAndSkipMetadata(); 876 Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType); 877 Error parseFunctionBody(Function *F); 878 Error globalCleanup(); 879 Error resolveGlobalAndIndirectSymbolInits(); 880 Error parseUseLists(); 881 Error findFunctionInStream( 882 Function *F, 883 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); 884 885 SyncScope::ID getDecodedSyncScopeID(unsigned Val); 886 }; 887 888 /// Class to manage reading and parsing function summary index bitcode 889 /// files/sections. 890 class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase { 891 /// The module index built during parsing. 892 ModuleSummaryIndex &TheIndex; 893 894 /// Indicates whether we have encountered a global value summary section 895 /// yet during parsing. 896 bool SeenGlobalValSummary = false; 897 898 /// Indicates whether we have already parsed the VST, used for error checking. 899 bool SeenValueSymbolTable = false; 900 901 /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record. 902 /// Used to enable on-demand parsing of the VST. 903 uint64_t VSTOffset = 0; 904 905 // Map to save ValueId to ValueInfo association that was recorded in the 906 // ValueSymbolTable. It is used after the VST is parsed to convert 907 // call graph edges read from the function summary from referencing 908 // callees by their ValueId to using the ValueInfo instead, which is how 909 // they are recorded in the summary index being built. 910 // We save a GUID which refers to the same global as the ValueInfo, but 911 // ignoring the linkage, i.e. for values other than local linkage they are 912 // identical (this is the second tuple member). 913 // The third tuple member is the real GUID of the ValueInfo. 914 DenseMap<unsigned, 915 std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID>> 916 ValueIdToValueInfoMap; 917 918 /// Map populated during module path string table parsing, from the 919 /// module ID to a string reference owned by the index's module 920 /// path string table, used to correlate with combined index 921 /// summary records. 922 DenseMap<uint64_t, StringRef> ModuleIdMap; 923 924 /// Original source file name recorded in a bitcode record. 925 std::string SourceFileName; 926 927 /// The string identifier given to this module by the client, normally the 928 /// path to the bitcode file. 929 StringRef ModulePath; 930 931 /// Callback to ask whether a symbol is the prevailing copy when invoked 932 /// during combined index building. 933 std::function<bool(GlobalValue::GUID)> IsPrevailing; 934 935 /// Saves the stack ids from the STACK_IDS record to consult when adding stack 936 /// ids from the lists in the callsite and alloc entries to the index. 937 std::vector<uint64_t> StackIds; 938 939 public: 940 ModuleSummaryIndexBitcodeReader( 941 BitstreamCursor Stream, StringRef Strtab, ModuleSummaryIndex &TheIndex, 942 StringRef ModulePath, 943 std::function<bool(GlobalValue::GUID)> IsPrevailing = nullptr); 944 945 Error parseModule(); 946 947 private: 948 void setValueGUID(uint64_t ValueID, StringRef ValueName, 949 GlobalValue::LinkageTypes Linkage, 950 StringRef SourceFileName); 951 Error parseValueSymbolTable( 952 uint64_t Offset, 953 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap); 954 std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); 955 std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, 956 bool IsOldProfileFormat, 957 bool HasProfile, 958 bool HasRelBF); 959 Error parseEntireSummary(unsigned ID); 960 Error parseModuleStringTable(); 961 void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record); 962 void parseTypeIdCompatibleVtableInfo(ArrayRef<uint64_t> Record, size_t &Slot, 963 TypeIdCompatibleVtableInfo &TypeId); 964 std::vector<FunctionSummary::ParamAccess> 965 parseParamAccesses(ArrayRef<uint64_t> Record); 966 967 template <bool AllowNullValueInfo = false> 968 std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID> 969 getValueInfoFromValueId(unsigned ValueId); 970 971 void addThisModule(); 972 ModuleSummaryIndex::ModuleInfo *getThisModule(); 973 }; 974 975 } // end anonymous namespace 976 977 std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, 978 Error Err) { 979 if (Err) { 980 std::error_code EC; 981 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { 982 EC = EIB.convertToErrorCode(); 983 Ctx.emitError(EIB.message()); 984 }); 985 return EC; 986 } 987 return std::error_code(); 988 } 989 990 BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab, 991 StringRef ProducerIdentification, 992 LLVMContext &Context) 993 : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context), 994 ValueList(this->Stream.SizeInBytes(), 995 [this](unsigned ValID, BasicBlock *InsertBB) { 996 return materializeValue(ValID, InsertBB); 997 }) { 998 this->ProducerIdentification = std::string(ProducerIdentification); 999 } 1000 1001 Error BitcodeReader::materializeForwardReferencedFunctions() { 1002 if (WillMaterializeAllForwardRefs) 1003 return Error::success(); 1004 1005 // Prevent recursion. 1006 WillMaterializeAllForwardRefs = true; 1007 1008 while (!BasicBlockFwdRefQueue.empty()) { 1009 Function *F = BasicBlockFwdRefQueue.front(); 1010 BasicBlockFwdRefQueue.pop_front(); 1011 assert(F && "Expected valid function"); 1012 if (!BasicBlockFwdRefs.count(F)) 1013 // Already materialized. 1014 continue; 1015 1016 // Check for a function that isn't materializable to prevent an infinite 1017 // loop. When parsing a blockaddress stored in a global variable, there 1018 // isn't a trivial way to check if a function will have a body without a 1019 // linear search through FunctionsWithBodies, so just check it here. 1020 if (!F->isMaterializable()) 1021 return error("Never resolved function from blockaddress"); 1022 1023 // Try to materialize F. 1024 if (Error Err = materialize(F)) 1025 return Err; 1026 } 1027 assert(BasicBlockFwdRefs.empty() && "Function missing from queue"); 1028 1029 for (Function *F : BackwardRefFunctions) 1030 if (Error Err = materialize(F)) 1031 return Err; 1032 BackwardRefFunctions.clear(); 1033 1034 // Reset state. 1035 WillMaterializeAllForwardRefs = false; 1036 return Error::success(); 1037 } 1038 1039 //===----------------------------------------------------------------------===// 1040 // Helper functions to implement forward reference resolution, etc. 1041 //===----------------------------------------------------------------------===// 1042 1043 static bool hasImplicitComdat(size_t Val) { 1044 switch (Val) { 1045 default: 1046 return false; 1047 case 1: // Old WeakAnyLinkage 1048 case 4: // Old LinkOnceAnyLinkage 1049 case 10: // Old WeakODRLinkage 1050 case 11: // Old LinkOnceODRLinkage 1051 return true; 1052 } 1053 } 1054 1055 static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { 1056 switch (Val) { 1057 default: // Map unknown/new linkages to external 1058 case 0: 1059 return GlobalValue::ExternalLinkage; 1060 case 2: 1061 return GlobalValue::AppendingLinkage; 1062 case 3: 1063 return GlobalValue::InternalLinkage; 1064 case 5: 1065 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage 1066 case 6: 1067 return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage 1068 case 7: 1069 return GlobalValue::ExternalWeakLinkage; 1070 case 8: 1071 return GlobalValue::CommonLinkage; 1072 case 9: 1073 return GlobalValue::PrivateLinkage; 1074 case 12: 1075 return GlobalValue::AvailableExternallyLinkage; 1076 case 13: 1077 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage 1078 case 14: 1079 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage 1080 case 15: 1081 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage 1082 case 1: // Old value with implicit comdat. 1083 case 16: 1084 return GlobalValue::WeakAnyLinkage; 1085 case 10: // Old value with implicit comdat. 1086 case 17: 1087 return GlobalValue::WeakODRLinkage; 1088 case 4: // Old value with implicit comdat. 1089 case 18: 1090 return GlobalValue::LinkOnceAnyLinkage; 1091 case 11: // Old value with implicit comdat. 1092 case 19: 1093 return GlobalValue::LinkOnceODRLinkage; 1094 } 1095 } 1096 1097 static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) { 1098 FunctionSummary::FFlags Flags; 1099 Flags.ReadNone = RawFlags & 0x1; 1100 Flags.ReadOnly = (RawFlags >> 1) & 0x1; 1101 Flags.NoRecurse = (RawFlags >> 2) & 0x1; 1102 Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1; 1103 Flags.NoInline = (RawFlags >> 4) & 0x1; 1104 Flags.AlwaysInline = (RawFlags >> 5) & 0x1; 1105 Flags.NoUnwind = (RawFlags >> 6) & 0x1; 1106 Flags.MayThrow = (RawFlags >> 7) & 0x1; 1107 Flags.HasUnknownCall = (RawFlags >> 8) & 0x1; 1108 Flags.MustBeUnreachable = (RawFlags >> 9) & 0x1; 1109 return Flags; 1110 } 1111 1112 // Decode the flags for GlobalValue in the summary. The bits for each attribute: 1113 // 1114 // linkage: [0,4), notEligibleToImport: 4, live: 5, local: 6, canAutoHide: 7, 1115 // visibility: [8, 10). 1116 static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, 1117 uint64_t Version) { 1118 // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage 1119 // like getDecodedLinkage() above. Any future change to the linkage enum and 1120 // to getDecodedLinkage() will need to be taken into account here as above. 1121 auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits 1122 auto Visibility = GlobalValue::VisibilityTypes((RawFlags >> 8) & 3); // 2 bits 1123 RawFlags = RawFlags >> 4; 1124 bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; 1125 // The Live flag wasn't introduced until version 3. For dead stripping 1126 // to work correctly on earlier versions, we must conservatively treat all 1127 // values as live. 1128 bool Live = (RawFlags & 0x2) || Version < 3; 1129 bool Local = (RawFlags & 0x4); 1130 bool AutoHide = (RawFlags & 0x8); 1131 1132 return GlobalValueSummary::GVFlags(Linkage, Visibility, NotEligibleToImport, 1133 Live, Local, AutoHide); 1134 } 1135 1136 // Decode the flags for GlobalVariable in the summary 1137 static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) { 1138 return GlobalVarSummary::GVarFlags( 1139 (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false, 1140 (RawFlags & 0x4) ? true : false, 1141 (GlobalObject::VCallVisibility)(RawFlags >> 3)); 1142 } 1143 1144 static std::pair<CalleeInfo::HotnessType, bool> 1145 getDecodedHotnessCallEdgeInfo(uint64_t RawFlags) { 1146 CalleeInfo::HotnessType Hotness = 1147 static_cast<CalleeInfo::HotnessType>(RawFlags & 0x7); // 3 bits 1148 bool HasTailCall = (RawFlags & 0x8); // 1 bit 1149 return {Hotness, HasTailCall}; 1150 } 1151 1152 static void getDecodedRelBFCallEdgeInfo(uint64_t RawFlags, uint64_t &RelBF, 1153 bool &HasTailCall) { 1154 static constexpr uint64_t RelBlockFreqMask = 1155 (1 << CalleeInfo::RelBlockFreqBits) - 1; 1156 RelBF = RawFlags & RelBlockFreqMask; // RelBlockFreqBits bits 1157 HasTailCall = (RawFlags & (1 << CalleeInfo::RelBlockFreqBits)); // 1 bit 1158 } 1159 1160 static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { 1161 switch (Val) { 1162 default: // Map unknown visibilities to default. 1163 case 0: return GlobalValue::DefaultVisibility; 1164 case 1: return GlobalValue::HiddenVisibility; 1165 case 2: return GlobalValue::ProtectedVisibility; 1166 } 1167 } 1168 1169 static GlobalValue::DLLStorageClassTypes 1170 getDecodedDLLStorageClass(unsigned Val) { 1171 switch (Val) { 1172 default: // Map unknown values to default. 1173 case 0: return GlobalValue::DefaultStorageClass; 1174 case 1: return GlobalValue::DLLImportStorageClass; 1175 case 2: return GlobalValue::DLLExportStorageClass; 1176 } 1177 } 1178 1179 static bool getDecodedDSOLocal(unsigned Val) { 1180 switch(Val) { 1181 default: // Map unknown values to preemptable. 1182 case 0: return false; 1183 case 1: return true; 1184 } 1185 } 1186 1187 static std::optional<CodeModel::Model> getDecodedCodeModel(unsigned Val) { 1188 switch (Val) { 1189 case 1: 1190 return CodeModel::Tiny; 1191 case 2: 1192 return CodeModel::Small; 1193 case 3: 1194 return CodeModel::Kernel; 1195 case 4: 1196 return CodeModel::Medium; 1197 case 5: 1198 return CodeModel::Large; 1199 } 1200 1201 return {}; 1202 } 1203 1204 static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { 1205 switch (Val) { 1206 case 0: return GlobalVariable::NotThreadLocal; 1207 default: // Map unknown non-zero value to general dynamic. 1208 case 1: return GlobalVariable::GeneralDynamicTLSModel; 1209 case 2: return GlobalVariable::LocalDynamicTLSModel; 1210 case 3: return GlobalVariable::InitialExecTLSModel; 1211 case 4: return GlobalVariable::LocalExecTLSModel; 1212 } 1213 } 1214 1215 static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { 1216 switch (Val) { 1217 default: // Map unknown to UnnamedAddr::None. 1218 case 0: return GlobalVariable::UnnamedAddr::None; 1219 case 1: return GlobalVariable::UnnamedAddr::Global; 1220 case 2: return GlobalVariable::UnnamedAddr::Local; 1221 } 1222 } 1223 1224 static int getDecodedCastOpcode(unsigned Val) { 1225 switch (Val) { 1226 default: return -1; 1227 case bitc::CAST_TRUNC : return Instruction::Trunc; 1228 case bitc::CAST_ZEXT : return Instruction::ZExt; 1229 case bitc::CAST_SEXT : return Instruction::SExt; 1230 case bitc::CAST_FPTOUI : return Instruction::FPToUI; 1231 case bitc::CAST_FPTOSI : return Instruction::FPToSI; 1232 case bitc::CAST_UITOFP : return Instruction::UIToFP; 1233 case bitc::CAST_SITOFP : return Instruction::SIToFP; 1234 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; 1235 case bitc::CAST_FPEXT : return Instruction::FPExt; 1236 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; 1237 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; 1238 case bitc::CAST_BITCAST : return Instruction::BitCast; 1239 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; 1240 } 1241 } 1242 1243 static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) { 1244 bool IsFP = Ty->isFPOrFPVectorTy(); 1245 // UnOps are only valid for int/fp or vector of int/fp types 1246 if (!IsFP && !Ty->isIntOrIntVectorTy()) 1247 return -1; 1248 1249 switch (Val) { 1250 default: 1251 return -1; 1252 case bitc::UNOP_FNEG: 1253 return IsFP ? Instruction::FNeg : -1; 1254 } 1255 } 1256 1257 static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { 1258 bool IsFP = Ty->isFPOrFPVectorTy(); 1259 // BinOps are only valid for int/fp or vector of int/fp types 1260 if (!IsFP && !Ty->isIntOrIntVectorTy()) 1261 return -1; 1262 1263 switch (Val) { 1264 default: 1265 return -1; 1266 case bitc::BINOP_ADD: 1267 return IsFP ? Instruction::FAdd : Instruction::Add; 1268 case bitc::BINOP_SUB: 1269 return IsFP ? Instruction::FSub : Instruction::Sub; 1270 case bitc::BINOP_MUL: 1271 return IsFP ? Instruction::FMul : Instruction::Mul; 1272 case bitc::BINOP_UDIV: 1273 return IsFP ? -1 : Instruction::UDiv; 1274 case bitc::BINOP_SDIV: 1275 return IsFP ? Instruction::FDiv : Instruction::SDiv; 1276 case bitc::BINOP_UREM: 1277 return IsFP ? -1 : Instruction::URem; 1278 case bitc::BINOP_SREM: 1279 return IsFP ? Instruction::FRem : Instruction::SRem; 1280 case bitc::BINOP_SHL: 1281 return IsFP ? -1 : Instruction::Shl; 1282 case bitc::BINOP_LSHR: 1283 return IsFP ? -1 : Instruction::LShr; 1284 case bitc::BINOP_ASHR: 1285 return IsFP ? -1 : Instruction::AShr; 1286 case bitc::BINOP_AND: 1287 return IsFP ? -1 : Instruction::And; 1288 case bitc::BINOP_OR: 1289 return IsFP ? -1 : Instruction::Or; 1290 case bitc::BINOP_XOR: 1291 return IsFP ? -1 : Instruction::Xor; 1292 } 1293 } 1294 1295 static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { 1296 switch (Val) { 1297 default: return AtomicRMWInst::BAD_BINOP; 1298 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; 1299 case bitc::RMW_ADD: return AtomicRMWInst::Add; 1300 case bitc::RMW_SUB: return AtomicRMWInst::Sub; 1301 case bitc::RMW_AND: return AtomicRMWInst::And; 1302 case bitc::RMW_NAND: return AtomicRMWInst::Nand; 1303 case bitc::RMW_OR: return AtomicRMWInst::Or; 1304 case bitc::RMW_XOR: return AtomicRMWInst::Xor; 1305 case bitc::RMW_MAX: return AtomicRMWInst::Max; 1306 case bitc::RMW_MIN: return AtomicRMWInst::Min; 1307 case bitc::RMW_UMAX: return AtomicRMWInst::UMax; 1308 case bitc::RMW_UMIN: return AtomicRMWInst::UMin; 1309 case bitc::RMW_FADD: return AtomicRMWInst::FAdd; 1310 case bitc::RMW_FSUB: return AtomicRMWInst::FSub; 1311 case bitc::RMW_FMAX: return AtomicRMWInst::FMax; 1312 case bitc::RMW_FMIN: return AtomicRMWInst::FMin; 1313 case bitc::RMW_UINC_WRAP: 1314 return AtomicRMWInst::UIncWrap; 1315 case bitc::RMW_UDEC_WRAP: 1316 return AtomicRMWInst::UDecWrap; 1317 } 1318 } 1319 1320 static AtomicOrdering getDecodedOrdering(unsigned Val) { 1321 switch (Val) { 1322 case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; 1323 case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; 1324 case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; 1325 case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; 1326 case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; 1327 case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; 1328 default: // Map unknown orderings to sequentially-consistent. 1329 case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; 1330 } 1331 } 1332 1333 static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { 1334 switch (Val) { 1335 default: // Map unknown selection kinds to any. 1336 case bitc::COMDAT_SELECTION_KIND_ANY: 1337 return Comdat::Any; 1338 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: 1339 return Comdat::ExactMatch; 1340 case bitc::COMDAT_SELECTION_KIND_LARGEST: 1341 return Comdat::Largest; 1342 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: 1343 return Comdat::NoDeduplicate; 1344 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: 1345 return Comdat::SameSize; 1346 } 1347 } 1348 1349 static FastMathFlags getDecodedFastMathFlags(unsigned Val) { 1350 FastMathFlags FMF; 1351 if (0 != (Val & bitc::UnsafeAlgebra)) 1352 FMF.setFast(); 1353 if (0 != (Val & bitc::AllowReassoc)) 1354 FMF.setAllowReassoc(); 1355 if (0 != (Val & bitc::NoNaNs)) 1356 FMF.setNoNaNs(); 1357 if (0 != (Val & bitc::NoInfs)) 1358 FMF.setNoInfs(); 1359 if (0 != (Val & bitc::NoSignedZeros)) 1360 FMF.setNoSignedZeros(); 1361 if (0 != (Val & bitc::AllowReciprocal)) 1362 FMF.setAllowReciprocal(); 1363 if (0 != (Val & bitc::AllowContract)) 1364 FMF.setAllowContract(true); 1365 if (0 != (Val & bitc::ApproxFunc)) 1366 FMF.setApproxFunc(); 1367 return FMF; 1368 } 1369 1370 static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { 1371 // A GlobalValue with local linkage cannot have a DLL storage class. 1372 if (GV->hasLocalLinkage()) 1373 return; 1374 switch (Val) { 1375 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; 1376 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; 1377 } 1378 } 1379 1380 Type *BitcodeReader::getTypeByID(unsigned ID) { 1381 // The type table size is always specified correctly. 1382 if (ID >= TypeList.size()) 1383 return nullptr; 1384 1385 if (Type *Ty = TypeList[ID]) 1386 return Ty; 1387 1388 // If we have a forward reference, the only possible case is when it is to a 1389 // named struct. Just create a placeholder for now. 1390 return TypeList[ID] = createIdentifiedStructType(Context); 1391 } 1392 1393 unsigned BitcodeReader::getContainedTypeID(unsigned ID, unsigned Idx) { 1394 auto It = ContainedTypeIDs.find(ID); 1395 if (It == ContainedTypeIDs.end()) 1396 return InvalidTypeID; 1397 1398 if (Idx >= It->second.size()) 1399 return InvalidTypeID; 1400 1401 return It->second[Idx]; 1402 } 1403 1404 Type *BitcodeReader::getPtrElementTypeByID(unsigned ID) { 1405 if (ID >= TypeList.size()) 1406 return nullptr; 1407 1408 Type *Ty = TypeList[ID]; 1409 if (!Ty->isPointerTy()) 1410 return nullptr; 1411 1412 return getTypeByID(getContainedTypeID(ID, 0)); 1413 } 1414 1415 unsigned BitcodeReader::getVirtualTypeID(Type *Ty, 1416 ArrayRef<unsigned> ChildTypeIDs) { 1417 unsigned ChildTypeID = ChildTypeIDs.empty() ? InvalidTypeID : ChildTypeIDs[0]; 1418 auto CacheKey = std::make_pair(Ty, ChildTypeID); 1419 auto It = VirtualTypeIDs.find(CacheKey); 1420 if (It != VirtualTypeIDs.end()) { 1421 // The cmpxchg return value is the only place we need more than one 1422 // contained type ID, however the second one will always be the same (i1), 1423 // so we don't need to include it in the cache key. This asserts that the 1424 // contained types are indeed as expected and there are no collisions. 1425 assert((ChildTypeIDs.empty() || 1426 ContainedTypeIDs[It->second] == ChildTypeIDs) && 1427 "Incorrect cached contained type IDs"); 1428 return It->second; 1429 } 1430 1431 unsigned TypeID = TypeList.size(); 1432 TypeList.push_back(Ty); 1433 if (!ChildTypeIDs.empty()) 1434 append_range(ContainedTypeIDs[TypeID], ChildTypeIDs); 1435 VirtualTypeIDs.insert({CacheKey, TypeID}); 1436 return TypeID; 1437 } 1438 1439 static bool isConstExprSupported(const BitcodeConstant *BC) { 1440 uint8_t Opcode = BC->Opcode; 1441 1442 // These are not real constant expressions, always consider them supported. 1443 if (Opcode >= BitcodeConstant::FirstSpecialOpcode) 1444 return true; 1445 1446 // If -expand-constant-exprs is set, we want to consider all expressions 1447 // as unsupported. 1448 if (ExpandConstantExprs) 1449 return false; 1450 1451 if (Instruction::isBinaryOp(Opcode)) 1452 return ConstantExpr::isSupportedBinOp(Opcode); 1453 1454 if (Instruction::isCast(Opcode)) 1455 return ConstantExpr::isSupportedCastOp(Opcode); 1456 1457 if (Opcode == Instruction::GetElementPtr) 1458 return ConstantExpr::isSupportedGetElementPtr(BC->SrcElemTy); 1459 1460 switch (Opcode) { 1461 case Instruction::FNeg: 1462 case Instruction::Select: 1463 return false; 1464 default: 1465 return true; 1466 } 1467 } 1468 1469 Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID, 1470 BasicBlock *InsertBB) { 1471 // Quickly handle the case where there is no BitcodeConstant to resolve. 1472 if (StartValID < ValueList.size() && ValueList[StartValID] && 1473 !isa<BitcodeConstant>(ValueList[StartValID])) 1474 return ValueList[StartValID]; 1475 1476 SmallDenseMap<unsigned, Value *> MaterializedValues; 1477 SmallVector<unsigned> Worklist; 1478 Worklist.push_back(StartValID); 1479 while (!Worklist.empty()) { 1480 unsigned ValID = Worklist.back(); 1481 if (MaterializedValues.count(ValID)) { 1482 // Duplicate expression that was already handled. 1483 Worklist.pop_back(); 1484 continue; 1485 } 1486 1487 if (ValID >= ValueList.size() || !ValueList[ValID]) 1488 return error("Invalid value ID"); 1489 1490 Value *V = ValueList[ValID]; 1491 auto *BC = dyn_cast<BitcodeConstant>(V); 1492 if (!BC) { 1493 MaterializedValues.insert({ValID, V}); 1494 Worklist.pop_back(); 1495 continue; 1496 } 1497 1498 // Iterate in reverse, so values will get popped from the worklist in 1499 // expected order. 1500 SmallVector<Value *> Ops; 1501 for (unsigned OpID : reverse(BC->getOperandIDs())) { 1502 auto It = MaterializedValues.find(OpID); 1503 if (It != MaterializedValues.end()) 1504 Ops.push_back(It->second); 1505 else 1506 Worklist.push_back(OpID); 1507 } 1508 1509 // Some expressions have not been resolved yet, handle them first and then 1510 // revisit this one. 1511 if (Ops.size() != BC->getOperandIDs().size()) 1512 continue; 1513 std::reverse(Ops.begin(), Ops.end()); 1514 1515 SmallVector<Constant *> ConstOps; 1516 for (Value *Op : Ops) 1517 if (auto *C = dyn_cast<Constant>(Op)) 1518 ConstOps.push_back(C); 1519 1520 // Materialize as constant expression if possible. 1521 if (isConstExprSupported(BC) && ConstOps.size() == Ops.size()) { 1522 Constant *C; 1523 if (Instruction::isCast(BC->Opcode)) { 1524 C = UpgradeBitCastExpr(BC->Opcode, ConstOps[0], BC->getType()); 1525 if (!C) 1526 C = ConstantExpr::getCast(BC->Opcode, ConstOps[0], BC->getType()); 1527 } else if (Instruction::isBinaryOp(BC->Opcode)) { 1528 C = ConstantExpr::get(BC->Opcode, ConstOps[0], ConstOps[1], BC->Flags); 1529 } else { 1530 switch (BC->Opcode) { 1531 case BitcodeConstant::NoCFIOpcode: { 1532 auto *GV = dyn_cast<GlobalValue>(ConstOps[0]); 1533 if (!GV) 1534 return error("no_cfi operand must be GlobalValue"); 1535 C = NoCFIValue::get(GV); 1536 break; 1537 } 1538 case BitcodeConstant::DSOLocalEquivalentOpcode: { 1539 auto *GV = dyn_cast<GlobalValue>(ConstOps[0]); 1540 if (!GV) 1541 return error("dso_local operand must be GlobalValue"); 1542 C = DSOLocalEquivalent::get(GV); 1543 break; 1544 } 1545 case BitcodeConstant::BlockAddressOpcode: { 1546 Function *Fn = dyn_cast<Function>(ConstOps[0]); 1547 if (!Fn) 1548 return error("blockaddress operand must be a function"); 1549 1550 // If the function is already parsed we can insert the block address 1551 // right away. 1552 BasicBlock *BB; 1553 unsigned BBID = BC->Extra; 1554 if (!BBID) 1555 // Invalid reference to entry block. 1556 return error("Invalid ID"); 1557 if (!Fn->empty()) { 1558 Function::iterator BBI = Fn->begin(), BBE = Fn->end(); 1559 for (size_t I = 0, E = BBID; I != E; ++I) { 1560 if (BBI == BBE) 1561 return error("Invalid ID"); 1562 ++BBI; 1563 } 1564 BB = &*BBI; 1565 } else { 1566 // Otherwise insert a placeholder and remember it so it can be 1567 // inserted when the function is parsed. 1568 auto &FwdBBs = BasicBlockFwdRefs[Fn]; 1569 if (FwdBBs.empty()) 1570 BasicBlockFwdRefQueue.push_back(Fn); 1571 if (FwdBBs.size() < BBID + 1) 1572 FwdBBs.resize(BBID + 1); 1573 if (!FwdBBs[BBID]) 1574 FwdBBs[BBID] = BasicBlock::Create(Context); 1575 BB = FwdBBs[BBID]; 1576 } 1577 C = BlockAddress::get(Fn, BB); 1578 break; 1579 } 1580 case BitcodeConstant::ConstantStructOpcode: 1581 C = ConstantStruct::get(cast<StructType>(BC->getType()), ConstOps); 1582 break; 1583 case BitcodeConstant::ConstantArrayOpcode: 1584 C = ConstantArray::get(cast<ArrayType>(BC->getType()), ConstOps); 1585 break; 1586 case BitcodeConstant::ConstantVectorOpcode: 1587 C = ConstantVector::get(ConstOps); 1588 break; 1589 case Instruction::ICmp: 1590 case Instruction::FCmp: 1591 C = ConstantExpr::getCompare(BC->Flags, ConstOps[0], ConstOps[1]); 1592 break; 1593 case Instruction::GetElementPtr: 1594 C = ConstantExpr::getGetElementPtr(BC->SrcElemTy, ConstOps[0], 1595 ArrayRef(ConstOps).drop_front(), 1596 BC->Flags, BC->getInRangeIndex()); 1597 break; 1598 case Instruction::ExtractElement: 1599 C = ConstantExpr::getExtractElement(ConstOps[0], ConstOps[1]); 1600 break; 1601 case Instruction::InsertElement: 1602 C = ConstantExpr::getInsertElement(ConstOps[0], ConstOps[1], 1603 ConstOps[2]); 1604 break; 1605 case Instruction::ShuffleVector: { 1606 SmallVector<int, 16> Mask; 1607 ShuffleVectorInst::getShuffleMask(ConstOps[2], Mask); 1608 C = ConstantExpr::getShuffleVector(ConstOps[0], ConstOps[1], Mask); 1609 break; 1610 } 1611 default: 1612 llvm_unreachable("Unhandled bitcode constant"); 1613 } 1614 } 1615 1616 // Cache resolved constant. 1617 ValueList.replaceValueWithoutRAUW(ValID, C); 1618 MaterializedValues.insert({ValID, C}); 1619 Worklist.pop_back(); 1620 continue; 1621 } 1622 1623 if (!InsertBB) 1624 return error(Twine("Value referenced by initializer is an unsupported " 1625 "constant expression of type ") + 1626 BC->getOpcodeName()); 1627 1628 // Materialize as instructions if necessary. 1629 Instruction *I; 1630 if (Instruction::isCast(BC->Opcode)) { 1631 I = CastInst::Create((Instruction::CastOps)BC->Opcode, Ops[0], 1632 BC->getType(), "constexpr", InsertBB); 1633 } else if (Instruction::isUnaryOp(BC->Opcode)) { 1634 I = UnaryOperator::Create((Instruction::UnaryOps)BC->Opcode, Ops[0], 1635 "constexpr", InsertBB); 1636 } else if (Instruction::isBinaryOp(BC->Opcode)) { 1637 I = BinaryOperator::Create((Instruction::BinaryOps)BC->Opcode, Ops[0], 1638 Ops[1], "constexpr", InsertBB); 1639 if (isa<OverflowingBinaryOperator>(I)) { 1640 if (BC->Flags & OverflowingBinaryOperator::NoSignedWrap) 1641 I->setHasNoSignedWrap(); 1642 if (BC->Flags & OverflowingBinaryOperator::NoUnsignedWrap) 1643 I->setHasNoUnsignedWrap(); 1644 } 1645 if (isa<PossiblyExactOperator>(I) && 1646 (BC->Flags & PossiblyExactOperator::IsExact)) 1647 I->setIsExact(); 1648 } else { 1649 switch (BC->Opcode) { 1650 case BitcodeConstant::ConstantVectorOpcode: { 1651 Type *IdxTy = Type::getInt32Ty(BC->getContext()); 1652 Value *V = PoisonValue::get(BC->getType()); 1653 for (auto Pair : enumerate(Ops)) { 1654 Value *Idx = ConstantInt::get(IdxTy, Pair.index()); 1655 V = InsertElementInst::Create(V, Pair.value(), Idx, "constexpr.ins", 1656 InsertBB); 1657 } 1658 I = cast<Instruction>(V); 1659 break; 1660 } 1661 case BitcodeConstant::ConstantStructOpcode: 1662 case BitcodeConstant::ConstantArrayOpcode: { 1663 Value *V = PoisonValue::get(BC->getType()); 1664 for (auto Pair : enumerate(Ops)) 1665 V = InsertValueInst::Create(V, Pair.value(), Pair.index(), 1666 "constexpr.ins", InsertBB); 1667 I = cast<Instruction>(V); 1668 break; 1669 } 1670 case Instruction::ICmp: 1671 case Instruction::FCmp: 1672 I = CmpInst::Create((Instruction::OtherOps)BC->Opcode, 1673 (CmpInst::Predicate)BC->Flags, Ops[0], Ops[1], 1674 "constexpr", InsertBB); 1675 break; 1676 case Instruction::GetElementPtr: 1677 I = GetElementPtrInst::Create(BC->SrcElemTy, Ops[0], 1678 ArrayRef(Ops).drop_front(), "constexpr", 1679 InsertBB); 1680 if (BC->Flags) 1681 cast<GetElementPtrInst>(I)->setIsInBounds(); 1682 break; 1683 case Instruction::Select: 1684 I = SelectInst::Create(Ops[0], Ops[1], Ops[2], "constexpr", InsertBB); 1685 break; 1686 case Instruction::ExtractElement: 1687 I = ExtractElementInst::Create(Ops[0], Ops[1], "constexpr", InsertBB); 1688 break; 1689 case Instruction::InsertElement: 1690 I = InsertElementInst::Create(Ops[0], Ops[1], Ops[2], "constexpr", 1691 InsertBB); 1692 break; 1693 case Instruction::ShuffleVector: 1694 I = new ShuffleVectorInst(Ops[0], Ops[1], Ops[2], "constexpr", 1695 InsertBB); 1696 break; 1697 default: 1698 llvm_unreachable("Unhandled bitcode constant"); 1699 } 1700 } 1701 1702 MaterializedValues.insert({ValID, I}); 1703 Worklist.pop_back(); 1704 } 1705 1706 return MaterializedValues[StartValID]; 1707 } 1708 1709 Expected<Constant *> BitcodeReader::getValueForInitializer(unsigned ID) { 1710 Expected<Value *> MaybeV = materializeValue(ID, /* InsertBB */ nullptr); 1711 if (!MaybeV) 1712 return MaybeV.takeError(); 1713 1714 // Result must be Constant if InsertBB is nullptr. 1715 return cast<Constant>(MaybeV.get()); 1716 } 1717 1718 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, 1719 StringRef Name) { 1720 auto *Ret = StructType::create(Context, Name); 1721 IdentifiedStructTypes.push_back(Ret); 1722 return Ret; 1723 } 1724 1725 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { 1726 auto *Ret = StructType::create(Context); 1727 IdentifiedStructTypes.push_back(Ret); 1728 return Ret; 1729 } 1730 1731 //===----------------------------------------------------------------------===// 1732 // Functions for parsing blocks from the bitcode file 1733 //===----------------------------------------------------------------------===// 1734 1735 static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { 1736 switch (Val) { 1737 case Attribute::EndAttrKinds: 1738 case Attribute::EmptyKey: 1739 case Attribute::TombstoneKey: 1740 llvm_unreachable("Synthetic enumerators which should never get here"); 1741 1742 case Attribute::None: return 0; 1743 case Attribute::ZExt: return 1 << 0; 1744 case Attribute::SExt: return 1 << 1; 1745 case Attribute::NoReturn: return 1 << 2; 1746 case Attribute::InReg: return 1 << 3; 1747 case Attribute::StructRet: return 1 << 4; 1748 case Attribute::NoUnwind: return 1 << 5; 1749 case Attribute::NoAlias: return 1 << 6; 1750 case Attribute::ByVal: return 1 << 7; 1751 case Attribute::Nest: return 1 << 8; 1752 case Attribute::ReadNone: return 1 << 9; 1753 case Attribute::ReadOnly: return 1 << 10; 1754 case Attribute::NoInline: return 1 << 11; 1755 case Attribute::AlwaysInline: return 1 << 12; 1756 case Attribute::OptimizeForSize: return 1 << 13; 1757 case Attribute::StackProtect: return 1 << 14; 1758 case Attribute::StackProtectReq: return 1 << 15; 1759 case Attribute::Alignment: return 31 << 16; 1760 case Attribute::NoCapture: return 1 << 21; 1761 case Attribute::NoRedZone: return 1 << 22; 1762 case Attribute::NoImplicitFloat: return 1 << 23; 1763 case Attribute::Naked: return 1 << 24; 1764 case Attribute::InlineHint: return 1 << 25; 1765 case Attribute::StackAlignment: return 7 << 26; 1766 case Attribute::ReturnsTwice: return 1 << 29; 1767 case Attribute::UWTable: return 1 << 30; 1768 case Attribute::NonLazyBind: return 1U << 31; 1769 case Attribute::SanitizeAddress: return 1ULL << 32; 1770 case Attribute::MinSize: return 1ULL << 33; 1771 case Attribute::NoDuplicate: return 1ULL << 34; 1772 case Attribute::StackProtectStrong: return 1ULL << 35; 1773 case Attribute::SanitizeThread: return 1ULL << 36; 1774 case Attribute::SanitizeMemory: return 1ULL << 37; 1775 case Attribute::NoBuiltin: return 1ULL << 38; 1776 case Attribute::Returned: return 1ULL << 39; 1777 case Attribute::Cold: return 1ULL << 40; 1778 case Attribute::Builtin: return 1ULL << 41; 1779 case Attribute::OptimizeNone: return 1ULL << 42; 1780 case Attribute::InAlloca: return 1ULL << 43; 1781 case Attribute::NonNull: return 1ULL << 44; 1782 case Attribute::JumpTable: return 1ULL << 45; 1783 case Attribute::Convergent: return 1ULL << 46; 1784 case Attribute::SafeStack: return 1ULL << 47; 1785 case Attribute::NoRecurse: return 1ULL << 48; 1786 // 1ULL << 49 is InaccessibleMemOnly, which is upgraded separately. 1787 // 1ULL << 50 is InaccessibleMemOrArgMemOnly, which is upgraded separately. 1788 case Attribute::SwiftSelf: return 1ULL << 51; 1789 case Attribute::SwiftError: return 1ULL << 52; 1790 case Attribute::WriteOnly: return 1ULL << 53; 1791 case Attribute::Speculatable: return 1ULL << 54; 1792 case Attribute::StrictFP: return 1ULL << 55; 1793 case Attribute::SanitizeHWAddress: return 1ULL << 56; 1794 case Attribute::NoCfCheck: return 1ULL << 57; 1795 case Attribute::OptForFuzzing: return 1ULL << 58; 1796 case Attribute::ShadowCallStack: return 1ULL << 59; 1797 case Attribute::SpeculativeLoadHardening: 1798 return 1ULL << 60; 1799 case Attribute::ImmArg: 1800 return 1ULL << 61; 1801 case Attribute::WillReturn: 1802 return 1ULL << 62; 1803 case Attribute::NoFree: 1804 return 1ULL << 63; 1805 default: 1806 // Other attributes are not supported in the raw format, 1807 // as we ran out of space. 1808 return 0; 1809 } 1810 llvm_unreachable("Unsupported attribute type"); 1811 } 1812 1813 static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { 1814 if (!Val) return; 1815 1816 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; 1817 I = Attribute::AttrKind(I + 1)) { 1818 if (uint64_t A = (Val & getRawAttributeMask(I))) { 1819 if (I == Attribute::Alignment) 1820 B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); 1821 else if (I == Attribute::StackAlignment) 1822 B.addStackAlignmentAttr(1ULL << ((A >> 26)-1)); 1823 else if (Attribute::isTypeAttrKind(I)) 1824 B.addTypeAttr(I, nullptr); // Type will be auto-upgraded. 1825 else 1826 B.addAttribute(I); 1827 } 1828 } 1829 } 1830 1831 /// This fills an AttrBuilder object with the LLVM attributes that have 1832 /// been decoded from the given integer. This function must stay in sync with 1833 /// 'encodeLLVMAttributesForBitcode'. 1834 static void decodeLLVMAttributesForBitcode(AttrBuilder &B, 1835 uint64_t EncodedAttrs, 1836 uint64_t AttrIdx) { 1837 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift 1838 // the bits above 31 down by 11 bits. 1839 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; 1840 assert((!Alignment || isPowerOf2_32(Alignment)) && 1841 "Alignment must be a power of two."); 1842 1843 if (Alignment) 1844 B.addAlignmentAttr(Alignment); 1845 1846 uint64_t Attrs = ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | 1847 (EncodedAttrs & 0xffff); 1848 1849 if (AttrIdx == AttributeList::FunctionIndex) { 1850 // Upgrade old memory attributes. 1851 MemoryEffects ME = MemoryEffects::unknown(); 1852 if (Attrs & (1ULL << 9)) { 1853 // ReadNone 1854 Attrs &= ~(1ULL << 9); 1855 ME &= MemoryEffects::none(); 1856 } 1857 if (Attrs & (1ULL << 10)) { 1858 // ReadOnly 1859 Attrs &= ~(1ULL << 10); 1860 ME &= MemoryEffects::readOnly(); 1861 } 1862 if (Attrs & (1ULL << 49)) { 1863 // InaccessibleMemOnly 1864 Attrs &= ~(1ULL << 49); 1865 ME &= MemoryEffects::inaccessibleMemOnly(); 1866 } 1867 if (Attrs & (1ULL << 50)) { 1868 // InaccessibleMemOrArgMemOnly 1869 Attrs &= ~(1ULL << 50); 1870 ME &= MemoryEffects::inaccessibleOrArgMemOnly(); 1871 } 1872 if (Attrs & (1ULL << 53)) { 1873 // WriteOnly 1874 Attrs &= ~(1ULL << 53); 1875 ME &= MemoryEffects::writeOnly(); 1876 } 1877 if (ME != MemoryEffects::unknown()) 1878 B.addMemoryAttr(ME); 1879 } 1880 1881 addRawAttributeValue(B, Attrs); 1882 } 1883 1884 Error BitcodeReader::parseAttributeBlock() { 1885 if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) 1886 return Err; 1887 1888 if (!MAttributes.empty()) 1889 return error("Invalid multiple blocks"); 1890 1891 SmallVector<uint64_t, 64> Record; 1892 1893 SmallVector<AttributeList, 8> Attrs; 1894 1895 // Read all the records. 1896 while (true) { 1897 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 1898 if (!MaybeEntry) 1899 return MaybeEntry.takeError(); 1900 BitstreamEntry Entry = MaybeEntry.get(); 1901 1902 switch (Entry.Kind) { 1903 case BitstreamEntry::SubBlock: // Handled for us already. 1904 case BitstreamEntry::Error: 1905 return error("Malformed block"); 1906 case BitstreamEntry::EndBlock: 1907 return Error::success(); 1908 case BitstreamEntry::Record: 1909 // The interesting case. 1910 break; 1911 } 1912 1913 // Read a record. 1914 Record.clear(); 1915 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 1916 if (!MaybeRecord) 1917 return MaybeRecord.takeError(); 1918 switch (MaybeRecord.get()) { 1919 default: // Default behavior: ignore. 1920 break; 1921 case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...] 1922 // Deprecated, but still needed to read old bitcode files. 1923 if (Record.size() & 1) 1924 return error("Invalid parameter attribute record"); 1925 1926 for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 1927 AttrBuilder B(Context); 1928 decodeLLVMAttributesForBitcode(B, Record[i+1], Record[i]); 1929 Attrs.push_back(AttributeList::get(Context, Record[i], B)); 1930 } 1931 1932 MAttributes.push_back(AttributeList::get(Context, Attrs)); 1933 Attrs.clear(); 1934 break; 1935 case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...] 1936 for (unsigned i = 0, e = Record.size(); i != e; ++i) 1937 Attrs.push_back(MAttributeGroups[Record[i]]); 1938 1939 MAttributes.push_back(AttributeList::get(Context, Attrs)); 1940 Attrs.clear(); 1941 break; 1942 } 1943 } 1944 } 1945 1946 // Returns Attribute::None on unrecognized codes. 1947 static Attribute::AttrKind getAttrFromCode(uint64_t Code) { 1948 switch (Code) { 1949 default: 1950 return Attribute::None; 1951 case bitc::ATTR_KIND_ALIGNMENT: 1952 return Attribute::Alignment; 1953 case bitc::ATTR_KIND_ALWAYS_INLINE: 1954 return Attribute::AlwaysInline; 1955 case bitc::ATTR_KIND_BUILTIN: 1956 return Attribute::Builtin; 1957 case bitc::ATTR_KIND_BY_VAL: 1958 return Attribute::ByVal; 1959 case bitc::ATTR_KIND_IN_ALLOCA: 1960 return Attribute::InAlloca; 1961 case bitc::ATTR_KIND_COLD: 1962 return Attribute::Cold; 1963 case bitc::ATTR_KIND_CONVERGENT: 1964 return Attribute::Convergent; 1965 case bitc::ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION: 1966 return Attribute::DisableSanitizerInstrumentation; 1967 case bitc::ATTR_KIND_ELEMENTTYPE: 1968 return Attribute::ElementType; 1969 case bitc::ATTR_KIND_FNRETTHUNK_EXTERN: 1970 return Attribute::FnRetThunkExtern; 1971 case bitc::ATTR_KIND_INLINE_HINT: 1972 return Attribute::InlineHint; 1973 case bitc::ATTR_KIND_IN_REG: 1974 return Attribute::InReg; 1975 case bitc::ATTR_KIND_JUMP_TABLE: 1976 return Attribute::JumpTable; 1977 case bitc::ATTR_KIND_MEMORY: 1978 return Attribute::Memory; 1979 case bitc::ATTR_KIND_NOFPCLASS: 1980 return Attribute::NoFPClass; 1981 case bitc::ATTR_KIND_MIN_SIZE: 1982 return Attribute::MinSize; 1983 case bitc::ATTR_KIND_NAKED: 1984 return Attribute::Naked; 1985 case bitc::ATTR_KIND_NEST: 1986 return Attribute::Nest; 1987 case bitc::ATTR_KIND_NO_ALIAS: 1988 return Attribute::NoAlias; 1989 case bitc::ATTR_KIND_NO_BUILTIN: 1990 return Attribute::NoBuiltin; 1991 case bitc::ATTR_KIND_NO_CALLBACK: 1992 return Attribute::NoCallback; 1993 case bitc::ATTR_KIND_NO_CAPTURE: 1994 return Attribute::NoCapture; 1995 case bitc::ATTR_KIND_NO_DUPLICATE: 1996 return Attribute::NoDuplicate; 1997 case bitc::ATTR_KIND_NOFREE: 1998 return Attribute::NoFree; 1999 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: 2000 return Attribute::NoImplicitFloat; 2001 case bitc::ATTR_KIND_NO_INLINE: 2002 return Attribute::NoInline; 2003 case bitc::ATTR_KIND_NO_RECURSE: 2004 return Attribute::NoRecurse; 2005 case bitc::ATTR_KIND_NO_MERGE: 2006 return Attribute::NoMerge; 2007 case bitc::ATTR_KIND_NON_LAZY_BIND: 2008 return Attribute::NonLazyBind; 2009 case bitc::ATTR_KIND_NON_NULL: 2010 return Attribute::NonNull; 2011 case bitc::ATTR_KIND_DEREFERENCEABLE: 2012 return Attribute::Dereferenceable; 2013 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: 2014 return Attribute::DereferenceableOrNull; 2015 case bitc::ATTR_KIND_ALLOC_ALIGN: 2016 return Attribute::AllocAlign; 2017 case bitc::ATTR_KIND_ALLOC_KIND: 2018 return Attribute::AllocKind; 2019 case bitc::ATTR_KIND_ALLOC_SIZE: 2020 return Attribute::AllocSize; 2021 case bitc::ATTR_KIND_ALLOCATED_POINTER: 2022 return Attribute::AllocatedPointer; 2023 case bitc::ATTR_KIND_NO_RED_ZONE: 2024 return Attribute::NoRedZone; 2025 case bitc::ATTR_KIND_NO_RETURN: 2026 return Attribute::NoReturn; 2027 case bitc::ATTR_KIND_NOSYNC: 2028 return Attribute::NoSync; 2029 case bitc::ATTR_KIND_NOCF_CHECK: 2030 return Attribute::NoCfCheck; 2031 case bitc::ATTR_KIND_NO_PROFILE: 2032 return Attribute::NoProfile; 2033 case bitc::ATTR_KIND_SKIP_PROFILE: 2034 return Attribute::SkipProfile; 2035 case bitc::ATTR_KIND_NO_UNWIND: 2036 return Attribute::NoUnwind; 2037 case bitc::ATTR_KIND_NO_SANITIZE_BOUNDS: 2038 return Attribute::NoSanitizeBounds; 2039 case bitc::ATTR_KIND_NO_SANITIZE_COVERAGE: 2040 return Attribute::NoSanitizeCoverage; 2041 case bitc::ATTR_KIND_NULL_POINTER_IS_VALID: 2042 return Attribute::NullPointerIsValid; 2043 case bitc::ATTR_KIND_OPTIMIZE_FOR_DEBUGGING: 2044 return Attribute::OptimizeForDebugging; 2045 case bitc::ATTR_KIND_OPT_FOR_FUZZING: 2046 return Attribute::OptForFuzzing; 2047 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: 2048 return Attribute::OptimizeForSize; 2049 case bitc::ATTR_KIND_OPTIMIZE_NONE: 2050 return Attribute::OptimizeNone; 2051 case bitc::ATTR_KIND_READ_NONE: 2052 return Attribute::ReadNone; 2053 case bitc::ATTR_KIND_READ_ONLY: 2054 return Attribute::ReadOnly; 2055 case bitc::ATTR_KIND_RETURNED: 2056 return Attribute::Returned; 2057 case bitc::ATTR_KIND_RETURNS_TWICE: 2058 return Attribute::ReturnsTwice; 2059 case bitc::ATTR_KIND_S_EXT: 2060 return Attribute::SExt; 2061 case bitc::ATTR_KIND_SPECULATABLE: 2062 return Attribute::Speculatable; 2063 case bitc::ATTR_KIND_STACK_ALIGNMENT: 2064 return Attribute::StackAlignment; 2065 case bitc::ATTR_KIND_STACK_PROTECT: 2066 return Attribute::StackProtect; 2067 case bitc::ATTR_KIND_STACK_PROTECT_REQ: 2068 return Attribute::StackProtectReq; 2069 case bitc::ATTR_KIND_STACK_PROTECT_STRONG: 2070 return Attribute::StackProtectStrong; 2071 case bitc::ATTR_KIND_SAFESTACK: 2072 return Attribute::SafeStack; 2073 case bitc::ATTR_KIND_SHADOWCALLSTACK: 2074 return Attribute::ShadowCallStack; 2075 case bitc::ATTR_KIND_STRICT_FP: 2076 return Attribute::StrictFP; 2077 case bitc::ATTR_KIND_STRUCT_RET: 2078 return Attribute::StructRet; 2079 case bitc::ATTR_KIND_SANITIZE_ADDRESS: 2080 return Attribute::SanitizeAddress; 2081 case bitc::ATTR_KIND_SANITIZE_HWADDRESS: 2082 return Attribute::SanitizeHWAddress; 2083 case bitc::ATTR_KIND_SANITIZE_THREAD: 2084 return Attribute::SanitizeThread; 2085 case bitc::ATTR_KIND_SANITIZE_MEMORY: 2086 return Attribute::SanitizeMemory; 2087 case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING: 2088 return Attribute::SpeculativeLoadHardening; 2089 case bitc::ATTR_KIND_SWIFT_ERROR: 2090 return Attribute::SwiftError; 2091 case bitc::ATTR_KIND_SWIFT_SELF: 2092 return Attribute::SwiftSelf; 2093 case bitc::ATTR_KIND_SWIFT_ASYNC: 2094 return Attribute::SwiftAsync; 2095 case bitc::ATTR_KIND_UW_TABLE: 2096 return Attribute::UWTable; 2097 case bitc::ATTR_KIND_VSCALE_RANGE: 2098 return Attribute::VScaleRange; 2099 case bitc::ATTR_KIND_WILLRETURN: 2100 return Attribute::WillReturn; 2101 case bitc::ATTR_KIND_WRITEONLY: 2102 return Attribute::WriteOnly; 2103 case bitc::ATTR_KIND_Z_EXT: 2104 return Attribute::ZExt; 2105 case bitc::ATTR_KIND_IMMARG: 2106 return Attribute::ImmArg; 2107 case bitc::ATTR_KIND_SANITIZE_MEMTAG: 2108 return Attribute::SanitizeMemTag; 2109 case bitc::ATTR_KIND_PREALLOCATED: 2110 return Attribute::Preallocated; 2111 case bitc::ATTR_KIND_NOUNDEF: 2112 return Attribute::NoUndef; 2113 case bitc::ATTR_KIND_BYREF: 2114 return Attribute::ByRef; 2115 case bitc::ATTR_KIND_MUSTPROGRESS: 2116 return Attribute::MustProgress; 2117 case bitc::ATTR_KIND_HOT: 2118 return Attribute::Hot; 2119 case bitc::ATTR_KIND_PRESPLIT_COROUTINE: 2120 return Attribute::PresplitCoroutine; 2121 case bitc::ATTR_KIND_WRITABLE: 2122 return Attribute::Writable; 2123 case bitc::ATTR_KIND_CORO_ONLY_DESTROY_WHEN_COMPLETE: 2124 return Attribute::CoroDestroyOnlyWhenComplete; 2125 case bitc::ATTR_KIND_DEAD_ON_UNWIND: 2126 return Attribute::DeadOnUnwind; 2127 case bitc::ATTR_KIND_RANGE: 2128 return Attribute::Range; 2129 } 2130 } 2131 2132 Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, 2133 MaybeAlign &Alignment) { 2134 // Note: Alignment in bitcode files is incremented by 1, so that zero 2135 // can be used for default alignment. 2136 if (Exponent > Value::MaxAlignmentExponent + 1) 2137 return error("Invalid alignment value"); 2138 Alignment = decodeMaybeAlign(Exponent); 2139 return Error::success(); 2140 } 2141 2142 Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { 2143 *Kind = getAttrFromCode(Code); 2144 if (*Kind == Attribute::None) 2145 return error("Unknown attribute kind (" + Twine(Code) + ")"); 2146 return Error::success(); 2147 } 2148 2149 static bool upgradeOldMemoryAttribute(MemoryEffects &ME, uint64_t EncodedKind) { 2150 switch (EncodedKind) { 2151 case bitc::ATTR_KIND_READ_NONE: 2152 ME &= MemoryEffects::none(); 2153 return true; 2154 case bitc::ATTR_KIND_READ_ONLY: 2155 ME &= MemoryEffects::readOnly(); 2156 return true; 2157 case bitc::ATTR_KIND_WRITEONLY: 2158 ME &= MemoryEffects::writeOnly(); 2159 return true; 2160 case bitc::ATTR_KIND_ARGMEMONLY: 2161 ME &= MemoryEffects::argMemOnly(); 2162 return true; 2163 case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: 2164 ME &= MemoryEffects::inaccessibleMemOnly(); 2165 return true; 2166 case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: 2167 ME &= MemoryEffects::inaccessibleOrArgMemOnly(); 2168 return true; 2169 default: 2170 return false; 2171 } 2172 } 2173 2174 Error BitcodeReader::parseAttributeGroupBlock() { 2175 if (Error Err = Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) 2176 return Err; 2177 2178 if (!MAttributeGroups.empty()) 2179 return error("Invalid multiple blocks"); 2180 2181 SmallVector<uint64_t, 64> Record; 2182 2183 // Read all the records. 2184 while (true) { 2185 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2186 if (!MaybeEntry) 2187 return MaybeEntry.takeError(); 2188 BitstreamEntry Entry = MaybeEntry.get(); 2189 2190 switch (Entry.Kind) { 2191 case BitstreamEntry::SubBlock: // Handled for us already. 2192 case BitstreamEntry::Error: 2193 return error("Malformed block"); 2194 case BitstreamEntry::EndBlock: 2195 return Error::success(); 2196 case BitstreamEntry::Record: 2197 // The interesting case. 2198 break; 2199 } 2200 2201 // Read a record. 2202 Record.clear(); 2203 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2204 if (!MaybeRecord) 2205 return MaybeRecord.takeError(); 2206 switch (MaybeRecord.get()) { 2207 default: // Default behavior: ignore. 2208 break; 2209 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] 2210 if (Record.size() < 3) 2211 return error("Invalid grp record"); 2212 2213 uint64_t GrpID = Record[0]; 2214 uint64_t Idx = Record[1]; // Index of the object this attribute refers to. 2215 2216 AttrBuilder B(Context); 2217 MemoryEffects ME = MemoryEffects::unknown(); 2218 for (unsigned i = 2, e = Record.size(); i != e; ++i) { 2219 if (Record[i] == 0) { // Enum attribute 2220 Attribute::AttrKind Kind; 2221 uint64_t EncodedKind = Record[++i]; 2222 if (Idx == AttributeList::FunctionIndex && 2223 upgradeOldMemoryAttribute(ME, EncodedKind)) 2224 continue; 2225 2226 if (Error Err = parseAttrKind(EncodedKind, &Kind)) 2227 return Err; 2228 2229 // Upgrade old-style byval attribute to one with a type, even if it's 2230 // nullptr. We will have to insert the real type when we associate 2231 // this AttributeList with a function. 2232 if (Kind == Attribute::ByVal) 2233 B.addByValAttr(nullptr); 2234 else if (Kind == Attribute::StructRet) 2235 B.addStructRetAttr(nullptr); 2236 else if (Kind == Attribute::InAlloca) 2237 B.addInAllocaAttr(nullptr); 2238 else if (Kind == Attribute::UWTable) 2239 B.addUWTableAttr(UWTableKind::Default); 2240 else if (Attribute::isEnumAttrKind(Kind)) 2241 B.addAttribute(Kind); 2242 else 2243 return error("Not an enum attribute"); 2244 } else if (Record[i] == 1) { // Integer attribute 2245 Attribute::AttrKind Kind; 2246 if (Error Err = parseAttrKind(Record[++i], &Kind)) 2247 return Err; 2248 if (!Attribute::isIntAttrKind(Kind)) 2249 return error("Not an int attribute"); 2250 if (Kind == Attribute::Alignment) 2251 B.addAlignmentAttr(Record[++i]); 2252 else if (Kind == Attribute::StackAlignment) 2253 B.addStackAlignmentAttr(Record[++i]); 2254 else if (Kind == Attribute::Dereferenceable) 2255 B.addDereferenceableAttr(Record[++i]); 2256 else if (Kind == Attribute::DereferenceableOrNull) 2257 B.addDereferenceableOrNullAttr(Record[++i]); 2258 else if (Kind == Attribute::AllocSize) 2259 B.addAllocSizeAttrFromRawRepr(Record[++i]); 2260 else if (Kind == Attribute::VScaleRange) 2261 B.addVScaleRangeAttrFromRawRepr(Record[++i]); 2262 else if (Kind == Attribute::UWTable) 2263 B.addUWTableAttr(UWTableKind(Record[++i])); 2264 else if (Kind == Attribute::AllocKind) 2265 B.addAllocKindAttr(static_cast<AllocFnKind>(Record[++i])); 2266 else if (Kind == Attribute::Memory) 2267 B.addMemoryAttr(MemoryEffects::createFromIntValue(Record[++i])); 2268 else if (Kind == Attribute::NoFPClass) 2269 B.addNoFPClassAttr( 2270 static_cast<FPClassTest>(Record[++i] & fcAllFlags)); 2271 } else if (Record[i] == 3 || Record[i] == 4) { // String attribute 2272 bool HasValue = (Record[i++] == 4); 2273 SmallString<64> KindStr; 2274 SmallString<64> ValStr; 2275 2276 while (Record[i] != 0 && i != e) 2277 KindStr += Record[i++]; 2278 assert(Record[i] == 0 && "Kind string not null terminated"); 2279 2280 if (HasValue) { 2281 // Has a value associated with it. 2282 ++i; // Skip the '0' that terminates the "kind" string. 2283 while (Record[i] != 0 && i != e) 2284 ValStr += Record[i++]; 2285 assert(Record[i] == 0 && "Value string not null terminated"); 2286 } 2287 2288 B.addAttribute(KindStr.str(), ValStr.str()); 2289 } else if (Record[i] == 5 || Record[i] == 6) { 2290 bool HasType = Record[i] == 6; 2291 Attribute::AttrKind Kind; 2292 if (Error Err = parseAttrKind(Record[++i], &Kind)) 2293 return Err; 2294 if (!Attribute::isTypeAttrKind(Kind)) 2295 return error("Not a type attribute"); 2296 2297 B.addTypeAttr(Kind, HasType ? getTypeByID(Record[++i]) : nullptr); 2298 } else if (Record[i] == 7) { 2299 Attribute::AttrKind Kind; 2300 2301 i++; 2302 if (Error Err = parseAttrKind(Record[i++], &Kind)) 2303 return Err; 2304 if (!Attribute::isConstantRangeAttrKind(Kind)) 2305 return error("Not a ConstantRange attribute"); 2306 2307 Expected<ConstantRange> MaybeCR = readConstantRange(Record, i); 2308 if (!MaybeCR) 2309 return MaybeCR.takeError(); 2310 i--; 2311 2312 B.addConstantRangeAttr(Kind, MaybeCR.get()); 2313 } else { 2314 return error("Invalid attribute group entry"); 2315 } 2316 } 2317 2318 if (ME != MemoryEffects::unknown()) 2319 B.addMemoryAttr(ME); 2320 2321 UpgradeAttributes(B); 2322 MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B); 2323 break; 2324 } 2325 } 2326 } 2327 } 2328 2329 Error BitcodeReader::parseTypeTable() { 2330 if (Error Err = Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) 2331 return Err; 2332 2333 return parseTypeTableBody(); 2334 } 2335 2336 Error BitcodeReader::parseTypeTableBody() { 2337 if (!TypeList.empty()) 2338 return error("Invalid multiple blocks"); 2339 2340 SmallVector<uint64_t, 64> Record; 2341 unsigned NumRecords = 0; 2342 2343 SmallString<64> TypeName; 2344 2345 // Read all the records for this type table. 2346 while (true) { 2347 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2348 if (!MaybeEntry) 2349 return MaybeEntry.takeError(); 2350 BitstreamEntry Entry = MaybeEntry.get(); 2351 2352 switch (Entry.Kind) { 2353 case BitstreamEntry::SubBlock: // Handled for us already. 2354 case BitstreamEntry::Error: 2355 return error("Malformed block"); 2356 case BitstreamEntry::EndBlock: 2357 if (NumRecords != TypeList.size()) 2358 return error("Malformed block"); 2359 return Error::success(); 2360 case BitstreamEntry::Record: 2361 // The interesting case. 2362 break; 2363 } 2364 2365 // Read a record. 2366 Record.clear(); 2367 Type *ResultTy = nullptr; 2368 SmallVector<unsigned> ContainedIDs; 2369 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2370 if (!MaybeRecord) 2371 return MaybeRecord.takeError(); 2372 switch (MaybeRecord.get()) { 2373 default: 2374 return error("Invalid value"); 2375 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] 2376 // TYPE_CODE_NUMENTRY contains a count of the number of types in the 2377 // type list. This allows us to reserve space. 2378 if (Record.empty()) 2379 return error("Invalid numentry record"); 2380 TypeList.resize(Record[0]); 2381 continue; 2382 case bitc::TYPE_CODE_VOID: // VOID 2383 ResultTy = Type::getVoidTy(Context); 2384 break; 2385 case bitc::TYPE_CODE_HALF: // HALF 2386 ResultTy = Type::getHalfTy(Context); 2387 break; 2388 case bitc::TYPE_CODE_BFLOAT: // BFLOAT 2389 ResultTy = Type::getBFloatTy(Context); 2390 break; 2391 case bitc::TYPE_CODE_FLOAT: // FLOAT 2392 ResultTy = Type::getFloatTy(Context); 2393 break; 2394 case bitc::TYPE_CODE_DOUBLE: // DOUBLE 2395 ResultTy = Type::getDoubleTy(Context); 2396 break; 2397 case bitc::TYPE_CODE_X86_FP80: // X86_FP80 2398 ResultTy = Type::getX86_FP80Ty(Context); 2399 break; 2400 case bitc::TYPE_CODE_FP128: // FP128 2401 ResultTy = Type::getFP128Ty(Context); 2402 break; 2403 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 2404 ResultTy = Type::getPPC_FP128Ty(Context); 2405 break; 2406 case bitc::TYPE_CODE_LABEL: // LABEL 2407 ResultTy = Type::getLabelTy(Context); 2408 break; 2409 case bitc::TYPE_CODE_METADATA: // METADATA 2410 ResultTy = Type::getMetadataTy(Context); 2411 break; 2412 case bitc::TYPE_CODE_X86_MMX: // X86_MMX 2413 ResultTy = Type::getX86_MMXTy(Context); 2414 break; 2415 case bitc::TYPE_CODE_X86_AMX: // X86_AMX 2416 ResultTy = Type::getX86_AMXTy(Context); 2417 break; 2418 case bitc::TYPE_CODE_TOKEN: // TOKEN 2419 ResultTy = Type::getTokenTy(Context); 2420 break; 2421 case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] 2422 if (Record.empty()) 2423 return error("Invalid integer record"); 2424 2425 uint64_t NumBits = Record[0]; 2426 if (NumBits < IntegerType::MIN_INT_BITS || 2427 NumBits > IntegerType::MAX_INT_BITS) 2428 return error("Bitwidth for integer type out of range"); 2429 ResultTy = IntegerType::get(Context, NumBits); 2430 break; 2431 } 2432 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or 2433 // [pointee type, address space] 2434 if (Record.empty()) 2435 return error("Invalid pointer record"); 2436 unsigned AddressSpace = 0; 2437 if (Record.size() == 2) 2438 AddressSpace = Record[1]; 2439 ResultTy = getTypeByID(Record[0]); 2440 if (!ResultTy || 2441 !PointerType::isValidElementType(ResultTy)) 2442 return error("Invalid type"); 2443 ContainedIDs.push_back(Record[0]); 2444 ResultTy = PointerType::get(ResultTy, AddressSpace); 2445 break; 2446 } 2447 case bitc::TYPE_CODE_OPAQUE_POINTER: { // OPAQUE_POINTER: [addrspace] 2448 if (Record.size() != 1) 2449 return error("Invalid opaque pointer record"); 2450 unsigned AddressSpace = Record[0]; 2451 ResultTy = PointerType::get(Context, AddressSpace); 2452 break; 2453 } 2454 case bitc::TYPE_CODE_FUNCTION_OLD: { 2455 // Deprecated, but still needed to read old bitcode files. 2456 // FUNCTION: [vararg, attrid, retty, paramty x N] 2457 if (Record.size() < 3) 2458 return error("Invalid function record"); 2459 SmallVector<Type*, 8> ArgTys; 2460 for (unsigned i = 3, e = Record.size(); i != e; ++i) { 2461 if (Type *T = getTypeByID(Record[i])) 2462 ArgTys.push_back(T); 2463 else 2464 break; 2465 } 2466 2467 ResultTy = getTypeByID(Record[2]); 2468 if (!ResultTy || ArgTys.size() < Record.size()-3) 2469 return error("Invalid type"); 2470 2471 ContainedIDs.append(Record.begin() + 2, Record.end()); 2472 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 2473 break; 2474 } 2475 case bitc::TYPE_CODE_FUNCTION: { 2476 // FUNCTION: [vararg, retty, paramty x N] 2477 if (Record.size() < 2) 2478 return error("Invalid function record"); 2479 SmallVector<Type*, 8> ArgTys; 2480 for (unsigned i = 2, e = Record.size(); i != e; ++i) { 2481 if (Type *T = getTypeByID(Record[i])) { 2482 if (!FunctionType::isValidArgumentType(T)) 2483 return error("Invalid function argument type"); 2484 ArgTys.push_back(T); 2485 } 2486 else 2487 break; 2488 } 2489 2490 ResultTy = getTypeByID(Record[1]); 2491 if (!ResultTy || ArgTys.size() < Record.size()-2) 2492 return error("Invalid type"); 2493 2494 ContainedIDs.append(Record.begin() + 1, Record.end()); 2495 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 2496 break; 2497 } 2498 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] 2499 if (Record.empty()) 2500 return error("Invalid anon struct record"); 2501 SmallVector<Type*, 8> EltTys; 2502 for (unsigned i = 1, e = Record.size(); i != e; ++i) { 2503 if (Type *T = getTypeByID(Record[i])) 2504 EltTys.push_back(T); 2505 else 2506 break; 2507 } 2508 if (EltTys.size() != Record.size()-1) 2509 return error("Invalid type"); 2510 ContainedIDs.append(Record.begin() + 1, Record.end()); 2511 ResultTy = StructType::get(Context, EltTys, Record[0]); 2512 break; 2513 } 2514 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] 2515 if (convertToString(Record, 0, TypeName)) 2516 return error("Invalid struct name record"); 2517 continue; 2518 2519 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] 2520 if (Record.empty()) 2521 return error("Invalid named struct record"); 2522 2523 if (NumRecords >= TypeList.size()) 2524 return error("Invalid TYPE table"); 2525 2526 // Check to see if this was forward referenced, if so fill in the temp. 2527 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 2528 if (Res) { 2529 Res->setName(TypeName); 2530 TypeList[NumRecords] = nullptr; 2531 } else // Otherwise, create a new struct. 2532 Res = createIdentifiedStructType(Context, TypeName); 2533 TypeName.clear(); 2534 2535 SmallVector<Type*, 8> EltTys; 2536 for (unsigned i = 1, e = Record.size(); i != e; ++i) { 2537 if (Type *T = getTypeByID(Record[i])) 2538 EltTys.push_back(T); 2539 else 2540 break; 2541 } 2542 if (EltTys.size() != Record.size()-1) 2543 return error("Invalid named struct record"); 2544 Res->setBody(EltTys, Record[0]); 2545 ContainedIDs.append(Record.begin() + 1, Record.end()); 2546 ResultTy = Res; 2547 break; 2548 } 2549 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] 2550 if (Record.size() != 1) 2551 return error("Invalid opaque type record"); 2552 2553 if (NumRecords >= TypeList.size()) 2554 return error("Invalid TYPE table"); 2555 2556 // Check to see if this was forward referenced, if so fill in the temp. 2557 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 2558 if (Res) { 2559 Res->setName(TypeName); 2560 TypeList[NumRecords] = nullptr; 2561 } else // Otherwise, create a new struct with no body. 2562 Res = createIdentifiedStructType(Context, TypeName); 2563 TypeName.clear(); 2564 ResultTy = Res; 2565 break; 2566 } 2567 case bitc::TYPE_CODE_TARGET_TYPE: { // TARGET_TYPE: [NumTy, Tys..., Ints...] 2568 if (Record.size() < 1) 2569 return error("Invalid target extension type record"); 2570 2571 if (NumRecords >= TypeList.size()) 2572 return error("Invalid TYPE table"); 2573 2574 if (Record[0] >= Record.size()) 2575 return error("Too many type parameters"); 2576 2577 unsigned NumTys = Record[0]; 2578 SmallVector<Type *, 4> TypeParams; 2579 SmallVector<unsigned, 8> IntParams; 2580 for (unsigned i = 0; i < NumTys; i++) { 2581 if (Type *T = getTypeByID(Record[i + 1])) 2582 TypeParams.push_back(T); 2583 else 2584 return error("Invalid type"); 2585 } 2586 2587 for (unsigned i = NumTys + 1, e = Record.size(); i < e; i++) { 2588 if (Record[i] > UINT_MAX) 2589 return error("Integer parameter too large"); 2590 IntParams.push_back(Record[i]); 2591 } 2592 ResultTy = TargetExtType::get(Context, TypeName, TypeParams, IntParams); 2593 TypeName.clear(); 2594 break; 2595 } 2596 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] 2597 if (Record.size() < 2) 2598 return error("Invalid array type record"); 2599 ResultTy = getTypeByID(Record[1]); 2600 if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) 2601 return error("Invalid type"); 2602 ContainedIDs.push_back(Record[1]); 2603 ResultTy = ArrayType::get(ResultTy, Record[0]); 2604 break; 2605 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] or 2606 // [numelts, eltty, scalable] 2607 if (Record.size() < 2) 2608 return error("Invalid vector type record"); 2609 if (Record[0] == 0) 2610 return error("Invalid vector length"); 2611 ResultTy = getTypeByID(Record[1]); 2612 if (!ResultTy || !VectorType::isValidElementType(ResultTy)) 2613 return error("Invalid type"); 2614 bool Scalable = Record.size() > 2 ? Record[2] : false; 2615 ContainedIDs.push_back(Record[1]); 2616 ResultTy = VectorType::get(ResultTy, Record[0], Scalable); 2617 break; 2618 } 2619 2620 if (NumRecords >= TypeList.size()) 2621 return error("Invalid TYPE table"); 2622 if (TypeList[NumRecords]) 2623 return error( 2624 "Invalid TYPE table: Only named structs can be forward referenced"); 2625 assert(ResultTy && "Didn't read a type?"); 2626 TypeList[NumRecords] = ResultTy; 2627 if (!ContainedIDs.empty()) 2628 ContainedTypeIDs[NumRecords] = std::move(ContainedIDs); 2629 ++NumRecords; 2630 } 2631 } 2632 2633 Error BitcodeReader::parseOperandBundleTags() { 2634 if (Error Err = Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) 2635 return Err; 2636 2637 if (!BundleTags.empty()) 2638 return error("Invalid multiple blocks"); 2639 2640 SmallVector<uint64_t, 64> Record; 2641 2642 while (true) { 2643 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2644 if (!MaybeEntry) 2645 return MaybeEntry.takeError(); 2646 BitstreamEntry Entry = MaybeEntry.get(); 2647 2648 switch (Entry.Kind) { 2649 case BitstreamEntry::SubBlock: // Handled for us already. 2650 case BitstreamEntry::Error: 2651 return error("Malformed block"); 2652 case BitstreamEntry::EndBlock: 2653 return Error::success(); 2654 case BitstreamEntry::Record: 2655 // The interesting case. 2656 break; 2657 } 2658 2659 // Tags are implicitly mapped to integers by their order. 2660 2661 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2662 if (!MaybeRecord) 2663 return MaybeRecord.takeError(); 2664 if (MaybeRecord.get() != bitc::OPERAND_BUNDLE_TAG) 2665 return error("Invalid operand bundle record"); 2666 2667 // OPERAND_BUNDLE_TAG: [strchr x N] 2668 BundleTags.emplace_back(); 2669 if (convertToString(Record, 0, BundleTags.back())) 2670 return error("Invalid operand bundle record"); 2671 Record.clear(); 2672 } 2673 } 2674 2675 Error BitcodeReader::parseSyncScopeNames() { 2676 if (Error Err = Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID)) 2677 return Err; 2678 2679 if (!SSIDs.empty()) 2680 return error("Invalid multiple synchronization scope names blocks"); 2681 2682 SmallVector<uint64_t, 64> Record; 2683 while (true) { 2684 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2685 if (!MaybeEntry) 2686 return MaybeEntry.takeError(); 2687 BitstreamEntry Entry = MaybeEntry.get(); 2688 2689 switch (Entry.Kind) { 2690 case BitstreamEntry::SubBlock: // Handled for us already. 2691 case BitstreamEntry::Error: 2692 return error("Malformed block"); 2693 case BitstreamEntry::EndBlock: 2694 if (SSIDs.empty()) 2695 return error("Invalid empty synchronization scope names block"); 2696 return Error::success(); 2697 case BitstreamEntry::Record: 2698 // The interesting case. 2699 break; 2700 } 2701 2702 // Synchronization scope names are implicitly mapped to synchronization 2703 // scope IDs by their order. 2704 2705 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2706 if (!MaybeRecord) 2707 return MaybeRecord.takeError(); 2708 if (MaybeRecord.get() != bitc::SYNC_SCOPE_NAME) 2709 return error("Invalid sync scope record"); 2710 2711 SmallString<16> SSN; 2712 if (convertToString(Record, 0, SSN)) 2713 return error("Invalid sync scope record"); 2714 2715 SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN)); 2716 Record.clear(); 2717 } 2718 } 2719 2720 /// Associate a value with its name from the given index in the provided record. 2721 Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, 2722 unsigned NameIndex, Triple &TT) { 2723 SmallString<128> ValueName; 2724 if (convertToString(Record, NameIndex, ValueName)) 2725 return error("Invalid record"); 2726 unsigned ValueID = Record[0]; 2727 if (ValueID >= ValueList.size() || !ValueList[ValueID]) 2728 return error("Invalid record"); 2729 Value *V = ValueList[ValueID]; 2730 2731 StringRef NameStr(ValueName.data(), ValueName.size()); 2732 if (NameStr.contains(0)) 2733 return error("Invalid value name"); 2734 V->setName(NameStr); 2735 auto *GO = dyn_cast<GlobalObject>(V); 2736 if (GO && ImplicitComdatObjects.contains(GO) && TT.supportsCOMDAT()) 2737 GO->setComdat(TheModule->getOrInsertComdat(V->getName())); 2738 return V; 2739 } 2740 2741 /// Helper to note and return the current location, and jump to the given 2742 /// offset. 2743 static Expected<uint64_t> jumpToValueSymbolTable(uint64_t Offset, 2744 BitstreamCursor &Stream) { 2745 // Save the current parsing location so we can jump back at the end 2746 // of the VST read. 2747 uint64_t CurrentBit = Stream.GetCurrentBitNo(); 2748 if (Error JumpFailed = Stream.JumpToBit(Offset * 32)) 2749 return std::move(JumpFailed); 2750 Expected<BitstreamEntry> MaybeEntry = Stream.advance(); 2751 if (!MaybeEntry) 2752 return MaybeEntry.takeError(); 2753 if (MaybeEntry.get().Kind != BitstreamEntry::SubBlock || 2754 MaybeEntry.get().ID != bitc::VALUE_SYMTAB_BLOCK_ID) 2755 return error("Expected value symbol table subblock"); 2756 return CurrentBit; 2757 } 2758 2759 void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, 2760 Function *F, 2761 ArrayRef<uint64_t> Record) { 2762 // Note that we subtract 1 here because the offset is relative to one word 2763 // before the start of the identification or module block, which was 2764 // historically always the start of the regular bitcode header. 2765 uint64_t FuncWordOffset = Record[1] - 1; 2766 uint64_t FuncBitOffset = FuncWordOffset * 32; 2767 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; 2768 // Set the LastFunctionBlockBit to point to the last function block. 2769 // Later when parsing is resumed after function materialization, 2770 // we can simply skip that last function block. 2771 if (FuncBitOffset > LastFunctionBlockBit) 2772 LastFunctionBlockBit = FuncBitOffset; 2773 } 2774 2775 /// Read a new-style GlobalValue symbol table. 2776 Error BitcodeReader::parseGlobalValueSymbolTable() { 2777 unsigned FuncBitcodeOffsetDelta = 2778 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; 2779 2780 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 2781 return Err; 2782 2783 SmallVector<uint64_t, 64> Record; 2784 while (true) { 2785 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2786 if (!MaybeEntry) 2787 return MaybeEntry.takeError(); 2788 BitstreamEntry Entry = MaybeEntry.get(); 2789 2790 switch (Entry.Kind) { 2791 case BitstreamEntry::SubBlock: 2792 case BitstreamEntry::Error: 2793 return error("Malformed block"); 2794 case BitstreamEntry::EndBlock: 2795 return Error::success(); 2796 case BitstreamEntry::Record: 2797 break; 2798 } 2799 2800 Record.clear(); 2801 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2802 if (!MaybeRecord) 2803 return MaybeRecord.takeError(); 2804 switch (MaybeRecord.get()) { 2805 case bitc::VST_CODE_FNENTRY: { // [valueid, offset] 2806 unsigned ValueID = Record[0]; 2807 if (ValueID >= ValueList.size() || !ValueList[ValueID]) 2808 return error("Invalid value reference in symbol table"); 2809 setDeferredFunctionInfo(FuncBitcodeOffsetDelta, 2810 cast<Function>(ValueList[ValueID]), Record); 2811 break; 2812 } 2813 } 2814 } 2815 } 2816 2817 /// Parse the value symbol table at either the current parsing location or 2818 /// at the given bit offset if provided. 2819 Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { 2820 uint64_t CurrentBit; 2821 // Pass in the Offset to distinguish between calling for the module-level 2822 // VST (where we want to jump to the VST offset) and the function-level 2823 // VST (where we don't). 2824 if (Offset > 0) { 2825 Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream); 2826 if (!MaybeCurrentBit) 2827 return MaybeCurrentBit.takeError(); 2828 CurrentBit = MaybeCurrentBit.get(); 2829 // If this module uses a string table, read this as a module-level VST. 2830 if (UseStrtab) { 2831 if (Error Err = parseGlobalValueSymbolTable()) 2832 return Err; 2833 if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) 2834 return JumpFailed; 2835 return Error::success(); 2836 } 2837 // Otherwise, the VST will be in a similar format to a function-level VST, 2838 // and will contain symbol names. 2839 } 2840 2841 // Compute the delta between the bitcode indices in the VST (the word offset 2842 // to the word-aligned ENTER_SUBBLOCK for the function block, and that 2843 // expected by the lazy reader. The reader's EnterSubBlock expects to have 2844 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID 2845 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here 2846 // just before entering the VST subblock because: 1) the EnterSubBlock 2847 // changes the AbbrevID width; 2) the VST block is nested within the same 2848 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same 2849 // AbbrevID width before calling EnterSubBlock; and 3) when we want to 2850 // jump to the FUNCTION_BLOCK using this offset later, we don't want 2851 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. 2852 unsigned FuncBitcodeOffsetDelta = 2853 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; 2854 2855 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 2856 return Err; 2857 2858 SmallVector<uint64_t, 64> Record; 2859 2860 Triple TT(TheModule->getTargetTriple()); 2861 2862 // Read all the records for this value table. 2863 SmallString<128> ValueName; 2864 2865 while (true) { 2866 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 2867 if (!MaybeEntry) 2868 return MaybeEntry.takeError(); 2869 BitstreamEntry Entry = MaybeEntry.get(); 2870 2871 switch (Entry.Kind) { 2872 case BitstreamEntry::SubBlock: // Handled for us already. 2873 case BitstreamEntry::Error: 2874 return error("Malformed block"); 2875 case BitstreamEntry::EndBlock: 2876 if (Offset > 0) 2877 if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) 2878 return JumpFailed; 2879 return Error::success(); 2880 case BitstreamEntry::Record: 2881 // The interesting case. 2882 break; 2883 } 2884 2885 // Read a record. 2886 Record.clear(); 2887 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 2888 if (!MaybeRecord) 2889 return MaybeRecord.takeError(); 2890 switch (MaybeRecord.get()) { 2891 default: // Default behavior: unknown type. 2892 break; 2893 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 2894 Expected<Value *> ValOrErr = recordValue(Record, 1, TT); 2895 if (Error Err = ValOrErr.takeError()) 2896 return Err; 2897 ValOrErr.get(); 2898 break; 2899 } 2900 case bitc::VST_CODE_FNENTRY: { 2901 // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 2902 Expected<Value *> ValOrErr = recordValue(Record, 2, TT); 2903 if (Error Err = ValOrErr.takeError()) 2904 return Err; 2905 Value *V = ValOrErr.get(); 2906 2907 // Ignore function offsets emitted for aliases of functions in older 2908 // versions of LLVM. 2909 if (auto *F = dyn_cast<Function>(V)) 2910 setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record); 2911 break; 2912 } 2913 case bitc::VST_CODE_BBENTRY: { 2914 if (convertToString(Record, 1, ValueName)) 2915 return error("Invalid bbentry record"); 2916 BasicBlock *BB = getBasicBlock(Record[0]); 2917 if (!BB) 2918 return error("Invalid bbentry record"); 2919 2920 BB->setName(StringRef(ValueName.data(), ValueName.size())); 2921 ValueName.clear(); 2922 break; 2923 } 2924 } 2925 } 2926 } 2927 2928 /// Decode a signed value stored with the sign bit in the LSB for dense VBR 2929 /// encoding. 2930 uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { 2931 if ((V & 1) == 0) 2932 return V >> 1; 2933 if (V != 1) 2934 return -(V >> 1); 2935 // There is no such thing as -0 with integers. "-0" really means MININT. 2936 return 1ULL << 63; 2937 } 2938 2939 /// Resolve all of the initializers for global values and aliases that we can. 2940 Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { 2941 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist; 2942 std::vector<std::pair<GlobalValue *, unsigned>> IndirectSymbolInitWorklist; 2943 std::vector<FunctionOperandInfo> FunctionOperandWorklist; 2944 2945 GlobalInitWorklist.swap(GlobalInits); 2946 IndirectSymbolInitWorklist.swap(IndirectSymbolInits); 2947 FunctionOperandWorklist.swap(FunctionOperands); 2948 2949 while (!GlobalInitWorklist.empty()) { 2950 unsigned ValID = GlobalInitWorklist.back().second; 2951 if (ValID >= ValueList.size()) { 2952 // Not ready to resolve this yet, it requires something later in the file. 2953 GlobalInits.push_back(GlobalInitWorklist.back()); 2954 } else { 2955 Expected<Constant *> MaybeC = getValueForInitializer(ValID); 2956 if (!MaybeC) 2957 return MaybeC.takeError(); 2958 GlobalInitWorklist.back().first->setInitializer(MaybeC.get()); 2959 } 2960 GlobalInitWorklist.pop_back(); 2961 } 2962 2963 while (!IndirectSymbolInitWorklist.empty()) { 2964 unsigned ValID = IndirectSymbolInitWorklist.back().second; 2965 if (ValID >= ValueList.size()) { 2966 IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back()); 2967 } else { 2968 Expected<Constant *> MaybeC = getValueForInitializer(ValID); 2969 if (!MaybeC) 2970 return MaybeC.takeError(); 2971 Constant *C = MaybeC.get(); 2972 GlobalValue *GV = IndirectSymbolInitWorklist.back().first; 2973 if (auto *GA = dyn_cast<GlobalAlias>(GV)) { 2974 if (C->getType() != GV->getType()) 2975 return error("Alias and aliasee types don't match"); 2976 GA->setAliasee(C); 2977 } else if (auto *GI = dyn_cast<GlobalIFunc>(GV)) { 2978 GI->setResolver(C); 2979 } else { 2980 return error("Expected an alias or an ifunc"); 2981 } 2982 } 2983 IndirectSymbolInitWorklist.pop_back(); 2984 } 2985 2986 while (!FunctionOperandWorklist.empty()) { 2987 FunctionOperandInfo &Info = FunctionOperandWorklist.back(); 2988 if (Info.PersonalityFn) { 2989 unsigned ValID = Info.PersonalityFn - 1; 2990 if (ValID < ValueList.size()) { 2991 Expected<Constant *> MaybeC = getValueForInitializer(ValID); 2992 if (!MaybeC) 2993 return MaybeC.takeError(); 2994 Info.F->setPersonalityFn(MaybeC.get()); 2995 Info.PersonalityFn = 0; 2996 } 2997 } 2998 if (Info.Prefix) { 2999 unsigned ValID = Info.Prefix - 1; 3000 if (ValID < ValueList.size()) { 3001 Expected<Constant *> MaybeC = getValueForInitializer(ValID); 3002 if (!MaybeC) 3003 return MaybeC.takeError(); 3004 Info.F->setPrefixData(MaybeC.get()); 3005 Info.Prefix = 0; 3006 } 3007 } 3008 if (Info.Prologue) { 3009 unsigned ValID = Info.Prologue - 1; 3010 if (ValID < ValueList.size()) { 3011 Expected<Constant *> MaybeC = getValueForInitializer(ValID); 3012 if (!MaybeC) 3013 return MaybeC.takeError(); 3014 Info.F->setPrologueData(MaybeC.get()); 3015 Info.Prologue = 0; 3016 } 3017 } 3018 if (Info.PersonalityFn || Info.Prefix || Info.Prologue) 3019 FunctionOperands.push_back(Info); 3020 FunctionOperandWorklist.pop_back(); 3021 } 3022 3023 return Error::success(); 3024 } 3025 3026 APInt llvm::readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { 3027 SmallVector<uint64_t, 8> Words(Vals.size()); 3028 transform(Vals, Words.begin(), 3029 BitcodeReader::decodeSignRotatedValue); 3030 3031 return APInt(TypeBits, Words); 3032 } 3033 3034 Error BitcodeReader::parseConstants() { 3035 if (Error Err = Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) 3036 return Err; 3037 3038 SmallVector<uint64_t, 64> Record; 3039 3040 // Read all the records for this value table. 3041 Type *CurTy = Type::getInt32Ty(Context); 3042 unsigned Int32TyID = getVirtualTypeID(CurTy); 3043 unsigned CurTyID = Int32TyID; 3044 Type *CurElemTy = nullptr; 3045 unsigned NextCstNo = ValueList.size(); 3046 3047 while (true) { 3048 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 3049 if (!MaybeEntry) 3050 return MaybeEntry.takeError(); 3051 BitstreamEntry Entry = MaybeEntry.get(); 3052 3053 switch (Entry.Kind) { 3054 case BitstreamEntry::SubBlock: // Handled for us already. 3055 case BitstreamEntry::Error: 3056 return error("Malformed block"); 3057 case BitstreamEntry::EndBlock: 3058 if (NextCstNo != ValueList.size()) 3059 return error("Invalid constant reference"); 3060 return Error::success(); 3061 case BitstreamEntry::Record: 3062 // The interesting case. 3063 break; 3064 } 3065 3066 // Read a record. 3067 Record.clear(); 3068 Type *VoidType = Type::getVoidTy(Context); 3069 Value *V = nullptr; 3070 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 3071 if (!MaybeBitCode) 3072 return MaybeBitCode.takeError(); 3073 switch (unsigned BitCode = MaybeBitCode.get()) { 3074 default: // Default behavior: unknown constant 3075 case bitc::CST_CODE_UNDEF: // UNDEF 3076 V = UndefValue::get(CurTy); 3077 break; 3078 case bitc::CST_CODE_POISON: // POISON 3079 V = PoisonValue::get(CurTy); 3080 break; 3081 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] 3082 if (Record.empty()) 3083 return error("Invalid settype record"); 3084 if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) 3085 return error("Invalid settype record"); 3086 if (TypeList[Record[0]] == VoidType) 3087 return error("Invalid constant type"); 3088 CurTyID = Record[0]; 3089 CurTy = TypeList[CurTyID]; 3090 CurElemTy = getPtrElementTypeByID(CurTyID); 3091 continue; // Skip the ValueList manipulation. 3092 case bitc::CST_CODE_NULL: // NULL 3093 if (CurTy->isVoidTy() || CurTy->isFunctionTy() || CurTy->isLabelTy()) 3094 return error("Invalid type for a constant null value"); 3095 if (auto *TETy = dyn_cast<TargetExtType>(CurTy)) 3096 if (!TETy->hasProperty(TargetExtType::HasZeroInit)) 3097 return error("Invalid type for a constant null value"); 3098 V = Constant::getNullValue(CurTy); 3099 break; 3100 case bitc::CST_CODE_INTEGER: // INTEGER: [intval] 3101 if (!CurTy->isIntOrIntVectorTy() || Record.empty()) 3102 return error("Invalid integer const record"); 3103 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); 3104 break; 3105 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] 3106 if (!CurTy->isIntOrIntVectorTy() || Record.empty()) 3107 return error("Invalid wide integer const record"); 3108 3109 auto *ScalarTy = cast<IntegerType>(CurTy->getScalarType()); 3110 APInt VInt = readWideAPInt(Record, ScalarTy->getBitWidth()); 3111 V = ConstantInt::get(CurTy, VInt); 3112 break; 3113 } 3114 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] 3115 if (Record.empty()) 3116 return error("Invalid float const record"); 3117 3118 auto *ScalarTy = CurTy->getScalarType(); 3119 if (ScalarTy->isHalfTy()) 3120 V = ConstantFP::get(CurTy, APFloat(APFloat::IEEEhalf(), 3121 APInt(16, (uint16_t)Record[0]))); 3122 else if (ScalarTy->isBFloatTy()) 3123 V = ConstantFP::get( 3124 CurTy, APFloat(APFloat::BFloat(), APInt(16, (uint32_t)Record[0]))); 3125 else if (ScalarTy->isFloatTy()) 3126 V = ConstantFP::get(CurTy, APFloat(APFloat::IEEEsingle(), 3127 APInt(32, (uint32_t)Record[0]))); 3128 else if (ScalarTy->isDoubleTy()) 3129 V = ConstantFP::get( 3130 CurTy, APFloat(APFloat::IEEEdouble(), APInt(64, Record[0]))); 3131 else if (ScalarTy->isX86_FP80Ty()) { 3132 // Bits are not stored the same way as a normal i80 APInt, compensate. 3133 uint64_t Rearrange[2]; 3134 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); 3135 Rearrange[1] = Record[0] >> 48; 3136 V = ConstantFP::get( 3137 CurTy, APFloat(APFloat::x87DoubleExtended(), APInt(80, Rearrange))); 3138 } else if (ScalarTy->isFP128Ty()) 3139 V = ConstantFP::get(CurTy, 3140 APFloat(APFloat::IEEEquad(), APInt(128, Record))); 3141 else if (ScalarTy->isPPC_FP128Ty()) 3142 V = ConstantFP::get( 3143 CurTy, APFloat(APFloat::PPCDoubleDouble(), APInt(128, Record))); 3144 else 3145 V = UndefValue::get(CurTy); 3146 break; 3147 } 3148 3149 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] 3150 if (Record.empty()) 3151 return error("Invalid aggregate record"); 3152 3153 unsigned Size = Record.size(); 3154 SmallVector<unsigned, 16> Elts; 3155 for (unsigned i = 0; i != Size; ++i) 3156 Elts.push_back(Record[i]); 3157 3158 if (isa<StructType>(CurTy)) { 3159 V = BitcodeConstant::create( 3160 Alloc, CurTy, BitcodeConstant::ConstantStructOpcode, Elts); 3161 } else if (isa<ArrayType>(CurTy)) { 3162 V = BitcodeConstant::create(Alloc, CurTy, 3163 BitcodeConstant::ConstantArrayOpcode, Elts); 3164 } else if (isa<VectorType>(CurTy)) { 3165 V = BitcodeConstant::create( 3166 Alloc, CurTy, BitcodeConstant::ConstantVectorOpcode, Elts); 3167 } else { 3168 V = UndefValue::get(CurTy); 3169 } 3170 break; 3171 } 3172 case bitc::CST_CODE_STRING: // STRING: [values] 3173 case bitc::CST_CODE_CSTRING: { // CSTRING: [values] 3174 if (Record.empty()) 3175 return error("Invalid string record"); 3176 3177 SmallString<16> Elts(Record.begin(), Record.end()); 3178 V = ConstantDataArray::getString(Context, Elts, 3179 BitCode == bitc::CST_CODE_CSTRING); 3180 break; 3181 } 3182 case bitc::CST_CODE_DATA: {// DATA: [n x value] 3183 if (Record.empty()) 3184 return error("Invalid data record"); 3185 3186 Type *EltTy; 3187 if (auto *Array = dyn_cast<ArrayType>(CurTy)) 3188 EltTy = Array->getElementType(); 3189 else 3190 EltTy = cast<VectorType>(CurTy)->getElementType(); 3191 if (EltTy->isIntegerTy(8)) { 3192 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); 3193 if (isa<VectorType>(CurTy)) 3194 V = ConstantDataVector::get(Context, Elts); 3195 else 3196 V = ConstantDataArray::get(Context, Elts); 3197 } else if (EltTy->isIntegerTy(16)) { 3198 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 3199 if (isa<VectorType>(CurTy)) 3200 V = ConstantDataVector::get(Context, Elts); 3201 else 3202 V = ConstantDataArray::get(Context, Elts); 3203 } else if (EltTy->isIntegerTy(32)) { 3204 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 3205 if (isa<VectorType>(CurTy)) 3206 V = ConstantDataVector::get(Context, Elts); 3207 else 3208 V = ConstantDataArray::get(Context, Elts); 3209 } else if (EltTy->isIntegerTy(64)) { 3210 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 3211 if (isa<VectorType>(CurTy)) 3212 V = ConstantDataVector::get(Context, Elts); 3213 else 3214 V = ConstantDataArray::get(Context, Elts); 3215 } else if (EltTy->isHalfTy()) { 3216 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 3217 if (isa<VectorType>(CurTy)) 3218 V = ConstantDataVector::getFP(EltTy, Elts); 3219 else 3220 V = ConstantDataArray::getFP(EltTy, Elts); 3221 } else if (EltTy->isBFloatTy()) { 3222 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 3223 if (isa<VectorType>(CurTy)) 3224 V = ConstantDataVector::getFP(EltTy, Elts); 3225 else 3226 V = ConstantDataArray::getFP(EltTy, Elts); 3227 } else if (EltTy->isFloatTy()) { 3228 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 3229 if (isa<VectorType>(CurTy)) 3230 V = ConstantDataVector::getFP(EltTy, Elts); 3231 else 3232 V = ConstantDataArray::getFP(EltTy, Elts); 3233 } else if (EltTy->isDoubleTy()) { 3234 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 3235 if (isa<VectorType>(CurTy)) 3236 V = ConstantDataVector::getFP(EltTy, Elts); 3237 else 3238 V = ConstantDataArray::getFP(EltTy, Elts); 3239 } else { 3240 return error("Invalid type for value"); 3241 } 3242 break; 3243 } 3244 case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval] 3245 if (Record.size() < 2) 3246 return error("Invalid unary op constexpr record"); 3247 int Opc = getDecodedUnaryOpcode(Record[0], CurTy); 3248 if (Opc < 0) { 3249 V = UndefValue::get(CurTy); // Unknown unop. 3250 } else { 3251 V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[1]); 3252 } 3253 break; 3254 } 3255 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] 3256 if (Record.size() < 3) 3257 return error("Invalid binary op constexpr record"); 3258 int Opc = getDecodedBinaryOpcode(Record[0], CurTy); 3259 if (Opc < 0) { 3260 V = UndefValue::get(CurTy); // Unknown binop. 3261 } else { 3262 uint8_t Flags = 0; 3263 if (Record.size() >= 4) { 3264 if (Opc == Instruction::Add || 3265 Opc == Instruction::Sub || 3266 Opc == Instruction::Mul || 3267 Opc == Instruction::Shl) { 3268 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 3269 Flags |= OverflowingBinaryOperator::NoSignedWrap; 3270 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 3271 Flags |= OverflowingBinaryOperator::NoUnsignedWrap; 3272 } else if (Opc == Instruction::SDiv || 3273 Opc == Instruction::UDiv || 3274 Opc == Instruction::LShr || 3275 Opc == Instruction::AShr) { 3276 if (Record[3] & (1 << bitc::PEO_EXACT)) 3277 Flags |= PossiblyExactOperator::IsExact; 3278 } 3279 } 3280 V = BitcodeConstant::create(Alloc, CurTy, {(uint8_t)Opc, Flags}, 3281 {(unsigned)Record[1], (unsigned)Record[2]}); 3282 } 3283 break; 3284 } 3285 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] 3286 if (Record.size() < 3) 3287 return error("Invalid cast constexpr record"); 3288 int Opc = getDecodedCastOpcode(Record[0]); 3289 if (Opc < 0) { 3290 V = UndefValue::get(CurTy); // Unknown cast. 3291 } else { 3292 unsigned OpTyID = Record[1]; 3293 Type *OpTy = getTypeByID(OpTyID); 3294 if (!OpTy) 3295 return error("Invalid cast constexpr record"); 3296 V = BitcodeConstant::create(Alloc, CurTy, Opc, (unsigned)Record[2]); 3297 } 3298 break; 3299 } 3300 case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] 3301 case bitc::CST_CODE_CE_GEP: // [ty, n x operands] 3302 case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x 3303 // operands] 3304 if (Record.size() < 2) 3305 return error("Constant GEP record must have at least two elements"); 3306 unsigned OpNum = 0; 3307 Type *PointeeType = nullptr; 3308 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX || 3309 Record.size() % 2) 3310 PointeeType = getTypeByID(Record[OpNum++]); 3311 3312 bool InBounds = false; 3313 std::optional<unsigned> InRangeIndex; 3314 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) { 3315 uint64_t Op = Record[OpNum++]; 3316 InBounds = Op & 1; 3317 InRangeIndex = Op >> 1; 3318 } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) 3319 InBounds = true; 3320 3321 SmallVector<unsigned, 16> Elts; 3322 unsigned BaseTypeID = Record[OpNum]; 3323 while (OpNum != Record.size()) { 3324 unsigned ElTyID = Record[OpNum++]; 3325 Type *ElTy = getTypeByID(ElTyID); 3326 if (!ElTy) 3327 return error("Invalid getelementptr constexpr record"); 3328 Elts.push_back(Record[OpNum++]); 3329 } 3330 3331 if (Elts.size() < 1) 3332 return error("Invalid gep with no operands"); 3333 3334 Type *BaseType = getTypeByID(BaseTypeID); 3335 if (isa<VectorType>(BaseType)) { 3336 BaseTypeID = getContainedTypeID(BaseTypeID, 0); 3337 BaseType = getTypeByID(BaseTypeID); 3338 } 3339 3340 PointerType *OrigPtrTy = dyn_cast_or_null<PointerType>(BaseType); 3341 if (!OrigPtrTy) 3342 return error("GEP base operand must be pointer or vector of pointer"); 3343 3344 if (!PointeeType) { 3345 PointeeType = getPtrElementTypeByID(BaseTypeID); 3346 if (!PointeeType) 3347 return error("Missing element type for old-style constant GEP"); 3348 } 3349 3350 V = BitcodeConstant::create(Alloc, CurTy, 3351 {Instruction::GetElementPtr, InBounds, 3352 InRangeIndex.value_or(-1), PointeeType}, 3353 Elts); 3354 break; 3355 } 3356 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] 3357 if (Record.size() < 3) 3358 return error("Invalid select constexpr record"); 3359 3360 V = BitcodeConstant::create( 3361 Alloc, CurTy, Instruction::Select, 3362 {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]}); 3363 break; 3364 } 3365 case bitc::CST_CODE_CE_EXTRACTELT 3366 : { // CE_EXTRACTELT: [opty, opval, opty, opval] 3367 if (Record.size() < 3) 3368 return error("Invalid extractelement constexpr record"); 3369 unsigned OpTyID = Record[0]; 3370 VectorType *OpTy = 3371 dyn_cast_or_null<VectorType>(getTypeByID(OpTyID)); 3372 if (!OpTy) 3373 return error("Invalid extractelement constexpr record"); 3374 unsigned IdxRecord; 3375 if (Record.size() == 4) { 3376 unsigned IdxTyID = Record[2]; 3377 Type *IdxTy = getTypeByID(IdxTyID); 3378 if (!IdxTy) 3379 return error("Invalid extractelement constexpr record"); 3380 IdxRecord = Record[3]; 3381 } else { 3382 // Deprecated, but still needed to read old bitcode files. 3383 IdxRecord = Record[2]; 3384 } 3385 V = BitcodeConstant::create(Alloc, CurTy, Instruction::ExtractElement, 3386 {(unsigned)Record[1], IdxRecord}); 3387 break; 3388 } 3389 case bitc::CST_CODE_CE_INSERTELT 3390 : { // CE_INSERTELT: [opval, opval, opty, opval] 3391 VectorType *OpTy = dyn_cast<VectorType>(CurTy); 3392 if (Record.size() < 3 || !OpTy) 3393 return error("Invalid insertelement constexpr record"); 3394 unsigned IdxRecord; 3395 if (Record.size() == 4) { 3396 unsigned IdxTyID = Record[2]; 3397 Type *IdxTy = getTypeByID(IdxTyID); 3398 if (!IdxTy) 3399 return error("Invalid insertelement constexpr record"); 3400 IdxRecord = Record[3]; 3401 } else { 3402 // Deprecated, but still needed to read old bitcode files. 3403 IdxRecord = Record[2]; 3404 } 3405 V = BitcodeConstant::create( 3406 Alloc, CurTy, Instruction::InsertElement, 3407 {(unsigned)Record[0], (unsigned)Record[1], IdxRecord}); 3408 break; 3409 } 3410 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] 3411 VectorType *OpTy = dyn_cast<VectorType>(CurTy); 3412 if (Record.size() < 3 || !OpTy) 3413 return error("Invalid shufflevector constexpr record"); 3414 V = BitcodeConstant::create( 3415 Alloc, CurTy, Instruction::ShuffleVector, 3416 {(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2]}); 3417 break; 3418 } 3419 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] 3420 VectorType *RTy = dyn_cast<VectorType>(CurTy); 3421 VectorType *OpTy = 3422 dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 3423 if (Record.size() < 4 || !RTy || !OpTy) 3424 return error("Invalid shufflevector constexpr record"); 3425 V = BitcodeConstant::create( 3426 Alloc, CurTy, Instruction::ShuffleVector, 3427 {(unsigned)Record[1], (unsigned)Record[2], (unsigned)Record[3]}); 3428 break; 3429 } 3430 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] 3431 if (Record.size() < 4) 3432 return error("Invalid cmp constexpt record"); 3433 unsigned OpTyID = Record[0]; 3434 Type *OpTy = getTypeByID(OpTyID); 3435 if (!OpTy) 3436 return error("Invalid cmp constexpr record"); 3437 V = BitcodeConstant::create( 3438 Alloc, CurTy, 3439 {(uint8_t)(OpTy->isFPOrFPVectorTy() ? Instruction::FCmp 3440 : Instruction::ICmp), 3441 (uint8_t)Record[3]}, 3442 {(unsigned)Record[1], (unsigned)Record[2]}); 3443 break; 3444 } 3445 // This maintains backward compatibility, pre-asm dialect keywords. 3446 // Deprecated, but still needed to read old bitcode files. 3447 case bitc::CST_CODE_INLINEASM_OLD: { 3448 if (Record.size() < 2) 3449 return error("Invalid inlineasm record"); 3450 std::string AsmStr, ConstrStr; 3451 bool HasSideEffects = Record[0] & 1; 3452 bool IsAlignStack = Record[0] >> 1; 3453 unsigned AsmStrSize = Record[1]; 3454 if (2+AsmStrSize >= Record.size()) 3455 return error("Invalid inlineasm record"); 3456 unsigned ConstStrSize = Record[2+AsmStrSize]; 3457 if (3+AsmStrSize+ConstStrSize > Record.size()) 3458 return error("Invalid inlineasm record"); 3459 3460 for (unsigned i = 0; i != AsmStrSize; ++i) 3461 AsmStr += (char)Record[2+i]; 3462 for (unsigned i = 0; i != ConstStrSize; ++i) 3463 ConstrStr += (char)Record[3+AsmStrSize+i]; 3464 UpgradeInlineAsmString(&AsmStr); 3465 if (!CurElemTy) 3466 return error("Missing element type for old-style inlineasm"); 3467 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, 3468 HasSideEffects, IsAlignStack); 3469 break; 3470 } 3471 // This version adds support for the asm dialect keywords (e.g., 3472 // inteldialect). 3473 case bitc::CST_CODE_INLINEASM_OLD2: { 3474 if (Record.size() < 2) 3475 return error("Invalid inlineasm record"); 3476 std::string AsmStr, ConstrStr; 3477 bool HasSideEffects = Record[0] & 1; 3478 bool IsAlignStack = (Record[0] >> 1) & 1; 3479 unsigned AsmDialect = Record[0] >> 2; 3480 unsigned AsmStrSize = Record[1]; 3481 if (2+AsmStrSize >= Record.size()) 3482 return error("Invalid inlineasm record"); 3483 unsigned ConstStrSize = Record[2+AsmStrSize]; 3484 if (3+AsmStrSize+ConstStrSize > Record.size()) 3485 return error("Invalid inlineasm record"); 3486 3487 for (unsigned i = 0; i != AsmStrSize; ++i) 3488 AsmStr += (char)Record[2+i]; 3489 for (unsigned i = 0; i != ConstStrSize; ++i) 3490 ConstrStr += (char)Record[3+AsmStrSize+i]; 3491 UpgradeInlineAsmString(&AsmStr); 3492 if (!CurElemTy) 3493 return error("Missing element type for old-style inlineasm"); 3494 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, 3495 HasSideEffects, IsAlignStack, 3496 InlineAsm::AsmDialect(AsmDialect)); 3497 break; 3498 } 3499 // This version adds support for the unwind keyword. 3500 case bitc::CST_CODE_INLINEASM_OLD3: { 3501 if (Record.size() < 2) 3502 return error("Invalid inlineasm record"); 3503 unsigned OpNum = 0; 3504 std::string AsmStr, ConstrStr; 3505 bool HasSideEffects = Record[OpNum] & 1; 3506 bool IsAlignStack = (Record[OpNum] >> 1) & 1; 3507 unsigned AsmDialect = (Record[OpNum] >> 2) & 1; 3508 bool CanThrow = (Record[OpNum] >> 3) & 1; 3509 ++OpNum; 3510 unsigned AsmStrSize = Record[OpNum]; 3511 ++OpNum; 3512 if (OpNum + AsmStrSize >= Record.size()) 3513 return error("Invalid inlineasm record"); 3514 unsigned ConstStrSize = Record[OpNum + AsmStrSize]; 3515 if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size()) 3516 return error("Invalid inlineasm record"); 3517 3518 for (unsigned i = 0; i != AsmStrSize; ++i) 3519 AsmStr += (char)Record[OpNum + i]; 3520 ++OpNum; 3521 for (unsigned i = 0; i != ConstStrSize; ++i) 3522 ConstrStr += (char)Record[OpNum + AsmStrSize + i]; 3523 UpgradeInlineAsmString(&AsmStr); 3524 if (!CurElemTy) 3525 return error("Missing element type for old-style inlineasm"); 3526 V = InlineAsm::get(cast<FunctionType>(CurElemTy), AsmStr, ConstrStr, 3527 HasSideEffects, IsAlignStack, 3528 InlineAsm::AsmDialect(AsmDialect), CanThrow); 3529 break; 3530 } 3531 // This version adds explicit function type. 3532 case bitc::CST_CODE_INLINEASM: { 3533 if (Record.size() < 3) 3534 return error("Invalid inlineasm record"); 3535 unsigned OpNum = 0; 3536 auto *FnTy = dyn_cast_or_null<FunctionType>(getTypeByID(Record[OpNum])); 3537 ++OpNum; 3538 if (!FnTy) 3539 return error("Invalid inlineasm record"); 3540 std::string AsmStr, ConstrStr; 3541 bool HasSideEffects = Record[OpNum] & 1; 3542 bool IsAlignStack = (Record[OpNum] >> 1) & 1; 3543 unsigned AsmDialect = (Record[OpNum] >> 2) & 1; 3544 bool CanThrow = (Record[OpNum] >> 3) & 1; 3545 ++OpNum; 3546 unsigned AsmStrSize = Record[OpNum]; 3547 ++OpNum; 3548 if (OpNum + AsmStrSize >= Record.size()) 3549 return error("Invalid inlineasm record"); 3550 unsigned ConstStrSize = Record[OpNum + AsmStrSize]; 3551 if (OpNum + 1 + AsmStrSize + ConstStrSize > Record.size()) 3552 return error("Invalid inlineasm record"); 3553 3554 for (unsigned i = 0; i != AsmStrSize; ++i) 3555 AsmStr += (char)Record[OpNum + i]; 3556 ++OpNum; 3557 for (unsigned i = 0; i != ConstStrSize; ++i) 3558 ConstrStr += (char)Record[OpNum + AsmStrSize + i]; 3559 UpgradeInlineAsmString(&AsmStr); 3560 V = InlineAsm::get(FnTy, AsmStr, ConstrStr, HasSideEffects, IsAlignStack, 3561 InlineAsm::AsmDialect(AsmDialect), CanThrow); 3562 break; 3563 } 3564 case bitc::CST_CODE_BLOCKADDRESS:{ 3565 if (Record.size() < 3) 3566 return error("Invalid blockaddress record"); 3567 unsigned FnTyID = Record[0]; 3568 Type *FnTy = getTypeByID(FnTyID); 3569 if (!FnTy) 3570 return error("Invalid blockaddress record"); 3571 V = BitcodeConstant::create( 3572 Alloc, CurTy, 3573 {BitcodeConstant::BlockAddressOpcode, 0, (unsigned)Record[2]}, 3574 Record[1]); 3575 break; 3576 } 3577 case bitc::CST_CODE_DSO_LOCAL_EQUIVALENT: { 3578 if (Record.size() < 2) 3579 return error("Invalid dso_local record"); 3580 unsigned GVTyID = Record[0]; 3581 Type *GVTy = getTypeByID(GVTyID); 3582 if (!GVTy) 3583 return error("Invalid dso_local record"); 3584 V = BitcodeConstant::create( 3585 Alloc, CurTy, BitcodeConstant::DSOLocalEquivalentOpcode, Record[1]); 3586 break; 3587 } 3588 case bitc::CST_CODE_NO_CFI_VALUE: { 3589 if (Record.size() < 2) 3590 return error("Invalid no_cfi record"); 3591 unsigned GVTyID = Record[0]; 3592 Type *GVTy = getTypeByID(GVTyID); 3593 if (!GVTy) 3594 return error("Invalid no_cfi record"); 3595 V = BitcodeConstant::create(Alloc, CurTy, BitcodeConstant::NoCFIOpcode, 3596 Record[1]); 3597 break; 3598 } 3599 } 3600 3601 assert(V->getType() == getTypeByID(CurTyID) && "Incorrect result type ID"); 3602 if (Error Err = ValueList.assignValue(NextCstNo, V, CurTyID)) 3603 return Err; 3604 ++NextCstNo; 3605 } 3606 } 3607 3608 Error BitcodeReader::parseUseLists() { 3609 if (Error Err = Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) 3610 return Err; 3611 3612 // Read all the records. 3613 SmallVector<uint64_t, 64> Record; 3614 3615 while (true) { 3616 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 3617 if (!MaybeEntry) 3618 return MaybeEntry.takeError(); 3619 BitstreamEntry Entry = MaybeEntry.get(); 3620 3621 switch (Entry.Kind) { 3622 case BitstreamEntry::SubBlock: // Handled for us already. 3623 case BitstreamEntry::Error: 3624 return error("Malformed block"); 3625 case BitstreamEntry::EndBlock: 3626 return Error::success(); 3627 case BitstreamEntry::Record: 3628 // The interesting case. 3629 break; 3630 } 3631 3632 // Read a use list record. 3633 Record.clear(); 3634 bool IsBB = false; 3635 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 3636 if (!MaybeRecord) 3637 return MaybeRecord.takeError(); 3638 switch (MaybeRecord.get()) { 3639 default: // Default behavior: unknown type. 3640 break; 3641 case bitc::USELIST_CODE_BB: 3642 IsBB = true; 3643 [[fallthrough]]; 3644 case bitc::USELIST_CODE_DEFAULT: { 3645 unsigned RecordLength = Record.size(); 3646 if (RecordLength < 3) 3647 // Records should have at least an ID and two indexes. 3648 return error("Invalid record"); 3649 unsigned ID = Record.pop_back_val(); 3650 3651 Value *V; 3652 if (IsBB) { 3653 assert(ID < FunctionBBs.size() && "Basic block not found"); 3654 V = FunctionBBs[ID]; 3655 } else 3656 V = ValueList[ID]; 3657 unsigned NumUses = 0; 3658 SmallDenseMap<const Use *, unsigned, 16> Order; 3659 for (const Use &U : V->materialized_uses()) { 3660 if (++NumUses > Record.size()) 3661 break; 3662 Order[&U] = Record[NumUses - 1]; 3663 } 3664 if (Order.size() != Record.size() || NumUses > Record.size()) 3665 // Mismatches can happen if the functions are being materialized lazily 3666 // (out-of-order), or a value has been upgraded. 3667 break; 3668 3669 V->sortUseList([&](const Use &L, const Use &R) { 3670 return Order.lookup(&L) < Order.lookup(&R); 3671 }); 3672 break; 3673 } 3674 } 3675 } 3676 } 3677 3678 /// When we see the block for metadata, remember where it is and then skip it. 3679 /// This lets us lazily deserialize the metadata. 3680 Error BitcodeReader::rememberAndSkipMetadata() { 3681 // Save the current stream state. 3682 uint64_t CurBit = Stream.GetCurrentBitNo(); 3683 DeferredMetadataInfo.push_back(CurBit); 3684 3685 // Skip over the block for now. 3686 if (Error Err = Stream.SkipBlock()) 3687 return Err; 3688 return Error::success(); 3689 } 3690 3691 Error BitcodeReader::materializeMetadata() { 3692 for (uint64_t BitPos : DeferredMetadataInfo) { 3693 // Move the bit stream to the saved position. 3694 if (Error JumpFailed = Stream.JumpToBit(BitPos)) 3695 return JumpFailed; 3696 if (Error Err = MDLoader->parseModuleMetadata()) 3697 return Err; 3698 } 3699 3700 // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level 3701 // metadata. Only upgrade if the new option doesn't exist to avoid upgrade 3702 // multiple times. 3703 if (!TheModule->getNamedMetadata("llvm.linker.options")) { 3704 if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) { 3705 NamedMDNode *LinkerOpts = 3706 TheModule->getOrInsertNamedMetadata("llvm.linker.options"); 3707 for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands()) 3708 LinkerOpts->addOperand(cast<MDNode>(MDOptions)); 3709 } 3710 } 3711 3712 DeferredMetadataInfo.clear(); 3713 return Error::success(); 3714 } 3715 3716 void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } 3717 3718 /// When we see the block for a function body, remember where it is and then 3719 /// skip it. This lets us lazily deserialize the functions. 3720 Error BitcodeReader::rememberAndSkipFunctionBody() { 3721 // Get the function we are talking about. 3722 if (FunctionsWithBodies.empty()) 3723 return error("Insufficient function protos"); 3724 3725 Function *Fn = FunctionsWithBodies.back(); 3726 FunctionsWithBodies.pop_back(); 3727 3728 // Save the current stream state. 3729 uint64_t CurBit = Stream.GetCurrentBitNo(); 3730 assert( 3731 (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && 3732 "Mismatch between VST and scanned function offsets"); 3733 DeferredFunctionInfo[Fn] = CurBit; 3734 3735 // Skip over the function block for now. 3736 if (Error Err = Stream.SkipBlock()) 3737 return Err; 3738 return Error::success(); 3739 } 3740 3741 Error BitcodeReader::globalCleanup() { 3742 // Patch the initializers for globals and aliases up. 3743 if (Error Err = resolveGlobalAndIndirectSymbolInits()) 3744 return Err; 3745 if (!GlobalInits.empty() || !IndirectSymbolInits.empty()) 3746 return error("Malformed global initializer set"); 3747 3748 // Look for intrinsic functions which need to be upgraded at some point 3749 // and functions that need to have their function attributes upgraded. 3750 for (Function &F : *TheModule) { 3751 MDLoader->upgradeDebugIntrinsics(F); 3752 Function *NewFn; 3753 if (UpgradeIntrinsicFunction(&F, NewFn)) 3754 UpgradedIntrinsics[&F] = NewFn; 3755 // Look for functions that rely on old function attribute behavior. 3756 UpgradeFunctionAttributes(F); 3757 } 3758 3759 // Look for global variables which need to be renamed. 3760 std::vector<std::pair<GlobalVariable *, GlobalVariable *>> UpgradedVariables; 3761 for (GlobalVariable &GV : TheModule->globals()) 3762 if (GlobalVariable *Upgraded = UpgradeGlobalVariable(&GV)) 3763 UpgradedVariables.emplace_back(&GV, Upgraded); 3764 for (auto &Pair : UpgradedVariables) { 3765 Pair.first->eraseFromParent(); 3766 TheModule->insertGlobalVariable(Pair.second); 3767 } 3768 3769 // Force deallocation of memory for these vectors to favor the client that 3770 // want lazy deserialization. 3771 std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits); 3772 std::vector<std::pair<GlobalValue *, unsigned>>().swap(IndirectSymbolInits); 3773 return Error::success(); 3774 } 3775 3776 /// Support for lazy parsing of function bodies. This is required if we 3777 /// either have an old bitcode file without a VST forward declaration record, 3778 /// or if we have an anonymous function being materialized, since anonymous 3779 /// functions do not have a name and are therefore not in the VST. 3780 Error BitcodeReader::rememberAndSkipFunctionBodies() { 3781 if (Error JumpFailed = Stream.JumpToBit(NextUnreadBit)) 3782 return JumpFailed; 3783 3784 if (Stream.AtEndOfStream()) 3785 return error("Could not find function in stream"); 3786 3787 if (!SeenFirstFunctionBody) 3788 return error("Trying to materialize functions before seeing function blocks"); 3789 3790 // An old bitcode file with the symbol table at the end would have 3791 // finished the parse greedily. 3792 assert(SeenValueSymbolTable); 3793 3794 SmallVector<uint64_t, 64> Record; 3795 3796 while (true) { 3797 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 3798 if (!MaybeEntry) 3799 return MaybeEntry.takeError(); 3800 llvm::BitstreamEntry Entry = MaybeEntry.get(); 3801 3802 switch (Entry.Kind) { 3803 default: 3804 return error("Expect SubBlock"); 3805 case BitstreamEntry::SubBlock: 3806 switch (Entry.ID) { 3807 default: 3808 return error("Expect function block"); 3809 case bitc::FUNCTION_BLOCK_ID: 3810 if (Error Err = rememberAndSkipFunctionBody()) 3811 return Err; 3812 NextUnreadBit = Stream.GetCurrentBitNo(); 3813 return Error::success(); 3814 } 3815 } 3816 } 3817 } 3818 3819 Error BitcodeReaderBase::readBlockInfo() { 3820 Expected<std::optional<BitstreamBlockInfo>> MaybeNewBlockInfo = 3821 Stream.ReadBlockInfoBlock(); 3822 if (!MaybeNewBlockInfo) 3823 return MaybeNewBlockInfo.takeError(); 3824 std::optional<BitstreamBlockInfo> NewBlockInfo = 3825 std::move(MaybeNewBlockInfo.get()); 3826 if (!NewBlockInfo) 3827 return error("Malformed block"); 3828 BlockInfo = std::move(*NewBlockInfo); 3829 return Error::success(); 3830 } 3831 3832 Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) { 3833 // v1: [selection_kind, name] 3834 // v2: [strtab_offset, strtab_size, selection_kind] 3835 StringRef Name; 3836 std::tie(Name, Record) = readNameFromStrtab(Record); 3837 3838 if (Record.empty()) 3839 return error("Invalid record"); 3840 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); 3841 std::string OldFormatName; 3842 if (!UseStrtab) { 3843 if (Record.size() < 2) 3844 return error("Invalid record"); 3845 unsigned ComdatNameSize = Record[1]; 3846 if (ComdatNameSize > Record.size() - 2) 3847 return error("Comdat name size too large"); 3848 OldFormatName.reserve(ComdatNameSize); 3849 for (unsigned i = 0; i != ComdatNameSize; ++i) 3850 OldFormatName += (char)Record[2 + i]; 3851 Name = OldFormatName; 3852 } 3853 Comdat *C = TheModule->getOrInsertComdat(Name); 3854 C->setSelectionKind(SK); 3855 ComdatList.push_back(C); 3856 return Error::success(); 3857 } 3858 3859 static void inferDSOLocal(GlobalValue *GV) { 3860 // infer dso_local from linkage and visibility if it is not encoded. 3861 if (GV->hasLocalLinkage() || 3862 (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())) 3863 GV->setDSOLocal(true); 3864 } 3865 3866 GlobalValue::SanitizerMetadata deserializeSanitizerMetadata(unsigned V) { 3867 GlobalValue::SanitizerMetadata Meta; 3868 if (V & (1 << 0)) 3869 Meta.NoAddress = true; 3870 if (V & (1 << 1)) 3871 Meta.NoHWAddress = true; 3872 if (V & (1 << 2)) 3873 Meta.Memtag = true; 3874 if (V & (1 << 3)) 3875 Meta.IsDynInit = true; 3876 return Meta; 3877 } 3878 3879 Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { 3880 // v1: [pointer type, isconst, initid, linkage, alignment, section, 3881 // visibility, threadlocal, unnamed_addr, externally_initialized, 3882 // dllstorageclass, comdat, attributes, preemption specifier, 3883 // partition strtab offset, partition strtab size] (name in VST) 3884 // v2: [strtab_offset, strtab_size, v1] 3885 // v3: [v2, code_model] 3886 StringRef Name; 3887 std::tie(Name, Record) = readNameFromStrtab(Record); 3888 3889 if (Record.size() < 6) 3890 return error("Invalid record"); 3891 unsigned TyID = Record[0]; 3892 Type *Ty = getTypeByID(TyID); 3893 if (!Ty) 3894 return error("Invalid record"); 3895 bool isConstant = Record[1] & 1; 3896 bool explicitType = Record[1] & 2; 3897 unsigned AddressSpace; 3898 if (explicitType) { 3899 AddressSpace = Record[1] >> 2; 3900 } else { 3901 if (!Ty->isPointerTy()) 3902 return error("Invalid type for value"); 3903 AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); 3904 TyID = getContainedTypeID(TyID); 3905 Ty = getTypeByID(TyID); 3906 if (!Ty) 3907 return error("Missing element type for old-style global"); 3908 } 3909 3910 uint64_t RawLinkage = Record[3]; 3911 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 3912 MaybeAlign Alignment; 3913 if (Error Err = parseAlignmentValue(Record[4], Alignment)) 3914 return Err; 3915 std::string Section; 3916 if (Record[5]) { 3917 if (Record[5] - 1 >= SectionTable.size()) 3918 return error("Invalid ID"); 3919 Section = SectionTable[Record[5] - 1]; 3920 } 3921 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; 3922 // Local linkage must have default visibility. 3923 // auto-upgrade `hidden` and `protected` for old bitcode. 3924 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) 3925 Visibility = getDecodedVisibility(Record[6]); 3926 3927 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; 3928 if (Record.size() > 7) 3929 TLM = getDecodedThreadLocalMode(Record[7]); 3930 3931 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 3932 if (Record.size() > 8) 3933 UnnamedAddr = getDecodedUnnamedAddrType(Record[8]); 3934 3935 bool ExternallyInitialized = false; 3936 if (Record.size() > 9) 3937 ExternallyInitialized = Record[9]; 3938 3939 GlobalVariable *NewGV = 3940 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name, 3941 nullptr, TLM, AddressSpace, ExternallyInitialized); 3942 if (Alignment) 3943 NewGV->setAlignment(*Alignment); 3944 if (!Section.empty()) 3945 NewGV->setSection(Section); 3946 NewGV->setVisibility(Visibility); 3947 NewGV->setUnnamedAddr(UnnamedAddr); 3948 3949 if (Record.size() > 10) { 3950 // A GlobalValue with local linkage cannot have a DLL storage class. 3951 if (!NewGV->hasLocalLinkage()) { 3952 NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); 3953 } 3954 } else { 3955 upgradeDLLImportExportLinkage(NewGV, RawLinkage); 3956 } 3957 3958 ValueList.push_back(NewGV, getVirtualTypeID(NewGV->getType(), TyID)); 3959 3960 // Remember which value to use for the global initializer. 3961 if (unsigned InitID = Record[2]) 3962 GlobalInits.push_back(std::make_pair(NewGV, InitID - 1)); 3963 3964 if (Record.size() > 11) { 3965 if (unsigned ComdatID = Record[11]) { 3966 if (ComdatID > ComdatList.size()) 3967 return error("Invalid global variable comdat ID"); 3968 NewGV->setComdat(ComdatList[ComdatID - 1]); 3969 } 3970 } else if (hasImplicitComdat(RawLinkage)) { 3971 ImplicitComdatObjects.insert(NewGV); 3972 } 3973 3974 if (Record.size() > 12) { 3975 auto AS = getAttributes(Record[12]).getFnAttrs(); 3976 NewGV->setAttributes(AS); 3977 } 3978 3979 if (Record.size() > 13) { 3980 NewGV->setDSOLocal(getDecodedDSOLocal(Record[13])); 3981 } 3982 inferDSOLocal(NewGV); 3983 3984 // Check whether we have enough values to read a partition name. 3985 if (Record.size() > 15) 3986 NewGV->setPartition(StringRef(Strtab.data() + Record[14], Record[15])); 3987 3988 if (Record.size() > 16 && Record[16]) { 3989 llvm::GlobalValue::SanitizerMetadata Meta = 3990 deserializeSanitizerMetadata(Record[16]); 3991 NewGV->setSanitizerMetadata(Meta); 3992 } 3993 3994 if (Record.size() > 17 && Record[17]) { 3995 if (auto CM = getDecodedCodeModel(Record[17])) 3996 NewGV->setCodeModel(*CM); 3997 else 3998 return error("Invalid global variable code model"); 3999 } 4000 4001 return Error::success(); 4002 } 4003 4004 void BitcodeReader::callValueTypeCallback(Value *F, unsigned TypeID) { 4005 if (ValueTypeCallback) { 4006 (*ValueTypeCallback)( 4007 F, TypeID, [this](unsigned I) { return getTypeByID(I); }, 4008 [this](unsigned I, unsigned J) { return getContainedTypeID(I, J); }); 4009 } 4010 } 4011 4012 Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) { 4013 // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section, 4014 // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat, 4015 // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST) 4016 // v2: [strtab_offset, strtab_size, v1] 4017 StringRef Name; 4018 std::tie(Name, Record) = readNameFromStrtab(Record); 4019 4020 if (Record.size() < 8) 4021 return error("Invalid record"); 4022 unsigned FTyID = Record[0]; 4023 Type *FTy = getTypeByID(FTyID); 4024 if (!FTy) 4025 return error("Invalid record"); 4026 if (isa<PointerType>(FTy)) { 4027 FTyID = getContainedTypeID(FTyID, 0); 4028 FTy = getTypeByID(FTyID); 4029 if (!FTy) 4030 return error("Missing element type for old-style function"); 4031 } 4032 4033 if (!isa<FunctionType>(FTy)) 4034 return error("Invalid type for value"); 4035 auto CC = static_cast<CallingConv::ID>(Record[1]); 4036 if (CC & ~CallingConv::MaxID) 4037 return error("Invalid calling convention ID"); 4038 4039 unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace(); 4040 if (Record.size() > 16) 4041 AddrSpace = Record[16]; 4042 4043 Function *Func = 4044 Function::Create(cast<FunctionType>(FTy), GlobalValue::ExternalLinkage, 4045 AddrSpace, Name, TheModule); 4046 4047 assert(Func->getFunctionType() == FTy && 4048 "Incorrect fully specified type provided for function"); 4049 FunctionTypeIDs[Func] = FTyID; 4050 4051 Func->setCallingConv(CC); 4052 bool isProto = Record[2]; 4053 uint64_t RawLinkage = Record[3]; 4054 Func->setLinkage(getDecodedLinkage(RawLinkage)); 4055 Func->setAttributes(getAttributes(Record[4])); 4056 callValueTypeCallback(Func, FTyID); 4057 4058 // Upgrade any old-style byval or sret without a type by propagating the 4059 // argument's pointee type. There should be no opaque pointers where the byval 4060 // type is implicit. 4061 for (unsigned i = 0; i != Func->arg_size(); ++i) { 4062 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, 4063 Attribute::InAlloca}) { 4064 if (!Func->hasParamAttribute(i, Kind)) 4065 continue; 4066 4067 if (Func->getParamAttribute(i, Kind).getValueAsType()) 4068 continue; 4069 4070 Func->removeParamAttr(i, Kind); 4071 4072 unsigned ParamTypeID = getContainedTypeID(FTyID, i + 1); 4073 Type *PtrEltTy = getPtrElementTypeByID(ParamTypeID); 4074 if (!PtrEltTy) 4075 return error("Missing param element type for attribute upgrade"); 4076 4077 Attribute NewAttr; 4078 switch (Kind) { 4079 case Attribute::ByVal: 4080 NewAttr = Attribute::getWithByValType(Context, PtrEltTy); 4081 break; 4082 case Attribute::StructRet: 4083 NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); 4084 break; 4085 case Attribute::InAlloca: 4086 NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); 4087 break; 4088 default: 4089 llvm_unreachable("not an upgraded type attribute"); 4090 } 4091 4092 Func->addParamAttr(i, NewAttr); 4093 } 4094 } 4095 4096 if (Func->getCallingConv() == CallingConv::X86_INTR && 4097 !Func->arg_empty() && !Func->hasParamAttribute(0, Attribute::ByVal)) { 4098 unsigned ParamTypeID = getContainedTypeID(FTyID, 1); 4099 Type *ByValTy = getPtrElementTypeByID(ParamTypeID); 4100 if (!ByValTy) 4101 return error("Missing param element type for x86_intrcc upgrade"); 4102 Attribute NewAttr = Attribute::getWithByValType(Context, ByValTy); 4103 Func->addParamAttr(0, NewAttr); 4104 } 4105 4106 MaybeAlign Alignment; 4107 if (Error Err = parseAlignmentValue(Record[5], Alignment)) 4108 return Err; 4109 if (Alignment) 4110 Func->setAlignment(*Alignment); 4111 if (Record[6]) { 4112 if (Record[6] - 1 >= SectionTable.size()) 4113 return error("Invalid ID"); 4114 Func->setSection(SectionTable[Record[6] - 1]); 4115 } 4116 // Local linkage must have default visibility. 4117 // auto-upgrade `hidden` and `protected` for old bitcode. 4118 if (!Func->hasLocalLinkage()) 4119 Func->setVisibility(getDecodedVisibility(Record[7])); 4120 if (Record.size() > 8 && Record[8]) { 4121 if (Record[8] - 1 >= GCTable.size()) 4122 return error("Invalid ID"); 4123 Func->setGC(GCTable[Record[8] - 1]); 4124 } 4125 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 4126 if (Record.size() > 9) 4127 UnnamedAddr = getDecodedUnnamedAddrType(Record[9]); 4128 Func->setUnnamedAddr(UnnamedAddr); 4129 4130 FunctionOperandInfo OperandInfo = {Func, 0, 0, 0}; 4131 if (Record.size() > 10) 4132 OperandInfo.Prologue = Record[10]; 4133 4134 if (Record.size() > 11) { 4135 // A GlobalValue with local linkage cannot have a DLL storage class. 4136 if (!Func->hasLocalLinkage()) { 4137 Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); 4138 } 4139 } else { 4140 upgradeDLLImportExportLinkage(Func, RawLinkage); 4141 } 4142 4143 if (Record.size() > 12) { 4144 if (unsigned ComdatID = Record[12]) { 4145 if (ComdatID > ComdatList.size()) 4146 return error("Invalid function comdat ID"); 4147 Func->setComdat(ComdatList[ComdatID - 1]); 4148 } 4149 } else if (hasImplicitComdat(RawLinkage)) { 4150 ImplicitComdatObjects.insert(Func); 4151 } 4152 4153 if (Record.size() > 13) 4154 OperandInfo.Prefix = Record[13]; 4155 4156 if (Record.size() > 14) 4157 OperandInfo.PersonalityFn = Record[14]; 4158 4159 if (Record.size() > 15) { 4160 Func->setDSOLocal(getDecodedDSOLocal(Record[15])); 4161 } 4162 inferDSOLocal(Func); 4163 4164 // Record[16] is the address space number. 4165 4166 // Check whether we have enough values to read a partition name. Also make 4167 // sure Strtab has enough values. 4168 if (Record.size() > 18 && Strtab.data() && 4169 Record[17] + Record[18] <= Strtab.size()) { 4170 Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18])); 4171 } 4172 4173 ValueList.push_back(Func, getVirtualTypeID(Func->getType(), FTyID)); 4174 4175 if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue) 4176 FunctionOperands.push_back(OperandInfo); 4177 4178 // If this is a function with a body, remember the prototype we are 4179 // creating now, so that we can match up the body with them later. 4180 if (!isProto) { 4181 Func->setIsMaterializable(true); 4182 FunctionsWithBodies.push_back(Func); 4183 DeferredFunctionInfo[Func] = 0; 4184 } 4185 return Error::success(); 4186 } 4187 4188 Error BitcodeReader::parseGlobalIndirectSymbolRecord( 4189 unsigned BitCode, ArrayRef<uint64_t> Record) { 4190 // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST) 4191 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, 4192 // dllstorageclass, threadlocal, unnamed_addr, 4193 // preemption specifier] (name in VST) 4194 // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage, 4195 // visibility, dllstorageclass, threadlocal, unnamed_addr, 4196 // preemption specifier] (name in VST) 4197 // v2: [strtab_offset, strtab_size, v1] 4198 StringRef Name; 4199 std::tie(Name, Record) = readNameFromStrtab(Record); 4200 4201 bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD; 4202 if (Record.size() < (3 + (unsigned)NewRecord)) 4203 return error("Invalid record"); 4204 unsigned OpNum = 0; 4205 unsigned TypeID = Record[OpNum++]; 4206 Type *Ty = getTypeByID(TypeID); 4207 if (!Ty) 4208 return error("Invalid record"); 4209 4210 unsigned AddrSpace; 4211 if (!NewRecord) { 4212 auto *PTy = dyn_cast<PointerType>(Ty); 4213 if (!PTy) 4214 return error("Invalid type for value"); 4215 AddrSpace = PTy->getAddressSpace(); 4216 TypeID = getContainedTypeID(TypeID); 4217 Ty = getTypeByID(TypeID); 4218 if (!Ty) 4219 return error("Missing element type for old-style indirect symbol"); 4220 } else { 4221 AddrSpace = Record[OpNum++]; 4222 } 4223 4224 auto Val = Record[OpNum++]; 4225 auto Linkage = Record[OpNum++]; 4226 GlobalValue *NewGA; 4227 if (BitCode == bitc::MODULE_CODE_ALIAS || 4228 BitCode == bitc::MODULE_CODE_ALIAS_OLD) 4229 NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, 4230 TheModule); 4231 else 4232 NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name, 4233 nullptr, TheModule); 4234 4235 // Local linkage must have default visibility. 4236 // auto-upgrade `hidden` and `protected` for old bitcode. 4237 if (OpNum != Record.size()) { 4238 auto VisInd = OpNum++; 4239 if (!NewGA->hasLocalLinkage()) 4240 NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); 4241 } 4242 if (BitCode == bitc::MODULE_CODE_ALIAS || 4243 BitCode == bitc::MODULE_CODE_ALIAS_OLD) { 4244 if (OpNum != Record.size()) { 4245 auto S = Record[OpNum++]; 4246 // A GlobalValue with local linkage cannot have a DLL storage class. 4247 if (!NewGA->hasLocalLinkage()) 4248 NewGA->setDLLStorageClass(getDecodedDLLStorageClass(S)); 4249 } 4250 else 4251 upgradeDLLImportExportLinkage(NewGA, Linkage); 4252 if (OpNum != Record.size()) 4253 NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); 4254 if (OpNum != Record.size()) 4255 NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); 4256 } 4257 if (OpNum != Record.size()) 4258 NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++])); 4259 inferDSOLocal(NewGA); 4260 4261 // Check whether we have enough values to read a partition name. 4262 if (OpNum + 1 < Record.size()) { 4263 // Check Strtab has enough values for the partition. 4264 if (Record[OpNum] + Record[OpNum + 1] > Strtab.size()) 4265 return error("Malformed partition, too large."); 4266 NewGA->setPartition( 4267 StringRef(Strtab.data() + Record[OpNum], Record[OpNum + 1])); 4268 OpNum += 2; 4269 } 4270 4271 ValueList.push_back(NewGA, getVirtualTypeID(NewGA->getType(), TypeID)); 4272 IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); 4273 return Error::success(); 4274 } 4275 4276 Error BitcodeReader::parseModule(uint64_t ResumeBit, 4277 bool ShouldLazyLoadMetadata, 4278 ParserCallbacks Callbacks) { 4279 // Force the debug-info mode into the old format for now. 4280 // FIXME: Remove this once all tools support RemoveDIs. 4281 TheModule->IsNewDbgInfoFormat = false; 4282 4283 this->ValueTypeCallback = std::move(Callbacks.ValueType); 4284 if (ResumeBit) { 4285 if (Error JumpFailed = Stream.JumpToBit(ResumeBit)) 4286 return JumpFailed; 4287 } else if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 4288 return Err; 4289 4290 SmallVector<uint64_t, 64> Record; 4291 4292 // Parts of bitcode parsing depend on the datalayout. Make sure we 4293 // finalize the datalayout before we run any of that code. 4294 bool ResolvedDataLayout = false; 4295 // In order to support importing modules with illegal data layout strings, 4296 // delay parsing the data layout string until after upgrades and overrides 4297 // have been applied, allowing to fix illegal data layout strings. 4298 // Initialize to the current module's layout string in case none is specified. 4299 std::string TentativeDataLayoutStr = TheModule->getDataLayoutStr(); 4300 4301 auto ResolveDataLayout = [&]() -> Error { 4302 if (ResolvedDataLayout) 4303 return Error::success(); 4304 4305 // Datalayout and triple can't be parsed after this point. 4306 ResolvedDataLayout = true; 4307 4308 // Auto-upgrade the layout string 4309 TentativeDataLayoutStr = llvm::UpgradeDataLayoutString( 4310 TentativeDataLayoutStr, TheModule->getTargetTriple()); 4311 4312 // Apply override 4313 if (Callbacks.DataLayout) { 4314 if (auto LayoutOverride = (*Callbacks.DataLayout)( 4315 TheModule->getTargetTriple(), TentativeDataLayoutStr)) 4316 TentativeDataLayoutStr = *LayoutOverride; 4317 } 4318 4319 // Now the layout string is finalized in TentativeDataLayoutStr. Parse it. 4320 Expected<DataLayout> MaybeDL = DataLayout::parse(TentativeDataLayoutStr); 4321 if (!MaybeDL) 4322 return MaybeDL.takeError(); 4323 4324 TheModule->setDataLayout(MaybeDL.get()); 4325 return Error::success(); 4326 }; 4327 4328 // Read all the records for this module. 4329 while (true) { 4330 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4331 if (!MaybeEntry) 4332 return MaybeEntry.takeError(); 4333 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4334 4335 switch (Entry.Kind) { 4336 case BitstreamEntry::Error: 4337 return error("Malformed block"); 4338 case BitstreamEntry::EndBlock: 4339 if (Error Err = ResolveDataLayout()) 4340 return Err; 4341 return globalCleanup(); 4342 4343 case BitstreamEntry::SubBlock: 4344 switch (Entry.ID) { 4345 default: // Skip unknown content. 4346 if (Error Err = Stream.SkipBlock()) 4347 return Err; 4348 break; 4349 case bitc::BLOCKINFO_BLOCK_ID: 4350 if (Error Err = readBlockInfo()) 4351 return Err; 4352 break; 4353 case bitc::PARAMATTR_BLOCK_ID: 4354 if (Error Err = parseAttributeBlock()) 4355 return Err; 4356 break; 4357 case bitc::PARAMATTR_GROUP_BLOCK_ID: 4358 if (Error Err = parseAttributeGroupBlock()) 4359 return Err; 4360 break; 4361 case bitc::TYPE_BLOCK_ID_NEW: 4362 if (Error Err = parseTypeTable()) 4363 return Err; 4364 break; 4365 case bitc::VALUE_SYMTAB_BLOCK_ID: 4366 if (!SeenValueSymbolTable) { 4367 // Either this is an old form VST without function index and an 4368 // associated VST forward declaration record (which would have caused 4369 // the VST to be jumped to and parsed before it was encountered 4370 // normally in the stream), or there were no function blocks to 4371 // trigger an earlier parsing of the VST. 4372 assert(VSTOffset == 0 || FunctionsWithBodies.empty()); 4373 if (Error Err = parseValueSymbolTable()) 4374 return Err; 4375 SeenValueSymbolTable = true; 4376 } else { 4377 // We must have had a VST forward declaration record, which caused 4378 // the parser to jump to and parse the VST earlier. 4379 assert(VSTOffset > 0); 4380 if (Error Err = Stream.SkipBlock()) 4381 return Err; 4382 } 4383 break; 4384 case bitc::CONSTANTS_BLOCK_ID: 4385 if (Error Err = parseConstants()) 4386 return Err; 4387 if (Error Err = resolveGlobalAndIndirectSymbolInits()) 4388 return Err; 4389 break; 4390 case bitc::METADATA_BLOCK_ID: 4391 if (ShouldLazyLoadMetadata) { 4392 if (Error Err = rememberAndSkipMetadata()) 4393 return Err; 4394 break; 4395 } 4396 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata"); 4397 if (Error Err = MDLoader->parseModuleMetadata()) 4398 return Err; 4399 break; 4400 case bitc::METADATA_KIND_BLOCK_ID: 4401 if (Error Err = MDLoader->parseMetadataKinds()) 4402 return Err; 4403 break; 4404 case bitc::FUNCTION_BLOCK_ID: 4405 if (Error Err = ResolveDataLayout()) 4406 return Err; 4407 4408 // If this is the first function body we've seen, reverse the 4409 // FunctionsWithBodies list. 4410 if (!SeenFirstFunctionBody) { 4411 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); 4412 if (Error Err = globalCleanup()) 4413 return Err; 4414 SeenFirstFunctionBody = true; 4415 } 4416 4417 if (VSTOffset > 0) { 4418 // If we have a VST forward declaration record, make sure we 4419 // parse the VST now if we haven't already. It is needed to 4420 // set up the DeferredFunctionInfo vector for lazy reading. 4421 if (!SeenValueSymbolTable) { 4422 if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset)) 4423 return Err; 4424 SeenValueSymbolTable = true; 4425 // Fall through so that we record the NextUnreadBit below. 4426 // This is necessary in case we have an anonymous function that 4427 // is later materialized. Since it will not have a VST entry we 4428 // need to fall back to the lazy parse to find its offset. 4429 } else { 4430 // If we have a VST forward declaration record, but have already 4431 // parsed the VST (just above, when the first function body was 4432 // encountered here), then we are resuming the parse after 4433 // materializing functions. The ResumeBit points to the 4434 // start of the last function block recorded in the 4435 // DeferredFunctionInfo map. Skip it. 4436 if (Error Err = Stream.SkipBlock()) 4437 return Err; 4438 continue; 4439 } 4440 } 4441 4442 // Support older bitcode files that did not have the function 4443 // index in the VST, nor a VST forward declaration record, as 4444 // well as anonymous functions that do not have VST entries. 4445 // Build the DeferredFunctionInfo vector on the fly. 4446 if (Error Err = rememberAndSkipFunctionBody()) 4447 return Err; 4448 4449 // Suspend parsing when we reach the function bodies. Subsequent 4450 // materialization calls will resume it when necessary. If the bitcode 4451 // file is old, the symbol table will be at the end instead and will not 4452 // have been seen yet. In this case, just finish the parse now. 4453 if (SeenValueSymbolTable) { 4454 NextUnreadBit = Stream.GetCurrentBitNo(); 4455 // After the VST has been parsed, we need to make sure intrinsic name 4456 // are auto-upgraded. 4457 return globalCleanup(); 4458 } 4459 break; 4460 case bitc::USELIST_BLOCK_ID: 4461 if (Error Err = parseUseLists()) 4462 return Err; 4463 break; 4464 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: 4465 if (Error Err = parseOperandBundleTags()) 4466 return Err; 4467 break; 4468 case bitc::SYNC_SCOPE_NAMES_BLOCK_ID: 4469 if (Error Err = parseSyncScopeNames()) 4470 return Err; 4471 break; 4472 } 4473 continue; 4474 4475 case BitstreamEntry::Record: 4476 // The interesting case. 4477 break; 4478 } 4479 4480 // Read a record. 4481 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 4482 if (!MaybeBitCode) 4483 return MaybeBitCode.takeError(); 4484 switch (unsigned BitCode = MaybeBitCode.get()) { 4485 default: break; // Default behavior, ignore unknown content. 4486 case bitc::MODULE_CODE_VERSION: { 4487 Expected<unsigned> VersionOrErr = parseVersionRecord(Record); 4488 if (!VersionOrErr) 4489 return VersionOrErr.takeError(); 4490 UseRelativeIDs = *VersionOrErr >= 1; 4491 break; 4492 } 4493 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 4494 if (ResolvedDataLayout) 4495 return error("target triple too late in module"); 4496 std::string S; 4497 if (convertToString(Record, 0, S)) 4498 return error("Invalid record"); 4499 TheModule->setTargetTriple(S); 4500 break; 4501 } 4502 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] 4503 if (ResolvedDataLayout) 4504 return error("datalayout too late in module"); 4505 if (convertToString(Record, 0, TentativeDataLayoutStr)) 4506 return error("Invalid record"); 4507 break; 4508 } 4509 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] 4510 std::string S; 4511 if (convertToString(Record, 0, S)) 4512 return error("Invalid record"); 4513 TheModule->setModuleInlineAsm(S); 4514 break; 4515 } 4516 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] 4517 // Deprecated, but still needed to read old bitcode files. 4518 std::string S; 4519 if (convertToString(Record, 0, S)) 4520 return error("Invalid record"); 4521 // Ignore value. 4522 break; 4523 } 4524 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 4525 std::string S; 4526 if (convertToString(Record, 0, S)) 4527 return error("Invalid record"); 4528 SectionTable.push_back(S); 4529 break; 4530 } 4531 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] 4532 std::string S; 4533 if (convertToString(Record, 0, S)) 4534 return error("Invalid record"); 4535 GCTable.push_back(S); 4536 break; 4537 } 4538 case bitc::MODULE_CODE_COMDAT: 4539 if (Error Err = parseComdatRecord(Record)) 4540 return Err; 4541 break; 4542 // FIXME: BitcodeReader should handle {GLOBALVAR, FUNCTION, ALIAS, IFUNC} 4543 // written by ThinLinkBitcodeWriter. See 4544 // `ThinLinkBitcodeWriter::writeSimplifiedModuleInfo` for the format of each 4545 // record 4546 // (https://github.com/llvm/llvm-project/blob/b6a93967d9c11e79802b5e75cec1584d6c8aa472/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp#L4714) 4547 case bitc::MODULE_CODE_GLOBALVAR: 4548 if (Error Err = parseGlobalVarRecord(Record)) 4549 return Err; 4550 break; 4551 case bitc::MODULE_CODE_FUNCTION: 4552 if (Error Err = ResolveDataLayout()) 4553 return Err; 4554 if (Error Err = parseFunctionRecord(Record)) 4555 return Err; 4556 break; 4557 case bitc::MODULE_CODE_IFUNC: 4558 case bitc::MODULE_CODE_ALIAS: 4559 case bitc::MODULE_CODE_ALIAS_OLD: 4560 if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record)) 4561 return Err; 4562 break; 4563 /// MODULE_CODE_VSTOFFSET: [offset] 4564 case bitc::MODULE_CODE_VSTOFFSET: 4565 if (Record.empty()) 4566 return error("Invalid record"); 4567 // Note that we subtract 1 here because the offset is relative to one word 4568 // before the start of the identification or module block, which was 4569 // historically always the start of the regular bitcode header. 4570 VSTOffset = Record[0] - 1; 4571 break; 4572 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 4573 case bitc::MODULE_CODE_SOURCE_FILENAME: 4574 SmallString<128> ValueName; 4575 if (convertToString(Record, 0, ValueName)) 4576 return error("Invalid record"); 4577 TheModule->setSourceFileName(ValueName); 4578 break; 4579 } 4580 Record.clear(); 4581 } 4582 this->ValueTypeCallback = std::nullopt; 4583 return Error::success(); 4584 } 4585 4586 Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, 4587 bool IsImporting, 4588 ParserCallbacks Callbacks) { 4589 TheModule = M; 4590 MetadataLoaderCallbacks MDCallbacks; 4591 MDCallbacks.GetTypeByID = [&](unsigned ID) { return getTypeByID(ID); }; 4592 MDCallbacks.GetContainedTypeID = [&](unsigned I, unsigned J) { 4593 return getContainedTypeID(I, J); 4594 }; 4595 MDCallbacks.MDType = Callbacks.MDType; 4596 MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, MDCallbacks); 4597 return parseModule(0, ShouldLazyLoadMetadata, Callbacks); 4598 } 4599 4600 Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { 4601 if (!isa<PointerType>(PtrType)) 4602 return error("Load/Store operand is not a pointer type"); 4603 if (!PointerType::isLoadableOrStorableType(ValType)) 4604 return error("Cannot load/store from pointer"); 4605 return Error::success(); 4606 } 4607 4608 Error BitcodeReader::propagateAttributeTypes(CallBase *CB, 4609 ArrayRef<unsigned> ArgTyIDs) { 4610 AttributeList Attrs = CB->getAttributes(); 4611 for (unsigned i = 0; i != CB->arg_size(); ++i) { 4612 for (Attribute::AttrKind Kind : {Attribute::ByVal, Attribute::StructRet, 4613 Attribute::InAlloca}) { 4614 if (!Attrs.hasParamAttr(i, Kind) || 4615 Attrs.getParamAttr(i, Kind).getValueAsType()) 4616 continue; 4617 4618 Type *PtrEltTy = getPtrElementTypeByID(ArgTyIDs[i]); 4619 if (!PtrEltTy) 4620 return error("Missing element type for typed attribute upgrade"); 4621 4622 Attribute NewAttr; 4623 switch (Kind) { 4624 case Attribute::ByVal: 4625 NewAttr = Attribute::getWithByValType(Context, PtrEltTy); 4626 break; 4627 case Attribute::StructRet: 4628 NewAttr = Attribute::getWithStructRetType(Context, PtrEltTy); 4629 break; 4630 case Attribute::InAlloca: 4631 NewAttr = Attribute::getWithInAllocaType(Context, PtrEltTy); 4632 break; 4633 default: 4634 llvm_unreachable("not an upgraded type attribute"); 4635 } 4636 4637 Attrs = Attrs.addParamAttribute(Context, i, NewAttr); 4638 } 4639 } 4640 4641 if (CB->isInlineAsm()) { 4642 const InlineAsm *IA = cast<InlineAsm>(CB->getCalledOperand()); 4643 unsigned ArgNo = 0; 4644 for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) { 4645 if (!CI.hasArg()) 4646 continue; 4647 4648 if (CI.isIndirect && !Attrs.getParamElementType(ArgNo)) { 4649 Type *ElemTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]); 4650 if (!ElemTy) 4651 return error("Missing element type for inline asm upgrade"); 4652 Attrs = Attrs.addParamAttribute( 4653 Context, ArgNo, 4654 Attribute::get(Context, Attribute::ElementType, ElemTy)); 4655 } 4656 4657 ArgNo++; 4658 } 4659 } 4660 4661 switch (CB->getIntrinsicID()) { 4662 case Intrinsic::preserve_array_access_index: 4663 case Intrinsic::preserve_struct_access_index: 4664 case Intrinsic::aarch64_ldaxr: 4665 case Intrinsic::aarch64_ldxr: 4666 case Intrinsic::aarch64_stlxr: 4667 case Intrinsic::aarch64_stxr: 4668 case Intrinsic::arm_ldaex: 4669 case Intrinsic::arm_ldrex: 4670 case Intrinsic::arm_stlex: 4671 case Intrinsic::arm_strex: { 4672 unsigned ArgNo; 4673 switch (CB->getIntrinsicID()) { 4674 case Intrinsic::aarch64_stlxr: 4675 case Intrinsic::aarch64_stxr: 4676 case Intrinsic::arm_stlex: 4677 case Intrinsic::arm_strex: 4678 ArgNo = 1; 4679 break; 4680 default: 4681 ArgNo = 0; 4682 break; 4683 } 4684 if (!Attrs.getParamElementType(ArgNo)) { 4685 Type *ElTy = getPtrElementTypeByID(ArgTyIDs[ArgNo]); 4686 if (!ElTy) 4687 return error("Missing element type for elementtype upgrade"); 4688 Attribute NewAttr = Attribute::get(Context, Attribute::ElementType, ElTy); 4689 Attrs = Attrs.addParamAttribute(Context, ArgNo, NewAttr); 4690 } 4691 break; 4692 } 4693 default: 4694 break; 4695 } 4696 4697 CB->setAttributes(Attrs); 4698 return Error::success(); 4699 } 4700 4701 /// Lazily parse the specified function body block. 4702 Error BitcodeReader::parseFunctionBody(Function *F) { 4703 if (Error Err = Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) 4704 return Err; 4705 4706 // Unexpected unresolved metadata when parsing function. 4707 if (MDLoader->hasFwdRefs()) 4708 return error("Invalid function metadata: incoming forward references"); 4709 4710 InstructionList.clear(); 4711 unsigned ModuleValueListSize = ValueList.size(); 4712 unsigned ModuleMDLoaderSize = MDLoader->size(); 4713 4714 // Add all the function arguments to the value table. 4715 unsigned ArgNo = 0; 4716 unsigned FTyID = FunctionTypeIDs[F]; 4717 for (Argument &I : F->args()) { 4718 unsigned ArgTyID = getContainedTypeID(FTyID, ArgNo + 1); 4719 assert(I.getType() == getTypeByID(ArgTyID) && 4720 "Incorrect fully specified type for Function Argument"); 4721 ValueList.push_back(&I, ArgTyID); 4722 ++ArgNo; 4723 } 4724 unsigned NextValueNo = ValueList.size(); 4725 BasicBlock *CurBB = nullptr; 4726 unsigned CurBBNo = 0; 4727 // Block into which constant expressions from phi nodes are materialized. 4728 BasicBlock *PhiConstExprBB = nullptr; 4729 // Edge blocks for phi nodes into which constant expressions have been 4730 // expanded. 4731 SmallMapVector<std::pair<BasicBlock *, BasicBlock *>, BasicBlock *, 4> 4732 ConstExprEdgeBBs; 4733 4734 DebugLoc LastLoc; 4735 auto getLastInstruction = [&]() -> Instruction * { 4736 if (CurBB && !CurBB->empty()) 4737 return &CurBB->back(); 4738 else if (CurBBNo && FunctionBBs[CurBBNo - 1] && 4739 !FunctionBBs[CurBBNo - 1]->empty()) 4740 return &FunctionBBs[CurBBNo - 1]->back(); 4741 return nullptr; 4742 }; 4743 4744 std::vector<OperandBundleDef> OperandBundles; 4745 4746 // Read all the records. 4747 SmallVector<uint64_t, 64> Record; 4748 4749 while (true) { 4750 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4751 if (!MaybeEntry) 4752 return MaybeEntry.takeError(); 4753 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4754 4755 switch (Entry.Kind) { 4756 case BitstreamEntry::Error: 4757 return error("Malformed block"); 4758 case BitstreamEntry::EndBlock: 4759 goto OutOfRecordLoop; 4760 4761 case BitstreamEntry::SubBlock: 4762 switch (Entry.ID) { 4763 default: // Skip unknown content. 4764 if (Error Err = Stream.SkipBlock()) 4765 return Err; 4766 break; 4767 case bitc::CONSTANTS_BLOCK_ID: 4768 if (Error Err = parseConstants()) 4769 return Err; 4770 NextValueNo = ValueList.size(); 4771 break; 4772 case bitc::VALUE_SYMTAB_BLOCK_ID: 4773 if (Error Err = parseValueSymbolTable()) 4774 return Err; 4775 break; 4776 case bitc::METADATA_ATTACHMENT_ID: 4777 if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList)) 4778 return Err; 4779 break; 4780 case bitc::METADATA_BLOCK_ID: 4781 assert(DeferredMetadataInfo.empty() && 4782 "Must read all module-level metadata before function-level"); 4783 if (Error Err = MDLoader->parseFunctionMetadata()) 4784 return Err; 4785 break; 4786 case bitc::USELIST_BLOCK_ID: 4787 if (Error Err = parseUseLists()) 4788 return Err; 4789 break; 4790 } 4791 continue; 4792 4793 case BitstreamEntry::Record: 4794 // The interesting case. 4795 break; 4796 } 4797 4798 // Read a record. 4799 Record.clear(); 4800 Instruction *I = nullptr; 4801 unsigned ResTypeID = InvalidTypeID; 4802 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 4803 if (!MaybeBitCode) 4804 return MaybeBitCode.takeError(); 4805 switch (unsigned BitCode = MaybeBitCode.get()) { 4806 default: // Default behavior: reject 4807 return error("Invalid value"); 4808 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] 4809 if (Record.empty() || Record[0] == 0) 4810 return error("Invalid record"); 4811 // Create all the basic blocks for the function. 4812 FunctionBBs.resize(Record[0]); 4813 4814 // See if anything took the address of blocks in this function. 4815 auto BBFRI = BasicBlockFwdRefs.find(F); 4816 if (BBFRI == BasicBlockFwdRefs.end()) { 4817 for (BasicBlock *&BB : FunctionBBs) 4818 BB = BasicBlock::Create(Context, "", F); 4819 } else { 4820 auto &BBRefs = BBFRI->second; 4821 // Check for invalid basic block references. 4822 if (BBRefs.size() > FunctionBBs.size()) 4823 return error("Invalid ID"); 4824 assert(!BBRefs.empty() && "Unexpected empty array"); 4825 assert(!BBRefs.front() && "Invalid reference to entry block"); 4826 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; 4827 ++I) 4828 if (I < RE && BBRefs[I]) { 4829 BBRefs[I]->insertInto(F); 4830 FunctionBBs[I] = BBRefs[I]; 4831 } else { 4832 FunctionBBs[I] = BasicBlock::Create(Context, "", F); 4833 } 4834 4835 // Erase from the table. 4836 BasicBlockFwdRefs.erase(BBFRI); 4837 } 4838 4839 CurBB = FunctionBBs[0]; 4840 continue; 4841 } 4842 4843 case bitc::FUNC_CODE_BLOCKADDR_USERS: // BLOCKADDR_USERS: [vals...] 4844 // The record should not be emitted if it's an empty list. 4845 if (Record.empty()) 4846 return error("Invalid record"); 4847 // When we have the RARE case of a BlockAddress Constant that is not 4848 // scoped to the Function it refers to, we need to conservatively 4849 // materialize the referred to Function, regardless of whether or not 4850 // that Function will ultimately be linked, otherwise users of 4851 // BitcodeReader might start splicing out Function bodies such that we 4852 // might no longer be able to materialize the BlockAddress since the 4853 // BasicBlock (and entire body of the Function) the BlockAddress refers 4854 // to may have been moved. In the case that the user of BitcodeReader 4855 // decides ultimately not to link the Function body, materializing here 4856 // could be considered wasteful, but it's better than a deserialization 4857 // failure as described. This keeps BitcodeReader unaware of complex 4858 // linkage policy decisions such as those use by LTO, leaving those 4859 // decisions "one layer up." 4860 for (uint64_t ValID : Record) 4861 if (auto *F = dyn_cast<Function>(ValueList[ValID])) 4862 BackwardRefFunctions.push_back(F); 4863 else 4864 return error("Invalid record"); 4865 4866 continue; 4867 4868 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN 4869 // This record indicates that the last instruction is at the same 4870 // location as the previous instruction with a location. 4871 I = getLastInstruction(); 4872 4873 if (!I) 4874 return error("Invalid record"); 4875 I->setDebugLoc(LastLoc); 4876 I = nullptr; 4877 continue; 4878 4879 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] 4880 I = getLastInstruction(); 4881 if (!I || Record.size() < 4) 4882 return error("Invalid record"); 4883 4884 unsigned Line = Record[0], Col = Record[1]; 4885 unsigned ScopeID = Record[2], IAID = Record[3]; 4886 bool isImplicitCode = Record.size() == 5 && Record[4]; 4887 4888 MDNode *Scope = nullptr, *IA = nullptr; 4889 if (ScopeID) { 4890 Scope = dyn_cast_or_null<MDNode>( 4891 MDLoader->getMetadataFwdRefOrLoad(ScopeID - 1)); 4892 if (!Scope) 4893 return error("Invalid record"); 4894 } 4895 if (IAID) { 4896 IA = dyn_cast_or_null<MDNode>( 4897 MDLoader->getMetadataFwdRefOrLoad(IAID - 1)); 4898 if (!IA) 4899 return error("Invalid record"); 4900 } 4901 LastLoc = DILocation::get(Scope->getContext(), Line, Col, Scope, IA, 4902 isImplicitCode); 4903 I->setDebugLoc(LastLoc); 4904 I = nullptr; 4905 continue; 4906 } 4907 case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode] 4908 unsigned OpNum = 0; 4909 Value *LHS; 4910 unsigned TypeID; 4911 if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) || 4912 OpNum+1 > Record.size()) 4913 return error("Invalid record"); 4914 4915 int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType()); 4916 if (Opc == -1) 4917 return error("Invalid record"); 4918 I = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS); 4919 ResTypeID = TypeID; 4920 InstructionList.push_back(I); 4921 if (OpNum < Record.size()) { 4922 if (isa<FPMathOperator>(I)) { 4923 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 4924 if (FMF.any()) 4925 I->setFastMathFlags(FMF); 4926 } 4927 } 4928 break; 4929 } 4930 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] 4931 unsigned OpNum = 0; 4932 Value *LHS, *RHS; 4933 unsigned TypeID; 4934 if (getValueTypePair(Record, OpNum, NextValueNo, LHS, TypeID, CurBB) || 4935 popValue(Record, OpNum, NextValueNo, LHS->getType(), TypeID, RHS, 4936 CurBB) || 4937 OpNum+1 > Record.size()) 4938 return error("Invalid record"); 4939 4940 int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); 4941 if (Opc == -1) 4942 return error("Invalid record"); 4943 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 4944 ResTypeID = TypeID; 4945 InstructionList.push_back(I); 4946 if (OpNum < Record.size()) { 4947 if (Opc == Instruction::Add || 4948 Opc == Instruction::Sub || 4949 Opc == Instruction::Mul || 4950 Opc == Instruction::Shl) { 4951 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 4952 cast<BinaryOperator>(I)->setHasNoSignedWrap(true); 4953 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 4954 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); 4955 } else if (Opc == Instruction::SDiv || 4956 Opc == Instruction::UDiv || 4957 Opc == Instruction::LShr || 4958 Opc == Instruction::AShr) { 4959 if (Record[OpNum] & (1 << bitc::PEO_EXACT)) 4960 cast<BinaryOperator>(I)->setIsExact(true); 4961 } else if (Opc == Instruction::Or) { 4962 if (Record[OpNum] & (1 << bitc::PDI_DISJOINT)) 4963 cast<PossiblyDisjointInst>(I)->setIsDisjoint(true); 4964 } else if (isa<FPMathOperator>(I)) { 4965 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 4966 if (FMF.any()) 4967 I->setFastMathFlags(FMF); 4968 } 4969 } 4970 break; 4971 } 4972 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] 4973 unsigned OpNum = 0; 4974 Value *Op; 4975 unsigned OpTypeID; 4976 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || 4977 OpNum + 1 > Record.size()) 4978 return error("Invalid record"); 4979 4980 ResTypeID = Record[OpNum++]; 4981 Type *ResTy = getTypeByID(ResTypeID); 4982 int Opc = getDecodedCastOpcode(Record[OpNum++]); 4983 4984 if (Opc == -1 || !ResTy) 4985 return error("Invalid record"); 4986 Instruction *Temp = nullptr; 4987 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { 4988 if (Temp) { 4989 InstructionList.push_back(Temp); 4990 assert(CurBB && "No current BB?"); 4991 Temp->insertInto(CurBB, CurBB->end()); 4992 } 4993 } else { 4994 auto CastOp = (Instruction::CastOps)Opc; 4995 if (!CastInst::castIsValid(CastOp, Op, ResTy)) 4996 return error("Invalid cast"); 4997 I = CastInst::Create(CastOp, Op, ResTy); 4998 } 4999 if (OpNum < Record.size() && isa<PossiblyNonNegInst>(I) && 5000 (Record[OpNum] & (1 << bitc::PNNI_NON_NEG))) 5001 I->setNonNeg(true); 5002 InstructionList.push_back(I); 5003 break; 5004 } 5005 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: 5006 case bitc::FUNC_CODE_INST_GEP_OLD: 5007 case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] 5008 unsigned OpNum = 0; 5009 5010 unsigned TyID; 5011 Type *Ty; 5012 bool InBounds; 5013 5014 if (BitCode == bitc::FUNC_CODE_INST_GEP) { 5015 InBounds = Record[OpNum++]; 5016 TyID = Record[OpNum++]; 5017 Ty = getTypeByID(TyID); 5018 } else { 5019 InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; 5020 TyID = InvalidTypeID; 5021 Ty = nullptr; 5022 } 5023 5024 Value *BasePtr; 5025 unsigned BasePtrTypeID; 5026 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr, BasePtrTypeID, 5027 CurBB)) 5028 return error("Invalid record"); 5029 5030 if (!Ty) { 5031 TyID = getContainedTypeID(BasePtrTypeID); 5032 if (BasePtr->getType()->isVectorTy()) 5033 TyID = getContainedTypeID(TyID); 5034 Ty = getTypeByID(TyID); 5035 } 5036 5037 SmallVector<Value*, 16> GEPIdx; 5038 while (OpNum != Record.size()) { 5039 Value *Op; 5040 unsigned OpTypeID; 5041 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 5042 return error("Invalid record"); 5043 GEPIdx.push_back(Op); 5044 } 5045 5046 I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); 5047 5048 ResTypeID = TyID; 5049 if (cast<GEPOperator>(I)->getNumIndices() != 0) { 5050 auto GTI = std::next(gep_type_begin(I)); 5051 for (Value *Idx : drop_begin(cast<GEPOperator>(I)->indices())) { 5052 unsigned SubType = 0; 5053 if (GTI.isStruct()) { 5054 ConstantInt *IdxC = 5055 Idx->getType()->isVectorTy() 5056 ? cast<ConstantInt>(cast<Constant>(Idx)->getSplatValue()) 5057 : cast<ConstantInt>(Idx); 5058 SubType = IdxC->getZExtValue(); 5059 } 5060 ResTypeID = getContainedTypeID(ResTypeID, SubType); 5061 ++GTI; 5062 } 5063 } 5064 5065 // At this point ResTypeID is the result element type. We need a pointer 5066 // or vector of pointer to it. 5067 ResTypeID = getVirtualTypeID(I->getType()->getScalarType(), ResTypeID); 5068 if (I->getType()->isVectorTy()) 5069 ResTypeID = getVirtualTypeID(I->getType(), ResTypeID); 5070 5071 InstructionList.push_back(I); 5072 if (InBounds) 5073 cast<GetElementPtrInst>(I)->setIsInBounds(true); 5074 break; 5075 } 5076 5077 case bitc::FUNC_CODE_INST_EXTRACTVAL: { 5078 // EXTRACTVAL: [opty, opval, n x indices] 5079 unsigned OpNum = 0; 5080 Value *Agg; 5081 unsigned AggTypeID; 5082 if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID, CurBB)) 5083 return error("Invalid record"); 5084 Type *Ty = Agg->getType(); 5085 5086 unsigned RecSize = Record.size(); 5087 if (OpNum == RecSize) 5088 return error("EXTRACTVAL: Invalid instruction with 0 indices"); 5089 5090 SmallVector<unsigned, 4> EXTRACTVALIdx; 5091 ResTypeID = AggTypeID; 5092 for (; OpNum != RecSize; ++OpNum) { 5093 bool IsArray = Ty->isArrayTy(); 5094 bool IsStruct = Ty->isStructTy(); 5095 uint64_t Index = Record[OpNum]; 5096 5097 if (!IsStruct && !IsArray) 5098 return error("EXTRACTVAL: Invalid type"); 5099 if ((unsigned)Index != Index) 5100 return error("Invalid value"); 5101 if (IsStruct && Index >= Ty->getStructNumElements()) 5102 return error("EXTRACTVAL: Invalid struct index"); 5103 if (IsArray && Index >= Ty->getArrayNumElements()) 5104 return error("EXTRACTVAL: Invalid array index"); 5105 EXTRACTVALIdx.push_back((unsigned)Index); 5106 5107 if (IsStruct) { 5108 Ty = Ty->getStructElementType(Index); 5109 ResTypeID = getContainedTypeID(ResTypeID, Index); 5110 } else { 5111 Ty = Ty->getArrayElementType(); 5112 ResTypeID = getContainedTypeID(ResTypeID); 5113 } 5114 } 5115 5116 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); 5117 InstructionList.push_back(I); 5118 break; 5119 } 5120 5121 case bitc::FUNC_CODE_INST_INSERTVAL: { 5122 // INSERTVAL: [opty, opval, opty, opval, n x indices] 5123 unsigned OpNum = 0; 5124 Value *Agg; 5125 unsigned AggTypeID; 5126 if (getValueTypePair(Record, OpNum, NextValueNo, Agg, AggTypeID, CurBB)) 5127 return error("Invalid record"); 5128 Value *Val; 5129 unsigned ValTypeID; 5130 if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 5131 return error("Invalid record"); 5132 5133 unsigned RecSize = Record.size(); 5134 if (OpNum == RecSize) 5135 return error("INSERTVAL: Invalid instruction with 0 indices"); 5136 5137 SmallVector<unsigned, 4> INSERTVALIdx; 5138 Type *CurTy = Agg->getType(); 5139 for (; OpNum != RecSize; ++OpNum) { 5140 bool IsArray = CurTy->isArrayTy(); 5141 bool IsStruct = CurTy->isStructTy(); 5142 uint64_t Index = Record[OpNum]; 5143 5144 if (!IsStruct && !IsArray) 5145 return error("INSERTVAL: Invalid type"); 5146 if ((unsigned)Index != Index) 5147 return error("Invalid value"); 5148 if (IsStruct && Index >= CurTy->getStructNumElements()) 5149 return error("INSERTVAL: Invalid struct index"); 5150 if (IsArray && Index >= CurTy->getArrayNumElements()) 5151 return error("INSERTVAL: Invalid array index"); 5152 5153 INSERTVALIdx.push_back((unsigned)Index); 5154 if (IsStruct) 5155 CurTy = CurTy->getStructElementType(Index); 5156 else 5157 CurTy = CurTy->getArrayElementType(); 5158 } 5159 5160 if (CurTy != Val->getType()) 5161 return error("Inserted value type doesn't match aggregate type"); 5162 5163 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); 5164 ResTypeID = AggTypeID; 5165 InstructionList.push_back(I); 5166 break; 5167 } 5168 5169 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] 5170 // obsolete form of select 5171 // handles select i1 ... in old bitcode 5172 unsigned OpNum = 0; 5173 Value *TrueVal, *FalseVal, *Cond; 5174 unsigned TypeID; 5175 Type *CondType = Type::getInt1Ty(Context); 5176 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, TypeID, 5177 CurBB) || 5178 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), TypeID, 5179 FalseVal, CurBB) || 5180 popValue(Record, OpNum, NextValueNo, CondType, 5181 getVirtualTypeID(CondType), Cond, CurBB)) 5182 return error("Invalid record"); 5183 5184 I = SelectInst::Create(Cond, TrueVal, FalseVal); 5185 ResTypeID = TypeID; 5186 InstructionList.push_back(I); 5187 break; 5188 } 5189 5190 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] 5191 // new form of select 5192 // handles select i1 or select [N x i1] 5193 unsigned OpNum = 0; 5194 Value *TrueVal, *FalseVal, *Cond; 5195 unsigned ValTypeID, CondTypeID; 5196 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal, ValTypeID, 5197 CurBB) || 5198 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), ValTypeID, 5199 FalseVal, CurBB) || 5200 getValueTypePair(Record, OpNum, NextValueNo, Cond, CondTypeID, CurBB)) 5201 return error("Invalid record"); 5202 5203 // select condition can be either i1 or [N x i1] 5204 if (VectorType* vector_type = 5205 dyn_cast<VectorType>(Cond->getType())) { 5206 // expect <n x i1> 5207 if (vector_type->getElementType() != Type::getInt1Ty(Context)) 5208 return error("Invalid type for value"); 5209 } else { 5210 // expect i1 5211 if (Cond->getType() != Type::getInt1Ty(Context)) 5212 return error("Invalid type for value"); 5213 } 5214 5215 I = SelectInst::Create(Cond, TrueVal, FalseVal); 5216 ResTypeID = ValTypeID; 5217 InstructionList.push_back(I); 5218 if (OpNum < Record.size() && isa<FPMathOperator>(I)) { 5219 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 5220 if (FMF.any()) 5221 I->setFastMathFlags(FMF); 5222 } 5223 break; 5224 } 5225 5226 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] 5227 unsigned OpNum = 0; 5228 Value *Vec, *Idx; 5229 unsigned VecTypeID, IdxTypeID; 5230 if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID, CurBB) || 5231 getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID, CurBB)) 5232 return error("Invalid record"); 5233 if (!Vec->getType()->isVectorTy()) 5234 return error("Invalid type for value"); 5235 I = ExtractElementInst::Create(Vec, Idx); 5236 ResTypeID = getContainedTypeID(VecTypeID); 5237 InstructionList.push_back(I); 5238 break; 5239 } 5240 5241 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] 5242 unsigned OpNum = 0; 5243 Value *Vec, *Elt, *Idx; 5244 unsigned VecTypeID, IdxTypeID; 5245 if (getValueTypePair(Record, OpNum, NextValueNo, Vec, VecTypeID, CurBB)) 5246 return error("Invalid record"); 5247 if (!Vec->getType()->isVectorTy()) 5248 return error("Invalid type for value"); 5249 if (popValue(Record, OpNum, NextValueNo, 5250 cast<VectorType>(Vec->getType())->getElementType(), 5251 getContainedTypeID(VecTypeID), Elt, CurBB) || 5252 getValueTypePair(Record, OpNum, NextValueNo, Idx, IdxTypeID, CurBB)) 5253 return error("Invalid record"); 5254 I = InsertElementInst::Create(Vec, Elt, Idx); 5255 ResTypeID = VecTypeID; 5256 InstructionList.push_back(I); 5257 break; 5258 } 5259 5260 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] 5261 unsigned OpNum = 0; 5262 Value *Vec1, *Vec2, *Mask; 5263 unsigned Vec1TypeID; 5264 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1, Vec1TypeID, 5265 CurBB) || 5266 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec1TypeID, 5267 Vec2, CurBB)) 5268 return error("Invalid record"); 5269 5270 unsigned MaskTypeID; 5271 if (getValueTypePair(Record, OpNum, NextValueNo, Mask, MaskTypeID, CurBB)) 5272 return error("Invalid record"); 5273 if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) 5274 return error("Invalid type for value"); 5275 5276 I = new ShuffleVectorInst(Vec1, Vec2, Mask); 5277 ResTypeID = 5278 getVirtualTypeID(I->getType(), getContainedTypeID(Vec1TypeID)); 5279 InstructionList.push_back(I); 5280 break; 5281 } 5282 5283 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] 5284 // Old form of ICmp/FCmp returning bool 5285 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were 5286 // both legal on vectors but had different behaviour. 5287 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] 5288 // FCmp/ICmp returning bool or vector of bool 5289 5290 unsigned OpNum = 0; 5291 Value *LHS, *RHS; 5292 unsigned LHSTypeID; 5293 if (getValueTypePair(Record, OpNum, NextValueNo, LHS, LHSTypeID, CurBB) || 5294 popValue(Record, OpNum, NextValueNo, LHS->getType(), LHSTypeID, RHS, 5295 CurBB)) 5296 return error("Invalid record"); 5297 5298 if (OpNum >= Record.size()) 5299 return error( 5300 "Invalid record: operand number exceeded available operands"); 5301 5302 CmpInst::Predicate PredVal = CmpInst::Predicate(Record[OpNum]); 5303 bool IsFP = LHS->getType()->isFPOrFPVectorTy(); 5304 FastMathFlags FMF; 5305 if (IsFP && Record.size() > OpNum+1) 5306 FMF = getDecodedFastMathFlags(Record[++OpNum]); 5307 5308 if (OpNum+1 != Record.size()) 5309 return error("Invalid record"); 5310 5311 if (IsFP) { 5312 if (!CmpInst::isFPPredicate(PredVal)) 5313 return error("Invalid fcmp predicate"); 5314 I = new FCmpInst(PredVal, LHS, RHS); 5315 } else { 5316 if (!CmpInst::isIntPredicate(PredVal)) 5317 return error("Invalid icmp predicate"); 5318 I = new ICmpInst(PredVal, LHS, RHS); 5319 } 5320 5321 ResTypeID = getVirtualTypeID(I->getType()->getScalarType()); 5322 if (LHS->getType()->isVectorTy()) 5323 ResTypeID = getVirtualTypeID(I->getType(), ResTypeID); 5324 5325 if (FMF.any()) 5326 I->setFastMathFlags(FMF); 5327 InstructionList.push_back(I); 5328 break; 5329 } 5330 5331 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] 5332 { 5333 unsigned Size = Record.size(); 5334 if (Size == 0) { 5335 I = ReturnInst::Create(Context); 5336 InstructionList.push_back(I); 5337 break; 5338 } 5339 5340 unsigned OpNum = 0; 5341 Value *Op = nullptr; 5342 unsigned OpTypeID; 5343 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 5344 return error("Invalid record"); 5345 if (OpNum != Record.size()) 5346 return error("Invalid record"); 5347 5348 I = ReturnInst::Create(Context, Op); 5349 InstructionList.push_back(I); 5350 break; 5351 } 5352 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] 5353 if (Record.size() != 1 && Record.size() != 3) 5354 return error("Invalid record"); 5355 BasicBlock *TrueDest = getBasicBlock(Record[0]); 5356 if (!TrueDest) 5357 return error("Invalid record"); 5358 5359 if (Record.size() == 1) { 5360 I = BranchInst::Create(TrueDest); 5361 InstructionList.push_back(I); 5362 } 5363 else { 5364 BasicBlock *FalseDest = getBasicBlock(Record[1]); 5365 Type *CondType = Type::getInt1Ty(Context); 5366 Value *Cond = getValue(Record, 2, NextValueNo, CondType, 5367 getVirtualTypeID(CondType), CurBB); 5368 if (!FalseDest || !Cond) 5369 return error("Invalid record"); 5370 I = BranchInst::Create(TrueDest, FalseDest, Cond); 5371 InstructionList.push_back(I); 5372 } 5373 break; 5374 } 5375 case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] 5376 if (Record.size() != 1 && Record.size() != 2) 5377 return error("Invalid record"); 5378 unsigned Idx = 0; 5379 Type *TokenTy = Type::getTokenTy(Context); 5380 Value *CleanupPad = getValue(Record, Idx++, NextValueNo, TokenTy, 5381 getVirtualTypeID(TokenTy), CurBB); 5382 if (!CleanupPad) 5383 return error("Invalid record"); 5384 BasicBlock *UnwindDest = nullptr; 5385 if (Record.size() == 2) { 5386 UnwindDest = getBasicBlock(Record[Idx++]); 5387 if (!UnwindDest) 5388 return error("Invalid record"); 5389 } 5390 5391 I = CleanupReturnInst::Create(CleanupPad, UnwindDest); 5392 InstructionList.push_back(I); 5393 break; 5394 } 5395 case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] 5396 if (Record.size() != 2) 5397 return error("Invalid record"); 5398 unsigned Idx = 0; 5399 Type *TokenTy = Type::getTokenTy(Context); 5400 Value *CatchPad = getValue(Record, Idx++, NextValueNo, TokenTy, 5401 getVirtualTypeID(TokenTy), CurBB); 5402 if (!CatchPad) 5403 return error("Invalid record"); 5404 BasicBlock *BB = getBasicBlock(Record[Idx++]); 5405 if (!BB) 5406 return error("Invalid record"); 5407 5408 I = CatchReturnInst::Create(CatchPad, BB); 5409 InstructionList.push_back(I); 5410 break; 5411 } 5412 case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] 5413 // We must have, at minimum, the outer scope and the number of arguments. 5414 if (Record.size() < 2) 5415 return error("Invalid record"); 5416 5417 unsigned Idx = 0; 5418 5419 Type *TokenTy = Type::getTokenTy(Context); 5420 Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy, 5421 getVirtualTypeID(TokenTy), CurBB); 5422 if (!ParentPad) 5423 return error("Invalid record"); 5424 5425 unsigned NumHandlers = Record[Idx++]; 5426 5427 SmallVector<BasicBlock *, 2> Handlers; 5428 for (unsigned Op = 0; Op != NumHandlers; ++Op) { 5429 BasicBlock *BB = getBasicBlock(Record[Idx++]); 5430 if (!BB) 5431 return error("Invalid record"); 5432 Handlers.push_back(BB); 5433 } 5434 5435 BasicBlock *UnwindDest = nullptr; 5436 if (Idx + 1 == Record.size()) { 5437 UnwindDest = getBasicBlock(Record[Idx++]); 5438 if (!UnwindDest) 5439 return error("Invalid record"); 5440 } 5441 5442 if (Record.size() != Idx) 5443 return error("Invalid record"); 5444 5445 auto *CatchSwitch = 5446 CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); 5447 for (BasicBlock *Handler : Handlers) 5448 CatchSwitch->addHandler(Handler); 5449 I = CatchSwitch; 5450 ResTypeID = getVirtualTypeID(I->getType()); 5451 InstructionList.push_back(I); 5452 break; 5453 } 5454 case bitc::FUNC_CODE_INST_CATCHPAD: 5455 case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] 5456 // We must have, at minimum, the outer scope and the number of arguments. 5457 if (Record.size() < 2) 5458 return error("Invalid record"); 5459 5460 unsigned Idx = 0; 5461 5462 Type *TokenTy = Type::getTokenTy(Context); 5463 Value *ParentPad = getValue(Record, Idx++, NextValueNo, TokenTy, 5464 getVirtualTypeID(TokenTy), CurBB); 5465 if (!ParentPad) 5466 return error("Invald record"); 5467 5468 unsigned NumArgOperands = Record[Idx++]; 5469 5470 SmallVector<Value *, 2> Args; 5471 for (unsigned Op = 0; Op != NumArgOperands; ++Op) { 5472 Value *Val; 5473 unsigned ValTypeID; 5474 if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, nullptr)) 5475 return error("Invalid record"); 5476 Args.push_back(Val); 5477 } 5478 5479 if (Record.size() != Idx) 5480 return error("Invalid record"); 5481 5482 if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) 5483 I = CleanupPadInst::Create(ParentPad, Args); 5484 else 5485 I = CatchPadInst::Create(ParentPad, Args); 5486 ResTypeID = getVirtualTypeID(I->getType()); 5487 InstructionList.push_back(I); 5488 break; 5489 } 5490 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] 5491 // Check magic 5492 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { 5493 // "New" SwitchInst format with case ranges. The changes to write this 5494 // format were reverted but we still recognize bitcode that uses it. 5495 // Hopefully someday we will have support for case ranges and can use 5496 // this format again. 5497 5498 unsigned OpTyID = Record[1]; 5499 Type *OpTy = getTypeByID(OpTyID); 5500 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); 5501 5502 Value *Cond = getValue(Record, 2, NextValueNo, OpTy, OpTyID, CurBB); 5503 BasicBlock *Default = getBasicBlock(Record[3]); 5504 if (!OpTy || !Cond || !Default) 5505 return error("Invalid record"); 5506 5507 unsigned NumCases = Record[4]; 5508 5509 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 5510 InstructionList.push_back(SI); 5511 5512 unsigned CurIdx = 5; 5513 for (unsigned i = 0; i != NumCases; ++i) { 5514 SmallVector<ConstantInt*, 1> CaseVals; 5515 unsigned NumItems = Record[CurIdx++]; 5516 for (unsigned ci = 0; ci != NumItems; ++ci) { 5517 bool isSingleNumber = Record[CurIdx++]; 5518 5519 APInt Low; 5520 unsigned ActiveWords = 1; 5521 if (ValueBitWidth > 64) 5522 ActiveWords = Record[CurIdx++]; 5523 Low = readWideAPInt(ArrayRef(&Record[CurIdx], ActiveWords), 5524 ValueBitWidth); 5525 CurIdx += ActiveWords; 5526 5527 if (!isSingleNumber) { 5528 ActiveWords = 1; 5529 if (ValueBitWidth > 64) 5530 ActiveWords = Record[CurIdx++]; 5531 APInt High = readWideAPInt(ArrayRef(&Record[CurIdx], ActiveWords), 5532 ValueBitWidth); 5533 CurIdx += ActiveWords; 5534 5535 // FIXME: It is not clear whether values in the range should be 5536 // compared as signed or unsigned values. The partially 5537 // implemented changes that used this format in the past used 5538 // unsigned comparisons. 5539 for ( ; Low.ule(High); ++Low) 5540 CaseVals.push_back(ConstantInt::get(Context, Low)); 5541 } else 5542 CaseVals.push_back(ConstantInt::get(Context, Low)); 5543 } 5544 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); 5545 for (ConstantInt *Cst : CaseVals) 5546 SI->addCase(Cst, DestBB); 5547 } 5548 I = SI; 5549 break; 5550 } 5551 5552 // Old SwitchInst format without case ranges. 5553 5554 if (Record.size() < 3 || (Record.size() & 1) == 0) 5555 return error("Invalid record"); 5556 unsigned OpTyID = Record[0]; 5557 Type *OpTy = getTypeByID(OpTyID); 5558 Value *Cond = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB); 5559 BasicBlock *Default = getBasicBlock(Record[2]); 5560 if (!OpTy || !Cond || !Default) 5561 return error("Invalid record"); 5562 unsigned NumCases = (Record.size()-3)/2; 5563 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 5564 InstructionList.push_back(SI); 5565 for (unsigned i = 0, e = NumCases; i != e; ++i) { 5566 ConstantInt *CaseVal = dyn_cast_or_null<ConstantInt>( 5567 getFnValueByID(Record[3+i*2], OpTy, OpTyID, nullptr)); 5568 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); 5569 if (!CaseVal || !DestBB) { 5570 delete SI; 5571 return error("Invalid record"); 5572 } 5573 SI->addCase(CaseVal, DestBB); 5574 } 5575 I = SI; 5576 break; 5577 } 5578 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] 5579 if (Record.size() < 2) 5580 return error("Invalid record"); 5581 unsigned OpTyID = Record[0]; 5582 Type *OpTy = getTypeByID(OpTyID); 5583 Value *Address = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB); 5584 if (!OpTy || !Address) 5585 return error("Invalid record"); 5586 unsigned NumDests = Record.size()-2; 5587 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); 5588 InstructionList.push_back(IBI); 5589 for (unsigned i = 0, e = NumDests; i != e; ++i) { 5590 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { 5591 IBI->addDestination(DestBB); 5592 } else { 5593 delete IBI; 5594 return error("Invalid record"); 5595 } 5596 } 5597 I = IBI; 5598 break; 5599 } 5600 5601 case bitc::FUNC_CODE_INST_INVOKE: { 5602 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] 5603 if (Record.size() < 4) 5604 return error("Invalid record"); 5605 unsigned OpNum = 0; 5606 AttributeList PAL = getAttributes(Record[OpNum++]); 5607 unsigned CCInfo = Record[OpNum++]; 5608 BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); 5609 BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); 5610 5611 unsigned FTyID = InvalidTypeID; 5612 FunctionType *FTy = nullptr; 5613 if ((CCInfo >> 13) & 1) { 5614 FTyID = Record[OpNum++]; 5615 FTy = dyn_cast<FunctionType>(getTypeByID(FTyID)); 5616 if (!FTy) 5617 return error("Explicit invoke type is not a function type"); 5618 } 5619 5620 Value *Callee; 5621 unsigned CalleeTypeID; 5622 if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID, 5623 CurBB)) 5624 return error("Invalid record"); 5625 5626 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); 5627 if (!CalleeTy) 5628 return error("Callee is not a pointer"); 5629 if (!FTy) { 5630 FTyID = getContainedTypeID(CalleeTypeID); 5631 FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 5632 if (!FTy) 5633 return error("Callee is not of pointer to function type"); 5634 } 5635 if (Record.size() < FTy->getNumParams() + OpNum) 5636 return error("Insufficient operands to call"); 5637 5638 SmallVector<Value*, 16> Ops; 5639 SmallVector<unsigned, 16> ArgTyIDs; 5640 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 5641 unsigned ArgTyID = getContainedTypeID(FTyID, i + 1); 5642 Ops.push_back(getValue(Record, OpNum, NextValueNo, FTy->getParamType(i), 5643 ArgTyID, CurBB)); 5644 ArgTyIDs.push_back(ArgTyID); 5645 if (!Ops.back()) 5646 return error("Invalid record"); 5647 } 5648 5649 if (!FTy->isVarArg()) { 5650 if (Record.size() != OpNum) 5651 return error("Invalid record"); 5652 } else { 5653 // Read type/value pairs for varargs params. 5654 while (OpNum != Record.size()) { 5655 Value *Op; 5656 unsigned OpTypeID; 5657 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 5658 return error("Invalid record"); 5659 Ops.push_back(Op); 5660 ArgTyIDs.push_back(OpTypeID); 5661 } 5662 } 5663 5664 // Upgrade the bundles if needed. 5665 if (!OperandBundles.empty()) 5666 UpgradeOperandBundles(OperandBundles); 5667 5668 I = InvokeInst::Create(FTy, Callee, NormalBB, UnwindBB, Ops, 5669 OperandBundles); 5670 ResTypeID = getContainedTypeID(FTyID); 5671 OperandBundles.clear(); 5672 InstructionList.push_back(I); 5673 cast<InvokeInst>(I)->setCallingConv( 5674 static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); 5675 cast<InvokeInst>(I)->setAttributes(PAL); 5676 if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) { 5677 I->deleteValue(); 5678 return Err; 5679 } 5680 5681 break; 5682 } 5683 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] 5684 unsigned Idx = 0; 5685 Value *Val = nullptr; 5686 unsigned ValTypeID; 5687 if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, CurBB)) 5688 return error("Invalid record"); 5689 I = ResumeInst::Create(Val); 5690 InstructionList.push_back(I); 5691 break; 5692 } 5693 case bitc::FUNC_CODE_INST_CALLBR: { 5694 // CALLBR: [attr, cc, norm, transfs, fty, fnid, args] 5695 unsigned OpNum = 0; 5696 AttributeList PAL = getAttributes(Record[OpNum++]); 5697 unsigned CCInfo = Record[OpNum++]; 5698 5699 BasicBlock *DefaultDest = getBasicBlock(Record[OpNum++]); 5700 unsigned NumIndirectDests = Record[OpNum++]; 5701 SmallVector<BasicBlock *, 16> IndirectDests; 5702 for (unsigned i = 0, e = NumIndirectDests; i != e; ++i) 5703 IndirectDests.push_back(getBasicBlock(Record[OpNum++])); 5704 5705 unsigned FTyID = InvalidTypeID; 5706 FunctionType *FTy = nullptr; 5707 if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) { 5708 FTyID = Record[OpNum++]; 5709 FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 5710 if (!FTy) 5711 return error("Explicit call type is not a function type"); 5712 } 5713 5714 Value *Callee; 5715 unsigned CalleeTypeID; 5716 if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID, 5717 CurBB)) 5718 return error("Invalid record"); 5719 5720 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); 5721 if (!OpTy) 5722 return error("Callee is not a pointer type"); 5723 if (!FTy) { 5724 FTyID = getContainedTypeID(CalleeTypeID); 5725 FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 5726 if (!FTy) 5727 return error("Callee is not of pointer to function type"); 5728 } 5729 if (Record.size() < FTy->getNumParams() + OpNum) 5730 return error("Insufficient operands to call"); 5731 5732 SmallVector<Value*, 16> Args; 5733 SmallVector<unsigned, 16> ArgTyIDs; 5734 // Read the fixed params. 5735 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 5736 Value *Arg; 5737 unsigned ArgTyID = getContainedTypeID(FTyID, i + 1); 5738 if (FTy->getParamType(i)->isLabelTy()) 5739 Arg = getBasicBlock(Record[OpNum]); 5740 else 5741 Arg = getValue(Record, OpNum, NextValueNo, FTy->getParamType(i), 5742 ArgTyID, CurBB); 5743 if (!Arg) 5744 return error("Invalid record"); 5745 Args.push_back(Arg); 5746 ArgTyIDs.push_back(ArgTyID); 5747 } 5748 5749 // Read type/value pairs for varargs params. 5750 if (!FTy->isVarArg()) { 5751 if (OpNum != Record.size()) 5752 return error("Invalid record"); 5753 } else { 5754 while (OpNum != Record.size()) { 5755 Value *Op; 5756 unsigned OpTypeID; 5757 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 5758 return error("Invalid record"); 5759 Args.push_back(Op); 5760 ArgTyIDs.push_back(OpTypeID); 5761 } 5762 } 5763 5764 // Upgrade the bundles if needed. 5765 if (!OperandBundles.empty()) 5766 UpgradeOperandBundles(OperandBundles); 5767 5768 if (auto *IA = dyn_cast<InlineAsm>(Callee)) { 5769 InlineAsm::ConstraintInfoVector ConstraintInfo = IA->ParseConstraints(); 5770 auto IsLabelConstraint = [](const InlineAsm::ConstraintInfo &CI) { 5771 return CI.Type == InlineAsm::isLabel; 5772 }; 5773 if (none_of(ConstraintInfo, IsLabelConstraint)) { 5774 // Upgrade explicit blockaddress arguments to label constraints. 5775 // Verify that the last arguments are blockaddress arguments that 5776 // match the indirect destinations. Clang always generates callbr 5777 // in this form. We could support reordering with more effort. 5778 unsigned FirstBlockArg = Args.size() - IndirectDests.size(); 5779 for (unsigned ArgNo = FirstBlockArg; ArgNo < Args.size(); ++ArgNo) { 5780 unsigned LabelNo = ArgNo - FirstBlockArg; 5781 auto *BA = dyn_cast<BlockAddress>(Args[ArgNo]); 5782 if (!BA || BA->getFunction() != F || 5783 LabelNo > IndirectDests.size() || 5784 BA->getBasicBlock() != IndirectDests[LabelNo]) 5785 return error("callbr argument does not match indirect dest"); 5786 } 5787 5788 // Remove blockaddress arguments. 5789 Args.erase(Args.begin() + FirstBlockArg, Args.end()); 5790 ArgTyIDs.erase(ArgTyIDs.begin() + FirstBlockArg, ArgTyIDs.end()); 5791 5792 // Recreate the function type with less arguments. 5793 SmallVector<Type *> ArgTys; 5794 for (Value *Arg : Args) 5795 ArgTys.push_back(Arg->getType()); 5796 FTy = 5797 FunctionType::get(FTy->getReturnType(), ArgTys, FTy->isVarArg()); 5798 5799 // Update constraint string to use label constraints. 5800 std::string Constraints = IA->getConstraintString(); 5801 unsigned ArgNo = 0; 5802 size_t Pos = 0; 5803 for (const auto &CI : ConstraintInfo) { 5804 if (CI.hasArg()) { 5805 if (ArgNo >= FirstBlockArg) 5806 Constraints.insert(Pos, "!"); 5807 ++ArgNo; 5808 } 5809 5810 // Go to next constraint in string. 5811 Pos = Constraints.find(',', Pos); 5812 if (Pos == std::string::npos) 5813 break; 5814 ++Pos; 5815 } 5816 5817 Callee = InlineAsm::get(FTy, IA->getAsmString(), Constraints, 5818 IA->hasSideEffects(), IA->isAlignStack(), 5819 IA->getDialect(), IA->canThrow()); 5820 } 5821 } 5822 5823 I = CallBrInst::Create(FTy, Callee, DefaultDest, IndirectDests, Args, 5824 OperandBundles); 5825 ResTypeID = getContainedTypeID(FTyID); 5826 OperandBundles.clear(); 5827 InstructionList.push_back(I); 5828 cast<CallBrInst>(I)->setCallingConv( 5829 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); 5830 cast<CallBrInst>(I)->setAttributes(PAL); 5831 if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) { 5832 I->deleteValue(); 5833 return Err; 5834 } 5835 break; 5836 } 5837 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE 5838 I = new UnreachableInst(Context); 5839 InstructionList.push_back(I); 5840 break; 5841 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] 5842 if (Record.empty()) 5843 return error("Invalid phi record"); 5844 // The first record specifies the type. 5845 unsigned TyID = Record[0]; 5846 Type *Ty = getTypeByID(TyID); 5847 if (!Ty) 5848 return error("Invalid phi record"); 5849 5850 // Phi arguments are pairs of records of [value, basic block]. 5851 // There is an optional final record for fast-math-flags if this phi has a 5852 // floating-point type. 5853 size_t NumArgs = (Record.size() - 1) / 2; 5854 PHINode *PN = PHINode::Create(Ty, NumArgs); 5855 if ((Record.size() - 1) % 2 == 1 && !isa<FPMathOperator>(PN)) { 5856 PN->deleteValue(); 5857 return error("Invalid phi record"); 5858 } 5859 InstructionList.push_back(PN); 5860 5861 SmallDenseMap<BasicBlock *, Value *> Args; 5862 for (unsigned i = 0; i != NumArgs; i++) { 5863 BasicBlock *BB = getBasicBlock(Record[i * 2 + 2]); 5864 if (!BB) { 5865 PN->deleteValue(); 5866 return error("Invalid phi BB"); 5867 } 5868 5869 // Phi nodes may contain the same predecessor multiple times, in which 5870 // case the incoming value must be identical. Directly reuse the already 5871 // seen value here, to avoid expanding a constant expression multiple 5872 // times. 5873 auto It = Args.find(BB); 5874 if (It != Args.end()) { 5875 PN->addIncoming(It->second, BB); 5876 continue; 5877 } 5878 5879 // If there already is a block for this edge (from a different phi), 5880 // use it. 5881 BasicBlock *EdgeBB = ConstExprEdgeBBs.lookup({BB, CurBB}); 5882 if (!EdgeBB) { 5883 // Otherwise, use a temporary block (that we will discard if it 5884 // turns out to be unnecessary). 5885 if (!PhiConstExprBB) 5886 PhiConstExprBB = BasicBlock::Create(Context, "phi.constexpr", F); 5887 EdgeBB = PhiConstExprBB; 5888 } 5889 5890 // With the new function encoding, it is possible that operands have 5891 // negative IDs (for forward references). Use a signed VBR 5892 // representation to keep the encoding small. 5893 Value *V; 5894 if (UseRelativeIDs) 5895 V = getValueSigned(Record, i * 2 + 1, NextValueNo, Ty, TyID, EdgeBB); 5896 else 5897 V = getValue(Record, i * 2 + 1, NextValueNo, Ty, TyID, EdgeBB); 5898 if (!V) { 5899 PN->deleteValue(); 5900 PhiConstExprBB->eraseFromParent(); 5901 return error("Invalid phi record"); 5902 } 5903 5904 if (EdgeBB == PhiConstExprBB && !EdgeBB->empty()) { 5905 ConstExprEdgeBBs.insert({{BB, CurBB}, EdgeBB}); 5906 PhiConstExprBB = nullptr; 5907 } 5908 PN->addIncoming(V, BB); 5909 Args.insert({BB, V}); 5910 } 5911 I = PN; 5912 ResTypeID = TyID; 5913 5914 // If there are an even number of records, the final record must be FMF. 5915 if (Record.size() % 2 == 0) { 5916 assert(isa<FPMathOperator>(I) && "Unexpected phi type"); 5917 FastMathFlags FMF = getDecodedFastMathFlags(Record[Record.size() - 1]); 5918 if (FMF.any()) 5919 I->setFastMathFlags(FMF); 5920 } 5921 5922 break; 5923 } 5924 5925 case bitc::FUNC_CODE_INST_LANDINGPAD: 5926 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { 5927 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] 5928 unsigned Idx = 0; 5929 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { 5930 if (Record.size() < 3) 5931 return error("Invalid record"); 5932 } else { 5933 assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD); 5934 if (Record.size() < 4) 5935 return error("Invalid record"); 5936 } 5937 ResTypeID = Record[Idx++]; 5938 Type *Ty = getTypeByID(ResTypeID); 5939 if (!Ty) 5940 return error("Invalid record"); 5941 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { 5942 Value *PersFn = nullptr; 5943 unsigned PersFnTypeID; 5944 if (getValueTypePair(Record, Idx, NextValueNo, PersFn, PersFnTypeID, 5945 nullptr)) 5946 return error("Invalid record"); 5947 5948 if (!F->hasPersonalityFn()) 5949 F->setPersonalityFn(cast<Constant>(PersFn)); 5950 else if (F->getPersonalityFn() != cast<Constant>(PersFn)) 5951 return error("Personality function mismatch"); 5952 } 5953 5954 bool IsCleanup = !!Record[Idx++]; 5955 unsigned NumClauses = Record[Idx++]; 5956 LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses); 5957 LP->setCleanup(IsCleanup); 5958 for (unsigned J = 0; J != NumClauses; ++J) { 5959 LandingPadInst::ClauseType CT = 5960 LandingPadInst::ClauseType(Record[Idx++]); (void)CT; 5961 Value *Val; 5962 unsigned ValTypeID; 5963 5964 if (getValueTypePair(Record, Idx, NextValueNo, Val, ValTypeID, 5965 nullptr)) { 5966 delete LP; 5967 return error("Invalid record"); 5968 } 5969 5970 assert((CT != LandingPadInst::Catch || 5971 !isa<ArrayType>(Val->getType())) && 5972 "Catch clause has a invalid type!"); 5973 assert((CT != LandingPadInst::Filter || 5974 isa<ArrayType>(Val->getType())) && 5975 "Filter clause has invalid type!"); 5976 LP->addClause(cast<Constant>(Val)); 5977 } 5978 5979 I = LP; 5980 InstructionList.push_back(I); 5981 break; 5982 } 5983 5984 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] 5985 if (Record.size() != 4 && Record.size() != 5) 5986 return error("Invalid record"); 5987 using APV = AllocaPackedValues; 5988 const uint64_t Rec = Record[3]; 5989 const bool InAlloca = Bitfield::get<APV::UsedWithInAlloca>(Rec); 5990 const bool SwiftError = Bitfield::get<APV::SwiftError>(Rec); 5991 unsigned TyID = Record[0]; 5992 Type *Ty = getTypeByID(TyID); 5993 if (!Bitfield::get<APV::ExplicitType>(Rec)) { 5994 TyID = getContainedTypeID(TyID); 5995 Ty = getTypeByID(TyID); 5996 if (!Ty) 5997 return error("Missing element type for old-style alloca"); 5998 } 5999 unsigned OpTyID = Record[1]; 6000 Type *OpTy = getTypeByID(OpTyID); 6001 Value *Size = getFnValueByID(Record[2], OpTy, OpTyID, CurBB); 6002 MaybeAlign Align; 6003 uint64_t AlignExp = 6004 Bitfield::get<APV::AlignLower>(Rec) | 6005 (Bitfield::get<APV::AlignUpper>(Rec) << APV::AlignLower::Bits); 6006 if (Error Err = parseAlignmentValue(AlignExp, Align)) { 6007 return Err; 6008 } 6009 if (!Ty || !Size) 6010 return error("Invalid record"); 6011 6012 const DataLayout &DL = TheModule->getDataLayout(); 6013 unsigned AS = Record.size() == 5 ? Record[4] : DL.getAllocaAddrSpace(); 6014 6015 SmallPtrSet<Type *, 4> Visited; 6016 if (!Align && !Ty->isSized(&Visited)) 6017 return error("alloca of unsized type"); 6018 if (!Align) 6019 Align = DL.getPrefTypeAlign(Ty); 6020 6021 if (!Size->getType()->isIntegerTy()) 6022 return error("alloca element count must have integer type"); 6023 6024 AllocaInst *AI = new AllocaInst(Ty, AS, Size, *Align); 6025 AI->setUsedWithInAlloca(InAlloca); 6026 AI->setSwiftError(SwiftError); 6027 I = AI; 6028 ResTypeID = getVirtualTypeID(AI->getType(), TyID); 6029 InstructionList.push_back(I); 6030 break; 6031 } 6032 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] 6033 unsigned OpNum = 0; 6034 Value *Op; 6035 unsigned OpTypeID; 6036 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || 6037 (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) 6038 return error("Invalid record"); 6039 6040 if (!isa<PointerType>(Op->getType())) 6041 return error("Load operand is not a pointer type"); 6042 6043 Type *Ty = nullptr; 6044 if (OpNum + 3 == Record.size()) { 6045 ResTypeID = Record[OpNum++]; 6046 Ty = getTypeByID(ResTypeID); 6047 } else { 6048 ResTypeID = getContainedTypeID(OpTypeID); 6049 Ty = getTypeByID(ResTypeID); 6050 } 6051 6052 if (!Ty) 6053 return error("Missing load type"); 6054 6055 if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 6056 return Err; 6057 6058 MaybeAlign Align; 6059 if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 6060 return Err; 6061 SmallPtrSet<Type *, 4> Visited; 6062 if (!Align && !Ty->isSized(&Visited)) 6063 return error("load of unsized type"); 6064 if (!Align) 6065 Align = TheModule->getDataLayout().getABITypeAlign(Ty); 6066 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align); 6067 InstructionList.push_back(I); 6068 break; 6069 } 6070 case bitc::FUNC_CODE_INST_LOADATOMIC: { 6071 // LOADATOMIC: [opty, op, align, vol, ordering, ssid] 6072 unsigned OpNum = 0; 6073 Value *Op; 6074 unsigned OpTypeID; 6075 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || 6076 (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) 6077 return error("Invalid record"); 6078 6079 if (!isa<PointerType>(Op->getType())) 6080 return error("Load operand is not a pointer type"); 6081 6082 Type *Ty = nullptr; 6083 if (OpNum + 5 == Record.size()) { 6084 ResTypeID = Record[OpNum++]; 6085 Ty = getTypeByID(ResTypeID); 6086 } else { 6087 ResTypeID = getContainedTypeID(OpTypeID); 6088 Ty = getTypeByID(ResTypeID); 6089 } 6090 6091 if (!Ty) 6092 return error("Missing atomic load type"); 6093 6094 if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 6095 return Err; 6096 6097 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 6098 if (Ordering == AtomicOrdering::NotAtomic || 6099 Ordering == AtomicOrdering::Release || 6100 Ordering == AtomicOrdering::AcquireRelease) 6101 return error("Invalid record"); 6102 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 6103 return error("Invalid record"); 6104 SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); 6105 6106 MaybeAlign Align; 6107 if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 6108 return Err; 6109 if (!Align) 6110 return error("Alignment missing from atomic load"); 6111 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align, Ordering, SSID); 6112 InstructionList.push_back(I); 6113 break; 6114 } 6115 case bitc::FUNC_CODE_INST_STORE: 6116 case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol] 6117 unsigned OpNum = 0; 6118 Value *Val, *Ptr; 6119 unsigned PtrTypeID, ValTypeID; 6120 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 6121 return error("Invalid record"); 6122 6123 if (BitCode == bitc::FUNC_CODE_INST_STORE) { 6124 if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 6125 return error("Invalid record"); 6126 } else { 6127 ValTypeID = getContainedTypeID(PtrTypeID); 6128 if (popValue(Record, OpNum, NextValueNo, getTypeByID(ValTypeID), 6129 ValTypeID, Val, CurBB)) 6130 return error("Invalid record"); 6131 } 6132 6133 if (OpNum + 2 != Record.size()) 6134 return error("Invalid record"); 6135 6136 if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 6137 return Err; 6138 MaybeAlign Align; 6139 if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 6140 return Err; 6141 SmallPtrSet<Type *, 4> Visited; 6142 if (!Align && !Val->getType()->isSized(&Visited)) 6143 return error("store of unsized type"); 6144 if (!Align) 6145 Align = TheModule->getDataLayout().getABITypeAlign(Val->getType()); 6146 I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align); 6147 InstructionList.push_back(I); 6148 break; 6149 } 6150 case bitc::FUNC_CODE_INST_STOREATOMIC: 6151 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { 6152 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid] 6153 unsigned OpNum = 0; 6154 Value *Val, *Ptr; 6155 unsigned PtrTypeID, ValTypeID; 6156 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB) || 6157 !isa<PointerType>(Ptr->getType())) 6158 return error("Invalid record"); 6159 if (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC) { 6160 if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 6161 return error("Invalid record"); 6162 } else { 6163 ValTypeID = getContainedTypeID(PtrTypeID); 6164 if (popValue(Record, OpNum, NextValueNo, getTypeByID(ValTypeID), 6165 ValTypeID, Val, CurBB)) 6166 return error("Invalid record"); 6167 } 6168 6169 if (OpNum + 4 != Record.size()) 6170 return error("Invalid record"); 6171 6172 if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 6173 return Err; 6174 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 6175 if (Ordering == AtomicOrdering::NotAtomic || 6176 Ordering == AtomicOrdering::Acquire || 6177 Ordering == AtomicOrdering::AcquireRelease) 6178 return error("Invalid record"); 6179 SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); 6180 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 6181 return error("Invalid record"); 6182 6183 MaybeAlign Align; 6184 if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 6185 return Err; 6186 if (!Align) 6187 return error("Alignment missing from atomic store"); 6188 I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align, Ordering, SSID); 6189 InstructionList.push_back(I); 6190 break; 6191 } 6192 case bitc::FUNC_CODE_INST_CMPXCHG_OLD: { 6193 // CMPXCHG_OLD: [ptrty, ptr, cmp, val, vol, ordering, synchscope, 6194 // failure_ordering?, weak?] 6195 const size_t NumRecords = Record.size(); 6196 unsigned OpNum = 0; 6197 Value *Ptr = nullptr; 6198 unsigned PtrTypeID; 6199 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 6200 return error("Invalid record"); 6201 6202 if (!isa<PointerType>(Ptr->getType())) 6203 return error("Cmpxchg operand is not a pointer type"); 6204 6205 Value *Cmp = nullptr; 6206 unsigned CmpTypeID = getContainedTypeID(PtrTypeID); 6207 if (popValue(Record, OpNum, NextValueNo, getTypeByID(CmpTypeID), 6208 CmpTypeID, Cmp, CurBB)) 6209 return error("Invalid record"); 6210 6211 Value *New = nullptr; 6212 if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), CmpTypeID, 6213 New, CurBB) || 6214 NumRecords < OpNum + 3 || NumRecords > OpNum + 5) 6215 return error("Invalid record"); 6216 6217 const AtomicOrdering SuccessOrdering = 6218 getDecodedOrdering(Record[OpNum + 1]); 6219 if (SuccessOrdering == AtomicOrdering::NotAtomic || 6220 SuccessOrdering == AtomicOrdering::Unordered) 6221 return error("Invalid record"); 6222 6223 const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]); 6224 6225 if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) 6226 return Err; 6227 6228 const AtomicOrdering FailureOrdering = 6229 NumRecords < 7 6230 ? AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering) 6231 : getDecodedOrdering(Record[OpNum + 3]); 6232 6233 if (FailureOrdering == AtomicOrdering::NotAtomic || 6234 FailureOrdering == AtomicOrdering::Unordered) 6235 return error("Invalid record"); 6236 6237 const Align Alignment( 6238 TheModule->getDataLayout().getTypeStoreSize(Cmp->getType())); 6239 6240 I = new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment, SuccessOrdering, 6241 FailureOrdering, SSID); 6242 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); 6243 6244 if (NumRecords < 8) { 6245 // Before weak cmpxchgs existed, the instruction simply returned the 6246 // value loaded from memory, so bitcode files from that era will be 6247 // expecting the first component of a modern cmpxchg. 6248 I->insertInto(CurBB, CurBB->end()); 6249 I = ExtractValueInst::Create(I, 0); 6250 ResTypeID = CmpTypeID; 6251 } else { 6252 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum + 4]); 6253 unsigned I1TypeID = getVirtualTypeID(Type::getInt1Ty(Context)); 6254 ResTypeID = getVirtualTypeID(I->getType(), {CmpTypeID, I1TypeID}); 6255 } 6256 6257 InstructionList.push_back(I); 6258 break; 6259 } 6260 case bitc::FUNC_CODE_INST_CMPXCHG: { 6261 // CMPXCHG: [ptrty, ptr, cmp, val, vol, success_ordering, synchscope, 6262 // failure_ordering, weak, align?] 6263 const size_t NumRecords = Record.size(); 6264 unsigned OpNum = 0; 6265 Value *Ptr = nullptr; 6266 unsigned PtrTypeID; 6267 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 6268 return error("Invalid record"); 6269 6270 if (!isa<PointerType>(Ptr->getType())) 6271 return error("Cmpxchg operand is not a pointer type"); 6272 6273 Value *Cmp = nullptr; 6274 unsigned CmpTypeID; 6275 if (getValueTypePair(Record, OpNum, NextValueNo, Cmp, CmpTypeID, CurBB)) 6276 return error("Invalid record"); 6277 6278 Value *Val = nullptr; 6279 if (popValue(Record, OpNum, NextValueNo, Cmp->getType(), CmpTypeID, Val, 6280 CurBB)) 6281 return error("Invalid record"); 6282 6283 if (NumRecords < OpNum + 3 || NumRecords > OpNum + 6) 6284 return error("Invalid record"); 6285 6286 const bool IsVol = Record[OpNum]; 6287 6288 const AtomicOrdering SuccessOrdering = 6289 getDecodedOrdering(Record[OpNum + 1]); 6290 if (!AtomicCmpXchgInst::isValidSuccessOrdering(SuccessOrdering)) 6291 return error("Invalid cmpxchg success ordering"); 6292 6293 const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]); 6294 6295 if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) 6296 return Err; 6297 6298 const AtomicOrdering FailureOrdering = 6299 getDecodedOrdering(Record[OpNum + 3]); 6300 if (!AtomicCmpXchgInst::isValidFailureOrdering(FailureOrdering)) 6301 return error("Invalid cmpxchg failure ordering"); 6302 6303 const bool IsWeak = Record[OpNum + 4]; 6304 6305 MaybeAlign Alignment; 6306 6307 if (NumRecords == (OpNum + 6)) { 6308 if (Error Err = parseAlignmentValue(Record[OpNum + 5], Alignment)) 6309 return Err; 6310 } 6311 if (!Alignment) 6312 Alignment = 6313 Align(TheModule->getDataLayout().getTypeStoreSize(Cmp->getType())); 6314 6315 I = new AtomicCmpXchgInst(Ptr, Cmp, Val, *Alignment, SuccessOrdering, 6316 FailureOrdering, SSID); 6317 cast<AtomicCmpXchgInst>(I)->setVolatile(IsVol); 6318 cast<AtomicCmpXchgInst>(I)->setWeak(IsWeak); 6319 6320 unsigned I1TypeID = getVirtualTypeID(Type::getInt1Ty(Context)); 6321 ResTypeID = getVirtualTypeID(I->getType(), {CmpTypeID, I1TypeID}); 6322 6323 InstructionList.push_back(I); 6324 break; 6325 } 6326 case bitc::FUNC_CODE_INST_ATOMICRMW_OLD: 6327 case bitc::FUNC_CODE_INST_ATOMICRMW: { 6328 // ATOMICRMW_OLD: [ptrty, ptr, val, op, vol, ordering, ssid, align?] 6329 // ATOMICRMW: [ptrty, ptr, valty, val, op, vol, ordering, ssid, align?] 6330 const size_t NumRecords = Record.size(); 6331 unsigned OpNum = 0; 6332 6333 Value *Ptr = nullptr; 6334 unsigned PtrTypeID; 6335 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr, PtrTypeID, CurBB)) 6336 return error("Invalid record"); 6337 6338 if (!isa<PointerType>(Ptr->getType())) 6339 return error("Invalid record"); 6340 6341 Value *Val = nullptr; 6342 unsigned ValTypeID = InvalidTypeID; 6343 if (BitCode == bitc::FUNC_CODE_INST_ATOMICRMW_OLD) { 6344 ValTypeID = getContainedTypeID(PtrTypeID); 6345 if (popValue(Record, OpNum, NextValueNo, 6346 getTypeByID(ValTypeID), ValTypeID, Val, CurBB)) 6347 return error("Invalid record"); 6348 } else { 6349 if (getValueTypePair(Record, OpNum, NextValueNo, Val, ValTypeID, CurBB)) 6350 return error("Invalid record"); 6351 } 6352 6353 if (!(NumRecords == (OpNum + 4) || NumRecords == (OpNum + 5))) 6354 return error("Invalid record"); 6355 6356 const AtomicRMWInst::BinOp Operation = 6357 getDecodedRMWOperation(Record[OpNum]); 6358 if (Operation < AtomicRMWInst::FIRST_BINOP || 6359 Operation > AtomicRMWInst::LAST_BINOP) 6360 return error("Invalid record"); 6361 6362 const bool IsVol = Record[OpNum + 1]; 6363 6364 const AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 6365 if (Ordering == AtomicOrdering::NotAtomic || 6366 Ordering == AtomicOrdering::Unordered) 6367 return error("Invalid record"); 6368 6369 const SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); 6370 6371 MaybeAlign Alignment; 6372 6373 if (NumRecords == (OpNum + 5)) { 6374 if (Error Err = parseAlignmentValue(Record[OpNum + 4], Alignment)) 6375 return Err; 6376 } 6377 6378 if (!Alignment) 6379 Alignment = 6380 Align(TheModule->getDataLayout().getTypeStoreSize(Val->getType())); 6381 6382 I = new AtomicRMWInst(Operation, Ptr, Val, *Alignment, Ordering, SSID); 6383 ResTypeID = ValTypeID; 6384 cast<AtomicRMWInst>(I)->setVolatile(IsVol); 6385 6386 InstructionList.push_back(I); 6387 break; 6388 } 6389 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, ssid] 6390 if (2 != Record.size()) 6391 return error("Invalid record"); 6392 AtomicOrdering Ordering = getDecodedOrdering(Record[0]); 6393 if (Ordering == AtomicOrdering::NotAtomic || 6394 Ordering == AtomicOrdering::Unordered || 6395 Ordering == AtomicOrdering::Monotonic) 6396 return error("Invalid record"); 6397 SyncScope::ID SSID = getDecodedSyncScopeID(Record[1]); 6398 I = new FenceInst(Context, Ordering, SSID); 6399 InstructionList.push_back(I); 6400 break; 6401 } 6402 case bitc::FUNC_CODE_DEBUG_RECORD_LABEL: { 6403 // DPLabels are placed after the Instructions that they are attached to. 6404 Instruction *Inst = getLastInstruction(); 6405 if (!Inst) 6406 return error("Invalid dbg record: missing instruction"); 6407 DILocation *DIL = cast<DILocation>(getFnMetadataByID(Record[0])); 6408 DILabel *Label = cast<DILabel>(getFnMetadataByID(Record[1])); 6409 Inst->getParent()->insertDbgRecordBefore( 6410 new DPLabel(Label, DebugLoc(DIL)), Inst->getIterator()); 6411 continue; // This isn't an instruction. 6412 } 6413 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE: 6414 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE: 6415 case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE: 6416 case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: { 6417 // DPValues are placed after the Instructions that they are attached to. 6418 Instruction *Inst = getLastInstruction(); 6419 if (!Inst) 6420 return error("Invalid dbg record: missing instruction"); 6421 6422 // First 3 fields are common to all kinds: 6423 // DILocation, DILocalVariable, DIExpression 6424 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE) 6425 // ..., LocationMetadata 6426 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE - abbrev'd) 6427 // ..., Value 6428 // dbg_declare (FUNC_CODE_DEBUG_RECORD_DECLARE) 6429 // ..., LocationMetadata 6430 // dbg_assign (FUNC_CODE_DEBUG_RECORD_ASSIGN) 6431 // ..., LocationMetadata, DIAssignID, DIExpression, LocationMetadata 6432 unsigned Slot = 0; 6433 // Common fields (0-2). 6434 DILocation *DIL = cast<DILocation>(getFnMetadataByID(Record[Slot++])); 6435 DILocalVariable *Var = 6436 cast<DILocalVariable>(getFnMetadataByID(Record[Slot++])); 6437 DIExpression *Expr = 6438 cast<DIExpression>(getFnMetadataByID(Record[Slot++])); 6439 6440 // Union field (3: LocationMetadata | Value). 6441 Metadata *RawLocation = nullptr; 6442 if (BitCode == bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE) { 6443 Value *V = nullptr; 6444 unsigned TyID = 0; 6445 // We never expect to see a fwd reference value here because 6446 // use-before-defs are encoded with the standard non-abbrev record 6447 // type (they'd require encoding the type too, and they're rare). As a 6448 // result, getValueTypePair only ever increments Slot by one here (once 6449 // for the value, never twice for value and type). 6450 unsigned SlotBefore = Slot; 6451 if (getValueTypePair(Record, Slot, NextValueNo, V, TyID, CurBB)) 6452 return error("Invalid dbg record: invalid value"); 6453 (void)SlotBefore; 6454 assert((SlotBefore == Slot - 1) && "unexpected fwd ref"); 6455 RawLocation = ValueAsMetadata::get(V); 6456 } else { 6457 RawLocation = getFnMetadataByID(Record[Slot++]); 6458 } 6459 6460 DPValue *DPV = nullptr; 6461 switch (BitCode) { 6462 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE: 6463 case bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE: 6464 DPV = new DPValue(RawLocation, Var, Expr, DIL, 6465 DPValue::LocationType::Value); 6466 break; 6467 case bitc::FUNC_CODE_DEBUG_RECORD_DECLARE: 6468 DPV = new DPValue(RawLocation, Var, Expr, DIL, 6469 DPValue::LocationType::Declare); 6470 break; 6471 case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: { 6472 DIAssignID *ID = cast<DIAssignID>(getFnMetadataByID(Record[Slot++])); 6473 DIExpression *AddrExpr = 6474 cast<DIExpression>(getFnMetadataByID(Record[Slot++])); 6475 Metadata *Addr = getFnMetadataByID(Record[Slot++]); 6476 DPV = new DPValue(RawLocation, Var, Expr, ID, Addr, AddrExpr, DIL); 6477 break; 6478 } 6479 default: 6480 llvm_unreachable("Unknown DPValue bitcode"); 6481 } 6482 Inst->getParent()->insertDbgRecordBefore(DPV, Inst->getIterator()); 6483 continue; // This isn't an instruction. 6484 } 6485 case bitc::FUNC_CODE_INST_CALL: { 6486 // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...] 6487 if (Record.size() < 3) 6488 return error("Invalid record"); 6489 6490 unsigned OpNum = 0; 6491 AttributeList PAL = getAttributes(Record[OpNum++]); 6492 unsigned CCInfo = Record[OpNum++]; 6493 6494 FastMathFlags FMF; 6495 if ((CCInfo >> bitc::CALL_FMF) & 1) { 6496 FMF = getDecodedFastMathFlags(Record[OpNum++]); 6497 if (!FMF.any()) 6498 return error("Fast math flags indicator set for call with no FMF"); 6499 } 6500 6501 unsigned FTyID = InvalidTypeID; 6502 FunctionType *FTy = nullptr; 6503 if ((CCInfo >> bitc::CALL_EXPLICIT_TYPE) & 1) { 6504 FTyID = Record[OpNum++]; 6505 FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 6506 if (!FTy) 6507 return error("Explicit call type is not a function type"); 6508 } 6509 6510 Value *Callee; 6511 unsigned CalleeTypeID; 6512 if (getValueTypePair(Record, OpNum, NextValueNo, Callee, CalleeTypeID, 6513 CurBB)) 6514 return error("Invalid record"); 6515 6516 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); 6517 if (!OpTy) 6518 return error("Callee is not a pointer type"); 6519 if (!FTy) { 6520 FTyID = getContainedTypeID(CalleeTypeID); 6521 FTy = dyn_cast_or_null<FunctionType>(getTypeByID(FTyID)); 6522 if (!FTy) 6523 return error("Callee is not of pointer to function type"); 6524 } 6525 if (Record.size() < FTy->getNumParams() + OpNum) 6526 return error("Insufficient operands to call"); 6527 6528 SmallVector<Value*, 16> Args; 6529 SmallVector<unsigned, 16> ArgTyIDs; 6530 // Read the fixed params. 6531 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 6532 unsigned ArgTyID = getContainedTypeID(FTyID, i + 1); 6533 if (FTy->getParamType(i)->isLabelTy()) 6534 Args.push_back(getBasicBlock(Record[OpNum])); 6535 else 6536 Args.push_back(getValue(Record, OpNum, NextValueNo, 6537 FTy->getParamType(i), ArgTyID, CurBB)); 6538 ArgTyIDs.push_back(ArgTyID); 6539 if (!Args.back()) 6540 return error("Invalid record"); 6541 } 6542 6543 // Read type/value pairs for varargs params. 6544 if (!FTy->isVarArg()) { 6545 if (OpNum != Record.size()) 6546 return error("Invalid record"); 6547 } else { 6548 while (OpNum != Record.size()) { 6549 Value *Op; 6550 unsigned OpTypeID; 6551 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 6552 return error("Invalid record"); 6553 Args.push_back(Op); 6554 ArgTyIDs.push_back(OpTypeID); 6555 } 6556 } 6557 6558 // Upgrade the bundles if needed. 6559 if (!OperandBundles.empty()) 6560 UpgradeOperandBundles(OperandBundles); 6561 6562 I = CallInst::Create(FTy, Callee, Args, OperandBundles); 6563 ResTypeID = getContainedTypeID(FTyID); 6564 OperandBundles.clear(); 6565 InstructionList.push_back(I); 6566 cast<CallInst>(I)->setCallingConv( 6567 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); 6568 CallInst::TailCallKind TCK = CallInst::TCK_None; 6569 if (CCInfo & (1 << bitc::CALL_TAIL)) 6570 TCK = CallInst::TCK_Tail; 6571 if (CCInfo & (1 << bitc::CALL_MUSTTAIL)) 6572 TCK = CallInst::TCK_MustTail; 6573 if (CCInfo & (1 << bitc::CALL_NOTAIL)) 6574 TCK = CallInst::TCK_NoTail; 6575 cast<CallInst>(I)->setTailCallKind(TCK); 6576 cast<CallInst>(I)->setAttributes(PAL); 6577 if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) { 6578 I->deleteValue(); 6579 return Err; 6580 } 6581 if (FMF.any()) { 6582 if (!isa<FPMathOperator>(I)) 6583 return error("Fast-math-flags specified for call without " 6584 "floating-point scalar or vector return type"); 6585 I->setFastMathFlags(FMF); 6586 } 6587 break; 6588 } 6589 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] 6590 if (Record.size() < 3) 6591 return error("Invalid record"); 6592 unsigned OpTyID = Record[0]; 6593 Type *OpTy = getTypeByID(OpTyID); 6594 Value *Op = getValue(Record, 1, NextValueNo, OpTy, OpTyID, CurBB); 6595 ResTypeID = Record[2]; 6596 Type *ResTy = getTypeByID(ResTypeID); 6597 if (!OpTy || !Op || !ResTy) 6598 return error("Invalid record"); 6599 I = new VAArgInst(Op, ResTy); 6600 InstructionList.push_back(I); 6601 break; 6602 } 6603 6604 case bitc::FUNC_CODE_OPERAND_BUNDLE: { 6605 // A call or an invoke can be optionally prefixed with some variable 6606 // number of operand bundle blocks. These blocks are read into 6607 // OperandBundles and consumed at the next call or invoke instruction. 6608 6609 if (Record.empty() || Record[0] >= BundleTags.size()) 6610 return error("Invalid record"); 6611 6612 std::vector<Value *> Inputs; 6613 6614 unsigned OpNum = 1; 6615 while (OpNum != Record.size()) { 6616 Value *Op; 6617 unsigned OpTypeID; 6618 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 6619 return error("Invalid record"); 6620 Inputs.push_back(Op); 6621 } 6622 6623 OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs)); 6624 continue; 6625 } 6626 6627 case bitc::FUNC_CODE_INST_FREEZE: { // FREEZE: [opty,opval] 6628 unsigned OpNum = 0; 6629 Value *Op = nullptr; 6630 unsigned OpTypeID; 6631 if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB)) 6632 return error("Invalid record"); 6633 if (OpNum != Record.size()) 6634 return error("Invalid record"); 6635 6636 I = new FreezeInst(Op); 6637 ResTypeID = OpTypeID; 6638 InstructionList.push_back(I); 6639 break; 6640 } 6641 } 6642 6643 // Add instruction to end of current BB. If there is no current BB, reject 6644 // this file. 6645 if (!CurBB) { 6646 I->deleteValue(); 6647 return error("Invalid instruction with no BB"); 6648 } 6649 if (!OperandBundles.empty()) { 6650 I->deleteValue(); 6651 return error("Operand bundles found with no consumer"); 6652 } 6653 I->insertInto(CurBB, CurBB->end()); 6654 6655 // If this was a terminator instruction, move to the next block. 6656 if (I->isTerminator()) { 6657 ++CurBBNo; 6658 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; 6659 } 6660 6661 // Non-void values get registered in the value table for future use. 6662 if (!I->getType()->isVoidTy()) { 6663 assert(I->getType() == getTypeByID(ResTypeID) && 6664 "Incorrect result type ID"); 6665 if (Error Err = ValueList.assignValue(NextValueNo++, I, ResTypeID)) 6666 return Err; 6667 } 6668 } 6669 6670 OutOfRecordLoop: 6671 6672 if (!OperandBundles.empty()) 6673 return error("Operand bundles found with no consumer"); 6674 6675 // Check the function list for unresolved values. 6676 if (Argument *A = dyn_cast<Argument>(ValueList.back())) { 6677 if (!A->getParent()) { 6678 // We found at least one unresolved value. Nuke them all to avoid leaks. 6679 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ 6680 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { 6681 A->replaceAllUsesWith(PoisonValue::get(A->getType())); 6682 delete A; 6683 } 6684 } 6685 return error("Never resolved value found in function"); 6686 } 6687 } 6688 6689 // Unexpected unresolved metadata about to be dropped. 6690 if (MDLoader->hasFwdRefs()) 6691 return error("Invalid function metadata: outgoing forward refs"); 6692 6693 if (PhiConstExprBB) 6694 PhiConstExprBB->eraseFromParent(); 6695 6696 for (const auto &Pair : ConstExprEdgeBBs) { 6697 BasicBlock *From = Pair.first.first; 6698 BasicBlock *To = Pair.first.second; 6699 BasicBlock *EdgeBB = Pair.second; 6700 BranchInst::Create(To, EdgeBB); 6701 From->getTerminator()->replaceSuccessorWith(To, EdgeBB); 6702 To->replacePhiUsesWith(From, EdgeBB); 6703 EdgeBB->moveBefore(To); 6704 } 6705 6706 // Trim the value list down to the size it was before we parsed this function. 6707 ValueList.shrinkTo(ModuleValueListSize); 6708 MDLoader->shrinkTo(ModuleMDLoaderSize); 6709 std::vector<BasicBlock*>().swap(FunctionBBs); 6710 return Error::success(); 6711 } 6712 6713 /// Find the function body in the bitcode stream 6714 Error BitcodeReader::findFunctionInStream( 6715 Function *F, 6716 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { 6717 while (DeferredFunctionInfoIterator->second == 0) { 6718 // This is the fallback handling for the old format bitcode that 6719 // didn't contain the function index in the VST, or when we have 6720 // an anonymous function which would not have a VST entry. 6721 // Assert that we have one of those two cases. 6722 assert(VSTOffset == 0 || !F->hasName()); 6723 // Parse the next body in the stream and set its position in the 6724 // DeferredFunctionInfo map. 6725 if (Error Err = rememberAndSkipFunctionBodies()) 6726 return Err; 6727 } 6728 return Error::success(); 6729 } 6730 6731 SyncScope::ID BitcodeReader::getDecodedSyncScopeID(unsigned Val) { 6732 if (Val == SyncScope::SingleThread || Val == SyncScope::System) 6733 return SyncScope::ID(Val); 6734 if (Val >= SSIDs.size()) 6735 return SyncScope::System; // Map unknown synchronization scopes to system. 6736 return SSIDs[Val]; 6737 } 6738 6739 //===----------------------------------------------------------------------===// 6740 // GVMaterializer implementation 6741 //===----------------------------------------------------------------------===// 6742 6743 Error BitcodeReader::materialize(GlobalValue *GV) { 6744 Function *F = dyn_cast<Function>(GV); 6745 // If it's not a function or is already material, ignore the request. 6746 if (!F || !F->isMaterializable()) 6747 return Error::success(); 6748 6749 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); 6750 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); 6751 // If its position is recorded as 0, its body is somewhere in the stream 6752 // but we haven't seen it yet. 6753 if (DFII->second == 0) 6754 if (Error Err = findFunctionInStream(F, DFII)) 6755 return Err; 6756 6757 // Materialize metadata before parsing any function bodies. 6758 if (Error Err = materializeMetadata()) 6759 return Err; 6760 6761 // Move the bit stream to the saved position of the deferred function body. 6762 if (Error JumpFailed = Stream.JumpToBit(DFII->second)) 6763 return JumpFailed; 6764 6765 // Set the debug info mode to "new", forcing a mismatch between 6766 // module and function debug modes. This is okay because we'll convert 6767 // everything back to the old mode after parsing. 6768 // FIXME: Remove this once all tools support RemoveDIs. 6769 F->IsNewDbgInfoFormat = true; 6770 6771 if (Error Err = parseFunctionBody(F)) 6772 return Err; 6773 F->setIsMaterializable(false); 6774 6775 // Convert new debug info records into intrinsics. 6776 // FIXME: Remove this once all tools support RemoveDIs. 6777 F->convertFromNewDbgValues(); 6778 6779 if (StripDebugInfo) 6780 stripDebugInfo(*F); 6781 6782 // Upgrade any old intrinsic calls in the function. 6783 for (auto &I : UpgradedIntrinsics) { 6784 for (User *U : llvm::make_early_inc_range(I.first->materialized_users())) 6785 if (CallInst *CI = dyn_cast<CallInst>(U)) 6786 UpgradeIntrinsicCall(CI, I.second); 6787 } 6788 6789 // Finish fn->subprogram upgrade for materialized functions. 6790 if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) 6791 F->setSubprogram(SP); 6792 6793 // Check if the TBAA Metadata are valid, otherwise we will need to strip them. 6794 if (!MDLoader->isStrippingTBAA()) { 6795 for (auto &I : instructions(F)) { 6796 MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa); 6797 if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA)) 6798 continue; 6799 MDLoader->setStripTBAA(true); 6800 stripTBAA(F->getParent()); 6801 } 6802 } 6803 6804 for (auto &I : instructions(F)) { 6805 // "Upgrade" older incorrect branch weights by dropping them. 6806 if (auto *MD = I.getMetadata(LLVMContext::MD_prof)) { 6807 if (MD->getOperand(0) != nullptr && isa<MDString>(MD->getOperand(0))) { 6808 MDString *MDS = cast<MDString>(MD->getOperand(0)); 6809 StringRef ProfName = MDS->getString(); 6810 // Check consistency of !prof branch_weights metadata. 6811 if (!ProfName.equals("branch_weights")) 6812 continue; 6813 unsigned ExpectedNumOperands = 0; 6814 if (BranchInst *BI = dyn_cast<BranchInst>(&I)) 6815 ExpectedNumOperands = BI->getNumSuccessors(); 6816 else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I)) 6817 ExpectedNumOperands = SI->getNumSuccessors(); 6818 else if (isa<CallInst>(&I)) 6819 ExpectedNumOperands = 1; 6820 else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I)) 6821 ExpectedNumOperands = IBI->getNumDestinations(); 6822 else if (isa<SelectInst>(&I)) 6823 ExpectedNumOperands = 2; 6824 else 6825 continue; // ignore and continue. 6826 6827 // If branch weight doesn't match, just strip branch weight. 6828 if (MD->getNumOperands() != 1 + ExpectedNumOperands) 6829 I.setMetadata(LLVMContext::MD_prof, nullptr); 6830 } 6831 } 6832 6833 // Remove incompatible attributes on function calls. 6834 if (auto *CI = dyn_cast<CallBase>(&I)) { 6835 CI->removeRetAttrs(AttributeFuncs::typeIncompatible( 6836 CI->getFunctionType()->getReturnType())); 6837 6838 for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ++ArgNo) 6839 CI->removeParamAttrs(ArgNo, AttributeFuncs::typeIncompatible( 6840 CI->getArgOperand(ArgNo)->getType())); 6841 } 6842 } 6843 6844 // Look for functions that rely on old function attribute behavior. 6845 UpgradeFunctionAttributes(*F); 6846 6847 // Bring in any functions that this function forward-referenced via 6848 // blockaddresses. 6849 return materializeForwardReferencedFunctions(); 6850 } 6851 6852 Error BitcodeReader::materializeModule() { 6853 if (Error Err = materializeMetadata()) 6854 return Err; 6855 6856 // Promise to materialize all forward references. 6857 WillMaterializeAllForwardRefs = true; 6858 6859 // Iterate over the module, deserializing any functions that are still on 6860 // disk. 6861 for (Function &F : *TheModule) { 6862 if (Error Err = materialize(&F)) 6863 return Err; 6864 } 6865 // At this point, if there are any function bodies, parse the rest of 6866 // the bits in the module past the last function block we have recorded 6867 // through either lazy scanning or the VST. 6868 if (LastFunctionBlockBit || NextUnreadBit) 6869 if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit 6870 ? LastFunctionBlockBit 6871 : NextUnreadBit)) 6872 return Err; 6873 6874 // Check that all block address forward references got resolved (as we 6875 // promised above). 6876 if (!BasicBlockFwdRefs.empty()) 6877 return error("Never resolved function from blockaddress"); 6878 6879 // Upgrade any intrinsic calls that slipped through (should not happen!) and 6880 // delete the old functions to clean up. We can't do this unless the entire 6881 // module is materialized because there could always be another function body 6882 // with calls to the old function. 6883 for (auto &I : UpgradedIntrinsics) { 6884 for (auto *U : I.first->users()) { 6885 if (CallInst *CI = dyn_cast<CallInst>(U)) 6886 UpgradeIntrinsicCall(CI, I.second); 6887 } 6888 if (!I.first->use_empty()) 6889 I.first->replaceAllUsesWith(I.second); 6890 I.first->eraseFromParent(); 6891 } 6892 UpgradedIntrinsics.clear(); 6893 6894 UpgradeDebugInfo(*TheModule); 6895 6896 UpgradeModuleFlags(*TheModule); 6897 6898 UpgradeARCRuntime(*TheModule); 6899 6900 return Error::success(); 6901 } 6902 6903 std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { 6904 return IdentifiedStructTypes; 6905 } 6906 6907 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( 6908 BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex, 6909 StringRef ModulePath, std::function<bool(GlobalValue::GUID)> IsPrevailing) 6910 : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex), 6911 ModulePath(ModulePath), IsPrevailing(IsPrevailing) {} 6912 6913 void ModuleSummaryIndexBitcodeReader::addThisModule() { 6914 TheIndex.addModule(ModulePath); 6915 } 6916 6917 ModuleSummaryIndex::ModuleInfo * 6918 ModuleSummaryIndexBitcodeReader::getThisModule() { 6919 return TheIndex.getModule(ModulePath); 6920 } 6921 6922 template <bool AllowNullValueInfo> 6923 std::tuple<ValueInfo, GlobalValue::GUID, GlobalValue::GUID> 6924 ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) { 6925 auto VGI = ValueIdToValueInfoMap[ValueId]; 6926 // We can have a null value info for memprof callsite info records in 6927 // distributed ThinLTO index files when the callee function summary is not 6928 // included in the index. The bitcode writer records 0 in that case, 6929 // and the caller of this helper will set AllowNullValueInfo to true. 6930 assert(AllowNullValueInfo || std::get<0>(VGI)); 6931 return VGI; 6932 } 6933 6934 void ModuleSummaryIndexBitcodeReader::setValueGUID( 6935 uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage, 6936 StringRef SourceFileName) { 6937 std::string GlobalId = 6938 GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName); 6939 auto ValueGUID = GlobalValue::getGUID(GlobalId); 6940 auto OriginalNameID = ValueGUID; 6941 if (GlobalValue::isLocalLinkage(Linkage)) 6942 OriginalNameID = GlobalValue::getGUID(ValueName); 6943 if (PrintSummaryGUIDs) 6944 dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " 6945 << ValueName << "\n"; 6946 6947 // UseStrtab is false for legacy summary formats and value names are 6948 // created on stack. In that case we save the name in a string saver in 6949 // the index so that the value name can be recorded. 6950 ValueIdToValueInfoMap[ValueID] = std::make_tuple( 6951 TheIndex.getOrInsertValueInfo( 6952 ValueGUID, UseStrtab ? ValueName : TheIndex.saveString(ValueName)), 6953 OriginalNameID, ValueGUID); 6954 } 6955 6956 // Specialized value symbol table parser used when reading module index 6957 // blocks where we don't actually create global values. The parsed information 6958 // is saved in the bitcode reader for use when later parsing summaries. 6959 Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable( 6960 uint64_t Offset, 6961 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) { 6962 // With a strtab the VST is not required to parse the summary. 6963 if (UseStrtab) 6964 return Error::success(); 6965 6966 assert(Offset > 0 && "Expected non-zero VST offset"); 6967 Expected<uint64_t> MaybeCurrentBit = jumpToValueSymbolTable(Offset, Stream); 6968 if (!MaybeCurrentBit) 6969 return MaybeCurrentBit.takeError(); 6970 uint64_t CurrentBit = MaybeCurrentBit.get(); 6971 6972 if (Error Err = Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 6973 return Err; 6974 6975 SmallVector<uint64_t, 64> Record; 6976 6977 // Read all the records for this value table. 6978 SmallString<128> ValueName; 6979 6980 while (true) { 6981 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 6982 if (!MaybeEntry) 6983 return MaybeEntry.takeError(); 6984 BitstreamEntry Entry = MaybeEntry.get(); 6985 6986 switch (Entry.Kind) { 6987 case BitstreamEntry::SubBlock: // Handled for us already. 6988 case BitstreamEntry::Error: 6989 return error("Malformed block"); 6990 case BitstreamEntry::EndBlock: 6991 // Done parsing VST, jump back to wherever we came from. 6992 if (Error JumpFailed = Stream.JumpToBit(CurrentBit)) 6993 return JumpFailed; 6994 return Error::success(); 6995 case BitstreamEntry::Record: 6996 // The interesting case. 6997 break; 6998 } 6999 7000 // Read a record. 7001 Record.clear(); 7002 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 7003 if (!MaybeRecord) 7004 return MaybeRecord.takeError(); 7005 switch (MaybeRecord.get()) { 7006 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records). 7007 break; 7008 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 7009 if (convertToString(Record, 1, ValueName)) 7010 return error("Invalid record"); 7011 unsigned ValueID = Record[0]; 7012 assert(!SourceFileName.empty()); 7013 auto VLI = ValueIdToLinkageMap.find(ValueID); 7014 assert(VLI != ValueIdToLinkageMap.end() && 7015 "No linkage found for VST entry?"); 7016 auto Linkage = VLI->second; 7017 setValueGUID(ValueID, ValueName, Linkage, SourceFileName); 7018 ValueName.clear(); 7019 break; 7020 } 7021 case bitc::VST_CODE_FNENTRY: { 7022 // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 7023 if (convertToString(Record, 2, ValueName)) 7024 return error("Invalid record"); 7025 unsigned ValueID = Record[0]; 7026 assert(!SourceFileName.empty()); 7027 auto VLI = ValueIdToLinkageMap.find(ValueID); 7028 assert(VLI != ValueIdToLinkageMap.end() && 7029 "No linkage found for VST entry?"); 7030 auto Linkage = VLI->second; 7031 setValueGUID(ValueID, ValueName, Linkage, SourceFileName); 7032 ValueName.clear(); 7033 break; 7034 } 7035 case bitc::VST_CODE_COMBINED_ENTRY: { 7036 // VST_CODE_COMBINED_ENTRY: [valueid, refguid] 7037 unsigned ValueID = Record[0]; 7038 GlobalValue::GUID RefGUID = Record[1]; 7039 // The "original name", which is the second value of the pair will be 7040 // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index. 7041 ValueIdToValueInfoMap[ValueID] = std::make_tuple( 7042 TheIndex.getOrInsertValueInfo(RefGUID), RefGUID, RefGUID); 7043 break; 7044 } 7045 } 7046 } 7047 } 7048 7049 // Parse just the blocks needed for building the index out of the module. 7050 // At the end of this routine the module Index is populated with a map 7051 // from global value id to GlobalValueSummary objects. 7052 Error ModuleSummaryIndexBitcodeReader::parseModule() { 7053 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 7054 return Err; 7055 7056 SmallVector<uint64_t, 64> Record; 7057 DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap; 7058 unsigned ValueId = 0; 7059 7060 // Read the index for this module. 7061 while (true) { 7062 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 7063 if (!MaybeEntry) 7064 return MaybeEntry.takeError(); 7065 llvm::BitstreamEntry Entry = MaybeEntry.get(); 7066 7067 switch (Entry.Kind) { 7068 case BitstreamEntry::Error: 7069 return error("Malformed block"); 7070 case BitstreamEntry::EndBlock: 7071 return Error::success(); 7072 7073 case BitstreamEntry::SubBlock: 7074 switch (Entry.ID) { 7075 default: // Skip unknown content. 7076 if (Error Err = Stream.SkipBlock()) 7077 return Err; 7078 break; 7079 case bitc::BLOCKINFO_BLOCK_ID: 7080 // Need to parse these to get abbrev ids (e.g. for VST) 7081 if (Error Err = readBlockInfo()) 7082 return Err; 7083 break; 7084 case bitc::VALUE_SYMTAB_BLOCK_ID: 7085 // Should have been parsed earlier via VSTOffset, unless there 7086 // is no summary section. 7087 assert(((SeenValueSymbolTable && VSTOffset > 0) || 7088 !SeenGlobalValSummary) && 7089 "Expected early VST parse via VSTOffset record"); 7090 if (Error Err = Stream.SkipBlock()) 7091 return Err; 7092 break; 7093 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: 7094 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: 7095 // Add the module if it is a per-module index (has a source file name). 7096 if (!SourceFileName.empty()) 7097 addThisModule(); 7098 assert(!SeenValueSymbolTable && 7099 "Already read VST when parsing summary block?"); 7100 // We might not have a VST if there were no values in the 7101 // summary. An empty summary block generated when we are 7102 // performing ThinLTO compiles so we don't later invoke 7103 // the regular LTO process on them. 7104 if (VSTOffset > 0) { 7105 if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap)) 7106 return Err; 7107 SeenValueSymbolTable = true; 7108 } 7109 SeenGlobalValSummary = true; 7110 if (Error Err = parseEntireSummary(Entry.ID)) 7111 return Err; 7112 break; 7113 case bitc::MODULE_STRTAB_BLOCK_ID: 7114 if (Error Err = parseModuleStringTable()) 7115 return Err; 7116 break; 7117 } 7118 continue; 7119 7120 case BitstreamEntry::Record: { 7121 Record.clear(); 7122 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 7123 if (!MaybeBitCode) 7124 return MaybeBitCode.takeError(); 7125 switch (MaybeBitCode.get()) { 7126 default: 7127 break; // Default behavior, ignore unknown content. 7128 case bitc::MODULE_CODE_VERSION: { 7129 if (Error Err = parseVersionRecord(Record).takeError()) 7130 return Err; 7131 break; 7132 } 7133 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 7134 case bitc::MODULE_CODE_SOURCE_FILENAME: { 7135 SmallString<128> ValueName; 7136 if (convertToString(Record, 0, ValueName)) 7137 return error("Invalid record"); 7138 SourceFileName = ValueName.c_str(); 7139 break; 7140 } 7141 /// MODULE_CODE_HASH: [5*i32] 7142 case bitc::MODULE_CODE_HASH: { 7143 if (Record.size() != 5) 7144 return error("Invalid hash length " + Twine(Record.size()).str()); 7145 auto &Hash = getThisModule()->second; 7146 int Pos = 0; 7147 for (auto &Val : Record) { 7148 assert(!(Val >> 32) && "Unexpected high bits set"); 7149 Hash[Pos++] = Val; 7150 } 7151 break; 7152 } 7153 /// MODULE_CODE_VSTOFFSET: [offset] 7154 case bitc::MODULE_CODE_VSTOFFSET: 7155 if (Record.empty()) 7156 return error("Invalid record"); 7157 // Note that we subtract 1 here because the offset is relative to one 7158 // word before the start of the identification or module block, which 7159 // was historically always the start of the regular bitcode header. 7160 VSTOffset = Record[0] - 1; 7161 break; 7162 // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...] 7163 // v1 FUNCTION: [type, callingconv, isproto, linkage, ...] 7164 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...] 7165 // v2: [strtab offset, strtab size, v1] 7166 case bitc::MODULE_CODE_GLOBALVAR: 7167 case bitc::MODULE_CODE_FUNCTION: 7168 case bitc::MODULE_CODE_ALIAS: { 7169 StringRef Name; 7170 ArrayRef<uint64_t> GVRecord; 7171 std::tie(Name, GVRecord) = readNameFromStrtab(Record); 7172 if (GVRecord.size() <= 3) 7173 return error("Invalid record"); 7174 uint64_t RawLinkage = GVRecord[3]; 7175 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 7176 if (!UseStrtab) { 7177 ValueIdToLinkageMap[ValueId++] = Linkage; 7178 break; 7179 } 7180 7181 setValueGUID(ValueId++, Name, Linkage, SourceFileName); 7182 break; 7183 } 7184 } 7185 } 7186 continue; 7187 } 7188 } 7189 } 7190 7191 std::vector<ValueInfo> 7192 ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) { 7193 std::vector<ValueInfo> Ret; 7194 Ret.reserve(Record.size()); 7195 for (uint64_t RefValueId : Record) 7196 Ret.push_back(std::get<0>(getValueInfoFromValueId(RefValueId))); 7197 return Ret; 7198 } 7199 7200 std::vector<FunctionSummary::EdgeTy> 7201 ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record, 7202 bool IsOldProfileFormat, 7203 bool HasProfile, bool HasRelBF) { 7204 std::vector<FunctionSummary::EdgeTy> Ret; 7205 Ret.reserve(Record.size()); 7206 for (unsigned I = 0, E = Record.size(); I != E; ++I) { 7207 CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; 7208 bool HasTailCall = false; 7209 uint64_t RelBF = 0; 7210 ValueInfo Callee = std::get<0>(getValueInfoFromValueId(Record[I])); 7211 if (IsOldProfileFormat) { 7212 I += 1; // Skip old callsitecount field 7213 if (HasProfile) 7214 I += 1; // Skip old profilecount field 7215 } else if (HasProfile) 7216 std::tie(Hotness, HasTailCall) = 7217 getDecodedHotnessCallEdgeInfo(Record[++I]); 7218 else if (HasRelBF) 7219 getDecodedRelBFCallEdgeInfo(Record[++I], RelBF, HasTailCall); 7220 Ret.push_back(FunctionSummary::EdgeTy{ 7221 Callee, CalleeInfo(Hotness, HasTailCall, RelBF)}); 7222 } 7223 return Ret; 7224 } 7225 7226 static void 7227 parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot, 7228 WholeProgramDevirtResolution &Wpd) { 7229 uint64_t ArgNum = Record[Slot++]; 7230 WholeProgramDevirtResolution::ByArg &B = 7231 Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}]; 7232 Slot += ArgNum; 7233 7234 B.TheKind = 7235 static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]); 7236 B.Info = Record[Slot++]; 7237 B.Byte = Record[Slot++]; 7238 B.Bit = Record[Slot++]; 7239 } 7240 7241 static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record, 7242 StringRef Strtab, size_t &Slot, 7243 TypeIdSummary &TypeId) { 7244 uint64_t Id = Record[Slot++]; 7245 WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id]; 7246 7247 Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]); 7248 Wpd.SingleImplName = {Strtab.data() + Record[Slot], 7249 static_cast<size_t>(Record[Slot + 1])}; 7250 Slot += 2; 7251 7252 uint64_t ResByArgNum = Record[Slot++]; 7253 for (uint64_t I = 0; I != ResByArgNum; ++I) 7254 parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd); 7255 } 7256 7257 static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record, 7258 StringRef Strtab, 7259 ModuleSummaryIndex &TheIndex) { 7260 size_t Slot = 0; 7261 TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary( 7262 {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])}); 7263 Slot += 2; 7264 7265 TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]); 7266 TypeId.TTRes.SizeM1BitWidth = Record[Slot++]; 7267 TypeId.TTRes.AlignLog2 = Record[Slot++]; 7268 TypeId.TTRes.SizeM1 = Record[Slot++]; 7269 TypeId.TTRes.BitMask = Record[Slot++]; 7270 TypeId.TTRes.InlineBits = Record[Slot++]; 7271 7272 while (Slot < Record.size()) 7273 parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId); 7274 } 7275 7276 std::vector<FunctionSummary::ParamAccess> 7277 ModuleSummaryIndexBitcodeReader::parseParamAccesses(ArrayRef<uint64_t> Record) { 7278 auto ReadRange = [&]() { 7279 APInt Lower(FunctionSummary::ParamAccess::RangeWidth, 7280 BitcodeReader::decodeSignRotatedValue(Record.front())); 7281 Record = Record.drop_front(); 7282 APInt Upper(FunctionSummary::ParamAccess::RangeWidth, 7283 BitcodeReader::decodeSignRotatedValue(Record.front())); 7284 Record = Record.drop_front(); 7285 ConstantRange Range{Lower, Upper}; 7286 assert(!Range.isFullSet()); 7287 assert(!Range.isUpperSignWrapped()); 7288 return Range; 7289 }; 7290 7291 std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; 7292 while (!Record.empty()) { 7293 PendingParamAccesses.emplace_back(); 7294 FunctionSummary::ParamAccess &ParamAccess = PendingParamAccesses.back(); 7295 ParamAccess.ParamNo = Record.front(); 7296 Record = Record.drop_front(); 7297 ParamAccess.Use = ReadRange(); 7298 ParamAccess.Calls.resize(Record.front()); 7299 Record = Record.drop_front(); 7300 for (auto &Call : ParamAccess.Calls) { 7301 Call.ParamNo = Record.front(); 7302 Record = Record.drop_front(); 7303 Call.Callee = std::get<0>(getValueInfoFromValueId(Record.front())); 7304 Record = Record.drop_front(); 7305 Call.Offsets = ReadRange(); 7306 } 7307 } 7308 return PendingParamAccesses; 7309 } 7310 7311 void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo( 7312 ArrayRef<uint64_t> Record, size_t &Slot, 7313 TypeIdCompatibleVtableInfo &TypeId) { 7314 uint64_t Offset = Record[Slot++]; 7315 ValueInfo Callee = std::get<0>(getValueInfoFromValueId(Record[Slot++])); 7316 TypeId.push_back({Offset, Callee}); 7317 } 7318 7319 void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableSummaryRecord( 7320 ArrayRef<uint64_t> Record) { 7321 size_t Slot = 0; 7322 TypeIdCompatibleVtableInfo &TypeId = 7323 TheIndex.getOrInsertTypeIdCompatibleVtableSummary( 7324 {Strtab.data() + Record[Slot], 7325 static_cast<size_t>(Record[Slot + 1])}); 7326 Slot += 2; 7327 7328 while (Slot < Record.size()) 7329 parseTypeIdCompatibleVtableInfo(Record, Slot, TypeId); 7330 } 7331 7332 static void setSpecialRefs(std::vector<ValueInfo> &Refs, unsigned ROCnt, 7333 unsigned WOCnt) { 7334 // Readonly and writeonly refs are in the end of the refs list. 7335 assert(ROCnt + WOCnt <= Refs.size()); 7336 unsigned FirstWORef = Refs.size() - WOCnt; 7337 unsigned RefNo = FirstWORef - ROCnt; 7338 for (; RefNo < FirstWORef; ++RefNo) 7339 Refs[RefNo].setReadOnly(); 7340 for (; RefNo < Refs.size(); ++RefNo) 7341 Refs[RefNo].setWriteOnly(); 7342 } 7343 7344 // Eagerly parse the entire summary block. This populates the GlobalValueSummary 7345 // objects in the index. 7346 Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { 7347 if (Error Err = Stream.EnterSubBlock(ID)) 7348 return Err; 7349 SmallVector<uint64_t, 64> Record; 7350 7351 // Parse version 7352 { 7353 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 7354 if (!MaybeEntry) 7355 return MaybeEntry.takeError(); 7356 BitstreamEntry Entry = MaybeEntry.get(); 7357 7358 if (Entry.Kind != BitstreamEntry::Record) 7359 return error("Invalid Summary Block: record for version expected"); 7360 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 7361 if (!MaybeRecord) 7362 return MaybeRecord.takeError(); 7363 if (MaybeRecord.get() != bitc::FS_VERSION) 7364 return error("Invalid Summary Block: version expected"); 7365 } 7366 const uint64_t Version = Record[0]; 7367 const bool IsOldProfileFormat = Version == 1; 7368 if (Version < 1 || Version > ModuleSummaryIndex::BitcodeSummaryVersion) 7369 return error("Invalid summary version " + Twine(Version) + 7370 ". Version should be in the range [1-" + 7371 Twine(ModuleSummaryIndex::BitcodeSummaryVersion) + 7372 "]."); 7373 Record.clear(); 7374 7375 // Keep around the last seen summary to be used when we see an optional 7376 // "OriginalName" attachement. 7377 GlobalValueSummary *LastSeenSummary = nullptr; 7378 GlobalValue::GUID LastSeenGUID = 0; 7379 7380 // We can expect to see any number of type ID information records before 7381 // each function summary records; these variables store the information 7382 // collected so far so that it can be used to create the summary object. 7383 std::vector<GlobalValue::GUID> PendingTypeTests; 7384 std::vector<FunctionSummary::VFuncId> PendingTypeTestAssumeVCalls, 7385 PendingTypeCheckedLoadVCalls; 7386 std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls, 7387 PendingTypeCheckedLoadConstVCalls; 7388 std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; 7389 7390 std::vector<CallsiteInfo> PendingCallsites; 7391 std::vector<AllocInfo> PendingAllocs; 7392 7393 while (true) { 7394 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 7395 if (!MaybeEntry) 7396 return MaybeEntry.takeError(); 7397 BitstreamEntry Entry = MaybeEntry.get(); 7398 7399 switch (Entry.Kind) { 7400 case BitstreamEntry::SubBlock: // Handled for us already. 7401 case BitstreamEntry::Error: 7402 return error("Malformed block"); 7403 case BitstreamEntry::EndBlock: 7404 return Error::success(); 7405 case BitstreamEntry::Record: 7406 // The interesting case. 7407 break; 7408 } 7409 7410 // Read a record. The record format depends on whether this 7411 // is a per-module index or a combined index file. In the per-module 7412 // case the records contain the associated value's ID for correlation 7413 // with VST entries. In the combined index the correlation is done 7414 // via the bitcode offset of the summary records (which were saved 7415 // in the combined index VST entries). The records also contain 7416 // information used for ThinLTO renaming and importing. 7417 Record.clear(); 7418 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 7419 if (!MaybeBitCode) 7420 return MaybeBitCode.takeError(); 7421 switch (unsigned BitCode = MaybeBitCode.get()) { 7422 default: // Default behavior: ignore. 7423 break; 7424 case bitc::FS_FLAGS: { // [flags] 7425 TheIndex.setFlags(Record[0]); 7426 break; 7427 } 7428 case bitc::FS_VALUE_GUID: { // [valueid, refguid] 7429 uint64_t ValueID = Record[0]; 7430 GlobalValue::GUID RefGUID = Record[1]; 7431 ValueIdToValueInfoMap[ValueID] = std::make_tuple( 7432 TheIndex.getOrInsertValueInfo(RefGUID), RefGUID, RefGUID); 7433 break; 7434 } 7435 // FS_PERMODULE is legacy and does not have support for the tail call flag. 7436 // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs, 7437 // numrefs x valueid, n x (valueid)] 7438 // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs, 7439 // numrefs x valueid, 7440 // n x (valueid, hotness+tailcall flags)] 7441 // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs, 7442 // numrefs x valueid, 7443 // n x (valueid, relblockfreq+tailcall)] 7444 case bitc::FS_PERMODULE: 7445 case bitc::FS_PERMODULE_RELBF: 7446 case bitc::FS_PERMODULE_PROFILE: { 7447 unsigned ValueID = Record[0]; 7448 uint64_t RawFlags = Record[1]; 7449 unsigned InstCount = Record[2]; 7450 uint64_t RawFunFlags = 0; 7451 unsigned NumRefs = Record[3]; 7452 unsigned NumRORefs = 0, NumWORefs = 0; 7453 int RefListStartIndex = 4; 7454 if (Version >= 4) { 7455 RawFunFlags = Record[3]; 7456 NumRefs = Record[4]; 7457 RefListStartIndex = 5; 7458 if (Version >= 5) { 7459 NumRORefs = Record[5]; 7460 RefListStartIndex = 6; 7461 if (Version >= 7) { 7462 NumWORefs = Record[6]; 7463 RefListStartIndex = 7; 7464 } 7465 } 7466 } 7467 7468 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7469 // The module path string ref set in the summary must be owned by the 7470 // index's module string table. Since we don't have a module path 7471 // string table section in the per-module index, we create a single 7472 // module path string table entry with an empty (0) ID to take 7473 // ownership. 7474 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 7475 assert(Record.size() >= RefListStartIndex + NumRefs && 7476 "Record size inconsistent with number of references"); 7477 std::vector<ValueInfo> Refs = makeRefList( 7478 ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 7479 bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE); 7480 bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF); 7481 std::vector<FunctionSummary::EdgeTy> Calls = makeCallList( 7482 ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 7483 IsOldProfileFormat, HasProfile, HasRelBF); 7484 setSpecialRefs(Refs, NumRORefs, NumWORefs); 7485 auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID); 7486 // In order to save memory, only record the memprof summaries if this is 7487 // the prevailing copy of a symbol. The linker doesn't resolve local 7488 // linkage values so don't check whether those are prevailing. 7489 auto LT = (GlobalValue::LinkageTypes)Flags.Linkage; 7490 if (IsPrevailing && 7491 !GlobalValue::isLocalLinkage(LT) && 7492 !IsPrevailing(std::get<2>(VIAndOriginalGUID))) { 7493 PendingCallsites.clear(); 7494 PendingAllocs.clear(); 7495 } 7496 auto FS = std::make_unique<FunctionSummary>( 7497 Flags, InstCount, getDecodedFFlags(RawFunFlags), /*EntryCount=*/0, 7498 std::move(Refs), std::move(Calls), std::move(PendingTypeTests), 7499 std::move(PendingTypeTestAssumeVCalls), 7500 std::move(PendingTypeCheckedLoadVCalls), 7501 std::move(PendingTypeTestAssumeConstVCalls), 7502 std::move(PendingTypeCheckedLoadConstVCalls), 7503 std::move(PendingParamAccesses), std::move(PendingCallsites), 7504 std::move(PendingAllocs)); 7505 FS->setModulePath(getThisModule()->first()); 7506 FS->setOriginalName(std::get<1>(VIAndOriginalGUID)); 7507 TheIndex.addGlobalValueSummary(std::get<0>(VIAndOriginalGUID), 7508 std::move(FS)); 7509 break; 7510 } 7511 // FS_ALIAS: [valueid, flags, valueid] 7512 // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as 7513 // they expect all aliasee summaries to be available. 7514 case bitc::FS_ALIAS: { 7515 unsigned ValueID = Record[0]; 7516 uint64_t RawFlags = Record[1]; 7517 unsigned AliaseeID = Record[2]; 7518 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7519 auto AS = std::make_unique<AliasSummary>(Flags); 7520 // The module path string ref set in the summary must be owned by the 7521 // index's module string table. Since we don't have a module path 7522 // string table section in the per-module index, we create a single 7523 // module path string table entry with an empty (0) ID to take 7524 // ownership. 7525 AS->setModulePath(getThisModule()->first()); 7526 7527 auto AliaseeVI = std::get<0>(getValueInfoFromValueId(AliaseeID)); 7528 auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, ModulePath); 7529 if (!AliaseeInModule) 7530 return error("Alias expects aliasee summary to be parsed"); 7531 AS->setAliasee(AliaseeVI, AliaseeInModule); 7532 7533 auto GUID = getValueInfoFromValueId(ValueID); 7534 AS->setOriginalName(std::get<1>(GUID)); 7535 TheIndex.addGlobalValueSummary(std::get<0>(GUID), std::move(AS)); 7536 break; 7537 } 7538 // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, n x valueid] 7539 case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: { 7540 unsigned ValueID = Record[0]; 7541 uint64_t RawFlags = Record[1]; 7542 unsigned RefArrayStart = 2; 7543 GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, 7544 /* WriteOnly */ false, 7545 /* Constant */ false, 7546 GlobalObject::VCallVisibilityPublic); 7547 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7548 if (Version >= 5) { 7549 GVF = getDecodedGVarFlags(Record[2]); 7550 RefArrayStart = 3; 7551 } 7552 std::vector<ValueInfo> Refs = 7553 makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart)); 7554 auto FS = 7555 std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); 7556 FS->setModulePath(getThisModule()->first()); 7557 auto GUID = getValueInfoFromValueId(ValueID); 7558 FS->setOriginalName(std::get<1>(GUID)); 7559 TheIndex.addGlobalValueSummary(std::get<0>(GUID), std::move(FS)); 7560 break; 7561 } 7562 // FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, 7563 // numrefs, numrefs x valueid, 7564 // n x (valueid, offset)] 7565 case bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS: { 7566 unsigned ValueID = Record[0]; 7567 uint64_t RawFlags = Record[1]; 7568 GlobalVarSummary::GVarFlags GVF = getDecodedGVarFlags(Record[2]); 7569 unsigned NumRefs = Record[3]; 7570 unsigned RefListStartIndex = 4; 7571 unsigned VTableListStartIndex = RefListStartIndex + NumRefs; 7572 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7573 std::vector<ValueInfo> Refs = makeRefList( 7574 ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 7575 VTableFuncList VTableFuncs; 7576 for (unsigned I = VTableListStartIndex, E = Record.size(); I != E; ++I) { 7577 ValueInfo Callee = std::get<0>(getValueInfoFromValueId(Record[I])); 7578 uint64_t Offset = Record[++I]; 7579 VTableFuncs.push_back({Callee, Offset}); 7580 } 7581 auto VS = 7582 std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); 7583 VS->setModulePath(getThisModule()->first()); 7584 VS->setVTableFuncs(VTableFuncs); 7585 auto GUID = getValueInfoFromValueId(ValueID); 7586 VS->setOriginalName(std::get<1>(GUID)); 7587 TheIndex.addGlobalValueSummary(std::get<0>(GUID), std::move(VS)); 7588 break; 7589 } 7590 // FS_COMBINED is legacy and does not have support for the tail call flag. 7591 // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs, 7592 // numrefs x valueid, n x (valueid)] 7593 // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs, 7594 // numrefs x valueid, 7595 // n x (valueid, hotness+tailcall flags)] 7596 case bitc::FS_COMBINED: 7597 case bitc::FS_COMBINED_PROFILE: { 7598 unsigned ValueID = Record[0]; 7599 uint64_t ModuleId = Record[1]; 7600 uint64_t RawFlags = Record[2]; 7601 unsigned InstCount = Record[3]; 7602 uint64_t RawFunFlags = 0; 7603 uint64_t EntryCount = 0; 7604 unsigned NumRefs = Record[4]; 7605 unsigned NumRORefs = 0, NumWORefs = 0; 7606 int RefListStartIndex = 5; 7607 7608 if (Version >= 4) { 7609 RawFunFlags = Record[4]; 7610 RefListStartIndex = 6; 7611 size_t NumRefsIndex = 5; 7612 if (Version >= 5) { 7613 unsigned NumRORefsOffset = 1; 7614 RefListStartIndex = 7; 7615 if (Version >= 6) { 7616 NumRefsIndex = 6; 7617 EntryCount = Record[5]; 7618 RefListStartIndex = 8; 7619 if (Version >= 7) { 7620 RefListStartIndex = 9; 7621 NumWORefs = Record[8]; 7622 NumRORefsOffset = 2; 7623 } 7624 } 7625 NumRORefs = Record[RefListStartIndex - NumRORefsOffset]; 7626 } 7627 NumRefs = Record[NumRefsIndex]; 7628 } 7629 7630 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7631 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 7632 assert(Record.size() >= RefListStartIndex + NumRefs && 7633 "Record size inconsistent with number of references"); 7634 std::vector<ValueInfo> Refs = makeRefList( 7635 ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 7636 bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE); 7637 std::vector<FunctionSummary::EdgeTy> Edges = makeCallList( 7638 ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 7639 IsOldProfileFormat, HasProfile, false); 7640 ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID)); 7641 setSpecialRefs(Refs, NumRORefs, NumWORefs); 7642 auto FS = std::make_unique<FunctionSummary>( 7643 Flags, InstCount, getDecodedFFlags(RawFunFlags), EntryCount, 7644 std::move(Refs), std::move(Edges), std::move(PendingTypeTests), 7645 std::move(PendingTypeTestAssumeVCalls), 7646 std::move(PendingTypeCheckedLoadVCalls), 7647 std::move(PendingTypeTestAssumeConstVCalls), 7648 std::move(PendingTypeCheckedLoadConstVCalls), 7649 std::move(PendingParamAccesses), std::move(PendingCallsites), 7650 std::move(PendingAllocs)); 7651 LastSeenSummary = FS.get(); 7652 LastSeenGUID = VI.getGUID(); 7653 FS->setModulePath(ModuleIdMap[ModuleId]); 7654 TheIndex.addGlobalValueSummary(VI, std::move(FS)); 7655 break; 7656 } 7657 // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid] 7658 // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as 7659 // they expect all aliasee summaries to be available. 7660 case bitc::FS_COMBINED_ALIAS: { 7661 unsigned ValueID = Record[0]; 7662 uint64_t ModuleId = Record[1]; 7663 uint64_t RawFlags = Record[2]; 7664 unsigned AliaseeValueId = Record[3]; 7665 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7666 auto AS = std::make_unique<AliasSummary>(Flags); 7667 LastSeenSummary = AS.get(); 7668 AS->setModulePath(ModuleIdMap[ModuleId]); 7669 7670 auto AliaseeVI = std::get<0>(getValueInfoFromValueId(AliaseeValueId)); 7671 auto AliaseeInModule = TheIndex.findSummaryInModule(AliaseeVI, AS->modulePath()); 7672 AS->setAliasee(AliaseeVI, AliaseeInModule); 7673 7674 ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID)); 7675 LastSeenGUID = VI.getGUID(); 7676 TheIndex.addGlobalValueSummary(VI, std::move(AS)); 7677 break; 7678 } 7679 // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid] 7680 case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: { 7681 unsigned ValueID = Record[0]; 7682 uint64_t ModuleId = Record[1]; 7683 uint64_t RawFlags = Record[2]; 7684 unsigned RefArrayStart = 3; 7685 GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, 7686 /* WriteOnly */ false, 7687 /* Constant */ false, 7688 GlobalObject::VCallVisibilityPublic); 7689 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 7690 if (Version >= 5) { 7691 GVF = getDecodedGVarFlags(Record[3]); 7692 RefArrayStart = 4; 7693 } 7694 std::vector<ValueInfo> Refs = 7695 makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart)); 7696 auto FS = 7697 std::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs)); 7698 LastSeenSummary = FS.get(); 7699 FS->setModulePath(ModuleIdMap[ModuleId]); 7700 ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID)); 7701 LastSeenGUID = VI.getGUID(); 7702 TheIndex.addGlobalValueSummary(VI, std::move(FS)); 7703 break; 7704 } 7705 // FS_COMBINED_ORIGINAL_NAME: [original_name] 7706 case bitc::FS_COMBINED_ORIGINAL_NAME: { 7707 uint64_t OriginalName = Record[0]; 7708 if (!LastSeenSummary) 7709 return error("Name attachment that does not follow a combined record"); 7710 LastSeenSummary->setOriginalName(OriginalName); 7711 TheIndex.addOriginalName(LastSeenGUID, OriginalName); 7712 // Reset the LastSeenSummary 7713 LastSeenSummary = nullptr; 7714 LastSeenGUID = 0; 7715 break; 7716 } 7717 case bitc::FS_TYPE_TESTS: 7718 assert(PendingTypeTests.empty()); 7719 llvm::append_range(PendingTypeTests, Record); 7720 break; 7721 7722 case bitc::FS_TYPE_TEST_ASSUME_VCALLS: 7723 assert(PendingTypeTestAssumeVCalls.empty()); 7724 for (unsigned I = 0; I != Record.size(); I += 2) 7725 PendingTypeTestAssumeVCalls.push_back({Record[I], Record[I+1]}); 7726 break; 7727 7728 case bitc::FS_TYPE_CHECKED_LOAD_VCALLS: 7729 assert(PendingTypeCheckedLoadVCalls.empty()); 7730 for (unsigned I = 0; I != Record.size(); I += 2) 7731 PendingTypeCheckedLoadVCalls.push_back({Record[I], Record[I+1]}); 7732 break; 7733 7734 case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL: 7735 PendingTypeTestAssumeConstVCalls.push_back( 7736 {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); 7737 break; 7738 7739 case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL: 7740 PendingTypeCheckedLoadConstVCalls.push_back( 7741 {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}}); 7742 break; 7743 7744 case bitc::FS_CFI_FUNCTION_DEFS: { 7745 std::set<std::string> &CfiFunctionDefs = TheIndex.cfiFunctionDefs(); 7746 for (unsigned I = 0; I != Record.size(); I += 2) 7747 CfiFunctionDefs.insert( 7748 {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); 7749 break; 7750 } 7751 7752 case bitc::FS_CFI_FUNCTION_DECLS: { 7753 std::set<std::string> &CfiFunctionDecls = TheIndex.cfiFunctionDecls(); 7754 for (unsigned I = 0; I != Record.size(); I += 2) 7755 CfiFunctionDecls.insert( 7756 {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])}); 7757 break; 7758 } 7759 7760 case bitc::FS_TYPE_ID: 7761 parseTypeIdSummaryRecord(Record, Strtab, TheIndex); 7762 break; 7763 7764 case bitc::FS_TYPE_ID_METADATA: 7765 parseTypeIdCompatibleVtableSummaryRecord(Record); 7766 break; 7767 7768 case bitc::FS_BLOCK_COUNT: 7769 TheIndex.addBlockCount(Record[0]); 7770 break; 7771 7772 case bitc::FS_PARAM_ACCESS: { 7773 PendingParamAccesses = parseParamAccesses(Record); 7774 break; 7775 } 7776 7777 case bitc::FS_STACK_IDS: { // [n x stackid] 7778 // Save stack ids in the reader to consult when adding stack ids from the 7779 // lists in the stack node and alloc node entries. 7780 StackIds = ArrayRef<uint64_t>(Record); 7781 break; 7782 } 7783 7784 case bitc::FS_PERMODULE_CALLSITE_INFO: { 7785 unsigned ValueID = Record[0]; 7786 SmallVector<unsigned> StackIdList; 7787 for (auto R = Record.begin() + 1; R != Record.end(); R++) { 7788 assert(*R < StackIds.size()); 7789 StackIdList.push_back(TheIndex.addOrGetStackIdIndex(StackIds[*R])); 7790 } 7791 ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID)); 7792 PendingCallsites.push_back(CallsiteInfo({VI, std::move(StackIdList)})); 7793 break; 7794 } 7795 7796 case bitc::FS_COMBINED_CALLSITE_INFO: { 7797 auto RecordIter = Record.begin(); 7798 unsigned ValueID = *RecordIter++; 7799 unsigned NumStackIds = *RecordIter++; 7800 unsigned NumVersions = *RecordIter++; 7801 assert(Record.size() == 3 + NumStackIds + NumVersions); 7802 SmallVector<unsigned> StackIdList; 7803 for (unsigned J = 0; J < NumStackIds; J++) { 7804 assert(*RecordIter < StackIds.size()); 7805 StackIdList.push_back( 7806 TheIndex.addOrGetStackIdIndex(StackIds[*RecordIter++])); 7807 } 7808 SmallVector<unsigned> Versions; 7809 for (unsigned J = 0; J < NumVersions; J++) 7810 Versions.push_back(*RecordIter++); 7811 ValueInfo VI = std::get<0>( 7812 getValueInfoFromValueId</*AllowNullValueInfo*/ true>(ValueID)); 7813 PendingCallsites.push_back( 7814 CallsiteInfo({VI, std::move(Versions), std::move(StackIdList)})); 7815 break; 7816 } 7817 7818 case bitc::FS_PERMODULE_ALLOC_INFO: { 7819 unsigned I = 0; 7820 std::vector<MIBInfo> MIBs; 7821 while (I < Record.size()) { 7822 assert(Record.size() - I >= 2); 7823 AllocationType AllocType = (AllocationType)Record[I++]; 7824 unsigned NumStackEntries = Record[I++]; 7825 assert(Record.size() - I >= NumStackEntries); 7826 SmallVector<unsigned> StackIdList; 7827 for (unsigned J = 0; J < NumStackEntries; J++) { 7828 assert(Record[I] < StackIds.size()); 7829 StackIdList.push_back( 7830 TheIndex.addOrGetStackIdIndex(StackIds[Record[I++]])); 7831 } 7832 MIBs.push_back(MIBInfo(AllocType, std::move(StackIdList))); 7833 } 7834 PendingAllocs.push_back(AllocInfo(std::move(MIBs))); 7835 break; 7836 } 7837 7838 case bitc::FS_COMBINED_ALLOC_INFO: { 7839 unsigned I = 0; 7840 std::vector<MIBInfo> MIBs; 7841 unsigned NumMIBs = Record[I++]; 7842 unsigned NumVersions = Record[I++]; 7843 unsigned MIBsRead = 0; 7844 while (MIBsRead++ < NumMIBs) { 7845 assert(Record.size() - I >= 2); 7846 AllocationType AllocType = (AllocationType)Record[I++]; 7847 unsigned NumStackEntries = Record[I++]; 7848 assert(Record.size() - I >= NumStackEntries); 7849 SmallVector<unsigned> StackIdList; 7850 for (unsigned J = 0; J < NumStackEntries; J++) { 7851 assert(Record[I] < StackIds.size()); 7852 StackIdList.push_back( 7853 TheIndex.addOrGetStackIdIndex(StackIds[Record[I++]])); 7854 } 7855 MIBs.push_back(MIBInfo(AllocType, std::move(StackIdList))); 7856 } 7857 assert(Record.size() - I >= NumVersions); 7858 SmallVector<uint8_t> Versions; 7859 for (unsigned J = 0; J < NumVersions; J++) 7860 Versions.push_back(Record[I++]); 7861 PendingAllocs.push_back( 7862 AllocInfo(std::move(Versions), std::move(MIBs))); 7863 break; 7864 } 7865 } 7866 } 7867 llvm_unreachable("Exit infinite loop"); 7868 } 7869 7870 // Parse the module string table block into the Index. 7871 // This populates the ModulePathStringTable map in the index. 7872 Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() { 7873 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID)) 7874 return Err; 7875 7876 SmallVector<uint64_t, 64> Record; 7877 7878 SmallString<128> ModulePath; 7879 ModuleSummaryIndex::ModuleInfo *LastSeenModule = nullptr; 7880 7881 while (true) { 7882 Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); 7883 if (!MaybeEntry) 7884 return MaybeEntry.takeError(); 7885 BitstreamEntry Entry = MaybeEntry.get(); 7886 7887 switch (Entry.Kind) { 7888 case BitstreamEntry::SubBlock: // Handled for us already. 7889 case BitstreamEntry::Error: 7890 return error("Malformed block"); 7891 case BitstreamEntry::EndBlock: 7892 return Error::success(); 7893 case BitstreamEntry::Record: 7894 // The interesting case. 7895 break; 7896 } 7897 7898 Record.clear(); 7899 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record); 7900 if (!MaybeRecord) 7901 return MaybeRecord.takeError(); 7902 switch (MaybeRecord.get()) { 7903 default: // Default behavior: ignore. 7904 break; 7905 case bitc::MST_CODE_ENTRY: { 7906 // MST_ENTRY: [modid, namechar x N] 7907 uint64_t ModuleId = Record[0]; 7908 7909 if (convertToString(Record, 1, ModulePath)) 7910 return error("Invalid record"); 7911 7912 LastSeenModule = TheIndex.addModule(ModulePath); 7913 ModuleIdMap[ModuleId] = LastSeenModule->first(); 7914 7915 ModulePath.clear(); 7916 break; 7917 } 7918 /// MST_CODE_HASH: [5*i32] 7919 case bitc::MST_CODE_HASH: { 7920 if (Record.size() != 5) 7921 return error("Invalid hash length " + Twine(Record.size()).str()); 7922 if (!LastSeenModule) 7923 return error("Invalid hash that does not follow a module path"); 7924 int Pos = 0; 7925 for (auto &Val : Record) { 7926 assert(!(Val >> 32) && "Unexpected high bits set"); 7927 LastSeenModule->second[Pos++] = Val; 7928 } 7929 // Reset LastSeenModule to avoid overriding the hash unexpectedly. 7930 LastSeenModule = nullptr; 7931 break; 7932 } 7933 } 7934 } 7935 llvm_unreachable("Exit infinite loop"); 7936 } 7937 7938 namespace { 7939 7940 // FIXME: This class is only here to support the transition to llvm::Error. It 7941 // will be removed once this transition is complete. Clients should prefer to 7942 // deal with the Error value directly, rather than converting to error_code. 7943 class BitcodeErrorCategoryType : public std::error_category { 7944 const char *name() const noexcept override { 7945 return "llvm.bitcode"; 7946 } 7947 7948 std::string message(int IE) const override { 7949 BitcodeError E = static_cast<BitcodeError>(IE); 7950 switch (E) { 7951 case BitcodeError::CorruptedBitcode: 7952 return "Corrupted bitcode"; 7953 } 7954 llvm_unreachable("Unknown error type!"); 7955 } 7956 }; 7957 7958 } // end anonymous namespace 7959 7960 const std::error_category &llvm::BitcodeErrorCategory() { 7961 static BitcodeErrorCategoryType ErrorCategory; 7962 return ErrorCategory; 7963 } 7964 7965 static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream, 7966 unsigned Block, unsigned RecordID) { 7967 if (Error Err = Stream.EnterSubBlock(Block)) 7968 return std::move(Err); 7969 7970 StringRef Strtab; 7971 while (true) { 7972 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 7973 if (!MaybeEntry) 7974 return MaybeEntry.takeError(); 7975 llvm::BitstreamEntry Entry = MaybeEntry.get(); 7976 7977 switch (Entry.Kind) { 7978 case BitstreamEntry::EndBlock: 7979 return Strtab; 7980 7981 case BitstreamEntry::Error: 7982 return error("Malformed block"); 7983 7984 case BitstreamEntry::SubBlock: 7985 if (Error Err = Stream.SkipBlock()) 7986 return std::move(Err); 7987 break; 7988 7989 case BitstreamEntry::Record: 7990 StringRef Blob; 7991 SmallVector<uint64_t, 1> Record; 7992 Expected<unsigned> MaybeRecord = 7993 Stream.readRecord(Entry.ID, Record, &Blob); 7994 if (!MaybeRecord) 7995 return MaybeRecord.takeError(); 7996 if (MaybeRecord.get() == RecordID) 7997 Strtab = Blob; 7998 break; 7999 } 8000 } 8001 } 8002 8003 //===----------------------------------------------------------------------===// 8004 // External interface 8005 //===----------------------------------------------------------------------===// 8006 8007 Expected<std::vector<BitcodeModule>> 8008 llvm::getBitcodeModuleList(MemoryBufferRef Buffer) { 8009 auto FOrErr = getBitcodeFileContents(Buffer); 8010 if (!FOrErr) 8011 return FOrErr.takeError(); 8012 return std::move(FOrErr->Mods); 8013 } 8014 8015 Expected<BitcodeFileContents> 8016 llvm::getBitcodeFileContents(MemoryBufferRef Buffer) { 8017 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 8018 if (!StreamOrErr) 8019 return StreamOrErr.takeError(); 8020 BitstreamCursor &Stream = *StreamOrErr; 8021 8022 BitcodeFileContents F; 8023 while (true) { 8024 uint64_t BCBegin = Stream.getCurrentByteNo(); 8025 8026 // We may be consuming bitcode from a client that leaves garbage at the end 8027 // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to 8028 // the end that there cannot possibly be another module, stop looking. 8029 if (BCBegin + 8 >= Stream.getBitcodeBytes().size()) 8030 return F; 8031 8032 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 8033 if (!MaybeEntry) 8034 return MaybeEntry.takeError(); 8035 llvm::BitstreamEntry Entry = MaybeEntry.get(); 8036 8037 switch (Entry.Kind) { 8038 case BitstreamEntry::EndBlock: 8039 case BitstreamEntry::Error: 8040 return error("Malformed block"); 8041 8042 case BitstreamEntry::SubBlock: { 8043 uint64_t IdentificationBit = -1ull; 8044 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { 8045 IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8; 8046 if (Error Err = Stream.SkipBlock()) 8047 return std::move(Err); 8048 8049 { 8050 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 8051 if (!MaybeEntry) 8052 return MaybeEntry.takeError(); 8053 Entry = MaybeEntry.get(); 8054 } 8055 8056 if (Entry.Kind != BitstreamEntry::SubBlock || 8057 Entry.ID != bitc::MODULE_BLOCK_ID) 8058 return error("Malformed block"); 8059 } 8060 8061 if (Entry.ID == bitc::MODULE_BLOCK_ID) { 8062 uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8; 8063 if (Error Err = Stream.SkipBlock()) 8064 return std::move(Err); 8065 8066 F.Mods.push_back({Stream.getBitcodeBytes().slice( 8067 BCBegin, Stream.getCurrentByteNo() - BCBegin), 8068 Buffer.getBufferIdentifier(), IdentificationBit, 8069 ModuleBit}); 8070 continue; 8071 } 8072 8073 if (Entry.ID == bitc::STRTAB_BLOCK_ID) { 8074 Expected<StringRef> Strtab = 8075 readBlobInRecord(Stream, bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB); 8076 if (!Strtab) 8077 return Strtab.takeError(); 8078 // This string table is used by every preceding bitcode module that does 8079 // not have its own string table. A bitcode file may have multiple 8080 // string tables if it was created by binary concatenation, for example 8081 // with "llvm-cat -b". 8082 for (BitcodeModule &I : llvm::reverse(F.Mods)) { 8083 if (!I.Strtab.empty()) 8084 break; 8085 I.Strtab = *Strtab; 8086 } 8087 // Similarly, the string table is used by every preceding symbol table; 8088 // normally there will be just one unless the bitcode file was created 8089 // by binary concatenation. 8090 if (!F.Symtab.empty() && F.StrtabForSymtab.empty()) 8091 F.StrtabForSymtab = *Strtab; 8092 continue; 8093 } 8094 8095 if (Entry.ID == bitc::SYMTAB_BLOCK_ID) { 8096 Expected<StringRef> SymtabOrErr = 8097 readBlobInRecord(Stream, bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB); 8098 if (!SymtabOrErr) 8099 return SymtabOrErr.takeError(); 8100 8101 // We can expect the bitcode file to have multiple symbol tables if it 8102 // was created by binary concatenation. In that case we silently 8103 // ignore any subsequent symbol tables, which is fine because this is a 8104 // low level function. The client is expected to notice that the number 8105 // of modules in the symbol table does not match the number of modules 8106 // in the input file and regenerate the symbol table. 8107 if (F.Symtab.empty()) 8108 F.Symtab = *SymtabOrErr; 8109 continue; 8110 } 8111 8112 if (Error Err = Stream.SkipBlock()) 8113 return std::move(Err); 8114 continue; 8115 } 8116 case BitstreamEntry::Record: 8117 if (Error E = Stream.skipRecord(Entry.ID).takeError()) 8118 return std::move(E); 8119 continue; 8120 } 8121 } 8122 } 8123 8124 /// Get a lazy one-at-time loading module from bitcode. 8125 /// 8126 /// This isn't always used in a lazy context. In particular, it's also used by 8127 /// \a parseModule(). If this is truly lazy, then we need to eagerly pull 8128 /// in forward-referenced functions from block address references. 8129 /// 8130 /// \param[in] MaterializeAll Set to \c true if we should materialize 8131 /// everything. 8132 Expected<std::unique_ptr<Module>> 8133 BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, 8134 bool ShouldLazyLoadMetadata, bool IsImporting, 8135 ParserCallbacks Callbacks) { 8136 BitstreamCursor Stream(Buffer); 8137 8138 std::string ProducerIdentification; 8139 if (IdentificationBit != -1ull) { 8140 if (Error JumpFailed = Stream.JumpToBit(IdentificationBit)) 8141 return std::move(JumpFailed); 8142 if (Error E = 8143 readIdentificationBlock(Stream).moveInto(ProducerIdentification)) 8144 return std::move(E); 8145 } 8146 8147 if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 8148 return std::move(JumpFailed); 8149 auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification, 8150 Context); 8151 8152 std::unique_ptr<Module> M = 8153 std::make_unique<Module>(ModuleIdentifier, Context); 8154 M->setMaterializer(R); 8155 8156 // Delay parsing Metadata if ShouldLazyLoadMetadata is true. 8157 if (Error Err = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, 8158 IsImporting, Callbacks)) 8159 return std::move(Err); 8160 8161 if (MaterializeAll) { 8162 // Read in the entire module, and destroy the BitcodeReader. 8163 if (Error Err = M->materializeAll()) 8164 return std::move(Err); 8165 } else { 8166 // Resolve forward references from blockaddresses. 8167 if (Error Err = R->materializeForwardReferencedFunctions()) 8168 return std::move(Err); 8169 } 8170 8171 return std::move(M); 8172 } 8173 8174 Expected<std::unique_ptr<Module>> 8175 BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, 8176 bool IsImporting, ParserCallbacks Callbacks) { 8177 return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting, 8178 Callbacks); 8179 } 8180 8181 // Parse the specified bitcode buffer and merge the index into CombinedIndex. 8182 // We don't use ModuleIdentifier here because the client may need to control the 8183 // module path used in the combined summary (e.g. when reading summaries for 8184 // regular LTO modules). 8185 Error BitcodeModule::readSummary( 8186 ModuleSummaryIndex &CombinedIndex, StringRef ModulePath, 8187 std::function<bool(GlobalValue::GUID)> IsPrevailing) { 8188 BitstreamCursor Stream(Buffer); 8189 if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 8190 return JumpFailed; 8191 8192 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex, 8193 ModulePath, IsPrevailing); 8194 return R.parseModule(); 8195 } 8196 8197 // Parse the specified bitcode buffer, returning the function info index. 8198 Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() { 8199 BitstreamCursor Stream(Buffer); 8200 if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 8201 return std::move(JumpFailed); 8202 8203 auto Index = std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false); 8204 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index, 8205 ModuleIdentifier, 0); 8206 8207 if (Error Err = R.parseModule()) 8208 return std::move(Err); 8209 8210 return std::move(Index); 8211 } 8212 8213 static Expected<std::pair<bool, bool>> 8214 getEnableSplitLTOUnitAndUnifiedFlag(BitstreamCursor &Stream, 8215 unsigned ID, 8216 BitcodeLTOInfo <OInfo) { 8217 if (Error Err = Stream.EnterSubBlock(ID)) 8218 return std::move(Err); 8219 SmallVector<uint64_t, 64> Record; 8220 8221 while (true) { 8222 BitstreamEntry Entry; 8223 std::pair<bool, bool> Result = {false,false}; 8224 if (Error E = Stream.advanceSkippingSubblocks().moveInto(Entry)) 8225 return std::move(E); 8226 8227 switch (Entry.Kind) { 8228 case BitstreamEntry::SubBlock: // Handled for us already. 8229 case BitstreamEntry::Error: 8230 return error("Malformed block"); 8231 case BitstreamEntry::EndBlock: { 8232 // If no flags record found, set both flags to false. 8233 return Result; 8234 } 8235 case BitstreamEntry::Record: 8236 // The interesting case. 8237 break; 8238 } 8239 8240 // Look for the FS_FLAGS record. 8241 Record.clear(); 8242 Expected<unsigned> MaybeBitCode = Stream.readRecord(Entry.ID, Record); 8243 if (!MaybeBitCode) 8244 return MaybeBitCode.takeError(); 8245 switch (MaybeBitCode.get()) { 8246 default: // Default behavior: ignore. 8247 break; 8248 case bitc::FS_FLAGS: { // [flags] 8249 uint64_t Flags = Record[0]; 8250 // Scan flags. 8251 assert(Flags <= 0x2ff && "Unexpected bits in flag"); 8252 8253 bool EnableSplitLTOUnit = Flags & 0x8; 8254 bool UnifiedLTO = Flags & 0x200; 8255 Result = {EnableSplitLTOUnit, UnifiedLTO}; 8256 8257 return Result; 8258 } 8259 } 8260 } 8261 llvm_unreachable("Exit infinite loop"); 8262 } 8263 8264 // Check if the given bitcode buffer contains a global value summary block. 8265 Expected<BitcodeLTOInfo> BitcodeModule::getLTOInfo() { 8266 BitstreamCursor Stream(Buffer); 8267 if (Error JumpFailed = Stream.JumpToBit(ModuleBit)) 8268 return std::move(JumpFailed); 8269 8270 if (Error Err = Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 8271 return std::move(Err); 8272 8273 while (true) { 8274 llvm::BitstreamEntry Entry; 8275 if (Error E = Stream.advance().moveInto(Entry)) 8276 return std::move(E); 8277 8278 switch (Entry.Kind) { 8279 case BitstreamEntry::Error: 8280 return error("Malformed block"); 8281 case BitstreamEntry::EndBlock: 8282 return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/false, 8283 /*EnableSplitLTOUnit=*/false, /*UnifiedLTO=*/false}; 8284 8285 case BitstreamEntry::SubBlock: 8286 if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) { 8287 BitcodeLTOInfo LTOInfo; 8288 Expected<std::pair<bool, bool>> Flags = 8289 getEnableSplitLTOUnitAndUnifiedFlag(Stream, Entry.ID, LTOInfo); 8290 if (!Flags) 8291 return Flags.takeError(); 8292 std::tie(LTOInfo.EnableSplitLTOUnit, LTOInfo.UnifiedLTO) = Flags.get(); 8293 LTOInfo.IsThinLTO = true; 8294 LTOInfo.HasSummary = true; 8295 return LTOInfo; 8296 } 8297 8298 if (Entry.ID == bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID) { 8299 BitcodeLTOInfo LTOInfo; 8300 Expected<std::pair<bool, bool>> Flags = 8301 getEnableSplitLTOUnitAndUnifiedFlag(Stream, Entry.ID, LTOInfo); 8302 if (!Flags) 8303 return Flags.takeError(); 8304 std::tie(LTOInfo.EnableSplitLTOUnit, LTOInfo.UnifiedLTO) = Flags.get(); 8305 LTOInfo.IsThinLTO = false; 8306 LTOInfo.HasSummary = true; 8307 return LTOInfo; 8308 } 8309 8310 // Ignore other sub-blocks. 8311 if (Error Err = Stream.SkipBlock()) 8312 return std::move(Err); 8313 continue; 8314 8315 case BitstreamEntry::Record: 8316 if (Expected<unsigned> StreamFailed = Stream.skipRecord(Entry.ID)) 8317 continue; 8318 else 8319 return StreamFailed.takeError(); 8320 } 8321 } 8322 } 8323 8324 static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) { 8325 Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer); 8326 if (!MsOrErr) 8327 return MsOrErr.takeError(); 8328 8329 if (MsOrErr->size() != 1) 8330 return error("Expected a single module"); 8331 8332 return (*MsOrErr)[0]; 8333 } 8334 8335 Expected<std::unique_ptr<Module>> 8336 llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, 8337 bool ShouldLazyLoadMetadata, bool IsImporting, 8338 ParserCallbacks Callbacks) { 8339 Expected<BitcodeModule> BM = getSingleModule(Buffer); 8340 if (!BM) 8341 return BM.takeError(); 8342 8343 return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting, 8344 Callbacks); 8345 } 8346 8347 Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule( 8348 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, 8349 bool ShouldLazyLoadMetadata, bool IsImporting, ParserCallbacks Callbacks) { 8350 auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata, 8351 IsImporting, Callbacks); 8352 if (MOrErr) 8353 (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer)); 8354 return MOrErr; 8355 } 8356 8357 Expected<std::unique_ptr<Module>> 8358 BitcodeModule::parseModule(LLVMContext &Context, ParserCallbacks Callbacks) { 8359 return getModuleImpl(Context, true, false, false, Callbacks); 8360 // TODO: Restore the use-lists to the in-memory state when the bitcode was 8361 // written. We must defer until the Module has been fully materialized. 8362 } 8363 8364 Expected<std::unique_ptr<Module>> 8365 llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, 8366 ParserCallbacks Callbacks) { 8367 Expected<BitcodeModule> BM = getSingleModule(Buffer); 8368 if (!BM) 8369 return BM.takeError(); 8370 8371 return BM->parseModule(Context, Callbacks); 8372 } 8373 8374 Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) { 8375 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 8376 if (!StreamOrErr) 8377 return StreamOrErr.takeError(); 8378 8379 return readTriple(*StreamOrErr); 8380 } 8381 8382 Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) { 8383 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 8384 if (!StreamOrErr) 8385 return StreamOrErr.takeError(); 8386 8387 return hasObjCCategory(*StreamOrErr); 8388 } 8389 8390 Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) { 8391 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 8392 if (!StreamOrErr) 8393 return StreamOrErr.takeError(); 8394 8395 return readIdentificationCode(*StreamOrErr); 8396 } 8397 8398 Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer, 8399 ModuleSummaryIndex &CombinedIndex) { 8400 Expected<BitcodeModule> BM = getSingleModule(Buffer); 8401 if (!BM) 8402 return BM.takeError(); 8403 8404 return BM->readSummary(CombinedIndex, BM->getModuleIdentifier()); 8405 } 8406 8407 Expected<std::unique_ptr<ModuleSummaryIndex>> 8408 llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) { 8409 Expected<BitcodeModule> BM = getSingleModule(Buffer); 8410 if (!BM) 8411 return BM.takeError(); 8412 8413 return BM->getSummary(); 8414 } 8415 8416 Expected<BitcodeLTOInfo> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer) { 8417 Expected<BitcodeModule> BM = getSingleModule(Buffer); 8418 if (!BM) 8419 return BM.takeError(); 8420 8421 return BM->getLTOInfo(); 8422 } 8423 8424 Expected<std::unique_ptr<ModuleSummaryIndex>> 8425 llvm::getModuleSummaryIndexForFile(StringRef Path, 8426 bool IgnoreEmptyThinLTOIndexFile) { 8427 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 8428 MemoryBuffer::getFileOrSTDIN(Path); 8429 if (!FileOrErr) 8430 return errorCodeToError(FileOrErr.getError()); 8431 if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize()) 8432 return nullptr; 8433 return getModuleSummaryIndex(**FileOrErr); 8434 } 8435