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