LLVM API Documentation

DataLayout.h
Go to the documentation of this file.
00001 //===--------- llvm/DataLayout.h - Data size & alignment info ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines layout properties related to datatype size/offset/alignment
00011 // information.  It uses lazy annotations to cache information about how
00012 // structure types are laid out and used.
00013 //
00014 // This structure should be created once, filled in if the defaults are not
00015 // correct and then passed around by const&.  None of the members functions
00016 // require modification to the object.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_IR_DATALAYOUT_H
00021 #define LLVM_IR_DATALAYOUT_H
00022 
00023 #include "llvm/ADT/DenseMap.h"
00024 #include "llvm/ADT/SmallVector.h"
00025 #include "llvm/IR/DerivedTypes.h"
00026 #include "llvm/IR/Type.h"
00027 #include "llvm/Pass.h"
00028 #include "llvm/Support/DataTypes.h"
00029 
00030 namespace llvm {
00031 
00032 class Value;
00033 class Type;
00034 class IntegerType;
00035 class StructType;
00036 class StructLayout;
00037 class Triple;
00038 class GlobalVariable;
00039 class LLVMContext;
00040 template<typename T>
00041 class ArrayRef;
00042 
00043 /// Enum used to categorize the alignment types stored by LayoutAlignElem
00044 enum AlignTypeEnum {
00045   INVALID_ALIGN = 0,                 ///< An invalid alignment
00046   INTEGER_ALIGN = 'i',               ///< Integer type alignment
00047   VECTOR_ALIGN = 'v',                ///< Vector type alignment
00048   FLOAT_ALIGN = 'f',                 ///< Floating point type alignment
00049   AGGREGATE_ALIGN = 'a'              ///< Aggregate alignment
00050 };
00051 
00052 /// Layout alignment element.
00053 ///
00054 /// Stores the alignment data associated with a given alignment type (integer,
00055 /// vector, float) and type bit width.
00056 ///
00057 /// @note The unusual order of elements in the structure attempts to reduce
00058 /// padding and make the structure slightly more cache friendly.
00059 struct LayoutAlignElem {
00060   unsigned AlignType    : 8;  ///< Alignment type (AlignTypeEnum)
00061   unsigned TypeBitWidth : 24; ///< Type bit width
00062   unsigned ABIAlign     : 16; ///< ABI alignment for this type/bitw
00063   unsigned PrefAlign    : 16; ///< Pref. alignment for this type/bitw
00064 
00065   /// Initializer
00066   static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align,
00067                              unsigned pref_align, uint32_t bit_width);
00068   /// Equality predicate
00069   bool operator==(const LayoutAlignElem &rhs) const;
00070 };
00071 
00072 /// Layout pointer alignment element.
00073 ///
00074 /// Stores the alignment data associated with a given pointer and address space.
00075 ///
00076 /// @note The unusual order of elements in the structure attempts to reduce
00077 /// padding and make the structure slightly more cache friendly.
00078 struct PointerAlignElem {
00079   unsigned            ABIAlign;       ///< ABI alignment for this type/bitw
00080   unsigned            PrefAlign;      ///< Pref. alignment for this type/bitw
00081   uint32_t            TypeByteWidth;  ///< Type byte width
00082   uint32_t            AddressSpace;   ///< Address space for the pointer type
00083 
00084   /// Initializer
00085   static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign,
00086                              unsigned PrefAlign, uint32_t TypeByteWidth);
00087   /// Equality predicate
00088   bool operator==(const PointerAlignElem &rhs) const;
00089 };
00090 
00091 /// This class holds a parsed version of the target data layout string in a
00092 /// module and provides methods for querying it. The target data layout string
00093 /// is specified *by the target* - a frontend generating LLVM IR is required to
00094 /// generate the right target data for the target being codegen'd to.
00095 class DataLayout {
00096 private:
00097   bool          LittleEndian;          ///< Defaults to false
00098   unsigned      StackNaturalAlign;     ///< Stack natural alignment
00099 
00100   enum ManglingModeT {
00101     MM_None,
00102     MM_ELF,
00103     MM_MachO,
00104     MM_WINCOFF,
00105     MM_Mips
00106   };
00107   ManglingModeT ManglingMode;
00108 
00109   SmallVector<unsigned char, 8> LegalIntWidths; ///< Legal Integers.
00110 
00111   /// Alignments - Where the primitive type alignment data is stored.
00112   ///
00113   /// @sa reset().
00114   /// @note Could support multiple size pointer alignments, e.g., 32-bit
00115   /// pointers vs. 64-bit pointers by extending LayoutAlignment, but for now,
00116   /// we don't.
00117   SmallVector<LayoutAlignElem, 16> Alignments;
00118   typedef SmallVector<PointerAlignElem, 8> PointersTy;
00119   PointersTy Pointers;
00120 
00121   PointersTy::const_iterator
00122   findPointerLowerBound(uint32_t AddressSpace) const {
00123     return const_cast<DataLayout *>(this)->findPointerLowerBound(AddressSpace);
00124   }
00125 
00126   PointersTy::iterator findPointerLowerBound(uint32_t AddressSpace);
00127 
00128   /// InvalidAlignmentElem - This member is a signal that a requested alignment
00129   /// type and bit width were not found in the SmallVector.
00130   static const LayoutAlignElem InvalidAlignmentElem;
00131 
00132   /// InvalidPointerElem - This member is a signal that a requested pointer
00133   /// type and bit width were not found in the DenseSet.
00134   static const PointerAlignElem InvalidPointerElem;
00135 
00136   // The StructType -> StructLayout map.
00137   mutable void *LayoutMap;
00138 
00139   //! Set/initialize target alignments
00140   void setAlignment(AlignTypeEnum align_type, unsigned abi_align,
00141                     unsigned pref_align, uint32_t bit_width);
00142   unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
00143                             bool ABIAlign, Type *Ty) const;
00144 
00145   //! Set/initialize pointer alignments
00146   void setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
00147                            unsigned PrefAlign, uint32_t TypeByteWidth);
00148 
00149   //! Internal helper method that returns requested alignment for type.
00150   unsigned getAlignment(Type *Ty, bool abi_or_pref) const;
00151 
00152   /// Valid alignment predicate.
00153   ///
00154   /// Predicate that tests a LayoutAlignElem reference returned by get() against
00155   /// InvalidAlignmentElem.
00156   bool validAlignment(const LayoutAlignElem &align) const {
00157     return &align != &InvalidAlignmentElem;
00158   }
00159 
00160   /// Valid pointer predicate.
00161   ///
00162   /// Predicate that tests a PointerAlignElem reference returned by get() against
00163   /// InvalidPointerElem.
00164   bool validPointer(const PointerAlignElem &align) const {
00165     return &align != &InvalidPointerElem;
00166   }
00167 
00168   /// Parses a target data specification string. Assert if the string is
00169   /// malformed.
00170   void parseSpecifier(StringRef LayoutDescription);
00171 
00172   // Free all internal data structures.
00173   void clear();
00174 
00175 public:
00176   /// Constructs a DataLayout from a specification string. See reset().
00177   explicit DataLayout(StringRef LayoutDescription) : LayoutMap(nullptr) {
00178     reset(LayoutDescription);
00179   }
00180 
00181   /// Initialize target data from properties stored in the module.
00182   explicit DataLayout(const Module *M);
00183 
00184   DataLayout(const DataLayout &DL) : LayoutMap(nullptr) { *this = DL; }
00185 
00186   DataLayout &operator=(const DataLayout &DL) {
00187     clear();
00188     LittleEndian = DL.isLittleEndian();
00189     StackNaturalAlign = DL.StackNaturalAlign;
00190     ManglingMode = DL.ManglingMode;
00191     LegalIntWidths = DL.LegalIntWidths;
00192     Alignments = DL.Alignments;
00193     Pointers = DL.Pointers;
00194     return *this;
00195   }
00196 
00197   bool operator==(const DataLayout &Other) const;
00198   bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
00199 
00200   ~DataLayout();  // Not virtual, do not subclass this class
00201 
00202   /// Parse a data layout string (with fallback to default values).
00203   void reset(StringRef LayoutDescription);
00204 
00205   /// Layout endianness...
00206   bool isLittleEndian() const { return LittleEndian; }
00207   bool isBigEndian() const { return !LittleEndian; }
00208 
00209   /// getStringRepresentation - Return the string representation of the
00210   /// DataLayout.  This representation is in the same format accepted by the
00211   /// string constructor above.
00212   std::string getStringRepresentation() const;
00213 
00214   /// isLegalInteger - This function returns true if the specified type is
00215   /// known to be a native integer type supported by the CPU.  For example,
00216   /// i64 is not native on most 32-bit CPUs and i37 is not native on any known
00217   /// one.  This returns false if the integer width is not legal.
00218   ///
00219   /// The width is specified in bits.
00220   ///
00221   bool isLegalInteger(unsigned Width) const {
00222     for (unsigned LegalIntWidth : LegalIntWidths)
00223       if (LegalIntWidth == Width)
00224         return true;
00225     return false;
00226   }
00227 
00228   bool isIllegalInteger(unsigned Width) const {
00229     return !isLegalInteger(Width);
00230   }
00231 
00232   /// Returns true if the given alignment exceeds the natural stack alignment.
00233   bool exceedsNaturalStackAlignment(unsigned Align) const {
00234     return (StackNaturalAlign != 0) && (Align > StackNaturalAlign);
00235   }
00236 
00237   bool hasMicrosoftFastStdCallMangling() const {
00238     return ManglingMode == MM_WINCOFF;
00239   }
00240 
00241   bool hasLinkerPrivateGlobalPrefix() const {
00242     return ManglingMode == MM_MachO;
00243   }
00244 
00245   const char *getLinkerPrivateGlobalPrefix() const {
00246     if (ManglingMode == MM_MachO)
00247       return "l";
00248     return getPrivateGlobalPrefix();
00249   }
00250 
00251   char getGlobalPrefix() const {
00252     switch (ManglingMode) {
00253     case MM_None:
00254     case MM_ELF:
00255     case MM_Mips:
00256       return '\0';
00257     case MM_MachO:
00258     case MM_WINCOFF:
00259       return '_';
00260     }
00261     llvm_unreachable("invalid mangling mode");
00262   }
00263 
00264   const char *getPrivateGlobalPrefix() const {
00265     switch (ManglingMode) {
00266     case MM_None:
00267       return "";
00268     case MM_ELF:
00269       return ".L";
00270     case MM_Mips:
00271       return "$";
00272     case MM_MachO:
00273     case MM_WINCOFF:
00274       return "L";
00275     }
00276     llvm_unreachable("invalid mangling mode");
00277   }
00278 
00279   static const char *getManglingComponent(const Triple &T);
00280 
00281   /// fitsInLegalInteger - This function returns true if the specified type fits
00282   /// in a native integer type supported by the CPU.  For example, if the CPU
00283   /// only supports i32 as a native integer type, then i27 fits in a legal
00284   /// integer type but i45 does not.
00285   bool fitsInLegalInteger(unsigned Width) const {
00286     for (unsigned LegalIntWidth : LegalIntWidths)
00287       if (Width <= LegalIntWidth)
00288         return true;
00289     return false;
00290   }
00291 
00292   /// Layout pointer alignment
00293   /// FIXME: The defaults need to be removed once all of
00294   /// the backends/clients are updated.
00295   unsigned getPointerABIAlignment(unsigned AS = 0) const;
00296 
00297   /// Return target's alignment for stack-based pointers
00298   /// FIXME: The defaults need to be removed once all of
00299   /// the backends/clients are updated.
00300   unsigned getPointerPrefAlignment(unsigned AS = 0) const;
00301 
00302   /// Layout pointer size
00303   /// FIXME: The defaults need to be removed once all of
00304   /// the backends/clients are updated.
00305   unsigned getPointerSize(unsigned AS = 0) const;
00306 
00307   /// Layout pointer size, in bits
00308   /// FIXME: The defaults need to be removed once all of
00309   /// the backends/clients are updated.
00310   unsigned getPointerSizeInBits(unsigned AS = 0) const {
00311     return getPointerSize(AS) * 8;
00312   }
00313 
00314   /// Layout pointer size, in bits, based on the type.  If this function is
00315   /// called with a pointer type, then the type size of the pointer is returned.
00316   /// If this function is called with a vector of pointers, then the type size
00317   /// of the pointer is returned.  This should only be called with a pointer or
00318   /// vector of pointers.
00319   unsigned getPointerTypeSizeInBits(Type *) const;
00320 
00321   unsigned getPointerTypeSize(Type *Ty) const {
00322     return getPointerTypeSizeInBits(Ty) / 8;
00323   }
00324 
00325   /// Size examples:
00326   ///
00327   /// Type        SizeInBits  StoreSizeInBits  AllocSizeInBits[*]
00328   /// ----        ----------  ---------------  ---------------
00329   ///  i1            1           8                8
00330   ///  i8            8           8                8
00331   ///  i19          19          24               32
00332   ///  i32          32          32               32
00333   ///  i100        100         104              128
00334   ///  i128        128         128              128
00335   ///  Float        32          32               32
00336   ///  Double       64          64               64
00337   ///  X86_FP80     80          80               96
00338   ///
00339   /// [*] The alloc size depends on the alignment, and thus on the target.
00340   ///     These values are for x86-32 linux.
00341 
00342   /// getTypeSizeInBits - Return the number of bits necessary to hold the
00343   /// specified type.  For example, returns 36 for i36 and 80 for x86_fp80.
00344   /// The type passed must have a size (Type::isSized() must return true).
00345   uint64_t getTypeSizeInBits(Type *Ty) const;
00346 
00347   /// getTypeStoreSize - Return the maximum number of bytes that may be
00348   /// overwritten by storing the specified type.  For example, returns 5
00349   /// for i36 and 10 for x86_fp80.
00350   uint64_t getTypeStoreSize(Type *Ty) const {
00351     return (getTypeSizeInBits(Ty)+7)/8;
00352   }
00353 
00354   /// getTypeStoreSizeInBits - Return the maximum number of bits that may be
00355   /// overwritten by storing the specified type; always a multiple of 8.  For
00356   /// example, returns 40 for i36 and 80 for x86_fp80.
00357   uint64_t getTypeStoreSizeInBits(Type *Ty) const {
00358     return 8*getTypeStoreSize(Ty);
00359   }
00360 
00361   /// getTypeAllocSize - Return the offset in bytes between successive objects
00362   /// of the specified type, including alignment padding.  This is the amount
00363   /// that alloca reserves for this type.  For example, returns 12 or 16 for
00364   /// x86_fp80, depending on alignment.
00365   uint64_t getTypeAllocSize(Type *Ty) const {
00366     // Round up to the next alignment boundary.
00367     return RoundUpAlignment(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
00368   }
00369 
00370   /// getTypeAllocSizeInBits - Return the offset in bits between successive
00371   /// objects of the specified type, including alignment padding; always a
00372   /// multiple of 8.  This is the amount that alloca reserves for this type.
00373   /// For example, returns 96 or 128 for x86_fp80, depending on alignment.
00374   uint64_t getTypeAllocSizeInBits(Type *Ty) const {
00375     return 8*getTypeAllocSize(Ty);
00376   }
00377 
00378   /// getABITypeAlignment - Return the minimum ABI-required alignment for the
00379   /// specified type.
00380   unsigned getABITypeAlignment(Type *Ty) const;
00381 
00382   /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
00383   /// an integer type of the specified bitwidth.
00384   unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const;
00385 
00386   /// getPrefTypeAlignment - Return the preferred stack/global alignment for
00387   /// the specified type.  This is always at least as good as the ABI alignment.
00388   unsigned getPrefTypeAlignment(Type *Ty) const;
00389 
00390   /// getPreferredTypeAlignmentShift - Return the preferred alignment for the
00391   /// specified type, returned as log2 of the value (a shift amount).
00392   unsigned getPreferredTypeAlignmentShift(Type *Ty) const;
00393 
00394   /// getIntPtrType - Return an integer type with size at least as big as that
00395   /// of a pointer in the given address space.
00396   IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
00397 
00398   /// getIntPtrType - Return an integer (vector of integer) type with size at
00399   /// least as big as that of a pointer of the given pointer (vector of pointer)
00400   /// type.
00401   Type *getIntPtrType(Type *) const;
00402 
00403   /// getSmallestLegalIntType - Return the smallest integer type with size at
00404   /// least as big as Width bits.
00405   Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const;
00406 
00407   /// getLargestLegalIntType - Return the largest legal integer type, or null if
00408   /// none are set.
00409   Type *getLargestLegalIntType(LLVMContext &C) const {
00410     unsigned LargestSize = getLargestLegalIntTypeSize();
00411     return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize);
00412   }
00413 
00414   /// getLargestLegalIntType - Return the size of largest legal integer type
00415   /// size, or 0 if none are set.
00416   unsigned getLargestLegalIntTypeSize() const;
00417 
00418   /// getIndexedOffset - return the offset from the beginning of the type for
00419   /// the specified indices.  This is used to implement getelementptr.
00420   uint64_t getIndexedOffset(Type *Ty, ArrayRef<Value *> Indices) const;
00421 
00422   /// getStructLayout - Return a StructLayout object, indicating the alignment
00423   /// of the struct, its size, and the offsets of its fields.  Note that this
00424   /// information is lazily cached.
00425   const StructLayout *getStructLayout(StructType *Ty) const;
00426 
00427   /// getPreferredAlignment - Return the preferred alignment of the specified
00428   /// global.  This includes an explicitly requested alignment (if the global
00429   /// has one).
00430   unsigned getPreferredAlignment(const GlobalVariable *GV) const;
00431 
00432   /// getPreferredAlignmentLog - Return the preferred alignment of the
00433   /// specified global, returned in log form.  This includes an explicitly
00434   /// requested alignment (if the global has one).
00435   unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
00436 
00437   /// RoundUpAlignment - Round the specified value up to the next alignment
00438   /// boundary specified by Alignment.  For example, 7 rounded up to an
00439   /// alignment boundary of 4 is 8.  8 rounded up to the alignment boundary of 4
00440   /// is 8 because it is already aligned.
00441   template <typename UIntTy>
00442   static UIntTy RoundUpAlignment(UIntTy Val, unsigned Alignment) {
00443     assert((Alignment & (Alignment-1)) == 0 && "Alignment must be power of 2!");
00444     return (Val + (Alignment-1)) & ~UIntTy(Alignment-1);
00445   }
00446 };
00447 
00448 class DataLayoutPass : public ImmutablePass {
00449   DataLayout DL;
00450 
00451 public:
00452   /// This has to exist, because this is a pass, but it should never be used.
00453   DataLayoutPass();
00454   ~DataLayoutPass();
00455 
00456   const DataLayout &getDataLayout() const { return DL; }
00457 
00458   // For use with the C API. C++ code should always use the constructor that
00459   // takes a module.
00460   explicit DataLayoutPass(const DataLayout &DL);
00461 
00462   explicit DataLayoutPass(const Module *M);
00463 
00464   static char ID; // Pass identification, replacement for typeid
00465 };
00466 
00467 /// StructLayout - used to lazily calculate structure layout information for a
00468 /// target machine, based on the DataLayout structure.
00469 ///
00470 class StructLayout {
00471   uint64_t StructSize;
00472   unsigned StructAlignment;
00473   unsigned NumElements;
00474   uint64_t MemberOffsets[1];  // variable sized array!
00475 public:
00476 
00477   uint64_t getSizeInBytes() const {
00478     return StructSize;
00479   }
00480 
00481   uint64_t getSizeInBits() const {
00482     return 8*StructSize;
00483   }
00484 
00485   unsigned getAlignment() const {
00486     return StructAlignment;
00487   }
00488 
00489   /// getElementContainingOffset - Given a valid byte offset into the structure,
00490   /// return the structure index that contains it.
00491   ///
00492   unsigned getElementContainingOffset(uint64_t Offset) const;
00493 
00494   uint64_t getElementOffset(unsigned Idx) const {
00495     assert(Idx < NumElements && "Invalid element idx!");
00496     return MemberOffsets[Idx];
00497   }
00498 
00499   uint64_t getElementOffsetInBits(unsigned Idx) const {
00500     return getElementOffset(Idx)*8;
00501   }
00502 
00503 private:
00504   friend class DataLayout;   // Only DataLayout can create this class
00505   StructLayout(StructType *ST, const DataLayout &DL);
00506 };
00507 
00508 
00509 // The implementation of this method is provided inline as it is particularly
00510 // well suited to constant folding when called on a specific Type subclass.
00511 inline uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const {
00512   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
00513   switch (Ty->getTypeID()) {
00514   case Type::LabelTyID:
00515     return getPointerSizeInBits(0);
00516   case Type::PointerTyID:
00517     return getPointerSizeInBits(Ty->getPointerAddressSpace());
00518   case Type::ArrayTyID: {
00519     ArrayType *ATy = cast<ArrayType>(Ty);
00520     return ATy->getNumElements() *
00521            getTypeAllocSizeInBits(ATy->getElementType());
00522   }
00523   case Type::StructTyID:
00524     // Get the layout annotation... which is lazily created on demand.
00525     return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
00526   case Type::IntegerTyID:
00527     return Ty->getIntegerBitWidth();
00528   case Type::HalfTyID:
00529     return 16;
00530   case Type::FloatTyID:
00531     return 32;
00532   case Type::DoubleTyID:
00533   case Type::X86_MMXTyID:
00534     return 64;
00535   case Type::PPC_FP128TyID:
00536   case Type::FP128TyID:
00537     return 128;
00538     // In memory objects this is always aligned to a higher boundary, but
00539   // only 80 bits contain information.
00540   case Type::X86_FP80TyID:
00541     return 80;
00542   case Type::VectorTyID: {
00543     VectorType *VTy = cast<VectorType>(Ty);
00544     return VTy->getNumElements() * getTypeSizeInBits(VTy->getElementType());
00545   }
00546   default:
00547     llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
00548   }
00549 }
00550 
00551 } // End llvm namespace
00552 
00553 #endif