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