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