| //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===// |
| // |
| // The LLVM Compiler Infrastructure |
| // |
| // This file is distributed under the University of Illinois Open Source |
| // License. See LICENSE.TXT for details. |
| // |
| //===----------------------------------------------------------------------===// |
| |
| #include "llvm/Bitcode/BitcodeReader.h" |
| #include "MetadataLoader.h" |
| #include "ValueList.h" |
| #include "llvm/ADT/APFloat.h" |
| #include "llvm/ADT/APInt.h" |
| #include "llvm/ADT/ArrayRef.h" |
| #include "llvm/ADT/DenseMap.h" |
| #include "llvm/ADT/Optional.h" |
| #include "llvm/ADT/STLExtras.h" |
| #include "llvm/ADT/SmallString.h" |
| #include "llvm/ADT/SmallVector.h" |
| #include "llvm/ADT/StringRef.h" |
| #include "llvm/ADT/Triple.h" |
| #include "llvm/ADT/Twine.h" |
| #include "llvm/Bitcode/BitstreamReader.h" |
| #include "llvm/Bitcode/LLVMBitCodes.h" |
| #include "llvm/IR/Argument.h" |
| #include "llvm/IR/Attributes.h" |
| #include "llvm/IR/AutoUpgrade.h" |
| #include "llvm/IR/BasicBlock.h" |
| #include "llvm/IR/CallSite.h" |
| #include "llvm/IR/CallingConv.h" |
| #include "llvm/IR/Comdat.h" |
| #include "llvm/IR/Constant.h" |
| #include "llvm/IR/Constants.h" |
| #include "llvm/IR/DataLayout.h" |
| #include "llvm/IR/DebugInfo.h" |
| #include "llvm/IR/DebugInfoMetadata.h" |
| #include "llvm/IR/DebugLoc.h" |
| #include "llvm/IR/DerivedTypes.h" |
| #include "llvm/IR/Function.h" |
| #include "llvm/IR/GVMaterializer.h" |
| #include "llvm/IR/GlobalAlias.h" |
| #include "llvm/IR/GlobalIFunc.h" |
| #include "llvm/IR/GlobalIndirectSymbol.h" |
| #include "llvm/IR/GlobalObject.h" |
| #include "llvm/IR/GlobalValue.h" |
| #include "llvm/IR/GlobalVariable.h" |
| #include "llvm/IR/InlineAsm.h" |
| #include "llvm/IR/InstIterator.h" |
| #include "llvm/IR/InstrTypes.h" |
| #include "llvm/IR/Instruction.h" |
| #include "llvm/IR/Instructions.h" |
| #include "llvm/IR/Intrinsics.h" |
| #include "llvm/IR/LLVMContext.h" |
| #include "llvm/IR/Metadata.h" |
| #include "llvm/IR/Module.h" |
| #include "llvm/IR/ModuleSummaryIndex.h" |
| #include "llvm/IR/Operator.h" |
| #include "llvm/IR/Type.h" |
| #include "llvm/IR/Value.h" |
| #include "llvm/IR/Verifier.h" |
| #include "llvm/Support/AtomicOrdering.h" |
| #include "llvm/Support/Casting.h" |
| #include "llvm/Support/CommandLine.h" |
| #include "llvm/Support/Compiler.h" |
| #include "llvm/Support/Debug.h" |
| #include "llvm/Support/Error.h" |
| #include "llvm/Support/ErrorHandling.h" |
| #include "llvm/Support/ErrorOr.h" |
| #include "llvm/Support/ManagedStatic.h" |
| #include "llvm/Support/MathExtras.h" |
| #include "llvm/Support/MemoryBuffer.h" |
| #include "llvm/Support/raw_ostream.h" |
| #include <algorithm> |
| #include <cassert> |
| #include <cstddef> |
| #include <cstdint> |
| #include <deque> |
| #include <map> |
| #include <memory> |
| #include <set> |
| #include <string> |
| #include <system_error> |
| #include <tuple> |
| #include <utility> |
| #include <vector> |
| |
| using namespace llvm; |
| |
| static cl::opt<bool> PrintSummaryGUIDs( |
| "print-summary-global-ids", cl::init(false), cl::Hidden, |
| cl::desc( |
| "Print the global id for each value when reading the module summary")); |
| |
| namespace { |
| |
| enum { |
| SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex |
| }; |
| |
| } // end anonymous namespace |
| |
| static Error error(const Twine &Message) { |
| return make_error<StringError>( |
| Message, make_error_code(BitcodeError::CorruptedBitcode)); |
| } |
| |
| /// Helper to read the header common to all bitcode files. |
| static bool hasValidBitcodeHeader(BitstreamCursor &Stream) { |
| // Sniff for the signature. |
| if (!Stream.canSkipToPos(4) || |
| Stream.Read(8) != 'B' || |
| Stream.Read(8) != 'C' || |
| Stream.Read(4) != 0x0 || |
| Stream.Read(4) != 0xC || |
| Stream.Read(4) != 0xE || |
| Stream.Read(4) != 0xD) |
| return false; |
| return true; |
| } |
| |
| static Expected<BitstreamCursor> initStream(MemoryBufferRef Buffer) { |
| const unsigned char *BufPtr = (const unsigned char *)Buffer.getBufferStart(); |
| const unsigned char *BufEnd = BufPtr + Buffer.getBufferSize(); |
| |
| if (Buffer.getBufferSize() & 3) |
| return error("Invalid bitcode signature"); |
| |
| // If we have a wrapper header, parse it and ignore the non-bc file contents. |
| // The magic number is 0x0B17C0DE stored in little endian. |
| if (isBitcodeWrapper(BufPtr, BufEnd)) |
| if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) |
| return error("Invalid bitcode wrapper header"); |
| |
| BitstreamCursor Stream(ArrayRef<uint8_t>(BufPtr, BufEnd)); |
| if (!hasValidBitcodeHeader(Stream)) |
| return error("Invalid bitcode signature"); |
| |
| return std::move(Stream); |
| } |
| |
| /// Convert a string from a record into an std::string, return true on failure. |
| template <typename StrTy> |
| static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, |
| StrTy &Result) { |
| if (Idx > Record.size()) |
| return true; |
| |
| for (unsigned i = Idx, e = Record.size(); i != e; ++i) |
| Result += (char)Record[i]; |
| return false; |
| } |
| |
| // Strip all the TBAA attachment for the module. |
| static void stripTBAA(Module *M) { |
| for (auto &F : *M) { |
| if (F.isMaterializable()) |
| continue; |
| for (auto &I : instructions(F)) |
| I.setMetadata(LLVMContext::MD_tbaa, nullptr); |
| } |
| } |
| |
| /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the |
| /// "epoch" encoded in the bitcode, and return the producer name if any. |
| static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) { |
| if (Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID)) |
| return error("Invalid record"); |
| |
| // Read all the records. |
| SmallVector<uint64_t, 64> Record; |
| |
| std::string ProducerIdentification; |
| |
| while (true) { |
| BitstreamEntry Entry = Stream.advance(); |
| |
| switch (Entry.Kind) { |
| default: |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| return ProducerIdentification; |
| case BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Read a record. |
| Record.clear(); |
| unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
| switch (BitCode) { |
| default: // Default behavior: reject |
| return error("Invalid value"); |
| case bitc::IDENTIFICATION_CODE_STRING: // IDENTIFICATION: [strchr x N] |
| convertToString(Record, 0, ProducerIdentification); |
| break; |
| case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#] |
| unsigned epoch = (unsigned)Record[0]; |
| if (epoch != bitc::BITCODE_CURRENT_EPOCH) { |
| return error( |
| Twine("Incompatible epoch: Bitcode '") + Twine(epoch) + |
| "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'"); |
| } |
| } |
| } |
| } |
| } |
| |
| static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) { |
| // We expect a number of well-defined blocks, though we don't necessarily |
| // need to understand them all. |
| while (true) { |
| if (Stream.AtEndOfStream()) |
| return ""; |
| |
| BitstreamEntry Entry = Stream.advance(); |
| switch (Entry.Kind) { |
| case BitstreamEntry::EndBlock: |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| |
| case BitstreamEntry::SubBlock: |
| if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) |
| return readIdentificationBlock(Stream); |
| |
| // Ignore other sub-blocks. |
| if (Stream.SkipBlock()) |
| return error("Malformed block"); |
| continue; |
| case BitstreamEntry::Record: |
| Stream.skipRecord(Entry.ID); |
| continue; |
| } |
| } |
| } |
| |
| static Expected<bool> hasObjCCategoryInModule(BitstreamCursor &Stream) { |
| if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
| return error("Invalid record"); |
| |
| SmallVector<uint64_t, 64> Record; |
| // Read all the records for this module. |
| |
| while (true) { |
| BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| |
| switch (Entry.Kind) { |
| case BitstreamEntry::SubBlock: // Handled for us already. |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| return false; |
| case BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Read a record. |
| switch (Stream.readRecord(Entry.ID, Record)) { |
| default: |
| break; // Default behavior, ignore unknown content. |
| case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] |
| std::string S; |
| if (convertToString(Record, 0, S)) |
| return error("Invalid record"); |
| // Check for the i386 and other (x86_64, ARM) conventions |
| if (S.find("__DATA,__objc_catlist") != std::string::npos || |
| S.find("__OBJC,__category") != std::string::npos) |
| return true; |
| break; |
| } |
| } |
| Record.clear(); |
| } |
| llvm_unreachable("Exit infinite loop"); |
| } |
| |
| static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) { |
| // We expect a number of well-defined blocks, though we don't necessarily |
| // need to understand them all. |
| while (true) { |
| BitstreamEntry Entry = Stream.advance(); |
| |
| switch (Entry.Kind) { |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| return false; |
| |
| case BitstreamEntry::SubBlock: |
| if (Entry.ID == bitc::MODULE_BLOCK_ID) |
| return hasObjCCategoryInModule(Stream); |
| |
| // Ignore other sub-blocks. |
| if (Stream.SkipBlock()) |
| return error("Malformed block"); |
| continue; |
| |
| case BitstreamEntry::Record: |
| Stream.skipRecord(Entry.ID); |
| continue; |
| } |
| } |
| } |
| |
| static Expected<std::string> readModuleTriple(BitstreamCursor &Stream) { |
| if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) |
| return error("Invalid record"); |
| |
| SmallVector<uint64_t, 64> Record; |
| |
| std::string Triple; |
| |
| // Read all the records for this module. |
| while (true) { |
| BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| |
| switch (Entry.Kind) { |
| case BitstreamEntry::SubBlock: // Handled for us already. |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| return Triple; |
| case BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Read a record. |
| switch (Stream.readRecord(Entry.ID, Record)) { |
| default: break; // Default behavior, ignore unknown content. |
| case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] |
| std::string S; |
| if (convertToString(Record, 0, S)) |
| return error("Invalid record"); |
| Triple = S; |
| break; |
| } |
| } |
| Record.clear(); |
| } |
| llvm_unreachable("Exit infinite loop"); |
| } |
| |
| static Expected<std::string> readTriple(BitstreamCursor &Stream) { |
| // We expect a number of well-defined blocks, though we don't necessarily |
| // need to understand them all. |
| while (true) { |
| BitstreamEntry Entry = Stream.advance(); |
| |
| switch (Entry.Kind) { |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| return ""; |
| |
| case BitstreamEntry::SubBlock: |
| if (Entry.ID == bitc::MODULE_BLOCK_ID) |
| return readModuleTriple(Stream); |
| |
| // Ignore other sub-blocks. |
| if (Stream.SkipBlock()) |
| return error("Malformed block"); |
| continue; |
| |
| case BitstreamEntry::Record: |
| Stream.skipRecord(Entry.ID); |
| continue; |
| } |
| } |
| } |
| |
| namespace { |
| |
| class BitcodeReaderBase { |
| protected: |
| BitcodeReaderBase(BitstreamCursor Stream, StringRef Strtab) |
| : Stream(std::move(Stream)), Strtab(Strtab) { |
| this->Stream.setBlockInfo(&BlockInfo); |
| } |
| |
| BitstreamBlockInfo BlockInfo; |
| BitstreamCursor Stream; |
| StringRef Strtab; |
| |
| /// In version 2 of the bitcode we store names of global values and comdats in |
| /// a string table rather than in the VST. |
| bool UseStrtab = false; |
| |
| Expected<unsigned> parseVersionRecord(ArrayRef<uint64_t> Record); |
| |
| /// If this module uses a string table, pop the reference to the string table |
| /// and return the referenced string and the rest of the record. Otherwise |
| /// just return the record itself. |
| std::pair<StringRef, ArrayRef<uint64_t>> |
| readNameFromStrtab(ArrayRef<uint64_t> Record); |
| |
| bool readBlockInfo(); |
| |
| // Contains an arbitrary and optional string identifying the bitcode producer |
| std::string ProducerIdentification; |
| |
| Error error(const Twine &Message); |
| }; |
| |
| } // end anonymous namespace |
| |
| Error BitcodeReaderBase::error(const Twine &Message) { |
| std::string FullMsg = Message.str(); |
| if (!ProducerIdentification.empty()) |
| FullMsg += " (Producer: '" + ProducerIdentification + "' Reader: 'LLVM " + |
| LLVM_VERSION_STRING "')"; |
| return ::error(FullMsg); |
| } |
| |
| Expected<unsigned> |
| BitcodeReaderBase::parseVersionRecord(ArrayRef<uint64_t> Record) { |
| if (Record.empty()) |
| return error("Invalid record"); |
| unsigned ModuleVersion = Record[0]; |
| if (ModuleVersion > 2) |
| return error("Invalid value"); |
| UseStrtab = ModuleVersion >= 2; |
| return ModuleVersion; |
| } |
| |
| std::pair<StringRef, ArrayRef<uint64_t>> |
| BitcodeReaderBase::readNameFromStrtab(ArrayRef<uint64_t> Record) { |
| if (!UseStrtab) |
| return {"", Record}; |
| // Invalid reference. Let the caller complain about the record being empty. |
| if (Record[0] + Record[1] > Strtab.size()) |
| return {"", {}}; |
| return {StringRef(Strtab.data() + Record[0], Record[1]), Record.slice(2)}; |
| } |
| |
| namespace { |
| |
| class BitcodeReader : public BitcodeReaderBase, public GVMaterializer { |
| LLVMContext &Context; |
| Module *TheModule = nullptr; |
| // Next offset to start scanning for lazy parsing of function bodies. |
| uint64_t NextUnreadBit = 0; |
| // Last function offset found in the VST. |
| uint64_t LastFunctionBlockBit = 0; |
| bool SeenValueSymbolTable = false; |
| uint64_t VSTOffset = 0; |
| |
| std::vector<std::string> SectionTable; |
| std::vector<std::string> GCTable; |
| |
| std::vector<Type*> TypeList; |
| BitcodeReaderValueList ValueList; |
| Optional<MetadataLoader> MDLoader; |
| std::vector<Comdat *> ComdatList; |
| SmallVector<Instruction *, 64> InstructionList; |
| |
| std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInits; |
| std::vector<std::pair<GlobalIndirectSymbol *, unsigned>> IndirectSymbolInits; |
| std::vector<std::pair<Function *, unsigned>> FunctionPrefixes; |
| std::vector<std::pair<Function *, unsigned>> FunctionPrologues; |
| std::vector<std::pair<Function *, unsigned>> FunctionPersonalityFns; |
| |
| /// The set of attributes by index. Index zero in the file is for null, and |
| /// is thus not represented here. As such all indices are off by one. |
| std::vector<AttributeList> MAttributes; |
| |
| /// The set of attribute groups. |
| std::map<unsigned, AttributeList> MAttributeGroups; |
| |
| /// While parsing a function body, this is a list of the basic blocks for the |
| /// function. |
| std::vector<BasicBlock*> FunctionBBs; |
| |
| // When reading the module header, this list is populated with functions that |
| // have bodies later in the file. |
| std::vector<Function*> FunctionsWithBodies; |
| |
| // When intrinsic functions are encountered which require upgrading they are |
| // stored here with their replacement function. |
| using UpdatedIntrinsicMap = DenseMap<Function *, Function *>; |
| UpdatedIntrinsicMap UpgradedIntrinsics; |
| // Intrinsics which were remangled because of types rename |
| UpdatedIntrinsicMap RemangledIntrinsics; |
| |
| // Several operations happen after the module header has been read, but |
| // before function bodies are processed. This keeps track of whether |
| // we've done this yet. |
| bool SeenFirstFunctionBody = false; |
| |
| /// When function bodies are initially scanned, this map contains info about |
| /// where to find deferred function body in the stream. |
| DenseMap<Function*, uint64_t> DeferredFunctionInfo; |
| |
| /// When Metadata block is initially scanned when parsing the module, we may |
| /// choose to defer parsing of the metadata. This vector contains info about |
| /// which Metadata blocks are deferred. |
| std::vector<uint64_t> DeferredMetadataInfo; |
| |
| /// These are basic blocks forward-referenced by block addresses. They are |
| /// inserted lazily into functions when they're loaded. The basic block ID is |
| /// its index into the vector. |
| DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs; |
| std::deque<Function *> BasicBlockFwdRefQueue; |
| |
| /// Indicates that we are using a new encoding for instruction operands where |
| /// most operands in the current FUNCTION_BLOCK are encoded relative to the |
| /// instruction number, for a more compact encoding. Some instruction |
| /// operands are not relative to the instruction ID: basic block numbers, and |
| /// types. Once the old style function blocks have been phased out, we would |
| /// not need this flag. |
| bool UseRelativeIDs = false; |
| |
| /// True if all functions will be materialized, negating the need to process |
| /// (e.g.) blockaddress forward references. |
| bool WillMaterializeAllForwardRefs = false; |
| |
| bool StripDebugInfo = false; |
| TBAAVerifier TBAAVerifyHelper; |
| |
| std::vector<std::string> BundleTags; |
| SmallVector<SyncScope::ID, 8> SSIDs; |
| |
| public: |
| BitcodeReader(BitstreamCursor Stream, StringRef Strtab, |
| StringRef ProducerIdentification, LLVMContext &Context); |
| |
| Error materializeForwardReferencedFunctions(); |
| |
| Error materialize(GlobalValue *GV) override; |
| Error materializeModule() override; |
| std::vector<StructType *> getIdentifiedStructTypes() const override; |
| |
| /// \brief Main interface to parsing a bitcode buffer. |
| /// \returns true if an error occurred. |
| Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata = false, |
| bool IsImporting = false); |
| |
| static uint64_t decodeSignRotatedValue(uint64_t V); |
| |
| /// Materialize any deferred Metadata block. |
| Error materializeMetadata() override; |
| |
| void setStripDebugInfo() override; |
| |
| private: |
| std::vector<StructType *> IdentifiedStructTypes; |
| StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); |
| StructType *createIdentifiedStructType(LLVMContext &Context); |
| |
| Type *getTypeByID(unsigned ID); |
| |
| Value *getFnValueByID(unsigned ID, Type *Ty) { |
| if (Ty && Ty->isMetadataTy()) |
| return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); |
| return ValueList.getValueFwdRef(ID, Ty); |
| } |
| |
| Metadata *getFnMetadataByID(unsigned ID) { |
| return MDLoader->getMetadataFwdRefOrLoad(ID); |
| } |
| |
| BasicBlock *getBasicBlock(unsigned ID) const { |
| if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID |
| return FunctionBBs[ID]; |
| } |
| |
| AttributeList getAttributes(unsigned i) const { |
| if (i-1 < MAttributes.size()) |
| return MAttributes[i-1]; |
| return AttributeList(); |
| } |
| |
| /// Read a value/type pair out of the specified record from slot 'Slot'. |
| /// Increment Slot past the number of slots used in the record. Return true on |
| /// failure. |
| bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
| unsigned InstNum, Value *&ResVal) { |
| if (Slot == Record.size()) return true; |
| unsigned ValNo = (unsigned)Record[Slot++]; |
| // Adjust the ValNo, if it was encoded relative to the InstNum. |
| if (UseRelativeIDs) |
| ValNo = InstNum - ValNo; |
| if (ValNo < InstNum) { |
| // If this is not a forward reference, just return the value we already |
| // have. |
| ResVal = getFnValueByID(ValNo, nullptr); |
| return ResVal == nullptr; |
| } |
| if (Slot == Record.size()) |
| return true; |
| |
| unsigned TypeNo = (unsigned)Record[Slot++]; |
| ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); |
| return ResVal == nullptr; |
| } |
| |
| /// Read a value out of the specified record from slot 'Slot'. Increment Slot |
| /// past the number of slots used by the value in the record. Return true if |
| /// there is an error. |
| bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot, |
| unsigned InstNum, Type *Ty, Value *&ResVal) { |
| if (getValue(Record, Slot, InstNum, Ty, ResVal)) |
| return true; |
| // All values currently take a single record slot. |
| ++Slot; |
| return false; |
| } |
| |
| /// Like popValue, but does not increment the Slot number. |
| bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
| unsigned InstNum, Type *Ty, Value *&ResVal) { |
| ResVal = getValue(Record, Slot, InstNum, Ty); |
| return ResVal == nullptr; |
| } |
| |
| /// Version of getValue that returns ResVal directly, or 0 if there is an |
| /// error. |
| Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
| unsigned InstNum, Type *Ty) { |
| if (Slot == Record.size()) return nullptr; |
| unsigned ValNo = (unsigned)Record[Slot]; |
| // Adjust the ValNo, if it was encoded relative to the InstNum. |
| if (UseRelativeIDs) |
| ValNo = InstNum - ValNo; |
| return getFnValueByID(ValNo, Ty); |
| } |
| |
| /// Like getValue, but decodes signed VBRs. |
| Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot, |
| unsigned InstNum, Type *Ty) { |
| if (Slot == Record.size()) return nullptr; |
| unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]); |
| // Adjust the ValNo, if it was encoded relative to the InstNum. |
| if (UseRelativeIDs) |
| ValNo = InstNum - ValNo; |
| return getFnValueByID(ValNo, Ty); |
| } |
| |
| /// Converts alignment exponent (i.e. power of two (or zero)) to the |
| /// corresponding alignment to use. If alignment is too large, returns |
| /// a corresponding error code. |
| Error parseAlignmentValue(uint64_t Exponent, unsigned &Alignment); |
| Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); |
| Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false); |
| |
| Error parseComdatRecord(ArrayRef<uint64_t> Record); |
| Error parseGlobalVarRecord(ArrayRef<uint64_t> Record); |
| Error parseFunctionRecord(ArrayRef<uint64_t> Record); |
| Error parseGlobalIndirectSymbolRecord(unsigned BitCode, |
| ArrayRef<uint64_t> Record); |
| |
| Error parseAttributeBlock(); |
| Error parseAttributeGroupBlock(); |
| Error parseTypeTable(); |
| Error parseTypeTableBody(); |
| Error parseOperandBundleTags(); |
| Error parseSyncScopeNames(); |
| |
| Expected<Value *> recordValue(SmallVectorImpl<uint64_t> &Record, |
| unsigned NameIndex, Triple &TT); |
| void setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, Function *F, |
| ArrayRef<uint64_t> Record); |
| Error parseValueSymbolTable(uint64_t Offset = 0); |
| Error parseGlobalValueSymbolTable(); |
| Error parseConstants(); |
| Error rememberAndSkipFunctionBodies(); |
| Error rememberAndSkipFunctionBody(); |
| /// Save the positions of the Metadata blocks and skip parsing the blocks. |
| Error rememberAndSkipMetadata(); |
| Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType); |
| Error parseFunctionBody(Function *F); |
| Error globalCleanup(); |
| Error resolveGlobalAndIndirectSymbolInits(); |
| Error parseUseLists(); |
| Error findFunctionInStream( |
| Function *F, |
| DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator); |
| |
| SyncScope::ID getDecodedSyncScopeID(unsigned Val); |
| }; |
| |
| /// Class to manage reading and parsing function summary index bitcode |
| /// files/sections. |
| class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase { |
| /// The module index built during parsing. |
| ModuleSummaryIndex &TheIndex; |
| |
| /// Indicates whether we have encountered a global value summary section |
| /// yet during parsing. |
| bool SeenGlobalValSummary = false; |
| |
| /// Indicates whether we have already parsed the VST, used for error checking. |
| bool SeenValueSymbolTable = false; |
| |
| /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record. |
| /// Used to enable on-demand parsing of the VST. |
| uint64_t VSTOffset = 0; |
| |
| // Map to save ValueId to ValueInfo association that was recorded in the |
| // ValueSymbolTable. It is used after the VST is parsed to convert |
| // call graph edges read from the function summary from referencing |
| // callees by their ValueId to using the ValueInfo instead, which is how |
| // they are recorded in the summary index being built. |
| // We save a GUID which refers to the same global as the ValueInfo, but |
| // ignoring the linkage, i.e. for values other than local linkage they are |
| // identical. |
| DenseMap<unsigned, std::pair<ValueInfo, GlobalValue::GUID>> |
| ValueIdToValueInfoMap; |
| |
| /// Map populated during module path string table parsing, from the |
| /// module ID to a string reference owned by the index's module |
| /// path string table, used to correlate with combined index |
| /// summary records. |
| DenseMap<uint64_t, StringRef> ModuleIdMap; |
| |
| /// Original source file name recorded in a bitcode record. |
| std::string SourceFileName; |
| |
| /// The string identifier given to this module by the client, normally the |
| /// path to the bitcode file. |
| StringRef ModulePath; |
| |
| /// For per-module summary indexes, the unique numerical identifier given to |
| /// this module by the client. |
| unsigned ModuleId; |
| |
| public: |
| ModuleSummaryIndexBitcodeReader(BitstreamCursor Stream, StringRef Strtab, |
| ModuleSummaryIndex &TheIndex, |
| StringRef ModulePath, unsigned ModuleId); |
| |
| Error parseModule(); |
| |
| private: |
| void setValueGUID(uint64_t ValueID, StringRef ValueName, |
| GlobalValue::LinkageTypes Linkage, |
| StringRef SourceFileName); |
| Error parseValueSymbolTable( |
| uint64_t Offset, |
| DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap); |
| std::vector<ValueInfo> makeRefList(ArrayRef<uint64_t> Record); |
| std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record, |
| bool IsOldProfileFormat, |
| bool HasProfile); |
| Error parseEntireSummary(unsigned ID); |
| Error parseModuleStringTable(); |
| |
| std::pair<ValueInfo, GlobalValue::GUID> |
| getValueInfoFromValueId(unsigned ValueId); |
| |
| ModuleSummaryIndex::ModuleInfo *addThisModule(); |
| }; |
| |
| } // end anonymous namespace |
| |
| std::error_code llvm::errorToErrorCodeAndEmitErrors(LLVMContext &Ctx, |
| Error Err) { |
| if (Err) { |
| std::error_code EC; |
| handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { |
| EC = EIB.convertToErrorCode(); |
| Ctx.emitError(EIB.message()); |
| }); |
| return EC; |
| } |
| return std::error_code(); |
| } |
| |
| BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab, |
| StringRef ProducerIdentification, |
| LLVMContext &Context) |
| : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context), |
| ValueList(Context) { |
| this->ProducerIdentification = ProducerIdentification; |
| } |
| |
| Error BitcodeReader::materializeForwardReferencedFunctions() { |
| if (WillMaterializeAllForwardRefs) |
| return Error::success(); |
| |
| // Prevent recursion. |
| WillMaterializeAllForwardRefs = true; |
| |
| while (!BasicBlockFwdRefQueue.empty()) { |
| Function *F = BasicBlockFwdRefQueue.front(); |
| BasicBlockFwdRefQueue.pop_front(); |
| assert(F && "Expected valid function"); |
| if (!BasicBlockFwdRefs.count(F)) |
| // Already materialized. |
| continue; |
| |
| // Check for a function that isn't materializable to prevent an infinite |
| // loop. When parsing a blockaddress stored in a global variable, there |
| // isn't a trivial way to check if a function will have a body without a |
| // linear search through FunctionsWithBodies, so just check it here. |
| if (!F->isMaterializable()) |
| return error("Never resolved function from blockaddress"); |
| |
| // Try to materialize F. |
| if (Error Err = materialize(F)) |
| return Err; |
| } |
| assert(BasicBlockFwdRefs.empty() && "Function missing from queue"); |
| |
| // Reset state. |
| WillMaterializeAllForwardRefs = false; |
| return Error::success(); |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // Helper functions to implement forward reference resolution, etc. |
| //===----------------------------------------------------------------------===// |
| |
| static bool hasImplicitComdat(size_t Val) { |
| switch (Val) { |
| default: |
| return false; |
| case 1: // Old WeakAnyLinkage |
| case 4: // Old LinkOnceAnyLinkage |
| case 10: // Old WeakODRLinkage |
| case 11: // Old LinkOnceODRLinkage |
| return true; |
| } |
| } |
| |
| static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { |
| switch (Val) { |
| default: // Map unknown/new linkages to external |
| case 0: |
| return GlobalValue::ExternalLinkage; |
| case 2: |
| return GlobalValue::AppendingLinkage; |
| case 3: |
| return GlobalValue::InternalLinkage; |
| case 5: |
| return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage |
| case 6: |
| return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage |
| case 7: |
| return GlobalValue::ExternalWeakLinkage; |
| case 8: |
| return GlobalValue::CommonLinkage; |
| case 9: |
| return GlobalValue::PrivateLinkage; |
| case 12: |
| return GlobalValue::AvailableExternallyLinkage; |
| case 13: |
| return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage |
| case 14: |
| return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage |
| case 15: |
| return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage |
| case 1: // Old value with implicit comdat. |
| case 16: |
| return GlobalValue::WeakAnyLinkage; |
| case 10: // Old value with implicit comdat. |
| case 17: |
| return GlobalValue::WeakODRLinkage; |
| case 4: // Old value with implicit comdat. |
| case 18: |
| return GlobalValue::LinkOnceAnyLinkage; |
| case 11: // Old value with implicit comdat. |
| case 19: |
| return GlobalValue::LinkOnceODRLinkage; |
| } |
| } |
| |
| static FunctionSummary::FFlags getDecodedFFlags(uint64_t RawFlags) { |
| FunctionSummary::FFlags Flags; |
| Flags.ReadNone = RawFlags & 0x1; |
| Flags.ReadOnly = (RawFlags >> 1) & 0x1; |
| Flags.NoRecurse = (RawFlags >> 2) & 0x1; |
| Flags.ReturnDoesNotAlias = (RawFlags >> 3) & 0x1; |
| return Flags; |
| } |
| |
| /// Decode the flags for GlobalValue in the summary. |
| static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, |
| uint64_t Version) { |
| // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage |
| // like getDecodedLinkage() above. Any future change to the linkage enum and |
| // to getDecodedLinkage() will need to be taken into account here as above. |
| auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits |
| RawFlags = RawFlags >> 4; |
| bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3; |
| // The Live flag wasn't introduced until version 3. For dead stripping |
| // to work correctly on earlier versions, we must conservatively treat all |
| // values as live. |
| bool Live = (RawFlags & 0x2) || Version < 3; |
| bool Local = (RawFlags & 0x4); |
| |
| return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, Live, Local); |
| } |
| |
| static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { |
| switch (Val) { |
| default: // Map unknown visibilities to default. |
| case 0: return GlobalValue::DefaultVisibility; |
| case 1: return GlobalValue::HiddenVisibility; |
| case 2: return GlobalValue::ProtectedVisibility; |
| } |
| } |
| |
| static GlobalValue::DLLStorageClassTypes |
| getDecodedDLLStorageClass(unsigned Val) { |
| switch (Val) { |
| default: // Map unknown values to default. |
| case 0: return GlobalValue::DefaultStorageClass; |
| case 1: return GlobalValue::DLLImportStorageClass; |
| case 2: return GlobalValue::DLLExportStorageClass; |
| } |
| } |
| |
| static bool getDecodedDSOLocal(unsigned Val) { |
| switch(Val) { |
| default: // Map unknown values to preemptable. |
| case 0: return false; |
| case 1: return true; |
| } |
| } |
| |
| static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) { |
| switch (Val) { |
| case 0: return GlobalVariable::NotThreadLocal; |
| default: // Map unknown non-zero value to general dynamic. |
| case 1: return GlobalVariable::GeneralDynamicTLSModel; |
| case 2: return GlobalVariable::LocalDynamicTLSModel; |
| case 3: return GlobalVariable::InitialExecTLSModel; |
| case 4: return GlobalVariable::LocalExecTLSModel; |
| } |
| } |
| |
| static GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) { |
| switch (Val) { |
| default: // Map unknown to UnnamedAddr::None. |
| case 0: return GlobalVariable::UnnamedAddr::None; |
| case 1: return GlobalVariable::UnnamedAddr::Global; |
| case 2: return GlobalVariable::UnnamedAddr::Local; |
| } |
| } |
| |
| static int getDecodedCastOpcode(unsigned Val) { |
| switch (Val) { |
| default: return -1; |
| case bitc::CAST_TRUNC : return Instruction::Trunc; |
| case bitc::CAST_ZEXT : return Instruction::ZExt; |
| case bitc::CAST_SEXT : return Instruction::SExt; |
| case bitc::CAST_FPTOUI : return Instruction::FPToUI; |
| case bitc::CAST_FPTOSI : return Instruction::FPToSI; |
| case bitc::CAST_UITOFP : return Instruction::UIToFP; |
| case bitc::CAST_SITOFP : return Instruction::SIToFP; |
| case bitc::CAST_FPTRUNC : return Instruction::FPTrunc; |
| case bitc::CAST_FPEXT : return Instruction::FPExt; |
| case bitc::CAST_PTRTOINT: return Instruction::PtrToInt; |
| case bitc::CAST_INTTOPTR: return Instruction::IntToPtr; |
| case bitc::CAST_BITCAST : return Instruction::BitCast; |
| case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast; |
| } |
| } |
| |
| static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) { |
| bool IsFP = Ty->isFPOrFPVectorTy(); |
| // BinOps are only valid for int/fp or vector of int/fp types |
| if (!IsFP && !Ty->isIntOrIntVectorTy()) |
| return -1; |
| |
| switch (Val) { |
| default: |
| return -1; |
| case bitc::BINOP_ADD: |
| return IsFP ? Instruction::FAdd : Instruction::Add; |
| case bitc::BINOP_SUB: |
| return IsFP ? Instruction::FSub : Instruction::Sub; |
| case bitc::BINOP_MUL: |
| return IsFP ? Instruction::FMul : Instruction::Mul; |
| case bitc::BINOP_UDIV: |
| return IsFP ? -1 : Instruction::UDiv; |
| case bitc::BINOP_SDIV: |
| return IsFP ? Instruction::FDiv : Instruction::SDiv; |
| case bitc::BINOP_UREM: |
| return IsFP ? -1 : Instruction::URem; |
| case bitc::BINOP_SREM: |
| return IsFP ? Instruction::FRem : Instruction::SRem; |
| case bitc::BINOP_SHL: |
| return IsFP ? -1 : Instruction::Shl; |
| case bitc::BINOP_LSHR: |
| return IsFP ? -1 : Instruction::LShr; |
| case bitc::BINOP_ASHR: |
| return IsFP ? -1 : Instruction::AShr; |
| case bitc::BINOP_AND: |
| return IsFP ? -1 : Instruction::And; |
| case bitc::BINOP_OR: |
| return IsFP ? -1 : Instruction::Or; |
| case bitc::BINOP_XOR: |
| return IsFP ? -1 : Instruction::Xor; |
| } |
| } |
| |
| static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) { |
| switch (Val) { |
| default: return AtomicRMWInst::BAD_BINOP; |
| case bitc::RMW_XCHG: return AtomicRMWInst::Xchg; |
| case bitc::RMW_ADD: return AtomicRMWInst::Add; |
| case bitc::RMW_SUB: return AtomicRMWInst::Sub; |
| case bitc::RMW_AND: return AtomicRMWInst::And; |
| case bitc::RMW_NAND: return AtomicRMWInst::Nand; |
| case bitc::RMW_OR: return AtomicRMWInst::Or; |
| case bitc::RMW_XOR: return AtomicRMWInst::Xor; |
| case bitc::RMW_MAX: return AtomicRMWInst::Max; |
| case bitc::RMW_MIN: return AtomicRMWInst::Min; |
| case bitc::RMW_UMAX: return AtomicRMWInst::UMax; |
| case bitc::RMW_UMIN: return AtomicRMWInst::UMin; |
| } |
| } |
| |
| static AtomicOrdering getDecodedOrdering(unsigned Val) { |
| switch (Val) { |
| case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic; |
| case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered; |
| case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic; |
| case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire; |
| case bitc::ORDERING_RELEASE: return AtomicOrdering::Release; |
| case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease; |
| default: // Map unknown orderings to sequentially-consistent. |
| case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent; |
| } |
| } |
| |
| static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) { |
| switch (Val) { |
| default: // Map unknown selection kinds to any. |
| case bitc::COMDAT_SELECTION_KIND_ANY: |
| return Comdat::Any; |
| case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH: |
| return Comdat::ExactMatch; |
| case bitc::COMDAT_SELECTION_KIND_LARGEST: |
| return Comdat::Largest; |
| case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES: |
| return Comdat::NoDuplicates; |
| case bitc::COMDAT_SELECTION_KIND_SAME_SIZE: |
| return Comdat::SameSize; |
| } |
| } |
| |
| static FastMathFlags getDecodedFastMathFlags(unsigned Val) { |
| FastMathFlags FMF; |
| if (0 != (Val & FastMathFlags::AllowReassoc)) |
| FMF.setAllowReassoc(); |
| if (0 != (Val & FastMathFlags::NoNaNs)) |
| FMF.setNoNaNs(); |
| if (0 != (Val & FastMathFlags::NoInfs)) |
| FMF.setNoInfs(); |
| if (0 != (Val & FastMathFlags::NoSignedZeros)) |
| FMF.setNoSignedZeros(); |
| if (0 != (Val & FastMathFlags::AllowReciprocal)) |
| FMF.setAllowReciprocal(); |
| if (0 != (Val & FastMathFlags::AllowContract)) |
| FMF.setAllowContract(true); |
| if (0 != (Val & FastMathFlags::ApproxFunc)) |
| FMF.setApproxFunc(); |
| return FMF; |
| } |
| |
| static void upgradeDLLImportExportLinkage(GlobalValue *GV, unsigned Val) { |
| switch (Val) { |
| case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break; |
| case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break; |
| } |
| } |
| |
| Type *BitcodeReader::getTypeByID(unsigned ID) { |
| // The type table size is always specified correctly. |
| if (ID >= TypeList.size()) |
| return nullptr; |
| |
| if (Type *Ty = TypeList[ID]) |
| return Ty; |
| |
| // If we have a forward reference, the only possible case is when it is to a |
| // named struct. Just create a placeholder for now. |
| return TypeList[ID] = createIdentifiedStructType(Context); |
| } |
| |
| StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, |
| StringRef Name) { |
| auto *Ret = StructType::create(Context, Name); |
| IdentifiedStructTypes.push_back(Ret); |
| return Ret; |
| } |
| |
| StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { |
| auto *Ret = StructType::create(Context); |
| IdentifiedStructTypes.push_back(Ret); |
| return Ret; |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // Functions for parsing blocks from the bitcode file |
| //===----------------------------------------------------------------------===// |
| |
| static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { |
| switch (Val) { |
| case Attribute::EndAttrKinds: |
| llvm_unreachable("Synthetic enumerators which should never get here"); |
| |
| case Attribute::None: return 0; |
| case Attribute::ZExt: return 1 << 0; |
| case Attribute::SExt: return 1 << 1; |
| case Attribute::NoReturn: return 1 << 2; |
| case Attribute::InReg: return 1 << 3; |
| case Attribute::StructRet: return 1 << 4; |
| case Attribute::NoUnwind: return 1 << 5; |
| case Attribute::NoAlias: return 1 << 6; |
| case Attribute::ByVal: return 1 << 7; |
| case Attribute::Nest: return 1 << 8; |
| case Attribute::ReadNone: return 1 << 9; |
| case Attribute::ReadOnly: return 1 << 10; |
| case Attribute::NoInline: return 1 << 11; |
| case Attribute::AlwaysInline: return 1 << 12; |
| case Attribute::OptimizeForSize: return 1 << 13; |
| case Attribute::StackProtect: return 1 << 14; |
| case Attribute::StackProtectReq: return 1 << 15; |
| case Attribute::Alignment: return 31 << 16; |
| case Attribute::NoCapture: return 1 << 21; |
| case Attribute::NoRedZone: return 1 << 22; |
| case Attribute::NoImplicitFloat: return 1 << 23; |
| case Attribute::Naked: return 1 << 24; |
| case Attribute::InlineHint: return 1 << 25; |
| case Attribute::StackAlignment: return 7 << 26; |
| case Attribute::ReturnsTwice: return 1 << 29; |
| case Attribute::UWTable: return 1 << 30; |
| case Attribute::NonLazyBind: return 1U << 31; |
| case Attribute::SanitizeAddress: return 1ULL << 32; |
| case Attribute::MinSize: return 1ULL << 33; |
| case Attribute::NoDuplicate: return 1ULL << 34; |
| case Attribute::StackProtectStrong: return 1ULL << 35; |
| case Attribute::SanitizeThread: return 1ULL << 36; |
| case Attribute::SanitizeMemory: return 1ULL << 37; |
| case Attribute::NoBuiltin: return 1ULL << 38; |
| case Attribute::Returned: return 1ULL << 39; |
| case Attribute::Cold: return 1ULL << 40; |
| case Attribute::Builtin: return 1ULL << 41; |
| case Attribute::OptimizeNone: return 1ULL << 42; |
| case Attribute::InAlloca: return 1ULL << 43; |
| case Attribute::NonNull: return 1ULL << 44; |
| case Attribute::JumpTable: return 1ULL << 45; |
| case Attribute::Convergent: return 1ULL << 46; |
| case Attribute::SafeStack: return 1ULL << 47; |
| case Attribute::NoRecurse: return 1ULL << 48; |
| case Attribute::InaccessibleMemOnly: return 1ULL << 49; |
| case Attribute::InaccessibleMemOrArgMemOnly: return 1ULL << 50; |
| case Attribute::SwiftSelf: return 1ULL << 51; |
| case Attribute::SwiftError: return 1ULL << 52; |
| case Attribute::WriteOnly: return 1ULL << 53; |
| case Attribute::Speculatable: return 1ULL << 54; |
| case Attribute::StrictFP: return 1ULL << 55; |
| case Attribute::Dereferenceable: |
| llvm_unreachable("dereferenceable attribute not supported in raw format"); |
| break; |
| case Attribute::DereferenceableOrNull: |
| llvm_unreachable("dereferenceable_or_null attribute not supported in raw " |
| "format"); |
| break; |
| case Attribute::ArgMemOnly: |
| llvm_unreachable("argmemonly attribute not supported in raw format"); |
| break; |
| case Attribute::AllocSize: |
| llvm_unreachable("allocsize not supported in raw format"); |
| break; |
| } |
| llvm_unreachable("Unsupported attribute type"); |
| } |
| |
| static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { |
| if (!Val) return; |
| |
| for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; |
| I = Attribute::AttrKind(I + 1)) { |
| if (I == Attribute::Dereferenceable || |
| I == Attribute::DereferenceableOrNull || |
| I == Attribute::ArgMemOnly || |
| I == Attribute::AllocSize) |
| continue; |
| if (uint64_t A = (Val & getRawAttributeMask(I))) { |
| if (I == Attribute::Alignment) |
| B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); |
| else if (I == Attribute::StackAlignment) |
| B.addStackAlignmentAttr(1ULL << ((A >> 26)-1)); |
| else |
| B.addAttribute(I); |
| } |
| } |
| } |
| |
| /// \brief This fills an AttrBuilder object with the LLVM attributes that have |
| /// been decoded from the given integer. This function must stay in sync with |
| /// 'encodeLLVMAttributesForBitcode'. |
| static void decodeLLVMAttributesForBitcode(AttrBuilder &B, |
| uint64_t EncodedAttrs) { |
| // FIXME: Remove in 4.0. |
| |
| // The alignment is stored as a 16-bit raw value from bits 31--16. We shift |
| // the bits above 31 down by 11 bits. |
| unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; |
| assert((!Alignment || isPowerOf2_32(Alignment)) && |
| "Alignment must be a power of two."); |
| |
| if (Alignment) |
| B.addAlignmentAttr(Alignment); |
| addRawAttributeValue(B, ((EncodedAttrs & (0xfffffULL << 32)) >> 11) | |
| (EncodedAttrs & 0xffff)); |
| } |
| |
| Error BitcodeReader::parseAttributeBlock() { |
| if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) |
| return error("Invalid record"); |
| |
| if (!MAttributes.empty()) |
| return error("Invalid multiple blocks"); |
| |
| SmallVector<uint64_t, 64> Record; |
| |
| SmallVector<AttributeList, 8> Attrs; |
| |
| // Read all the records. |
| while (true) { |
| BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| |
| switch (Entry.Kind) { |
| case BitstreamEntry::SubBlock: // Handled for us already. |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| return Error::success(); |
| case BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Read a record. |
| Record.clear(); |
| switch (Stream.readRecord(Entry.ID, Record)) { |
| default: // Default behavior: ignore. |
| break; |
| case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...] |
| // FIXME: Remove in 4.0. |
| if (Record.size() & 1) |
| return error("Invalid record"); |
| |
| for (unsigned i = 0, e = Record.size(); i != e; i += 2) { |
| AttrBuilder B; |
| decodeLLVMAttributesForBitcode(B, Record[i+1]); |
| Attrs.push_back(AttributeList::get(Context, Record[i], B)); |
| } |
| |
| MAttributes.push_back(AttributeList::get(Context, Attrs)); |
| Attrs.clear(); |
| break; |
| case bitc::PARAMATTR_CODE_ENTRY: // ENTRY: [attrgrp0, attrgrp1, ...] |
| for (unsigned i = 0, e = Record.size(); i != e; ++i) |
| Attrs.push_back(MAttributeGroups[Record[i]]); |
| |
| MAttributes.push_back(AttributeList::get(Context, Attrs)); |
| Attrs.clear(); |
| break; |
| } |
| } |
| } |
| |
| // Returns Attribute::None on unrecognized codes. |
| static Attribute::AttrKind getAttrFromCode(uint64_t Code) { |
| switch (Code) { |
| default: |
| return Attribute::None; |
| case bitc::ATTR_KIND_ALIGNMENT: |
| return Attribute::Alignment; |
| case bitc::ATTR_KIND_ALWAYS_INLINE: |
| return Attribute::AlwaysInline; |
| case bitc::ATTR_KIND_ARGMEMONLY: |
| return Attribute::ArgMemOnly; |
| case bitc::ATTR_KIND_BUILTIN: |
| return Attribute::Builtin; |
| case bitc::ATTR_KIND_BY_VAL: |
| return Attribute::ByVal; |
| case bitc::ATTR_KIND_IN_ALLOCA: |
| return Attribute::InAlloca; |
| case bitc::ATTR_KIND_COLD: |
| return Attribute::Cold; |
| case bitc::ATTR_KIND_CONVERGENT: |
| return Attribute::Convergent; |
| case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY: |
| return Attribute::InaccessibleMemOnly; |
| case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY: |
| return Attribute::InaccessibleMemOrArgMemOnly; |
| case bitc::ATTR_KIND_INLINE_HINT: |
| return Attribute::InlineHint; |
| case bitc::ATTR_KIND_IN_REG: |
| return Attribute::InReg; |
| case bitc::ATTR_KIND_JUMP_TABLE: |
| return Attribute::JumpTable; |
| case bitc::ATTR_KIND_MIN_SIZE: |
| return Attribute::MinSize; |
| case bitc::ATTR_KIND_NAKED: |
| return Attribute::Naked; |
| case bitc::ATTR_KIND_NEST: |
| return Attribute::Nest; |
| case bitc::ATTR_KIND_NO_ALIAS: |
| return Attribute::NoAlias; |
| case bitc::ATTR_KIND_NO_BUILTIN: |
| return Attribute::NoBuiltin; |
| case bitc::ATTR_KIND_NO_CAPTURE: |
| return Attribute::NoCapture; |
| case bitc::ATTR_KIND_NO_DUPLICATE: |
| return Attribute::NoDuplicate; |
| case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT: |
| return Attribute::NoImplicitFloat; |
| case bitc::ATTR_KIND_NO_INLINE: |
| return Attribute::NoInline; |
| case bitc::ATTR_KIND_NO_RECURSE: |
| return Attribute::NoRecurse; |
| case bitc::ATTR_KIND_NON_LAZY_BIND: |
| return Attribute::NonLazyBind; |
| case bitc::ATTR_KIND_NON_NULL: |
| return Attribute::NonNull; |
| case bitc::ATTR_KIND_DEREFERENCEABLE: |
| return Attribute::Dereferenceable; |
| case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL: |
| return Attribute::DereferenceableOrNull; |
| case bitc::ATTR_KIND_ALLOC_SIZE: |
| return Attribute::AllocSize; |
| case bitc::ATTR_KIND_NO_RED_ZONE: |
| return Attribute::NoRedZone; |
| case bitc::ATTR_KIND_NO_RETURN: |
| return Attribute::NoReturn; |
| case bitc::ATTR_KIND_NO_UNWIND: |
| return Attribute::NoUnwind; |
| case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: |
| return Attribute::OptimizeForSize; |
| case bitc::ATTR_KIND_OPTIMIZE_NONE: |
| return Attribute::OptimizeNone; |
| case bitc::ATTR_KIND_READ_NONE: |
| return Attribute::ReadNone; |
| case bitc::ATTR_KIND_READ_ONLY: |
| return Attribute::ReadOnly; |
| case bitc::ATTR_KIND_RETURNED: |
| return Attribute::Returned; |
| case bitc::ATTR_KIND_RETURNS_TWICE: |
| return Attribute::ReturnsTwice; |
| case bitc::ATTR_KIND_S_EXT: |
| return Attribute::SExt; |
| case bitc::ATTR_KIND_SPECULATABLE: |
| return Attribute::Speculatable; |
| case bitc::ATTR_KIND_STACK_ALIGNMENT: |
| return Attribute::StackAlignment; |
| case bitc::ATTR_KIND_STACK_PROTECT: |
| return Attribute::StackProtect; |
| case bitc::ATTR_KIND_STACK_PROTECT_REQ: |
| return Attribute::StackProtectReq; |
| case bitc::ATTR_KIND_STACK_PROTECT_STRONG: |
| return Attribute::StackProtectStrong; |
| case bitc::ATTR_KIND_SAFESTACK: |
| return Attribute::SafeStack; |
| case bitc::ATTR_KIND_STRICT_FP: |
| return Attribute::StrictFP; |
| case bitc::ATTR_KIND_STRUCT_RET: |
| return Attribute::StructRet; |
| case bitc::ATTR_KIND_SANITIZE_ADDRESS: |
| return Attribute::SanitizeAddress; |
| case bitc::ATTR_KIND_SANITIZE_THREAD: |
| return Attribute::SanitizeThread; |
| case bitc::ATTR_KIND_SANITIZE_MEMORY: |
| return Attribute::SanitizeMemory; |
| case bitc::ATTR_KIND_SWIFT_ERROR: |
| return Attribute::SwiftError; |
| case bitc::ATTR_KIND_SWIFT_SELF: |
| return Attribute::SwiftSelf; |
| case bitc::ATTR_KIND_UW_TABLE: |
| return Attribute::UWTable; |
| case bitc::ATTR_KIND_WRITEONLY: |
| return Attribute::WriteOnly; |
| case bitc::ATTR_KIND_Z_EXT: |
| return Attribute::ZExt; |
| } |
| } |
| |
| Error BitcodeReader::parseAlignmentValue(uint64_t Exponent, |
| unsigned &Alignment) { |
| // Note: Alignment in bitcode files is incremented by 1, so that zero |
| // can be used for default alignment. |
| if (Exponent > Value::MaxAlignmentExponent + 1) |
| return error("Invalid alignment value"); |
| Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1; |
| return Error::success(); |
| } |
| |
| Error BitcodeReader::parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { |
| *Kind = getAttrFromCode(Code); |
| if (*Kind == Attribute::None) |
| return error("Unknown attribute kind (" + Twine(Code) + ")"); |
| return Error::success(); |
| } |
| |
| Error BitcodeReader::parseAttributeGroupBlock() { |
| if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) |
| return error("Invalid record"); |
| |
| if (!MAttributeGroups.empty()) |
| return error("Invalid multiple blocks"); |
| |
| SmallVector<uint64_t, 64> Record; |
| |
| // Read all the records. |
| while (true) { |
| BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| |
| switch (Entry.Kind) { |
| case BitstreamEntry::SubBlock: // Handled for us already. |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| return Error::success(); |
| case BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Read a record. |
| Record.clear(); |
| switch (Stream.readRecord(Entry.ID, Record)) { |
| default: // Default behavior: ignore. |
| break; |
| case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] |
| if (Record.size() < 3) |
| return error("Invalid record"); |
| |
| uint64_t GrpID = Record[0]; |
| uint64_t Idx = Record[1]; // Index of the object this attribute refers to. |
| |
| AttrBuilder B; |
| for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
| if (Record[i] == 0) { // Enum attribute |
| Attribute::AttrKind Kind; |
| if (Error Err = parseAttrKind(Record[++i], &Kind)) |
| return Err; |
| |
| B.addAttribute(Kind); |
| } else if (Record[i] == 1) { // Integer attribute |
| Attribute::AttrKind Kind; |
| if (Error Err = parseAttrKind(Record[++i], &Kind)) |
| return Err; |
| if (Kind == Attribute::Alignment) |
| B.addAlignmentAttr(Record[++i]); |
| else if (Kind == Attribute::StackAlignment) |
| B.addStackAlignmentAttr(Record[++i]); |
| else if (Kind == Attribute::Dereferenceable) |
| B.addDereferenceableAttr(Record[++i]); |
| else if (Kind == Attribute::DereferenceableOrNull) |
| B.addDereferenceableOrNullAttr(Record[++i]); |
| else if (Kind == Attribute::AllocSize) |
| B.addAllocSizeAttrFromRawRepr(Record[++i]); |
| } else { // String attribute |
| assert((Record[i] == 3 || Record[i] == 4) && |
| "Invalid attribute group entry"); |
| bool HasValue = (Record[i++] == 4); |
| SmallString<64> KindStr; |
| SmallString<64> ValStr; |
| |
| while (Record[i] != 0 && i != e) |
| KindStr += Record[i++]; |
| assert(Record[i] == 0 && "Kind string not null terminated"); |
| |
| if (HasValue) { |
| // Has a value associated with it. |
| ++i; // Skip the '0' that terminates the "kind" string. |
| while (Record[i] != 0 && i != e) |
| ValStr += Record[i++]; |
| assert(Record[i] == 0 && "Value string not null terminated"); |
| } |
| |
| B.addAttribute(KindStr.str(), ValStr.str()); |
| } |
| } |
| |
| MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B); |
| break; |
| } |
| } |
| } |
| } |
| |
| Error BitcodeReader::parseTypeTable() { |
| if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) |
| return error("Invalid record"); |
| |
| return parseTypeTableBody(); |
| } |
| |
| Error BitcodeReader::parseTypeTableBody() { |
| if (!TypeList.empty()) |
| return error("Invalid multiple blocks"); |
| |
| SmallVector<uint64_t, 64> Record; |
| unsigned NumRecords = 0; |
| |
| SmallString<64> TypeName; |
| |
| // Read all the records for this type table. |
| while (true) { |
| BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| |
| switch (Entry.Kind) { |
| case BitstreamEntry::SubBlock: // Handled for us already. |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| if (NumRecords != TypeList.size()) |
| return error("Malformed block"); |
| return Error::success(); |
| case BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Read a record. |
| Record.clear(); |
| Type *ResultTy = nullptr; |
| switch (Stream.readRecord(Entry.ID, Record)) { |
| default: |
| return error("Invalid value"); |
| case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] |
| // TYPE_CODE_NUMENTRY contains a count of the number of types in the |
| // type list. This allows us to reserve space. |
| if (Record.size() < 1) |
| return error("Invalid record"); |
| TypeList.resize(Record[0]); |
| continue; |
| case bitc::TYPE_CODE_VOID: // VOID |
| ResultTy = Type::getVoidTy(Context); |
| break; |
| case bitc::TYPE_CODE_HALF: // HALF |
| ResultTy = Type::getHalfTy(Context); |
| break; |
| case bitc::TYPE_CODE_FLOAT: // FLOAT |
| ResultTy = Type::getFloatTy(Context); |
| break; |
| case bitc::TYPE_CODE_DOUBLE: // DOUBLE |
| ResultTy = Type::getDoubleTy(Context); |
| break; |
| case bitc::TYPE_CODE_X86_FP80: // X86_FP80 |
| ResultTy = Type::getX86_FP80Ty(Context); |
| break; |
| case bitc::TYPE_CODE_FP128: // FP128 |
| ResultTy = Type::getFP128Ty(Context); |
| break; |
| case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128 |
| ResultTy = Type::getPPC_FP128Ty(Context); |
| break; |
| case bitc::TYPE_CODE_LABEL: // LABEL |
| ResultTy = Type::getLabelTy(Context); |
| break; |
| case bitc::TYPE_CODE_METADATA: // METADATA |
| ResultTy = Type::getMetadataTy(Context); |
| break; |
| case bitc::TYPE_CODE_X86_MMX: // X86_MMX |
| ResultTy = Type::getX86_MMXTy(Context); |
| break; |
| case bitc::TYPE_CODE_TOKEN: // TOKEN |
| ResultTy = Type::getTokenTy(Context); |
| break; |
| case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] |
| if (Record.size() < 1) |
| return error("Invalid record"); |
| |
| uint64_t NumBits = Record[0]; |
| if (NumBits < IntegerType::MIN_INT_BITS || |
| NumBits > IntegerType::MAX_INT_BITS) |
| return error("Bitwidth for integer type out of range"); |
| ResultTy = IntegerType::get(Context, NumBits); |
| break; |
| } |
| case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or |
| // [pointee type, address space] |
| if (Record.size() < 1) |
| return error("Invalid record"); |
| unsigned AddressSpace = 0; |
| if (Record.size() == 2) |
| AddressSpace = Record[1]; |
| ResultTy = getTypeByID(Record[0]); |
| if (!ResultTy || |
| !PointerType::isValidElementType(ResultTy)) |
| return error("Invalid type"); |
| ResultTy = PointerType::get(ResultTy, AddressSpace); |
| break; |
| } |
| case bitc::TYPE_CODE_FUNCTION_OLD: { |
| // FIXME: attrid is dead, remove it in LLVM 4.0 |
| // FUNCTION: [vararg, attrid, retty, paramty x N] |
| if (Record.size() < 3) |
| return error("Invalid record"); |
| SmallVector<Type*, 8> ArgTys; |
| for (unsigned i = 3, e = Record.size(); i != e; ++i) { |
| if (Type *T = getTypeByID(Record[i])) |
| ArgTys.push_back(T); |
| else |
| break; |
| } |
| |
| ResultTy = getTypeByID(Record[2]); |
| if (!ResultTy || ArgTys.size() < Record.size()-3) |
| return error("Invalid type"); |
| |
| ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
| break; |
| } |
| case bitc::TYPE_CODE_FUNCTION: { |
| // FUNCTION: [vararg, retty, paramty x N] |
| if (Record.size() < 2) |
| return error("Invalid record"); |
| SmallVector<Type*, 8> ArgTys; |
| for (unsigned i = 2, e = Record.size(); i != e; ++i) { |
| if (Type *T = getTypeByID(Record[i])) { |
| if (!FunctionType::isValidArgumentType(T)) |
| return error("Invalid function argument type"); |
| ArgTys.push_back(T); |
| } |
| else |
| break; |
| } |
| |
| ResultTy = getTypeByID(Record[1]); |
| if (!ResultTy || ArgTys.size() < Record.size()-2) |
| return error("Invalid type"); |
| |
| ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); |
| break; |
| } |
| case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] |
| if (Record.size() < 1) |
| return error("Invalid record"); |
| SmallVector<Type*, 8> EltTys; |
| for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
| if (Type *T = getTypeByID(Record[i])) |
| EltTys.push_back(T); |
| else |
| break; |
| } |
| if (EltTys.size() != Record.size()-1) |
| return error("Invalid type"); |
| ResultTy = StructType::get(Context, EltTys, Record[0]); |
| break; |
| } |
| case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] |
| if (convertToString(Record, 0, TypeName)) |
| return error("Invalid record"); |
| continue; |
| |
| case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] |
| if (Record.size() < 1) |
| return error("Invalid record"); |
| |
| if (NumRecords >= TypeList.size()) |
| return error("Invalid TYPE table"); |
| |
| // Check to see if this was forward referenced, if so fill in the temp. |
| StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
| if (Res) { |
| Res->setName(TypeName); |
| TypeList[NumRecords] = nullptr; |
| } else // Otherwise, create a new struct. |
| Res = createIdentifiedStructType(Context, TypeName); |
| TypeName.clear(); |
| |
| SmallVector<Type*, 8> EltTys; |
| for (unsigned i = 1, e = Record.size(); i != e; ++i) { |
| if (Type *T = getTypeByID(Record[i])) |
| EltTys.push_back(T); |
| else |
| break; |
| } |
| if (EltTys.size() != Record.size()-1) |
| return error("Invalid record"); |
| Res->setBody(EltTys, Record[0]); |
| ResultTy = Res; |
| break; |
| } |
| case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] |
| if (Record.size() != 1) |
| return error("Invalid record"); |
| |
| if (NumRecords >= TypeList.size()) |
| return error("Invalid TYPE table"); |
| |
| // Check to see if this was forward referenced, if so fill in the temp. |
| StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); |
| if (Res) { |
| Res->setName(TypeName); |
| TypeList[NumRecords] = nullptr; |
| } else // Otherwise, create a new struct with no body. |
| Res = createIdentifiedStructType(Context, TypeName); |
| TypeName.clear(); |
| ResultTy = Res; |
| break; |
| } |
| case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] |
| if (Record.size() < 2) |
| return error("Invalid record"); |
| ResultTy = getTypeByID(Record[1]); |
| if (!ResultTy || !ArrayType::isValidElementType(ResultTy)) |
| return error("Invalid type"); |
| ResultTy = ArrayType::get(ResultTy, Record[0]); |
| break; |
| case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] |
| if (Record.size() < 2) |
| return error("Invalid record"); |
| if (Record[0] == 0) |
| return error("Invalid vector length"); |
| ResultTy = getTypeByID(Record[1]); |
| if (!ResultTy || !StructType::isValidElementType(ResultTy)) |
| return error("Invalid type"); |
| ResultTy = VectorType::get(ResultTy, Record[0]); |
| break; |
| } |
| |
| if (NumRecords >= TypeList.size()) |
| return error("Invalid TYPE table"); |
| if (TypeList[NumRecords]) |
| return error( |
| "Invalid TYPE table: Only named structs can be forward referenced"); |
| assert(ResultTy && "Didn't read a type?"); |
| TypeList[NumRecords++] = ResultTy; |
| } |
| } |
| |
| Error BitcodeReader::parseOperandBundleTags() { |
| if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID)) |
| return error("Invalid record"); |
| |
| if (!BundleTags.empty()) |
| return error("Invalid multiple blocks"); |
| |
| SmallVector<uint64_t, 64> Record; |
| |
| while (true) { |
| BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| |
| switch (Entry.Kind) { |
| case BitstreamEntry::SubBlock: // Handled for us already. |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| return Error::success(); |
| case BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Tags are implicitly mapped to integers by their order. |
| |
| if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG) |
| return error("Invalid record"); |
| |
| // OPERAND_BUNDLE_TAG: [strchr x N] |
| BundleTags.emplace_back(); |
| if (convertToString(Record, 0, BundleTags.back())) |
| return error("Invalid record"); |
| Record.clear(); |
| } |
| } |
| |
| Error BitcodeReader::parseSyncScopeNames() { |
| if (Stream.EnterSubBlock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID)) |
| return error("Invalid record"); |
| |
| if (!SSIDs.empty()) |
| return error("Invalid multiple synchronization scope names blocks"); |
| |
| SmallVector<uint64_t, 64> Record; |
| while (true) { |
| BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| switch (Entry.Kind) { |
| case BitstreamEntry::SubBlock: // Handled for us already. |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| if (SSIDs.empty()) |
| return error("Invalid empty synchronization scope names block"); |
| return Error::success(); |
| case BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Synchronization scope names are implicitly mapped to synchronization |
| // scope IDs by their order. |
| |
| if (Stream.readRecord(Entry.ID, Record) != bitc::SYNC_SCOPE_NAME) |
| return error("Invalid record"); |
| |
| SmallString<16> SSN; |
| if (convertToString(Record, 0, SSN)) |
| return error("Invalid record"); |
| |
| SSIDs.push_back(Context.getOrInsertSyncScopeID(SSN)); |
| Record.clear(); |
| } |
| } |
| |
| /// Associate a value with its name from the given index in the provided record. |
| Expected<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record, |
| unsigned NameIndex, Triple &TT) { |
| SmallString<128> ValueName; |
| if (convertToString(Record, NameIndex, ValueName)) |
| return error("Invalid record"); |
| unsigned ValueID = Record[0]; |
| if (ValueID >= ValueList.size() || !ValueList[ValueID]) |
| return error("Invalid record"); |
| Value *V = ValueList[ValueID]; |
| |
| StringRef NameStr(ValueName.data(), ValueName.size()); |
| if (NameStr.find_first_of(0) != StringRef::npos) |
| return error("Invalid value name"); |
| V->setName(NameStr); |
| auto *GO = dyn_cast<GlobalObject>(V); |
| if (GO) { |
| if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) { |
| if (TT.isOSBinFormatMachO()) |
| GO->setComdat(nullptr); |
| else |
| GO->setComdat(TheModule->getOrInsertComdat(V->getName())); |
| } |
| } |
| return V; |
| } |
| |
| /// Helper to note and return the current location, and jump to the given |
| /// offset. |
| static uint64_t jumpToValueSymbolTable(uint64_t Offset, |
| BitstreamCursor &Stream) { |
| // Save the current parsing location so we can jump back at the end |
| // of the VST read. |
| uint64_t CurrentBit = Stream.GetCurrentBitNo(); |
| Stream.JumpToBit(Offset * 32); |
| #ifndef NDEBUG |
| // Do some checking if we are in debug mode. |
| BitstreamEntry Entry = Stream.advance(); |
| assert(Entry.Kind == BitstreamEntry::SubBlock); |
| assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID); |
| #else |
| // In NDEBUG mode ignore the output so we don't get an unused variable |
| // warning. |
| Stream.advance(); |
| #endif |
| return CurrentBit; |
| } |
| |
| void BitcodeReader::setDeferredFunctionInfo(unsigned FuncBitcodeOffsetDelta, |
| Function *F, |
| ArrayRef<uint64_t> Record) { |
| // Note that we subtract 1 here because the offset is relative to one word |
| // before the start of the identification or module block, which was |
| // historically always the start of the regular bitcode header. |
| uint64_t FuncWordOffset = Record[1] - 1; |
| uint64_t FuncBitOffset = FuncWordOffset * 32; |
| DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta; |
| // Set the LastFunctionBlockBit to point to the last function block. |
| // Later when parsing is resumed after function materialization, |
| // we can simply skip that last function block. |
| if (FuncBitOffset > LastFunctionBlockBit) |
| LastFunctionBlockBit = FuncBitOffset; |
| } |
| |
| /// Read a new-style GlobalValue symbol table. |
| Error BitcodeReader::parseGlobalValueSymbolTable() { |
| unsigned FuncBitcodeOffsetDelta = |
| Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; |
| |
| if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
| return error("Invalid record"); |
| |
| SmallVector<uint64_t, 64> Record; |
| while (true) { |
| BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| |
| switch (Entry.Kind) { |
| case BitstreamEntry::SubBlock: |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| return Error::success(); |
| case BitstreamEntry::Record: |
| break; |
| } |
| |
| Record.clear(); |
| switch (Stream.readRecord(Entry.ID, Record)) { |
| case bitc::VST_CODE_FNENTRY: // [valueid, offset] |
| setDeferredFunctionInfo(FuncBitcodeOffsetDelta, |
| cast<Function>(ValueList[Record[0]]), Record); |
| break; |
| } |
| } |
| } |
| |
| /// Parse the value symbol table at either the current parsing location or |
| /// at the given bit offset if provided. |
| Error BitcodeReader::parseValueSymbolTable(uint64_t Offset) { |
| uint64_t CurrentBit; |
| // Pass in the Offset to distinguish between calling for the module-level |
| // VST (where we want to jump to the VST offset) and the function-level |
| // VST (where we don't). |
| if (Offset > 0) { |
| CurrentBit = jumpToValueSymbolTable(Offset, Stream); |
| // If this module uses a string table, read this as a module-level VST. |
| if (UseStrtab) { |
| if (Error Err = parseGlobalValueSymbolTable()) |
| return Err; |
| Stream.JumpToBit(CurrentBit); |
| return Error::success(); |
| } |
| // Otherwise, the VST will be in a similar format to a function-level VST, |
| // and will contain symbol names. |
| } |
| |
| // Compute the delta between the bitcode indices in the VST (the word offset |
| // to the word-aligned ENTER_SUBBLOCK for the function block, and that |
| // expected by the lazy reader. The reader's EnterSubBlock expects to have |
| // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID |
| // (size BlockIDWidth). Note that we access the stream's AbbrevID width here |
| // just before entering the VST subblock because: 1) the EnterSubBlock |
| // changes the AbbrevID width; 2) the VST block is nested within the same |
| // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same |
| // AbbrevID width before calling EnterSubBlock; and 3) when we want to |
| // jump to the FUNCTION_BLOCK using this offset later, we don't want |
| // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK. |
| unsigned FuncBitcodeOffsetDelta = |
| Stream.getAbbrevIDWidth() + bitc::BlockIDWidth; |
| |
| if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) |
| return error("Invalid record"); |
| |
| SmallVector<uint64_t, 64> Record; |
| |
| Triple TT(TheModule->getTargetTriple()); |
| |
| // Read all the records for this value table. |
| SmallString<128> ValueName; |
| |
| while (true) { |
| BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| |
| switch (Entry.Kind) { |
| case BitstreamEntry::SubBlock: // Handled for us already. |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| if (Offset > 0) |
| Stream.JumpToBit(CurrentBit); |
| return Error::success(); |
| case BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Read a record. |
| Record.clear(); |
| switch (Stream.readRecord(Entry.ID, Record)) { |
| default: // Default behavior: unknown type. |
| break; |
| case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N] |
| Expected<Value *> ValOrErr = recordValue(Record, 1, TT); |
| if (Error Err = ValOrErr.takeError()) |
| return Err; |
| ValOrErr.get(); |
| break; |
| } |
| case bitc::VST_CODE_FNENTRY: { |
| // VST_CODE_FNENTRY: [valueid, offset, namechar x N] |
| Expected<Value *> ValOrErr = recordValue(Record, 2, TT); |
| if (Error Err = ValOrErr.takeError()) |
| return Err; |
| Value *V = ValOrErr.get(); |
| |
| // Ignore function offsets emitted for aliases of functions in older |
| // versions of LLVM. |
| if (auto *F = dyn_cast<Function>(V)) |
| setDeferredFunctionInfo(FuncBitcodeOffsetDelta, F, Record); |
| break; |
| } |
| case bitc::VST_CODE_BBENTRY: { |
| if (convertToString(Record, 1, ValueName)) |
| return error("Invalid record"); |
| BasicBlock *BB = getBasicBlock(Record[0]); |
| if (!BB) |
| return error("Invalid record"); |
| |
| BB->setName(StringRef(ValueName.data(), ValueName.size())); |
| ValueName.clear(); |
| break; |
| } |
| } |
| } |
| } |
| |
| /// Decode a signed value stored with the sign bit in the LSB for dense VBR |
| /// encoding. |
| uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) { |
| if ((V & 1) == 0) |
| return V >> 1; |
| if (V != 1) |
| return -(V >> 1); |
| // There is no such thing as -0 with integers. "-0" really means MININT. |
| return 1ULL << 63; |
| } |
| |
| /// Resolve all of the initializers for global values and aliases that we can. |
| Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { |
| std::vector<std::pair<GlobalVariable *, unsigned>> GlobalInitWorklist; |
| std::vector<std::pair<GlobalIndirectSymbol *, unsigned>> |
| IndirectSymbolInitWorklist; |
| std::vector<std::pair<Function *, unsigned>> FunctionPrefixWorklist; |
| std::vector<std::pair<Function *, unsigned>> FunctionPrologueWorklist; |
| std::vector<std::pair<Function *, unsigned>> FunctionPersonalityFnWorklist; |
| |
| GlobalInitWorklist.swap(GlobalInits); |
| IndirectSymbolInitWorklist.swap(IndirectSymbolInits); |
| FunctionPrefixWorklist.swap(FunctionPrefixes); |
| FunctionPrologueWorklist.swap(FunctionPrologues); |
| FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns); |
| |
| while (!GlobalInitWorklist.empty()) { |
| unsigned ValID = GlobalInitWorklist.back().second; |
| if (ValID >= ValueList.size()) { |
| // Not ready to resolve this yet, it requires something later in the file. |
| GlobalInits.push_back(GlobalInitWorklist.back()); |
| } else { |
| if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
| GlobalInitWorklist.back().first->setInitializer(C); |
| else |
| return error("Expected a constant"); |
| } |
| GlobalInitWorklist.pop_back(); |
| } |
| |
| while (!IndirectSymbolInitWorklist.empty()) { |
| unsigned ValID = IndirectSymbolInitWorklist.back().second; |
| if (ValID >= ValueList.size()) { |
| IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back()); |
| } else { |
| Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]); |
| if (!C) |
| return error("Expected a constant"); |
| GlobalIndirectSymbol *GIS = IndirectSymbolInitWorklist.back().first; |
| if (isa<GlobalAlias>(GIS) && C->getType() != GIS->getType()) |
| return error("Alias and aliasee types don't match"); |
| GIS->setIndirectSymbol(C); |
| } |
| IndirectSymbolInitWorklist.pop_back(); |
| } |
| |
| while (!FunctionPrefixWorklist.empty()) { |
| unsigned ValID = FunctionPrefixWorklist.back().second; |
| if (ValID >= ValueList.size()) { |
| FunctionPrefixes.push_back(FunctionPrefixWorklist.back()); |
| } else { |
| if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
| FunctionPrefixWorklist.back().first->setPrefixData(C); |
| else |
| return error("Expected a constant"); |
| } |
| FunctionPrefixWorklist.pop_back(); |
| } |
| |
| while (!FunctionPrologueWorklist.empty()) { |
| unsigned ValID = FunctionPrologueWorklist.back().second; |
| if (ValID >= ValueList.size()) { |
| FunctionPrologues.push_back(FunctionPrologueWorklist.back()); |
| } else { |
| if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
| FunctionPrologueWorklist.back().first->setPrologueData(C); |
| else |
| return error("Expected a constant"); |
| } |
| FunctionPrologueWorklist.pop_back(); |
| } |
| |
| while (!FunctionPersonalityFnWorklist.empty()) { |
| unsigned ValID = FunctionPersonalityFnWorklist.back().second; |
| if (ValID >= ValueList.size()) { |
| FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back()); |
| } else { |
| if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) |
| FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C); |
| else |
| return error("Expected a constant"); |
| } |
| FunctionPersonalityFnWorklist.pop_back(); |
| } |
| |
| return Error::success(); |
| } |
| |
| static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { |
| SmallVector<uint64_t, 8> Words(Vals.size()); |
| transform(Vals, Words.begin(), |
| BitcodeReader::decodeSignRotatedValue); |
| |
| return APInt(TypeBits, Words); |
| } |
| |
| Error BitcodeReader::parseConstants() { |
| if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) |
| return error("Invalid record"); |
| |
| SmallVector<uint64_t, 64> Record; |
| |
| // Read all the records for this value table. |
| Type *CurTy = Type::getInt32Ty(Context); |
| unsigned NextCstNo = ValueList.size(); |
| |
| while (true) { |
| BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); |
| |
| switch (Entry.Kind) { |
| case BitstreamEntry::SubBlock: // Handled for us already. |
| case BitstreamEntry::Error: |
| return error("Malformed block"); |
| case BitstreamEntry::EndBlock: |
| if (NextCstNo != ValueList.size()) |
| return error("Invalid constant reference"); |
| |
| // Once all the constants have been read, go through and resolve forward |
| // references. |
| ValueList.resolveConstantForwardRefs(); |
| return Error::success(); |
| case BitstreamEntry::Record: |
| // The interesting case. |
| break; |
| } |
| |
| // Read a record. |
| Record.clear(); |
| Type *VoidType = Type::getVoidTy(Context); |
| Value *V = nullptr; |
| unsigned BitCode = Stream.readRecord(Entry.ID, Record); |
| switch (BitCode) { |
| default: // Default behavior: unknown constant |
| case bitc::CST_CODE_UNDEF: // UNDEF |
| V = UndefValue::get(CurTy); |
| break; |
| case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] |
| if (Record.empty()) |
| return error("Invalid record"); |
| if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) |
| return error("Invalid record"); |
| if (TypeList[Record[0]] == VoidType) |
| return error("Invalid constant type"); |
| CurTy = TypeList[Record[0]]; |
| continue; // Skip the ValueList manipulation. |
| case bitc::CST_CODE_NULL: // NULL |
| V = Constant::getNullValue(CurTy); |
| break; |
| case bitc::CST_CODE_INTEGER: // INTEGER: [intval] |
| if (!CurTy->isIntegerTy() || Record.empty()) |
| return error("Invalid record"); |
| V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); |
| break; |
| case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] |
| if (!CurTy->isIntegerTy() || Record.empty()) |
| return error("Invalid record"); |
| |
| APInt VInt = |
| readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); |
| V = ConstantInt::get(Context, VInt); |
| |
| break; |
| } |
| case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] |
| if (Record.empty()) |
| return error("Invalid record"); |
| if (CurTy->isHalfTy()) |
| V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(), |
| APInt(16, (uint16_t)Record[0]))); |
| else if (CurTy->isFloatTy()) |
| V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(), |
| APInt(32, (uint32_t)Record[0]))); |
| else if (CurTy->isDoubleTy()) |
| V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble(), |
| APInt(64, Record[0]))); |
| else if (CurTy->isX86_FP80Ty()) { |
| // Bits are not stored the same way as a normal i80 APInt, compensate. |
| uint64_t Rearrange[2]; |
| Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16); |
| Rearrange[1] = Record[0] >> 48; |
| V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended(), |
| APInt(80, Rearrange))); |
| } else if (CurTy->isFP128Ty()) |
| V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad(), |
| APInt(128, Record))); |
| else if (CurTy->isPPC_FP128Ty()) |
| V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble(), |
| APInt(128, Record))); |
| else |
| V = UndefValue::get(CurTy); |
| break; |
| } |
| |
| case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] |
| if (Record.empty()) |
| return error("Invalid record"); |
| |
| unsigned Size = Record.size(); |
| SmallVector<Constant*, 16> Elts; |
| |
| if (StructType *STy = dyn_cast<StructType>(CurTy)) { |
| for (unsigned i = 0; i != Size; ++i) |
| Elts.push_back(ValueList.getConstantFwdRef(Record[i], |
| STy->getElementType(i))); |
| V = ConstantStruct::get(STy, Elts); |
| } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) { |
| Type *EltTy = ATy->getElementType(); |
| for (unsigned i = 0; i != Size; ++i) |
| Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
| V = ConstantArray::get(ATy, Elts); |
| } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) { |
| Type *EltTy = VTy->getElementType(); |
| for (unsigned i = 0; i != Size; ++i) |
| Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy)); |
| V = ConstantVector::get(Elts); |
| } else { |
| V = UndefValue::get(CurTy); |
| } |
| break; |
| } |
| case bitc::CST_CODE_STRING: // STRING: [values] |
| case bitc::CST_CODE_CSTRING: { // CSTRING: [values] |
| if (Record.empty()) |
| return error("Invalid record"); |
| |
| SmallString<16> Elts(Record.begin(), Record.end()); |
| V = ConstantDataArray::getString(Context, Elts, |
| BitCode == bitc::CST_CODE_CSTRING); |
| break; |
| } |
| case bitc::CST_CODE_DATA: {// DATA: [n x value] |
| if (Record.empty()) |
| return error("Invalid record"); |
| |
| Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); |
| if (EltTy->isIntegerTy(8)) { |
| SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); |
| if (isa<VectorType>(CurTy)) |
| V = ConstantDataVector::get(Context, Elts); |
| else |
| V = ConstantDataArray::get(Context, Elts); |
| } else if (EltTy->isIntegerTy(16)) { |
| SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
| if (isa<VectorType>(CurTy)) |
| V = ConstantDataVector::get(Context, Elts); |
| else |
| V = ConstantDataArray::get(Context, Elts); |
| } else if (EltTy->isIntegerTy(32)) { |
| SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
| if (isa<VectorType>(CurTy)) |
| V = ConstantDataVector::get(Context, Elts); |
| else |
| V = ConstantDataArray::get(Context, Elts); |
| } else if (EltTy->isIntegerTy(64)) { |
| SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
| if (isa<VectorType>(CurTy)) |
| V = ConstantDataVector::get(Context, Elts); |
| else |
| V = ConstantDataArray::get(Context, Elts); |
| } else if (EltTy->isHalfTy()) { |
| SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); |
| if (isa<VectorType>(CurTy)) |
| V = ConstantDataVector::getFP(Context, Elts); |
| else |
| V = ConstantDataArray::getFP(Context, Elts); |
| } else if (EltTy->isFloatTy()) { |
| SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); |
| if (isa<VectorType>(CurTy)) |
| V = ConstantDataVector::getFP(Context, Elts); |
| else |
| V = ConstantDataArray::getFP(Context, Elts); |
| } else if (EltTy->isDoubleTy()) { |
| SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); |
| if (isa<VectorType>(CurTy)) |
| V = ConstantDataVector::getFP(Context, Elts); |
| else |
| V = ConstantDataArray::getFP(Context, Elts); |
| } else { |
| return error("Invalid type for value"); |
| } |
| break; |
| } |
| case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] |
| if (Record.size() < 3) |
| return error("Invalid record"); |
| int Opc = getDecodedBinaryOpcode(Record[0], CurTy); |
| if (Opc < 0) { |
| V = UndefValue::get(CurTy); // Unknown binop. |
| } else { |
| Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy); |
| Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy); |
| unsigned Flags = 0; |
| if (Record.size() >= 4) { |
| if (Opc == Instruction::Add || |
| Opc == Instruction::Sub || |
| Opc == Instruction::Mul || |
| Opc == Instruction::Shl) { |
| if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP)) |
| Flags |= OverflowingBinaryOperator::NoSignedWrap; |
| if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP)) |
| Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
| } else if (Opc == Instruction::SDiv || |
| Opc == Instruction::UDiv || |
| Opc == Instruction::LShr || |
| Opc == Instruction::AShr) { |
| if (Record[3] & (1 << bitc::PEO_EXACT)) |
| Flags |= SDivOperator::IsExact; |
| } |
| } |
| V = ConstantExpr::get(Opc, LHS, RHS, Flags); |
| } |
| break; |
| } |
| case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] |
| if (Record.size() < 3) |
| return error("Invalid record"); |
| int Opc = getDecodedCastOpcode(Record[0]); |
| if (Opc < 0) { |
| V = UndefValue::get(CurTy); // Unknown cast. |
| } else { |
| Type *OpTy = getTypeByID(Record[1]); |
| if (!OpTy) |
| return error("Invalid record"); |
| Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); |
| V = UpgradeBitCastExpr(Opc, Op, CurTy); |
| if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy); |
| } |
| break; |
| } |
| case bitc::CST_CODE_CE_INBOUNDS_GEP: // [ty, n x operands] |
| case bitc::CST_CODE_CE_GEP: // [ty, n x operands] |
| case bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX: { // [ty, flags, n x |
| // operands] |
| unsigned OpNum = 0; |
| Type *PointeeType = nullptr; |
| if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX || |
| Record.size() % 2) |
| PointeeType = getTypeByID(Record[OpNum++]); |
| |
| bool InBounds = false; |
| Optional<unsigned> InRangeIndex; |
| if (BitCode == bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX) { |
| uint64_t Op = Record[OpNum++]; |
| InBounds = Op & 1; |
| InRangeIndex = Op >> 1; |
| } else if (BitCode == bitc::CST_CODE_CE_INBOUNDS_GEP) |
| InBounds = true; |
| |
| SmallVector<Constant*, 16> Elts; |
| while (OpNum != Record.size()) { |
| Type *ElTy = getTypeByID(Record[OpNum++]); |
| if (!ElTy) |
| return error("Invalid record"); |
| Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy)); |
| } |
| |
| if (PointeeType && |
| PointeeType != |
| cast<PointerType>(Elts[0]->getType()->getScalarType()) |
| ->getElementType()) |
| return error("Explicit gep operator type does not match pointee type " |
| "of pointer operand"); |
| |
| if (Elts.size() < 1) |
| return error("Invalid gep with no operands"); |
| |
| ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); |
| V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices, |
| InBounds, InRangeIndex); |
| break; |
| } |
| case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] |
| if (Record.size() < 3) |
| return error("Invalid record"); |
| |
| Type *SelectorTy = Type::getInt1Ty(Context); |
| |
| // The selector might be an i1 or an <n x i1> |
| // Get the type from the ValueList before getting a forward ref. |
| if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) |
| if (Value *V = ValueList[Record[0]]) |
| if (SelectorTy != V->getType()) |
| SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements()); |
| |
| V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], |
| SelectorTy), |
| ValueList.getConstantFwdRef(Record[1],CurTy), |
| ValueList.getConstantFwdRef(Record[2],CurTy)); |
|