1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Bitcode/BitcodeReader.h"
11 #include "MetadataLoader.h"
12 #include "ValueList.h"
13 #include "llvm/ADT/APFloat.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Bitcode/BitstreamReader.h"
25 #include "llvm/Bitcode/LLVMBitCodes.h"
26 #include "llvm/Config/llvm-config.h"
27 #include "llvm/IR/Argument.h"
28 #include "llvm/IR/Attributes.h"
29 #include "llvm/IR/AutoUpgrade.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/CallSite.h"
32 #include "llvm/IR/CallingConv.h"
33 #include "llvm/IR/Comdat.h"
34 #include "llvm/IR/Constant.h"
35 #include "llvm/IR/Constants.h"
36 #include "llvm/IR/DataLayout.h"
37 #include "llvm/IR/DebugInfo.h"
38 #include "llvm/IR/DebugInfoMetadata.h"
39 #include "llvm/IR/DebugLoc.h"
40 #include "llvm/IR/DerivedTypes.h"
41 #include "llvm/IR/Function.h"
42 #include "llvm/IR/GVMaterializer.h"
43 #include "llvm/IR/GlobalAlias.h"
44 #include "llvm/IR/GlobalIFunc.h"
45 #include "llvm/IR/GlobalIndirectSymbol.h"
46 #include "llvm/IR/GlobalObject.h"
47 #include "llvm/IR/GlobalValue.h"
48 #include "llvm/IR/GlobalVariable.h"
49 #include "llvm/IR/InlineAsm.h"
50 #include "llvm/IR/InstIterator.h"
51 #include "llvm/IR/InstrTypes.h"
52 #include "llvm/IR/Instruction.h"
53 #include "llvm/IR/Instructions.h"
54 #include "llvm/IR/Intrinsics.h"
55 #include "llvm/IR/LLVMContext.h"
56 #include "llvm/IR/Metadata.h"
57 #include "llvm/IR/Module.h"
58 #include "llvm/IR/ModuleSummaryIndex.h"
59 #include "llvm/IR/Operator.h"
60 #include "llvm/IR/Type.h"
61 #include "llvm/IR/Value.h"
62 #include "llvm/IR/Verifier.h"
63 #include "llvm/Support/AtomicOrdering.h"
64 #include "llvm/Support/Casting.h"
65 #include "llvm/Support/CommandLine.h"
66 #include "llvm/Support/Compiler.h"
67 #include "llvm/Support/Debug.h"
68 #include "llvm/Support/Error.h"
69 #include "llvm/Support/ErrorHandling.h"
70 #include "llvm/Support/ErrorOr.h"
71 #include "llvm/Support/ManagedStatic.h"
72 #include "llvm/Support/MathExtras.h"
73 #include "llvm/Support/MemoryBuffer.h"
74 #include "llvm/Support/raw_ostream.h"
84 #include <system_error>
91 static cl::opt<bool> PrintSummaryGUIDs(
92 "print-summary-global-ids", cl::init(false), cl::Hidden,
94 "Print the global id for each value when reading the module summary"));
99 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
102 } // end anonymous namespace
104 static Error error(const Twine &Message) {
105 return make_error<StringError>(
106 Message, make_error_code(BitcodeError::CorruptedBitcode));
109 /// Helper to read the header common to all bitcode files.
110 static bool hasValidBitcodeHeader(BitstreamCursor &Stream) {
111 // Sniff for the signature.
112 if (!Stream.canSkipToPos(4) ||
113 Stream.Read(8) != 'B' ||
114 Stream.Read(8) != 'C' ||
115 Stream.Read(4) != 0x0 ||
116 Stream.Read(4) != 0xC ||
117 Stream.Read(4) != 0xE ||
118 Stream.Read(4) != 0xD)
123 static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) {
124 const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart();
125 const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize();
127 if (Buffer.getBufferSize() & 3)
128 return error("Invalid bitcode signature");
130 // If we have a wrapper header, parse it and ignore the non-bc file contents.
131 // The magic number is 0x0B17C0DE stored in little endian.
132 if (isBitcodeWrapper(BufPtr, BufEnd))
133 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
134 return error("Invalid bitcode wrapper header");
136 BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd));
137 if (!hasValidBitcodeHeader(Stream))
138 return error("Invalid bitcode signature");
140 return std::move(Stream);
143 /// Convert a string from a record into an std::string, return true on failure.
144 template <typename StrTy>
145 static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx,
147 if (Idx > Record.size())
150 for (unsigned i = Idx, e = Record.size(); i != e; ++i)
151 Result += (char)Record[i];
155 // Strip all the TBAA attachment for the module.
156 static void stripTBAA(Module *M) {
158 if (F.isMaterializable())
160 for (auto &I : instructions(F))
161 I.setMetadata(LLVMContext::MD_tbaa, nullptr);
165 /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the
166 /// "epoch" encoded in the bitcode, and return the producer name if any.
167 static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) {
168 if (Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID))
169 return error("Invalid record");
171 // Read all the records.
172 SmallVector<uint64_t, 64> Record;
174 std::string ProducerIdentification;
177 BitstreamEntry Entry = Stream.advance();
179 switch (Entry.Kind) {
181 case BitstreamEntry::Error:
182 return error("Malformed block");
183 case BitstreamEntry::EndBlock:
184 return ProducerIdentification;
185 case BitstreamEntry::Record:
186 // The interesting case.
192 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
194 default: // Default behavior: reject
195 return error("Invalid value");
196 case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N]
197 convertToString(Record, 0, ProducerIdentification);
199 case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#]
200 unsigned epoch = (unsigned)Record[0];
201 if (epoch != bitc::BITCODE_CURRENT_EPOCH) {
203 Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +
204 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");
211 static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) {
212 // We expect a number of well-defined blocks, though we don't necessarily
213 // need to understand them all.
215 if (Stream.AtEndOfStream())
218 BitstreamEntry Entry = Stream.advance();
219 switch (Entry.Kind) {
220 case BitstreamEntry::EndBlock:
221 case BitstreamEntry::Error:
222 return error("Malformed block");
224 case BitstreamEntry::SubBlock:
225 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID)
226 return readIdentificationBlock(Stream);
228 // Ignore other sub-blocks.
229 if (Stream.SkipBlock())
230 return error("Malformed block");
232 case BitstreamEntry::Record:
233 Stream.skipRecord(Entry.ID);
239 static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) {
240 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
241 return error("Invalid record");
243 SmallVector<uint64_t, 64> Record;
244 // Read all the records for this module.
247 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
249 switch (Entry.Kind) {
250 case BitstreamEntry::SubBlock: // Handled for us already.
251 case BitstreamEntry::Error:
252 return error("Malformed block");
253 case BitstreamEntry::EndBlock:
255 case BitstreamEntry::Record:
256 // The interesting case.
261 switch (Stream.readRecord(Entry.ID, Record)) {
263 break; // Default behavior, ignore unknown content.
264 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
266 if (convertToString(Record, 0, S))
267 return error("Invalid record");
268 // Check for the i386 and other (x86_64, ARM) conventions
269 if (S.find("__DATA,__objc_catlist") != std::string::npos ||
270 S.find("__OBJC,__category") != std::string::npos)
277 llvm_unreachable("Exit infinite loop");
280 static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) {
281 // We expect a number of well-defined blocks, though we don't necessarily
282 // need to understand them all.
284 BitstreamEntry Entry = Stream.advance();
286 switch (Entry.Kind) {
287 case BitstreamEntry::Error:
288 return error("Malformed block");
289 case BitstreamEntry::EndBlock:
292 case BitstreamEntry::SubBlock:
293 if (Entry.ID == bitc::MODULE_BLOCK_ID)
294 return hasObjCCategoryInModule(Stream);
296 // Ignore other sub-blocks.
297 if (Stream.SkipBlock())
298 return error("Malformed block");
301 case BitstreamEntry::Record:
302 Stream.skipRecord(Entry.ID);
308 static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) {
309 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
310 return error("Invalid record");
312 SmallVector<uint64_t, 64> Record;
316 // Read all the records for this module.
318 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
320 switch (Entry.Kind) {
321 case BitstreamEntry::SubBlock: // Handled for us already.
322 case BitstreamEntry::Error:
323 return error("Malformed block");
324 case BitstreamEntry::EndBlock:
326 case BitstreamEntry::Record:
327 // The interesting case.
332 switch (Stream.readRecord(Entry.ID, Record)) {
333 default: break; // Default behavior, ignore unknown content.
334 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
336 if (convertToString(Record, 0, S))
337 return error("Invalid record");
344 llvm_unreachable("Exit infinite loop");
347 static Expected<std::string> readTriple(BitstreamCursor &Stream) {
348 // We expect a number of well-defined blocks, though we don't necessarily
349 // need to understand them all.
351 BitstreamEntry Entry = Stream.advance();
353 switch (Entry.Kind) {
354 case BitstreamEntry::Error:
355 return error("Malformed block");
356 case BitstreamEntry::EndBlock:
359 case BitstreamEntry::SubBlock:
360 if (Entry.ID == bitc::MODULE_BLOCK_ID)
361 return readModuleTriple(Stream);
363 // Ignore other sub-blocks.
364 if (Stream.SkipBlock())
365 return error("Malformed block");
368 case BitstreamEntry::Record:
369 Stream.skipRecord(Entry.ID);
377 class BitcodeReaderBase {
379 BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab)
380 : Stream(std::move(Stream)), Strtab(Strtab) {
381 this->Stream.setBlockInfo(&BlockInfo);
384 BitstreamBlockInfo BlockInfo;
385 BitstreamCursor Stream;
388 /// In version 2 of the bitcode we store names of global values and comdats in
389 /// a string table rather than in the VST.
390 bool UseStrtab = false;
392 Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record);
394 /// If this module uses a string table, pop the reference to the string table
395 /// and return the referenced string and the rest of the record. Otherwise
396 /// just return the record itself.
397 std::pair<StringRef, ArrayRef<uint64_t>>
398 readNameFromStrtab(ArrayRef<uint64_t> Record);
400 bool readBlockInfo();
402 // Contains an arbitrary and optional string identifying the bitcode producer
403 std::string ProducerIdentification;
405 Error error(const Twine &Message);
408 } // end anonymous namespace
410 Error BitcodeReaderBase::error(const Twine &Message) {
411 std::string FullMsg = Message.str();
412 if (!ProducerIdentification.empty())
413 FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " +
414 LLVM_VERSION_STRING "')";
415 return ::error(FullMsg);
419 BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) {
421 return error("Invalid record");
422 unsigned ModuleVersion = Record[0];
423 if (ModuleVersion > 2)
424 return error("Invalid value");
425 UseStrtab = ModuleVersion >= 2;
426 return ModuleVersion;
429 std::pair<StringRef, ArrayRef<uint64_t>>
430 BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) {
433 // Invalid reference. Let the caller complain about the record being empty.
434 if (Record[0] + Record[1] > Strtab.size())
436 return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)};
441 class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
442 LLVMContext &Context;
443 Module *TheModule = nullptr;
444 // Next offset to start scanning for lazy parsing of function bodies.
445 uint64_t NextUnreadBit = 0;
446 // Last function offset found in the VST.
447 uint64_t LastFunctionBlockBit = 0;
448 bool SeenValueSymbolTable = false;
449 uint64_t VSTOffset = 0;
451 std::vector<std::string> SectionTable;
452 std::vector<std::string> GCTable;
454 std::vector<Type*> TypeList;
455 BitcodeReaderValueList ValueList;
456 Optional<MetadataLoader> MDLoader;
457 std::vector<Comdat *> ComdatList;
458 SmallVector<Instruction *, 64> InstructionList;
460 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits;
461 std::vector<std::pair<GlobalIndirectSymbol *, unsigned>> IndirectSymbolInits;
462 std::vector<std::pair<Function *, unsigned>> FunctionPrefixes;
463 std::vector<std::pair<Function *, unsigned>> FunctionPrologues;
464 std::vector<std::pair<Function *, unsigned>> FunctionPersonalityFns;
466 /// The set of attributes by index. Index zero in the file is for null, and
467 /// is thus not represented here. As such all indices are off by one.
468 std::vector<AttributeList> MAttributes;
470 /// The set of attribute groups.
471 std::map<unsigned, AttributeList> MAttributeGroups;
473 /// While parsing a function body, this is a list of the basic blocks for the
475 std::vector<BasicBlock*> FunctionBBs;
477 // When reading the module header, this list is populated with functions that
478 // have bodies later in the file.
479 std::vector<Function*> FunctionsWithBodies;
481 // When intrinsic functions are encountered which require upgrading they are
482 // stored here with their replacement function.
483 using UpdatedIntrinsicMap = DenseMap<Function *, Function *>;
484 UpdatedIntrinsicMap UpgradedIntrinsics;
485 // Intrinsics which were remangled because of types rename
486 UpdatedIntrinsicMap RemangledIntrinsics;
488 // Several operations happen after the module header has been read, but
489 // before function bodies are processed. This keeps track of whether
490 // we've done this yet.
491 bool SeenFirstFunctionBody = false;
493 /// When function bodies are initially scanned, this map contains info about
494 /// where to find deferred function body in the stream.
495 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
497 /// When Metadata block is initially scanned when parsing the module, we may
498 /// choose to defer parsing of the metadata. This vector contains info about
499 /// which Metadata blocks are deferred.
500 std::vector<uint64_t> DeferredMetadataInfo;
502 /// These are basic blocks forward-referenced by block addresses. They are
503 /// inserted lazily into functions when they're loaded. The basic block ID is
504 /// its index into the vector.
505 DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
506 std::deque<Function *> BasicBlockFwdRefQueue;
508 /// Indicates that we are using a new encoding for instruction operands where
509 /// most operands in the current FUNCTION_BLOCK are encoded relative to the
510 /// instruction number, for a more compact encoding. Some instruction
511 /// operands are not relative to the instruction ID: basic block numbers, and
512 /// types. Once the old style function blocks have been phased out, we would
513 /// not need this flag.
514 bool UseRelativeIDs = false;
516 /// True if all functions will be materialized, negating the need to process
517 /// (e.g.) blockaddress forward references.
518 bool WillMaterializeAllForwardRefs = false;
520 bool StripDebugInfo = false;
521 TBAAVerifier TBAAVerifyHelper;
523 std::vector<std::string> BundleTags;
524 SmallVector<SyncScope::ID, 8> SSIDs;
527 BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
528 StringRef ProducerIdentification, LLVMContext &Context);
530 Error materializeForwardReferencedFunctions();
532 Error materialize(GlobalValue *GV) override;
533 Error materializeModule() override;
534 std::vector<StructType *> getIdentifiedStructTypes() const override;
536 /// Main interface to parsing a bitcode buffer.
537 /// \returns true if an error occurred.
538 Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata = false,
539 bool IsImporting = false);
541 static uint64_t decodeSignRotatedValue(uint64_t V);
543 /// Materialize any deferred Metadata block.
544 Error materializeMetadata() override;
546 void setStripDebugInfo() override;
549 std::vector<StructType *> IdentifiedStructTypes;
550 StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
551 StructType *createIdentifiedStructType(LLVMContext &Context);
553 Type *getTypeByID(unsigned ID);
555 Value *getFnValueByID(unsigned ID, Type *Ty) {
556 if (Ty && Ty->isMetadataTy())
557 return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
558 return ValueList.getValueFwdRef(ID, Ty);
561 Metadata *getFnMetadataByID(unsigned ID) {
562 return MDLoader->getMetadataFwdRefOrLoad(ID);
565 BasicBlock *getBasicBlock(unsigned ID) const {
566 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
567 return FunctionBBs[ID];
570 AttributeList getAttributes(unsigned i) const {
571 if (i-1 < MAttributes.size())
572 return MAttributes[i-1];
573 return AttributeList();
576 /// Read a value/type pair out of the specified record from slot 'Slot'.
577 /// Increment Slot past the number of slots used in the record. Return true on
579 bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
580 unsigned InstNum, Value *&ResVal) {
581 if (Slot == Record.size()) return true;
582 unsigned ValNo = (unsigned)Record[Slot++];
583 // Adjust the ValNo, if it was encoded relative to the InstNum.
585 ValNo = InstNum - ValNo;
586 if (ValNo < InstNum) {
587 // If this is not a forward reference, just return the value we already
589 ResVal = getFnValueByID(ValNo, nullptr);
590 return ResVal == nullptr;
592 if (Slot == Record.size())
595 unsigned TypeNo = (unsigned)Record[Slot++];
596 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
597 return ResVal == nullptr;
600 /// Read a value out of the specified record from slot 'Slot'. Increment Slot
601 /// past the number of slots used by the value in the record. Return true if
602 /// there is an error.
603 bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
604 unsigned InstNum, Type *Ty, Value *&ResVal) {
605 if (getValue(Record, Slot, InstNum, Ty, ResVal))
607 // All values currently take a single record slot.
612 /// Like popValue, but does not increment the Slot number.
613 bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
614 unsigned InstNum, Type *Ty, Value *&ResVal) {
615 ResVal = getValue(Record, Slot, InstNum, Ty);
616 return ResVal == nullptr;
619 /// Version of getValue that returns ResVal directly, or 0 if there is an
621 Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
622 unsigned InstNum, Type *Ty) {
623 if (Slot == Record.size()) return nullptr;
624 unsigned ValNo = (unsigned)Record[Slot];
625 // Adjust the ValNo, if it was encoded relative to the InstNum.
627 ValNo = InstNum - ValNo;
628 return getFnValueByID(ValNo, Ty);
631 /// Like getValue, but decodes signed VBRs.
632 Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
633 unsigned InstNum, Type *Ty) {
634 if (Slot == Record.size()) return nullptr;
635 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
636 // Adjust the ValNo, if it was encoded relative to the InstNum.
638 ValNo = InstNum - ValNo;
639 return getFnValueByID(ValNo, Ty);
642 /// Converts alignment exponent (i.e. power of two (or zero)) to the
643 /// corresponding alignment to use. If alignment is too large, returns
644 /// a corresponding error code.
645 Error parseAlignmentValue(uint64_t Exponent, unsigned &Alignment);
646 Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
647 Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false);
649 Error parseComdatRecord(ArrayRef<uint64_t> Record);
650 Error parseGlobalVarRecord(ArrayRef<uint64_t> Record);
651 Error parseFunctionRecord(ArrayRef<uint64_t> Record);
652 Error parseGlobalIndirectSymbolRecord(unsigned BitCode,
653 ArrayRef<uint64_t> Record);
655 Error parseAttributeBlock();
656 Error parseAttributeGroupBlock();
657 Error parseTypeTable();
658 Error parseTypeTableBody();
659 Error parseOperandBundleTags();
660 Error parseSyncScopeNames();
662 Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record,
663 unsigned NameIndex, Triple &TT);
664 void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F,
665 ArrayRef<uint64_t> Record);
666 Error parseValueSymbolTable(uint64_t Offset = 0);
667 Error parseGlobalValueSymbolTable();
668 Error parseConstants();
669 Error rememberAndSkipFunctionBodies();
670 Error rememberAndSkipFunctionBody();
671 /// Save the positions of the Metadata blocks and skip parsing the blocks.
672 Error rememberAndSkipMetadata();
673 Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType);
674 Error parseFunctionBody(Function *F);
675 Error globalCleanup();
676 Error resolveGlobalAndIndirectSymbolInits();
677 Error parseUseLists();
678 Error findFunctionInStream(
680 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
682 SyncScope::ID getDecodedSyncScopeID(unsigned Val);
685 /// Class to manage reading and parsing function summary index bitcode
687 class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
688 /// The module index built during parsing.
689 ModuleSummaryIndex &TheIndex;
691 /// Indicates whether we have encountered a global value summary section
692 /// yet during parsing.
693 bool SeenGlobalValSummary = false;
695 /// Indicates whether we have already parsed the VST, used for error checking.
696 bool SeenValueSymbolTable = false;
698 /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.
699 /// Used to enable on-demand parsing of the VST.
700 uint64_t VSTOffset = 0;
702 // Map to save ValueId to ValueInfo association that was recorded in the
703 // ValueSymbolTable. It is used after the VST is parsed to convert
704 // call graph edges read from the function summary from referencing
705 // callees by their ValueId to using the ValueInfo instead, which is how
706 // they are recorded in the summary index being built.
707 // We save a GUID which refers to the same global as the ValueInfo, but
708 // ignoring the linkage, i.e. for values other than local linkage they are
710 DenseMap<unsigned, std::pair<ValueInfo, GlobalValue::GUID>>
711 ValueIdToValueInfoMap;
713 /// Map populated during module path string table parsing, from the
714 /// module ID to a string reference owned by the index's module
715 /// path string table, used to correlate with combined index
717 DenseMap<uint64_t, StringRef> ModuleIdMap;
719 /// Original source file name recorded in a bitcode record.
720 std::string SourceFileName;
722 /// The string identifier given to this module by the client, normally the
723 /// path to the bitcode file.
724 StringRef ModulePath;
726 /// For per-module summary indexes, the unique numerical identifier given to
727 /// this module by the client.
731 ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab,
732 ModuleSummaryIndex &TheIndex,
733 StringRef ModulePath, unsigned ModuleId);
738 void setValueGUID(uint64_t ValueID, StringRef ValueName,
739 GlobalValue::LinkageTypes Linkage,
740 StringRef SourceFileName);
741 Error parseValueSymbolTable(
743 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap);
744 std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record);
745 std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record,
746 bool IsOldProfileFormat,
749 Error parseEntireSummary(unsigned ID);
750 Error parseModuleStringTable();
752 std::pair<ValueInfo, GlobalValue::GUID>
753 getValueInfoFromValueId(unsigned ValueId);
755 void addThisModule();
756 ModuleSummaryIndex::ModuleInfo *getThisModule();
759 } // end anonymous namespace
761 std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx,
765 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
766 EC = EIB.convertToErrorCode();
767 Ctx.emitError(EIB.message());
771 return std::error_code();
774 BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab,
775 StringRef ProducerIdentification,
776 LLVMContext &Context)
777 : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context),
779 this->ProducerIdentification = ProducerIdentification;
782 Error BitcodeReader::materializeForwardReferencedFunctions() {
783 if (WillMaterializeAllForwardRefs)
784 return Error::success();
786 // Prevent recursion.
787 WillMaterializeAllForwardRefs = true;
789 while (!BasicBlockFwdRefQueue.empty()) {
790 Function *F = BasicBlockFwdRefQueue.front();
791 BasicBlockFwdRefQueue.pop_front();
792 assert(F && "Expected valid function");
793 if (!BasicBlockFwdRefs.count(F))
794 // Already materialized.
797 // Check for a function that isn't materializable to prevent an infinite
798 // loop. When parsing a blockaddress stored in a global variable, there
799 // isn't a trivial way to check if a function will have a body without a
800 // linear search through FunctionsWithBodies, so just check it here.
801 if (!F->isMaterializable())
802 return error("Never resolved function from blockaddress");
804 // Try to materialize F.
805 if (Error Err = materialize(F))
808 assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
811 WillMaterializeAllForwardRefs = false;
812 return Error::success();
815 //===----------------------------------------------------------------------===//
816 // Helper functions to implement forward reference resolution, etc.
817 //===----------------------------------------------------------------------===//
819 static bool hasImplicitComdat(size_t Val) {
823 case 1: // Old WeakAnyLinkage
824 case 4: // Old LinkOnceAnyLinkage
825 case 10: // Old WeakODRLinkage
826 case 11: // Old LinkOnceODRLinkage
831 static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
833 default: // Map unknown/new linkages to external
835 return GlobalValue::ExternalLinkage;
837 return GlobalValue::AppendingLinkage;
839 return GlobalValue::InternalLinkage;
841 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
843 return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
845 return GlobalValue::ExternalWeakLinkage;
847 return GlobalValue::CommonLinkage;
849 return GlobalValue::PrivateLinkage;
851 return GlobalValue::AvailableExternallyLinkage;
853 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
855 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
857 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
858 case 1: // Old value with implicit comdat.
860 return GlobalValue::WeakAnyLinkage;
861 case 10: // Old value with implicit comdat.
863 return GlobalValue::WeakODRLinkage;
864 case 4: // Old value with implicit comdat.
866 return GlobalValue::LinkOnceAnyLinkage;
867 case 11: // Old value with implicit comdat.
869 return GlobalValue::LinkOnceODRLinkage;
873 static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) {
874 FunctionSummary::FFlags Flags;
875 Flags.ReadNone = RawFlags & 0x1;
876 Flags.ReadOnly = (RawFlags >> 1) & 0x1;
877 Flags.NoRecurse = (RawFlags >> 2) & 0x1;
878 Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1;
879 Flags.NoInline = (RawFlags >> 4) & 0x1;
883 /// Decode the flags for GlobalValue in the summary.
884 static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags,
886 // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage
887 // like getDecodedLinkage() above. Any future change to the linkage enum and
888 // to getDecodedLinkage() will need to be taken into account here as above.
889 auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits
890 RawFlags = RawFlags >> 4;
891 bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3;
892 // The Live flag wasn't introduced until version 3. For dead stripping
893 // to work correctly on earlier versions, we must conservatively treat all
895 bool Live = (RawFlags & 0x2) || Version < 3;
896 bool Local = (RawFlags & 0x4);
898 return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, Live, Local);
901 // Decode the flags for GlobalVariable in the summary
902 static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) {
903 return GlobalVarSummary::GVarFlags((RawFlags & 0x1) ? true : false);
906 static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {
908 default: // Map unknown visibilities to default.
909 case 0: return GlobalValue::DefaultVisibility;
910 case 1: return GlobalValue::HiddenVisibility;
911 case 2: return GlobalValue::ProtectedVisibility;
915 static GlobalValue::DLLStorageClassTypes
916 getDecodedDLLStorageClass(unsigned Val) {
918 default: // Map unknown values to default.
919 case 0: return GlobalValue::DefaultStorageClass;
920 case 1: return GlobalValue::DLLImportStorageClass;
921 case 2: return GlobalValue::DLLExportStorageClass;
925 static bool getDecodedDSOLocal(unsigned Val) {
927 default: // Map unknown values to preemptable.
928 case 0: return false;
933 static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {
935 case 0: return GlobalVariable::NotThreadLocal;
936 default: // Map unknown non-zero value to general dynamic.
937 case 1: return GlobalVariable::GeneralDynamicTLSModel;
938 case 2: return GlobalVariable::LocalDynamicTLSModel;
939 case 3: return GlobalVariable::InitialExecTLSModel;
940 case 4: return GlobalVariable::LocalExecTLSModel;
944 static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) {
946 default: // Map unknown to UnnamedAddr::None.
947 case 0: return GlobalVariable::UnnamedAddr::None;
948 case 1: return GlobalVariable::UnnamedAddr::Global;
949 case 2: return GlobalVariable::UnnamedAddr::Local;
953 static int getDecodedCastOpcode(unsigned Val) {
956 case bitc::CAST_TRUNC : return Instruction::Trunc;
957 case bitc::CAST_ZEXT : return Instruction::ZExt;
958 case bitc::CAST_SEXT : return Instruction::SExt;
959 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
960 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
961 case bitc::CAST_UITOFP : return Instruction::UIToFP;
962 case bitc::CAST_SITOFP : return Instruction::SIToFP;
963 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
964 case bitc::CAST_FPEXT : return Instruction::FPExt;
965 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
966 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
967 case bitc::CAST_BITCAST : return Instruction::BitCast;
968 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
972 static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) {
973 bool IsFP = Ty->isFPOrFPVectorTy();
974 // UnOps are only valid for int/fp or vector of int/fp types
975 if (!IsFP && !Ty->isIntOrIntVectorTy())
982 return IsFP ? Instruction::FNeg : -1;
986 static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {
987 bool IsFP = Ty->isFPOrFPVectorTy();
988 // BinOps are only valid for int/fp or vector of int/fp types
989 if (!IsFP && !Ty->isIntOrIntVectorTy())
995 case bitc::BINOP_ADD:
996 return IsFP ? Instruction::FAdd : Instruction::Add;
997 case bitc::BINOP_SUB:
998 return IsFP ? Instruction::FSub : Instruction::Sub;
999 case bitc::BINOP_MUL:
1000 return IsFP ? Instruction::FMul : Instruction::Mul;
1001 case bitc::BINOP_UDIV:
1002 return IsFP ? -1 : Instruction::UDiv;
1003 case bitc::BINOP_SDIV:
1004 return IsFP ? Instruction::FDiv : Instruction::SDiv;
1005 case bitc::BINOP_UREM:
1006 return IsFP ? -1 : Instruction::URem;
1007 case bitc::BINOP_SREM:
1008 return IsFP ? Instruction::FRem : Instruction::SRem;
1009 case bitc::BINOP_SHL:
1010 return IsFP ? -1 : Instruction::Shl;
1011 case bitc::BINOP_LSHR:
1012 return IsFP ? -1 : Instruction::LShr;
1013 case bitc::BINOP_ASHR:
1014 return IsFP ? -1 : Instruction::AShr;
1015 case bitc::BINOP_AND:
1016 return IsFP ? -1 : Instruction::And;
1017 case bitc::BINOP_OR:
1018 return IsFP ? -1 : Instruction::Or;
1019 case bitc::BINOP_XOR:
1020 return IsFP ? -1 : Instruction::Xor;
1024 static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) {
1026 default: return AtomicRMWInst::BAD_BINOP;
1027 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
1028 case bitc::RMW_ADD: return AtomicRMWInst::Add;
1029 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
1030 case bitc::RMW_AND: return AtomicRMWInst::And;
1031 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
1032 case bitc::RMW_OR: return AtomicRMWInst::Or;
1033 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
1034 case bitc::RMW_MAX: return AtomicRMWInst::Max;
1035 case bitc::RMW_MIN: return AtomicRMWInst::Min;
1036 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
1037 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
1041 static AtomicOrdering getDecodedOrdering(unsigned Val) {
1043 case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic;
1044 case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered;
1045 case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic;
1046 case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire;
1047 case bitc::ORDERING_RELEASE: return AtomicOrdering::Release;
1048 case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease;
1049 default: // Map unknown orderings to sequentially-consistent.
1050 case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent;
1054 static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
1056 default: // Map unknown selection kinds to any.
1057 case bitc::COMDAT_SELECTION_KIND_ANY:
1059 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
1060 return Comdat::ExactMatch;
1061 case bitc::COMDAT_SELECTION_KIND_LARGEST:
1062 return Comdat::Largest;
1063 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
1064 return Comdat::NoDuplicates;
1065 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
1066 return Comdat::SameSize;
1070 static FastMathFlags getDecodedFastMathFlags(unsigned Val) {
1072 if (0 != (Val & bitc::UnsafeAlgebra))
1074 if (0 != (Val & bitc::AllowReassoc))
1075 FMF.setAllowReassoc();
1076 if (0 != (Val & bitc::NoNaNs))
1078 if (0 != (Val & bitc::NoInfs))
1080 if (0 != (Val & bitc::NoSignedZeros))
1081 FMF.setNoSignedZeros();
1082 if (0 != (Val & bitc::AllowReciprocal))
1083 FMF.setAllowReciprocal();
1084 if (0 != (Val & bitc::AllowContract))
1085 FMF.setAllowContract(true);
1086 if (0 != (Val & bitc::ApproxFunc))
1087 FMF.setApproxFunc();
1091 static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) {
1093 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
1094 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
1098 Type *BitcodeReader::getTypeByID(unsigned ID) {
1099 // The type table size is always specified correctly.
1100 if (ID >= TypeList.size())
1103 if (Type *Ty = TypeList[ID])
1106 // If we have a forward reference, the only possible case is when it is to a
1107 // named struct. Just create a placeholder for now.
1108 return TypeList[ID] = createIdentifiedStructType(Context);
1111 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
1113 auto *Ret = StructType::create(Context, Name);
1114 IdentifiedStructTypes.push_back(Ret);
1118 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
1119 auto *Ret = StructType::create(Context);
1120 IdentifiedStructTypes.push_back(Ret);
1124 //===----------------------------------------------------------------------===//
1125 // Functions for parsing blocks from the bitcode file
1126 //===----------------------------------------------------------------------===//
1128 static uint64_t getRawAttributeMask(Attribute::AttrKind Val) {
1130 case Attribute::EndAttrKinds:
1131 llvm_unreachable("Synthetic enumerators which should never get here");
1133 case Attribute::None: return 0;
1134 case Attribute::ZExt: return 1 << 0;
1135 case Attribute::SExt: return 1 << 1;
1136 case Attribute::NoReturn: return 1 << 2;
1137 case Attribute::InReg: return 1 << 3;
1138 case Attribute::StructRet: return 1 << 4;
1139 case Attribute::NoUnwind: return 1 << 5;
1140 case Attribute::NoAlias: return 1 << 6;
1141 case Attribute::ByVal: return 1 << 7;
1142 case Attribute::Nest: return 1 << 8;
1143 case Attribute::ReadNone: return 1 << 9;
1144 case Attribute::ReadOnly: return 1 << 10;
1145 case Attribute::NoInline: return 1 << 11;
1146 case Attribute::AlwaysInline: return 1 << 12;
1147 case Attribute::OptimizeForSize: return 1 << 13;
1148 case Attribute::StackProtect: return 1 << 14;
1149 case Attribute::StackProtectReq: return 1 << 15;
1150 case Attribute::Alignment: return 31 << 16;
1151 case Attribute::NoCapture: return 1 << 21;
1152 case Attribute::NoRedZone: return 1 << 22;
1153 case Attribute::NoImplicitFloat: return 1 << 23;
1154 case Attribute::Naked: return 1 << 24;
1155 case Attribute::InlineHint: return 1 << 25;
1156 case Attribute::StackAlignment: return 7 << 26;
1157 case Attribute::ReturnsTwice: return 1 << 29;
1158 case Attribute::UWTable: return 1 << 30;
1159 case Attribute::NonLazyBind: return 1U << 31;
1160 case Attribute::SanitizeAddress: return 1ULL << 32;
1161 case Attribute::MinSize: return 1ULL << 33;
1162 case Attribute::NoDuplicate: return 1ULL << 34;
1163 case Attribute::StackProtectStrong: return 1ULL << 35;
1164 case Attribute::SanitizeThread: return 1ULL << 36;
1165 case Attribute::SanitizeMemory: return 1ULL << 37;
1166 case Attribute::NoBuiltin: return 1ULL << 38;
1167 case Attribute::Returned: return 1ULL << 39;
1168 case Attribute::Cold: return 1ULL << 40;
1169 case Attribute::Builtin: return 1ULL << 41;
1170 case Attribute::OptimizeNone: return 1ULL << 42;
1171 case Attribute::InAlloca: return 1ULL << 43;
1172 case Attribute::NonNull: return 1ULL << 44;
1173 case Attribute::JumpTable: return 1ULL << 45;
1174 case Attribute::Convergent: return 1ULL << 46;
1175 case Attribute::SafeStack: return 1ULL << 47;
1176 case Attribute::NoRecurse: return 1ULL << 48;
1177 case Attribute::InaccessibleMemOnly: return 1ULL << 49;
1178 case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50;
1179 case Attribute::SwiftSelf: return 1ULL << 51;
1180 case Attribute::SwiftError: return 1ULL << 52;
1181 case Attribute::WriteOnly: return 1ULL << 53;
1182 case Attribute::Speculatable: return 1ULL << 54;
1183 case Attribute::StrictFP: return 1ULL << 55;
1184 case Attribute::SanitizeHWAddress: return 1ULL << 56;
1185 case Attribute::NoCfCheck: return 1ULL << 57;
1186 case Attribute::OptForFuzzing: return 1ULL << 58;
1187 case Attribute::ShadowCallStack: return 1ULL << 59;
1188 case Attribute::SpeculativeLoadHardening:
1190 case Attribute::Dereferenceable:
1191 llvm_unreachable("dereferenceable attribute not supported in raw format");
1193 case Attribute::DereferenceableOrNull:
1194 llvm_unreachable("dereferenceable_or_null attribute not supported in raw "
1197 case Attribute::ArgMemOnly:
1198 llvm_unreachable("argmemonly attribute not supported in raw format");
1200 case Attribute::AllocSize:
1201 llvm_unreachable("allocsize not supported in raw format");
1204 llvm_unreachable("Unsupported attribute type");
1207 static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) {
1210 for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
1211 I = Attribute::AttrKind(I + 1)) {
1212 if (I == Attribute::Dereferenceable ||
1213 I == Attribute::DereferenceableOrNull ||
1214 I == Attribute::ArgMemOnly ||
1215 I == Attribute::AllocSize)
1217 if (uint64_t A = (Val & getRawAttributeMask(I))) {
1218 if (I == Attribute::Alignment)
1219 B.addAlignmentAttr(1ULL << ((A >> 16) - 1));
1220 else if (I == Attribute::StackAlignment)
1221 B.addStackAlignmentAttr(1ULL << ((A >> 26)-1));
1228 /// This fills an AttrBuilder object with the LLVM attributes that have
1229 /// been decoded from the given integer. This function must stay in sync with
1230 /// 'encodeLLVMAttributesForBitcode'.
1231 static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
1232 uint64_t EncodedAttrs) {
1233 // FIXME: Remove in 4.0.
1235 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1236 // the bits above 31 down by 11 bits.
1237 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1238 assert((!Alignment || isPowerOf2_32(Alignment)) &&
1239 "Alignment must be a power of two.");
1242 B.addAlignmentAttr(Alignment);
1243 addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
1244 (EncodedAttrs & 0xffff));
1247 Error BitcodeReader::parseAttributeBlock() {
1248 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
1249 return error("Invalid record");
1251 if (!MAttributes.empty())
1252 return error("Invalid multiple blocks");
1254 SmallVector<uint64_t, 64> Record;
1256 SmallVector<AttributeList, 8> Attrs;
1258 // Read all the records.
1260 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1262 switch (Entry.Kind) {
1263 case BitstreamEntry::SubBlock: // Handled for us already.
1264 case BitstreamEntry::Error:
1265 return error("Malformed block");
1266 case BitstreamEntry::EndBlock:
1267 return Error::success();
1268 case BitstreamEntry::Record:
1269 // The interesting case.
1275 switch (Stream.readRecord(Entry.ID, Record)) {
1276 default: // Default behavior: ignore.
1278 case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...]
1279 // FIXME: Remove in 4.0.
1280 if (Record.size() & 1)
1281 return error("Invalid record");
1283 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
1285 decodeLLVMAttributesForBitcode(B, Record[i+1]);
1286 Attrs.push_back(AttributeList::get(Context, Record[i], B));
1289 MAttributes.push_back(AttributeList::get(Context, Attrs));
1292 case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...]
1293 for (unsigned i = 0, e = Record.size(); i != e; ++i)
1294 Attrs.push_back(MAttributeGroups[Record[i]]);
1296 MAttributes.push_back(AttributeList::get(Context, Attrs));
1303 // Returns Attribute::None on unrecognized codes.
1304 static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
1307 return Attribute::None;
1308 case bitc::ATTR_KIND_ALIGNMENT:
1309 return Attribute::Alignment;
1310 case bitc::ATTR_KIND_ALWAYS_INLINE:
1311 return Attribute::AlwaysInline;
1312 case bitc::ATTR_KIND_ARGMEMONLY:
1313 return Attribute::ArgMemOnly;
1314 case bitc::ATTR_KIND_BUILTIN:
1315 return Attribute::Builtin;
1316 case bitc::ATTR_KIND_BY_VAL:
1317 return Attribute::ByVal;
1318 case bitc::ATTR_KIND_IN_ALLOCA:
1319 return Attribute::InAlloca;
1320 case bitc::ATTR_KIND_COLD:
1321 return Attribute::Cold;
1322 case bitc::ATTR_KIND_CONVERGENT:
1323 return Attribute::Convergent;
1324 case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY:
1325 return Attribute::InaccessibleMemOnly;
1326 case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY:
1327 return Attribute::InaccessibleMemOrArgMemOnly;
1328 case bitc::ATTR_KIND_INLINE_HINT:
1329 return Attribute::InlineHint;
1330 case bitc::ATTR_KIND_IN_REG:
1331 return Attribute::InReg;
1332 case bitc::ATTR_KIND_JUMP_TABLE:
1333 return Attribute::JumpTable;
1334 case bitc::ATTR_KIND_MIN_SIZE:
1335 return Attribute::MinSize;
1336 case bitc::ATTR_KIND_NAKED:
1337 return Attribute::Naked;
1338 case bitc::ATTR_KIND_NEST:
1339 return Attribute::Nest;
1340 case bitc::ATTR_KIND_NO_ALIAS:
1341 return Attribute::NoAlias;
1342 case bitc::ATTR_KIND_NO_BUILTIN:
1343 return Attribute::NoBuiltin;
1344 case bitc::ATTR_KIND_NO_CAPTURE:
1345 return Attribute::NoCapture;
1346 case bitc::ATTR_KIND_NO_DUPLICATE:
1347 return Attribute::NoDuplicate;
1348 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
1349 return Attribute::NoImplicitFloat;
1350 case bitc::ATTR_KIND_NO_INLINE:
1351 return Attribute::NoInline;
1352 case bitc::ATTR_KIND_NO_RECURSE:
1353 return Attribute::NoRecurse;
1354 case bitc::ATTR_KIND_NON_LAZY_BIND:
1355 return Attribute::NonLazyBind;
1356 case bitc::ATTR_KIND_NON_NULL:
1357 return Attribute::NonNull;
1358 case bitc::ATTR_KIND_DEREFERENCEABLE:
1359 return Attribute::Dereferenceable;
1360 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL:
1361 return Attribute::DereferenceableOrNull;
1362 case bitc::ATTR_KIND_ALLOC_SIZE:
1363 return Attribute::AllocSize;
1364 case bitc::ATTR_KIND_NO_RED_ZONE:
1365 return Attribute::NoRedZone;
1366 case bitc::ATTR_KIND_NO_RETURN:
1367 return Attribute::NoReturn;
1368 case bitc::ATTR_KIND_NOCF_CHECK:
1369 return Attribute::NoCfCheck;
1370 case bitc::ATTR_KIND_NO_UNWIND:
1371 return Attribute::NoUnwind;
1372 case bitc::ATTR_KIND_OPT_FOR_FUZZING:
1373 return Attribute::OptForFuzzing;
1374 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
1375 return Attribute::OptimizeForSize;
1376 case bitc::ATTR_KIND_OPTIMIZE_NONE:
1377 return Attribute::OptimizeNone;
1378 case bitc::ATTR_KIND_READ_NONE:
1379 return Attribute::ReadNone;
1380 case bitc::ATTR_KIND_READ_ONLY:
1381 return Attribute::ReadOnly;
1382 case bitc::ATTR_KIND_RETURNED:
1383 return Attribute::Returned;
1384 case bitc::ATTR_KIND_RETURNS_TWICE:
1385 return Attribute::ReturnsTwice;
1386 case bitc::ATTR_KIND_S_EXT:
1387 return Attribute::SExt;
1388 case bitc::ATTR_KIND_SPECULATABLE:
1389 return Attribute::Speculatable;
1390 case bitc::ATTR_KIND_STACK_ALIGNMENT:
1391 return Attribute::StackAlignment;
1392 case bitc::ATTR_KIND_STACK_PROTECT:
1393 return Attribute::StackProtect;
1394 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
1395 return Attribute::StackProtectReq;
1396 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
1397 return Attribute::StackProtectStrong;
1398 case bitc::ATTR_KIND_SAFESTACK:
1399 return Attribute::SafeStack;
1400 case bitc::ATTR_KIND_SHADOWCALLSTACK:
1401 return Attribute::ShadowCallStack;
1402 case bitc::ATTR_KIND_STRICT_FP:
1403 return Attribute::StrictFP;
1404 case bitc::ATTR_KIND_STRUCT_RET:
1405 return Attribute::StructRet;
1406 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
1407 return Attribute::SanitizeAddress;
1408 case bitc::ATTR_KIND_SANITIZE_HWADDRESS:
1409 return Attribute::SanitizeHWAddress;
1410 case bitc::ATTR_KIND_SANITIZE_THREAD:
1411 return Attribute::SanitizeThread;
1412 case bitc::ATTR_KIND_SANITIZE_MEMORY:
1413 return Attribute::SanitizeMemory;
1414 case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING:
1415 return Attribute::SpeculativeLoadHardening;
1416 case bitc::ATTR_KIND_SWIFT_ERROR:
1417 return Attribute::SwiftError;
1418 case bitc::ATTR_KIND_SWIFT_SELF:
1419 return Attribute::SwiftSelf;
1420 case bitc::ATTR_KIND_UW_TABLE:
1421 return Attribute::UWTable;
1422 case bitc::ATTR_KIND_WRITEONLY:
1423 return Attribute::WriteOnly;
1424 case bitc::ATTR_KIND_Z_EXT:
1425 return Attribute::ZExt;
1429 Error BitcodeReader::parseAlignmentValue(uint64_t Exponent,
1430 unsigned &Alignment) {
1431 // Note: Alignment in bitcode files is incremented by 1, so that zero
1432 // can be used for default alignment.
1433 if (Exponent > Value::MaxAlignmentExponent + 1)
1434 return error("Invalid alignment value");
1435 Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
1436 return Error::success();
1439 Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) {
1440 *Kind = getAttrFromCode(Code);
1441 if (*Kind == Attribute::None)
1442 return error("Unknown attribute kind (" + Twine(Code) + ")");
1443 return Error::success();
1446 Error BitcodeReader::parseAttributeGroupBlock() {
1447 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
1448 return error("Invalid record");
1450 if (!MAttributeGroups.empty())
1451 return error("Invalid multiple blocks");
1453 SmallVector<uint64_t, 64> Record;
1455 // Read all the records.
1457 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1459 switch (Entry.Kind) {
1460 case BitstreamEntry::SubBlock: // Handled for us already.
1461 case BitstreamEntry::Error:
1462 return error("Malformed block");
1463 case BitstreamEntry::EndBlock:
1464 return Error::success();
1465 case BitstreamEntry::Record:
1466 // The interesting case.
1472 switch (Stream.readRecord(Entry.ID, Record)) {
1473 default: // Default behavior: ignore.
1475 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
1476 if (Record.size() < 3)
1477 return error("Invalid record");
1479 uint64_t GrpID = Record[0];
1480 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
1483 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1484 if (Record[i] == 0) { // Enum attribute
1485 Attribute::AttrKind Kind;
1486 if (Error Err = parseAttrKind(Record[++i], &Kind))
1489 B.addAttribute(Kind);
1490 } else if (Record[i] == 1) { // Integer attribute
1491 Attribute::AttrKind Kind;
1492 if (Error Err = parseAttrKind(Record[++i], &Kind))
1494 if (Kind == Attribute::Alignment)
1495 B.addAlignmentAttr(Record[++i]);
1496 else if (Kind == Attribute::StackAlignment)
1497 B.addStackAlignmentAttr(Record[++i]);
1498 else if (Kind == Attribute::Dereferenceable)
1499 B.addDereferenceableAttr(Record[++i]);
1500 else if (Kind == Attribute::DereferenceableOrNull)
1501 B.addDereferenceableOrNullAttr(Record[++i]);
1502 else if (Kind == Attribute::AllocSize)
1503 B.addAllocSizeAttrFromRawRepr(Record[++i]);
1504 } else { // String attribute
1505 assert((Record[i] == 3 || Record[i] == 4) &&
1506 "Invalid attribute group entry");
1507 bool HasValue = (Record[i++] == 4);
1508 SmallString<64> KindStr;
1509 SmallString<64> ValStr;
1511 while (Record[i] != 0 && i != e)
1512 KindStr += Record[i++];
1513 assert(Record[i] == 0 && "Kind string not null terminated");
1516 // Has a value associated with it.
1517 ++i; // Skip the '0' that terminates the "kind" string.
1518 while (Record[i] != 0 && i != e)
1519 ValStr += Record[i++];
1520 assert(Record[i] == 0 && "Value string not null terminated");
1523 B.addAttribute(KindStr.str(), ValStr.str());
1527 MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B);
1534 Error BitcodeReader::parseTypeTable() {
1535 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
1536 return error("Invalid record");
1538 return parseTypeTableBody();
1541 Error BitcodeReader::parseTypeTableBody() {
1542 if (!TypeList.empty())
1543 return error("Invalid multiple blocks");
1545 SmallVector<uint64_t, 64> Record;
1546 unsigned NumRecords = 0;
1548 SmallString<64> TypeName;
1550 // Read all the records for this type table.
1552 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1554 switch (Entry.Kind) {
1555 case BitstreamEntry::SubBlock: // Handled for us already.
1556 case BitstreamEntry::Error:
1557 return error("Malformed block");
1558 case BitstreamEntry::EndBlock:
1559 if (NumRecords != TypeList.size())
1560 return error("Malformed block");
1561 return Error::success();
1562 case BitstreamEntry::Record:
1563 // The interesting case.
1569 Type *ResultTy = nullptr;
1570 switch (Stream.readRecord(Entry.ID, Record)) {
1572 return error("Invalid value");
1573 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
1574 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1575 // type list. This allows us to reserve space.
1576 if (Record.size() < 1)
1577 return error("Invalid record");
1578 TypeList.resize(Record[0]);
1580 case bitc::TYPE_CODE_VOID: // VOID
1581 ResultTy = Type::getVoidTy(Context);
1583 case bitc::TYPE_CODE_HALF: // HALF
1584 ResultTy = Type::getHalfTy(Context);
1586 case bitc::TYPE_CODE_FLOAT: // FLOAT
1587 ResultTy = Type::getFloatTy(Context);
1589 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
1590 ResultTy = Type::getDoubleTy(Context);
1592 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
1593 ResultTy = Type::getX86_FP80Ty(Context);
1595 case bitc::TYPE_CODE_FP128: // FP128
1596 ResultTy = Type::getFP128Ty(Context);
1598 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
1599 ResultTy = Type::getPPC_FP128Ty(Context);
1601 case bitc::TYPE_CODE_LABEL: // LABEL
1602 ResultTy = Type::getLabelTy(Context);
1604 case bitc::TYPE_CODE_METADATA: // METADATA
1605 ResultTy = Type::getMetadataTy(Context);
1607 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
1608 ResultTy = Type::getX86_MMXTy(Context);
1610 case bitc::TYPE_CODE_TOKEN: // TOKEN
1611 ResultTy = Type::getTokenTy(Context);
1613 case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
1614 if (Record.size() < 1)
1615 return error("Invalid record");
1617 uint64_t NumBits = Record[0];
1618 if (NumBits < IntegerType::MIN_INT_BITS ||
1619 NumBits > IntegerType::MAX_INT_BITS)
1620 return error("Bitwidth for integer type out of range");
1621 ResultTy = IntegerType::get(Context, NumBits);
1624 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
1625 // [pointee type, address space]
1626 if (Record.size() < 1)
1627 return error("Invalid record");
1628 unsigned AddressSpace = 0;
1629 if (Record.size() == 2)
1630 AddressSpace = Record[1];
1631 ResultTy = getTypeByID(Record[0]);
1633 !PointerType::isValidElementType(ResultTy))
1634 return error("Invalid type");
1635 ResultTy = PointerType::get(ResultTy, AddressSpace);
1638 case bitc::TYPE_CODE_FUNCTION_OLD: {
1639 // FIXME: attrid is dead, remove it in LLVM 4.0
1640 // FUNCTION: [vararg, attrid, retty, paramty x N]
1641 if (Record.size() < 3)
1642 return error("Invalid record");
1643 SmallVector<Type*, 8> ArgTys;
1644 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
1645 if (Type *T = getTypeByID(Record[i]))
1646 ArgTys.push_back(T);
1651 ResultTy = getTypeByID(Record[2]);
1652 if (!ResultTy || ArgTys.size() < Record.size()-3)
1653 return error("Invalid type");
1655 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1658 case bitc::TYPE_CODE_FUNCTION: {
1659 // FUNCTION: [vararg, retty, paramty x N]
1660 if (Record.size() < 2)
1661 return error("Invalid record");
1662 SmallVector<Type*, 8> ArgTys;
1663 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1664 if (Type *T = getTypeByID(Record[i])) {
1665 if (!FunctionType::isValidArgumentType(T))
1666 return error("Invalid function argument type");
1667 ArgTys.push_back(T);
1673 ResultTy = getTypeByID(Record[1]);
1674 if (!ResultTy || ArgTys.size() < Record.size()-2)
1675 return error("Invalid type");
1677 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1680 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
1681 if (Record.size() < 1)
1682 return error("Invalid record");
1683 SmallVector<Type*, 8> EltTys;
1684 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1685 if (Type *T = getTypeByID(Record[i]))
1686 EltTys.push_back(T);
1690 if (EltTys.size() != Record.size()-1)
1691 return error("Invalid type");
1692 ResultTy = StructType::get(Context, EltTys, Record[0]);
1695 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
1696 if (convertToString(Record, 0, TypeName))
1697 return error("Invalid record");
1700 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
1701 if (Record.size() < 1)
1702 return error("Invalid record");
1704 if (NumRecords >= TypeList.size())
1705 return error("Invalid TYPE table");
1707 // Check to see if this was forward referenced, if so fill in the temp.
1708 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1710 Res->setName(TypeName);
1711 TypeList[NumRecords] = nullptr;
1712 } else // Otherwise, create a new struct.
1713 Res = createIdentifiedStructType(Context, TypeName);
1716 SmallVector<Type*, 8> EltTys;
1717 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1718 if (Type *T = getTypeByID(Record[i]))
1719 EltTys.push_back(T);
1723 if (EltTys.size() != Record.size()-1)
1724 return error("Invalid record");
1725 Res->setBody(EltTys, Record[0]);
1729 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
1730 if (Record.size() != 1)
1731 return error("Invalid record");
1733 if (NumRecords >= TypeList.size())
1734 return error("Invalid TYPE table");
1736 // Check to see if this was forward referenced, if so fill in the temp.
1737 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1739 Res->setName(TypeName);
1740 TypeList[NumRecords] = nullptr;
1741 } else // Otherwise, create a new struct with no body.
1742 Res = createIdentifiedStructType(Context, TypeName);
1747 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
1748 if (Record.size() < 2)
1749 return error("Invalid record");
1750 ResultTy = getTypeByID(Record[1]);
1751 if (!ResultTy || !ArrayType::isValidElementType(ResultTy))
1752 return error("Invalid type");
1753 ResultTy = ArrayType::get(ResultTy, Record[0]);
1755 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
1756 if (Record.size() < 2)
1757 return error("Invalid record");
1759 return error("Invalid vector length");
1760 ResultTy = getTypeByID(Record[1]);
1761 if (!ResultTy || !StructType::isValidElementType(ResultTy))
1762 return error("Invalid type");
1763 ResultTy = VectorType::get(ResultTy, Record[0]);
1767 if (NumRecords >= TypeList.size())
1768 return error("Invalid TYPE table");
1769 if (TypeList[NumRecords])
1771 "Invalid TYPE table: Only named structs can be forward referenced");
1772 assert(ResultTy && "Didn't read a type?");
1773 TypeList[NumRecords++] = ResultTy;
1777 Error BitcodeReader::parseOperandBundleTags() {
1778 if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
1779 return error("Invalid record");
1781 if (!BundleTags.empty())
1782 return error("Invalid multiple blocks");
1784 SmallVector<uint64_t, 64> Record;
1787 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1789 switch (Entry.Kind) {
1790 case BitstreamEntry::SubBlock: // Handled for us already.
1791 case BitstreamEntry::Error:
1792 return error("Malformed block");
1793 case BitstreamEntry::EndBlock:
1794 return Error::success();
1795 case BitstreamEntry::Record:
1796 // The interesting case.
1800 // Tags are implicitly mapped to integers by their order.
1802 if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG)
1803 return error("Invalid record");
1805 // OPERAND_BUNDLE_TAG: [strchr x N]
1806 BundleTags.emplace_back();
1807 if (convertToString(Record, 0, BundleTags.back()))
1808 return error("Invalid record");
1813 Error BitcodeReader::parseSyncScopeNames() {
1814 if (Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID))
1815 return error("Invalid record");
1818 return error("Invalid multiple synchronization scope names blocks");
1820 SmallVector<uint64_t, 64> Record;
1822 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1823 switch (Entry.Kind) {
1824 case BitstreamEntry::SubBlock: // Handled for us already.
1825 case BitstreamEntry::Error:
1826 return error("Malformed block");
1827 case BitstreamEntry::EndBlock:
1829 return error("Invalid empty synchronization scope names block");
1830 return Error::success();
1831 case BitstreamEntry::Record:
1832 // The interesting case.
1836 // Synchronization scope names are implicitly mapped to synchronization
1837 // scope IDs by their order.
1839 if (Stream.readRecord(Entry.ID, Record) != bitc::SYNC_SCOPE_NAME)
1840 return error("Invalid record");
1842 SmallString<16> SSN;
1843 if (convertToString(Record, 0, SSN))
1844 return error("Invalid record");
1846 SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN));
1851 /// Associate a value with its name from the given index in the provided record.
1852 Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
1853 unsigned NameIndex, Triple &TT) {
1854 SmallString<128> ValueName;
1855 if (convertToString(Record, NameIndex, ValueName))
1856 return error("Invalid record");
1857 unsigned ValueID = Record[0];
1858 if (ValueID >= ValueList.size() || !ValueList[ValueID])
1859 return error("Invalid record");
1860 Value *V = ValueList[ValueID];
1862 StringRef NameStr(ValueName.data(), ValueName.size());
1863 if (NameStr.find_first_of(0) != StringRef::npos)
1864 return error("Invalid value name");
1865 V->setName(NameStr);
1866 auto *GO = dyn_cast<GlobalObject>(V);
1868 if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) {
1869 if (TT.supportsCOMDAT())
1870 GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
1872 GO->setComdat(nullptr);
1878 /// Helper to note and return the current location, and jump to the given
1880 static uint64_t jumpToValueSymbolTable(uint64_t Offset,
1881 BitstreamCursor &Stream) {
1882 // Save the current parsing location so we can jump back at the end
1884 uint64_t CurrentBit = Stream.GetCurrentBitNo();
1885 Stream.JumpToBit(Offset * 32);
1887 // Do some checking if we are in debug mode.
1888 BitstreamEntry Entry = Stream.advance();
1889 assert(Entry.Kind == BitstreamEntry::SubBlock);
1890 assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID);
1892 // In NDEBUG mode ignore the output so we don't get an unused variable
1899 void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta,
1901 ArrayRef<uint64_t> Record) {
1902 // Note that we subtract 1 here because the offset is relative to one word
1903 // before the start of the identification or module block, which was
1904 // historically always the start of the regular bitcode header.
1905 uint64_t FuncWordOffset = Record[1] - 1;
1906 uint64_t FuncBitOffset = FuncWordOffset * 32;
1907 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
1908 // Set the LastFunctionBlockBit to point to the last function block.
1909 // Later when parsing is resumed after function materialization,
1910 // we can simply skip that last function block.
1911 if (FuncBitOffset > LastFunctionBlockBit)
1912 LastFunctionBlockBit = FuncBitOffset;
1915 /// Read a new-style GlobalValue symbol table.
1916 Error BitcodeReader::parseGlobalValueSymbolTable() {
1917 unsigned FuncBitcodeOffsetDelta =
1918 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
1920 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
1921 return error("Invalid record");
1923 SmallVector<uint64_t, 64> Record;
1925 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1927 switch (Entry.Kind) {
1928 case BitstreamEntry::SubBlock:
1929 case BitstreamEntry::Error:
1930 return error("Malformed block");
1931 case BitstreamEntry::EndBlock:
1932 return Error::success();
1933 case BitstreamEntry::Record:
1938 switch (Stream.readRecord(Entry.ID, Record)) {
1939 case bitc::VST_CODE_FNENTRY: // [valueid, offset]
1940 setDeferredFunctionInfo(FuncBitcodeOffsetDelta,
1941 cast<Function>(ValueList[Record[0]]), Record);
1947 /// Parse the value symbol table at either the current parsing location or
1948 /// at the given bit offset if provided.
1949 Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
1950 uint64_t CurrentBit;
1951 // Pass in the Offset to distinguish between calling for the module-level
1952 // VST (where we want to jump to the VST offset) and the function-level
1953 // VST (where we don't).
1955 CurrentBit = jumpToValueSymbolTable(Offset, Stream);
1956 // If this module uses a string table, read this as a module-level VST.
1958 if (Error Err = parseGlobalValueSymbolTable())
1960 Stream.JumpToBit(CurrentBit);
1961 return Error::success();
1963 // Otherwise, the VST will be in a similar format to a function-level VST,
1964 // and will contain symbol names.
1967 // Compute the delta between the bitcode indices in the VST (the word offset
1968 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
1969 // expected by the lazy reader. The reader's EnterSubBlock expects to have
1970 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
1971 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
1972 // just before entering the VST subblock because: 1) the EnterSubBlock
1973 // changes the AbbrevID width; 2) the VST block is nested within the same
1974 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
1975 // AbbrevID width before calling EnterSubBlock; and 3) when we want to
1976 // jump to the FUNCTION_BLOCK using this offset later, we don't want
1977 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
1978 unsigned FuncBitcodeOffsetDelta =
1979 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
1981 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
1982 return error("Invalid record");
1984 SmallVector<uint64_t, 64> Record;
1986 Triple TT(TheModule->getTargetTriple());
1988 // Read all the records for this value table.
1989 SmallString<128> ValueName;
1992 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1994 switch (Entry.Kind) {
1995 case BitstreamEntry::SubBlock: // Handled for us already.
1996 case BitstreamEntry::Error:
1997 return error("Malformed block");
1998 case BitstreamEntry::EndBlock:
2000 Stream.JumpToBit(CurrentBit);
2001 return Error::success();
2002 case BitstreamEntry::Record:
2003 // The interesting case.
2009 switch (Stream.readRecord(Entry.ID, Record)) {
2010 default: // Default behavior: unknown type.
2012 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
2013 Expected<Value *> ValOrErr = recordValue(Record, 1, TT);
2014 if (Error Err = ValOrErr.takeError())
2019 case bitc::VST_CODE_FNENTRY: {
2020 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
2021 Expected<Value *> ValOrErr = recordValue(Record, 2, TT);
2022 if (Error Err = ValOrErr.takeError())
2024 Value *V = ValOrErr.get();
2026 // Ignore function offsets emitted for aliases of functions in older
2027 // versions of LLVM.
2028 if (auto *F = dyn_cast<Function>(V))
2029 setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record);
2032 case bitc::VST_CODE_BBENTRY: {
2033 if (convertToString(Record, 1, ValueName))
2034 return error("Invalid record");
2035 BasicBlock *BB = getBasicBlock(Record[0]);
2037 return error("Invalid record");
2039 BB->setName(StringRef(ValueName.data(), ValueName.size()));
2047 /// Decode a signed value stored with the sign bit in the LSB for dense VBR
2049 uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
2054 // There is no such thing as -0 with integers. "-0" really means MININT.
2058 /// Resolve all of the initializers for global values and aliases that we can.
2059 Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() {
2060 std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist;
2061 std::vector<std::pair<GlobalIndirectSymbol *, unsigned>>
2062 IndirectSymbolInitWorklist;
2063 std::vector<std::pair<Function *, unsigned>> FunctionPrefixWorklist;
2064 std::vector<std::pair<Function *, unsigned>> FunctionPrologueWorklist;
2065 std::vector<std::pair<Function *, unsigned>> FunctionPersonalityFnWorklist;
2067 GlobalInitWorklist.swap(GlobalInits);
2068 IndirectSymbolInitWorklist.swap(IndirectSymbolInits);
2069 FunctionPrefixWorklist.swap(FunctionPrefixes);
2070 FunctionPrologueWorklist.swap(FunctionPrologues);
2071 FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns);
2073 while (!GlobalInitWorklist.empty()) {
2074 unsigned ValID = GlobalInitWorklist.back().second;
2075 if (ValID >= ValueList.size()) {
2076 // Not ready to resolve this yet, it requires something later in the file.
2077 GlobalInits.push_back(GlobalInitWorklist.back());
2079 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2080 GlobalInitWorklist.back().first->setInitializer(C);
2082 return error("Expected a constant");
2084 GlobalInitWorklist.pop_back();
2087 while (!IndirectSymbolInitWorklist.empty()) {
2088 unsigned ValID = IndirectSymbolInitWorklist.back().second;
2089 if (ValID >= ValueList.size()) {
2090 IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back());
2092 Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]);
2094 return error("Expected a constant");
2095 GlobalIndirectSymbol *GIS = IndirectSymbolInitWorklist.back().first;
2096 if (isa<GlobalAlias>(GIS) && C->getType() != GIS->getType())
2097 return error("Alias and aliasee types don't match");
2098 GIS->setIndirectSymbol(C);
2100 IndirectSymbolInitWorklist.pop_back();
2103 while (!FunctionPrefixWorklist.empty()) {
2104 unsigned ValID = FunctionPrefixWorklist.back().second;
2105 if (ValID >= ValueList.size()) {
2106 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
2108 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2109 FunctionPrefixWorklist.back().first->setPrefixData(C);
2111 return error("Expected a constant");
2113 FunctionPrefixWorklist.pop_back();
2116 while (!FunctionPrologueWorklist.empty()) {
2117 unsigned ValID = FunctionPrologueWorklist.back().second;
2118 if (ValID >= ValueList.size()) {
2119 FunctionPrologues.push_back(FunctionPrologueWorklist.back());
2121 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2122 FunctionPrologueWorklist.back().first->setPrologueData(C);
2124 return error("Expected a constant");
2126 FunctionPrologueWorklist.pop_back();
2129 while (!FunctionPersonalityFnWorklist.empty()) {
2130 unsigned ValID = FunctionPersonalityFnWorklist.back().second;
2131 if (ValID >= ValueList.size()) {
2132 FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back());
2134 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2135 FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C);
2137 return error("Expected a constant");
2139 FunctionPersonalityFnWorklist.pop_back();
2142 return Error::success();
2145 static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
2146 SmallVector<uint64_t, 8> Words(Vals.size());
2147 transform(Vals, Words.begin(),
2148 BitcodeReader::decodeSignRotatedValue);
2150 return APInt(TypeBits, Words);
2153 Error BitcodeReader::parseConstants() {
2154 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
2155 return error("Invalid record");
2157 SmallVector<uint64_t, 64> Record;
2159 // Read all the records for this value table.
2160 Type *CurTy = Type::getInt32Ty(Context);
2161 unsigned NextCstNo = ValueList.size();
2164 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2166 switch (Entry.Kind) {
2167 case BitstreamEntry::SubBlock: // Handled for us already.
2168 case BitstreamEntry::Error:
2169 return error("Malformed block");
2170 case BitstreamEntry::EndBlock:
2171 if (NextCstNo != ValueList.size())
2172 return error("Invalid constant reference");
2174 // Once all the constants have been read, go through and resolve forward
2176 ValueList.resolveConstantForwardRefs();
2177 return Error::success();
2178 case BitstreamEntry::Record:
2179 // The interesting case.
2185 Type *VoidType = Type::getVoidTy(Context);
2187 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
2189 default: // Default behavior: unknown constant
2190 case bitc::CST_CODE_UNDEF: // UNDEF
2191 V = UndefValue::get(CurTy);
2193 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
2195 return error("Invalid record");
2196 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
2197 return error("Invalid record");
2198 if (TypeList[Record[0]] == VoidType)
2199 return error("Invalid constant type");
2200 CurTy = TypeList[Record[0]];
2201 continue; // Skip the ValueList manipulation.
2202 case bitc::CST_CODE_NULL: // NULL
2203 V = Constant::getNullValue(CurTy);
2205 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
2206 if (!CurTy->isIntegerTy() || Record.empty())
2207 return error("Invalid record");
2208 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
2210 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
2211 if (!CurTy->isIntegerTy() || Record.empty())
2212 return error("Invalid record");
2215 readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth());
2216 V = ConstantInt::get(Context, VInt);
2220 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
2222 return error("Invalid record");
2223 if (CurTy->isHalfTy())
2224 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(),
2225 APInt(16, (uint16_t)Record[0])));
2226 else if (CurTy->isFloatTy())
2227 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(),
2228 APInt(32, (uint32_t)Record[0])));
2229 else if (CurTy->isDoubleTy())
2230 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(),
2231 APInt(64, Record[0])));
2232 else if (CurTy->isX86_FP80Ty()) {
2233 // Bits are not stored the same way as a normal i80 APInt, compensate.
2234 uint64_t Rearrange[2];
2235 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
2236 Rearrange[1] = Record[0] >> 48;
2237 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(),
2238 APInt(80, Rearrange)));
2239 } else if (CurTy->isFP128Ty())
2240 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(),
2241 APInt(128, Record)));
2242 else if (CurTy->isPPC_FP128Ty())
2243 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(),
2244 APInt(128, Record)));
2246 V = UndefValue::get(CurTy);
2250 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
2252 return error("Invalid record");
2254 unsigned Size = Record.size();
2255 SmallVector<Constant*, 16> Elts;
2257 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
2258 for (unsigned i = 0; i != Size; ++i)
2259 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
2260 STy->getElementType(i)));
2261 V = ConstantStruct::get(STy, Elts);
2262 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
2263 Type *EltTy = ATy->getElementType();
2264 for (unsigned i = 0; i != Size; ++i)
2265 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
2266 V = ConstantArray::get(ATy, Elts);
2267 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
2268 Type *EltTy = VTy->getElementType();
2269 for (unsigned i = 0; i != Size; ++i)
2270 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
2271 V = ConstantVector::get(Elts);
2273 V = UndefValue::get(CurTy);
2277 case bitc::CST_CODE_STRING: // STRING: [values]
2278 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
2280 return error("Invalid record");
2282 SmallString<16> Elts(Record.begin(), Record.end());
2283 V = ConstantDataArray::getString(Context, Elts,
2284 BitCode == bitc::CST_CODE_CSTRING);
2287 case bitc::CST_CODE_DATA: {// DATA: [n x value]
2289 return error("Invalid record");
2291 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
2292 if (EltTy->isIntegerTy(8)) {
2293 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
2294 if (isa<VectorType>(CurTy))
2295 V = ConstantDataVector::get(Context, Elts);
2297 V = ConstantDataArray::get(Context, Elts);
2298 } else if (EltTy->isIntegerTy(16)) {
2299 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2300 if (isa<VectorType>(CurTy))
2301 V = ConstantDataVector::get(Context, Elts);
2303 V = ConstantDataArray::get(Context, Elts);
2304 } else if (EltTy->isIntegerTy(32)) {
2305 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2306 if (isa<VectorType>(CurTy))
2307 V = ConstantDataVector::get(Context, Elts);
2309 V = ConstantDataArray::get(Context, Elts);
2310 } else if (EltTy->isIntegerTy(64)) {
2311 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
2312 if (isa<VectorType>(CurTy))
2313 V = ConstantDataVector::get(Context, Elts);
2315 V = ConstantDataArray::get(Context, Elts);
2316 } else if (EltTy->isHalfTy()) {
2317 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2318 if (isa<VectorType>(CurTy))
2319 V = ConstantDataVector::getFP(Context, Elts);
2321 V = ConstantDataArray::getFP(Context, Elts);
2322 } else if (EltTy->isFloatTy()) {
2323 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2324 if (isa<VectorType>(CurTy))
2325 V = ConstantDataVector::getFP(Context, Elts);
2327 V = ConstantDataArray::getFP(Context, Elts);
2328 } else if (EltTy->isDoubleTy()) {
2329 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
2330 if (isa<VectorType>(CurTy))
2331 V = ConstantDataVector::getFP(Context, Elts);
2333 V = ConstantDataArray::getFP(Context, Elts);
2335 return error("Invalid type for value");
2339 case bitc::CST_CODE_CE_UNOP: { // CE_UNOP: [opcode, opval]
2340 if (Record.size() < 2)
2341 return error("Invalid record");
2342 int Opc = getDecodedUnaryOpcode(Record[0], CurTy);
2344 V = UndefValue::get(CurTy); // Unknown unop.
2346 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
2348 V = ConstantExpr::get(Opc, LHS, Flags);
2352 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
2353 if (Record.size() < 3)
2354 return error("Invalid record");
2355 int Opc = getDecodedBinaryOpcode(Record[0], CurTy);
2357 V = UndefValue::get(CurTy); // Unknown binop.
2359 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
2360 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
2362 if (Record.size() >= 4) {
2363 if (Opc == Instruction::Add ||
2364 Opc == Instruction::Sub ||
2365 Opc == Instruction::Mul ||
2366 Opc == Instruction::Shl) {
2367 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
2368 Flags |= OverflowingBinaryOperator::NoSignedWrap;
2369 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
2370 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2371 } else if (Opc == Instruction::SDiv ||
2372 Opc == Instruction::UDiv ||
2373 Opc == Instruction::LShr ||
2374 Opc == Instruction::AShr) {
2375 if (Record[3] & (1 << bitc::PEO_EXACT))
2376 Flags |= SDivOperator::IsExact;
2379 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
2383 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
2384 if (Record.size() < 3)
2385 return error("Invalid record");
2386 int Opc = getDecodedCastOpcode(Record[0]);
2388 V = UndefValue::get(CurTy); // Unknown cast.
2390 Type *OpTy = getTypeByID(Record[1]);
2392 return error("Invalid record");
2393 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
2394 V = UpgradeBitCastExpr(Opc, Op, CurTy);
2395 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
2399 case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands]
2400 case bitc::CST_CODE_CE_GEP: // [ty, n x operands]
2401 case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x
2404 Type *PointeeType = nullptr;
2405 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX ||
2407 PointeeType = getTypeByID(Record[OpNum++]);
2409 bool InBounds = false;
2410 Optional<unsigned> InRangeIndex;
2411 if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) {
2412 uint64_t Op = Record[OpNum++];
2414 InRangeIndex = Op >> 1;
2415 } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP)
2418 SmallVector<Constant*, 16> Elts;
2419 while (OpNum != Record.size()) {
2420 Type *ElTy = getTypeByID(Record[OpNum++]);
2422 return error("Invalid record");
2423 Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy));
2428 cast<PointerType>(Elts[0]->getType()->getScalarType())
2430 return error("Explicit gep operator type does not match pointee type "
2431 "of pointer operand");
2433 if (Elts.size() < 1)
2434 return error("Invalid gep with no operands");
2436 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
2437 V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices,
2438 InBounds, InRangeIndex);
2441 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
2442 if (Record.size() < 3)
2443 return error("Invalid record");
2445 Type *SelectorTy = Type::getInt1Ty(Context);
2447 // The selector might be an i1 or an <n x i1>
2448 // Get the type from the ValueList before getting a forward ref.
2449 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
2450 if (Value *V = ValueList[Record[0]])
2451 if (SelectorTy != V->getType())
2452 SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements());
2454 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
2456 ValueList.getConstantFwdRef(Record[1],CurTy),
2457 ValueList.getConstantFwdRef(Record[2],CurTy));
2460 case bitc::CST_CODE_CE_EXTRACTELT
2461 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
2462 if (Record.size() < 3)
2463 return error("Invalid record");
2465 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
2467 return error("Invalid record");
2468 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2469 Constant *Op1 = nullptr;
2470 if (Record.size() == 4) {
2471 Type *IdxTy = getTypeByID(Record[2]);
2473 return error("Invalid record");
2474 Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2475 } else // TODO: Remove with llvm 4.0
2476 Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2478 return error("Invalid record");
2479 V = ConstantExpr::getExtractElement(Op0, Op1);
2482 case bitc::CST_CODE_CE_INSERTELT
2483 : { // CE_INSERTELT: [opval, opval, opty, opval]
2484 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2485 if (Record.size() < 3 || !OpTy)
2486 return error("Invalid record");
2487 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2488 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
2489 OpTy->getElementType());
2490 Constant *Op2 = nullptr;
2491 if (Record.size() == 4) {
2492 Type *IdxTy = getTypeByID(Record[2]);
2494 return error("Invalid record");
2495 Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2496 } else // TODO: Remove with llvm 4.0
2497 Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2499 return error("Invalid record");
2500 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
2503 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
2504 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2505 if (Record.size() < 3 || !OpTy)
2506 return error("Invalid record");
2507 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2508 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
2509 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
2510 OpTy->getNumElements());
2511 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
2512 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
2515 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
2516 VectorType *RTy = dyn_cast<VectorType>(CurTy);
2518 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
2519 if (Record.size() < 4 || !RTy || !OpTy)
2520 return error("Invalid record");
2521 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2522 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
2523 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
2524 RTy->getNumElements());
2525 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
2526 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
2529 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
2530 if (Record.size() < 4)
2531 return error("Invalid record");
2532 Type *OpTy = getTypeByID(Record[0]);
2534 return error("Invalid record");
2535 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2536 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
2538 if (OpTy->isFPOrFPVectorTy())
2539 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
2541 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
2544 // This maintains backward compatibility, pre-asm dialect keywords.
2545 // FIXME: Remove with the 4.0 release.
2546 case bitc::CST_CODE_INLINEASM_OLD: {
2547 if (Record.size() < 2)
2548 return error("Invalid record");
2549 std::string AsmStr, ConstrStr;
2550 bool HasSideEffects = Record[0] & 1;
2551 bool IsAlignStack = Record[0] >> 1;
2552 unsigned AsmStrSize = Record[1];
2553 if (2+AsmStrSize >= Record.size())
2554 return error("Invalid record");
2555 unsigned ConstStrSize = Record[2+AsmStrSize];
2556 if (3+AsmStrSize+ConstStrSize > Record.size())
2557 return error("Invalid record");
2559 for (unsigned i = 0; i != AsmStrSize; ++i)
2560 AsmStr += (char)Record[2+i];
2561 for (unsigned i = 0; i != ConstStrSize; ++i)
2562 ConstrStr += (char)Record[3+AsmStrSize+i];
2563 PointerType *PTy = cast<PointerType>(CurTy);
2564 UpgradeInlineAsmString(&AsmStr);
2565 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
2566 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
2569 // This version adds support for the asm dialect keywords (e.g.,
2571 case bitc::CST_CODE_INLINEASM: {
2572 if (Record.size() < 2)
2573 return error("Invalid record");
2574 std::string AsmStr, ConstrStr;
2575 bool HasSideEffects = Record[0] & 1;
2576 bool IsAlignStack = (Record[0] >> 1) & 1;
2577 unsigned AsmDialect = Record[0] >> 2;
2578 unsigned AsmStrSize = Record[1];
2579 if (2+AsmStrSize >= Record.size())
2580 return error("Invalid record");
2581 unsigned ConstStrSize = Record[2+AsmStrSize];
2582 if (3+AsmStrSize+ConstStrSize > Record.size())
2583 return error("Invalid record");
2585 for (unsigned i = 0; i != AsmStrSize; ++i)
2586 AsmStr += (char)Record[2+i];
2587 for (unsigned i = 0; i != ConstStrSize; ++i)
2588 ConstrStr += (char)Record[3+AsmStrSize+i];
2589 PointerType *PTy = cast<PointerType>(CurTy);
2590 UpgradeInlineAsmString(&AsmStr);
2591 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
2592 AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
2593 InlineAsm::AsmDialect(AsmDialect));
2596 case bitc::CST_CODE_BLOCKADDRESS:{
2597 if (Record.size() < 3)
2598 return error("Invalid record");
2599 Type *FnTy = getTypeByID(Record[0]);
2601 return error("Invalid record");
2603 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
2605 return error("Invalid record");
2607 // If the function is already parsed we can insert the block address right
2610 unsigned BBID = Record[2];
2612 // Invalid reference to entry block.
2613 return error("Invalid ID");
2615 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
2616 for (size_t I = 0, E = BBID; I != E; ++I) {
2618 return error("Invalid ID");
2623 // Otherwise insert a placeholder and remember it so it can be inserted
2624 // when the function is parsed.
2625 auto &FwdBBs = BasicBlockFwdRefs[Fn];
2627 BasicBlockFwdRefQueue.push_back(Fn);
2628 if (FwdBBs.size() < BBID + 1)
2629 FwdBBs.resize(BBID + 1);
2631 FwdBBs[BBID] = BasicBlock::Create(Context);
2634 V = BlockAddress::get(Fn, BB);
2639 ValueList.assignValue(V, NextCstNo);
2644 Error BitcodeReader::parseUseLists() {
2645 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
2646 return error("Invalid record");
2648 // Read all the records.
2649 SmallVector<uint64_t, 64> Record;
2652 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2654 switch (Entry.Kind) {
2655 case BitstreamEntry::SubBlock: // Handled for us already.
2656 case BitstreamEntry::Error:
2657 return error("Malformed block");
2658 case BitstreamEntry::EndBlock:
2659 return Error::success();
2660 case BitstreamEntry::Record:
2661 // The interesting case.
2665 // Read a use list record.
2668 switch (Stream.readRecord(Entry.ID, Record)) {
2669 default: // Default behavior: unknown type.
2671 case bitc::USELIST_CODE_BB:
2674 case bitc::USELIST_CODE_DEFAULT: {
2675 unsigned RecordLength = Record.size();
2676 if (RecordLength < 3)
2677 // Records should have at least an ID and two indexes.
2678 return error("Invalid record");
2679 unsigned ID = Record.back();
2684 assert(ID < FunctionBBs.size() && "Basic block not found");
2685 V = FunctionBBs[ID];
2688 unsigned NumUses = 0;
2689 SmallDenseMap<const Use *, unsigned, 16> Order;
2690 for (const Use &U : V->materialized_uses()) {
2691 if (++NumUses > Record.size())
2693 Order[&U] = Record[NumUses - 1];
2695 if (Order.size() != Record.size() || NumUses > Record.size())
2696 // Mismatches can happen if the functions are being materialized lazily
2697 // (out-of-order), or a value has been upgraded.
2700 V->sortUseList([&](const Use &L, const Use &R) {
2701 return Order.lookup(&L) < Order.lookup(&R);
2709 /// When we see the block for metadata, remember where it is and then skip it.
2710 /// This lets us lazily deserialize the metadata.
2711 Error BitcodeReader::rememberAndSkipMetadata() {
2712 // Save the current stream state.
2713 uint64_t CurBit = Stream.GetCurrentBitNo();
2714 DeferredMetadataInfo.push_back(CurBit);
2716 // Skip over the block for now.
2717 if (Stream.SkipBlock())
2718 return error("Invalid record");
2719 return Error::success();
2722 Error BitcodeReader::materializeMetadata() {
2723 for (uint64_t BitPos : DeferredMetadataInfo) {
2724 // Move the bit stream to the saved position.
2725 Stream.JumpToBit(BitPos);
2726 if (Error Err = MDLoader->parseModuleMetadata())
2730 // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
2732 if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) {
2733 NamedMDNode *LinkerOpts =
2734 TheModule->getOrInsertNamedMetadata("llvm.linker.options");
2735 for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands())
2736 LinkerOpts->addOperand(cast<MDNode>(MDOptions));
2739 DeferredMetadataInfo.clear();
2740 return Error::success();
2743 void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
2745 /// When we see the block for a function body, remember where it is and then
2746 /// skip it. This lets us lazily deserialize the functions.
2747 Error BitcodeReader::rememberAndSkipFunctionBody() {
2748 // Get the function we are talking about.
2749 if (FunctionsWithBodies.empty())
2750 return error("Insufficient function protos");
2752 Function *Fn = FunctionsWithBodies.back();
2753 FunctionsWithBodies.pop_back();
2755 // Save the current stream state.
2756 uint64_t CurBit = Stream.GetCurrentBitNo();
2758 (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&
2759 "Mismatch between VST and scanned function offsets");
2760 DeferredFunctionInfo[Fn] = CurBit;
2762 // Skip over the function block for now.
2763 if (Stream.SkipBlock())
2764 return error("Invalid record");
2765 return Error::success();
2768 Error BitcodeReader::globalCleanup() {
2769 // Patch the initializers for globals and aliases up.
2770 if (Error Err = resolveGlobalAndIndirectSymbolInits())
2772 if (!GlobalInits.empty() || !IndirectSymbolInits.empty())
2773 return error("Malformed global initializer set");
2775 // Look for intrinsic functions which need to be upgraded at some point
2776 for (Function &F : *TheModule) {
2777 MDLoader->upgradeDebugIntrinsics(F);
2779 if (UpgradeIntrinsicFunction(&F, NewFn))
2780 UpgradedIntrinsics[&F] = NewFn;
2781 else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F))
2782 // Some types could be renamed during loading if several modules are
2783 // loaded in the same LLVMContext (LTO scenario). In this case we should
2784 // remangle intrinsics names as well.
2785 RemangledIntrinsics[&F] = Remangled.getValue();
2788 // Look for global variables which need to be renamed.
2789 for (GlobalVariable &GV : TheModule->globals())
2790 UpgradeGlobalVariable(&GV);
2792 // Force deallocation of memory for these vectors to favor the client that
2793 // want lazy deserialization.
2794 std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits);
2795 std::vector<std::pair<GlobalIndirectSymbol *, unsigned>>().swap(
2796 IndirectSymbolInits);
2797 return Error::success();
2800 /// Support for lazy parsing of function bodies. This is required if we
2801 /// either have an old bitcode file without a VST forward declaration record,
2802 /// or if we have an anonymous function being materialized, since anonymous
2803 /// functions do not have a name and are therefore not in the VST.
2804 Error BitcodeReader::rememberAndSkipFunctionBodies() {
2805 Stream.JumpToBit(NextUnreadBit);
2807 if (Stream.AtEndOfStream())
2808 return error("Could not find function in stream");
2810 if (!SeenFirstFunctionBody)
2811 return error("Trying to materialize functions before seeing function blocks");
2813 // An old bitcode file with the symbol table at the end would have
2814 // finished the parse greedily.
2815 assert(SeenValueSymbolTable);
2817 SmallVector<uint64_t, 64> Record;
2820 BitstreamEntry Entry = Stream.advance();
2821 switch (Entry.Kind) {
2823 return error("Expect SubBlock");
2824 case BitstreamEntry::SubBlock:
2827 return error("Expect function block");
2828 case bitc::FUNCTION_BLOCK_ID:
2829 if (Error Err = rememberAndSkipFunctionBody())
2831 NextUnreadBit = Stream.GetCurrentBitNo();
2832 return Error::success();
2838 bool BitcodeReaderBase::readBlockInfo() {
2839 Optional<BitstreamBlockInfo> NewBlockInfo = Stream.ReadBlockInfoBlock();
2842 BlockInfo = std::move(*NewBlockInfo);
2846 Error BitcodeReader::parseComdatRecord(ArrayRef<uint64_t> Record) {
2847 // v1: [selection_kind, name]
2848 // v2: [strtab_offset, strtab_size, selection_kind]
2850 std::tie(Name, Record) = readNameFromStrtab(Record);
2853 return error("Invalid record");
2854 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
2855 std::string OldFormatName;
2857 if (Record.size() < 2)
2858 return error("Invalid record");
2859 unsigned ComdatNameSize = Record[1];
2860 OldFormatName.reserve(ComdatNameSize);
2861 for (unsigned i = 0; i != ComdatNameSize; ++i)
2862 OldFormatName += (char)Record[2 + i];
2863 Name = OldFormatName;
2865 Comdat *C = TheModule->getOrInsertComdat(Name);
2866 C->setSelectionKind(SK);
2867 ComdatList.push_back(C);
2868 return Error::success();
2871 static void inferDSOLocal(GlobalValue *GV) {
2872 // infer dso_local from linkage and visibility if it is not encoded.
2873 if (GV->hasLocalLinkage() ||
2874 (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage()))
2875 GV->setDSOLocal(true);
2878 Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) {
2879 // v1: [pointer type, isconst, initid, linkage, alignment, section,
2880 // visibility, threadlocal, unnamed_addr, externally_initialized,
2881 // dllstorageclass, comdat, attributes, preemption specifier] (name in VST)
2882 // v2: [strtab_offset, strtab_size, v1]
2884 std::tie(Name, Record) = readNameFromStrtab(Record);
2886 if (Record.size() < 6)
2887 return error("Invalid record");
2888 Type *Ty = getTypeByID(Record[0]);
2890 return error("Invalid record");
2891 bool isConstant = Record[1] & 1;
2892 bool explicitType = Record[1] & 2;
2893 unsigned AddressSpace;
2895 AddressSpace = Record[1] >> 2;
2897 if (!Ty->isPointerTy())
2898 return error("Invalid type for value");
2899 AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
2900 Ty = cast<PointerType>(Ty)->getElementType();
2903 uint64_t RawLinkage = Record[3];
2904 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
2906 if (Error Err = parseAlignmentValue(Record[4], Alignment))
2908 std::string Section;
2910 if (Record[5] - 1 >= SectionTable.size())
2911 return error("Invalid ID");
2912 Section = SectionTable[Record[5] - 1];
2914 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
2915 // Local linkage must have default visibility.
2916 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
2917 // FIXME: Change to an error if non-default in 4.0.
2918 Visibility = getDecodedVisibility(Record[6]);
2920 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
2921 if (Record.size() > 7)
2922 TLM = getDecodedThreadLocalMode(Record[7]);
2924 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
2925 if (Record.size() > 8)
2926 UnnamedAddr = getDecodedUnnamedAddrType(Record[8]);
2928 bool ExternallyInitialized = false;
2929 if (Record.size() > 9)
2930 ExternallyInitialized = Record[9];
2932 GlobalVariable *NewGV =
2933 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, Name,
2934 nullptr, TLM, AddressSpace, ExternallyInitialized);
2935 NewGV->setAlignment(Alignment);
2936 if (!Section.empty())
2937 NewGV->setSection(Section);
2938 NewGV->setVisibility(Visibility);
2939 NewGV->setUnnamedAddr(UnnamedAddr);
2941 if (Record.size() > 10)
2942 NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10]));
2944 upgradeDLLImportExportLinkage(NewGV, RawLinkage);
2946 ValueList.push_back(NewGV);
2948 // Remember which value to use for the global initializer.
2949 if (unsigned InitID = Record[2])
2950 GlobalInits.push_back(std::make_pair(NewGV, InitID - 1));
2952 if (Record.size() > 11) {
2953 if (unsigned ComdatID = Record[11]) {
2954 if (ComdatID > ComdatList.size())
2955 return error("Invalid global variable comdat ID");
2956 NewGV->setComdat(ComdatList[ComdatID - 1]);
2958 } else if (hasImplicitComdat(RawLinkage)) {
2959 NewGV->setComdat(reinterpret_cast<Comdat *>(1));
2962 if (Record.size() > 12) {
2963 auto AS = getAttributes(Record[12]).getFnAttributes();
2964 NewGV->setAttributes(AS);
2967 if (Record.size() > 13) {
2968 NewGV->setDSOLocal(getDecodedDSOLocal(Record[13]));
2970 inferDSOLocal(NewGV);
2972 return Error::success();
2975 Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) {
2976 // v1: [type, callingconv, isproto, linkage, paramattr, alignment, section,
2977 // visibility, gc, unnamed_addr, prologuedata, dllstorageclass, comdat,
2978 // prefixdata, personalityfn, preemption specifier, addrspace] (name in VST)
2979 // v2: [strtab_offset, strtab_size, v1]
2981 std::tie(Name, Record) = readNameFromStrtab(Record);
2983 if (Record.size() < 8)
2984 return error("Invalid record");
2985 Type *Ty = getTypeByID(Record[0]);
2987 return error("Invalid record");
2988 if (auto *PTy = dyn_cast<PointerType>(Ty))
2989 Ty = PTy->getElementType();
2990 auto *FTy = dyn_cast<FunctionType>(Ty);
2992 return error("Invalid type for value");
2993 auto CC = static_cast<CallingConv::ID>(Record[1]);
2994 if (CC & ~CallingConv::MaxID)
2995 return error("Invalid calling convention ID");
2997 unsigned AddrSpace = TheModule->getDataLayout().getProgramAddressSpace();
2998 if (Record.size() > 16)
2999 AddrSpace = Record[16];
3001 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
3002 AddrSpace, Name, TheModule);
3004 Func->setCallingConv(CC);
3005 bool isProto = Record[2];
3006 uint64_t RawLinkage = Record[3];
3007 Func->setLinkage(getDecodedLinkage(RawLinkage));
3008 Func->setAttributes(getAttributes(Record[4]));
3011 if (Error Err = parseAlignmentValue(Record[5], Alignment))
3013 Func->setAlignment(Alignment);
3015 if (Record[6] - 1 >= SectionTable.size())
3016 return error("Invalid ID");
3017 Func->setSection(SectionTable[Record[6] - 1]);
3019 // Local linkage must have default visibility.
3020 if (!Func->hasLocalLinkage())
3021 // FIXME: Change to an error if non-default in 4.0.
3022 Func->setVisibility(getDecodedVisibility(Record[7]));
3023 if (Record.size() > 8 && Record[8]) {
3024 if (Record[8] - 1 >= GCTable.size())
3025 return error("Invalid ID");
3026 Func->setGC(GCTable[Record[8] - 1]);
3028 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
3029 if (Record.size() > 9)
3030 UnnamedAddr = getDecodedUnnamedAddrType(Record[9]);
3031 Func->setUnnamedAddr(UnnamedAddr);
3032 if (Record.size() > 10 && Record[10] != 0)
3033 FunctionPrologues.push_back(std::make_pair(Func, Record[10] - 1));
3035 if (Record.size() > 11)
3036 Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));
3038 upgradeDLLImportExportLinkage(Func, RawLinkage);
3040 if (Record.size() > 12) {
3041 if (unsigned ComdatID = Record[12]) {
3042 if (ComdatID > ComdatList.size())
3043 return error("Invalid function comdat ID");
3044 Func->setComdat(ComdatList[ComdatID - 1]);
3046 } else if (hasImplicitComdat(RawLinkage)) {
3047 Func->setComdat(reinterpret_cast<Comdat *>(1));
3050 if (Record.size() > 13 && Record[13] != 0)
3051 FunctionPrefixes.push_back(std::make_pair(Func, Record[13] - 1));
3053 if (Record.size() > 14 && Record[14] != 0)
3054 FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1));
3056 if (Record.size() > 15) {
3057 Func->setDSOLocal(getDecodedDSOLocal(Record[15]));
3059 inferDSOLocal(Func);
3061 ValueList.push_back(Func);
3063 // If this is a function with a body, remember the prototype we are
3064 // creating now, so that we can match up the body with them later.
3066 Func->setIsMaterializable(true);
3067 FunctionsWithBodies.push_back(Func);
3068 DeferredFunctionInfo[Func] = 0;
3070 return Error::success();
3073 Error BitcodeReader::parseGlobalIndirectSymbolRecord(
3074 unsigned BitCode, ArrayRef<uint64_t> Record) {
3075 // v1 ALIAS_OLD: [alias type, aliasee val#, linkage] (name in VST)
3076 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
3077 // dllstorageclass, threadlocal, unnamed_addr,
3078 // preemption specifier] (name in VST)
3079 // v1 IFUNC: [alias type, addrspace, aliasee val#, linkage,
3080 // visibility, dllstorageclass, threadlocal, unnamed_addr,
3081 // preemption specifier] (name in VST)
3082 // v2: [strtab_offset, strtab_size, v1]
3084 std::tie(Name, Record) = readNameFromStrtab(Record);
3086 bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD;
3087 if (Record.size() < (3 + (unsigned)NewRecord))
3088 return error("Invalid record");
3090 Type *Ty = getTypeByID(Record[OpNum++]);
3092 return error("Invalid record");
3096 auto *PTy = dyn_cast<PointerType>(Ty);
3098 return error("Invalid type for value");
3099 Ty = PTy->getElementType();
3100 AddrSpace = PTy->getAddressSpace();
3102 AddrSpace = Record[OpNum++];
3105 auto Val = Record[OpNum++];
3106 auto Linkage = Record[OpNum++];
3107 GlobalIndirectSymbol *NewGA;
3108 if (BitCode == bitc::MODULE_CODE_ALIAS ||
3109 BitCode == bitc::MODULE_CODE_ALIAS_OLD)
3110 NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
3113 NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage), Name,
3114 nullptr, TheModule);
3115 // Old bitcode files didn't have visibility field.
3116 // Local linkage must have default visibility.
3117 if (OpNum != Record.size()) {
3118 auto VisInd = OpNum++;
3119 if (!NewGA->hasLocalLinkage())
3120 // FIXME: Change to an error if non-default in 4.0.
3121 NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
3123 if (BitCode == bitc::MODULE_CODE_ALIAS ||
3124 BitCode == bitc::MODULE_CODE_ALIAS_OLD) {
3125 if (OpNum != Record.size())
3126 NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++]));
3128 upgradeDLLImportExportLinkage(NewGA, Linkage);
3129 if (OpNum != Record.size())
3130 NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++]));
3131 if (OpNum != Record.size())
3132 NewGA->setUnnamedAddr(getDecodedUnnamedAddrType(Record[OpNum++]));
3134 if (OpNum != Record.size())
3135 NewGA->setDSOLocal(getDecodedDSOLocal(Record[OpNum++]));
3136 inferDSOLocal(NewGA);
3138 ValueList.push_back(NewGA);
3139 IndirectSymbolInits.push_back(std::make_pair(NewGA, Val));
3140 return Error::success();
3143 Error BitcodeReader::parseModule(uint64_t ResumeBit,
3144 bool ShouldLazyLoadMetadata) {
3146 Stream.JumpToBit(ResumeBit);
3147 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
3148 return error("Invalid record");
3150 SmallVector<uint64_t, 64> Record;
3152 // Read all the records for this module.
3154 BitstreamEntry Entry = Stream.advance();
3156 switch (Entry.Kind) {
3157 case BitstreamEntry::Error:
3158 return error("Malformed block");
3159 case BitstreamEntry::EndBlock:
3160 return globalCleanup();
3162 case BitstreamEntry::SubBlock:
3164 default: // Skip unknown content.
3165 if (Stream.SkipBlock())
3166 return error("Invalid record");
3168 case bitc::BLOCKINFO_BLOCK_ID:
3169 if (readBlockInfo())
3170 return error("Malformed block");
3172 case bitc::PARAMATTR_BLOCK_ID:
3173 if (Error Err = parseAttributeBlock())
3176 case bitc::PARAMATTR_GROUP_BLOCK_ID:
3177 if (Error Err = parseAttributeGroupBlock())
3180 case bitc::TYPE_BLOCK_ID_NEW:
3181 if (Error Err = parseTypeTable())
3184 case bitc::VALUE_SYMTAB_BLOCK_ID:
3185 if (!SeenValueSymbolTable) {
3186 // Either this is an old form VST without function index and an
3187 // associated VST forward declaration record (which would have caused
3188 // the VST to be jumped to and parsed before it was encountered
3189 // normally in the stream), or there were no function blocks to
3190 // trigger an earlier parsing of the VST.
3191 assert(VSTOffset == 0 || FunctionsWithBodies.empty());
3192 if (Error Err = parseValueSymbolTable())
3194 SeenValueSymbolTable = true;
3196 // We must have had a VST forward declaration record, which caused
3197 // the parser to jump to and parse the VST earlier.
3198 assert(VSTOffset > 0);
3199 if (Stream.SkipBlock())
3200 return error("Invalid record");
3203 case bitc::CONSTANTS_BLOCK_ID:
3204 if (Error Err = parseConstants())
3206 if (Error Err = resolveGlobalAndIndirectSymbolInits())
3209 case bitc::METADATA_BLOCK_ID:
3210 if (ShouldLazyLoadMetadata) {
3211 if (Error Err = rememberAndSkipMetadata())
3215 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
3216 if (Error Err = MDLoader->parseModuleMetadata())
3219 case bitc::METADATA_KIND_BLOCK_ID:
3220 if (Error Err = MDLoader->parseMetadataKinds())
3223 case bitc::FUNCTION_BLOCK_ID:
3224 // If this is the first function body we've seen, reverse the
3225 // FunctionsWithBodies list.
3226 if (!SeenFirstFunctionBody) {
3227 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
3228 if (Error Err = globalCleanup())
3230 SeenFirstFunctionBody = true;
3233 if (VSTOffset > 0) {
3234 // If we have a VST forward declaration record, make sure we
3235 // parse the VST now if we haven't already. It is needed to
3236 // set up the DeferredFunctionInfo vector for lazy reading.
3237 if (!SeenValueSymbolTable) {
3238 if (Error Err = BitcodeReader::parseValueSymbolTable(VSTOffset))
3240 SeenValueSymbolTable = true;
3241 // Fall through so that we record the NextUnreadBit below.
3242 // This is necessary in case we have an anonymous function that
3243 // is later materialized. Since it will not have a VST entry we
3244 // need to fall back to the lazy parse to find its offset.
3246 // If we have a VST forward declaration record, but have already
3247 // parsed the VST (just above, when the first function body was
3248 // encountered here), then we are resuming the parse after
3249 // materializing functions. The ResumeBit points to the
3250 // start of the last function block recorded in the
3251 // DeferredFunctionInfo map. Skip it.
3252 if (Stream.SkipBlock())
3253 return error("Invalid record");
3258 // Support older bitcode files that did not have the function
3259 // index in the VST, nor a VST forward declaration record, as
3260 // well as anonymous functions that do not have VST entries.
3261 // Build the DeferredFunctionInfo vector on the fly.
3262 if (Error Err = rememberAndSkipFunctionBody())
3265 // Suspend parsing when we reach the function bodies. Subsequent
3266 // materialization calls will resume it when necessary. If the bitcode
3267 // file is old, the symbol table will be at the end instead and will not
3268 // have been seen yet. In this case, just finish the parse now.
3269 if (SeenValueSymbolTable) {
3270 NextUnreadBit = Stream.GetCurrentBitNo();
3271 // After the VST has been parsed, we need to make sure intrinsic name
3272 // are auto-upgraded.
3273 return globalCleanup();
3276 case bitc::USELIST_BLOCK_ID:
3277 if (Error Err = parseUseLists())
3280 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
3281 if (Error Err = parseOperandBundleTags())
3284 case bitc::SYNC_SCOPE_NAMES_BLOCK_ID:
3285 if (Error Err = parseSyncScopeNames())
3291 case BitstreamEntry::Record:
3292 // The interesting case.
3297 auto BitCode = Stream.readRecord(Entry.ID, Record);
3299 default: break; // Default behavior, ignore unknown content.
3300 case bitc::MODULE_CODE_VERSION: {
3301 Expected<unsigned> VersionOrErr = parseVersionRecord(Record);
3303 return VersionOrErr.takeError();
3304 UseRelativeIDs = *VersionOrErr >= 1;
3307 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
3309 if (convertToString(Record, 0, S))
3310 return error("Invalid record");
3311 TheModule->setTargetTriple(S);
3314 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
3316 if (convertToString(Record, 0, S))
3317 return error("Invalid record");
3318 TheModule->setDataLayout(S);
3321 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
3323 if (convertToString(Record, 0, S))
3324 return error("Invalid record");
3325 TheModule->setModuleInlineAsm(S);
3328 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
3329 // FIXME: Remove in 4.0.
3331 if (convertToString(Record, 0, S))
3332 return error("Invalid record");
3336 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
3338 if (convertToString(Record, 0, S))
3339 return error("Invalid record");
3340 SectionTable.push_back(S);
3343 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
3345 if (convertToString(Record, 0, S))
3346 return error("Invalid record");
3347 GCTable.push_back(S);
3350 case bitc::MODULE_CODE_COMDAT:
3351 if (Error Err = parseComdatRecord(Record))
3354 case bitc::MODULE_CODE_GLOBALVAR:
3355 if (Error Err = parseGlobalVarRecord(Record))
3358 case bitc::MODULE_CODE_FUNCTION:
3359 if (Error Err = parseFunctionRecord(Record))
3362 case bitc::MODULE_CODE_IFUNC:
3363 case bitc::MODULE_CODE_ALIAS:
3364 case bitc::MODULE_CODE_ALIAS_OLD:
3365 if (Error Err = parseGlobalIndirectSymbolRecord(BitCode, Record))
3368 /// MODULE_CODE_VSTOFFSET: [offset]
3369 case bitc::MODULE_CODE_VSTOFFSET:
3370 if (Record.size() < 1)
3371 return error("Invalid record");
3372 // Note that we subtract 1 here because the offset is relative to one word
3373 // before the start of the identification or module block, which was
3374 // historically always the start of the regular bitcode header.
3375 VSTOffset = Record[0] - 1;
3377 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
3378 case bitc::MODULE_CODE_SOURCE_FILENAME:
3379 SmallString<128> ValueName;
3380 if (convertToString(Record, 0, ValueName))
3381 return error("Invalid record");
3382 TheModule->setSourceFileName(ValueName);
3389 Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata,
3392 MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting,
3393 [&](unsigned ID) { return getTypeByID(ID); });
3394 return parseModule(0, ShouldLazyLoadMetadata);
3397 Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
3398 if (!isa<PointerType>(PtrType))
3399 return error("Load/Store operand is not a pointer type");
3400 Type *ElemType = cast<PointerType>(PtrType)->getElementType();
3402 if (ValType && ValType != ElemType)
3403 return error("Explicit load/store type does not match pointee "
3404 "type of pointer operand");
3405 if (!PointerType::isLoadableOrStorableType(ElemType))
3406 return error("Cannot load/store from pointer");
3407 return Error::success();
3410 /// Lazily parse the specified function body block.
3411 Error BitcodeReader::parseFunctionBody(Function *F) {
3412 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
3413 return error("Invalid record");
3415 // Unexpected unresolved metadata when parsing function.
3416 if (MDLoader->hasFwdRefs())
3417 return error("Invalid function metadata: incoming forward references");
3419 InstructionList.clear();
3420 unsigned ModuleValueListSize = ValueList.size();
3421 unsigned ModuleMDLoaderSize = MDLoader->size();
3423 // Add all the function arguments to the value table.
3424 for (Argument &I : F->args())
3425 ValueList.push_back(&I);
3427 unsigned NextValueNo = ValueList.size();
3428 BasicBlock *CurBB = nullptr;
3429 unsigned CurBBNo = 0;
3432 auto getLastInstruction = [&]() -> Instruction * {
3433 if (CurBB && !CurBB->empty())
3434 return &CurBB->back();
3435 else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
3436 !FunctionBBs[CurBBNo - 1]->empty())
3437 return &FunctionBBs[CurBBNo - 1]->back();
3441 std::vector<OperandBundleDef> OperandBundles;
3443 // Read all the records.
3444 SmallVector<uint64_t, 64> Record;
3447 BitstreamEntry Entry = Stream.advance();
3449 switch (Entry.Kind) {
3450 case BitstreamEntry::Error:
3451 return error("Malformed block");
3452 case BitstreamEntry::EndBlock:
3453 goto OutOfRecordLoop;
3455 case BitstreamEntry::SubBlock:
3457 default: // Skip unknown content.
3458 if (Stream.SkipBlock())
3459 return error("Invalid record");
3461 case bitc::CONSTANTS_BLOCK_ID:
3462 if (Error Err = parseConstants())
3464 NextValueNo = ValueList.size();
3466 case bitc::VALUE_SYMTAB_BLOCK_ID:
3467 if (Error Err = parseValueSymbolTable())
3470 case bitc::METADATA_ATTACHMENT_ID:
3471 if (Error Err = MDLoader->parseMetadataAttachment(*F, InstructionList))
3474 case bitc::METADATA_BLOCK_ID:
3475 assert(DeferredMetadataInfo.empty() &&
3476 "Must read all module-level metadata before function-level");
3477 if (Error Err = MDLoader->parseFunctionMetadata())
3480 case bitc::USELIST_BLOCK_ID:
3481 if (Error Err = parseUseLists())
3487 case BitstreamEntry::Record:
3488 // The interesting case.
3494 Instruction *I = nullptr;
3495 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
3497 default: // Default behavior: reject
3498 return error("Invalid value");
3499 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
3500 if (Record.size() < 1 || Record[0] == 0)
3501 return error("Invalid record");
3502 // Create all the basic blocks for the function.
3503 FunctionBBs.resize(Record[0]);
3505 // See if anything took the address of blocks in this function.
3506 auto BBFRI = BasicBlockFwdRefs.find(F);
3507 if (BBFRI == BasicBlockFwdRefs.end()) {
3508 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
3509 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
3511 auto &BBRefs = BBFRI->second;
3512 // Check for invalid basic block references.
3513 if (BBRefs.size() > FunctionBBs.size())
3514 return error("Invalid ID");
3515 assert(!BBRefs.empty() && "Unexpected empty array");
3516 assert(!BBRefs.front() && "Invalid reference to entry block");
3517 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
3519 if (I < RE && BBRefs[I]) {
3520 BBRefs[I]->insertInto(F);
3521 FunctionBBs[I] = BBRefs[I];
3523 FunctionBBs[I] = BasicBlock::Create(Context, "", F);
3526 // Erase from the table.
3527 BasicBlockFwdRefs.erase(BBFRI);
3530 CurBB = FunctionBBs[0];
3534 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
3535 // This record indicates that the last instruction is at the same
3536 // location as the previous instruction with a location.
3537 I = getLastInstruction();
3540 return error("Invalid record");
3541 I->setDebugLoc(LastLoc);
3545 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
3546 I = getLastInstruction();
3547 if (!I || Record.size() < 4)
3548 return error("Invalid record");
3550 unsigned Line = Record[0], Col = Record[1];
3551 unsigned ScopeID = Record[2], IAID = Record[3];
3552 bool isImplicitCode = Record.size() == 5 && Record[4];
3554 MDNode *Scope = nullptr, *IA = nullptr;
3556 Scope = dyn_cast_or_null<MDNode>(
3557 MDLoader->getMetadataFwdRefOrLoad(ScopeID - 1));
3559 return error("Invalid record");
3562 IA = dyn_cast_or_null<MDNode>(
3563 MDLoader->getMetadataFwdRefOrLoad(IAID - 1));
3565 return error("Invalid record");
3567 LastLoc = DebugLoc::get(Line, Col, Scope, IA, isImplicitCode);
3568 I->setDebugLoc(LastLoc);
3572 case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode]
3575 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
3576 OpNum+1 > Record.size())
3577 return error("Invalid record");
3579 int Opc = getDecodedUnaryOpcode(Record[OpNum++], LHS->getType());
3581 return error("Invalid record");
3582 I = UnaryOperator::Create((Instruction::UnaryOps)Opc, LHS);
3583 InstructionList.push_back(I);
3584 if (OpNum < Record.size()) {
3585 if (isa<FPMathOperator>(I)) {
3586 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
3588 I->setFastMathFlags(FMF);
3593 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
3596 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
3597 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
3598 OpNum+1 > Record.size())
3599 return error("Invalid record");
3601 int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
3603 return error("Invalid record");
3604 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3605 InstructionList.push_back(I);
3606 if (OpNum < Record.size()) {
3607 if (Opc == Instruction::Add ||
3608 Opc == Instruction::Sub ||
3609 Opc == Instruction::Mul ||
3610 Opc == Instruction::Shl) {
3611 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
3612 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
3613 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
3614 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
3615 } else if (Opc == Instruction::SDiv ||
3616 Opc == Instruction::UDiv ||
3617 Opc == Instruction::LShr ||
3618 Opc == Instruction::AShr) {
3619 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
3620 cast<BinaryOperator>(I)->setIsExact(true);
3621 } else if (isa<FPMathOperator>(I)) {
3622 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
3624 I->setFastMathFlags(FMF);
3630 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
3633 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
3634 OpNum+2 != Record.size())
3635 return error("Invalid record");
3637 Type *ResTy = getTypeByID(Record[OpNum]);
3638 int Opc = getDecodedCastOpcode(Record[OpNum + 1]);
3639 if (Opc == -1 || !ResTy)
3640 return error("Invalid record");
3641 Instruction *Temp = nullptr;
3642 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
3644 InstructionList.push_back(Temp);
3645 CurBB->getInstList().push_back(Temp);
3648 auto CastOp = (Instruction::CastOps)Opc;
3649 if (!CastInst::castIsValid(CastOp, Op, ResTy))
3650 return error("Invalid cast");
3651 I = CastInst::Create(CastOp, Op, ResTy);
3653 InstructionList.push_back(I);
3656 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
3657 case bitc::FUNC_CODE_INST_GEP_OLD:
3658 case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
3664 if (BitCode == bitc::FUNC_CODE_INST_GEP) {
3665 InBounds = Record[OpNum++];
3666 Ty = getTypeByID(Record[OpNum++]);
3668 InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
3673 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
3674 return error("Invalid record");
3677 Ty = cast<PointerType>(BasePtr->getType()->getScalarType())
3680 cast<PointerType>(BasePtr->getType()->getScalarType())
3683 "Explicit gep type does not match pointee type of pointer operand");
3685 SmallVector<Value*, 16> GEPIdx;
3686 while (OpNum != Record.size()) {
3688 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
3689 return error("Invalid record");
3690 GEPIdx.push_back(Op);
3693 I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
3695 InstructionList.push_back(I);
3697 cast<GetElementPtrInst>(I)->setIsInBounds(true);
3701 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
3702 // EXTRACTVAL: [opty, opval, n x indices]
3705 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
3706 return error("Invalid record");
3708 unsigned RecSize = Record.size();
3709 if (OpNum == RecSize)
3710 return error("EXTRACTVAL: Invalid instruction with 0 indices");
3712 SmallVector<unsigned, 4> EXTRACTVALIdx;
3713 Type *CurTy = Agg->getType();
3714 for (; OpNum != RecSize; ++OpNum) {
3715 bool IsArray = CurTy->isArrayTy();
3716 bool IsStruct = CurTy->isStructTy();
3717 uint64_t Index = Record[OpNum];
3719 if (!IsStruct && !IsArray)
3720 return error("EXTRACTVAL: Invalid type");
3721 if ((unsigned)Index != Index)
3722 return error("Invalid value");
3723 if (IsStruct && Index >= CurTy->getStructNumElements())
3724 return error("EXTRACTVAL: Invalid struct index");
3725 if (IsArray && Index >= CurTy->getArrayNumElements())
3726 return error("EXTRACTVAL: Invalid array index");
3727 EXTRACTVALIdx.push_back((unsigned)Index);
3730 CurTy = CurTy->getStructElementType(Index);
3732 CurTy = CurTy->getArrayElementType();
3735 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
3736 InstructionList.push_back(I);
3740 case bitc::FUNC_CODE_INST_INSERTVAL: {
3741 // INSERTVAL: [opty, opval, opty, opval, n x indices]
3744 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
3745 return error("Invalid record");
3747 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
3748 return error("Invalid record");
3750 unsigned RecSize = Record.size();
3751 if (OpNum == RecSize)
3752 return error("INSERTVAL: Invalid instruction with 0 indices");
3754 SmallVector<unsigned, 4> INSERTVALIdx;
3755 Type *CurTy = Agg->getType();
3756 for (; OpNum != RecSize; ++OpNum) {
3757 bool IsArray = CurTy->isArrayTy();
3758 bool IsStruct = CurTy->isStructTy();
3759 uint64_t Index = Record[OpNum];
3761 if (!IsStruct && !IsArray)
3762 return error("INSERTVAL: Invalid type");
3763 if ((unsigned)Index != Index)
3764 return error("Invalid value");
3765 if (IsStruct && Index >= CurTy->getStructNumElements())
3766 return error("INSERTVAL: Invalid struct index");
3767 if (IsArray && Index >= CurTy->getArrayNumElements())
3768 return error("INSERTVAL: Invalid array index");
3770 INSERTVALIdx.push_back((unsigned)Index);
3772 CurTy = CurTy->getStructElementType(Index);
3774 CurTy = CurTy->getArrayElementType();
3777 if (CurTy != Val->getType())
3778 return error("Inserted value type doesn't match aggregate type");
3780 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
3781 InstructionList.push_back(I);
3785 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
3786 // obsolete form of select
3787 // handles select i1 ... in old bitcode
3789 Value *TrueVal, *FalseVal, *Cond;
3790 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
3791 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
3792 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
3793 return error("Invalid record");
3795 I = SelectInst::Create(Cond, TrueVal, FalseVal);
3796 InstructionList.push_back(I);
3800 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
3801 // new form of select
3802 // handles select i1 or select [N x i1]
3804 Value *TrueVal, *FalseVal, *Cond;
3805 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
3806 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
3807 getValueTypePair(Record, OpNum, NextValueNo, Cond))
3808 return error("Invalid record");
3810 // select condition can be either i1 or [N x i1]
3811 if (VectorType* vector_type =
3812 dyn_cast<VectorType>(Cond->getType())) {
3814 if (vector_type->getElementType() != Type::getInt1Ty(Context))
3815 return error("Invalid type for value");
3818 if (Cond->getType() != Type::getInt1Ty(Context))
3819 return error("Invalid type for value");
3822 I = SelectInst::Create(Cond, TrueVal, FalseVal);
3823 InstructionList.push_back(I);
3827 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
3830 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
3831 getValueTypePair(Record, OpNum, NextValueNo, Idx))
3832 return error("Invalid record");
3833 if (!Vec->getType()->isVectorTy())
3834 return error("Invalid type for value");
3835 I = ExtractElementInst::Create(Vec, Idx);
3836 InstructionList.push_back(I);
3840 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
3842 Value *Vec, *Elt, *Idx;
3843 if (getValueTypePair(Record, OpNum, NextValueNo, Vec))
3844 return error("Invalid record");
3845 if (!Vec->getType()->isVectorTy())
3846 return error("Invalid type for value");
3847 if (popValue(Record, OpNum, NextValueNo,
3848 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
3849 getValueTypePair(Record, OpNum, NextValueNo, Idx))
3850 return error("Invalid record");
3851 I = InsertElementInst::Create(Vec, Elt, Idx);
3852 InstructionList.push_back(I);
3856 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
3858 Value *Vec1, *Vec2, *Mask;
3859 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
3860 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
3861 return error("Invalid record");
3863 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
3864 return error("Invalid record");
3865 if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy())
3866 return error("Invalid type for value");
3867 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
3868 InstructionList.push_back(I);
3872 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
3873 // Old form of ICmp/FCmp returning bool
3874 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
3875 // both legal on vectors but had different behaviour.
3876 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
3877 // FCmp/ICmp returning bool or vector of bool
3881 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
3882 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS))
3883 return error("Invalid record");
3885 unsigned PredVal = Record[OpNum];
3886 bool IsFP = LHS->getType()->isFPOrFPVectorTy();
3888 if (IsFP && Record.size() > OpNum+1)
3889 FMF = getDecodedFastMathFlags(Record[++OpNum]);
3891 if (OpNum+1 != Record.size())
3892 return error("Invalid record");
3894 if (LHS->getType()->isFPOrFPVectorTy())
3895 I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS);
3897 I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS);
3900 I->setFastMathFlags(FMF);
3901 InstructionList.push_back(I);
3905 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
3907 unsigned Size = Record.size();
3909 I = ReturnInst::Create(Context);
3910 InstructionList.push_back(I);
3915 Value *Op = nullptr;
3916 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
3917 return error("Invalid record");
3918 if (OpNum != Record.size())
3919 return error("Invalid record");
3921 I = ReturnInst::Create(Context, Op);
3922 InstructionList.push_back(I);
3925 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
3926 if (Record.size() != 1 && Record.size() != 3)
3927 return error("Invalid record");
3928 BasicBlock *TrueDest = getBasicBlock(Record[0]);
3930 return error("Invalid record");
3932 if (Record.size() == 1) {
3933 I = BranchInst::Create(TrueDest);
3934 InstructionList.push_back(I);
3937 BasicBlock *FalseDest = getBasicBlock(Record[1]);
3938 Value *Cond = getValue(Record, 2, NextValueNo,
3939 Type::getInt1Ty(Context));
3940 if (!FalseDest || !Cond)
3941 return error("Invalid record");
3942 I = BranchInst::Create(TrueDest, FalseDest, Cond);
3943 InstructionList.push_back(I);
3947 case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#]
3948 if (Record.size() != 1 && Record.size() != 2)
3949 return error("Invalid record");
3952 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
3954 return error("Invalid record");
3955 BasicBlock *UnwindDest = nullptr;
3956 if (Record.size() == 2) {
3957 UnwindDest = getBasicBlock(Record[Idx++]);
3959 return error("Invalid record");
3962 I = CleanupReturnInst::Create(CleanupPad, UnwindDest);
3963 InstructionList.push_back(I);
3966 case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#]
3967 if (Record.size() != 2)
3968 return error("Invalid record");
3971 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
3973 return error("Invalid record");
3974 BasicBlock *BB = getBasicBlock(Record[Idx++]);
3976 return error("Invalid record");
3978 I = CatchReturnInst::Create(CatchPad, BB);
3979 InstructionList.push_back(I);
3982 case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
3983 // We must have, at minimum, the outer scope and the number of arguments.
3984 if (Record.size() < 2)
3985 return error("Invalid record");
3990 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
3992 unsigned NumHandlers = Record[Idx++];
3994 SmallVector<BasicBlock *, 2> Handlers;
3995 for (unsigned Op = 0; Op != NumHandlers; ++Op) {
3996 BasicBlock *BB = getBasicBlock(Record[Idx++]);
3998 return error("Invalid record");
3999 Handlers.push_back(BB);
4002 BasicBlock *UnwindDest = nullptr;
4003 if (Idx + 1 == Record.size()) {
4004 UnwindDest = getBasicBlock(Record[Idx++]);
4006 return error("Invalid record");
4009 if (Record.size() != Idx)
4010 return error("Invalid record");
4013 CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers);
4014 for (BasicBlock *Handler : Handlers)
4015 CatchSwitch->addHandler(Handler);
4017 InstructionList.push_back(I);
4020 case bitc::FUNC_CODE_INST_CATCHPAD:
4021 case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*]
4022 // We must have, at minimum, the outer scope and the number of arguments.
4023 if (Record.size() < 2)
4024 return error("Invalid record");
4029 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4031 unsigned NumArgOperands = Record[Idx++];
4033 SmallVector<Value *, 2> Args;
4034 for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
4036 if (getValueTypePair(Record, Idx, NextValueNo, Val))
4037 return error("Invalid record");
4038 Args.push_back(Val);
4041 if (Record.size() != Idx)
4042 return error("Invalid record");
4044 if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD)
4045 I = CleanupPadInst::Create(ParentPad, Args);
4047 I = CatchPadInst::Create(ParentPad, Args);
4048 InstructionList.push_back(I);
4051 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
4053 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
4054 // "New" SwitchInst format with case ranges. The changes to write this
4055 // format were reverted but we still recognize bitcode that uses it.
4056 // Hopefully someday we will have support for case ranges and can use
4057 // this format again.
4059 Type *OpTy = getTypeByID(Record[1]);
4060 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
4062 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
4063 BasicBlock *Default = getBasicBlock(Record[3]);
4064 if (!OpTy || !Cond || !Default)
4065 return error("Invalid record");
4067 unsigned NumCases = Record[4];
4069 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4070 InstructionList.push_back(SI);
4072 unsigned CurIdx = 5;
4073 for (unsigned i = 0; i != NumCases; ++i) {
4074 SmallVector<ConstantInt*, 1> CaseVals;
4075 unsigned NumItems = Record[CurIdx++];
4076 for (unsigned ci = 0; ci != NumItems; ++ci) {
4077 bool isSingleNumber = Record[CurIdx++];
4080 unsigned ActiveWords = 1;
4081 if (ValueBitWidth > 64)
4082 ActiveWords = Record[CurIdx++];
4083 Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
4085 CurIdx += ActiveWords;
4087 if (!isSingleNumber) {
4089 if (ValueBitWidth > 64)
4090 ActiveWords = Record[CurIdx++];
4091 APInt High = readWideAPInt(
4092 makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth);
4093 CurIdx += ActiveWords;
4095 // FIXME: It is not clear whether values in the range should be
4096 // compared as signed or unsigned values. The partially
4097 // implemented changes that used this format in the past used
4098 // unsigned comparisons.
4099 for ( ; Low.ule(High); ++Low)
4100 CaseVals.push_back(ConstantInt::get(Context, Low));
4102 CaseVals.push_back(ConstantInt::get(Context, Low));
4104 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
4105 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
4106 cve = CaseVals.end(); cvi != cve; ++cvi)
4107 SI->addCase(*cvi, DestBB);
4113 // Old SwitchInst format without case ranges.
4115 if (Record.size() < 3 || (Record.size() & 1) == 0)
4116 return error("Invalid record");
4117 Type *OpTy = getTypeByID(Record[0]);
4118 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
4119 BasicBlock *Default = getBasicBlock(Record[2]);
4120 if (!OpTy || !Cond || !Default)
4121 return error("Invalid record");
4122 unsigned NumCases = (Record.size()-3)/2;
4123 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4124 InstructionList.push_back(SI);
4125 for (unsigned i = 0, e = NumCases; i != e; ++i) {
4126 ConstantInt *CaseVal =
4127 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
4128 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
4129 if (!CaseVal || !DestBB) {
4131 return error("Invalid record");
4133 SI->addCase(CaseVal, DestBB);
4138 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
4139 if (Record.size() < 2)
4140 return error("Invalid record");
4141 Type *OpTy = getTypeByID(Record[0]);
4142 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
4143 if (!OpTy || !Address)
4144 return error("Invalid record");
4145 unsigned NumDests = Record.size()-2;
4146 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
4147 InstructionList.push_back(IBI);
4148 for (unsigned i = 0, e = NumDests; i != e; ++i) {
4149 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
4150 IBI->addDestination(DestBB);
4153 return error("Invalid record");
4160 case bitc::FUNC_CODE_INST_INVOKE: {
4161 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
4162 if (Record.size() < 4)
4163 return error("Invalid record");
4165 AttributeList PAL = getAttributes(Record[OpNum++]);
4166 unsigned CCInfo = Record[OpNum++];
4167 BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]);
4168 BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]);
4170 FunctionType *FTy = nullptr;
4171 if (CCInfo >> 13 & 1 &&
4172 !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
4173 return error("Explicit invoke type is not a function type");
4176 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
4177 return error("Invalid record");
4179 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
4181 return error("Callee is not a pointer");
4183 FTy = dyn_cast<FunctionType>(CalleeTy->getElementType());
4185 return error("Callee is not of pointer to function type");
4186 } else if (CalleeTy->getElementType() != FTy)
4187 return error("Explicit invoke type does not match pointee type of "
4189 if (Record.size() < FTy->getNumParams() + OpNum)
4190 return error("Insufficient operands to call");
4192 SmallVector<Value*, 16> Ops;
4193 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
4194 Ops.push_back(getValue(Record, OpNum, NextValueNo,
4195 FTy->getParamType(i)));
4197 return error("Invalid record");
4200 if (!FTy->isVarArg()) {
4201 if (Record.size() != OpNum)
4202 return error("Invalid record");
4204 // Read type/value pairs for varargs params.
4205 while (OpNum != Record.size()) {
4207 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4208 return error("Invalid record");
4213 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles);
4214 OperandBundles.clear();
4215 InstructionList.push_back(I);
4216 cast<InvokeInst>(I)->setCallingConv(
4217 static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));
4218 cast<InvokeInst>(I)->setAttributes(PAL);
4221 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
4223 Value *Val = nullptr;
4224 if (getValueTypePair(Record, Idx, NextValueNo, Val))
4225 return error("Invalid record");
4226 I = ResumeInst::Create(Val);
4227 InstructionList.push_back(I);
4230 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
4231 I = new UnreachableInst(Context);
4232 InstructionList.push_back(I);
4234 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
4235 if (Record.size() < 1 || ((Record.size()-1)&1))
4236 return error("Invalid record");
4237 Type *Ty = getTypeByID(Record[0]);
4239 return error("Invalid record");
4241 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
4242 InstructionList.push_back(PN);
4244 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
4246 // With the new function encoding, it is possible that operands have
4247 // negative IDs (for forward references). Use a signed VBR
4248 // representation to keep the encoding small.
4250 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
4252 V = getValue(Record, 1+i, NextValueNo, Ty);
4253 BasicBlock *BB = getBasicBlock(Record[2+i]);
4255 return error("Invalid record");
4256 PN->addIncoming(V, BB);
4262 case bitc::FUNC_CODE_INST_LANDINGPAD:
4263 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: {
4264 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
4266 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) {
4267 if (Record.size() < 3)
4268 return error("Invalid record");
4270 assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD);
4271 if (Record.size() < 4)
4272 return error("Invalid record");
4274 Type *Ty = getTypeByID(Record[Idx++]);
4276 return error("Invalid record");
4277 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) {
4278 Value *PersFn = nullptr;
4279 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
4280 return error("Invalid record");
4282 if (!F->hasPersonalityFn())
4283 F->setPersonalityFn(cast<Constant>(PersFn));
4284 else if (F->getPersonalityFn() != cast<Constant>(PersFn))
4285 return error("Personality function mismatch");
4288 bool IsCleanup = !!Record[Idx++];
4289 unsigned NumClauses = Record[Idx++];
4290 LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses);
4291 LP->setCleanup(IsCleanup);
4292 for (unsigned J = 0; J != NumClauses; ++J) {
4293 LandingPadInst::ClauseType CT =
4294 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
4297 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
4299 return error("Invalid record");
4302 assert((CT != LandingPadInst::Catch ||
4303 !isa<ArrayType>(Val->getType())) &&
4304 "Catch clause has a invalid type!");
4305 assert((CT != LandingPadInst::Filter ||
4306 isa<ArrayType>(Val->getType())) &&
4307 "Filter clause has invalid type!");
4308 LP->addClause(cast<Constant>(Val));
4312 InstructionList.push_back(I);
4316 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
4317 if (Record.size() != 4)
4318 return error("Invalid record");
4319 uint64_t AlignRecord = Record[3];
4320 const uint64_t InAllocaMask = uint64_t(1) << 5;
4321 const uint64_t ExplicitTypeMask = uint64_t(1) << 6;
4322 const uint64_t SwiftErrorMask = uint64_t(1) << 7;
4323 const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask |
4325 bool InAlloca = AlignRecord & InAllocaMask;
4326 bool SwiftError = AlignRecord & SwiftErrorMask;
4327 Type *Ty = getTypeByID(Record[0]);
4328 if ((AlignRecord & ExplicitTypeMask) == 0) {
4329 auto *PTy = dyn_cast_or_null<PointerType>(Ty);
4331 return error("Old-style alloca with a non-pointer type");
4332 Ty = PTy->getElementType();
4334 Type *OpTy = getTypeByID(Record[1]);
4335 Value *Size = getFnValueByID(Record[2], OpTy);
4337 if (Error Err = parseAlignmentValue(AlignRecord & ~FlagMask, Align)) {
4341 return error("Invalid record");
4343 // FIXME: Make this an optional field.
4344 const DataLayout &DL = TheModule->getDataLayout();
4345 unsigned AS = DL.getAllocaAddrSpace();
4347 AllocaInst *AI = new AllocaInst(Ty, AS, Size, Align);
4348 AI->setUsedWithInAlloca(InAlloca);
4349 AI->setSwiftError(SwiftError);
4351 InstructionList.push_back(I);
4354 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
4357 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
4358 (OpNum + 2 != Record.size() && OpNum + 3 != Record.size()))
4359 return error("Invalid record");
4362 if (OpNum + 3 == Record.size())
4363 Ty = getTypeByID(Record[OpNum++]);
4364 if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType()))
4367 Ty = cast<PointerType>(Op->getType())->getElementType();
4370 if (Error Err = parseAlignmentValue(Record[OpNum], Align))
4372 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align);
4374 InstructionList.push_back(I);
4377 case bitc::FUNC_CODE_INST_LOADATOMIC: {
4378 // LOADATOMIC: [opty, op, align, vol, ordering, ssid]
4381 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
4382 (OpNum + 4 != Record.size() && OpNum + 5 != Record.size()))
4383 return error("Invalid record");
4386 if (OpNum + 5 == Record.size())
4387 Ty = getTypeByID(Record[OpNum++]);
4388 if (Error Err = typeCheckLoadStoreInst(Ty, Op->getType()))
4391 Ty = cast<PointerType>(Op->getType())->getElementType();
4393 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
4394 if (Ordering == AtomicOrdering::NotAtomic ||
4395 Ordering == AtomicOrdering::Release ||
4396 Ordering == AtomicOrdering::AcquireRelease)
4397 return error("Invalid record");
4398 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)
4399 return error("Invalid record");
4400 SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);
4403 if (Error Err = parseAlignmentValue(Record[OpNum], Align))
4405 I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SSID);
4407 InstructionList.push_back(I);
4410 case bitc::FUNC_CODE_INST_STORE:
4411 case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol]
4414 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
4415 (BitCode == bitc::FUNC_CODE_INST_STORE
4416 ? getValueTypePair(Record, OpNum, NextValueNo, Val)
4417 : popValue(Record, OpNum, NextValueNo,
4418 cast<PointerType>(Ptr->getType())->getElementType(),
4420 OpNum + 2 != Record.size())
4421 return error("Invalid record");
4423 if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
4426 if (Error Err = parseAlignmentValue(Record[OpNum], Align))
4428 I = new StoreInst(Val, Ptr, Record[OpNum+1], Align);
4429 InstructionList.push_back(I);
4432 case bitc::FUNC_CODE_INST_STOREATOMIC:
4433 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: {
4434 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, ssid]
4437 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
4438 !isa<PointerType>(Ptr->getType()) ||
4439 (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC
4440 ? getValueTypePair(Record, OpNum, NextValueNo, Val)
4441 : popValue(Record, OpNum, NextValueNo,
4442 cast<PointerType>(Ptr->getType())->getElementType(),
4444 OpNum + 4 != Record.size())
4445 return error("Invalid record");
4447 if (Error Err = typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
4449 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
4450 if (Ordering == AtomicOrdering::NotAtomic ||
4451 Ordering == AtomicOrdering::Acquire ||
4452 Ordering == AtomicOrdering::AcquireRelease)
4453 return error("Invalid record");
4454 SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);
4455 if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)
4456 return error("Invalid record");
4459 if (Error Err = parseAlignmentValue(Record[OpNum], Align))
4461 I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SSID);
4462 InstructionList.push_back(I);
4465 case bitc::FUNC_CODE_INST_CMPXCHG_OLD:
4466 case bitc::FUNC_CODE_INST_CMPXCHG: {
4467 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, ssid,
4468 // failureordering?, isweak?]
4470 Value *Ptr, *Cmp, *New;
4471 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
4472 (BitCode == bitc::FUNC_CODE_INST_CMPXCHG
4473 ? getValueTypePair(Record, OpNum, NextValueNo, Cmp)
4474 : popValue(Record, OpNum, NextValueNo,
4475 cast<PointerType>(Ptr->getType())->getElementType(),
4477 popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) ||
4478 Record.size() < OpNum + 3 || Record.size() > OpNum + 5)
4479 return error("Invalid record");
4480 AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]);
4481 if (SuccessOrdering == AtomicOrdering::NotAtomic ||
4482 SuccessOrdering == AtomicOrdering::Unordered)
4483 return error("Invalid record");
4484 SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 2]);
4486 if (Error Err = typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType()))
4488 AtomicOrdering FailureOrdering;
4489 if (Record.size() < 7)
4491 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
4493 FailureOrdering = getDecodedOrdering(Record[OpNum + 3]);
4495 I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
4497 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
4499 if (Record.size() < 8) {
4500 // Before weak cmpxchgs existed, the instruction simply returned the
4501 // value loaded from memory, so bitcode files from that era will be
4502 // expecting the first component of a modern cmpxchg.
4503 CurBB->getInstList().push_back(I);
4504 I = ExtractValueInst::Create(I, 0);
4506 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
4509 InstructionList.push_back(I);
4512 case bitc::FUNC_CODE_INST_ATOMICRMW: {
4513 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, ssid]
4516 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
4517 !isa<PointerType>(Ptr->getType()) ||
4518 popValue(Record, OpNum, NextValueNo,
4519 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
4520 OpNum+4 != Record.size())
4521 return error("Invalid record");
4522 AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]);
4523 if (Operation < AtomicRMWInst::FIRST_BINOP ||
4524 Operation > AtomicRMWInst::LAST_BINOP)
4525 return error("Invalid record");
4526 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
4527 if (Ordering == AtomicOrdering::NotAtomic ||
4528 Ordering == AtomicOrdering::Unordered)
4529 return error("Invalid record");
4530 SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]);
4531 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SSID);
4532 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
4533 InstructionList.push_back(I);
4536 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, ssid]
4537 if (2 != Record.size())
4538 return error("Invalid record");
4539 AtomicOrdering Ordering = getDecodedOrdering(Record[0]);
4540 if (Ordering == AtomicOrdering::NotAtomic ||
4541 Ordering == AtomicOrdering::Unordered ||
4542 Ordering == AtomicOrdering::Monotonic)
4543 return error("Invalid record");
4544 SyncScope::ID SSID = getDecodedSyncScopeID(Record[1]);
4545 I = new FenceInst(Context, Ordering, SSID);
4546 InstructionList.push_back(I);
4549 case bitc::FUNC_CODE_INST_CALL: {
4550 // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]
4551 if (Record.size() < 3)
4552 return error("Invalid record");
4555 AttributeList PAL = getAttributes(Record[OpNum++]);
4556 unsigned CCInfo = Record[OpNum++];
4559 if ((CCInfo >> bitc::CALL_FMF) & 1) {
4560 FMF = getDecodedFastMathFlags(Record[OpNum++]);
4562 return error("Fast math flags indicator set for call with no FMF");
4565 FunctionType *FTy = nullptr;
4566 if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 &&
4567 !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
4568 return error("Explicit call type is not a function type");
4571 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
4572 return error("Invalid record");
4574 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
4576 return error("Callee is not a pointer type");
4578 FTy = dyn_cast<FunctionType>(OpTy->getElementType());
4580 return error("Callee is not of pointer to function type");
4581 } else if (OpTy->getElementType() != FTy)
4582 return error("Explicit call type does not match pointee type of "
4584 if (Record.size() < FTy->getNumParams() + OpNum)
4585 return error("Insufficient operands to call");
4587 SmallVector<Value*, 16> Args;
4588 // Read the fixed params.
4589 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
4590 if (FTy->getParamType(i)->isLabelTy())
4591 Args.push_back(getBasicBlock(Record[OpNum]));
4593 Args.push_back(getValue(Record, OpNum, NextValueNo,
4594 FTy->getParamType(i)));
4596 return error("Invalid record");
4599 // Read type/value pairs for varargs params.
4600 if (!FTy->isVarArg()) {
4601 if (OpNum != Record.size())
4602 return error("Invalid record");
4604 while (OpNum != Record.size()) {
4606 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4607 return error("Invalid record");
4612 I = CallInst::Create(FTy, Callee, Args, OperandBundles);
4613 OperandBundles.clear();
4614 InstructionList.push_back(I);
4615 cast<CallInst>(I)->setCallingConv(
4616 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
4617 CallInst::TailCallKind TCK = CallInst::TCK_None;
4618 if (CCInfo & 1 << bitc::CALL_TAIL)
4619 TCK = CallInst::TCK_Tail;
4620 if (CCInfo & (1 << bitc::CALL_MUSTTAIL))
4621 TCK = CallInst::TCK_MustTail;
4622 if (CCInfo & (1 << bitc::CALL_NOTAIL))
4623 TCK = CallInst::TCK_NoTail;
4624 cast<CallInst>(I)->setTailCallKind(TCK);
4625 cast<CallInst>(I)->setAttributes(PAL);
4627 if (!isa<FPMathOperator>(I))
4628 return error("Fast-math-flags specified for call without "
4629 "floating-point scalar or vector return type");
4630 I->setFastMathFlags(FMF);
4634 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
4635 if (Record.size() < 3)
4636 return error("Invalid record");
4637 Type *OpTy = getTypeByID(Record[0]);
4638 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
4639 Type *ResTy = getTypeByID(Record[2]);
4640 if (!OpTy || !Op || !ResTy)
4641 return error("Invalid record");
4642 I = new VAArgInst(Op, ResTy);
4643 InstructionList.push_back(I);
4647 case bitc::FUNC_CODE_OPERAND_BUNDLE: {
4648 // A call or an invoke can be optionally prefixed with some variable
4649 // number of operand bundle blocks. These blocks are read into
4650 // OperandBundles and consumed at the next call or invoke instruction.
4652 if (Record.size() < 1 || Record[0] >= BundleTags.size())
4653 return error("Invalid record");
4655 std::vector<Value *> Inputs;
4658 while (OpNum != Record.size()) {
4660 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4661 return error("Invalid record");
4662 Inputs.push_back(Op);
4665 OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs));
4670 // Add instruction to end of current BB. If there is no current BB, reject
4674 return error("Invalid instruction with no BB");
4676 if (!OperandBundles.empty()) {
4678 return error("Operand bundles found with no consumer");
4680 CurBB->getInstList().push_back(I);
4682 // If this was a terminator instruction, move to the next block.
4683 if (I->isTerminator()) {
4685 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
4688 // Non-void values get registered in the value table for future use.
4689 if (I && !I->getType()->isVoidTy())
4690 ValueList.assignValue(I, NextValueNo++);
4695 if (!OperandBundles.empty())
4696 return error("Operand bundles found with no consumer");
4698 // Check the function list for unresolved values.
4699 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
4700 if (!A->getParent()) {
4701 // We found at least one unresolved value. Nuke them all to avoid leaks.
4702 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
4703 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
4704 A->replaceAllUsesWith(UndefValue::get(A->getType()));
4708 return error("Never resolved value found in function");
4712 // Unexpected unresolved metadata about to be dropped.
4713 if (MDLoader->hasFwdRefs())
4714 return error("Invalid function metadata: outgoing forward refs");
4716 // Trim the value list down to the size it was before we parsed this function.
4717 ValueList.shrinkTo(ModuleValueListSize);
4718 MDLoader->shrinkTo(ModuleMDLoaderSize);
4719 std::vector<BasicBlock*>().swap(FunctionBBs);
4720 return Error::success();
4723 /// Find the function body in the bitcode stream
4724 Error BitcodeReader::findFunctionInStream(
4726 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
4727 while (DeferredFunctionInfoIterator->second == 0) {
4728 // This is the fallback handling for the old format bitcode that
4729 // didn't contain the function index in the VST, or when we have
4730 // an anonymous function which would not have a VST entry.
4731 // Assert that we have one of those two cases.
4732 assert(VSTOffset == 0 || !F->hasName());
4733 // Parse the next body in the stream and set its position in the
4734 // DeferredFunctionInfo map.
4735 if (Error Err = rememberAndSkipFunctionBodies())
4738 return Error::success();
4741 SyncScope::ID BitcodeReader::getDecodedSyncScopeID(unsigned Val) {
4742 if (Val == SyncScope::SingleThread || Val == SyncScope::System)
4743 return SyncScope::ID(Val);
4744 if (Val >= SSIDs.size())
4745 return SyncScope::System; // Map unknown synchronization scopes to system.
4749 //===----------------------------------------------------------------------===//
4750 // GVMaterializer implementation
4751 //===----------------------------------------------------------------------===//
4753 Error BitcodeReader::materialize(GlobalValue *GV) {
4754 Function *F = dyn_cast<Function>(GV);
4755 // If it's not a function or is already material, ignore the request.
4756 if (!F || !F->isMaterializable())
4757 return Error::success();
4759 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
4760 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
4761 // If its position is recorded as 0, its body is somewhere in the stream
4762 // but we haven't seen it yet.
4763 if (DFII->second == 0)
4764 if (Error Err = findFunctionInStream(F, DFII))
4767 // Materialize metadata before parsing any function bodies.
4768 if (Error Err = materializeMetadata())
4771 // Move the bit stream to the saved position of the deferred function body.
4772 Stream.JumpToBit(DFII->second);
4774 if (Error Err = parseFunctionBody(F))
4776 F->setIsMaterializable(false);
4781 // Upgrade any old intrinsic calls in the function.
4782 for (auto &I : UpgradedIntrinsics) {
4783 for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end();
4787 if (CallInst *CI = dyn_cast<CallInst>(U))
4788 UpgradeIntrinsicCall(CI, I.second);
4792 // Update calls to the remangled intrinsics
4793 for (auto &I : RemangledIntrinsics)
4794 for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end();
4796 // Don't expect any other users than call sites
4797 CallSite(*UI++).setCalledFunction(I.second);
4799 // Finish fn->subprogram upgrade for materialized functions.
4800 if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F))
4801 F->setSubprogram(SP);
4803 // Check if the TBAA Metadata are valid, otherwise we will need to strip them.
4804 if (!MDLoader->isStrippingTBAA()) {
4805 for (auto &I : instructions(F)) {
4806 MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa);
4807 if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA))
4809 MDLoader->setStripTBAA(true);
4810 stripTBAA(F->getParent());
4814 // Bring in any functions that this function forward-referenced via
4816 return materializeForwardReferencedFunctions();
4819 Error BitcodeReader::materializeModule() {
4820 if (Error Err = materializeMetadata())
4823 // Promise to materialize all forward references.
4824 WillMaterializeAllForwardRefs = true;
4826 // Iterate over the module, deserializing any functions that are still on
4828 for (Function &F : *TheModule) {
4829 if (Error Err = materialize(&F))
4832 // At this point, if there are any function bodies, parse the rest of
4833 // the bits in the module past the last function block we have recorded
4834 // through either lazy scanning or the VST.
4835 if (LastFunctionBlockBit || NextUnreadBit)
4836 if (Error Err = parseModule(LastFunctionBlockBit > NextUnreadBit
4837 ? LastFunctionBlockBit
4841 // Check that all block address forward references got resolved (as we
4843 if (!BasicBlockFwdRefs.empty())
4844 return error("Never resolved function from blockaddress");
4846 // Upgrade any intrinsic calls that slipped through (should not happen!) and
4847 // delete the old functions to clean up. We can't do this unless the entire
4848 // module is materialized because there could always be another function body
4849 // with calls to the old function.
4850 for (auto &I : UpgradedIntrinsics) {
4851 for (auto *U : I.first->users()) {
4852 if (CallInst *CI = dyn_cast<CallInst>(U))
4853 UpgradeIntrinsicCall(CI, I.second);
4855 if (!I.first->use_empty())
4856 I.first->replaceAllUsesWith(I.second);
4857 I.first->eraseFromParent();
4859 UpgradedIntrinsics.clear();
4860 // Do the same for remangled intrinsics
4861 for (auto &I : RemangledIntrinsics) {
4862 I.first->replaceAllUsesWith(I.second);
4863 I.first->eraseFromParent();
4865 RemangledIntrinsics.clear();
4867 UpgradeDebugInfo(*TheModule);
4869 UpgradeModuleFlags(*TheModule);
4871 UpgradeRetainReleaseMarker(*TheModule);
4873 return Error::success();
4876 std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
4877 return IdentifiedStructTypes;
4880 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
4881 BitstreamCursor Cursor, StringRef Strtab, ModuleSummaryIndex &TheIndex,
4882 StringRef ModulePath, unsigned ModuleId)
4883 : BitcodeReaderBase(std::move(Cursor), Strtab), TheIndex(TheIndex),
4884 ModulePath(ModulePath), ModuleId(ModuleId) {}
4886 void ModuleSummaryIndexBitcodeReader::addThisModule() {
4887 TheIndex.addModule(ModulePath, ModuleId);
4890 ModuleSummaryIndex::ModuleInfo *
4891 ModuleSummaryIndexBitcodeReader::getThisModule() {
4892 return TheIndex.getModule(ModulePath);
4895 std::pair<ValueInfo, GlobalValue::GUID>
4896 ModuleSummaryIndexBitcodeReader::getValueInfoFromValueId(unsigned ValueId) {
4897 auto VGI = ValueIdToValueInfoMap[ValueId];
4902 void ModuleSummaryIndexBitcodeReader::setValueGUID(
4903 uint64_t ValueID, StringRef ValueName, GlobalValue::LinkageTypes Linkage,
4904 StringRef SourceFileName) {
4905 std::string GlobalId =
4906 GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName);
4907 auto ValueGUID = GlobalValue::getGUID(GlobalId);
4908 auto OriginalNameID = ValueGUID;
4909 if (GlobalValue::isLocalLinkage(Linkage))
4910 OriginalNameID = GlobalValue::getGUID(ValueName);
4911 if (PrintSummaryGUIDs)
4912 dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is "
4913 << ValueName << "\n";
4915 // UseStrtab is false for legacy summary formats and value names are
4916 // created on stack. In that case we save the name in a string saver in
4917 // the index so that the value name can be recorded.
4918 ValueIdToValueInfoMap[ValueID] = std::make_pair(
4919 TheIndex.getOrInsertValueInfo(
4921 UseStrtab ? ValueName : TheIndex.saveString(ValueName)),
4925 // Specialized value symbol table parser used when reading module index
4926 // blocks where we don't actually create global values. The parsed information
4927 // is saved in the bitcode reader for use when later parsing summaries.
4928 Error ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(
4930 DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) {
4931 // With a strtab the VST is not required to parse the summary.
4933 return Error::success();
4935 assert(Offset > 0 && "Expected non-zero VST offset");
4936 uint64_t CurrentBit = jumpToValueSymbolTable(Offset, Stream);
4938 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
4939 return error("Invalid record");
4941 SmallVector<uint64_t, 64> Record;
4943 // Read all the records for this value table.
4944 SmallString<128> ValueName;
4947 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4949 switch (Entry.Kind) {
4950 case BitstreamEntry::SubBlock: // Handled for us already.
4951 case BitstreamEntry::Error:
4952 return error("Malformed block");
4953 case BitstreamEntry::EndBlock:
4954 // Done parsing VST, jump back to wherever we came from.
4955 Stream.JumpToBit(CurrentBit);
4956 return Error::success();
4957 case BitstreamEntry::Record:
4958 // The interesting case.
4964 switch (Stream.readRecord(Entry.ID, Record)) {
4965 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
4967 case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
4968 if (convertToString(Record, 1, ValueName))
4969 return error("Invalid record");
4970 unsigned ValueID = Record[0];
4971 assert(!SourceFileName.empty());
4972 auto VLI = ValueIdToLinkageMap.find(ValueID);
4973 assert(VLI != ValueIdToLinkageMap.end() &&
4974 "No linkage found for VST entry?");
4975 auto Linkage = VLI->second;
4976 setValueGUID(ValueID, ValueName, Linkage, SourceFileName);
4980 case bitc::VST_CODE_FNENTRY: {
4981 // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
4982 if (convertToString(Record, 2, ValueName))
4983 return error("Invalid record");
4984 unsigned ValueID = Record[0];
4985 assert(!SourceFileName.empty());
4986 auto VLI = ValueIdToLinkageMap.find(ValueID);
4987 assert(VLI != ValueIdToLinkageMap.end() &&
4988 "No linkage found for VST entry?");
4989 auto Linkage = VLI->second;
4990 setValueGUID(ValueID, ValueName, Linkage, SourceFileName);
4994 case bitc::VST_CODE_COMBINED_ENTRY: {
4995 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
4996 unsigned ValueID = Record[0];
4997 GlobalValue::GUID RefGUID = Record[1];
4998 // The "original name", which is the second value of the pair will be
4999 // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index.
5000 ValueIdToValueInfoMap[ValueID] =
5001 std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID);
5008 // Parse just the blocks needed for building the index out of the module.
5009 // At the end of this routine the module Index is populated with a map
5010 // from global value id to GlobalValueSummary objects.
5011 Error ModuleSummaryIndexBitcodeReader::parseModule() {
5012 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
5013 return error("Invalid record");
5015 SmallVector<uint64_t, 64> Record;
5016 DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap;
5017 unsigned ValueId = 0;
5019 // Read the index for this module.
5021 BitstreamEntry Entry = Stream.advance();
5023 switch (Entry.Kind) {
5024 case BitstreamEntry::Error:
5025 return error("Malformed block");
5026 case BitstreamEntry::EndBlock:
5027 return Error::success();
5029 case BitstreamEntry::SubBlock:
5031 default: // Skip unknown content.
5032 if (Stream.SkipBlock())
5033 return error("Invalid record");
5035 case bitc::BLOCKINFO_BLOCK_ID:
5036 // Need to parse these to get abbrev ids (e.g. for VST)
5037 if (readBlockInfo())
5038 return error("Malformed block");
5040 case bitc::VALUE_SYMTAB_BLOCK_ID:
5041 // Should have been parsed earlier via VSTOffset, unless there
5042 // is no summary section.
5043 assert(((SeenValueSymbolTable && VSTOffset > 0) ||
5044 !SeenGlobalValSummary) &&
5045 "Expected early VST parse via VSTOffset record");
5046 if (Stream.SkipBlock())
5047 return error("Invalid record");
5049 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
5050 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
5051 // Add the module if it is a per-module index (has a source file name).
5052 if (!SourceFileName.empty())
5054 assert(!SeenValueSymbolTable &&
5055 "Already read VST when parsing summary block?");
5056 // We might not have a VST if there were no values in the
5057 // summary. An empty summary block generated when we are
5058 // performing ThinLTO compiles so we don't later invoke
5059 // the regular LTO process on them.
5060 if (VSTOffset > 0) {
5061 if (Error Err = parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap))
5063 SeenValueSymbolTable = true;
5065 SeenGlobalValSummary = true;
5066 if (Error Err = parseEntireSummary(Entry.ID))
5069 case bitc::MODULE_STRTAB_BLOCK_ID:
5070 if (Error Err = parseModuleStringTable())
5076 case BitstreamEntry::Record: {
5078 auto BitCode = Stream.readRecord(Entry.ID, Record);
5081 break; // Default behavior, ignore unknown content.
5082 case bitc::MODULE_CODE_VERSION: {
5083 if (Error Err = parseVersionRecord(Record).takeError())
5087 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
5088 case bitc::MODULE_CODE_SOURCE_FILENAME: {
5089 SmallString<128> ValueName;
5090 if (convertToString(Record, 0, ValueName))
5091 return error("Invalid record");
5092 SourceFileName = ValueName.c_str();
5095 /// MODULE_CODE_HASH: [5*i32]
5096 case bitc::MODULE_CODE_HASH: {
5097 if (Record.size() != 5)
5098 return error("Invalid hash length " + Twine(Record.size()).str());
5099 auto &Hash = getThisModule()->second.second;
5101 for (auto &Val : Record) {
5102 assert(!(Val >> 32) && "Unexpected high bits set");
5107 /// MODULE_CODE_VSTOFFSET: [offset]
5108 case bitc::MODULE_CODE_VSTOFFSET:
5109 if (Record.size() < 1)
5110 return error("Invalid record");
5111 // Note that we subtract 1 here because the offset is relative to one
5112 // word before the start of the identification or module block, which
5113 // was historically always the start of the regular bitcode header.
5114 VSTOffset = Record[0] - 1;
5116 // v1 GLOBALVAR: [pointer type, isconst, initid, linkage, ...]
5117 // v1 FUNCTION: [type, callingconv, isproto, linkage, ...]
5118 // v1 ALIAS: [alias type, addrspace, aliasee val#, linkage, ...]
5119 // v2: [strtab offset, strtab size, v1]
5120 case bitc::MODULE_CODE_GLOBALVAR:
5121 case bitc::MODULE_CODE_FUNCTION:
5122 case bitc::MODULE_CODE_ALIAS: {
5124 ArrayRef<uint64_t> GVRecord;
5125 std::tie(Name, GVRecord) = readNameFromStrtab(Record);
5126 if (GVRecord.size() <= 3)
5127 return error("Invalid record");
5128 uint64_t RawLinkage = GVRecord[3];
5129 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
5131 ValueIdToLinkageMap[ValueId++] = Linkage;
5135 setValueGUID(ValueId++, Name, Linkage, SourceFileName);
5145 std::vector<ValueInfo>
5146 ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) {
5147 std::vector<ValueInfo> Ret;
5148 Ret.reserve(Record.size());
5149 for (uint64_t RefValueId : Record)
5150 Ret.push_back(getValueInfoFromValueId(RefValueId).first);
5154 std::vector<FunctionSummary::EdgeTy>
5155 ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record,
5156 bool IsOldProfileFormat,
5157 bool HasProfile, bool HasRelBF) {
5158 std::vector<FunctionSummary::EdgeTy> Ret;
5159 Ret.reserve(Record.size());
5160 for (unsigned I = 0, E = Record.size(); I != E; ++I) {
5161 CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown;
5163 ValueInfo Callee = getValueInfoFromValueId(Record[I]).first;
5164 if (IsOldProfileFormat) {
5165 I += 1; // Skip old callsitecount field
5167 I += 1; // Skip old profilecount field
5168 } else if (HasProfile)
5169 Hotness = static_cast<CalleeInfo::HotnessType>(Record[++I]);
5171 RelBF = Record[++I];
5172 Ret.push_back(FunctionSummary::EdgeTy{Callee, CalleeInfo(Hotness, RelBF)});
5178 parseWholeProgramDevirtResolutionByArg(ArrayRef<uint64_t> Record, size_t &Slot,
5179 WholeProgramDevirtResolution &Wpd) {
5180 uint64_t ArgNum = Record[Slot++];
5181 WholeProgramDevirtResolution::ByArg &B =
5182 Wpd.ResByArg[{Record.begin() + Slot, Record.begin() + Slot + ArgNum}];
5186 static_cast<WholeProgramDevirtResolution::ByArg::Kind>(Record[Slot++]);
5187 B.Info = Record[Slot++];
5188 B.Byte = Record[Slot++];
5189 B.Bit = Record[Slot++];
5192 static void parseWholeProgramDevirtResolution(ArrayRef<uint64_t> Record,
5193 StringRef Strtab, size_t &Slot,
5194 TypeIdSummary &TypeId) {
5195 uint64_t Id = Record[Slot++];
5196 WholeProgramDevirtResolution &Wpd = TypeId.WPDRes[Id];
5198 Wpd.TheKind = static_cast<WholeProgramDevirtResolution::Kind>(Record[Slot++]);
5199 Wpd.SingleImplName = {Strtab.data() + Record[Slot],
5200 static_cast<size_t>(Record[Slot + 1])};
5203 uint64_t ResByArgNum = Record[Slot++];
5204 for (uint64_t I = 0; I != ResByArgNum; ++I)
5205 parseWholeProgramDevirtResolutionByArg(Record, Slot, Wpd);
5208 static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record,
5210 ModuleSummaryIndex &TheIndex) {
5212 TypeIdSummary &TypeId = TheIndex.getOrInsertTypeIdSummary(
5213 {Strtab.data() + Record[Slot], static_cast<size_t>(Record[Slot + 1])});
5216 TypeId.TTRes.TheKind = static_cast<TypeTestResolution::Kind>(Record[Slot++]);
5217 TypeId.TTRes.SizeM1BitWidth = Record[Slot++];
5218 TypeId.TTRes.AlignLog2 = Record[Slot++];
5219 TypeId.TTRes.SizeM1 = Record[Slot++];
5220 TypeId.TTRes.BitMask = Record[Slot++];
5221 TypeId.TTRes.InlineBits = Record[Slot++];
5223 while (Slot < Record.size())
5224 parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId);
5227 static void setImmutableRefs(std::vector<ValueInfo> &Refs, unsigned Count) {
5228 // Read-only refs are in the end of the refs list.
5229 for (unsigned RefNo = Refs.size() - Count; RefNo < Refs.size(); ++RefNo)
5230 Refs[RefNo].setReadOnly();
5233 // Eagerly parse the entire summary block. This populates the GlobalValueSummary
5234 // objects in the index.
5235 Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
5236 if (Stream.EnterSubBlock(ID))
5237 return error("Invalid record");
5238 SmallVector<uint64_t, 64> Record;
5242 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5243 if (Entry.Kind != BitstreamEntry::Record)
5244 return error("Invalid Summary Block: record for version expected");
5245 if (Stream.readRecord(Entry.ID, Record) != bitc::FS_VERSION)
5246 return error("Invalid Summary Block: version expected");
5248 const uint64_t Version = Record[0];
5249 const bool IsOldProfileFormat = Version == 1;
5250 if (Version < 1 || Version > 6)
5251 return error("Invalid summary version " + Twine(Version) +
5252 ". Version should be in the range [1-6].");
5255 // Keep around the last seen summary to be used when we see an optional
5256 // "OriginalName" attachement.
5257 GlobalValueSummary *LastSeenSummary = nullptr;
5258 GlobalValue::GUID LastSeenGUID = 0;
5260 // We can expect to see any number of type ID information records before
5261 // each function summary records; these variables store the information
5262 // collected so far so that it can be used to create the summary object.
5263 std::vector<GlobalValue::GUID> PendingTypeTests;
5264 std::vector<FunctionSummary::VFuncId> PendingTypeTestAssumeVCalls,
5265 PendingTypeCheckedLoadVCalls;
5266 std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls,
5267 PendingTypeCheckedLoadConstVCalls;
5270 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5272 switch (Entry.Kind) {
5273 case BitstreamEntry::SubBlock: // Handled for us already.
5274 case BitstreamEntry::Error:
5275 return error("Malformed block");
5276 case BitstreamEntry::EndBlock:
5277 return Error::success();
5278 case BitstreamEntry::Record:
5279 // The interesting case.
5283 // Read a record. The record format depends on whether this
5284 // is a per-module index or a combined index file. In the per-module
5285 // case the records contain the associated value's ID for correlation
5286 // with VST entries. In the combined index the correlation is done
5287 // via the bitcode offset of the summary records (which were saved
5288 // in the combined index VST entries). The records also contain
5289 // information used for ThinLTO renaming and importing.
5291 auto BitCode = Stream.readRecord(Entry.ID, Record);
5293 default: // Default behavior: ignore.
5295 case bitc::FS_FLAGS: { // [flags]
5296 uint64_t Flags = Record[0];
5298 assert(Flags <= 0x1f && "Unexpected bits in flag");
5300 // 1 bit: WithGlobalValueDeadStripping flag.
5301 // Set on combined index only.
5303 TheIndex.setWithGlobalValueDeadStripping();
5304 // 1 bit: SkipModuleByDistributedBackend flag.
5305 // Set on combined index only.
5307 TheIndex.setSkipModuleByDistributedBackend();
5308 // 1 bit: HasSyntheticEntryCounts flag.
5309 // Set on combined index only.
5311 TheIndex.setHasSyntheticEntryCounts();
5312 // 1 bit: DisableSplitLTOUnit flag.
5313 // Set on per module indexes. It is up to the client to validate
5314 // the consistency of this flag across modules being linked.
5316 TheIndex.setEnableSplitLTOUnit();
5317 // 1 bit: PartiallySplitLTOUnits flag.
5318 // Set on combined index only.
5320 TheIndex.setPartiallySplitLTOUnits();
5323 case bitc::FS_VALUE_GUID: { // [valueid, refguid]
5324 uint64_t ValueID = Record[0];
5325 GlobalValue::GUID RefGUID = Record[1];
5326 ValueIdToValueInfoMap[ValueID] =
5327 std::make_pair(TheIndex.getOrInsertValueInfo(RefGUID), RefGUID);
5330 // FS_PERMODULE: [valueid, flags, instcount, fflags, numrefs,
5331 // numrefs x valueid, n x (valueid)]
5332 // FS_PERMODULE_PROFILE: [valueid, flags, instcount, fflags, numrefs,
5333 // numrefs x valueid,
5334 // n x (valueid, hotness)]
5335 // FS_PERMODULE_RELBF: [valueid, flags, instcount, fflags, numrefs,
5336 // numrefs x valueid,
5337 // n x (valueid, relblockfreq)]
5338 case bitc::FS_PERMODULE:
5339 case bitc::FS_PERMODULE_RELBF:
5340 case bitc::FS_PERMODULE_PROFILE: {
5341 unsigned ValueID = Record[0];
5342 uint64_t RawFlags = Record[1];
5343 unsigned InstCount = Record[2];
5344 uint64_t RawFunFlags = 0;
5345 unsigned NumRefs = Record[3];
5346 unsigned NumImmutableRefs = 0;
5347 int RefListStartIndex = 4;
5349 RawFunFlags = Record[3];
5350 NumRefs = Record[4];
5351 RefListStartIndex = 5;
5353 NumImmutableRefs = Record[5];
5354 RefListStartIndex = 6;
5358 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
5359 // The module path string ref set in the summary must be owned by the
5360 // index's module string table. Since we don't have a module path
5361 // string table section in the per-module index, we create a single
5362 // module path string table entry with an empty (0) ID to take
5364 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;
5365 assert(Record.size() >= RefListStartIndex + NumRefs &&
5366 "Record size inconsistent with number of references");
5367 std::vector<ValueInfo> Refs = makeRefList(
5368 ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs));
5369 bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE);
5370 bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF);
5371 std::vector<FunctionSummary::EdgeTy> Calls = makeCallList(
5372 ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex),
5373 IsOldProfileFormat, HasProfile, HasRelBF);
5374 setImmutableRefs(Refs, NumImmutableRefs);
5375 auto FS = llvm::make_unique<FunctionSummary>(
5376 Flags, InstCount, getDecodedFFlags(RawFunFlags), /*EntryCount=*/0,
5377 std::move(Refs), std::move(Calls), std::move(PendingTypeTests),
5378 std::move(PendingTypeTestAssumeVCalls),
5379 std::move(PendingTypeCheckedLoadVCalls),
5380 std::move(PendingTypeTestAssumeConstVCalls),
5381 std::move(PendingTypeCheckedLoadConstVCalls));
5382 PendingTypeTests.clear();
5383 PendingTypeTestAssumeVCalls.clear();
5384 PendingTypeCheckedLoadVCalls.clear();
5385 PendingTypeTestAssumeConstVCalls.clear();
5386 PendingTypeCheckedLoadConstVCalls.clear();
5387 auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID);
5388 FS->setModulePath(getThisModule()->first());
5389 FS->setOriginalName(VIAndOriginalGUID.second);
5390 TheIndex.addGlobalValueSummary(VIAndOriginalGUID.first, std::move(FS));
5393 // FS_ALIAS: [valueid, flags, valueid]
5394 // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as
5395 // they expect all aliasee summaries to be available.
5396 case bitc::FS_ALIAS: {
5397 unsigned ValueID = Record[0];
5398 uint64_t RawFlags = Record[1];
5399 unsigned AliaseeID = Record[2];
5400 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
5401 auto AS = llvm::make_unique<AliasSummary>(Flags);
5402 // The module path string ref set in the summary must be owned by the
5403 // index's module string table. Since we don't have a module path
5404 // string table section in the per-module index, we create a single
5405 // module path string table entry with an empty (0) ID to take
5407 AS->setModulePath(getThisModule()->first());
5409 GlobalValue::GUID AliaseeGUID =
5410 getValueInfoFromValueId(AliaseeID).first.getGUID();
5411 auto AliaseeInModule =
5412 TheIndex.findSummaryInModule(AliaseeGUID, ModulePath);
5413 if (!AliaseeInModule)
5414 return error("Alias expects aliasee summary to be parsed");
5415 AS->setAliasee(AliaseeInModule);
5416 AS->setAliaseeGUID(AliaseeGUID);
5418 auto GUID = getValueInfoFromValueId(ValueID);
5419 AS->setOriginalName(GUID.second);
5420 TheIndex.addGlobalValueSummary(GUID.first, std::move(AS));
5423 // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, varflags, n x valueid]
5424 case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: {
5425 unsigned ValueID = Record[0];
5426 uint64_t RawFlags = Record[1];
5427 unsigned RefArrayStart = 2;
5428 GlobalVarSummary::GVarFlags GVF;
5429 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
5431 GVF = getDecodedGVarFlags(Record[2]);
5434 std::vector<ValueInfo> Refs =
5435 makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart));
5437 llvm::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs));
5438 FS->setModulePath(getThisModule()->first());
5439 auto GUID = getValueInfoFromValueId(ValueID);
5440 FS->setOriginalName(GUID.second);
5441 TheIndex.addGlobalValueSummary(GUID.first, std::move(FS));
5444 // FS_COMBINED: [valueid, modid, flags, instcount, fflags, numrefs,
5445 // numrefs x valueid, n x (valueid)]
5446 // FS_COMBINED_PROFILE: [valueid, modid, flags, instcount, fflags, numrefs,
5447 // numrefs x valueid, n x (valueid, hotness)]
5448 case bitc::FS_COMBINED:
5449 case bitc::FS_COMBINED_PROFILE: {
5450 unsigned ValueID = Record[0];
5451 uint64_t ModuleId = Record[1];
5452 uint64_t RawFlags = Record[2];
5453 unsigned InstCount = Record[3];
5454 uint64_t RawFunFlags = 0;
5455 uint64_t EntryCount = 0;
5456 unsigned NumRefs = Record[4];
5457 unsigned NumImmutableRefs = 0;
5458 int RefListStartIndex = 5;
5461 RawFunFlags = Record[4];
5462 RefListStartIndex = 6;
5463 size_t NumRefsIndex = 5;
5465 RefListStartIndex = 7;
5468 EntryCount = Record[5];
5469 RefListStartIndex = 8;
5471 NumImmutableRefs = Record[RefListStartIndex - 1];
5473 NumRefs = Record[NumRefsIndex];
5476 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
5477 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;
5478 assert(Record.size() >= RefListStartIndex + NumRefs &&
5479 "Record size inconsistent with number of references");
5480 std::vector<ValueInfo> Refs = makeRefList(
5481 ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs));
5482 bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE);
5483 std::vector<FunctionSummary::EdgeTy> Edges = makeCallList(
5484 ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex),
5485 IsOldProfileFormat, HasProfile, false);
5486 ValueInfo VI = getValueInfoFromValueId(ValueID).first;
5487 setImmutableRefs(Refs, NumImmutableRefs);
5488 auto FS = llvm::make_unique<FunctionSummary>(
5489 Flags, InstCount, getDecodedFFlags(RawFunFlags), EntryCount,
5490 std::move(Refs), std::move(Edges), std::move(PendingTypeTests),
5491 std::move(PendingTypeTestAssumeVCalls),
5492 std::move(PendingTypeCheckedLoadVCalls),
5493 std::move(PendingTypeTestAssumeConstVCalls),
5494 std::move(PendingTypeCheckedLoadConstVCalls));
5495 PendingTypeTests.clear();
5496 PendingTypeTestAssumeVCalls.clear();
5497 PendingTypeCheckedLoadVCalls.clear();
5498 PendingTypeTestAssumeConstVCalls.clear();
5499 PendingTypeCheckedLoadConstVCalls.clear();
5500 LastSeenSummary = FS.get();
5501 LastSeenGUID = VI.getGUID();
5502 FS->setModulePath(ModuleIdMap[ModuleId]);
5503 TheIndex.addGlobalValueSummary(VI, std::move(FS));
5506 // FS_COMBINED_ALIAS: [valueid, modid, flags, valueid]
5507 // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as
5508 // they expect all aliasee summaries to be available.
5509 case bitc::FS_COMBINED_ALIAS: {
5510 unsigned ValueID = Record[0];
5511 uint64_t ModuleId = Record[1];
5512 uint64_t RawFlags = Record[2];
5513 unsigned AliaseeValueId = Record[3];
5514 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
5515 auto AS = llvm::make_unique<AliasSummary>(Flags);
5516 LastSeenSummary = AS.get();
5517 AS->setModulePath(ModuleIdMap[ModuleId]);
5520 getValueInfoFromValueId(AliaseeValueId).first.getGUID();
5521 auto AliaseeInModule =
5522 TheIndex.findSummaryInModule(AliaseeGUID, AS->modulePath());
5523 AS->setAliasee(AliaseeInModule);
5524 AS->setAliaseeGUID(AliaseeGUID);
5526 ValueInfo VI = getValueInfoFromValueId(ValueID).first;
5527 LastSeenGUID = VI.getGUID();
5528 TheIndex.addGlobalValueSummary(VI, std::move(AS));
5531 // FS_COMBINED_GLOBALVAR_INIT_REFS: [valueid, modid, flags, n x valueid]
5532 case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: {
5533 unsigned ValueID = Record[0];
5534 uint64_t ModuleId = Record[1];
5535 uint64_t RawFlags = Record[2];
5536 unsigned RefArrayStart = 3;
5537 GlobalVarSummary::GVarFlags GVF;
5538 auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
5540 GVF = getDecodedGVarFlags(Record[3]);
5543 std::vector<ValueInfo> Refs =
5544 makeRefList(ArrayRef<uint64_t>(Record).slice(RefArrayStart));
5546 llvm::make_unique<GlobalVarSummary>(Flags, GVF, std::move(Refs));
5547 LastSeenSummary = FS.get();
5548 FS->setModulePath(ModuleIdMap[ModuleId]);
5549 ValueInfo VI = getValueInfoFromValueId(ValueID).first;
5550 LastSeenGUID = VI.getGUID();
5551 TheIndex.addGlobalValueSummary(VI, std::move(FS));
5554 // FS_COMBINED_ORIGINAL_NAME: [original_name]
5555 case bitc::FS_COMBINED_ORIGINAL_NAME: {
5556 uint64_t OriginalName = Record[0];
5557 if (!LastSeenSummary)
5558 return error("Name attachment that does not follow a combined record");
5559 LastSeenSummary->setOriginalName(OriginalName);
5560 TheIndex.addOriginalName(LastSeenGUID, OriginalName);
5561 // Reset the LastSeenSummary
5562 LastSeenSummary = nullptr;
5566 case bitc::FS_TYPE_TESTS:
5567 assert(PendingTypeTests.empty());
5568 PendingTypeTests.insert(PendingTypeTests.end(), Record.begin(),
5572 case bitc::FS_TYPE_TEST_ASSUME_VCALLS:
5573 assert(PendingTypeTestAssumeVCalls.empty());
5574 for (unsigned I = 0; I != Record.size(); I += 2)
5575 PendingTypeTestAssumeVCalls.push_back({Record[I], Record[I+1]});
5578 case bitc::FS_TYPE_CHECKED_LOAD_VCALLS:
5579 assert(PendingTypeCheckedLoadVCalls.empty());
5580 for (unsigned I = 0; I != Record.size(); I += 2)
5581 PendingTypeCheckedLoadVCalls.push_back({Record[I], Record[I+1]});
5584 case bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL:
5585 PendingTypeTestAssumeConstVCalls.push_back(
5586 {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}});
5589 case bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL:
5590 PendingTypeCheckedLoadConstVCalls.push_back(
5591 {{Record[0], Record[1]}, {Record.begin() + 2, Record.end()}});
5594 case bitc::FS_CFI_FUNCTION_DEFS: {
5595 std::set<std::string> &CfiFunctionDefs = TheIndex.cfiFunctionDefs();
5596 for (unsigned I = 0; I != Record.size(); I += 2)
5597 CfiFunctionDefs.insert(
5598 {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])});
5602 case bitc::FS_CFI_FUNCTION_DECLS: {
5603 std::set<std::string> &CfiFunctionDecls = TheIndex.cfiFunctionDecls();
5604 for (unsigned I = 0; I != Record.size(); I += 2)
5605 CfiFunctionDecls.insert(
5606 {Strtab.data() + Record[I], static_cast<size_t>(Record[I + 1])});
5610 case bitc::FS_TYPE_ID:
5611 parseTypeIdSummaryRecord(Record, Strtab, TheIndex);
5615 llvm_unreachable("Exit infinite loop");
5618 // Parse the module string table block into the Index.
5619 // This populates the ModulePathStringTable map in the index.
5620 Error ModuleSummaryIndexBitcodeReader::parseModuleStringTable() {
5621 if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID))
5622 return error("Invalid record");
5624 SmallVector<uint64_t, 64> Record;
5626 SmallString<128> ModulePath;
5627 ModuleSummaryIndex::ModuleInfo *LastSeenModule = nullptr;
5630 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5632 switch (Entry.Kind) {
5633 case BitstreamEntry::SubBlock: // Handled for us already.
5634 case BitstreamEntry::Error:
5635 return error("Malformed block");
5636 case BitstreamEntry::EndBlock:
5637 return Error::success();
5638 case BitstreamEntry::Record:
5639 // The interesting case.
5644 switch (Stream.readRecord(Entry.ID, Record)) {
5645 default: // Default behavior: ignore.
5647 case bitc::MST_CODE_ENTRY: {
5648 // MST_ENTRY: [modid, namechar x N]
5649 uint64_t ModuleId = Record[0];
5651 if (convertToString(Record, 1, ModulePath))
5652 return error("Invalid record");
5654 LastSeenModule = TheIndex.addModule(ModulePath, ModuleId);
5655 ModuleIdMap[ModuleId] = LastSeenModule->first();
5660 /// MST_CODE_HASH: [5*i32]
5661 case bitc::MST_CODE_HASH: {
5662 if (Record.size() != 5)
5663 return error("Invalid hash length " + Twine(Record.size()).str());
5664 if (!LastSeenModule)
5665 return error("Invalid hash that does not follow a module path");
5667 for (auto &Val : Record) {
5668 assert(!(Val >> 32) && "Unexpected high bits set");
5669 LastSeenModule->second.second[Pos++] = Val;
5671 // Reset LastSeenModule to avoid overriding the hash unexpectedly.
5672 LastSeenModule = nullptr;
5677 llvm_unreachable("Exit infinite loop");
5682 // FIXME: This class is only here to support the transition to llvm::Error. It
5683 // will be removed once this transition is complete. Clients should prefer to
5684 // deal with the Error value directly, rather than converting to error_code.
5685 class BitcodeErrorCategoryType : public std::error_category {
5686 const char *name() const noexcept override {
5687 return "llvm.bitcode";
5690 std::string message(int IE) const override {
5691 BitcodeError E = static_cast<BitcodeError>(IE);
5693 case BitcodeError::CorruptedBitcode:
5694 return "Corrupted bitcode";
5696 llvm_unreachable("Unknown error type!");
5700 } // end anonymous namespace
5702 static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;
5704 const std::error_category &llvm::BitcodeErrorCategory() {
5705 return *ErrorCategory;
5708 static Expected<StringRef> readBlobInRecord(BitstreamCursor &Stream,
5709 unsigned Block, unsigned RecordID) {
5710 if (Stream.EnterSubBlock(Block))
5711 return error("Invalid record");
5715 BitstreamEntry Entry = Stream.advance();
5716 switch (Entry.Kind) {
5717 case BitstreamEntry::EndBlock:
5720 case BitstreamEntry::Error:
5721 return error("Malformed block");
5723 case BitstreamEntry::SubBlock:
5724 if (Stream.SkipBlock())
5725 return error("Malformed block");
5728 case BitstreamEntry::Record:
5730 SmallVector<uint64_t, 1> Record;
5731 if (Stream.readRecord(Entry.ID, Record, &Blob) == RecordID)
5738 //===----------------------------------------------------------------------===//
5739 // External interface
5740 //===----------------------------------------------------------------------===//
5742 Expected<std::vector<BitcodeModule>>
5743 llvm::getBitcodeModuleList(MemoryBufferRef Buffer) {
5744 auto FOrErr = getBitcodeFileContents(Buffer);
5746 return FOrErr.takeError();
5747 return std::move(FOrErr->Mods);
5750 Expected<BitcodeFileContents>
5751 llvm::getBitcodeFileContents(MemoryBufferRef Buffer) {
5752 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);
5754 return StreamOrErr.takeError();
5755 BitstreamCursor &Stream = *StreamOrErr;
5757 BitcodeFileContents F;
5759 uint64_t BCBegin = Stream.getCurrentByteNo();
5761 // We may be consuming bitcode from a client that leaves garbage at the end
5762 // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to
5763 // the end that there cannot possibly be another module, stop looking.
5764 if (BCBegin + 8 >= Stream.getBitcodeBytes().size())
5767 BitstreamEntry Entry = Stream.advance();
5768 switch (Entry.Kind) {
5769 case BitstreamEntry::EndBlock:
5770 case BitstreamEntry::Error:
5771 return error("Malformed block");
5773 case BitstreamEntry::SubBlock: {
5774 uint64_t IdentificationBit = -1ull;
5775 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
5776 IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8;
5777 if (Stream.SkipBlock())
5778 return error("Malformed block");
5780 Entry = Stream.advance();
5781 if (Entry.Kind != BitstreamEntry::SubBlock ||
5782 Entry.ID != bitc::MODULE_BLOCK_ID)
5783 return error("Malformed block");
5786 if (Entry.ID == bitc::MODULE_BLOCK_ID) {
5787 uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8;
5788 if (Stream.SkipBlock())
5789 return error("Malformed block");
5791 F.Mods.push_back({Stream.getBitcodeBytes().slice(
5792 BCBegin, Stream.getCurrentByteNo() - BCBegin),
5793 Buffer.getBufferIdentifier(), IdentificationBit,
5798 if (Entry.ID == bitc::STRTAB_BLOCK_ID) {
5799 Expected<StringRef> Strtab =
5800 readBlobInRecord(Stream, bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB);
5802 return Strtab.takeError();
5803 // This string table is used by every preceding bitcode module that does
5804 // not have its own string table. A bitcode file may have multiple
5805 // string tables if it was created by binary concatenation, for example
5806 // with "llvm-cat -b".
5807 for (auto I = F.Mods.rbegin(), E = F.Mods.rend(); I != E; ++I) {
5808 if (!I->Strtab.empty())
5810 I->Strtab = *Strtab;
5812 // Similarly, the string table is used by every preceding symbol table;
5813 // normally there will be just one unless the bitcode file was created
5814 // by binary concatenation.
5815 if (!F.Symtab.empty() && F.StrtabForSymtab.empty())
5816 F.StrtabForSymtab = *Strtab;
5820 if (Entry.ID == bitc::SYMTAB_BLOCK_ID) {
5821 Expected<StringRef> SymtabOrErr =
5822 readBlobInRecord(Stream, bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB);
5824 return SymtabOrErr.takeError();
5826 // We can expect the bitcode file to have multiple symbol tables if it
5827 // was created by binary concatenation. In that case we silently
5828 // ignore any subsequent symbol tables, which is fine because this is a
5829 // low level function. The client is expected to notice that the number
5830 // of modules in the symbol table does not match the number of modules
5831 // in the input file and regenerate the symbol table.
5832 if (F.Symtab.empty())
5833 F.Symtab = *SymtabOrErr;
5837 if (Stream.SkipBlock())
5838 return error("Malformed block");
5841 case BitstreamEntry::Record:
5842 Stream.skipRecord(Entry.ID);
5848 /// Get a lazy one-at-time loading module from bitcode.
5850 /// This isn't always used in a lazy context. In particular, it's also used by
5851 /// \a parseModule(). If this is truly lazy, then we need to eagerly pull
5852 /// in forward-referenced functions from block address references.
5854 /// \param[in] MaterializeAll Set to \c true if we should materialize
5856 Expected<std::unique_ptr<Module>>
5857 BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll,
5858 bool ShouldLazyLoadMetadata, bool IsImporting) {
5859 BitstreamCursor Stream(Buffer);
5861 std::string ProducerIdentification;
5862 if (IdentificationBit != -1ull) {
5863 Stream.JumpToBit(IdentificationBit);
5864 Expected<std::string> ProducerIdentificationOrErr =
5865 readIdentificationBlock(Stream);
5866 if (!ProducerIdentificationOrErr)
5867 return ProducerIdentificationOrErr.takeError();
5869 ProducerIdentification = *ProducerIdentificationOrErr;
5872 Stream.JumpToBit(ModuleBit);
5873 auto *R = new BitcodeReader(std::move(Stream), Strtab, ProducerIdentification,
5876 std::unique_ptr<Module> M =
5877 llvm::make_unique<Module>(ModuleIdentifier, Context);
5878 M->setMaterializer(R);
5880 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
5882 R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, IsImporting))
5883 return std::move(Err);
5885 if (MaterializeAll) {
5886 // Read in the entire module, and destroy the BitcodeReader.
5887 if (Error Err = M->materializeAll())
5888 return std::move(Err);
5890 // Resolve forward references from blockaddresses.
5891 if (Error Err = R->materializeForwardReferencedFunctions())
5892 return std::move(Err);
5894 return std::move(M);
5897 Expected<std::unique_ptr<Module>>
5898 BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata,
5900 return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting);
5903 // Parse the specified bitcode buffer and merge the index into CombinedIndex.
5904 // We don't use ModuleIdentifier here because the client may need to control the
5905 // module path used in the combined summary (e.g. when reading summaries for
5906 // regular LTO modules).
5907 Error BitcodeModule::readSummary(ModuleSummaryIndex &CombinedIndex,
5908 StringRef ModulePath, uint64_t ModuleId) {
5909 BitstreamCursor Stream(Buffer);
5910 Stream.JumpToBit(ModuleBit);
5912 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, CombinedIndex,
5913 ModulePath, ModuleId);
5914 return R.parseModule();
5917 // Parse the specified bitcode buffer, returning the function info index.
5918 Expected<std::unique_ptr<ModuleSummaryIndex>> BitcodeModule::getSummary() {
5919 BitstreamCursor Stream(Buffer);
5920 Stream.JumpToBit(ModuleBit);
5922 auto Index = llvm::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
5923 ModuleSummaryIndexBitcodeReader R(std::move(Stream), Strtab, *Index,
5924 ModuleIdentifier, 0);
5926 if (Error Err = R.parseModule())
5927 return std::move(Err);
5929 return std::move(Index);
5932 static Expected<bool> getEnableSplitLTOUnitFlag(BitstreamCursor &Stream,
5934 if (Stream.EnterSubBlock(ID))
5935 return error("Invalid record");
5936 SmallVector<uint64_t, 64> Record;
5939 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5941 switch (Entry.Kind) {
5942 case BitstreamEntry::SubBlock: // Handled for us already.
5943 case BitstreamEntry::Error:
5944 return error("Malformed block");
5945 case BitstreamEntry::EndBlock:
5946 // If no flags record found, conservatively return true to mimic
5947 // behavior before this flag was added.
5949 case BitstreamEntry::Record:
5950 // The interesting case.
5954 // Look for the FS_FLAGS record.
5956 auto BitCode = Stream.readRecord(Entry.ID, Record);
5958 default: // Default behavior: ignore.
5960 case bitc::FS_FLAGS: { // [flags]
5961 uint64_t Flags = Record[0];
5963 assert(Flags <= 0x1f && "Unexpected bits in flag");
5969 llvm_unreachable("Exit infinite loop");
5972 // Check if the given bitcode buffer contains a global value summary block.
5973 Expected<BitcodeLTOInfo> BitcodeModule::getLTOInfo() {
5974 BitstreamCursor Stream(Buffer);
5975 Stream.JumpToBit(ModuleBit);
5977 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
5978 return error("Invalid record");
5981 BitstreamEntry Entry = Stream.advance();
5983 switch (Entry.Kind) {
5984 case BitstreamEntry::Error:
5985 return error("Malformed block");
5986 case BitstreamEntry::EndBlock:
5987 return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/false,
5988 /*EnableSplitLTOUnit=*/false};
5990 case BitstreamEntry::SubBlock:
5991 if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) {
5992 Expected<bool> EnableSplitLTOUnit =
5993 getEnableSplitLTOUnitFlag(Stream, Entry.ID);
5994 if (!EnableSplitLTOUnit)
5995 return EnableSplitLTOUnit.takeError();
5996 return BitcodeLTOInfo{/*IsThinLTO=*/true, /*HasSummary=*/true,
5997 *EnableSplitLTOUnit};
6000 if (Entry.ID == bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID) {
6001 Expected<bool> EnableSplitLTOUnit =
6002 getEnableSplitLTOUnitFlag(Stream, Entry.ID);
6003 if (!EnableSplitLTOUnit)
6004 return EnableSplitLTOUnit.takeError();
6005 return BitcodeLTOInfo{/*IsThinLTO=*/false, /*HasSummary=*/true,
6006 *EnableSplitLTOUnit};
6009 // Ignore other sub-blocks.
6010 if (Stream.SkipBlock())
6011 return error("Malformed block");
6014 case BitstreamEntry::Record:
6015 Stream.skipRecord(Entry.ID);
6021 static Expected<BitcodeModule> getSingleModule(MemoryBufferRef Buffer) {
6022 Expected<std::vector<BitcodeModule>> MsOrErr = getBitcodeModuleList(Buffer);
6024 return MsOrErr.takeError();
6026 if (MsOrErr->size() != 1)
6027 return error("Expected a single module");
6029 return (*MsOrErr)[0];
6032 Expected<std::unique_ptr<Module>>
6033 llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context,
6034 bool ShouldLazyLoadMetadata, bool IsImporting) {
6035 Expected<BitcodeModule> BM = getSingleModule(Buffer);
6037 return BM.takeError();
6039 return BM->getLazyModule(Context, ShouldLazyLoadMetadata, IsImporting);
6042 Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule(
6043 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,
6044 bool ShouldLazyLoadMetadata, bool IsImporting) {
6045 auto MOrErr = getLazyBitcodeModule(*Buffer, Context, ShouldLazyLoadMetadata,
6048 (*MOrErr)->setOwnedMemoryBuffer(std::move(Buffer));
6052 Expected<std::unique_ptr<Module>>
6053 BitcodeModule::parseModule(LLVMContext &Context) {
6054 return getModuleImpl(Context, true, false, false);
6055 // TODO: Restore the use-lists to the in-memory state when the bitcode was
6056 // written. We must defer until the Module has been fully materialized.
6059 Expected<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer,
6060 LLVMContext &Context) {
6061 Expected<BitcodeModule> BM = getSingleModule(Buffer);
6063 return BM.takeError();
6065 return BM->parseModule(Context);
6068 Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) {
6069 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);
6071 return StreamOrErr.takeError();
6073 return readTriple(*StreamOrErr);
6076 Expected<bool> llvm::isBitcodeContainingObjCCategory(MemoryBufferRef Buffer) {
6077 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);
6079 return StreamOrErr.takeError();
6081 return hasObjCCategory(*StreamOrErr);
6084 Expected<std::string> llvm::getBitcodeProducerString(MemoryBufferRef Buffer) {
6085 Expected<BitstreamCursor> StreamOrErr = initStream(Buffer);
6087 return StreamOrErr.takeError();
6089 return readIdentificationCode(*StreamOrErr);
6092 Error llvm::readModuleSummaryIndex(MemoryBufferRef Buffer,
6093 ModuleSummaryIndex &CombinedIndex,
6094 uint64_t ModuleId) {
6095 Expected<BitcodeModule> BM = getSingleModule(Buffer);
6097 return BM.takeError();
6099 return BM->readSummary(CombinedIndex, BM->getModuleIdentifier(), ModuleId);
6102 Expected<std::unique_ptr<ModuleSummaryIndex>>
6103 llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) {
6104 Expected<BitcodeModule> BM = getSingleModule(Buffer);
6106 return BM.takeError();
6108 return BM->getSummary();
6111 Expected<BitcodeLTOInfo> llvm::getBitcodeLTOInfo(MemoryBufferRef Buffer) {
6112 Expected<BitcodeModule> BM = getSingleModule(Buffer);
6114 return BM.takeError();
6116 return BM->getLTOInfo();
6119 Expected<std::unique_ptr<ModuleSummaryIndex>>
6120 llvm::getModuleSummaryIndexForFile(StringRef Path,
6121 bool IgnoreEmptyThinLTOIndexFile) {
6122 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
6123 MemoryBuffer::getFileOrSTDIN(Path);
6125 return errorCodeToError(FileOrErr.getError());
6126 if (IgnoreEmptyThinLTOIndexFile && !(*FileOrErr)->getBufferSize())
6128 return getModuleSummaryIndex(**FileOrErr);