diff options
-rw-r--r-- | include/llvm/Target/TargetData.h | 250 | ||||
-rw-r--r-- | lib/CodeGen/ELFWriter.cpp | 2 | ||||
-rw-r--r-- | lib/CodeGen/MachOWriter.cpp | 4 | ||||
-rw-r--r-- | lib/CodeGen/MachineFunction.cpp | 3 | ||||
-rw-r--r-- | lib/CodeGen/SelectionDAG/LegalizeDAG.cpp | 6 | ||||
-rw-r--r-- | lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp | 8 | ||||
-rw-r--r-- | lib/ExecutionEngine/JIT/JIT.cpp | 2 | ||||
-rw-r--r-- | lib/Target/ARM/ARMTargetMachine.cpp | 10 | ||||
-rw-r--r-- | lib/Target/PowerPC/PPCSubtarget.h | 4 | ||||
-rw-r--r-- | lib/Target/Sparc/SparcAsmPrinter.cpp | 2 | ||||
-rw-r--r-- | lib/Target/TargetData.cpp | 521 | ||||
-rw-r--r-- | lib/Target/X86/X86TargetMachine.cpp | 4 | ||||
-rw-r--r-- | lib/Transforms/Scalar/InstructionCombining.cpp | 25 |
13 files changed, 442 insertions, 399 deletions
diff --git a/include/llvm/Target/TargetData.h b/include/llvm/Target/TargetData.h index ed9d86e935..4f9d7c9f48 100644 --- a/include/llvm/Target/TargetData.h +++ b/include/llvm/Target/TargetData.h @@ -22,6 +22,8 @@ #include "llvm/Pass.h" #include "llvm/Support/DataTypes.h" +#include "llvm/ADT/SmallVector.h" +#include <string> namespace llvm { @@ -31,45 +33,96 @@ class StructType; class StructLayout; class GlobalVariable; -class TargetData : public ImmutablePass { - bool LittleEndian; // Defaults to false +/// Enum used to categorize the alignment types stored by TargetAlignElem +enum AlignTypeEnum { + INTEGER_ALIGN = 'i', ///< Integer type alignment + PACKED_ALIGN = 'v', ///< Vector type alignment + FLOAT_ALIGN = 'f', ///< Floating point type alignment + AGGREGATE_ALIGN = 'a' ///< Aggregate alignment +}; +/// Target alignment element. +/// +/// Stores the alignment data associated with a given alignment type (pointer, +/// integer, packed/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 { + unsigned char AlignType; //< Alignment type (AlignTypeEnum) + unsigned char ABIAlign; //< ABI alignment for this type/bitw + unsigned char PrefAlign; //< Pref. alignment for this type/bitw + short TypeBitWidth; //< Type bit width + + /// Initializer + static TargetAlignElem get(AlignTypeEnum align_type, unsigned char abi_align, + unsigned char pref_align, short bit_width); + /// Less-than predicate + bool operator<(const TargetAlignElem &rhs) const; + /// Equality predicate + bool operator==(const TargetAlignElem &rhs) const; + /// output stream operator + std::ostream &dump(std::ostream &os) const; +}; - // 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 +//! TargetAlignElem output stream inserter +/*! + @sa TargetAlignElem::dump() + */ +std::ostream &operator<<(std::ostream &os, const TargetAlignElem &elem); - // 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 +class TargetData : public ImmutablePass { +private: + bool LittleEndian; ///< Defaults to false + unsigned char PointerMemSize; ///< Pointer size in bytes + unsigned char PointerABIAlign; ///< Pointer ABI alignment + unsigned char PointerPrefAlign; ///< Pointer preferred global 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<TargetAlignElem, 16> Alignments; + //! Alignment iterator shorthand + typedef SmallVector<TargetAlignElem, 16>::iterator align_iterator; + //! Constant alignment iterator shorthand + typedef SmallVector<TargetAlignElem, 16>::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, short bit_width); + //! Get TargetAlignElem from alignment type and bit width + const TargetAlignElem &getAlignment(AlignTypeEnum, short) 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. + /// Default ctor. + /// + /// @note This has to exist, because this is a pass, but it should never be + /// used. TargetData() { 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. + /// Constructs a TargetData from a specification string. See init(). TargetData(const std::string &TargetDescription) { init(TargetDescription); } @@ -80,143 +133,36 @@ public: TargetData(const TargetData &TD) : ImmutablePass(), 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: - /// <br> - /// <em>E</em> specifies big endian architecture (1234) [default]<br> - /// <em>e</em> specifies little endian architecture (4321) <br> - /// <em>p:[ptr size]:[ptr align]</em> specifies pointer size and alignment - /// [default = 64:64] <br> - /// <em>d:[align]:[prefalign]</em> specifies double floating - /// point alignment [default = 64] <br> - /// <em>f:[align]:[prefalign]</em> specifies single floating - /// point alignment [default = 32] <br> - /// <em>l:[align]:[prefalign]:[globalign[</em> specifies long integer - /// alignment [default = 64] <br> - /// <em>i:[align]:[prefalign]</em> specifies integer alignment - /// [default = 32] <br> - /// <em>s:[align]:[prefalign]</em> specifies short integer - /// alignment [default = 16] <br> - /// <em>b:[align]:[prefalign]</em> specifies byte data type - /// alignment [default = 8] <br> - /// <em>B:[align]:[prefalign]</em> specifies boolean data type - /// alignment [default = 8] <br> - /// <em>A:[prefalign]</em> 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; } - /// Target pointer alignment - unsigned char getPointerABIAlignment() const { return PointerABIAlignment; } - /// Target pointer size - unsigned char getPointerSize() const { return PointerMemSize; } - /// Target pointer size, in bits - 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; - } - /// Return target's alignment for double preceision floats on stack - unsigned char getDoublePrefAlignment() const { - return DoublePrefAlignment; - } - /// Return target's alignment for stack-based pointers - unsigned char getPointerPrefAlignment() const { - return PointerPrefAlignment; - } - /// Return target's alignment for stack-based structures - unsigned char getAggMinPrefAlignment() const { - return AggMinPrefAlignment; - } - /// 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 PointerABIAlign; } + /// Return target's alignment for stack-based pointers + unsigned char getPointerPrefAlignment() const { return PointerPrefAlign; } + /// Target pointer size + unsigned char getPointerSize() const { return PointerMemSize; } + /// Target pointer size, in bits + unsigned char getPointerSizeInBits() const { return 8*PointerMemSize; } /// getTypeSize - Return the number of bytes necessary to hold the specified /// type. - /// uint64_t getTypeSize(const Type *Ty) const; /// getTypeSizeInBits - Return the number of bytes necessary to hold the @@ -225,11 +171,11 @@ public: /// getTypeAlignmentABI - Return the minimum ABI-required alignment for the /// specified type. - unsigned char getTypeAlignmentABI(const Type *Ty) const; + unsigned char getABITypeAlignment(const Type *Ty) const; /// getTypeAlignmentPref - Return the preferred stack/global alignment for /// the specified type. - unsigned char getTypeAlignmentPref(const Type *Ty) const; + unsigned char getPrefTypeAlignment(const Type *Ty) const; /// getPreferredTypeAlignmentShift - Return the preferred alignment for the /// specified type, returned as log2 of the value (a shift amount). diff --git a/lib/CodeGen/ELFWriter.cpp b/lib/CodeGen/ELFWriter.cpp index 5fd3dcc1d6..ffc0a929f3 100644 --- a/lib/CodeGen/ELFWriter.cpp +++ b/lib/CodeGen/ELFWriter.cpp @@ -255,7 +255,7 @@ void ELFWriter::EmitGlobal(GlobalVariable *GV) { } const Type *GVType = (const Type*)GV->getType(); - unsigned Align = TM.getTargetData()->getTypeAlignmentPref(GVType); + unsigned Align = TM.getTargetData()->getPrefTypeAlignment(GVType); unsigned Size = TM.getTargetData()->getTypeSize(GVType); // If this global has a zero initializer, it is part of the .bss or common diff --git a/lib/CodeGen/MachOWriter.cpp b/lib/CodeGen/MachOWriter.cpp index fe9d997ec5..0b7dfab80f 100644 --- a/lib/CodeGen/MachOWriter.cpp +++ b/lib/CodeGen/MachOWriter.cpp @@ -147,7 +147,7 @@ void MachOCodeEmitter::startFunction(MachineFunction &MF) { // Align the output buffer to the appropriate alignment, power of 2. unsigned FnAlign = F->getAlignment(); - unsigned TDAlign = TD->getTypeAlignmentPref(F->getType()); + unsigned TDAlign = TD->getPrefTypeAlignment(F->getType()); unsigned Align = Log2_32(std::max(FnAlign, TDAlign)); assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); @@ -332,7 +332,7 @@ void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) { unsigned Size = TM.getTargetData()->getTypeSize(Ty); unsigned Align = GV->getAlignment(); if (Align == 0) - Align = TM.getTargetData()->getTypeAlignmentPref(Ty); + Align = TM.getTargetData()->getPrefTypeAlignment(Ty); MachOSym Sym(GV, Mang->getValueName(GV), Sec->Index, TM); diff --git a/lib/CodeGen/MachineFunction.cpp b/lib/CodeGen/MachineFunction.cpp index 0cba1567e1..5ae73ca40a 100644 --- a/lib/CodeGen/MachineFunction.cpp +++ b/lib/CodeGen/MachineFunction.cpp @@ -13,6 +13,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/DerivedTypes.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineInstr.h" #include "llvm/CodeGen/SSARegMap.h" @@ -123,7 +124,7 @@ MachineFunction::MachineFunction(const Function *F, const TargetData &TD = *TM.getTargetData(); bool IsPic = TM.getRelocationModel() == Reloc::PIC_; unsigned EntrySize = IsPic ? 4 : TD.getPointerSize(); - unsigned Alignment = IsPic ? TD.getIntABIAlignment() + unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty) : TD.getPointerABIAlignment(); JumpTableInfo = new MachineJumpTableInfo(EntrySize, Alignment); diff --git a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp index 7b148f1158..0bd40c2934 100644 --- a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp +++ b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp @@ -3056,7 +3056,7 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { // new ones, as reuse may inhibit scheduling. const Type *Ty = MVT::getTypeForValueType(ExtraVT); unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty); - unsigned Align = TLI.getTargetData()->getTypeAlignmentPref(Ty); + unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); MachineFunction &MF = DAG.getMachineFunction(); int SSFI = MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); @@ -3979,7 +3979,7 @@ SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) { MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); unsigned ByteSize = MVT::getSizeInBits(VT)/8; const Type *Ty = MVT::getTypeForValueType(VT); - unsigned StackAlign = (unsigned)TLI.getTargetData()->getTypeAlignmentPref(Ty); + unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty); int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign); return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); } @@ -4289,7 +4289,7 @@ SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, MachineFunction &MF = DAG.getMachineFunction(); const Type *F64Type = MVT::getTypeForValueType(MVT::f64); unsigned StackAlign = - (unsigned)TLI.getTargetData()->getTypeAlignmentPref(F64Type); + (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type); int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign); // get address of 8 byte buffer SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index e2bda6d4c4..e72d88c734 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -244,7 +244,7 @@ FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli, const Type *Ty = AI->getAllocatedType(); uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty); unsigned Align = - std::max((unsigned)TLI.getTargetData()->getTypeAlignmentPref(Ty), + std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), AI->getAlignment()); TySize *= CUI->getZExtValue(); // Get total allocated size. @@ -1733,7 +1733,7 @@ void SelectionDAGLowering::visitAlloca(AllocaInst &I) { const Type *Ty = I.getAllocatedType(); uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty); unsigned Align = - std::max((unsigned)TLI.getTargetData()->getTypeAlignmentPref(Ty), + std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), I.getAlignment()); SDOperand AllocSize = getValue(I.getArraySize()); @@ -2934,7 +2934,7 @@ TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { bool isInReg = FTy->paramHasAttr(j, FunctionType::InRegAttribute); bool isSRet = FTy->paramHasAttr(j, FunctionType::StructRetAttribute); unsigned OriginalAlignment = - getTargetData()->getTypeAlignmentABI(I->getType()); + getTargetData()->getABITypeAlignment(I->getType()); // Flags[31:27] -> OriginalAlignment // Flags[2] -> isSRet // Flags[1] -> isInReg @@ -3120,7 +3120,7 @@ TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy, bool isInReg = Args[i].isInReg; bool isSRet = Args[i].isSRet; unsigned OriginalAlignment = - getTargetData()->getTypeAlignmentABI(Args[i].Ty); + getTargetData()->getABITypeAlignment(Args[i].Ty); // Flags[31:27] -> OriginalAlignment // Flags[2] -> isSRet // Flags[1] -> isInReg diff --git a/lib/ExecutionEngine/JIT/JIT.cpp b/lib/ExecutionEngine/JIT/JIT.cpp index ca3dbb96a7..070947bc1d 100644 --- a/lib/ExecutionEngine/JIT/JIT.cpp +++ b/lib/ExecutionEngine/JIT/JIT.cpp @@ -338,7 +338,7 @@ void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) { // compilation. const Type *GlobalType = GV->getType()->getElementType(); size_t S = getTargetData()->getTypeSize(GlobalType); - size_t A = getTargetData()->getTypeAlignmentPref(GlobalType); + size_t A = getTargetData()->getPrefTypeAlignment(GlobalType); if (A <= 8) { Ptr = malloc(S); } else { diff --git a/lib/Target/ARM/ARMTargetMachine.cpp b/lib/Target/ARM/ARMTargetMachine.cpp index 19d917c31d..2fa7016085 100644 --- a/lib/Target/ARM/ARMTargetMachine.cpp +++ b/lib/Target/ARM/ARMTargetMachine.cpp @@ -37,12 +37,14 @@ ARMTargetMachine::ARMTargetMachine(const Module &M, const std::string &FS) DataLayout(Subtarget.isAPCS_ABI() ? // APCS ABI (Subtarget.isThumb() ? - std::string("e-p:32:32-d:32:32-l:32:32-s:16:32-b:8:32-B:8:32-A:32") : - std::string("e-p:32:32-d:32:32-l:32:32")) : + std::string("e-p:32:32-f64:32:32-i64:32:32-" + "i16:16:32-i8:8:32-i1:8:32-a:0:32") : + std::string("e-p:32:32-f64:32:32-i64:32:32")) : // AAPCS ABI (Subtarget.isThumb() ? - std::string("e-p:32:32-d:64:64-l:64:64-s:16:32-b:8:32-B:8:32-A:32") : - std::string("e-p:32:32-d:64:64-l:64:64"))), + std::string("e-p:32:32-f64:64:64-i64:64:64-" + "i16:16:32-i8:8:32-i1:8:32-a:0:32") : + std::string("e-p:32:32-f64:64:64-i64:64:64"))), InstrInfo(Subtarget), FrameInfo(Subtarget) {} diff --git a/lib/Target/PowerPC/PPCSubtarget.h b/lib/Target/PowerPC/PPCSubtarget.h index d8c04871f5..7bc2b803b5 100644 --- a/lib/Target/PowerPC/PPCSubtarget.h +++ b/lib/Target/PowerPC/PPCSubtarget.h @@ -104,8 +104,8 @@ public: /// getTargetDataString - Return the pointer size and type alignment /// properties of this subtarget. const char *getTargetDataString() const { - return isPPC64() ? "E-p:64:64-d:32:64-l:32:64" - : "E-p:32:32-d:32:64-l:32:64"; + return isPPC64() ? "E-p:64:64-f64:32:64-i64:32:64" + : "E-p:32:32-f64:32:64-i64:32:64"; } /// isPPC64 - Return true if we are generating code for 64-bit pointer mode. diff --git a/lib/Target/Sparc/SparcAsmPrinter.cpp b/lib/Target/Sparc/SparcAsmPrinter.cpp index d0d34fca38..1f823265d0 100644 --- a/lib/Target/Sparc/SparcAsmPrinter.cpp +++ b/lib/Target/Sparc/SparcAsmPrinter.cpp @@ -229,7 +229,7 @@ bool SparcAsmPrinter::doFinalization(Module &M) { std::string name = Mang->getValueName(I); Constant *C = I->getInitializer(); unsigned Size = TD->getTypeSize(C->getType()); - unsigned Align = TD->getTypeAlignmentPref(C->getType()); + unsigned Align = TD->getPrefTypeAlignment(C->getType()); if (C->isNullValue() && (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || diff --git a/lib/Target/TargetData.cpp b/lib/Target/TargetData.cpp index 38f0854f80..26a30a7809 100644 --- a/lib/Target/TargetData.cpp +++ b/lib/Target/TargetData.cpp @@ -36,12 +36,6 @@ namespace { RegisterPass<TargetData> X("targetdata", "Target Data Layout"); } -static inline void getTypeInfoABI(const Type *Ty, const TargetData *TD, - uint64_t &Size, unsigned char &Alignment); - -static inline void getTypeInfoPref(const Type *Ty, const TargetData *TD, - uint64_t &Size, unsigned char &Alignment); - //===----------------------------------------------------------------------===// // Support for StructLayout //===----------------------------------------------------------------------===// @@ -54,11 +48,10 @@ StructLayout::StructLayout(const StructType *ST, const TargetData &TD) { // Loop over each of the elements, placing them in memory... for (unsigned i = 0, e = NumElements; i != e; ++i) { const Type *Ty = ST->getElementType(i); - unsigned char A; unsigned TyAlign; uint64_t TySize; - getTypeInfoABI(Ty, &TD, TySize, A); - TyAlign = ST->isPacked() ? 1 : A; + TyAlign = (unsigned) TD.getABITypeAlignment(Ty); + TySize = (unsigned) TD.getTypeSize(Ty); // Add padding if necessary to make the data element aligned properly... if (StructSize % TyAlign != 0) @@ -96,38 +89,126 @@ unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const { } //===----------------------------------------------------------------------===// +// TargetAlignElem, TargetAlign support +//===----------------------------------------------------------------------===// + +TargetAlignElem +TargetAlignElem::get(AlignTypeEnum align_type, unsigned char abi_align, + unsigned char pref_align, short bit_width) +{ + TargetAlignElem retval; + retval.AlignType = align_type; + retval.ABIAlign = abi_align; + retval.PrefAlign = pref_align; + retval.TypeBitWidth = bit_width; + return retval; +} + +bool +TargetAlignElem::operator<(const TargetAlignElem &rhs) const +{ + return ((AlignType < rhs.AlignType) + || (AlignType == rhs.AlignType && TypeBitWidth < rhs.TypeBitWidth)); +} + +bool +TargetAlignElem::operator==(const TargetAlignElem &rhs) const +{ + return (AlignType == rhs.AlignType + && ABIAlign == rhs.ABIAlign + && PrefAlign == rhs.PrefAlign + && TypeBitWidth == rhs.TypeBitWidth); +} + +std::ostream & +TargetAlignElem::dump(std::ostream &os) const +{ + return os << AlignType + << TypeBitWidth + << ":" << (int) (ABIAlign * 8) + << ":" << (int) (PrefAlign * 8); +} + +std::ostream & +llvm::operator<<(std::ostream &os, const TargetAlignElem &elem) +{ + return elem.dump(os); +} + +const TargetAlignElem TargetData::InvalidAlignmentElem = + TargetAlignElem::get((AlignTypeEnum) -1, 0, 0, 0); + +//===----------------------------------------------------------------------===// // TargetData Class Implementation //===----------------------------------------------------------------------===// +/*! + A TargetDescription string consists of a sequence of hyphen-delimited + specifiers for target endianness, pointer size and alignments, and various + primitive type sizes and alignments. A typical string looks something like: + <br> + "E-p:32:32:32-i1:8:8-i8:8:8-i32:32:32-i64:32:64-f32:32:32-f64:32:64" + <br> + (note: this string is not fully specified and is only an example.) + \p + Alignments come in two flavors: ABI and preferred. ABI alignment (abi_align, + below) dictates how a type will be aligned within an aggregate and when used + as an argument. Preferred alignment (pref_align, below) determines a type's + alignment when emitted as a global. + \p + Specifier string details: + <br><br> + <i>[E|e]</i>: Endianness. "E" specifies a big-endian target data model, "e" + specifies a little-endian target data model. + <br><br> + <i>p:<size>:<abi_align>:<pref_align></i>: Pointer size, ABI and preferred + alignment. + <br><br> + <i><type><size>:<abi_align>:<pref_align></i>: Numeric type alignment. Type is + one of <i>i|f|v|a</i>, corresponding to integer, floating point, vector (aka + packed) or aggregate. Size indicates the size, e.g., 32 or 64 bits. + \p + The default string, fully specified is: + <br><br> + "E-p:64:64:64-a0:0:0-f32:32:32-f64:0:64" + "-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:0:64" + "-v64:64:64-v128:128:128" + <br><br> + Note that in the case of aggregates, 0 is the default ABI and preferred + alignment. This is a special case, where the aggregate's computed worst-case + alignment will be used. + */ void TargetData::init(const std::string &TargetDescription) { std::string temp = TargetDescription; LittleEndian = false; PointerMemSize = 8; - PointerABIAlignment = 8; - DoubleABIAlignment = 0; - FloatABIAlignment = 4; - LongABIAlignment = 0; - IntABIAlignment = 4; - ShortABIAlignment = 2; - ByteABIAlignment = 1; - BoolABIAlignment = 1; - BoolPrefAlignment = BoolABIAlignment; - BytePrefAlignment = ByteABIAlignment; - ShortPrefAlignment = ShortABIAlignment; - IntPrefAlignment = IntABIAlignment; - LongPrefAlignment = 8; - FloatPrefAlignment = FloatABIAlignment; - DoublePrefAlignment = 8; - PointerPrefAlignment = PointerABIAlignment; - AggMinPrefAlignment = 0; + PointerABIAlign = 8; + PointerPrefAlign = PointerABIAlign; + + // Default alignments + setAlignment(INTEGER_ALIGN, 1, 1, 1); // Bool + setAlignment(INTEGER_ALIGN, 1, 1, 8); // Byte + setAlignment(INTEGER_ALIGN, 2, 2, 16); // short + setAlignment(INTEGER_ALIGN, 4, 4, 32); // int + setAlignment(INTEGER_ALIGN, 0, 8, 64); // long + setAlignment(FLOAT_ALIGN, 4, 4, 32); // float + setAlignment(FLOAT_ALIGN, 0, 8, 64); // double + setAlignment(PACKED_ALIGN, 8, 8, 64); // v2i32 + setAlignment(PACKED_ALIGN, 16, 16, 128); // v16i8, v8i16, v4i32, ... + setAlignment(AGGREGATE_ALIGN, 0, 0, 0); // struct, union, class, ... while (!temp.empty()) { std::string token = getToken(temp, "-"); - char signal = getToken(token, ":")[0]; - - switch(signal) { + std::string arg0 = getToken(token, ":"); + const char *p = arg0.c_str(); + AlignTypeEnum align_type; + short size; + unsigned char abi_align; + unsigned char pref_align; + + switch(*p) { case 'E': LittleEndian = false; break; @@ -136,56 +217,26 @@ void TargetData::init(const std::string &TargetDescription) { break; case 'p': PointerMemSize = atoi(getToken(token,":").c_str()) / 8; - PointerABIAlignment = atoi(getToken(token,":").c_str()) / 8; - PointerPrefAlignment = atoi(getToken(token,":").c_str()) / 8; - if (PointerPrefAlignment == 0) - PointerPrefAlignment = PointerABIAlignment; - break; - case 'd': - DoubleABIAlignment = atoi(getToken(token,":").c_str()) / 8; - DoublePrefAlignment = atoi(getToken(token,":").c_str()) / 8; - if (DoublePrefAlignment == 0) - DoublePrefAlignment = DoubleABIAlignment; - break; - case 'f': - FloatABIAlignment = atoi(getToken(token, ":").c_str()) / 8; - FloatPrefAlignment = atoi(getToken(token,":").c_str()) / 8; - if (FloatPrefAlignment == 0) - FloatPrefAlignment = FloatABIAlignment; - break; - case 'l': - LongABIAlignment = atoi(getToken(token, ":").c_str()) / 8; - LongPrefAlignment = atoi(getToken(token,":").c_str()) / 8; - if (LongPrefAlignment == 0) - LongPrefAlignment = LongABIAlignment; + PointerABIAlign = atoi(getToken(token,":").c_str()) / 8; + PointerPrefAlign = atoi(getToken(token,":").c_str()) / 8; + if (PointerPrefAlign == 0) + PointerPrefAlign = PointerABIAlign; break; case 'i': - IntABIAlignment = atoi(getToken(token, ":").c_str()) / 8; - IntPrefAlignment = atoi(getToken(token,":").c_str()) / 8; - if (IntPrefAlignment == 0) - IntPrefAlignment = IntABIAlignment; - break; - case 's': - ShortABIAlignment = atoi(getToken(token, ":").c_str()) / 8; - ShortPrefAlignment = atoi(getToken(token,":").c_str()) / 8; - if (ShortPrefAlignment == 0) - ShortPrefAlignment = ShortABIAlignment; - break; - case 'b': - ByteABIAlignment = atoi(getToken(token, ":").c_str()) / 8; - BytePrefAlignment = atoi(getToken(token,":").c_str()) / 8; - if (BytePrefAlignment == 0) - BytePrefAlignment = ByteABIAlignment; - break; - case 'B': - BoolABIAlignment = atoi(getToken(token, ":").c_str()) / 8; - BoolPrefAlignment = atoi(getToken(token,":").c_str()) / 8; - if (BoolPrefAlignment == 0) - BoolPrefAlignment = BoolABIAlignment; - break; - case 'A': - AggMinPrefAlignment = atoi(getToken(token,":").c_str()) / 8; + case 'v': + case 'f': + case 'a': { + align_type = (*p == 'i' ? INTEGER_ALIGN : + (*p == 'f' ? FLOAT_ALIGN : + (*p == 'v' ? PACKED_ALIGN : AGGREGATE_ALIGN))); + size = (short) atoi(++p); + abi_align = atoi(getToken(token, ":").c_str()) / 8; + pref_align = atoi(getToken(token, ":").c_str()) / 8; + if (pref_align == 0) + pref_align = abi_align; + setAlignment(align_type, abi_align, pref_align, size); break; + } default: break; } @@ -193,16 +244,62 @@ void TargetData::init(const std::string &TargetDescription) { // Unless explicitly specified, the alignments for longs and doubles is // capped by pointer size. - if (LongABIAlignment == 0) - LongABIAlignment = LongPrefAlignment = PointerMemSize; - if (DoubleABIAlignment == 0) - DoubleABIAlignment = DoublePrefAlignment = PointerMemSize; + // FIXME: Is this still necessary? + const TargetAlignElem &long_align = getAlignment(INTEGER_ALIGN, 64); + if (long_align.ABIAlign == 0) + setAlignment(INTEGER_ALIGN, PointerMemSize, PointerMemSize, 64); + + const TargetAlignElem &double_align = getAlignment(FLOAT_ALIGN, 64); + if (double_align.ABIAlign == 0) + setAlignment(FLOAT_ALIGN, PointerMemSize, PointerMemSize, 64); } TargetData::TargetData(const Module *M) { init(M->getDataLayout()); } +void +TargetData::setAlignment(AlignTypeEnum align_type, unsigned char abi_align, + unsigned char pref_align, short bit_width) { + TargetAlignElem elt = TargetAlignElem::get(align_type, abi_align, + |