Home | History | Annotate | Download | only in IR
      1 //===--------- llvm/DataLayout.h - Data size & alignment info ---*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines layout properties related to datatype size/offset/alignment
     11 // information.  It uses lazy annotations to cache information about how
     12 // structure types are laid out and used.
     13 //
     14 // This structure should be created once, filled in if the defaults are not
     15 // correct and then passed around by const&.  None of the members functions
     16 // require modification to the object.
     17 //
     18 //===----------------------------------------------------------------------===//
     19 
     20 #ifndef LLVM_IR_DATALAYOUT_H
     21 #define LLVM_IR_DATALAYOUT_H
     22 
     23 #include "llvm/ADT/DenseMap.h"
     24 #include "llvm/ADT/SmallVector.h"
     25 #include "llvm/IR/DerivedTypes.h"
     26 #include "llvm/IR/Type.h"
     27 #include "llvm/Pass.h"
     28 #include "llvm/Support/DataTypes.h"
     29 
     30 // this needs to be outside of the namespace, to avoid conflict with llvm-c decl
     31 typedef struct LLVMOpaqueTargetData *LLVMTargetDataRef;
     32 
     33 namespace llvm {
     34 
     35 class Value;
     36 class Type;
     37 class IntegerType;
     38 class StructType;
     39 class StructLayout;
     40 class Triple;
     41 class GlobalVariable;
     42 class LLVMContext;
     43 template<typename T>
     44 class ArrayRef;
     45 
     46 /// Enum used to categorize the alignment types stored by LayoutAlignElem
     47 enum AlignTypeEnum {
     48   INVALID_ALIGN = 0,                 ///< An invalid alignment
     49   INTEGER_ALIGN = 'i',               ///< Integer type alignment
     50   VECTOR_ALIGN = 'v',                ///< Vector type alignment
     51   FLOAT_ALIGN = 'f',                 ///< Floating point type alignment
     52   AGGREGATE_ALIGN = 'a'              ///< Aggregate alignment
     53 };
     54 
     55 /// Layout alignment element.
     56 ///
     57 /// Stores the alignment data associated with a given alignment type (integer,
     58 /// vector, float) and type bit width.
     59 ///
     60 /// @note The unusual order of elements in the structure attempts to reduce
     61 /// padding and make the structure slightly more cache friendly.
     62 struct LayoutAlignElem {
     63   unsigned AlignType    : 8;  ///< Alignment type (AlignTypeEnum)
     64   unsigned TypeBitWidth : 24; ///< Type bit width
     65   unsigned ABIAlign     : 16; ///< ABI alignment for this type/bitw
     66   unsigned PrefAlign    : 16; ///< Pref. alignment for this type/bitw
     67 
     68   /// Initializer
     69   static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align,
     70                              unsigned pref_align, uint32_t bit_width);
     71   /// Equality predicate
     72   bool operator==(const LayoutAlignElem &rhs) const;
     73 };
     74 
     75 /// Layout pointer alignment element.
     76 ///
     77 /// Stores the alignment data associated with a given pointer and address space.
     78 ///
     79 /// @note The unusual order of elements in the structure attempts to reduce
     80 /// padding and make the structure slightly more cache friendly.
     81 struct PointerAlignElem {
     82   unsigned            ABIAlign;       ///< ABI alignment for this type/bitw
     83   unsigned            PrefAlign;      ///< Pref. alignment for this type/bitw
     84   uint32_t            TypeByteWidth;  ///< Type byte width
     85   uint32_t            AddressSpace;   ///< Address space for the pointer type
     86 
     87   /// Initializer
     88   static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign,
     89                              unsigned PrefAlign, uint32_t TypeByteWidth);
     90   /// Equality predicate
     91   bool operator==(const PointerAlignElem &rhs) const;
     92 };
     93 
     94 /// This class holds a parsed version of the target data layout string in a
     95 /// module and provides methods for querying it. The target data layout string
     96 /// is specified *by the target* - a frontend generating LLVM IR is required to
     97 /// generate the right target data for the target being codegen'd to.
     98 class DataLayout {
     99 private:
    100   bool          LittleEndian;          ///< Defaults to false
    101   unsigned      StackNaturalAlign;     ///< Stack natural alignment
    102 
    103   enum ManglingModeT {
    104     MM_None,
    105     MM_ELF,
    106     MM_MachO,
    107     MM_WINCOFF,
    108     MM_Mips
    109   };
    110   ManglingModeT ManglingMode;
    111 
    112   SmallVector<unsigned char, 8> LegalIntWidths; ///< Legal Integers.
    113 
    114   /// Alignments - Where the primitive type alignment data is stored.
    115   ///
    116   /// @sa reset().
    117   /// @note Could support multiple size pointer alignments, e.g., 32-bit
    118   /// pointers vs. 64-bit pointers by extending LayoutAlignment, but for now,
    119   /// we don't.
    120   SmallVector<LayoutAlignElem, 16> Alignments;
    121   typedef SmallVector<PointerAlignElem, 8> PointersTy;
    122   PointersTy Pointers;
    123 
    124   PointersTy::const_iterator
    125   findPointerLowerBound(uint32_t AddressSpace) const {
    126     return const_cast<DataLayout *>(this)->findPointerLowerBound(AddressSpace);
    127   }
    128 
    129   PointersTy::iterator findPointerLowerBound(uint32_t AddressSpace);
    130 
    131   /// InvalidAlignmentElem - This member is a signal that a requested alignment
    132   /// type and bit width were not found in the SmallVector.
    133   static const LayoutAlignElem InvalidAlignmentElem;
    134 
    135   /// InvalidPointerElem - This member is a signal that a requested pointer
    136   /// type and bit width were not found in the DenseSet.
    137   static const PointerAlignElem InvalidPointerElem;
    138 
    139   // The StructType -> StructLayout map.
    140   mutable void *LayoutMap;
    141 
    142   //! Set/initialize target alignments
    143   void setAlignment(AlignTypeEnum align_type, unsigned abi_align,
    144                     unsigned pref_align, uint32_t bit_width);
    145   unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
    146                             bool ABIAlign, Type *Ty) const;
    147 
    148   //! Set/initialize pointer alignments
    149   void setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
    150                            unsigned PrefAlign, uint32_t TypeByteWidth);
    151 
    152   //! Internal helper method that returns requested alignment for type.
    153   unsigned getAlignment(Type *Ty, bool abi_or_pref) const;
    154 
    155   /// Valid alignment predicate.
    156   ///
    157   /// Predicate that tests a LayoutAlignElem reference returned by get() against
    158   /// InvalidAlignmentElem.
    159   bool validAlignment(const LayoutAlignElem &align) const {
    160     return &align != &InvalidAlignmentElem;
    161   }
    162 
    163   /// Valid pointer predicate.
    164   ///
    165   /// Predicate that tests a PointerAlignElem reference returned by get() against
    166   /// InvalidPointerElem.
    167   bool validPointer(const PointerAlignElem &align) const {
    168     return &align != &InvalidPointerElem;
    169   }
    170 
    171   /// Parses a target data specification string. Assert if the string is
    172   /// malformed.
    173   void parseSpecifier(StringRef LayoutDescription);
    174 
    175   // Free all internal data structures.
    176   void clear();
    177 
    178 public:
    179   /// Constructs a DataLayout from a specification string. See reset().
    180   explicit DataLayout(StringRef LayoutDescription) : LayoutMap(nullptr) {
    181     reset(LayoutDescription);
    182   }
    183 
    184   /// Initialize target data from properties stored in the module.
    185   explicit DataLayout(const Module *M);
    186 
    187   DataLayout(const DataLayout &DL) : LayoutMap(nullptr) { *this = DL; }
    188 
    189   DataLayout &operator=(const DataLayout &DL) {
    190     clear();
    191     LittleEndian = DL.isLittleEndian();
    192     StackNaturalAlign = DL.StackNaturalAlign;
    193     ManglingMode = DL.ManglingMode;
    194     LegalIntWidths = DL.LegalIntWidths;
    195     Alignments = DL.Alignments;
    196     Pointers = DL.Pointers;
    197     return *this;
    198   }
    199 
    200   bool operator==(const DataLayout &Other) const;
    201   bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
    202 
    203   ~DataLayout();  // Not virtual, do not subclass this class
    204 
    205   /// Parse a data layout string (with fallback to default values).
    206   void reset(StringRef LayoutDescription);
    207 
    208   /// Layout endianness...
    209   bool isLittleEndian() const { return LittleEndian; }
    210   bool isBigEndian() const { return !LittleEndian; }
    211 
    212   /// getStringRepresentation - Return the string representation of the
    213   /// DataLayout.  This representation is in the same format accepted by the
    214   /// string constructor above.
    215   std::string getStringRepresentation() const;
    216 
    217   /// isLegalInteger - This function returns true if the specified type is
    218   /// known to be a native integer type supported by the CPU.  For example,
    219   /// i64 is not native on most 32-bit CPUs and i37 is not native on any known
    220   /// one.  This returns false if the integer width is not legal.
    221   ///
    222   /// The width is specified in bits.
    223   ///
    224   bool isLegalInteger(unsigned Width) const {
    225     for (unsigned LegalIntWidth : LegalIntWidths)
    226       if (LegalIntWidth == Width)
    227         return true;
    228     return false;
    229   }
    230 
    231   bool isIllegalInteger(unsigned Width) const {
    232     return !isLegalInteger(Width);
    233   }
    234 
    235   /// Returns true if the given alignment exceeds the natural stack alignment.
    236   bool exceedsNaturalStackAlignment(unsigned Align) const {
    237     return (StackNaturalAlign != 0) && (Align > StackNaturalAlign);
    238   }
    239 
    240   bool hasMicrosoftFastStdCallMangling() const {
    241     return ManglingMode == MM_WINCOFF;
    242   }
    243 
    244   bool hasLinkerPrivateGlobalPrefix() const {
    245     return ManglingMode == MM_MachO;
    246   }
    247 
    248   const char *getLinkerPrivateGlobalPrefix() const {
    249     if (ManglingMode == MM_MachO)
    250       return "l";
    251     return getPrivateGlobalPrefix();
    252   }
    253 
    254   char getGlobalPrefix() const {
    255     switch (ManglingMode) {
    256     case MM_None:
    257     case MM_ELF:
    258     case MM_Mips:
    259       return '\0';
    260     case MM_MachO:
    261     case MM_WINCOFF:
    262       return '_';
    263     }
    264     llvm_unreachable("invalid mangling mode");
    265   }
    266 
    267   const char *getPrivateGlobalPrefix() const {
    268     switch (ManglingMode) {
    269     case MM_None:
    270       return "";
    271     case MM_ELF:
    272       return ".L";
    273     case MM_Mips:
    274       return "$";
    275     case MM_MachO:
    276     case MM_WINCOFF:
    277       return "L";
    278     }
    279     llvm_unreachable("invalid mangling mode");
    280   }
    281 
    282   static const char *getManglingComponent(const Triple &T);
    283 
    284   /// fitsInLegalInteger - This function returns true if the specified type fits
    285   /// in a native integer type supported by the CPU.  For example, if the CPU
    286   /// only supports i32 as a native integer type, then i27 fits in a legal
    287   /// integer type but i45 does not.
    288   bool fitsInLegalInteger(unsigned Width) const {
    289     for (unsigned LegalIntWidth : LegalIntWidths)
    290       if (Width <= LegalIntWidth)
    291         return true;
    292     return false;
    293   }
    294 
    295   /// Layout pointer alignment
    296   /// FIXME: The defaults need to be removed once all of
    297   /// the backends/clients are updated.
    298   unsigned getPointerABIAlignment(unsigned AS = 0) const;
    299 
    300   /// Return target's alignment for stack-based pointers
    301   /// FIXME: The defaults need to be removed once all of
    302   /// the backends/clients are updated.
    303   unsigned getPointerPrefAlignment(unsigned AS = 0) const;
    304 
    305   /// Layout pointer size
    306   /// FIXME: The defaults need to be removed once all of
    307   /// the backends/clients are updated.
    308   unsigned getPointerSize(unsigned AS = 0) const;
    309 
    310   /// Layout pointer size, in bits
    311   /// FIXME: The defaults need to be removed once all of
    312   /// the backends/clients are updated.
    313   unsigned getPointerSizeInBits(unsigned AS = 0) const {
    314     return getPointerSize(AS) * 8;
    315   }
    316 
    317   /// Layout pointer size, in bits, based on the type.  If this function is
    318   /// called with a pointer type, then the type size of the pointer is returned.
    319   /// If this function is called with a vector of pointers, then the type size
    320   /// of the pointer is returned.  This should only be called with a pointer or
    321   /// vector of pointers.
    322   unsigned getPointerTypeSizeInBits(Type *) const;
    323 
    324   unsigned getPointerTypeSize(Type *Ty) const {
    325     return getPointerTypeSizeInBits(Ty) / 8;
    326   }
    327 
    328   /// Size examples:
    329   ///
    330   /// Type        SizeInBits  StoreSizeInBits  AllocSizeInBits[*]
    331   /// ----        ----------  ---------------  ---------------
    332   ///  i1            1           8                8
    333   ///  i8            8           8                8
    334   ///  i19          19          24               32
    335   ///  i32          32          32               32
    336   ///  i100        100         104              128
    337   ///  i128        128         128              128
    338   ///  Float        32          32               32
    339   ///  Double       64          64               64
    340   ///  X86_FP80     80          80               96
    341   ///
    342   /// [*] The alloc size depends on the alignment, and thus on the target.
    343   ///     These values are for x86-32 linux.
    344 
    345   /// getTypeSizeInBits - Return the number of bits necessary to hold the
    346   /// specified type.  For example, returns 36 for i36 and 80 for x86_fp80.
    347   /// The type passed must have a size (Type::isSized() must return true).
    348   uint64_t getTypeSizeInBits(Type *Ty) const;
    349 
    350   /// getTypeStoreSize - Return the maximum number of bytes that may be
    351   /// overwritten by storing the specified type.  For example, returns 5
    352   /// for i36 and 10 for x86_fp80.
    353   uint64_t getTypeStoreSize(Type *Ty) const {
    354     return (getTypeSizeInBits(Ty)+7)/8;
    355   }
    356 
    357   /// getTypeStoreSizeInBits - Return the maximum number of bits that may be
    358   /// overwritten by storing the specified type; always a multiple of 8.  For
    359   /// example, returns 40 for i36 and 80 for x86_fp80.
    360   uint64_t getTypeStoreSizeInBits(Type *Ty) const {
    361     return 8*getTypeStoreSize(Ty);
    362   }
    363 
    364   /// getTypeAllocSize - Return the offset in bytes between successive objects
    365   /// of the specified type, including alignment padding.  This is the amount
    366   /// that alloca reserves for this type.  For example, returns 12 or 16 for
    367   /// x86_fp80, depending on alignment.
    368   uint64_t getTypeAllocSize(Type *Ty) const {
    369     // Round up to the next alignment boundary.
    370     return RoundUpAlignment(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
    371   }
    372 
    373   /// getTypeAllocSizeInBits - Return the offset in bits between successive
    374   /// objects of the specified type, including alignment padding; always a
    375   /// multiple of 8.  This is the amount that alloca reserves for this type.
    376   /// For example, returns 96 or 128 for x86_fp80, depending on alignment.
    377   uint64_t getTypeAllocSizeInBits(Type *Ty) const {
    378     return 8*getTypeAllocSize(Ty);
    379   }
    380 
    381   /// getABITypeAlignment - Return the minimum ABI-required alignment for the
    382   /// specified type.
    383   unsigned getABITypeAlignment(Type *Ty) const;
    384 
    385   /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
    386   /// an integer type of the specified bitwidth.
    387   unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const;
    388 
    389   /// getPrefTypeAlignment - Return the preferred stack/global alignment for
    390   /// the specified type.  This is always at least as good as the ABI alignment.
    391   unsigned getPrefTypeAlignment(Type *Ty) const;
    392 
    393   /// getPreferredTypeAlignmentShift - Return the preferred alignment for the
    394   /// specified type, returned as log2 of the value (a shift amount).
    395   unsigned getPreferredTypeAlignmentShift(Type *Ty) const;
    396 
    397   /// getIntPtrType - Return an integer type with size at least as big as that
    398   /// of a pointer in the given address space.
    399   IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
    400 
    401   /// getIntPtrType - Return an integer (vector of integer) type with size at
    402   /// least as big as that of a pointer of the given pointer (vector of pointer)
    403   /// type.
    404   Type *getIntPtrType(Type *) const;
    405 
    406   /// getSmallestLegalIntType - Return the smallest integer type with size at
    407   /// least as big as Width bits.
    408   Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const;
    409 
    410   /// getLargestLegalIntType - Return the largest legal integer type, or null if
    411   /// none are set.
    412   Type *getLargestLegalIntType(LLVMContext &C) const {
    413     unsigned LargestSize = getLargestLegalIntTypeSize();
    414     return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize);
    415   }
    416 
    417   /// getLargestLegalIntTypeSize - Return the size of largest legal integer
    418   /// type size, or 0 if none are set.
    419   unsigned getLargestLegalIntTypeSize() const;
    420 
    421   /// getIndexedOffset - return the offset from the beginning of the type for
    422   /// the specified indices.  This is used to implement getelementptr.
    423   uint64_t getIndexedOffset(Type *Ty, ArrayRef<Value *> Indices) const;
    424 
    425   /// getStructLayout - Return a StructLayout object, indicating the alignment
    426   /// of the struct, its size, and the offsets of its fields.  Note that this
    427   /// information is lazily cached.
    428   const StructLayout *getStructLayout(StructType *Ty) const;
    429 
    430   /// getPreferredAlignment - Return the preferred alignment of the specified
    431   /// global.  This includes an explicitly requested alignment (if the global
    432   /// has one).
    433   unsigned getPreferredAlignment(const GlobalVariable *GV) const;
    434 
    435   /// getPreferredAlignmentLog - Return the preferred alignment of the
    436   /// specified global, returned in log form.  This includes an explicitly
    437   /// requested alignment (if the global has one).
    438   unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
    439 
    440   /// RoundUpAlignment - Round the specified value up to the next alignment
    441   /// boundary specified by Alignment.  For example, 7 rounded up to an
    442   /// alignment boundary of 4 is 8.  8 rounded up to the alignment boundary of 4
    443   /// is 8 because it is already aligned.
    444   template <typename UIntTy>
    445   static UIntTy RoundUpAlignment(UIntTy Val, unsigned Alignment) {
    446     assert((Alignment & (Alignment-1)) == 0 && "Alignment must be power of 2!");
    447     return (Val + (Alignment-1)) & ~UIntTy(Alignment-1);
    448   }
    449 };
    450 
    451 inline DataLayout *unwrap(LLVMTargetDataRef P) {
    452    return reinterpret_cast<DataLayout*>(P);
    453 }
    454 
    455 inline LLVMTargetDataRef wrap(const DataLayout *P) {
    456    return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
    457 }
    458 
    459 class DataLayoutPass : public ImmutablePass {
    460   DataLayout DL;
    461 
    462 public:
    463   /// This has to exist, because this is a pass, but it should never be used.
    464   DataLayoutPass();
    465   ~DataLayoutPass();
    466 
    467   const DataLayout &getDataLayout() const { return DL; }
    468 
    469   // For use with the C API. C++ code should always use the constructor that
    470   // takes a module.
    471   explicit DataLayoutPass(const DataLayout &DL);
    472 
    473   explicit DataLayoutPass(const Module *M);
    474 
    475   static char ID; // Pass identification, replacement for typeid
    476 };
    477 
    478 /// StructLayout - used to lazily calculate structure layout information for a
    479 /// target machine, based on the DataLayout structure.
    480 ///
    481 class StructLayout {
    482   uint64_t StructSize;
    483   unsigned StructAlignment;
    484   unsigned NumElements;
    485   uint64_t MemberOffsets[1];  // variable sized array!
    486 public:
    487 
    488   uint64_t getSizeInBytes() const {
    489     return StructSize;
    490   }
    491 
    492   uint64_t getSizeInBits() const {
    493     return 8*StructSize;
    494   }
    495 
    496   unsigned getAlignment() const {
    497     return StructAlignment;
    498   }
    499 
    500   /// getElementContainingOffset - Given a valid byte offset into the structure,
    501   /// return the structure index that contains it.
    502   ///
    503   unsigned getElementContainingOffset(uint64_t Offset) const;
    504 
    505   uint64_t getElementOffset(unsigned Idx) const {
    506     assert(Idx < NumElements && "Invalid element idx!");
    507     return MemberOffsets[Idx];
    508   }
    509 
    510   uint64_t getElementOffsetInBits(unsigned Idx) const {
    511     return getElementOffset(Idx)*8;
    512   }
    513 
    514 private:
    515   friend class DataLayout;   // Only DataLayout can create this class
    516   StructLayout(StructType *ST, const DataLayout &DL);
    517 };
    518 
    519 
    520 // The implementation of this method is provided inline as it is particularly
    521 // well suited to constant folding when called on a specific Type subclass.
    522 inline uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const {
    523   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
    524   switch (Ty->getTypeID()) {
    525   case Type::LabelTyID:
    526     return getPointerSizeInBits(0);
    527   case Type::PointerTyID:
    528     return getPointerSizeInBits(Ty->getPointerAddressSpace());
    529   case Type::ArrayTyID: {
    530     ArrayType *ATy = cast<ArrayType>(Ty);
    531     return ATy->getNumElements() *
    532            getTypeAllocSizeInBits(ATy->getElementType());
    533   }
    534   case Type::StructTyID:
    535     // Get the layout annotation... which is lazily created on demand.
    536     return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
    537   case Type::IntegerTyID:
    538     return Ty->getIntegerBitWidth();
    539   case Type::HalfTyID:
    540     return 16;
    541   case Type::FloatTyID:
    542     return 32;
    543   case Type::DoubleTyID:
    544   case Type::X86_MMXTyID:
    545     return 64;
    546   case Type::PPC_FP128TyID:
    547   case Type::FP128TyID:
    548     return 128;
    549     // In memory objects this is always aligned to a higher boundary, but
    550   // only 80 bits contain information.
    551   case Type::X86_FP80TyID:
    552     return 80;
    553   case Type::VectorTyID: {
    554     VectorType *VTy = cast<VectorType>(Ty);
    555     return VTy->getNumElements() * getTypeSizeInBits(VTy->getElementType());
    556   }
    557   default:
    558     llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
    559   }
    560 }
    561 
    562 } // End llvm namespace
    563 
    564 #endif
    565