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