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