diff options
author | Stephen Hines <srhines@google.com> | 2015-04-07 14:55:28 -0700 |
---|---|---|
committer | Stephen Hines <srhines@google.com> | 2015-04-07 16:37:14 -0700 |
commit | 5cb1f3949c5bdd43c84647920d147a6b8509e256 (patch) | |
tree | f7c2173489a6c9bec7bfc6b5ac018a1e0e8fcc1b | |
parent | fa0c1b9d9f2d6876f0f37049e2ea59abc7a217ae (diff) | |
download | libbcc-5cb1f3949c5bdd43c84647920d147a6b8509e256.tar.gz |
Update libbcc for LLVM rebase to r233350.
- Remove direct calls/use of DataLayoutPass.
- Move BitcodeReader.h (declarations) into BitcodeReader.cpp files.
- Fix typed/untyped GEP.
- Add empty materializeMetadata(), since we don't defer it ever.
- Fix longstanding (but insconsequential) difference between
bitc::CST_CODE_INLINEASM and bitc::CST_CODE_INLINEASM_OLD.
Change-Id: I135e494beea2f5992db1cb5c038d52cced991e72
-rw-r--r-- | bcinfo/BitReader_2_7/BitcodeReader.cpp | 308 | ||||
-rw-r--r-- | bcinfo/BitReader_2_7/BitcodeReader.h | 306 | ||||
-rw-r--r-- | bcinfo/BitReader_3_0/BitcodeReader.cpp | 305 | ||||
-rw-r--r-- | bcinfo/BitReader_3_0/BitcodeReader.h | 301 | ||||
-rw-r--r-- | include/bcc/Compiler.h | 1 | ||||
-rw-r--r-- | lib/Core/Compiler.cpp | 13 |
6 files changed, 603 insertions, 631 deletions
diff --git a/bcinfo/BitReader_2_7/BitcodeReader.cpp b/bcinfo/BitReader_2_7/BitcodeReader.cpp index ba41a4a..00897fd 100644 --- a/bcinfo/BitReader_2_7/BitcodeReader.cpp +++ b/bcinfo/BitReader_2_7/BitcodeReader.cpp @@ -12,14 +12,15 @@ //===----------------------------------------------------------------------===// #include "llvm/Bitcode/ReaderWriter.h" -#include "BitcodeReader.h" #include "BitReader_2_7.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/IR/AutoUpgrade.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/DiagnosticPrinter.h" +#include "llvm/IR/GVMaterializer.h" #include "llvm/IR/InlineAsm.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" @@ -83,6 +84,275 @@ namespace { } } } + +//===----------------------------------------------------------------------===// +// BitcodeReaderValueList Class +//===----------------------------------------------------------------------===// + +class BitcodeReaderValueList { + std::vector<WeakVH> ValuePtrs; + + /// ResolveConstants - As we resolve forward-referenced constants, we add + /// information about them to this vector. This allows us to resolve them in + /// bulk instead of resolving each reference at a time. See the code in + /// ResolveConstantForwardRefs for more information about this. + /// + /// The key of this vector is the placeholder constant, the value is the slot + /// number that holds the resolved value. + typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy; + ResolveConstantsTy ResolveConstants; + LLVMContext &Context; +public: + BitcodeReaderValueList(LLVMContext &C) : Context(C) {} + ~BitcodeReaderValueList() { + assert(ResolveConstants.empty() && "Constants not resolved?"); + } + + // vector compatibility methods + unsigned size() const { return ValuePtrs.size(); } + void resize(unsigned N) { ValuePtrs.resize(N); } + void push_back(Value *V) { + ValuePtrs.push_back(V); + } + + void clear() { + assert(ResolveConstants.empty() && "Constants not resolved?"); + ValuePtrs.clear(); + } + + Value *operator[](unsigned i) const { + assert(i < ValuePtrs.size()); + return ValuePtrs[i]; + } + + Value *back() const { return ValuePtrs.back(); } + void pop_back() { ValuePtrs.pop_back(); } + bool empty() const { return ValuePtrs.empty(); } + void shrinkTo(unsigned N) { + assert(N <= size() && "Invalid shrinkTo request!"); + ValuePtrs.resize(N); + } + + Constant *getConstantFwdRef(unsigned Idx, Type *Ty); + Value *getValueFwdRef(unsigned Idx, Type *Ty); + + void AssignValue(Value *V, unsigned Idx); + + /// ResolveConstantForwardRefs - Once all constants are read, this method bulk + /// resolves any forward references. + void ResolveConstantForwardRefs(); +}; + + +//===----------------------------------------------------------------------===// +// BitcodeReaderMDValueList Class +//===----------------------------------------------------------------------===// + +class BitcodeReaderMDValueList { + unsigned NumFwdRefs; + bool AnyFwdRefs; + std::vector<TrackingMDRef> MDValuePtrs; + + LLVMContext &Context; +public: + BitcodeReaderMDValueList(LLVMContext &C) + : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {} + + // vector compatibility methods + unsigned size() const { return MDValuePtrs.size(); } + void resize(unsigned N) { MDValuePtrs.resize(N); } + void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); } + void clear() { MDValuePtrs.clear(); } + Metadata *back() const { return MDValuePtrs.back(); } + void pop_back() { MDValuePtrs.pop_back(); } + bool empty() const { return MDValuePtrs.empty(); } + + Metadata *operator[](unsigned i) const { + assert(i < MDValuePtrs.size()); + return MDValuePtrs[i]; + } + + void shrinkTo(unsigned N) { + assert(N <= size() && "Invalid shrinkTo request!"); + MDValuePtrs.resize(N); + } + + Metadata *getValueFwdRef(unsigned Idx); + void AssignValue(Metadata *MD, unsigned Idx); + void tryToResolveCycles(); +}; + +class BitcodeReader : public GVMaterializer { + LLVMContext &Context; + DiagnosticHandlerFunction DiagnosticHandler; + Module *TheModule; + std::unique_ptr<MemoryBuffer> Buffer; + std::unique_ptr<BitstreamReader> StreamFile; + BitstreamCursor Stream; + DataStreamer *LazyStreamer; + uint64_t NextUnreadBit; + bool SeenValueSymbolTable; + + std::vector<Type*> TypeList; + BitcodeReaderValueList ValueList; + BitcodeReaderMDValueList MDValueList; + SmallVector<Instruction *, 64> InstructionList; + + std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; + std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; + + /// MAttributes - 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<AttributeSet> MAttributes; + + /// \brief The set of attribute groups. + std::map<unsigned, AttributeSet> MAttributeGroups; + + /// FunctionBBs - 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. + typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; + UpgradedIntrinsicMap UpgradedIntrinsics; + + // Map the bitcode's custom MDKind ID to the Module's MDKind ID. + DenseMap<unsigned, unsigned> MDKindMap; + + // 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; + + /// DeferredFunctionInfo - 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; + + /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These + /// are resolved lazily when functions are loaded. + typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy; + DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs; + + /// LLVM2_7MetadataDetected - True if metadata produced by LLVM 2.7 or + /// earlier was detected, in which case we behave slightly differently, + /// for compatibility. + /// FIXME: Remove in LLVM 3.0. + bool LLVM2_7MetadataDetected; + static const std::error_category &BitcodeErrorCategory(); + +public: + std::error_code Error(BitcodeError E, const Twine &Message); + std::error_code Error(BitcodeError E); + std::error_code Error(const Twine &Message); + + explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C, + DiagnosticHandlerFunction DiagnosticHandler); + ~BitcodeReader() { FreeState(); } + + void FreeState(); + + void releaseBuffer(); + + bool isDematerializable(const GlobalValue *GV) const override; + std::error_code materialize(GlobalValue *GV) override; + std::error_code MaterializeModule(Module *M) override; + std::vector<StructType *> getIdentifiedStructTypes() const override; + void Dematerialize(GlobalValue *GV) override; + + /// @brief Main interface to parsing a bitcode buffer. + /// @returns true if an error occurred. + std::error_code ParseBitcodeInto(Module *M); + + /// @brief Cheap mechanism to just extract module triple + /// @returns true if an error occurred. + llvm::ErrorOr<std::string> parseTriple(); + + static uint64_t decodeSignRotatedValue(uint64_t V); + + /// Materialize any deferred Metadata block. + std::error_code materializeMetadata() override; + +private: + std::vector<StructType *> IdentifiedStructTypes; + StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); + StructType *createIdentifiedStructType(LLVMContext &Context); + + Type *getTypeByID(unsigned ID); + Type *getTypeByIDOrNull(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 MDValueList.getValueFwdRef(ID); + } + BasicBlock *getBasicBlock(unsigned ID) const { + if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID + return FunctionBBs[ID]; + } + AttributeSet getAttributes(unsigned i) const { + if (i-1 < MAttributes.size()) + return MAttributes[i-1]; + return AttributeSet(); + } + + /// getValueTypePair - 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++]; + if (ValNo < InstNum) { + // If this is not a forward reference, just return the value we already + // have. + ResVal = getFnValueByID(ValNo, nullptr); + return ResVal == nullptr; + } else if (Slot == Record.size()) { + return true; + } + + unsigned TypeNo = (unsigned)Record[Slot++]; + ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); + return ResVal == nullptr; + } + bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot, + Type *Ty, Value *&ResVal) { + if (Slot == Record.size()) return true; + unsigned ValNo = (unsigned)Record[Slot++]; + ResVal = getFnValueByID(ValNo, Ty); + return ResVal == 0; + } + + + std::error_code ParseModule(bool Resume); + std::error_code ParseAttributeBlock(); + std::error_code ParseTypeTable(); + std::error_code ParseOldTypeTable(); // FIXME: Remove in LLVM 3.1 + std::error_code ParseTypeTableBody(); + + std::error_code ParseOldTypeSymbolTable(); // FIXME: Remove in LLVM 3.1 + std::error_code ParseValueSymbolTable(); + std::error_code ParseConstants(); + std::error_code RememberAndSkipFunctionBody(); + std::error_code ParseFunctionBody(Function *F); + std::error_code GlobalCleanup(); + std::error_code ResolveGlobalAndAliasInits(); + std::error_code ParseMetadata(); + std::error_code ParseMetadataAttachment(); + llvm::ErrorOr<std::string> parseModuleTriple(); + std::error_code InitStream(); + std::error_code InitStreamFromBuffer(); + std::error_code InitLazyStream(); +}; } // end anonymous namespace static std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler, @@ -1622,7 +1892,8 @@ std::error_code BitcodeReader::ParseConstants() { V = ConstantExpr::getICmp(Record[3], Op0, Op1); break; } - case bitc::CST_CODE_INLINEASM: { + case bitc::CST_CODE_INLINEASM: + case bitc::CST_CODE_INLINEASM_OLD: { if (Record.size() < 2) return Error("Invalid record"); std::string AsmStr, ConstrStr; @@ -1681,6 +1952,10 @@ std::error_code BitcodeReader::ParseConstants() { return std::error_code(); } +std::error_code BitcodeReader::materializeMetadata() { + return std::error_code(); +} + /// RememberAndSkipFunctionBody - When we see the block for a function body, /// remember where it is and then skip it. This lets us lazily deserialize the /// functions. @@ -2417,10 +2692,29 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { case bitc::FUNC_CODE_INST_GEP_OLD: // GEP: [n x operands] case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands] unsigned OpNum = 0; + + Type *Ty; + bool InBounds; + + if (BitCode == bitc::FUNC_CODE_INST_GEP) { + InBounds = Record[OpNum++]; + Ty = getTypeByID(Record[OpNum++]); + } else { + InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; + Ty = nullptr; + } + Value *BasePtr; if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) return Error("Invalid record"); + if (Ty && + Ty != + cast<SequentialType>(BasePtr->getType()->getScalarType()) + ->getElementType()) + return Error( + "Explicit gep type does not match pointee type of pointer operand"); + SmallVector<Value*, 16> GEPIdx; while (OpNum != Record.size()) { Value *Op; @@ -2429,9 +2723,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { GEPIdx.push_back(Op); } - I = GetElementPtrInst::Create(BasePtr, GEPIdx); + I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); + InstructionList.push_back(I); - if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD) + if (InBounds) cast<GetElementPtrInst>(I)->setIsInBounds(true); break; } @@ -3009,6 +3304,9 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { void BitcodeReader::releaseBuffer() { Buffer.release(); } std::error_code BitcodeReader::materialize(GlobalValue *GV) { + if (std::error_code EC = materializeMetadata()) + return EC; + Function *F = dyn_cast<Function>(GV); // If it's not a function or is already material, ignore the request. if (!F || !F->isMaterializable()) @@ -3174,7 +3472,7 @@ class BitcodeErrorCategoryType : public std::error_category { static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory; -const std::error_category &llvm_2_7::BitcodeReader::BitcodeErrorCategory() { +const std::error_category &BitcodeReader::BitcodeErrorCategory() { return *ErrorCategory; } diff --git a/bcinfo/BitReader_2_7/BitcodeReader.h b/bcinfo/BitReader_2_7/BitcodeReader.h deleted file mode 100644 index 29793f5..0000000 --- a/bcinfo/BitReader_2_7/BitcodeReader.h +++ /dev/null @@ -1,306 +0,0 @@ -//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This header defines the BitcodeReader class. -// -//===----------------------------------------------------------------------===// - -#ifndef BITCODE_READER_H -#define BITCODE_READER_H - -#include "llvm/ADT/DenseMap.h" -#include "llvm/Bitcode/BitstreamReader.h" -#include "llvm/Bitcode/LLVMBitCodes.h" -#include "llvm/IR/Attributes.h" -#include "llvm/IR/GVMaterializer.h" -#include "llvm/IR/Metadata.h" -#include "llvm/IR/OperandTraits.h" -#include "llvm/IR/TrackingMDRef.h" -#include "llvm/IR/Type.h" -#include "llvm/IR/ValueHandle.h" -#include <vector> - -namespace llvm { - class MemoryBuffer; - class LLVMContext; -} - -namespace llvm_2_7 { - -using namespace llvm; - -//===----------------------------------------------------------------------===// -// BitcodeReaderValueList Class -//===----------------------------------------------------------------------===// - -class BitcodeReaderValueList { - std::vector<WeakVH> ValuePtrs; - - /// ResolveConstants - As we resolve forward-referenced constants, we add - /// information about them to this vector. This allows us to resolve them in - /// bulk instead of resolving each reference at a time. See the code in - /// ResolveConstantForwardRefs for more information about this. - /// - /// The key of this vector is the placeholder constant, the value is the slot - /// number that holds the resolved value. - typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy; - ResolveConstantsTy ResolveConstants; - LLVMContext &Context; -public: - BitcodeReaderValueList(LLVMContext &C) : Context(C) {} - ~BitcodeReaderValueList() { - assert(ResolveConstants.empty() && "Constants not resolved?"); - } - - // vector compatibility methods - unsigned size() const { return ValuePtrs.size(); } - void resize(unsigned N) { ValuePtrs.resize(N); } - void push_back(Value *V) { - ValuePtrs.push_back(V); - } - - void clear() { - assert(ResolveConstants.empty() && "Constants not resolved?"); - ValuePtrs.clear(); - } - - Value *operator[](unsigned i) const { - assert(i < ValuePtrs.size()); - return ValuePtrs[i]; - } - - Value *back() const { return ValuePtrs.back(); } - void pop_back() { ValuePtrs.pop_back(); } - bool empty() const { return ValuePtrs.empty(); } - void shrinkTo(unsigned N) { - assert(N <= size() && "Invalid shrinkTo request!"); - ValuePtrs.resize(N); - } - - Constant *getConstantFwdRef(unsigned Idx, Type *Ty); - Value *getValueFwdRef(unsigned Idx, Type *Ty); - - void AssignValue(Value *V, unsigned Idx); - - /// ResolveConstantForwardRefs - Once all constants are read, this method bulk - /// resolves any forward references. - void ResolveConstantForwardRefs(); -}; - - -//===----------------------------------------------------------------------===// -// BitcodeReaderMDValueList Class -//===----------------------------------------------------------------------===// - -class BitcodeReaderMDValueList { - unsigned NumFwdRefs; - bool AnyFwdRefs; - std::vector<TrackingMDRef> MDValuePtrs; - - LLVMContext &Context; -public: - BitcodeReaderMDValueList(LLVMContext &C) - : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {} - - // vector compatibility methods - unsigned size() const { return MDValuePtrs.size(); } - void resize(unsigned N) { MDValuePtrs.resize(N); } - void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); } - void clear() { MDValuePtrs.clear(); } - Metadata *back() const { return MDValuePtrs.back(); } - void pop_back() { MDValuePtrs.pop_back(); } - bool empty() const { return MDValuePtrs.empty(); } - - Metadata *operator[](unsigned i) const { - assert(i < MDValuePtrs.size()); - return MDValuePtrs[i]; - } - - void shrinkTo(unsigned N) { - assert(N <= size() && "Invalid shrinkTo request!"); - MDValuePtrs.resize(N); - } - - Metadata *getValueFwdRef(unsigned Idx); - void AssignValue(Metadata *MD, unsigned Idx); - void tryToResolveCycles(); -}; - -class BitcodeReader : public GVMaterializer { - LLVMContext &Context; - DiagnosticHandlerFunction DiagnosticHandler; - Module *TheModule; - std::unique_ptr<MemoryBuffer> Buffer; - std::unique_ptr<BitstreamReader> StreamFile; - BitstreamCursor Stream; - DataStreamer *LazyStreamer; - uint64_t NextUnreadBit; - bool SeenValueSymbolTable; - - std::vector<Type*> TypeList; - BitcodeReaderValueList ValueList; - BitcodeReaderMDValueList MDValueList; - SmallVector<Instruction *, 64> InstructionList; - - std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; - std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; - - /// MAttributes - 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<AttributeSet> MAttributes; - - /// \brief The set of attribute groups. - std::map<unsigned, AttributeSet> MAttributeGroups; - - /// FunctionBBs - 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. - typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; - UpgradedIntrinsicMap UpgradedIntrinsics; - - // Map the bitcode's custom MDKind ID to the Module's MDKind ID. - DenseMap<unsigned, unsigned> MDKindMap; - - // 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; - - /// DeferredFunctionInfo - 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; - - /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These - /// are resolved lazily when functions are loaded. - typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy; - DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs; - - /// LLVM2_7MetadataDetected - True if metadata produced by LLVM 2.7 or - /// earlier was detected, in which case we behave slightly differently, - /// for compatibility. - /// FIXME: Remove in LLVM 3.0. - bool LLVM2_7MetadataDetected; - static const std::error_category &BitcodeErrorCategory(); - -public: - std::error_code Error(BitcodeError E, const Twine &Message); - std::error_code Error(BitcodeError E); - std::error_code Error(const Twine &Message); - - explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C, - DiagnosticHandlerFunction DiagnosticHandler); - ~BitcodeReader() { FreeState(); } - - void FreeState(); - - void releaseBuffer(); - - bool isDematerializable(const GlobalValue *GV) const override; - std::error_code materialize(GlobalValue *GV) override; - std::error_code MaterializeModule(Module *M) override; - std::vector<StructType *> getIdentifiedStructTypes() const override; - void Dematerialize(GlobalValue *GV) override; - - /// @brief Main interface to parsing a bitcode buffer. - /// @returns true if an error occurred. - std::error_code ParseBitcodeInto(Module *M); - - /// @brief Cheap mechanism to just extract module triple - /// @returns true if an error occurred. - llvm::ErrorOr<std::string> parseTriple(); - - static uint64_t decodeSignRotatedValue(uint64_t V); - -private: - std::vector<StructType *> IdentifiedStructTypes; - StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); - StructType *createIdentifiedStructType(LLVMContext &Context); - - Type *getTypeByID(unsigned ID); - Type *getTypeByIDOrNull(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 MDValueList.getValueFwdRef(ID); - } - BasicBlock *getBasicBlock(unsigned ID) const { - if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID - return FunctionBBs[ID]; - } - AttributeSet getAttributes(unsigned i) const { - if (i-1 < MAttributes.size()) - return MAttributes[i-1]; - return AttributeSet(); - } - - /// getValueTypePair - 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++]; - if (ValNo < InstNum) { - // If this is not a forward reference, just return the value we already - // have. - ResVal = getFnValueByID(ValNo, nullptr); - return ResVal == nullptr; - } else if (Slot == Record.size()) { - return true; - } - - unsigned TypeNo = (unsigned)Record[Slot++]; - ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); - return ResVal == nullptr; - } - bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot, - Type *Ty, Value *&ResVal) { - if (Slot == Record.size()) return true; - unsigned ValNo = (unsigned)Record[Slot++]; - ResVal = getFnValueByID(ValNo, Ty); - return ResVal == 0; - } - - - std::error_code ParseModule(bool Resume); - std::error_code ParseAttributeBlock(); - std::error_code ParseTypeTable(); - std::error_code ParseOldTypeTable(); // FIXME: Remove in LLVM 3.1 - std::error_code ParseTypeTableBody(); - - std::error_code ParseOldTypeSymbolTable(); // FIXME: Remove in LLVM 3.1 - std::error_code ParseValueSymbolTable(); - std::error_code ParseConstants(); - std::error_code RememberAndSkipFunctionBody(); - std::error_code ParseFunctionBody(Function *F); - std::error_code GlobalCleanup(); - std::error_code ResolveGlobalAndAliasInits(); - std::error_code ParseMetadata(); - std::error_code ParseMetadataAttachment(); - llvm::ErrorOr<std::string> parseModuleTriple(); - std::error_code InitStream(); - std::error_code InitStreamFromBuffer(); - std::error_code InitLazyStream(); -}; - -} // End llvm_2_7 namespace - -#endif diff --git a/bcinfo/BitReader_3_0/BitcodeReader.cpp b/bcinfo/BitReader_3_0/BitcodeReader.cpp index 8b8dc66..2d79bfc 100644 --- a/bcinfo/BitReader_3_0/BitcodeReader.cpp +++ b/bcinfo/BitReader_3_0/BitcodeReader.cpp @@ -12,8 +12,8 @@ //===----------------------------------------------------------------------===// #include "llvm/Bitcode/ReaderWriter.h" -#include "BitcodeReader.h" #include "BitReader_3_0.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/IR/AutoUpgrade.h" @@ -21,6 +21,7 @@ #include "llvm/IR/CFG.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/DiagnosticPrinter.h" +#include "llvm/IR/GVMaterializer.h" #include "llvm/IR/InlineAsm.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/IRBuilder.h" @@ -325,6 +326,272 @@ namespace { } } } + + +//===----------------------------------------------------------------------===// +// BitcodeReaderValueList Class +//===----------------------------------------------------------------------===// + +class BitcodeReaderValueList { + std::vector<WeakVH> ValuePtrs; + + /// ResolveConstants - As we resolve forward-referenced constants, we add + /// information about them to this vector. This allows us to resolve them in + /// bulk instead of resolving each reference at a time. See the code in + /// ResolveConstantForwardRefs for more information about this. + /// + /// The key of this vector is the placeholder constant, the value is the slot + /// number that holds the resolved value. + typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy; + ResolveConstantsTy ResolveConstants; + LLVMContext &Context; +public: + BitcodeReaderValueList(LLVMContext &C) : Context(C) {} + ~BitcodeReaderValueList() { + assert(ResolveConstants.empty() && "Constants not resolved?"); + } + + // vector compatibility methods + unsigned size() const { return ValuePtrs.size(); } + void resize(unsigned N) { ValuePtrs.resize(N); } + void push_back(Value *V) { + ValuePtrs.push_back(V); + } + + void clear() { + assert(ResolveConstants.empty() && "Constants not resolved?"); + ValuePtrs.clear(); + } + + Value *operator[](unsigned i) const { + assert(i < ValuePtrs.size()); + return ValuePtrs[i]; + } + + Value *back() const { return ValuePtrs.back(); } + void pop_back() { ValuePtrs.pop_back(); } + bool empty() const { return ValuePtrs.empty(); } + void shrinkTo(unsigned N) { + assert(N <= size() && "Invalid shrinkTo request!"); + ValuePtrs.resize(N); + } + + Constant *getConstantFwdRef(unsigned Idx, Type *Ty); + Value *getValueFwdRef(unsigned Idx, Type *Ty); + + void AssignValue(Value *V, unsigned Idx); + + /// ResolveConstantForwardRefs - Once all constants are read, this method bulk + /// resolves any forward references. + void ResolveConstantForwardRefs(); +}; + + +//===----------------------------------------------------------------------===// +// BitcodeReaderMDValueList Class +//===----------------------------------------------------------------------===// + +class BitcodeReaderMDValueList { + unsigned NumFwdRefs; + bool AnyFwdRefs; + std::vector<TrackingMDRef> MDValuePtrs; + + LLVMContext &Context; +public: + BitcodeReaderMDValueList(LLVMContext &C) + : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {} + + // vector compatibility methods + unsigned size() const { return MDValuePtrs.size(); } + void resize(unsigned N) { MDValuePtrs.resize(N); } + void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); } + void clear() { MDValuePtrs.clear(); } + Metadata *back() const { return MDValuePtrs.back(); } + void pop_back() { MDValuePtrs.pop_back(); } + bool empty() const { return MDValuePtrs.empty(); } + + Metadata *operator[](unsigned i) const { + assert(i < MDValuePtrs.size()); + return MDValuePtrs[i]; + } + + void shrinkTo(unsigned N) { + assert(N <= size() && "Invalid shrinkTo request!"); + MDValuePtrs.resize(N); + } + + Metadata *getValueFwdRef(unsigned Idx); + void AssignValue(Metadata *MD, unsigned Idx); + void tryToResolveCycles(); +}; + +class BitcodeReader : public GVMaterializer { + LLVMContext &Context; + DiagnosticHandlerFunction DiagnosticHandler; + Module *TheModule; + std::unique_ptr<MemoryBuffer> Buffer; + std::unique_ptr<BitstreamReader> StreamFile; + BitstreamCursor Stream; + DataStreamer *LazyStreamer; + uint64_t NextUnreadBit; + bool SeenValueSymbolTable; + + std::vector<Type*> TypeList; + BitcodeReaderValueList ValueList; + BitcodeReaderMDValueList MDValueList; + SmallVector<Instruction *, 64> InstructionList; + + std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; + std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; + + /// MAttributes - 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<AttributeSet> MAttributes; + + /// \brief The set of attribute groups. + std::map<unsigned, AttributeSet> MAttributeGroups; + + /// FunctionBBs - 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. + typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; + UpgradedIntrinsicMap UpgradedIntrinsics; + + // Map the bitcode's custom MDKind ID to the Module's MDKind ID. + DenseMap<unsigned, unsigned> MDKindMap; + + // 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; + + /// DeferredFunctionInfo - 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; + + /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These + /// are resolved lazily when functions are loaded. + typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy; + DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs; + + static const std::error_category &BitcodeErrorCategory(); + +public: + std::error_code Error(BitcodeError E, const Twine &Message); + std::error_code Error(BitcodeError E); + std::error_code Error(const Twine &Message); + + explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C, + DiagnosticHandlerFunction DiagnosticHandler); + ~BitcodeReader() { FreeState(); } + + void FreeState(); + + void releaseBuffer(); + + bool isDematerializable(const GlobalValue *GV) const override; + std::error_code materialize(GlobalValue *GV) override; + std::error_code MaterializeModule(Module *M) override; + std::vector<StructType *> getIdentifiedStructTypes() const override; + void Dematerialize(GlobalValue *GV) override; + + /// @brief Main interface to parsing a bitcode buffer. + /// @returns true if an error occurred. + std::error_code ParseBitcodeInto(Module *M); + + /// @brief Cheap mechanism to just extract module triple + /// @returns true if an error occurred. + llvm::ErrorOr<std::string> parseTriple(); + + static uint64_t decodeSignRotatedValue(uint64_t V); + + /// Materialize any deferred Metadata block. + std::error_code materializeMetadata() override; + +private: + std::vector<StructType *> IdentifiedStructTypes; + StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); + StructType *createIdentifiedStructType(LLVMContext &Context); + + Type *getTypeByID(unsigned ID); + Type *getTypeByIDOrNull(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 MDValueList.getValueFwdRef(ID); + } + BasicBlock *getBasicBlock(unsigned ID) const { + if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID + return FunctionBBs[ID]; + } + AttributeSet getAttributes(unsigned i) const { + if (i-1 < MAttributes.size()) + return MAttributes[i-1]; + return AttributeSet(); + } + + /// getValueTypePair - 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++]; + if (ValNo < InstNum) { + // If this is not a forward reference, just return the value we already + // have. + ResVal = getFnValueByID(ValNo, nullptr); + return ResVal == nullptr; + } else if (Slot == Record.size()) { + return true; + } + + unsigned TypeNo = (unsigned)Record[Slot++]; + ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); + return ResVal == nullptr; + } + bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot, + Type *Ty, Value *&ResVal) { + if (Slot == Record.size()) return true; + unsigned ValNo = (unsigned)Record[Slot++]; + ResVal = getFnValueByID(ValNo, Ty); + return ResVal == 0; + } + + + std::error_code ParseModule(bool Resume); + std::error_code ParseAttributeBlock(); + std::error_code ParseTypeTable(); + std::error_code ParseOldTypeTable(); // FIXME: Remove in LLVM 3.1 + std::error_code ParseTypeTableBody(); + + std::error_code ParseOldTypeSymbolTable(); // FIXME: Remove in LLVM 3.1 + std::error_code ParseValueSymbolTable(); + std::error_code ParseConstants(); + std::error_code RememberAndSkipFunctionBody(); + std::error_code ParseFunctionBody(Function *F); + std::error_code GlobalCleanup(); + std::error_code ResolveGlobalAndAliasInits(); + std::error_code ParseMetadata(); + std::error_code ParseMetadataAttachment(); + llvm::ErrorOr<std::string> parseModuleTriple(); + std::error_code InitStream(); + std::error_code InitStreamFromBuffer(); + std::error_code InitLazyStream(); +}; + } // end anonymous namespace static std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler, @@ -1919,7 +2186,8 @@ std::error_code BitcodeReader::ParseConstants() { V = ConstantExpr::getICmp(Record[3], Op0, Op1); break; } - case bitc::CST_CODE_INLINEASM: { + case bitc::CST_CODE_INLINEASM: + case bitc::CST_CODE_INLINEASM_OLD: { if (Record.size() < 2) return Error("Invalid record"); std::string AsmStr, ConstrStr; @@ -1978,6 +2246,10 @@ std::error_code BitcodeReader::ParseConstants() { return std::error_code(); } +std::error_code BitcodeReader::materializeMetadata() { + return std::error_code(); +} + /// RememberAndSkipFunctionBody - When we see the block for a function body, /// remember where it is and then skip it. This lets us lazily deserialize the /// functions. @@ -2682,10 +2954,29 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { case bitc::FUNC_CODE_INST_GEP_OLD: // GEP: [n x operands] case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands] unsigned OpNum = 0; + + Type *Ty; + bool InBounds; + + if (BitCode == bitc::FUNC_CODE_INST_GEP) { + InBounds = Record[OpNum++]; + Ty = getTypeByID(Record[OpNum++]); + } else { + InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; + Ty = nullptr; + } + Value *BasePtr; if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) return Error("Invalid record"); + if (Ty && + Ty != + cast<SequentialType>(BasePtr->getType()->getScalarType()) + ->getElementType()) + return Error( + "Explicit gep type does not match pointee type of pointer operand"); + SmallVector<Value*, 16> GEPIdx; while (OpNum != Record.size()) { Value *Op; @@ -2694,9 +2985,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { GEPIdx.push_back(Op); } - I = GetElementPtrInst::Create(BasePtr, GEPIdx); + I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx); + InstructionList.push_back(I); - if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD) + if (InBounds) cast<GetElementPtrInst>(I)->setIsInBounds(true); break; } @@ -3347,6 +3639,9 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { void BitcodeReader::releaseBuffer() { Buffer.release(); } std::error_code BitcodeReader::materialize(GlobalValue *GV) { + if (std::error_code EC = materializeMetadata()) + return EC; + Function *F = dyn_cast<Function>(GV); // If it's not a function or is already material, ignore the request. if (!F || !F->isMaterializable()) @@ -3515,7 +3810,7 @@ class BitcodeErrorCategoryType : public std::error_category { static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory; -const std::error_category &llvm_3_0::BitcodeReader::BitcodeErrorCategory() { +const std::error_category &BitcodeReader::BitcodeErrorCategory() { return *ErrorCategory; } diff --git a/bcinfo/BitReader_3_0/BitcodeReader.h b/bcinfo/BitReader_3_0/BitcodeReader.h deleted file mode 100644 index 507c718..0000000 --- a/bcinfo/BitReader_3_0/BitcodeReader.h +++ /dev/null @@ -1,301 +0,0 @@ -//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This header defines the BitcodeReader class. -// -//===----------------------------------------------------------------------===// - -#ifndef BITCODE_READER_H -#define BITCODE_READER_H - -#include "llvm/ADT/DenseMap.h" -#include "llvm/Bitcode/BitstreamReader.h" -#include "llvm/Bitcode/LLVMBitCodes.h" -#include "llvm/IR/Attributes.h" -#include "llvm/IR/GVMaterializer.h" -#include "llvm/IR/Metadata.h" -#include "llvm/IR/OperandTraits.h" -#include "llvm/IR/TrackingMDRef.h" -#include "llvm/IR/Type.h" -#include "llvm/IR/ValueHandle.h" -#include <vector> - -namespace llvm { - class MemoryBuffer; - class LLVMContext; -} - -namespace llvm_3_0 { - -using namespace llvm; - -//===----------------------------------------------------------------------===// -// BitcodeReaderValueList Class -//===----------------------------------------------------------------------===// - -class BitcodeReaderValueList { - std::vector<WeakVH> ValuePtrs; - - /// ResolveConstants - As we resolve forward-referenced constants, we add - /// information about them to this vector. This allows us to resolve them in - /// bulk instead of resolving each reference at a time. See the code in - /// ResolveConstantForwardRefs for more information about this. - /// - /// The key of this vector is the placeholder constant, the value is the slot - /// number that holds the resolved value. - typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy; - ResolveConstantsTy ResolveConstants; - LLVMContext &Context; -public: - BitcodeReaderValueList(LLVMContext &C) : Context(C) {} - ~BitcodeReaderValueList() { - assert(ResolveConstants.empty() && "Constants not resolved?"); - } - - // vector compatibility methods - unsigned size() const { return ValuePtrs.size(); } - void resize(unsigned N) { ValuePtrs.resize(N); } - void push_back(Value *V) { - ValuePtrs.push_back(V); - } - - void clear() { - assert(ResolveConstants.empty() && "Constants not resolved?"); - ValuePtrs.clear(); - } - - Value *operator[](unsigned i) const { - assert(i < ValuePtrs.size()); - return ValuePtrs[i]; - } - - Value *back() const { return ValuePtrs.back(); } - void pop_back() { ValuePtrs.pop_back(); } - bool empty() const { return ValuePtrs.empty(); } - void shrinkTo(unsigned N) { - assert(N <= size() && "Invalid shrinkTo request!"); - ValuePtrs.resize(N); - } - - Constant *getConstantFwdRef(unsigned Idx, Type *Ty); - Value *getValueFwdRef(unsigned Idx, Type *Ty); - - void AssignValue(Value *V, unsigned Idx); - - /// ResolveConstantForwardRefs - Once all constants are read, this method bulk - /// resolves any forward references. - void ResolveConstantForwardRefs(); -}; - - -//===----------------------------------------------------------------------===// -// BitcodeReaderMDValueList Class -//===----------------------------------------------------------------------===// - -class BitcodeReaderMDValueList { - unsigned NumFwdRefs; - bool AnyFwdRefs; - std::vector<TrackingMDRef> MDValuePtrs; - - LLVMContext &Context; -public: - BitcodeReaderMDValueList(LLVMContext &C) - : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {} - - // vector compatibility methods - unsigned size() const { return MDValuePtrs.size(); } - void resize(unsigned N) { MDValuePtrs.resize(N); } - void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); } - void clear() { MDValuePtrs.clear(); } - Metadata *back() const { return MDValuePtrs.back(); } - void pop_back() { MDValuePtrs.pop_back(); } - bool empty() const { return MDValuePtrs.empty(); } - - Metadata *operator[](unsigned i) const { - assert(i < MDValuePtrs.size()); - return MDValuePtrs[i]; - } - - void shrinkTo(unsigned N) { - assert(N <= size() && "Invalid shrinkTo request!"); - MDValuePtrs.resize(N); - } - - Metadata *getValueFwdRef(unsigned Idx); - void AssignValue(Metadata *MD, unsigned Idx); - void tryToResolveCycles(); -}; - -class BitcodeReader : public GVMaterializer { - LLVMContext &Context; - DiagnosticHandlerFunction DiagnosticHandler; - Module *TheModule; - std::unique_ptr<MemoryBuffer> Buffer; - std::unique_ptr<BitstreamReader> StreamFile; - BitstreamCursor Stream; - DataStreamer *LazyStreamer; - uint64_t NextUnreadBit; - bool SeenValueSymbolTable; - - std::vector<Type*> TypeList; - BitcodeReaderValueList ValueList; - BitcodeReaderMDValueList MDValueList; - SmallVector<Instruction *, 64> InstructionList; - - std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; - std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; - - /// MAttributes - 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<AttributeSet> MAttributes; - - /// \brief The set of attribute groups. - std::map<unsigned, AttributeSet> MAttributeGroups; - - /// FunctionBBs - 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. - typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap; - UpgradedIntrinsicMap UpgradedIntrinsics; - - // Map the bitcode's custom MDKind ID to the Module's MDKind ID. - DenseMap<unsigned, unsigned> MDKindMap; - - // 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; - - /// DeferredFunctionInfo - 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; - - /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These - /// are resolved lazily when functions are loaded. - typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy; - DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs; - - static const std::error_category &BitcodeErrorCategory(); - -public: - std::error_code Error(BitcodeError E, const Twine &Message); - std::error_code Error(BitcodeError E); - std::error_code Error(const Twine &Message); - - explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C, - DiagnosticHandlerFunction DiagnosticHandler); - ~BitcodeReader() { FreeState(); } - - void FreeState(); - - void releaseBuffer(); - - bool isDematerializable(const GlobalValue *GV) const override; - std::error_code materialize(GlobalValue *GV) override; - std::error_code MaterializeModule(Module *M) override; - std::vector<StructType *> getIdentifiedStructTypes() const override; - void Dematerialize(GlobalValue *GV) override; - - /// @brief Main interface to parsing a bitcode buffer. - /// @returns true if an error occurred. - std::error_code ParseBitcodeInto(Module *M); - - /// @brief Cheap mechanism to just extract module triple - /// @returns true if an error occurred. - llvm::ErrorOr<std::string> parseTriple(); - - static uint64_t decodeSignRotatedValue(uint64_t V); - -private: - std::vector<StructType *> IdentifiedStructTypes; - StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); - StructType *createIdentifiedStructType(LLVMContext &Context); - - Type *getTypeByID(unsigned ID); - Type *getTypeByIDOrNull(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 MDValueList.getValueFwdRef(ID); - } - BasicBlock *getBasicBlock(unsigned ID) const { - if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID - return FunctionBBs[ID]; - } - AttributeSet getAttributes(unsigned i) const { - if (i-1 < MAttributes.size()) - return MAttributes[i-1]; - return AttributeSet(); - } - - /// getValueTypePair - 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++]; - if (ValNo < InstNum) { - // If this is not a forward reference, just return the value we already - // have. - ResVal = getFnValueByID(ValNo, nullptr); - return ResVal == nullptr; - } else if (Slot == Record.size()) { - return true; - } - - unsigned TypeNo = (unsigned)Record[Slot++]; - ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo)); - return ResVal == nullptr; - } - bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot, - Type *Ty, Value *&ResVal) { - if (Slot == Record.size()) return true; - unsigned ValNo = (unsigned)Record[Slot++]; - ResVal = getFnValueByID(ValNo, Ty); - return ResVal == 0; - } - - - std::error_code ParseModule(bool Resume); - std::error_code ParseAttributeBlock(); - std::error_code ParseTypeTable(); - std::error_code ParseOldTypeTable(); // FIXME: Remove in LLVM 3.1 - std::error_code ParseTypeTableBody(); - - std::error_code ParseOldTypeSymbolTable(); // FIXME: Remove in LLVM 3.1 - std::error_code ParseValueSymbolTable(); - std::error_code ParseConstants(); - std::error_code RememberAndSkipFunctionBody(); - std::error_code ParseFunctionBody(Function *F); - std::error_code GlobalCleanup(); - std::error_code ResolveGlobalAndAliasInits(); - std::error_code ParseMetadata(); - std::error_code ParseMetadataAttachment(); - llvm::ErrorOr<std::string> parseModuleTriple(); - std::error_code InitStream(); - std::error_code InitStreamFromBuffer(); - std::error_code InitLazyStream(); -}; - -} // End llvm_3_0 namespace - -#endif diff --git a/include/bcc/Compiler.h b/include/bcc/Compiler.h index 53e42ff..e54dc2c 100644 --- a/include/bcc/Compiler.h +++ b/include/bcc/Compiler.h @@ -58,7 +58,6 @@ public: kErrCreateTargetMachine, kErrSwitchTargetMachine, kErrNoTargetMachine, - kErrDataLayoutNoMemory, kErrMaterialization, kErrInvalidOutputFileState, kErrPrepareOutput, diff --git a/lib/Core/Compiler.cpp b/lib/Core/Compiler.cpp index e446f9c..a1ce90b 100644 --- a/lib/Core/Compiler.cpp +++ b/lib/Core/Compiler.cpp @@ -59,8 +59,6 @@ const char *Compiler::GetErrorString(enum ErrorCode pErrCode) { case kErrNoTargetMachine: return "Failed to compile the script since there's no available " "TargetMachine. (missing call to Compiler::config()?)"; - case kErrDataLayoutNoMemory: - return "Out of memory when create DataLayout during compilation."; case kErrMaterialization: return "Failed to materialize the module."; case kErrInvalidOutputFileState: @@ -155,17 +153,6 @@ enum Compiler::ErrorCode Compiler::runPasses(Script &pScript, passes.add(createTargetTransformInfoWrapperPass(mTarget->getTargetIRAnalysis())); //mTarget->addAnalysisPasses(passes); - // Prepare DataLayout target data from Module - llvm::DataLayoutPass *data_layout_pass = - new (std::nothrow) llvm::DataLayoutPass(); - - if (data_layout_pass == nullptr) { - return kErrDataLayoutNoMemory; - } - - // Add DataLayout to the pass manager. - passes.add(data_layout_pass); - // Add our custom passes. if (!addCustomPasses(pScript, passes)) { return kErrCustomPasses; |