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