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