1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Bitcode/BitcodeReader.h" 11 #include "MetadataLoader.h" 12 #include "ValueList.h" 13 14 #include "llvm/ADT/APFloat.h" 15 #include "llvm/ADT/APInt.h" 16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/None.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/SmallString.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/ADT/Triple.h" 24 #include "llvm/ADT/Twine.h" 25 #include "llvm/Bitcode/BitstreamReader.h" 26 #include "llvm/Bitcode/LLVMBitCodes.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/CallSite.h" 33 #include "llvm/IR/Comdat.h" 34 #include "llvm/IR/Constant.h" 35 #include "llvm/IR/Constants.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/DiagnosticInfo.h" 41 #include "llvm/IR/DiagnosticPrinter.h" 42 #include "llvm/IR/Function.h" 43 #include "llvm/IR/GlobalAlias.h" 44 #include "llvm/IR/GlobalIFunc.h" 45 #include "llvm/IR/GlobalIndirectSymbol.h" 46 #include "llvm/IR/GlobalObject.h" 47 #include "llvm/IR/GlobalValue.h" 48 #include "llvm/IR/GlobalVariable.h" 49 #include "llvm/IR/GVMaterializer.h" 50 #include "llvm/IR/InlineAsm.h" 51 #include "llvm/IR/InstIterator.h" 52 #include "llvm/IR/InstrTypes.h" 53 #include "llvm/IR/Instruction.h" 54 #include "llvm/IR/Instructions.h" 55 #include "llvm/IR/Intrinsics.h" 56 #include "llvm/IR/LLVMContext.h" 57 #include "llvm/IR/Module.h" 58 #include "llvm/IR/ModuleSummaryIndex.h" 59 #include "llvm/IR/OperandTraits.h" 60 #include "llvm/IR/Operator.h" 61 #include "llvm/IR/TrackingMDRef.h" 62 #include "llvm/IR/Type.h" 63 #include "llvm/IR/ValueHandle.h" 64 #include "llvm/IR/Verifier.h" 65 #include "llvm/Support/AtomicOrdering.h" 66 #include "llvm/Support/Casting.h" 67 #include "llvm/Support/CommandLine.h" 68 #include "llvm/Support/Compiler.h" 69 #include "llvm/Support/Debug.h" 70 #include "llvm/Support/Error.h" 71 #include "llvm/Support/ErrorHandling.h" 72 #include "llvm/Support/ManagedStatic.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 <limits> 81 #include <map> 82 #include <memory> 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 namespace { 97 98 enum { 99 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex 100 }; 101 102 Error error(const Twine &Message) { 103 return make_error<StringError>( 104 Message, make_error_code(BitcodeError::CorruptedBitcode)); 105 } 106 107 /// Helper to read the header common to all bitcode files. 108 bool hasValidBitcodeHeader(BitstreamCursor &Stream) { 109 // Sniff for the signature. 110 if (!Stream.canSkipToPos(4) || 111 Stream.Read(8) != 'B' || 112 Stream.Read(8) != 'C' || 113 Stream.Read(4) != 0x0 || 114 Stream.Read(4) != 0xC || 115 Stream.Read(4) != 0xE || 116 Stream.Read(4) != 0xD) 117 return false; 118 return true; 119 } 120 121 Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) { 122 const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart(); 123 const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize(); 124 125 if (Buffer.getBufferSize() & 3) 126 return error("Invalid bitcode signature"); 127 128 // If we have a wrapper header, parse it and ignore the non-bc file contents. 129 // The magic number is 0x0B17C0DE stored in little endian. 130 if (isBitcodeWrapper(BufPtr, BufEnd)) 131 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) 132 return error("Invalid bitcode wrapper header"); 133 134 BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd)); 135 if (!hasValidBitcodeHeader(Stream)) 136 return error("Invalid bitcode signature"); 137 138 return std::move(Stream); 139 } 140 141 /// Convert a string from a record into an std::string, return true on failure. 142 template <typename StrTy> 143 static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, 144 StrTy &Result) { 145 if (Idx > Record.size()) 146 return true; 147 148 for (unsigned i = Idx, e = Record.size(); i != e; ++i) 149 Result += (char)Record[i]; 150 return false; 151 } 152 153 // Strip all the TBAA attachment for the module. 154 void stripTBAA(Module *M) { 155 for (auto &F : *M) { 156 if (F.isMaterializable()) 157 continue; 158 for (auto &I : instructions(F)) 159 I.setMetadata(LLVMContext::MD_tbaa, nullptr); 160 } 161 } 162 163 /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the 164 /// "epoch" encoded in the bitcode, and return the producer name if any. 165 Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) { 166 if (Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID)) 167 return error("Invalid record"); 168 169 // Read all the records. 170 SmallVector<uint64_t, 64> Record; 171 172 std::string ProducerIdentification; 173 174 while (true) { 175 BitstreamEntry Entry = Stream.advance(); 176 177 switch (Entry.Kind) { 178 default: 179 case BitstreamEntry::Error: 180 return error("Malformed block"); 181 case BitstreamEntry::EndBlock: 182 return ProducerIdentification; 183 case BitstreamEntry::Record: 184 // The interesting case. 185 break; 186 } 187 188 // Read a record. 189 Record.clear(); 190 unsigned BitCode = Stream.readRecord(Entry.ID, Record); 191 switch (BitCode) { 192 default: // Default behavior: reject 193 return error("Invalid value"); 194 case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N] 195 convertToString(Record, 0, ProducerIdentification); 196 break; 197 case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#] 198 unsigned epoch = (unsigned)Record[0]; 199 if (epoch != bitc::BITCODE_CURRENT_EPOCH) { 200 return error( 201 Twine("Incompatible epoch: Bitcode '") + Twine(epoch) + 202 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'"); 203 } 204 } 205 } 206 } 207 } 208 209 Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) { 210 // We expect a number of well-defined blocks, though we don't necessarily 211 // need to understand them all. 212 while (true) { 213 if (Stream.AtEndOfStream()) 214 return ""; 215 216 BitstreamEntry Entry = Stream.advance(); 217 switch (Entry.Kind) { 218 case BitstreamEntry::EndBlock: 219 case BitstreamEntry::Error: 220 return error("Malformed block"); 221 222 case BitstreamEntry::SubBlock: 223 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) 224 return readIdentificationBlock(Stream); 225 226 // Ignore other sub-blocks. 227 if (Stream.SkipBlock()) 228 return error("Malformed block"); 229 continue; 230 case BitstreamEntry::Record: 231 Stream.skipRecord(Entry.ID); 232 continue; 233 } 234 } 235 } 236 237 Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) { 238 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 239 return error("Invalid record"); 240 241 SmallVector<uint64_t, 64> Record; 242 // Read all the records for this module. 243 244 while (true) { 245 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 246 247 switch (Entry.Kind) { 248 case BitstreamEntry::SubBlock: // Handled for us already. 249 case BitstreamEntry::Error: 250 return error("Malformed block"); 251 case BitstreamEntry::EndBlock: 252 return false; 253 case BitstreamEntry::Record: 254 // The interesting case. 255 break; 256 } 257 258 // Read a record. 259 switch (Stream.readRecord(Entry.ID, Record)) { 260 default: 261 break; // Default behavior, ignore unknown content. 262 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 263 std::string S; 264 if (convertToString(Record, 0, S)) 265 return error("Invalid record"); 266 // Check for the i386 and other (x86_64, ARM) conventions 267 if (S.find("__DATA, __objc_catlist") != std::string::npos || 268 S.find("__OBJC,__category") != std::string::npos) 269 return true; 270 break; 271 } 272 } 273 Record.clear(); 274 } 275 llvm_unreachable("Exit infinite loop"); 276 } 277 278 Expected<bool> hasObjCCategory(BitstreamCursor &Stream) { 279 // We expect a number of well-defined blocks, though we don't necessarily 280 // need to understand them all. 281 while (true) { 282 BitstreamEntry Entry = Stream.advance(); 283 284 switch (Entry.Kind) { 285 case BitstreamEntry::Error: 286 return error("Malformed block"); 287 case BitstreamEntry::EndBlock: 288 return false; 289 290 case BitstreamEntry::SubBlock: 291 if (Entry.ID == bitc::MODULE_BLOCK_ID) 292 return hasObjCCategoryInModule(Stream); 293 294 // Ignore other sub-blocks. 295 if (Stream.SkipBlock()) 296 return error("Malformed block"); 297 continue; 298 299 case BitstreamEntry::Record: 300 Stream.skipRecord(Entry.ID); 301 continue; 302 } 303 } 304 } 305 306 Expected<std::string> readModuleTriple(BitstreamCursor &Stream) { 307 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 308 return error("Invalid record"); 309 310 SmallVector<uint64_t, 64> Record; 311 312 std::string Triple; 313 314 // Read all the records for this module. 315 while (true) { 316 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 317 318 switch (Entry.Kind) { 319 case BitstreamEntry::SubBlock: // Handled for us already. 320 case BitstreamEntry::Error: 321 return error("Malformed block"); 322 case BitstreamEntry::EndBlock: 323 return Triple; 324 case BitstreamEntry::Record: 325 // The interesting case. 326 break; 327 } 328 329 // Read a record. 330 switch (Stream.readRecord(Entry.ID, Record)) { 331 default: break; // Default behavior, ignore unknown content. 332 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 333 std::string S; 334 if (convertToString(Record, 0, S)) 335 return error("Invalid record"); 336 Triple = S; 337 break; 338 } 339 } 340 Record.clear(); 341 } 342 llvm_unreachable("Exit infinite loop"); 343 } 344 345 Expected<std::string> readTriple(BitstreamCursor &Stream) { 346 // We expect a number of well-defined blocks, though we don't necessarily 347 // need to understand them all. 348 while (true) { 349 BitstreamEntry Entry = Stream.advance(); 350 351 switch (Entry.Kind) { 352 case BitstreamEntry::Error: 353 return error("Malformed block"); 354 case BitstreamEntry::EndBlock: 355 return ""; 356 357 case BitstreamEntry::SubBlock: 358 if (Entry.ID == bitc::MODULE_BLOCK_ID) 359 return readModuleTriple(Stream); 360 361 // Ignore other sub-blocks. 362 if (Stream.SkipBlock()) 363 return error("Malformed block"); 364 continue; 365 366 case BitstreamEntry::Record: 367 Stream.skipRecord(Entry.ID); 368 continue; 369 } 370 } 371 } 372 373 class BitcodeReaderBase { 374 protected: 375 BitcodeReaderBase(BitstreamCursor Stream) : Stream(std::move(Stream)) { 376 this->Stream.setBlockInfo(&BlockInfo); 377 } 378 379 BitstreamBlockInfo BlockInfo; 380 BitstreamCursor Stream; 381 382 bool readBlockInfo(); 383 384 // Contains an arbitrary and optional string identifying the bitcode producer 385 std::string ProducerIdentification; 386 387 Error error(const Twine &Message); 388 }; 389 390 Error BitcodeReaderBase::error(const Twine &Message) { 391 std::string FullMsg = Message.str(); 392 if (!ProducerIdentification.empty()) 393 FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + 394 LLVM_VERSION_STRING "')"; 395 return ::error(FullMsg); 396 } 397 398 class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { 399 LLVMContext &Context; 400 Module *TheModule = nullptr; 401 // Next offset to start scanning for lazy parsing of function bodies. 402 uint64_t NextUnreadBit = 0; 403 // Last function offset found in the VST. 404 uint64_t LastFunctionBlockBit = 0; 405 bool SeenValueSymbolTable = false; 406 uint64_t VSTOffset = 0; 407 408 std::vector<Type*> TypeList; 409 BitcodeReaderValueList ValueList; 410 Optional<MetadataLoader> MDLoader; 411 std::vector<Comdat *> ComdatList; 412 SmallVector<Instruction *, 64> InstructionList; 413 414 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; 415 std::vector<std::pair<GlobalIndirectSymbol*, unsigned> > IndirectSymbolInits; 416 std::vector<std::pair<Function*, unsigned> > FunctionPrefixes; 417 std::vector<std::pair<Function*, unsigned> > FunctionPrologues; 418 std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFns; 419 420 /// The set of attributes by index. Index zero in the file is for null, and 421 /// is thus not represented here. As such all indices are off by one. 422 std::vector<AttributeSet> MAttributes; 423 424 /// The set of attribute groups. 425 std::map<unsigned, AttributeSet> MAttributeGroups; 426 427 /// While parsing a function body, this is a list of the basic blocks for the 428 /// function. 429 std::vector<BasicBlock*> FunctionBBs; 430 431 // When reading the module header, this list is populated with functions that 432 // have bodies later in the file. 433 std::vector<Function*> FunctionsWithBodies; 434 435 // When intrinsic functions are encountered which require upgrading they are 436 // stored here with their replacement function. 437 typedef DenseMap<Function*, Function*> UpdatedIntrinsicMap; 438 UpdatedIntrinsicMap UpgradedIntrinsics; 439 // Intrinsics which were remangled because of types rename 440 UpdatedIntrinsicMap RemangledIntrinsics; 441 442 // Several operations happen after the module header has been read, but 443 // before function bodies are processed. This keeps track of whether 444 // we've done this yet. 445 bool SeenFirstFunctionBody = false; 446 447 /// When function bodies are initially scanned, this map contains info about 448 /// where to find deferred function body in the stream. 449 DenseMap<Function*, uint64_t> DeferredFunctionInfo; 450 451 /// When Metadata block is initially scanned when parsing the module, we may 452 /// choose to defer parsing of the metadata. This vector contains info about 453 /// which Metadata blocks are deferred. 454 std::vector<uint64_t> DeferredMetadataInfo; 455 456 /// These are basic blocks forward-referenced by block addresses. They are 457 /// inserted lazily into functions when they're loaded. The basic block ID is 458 /// its index into the vector. 459 DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; 460 std::deque<Function *> BasicBlockFwdRefQueue; 461 462 /// Indicates that we are using a new encoding for instruction operands where 463 /// most operands in the current FUNCTION_BLOCK are encoded relative to the 464 /// instruction number, for a more compact encoding. Some instruction 465 /// operands are not relative to the instruction ID: basic block numbers, and 466 /// types. Once the old style function blocks have been phased out, we would 467 /// not need this flag. 468 bool UseRelativeIDs = false; 469 470 /// True if all functions will be materialized, negating the need to process 471 /// (e.g.) blockaddress forward references. 472 bool WillMaterializeAllForwardRefs = false; 473 474 bool StripDebugInfo = false; 475 TBAAVerifier TBAAVerifyHelper; 476 477 std::vector<std::string> BundleTags; 478 479 public: 480 BitcodeReader(BitstreamCursor Stream, StringRef ProducerIdentification, 481 LLVMContext &Context); 482 483 Error materializeForwardReferencedFunctions(); 484 485 Error materialize(GlobalValue *GV) override; 486 Error materializeModule() override; 487 std::vector<StructType *> getIdentifiedStructTypes() const override; 488 489 /// \brief Main interface to parsing a bitcode buffer. 490 /// \returns true if an error occurred. 491 Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata = false, 492 bool IsImporting = false); 493 494 static uint64_t decodeSignRotatedValue(uint64_t V); 495 496 /// Materialize any deferred Metadata block. 497 Error materializeMetadata() override; 498 499 void setStripDebugInfo() override; 500 501 private: 502 std::vector<StructType *> IdentifiedStructTypes; 503 StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); 504 StructType *createIdentifiedStructType(LLVMContext &Context); 505 506 Type *getTypeByID(unsigned ID); 507 508 Value *getFnValueByID(unsigned ID, Type *Ty) { 509 if (Ty && Ty->isMetadataTy()) 510 return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); 511 return ValueList.getValueFwdRef(ID, Ty); 512 } 513 514 Metadata *getFnMetadataByID(unsigned ID) { 515 return MDLoader->getMetadataFwdRef(ID); 516 } 517 518 BasicBlock *getBasicBlock(unsigned ID) const { 519 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID 520 return FunctionBBs[ID]; 521 } 522 523 AttributeSet getAttributes(unsigned i) const { 524 if (i-1 < MAttributes.size()) 525 return MAttributes[i-1]; 526 return AttributeSet(); 527 } 528 529 /// Read a value/type pair out of the specified record from slot 'Slot'. 530 /// Increment Slot past the number of slots used in the record. Return true on 531 /// failure. 532 bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 533 unsigned InstNum, Value *&ResVal) { 534 if (Slot == Record.size()) return true; 535 unsigned ValNo = (unsigned)Record[Slot++]; 536 // Adjust the ValNo, if it was encoded relative to the InstNum. 537 if (UseRelativeIDs) 538 ValNo = InstNum - ValNo; 539 if (ValNo < InstNum) { 540 // If this is not a forward reference, just return the value we already 541 // have. 542 ResVal = getFnValueByID(ValNo, nullptr); 543 return ResVal == nullptr; 544 } 545 if (Slot == Record.size()) 546 return true; 547 548 unsigned TypeNo = (unsigned)Record[Slot++]; 549 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); 550 return ResVal == nullptr; 551 } 552 553 /// Read a value out of the specified record from slot 'Slot'. Increment Slot 554 /// past the number of slots used by the value in the record. Return true if 555 /// there is an error. 556 bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, 557 unsigned InstNum, Type *Ty, Value *&ResVal) { 558 if (getValue(Record, Slot, InstNum, Ty, ResVal)) 559 return true; 560 // All values currently take a single record slot. 561 ++Slot; 562 return false; 563 } 564 565 /// Like popValue, but does not increment the Slot number. 566 bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 567 unsigned InstNum, Type *Ty, Value *&ResVal) { 568 ResVal = getValue(Record, Slot, InstNum, Ty); 569 return ResVal == nullptr; 570 } 571 572 /// Version of getValue that returns ResVal directly, or 0 if there is an 573 /// error. 574 Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 575 unsigned InstNum, Type *Ty) { 576 if (Slot == Record.size()) return nullptr; 577 unsigned ValNo = (unsigned)Record[Slot]; 578 // Adjust the ValNo, if it was encoded relative to the InstNum. 579 if (UseRelativeIDs) 580 ValNo = InstNum - ValNo; 581 return getFnValueByID(ValNo, Ty); 582 } 583 584 /// Like getValue, but decodes signed VBRs. 585 Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot, 586 unsigned InstNum, Type *Ty) { 587 if (Slot == Record.size()) return nullptr; 588 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); 589 // Adjust the ValNo, if it was encoded relative to the InstNum. 590 if (UseRelativeIDs) 591 ValNo = InstNum - ValNo; 592 return getFnValueByID(ValNo, Ty); 593 } 594 595 /// Converts alignment exponent (i.e. power of two (or zero)) to the 596 /// corresponding alignment to use. If alignment is too large, returns 597 /// a corresponding error code. 598 Error parseAlignmentValue(uint64_t Exponent, unsigned &Alignment); 599 Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); 600 Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false); 601 Error parseAttributeBlock(); 602 Error parseAttributeGroupBlock(); 603 Error parseTypeTable(); 604 Error parseTypeTableBody(); 605 Error parseOperandBundleTags(); 606 607 Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record, 608 unsigned NameIndex, Triple &TT); 609 Error parseValueSymbolTable(uint64_t Offset = 0); 610 Error parseConstants(); 611 Error rememberAndSkipFunctionBodies(); 612 Error rememberAndSkipFunctionBody(); 613 /// Save the positions of the Metadata blocks and skip parsing the blocks. 614 Error rememberAndSkipMetadata(); 615 Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType); 616 Error parseFunctionBody(Function *F); 617 Error globalCleanup(); 618 Error resolveGlobalAndIndirectSymbolInits(); 619 Error parseUseLists(); 620 Error findFunctionInStream( 621 Function *F, 622 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); 623 }; 624 625 /// Class to manage reading and parsing function summary index bitcode 626 /// files/sections. 627 class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase { 628 /// The module index built during parsing. 629 ModuleSummaryIndex &TheIndex; 630 631 /// Indicates whether we have encountered a global value summary section 632 /// yet during parsing. 633 bool SeenGlobalValSummary = false; 634 635 /// Indicates whether we have already parsed the VST, used for error checking. 636 bool SeenValueSymbolTable = false; 637 638 /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record. 639 /// Used to enable on-demand parsing of the VST. 640 uint64_t VSTOffset = 0; 641 642 // Map to save ValueId to GUID association that was recorded in the 643 // ValueSymbolTable. It is used after the VST is parsed to convert 644 // call graph edges read from the function summary from referencing 645 // callees by their ValueId to using the GUID instead, which is how 646 // they are recorded in the summary index being built. 647 // We save a second GUID which is the same as the first one, but ignoring the 648 // linkage, i.e. for value other than local linkage they are identical. 649 DenseMap<unsigned, std::pair<GlobalValue::GUID, GlobalValue::GUID>> 650 ValueIdToCallGraphGUIDMap; 651 652 /// Map populated during module path string table parsing, from the 653 /// module ID to a string reference owned by the index's module 654 /// path string table, used to correlate with combined index 655 /// summary records. 656 DenseMap<uint64_t, StringRef> ModuleIdMap; 657 658 /// Original source file name recorded in a bitcode record. 659 std::string SourceFileName; 660 661 public: 662 ModuleSummaryIndexBitcodeReader( 663 BitstreamCursor Stream, ModuleSummaryIndex &TheIndex); 664 665 Error parseModule(StringRef ModulePath); 666 667 private: 668 Error parseValueSymbolTable( 669 uint64_t Offset, 670 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap); 671 std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); 672 std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, 673 bool IsOldProfileFormat, 674 bool HasProfile); 675 Error parseEntireSummary(StringRef ModulePath); 676 Error parseModuleStringTable(); 677 678 std::pair<GlobalValue::GUID, GlobalValue::GUID> 679 getGUIDFromValueId(unsigned ValueId); 680 }; 681 682 } // end anonymous namespace 683 684 std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, 685 Error Err) { 686 if (Err) { 687 std::error_code EC; 688 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { 689 EC = EIB.convertToErrorCode(); 690 Ctx.emitError(EIB.message()); 691 }); 692 return EC; 693 } 694 return std::error_code(); 695 } 696 697 BitcodeReader::BitcodeReader(BitstreamCursor Stream, 698 StringRef ProducerIdentification, 699 LLVMContext &Context) 700 : BitcodeReaderBase(std::move(Stream)), Context(Context), 701 ValueList(Context) { 702 this->ProducerIdentification = ProducerIdentification; 703 } 704 705 Error BitcodeReader::materializeForwardReferencedFunctions() { 706 if (WillMaterializeAllForwardRefs) 707 return Error::success(); 708 709 // Prevent recursion. 710 WillMaterializeAllForwardRefs = true; 711 712 while (!BasicBlockFwdRefQueue.empty()) { 713 Function *F = BasicBlockFwdRefQueue.front(); 714 BasicBlockFwdRefQueue.pop_front(); 715 assert(F && "Expected valid function"); 716 if (!BasicBlockFwdRefs.count(F)) 717 // Already materialized. 718 continue; 719 720 // Check for a function that isn't materializable to prevent an infinite 721 // loop. When parsing a blockaddress stored in a global variable, there 722 // isn't a trivial way to check if a function will have a body without a 723 // linear search through FunctionsWithBodies, so just check it here. 724 if (!F->isMaterializable()) 725 return error("Never resolved function from blockaddress"); 726 727 // Try to materialize F. 728 if (Error Err = materialize(F)) 729 return Err; 730 } 731 assert(BasicBlockFwdRefs.empty() && "Function missing from queue"); 732 733 // Reset state. 734 WillMaterializeAllForwardRefs = false; 735 return Error::success(); 736 } 737 738 //===----------------------------------------------------------------------===// 739 // Helper functions to implement forward reference resolution, etc. 740 //===----------------------------------------------------------------------===// 741 742 static bool hasImplicitComdat(size_t Val) { 743 switch (Val) { 744 default: 745 return false; 746 case 1: // Old WeakAnyLinkage 747 case 4: // Old LinkOnceAnyLinkage 748 case 10: // Old WeakODRLinkage 749 case 11: // Old LinkOnceODRLinkage 750 return true; 751 } 752 } 753 754 static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { 755 switch (Val) { 756 default: // Map unknown/new linkages to external 757 case 0: 758 return GlobalValue::ExternalLinkage; 759 case 2: 760 return GlobalValue::AppendingLinkage; 761 case 3: 762 return GlobalValue::InternalLinkage; 763 case 5: 764 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage 765 case 6: 766 return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage 767 case 7: 768 return GlobalValue::ExternalWeakLinkage; 769 case 8: 770 return GlobalValue::CommonLinkage; 771 case 9: 772 return GlobalValue::PrivateLinkage; 773 case 12: 774 return GlobalValue::AvailableExternallyLinkage; 775 case 13: 776 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage 777 case 14: 778 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage 779 case 15: 780 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage 781 case 1: // Old value with implicit comdat. 782 case 16: 783 return GlobalValue::WeakAnyLinkage; 784 case 10: // Old value with implicit comdat. 785 case 17: 786 return GlobalValue::WeakODRLinkage; 787 case 4: // Old value with implicit comdat. 788 case 18: 789 return GlobalValue::LinkOnceAnyLinkage; 790 case 11: // Old value with implicit comdat. 791 case 19: 792 return GlobalValue::LinkOnceODRLinkage; 793 } 794 } 795 796 /// Decode the flags for GlobalValue in the summary. 797 static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, 798 uint64_t Version) { 799 // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage 800 // like getDecodedLinkage() above. Any future change to the linkage enum and 801 // to getDecodedLinkage() will need to be taken into account here as above. 802 auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits 803 RawFlags = RawFlags >> 4; 804 bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; 805 return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport); 806 } 807 808 static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { 809 switch (Val) { 810 default: // Map unknown visibilities to default. 811 case 0: return GlobalValue::DefaultVisibility; 812 case 1: return GlobalValue::HiddenVisibility; 813 case 2: return GlobalValue::ProtectedVisibility; 814 } 815 } 816 817 static GlobalValue::DLLStorageClassTypes 818 getDecodedDLLStorageClass(unsigned Val) { 819 switch (Val) { 820 default: // Map unknown values to default. 821 case 0: return GlobalValue::DefaultStorageClass; 822 case 1: return GlobalValue::DLLImportStorageClass; 823 case 2: return GlobalValue::DLLExportStorageClass; 824 } 825 } 826 827 static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { 828 switch (Val) { 829 case 0: return GlobalVariable::NotThreadLocal; 830 default: // Map unknown non-zero value to general dynamic. 831 case 1: return GlobalVariable::GeneralDynamicTLSModel; 832 case 2: return GlobalVariable::LocalDynamicTLSModel; 833 case 3: return GlobalVariable::InitialExecTLSModel; 834 case 4: return GlobalVariable::LocalExecTLSModel; 835 } 836 } 837 838 static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { 839 switch (Val) { 840 default: // Map unknown to UnnamedAddr::None. 841 case 0: return GlobalVariable::UnnamedAddr::None; 842 case 1: return GlobalVariable::UnnamedAddr::Global; 843 case 2: return GlobalVariable::UnnamedAddr::Local; 844 } 845 } 846 847 static int getDecodedCastOpcode(unsigned Val) { 848 switch (Val) { 849 default: return -1; 850 case bitc::CAST_TRUNC : return Instruction::Trunc; 851 case bitc::CAST_ZEXT : return Instruction::ZExt; 852 case bitc::CAST_SEXT : return Instruction::SExt; 853 case bitc::CAST_FPTOUI : return Instruction::FPToUI; 854 case bitc::CAST_FPTOSI : return Instruction::FPToSI; 855 case bitc::CAST_UITOFP : return Instruction::UIToFP; 856 case bitc::CAST_SITOFP : return Instruction::SIToFP; 857 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; 858 case bitc::CAST_FPEXT : return Instruction::FPExt; 859 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; 860 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; 861 case bitc::CAST_BITCAST : return Instruction::BitCast; 862 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; 863 } 864 } 865 866 static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { 867 bool IsFP = Ty->isFPOrFPVectorTy(); 868 // BinOps are only valid for int/fp or vector of int/fp types 869 if (!IsFP && !Ty->isIntOrIntVectorTy()) 870 return -1; 871 872 switch (Val) { 873 default: 874 return -1; 875 case bitc::BINOP_ADD: 876 return IsFP ? Instruction::FAdd : Instruction::Add; 877 case bitc::BINOP_SUB: 878 return IsFP ? Instruction::FSub : Instruction::Sub; 879 case bitc::BINOP_MUL: 880 return IsFP ? Instruction::FMul : Instruction::Mul; 881 case bitc::BINOP_UDIV: 882 return IsFP ? -1 : Instruction::UDiv; 883 case bitc::BINOP_SDIV: 884 return IsFP ? Instruction::FDiv : Instruction::SDiv; 885 case bitc::BINOP_UREM: 886 return IsFP ? -1 : Instruction::URem; 887 case bitc::BINOP_SREM: 888 return IsFP ? Instruction::FRem : Instruction::SRem; 889 case bitc::BINOP_SHL: 890 return IsFP ? -1 : Instruction::Shl; 891 case bitc::BINOP_LSHR: 892 return IsFP ? -1 : Instruction::LShr; 893 case bitc::BINOP_ASHR: 894 return IsFP ? -1 : Instruction::AShr; 895 case bitc::BINOP_AND: 896 return IsFP ? -1 : Instruction::And; 897 case bitc::BINOP_OR: 898 return IsFP ? -1 : Instruction::Or; 899 case bitc::BINOP_XOR: 900 return IsFP ? -1 : Instruction::Xor; 901 } 902 } 903 904 static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { 905 switch (Val) { 906 default: return AtomicRMWInst::BAD_BINOP; 907 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; 908 case bitc::RMW_ADD: return AtomicRMWInst::Add; 909 case bitc::RMW_SUB: return AtomicRMWInst::Sub; 910 case bitc::RMW_AND: return AtomicRMWInst::And; 911 case bitc::RMW_NAND: return AtomicRMWInst::Nand; 912 case bitc::RMW_OR: return AtomicRMWInst::Or; 913 case bitc::RMW_XOR: return AtomicRMWInst::Xor; 914 case bitc::RMW_MAX: return AtomicRMWInst::Max; 915 case bitc::RMW_MIN: return AtomicRMWInst::Min; 916 case bitc::RMW_UMAX: return AtomicRMWInst::UMax; 917 case bitc::RMW_UMIN: return AtomicRMWInst::UMin; 918 } 919 } 920 921 static AtomicOrdering getDecodedOrdering(unsigned Val) { 922 switch (Val) { 923 case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; 924 case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; 925 case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; 926 case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; 927 case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; 928 case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; 929 default: // Map unknown orderings to sequentially-consistent. 930 case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; 931 } 932 } 933 934 static SynchronizationScope getDecodedSynchScope(unsigned Val) { 935 switch (Val) { 936 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread; 937 default: // Map unknown scopes to cross-thread. 938 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread; 939 } 940 } 941 942 static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { 943 switch (Val) { 944 default: // Map unknown selection kinds to any. 945 case bitc::COMDAT_SELECTION_KIND_ANY: 946 return Comdat::Any; 947 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: 948 return Comdat::ExactMatch; 949 case bitc::COMDAT_SELECTION_KIND_LARGEST: 950 return Comdat::Largest; 951 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: 952 return Comdat::NoDuplicates; 953 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: 954 return Comdat::SameSize; 955 } 956 } 957 958 static FastMathFlags getDecodedFastMathFlags(unsigned Val) { 959 FastMathFlags FMF; 960 if (0 != (Val & FastMathFlags::UnsafeAlgebra)) 961 FMF.setUnsafeAlgebra(); 962 if (0 != (Val & FastMathFlags::NoNaNs)) 963 FMF.setNoNaNs(); 964 if (0 != (Val & FastMathFlags::NoInfs)) 965 FMF.setNoInfs(); 966 if (0 != (Val & FastMathFlags::NoSignedZeros)) 967 FMF.setNoSignedZeros(); 968 if (0 != (Val & FastMathFlags::AllowReciprocal)) 969 FMF.setAllowReciprocal(); 970 return FMF; 971 } 972 973 static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { 974 switch (Val) { 975 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; 976 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; 977 } 978 } 979 980 981 Type *BitcodeReader::getTypeByID(unsigned ID) { 982 // The type table size is always specified correctly. 983 if (ID >= TypeList.size()) 984 return nullptr; 985 986 if (Type *Ty = TypeList[ID]) 987 return Ty; 988 989 // If we have a forward reference, the only possible case is when it is to a 990 // named struct. Just create a placeholder for now. 991 return TypeList[ID] = createIdentifiedStructType(Context); 992 } 993 994 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, 995 StringRef Name) { 996 auto *Ret = StructType::create(Context, Name); 997 IdentifiedStructTypes.push_back(Ret); 998 return Ret; 999 } 1000 1001 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { 1002 auto *Ret = StructType::create(Context); 1003 IdentifiedStructTypes.push_back(Ret); 1004 return Ret; 1005 } 1006 1007 //===----------------------------------------------------------------------===// 1008 // Functions for parsing blocks from the bitcode file 1009 //===----------------------------------------------------------------------===// 1010 1011 static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { 1012 switch (Val) { 1013 case Attribute::EndAttrKinds: 1014 llvm_unreachable("Synthetic enumerators which should never get here"); 1015 1016 case Attribute::None: return 0; 1017 case Attribute::ZExt: return 1 << 0; 1018 case Attribute::SExt: return 1 << 1; 1019 case Attribute::NoReturn: return 1 << 2; 1020 case Attribute::InReg: return 1 << 3; 1021 case Attribute::StructRet: return 1 << 4; 1022 case Attribute::NoUnwind: return 1 << 5; 1023 case Attribute::NoAlias: return 1 << 6; 1024 case Attribute::ByVal: return 1 << 7; 1025 case Attribute::Nest: return 1 << 8; 1026 case Attribute::ReadNone: return 1 << 9; 1027 case Attribute::ReadOnly: return 1 << 10; 1028 case Attribute::NoInline: return 1 << 11; 1029 case Attribute::AlwaysInline: return 1 << 12; 1030 case Attribute::OptimizeForSize: return 1 << 13; 1031 case Attribute::StackProtect: return 1 << 14; 1032 case Attribute::StackProtectReq: return 1 << 15; 1033 case Attribute::Alignment: return 31 << 16; 1034 case Attribute::NoCapture: return 1 << 21; 1035 case Attribute::NoRedZone: return 1 << 22; 1036 case Attribute::NoImplicitFloat: return 1 << 23; 1037 case Attribute::Naked: return 1 << 24; 1038 case Attribute::InlineHint: return 1 << 25; 1039 case Attribute::StackAlignment: return 7 << 26; 1040 case Attribute::ReturnsTwice: return 1 << 29; 1041 case Attribute::UWTable: return 1 << 30; 1042 case Attribute::NonLazyBind: return 1U << 31; 1043 case Attribute::SanitizeAddress: return 1ULL << 32; 1044 case Attribute::MinSize: return 1ULL << 33; 1045 case Attribute::NoDuplicate: return 1ULL << 34; 1046 case Attribute::StackProtectStrong: return 1ULL << 35; 1047 case Attribute::SanitizeThread: return 1ULL << 36; 1048 case Attribute::SanitizeMemory: return 1ULL << 37; 1049 case Attribute::NoBuiltin: return 1ULL << 38; 1050 case Attribute::Returned: return 1ULL << 39; 1051 case Attribute::Cold: return 1ULL << 40; 1052 case Attribute::Builtin: return 1ULL << 41; 1053 case Attribute::OptimizeNone: return 1ULL << 42; 1054 case Attribute::InAlloca: return 1ULL << 43; 1055 case Attribute::NonNull: return 1ULL << 44; 1056 case Attribute::JumpTable: return 1ULL << 45; 1057 case Attribute::Convergent: return 1ULL << 46; 1058 case Attribute::SafeStack: return 1ULL << 47; 1059 case Attribute::NoRecurse: return 1ULL << 48; 1060 case Attribute::InaccessibleMemOnly: return 1ULL << 49; 1061 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50; 1062 case Attribute::SwiftSelf: return 1ULL << 51; 1063 case Attribute::SwiftError: return 1ULL << 52; 1064 case Attribute::WriteOnly: return 1ULL << 53; 1065 case Attribute::Dereferenceable: 1066 llvm_unreachable("dereferenceable attribute not supported in raw format"); 1067 break; 1068 case Attribute::DereferenceableOrNull: 1069 llvm_unreachable("dereferenceable_or_null attribute not supported in raw " 1070 "format"); 1071 break; 1072 case Attribute::ArgMemOnly: 1073 llvm_unreachable("argmemonly attribute not supported in raw format"); 1074 break; 1075 case Attribute::AllocSize: 1076 llvm_unreachable("allocsize not supported in raw format"); 1077 break; 1078 } 1079 llvm_unreachable("Unsupported attribute type"); 1080 } 1081 1082 static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { 1083 if (!Val) return; 1084 1085 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; 1086 I = Attribute::AttrKind(I + 1)) { 1087 if (I == Attribute::Dereferenceable || 1088 I == Attribute::DereferenceableOrNull || 1089 I == Attribute::ArgMemOnly || 1090 I == Attribute::AllocSize) 1091 continue; 1092 if (uint64_t A = (Val & getRawAttributeMask(I))) { 1093 if (I == Attribute::Alignment) 1094 B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); 1095 else if (I == Attribute::StackAlignment) 1096 B.addStackAlignmentAttr(1ULL << ((A >> 26)-1)); 1097 else 1098 B.addAttribute(I); 1099 } 1100 } 1101 } 1102 1103 /// \brief This fills an AttrBuilder object with the LLVM attributes that have 1104 /// been decoded from the given integer. This function must stay in sync with 1105 /// 'encodeLLVMAttributesForBitcode'. 1106 static void decodeLLVMAttributesForBitcode(AttrBuilder &B, 1107 uint64_t EncodedAttrs) { 1108 // FIXME: Remove in 4.0. 1109 1110 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift 1111 // the bits above 31 down by 11 bits. 1112 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; 1113 assert((!Alignment || isPowerOf2_32(Alignment)) && 1114 "Alignment must be a power of two."); 1115 1116 if (Alignment) 1117 B.addAlignmentAttr(Alignment); 1118 addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | 1119 (EncodedAttrs & 0xffff)); 1120 } 1121 1122 Error BitcodeReader::parseAttributeBlock() { 1123 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) 1124 return error("Invalid record"); 1125 1126 if (!MAttributes.empty()) 1127 return error("Invalid multiple blocks"); 1128 1129 SmallVector<uint64_t, 64> Record; 1130 1131 SmallVector<AttributeSet, 8> Attrs; 1132 1133 // Read all the records. 1134 while (true) { 1135 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1136 1137 switch (Entry.Kind) { 1138 case BitstreamEntry::SubBlock: // Handled for us already. 1139 case BitstreamEntry::Error: 1140 return error("Malformed block"); 1141 case BitstreamEntry::EndBlock: 1142 return Error::success(); 1143 case BitstreamEntry::Record: 1144 // The interesting case. 1145 break; 1146 } 1147 1148 // Read a record. 1149 Record.clear(); 1150 switch (Stream.readRecord(Entry.ID, Record)) { 1151 default: // Default behavior: ignore. 1152 break; 1153 case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...] 1154 // FIXME: Remove in 4.0. 1155 if (Record.size() & 1) 1156 return error("Invalid record"); 1157 1158 for (unsigned i = 0, e = Record.size(); i != e; i += 2) { 1159 AttrBuilder B; 1160 decodeLLVMAttributesForBitcode(B, Record[i+1]); 1161 Attrs.push_back(AttributeSet::get(Context, Record[i], B)); 1162 } 1163 1164 MAttributes.push_back(AttributeSet::get(Context, Attrs)); 1165 Attrs.clear(); 1166 break; 1167 } 1168 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...] 1169 for (unsigned i = 0, e = Record.size(); i != e; ++i) 1170 Attrs.push_back(MAttributeGroups[Record[i]]); 1171 1172 MAttributes.push_back(AttributeSet::get(Context, Attrs)); 1173 Attrs.clear(); 1174 break; 1175 } 1176 } 1177 } 1178 } 1179 1180 // Returns Attribute::None on unrecognized codes. 1181 static Attribute::AttrKind getAttrFromCode(uint64_t Code) { 1182 switch (Code) { 1183 default: 1184 return Attribute::None; 1185 case bitc::ATTR_KIND_ALIGNMENT: 1186 return Attribute::Alignment; 1187 case bitc::ATTR_KIND_ALWAYS_INLINE: 1188 return Attribute::AlwaysInline; 1189 case bitc::ATTR_KIND_ARGMEMONLY: 1190 return Attribute::ArgMemOnly; 1191 case bitc::ATTR_KIND_BUILTIN: 1192 return Attribute::Builtin; 1193 case bitc::ATTR_KIND_BY_VAL: 1194 return Attribute::ByVal; 1195 case bitc::ATTR_KIND_IN_ALLOCA: 1196 return Attribute::InAlloca; 1197 case bitc::ATTR_KIND_COLD: 1198 return Attribute::Cold; 1199 case bitc::ATTR_KIND_CONVERGENT: 1200 return Attribute::Convergent; 1201 case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: 1202 return Attribute::InaccessibleMemOnly; 1203 case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: 1204 return Attribute::InaccessibleMemOrArgMemOnly; 1205 case bitc::ATTR_KIND_INLINE_HINT: 1206 return Attribute::InlineHint; 1207 case bitc::ATTR_KIND_IN_REG: 1208 return Attribute::InReg; 1209 case bitc::ATTR_KIND_JUMP_TABLE: 1210 return Attribute::JumpTable; 1211 case bitc::ATTR_KIND_MIN_SIZE: 1212 return Attribute::MinSize; 1213 case bitc::ATTR_KIND_NAKED: 1214 return Attribute::Naked; 1215 case bitc::ATTR_KIND_NEST: 1216 return Attribute::Nest; 1217 case bitc::ATTR_KIND_NO_ALIAS: 1218 return Attribute::NoAlias; 1219 case bitc::ATTR_KIND_NO_BUILTIN: 1220 return Attribute::NoBuiltin; 1221 case bitc::ATTR_KIND_NO_CAPTURE: 1222 return Attribute::NoCapture; 1223 case bitc::ATTR_KIND_NO_DUPLICATE: 1224 return Attribute::NoDuplicate; 1225 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: 1226 return Attribute::NoImplicitFloat; 1227 case bitc::ATTR_KIND_NO_INLINE: 1228 return Attribute::NoInline; 1229 case bitc::ATTR_KIND_NO_RECURSE: 1230 return Attribute::NoRecurse; 1231 case bitc::ATTR_KIND_NON_LAZY_BIND: 1232 return Attribute::NonLazyBind; 1233 case bitc::ATTR_KIND_NON_NULL: 1234 return Attribute::NonNull; 1235 case bitc::ATTR_KIND_DEREFERENCEABLE: 1236 return Attribute::Dereferenceable; 1237 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: 1238 return Attribute::DereferenceableOrNull; 1239 case bitc::ATTR_KIND_ALLOC_SIZE: 1240 return Attribute::AllocSize; 1241 case bitc::ATTR_KIND_NO_RED_ZONE: 1242 return Attribute::NoRedZone; 1243 case bitc::ATTR_KIND_NO_RETURN: 1244 return Attribute::NoReturn; 1245 case bitc::ATTR_KIND_NO_UNWIND: 1246 return Attribute::NoUnwind; 1247 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: 1248 return Attribute::OptimizeForSize; 1249 case bitc::ATTR_KIND_OPTIMIZE_NONE: 1250 return Attribute::OptimizeNone; 1251 case bitc::ATTR_KIND_READ_NONE: 1252 return Attribute::ReadNone; 1253 case bitc::ATTR_KIND_READ_ONLY: 1254 return Attribute::ReadOnly; 1255 case bitc::ATTR_KIND_RETURNED: 1256 return Attribute::Returned; 1257 case bitc::ATTR_KIND_RETURNS_TWICE: 1258 return Attribute::ReturnsTwice; 1259 case bitc::ATTR_KIND_S_EXT: 1260 return Attribute::SExt; 1261 case bitc::ATTR_KIND_STACK_ALIGNMENT: 1262 return Attribute::StackAlignment; 1263 case bitc::ATTR_KIND_STACK_PROTECT: 1264 return Attribute::StackProtect; 1265 case bitc::ATTR_KIND_STACK_PROTECT_REQ: 1266 return Attribute::StackProtectReq; 1267 case bitc::ATTR_KIND_STACK_PROTECT_STRONG: 1268 return Attribute::StackProtectStrong; 1269 case bitc::ATTR_KIND_SAFESTACK: 1270 return Attribute::SafeStack; 1271 case bitc::ATTR_KIND_STRUCT_RET: 1272 return Attribute::StructRet; 1273 case bitc::ATTR_KIND_SANITIZE_ADDRESS: 1274 return Attribute::SanitizeAddress; 1275 case bitc::ATTR_KIND_SANITIZE_THREAD: 1276 return Attribute::SanitizeThread; 1277 case bitc::ATTR_KIND_SANITIZE_MEMORY: 1278 return Attribute::SanitizeMemory; 1279 case bitc::ATTR_KIND_SWIFT_ERROR: 1280 return Attribute::SwiftError; 1281 case bitc::ATTR_KIND_SWIFT_SELF: 1282 return Attribute::SwiftSelf; 1283 case bitc::ATTR_KIND_UW_TABLE: 1284 return Attribute::UWTable; 1285 case bitc::ATTR_KIND_WRITEONLY: 1286 return Attribute::WriteOnly; 1287 case bitc::ATTR_KIND_Z_EXT: 1288 return Attribute::ZExt; 1289 } 1290 } 1291 1292 Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, 1293 unsigned &Alignment) { 1294 // Note: Alignment in bitcode files is incremented by 1, so that zero 1295 // can be used for default alignment. 1296 if (Exponent > Value::MaxAlignmentExponent + 1) 1297 return error("Invalid alignment value"); 1298 Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1; 1299 return Error::success(); 1300 } 1301 1302 Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { 1303 *Kind = getAttrFromCode(Code); 1304 if (*Kind == Attribute::None) 1305 return error("Unknown attribute kind (" + Twine(Code) + ")"); 1306 return Error::success(); 1307 } 1308 1309 Error BitcodeReader::parseAttributeGroupBlock() { 1310 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) 1311 return error("Invalid record"); 1312 1313 if (!MAttributeGroups.empty()) 1314 return error("Invalid multiple blocks"); 1315 1316 SmallVector<uint64_t, 64> Record; 1317 1318 // Read all the records. 1319 while (true) { 1320 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1321 1322 switch (Entry.Kind) { 1323 case BitstreamEntry::SubBlock: // Handled for us already. 1324 case BitstreamEntry::Error: 1325 return error("Malformed block"); 1326 case BitstreamEntry::EndBlock: 1327 return Error::success(); 1328 case BitstreamEntry::Record: 1329 // The interesting case. 1330 break; 1331 } 1332 1333 // Read a record. 1334 Record.clear(); 1335 switch (Stream.readRecord(Entry.ID, Record)) { 1336 default: // Default behavior: ignore. 1337 break; 1338 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] 1339 if (Record.size() < 3) 1340 return error("Invalid record"); 1341 1342 uint64_t GrpID = Record[0]; 1343 uint64_t Idx = Record[1]; // Index of the object this attribute refers to. 1344 1345 AttrBuilder B; 1346 for (unsigned i = 2, e = Record.size(); i != e; ++i) { 1347 if (Record[i] == 0) { // Enum attribute 1348 Attribute::AttrKind Kind; 1349 if (Error Err = parseAttrKind(Record[++i], &Kind)) 1350 return Err; 1351 1352 B.addAttribute(Kind); 1353 } else if (Record[i] == 1) { // Integer attribute 1354 Attribute::AttrKind Kind; 1355 if (Error Err = parseAttrKind(Record[++i], &Kind)) 1356 return Err; 1357 if (Kind == Attribute::Alignment) 1358 B.addAlignmentAttr(Record[++i]); 1359 else if (Kind == Attribute::StackAlignment) 1360 B.addStackAlignmentAttr(Record[++i]); 1361 else if (Kind == Attribute::Dereferenceable) 1362 B.addDereferenceableAttr(Record[++i]); 1363 else if (Kind == Attribute::DereferenceableOrNull) 1364 B.addDereferenceableOrNullAttr(Record[++i]); 1365 else if (Kind == Attribute::AllocSize) 1366 B.addAllocSizeAttrFromRawRepr(Record[++i]); 1367 } else { // String attribute 1368 assert((Record[i] == 3 || Record[i] == 4) && 1369 "Invalid attribute group entry"); 1370 bool HasValue = (Record[i++] == 4); 1371 SmallString<64> KindStr; 1372 SmallString<64> ValStr; 1373 1374 while (Record[i] != 0 && i != e) 1375 KindStr += Record[i++]; 1376 assert(Record[i] == 0 && "Kind string not null terminated"); 1377 1378 if (HasValue) { 1379 // Has a value associated with it. 1380 ++i; // Skip the '0' that terminates the "kind" string. 1381 while (Record[i] != 0 && i != e) 1382 ValStr += Record[i++]; 1383 assert(Record[i] == 0 && "Value string not null terminated"); 1384 } 1385 1386 B.addAttribute(KindStr.str(), ValStr.str()); 1387 } 1388 } 1389 1390 MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B); 1391 break; 1392 } 1393 } 1394 } 1395 } 1396 1397 Error BitcodeReader::parseTypeTable() { 1398 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) 1399 return error("Invalid record"); 1400 1401 return parseTypeTableBody(); 1402 } 1403 1404 Error BitcodeReader::parseTypeTableBody() { 1405 if (!TypeList.empty()) 1406 return error("Invalid multiple blocks"); 1407 1408 SmallVector<uint64_t, 64> Record; 1409 unsigned NumRecords = 0; 1410 1411 SmallString<64> TypeName; 1412 1413 // Read all the records for this type table. 1414 while (true) { 1415 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1416 1417 switch (Entry.Kind) { 1418 case BitstreamEntry::SubBlock: // Handled for us already. 1419 case BitstreamEntry::Error: 1420 return error("Malformed block"); 1421 case BitstreamEntry::EndBlock: 1422 if (NumRecords != TypeList.size()) 1423 return error("Malformed block"); 1424 return Error::success(); 1425 case BitstreamEntry::Record: 1426 // The interesting case. 1427 break; 1428 } 1429 1430 // Read a record. 1431 Record.clear(); 1432 Type *ResultTy = nullptr; 1433 switch (Stream.readRecord(Entry.ID, Record)) { 1434 default: 1435 return error("Invalid value"); 1436 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] 1437 // TYPE_CODE_NUMENTRY contains a count of the number of types in the 1438 // type list. This allows us to reserve space. 1439 if (Record.size() < 1) 1440 return error("Invalid record"); 1441 TypeList.resize(Record[0]); 1442 continue; 1443 case bitc::TYPE_CODE_VOID: // VOID 1444 ResultTy = Type::getVoidTy(Context); 1445 break; 1446 case bitc::TYPE_CODE_HALF: // HALF 1447 ResultTy = Type::getHalfTy(Context); 1448 break; 1449 case bitc::TYPE_CODE_FLOAT: // FLOAT 1450 ResultTy = Type::getFloatTy(Context); 1451 break; 1452 case bitc::TYPE_CODE_DOUBLE: // DOUBLE 1453 ResultTy = Type::getDoubleTy(Context); 1454 break; 1455 case bitc::TYPE_CODE_X86_FP80: // X86_FP80 1456 ResultTy = Type::getX86_FP80Ty(Context); 1457 break; 1458 case bitc::TYPE_CODE_FP128: // FP128 1459 ResultTy = Type::getFP128Ty(Context); 1460 break; 1461 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 1462 ResultTy = Type::getPPC_FP128Ty(Context); 1463 break; 1464 case bitc::TYPE_CODE_LABEL: // LABEL 1465 ResultTy = Type::getLabelTy(Context); 1466 break; 1467 case bitc::TYPE_CODE_METADATA: // METADATA 1468 ResultTy = Type::getMetadataTy(Context); 1469 break; 1470 case bitc::TYPE_CODE_X86_MMX: // X86_MMX 1471 ResultTy = Type::getX86_MMXTy(Context); 1472 break; 1473 case bitc::TYPE_CODE_TOKEN: // TOKEN 1474 ResultTy = Type::getTokenTy(Context); 1475 break; 1476 case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] 1477 if (Record.size() < 1) 1478 return error("Invalid record"); 1479 1480 uint64_t NumBits = Record[0]; 1481 if (NumBits < IntegerType::MIN_INT_BITS || 1482 NumBits > IntegerType::MAX_INT_BITS) 1483 return error("Bitwidth for integer type out of range"); 1484 ResultTy = IntegerType::get(Context, NumBits); 1485 break; 1486 } 1487 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or 1488 // [pointee type, address space] 1489 if (Record.size() < 1) 1490 return error("Invalid record"); 1491 unsigned AddressSpace = 0; 1492 if (Record.size() == 2) 1493 AddressSpace = Record[1]; 1494 ResultTy = getTypeByID(Record[0]); 1495 if (!ResultTy || 1496 !PointerType::isValidElementType(ResultTy)) 1497 return error("Invalid type"); 1498 ResultTy = PointerType::get(ResultTy, AddressSpace); 1499 break; 1500 } 1501 case bitc::TYPE_CODE_FUNCTION_OLD: { 1502 // FIXME: attrid is dead, remove it in LLVM 4.0 1503 // FUNCTION: [vararg, attrid, retty, paramty x N] 1504 if (Record.size() < 3) 1505 return error("Invalid record"); 1506 SmallVector<Type*, 8> ArgTys; 1507 for (unsigned i = 3, e = Record.size(); i != e; ++i) { 1508 if (Type *T = getTypeByID(Record[i])) 1509 ArgTys.push_back(T); 1510 else 1511 break; 1512 } 1513 1514 ResultTy = getTypeByID(Record[2]); 1515 if (!ResultTy || ArgTys.size() < Record.size()-3) 1516 return error("Invalid type"); 1517 1518 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 1519 break; 1520 } 1521 case bitc::TYPE_CODE_FUNCTION: { 1522 // FUNCTION: [vararg, retty, paramty x N] 1523 if (Record.size() < 2) 1524 return error("Invalid record"); 1525 SmallVector<Type*, 8> ArgTys; 1526 for (unsigned i = 2, e = Record.size(); i != e; ++i) { 1527 if (Type *T = getTypeByID(Record[i])) { 1528 if (!FunctionType::isValidArgumentType(T)) 1529 return error("Invalid function argument type"); 1530 ArgTys.push_back(T); 1531 } 1532 else 1533 break; 1534 } 1535 1536 ResultTy = getTypeByID(Record[1]); 1537 if (!ResultTy || ArgTys.size() < Record.size()-2) 1538 return error("Invalid type"); 1539 1540 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); 1541 break; 1542 } 1543 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] 1544 if (Record.size() < 1) 1545 return error("Invalid record"); 1546 SmallVector<Type*, 8> EltTys; 1547 for (unsigned i = 1, e = Record.size(); i != e; ++i) { 1548 if (Type *T = getTypeByID(Record[i])) 1549 EltTys.push_back(T); 1550 else 1551 break; 1552 } 1553 if (EltTys.size() != Record.size()-1) 1554 return error("Invalid type"); 1555 ResultTy = StructType::get(Context, EltTys, Record[0]); 1556 break; 1557 } 1558 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] 1559 if (convertToString(Record, 0, TypeName)) 1560 return error("Invalid record"); 1561 continue; 1562 1563 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] 1564 if (Record.size() < 1) 1565 return error("Invalid record"); 1566 1567 if (NumRecords >= TypeList.size()) 1568 return error("Invalid TYPE table"); 1569 1570 // Check to see if this was forward referenced, if so fill in the temp. 1571 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 1572 if (Res) { 1573 Res->setName(TypeName); 1574 TypeList[NumRecords] = nullptr; 1575 } else // Otherwise, create a new struct. 1576 Res = createIdentifiedStructType(Context, TypeName); 1577 TypeName.clear(); 1578 1579 SmallVector<Type*, 8> EltTys; 1580 for (unsigned i = 1, e = Record.size(); i != e; ++i) { 1581 if (Type *T = getTypeByID(Record[i])) 1582 EltTys.push_back(T); 1583 else 1584 break; 1585 } 1586 if (EltTys.size() != Record.size()-1) 1587 return error("Invalid record"); 1588 Res->setBody(EltTys, Record[0]); 1589 ResultTy = Res; 1590 break; 1591 } 1592 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] 1593 if (Record.size() != 1) 1594 return error("Invalid record"); 1595 1596 if (NumRecords >= TypeList.size()) 1597 return error("Invalid TYPE table"); 1598 1599 // Check to see if this was forward referenced, if so fill in the temp. 1600 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); 1601 if (Res) { 1602 Res->setName(TypeName); 1603 TypeList[NumRecords] = nullptr; 1604 } else // Otherwise, create a new struct with no body. 1605 Res = createIdentifiedStructType(Context, TypeName); 1606 TypeName.clear(); 1607 ResultTy = Res; 1608 break; 1609 } 1610 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] 1611 if (Record.size() < 2) 1612 return error("Invalid record"); 1613 ResultTy = getTypeByID(Record[1]); 1614 if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) 1615 return error("Invalid type"); 1616 ResultTy = ArrayType::get(ResultTy, Record[0]); 1617 break; 1618 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] 1619 if (Record.size() < 2) 1620 return error("Invalid record"); 1621 if (Record[0] == 0) 1622 return error("Invalid vector length"); 1623 ResultTy = getTypeByID(Record[1]); 1624 if (!ResultTy || !StructType::isValidElementType(ResultTy)) 1625 return error("Invalid type"); 1626 ResultTy = VectorType::get(ResultTy, Record[0]); 1627 break; 1628 } 1629 1630 if (NumRecords >= TypeList.size()) 1631 return error("Invalid TYPE table"); 1632 if (TypeList[NumRecords]) 1633 return error( 1634 "Invalid TYPE table: Only named structs can be forward referenced"); 1635 assert(ResultTy && "Didn't read a type?"); 1636 TypeList[NumRecords++] = ResultTy; 1637 } 1638 } 1639 1640 Error BitcodeReader::parseOperandBundleTags() { 1641 if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) 1642 return error("Invalid record"); 1643 1644 if (!BundleTags.empty()) 1645 return error("Invalid multiple blocks"); 1646 1647 SmallVector<uint64_t, 64> Record; 1648 1649 while (true) { 1650 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1651 1652 switch (Entry.Kind) { 1653 case BitstreamEntry::SubBlock: // Handled for us already. 1654 case BitstreamEntry::Error: 1655 return error("Malformed block"); 1656 case BitstreamEntry::EndBlock: 1657 return Error::success(); 1658 case BitstreamEntry::Record: 1659 // The interesting case. 1660 break; 1661 } 1662 1663 // Tags are implicitly mapped to integers by their order. 1664 1665 if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG) 1666 return error("Invalid record"); 1667 1668 // OPERAND_BUNDLE_TAG: [strchr x N] 1669 BundleTags.emplace_back(); 1670 if (convertToString(Record, 0, BundleTags.back())) 1671 return error("Invalid record"); 1672 Record.clear(); 1673 } 1674 } 1675 1676 /// Associate a value with its name from the given index in the provided record. 1677 Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, 1678 unsigned NameIndex, Triple &TT) { 1679 SmallString<128> ValueName; 1680 if (convertToString(Record, NameIndex, ValueName)) 1681 return error("Invalid record"); 1682 unsigned ValueID = Record[0]; 1683 if (ValueID >= ValueList.size() || !ValueList[ValueID]) 1684 return error("Invalid record"); 1685 Value *V = ValueList[ValueID]; 1686 1687 StringRef NameStr(ValueName.data(), ValueName.size()); 1688 if (NameStr.find_first_of(0) != StringRef::npos) 1689 return error("Invalid value name"); 1690 V->setName(NameStr); 1691 auto *GO = dyn_cast<GlobalObject>(V); 1692 if (GO) { 1693 if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) { 1694 if (TT.isOSBinFormatMachO()) 1695 GO->setComdat(nullptr); 1696 else 1697 GO->setComdat(TheModule->getOrInsertComdat(V->getName())); 1698 } 1699 } 1700 return V; 1701 } 1702 1703 /// Helper to note and return the current location, and jump to the given 1704 /// offset. 1705 static uint64_t jumpToValueSymbolTable(uint64_t Offset, 1706 BitstreamCursor &Stream) { 1707 // Save the current parsing location so we can jump back at the end 1708 // of the VST read. 1709 uint64_t CurrentBit = Stream.GetCurrentBitNo(); 1710 Stream.JumpToBit(Offset * 32); 1711 #ifndef NDEBUG 1712 // Do some checking if we are in debug mode. 1713 BitstreamEntry Entry = Stream.advance(); 1714 assert(Entry.Kind == BitstreamEntry::SubBlock); 1715 assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID); 1716 #else 1717 // In NDEBUG mode ignore the output so we don't get an unused variable 1718 // warning. 1719 Stream.advance(); 1720 #endif 1721 return CurrentBit; 1722 } 1723 1724 /// Parse the value symbol table at either the current parsing location or 1725 /// at the given bit offset if provided. 1726 Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { 1727 uint64_t CurrentBit; 1728 // Pass in the Offset to distinguish between calling for the module-level 1729 // VST (where we want to jump to the VST offset) and the function-level 1730 // VST (where we don't). 1731 if (Offset > 0) 1732 CurrentBit = jumpToValueSymbolTable(Offset, Stream); 1733 1734 // Compute the delta between the bitcode indices in the VST (the word offset 1735 // to the word-aligned ENTER_SUBBLOCK for the function block, and that 1736 // expected by the lazy reader. The reader's EnterSubBlock expects to have 1737 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID 1738 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here 1739 // just before entering the VST subblock because: 1) the EnterSubBlock 1740 // changes the AbbrevID width; 2) the VST block is nested within the same 1741 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same 1742 // AbbrevID width before calling EnterSubBlock; and 3) when we want to 1743 // jump to the FUNCTION_BLOCK using this offset later, we don't want 1744 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. 1745 unsigned FuncBitcodeOffsetDelta = 1746 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; 1747 1748 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 1749 return error("Invalid record"); 1750 1751 SmallVector<uint64_t, 64> Record; 1752 1753 Triple TT(TheModule->getTargetTriple()); 1754 1755 // Read all the records for this value table. 1756 SmallString<128> ValueName; 1757 1758 while (true) { 1759 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1760 1761 switch (Entry.Kind) { 1762 case BitstreamEntry::SubBlock: // Handled for us already. 1763 case BitstreamEntry::Error: 1764 return error("Malformed block"); 1765 case BitstreamEntry::EndBlock: 1766 if (Offset > 0) 1767 Stream.JumpToBit(CurrentBit); 1768 return Error::success(); 1769 case BitstreamEntry::Record: 1770 // The interesting case. 1771 break; 1772 } 1773 1774 // Read a record. 1775 Record.clear(); 1776 switch (Stream.readRecord(Entry.ID, Record)) { 1777 default: // Default behavior: unknown type. 1778 break; 1779 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 1780 Expected<Value *> ValOrErr = recordValue(Record, 1, TT); 1781 if (Error Err = ValOrErr.takeError()) 1782 return Err; 1783 ValOrErr.get(); 1784 break; 1785 } 1786 case bitc::VST_CODE_FNENTRY: { 1787 // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 1788 Expected<Value *> ValOrErr = recordValue(Record, 2, TT); 1789 if (Error Err = ValOrErr.takeError()) 1790 return Err; 1791 Value *V = ValOrErr.get(); 1792 1793 auto *GO = dyn_cast<GlobalObject>(V); 1794 if (!GO) { 1795 // If this is an alias, need to get the actual Function object 1796 // it aliases, in order to set up the DeferredFunctionInfo entry below. 1797 auto *GA = dyn_cast<GlobalAlias>(V); 1798 if (GA) 1799 GO = GA->getBaseObject(); 1800 assert(GO); 1801 } 1802 1803 // Note that we subtract 1 here because the offset is relative to one word 1804 // before the start of the identification or module block, which was 1805 // historically always the start of the regular bitcode header. 1806 uint64_t FuncWordOffset = Record[1] - 1; 1807 Function *F = dyn_cast<Function>(GO); 1808 assert(F); 1809 uint64_t FuncBitOffset = FuncWordOffset * 32; 1810 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; 1811 // Set the LastFunctionBlockBit to point to the last function block. 1812 // Later when parsing is resumed after function materialization, 1813 // we can simply skip that last function block. 1814 if (FuncBitOffset > LastFunctionBlockBit) 1815 LastFunctionBlockBit = FuncBitOffset; 1816 break; 1817 } 1818 case bitc::VST_CODE_BBENTRY: { 1819 if (convertToString(Record, 1, ValueName)) 1820 return error("Invalid record"); 1821 BasicBlock *BB = getBasicBlock(Record[0]); 1822 if (!BB) 1823 return error("Invalid record"); 1824 1825 BB->setName(StringRef(ValueName.data(), ValueName.size())); 1826 ValueName.clear(); 1827 break; 1828 } 1829 } 1830 } 1831 } 1832 1833 /// Decode a signed value stored with the sign bit in the LSB for dense VBR 1834 /// encoding. 1835 uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { 1836 if ((V & 1) == 0) 1837 return V >> 1; 1838 if (V != 1) 1839 return -(V >> 1); 1840 // There is no such thing as -0 with integers. "-0" really means MININT. 1841 return 1ULL << 63; 1842 } 1843 1844 /// Resolve all of the initializers for global values and aliases that we can. 1845 Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { 1846 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; 1847 std::vector<std::pair<GlobalIndirectSymbol*, unsigned> > 1848 IndirectSymbolInitWorklist; 1849 std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist; 1850 std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist; 1851 std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFnWorklist; 1852 1853 GlobalInitWorklist.swap(GlobalInits); 1854 IndirectSymbolInitWorklist.swap(IndirectSymbolInits); 1855 FunctionPrefixWorklist.swap(FunctionPrefixes); 1856 FunctionPrologueWorklist.swap(FunctionPrologues); 1857 FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns); 1858 1859 while (!GlobalInitWorklist.empty()) { 1860 unsigned ValID = GlobalInitWorklist.back().second; 1861 if (ValID >= ValueList.size()) { 1862 // Not ready to resolve this yet, it requires something later in the file. 1863 GlobalInits.push_back(GlobalInitWorklist.back()); 1864 } else { 1865 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 1866 GlobalInitWorklist.back().first->setInitializer(C); 1867 else 1868 return error("Expected a constant"); 1869 } 1870 GlobalInitWorklist.pop_back(); 1871 } 1872 1873 while (!IndirectSymbolInitWorklist.empty()) { 1874 unsigned ValID = IndirectSymbolInitWorklist.back().second; 1875 if (ValID >= ValueList.size()) { 1876 IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back()); 1877 } else { 1878 Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]); 1879 if (!C) 1880 return error("Expected a constant"); 1881 GlobalIndirectSymbol *GIS = IndirectSymbolInitWorklist.back().first; 1882 if (isa<GlobalAlias>(GIS) && C->getType() != GIS->getType()) 1883 return error("Alias and aliasee types don't match"); 1884 GIS->setIndirectSymbol(C); 1885 } 1886 IndirectSymbolInitWorklist.pop_back(); 1887 } 1888 1889 while (!FunctionPrefixWorklist.empty()) { 1890 unsigned ValID = FunctionPrefixWorklist.back().second; 1891 if (ValID >= ValueList.size()) { 1892 FunctionPrefixes.push_back(FunctionPrefixWorklist.back()); 1893 } else { 1894 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 1895 FunctionPrefixWorklist.back().first->setPrefixData(C); 1896 else 1897 return error("Expected a constant"); 1898 } 1899 FunctionPrefixWorklist.pop_back(); 1900 } 1901 1902 while (!FunctionPrologueWorklist.empty()) { 1903 unsigned ValID = FunctionPrologueWorklist.back().second; 1904 if (ValID >= ValueList.size()) { 1905 FunctionPrologues.push_back(FunctionPrologueWorklist.back()); 1906 } else { 1907 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 1908 FunctionPrologueWorklist.back().first->setPrologueData(C); 1909 else 1910 return error("Expected a constant"); 1911 } 1912 FunctionPrologueWorklist.pop_back(); 1913 } 1914 1915 while (!FunctionPersonalityFnWorklist.empty()) { 1916 unsigned ValID = FunctionPersonalityFnWorklist.back().second; 1917 if (ValID >= ValueList.size()) { 1918 FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back()); 1919 } else { 1920 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) 1921 FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C); 1922 else 1923 return error("Expected a constant"); 1924 } 1925 FunctionPersonalityFnWorklist.pop_back(); 1926 } 1927 1928 return Error::success(); 1929 } 1930 1931 static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { 1932 SmallVector<uint64_t, 8> Words(Vals.size()); 1933 transform(Vals, Words.begin(), 1934 BitcodeReader::decodeSignRotatedValue); 1935 1936 return APInt(TypeBits, Words); 1937 } 1938 1939 Error BitcodeReader::parseConstants() { 1940 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) 1941 return error("Invalid record"); 1942 1943 SmallVector<uint64_t, 64> Record; 1944 1945 // Read all the records for this value table. 1946 Type *CurTy = Type::getInt32Ty(Context); 1947 unsigned NextCstNo = ValueList.size(); 1948 1949 while (true) { 1950 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 1951 1952 switch (Entry.Kind) { 1953 case BitstreamEntry::SubBlock: // Handled for us already. 1954 case BitstreamEntry::Error: 1955 return error("Malformed block"); 1956 case BitstreamEntry::EndBlock: 1957 if (NextCstNo != ValueList.size()) 1958 return error("Invalid constant reference"); 1959 1960 // Once all the constants have been read, go through and resolve forward 1961 // references. 1962 ValueList.resolveConstantForwardRefs(); 1963 return Error::success(); 1964 case BitstreamEntry::Record: 1965 // The interesting case. 1966 break; 1967 } 1968 1969 // Read a record. 1970 Record.clear(); 1971 Type *VoidType = Type::getVoidTy(Context); 1972 Value *V = nullptr; 1973 unsigned BitCode = Stream.readRecord(Entry.ID, Record); 1974 switch (BitCode) { 1975 default: // Default behavior: unknown constant 1976 case bitc::CST_CODE_UNDEF: // UNDEF 1977 V = UndefValue::get(CurTy); 1978 break; 1979 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] 1980 if (Record.empty()) 1981 return error("Invalid record"); 1982 if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) 1983 return error("Invalid record"); 1984 if (TypeList[Record[0]] == VoidType) 1985 return error("Invalid constant type"); 1986 CurTy = TypeList[Record[0]]; 1987 continue; // Skip the ValueList manipulation. 1988 case bitc::CST_CODE_NULL: // NULL 1989 V = Constant::getNullValue(CurTy); 1990 break; 1991 case bitc::CST_CODE_INTEGER: // INTEGER: [intval] 1992 if (!CurTy->isIntegerTy() || Record.empty()) 1993 return error("Invalid record"); 1994 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); 1995 break; 1996 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] 1997 if (!CurTy->isIntegerTy() || Record.empty()) 1998 return error("Invalid record"); 1999 2000 APInt VInt = 2001 readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); 2002 V = ConstantInt::get(Context, VInt); 2003 2004 break; 2005 } 2006 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] 2007 if (Record.empty()) 2008 return error("Invalid record"); 2009 if (CurTy->isHalfTy()) 2010 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(), 2011 APInt(16, (uint16_t)Record[0]))); 2012 else if (CurTy->isFloatTy()) 2013 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(), 2014 APInt(32, (uint32_t)Record[0]))); 2015 else if (CurTy->isDoubleTy()) 2016 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(), 2017 APInt(64, Record[0]))); 2018 else if (CurTy->isX86_FP80Ty()) { 2019 // Bits are not stored the same way as a normal i80 APInt, compensate. 2020 uint64_t Rearrange[2]; 2021 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); 2022 Rearrange[1] = Record[0] >> 48; 2023 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(), 2024 APInt(80, Rearrange))); 2025 } else if (CurTy->isFP128Ty()) 2026 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(), 2027 APInt(128, Record))); 2028 else if (CurTy->isPPC_FP128Ty()) 2029 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(), 2030 APInt(128, Record))); 2031 else 2032 V = UndefValue::get(CurTy); 2033 break; 2034 } 2035 2036 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] 2037 if (Record.empty()) 2038 return error("Invalid record"); 2039 2040 unsigned Size = Record.size(); 2041 SmallVector<Constant*, 16> Elts; 2042 2043 if (StructType *STy = dyn_cast<StructType>(CurTy)) { 2044 for (unsigned i = 0; i != Size; ++i) 2045 Elts.push_back(ValueList.getConstantFwdRef(Record[i], 2046 STy->getElementType(i))); 2047 V = ConstantStruct::get(STy, Elts); 2048 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { 2049 Type *EltTy = ATy->getElementType(); 2050 for (unsigned i = 0; i != Size; ++i) 2051 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 2052 V = ConstantArray::get(ATy, Elts); 2053 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) { 2054 Type *EltTy = VTy->getElementType(); 2055 for (unsigned i = 0; i != Size; ++i) 2056 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); 2057 V = ConstantVector::get(Elts); 2058 } else { 2059 V = UndefValue::get(CurTy); 2060 } 2061 break; 2062 } 2063 case bitc::CST_CODE_STRING: // STRING: [values] 2064 case bitc::CST_CODE_CSTRING: { // CSTRING: [values] 2065 if (Record.empty()) 2066 return error("Invalid record"); 2067 2068 SmallString<16> Elts(Record.begin(), Record.end()); 2069 V = ConstantDataArray::getString(Context, Elts, 2070 BitCode == bitc::CST_CODE_CSTRING); 2071 break; 2072 } 2073 case bitc::CST_CODE_DATA: {// DATA: [n x value] 2074 if (Record.empty()) 2075 return error("Invalid record"); 2076 2077 Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); 2078 if (EltTy->isIntegerTy(8)) { 2079 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); 2080 if (isa<VectorType>(CurTy)) 2081 V = ConstantDataVector::get(Context, Elts); 2082 else 2083 V = ConstantDataArray::get(Context, Elts); 2084 } else if (EltTy->isIntegerTy(16)) { 2085 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 2086 if (isa<VectorType>(CurTy)) 2087 V = ConstantDataVector::get(Context, Elts); 2088 else 2089 V = ConstantDataArray::get(Context, Elts); 2090 } else if (EltTy->isIntegerTy(32)) { 2091 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 2092 if (isa<VectorType>(CurTy)) 2093 V = ConstantDataVector::get(Context, Elts); 2094 else 2095 V = ConstantDataArray::get(Context, Elts); 2096 } else if (EltTy->isIntegerTy(64)) { 2097 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 2098 if (isa<VectorType>(CurTy)) 2099 V = ConstantDataVector::get(Context, Elts); 2100 else 2101 V = ConstantDataArray::get(Context, Elts); 2102 } else if (EltTy->isHalfTy()) { 2103 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); 2104 if (isa<VectorType>(CurTy)) 2105 V = ConstantDataVector::getFP(Context, Elts); 2106 else 2107 V = ConstantDataArray::getFP(Context, Elts); 2108 } else if (EltTy->isFloatTy()) { 2109 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); 2110 if (isa<VectorType>(CurTy)) 2111 V = ConstantDataVector::getFP(Context, Elts); 2112 else 2113 V = ConstantDataArray::getFP(Context, Elts); 2114 } else if (EltTy->isDoubleTy()) { 2115 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); 2116 if (isa<VectorType>(CurTy)) 2117 V = ConstantDataVector::getFP(Context, Elts); 2118 else 2119 V = ConstantDataArray::getFP(Context, Elts); 2120 } else { 2121 return error("Invalid type for value"); 2122 } 2123 break; 2124 } 2125 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] 2126 if (Record.size() < 3) 2127 return error("Invalid record"); 2128 int Opc = getDecodedBinaryOpcode(Record[0], CurTy); 2129 if (Opc < 0) { 2130 V = UndefValue::get(CurTy); // Unknown binop. 2131 } else { 2132 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); 2133 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); 2134 unsigned Flags = 0; 2135 if (Record.size() >= 4) { 2136 if (Opc == Instruction::Add || 2137 Opc == Instruction::Sub || 2138 Opc == Instruction::Mul || 2139 Opc == Instruction::Shl) { 2140 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 2141 Flags |= OverflowingBinaryOperator::NoSignedWrap; 2142 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 2143 Flags |= OverflowingBinaryOperator::NoUnsignedWrap; 2144 } else if (Opc == Instruction::SDiv || 2145 Opc == Instruction::UDiv || 2146 Opc == Instruction::LShr || 2147 Opc == Instruction::AShr) { 2148 if (Record[3] & (1 << bitc::PEO_EXACT)) 2149 Flags |= SDivOperator::IsExact; 2150 } 2151 } 2152 V = ConstantExpr::get(Opc, LHS, RHS, Flags); 2153 } 2154 break; 2155 } 2156 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] 2157 if (Record.size() < 3) 2158 return error("Invalid record"); 2159 int Opc = getDecodedCastOpcode(Record[0]); 2160 if (Opc < 0) { 2161 V = UndefValue::get(CurTy); // Unknown cast. 2162 } else { 2163 Type *OpTy = getTypeByID(Record[1]); 2164 if (!OpTy) 2165 return error("Invalid record"); 2166 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); 2167 V = UpgradeBitCastExpr(Opc, Op, CurTy); 2168 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy); 2169 } 2170 break; 2171 } 2172 case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] 2173 case bitc::CST_CODE_CE_GEP: // [ty, n x operands] 2174 case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x 2175 // operands] 2176 unsigned OpNum = 0; 2177 Type *PointeeType = nullptr; 2178 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX || 2179 Record.size() % 2) 2180 PointeeType = getTypeByID(Record[OpNum++]); 2181 2182 bool InBounds = false; 2183 Optional<unsigned> InRangeIndex; 2184 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) { 2185 uint64_t Op = Record[OpNum++]; 2186 InBounds = Op & 1; 2187 InRangeIndex = Op >> 1; 2188 } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) 2189 InBounds = true; 2190 2191 SmallVector<Constant*, 16> Elts; 2192 while (OpNum != Record.size()) { 2193 Type *ElTy = getTypeByID(Record[OpNum++]); 2194 if (!ElTy) 2195 return error("Invalid record"); 2196 Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy)); 2197 } 2198 2199 if (PointeeType && 2200 PointeeType != 2201 cast<PointerType>(Elts[0]->getType()->getScalarType()) 2202 ->getElementType()) 2203 return error("Explicit gep operator type does not match pointee type " 2204 "of pointer operand"); 2205 2206 if (Elts.size() < 1) 2207 return error("Invalid gep with no operands"); 2208 2209 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); 2210 V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices, 2211 InBounds, InRangeIndex); 2212 break; 2213 } 2214 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] 2215 if (Record.size() < 3) 2216 return error("Invalid record"); 2217 2218 Type *SelectorTy = Type::getInt1Ty(Context); 2219 2220 // The selector might be an i1 or an <n x i1> 2221 // Get the type from the ValueList before getting a forward ref. 2222 if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) 2223 if (Value *V = ValueList[Record[0]]) 2224 if (SelectorTy != V->getType()) 2225 SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements()); 2226 2227 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], 2228 SelectorTy), 2229 ValueList.getConstantFwdRef(Record[1],CurTy), 2230 ValueList.getConstantFwdRef(Record[2],CurTy)); 2231 break; 2232 } 2233 case bitc::CST_CODE_CE_EXTRACTELT 2234 : { // CE_EXTRACTELT: [opty, opval, opty, opval] 2235 if (Record.size() < 3) 2236 return error("Invalid record"); 2237 VectorType *OpTy = 2238 dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 2239 if (!OpTy) 2240 return error("Invalid record"); 2241 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 2242 Constant *Op1 = nullptr; 2243 if (Record.size() == 4) { 2244 Type *IdxTy = getTypeByID(Record[2]); 2245 if (!IdxTy) 2246 return error("Invalid record"); 2247 Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy); 2248 } else // TODO: Remove with llvm 4.0 2249 Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); 2250 if (!Op1) 2251 return error("Invalid record"); 2252 V = ConstantExpr::getExtractElement(Op0, Op1); 2253 break; 2254 } 2255 case bitc::CST_CODE_CE_INSERTELT 2256 : { // CE_INSERTELT: [opval, opval, opty, opval] 2257 VectorType *OpTy = dyn_cast<VectorType>(CurTy); 2258 if (Record.size() < 3 || !OpTy) 2259 return error("Invalid record"); 2260 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); 2261 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], 2262 OpTy->getElementType()); 2263 Constant *Op2 = nullptr; 2264 if (Record.size() == 4) { 2265 Type *IdxTy = getTypeByID(Record[2]); 2266 if (!IdxTy) 2267 return error("Invalid record"); 2268 Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy); 2269 } else // TODO: Remove with llvm 4.0 2270 Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); 2271 if (!Op2) 2272 return error("Invalid record"); 2273 V = ConstantExpr::getInsertElement(Op0, Op1, Op2); 2274 break; 2275 } 2276 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] 2277 VectorType *OpTy = dyn_cast<VectorType>(CurTy); 2278 if (Record.size() < 3 || !OpTy) 2279 return error("Invalid record"); 2280 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); 2281 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); 2282 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), 2283 OpTy->getNumElements()); 2284 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); 2285 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); 2286 break; 2287 } 2288 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] 2289 VectorType *RTy = dyn_cast<VectorType>(CurTy); 2290 VectorType *OpTy = 2291 dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); 2292 if (Record.size() < 4 || !RTy || !OpTy) 2293 return error("Invalid record"); 2294 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 2295 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); 2296 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), 2297 RTy->getNumElements()); 2298 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy); 2299 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); 2300 break; 2301 } 2302 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] 2303 if (Record.size() < 4) 2304 return error("Invalid record"); 2305 Type *OpTy = getTypeByID(Record[0]); 2306 if (!OpTy) 2307 return error("Invalid record"); 2308 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); 2309 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); 2310 2311 if (OpTy->isFPOrFPVectorTy()) 2312 V = ConstantExpr::getFCmp(Record[3], Op0, Op1); 2313 else 2314 V = ConstantExpr::getICmp(Record[3], Op0, Op1); 2315 break; 2316 } 2317 // This maintains backward compatibility, pre-asm dialect keywords. 2318 // FIXME: Remove with the 4.0 release. 2319 case bitc::CST_CODE_INLINEASM_OLD: { 2320 if (Record.size() < 2) 2321 return error("Invalid record"); 2322 std::string AsmStr, ConstrStr; 2323 bool HasSideEffects = Record[0] & 1; 2324 bool IsAlignStack = Record[0] >> 1; 2325 unsigned AsmStrSize = Record[1]; 2326 if (2+AsmStrSize >= Record.size()) 2327 return error("Invalid record"); 2328 unsigned ConstStrSize = Record[2+AsmStrSize]; 2329 if (3+AsmStrSize+ConstStrSize > Record.size()) 2330 return error("Invalid record"); 2331 2332 for (unsigned i = 0; i != AsmStrSize; ++i) 2333 AsmStr += (char)Record[2+i]; 2334 for (unsigned i = 0; i != ConstStrSize; ++i) 2335 ConstrStr += (char)Record[3+AsmStrSize+i]; 2336 PointerType *PTy = cast<PointerType>(CurTy); 2337 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), 2338 AsmStr, ConstrStr, HasSideEffects, IsAlignStack); 2339 break; 2340 } 2341 // This version adds support for the asm dialect keywords (e.g., 2342 // inteldialect). 2343 case bitc::CST_CODE_INLINEASM: { 2344 if (Record.size() < 2) 2345 return error("Invalid record"); 2346 std::string AsmStr, ConstrStr; 2347 bool HasSideEffects = Record[0] & 1; 2348 bool IsAlignStack = (Record[0] >> 1) & 1; 2349 unsigned AsmDialect = Record[0] >> 2; 2350 unsigned AsmStrSize = Record[1]; 2351 if (2+AsmStrSize >= Record.size()) 2352 return error("Invalid record"); 2353 unsigned ConstStrSize = Record[2+AsmStrSize]; 2354 if (3+AsmStrSize+ConstStrSize > Record.size()) 2355 return error("Invalid record"); 2356 2357 for (unsigned i = 0; i != AsmStrSize; ++i) 2358 AsmStr += (char)Record[2+i]; 2359 for (unsigned i = 0; i != ConstStrSize; ++i) 2360 ConstrStr += (char)Record[3+AsmStrSize+i]; 2361 PointerType *PTy = cast<PointerType>(CurTy); 2362 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()), 2363 AsmStr, ConstrStr, HasSideEffects, IsAlignStack, 2364 InlineAsm::AsmDialect(AsmDialect)); 2365 break; 2366 } 2367 case bitc::CST_CODE_BLOCKADDRESS:{ 2368 if (Record.size() < 3) 2369 return error("Invalid record"); 2370 Type *FnTy = getTypeByID(Record[0]); 2371 if (!FnTy) 2372 return error("Invalid record"); 2373 Function *Fn = 2374 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); 2375 if (!Fn) 2376 return error("Invalid record"); 2377 2378 // If the function is already parsed we can insert the block address right 2379 // away. 2380 BasicBlock *BB; 2381 unsigned BBID = Record[2]; 2382 if (!BBID) 2383 // Invalid reference to entry block. 2384 return error("Invalid ID"); 2385 if (!Fn->empty()) { 2386 Function::iterator BBI = Fn->begin(), BBE = Fn->end(); 2387 for (size_t I = 0, E = BBID; I != E; ++I) { 2388 if (BBI == BBE) 2389 return error("Invalid ID"); 2390 ++BBI; 2391 } 2392 BB = &*BBI; 2393 } else { 2394 // Otherwise insert a placeholder and remember it so it can be inserted 2395 // when the function is parsed. 2396 auto &FwdBBs = BasicBlockFwdRefs[Fn]; 2397 if (FwdBBs.empty()) 2398 BasicBlockFwdRefQueue.push_back(Fn); 2399 if (FwdBBs.size() < BBID + 1) 2400 FwdBBs.resize(BBID + 1); 2401 if (!FwdBBs[BBID]) 2402 FwdBBs[BBID] = BasicBlock::Create(Context); 2403 BB = FwdBBs[BBID]; 2404 } 2405 V = BlockAddress::get(Fn, BB); 2406 break; 2407 } 2408 } 2409 2410 ValueList.assignValue(V, NextCstNo); 2411 ++NextCstNo; 2412 } 2413 } 2414 2415 Error BitcodeReader::parseUseLists() { 2416 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) 2417 return error("Invalid record"); 2418 2419 // Read all the records. 2420 SmallVector<uint64_t, 64> Record; 2421 2422 while (true) { 2423 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 2424 2425 switch (Entry.Kind) { 2426 case BitstreamEntry::SubBlock: // Handled for us already. 2427 case BitstreamEntry::Error: 2428 return error("Malformed block"); 2429 case BitstreamEntry::EndBlock: 2430 return Error::success(); 2431 case BitstreamEntry::Record: 2432 // The interesting case. 2433 break; 2434 } 2435 2436 // Read a use list record. 2437 Record.clear(); 2438 bool IsBB = false; 2439 switch (Stream.readRecord(Entry.ID, Record)) { 2440 default: // Default behavior: unknown type. 2441 break; 2442 case bitc::USELIST_CODE_BB: 2443 IsBB = true; 2444 LLVM_FALLTHROUGH; 2445 case bitc::USELIST_CODE_DEFAULT: { 2446 unsigned RecordLength = Record.size(); 2447 if (RecordLength < 3) 2448 // Records should have at least an ID and two indexes. 2449 return error("Invalid record"); 2450 unsigned ID = Record.back(); 2451 Record.pop_back(); 2452 2453 Value *V; 2454 if (IsBB) { 2455 assert(ID < FunctionBBs.size() && "Basic block not found"); 2456 V = FunctionBBs[ID]; 2457 } else 2458 V = ValueList[ID]; 2459 unsigned NumUses = 0; 2460 SmallDenseMap<const Use *, unsigned, 16> Order; 2461 for (const Use &U : V->materialized_uses()) { 2462 if (++NumUses > Record.size()) 2463 break; 2464 Order[&U] = Record[NumUses - 1]; 2465 } 2466 if (Order.size() != Record.size() || NumUses > Record.size()) 2467 // Mismatches can happen if the functions are being materialized lazily 2468 // (out-of-order), or a value has been upgraded. 2469 break; 2470 2471 V->sortUseList([&](const Use &L, const Use &R) { 2472 return Order.lookup(&L) < Order.lookup(&R); 2473 }); 2474 break; 2475 } 2476 } 2477 } 2478 } 2479 2480 /// When we see the block for metadata, remember where it is and then skip it. 2481 /// This lets us lazily deserialize the metadata. 2482 Error BitcodeReader::rememberAndSkipMetadata() { 2483 // Save the current stream state. 2484 uint64_t CurBit = Stream.GetCurrentBitNo(); 2485 DeferredMetadataInfo.push_back(CurBit); 2486 2487 // Skip over the block for now. 2488 if (Stream.SkipBlock()) 2489 return error("Invalid record"); 2490 return Error::success(); 2491 } 2492 2493 Error BitcodeReader::materializeMetadata() { 2494 for (uint64_t BitPos : DeferredMetadataInfo) { 2495 // Move the bit stream to the saved position. 2496 Stream.JumpToBit(BitPos); 2497 if (Error Err = MDLoader->parseModuleMetadata()) 2498 return Err; 2499 } 2500 DeferredMetadataInfo.clear(); 2501 return Error::success(); 2502 } 2503 2504 void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; } 2505 2506 /// When we see the block for a function body, remember where it is and then 2507 /// skip it. This lets us lazily deserialize the functions. 2508 Error BitcodeReader::rememberAndSkipFunctionBody() { 2509 // Get the function we are talking about. 2510 if (FunctionsWithBodies.empty()) 2511 return error("Insufficient function protos"); 2512 2513 Function *Fn = FunctionsWithBodies.back(); 2514 FunctionsWithBodies.pop_back(); 2515 2516 // Save the current stream state. 2517 uint64_t CurBit = Stream.GetCurrentBitNo(); 2518 assert( 2519 (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) && 2520 "Mismatch between VST and scanned function offsets"); 2521 DeferredFunctionInfo[Fn] = CurBit; 2522 2523 // Skip over the function block for now. 2524 if (Stream.SkipBlock()) 2525 return error("Invalid record"); 2526 return Error::success(); 2527 } 2528 2529 Error BitcodeReader::globalCleanup() { 2530 // Patch the initializers for globals and aliases up. 2531 if (Error Err = resolveGlobalAndIndirectSymbolInits()) 2532 return Err; 2533 if (!GlobalInits.empty() || !IndirectSymbolInits.empty()) 2534 return error("Malformed global initializer set"); 2535 2536 // Look for intrinsic functions which need to be upgraded at some point 2537 for (Function &F : *TheModule) { 2538 Function *NewFn; 2539 if (UpgradeIntrinsicFunction(&F, NewFn)) 2540 UpgradedIntrinsics[&F] = NewFn; 2541 else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F)) 2542 // Some types could be renamed during loading if several modules are 2543 // loaded in the same LLVMContext (LTO scenario). In this case we should 2544 // remangle intrinsics names as well. 2545 RemangledIntrinsics[&F] = Remangled.getValue(); 2546 } 2547 2548 // Look for global variables which need to be renamed. 2549 for (GlobalVariable &GV : TheModule->globals()) 2550 UpgradeGlobalVariable(&GV); 2551 2552 // Force deallocation of memory for these vectors to favor the client that 2553 // want lazy deserialization. 2554 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits); 2555 std::vector<std::pair<GlobalIndirectSymbol*, unsigned> >().swap( 2556 IndirectSymbolInits); 2557 return Error::success(); 2558 } 2559 2560 /// Support for lazy parsing of function bodies. This is required if we 2561 /// either have an old bitcode file without a VST forward declaration record, 2562 /// or if we have an anonymous function being materialized, since anonymous 2563 /// functions do not have a name and are therefore not in the VST. 2564 Error BitcodeReader::rememberAndSkipFunctionBodies() { 2565 Stream.JumpToBit(NextUnreadBit); 2566 2567 if (Stream.AtEndOfStream()) 2568 return error("Could not find function in stream"); 2569 2570 if (!SeenFirstFunctionBody) 2571 return error("Trying to materialize functions before seeing function blocks"); 2572 2573 // An old bitcode file with the symbol table at the end would have 2574 // finished the parse greedily. 2575 assert(SeenValueSymbolTable); 2576 2577 SmallVector<uint64_t, 64> Record; 2578 2579 while (true) { 2580 BitstreamEntry Entry = Stream.advance(); 2581 switch (Entry.Kind) { 2582 default: 2583 return error("Expect SubBlock"); 2584 case BitstreamEntry::SubBlock: 2585 switch (Entry.ID) { 2586 default: 2587 return error("Expect function block"); 2588 case bitc::FUNCTION_BLOCK_ID: 2589 if (Error Err = rememberAndSkipFunctionBody()) 2590 return Err; 2591 NextUnreadBit = Stream.GetCurrentBitNo(); 2592 return Error::success(); 2593 } 2594 } 2595 } 2596 } 2597 2598 bool BitcodeReaderBase::readBlockInfo() { 2599 Optional<BitstreamBlockInfo> NewBlockInfo = Stream.ReadBlockInfoBlock(); 2600 if (!NewBlockInfo) 2601 return true; 2602 BlockInfo = std::move(*NewBlockInfo); 2603 return false; 2604 } 2605 2606 Error BitcodeReader::parseModule(uint64_t ResumeBit, 2607 bool ShouldLazyLoadMetadata) { 2608 if (ResumeBit) 2609 Stream.JumpToBit(ResumeBit); 2610 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 2611 return error("Invalid record"); 2612 2613 SmallVector<uint64_t, 64> Record; 2614 std::vector<std::string> SectionTable; 2615 std::vector<std::string> GCTable; 2616 2617 // Read all the records for this module. 2618 while (true) { 2619 BitstreamEntry Entry = Stream.advance(); 2620 2621 switch (Entry.Kind) { 2622 case BitstreamEntry::Error: 2623 return error("Malformed block"); 2624 case BitstreamEntry::EndBlock: 2625 return globalCleanup(); 2626 2627 case BitstreamEntry::SubBlock: 2628 switch (Entry.ID) { 2629 default: // Skip unknown content. 2630 if (Stream.SkipBlock()) 2631 return error("Invalid record"); 2632 break; 2633 case bitc::BLOCKINFO_BLOCK_ID: 2634 if (readBlockInfo()) 2635 return error("Malformed block"); 2636 break; 2637 case bitc::PARAMATTR_BLOCK_ID: 2638 if (Error Err = parseAttributeBlock()) 2639 return Err; 2640 break; 2641 case bitc::PARAMATTR_GROUP_BLOCK_ID: 2642 if (Error Err = parseAttributeGroupBlock()) 2643 return Err; 2644 break; 2645 case bitc::TYPE_BLOCK_ID_NEW: 2646 if (Error Err = parseTypeTable()) 2647 return Err; 2648 break; 2649 case bitc::VALUE_SYMTAB_BLOCK_ID: 2650 if (!SeenValueSymbolTable) { 2651 // Either this is an old form VST without function index and an 2652 // associated VST forward declaration record (which would have caused 2653 // the VST to be jumped to and parsed before it was encountered 2654 // normally in the stream), or there were no function blocks to 2655 // trigger an earlier parsing of the VST. 2656 assert(VSTOffset == 0 || FunctionsWithBodies.empty()); 2657 if (Error Err = parseValueSymbolTable()) 2658 return Err; 2659 SeenValueSymbolTable = true; 2660 } else { 2661 // We must have had a VST forward declaration record, which caused 2662 // the parser to jump to and parse the VST earlier. 2663 assert(VSTOffset > 0); 2664 if (Stream.SkipBlock()) 2665 return error("Invalid record"); 2666 } 2667 break; 2668 case bitc::CONSTANTS_BLOCK_ID: 2669 if (Error Err = parseConstants()) 2670 return Err; 2671 if (Error Err = resolveGlobalAndIndirectSymbolInits()) 2672 return Err; 2673 break; 2674 case bitc::METADATA_BLOCK_ID: 2675 if (ShouldLazyLoadMetadata) { 2676 if (Error Err = rememberAndSkipMetadata()) 2677 return Err; 2678 break; 2679 } 2680 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata"); 2681 if (Error Err = MDLoader->parseModuleMetadata()) 2682 return Err; 2683 break; 2684 case bitc::METADATA_KIND_BLOCK_ID: 2685 if (Error Err = MDLoader->parseMetadataKinds()) 2686 return Err; 2687 break; 2688 case bitc::FUNCTION_BLOCK_ID: 2689 // If this is the first function body we've seen, reverse the 2690 // FunctionsWithBodies list. 2691 if (!SeenFirstFunctionBody) { 2692 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end()); 2693 if (Error Err = globalCleanup()) 2694 return Err; 2695 SeenFirstFunctionBody = true; 2696 } 2697 2698 if (VSTOffset > 0) { 2699 // If we have a VST forward declaration record, make sure we 2700 // parse the VST now if we haven't already. It is needed to 2701 // set up the DeferredFunctionInfo vector for lazy reading. 2702 if (!SeenValueSymbolTable) { 2703 if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset)) 2704 return Err; 2705 SeenValueSymbolTable = true; 2706 // Fall through so that we record the NextUnreadBit below. 2707 // This is necessary in case we have an anonymous function that 2708 // is later materialized. Since it will not have a VST entry we 2709 // need to fall back to the lazy parse to find its offset. 2710 } else { 2711 // If we have a VST forward declaration record, but have already 2712 // parsed the VST (just above, when the first function body was 2713 // encountered here), then we are resuming the parse after 2714 // materializing functions. The ResumeBit points to the 2715 // start of the last function block recorded in the 2716 // DeferredFunctionInfo map. Skip it. 2717 if (Stream.SkipBlock()) 2718 return error("Invalid record"); 2719 continue; 2720 } 2721 } 2722 2723 // Support older bitcode files that did not have the function 2724 // index in the VST, nor a VST forward declaration record, as 2725 // well as anonymous functions that do not have VST entries. 2726 // Build the DeferredFunctionInfo vector on the fly. 2727 if (Error Err = rememberAndSkipFunctionBody()) 2728 return Err; 2729 2730 // Suspend parsing when we reach the function bodies. Subsequent 2731 // materialization calls will resume it when necessary. If the bitcode 2732 // file is old, the symbol table will be at the end instead and will not 2733 // have been seen yet. In this case, just finish the parse now. 2734 if (SeenValueSymbolTable) { 2735 NextUnreadBit = Stream.GetCurrentBitNo(); 2736 // After the VST has been parsed, we need to make sure intrinsic name 2737 // are auto-upgraded. 2738 return globalCleanup(); 2739 } 2740 break; 2741 case bitc::USELIST_BLOCK_ID: 2742 if (Error Err = parseUseLists()) 2743 return Err; 2744 break; 2745 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: 2746 if (Error Err = parseOperandBundleTags()) 2747 return Err; 2748 break; 2749 } 2750 continue; 2751 2752 case BitstreamEntry::Record: 2753 // The interesting case. 2754 break; 2755 } 2756 2757 // Read a record. 2758 auto BitCode = Stream.readRecord(Entry.ID, Record); 2759 switch (BitCode) { 2760 default: break; // Default behavior, ignore unknown content. 2761 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#] 2762 if (Record.size() < 1) 2763 return error("Invalid record"); 2764 // Only version #0 and #1 are supported so far. 2765 unsigned module_version = Record[0]; 2766 switch (module_version) { 2767 default: 2768 return error("Invalid value"); 2769 case 0: 2770 UseRelativeIDs = false; 2771 break; 2772 case 1: 2773 UseRelativeIDs = true; 2774 break; 2775 } 2776 break; 2777 } 2778 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] 2779 std::string S; 2780 if (convertToString(Record, 0, S)) 2781 return error("Invalid record"); 2782 TheModule->setTargetTriple(S); 2783 break; 2784 } 2785 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] 2786 std::string S; 2787 if (convertToString(Record, 0, S)) 2788 return error("Invalid record"); 2789 TheModule->setDataLayout(S); 2790 break; 2791 } 2792 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] 2793 std::string S; 2794 if (convertToString(Record, 0, S)) 2795 return error("Invalid record"); 2796 TheModule->setModuleInlineAsm(S); 2797 break; 2798 } 2799 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] 2800 // FIXME: Remove in 4.0. 2801 std::string S; 2802 if (convertToString(Record, 0, S)) 2803 return error("Invalid record"); 2804 // Ignore value. 2805 break; 2806 } 2807 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] 2808 std::string S; 2809 if (convertToString(Record, 0, S)) 2810 return error("Invalid record"); 2811 SectionTable.push_back(S); 2812 break; 2813 } 2814 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] 2815 std::string S; 2816 if (convertToString(Record, 0, S)) 2817 return error("Invalid record"); 2818 GCTable.push_back(S); 2819 break; 2820 } 2821 case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name] 2822 if (Record.size() < 2) 2823 return error("Invalid record"); 2824 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); 2825 unsigned ComdatNameSize = Record[1]; 2826 std::string ComdatName; 2827 ComdatName.reserve(ComdatNameSize); 2828 for (unsigned i = 0; i != ComdatNameSize; ++i) 2829 ComdatName += (char)Record[2 + i]; 2830 Comdat *C = TheModule->getOrInsertComdat(ComdatName); 2831 C->setSelectionKind(SK); 2832 ComdatList.push_back(C); 2833 break; 2834 } 2835 // GLOBALVAR: [pointer type, isconst, initid, 2836 // linkage, alignment, section, visibility, threadlocal, 2837 // unnamed_addr, externally_initialized, dllstorageclass, 2838 // comdat] 2839 case bitc::MODULE_CODE_GLOBALVAR: { 2840 if (Record.size() < 6) 2841 return error("Invalid record"); 2842 Type *Ty = getTypeByID(Record[0]); 2843 if (!Ty) 2844 return error("Invalid record"); 2845 bool isConstant = Record[1] & 1; 2846 bool explicitType = Record[1] & 2; 2847 unsigned AddressSpace; 2848 if (explicitType) { 2849 AddressSpace = Record[1] >> 2; 2850 } else { 2851 if (!Ty->isPointerTy()) 2852 return error("Invalid type for value"); 2853 AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); 2854 Ty = cast<PointerType>(Ty)->getElementType(); 2855 } 2856 2857 uint64_t RawLinkage = Record[3]; 2858 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 2859 unsigned Alignment; 2860 if (Error Err = parseAlignmentValue(Record[4], Alignment)) 2861 return Err; 2862 std::string Section; 2863 if (Record[5]) { 2864 if (Record[5]-1 >= SectionTable.size()) 2865 return error("Invalid ID"); 2866 Section = SectionTable[Record[5]-1]; 2867 } 2868 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; 2869 // Local linkage must have default visibility. 2870 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) 2871 // FIXME: Change to an error if non-default in 4.0. 2872 Visibility = getDecodedVisibility(Record[6]); 2873 2874 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; 2875 if (Record.size() > 7) 2876 TLM = getDecodedThreadLocalMode(Record[7]); 2877 2878 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 2879 if (Record.size() > 8) 2880 UnnamedAddr = getDecodedUnnamedAddrType(Record[8]); 2881 2882 bool ExternallyInitialized = false; 2883 if (Record.size() > 9) 2884 ExternallyInitialized = Record[9]; 2885 2886 GlobalVariable *NewGV = 2887 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr, 2888 TLM, AddressSpace, ExternallyInitialized); 2889 NewGV->setAlignment(Alignment); 2890 if (!Section.empty()) 2891 NewGV->setSection(Section); 2892 NewGV->setVisibility(Visibility); 2893 NewGV->setUnnamedAddr(UnnamedAddr); 2894 2895 if (Record.size() > 10) 2896 NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10])); 2897 else 2898 upgradeDLLImportExportLinkage(NewGV, RawLinkage); 2899 2900 ValueList.push_back(NewGV); 2901 2902 // Remember which value to use for the global initializer. 2903 if (unsigned InitID = Record[2]) 2904 GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); 2905 2906 if (Record.size() > 11) { 2907 if (unsigned ComdatID = Record[11]) { 2908 if (ComdatID > ComdatList.size()) 2909 return error("Invalid global variable comdat ID"); 2910 NewGV->setComdat(ComdatList[ComdatID - 1]); 2911 } 2912 } else if (hasImplicitComdat(RawLinkage)) { 2913 NewGV->setComdat(reinterpret_cast<Comdat *>(1)); 2914 } 2915 2916 break; 2917 } 2918 // FUNCTION: [type, callingconv, isproto, linkage, paramattr, 2919 // alignment, section, visibility, gc, unnamed_addr, 2920 // prologuedata, dllstorageclass, comdat, prefixdata] 2921 case bitc::MODULE_CODE_FUNCTION: { 2922 if (Record.size() < 8) 2923 return error("Invalid record"); 2924 Type *Ty = getTypeByID(Record[0]); 2925 if (!Ty) 2926 return error("Invalid record"); 2927 if (auto *PTy = dyn_cast<PointerType>(Ty)) 2928 Ty = PTy->getElementType(); 2929 auto *FTy = dyn_cast<FunctionType>(Ty); 2930 if (!FTy) 2931 return error("Invalid type for value"); 2932 auto CC = static_cast<CallingConv::ID>(Record[1]); 2933 if (CC & ~CallingConv::MaxID) 2934 return error("Invalid calling convention ID"); 2935 2936 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, 2937 "", TheModule); 2938 2939 Func->setCallingConv(CC); 2940 bool isProto = Record[2]; 2941 uint64_t RawLinkage = Record[3]; 2942 Func->setLinkage(getDecodedLinkage(RawLinkage)); 2943 Func->setAttributes(getAttributes(Record[4])); 2944 2945 unsigned Alignment; 2946 if (Error Err = parseAlignmentValue(Record[5], Alignment)) 2947 return Err; 2948 Func->setAlignment(Alignment); 2949 if (Record[6]) { 2950 if (Record[6]-1 >= SectionTable.size()) 2951 return error("Invalid ID"); 2952 Func->setSection(SectionTable[Record[6]-1]); 2953 } 2954 // Local linkage must have default visibility. 2955 if (!Func->hasLocalLinkage()) 2956 // FIXME: Change to an error if non-default in 4.0. 2957 Func->setVisibility(getDecodedVisibility(Record[7])); 2958 if (Record.size() > 8 && Record[8]) { 2959 if (Record[8]-1 >= GCTable.size()) 2960 return error("Invalid ID"); 2961 Func->setGC(GCTable[Record[8] - 1]); 2962 } 2963 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; 2964 if (Record.size() > 9) 2965 UnnamedAddr = getDecodedUnnamedAddrType(Record[9]); 2966 Func->setUnnamedAddr(UnnamedAddr); 2967 if (Record.size() > 10 && Record[10] != 0) 2968 FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1)); 2969 2970 if (Record.size() > 11) 2971 Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11])); 2972 else 2973 upgradeDLLImportExportLinkage(Func, RawLinkage); 2974 2975 if (Record.size() > 12) { 2976 if (unsigned ComdatID = Record[12]) { 2977 if (ComdatID > ComdatList.size()) 2978 return error("Invalid function comdat ID"); 2979 Func->setComdat(ComdatList[ComdatID - 1]); 2980 } 2981 } else if (hasImplicitComdat(RawLinkage)) { 2982 Func->setComdat(reinterpret_cast<Comdat *>(1)); 2983 } 2984 2985 if (Record.size() > 13 && Record[13] != 0) 2986 FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1)); 2987 2988 if (Record.size() > 14 && Record[14] != 0) 2989 FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1)); 2990 2991 ValueList.push_back(Func); 2992 2993 // If this is a function with a body, remember the prototype we are 2994 // creating now, so that we can match up the body with them later. 2995 if (!isProto) { 2996 Func->setIsMaterializable(true); 2997 FunctionsWithBodies.push_back(Func); 2998 DeferredFunctionInfo[Func] = 0; 2999 } 3000 break; 3001 } 3002 // ALIAS: [alias type, addrspace, aliasee val#, linkage] 3003 // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass] 3004 // IFUNC: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass] 3005 case bitc::MODULE_CODE_IFUNC: 3006 case bitc::MODULE_CODE_ALIAS: 3007 case bitc::MODULE_CODE_ALIAS_OLD: { 3008 bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD; 3009 if (Record.size() < (3 + (unsigned)NewRecord)) 3010 return error("Invalid record"); 3011 unsigned OpNum = 0; 3012 Type *Ty = getTypeByID(Record[OpNum++]); 3013 if (!Ty) 3014 return error("Invalid record"); 3015 3016 unsigned AddrSpace; 3017 if (!NewRecord) { 3018 auto *PTy = dyn_cast<PointerType>(Ty); 3019 if (!PTy) 3020 return error("Invalid type for value"); 3021 Ty = PTy->getElementType(); 3022 AddrSpace = PTy->getAddressSpace(); 3023 } else { 3024 AddrSpace = Record[OpNum++]; 3025 } 3026 3027 auto Val = Record[OpNum++]; 3028 auto Linkage = Record[OpNum++]; 3029 GlobalIndirectSymbol *NewGA; 3030 if (BitCode == bitc::MODULE_CODE_ALIAS || 3031 BitCode == bitc::MODULE_CODE_ALIAS_OLD) 3032 NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), 3033 "", TheModule); 3034 else 3035 NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), 3036 "", nullptr, TheModule); 3037 // Old bitcode files didn't have visibility field. 3038 // Local linkage must have default visibility. 3039 if (OpNum != Record.size()) { 3040 auto VisInd = OpNum++; 3041 if (!NewGA->hasLocalLinkage()) 3042 // FIXME: Change to an error if non-default in 4.0. 3043 NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); 3044 } 3045 if (OpNum != Record.size()) 3046 NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++])); 3047 else 3048 upgradeDLLImportExportLinkage(NewGA, Linkage); 3049 if (OpNum != Record.size()) 3050 NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++])); 3051 if (OpNum != Record.size()) 3052 NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++])); 3053 ValueList.push_back(NewGA); 3054 IndirectSymbolInits.push_back(std::make_pair(NewGA, Val)); 3055 break; 3056 } 3057 /// MODULE_CODE_PURGEVALS: [numvals] 3058 case bitc::MODULE_CODE_PURGEVALS: 3059 // Trim down the value list to the specified size. 3060 if (Record.size() < 1 || Record[0] > ValueList.size()) 3061 return error("Invalid record"); 3062 ValueList.shrinkTo(Record[0]); 3063 break; 3064 /// MODULE_CODE_VSTOFFSET: [offset] 3065 case bitc::MODULE_CODE_VSTOFFSET: 3066 if (Record.size() < 1) 3067 return error("Invalid record"); 3068 // Note that we subtract 1 here because the offset is relative to one word 3069 // before the start of the identification or module block, which was 3070 // historically always the start of the regular bitcode header. 3071 VSTOffset = Record[0] - 1; 3072 break; 3073 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 3074 case bitc::MODULE_CODE_SOURCE_FILENAME: 3075 SmallString<128> ValueName; 3076 if (convertToString(Record, 0, ValueName)) 3077 return error("Invalid record"); 3078 TheModule->setSourceFileName(ValueName); 3079 break; 3080 } 3081 Record.clear(); 3082 } 3083 } 3084 3085 Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, 3086 bool IsImporting) { 3087 TheModule = M; 3088 MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, 3089 [&](unsigned ID) { return getTypeByID(ID); }); 3090 return parseModule(0, ShouldLazyLoadMetadata); 3091 } 3092 3093 3094 Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { 3095 if (!isa<PointerType>(PtrType)) 3096 return error("Load/Store operand is not a pointer type"); 3097 Type *ElemType = cast<PointerType>(PtrType)->getElementType(); 3098 3099 if (ValType && ValType != ElemType) 3100 return error("Explicit load/store type does not match pointee " 3101 "type of pointer operand"); 3102 if (!PointerType::isLoadableOrStorableType(ElemType)) 3103 return error("Cannot load/store from pointer"); 3104 return Error::success(); 3105 } 3106 3107 /// Lazily parse the specified function body block. 3108 Error BitcodeReader::parseFunctionBody(Function *F) { 3109 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) 3110 return error("Invalid record"); 3111 3112 // Unexpected unresolved metadata when parsing function. 3113 if (MDLoader->hasFwdRefs()) 3114 return error("Invalid function metadata: incoming forward references"); 3115 3116 InstructionList.clear(); 3117 unsigned ModuleValueListSize = ValueList.size(); 3118 unsigned ModuleMDLoaderSize = MDLoader->size(); 3119 3120 // Add all the function arguments to the value table. 3121 for (Argument &I : F->args()) 3122 ValueList.push_back(&I); 3123 3124 unsigned NextValueNo = ValueList.size(); 3125 BasicBlock *CurBB = nullptr; 3126 unsigned CurBBNo = 0; 3127 3128 DebugLoc LastLoc; 3129 auto getLastInstruction = [&]() -> Instruction * { 3130 if (CurBB && !CurBB->empty()) 3131 return &CurBB->back(); 3132 else if (CurBBNo && FunctionBBs[CurBBNo - 1] && 3133 !FunctionBBs[CurBBNo - 1]->empty()) 3134 return &FunctionBBs[CurBBNo - 1]->back(); 3135 return nullptr; 3136 }; 3137 3138 std::vector<OperandBundleDef> OperandBundles; 3139 3140 // Read all the records. 3141 SmallVector<uint64_t, 64> Record; 3142 3143 while (true) { 3144 BitstreamEntry Entry = Stream.advance(); 3145 3146 switch (Entry.Kind) { 3147 case BitstreamEntry::Error: 3148 return error("Malformed block"); 3149 case BitstreamEntry::EndBlock: 3150 goto OutOfRecordLoop; 3151 3152 case BitstreamEntry::SubBlock: 3153 switch (Entry.ID) { 3154 default: // Skip unknown content. 3155 if (Stream.SkipBlock()) 3156 return error("Invalid record"); 3157 break; 3158 case bitc::CONSTANTS_BLOCK_ID: 3159 if (Error Err = parseConstants()) 3160 return Err; 3161 NextValueNo = ValueList.size(); 3162 break; 3163 case bitc::VALUE_SYMTAB_BLOCK_ID: 3164 if (Error Err = parseValueSymbolTable()) 3165 return Err; 3166 break; 3167 case bitc::METADATA_ATTACHMENT_ID: 3168 if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList)) 3169 return Err; 3170 break; 3171 case bitc::METADATA_BLOCK_ID: 3172 assert(DeferredMetadataInfo.empty() && 3173 "Must read all module-level metadata before function-level"); 3174 if (Error Err = MDLoader->parseFunctionMetadata()) 3175 return Err; 3176 break; 3177 case bitc::USELIST_BLOCK_ID: 3178 if (Error Err = parseUseLists()) 3179 return Err; 3180 break; 3181 } 3182 continue; 3183 3184 case BitstreamEntry::Record: 3185 // The interesting case. 3186 break; 3187 } 3188 3189 // Read a record. 3190 Record.clear(); 3191 Instruction *I = nullptr; 3192 unsigned BitCode = Stream.readRecord(Entry.ID, Record); 3193 switch (BitCode) { 3194 default: // Default behavior: reject 3195 return error("Invalid value"); 3196 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] 3197 if (Record.size() < 1 || Record[0] == 0) 3198 return error("Invalid record"); 3199 // Create all the basic blocks for the function. 3200 FunctionBBs.resize(Record[0]); 3201 3202 // See if anything took the address of blocks in this function. 3203 auto BBFRI = BasicBlockFwdRefs.find(F); 3204 if (BBFRI == BasicBlockFwdRefs.end()) { 3205 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i) 3206 FunctionBBs[i] = BasicBlock::Create(Context, "", F); 3207 } else { 3208 auto &BBRefs = BBFRI->second; 3209 // Check for invalid basic block references. 3210 if (BBRefs.size() > FunctionBBs.size()) 3211 return error("Invalid ID"); 3212 assert(!BBRefs.empty() && "Unexpected empty array"); 3213 assert(!BBRefs.front() && "Invalid reference to entry block"); 3214 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; 3215 ++I) 3216 if (I < RE && BBRefs[I]) { 3217 BBRefs[I]->insertInto(F); 3218 FunctionBBs[I] = BBRefs[I]; 3219 } else { 3220 FunctionBBs[I] = BasicBlock::Create(Context, "", F); 3221 } 3222 3223 // Erase from the table. 3224 BasicBlockFwdRefs.erase(BBFRI); 3225 } 3226 3227 CurBB = FunctionBBs[0]; 3228 continue; 3229 } 3230 3231 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN 3232 // This record indicates that the last instruction is at the same 3233 // location as the previous instruction with a location. 3234 I = getLastInstruction(); 3235 3236 if (!I) 3237 return error("Invalid record"); 3238 I->setDebugLoc(LastLoc); 3239 I = nullptr; 3240 continue; 3241 3242 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] 3243 I = getLastInstruction(); 3244 if (!I || Record.size() < 4) 3245 return error("Invalid record"); 3246 3247 unsigned Line = Record[0], Col = Record[1]; 3248 unsigned ScopeID = Record[2], IAID = Record[3]; 3249 3250 MDNode *Scope = nullptr, *IA = nullptr; 3251 if (ScopeID) { 3252 Scope = MDLoader->getMDNodeFwdRefOrNull(ScopeID - 1); 3253 if (!Scope) 3254 return error("Invalid record"); 3255 } 3256 if (IAID) { 3257 IA = MDLoader->getMDNodeFwdRefOrNull(IAID - 1); 3258 if (!IA) 3259 return error("Invalid record"); 3260 } 3261 LastLoc = DebugLoc::get(Line, Col, Scope, IA); 3262 I->setDebugLoc(LastLoc); 3263 I = nullptr; 3264 continue; 3265 } 3266 3267 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode] 3268 unsigned OpNum = 0; 3269 Value *LHS, *RHS; 3270 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || 3271 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || 3272 OpNum+1 > Record.size()) 3273 return error("Invalid record"); 3274 3275 int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); 3276 if (Opc == -1) 3277 return error("Invalid record"); 3278 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 3279 InstructionList.push_back(I); 3280 if (OpNum < Record.size()) { 3281 if (Opc == Instruction::Add || 3282 Opc == Instruction::Sub || 3283 Opc == Instruction::Mul || 3284 Opc == Instruction::Shl) { 3285 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP)) 3286 cast<BinaryOperator>(I)->setHasNoSignedWrap(true); 3287 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) 3288 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true); 3289 } else if (Opc == Instruction::SDiv || 3290 Opc == Instruction::UDiv || 3291 Opc == Instruction::LShr || 3292 Opc == Instruction::AShr) { 3293 if (Record[OpNum] & (1 << bitc::PEO_EXACT)) 3294 cast<BinaryOperator>(I)->setIsExact(true); 3295 } else if (isa<FPMathOperator>(I)) { 3296 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]); 3297 if (FMF.any()) 3298 I->setFastMathFlags(FMF); 3299 } 3300 3301 } 3302 break; 3303 } 3304 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc] 3305 unsigned OpNum = 0; 3306 Value *Op; 3307 if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 3308 OpNum+2 != Record.size()) 3309 return error("Invalid record"); 3310 3311 Type *ResTy = getTypeByID(Record[OpNum]); 3312 int Opc = getDecodedCastOpcode(Record[OpNum + 1]); 3313 if (Opc == -1 || !ResTy) 3314 return error("Invalid record"); 3315 Instruction *Temp = nullptr; 3316 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { 3317 if (Temp) { 3318 InstructionList.push_back(Temp); 3319 CurBB->getInstList().push_back(Temp); 3320 } 3321 } else { 3322 auto CastOp = (Instruction::CastOps)Opc; 3323 if (!CastInst::castIsValid(CastOp, Op, ResTy)) 3324 return error("Invalid cast"); 3325 I = CastInst::Create(CastOp, Op, ResTy); 3326 } 3327 InstructionList.push_back(I); 3328 break; 3329 } 3330 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: 3331 case bitc::FUNC_CODE_INST_GEP_OLD: 3332 case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] 3333 unsigned OpNum = 0; 3334 3335 Type *Ty; 3336 bool InBounds; 3337 3338 if (BitCode == bitc::FUNC_CODE_INST_GEP) { 3339 InBounds = Record[OpNum++]; 3340 Ty = getTypeByID(Record[OpNum++]); 3341 } else { 3342 InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; 3343 Ty = nullptr; 3344 } 3345 3346 Value *BasePtr; 3347 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) 3348 return error("Invalid record"); 3349 3350 if (!Ty) 3351 Ty = cast<PointerType>(BasePtr->getType()->getScalarType()) 3352 ->getElementType(); 3353 else if (Ty != 3354 cast<PointerType>(BasePtr->getType()->getScalarType()) 3355 ->getElementType()) 3356 return error( 3357 "Explicit gep type does not match pointee type of pointer operand"); 3358 3359 SmallVector<Value*, 16> GEPIdx; 3360 while (OpNum != Record.size()) { 3361 Value *Op; 3362 if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 3363 return error("Invalid record"); 3364 GEPIdx.push_back(Op); 3365 } 3366 3367 I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); 3368 3369 InstructionList.push_back(I); 3370 if (InBounds) 3371 cast<GetElementPtrInst>(I)->setIsInBounds(true); 3372 break; 3373 } 3374 3375 case bitc::FUNC_CODE_INST_EXTRACTVAL: { 3376 // EXTRACTVAL: [opty, opval, n x indices] 3377 unsigned OpNum = 0; 3378 Value *Agg; 3379 if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) 3380 return error("Invalid record"); 3381 3382 unsigned RecSize = Record.size(); 3383 if (OpNum == RecSize) 3384 return error("EXTRACTVAL: Invalid instruction with 0 indices"); 3385 3386 SmallVector<unsigned, 4> EXTRACTVALIdx; 3387 Type *CurTy = Agg->getType(); 3388 for (; OpNum != RecSize; ++OpNum) { 3389 bool IsArray = CurTy->isArrayTy(); 3390 bool IsStruct = CurTy->isStructTy(); 3391 uint64_t Index = Record[OpNum]; 3392 3393 if (!IsStruct && !IsArray) 3394 return error("EXTRACTVAL: Invalid type"); 3395 if ((unsigned)Index != Index) 3396 return error("Invalid value"); 3397 if (IsStruct && Index >= CurTy->subtypes().size()) 3398 return error("EXTRACTVAL: Invalid struct index"); 3399 if (IsArray && Index >= CurTy->getArrayNumElements()) 3400 return error("EXTRACTVAL: Invalid array index"); 3401 EXTRACTVALIdx.push_back((unsigned)Index); 3402 3403 if (IsStruct) 3404 CurTy = CurTy->subtypes()[Index]; 3405 else 3406 CurTy = CurTy->subtypes()[0]; 3407 } 3408 3409 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); 3410 InstructionList.push_back(I); 3411 break; 3412 } 3413 3414 case bitc::FUNC_CODE_INST_INSERTVAL: { 3415 // INSERTVAL: [opty, opval, opty, opval, n x indices] 3416 unsigned OpNum = 0; 3417 Value *Agg; 3418 if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) 3419 return error("Invalid record"); 3420 Value *Val; 3421 if (getValueTypePair(Record, OpNum, NextValueNo, Val)) 3422 return error("Invalid record"); 3423 3424 unsigned RecSize = Record.size(); 3425 if (OpNum == RecSize) 3426 return error("INSERTVAL: Invalid instruction with 0 indices"); 3427 3428 SmallVector<unsigned, 4> INSERTVALIdx; 3429 Type *CurTy = Agg->getType(); 3430 for (; OpNum != RecSize; ++OpNum) { 3431 bool IsArray = CurTy->isArrayTy(); 3432 bool IsStruct = CurTy->isStructTy(); 3433 uint64_t Index = Record[OpNum]; 3434 3435 if (!IsStruct && !IsArray) 3436 return error("INSERTVAL: Invalid type"); 3437 if ((unsigned)Index != Index) 3438 return error("Invalid value"); 3439 if (IsStruct && Index >= CurTy->subtypes().size()) 3440 return error("INSERTVAL: Invalid struct index"); 3441 if (IsArray && Index >= CurTy->getArrayNumElements()) 3442 return error("INSERTVAL: Invalid array index"); 3443 3444 INSERTVALIdx.push_back((unsigned)Index); 3445 if (IsStruct) 3446 CurTy = CurTy->subtypes()[Index]; 3447 else 3448 CurTy = CurTy->subtypes()[0]; 3449 } 3450 3451 if (CurTy != Val->getType()) 3452 return error("Inserted value type doesn't match aggregate type"); 3453 3454 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); 3455 InstructionList.push_back(I); 3456 break; 3457 } 3458 3459 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval] 3460 // obsolete form of select 3461 // handles select i1 ... in old bitcode 3462 unsigned OpNum = 0; 3463 Value *TrueVal, *FalseVal, *Cond; 3464 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || 3465 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || 3466 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond)) 3467 return error("Invalid record"); 3468 3469 I = SelectInst::Create(Cond, TrueVal, FalseVal); 3470 InstructionList.push_back(I); 3471 break; 3472 } 3473 3474 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred] 3475 // new form of select 3476 // handles select i1 or select [N x i1] 3477 unsigned OpNum = 0; 3478 Value *TrueVal, *FalseVal, *Cond; 3479 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || 3480 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || 3481 getValueTypePair(Record, OpNum, NextValueNo, Cond)) 3482 return error("Invalid record"); 3483 3484 // select condition can be either i1 or [N x i1] 3485 if (VectorType* vector_type = 3486 dyn_cast<VectorType>(Cond->getType())) { 3487 // expect <n x i1> 3488 if (vector_type->getElementType() != Type::getInt1Ty(Context)) 3489 return error("Invalid type for value"); 3490 } else { 3491 // expect i1 3492 if (Cond->getType() != Type::getInt1Ty(Context)) 3493 return error("Invalid type for value"); 3494 } 3495 3496 I = SelectInst::Create(Cond, TrueVal, FalseVal); 3497 InstructionList.push_back(I); 3498 break; 3499 } 3500 3501 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval] 3502 unsigned OpNum = 0; 3503 Value *Vec, *Idx; 3504 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || 3505 getValueTypePair(Record, OpNum, NextValueNo, Idx)) 3506 return error("Invalid record"); 3507 if (!Vec->getType()->isVectorTy()) 3508 return error("Invalid type for value"); 3509 I = ExtractElementInst::Create(Vec, Idx); 3510 InstructionList.push_back(I); 3511 break; 3512 } 3513 3514 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval] 3515 unsigned OpNum = 0; 3516 Value *Vec, *Elt, *Idx; 3517 if (getValueTypePair(Record, OpNum, NextValueNo, Vec)) 3518 return error("Invalid record"); 3519 if (!Vec->getType()->isVectorTy()) 3520 return error("Invalid type for value"); 3521 if (popValue(Record, OpNum, NextValueNo, 3522 cast<VectorType>(Vec->getType())->getElementType(), Elt) || 3523 getValueTypePair(Record, OpNum, NextValueNo, Idx)) 3524 return error("Invalid record"); 3525 I = InsertElementInst::Create(Vec, Elt, Idx); 3526 InstructionList.push_back(I); 3527 break; 3528 } 3529 3530 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval] 3531 unsigned OpNum = 0; 3532 Value *Vec1, *Vec2, *Mask; 3533 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || 3534 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2)) 3535 return error("Invalid record"); 3536 3537 if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) 3538 return error("Invalid record"); 3539 if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) 3540 return error("Invalid type for value"); 3541 I = new ShuffleVectorInst(Vec1, Vec2, Mask); 3542 InstructionList.push_back(I); 3543 break; 3544 } 3545 3546 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred] 3547 // Old form of ICmp/FCmp returning bool 3548 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were 3549 // both legal on vectors but had different behaviour. 3550 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred] 3551 // FCmp/ICmp returning bool or vector of bool 3552 3553 unsigned OpNum = 0; 3554 Value *LHS, *RHS; 3555 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || 3556 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS)) 3557 return error("Invalid record"); 3558 3559 unsigned PredVal = Record[OpNum]; 3560 bool IsFP = LHS->getType()->isFPOrFPVectorTy(); 3561 FastMathFlags FMF; 3562 if (IsFP && Record.size() > OpNum+1) 3563 FMF = getDecodedFastMathFlags(Record[++OpNum]); 3564 3565 if (OpNum+1 != Record.size()) 3566 return error("Invalid record"); 3567 3568 if (LHS->getType()->isFPOrFPVectorTy()) 3569 I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS); 3570 else 3571 I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS); 3572 3573 if (FMF.any()) 3574 I->setFastMathFlags(FMF); 3575 InstructionList.push_back(I); 3576 break; 3577 } 3578 3579 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>] 3580 { 3581 unsigned Size = Record.size(); 3582 if (Size == 0) { 3583 I = ReturnInst::Create(Context); 3584 InstructionList.push_back(I); 3585 break; 3586 } 3587 3588 unsigned OpNum = 0; 3589 Value *Op = nullptr; 3590 if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 3591 return error("Invalid record"); 3592 if (OpNum != Record.size()) 3593 return error("Invalid record"); 3594 3595 I = ReturnInst::Create(Context, Op); 3596 InstructionList.push_back(I); 3597 break; 3598 } 3599 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] 3600 if (Record.size() != 1 && Record.size() != 3) 3601 return error("Invalid record"); 3602 BasicBlock *TrueDest = getBasicBlock(Record[0]); 3603 if (!TrueDest) 3604 return error("Invalid record"); 3605 3606 if (Record.size() == 1) { 3607 I = BranchInst::Create(TrueDest); 3608 InstructionList.push_back(I); 3609 } 3610 else { 3611 BasicBlock *FalseDest = getBasicBlock(Record[1]); 3612 Value *Cond = getValue(Record, 2, NextValueNo, 3613 Type::getInt1Ty(Context)); 3614 if (!FalseDest || !Cond) 3615 return error("Invalid record"); 3616 I = BranchInst::Create(TrueDest, FalseDest, Cond); 3617 InstructionList.push_back(I); 3618 } 3619 break; 3620 } 3621 case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#] 3622 if (Record.size() != 1 && Record.size() != 2) 3623 return error("Invalid record"); 3624 unsigned Idx = 0; 3625 Value *CleanupPad = 3626 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 3627 if (!CleanupPad) 3628 return error("Invalid record"); 3629 BasicBlock *UnwindDest = nullptr; 3630 if (Record.size() == 2) { 3631 UnwindDest = getBasicBlock(Record[Idx++]); 3632 if (!UnwindDest) 3633 return error("Invalid record"); 3634 } 3635 3636 I = CleanupReturnInst::Create(CleanupPad, UnwindDest); 3637 InstructionList.push_back(I); 3638 break; 3639 } 3640 case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#] 3641 if (Record.size() != 2) 3642 return error("Invalid record"); 3643 unsigned Idx = 0; 3644 Value *CatchPad = 3645 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 3646 if (!CatchPad) 3647 return error("Invalid record"); 3648 BasicBlock *BB = getBasicBlock(Record[Idx++]); 3649 if (!BB) 3650 return error("Invalid record"); 3651 3652 I = CatchReturnInst::Create(CatchPad, BB); 3653 InstructionList.push_back(I); 3654 break; 3655 } 3656 case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?] 3657 // We must have, at minimum, the outer scope and the number of arguments. 3658 if (Record.size() < 2) 3659 return error("Invalid record"); 3660 3661 unsigned Idx = 0; 3662 3663 Value *ParentPad = 3664 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 3665 3666 unsigned NumHandlers = Record[Idx++]; 3667 3668 SmallVector<BasicBlock *, 2> Handlers; 3669 for (unsigned Op = 0; Op != NumHandlers; ++Op) { 3670 BasicBlock *BB = getBasicBlock(Record[Idx++]); 3671 if (!BB) 3672 return error("Invalid record"); 3673 Handlers.push_back(BB); 3674 } 3675 3676 BasicBlock *UnwindDest = nullptr; 3677 if (Idx + 1 == Record.size()) { 3678 UnwindDest = getBasicBlock(Record[Idx++]); 3679 if (!UnwindDest) 3680 return error("Invalid record"); 3681 } 3682 3683 if (Record.size() != Idx) 3684 return error("Invalid record"); 3685 3686 auto *CatchSwitch = 3687 CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers); 3688 for (BasicBlock *Handler : Handlers) 3689 CatchSwitch->addHandler(Handler); 3690 I = CatchSwitch; 3691 InstructionList.push_back(I); 3692 break; 3693 } 3694 case bitc::FUNC_CODE_INST_CATCHPAD: 3695 case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*] 3696 // We must have, at minimum, the outer scope and the number of arguments. 3697 if (Record.size() < 2) 3698 return error("Invalid record"); 3699 3700 unsigned Idx = 0; 3701 3702 Value *ParentPad = 3703 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context)); 3704 3705 unsigned NumArgOperands = Record[Idx++]; 3706 3707 SmallVector<Value *, 2> Args; 3708 for (unsigned Op = 0; Op != NumArgOperands; ++Op) { 3709 Value *Val; 3710 if (getValueTypePair(Record, Idx, NextValueNo, Val)) 3711 return error("Invalid record"); 3712 Args.push_back(Val); 3713 } 3714 3715 if (Record.size() != Idx) 3716 return error("Invalid record"); 3717 3718 if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD) 3719 I = CleanupPadInst::Create(ParentPad, Args); 3720 else 3721 I = CatchPadInst::Create(ParentPad, Args); 3722 InstructionList.push_back(I); 3723 break; 3724 } 3725 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...] 3726 // Check magic 3727 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) { 3728 // "New" SwitchInst format with case ranges. The changes to write this 3729 // format were reverted but we still recognize bitcode that uses it. 3730 // Hopefully someday we will have support for case ranges and can use 3731 // this format again. 3732 3733 Type *OpTy = getTypeByID(Record[1]); 3734 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth(); 3735 3736 Value *Cond = getValue(Record, 2, NextValueNo, OpTy); 3737 BasicBlock *Default = getBasicBlock(Record[3]); 3738 if (!OpTy || !Cond || !Default) 3739 return error("Invalid record"); 3740 3741 unsigned NumCases = Record[4]; 3742 3743 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 3744 InstructionList.push_back(SI); 3745 3746 unsigned CurIdx = 5; 3747 for (unsigned i = 0; i != NumCases; ++i) { 3748 SmallVector<ConstantInt*, 1> CaseVals; 3749 unsigned NumItems = Record[CurIdx++]; 3750 for (unsigned ci = 0; ci != NumItems; ++ci) { 3751 bool isSingleNumber = Record[CurIdx++]; 3752 3753 APInt Low; 3754 unsigned ActiveWords = 1; 3755 if (ValueBitWidth > 64) 3756 ActiveWords = Record[CurIdx++]; 3757 Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords), 3758 ValueBitWidth); 3759 CurIdx += ActiveWords; 3760 3761 if (!isSingleNumber) { 3762 ActiveWords = 1; 3763 if (ValueBitWidth > 64) 3764 ActiveWords = Record[CurIdx++]; 3765 APInt High = readWideAPInt( 3766 makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth); 3767 CurIdx += ActiveWords; 3768 3769 // FIXME: It is not clear whether values in the range should be 3770 // compared as signed or unsigned values. The partially 3771 // implemented changes that used this format in the past used 3772 // unsigned comparisons. 3773 for ( ; Low.ule(High); ++Low) 3774 CaseVals.push_back(ConstantInt::get(Context, Low)); 3775 } else 3776 CaseVals.push_back(ConstantInt::get(Context, Low)); 3777 } 3778 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]); 3779 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(), 3780 cve = CaseVals.end(); cvi != cve; ++cvi) 3781 SI->addCase(*cvi, DestBB); 3782 } 3783 I = SI; 3784 break; 3785 } 3786 3787 // Old SwitchInst format without case ranges. 3788 3789 if (Record.size() < 3 || (Record.size() & 1) == 0) 3790 return error("Invalid record"); 3791 Type *OpTy = getTypeByID(Record[0]); 3792 Value *Cond = getValue(Record, 1, NextValueNo, OpTy); 3793 BasicBlock *Default = getBasicBlock(Record[2]); 3794 if (!OpTy || !Cond || !Default) 3795 return error("Invalid record"); 3796 unsigned NumCases = (Record.size()-3)/2; 3797 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); 3798 InstructionList.push_back(SI); 3799 for (unsigned i = 0, e = NumCases; i != e; ++i) { 3800 ConstantInt *CaseVal = 3801 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy)); 3802 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); 3803 if (!CaseVal || !DestBB) { 3804 delete SI; 3805 return error("Invalid record"); 3806 } 3807 SI->addCase(CaseVal, DestBB); 3808 } 3809 I = SI; 3810 break; 3811 } 3812 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] 3813 if (Record.size() < 2) 3814 return error("Invalid record"); 3815 Type *OpTy = getTypeByID(Record[0]); 3816 Value *Address = getValue(Record, 1, NextValueNo, OpTy); 3817 if (!OpTy || !Address) 3818 return error("Invalid record"); 3819 unsigned NumDests = Record.size()-2; 3820 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); 3821 InstructionList.push_back(IBI); 3822 for (unsigned i = 0, e = NumDests; i != e; ++i) { 3823 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) { 3824 IBI->addDestination(DestBB); 3825 } else { 3826 delete IBI; 3827 return error("Invalid record"); 3828 } 3829 } 3830 I = IBI; 3831 break; 3832 } 3833 3834 case bitc::FUNC_CODE_INST_INVOKE: { 3835 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] 3836 if (Record.size() < 4) 3837 return error("Invalid record"); 3838 unsigned OpNum = 0; 3839 AttributeSet PAL = getAttributes(Record[OpNum++]); 3840 unsigned CCInfo = Record[OpNum++]; 3841 BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]); 3842 BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]); 3843 3844 FunctionType *FTy = nullptr; 3845 if (CCInfo >> 13 & 1 && 3846 !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) 3847 return error("Explicit invoke type is not a function type"); 3848 3849 Value *Callee; 3850 if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) 3851 return error("Invalid record"); 3852 3853 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); 3854 if (!CalleeTy) 3855 return error("Callee is not a pointer"); 3856 if (!FTy) { 3857 FTy = dyn_cast<FunctionType>(CalleeTy->getElementType()); 3858 if (!FTy) 3859 return error("Callee is not of pointer to function type"); 3860 } else if (CalleeTy->getElementType() != FTy) 3861 return error("Explicit invoke type does not match pointee type of " 3862 "callee operand"); 3863 if (Record.size() < FTy->getNumParams() + OpNum) 3864 return error("Insufficient operands to call"); 3865 3866 SmallVector<Value*, 16> Ops; 3867 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 3868 Ops.push_back(getValue(Record, OpNum, NextValueNo, 3869 FTy->getParamType(i))); 3870 if (!Ops.back()) 3871 return error("Invalid record"); 3872 } 3873 3874 if (!FTy->isVarArg()) { 3875 if (Record.size() != OpNum) 3876 return error("Invalid record"); 3877 } else { 3878 // Read type/value pairs for varargs params. 3879 while (OpNum != Record.size()) { 3880 Value *Op; 3881 if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 3882 return error("Invalid record"); 3883 Ops.push_back(Op); 3884 } 3885 } 3886 3887 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles); 3888 OperandBundles.clear(); 3889 InstructionList.push_back(I); 3890 cast<InvokeInst>(I)->setCallingConv( 3891 static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo)); 3892 cast<InvokeInst>(I)->setAttributes(PAL); 3893 break; 3894 } 3895 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval] 3896 unsigned Idx = 0; 3897 Value *Val = nullptr; 3898 if (getValueTypePair(Record, Idx, NextValueNo, Val)) 3899 return error("Invalid record"); 3900 I = ResumeInst::Create(Val); 3901 InstructionList.push_back(I); 3902 break; 3903 } 3904 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE 3905 I = new UnreachableInst(Context); 3906 InstructionList.push_back(I); 3907 break; 3908 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] 3909 if (Record.size() < 1 || ((Record.size()-1)&1)) 3910 return error("Invalid record"); 3911 Type *Ty = getTypeByID(Record[0]); 3912 if (!Ty) 3913 return error("Invalid record"); 3914 3915 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); 3916 InstructionList.push_back(PN); 3917 3918 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) { 3919 Value *V; 3920 // With the new function encoding, it is possible that operands have 3921 // negative IDs (for forward references). Use a signed VBR 3922 // representation to keep the encoding small. 3923 if (UseRelativeIDs) 3924 V = getValueSigned(Record, 1+i, NextValueNo, Ty); 3925 else 3926 V = getValue(Record, 1+i, NextValueNo, Ty); 3927 BasicBlock *BB = getBasicBlock(Record[2+i]); 3928 if (!V || !BB) 3929 return error("Invalid record"); 3930 PN->addIncoming(V, BB); 3931 } 3932 I = PN; 3933 break; 3934 } 3935 3936 case bitc::FUNC_CODE_INST_LANDINGPAD: 3937 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: { 3938 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] 3939 unsigned Idx = 0; 3940 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) { 3941 if (Record.size() < 3) 3942 return error("Invalid record"); 3943 } else { 3944 assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD); 3945 if (Record.size() < 4) 3946 return error("Invalid record"); 3947 } 3948 Type *Ty = getTypeByID(Record[Idx++]); 3949 if (!Ty) 3950 return error("Invalid record"); 3951 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) { 3952 Value *PersFn = nullptr; 3953 if (getValueTypePair(Record, Idx, NextValueNo, PersFn)) 3954 return error("Invalid record"); 3955 3956 if (!F->hasPersonalityFn()) 3957 F->setPersonalityFn(cast<Constant>(PersFn)); 3958 else if (F->getPersonalityFn() != cast<Constant>(PersFn)) 3959 return error("Personality function mismatch"); 3960 } 3961 3962 bool IsCleanup = !!Record[Idx++]; 3963 unsigned NumClauses = Record[Idx++]; 3964 LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses); 3965 LP->setCleanup(IsCleanup); 3966 for (unsigned J = 0; J != NumClauses; ++J) { 3967 LandingPadInst::ClauseType CT = 3968 LandingPadInst::ClauseType(Record[Idx++]); (void)CT; 3969 Value *Val; 3970 3971 if (getValueTypePair(Record, Idx, NextValueNo, Val)) { 3972 delete LP; 3973 return error("Invalid record"); 3974 } 3975 3976 assert((CT != LandingPadInst::Catch || 3977 !isa<ArrayType>(Val->getType())) && 3978 "Catch clause has a invalid type!"); 3979 assert((CT != LandingPadInst::Filter || 3980 isa<ArrayType>(Val->getType())) && 3981 "Filter clause has invalid type!"); 3982 LP->addClause(cast<Constant>(Val)); 3983 } 3984 3985 I = LP; 3986 InstructionList.push_back(I); 3987 break; 3988 } 3989 3990 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] 3991 if (Record.size() != 4) 3992 return error("Invalid record"); 3993 uint64_t AlignRecord = Record[3]; 3994 const uint64_t InAllocaMask = uint64_t(1) << 5; 3995 const uint64_t ExplicitTypeMask = uint64_t(1) << 6; 3996 const uint64_t SwiftErrorMask = uint64_t(1) << 7; 3997 const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask | 3998 SwiftErrorMask; 3999 bool InAlloca = AlignRecord & InAllocaMask; 4000 bool SwiftError = AlignRecord & SwiftErrorMask; 4001 Type *Ty = getTypeByID(Record[0]); 4002 if ((AlignRecord & ExplicitTypeMask) == 0) { 4003 auto *PTy = dyn_cast_or_null<PointerType>(Ty); 4004 if (!PTy) 4005 return error("Old-style alloca with a non-pointer type"); 4006 Ty = PTy->getElementType(); 4007 } 4008 Type *OpTy = getTypeByID(Record[1]); 4009 Value *Size = getFnValueByID(Record[2], OpTy); 4010 unsigned Align; 4011 if (Error Err = parseAlignmentValue(AlignRecord & ~FlagMask, Align)) { 4012 return Err; 4013 } 4014 if (!Ty || !Size) 4015 return error("Invalid record"); 4016 AllocaInst *AI = new AllocaInst(Ty, Size, Align); 4017 AI->setUsedWithInAlloca(InAlloca); 4018 AI->setSwiftError(SwiftError); 4019 I = AI; 4020 InstructionList.push_back(I); 4021 break; 4022 } 4023 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol] 4024 unsigned OpNum = 0; 4025 Value *Op; 4026 if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 4027 (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) 4028 return error("Invalid record"); 4029 4030 Type *Ty = nullptr; 4031 if (OpNum + 3 == Record.size()) 4032 Ty = getTypeByID(Record[OpNum++]); 4033 if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 4034 return Err; 4035 if (!Ty) 4036 Ty = cast<PointerType>(Op->getType())->getElementType(); 4037 4038 unsigned Align; 4039 if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4040 return Err; 4041 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align); 4042 4043 InstructionList.push_back(I); 4044 break; 4045 } 4046 case bitc::FUNC_CODE_INST_LOADATOMIC: { 4047 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope] 4048 unsigned OpNum = 0; 4049 Value *Op; 4050 if (getValueTypePair(Record, OpNum, NextValueNo, Op) || 4051 (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) 4052 return error("Invalid record"); 4053 4054 Type *Ty = nullptr; 4055 if (OpNum + 5 == Record.size()) 4056 Ty = getTypeByID(Record[OpNum++]); 4057 if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType())) 4058 return Err; 4059 if (!Ty) 4060 Ty = cast<PointerType>(Op->getType())->getElementType(); 4061 4062 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 4063 if (Ordering == AtomicOrdering::NotAtomic || 4064 Ordering == AtomicOrdering::Release || 4065 Ordering == AtomicOrdering::AcquireRelease) 4066 return error("Invalid record"); 4067 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 4068 return error("Invalid record"); 4069 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); 4070 4071 unsigned Align; 4072 if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4073 return Err; 4074 I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope); 4075 4076 InstructionList.push_back(I); 4077 break; 4078 } 4079 case bitc::FUNC_CODE_INST_STORE: 4080 case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol] 4081 unsigned OpNum = 0; 4082 Value *Val, *Ptr; 4083 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 4084 (BitCode == bitc::FUNC_CODE_INST_STORE 4085 ? getValueTypePair(Record, OpNum, NextValueNo, Val) 4086 : popValue(Record, OpNum, NextValueNo, 4087 cast<PointerType>(Ptr->getType())->getElementType(), 4088 Val)) || 4089 OpNum + 2 != Record.size()) 4090 return error("Invalid record"); 4091 4092 if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 4093 return Err; 4094 unsigned Align; 4095 if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4096 return Err; 4097 I = new StoreInst(Val, Ptr, Record[OpNum+1], Align); 4098 InstructionList.push_back(I); 4099 break; 4100 } 4101 case bitc::FUNC_CODE_INST_STOREATOMIC: 4102 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: { 4103 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope] 4104 unsigned OpNum = 0; 4105 Value *Val, *Ptr; 4106 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 4107 !isa<PointerType>(Ptr->getType()) || 4108 (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC 4109 ? getValueTypePair(Record, OpNum, NextValueNo, Val) 4110 : popValue(Record, OpNum, NextValueNo, 4111 cast<PointerType>(Ptr->getType())->getElementType(), 4112 Val)) || 4113 OpNum + 4 != Record.size()) 4114 return error("Invalid record"); 4115 4116 if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType())) 4117 return Err; 4118 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 4119 if (Ordering == AtomicOrdering::NotAtomic || 4120 Ordering == AtomicOrdering::Acquire || 4121 Ordering == AtomicOrdering::AcquireRelease) 4122 return error("Invalid record"); 4123 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); 4124 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0) 4125 return error("Invalid record"); 4126 4127 unsigned Align; 4128 if (Error Err = parseAlignmentValue(Record[OpNum], Align)) 4129 return Err; 4130 I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope); 4131 InstructionList.push_back(I); 4132 break; 4133 } 4134 case bitc::FUNC_CODE_INST_CMPXCHG_OLD: 4135 case bitc::FUNC_CODE_INST_CMPXCHG: { 4136 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope, 4137 // failureordering?, isweak?] 4138 unsigned OpNum = 0; 4139 Value *Ptr, *Cmp, *New; 4140 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 4141 (BitCode == bitc::FUNC_CODE_INST_CMPXCHG 4142 ? getValueTypePair(Record, OpNum, NextValueNo, Cmp) 4143 : popValue(Record, OpNum, NextValueNo, 4144 cast<PointerType>(Ptr->getType())->getElementType(), 4145 Cmp)) || 4146 popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) || 4147 Record.size() < OpNum + 3 || Record.size() > OpNum + 5) 4148 return error("Invalid record"); 4149 AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]); 4150 if (SuccessOrdering == AtomicOrdering::NotAtomic || 4151 SuccessOrdering == AtomicOrdering::Unordered) 4152 return error("Invalid record"); 4153 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 2]); 4154 4155 if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType())) 4156 return Err; 4157 AtomicOrdering FailureOrdering; 4158 if (Record.size() < 7) 4159 FailureOrdering = 4160 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering); 4161 else 4162 FailureOrdering = getDecodedOrdering(Record[OpNum + 3]); 4163 4164 I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering, 4165 SynchScope); 4166 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); 4167 4168 if (Record.size() < 8) { 4169 // Before weak cmpxchgs existed, the instruction simply returned the 4170 // value loaded from memory, so bitcode files from that era will be 4171 // expecting the first component of a modern cmpxchg. 4172 CurBB->getInstList().push_back(I); 4173 I = ExtractValueInst::Create(I, 0); 4174 } else { 4175 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]); 4176 } 4177 4178 InstructionList.push_back(I); 4179 break; 4180 } 4181 case bitc::FUNC_CODE_INST_ATOMICRMW: { 4182 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope] 4183 unsigned OpNum = 0; 4184 Value *Ptr, *Val; 4185 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) || 4186 !isa<PointerType>(Ptr->getType()) || 4187 popValue(Record, OpNum, NextValueNo, 4188 cast<PointerType>(Ptr->getType())->getElementType(), Val) || 4189 OpNum+4 != Record.size()) 4190 return error("Invalid record"); 4191 AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]); 4192 if (Operation < AtomicRMWInst::FIRST_BINOP || 4193 Operation > AtomicRMWInst::LAST_BINOP) 4194 return error("Invalid record"); 4195 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]); 4196 if (Ordering == AtomicOrdering::NotAtomic || 4197 Ordering == AtomicOrdering::Unordered) 4198 return error("Invalid record"); 4199 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]); 4200 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope); 4201 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]); 4202 InstructionList.push_back(I); 4203 break; 4204 } 4205 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope] 4206 if (2 != Record.size()) 4207 return error("Invalid record"); 4208 AtomicOrdering Ordering = getDecodedOrdering(Record[0]); 4209 if (Ordering == AtomicOrdering::NotAtomic || 4210 Ordering == AtomicOrdering::Unordered || 4211 Ordering == AtomicOrdering::Monotonic) 4212 return error("Invalid record"); 4213 SynchronizationScope SynchScope = getDecodedSynchScope(Record[1]); 4214 I = new FenceInst(Context, Ordering, SynchScope); 4215 InstructionList.push_back(I); 4216 break; 4217 } 4218 case bitc::FUNC_CODE_INST_CALL: { 4219 // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...] 4220 if (Record.size() < 3) 4221 return error("Invalid record"); 4222 4223 unsigned OpNum = 0; 4224 AttributeSet PAL = getAttributes(Record[OpNum++]); 4225 unsigned CCInfo = Record[OpNum++]; 4226 4227 FastMathFlags FMF; 4228 if ((CCInfo >> bitc::CALL_FMF) & 1) { 4229 FMF = getDecodedFastMathFlags(Record[OpNum++]); 4230 if (!FMF.any()) 4231 return error("Fast math flags indicator set for call with no FMF"); 4232 } 4233 4234 FunctionType *FTy = nullptr; 4235 if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 && 4236 !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++])))) 4237 return error("Explicit call type is not a function type"); 4238 4239 Value *Callee; 4240 if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) 4241 return error("Invalid record"); 4242 4243 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); 4244 if (!OpTy) 4245 return error("Callee is not a pointer type"); 4246 if (!FTy) { 4247 FTy = dyn_cast<FunctionType>(OpTy->getElementType()); 4248 if (!FTy) 4249 return error("Callee is not of pointer to function type"); 4250 } else if (OpTy->getElementType() != FTy) 4251 return error("Explicit call type does not match pointee type of " 4252 "callee operand"); 4253 if (Record.size() < FTy->getNumParams() + OpNum) 4254 return error("Insufficient operands to call"); 4255 4256 SmallVector<Value*, 16> Args; 4257 // Read the fixed params. 4258 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { 4259 if (FTy->getParamType(i)->isLabelTy()) 4260 Args.push_back(getBasicBlock(Record[OpNum])); 4261 else 4262 Args.push_back(getValue(Record, OpNum, NextValueNo, 4263 FTy->getParamType(i))); 4264 if (!Args.back()) 4265 return error("Invalid record"); 4266 } 4267 4268 // Read type/value pairs for varargs params. 4269 if (!FTy->isVarArg()) { 4270 if (OpNum != Record.size()) 4271 return error("Invalid record"); 4272 } else { 4273 while (OpNum != Record.size()) { 4274 Value *Op; 4275 if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 4276 return error("Invalid record"); 4277 Args.push_back(Op); 4278 } 4279 } 4280 4281 I = CallInst::Create(FTy, Callee, Args, OperandBundles); 4282 OperandBundles.clear(); 4283 InstructionList.push_back(I); 4284 cast<CallInst>(I)->setCallingConv( 4285 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV)); 4286 CallInst::TailCallKind TCK = CallInst::TCK_None; 4287 if (CCInfo & 1 << bitc::CALL_TAIL) 4288 TCK = CallInst::TCK_Tail; 4289 if (CCInfo & (1 << bitc::CALL_MUSTTAIL)) 4290 TCK = CallInst::TCK_MustTail; 4291 if (CCInfo & (1 << bitc::CALL_NOTAIL)) 4292 TCK = CallInst::TCK_NoTail; 4293 cast<CallInst>(I)->setTailCallKind(TCK); 4294 cast<CallInst>(I)->setAttributes(PAL); 4295 if (FMF.any()) { 4296 if (!isa<FPMathOperator>(I)) 4297 return error("Fast-math-flags specified for call without " 4298 "floating-point scalar or vector return type"); 4299 I->setFastMathFlags(FMF); 4300 } 4301 break; 4302 } 4303 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] 4304 if (Record.size() < 3) 4305 return error("Invalid record"); 4306 Type *OpTy = getTypeByID(Record[0]); 4307 Value *Op = getValue(Record, 1, NextValueNo, OpTy); 4308 Type *ResTy = getTypeByID(Record[2]); 4309 if (!OpTy || !Op || !ResTy) 4310 return error("Invalid record"); 4311 I = new VAArgInst(Op, ResTy); 4312 InstructionList.push_back(I); 4313 break; 4314 } 4315 4316 case bitc::FUNC_CODE_OPERAND_BUNDLE: { 4317 // A call or an invoke can be optionally prefixed with some variable 4318 // number of operand bundle blocks. These blocks are read into 4319 // OperandBundles and consumed at the next call or invoke instruction. 4320 4321 if (Record.size() < 1 || Record[0] >= BundleTags.size()) 4322 return error("Invalid record"); 4323 4324 std::vector<Value *> Inputs; 4325 4326 unsigned OpNum = 1; 4327 while (OpNum != Record.size()) { 4328 Value *Op; 4329 if (getValueTypePair(Record, OpNum, NextValueNo, Op)) 4330 return error("Invalid record"); 4331 Inputs.push_back(Op); 4332 } 4333 4334 OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs)); 4335 continue; 4336 } 4337 } 4338 4339 // Add instruction to end of current BB. If there is no current BB, reject 4340 // this file. 4341 if (!CurBB) { 4342 delete I; 4343 return error("Invalid instruction with no BB"); 4344 } 4345 if (!OperandBundles.empty()) { 4346 delete I; 4347 return error("Operand bundles found with no consumer"); 4348 } 4349 CurBB->getInstList().push_back(I); 4350 4351 // If this was a terminator instruction, move to the next block. 4352 if (isa<TerminatorInst>(I)) { 4353 ++CurBBNo; 4354 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr; 4355 } 4356 4357 // Non-void values get registered in the value table for future use. 4358 if (I && !I->getType()->isVoidTy()) 4359 ValueList.assignValue(I, NextValueNo++); 4360 } 4361 4362 OutOfRecordLoop: 4363 4364 if (!OperandBundles.empty()) 4365 return error("Operand bundles found with no consumer"); 4366 4367 // Check the function list for unresolved values. 4368 if (Argument *A = dyn_cast<Argument>(ValueList.back())) { 4369 if (!A->getParent()) { 4370 // We found at least one unresolved value. Nuke them all to avoid leaks. 4371 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){ 4372 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) { 4373 A->replaceAllUsesWith(UndefValue::get(A->getType())); 4374 delete A; 4375 } 4376 } 4377 return error("Never resolved value found in function"); 4378 } 4379 } 4380 4381 // Unexpected unresolved metadata about to be dropped. 4382 if (MDLoader->hasFwdRefs()) 4383 return error("Invalid function metadata: outgoing forward refs"); 4384 4385 // Trim the value list down to the size it was before we parsed this function. 4386 ValueList.shrinkTo(ModuleValueListSize); 4387 MDLoader->shrinkTo(ModuleMDLoaderSize); 4388 std::vector<BasicBlock*>().swap(FunctionBBs); 4389 return Error::success(); 4390 } 4391 4392 /// Find the function body in the bitcode stream 4393 Error BitcodeReader::findFunctionInStream( 4394 Function *F, 4395 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { 4396 while (DeferredFunctionInfoIterator->second == 0) { 4397 // This is the fallback handling for the old format bitcode that 4398 // didn't contain the function index in the VST, or when we have 4399 // an anonymous function which would not have a VST entry. 4400 // Assert that we have one of those two cases. 4401 assert(VSTOffset == 0 || !F->hasName()); 4402 // Parse the next body in the stream and set its position in the 4403 // DeferredFunctionInfo map. 4404 if (Error Err = rememberAndSkipFunctionBodies()) 4405 return Err; 4406 } 4407 return Error::success(); 4408 } 4409 4410 //===----------------------------------------------------------------------===// 4411 // GVMaterializer implementation 4412 //===----------------------------------------------------------------------===// 4413 4414 Error BitcodeReader::materialize(GlobalValue *GV) { 4415 Function *F = dyn_cast<Function>(GV); 4416 // If it's not a function or is already material, ignore the request. 4417 if (!F || !F->isMaterializable()) 4418 return Error::success(); 4419 4420 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F); 4421 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!"); 4422 // If its position is recorded as 0, its body is somewhere in the stream 4423 // but we haven't seen it yet. 4424 if (DFII->second == 0) 4425 if (Error Err = findFunctionInStream(F, DFII)) 4426 return Err; 4427 4428 // Materialize metadata before parsing any function bodies. 4429 if (Error Err = materializeMetadata()) 4430 return Err; 4431 4432 // Move the bit stream to the saved position of the deferred function body. 4433 Stream.JumpToBit(DFII->second); 4434 4435 if (Error Err = parseFunctionBody(F)) 4436 return Err; 4437 F->setIsMaterializable(false); 4438 4439 if (StripDebugInfo) 4440 stripDebugInfo(*F); 4441 4442 // Upgrade any old intrinsic calls in the function. 4443 for (auto &I : UpgradedIntrinsics) { 4444 for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); 4445 UI != UE;) { 4446 User *U = *UI; 4447 ++UI; 4448 if (CallInst *CI = dyn_cast<CallInst>(U)) 4449 UpgradeIntrinsicCall(CI, I.second); 4450 } 4451 } 4452 4453 // Update calls to the remangled intrinsics 4454 for (auto &I : RemangledIntrinsics) 4455 for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); 4456 UI != UE;) 4457 // Don't expect any other users than call sites 4458 CallSite(*UI++).setCalledFunction(I.second); 4459 4460 // Finish fn->subprogram upgrade for materialized functions. 4461 if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) 4462 F->setSubprogram(SP); 4463 4464 // Check if the TBAA Metadata are valid, otherwise we will need to strip them. 4465 if (!MDLoader->isStrippingTBAA()) { 4466 for (auto &I : instructions(F)) { 4467 MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa); 4468 if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA)) 4469 continue; 4470 MDLoader->setStripTBAA(true); 4471 stripTBAA(F->getParent()); 4472 } 4473 } 4474 4475 // Bring in any functions that this function forward-referenced via 4476 // blockaddresses. 4477 return materializeForwardReferencedFunctions(); 4478 } 4479 4480 Error BitcodeReader::materializeModule() { 4481 if (Error Err = materializeMetadata()) 4482 return Err; 4483 4484 // Promise to materialize all forward references. 4485 WillMaterializeAllForwardRefs = true; 4486 4487 // Iterate over the module, deserializing any functions that are still on 4488 // disk. 4489 for (Function &F : *TheModule) { 4490 if (Error Err = materialize(&F)) 4491 return Err; 4492 } 4493 // At this point, if there are any function bodies, parse the rest of 4494 // the bits in the module past the last function block we have recorded 4495 // through either lazy scanning or the VST. 4496 if (LastFunctionBlockBit || NextUnreadBit) 4497 if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit 4498 ? LastFunctionBlockBit 4499 : NextUnreadBit)) 4500 return Err; 4501 4502 // Check that all block address forward references got resolved (as we 4503 // promised above). 4504 if (!BasicBlockFwdRefs.empty()) 4505 return error("Never resolved function from blockaddress"); 4506 4507 // Upgrade any intrinsic calls that slipped through (should not happen!) and 4508 // delete the old functions to clean up. We can't do this unless the entire 4509 // module is materialized because there could always be another function body 4510 // with calls to the old function. 4511 for (auto &I : UpgradedIntrinsics) { 4512 for (auto *U : I.first->users()) { 4513 if (CallInst *CI = dyn_cast<CallInst>(U)) 4514 UpgradeIntrinsicCall(CI, I.second); 4515 } 4516 if (!I.first->use_empty()) 4517 I.first->replaceAllUsesWith(I.second); 4518 I.first->eraseFromParent(); 4519 } 4520 UpgradedIntrinsics.clear(); 4521 // Do the same for remangled intrinsics 4522 for (auto &I : RemangledIntrinsics) { 4523 I.first->replaceAllUsesWith(I.second); 4524 I.first->eraseFromParent(); 4525 } 4526 RemangledIntrinsics.clear(); 4527 4528 UpgradeDebugInfo(*TheModule); 4529 4530 UpgradeModuleFlags(*TheModule); 4531 return Error::success(); 4532 } 4533 4534 std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { 4535 return IdentifiedStructTypes; 4536 } 4537 4538 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader( 4539 BitstreamCursor Cursor, ModuleSummaryIndex &TheIndex) 4540 : BitcodeReaderBase(std::move(Cursor)), TheIndex(TheIndex) {} 4541 4542 std::pair<GlobalValue::GUID, GlobalValue::GUID> 4543 ModuleSummaryIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) { 4544 auto VGI = ValueIdToCallGraphGUIDMap.find(ValueId); 4545 assert(VGI != ValueIdToCallGraphGUIDMap.end()); 4546 return VGI->second; 4547 } 4548 4549 // Specialized value symbol table parser used when reading module index 4550 // blocks where we don't actually create global values. The parsed information 4551 // is saved in the bitcode reader for use when later parsing summaries. 4552 Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable( 4553 uint64_t Offset, 4554 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) { 4555 assert(Offset > 0 && "Expected non-zero VST offset"); 4556 uint64_t CurrentBit = jumpToValueSymbolTable(Offset, Stream); 4557 4558 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) 4559 return error("Invalid record"); 4560 4561 SmallVector<uint64_t, 64> Record; 4562 4563 // Read all the records for this value table. 4564 SmallString<128> ValueName; 4565 4566 while (true) { 4567 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 4568 4569 switch (Entry.Kind) { 4570 case BitstreamEntry::SubBlock: // Handled for us already. 4571 case BitstreamEntry::Error: 4572 return error("Malformed block"); 4573 case BitstreamEntry::EndBlock: 4574 // Done parsing VST, jump back to wherever we came from. 4575 Stream.JumpToBit(CurrentBit); 4576 return Error::success(); 4577 case BitstreamEntry::Record: 4578 // The interesting case. 4579 break; 4580 } 4581 4582 // Read a record. 4583 Record.clear(); 4584 switch (Stream.readRecord(Entry.ID, Record)) { 4585 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records). 4586 break; 4587 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] 4588 if (convertToString(Record, 1, ValueName)) 4589 return error("Invalid record"); 4590 unsigned ValueID = Record[0]; 4591 assert(!SourceFileName.empty()); 4592 auto VLI = ValueIdToLinkageMap.find(ValueID); 4593 assert(VLI != ValueIdToLinkageMap.end() && 4594 "No linkage found for VST entry?"); 4595 auto Linkage = VLI->second; 4596 std::string GlobalId = 4597 GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName); 4598 auto ValueGUID = GlobalValue::getGUID(GlobalId); 4599 auto OriginalNameID = ValueGUID; 4600 if (GlobalValue::isLocalLinkage(Linkage)) 4601 OriginalNameID = GlobalValue::getGUID(ValueName); 4602 if (PrintSummaryGUIDs) 4603 dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is " 4604 << ValueName << "\n"; 4605 ValueIdToCallGraphGUIDMap[ValueID] = 4606 std::make_pair(ValueGUID, OriginalNameID); 4607 ValueName.clear(); 4608 break; 4609 } 4610 case bitc::VST_CODE_FNENTRY: { 4611 // VST_CODE_FNENTRY: [valueid, offset, namechar x N] 4612 if (convertToString(Record, 2, ValueName)) 4613 return error("Invalid record"); 4614 unsigned ValueID = Record[0]; 4615 assert(!SourceFileName.empty()); 4616 auto VLI = ValueIdToLinkageMap.find(ValueID); 4617 assert(VLI != ValueIdToLinkageMap.end() && 4618 "No linkage found for VST entry?"); 4619 auto Linkage = VLI->second; 4620 std::string FunctionGlobalId = GlobalValue::getGlobalIdentifier( 4621 ValueName, VLI->second, SourceFileName); 4622 auto FunctionGUID = GlobalValue::getGUID(FunctionGlobalId); 4623 auto OriginalNameID = FunctionGUID; 4624 if (GlobalValue::isLocalLinkage(Linkage)) 4625 OriginalNameID = GlobalValue::getGUID(ValueName); 4626 if (PrintSummaryGUIDs) 4627 dbgs() << "GUID " << FunctionGUID << "(" << OriginalNameID << ") is " 4628 << ValueName << "\n"; 4629 ValueIdToCallGraphGUIDMap[ValueID] = 4630 std::make_pair(FunctionGUID, OriginalNameID); 4631 4632 ValueName.clear(); 4633 break; 4634 } 4635 case bitc::VST_CODE_COMBINED_ENTRY: { 4636 // VST_CODE_COMBINED_ENTRY: [valueid, refguid] 4637 unsigned ValueID = Record[0]; 4638 GlobalValue::GUID RefGUID = Record[1]; 4639 // The "original name", which is the second value of the pair will be 4640 // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index. 4641 ValueIdToCallGraphGUIDMap[ValueID] = std::make_pair(RefGUID, RefGUID); 4642 break; 4643 } 4644 } 4645 } 4646 } 4647 4648 // Parse just the blocks needed for building the index out of the module. 4649 // At the end of this routine the module Index is populated with a map 4650 // from global value id to GlobalValueSummary objects. 4651 Error ModuleSummaryIndexBitcodeReader::parseModule(StringRef ModulePath) { 4652 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 4653 return error("Invalid record"); 4654 4655 SmallVector<uint64_t, 64> Record; 4656 DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap; 4657 unsigned ValueId = 0; 4658 4659 // Read the index for this module. 4660 while (true) { 4661 BitstreamEntry Entry = Stream.advance(); 4662 4663 switch (Entry.Kind) { 4664 case BitstreamEntry::Error: 4665 return error("Malformed block"); 4666 case BitstreamEntry::EndBlock: 4667 return Error::success(); 4668 4669 case BitstreamEntry::SubBlock: 4670 switch (Entry.ID) { 4671 default: // Skip unknown content. 4672 if (Stream.SkipBlock()) 4673 return error("Invalid record"); 4674 break; 4675 case bitc::BLOCKINFO_BLOCK_ID: 4676 // Need to parse these to get abbrev ids (e.g. for VST) 4677 if (readBlockInfo()) 4678 return error("Malformed block"); 4679 break; 4680 case bitc::VALUE_SYMTAB_BLOCK_ID: 4681 // Should have been parsed earlier via VSTOffset, unless there 4682 // is no summary section. 4683 assert(((SeenValueSymbolTable && VSTOffset > 0) || 4684 !SeenGlobalValSummary) && 4685 "Expected early VST parse via VSTOffset record"); 4686 if (Stream.SkipBlock()) 4687 return error("Invalid record"); 4688 break; 4689 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: 4690 assert(!SeenValueSymbolTable && 4691 "Already read VST when parsing summary block?"); 4692 // We might not have a VST if there were no values in the 4693 // summary. An empty summary block generated when we are 4694 // performing ThinLTO compiles so we don't later invoke 4695 // the regular LTO process on them. 4696 if (VSTOffset > 0) { 4697 if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap)) 4698 return Err; 4699 SeenValueSymbolTable = true; 4700 } 4701 SeenGlobalValSummary = true; 4702 if (Error Err = parseEntireSummary(ModulePath)) 4703 return Err; 4704 break; 4705 case bitc::MODULE_STRTAB_BLOCK_ID: 4706 if (Error Err = parseModuleStringTable()) 4707 return Err; 4708 break; 4709 } 4710 continue; 4711 4712 case BitstreamEntry::Record: { 4713 Record.clear(); 4714 auto BitCode = Stream.readRecord(Entry.ID, Record); 4715 switch (BitCode) { 4716 default: 4717 break; // Default behavior, ignore unknown content. 4718 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N] 4719 case bitc::MODULE_CODE_SOURCE_FILENAME: { 4720 SmallString<128> ValueName; 4721 if (convertToString(Record, 0, ValueName)) 4722 return error("Invalid record"); 4723 SourceFileName = ValueName.c_str(); 4724 break; 4725 } 4726 /// MODULE_CODE_HASH: [5*i32] 4727 case bitc::MODULE_CODE_HASH: { 4728 if (Record.size() != 5) 4729 return error("Invalid hash length " + Twine(Record.size()).str()); 4730 if (TheIndex.modulePaths().empty()) 4731 // We always seed the index with the module. 4732 TheIndex.addModulePath(ModulePath, 0); 4733 if (TheIndex.modulePaths().size() != 1) 4734 return error("Don't expect multiple modules defined?"); 4735 auto &Hash = TheIndex.modulePaths().begin()->second.second; 4736 int Pos = 0; 4737 for (auto &Val : Record) { 4738 assert(!(Val >> 32) && "Unexpected high bits set"); 4739 Hash[Pos++] = Val; 4740 } 4741 break; 4742 } 4743 /// MODULE_CODE_VSTOFFSET: [offset] 4744 case bitc::MODULE_CODE_VSTOFFSET: 4745 if (Record.size() < 1) 4746 return error("Invalid record"); 4747 // Note that we subtract 1 here because the offset is relative to one 4748 // word before the start of the identification or module block, which 4749 // was historically always the start of the regular bitcode header. 4750 VSTOffset = Record[0] - 1; 4751 break; 4752 // GLOBALVAR: [pointer type, isconst, initid, 4753 // linkage, alignment, section, visibility, threadlocal, 4754 // unnamed_addr, externally_initialized, dllstorageclass, 4755 // comdat] 4756 case bitc::MODULE_CODE_GLOBALVAR: { 4757 if (Record.size() < 6) 4758 return error("Invalid record"); 4759 uint64_t RawLinkage = Record[3]; 4760 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 4761 ValueIdToLinkageMap[ValueId++] = Linkage; 4762 break; 4763 } 4764 // FUNCTION: [type, callingconv, isproto, linkage, paramattr, 4765 // alignment, section, visibility, gc, unnamed_addr, 4766 // prologuedata, dllstorageclass, comdat, prefixdata] 4767 case bitc::MODULE_CODE_FUNCTION: { 4768 if (Record.size() < 8) 4769 return error("Invalid record"); 4770 uint64_t RawLinkage = Record[3]; 4771 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 4772 ValueIdToLinkageMap[ValueId++] = Linkage; 4773 break; 4774 } 4775 // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, 4776 // dllstorageclass] 4777 case bitc::MODULE_CODE_ALIAS: { 4778 if (Record.size() < 6) 4779 return error("Invalid record"); 4780 uint64_t RawLinkage = Record[3]; 4781 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); 4782 ValueIdToLinkageMap[ValueId++] = Linkage; 4783 break; 4784 } 4785 } 4786 } 4787 continue; 4788 } 4789 } 4790 } 4791 4792 std::vector<ValueInfo> 4793 ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) { 4794 std::vector<ValueInfo> Ret; 4795 Ret.reserve(Record.size()); 4796 for (uint64_t RefValueId : Record) 4797 Ret.push_back(getGUIDFromValueId(RefValueId).first); 4798 return Ret; 4799 } 4800 4801 std::vector<FunctionSummary::EdgeTy> ModuleSummaryIndexBitcodeReader::makeCallList( 4802 ArrayRef<uint64_t> Record, bool IsOldProfileFormat, bool HasProfile) { 4803 std::vector<FunctionSummary::EdgeTy> Ret; 4804 Ret.reserve(Record.size()); 4805 for (unsigned I = 0, E = Record.size(); I != E; ++I) { 4806 CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; 4807 GlobalValue::GUID CalleeGUID = getGUIDFromValueId(Record[I]).first; 4808 if (IsOldProfileFormat) { 4809 I += 1; // Skip old callsitecount field 4810 if (HasProfile) 4811 I += 1; // Skip old profilecount field 4812 } else if (HasProfile) 4813 Hotness = static_cast<CalleeInfo::HotnessType>(Record[++I]); 4814 Ret.push_back(FunctionSummary::EdgeTy{CalleeGUID, CalleeInfo{Hotness}}); 4815 } 4816 return Ret; 4817 } 4818 4819 // Eagerly parse the entire summary block. This populates the GlobalValueSummary 4820 // objects in the index. 4821 Error ModuleSummaryIndexBitcodeReader::parseEntireSummary( 4822 StringRef ModulePath) { 4823 if (Stream.EnterSubBlock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID)) 4824 return error("Invalid record"); 4825 SmallVector<uint64_t, 64> Record; 4826 4827 // Parse version 4828 { 4829 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 4830 if (Entry.Kind != BitstreamEntry::Record) 4831 return error("Invalid Summary Block: record for version expected"); 4832 if (Stream.readRecord(Entry.ID, Record) != bitc::FS_VERSION) 4833 return error("Invalid Summary Block: version expected"); 4834 } 4835 const uint64_t Version = Record[0]; 4836 const bool IsOldProfileFormat = Version == 1; 4837 if (Version < 1 || Version > 3) 4838 return error("Invalid summary version " + Twine(Version) + 4839 ", 1, 2 or 3 expected"); 4840 Record.clear(); 4841 4842 // Keep around the last seen summary to be used when we see an optional 4843 // "OriginalName" attachement. 4844 GlobalValueSummary *LastSeenSummary = nullptr; 4845 bool Combined = false; 4846 std::vector<GlobalValue::GUID> PendingTypeTests; 4847 4848 while (true) { 4849 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 4850 4851 switch (Entry.Kind) { 4852 case BitstreamEntry::SubBlock: // Handled for us already. 4853 case BitstreamEntry::Error: 4854 return error("Malformed block"); 4855 case BitstreamEntry::EndBlock: 4856 // For a per-module index, remove any entries that still have empty 4857 // summaries. The VST parsing creates entries eagerly for all symbols, 4858 // but not all have associated summaries (e.g. it doesn't know how to 4859 // distinguish between VST_CODE_ENTRY for function declarations vs global 4860 // variables with initializers that end up with a summary). Remove those 4861 // entries now so that we don't need to rely on the combined index merger 4862 // to clean them up (especially since that may not run for the first 4863 // module's index if we merge into that). 4864 if (!Combined) 4865 TheIndex.removeEmptySummaryEntries(); 4866 return Error::success(); 4867 case BitstreamEntry::Record: 4868 // The interesting case. 4869 break; 4870 } 4871 4872 // Read a record. The record format depends on whether this 4873 // is a per-module index or a combined index file. In the per-module 4874 // case the records contain the associated value's ID for correlation 4875 // with VST entries. In the combined index the correlation is done 4876 // via the bitcode offset of the summary records (which were saved 4877 // in the combined index VST entries). The records also contain 4878 // information used for ThinLTO renaming and importing. 4879 Record.clear(); 4880 auto BitCode = Stream.readRecord(Entry.ID, Record); 4881 switch (BitCode) { 4882 default: // Default behavior: ignore. 4883 break; 4884 // FS_PERMODULE: [valueid, flags, instcount, numrefs, numrefs x valueid, 4885 // n x (valueid)] 4886 // FS_PERMODULE_PROFILE: [valueid, flags, instcount, numrefs, 4887 // numrefs x valueid, 4888 // n x (valueid, hotness)] 4889 case bitc::FS_PERMODULE: 4890 case bitc::FS_PERMODULE_PROFILE: { 4891 unsigned ValueID = Record[0]; 4892 uint64_t RawFlags = Record[1]; 4893 unsigned InstCount = Record[2]; 4894 unsigned NumRefs = Record[3]; 4895 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 4896 // The module path string ref set in the summary must be owned by the 4897 // index's module string table. Since we don't have a module path 4898 // string table section in the per-module index, we create a single 4899 // module path string table entry with an empty (0) ID to take 4900 // ownership. 4901 static int RefListStartIndex = 4; 4902 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 4903 assert(Record.size() >= RefListStartIndex + NumRefs && 4904 "Record size inconsistent with number of references"); 4905 std::vector<ValueInfo> Refs = makeRefList( 4906 ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 4907 bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE); 4908 std::vector<FunctionSummary::EdgeTy> Calls = makeCallList( 4909 ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 4910 IsOldProfileFormat, HasProfile); 4911 auto FS = llvm::make_unique<FunctionSummary>( 4912 Flags, InstCount, std::move(Refs), std::move(Calls), 4913 std::move(PendingTypeTests)); 4914 PendingTypeTests.clear(); 4915 auto GUID = getGUIDFromValueId(ValueID); 4916 FS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first()); 4917 FS->setOriginalName(GUID.second); 4918 TheIndex.addGlobalValueSummary(GUID.first, std::move(FS)); 4919 break; 4920 } 4921 // FS_ALIAS: [valueid, flags, valueid] 4922 // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as 4923 // they expect all aliasee summaries to be available. 4924 case bitc::FS_ALIAS: { 4925 unsigned ValueID = Record[0]; 4926 uint64_t RawFlags = Record[1]; 4927 unsigned AliaseeID = Record[2]; 4928 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 4929 auto AS = 4930 llvm::make_unique<AliasSummary>(Flags, std::vector<ValueInfo>{}); 4931 // The module path string ref set in the summary must be owned by the 4932 // index's module string table. Since we don't have a module path 4933 // string table section in the per-module index, we create a single 4934 // module path string table entry with an empty (0) ID to take 4935 // ownership. 4936 AS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first()); 4937 4938 GlobalValue::GUID AliaseeGUID = getGUIDFromValueId(AliaseeID).first; 4939 auto *AliaseeSummary = TheIndex.getGlobalValueSummary(AliaseeGUID); 4940 if (!AliaseeSummary) 4941 return error("Alias expects aliasee summary to be parsed"); 4942 AS->setAliasee(AliaseeSummary); 4943 4944 auto GUID = getGUIDFromValueId(ValueID); 4945 AS->setOriginalName(GUID.second); 4946 TheIndex.addGlobalValueSummary(GUID.first, std::move(AS)); 4947 break; 4948 } 4949 // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, n x valueid] 4950 case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: { 4951 unsigned ValueID = Record[0]; 4952 uint64_t RawFlags = Record[1]; 4953 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 4954 std::vector<ValueInfo> Refs = 4955 makeRefList(ArrayRef<uint64_t>(Record).slice(2)); 4956 auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs)); 4957 FS->setModulePath(TheIndex.addModulePath(ModulePath, 0)->first()); 4958 auto GUID = getGUIDFromValueId(ValueID); 4959 FS->setOriginalName(GUID.second); 4960 TheIndex.addGlobalValueSummary(GUID.first, std::move(FS)); 4961 break; 4962 } 4963 // FS_COMBINED: [valueid, modid, flags, instcount, numrefs, 4964 // numrefs x valueid, n x (valueid)] 4965 // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, numrefs, 4966 // numrefs x valueid, n x (valueid, hotness)] 4967 case bitc::FS_COMBINED: 4968 case bitc::FS_COMBINED_PROFILE: { 4969 unsigned ValueID = Record[0]; 4970 uint64_t ModuleId = Record[1]; 4971 uint64_t RawFlags = Record[2]; 4972 unsigned InstCount = Record[3]; 4973 unsigned NumRefs = Record[4]; 4974 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 4975 static int RefListStartIndex = 5; 4976 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs; 4977 assert(Record.size() >= RefListStartIndex + NumRefs && 4978 "Record size inconsistent with number of references"); 4979 std::vector<ValueInfo> Refs = makeRefList( 4980 ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs)); 4981 bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE); 4982 std::vector<FunctionSummary::EdgeTy> Edges = makeCallList( 4983 ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex), 4984 IsOldProfileFormat, HasProfile); 4985 GlobalValue::GUID GUID = getGUIDFromValueId(ValueID).first; 4986 auto FS = llvm::make_unique<FunctionSummary>( 4987 Flags, InstCount, std::move(Refs), std::move(Edges), 4988 std::move(PendingTypeTests)); 4989 PendingTypeTests.clear(); 4990 LastSeenSummary = FS.get(); 4991 FS->setModulePath(ModuleIdMap[ModuleId]); 4992 TheIndex.addGlobalValueSummary(GUID, std::move(FS)); 4993 Combined = true; 4994 break; 4995 } 4996 // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid] 4997 // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as 4998 // they expect all aliasee summaries to be available. 4999 case bitc::FS_COMBINED_ALIAS: { 5000 unsigned ValueID = Record[0]; 5001 uint64_t ModuleId = Record[1]; 5002 uint64_t RawFlags = Record[2]; 5003 unsigned AliaseeValueId = Record[3]; 5004 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 5005 auto AS = llvm::make_unique<AliasSummary>(Flags, std::vector<ValueInfo>{}); 5006 LastSeenSummary = AS.get(); 5007 AS->setModulePath(ModuleIdMap[ModuleId]); 5008 5009 auto AliaseeGUID = getGUIDFromValueId(AliaseeValueId).first; 5010 auto AliaseeInModule = 5011 TheIndex.findSummaryInModule(AliaseeGUID, AS->modulePath()); 5012 if (!AliaseeInModule) 5013 return error("Alias expects aliasee summary to be parsed"); 5014 AS->setAliasee(AliaseeInModule); 5015 5016 GlobalValue::GUID GUID = getGUIDFromValueId(ValueID).first; 5017 TheIndex.addGlobalValueSummary(GUID, std::move(AS)); 5018 Combined = true; 5019 break; 5020 } 5021 // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid] 5022 case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: { 5023 unsigned ValueID = Record[0]; 5024 uint64_t ModuleId = Record[1]; 5025 uint64_t RawFlags = Record[2]; 5026 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); 5027 std::vector<ValueInfo> Refs = 5028 makeRefList(ArrayRef<uint64_t>(Record).slice(3)); 5029 auto FS = llvm::make_unique<GlobalVarSummary>(Flags, std::move(Refs)); 5030 LastSeenSummary = FS.get(); 5031 FS->setModulePath(ModuleIdMap[ModuleId]); 5032 GlobalValue::GUID GUID = getGUIDFromValueId(ValueID).first; 5033 TheIndex.addGlobalValueSummary(GUID, std::move(FS)); 5034 Combined = true; 5035 break; 5036 } 5037 // FS_COMBINED_ORIGINAL_NAME: [original_name] 5038 case bitc::FS_COMBINED_ORIGINAL_NAME: { 5039 uint64_t OriginalName = Record[0]; 5040 if (!LastSeenSummary) 5041 return error("Name attachment that does not follow a combined record"); 5042 LastSeenSummary->setOriginalName(OriginalName); 5043 // Reset the LastSeenSummary 5044 LastSeenSummary = nullptr; 5045 break; 5046 } 5047 case bitc::FS_TYPE_TESTS: { 5048 assert(PendingTypeTests.empty()); 5049 PendingTypeTests.insert(PendingTypeTests.end(), Record.begin(), 5050 Record.end()); 5051 break; 5052 } 5053 } 5054 } 5055 llvm_unreachable("Exit infinite loop"); 5056 } 5057 5058 // Parse the module string table block into the Index. 5059 // This populates the ModulePathStringTable map in the index. 5060 Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() { 5061 if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID)) 5062 return error("Invalid record"); 5063 5064 SmallVector<uint64_t, 64> Record; 5065 5066 SmallString<128> ModulePath; 5067 ModulePathStringTableTy::iterator LastSeenModulePath; 5068 5069 while (true) { 5070 BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 5071 5072 switch (Entry.Kind) { 5073 case BitstreamEntry::SubBlock: // Handled for us already. 5074 case BitstreamEntry::Error: 5075 return error("Malformed block"); 5076 case BitstreamEntry::EndBlock: 5077 return Error::success(); 5078 case BitstreamEntry::Record: 5079 // The interesting case. 5080 break; 5081 } 5082 5083 Record.clear(); 5084 switch (Stream.readRecord(Entry.ID, Record)) { 5085 default: // Default behavior: ignore. 5086 break; 5087 case bitc::MST_CODE_ENTRY: { 5088 // MST_ENTRY: [modid, namechar x N] 5089 uint64_t ModuleId = Record[0]; 5090 5091 if (convertToString(Record, 1, ModulePath)) 5092 return error("Invalid record"); 5093 5094 LastSeenModulePath = TheIndex.addModulePath(ModulePath, ModuleId); 5095 ModuleIdMap[ModuleId] = LastSeenModulePath->first(); 5096 5097 ModulePath.clear(); 5098 break; 5099 } 5100 /// MST_CODE_HASH: [5*i32] 5101 case bitc::MST_CODE_HASH: { 5102 if (Record.size() != 5) 5103 return error("Invalid hash length " + Twine(Record.size()).str()); 5104 if (LastSeenModulePath == TheIndex.modulePaths().end()) 5105 return error("Invalid hash that does not follow a module path"); 5106 int Pos = 0; 5107 for (auto &Val : Record) { 5108 assert(!(Val >> 32) && "Unexpected high bits set"); 5109 LastSeenModulePath->second.second[Pos++] = Val; 5110 } 5111 // Reset LastSeenModulePath to avoid overriding the hash unexpectedly. 5112 LastSeenModulePath = TheIndex.modulePaths().end(); 5113 break; 5114 } 5115 } 5116 } 5117 llvm_unreachable("Exit infinite loop"); 5118 } 5119 5120 namespace { 5121 5122 // FIXME: This class is only here to support the transition to llvm::Error. It 5123 // will be removed once this transition is complete. Clients should prefer to 5124 // deal with the Error value directly, rather than converting to error_code. 5125 class BitcodeErrorCategoryType : public std::error_category { 5126 const char *name() const noexcept override { 5127 return "llvm.bitcode"; 5128 } 5129 std::string message(int IE) const override { 5130 BitcodeError E = static_cast<BitcodeError>(IE); 5131 switch (E) { 5132 case BitcodeError::CorruptedBitcode: 5133 return "Corrupted bitcode"; 5134 } 5135 llvm_unreachable("Unknown error type!"); 5136 } 5137 }; 5138 5139 } // end anonymous namespace 5140 5141 static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory; 5142 5143 const std::error_category &llvm::BitcodeErrorCategory() { 5144 return *ErrorCategory; 5145 } 5146 5147 //===----------------------------------------------------------------------===// 5148 // External interface 5149 //===----------------------------------------------------------------------===// 5150 5151 Expected<std::vector<BitcodeModule>> 5152 llvm::getBitcodeModuleList(MemoryBufferRef Buffer) { 5153 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5154 if (!StreamOrErr) 5155 return StreamOrErr.takeError(); 5156 BitstreamCursor &Stream = *StreamOrErr; 5157 5158 std::vector<BitcodeModule> Modules; 5159 while (true) { 5160 uint64_t BCBegin = Stream.getCurrentByteNo(); 5161 5162 // We may be consuming bitcode from a client that leaves garbage at the end 5163 // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to 5164 // the end that there cannot possibly be another module, stop looking. 5165 if (BCBegin + 8 >= Stream.getBitcodeBytes().size()) 5166 return Modules; 5167 5168 BitstreamEntry Entry = Stream.advance(); 5169 switch (Entry.Kind) { 5170 case BitstreamEntry::EndBlock: 5171 case BitstreamEntry::Error: 5172 return error("Malformed block"); 5173 5174 case BitstreamEntry::SubBlock: { 5175 uint64_t IdentificationBit = -1ull; 5176 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { 5177 IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8; 5178 if (Stream.SkipBlock()) 5179 return error("Malformed block"); 5180 5181 Entry = Stream.advance(); 5182 if (Entry.Kind != BitstreamEntry::SubBlock || 5183 Entry.ID != bitc::MODULE_BLOCK_ID) 5184 return error("Malformed block"); 5185 } 5186 5187 if (Entry.ID == bitc::MODULE_BLOCK_ID) { 5188 uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8; 5189 if (Stream.SkipBlock()) 5190 return error("Malformed block"); 5191 5192 Modules.push_back({Stream.getBitcodeBytes().slice( 5193 BCBegin, Stream.getCurrentByteNo() - BCBegin), 5194 Buffer.getBufferIdentifier(), IdentificationBit, 5195 ModuleBit}); 5196 continue; 5197 } 5198 5199 if (Stream.SkipBlock()) 5200 return error("Malformed block"); 5201 continue; 5202 } 5203 case BitstreamEntry::Record: 5204 Stream.skipRecord(Entry.ID); 5205 continue; 5206 } 5207 } 5208 } 5209 5210 /// \brief Get a lazy one-at-time loading module from bitcode. 5211 /// 5212 /// This isn't always used in a lazy context. In particular, it's also used by 5213 /// \a parseModule(). If this is truly lazy, then we need to eagerly pull 5214 /// in forward-referenced functions from block address references. 5215 /// 5216 /// \param[in] MaterializeAll Set to \c true if we should materialize 5217 /// everything. 5218 Expected<std::unique_ptr<Module>> 5219 BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, 5220 bool ShouldLazyLoadMetadata, bool IsImporting) { 5221 BitstreamCursor Stream(Buffer); 5222 5223 std::string ProducerIdentification; 5224 if (IdentificationBit != -1ull) { 5225 Stream.JumpToBit(IdentificationBit); 5226 Expected<std::string> ProducerIdentificationOrErr = 5227 readIdentificationBlock(Stream); 5228 if (!ProducerIdentificationOrErr) 5229 return ProducerIdentificationOrErr.takeError(); 5230 5231 ProducerIdentification = *ProducerIdentificationOrErr; 5232 } 5233 5234 Stream.JumpToBit(ModuleBit); 5235 auto *R = 5236 new BitcodeReader(std::move(Stream), ProducerIdentification, Context); 5237 5238 std::unique_ptr<Module> M = 5239 llvm::make_unique<Module>(ModuleIdentifier, Context); 5240 M->setMaterializer(R); 5241 5242 // Delay parsing Metadata if ShouldLazyLoadMetadata is true. 5243 if (Error Err = 5244 R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, IsImporting)) 5245 return std::move(Err); 5246 5247 if (MaterializeAll) { 5248 // Read in the entire module, and destroy the BitcodeReader. 5249 if (Error Err = M->materializeAll()) 5250 return std::move(Err); 5251 } else { 5252 // Resolve forward references from blockaddresses. 5253 if (Error Err = R->materializeForwardReferencedFunctions()) 5254 return std::move(Err); 5255 } 5256 return std::move(M); 5257 } 5258 5259 Expected<std::unique_ptr<Module>> 5260 BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, 5261 bool IsImporting) { 5262 return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting); 5263 } 5264 5265 // Parse the specified bitcode buffer, returning the function info index. 5266 Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() { 5267 BitstreamCursor Stream(Buffer); 5268 Stream.JumpToBit(ModuleBit); 5269 5270 auto Index = llvm::make_unique<ModuleSummaryIndex>(); 5271 ModuleSummaryIndexBitcodeReader R(std::move(Stream), *Index); 5272 5273 if (Error Err = R.parseModule(ModuleIdentifier)) 5274 return std::move(Err); 5275 5276 return std::move(Index); 5277 } 5278 5279 // Check if the given bitcode buffer contains a global value summary block. 5280 Expected<bool> BitcodeModule::hasSummary() { 5281 BitstreamCursor Stream(Buffer); 5282 Stream.JumpToBit(ModuleBit); 5283 5284 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) 5285 return error("Invalid record"); 5286 5287 while (true) { 5288 BitstreamEntry Entry = Stream.advance(); 5289 5290 switch (Entry.Kind) { 5291 case BitstreamEntry::Error: 5292 return error("Malformed block"); 5293 case BitstreamEntry::EndBlock: 5294 return false; 5295 5296 case BitstreamEntry::SubBlock: 5297 if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) 5298 return true; 5299 5300 // Ignore other sub-blocks. 5301 if (Stream.SkipBlock()) 5302 return error("Malformed block"); 5303 continue; 5304 5305 case BitstreamEntry::Record: 5306 Stream.skipRecord(Entry.ID); 5307 continue; 5308 } 5309 } 5310 } 5311 5312 static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) { 5313 Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer); 5314 if (!MsOrErr) 5315 return MsOrErr.takeError(); 5316 5317 if (MsOrErr->size() != 1) 5318 return error("Expected a single module"); 5319 5320 return (*MsOrErr)[0]; 5321 } 5322 5323 Expected<std::unique_ptr<Module>> 5324 llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, 5325 bool ShouldLazyLoadMetadata, bool IsImporting) { 5326 Expected<BitcodeModule> BM = getSingleModule(Buffer); 5327 if (!BM) 5328 return BM.takeError(); 5329 5330 return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting); 5331 } 5332 5333 Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule( 5334 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context, 5335 bool ShouldLazyLoadMetadata, bool IsImporting) { 5336 auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata, 5337 IsImporting); 5338 if (MOrErr) 5339 (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer)); 5340 return MOrErr; 5341 } 5342 5343 Expected<std::unique_ptr<Module>> 5344 BitcodeModule::parseModule(LLVMContext &Context) { 5345 return getModuleImpl(Context, true, false, false); 5346 // TODO: Restore the use-lists to the in-memory state when the bitcode was 5347 // written. We must defer until the Module has been fully materialized. 5348 } 5349 5350 Expected<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer, 5351 LLVMContext &Context) { 5352 Expected<BitcodeModule> BM = getSingleModule(Buffer); 5353 if (!BM) 5354 return BM.takeError(); 5355 5356 return BM->parseModule(Context); 5357 } 5358 5359 Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) { 5360 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5361 if (!StreamOrErr) 5362 return StreamOrErr.takeError(); 5363 5364 return readTriple(*StreamOrErr); 5365 } 5366 5367 Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) { 5368 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5369 if (!StreamOrErr) 5370 return StreamOrErr.takeError(); 5371 5372 return hasObjCCategory(*StreamOrErr); 5373 } 5374 5375 Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) { 5376 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); 5377 if (!StreamOrErr) 5378 return StreamOrErr.takeError(); 5379 5380 return readIdentificationCode(*StreamOrErr); 5381 } 5382 5383 Expected<std::unique_ptr<ModuleSummaryIndex>> 5384 llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) { 5385 Expected<BitcodeModule> BM = getSingleModule(Buffer); 5386 if (!BM) 5387 return BM.takeError(); 5388 5389 return BM->getSummary(); 5390 } 5391 5392 Expected<bool> llvm::hasGlobalValueSummary(MemoryBufferRef Buffer) { 5393 Expected<BitcodeModule> BM = getSingleModule(Buffer); 5394 if (!BM) 5395 return BM.takeError(); 5396 5397 return BM->hasSummary(); 5398 } 5399