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