X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FTarget%2FTargetData.h;h=3136a38ac0fade7390b5cd56612901901976a1b9;hb=ca0ed744852a7d9625572fbb793f65e81225a3e8;hp=b045bef616f71250fd443c228e044685d855478d;hpb=1027a533d4f1806ff7d2f721504028201cffa7b5;p=oota-llvm.git diff --git a/include/llvm/Target/TargetData.h b/include/llvm/Target/TargetData.h index b045bef616f..3136a38ac0f 100644 --- a/include/llvm/Target/TargetData.h +++ b/include/llvm/Target/TargetData.h @@ -22,7 +22,7 @@ #include "llvm/Pass.h" #include "llvm/Support/DataTypes.h" -#include +#include "llvm/ADT/SmallVector.h" #include namespace llvm { @@ -33,218 +33,192 @@ class StructType; class StructLayout; class GlobalVariable; +/// Enum used to categorize the alignment types stored by TargetAlignElem +enum AlignTypeEnum { + INTEGER_ALIGN = 'i', ///< Integer type alignment + VECTOR_ALIGN = 'v', ///< Vector type alignment + FLOAT_ALIGN = 'f', ///< Floating point type alignment + AGGREGATE_ALIGN = 'a', ///< Aggregate alignment + STACK_ALIGN = 's' ///< Stack objects alignment +}; +/// Target alignment element. +/// +/// Stores the alignment data associated with a given alignment type (pointer, +/// integer, vector, float) and type bit width. +/// +/// @note The unusual order of elements in the structure attempts to reduce +/// padding and make the structure slightly more cache friendly. +struct TargetAlignElem { + AlignTypeEnum AlignType : 8; //< Alignment type (AlignTypeEnum) + unsigned char ABIAlign; //< ABI alignment for this type/bitw + unsigned char PrefAlign; //< Pref. alignment for this type/bitw + uint32_t TypeBitWidth; //< Type bit width + + /// Initializer + static TargetAlignElem get(AlignTypeEnum align_type, unsigned char abi_align, + unsigned char pref_align, uint32_t bit_width); + /// Equality predicate + bool operator==(const TargetAlignElem &rhs) const; + /// output stream operator + std::ostream &dump(std::ostream &os) const; +}; + class TargetData : public ImmutablePass { - bool LittleEndian; // Defaults to false - - // ABI alignments - unsigned char BoolABIAlignment; // Defaults to 1 byte - unsigned char ByteABIAlignment; // Defaults to 1 byte - unsigned char ShortABIAlignment; // Defaults to 2 bytes - unsigned char IntABIAlignment; // Defaults to 4 bytes - unsigned char LongABIAlignment; // Defaults to 8 bytes - unsigned char FloatABIAlignment; // Defaults to 4 bytes - unsigned char DoubleABIAlignment; // Defaults to 8 bytes - unsigned char PointerMemSize; // Defaults to 8 bytes - unsigned char PointerABIAlignment; // Defaults to 8 bytes - - // Preferred stack/global type alignments - unsigned char BoolPrefAlignment; // Defaults to BoolABIAlignment - unsigned char BytePrefAlignment; // Defaults to ByteABIAlignment - unsigned char ShortPrefAlignment; // Defaults to ShortABIAlignment - unsigned char IntPrefAlignment; // Defaults to IntABIAlignment - unsigned char LongPrefAlignment; // Defaults to LongABIAlignment - unsigned char FloatPrefAlignment; // Defaults to FloatABIAlignment - unsigned char DoublePrefAlignment; // Defaults to DoubleABIAlignment - unsigned char PointerPrefAlignment; // Defaults to PointerABIAlignment - unsigned char AggMinPrefAlignment; // Defaults to 0 bytes +private: + bool LittleEndian; ///< Defaults to false + unsigned char PointerMemSize; ///< Pointer size in bytes + unsigned char PointerABIAlign; ///< Pointer ABI alignment + unsigned char PointerPrefAlign; ///< Pointer preferred alignment + + //! Where the primitive type alignment data is stored. + /*! + @sa init(). + @note Could support multiple size pointer alignments, e.g., 32-bit pointers + vs. 64-bit pointers by extending TargetAlignment, but for now, we don't. + */ + SmallVector Alignments; + //! Alignment iterator shorthand + typedef SmallVector::iterator align_iterator; + //! Constant alignment iterator shorthand + typedef SmallVector::const_iterator align_const_iterator; + //! Invalid alignment. + /*! + This member is a signal that a requested alignment type and bit width were + not found in the SmallVector. + */ + static const TargetAlignElem InvalidAlignmentElem; + + //! Set/initialize target alignments + void setAlignment(AlignTypeEnum align_type, unsigned char abi_align, + unsigned char pref_align, uint32_t bit_width); + unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width, + bool ABIAlign) const; + //! Internal helper method that returns requested alignment for type. + unsigned char getAlignment(const Type *Ty, bool abi_or_pref) const; + + /// Valid alignment predicate. + /// + /// Predicate that tests a TargetAlignElem reference returned by get() against + /// InvalidAlignmentElem. + inline bool validAlignment(const TargetAlignElem &align) const { + return (&align != &InvalidAlignmentElem); + } public: - /// Default ctor - This has to exist, because this is a pass, but it should - /// never be used. - TargetData() { + /// Default ctor. + /// + /// @note This has to exist, because this is a pass, but it should never be + /// used. + TargetData() : ImmutablePass(intptr_t(&ID)) { assert(0 && "ERROR: Bad TargetData ctor used. " "Tool did not specify a TargetData to use?"); abort(); } - /// Constructs a TargetData from a string of the following format: - /// "E-p:64:64-d:64-f:32-l:64-i:32-s:16-b:8-B:8" - /// The above string is considered the default, and any values not specified - /// in the string will be assumed to be as above, with the caveat that unspecified - /// values are always assumed to be smaller than the size of a pointer. - TargetData(const std::string &TargetDescription) { + /// Constructs a TargetData from a specification string. See init(). + explicit TargetData(const std::string &TargetDescription) + : ImmutablePass(intptr_t(&ID)) { init(TargetDescription); } /// Initialize target data from properties stored in the module. - TargetData(const Module *M); + explicit TargetData(const Module *M); TargetData(const TargetData &TD) : - ImmutablePass(), + ImmutablePass(intptr_t(&ID)), LittleEndian(TD.isLittleEndian()), - BoolABIAlignment(TD.getBoolABIAlignment()), - ByteABIAlignment(TD.getByteABIAlignment()), - ShortABIAlignment(TD.getShortABIAlignment()), - IntABIAlignment(TD.getIntABIAlignment()), - LongABIAlignment(TD.getLongABIAlignment()), - FloatABIAlignment(TD.getFloatABIAlignment()), - DoubleABIAlignment(TD.getDoubleABIAlignment()), - PointerMemSize(TD.getPointerSize()), - PointerABIAlignment(TD.getPointerABIAlignment()), - BoolPrefAlignment(TD.getBoolPrefAlignment()), - BytePrefAlignment(TD.getBytePrefAlignment()), - ShortPrefAlignment(TD.getShortPrefAlignment()), - IntPrefAlignment(TD.getIntPrefAlignment()), - LongPrefAlignment(TD.getLongPrefAlignment()), - FloatPrefAlignment(TD.getFloatPrefAlignment()), - DoublePrefAlignment(TD.getDoublePrefAlignment()), - PointerPrefAlignment(TD.getPointerPrefAlignment()), - AggMinPrefAlignment(TD.getAggMinPrefAlignment()) { - } + PointerMemSize(TD.PointerMemSize), + PointerABIAlign(TD.PointerABIAlign), + PointerPrefAlign(TD.PointerPrefAlign), + Alignments(TD.Alignments) + { } ~TargetData(); // Not virtual, do not subclass this class - /// Parse a target data layout string and initialize TargetData members. - /// - /// Parse a target data layout string, initializing the various TargetData - /// members along the way. A TargetData specification string looks like - /// "E-p:64:64-d:64-f:32-l:64-i:32-s:16-b:8-B:8" and specifies the - /// target's endianess, the ABI alignments of various data types and - /// the size of pointers. - /// - /// "-" is used as a separator and ":" separates a token from its argument. - /// - /// Alignment is indicated in bits and internally converted to the - /// appropriate number of bytes. - /// - /// The preferred stack/global alignment specifications (":[prefalign]") are - /// optional and default to the ABI alignment. - /// - /// Valid tokens: - ///
- /// E specifies big endian architecture (1234) [default]
- /// e specifies little endian architecture (4321)
- /// p:[ptr size]:[ptr align] specifies pointer size and alignment - /// [default = 64:64]
- /// d:[align]:[prefalign] specifies double floating - /// point alignment [default = 64]
- /// f:[align]:[prefalign] specifies single floating - /// point alignment [default = 32]
- /// l:[align]:[prefalign]:[globalign[ specifies long integer - /// alignment [default = 64]
- /// i:[align]:[prefalign] specifies integer alignment - /// [default = 32]
- /// s:[align]:[prefalign] specifies short integer - /// alignment [default = 16]
- /// b:[align]:[prefalign] specifies byte data type - /// alignment [default = 8]
- /// B:[align]:[prefalign] specifies boolean data type - /// alignment [default = 8]
- /// A:[prefalign] specifies an aggregates' minimum alignment - /// on the stack and when emitted as a global. The default minimum aggregate - /// alignment defaults to 0, which causes the aggregate's "natural" internal - /// alignment calculated by llvm to be preferred. - /// - /// All other token types are silently ignored. + //! Parse a target data layout string and initialize TargetData alignments. void init(const std::string &TargetDescription); - /// Target endianness... bool isLittleEndian() const { return LittleEndian; } bool isBigEndian() const { return !LittleEndian; } - /// Target boolean alignment - unsigned char getBoolABIAlignment() const { return BoolABIAlignment; } - /// Target byte alignment - unsigned char getByteABIAlignment() const { return ByteABIAlignment; } - /// Target short alignment - unsigned char getShortABIAlignment() const { return ShortABIAlignment; } - /// Target integer alignment - unsigned char getIntABIAlignment() const { return IntABIAlignment; } - /// Target long alignment - unsigned char getLongABIAlignment() const { return LongABIAlignment; } - /// Target single precision float alignment - unsigned char getFloatABIAlignment() const { return FloatABIAlignment; } - /// Target double precision float alignment - unsigned char getDoubleABIAlignment() const { return DoubleABIAlignment; } + /// getStringRepresentation - Return the string representation of the + /// TargetData. This representation is in the same format accepted by the + /// string constructor above. + std::string getStringRepresentation() const; /// Target pointer alignment - unsigned char getPointerABIAlignment() const { return PointerABIAlignment; } + unsigned char getPointerABIAlignment() const { return PointerABIAlign; } + /// Return target's alignment for stack-based pointers + unsigned char getPointerPrefAlignment() const { return PointerPrefAlign; } /// Target pointer size - unsigned char getPointerSize() const { return PointerMemSize; } + unsigned char getPointerSize() const { return PointerMemSize; } /// Target pointer size, in bits - unsigned char getPointerSizeInBits() const { return 8*PointerMemSize; } + unsigned char getPointerSizeInBits() const { return 8*PointerMemSize; } - /// Return target's alignment for booleans on stack - unsigned char getBoolPrefAlignment() const { - return BoolPrefAlignment; - } - /// Return target's alignment for integers on stack - unsigned char getBytePrefAlignment() const { - return BytePrefAlignment; - } - /// Return target's alignment for shorts on stack - unsigned char getShortPrefAlignment() const { - return ShortPrefAlignment; - } - /// Return target's alignment for integers on stack - unsigned char getIntPrefAlignment() const { - return IntPrefAlignment; - } - /// Return target's alignment for longs on stack - unsigned char getLongPrefAlignment() const { - return LongPrefAlignment; - } - /// Return target's alignment for single precision floats on stack - unsigned char getFloatPrefAlignment() const { - return FloatPrefAlignment; + /// getTypeSizeInBits - Return the number of bits necessary to hold the + /// specified type. For example, returns 36 for i36 and 80 for x86_fp80. + uint64_t getTypeSizeInBits(const Type* Ty) const; + + /// getTypeStoreSize - Return the maximum number of bytes that may be + /// overwritten by storing the specified type. For example, returns 5 + /// for i36 and 10 for x86_fp80. + uint64_t getTypeStoreSize(const Type *Ty) const { + return (getTypeSizeInBits(Ty)+7)/8; } - /// Return target's alignment for double preceision floats on stack - unsigned char getDoublePrefAlignment() const { - return DoublePrefAlignment; + + /// getTypeStoreSizeInBits - Return the maximum number of bits that may be + /// overwritten by storing the specified type; always a multiple of 8. For + /// example, returns 40 for i36 and 80 for x86_fp80. + uint64_t getTypeStoreSizeInBits(const Type *Ty) const { + return 8*getTypeStoreSize(Ty); } - /// Return target's alignment for stack-based pointers - unsigned char getPointerPrefAlignment() const { - return PointerPrefAlignment; + + /// getABITypeSize - Return the offset in bytes between successive objects + /// of the specified type, including alignment padding. This is the amount + /// that alloca reserves for this type. For example, returns 12 or 16 for + /// x86_fp80, depending on alignment. + uint64_t getABITypeSize(const Type* Ty) const { + unsigned char Align = getABITypeAlignment(Ty); + return (getTypeStoreSize(Ty) + Align - 1)/Align*Align; } - /// Return target's alignment for stack-based structures - unsigned char getAggMinPrefAlignment() const { - return AggMinPrefAlignment; + + /// getABITypeSizeInBits - Return the offset in bits between successive + /// objects of the specified type, including alignment padding; always a + /// multiple of 8. This is the amount that alloca reserves for this type. + /// For example, returns 96 or 128 for x86_fp80, depending on alignment. + uint64_t getABITypeSizeInBits(const Type* Ty) const { + return 8*getABITypeSize(Ty); } - /// getStringRepresentation - Return the string representation of the - /// TargetData. This representation is in the same format accepted by the - /// string constructor above. - std::string getStringRepresentation() const; + /// getABITypeAlignment - Return the minimum ABI-required alignment for the + /// specified type. + unsigned char getABITypeAlignment(const Type *Ty) const; - /// getTypeSize - Return the number of bytes necessary to hold the specified - /// type. - /// - uint64_t getTypeSize(const Type *Ty) const; + /// getCallFrameTypeAlignment - Return the minimum ABI-required alignment + /// for the specified type when it is part of a call frame. + unsigned char getCallFrameTypeAlignment(const Type *Ty) const; - /// getTypeAlignmentABI - Return the minimum ABI-required alignment for the - /// specified type. - unsigned char getTypeAlignmentABI(const Type *Ty) const; - /// getTypeAlignmentPref - Return the preferred stack/global alignment for + /// getPrefTypeAlignment - Return the preferred stack/global alignment for /// the specified type. - unsigned char getTypeAlignmentPref(const Type *Ty) const; + unsigned char getPrefTypeAlignment(const Type *Ty) const; - /// getTypeAlignmentShift - Return the minimum required alignment for the + /// getPreferredTypeAlignmentShift - Return the preferred alignment for the /// specified type, returned as log2 of the value (a shift amount). /// - unsigned char getTypeAlignmentShift(const Type *Ty) const; + unsigned char getPreferredTypeAlignmentShift(const Type *Ty) const; /// getIntPtrType - Return an unsigned integer type that is the same size or /// greater to the host pointer size. /// const Type *getIntPtrType() const; - /// getIndexOffset - return the offset from the beginning of the type for the + /// getIndexedOffset - return the offset from the beginning of the type for the /// specified indices. This is used to implement getelementptr. /// uint64_t getIndexedOffset(const Type *Ty, - const std::vector &Indices) const; - + Value* const* Indices, unsigned NumIndices) const; + /// getStructLayout - Return a StructLayout object, indicating the alignment /// of the struct, its size, and the offsets of its fields. Note that this /// information is lazily cached. @@ -260,22 +234,46 @@ public: /// specified global, returned in log form. This includes an explicitly /// requested alignment (if the global has one). unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const; + + static char ID; // Pass identification, replacement for typeid }; /// StructLayout - used to lazily calculate structure layout information for a /// target machine, based on the TargetData structure. /// class StructLayout { -public: - std::vector MemberOffsets; uint64_t StructSize; unsigned StructAlignment; + unsigned NumElements; + uint64_t MemberOffsets[1]; // variable sized array! +public: + + uint64_t getSizeInBytes() const { + return StructSize; + } + + uint64_t getSizeInBits() const { + return 8*StructSize; + } + unsigned getAlignment() const { + return StructAlignment; + } + /// getElementContainingOffset - Given a valid offset into the structure, /// return the structure index that contains it. /// unsigned getElementContainingOffset(uint64_t Offset) const; + uint64_t getElementOffset(unsigned Idx) const { + assert(Idx < NumElements && "Invalid element idx!"); + return MemberOffsets[Idx]; + } + + uint64_t getElementOffsetInBits(unsigned Idx) const { + return getElementOffset(Idx)*8; + } + private: friend class TargetData; // Only TargetData can create this class StructLayout(const StructType *ST, const TargetData &TD);