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