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   void init(const Module *M);
00188 
00189   DataLayout(const DataLayout &DL) : LayoutMap(nullptr) { *this = DL; }
00190 
00191   DataLayout &operator=(const DataLayout &DL) {
00192     clear();
00193     LittleEndian = DL.isLittleEndian();
00194     StackNaturalAlign = DL.StackNaturalAlign;
00195     ManglingMode = DL.ManglingMode;
00196     LegalIntWidths = DL.LegalIntWidths;
00197     Alignments = DL.Alignments;
00198     Pointers = DL.Pointers;
00199     return *this;
00200   }
00201 
00202   bool operator==(const DataLayout &Other) const;
00203   bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
00204 
00205   ~DataLayout();  // Not virtual, do not subclass this class
00206 
00207   /// Parse a data layout string (with fallback to default values).
00208   void reset(StringRef LayoutDescription);
00209 
00210   /// Layout endianness...
00211   bool isLittleEndian() const { return LittleEndian; }
00212   bool isBigEndian() const { return !LittleEndian; }
00213 
00214   /// getStringRepresentation - Return the string representation of the
00215   /// DataLayout.  This representation is in the same format accepted by the
00216   /// string constructor above.
00217   std::string getStringRepresentation() const;
00218 
00219   /// isLegalInteger - This function returns true if the specified type is
00220   /// known to be a native integer type supported by the CPU.  For example,
00221   /// i64 is not native on most 32-bit CPUs and i37 is not native on any known
00222   /// one.  This returns false if the integer width is not legal.
00223   ///
00224   /// The width is specified in bits.
00225   ///
00226   bool isLegalInteger(unsigned Width) const {
00227     for (unsigned LegalIntWidth : LegalIntWidths)
00228       if (LegalIntWidth == Width)
00229         return true;
00230     return false;
00231   }
00232 
00233   bool isIllegalInteger(unsigned Width) const {
00234     return !isLegalInteger(Width);
00235   }
00236 
00237   /// Returns true if the given alignment exceeds the natural stack alignment.
00238   bool exceedsNaturalStackAlignment(unsigned Align) const {
00239     return (StackNaturalAlign != 0) && (Align > StackNaturalAlign);
00240   }
00241 
00242   bool hasMicrosoftFastStdCallMangling() const {
00243     return ManglingMode == MM_WINCOFF;
00244   }
00245 
00246   bool hasLinkerPrivateGlobalPrefix() const {
00247     return ManglingMode == MM_MachO;
00248   }
00249 
00250   const char *getLinkerPrivateGlobalPrefix() const {
00251     if (ManglingMode == MM_MachO)
00252       return "l";
00253     return getPrivateGlobalPrefix();
00254   }
00255 
00256   char getGlobalPrefix() const {
00257     switch (ManglingMode) {
00258     case MM_None:
00259     case MM_ELF:
00260     case MM_Mips:
00261       return '\0';
00262     case MM_MachO:
00263     case MM_WINCOFF:
00264       return '_';
00265     }
00266     llvm_unreachable("invalid mangling mode");
00267   }
00268 
00269   const char *getPrivateGlobalPrefix() const {
00270     switch (ManglingMode) {
00271     case MM_None:
00272       return "";
00273     case MM_ELF:
00274       return ".L";
00275     case MM_Mips:
00276       return "$";
00277     case MM_MachO:
00278     case MM_WINCOFF:
00279       return "L";
00280     }
00281     llvm_unreachable("invalid mangling mode");
00282   }
00283 
00284   static const char *getManglingComponent(const Triple &T);
00285 
00286   /// fitsInLegalInteger - This function returns true if the specified type fits
00287   /// in a native integer type supported by the CPU.  For example, if the CPU
00288   /// only supports i32 as a native integer type, then i27 fits in a legal
00289   /// integer type but i45 does not.
00290   bool fitsInLegalInteger(unsigned Width) const {
00291     for (unsigned LegalIntWidth : LegalIntWidths)
00292       if (Width <= LegalIntWidth)
00293         return true;
00294     return false;
00295   }
00296 
00297   /// Layout pointer alignment
00298   /// FIXME: The defaults need to be removed once all of
00299   /// the backends/clients are updated.
00300   unsigned getPointerABIAlignment(unsigned AS = 0) const;
00301 
00302   /// Return target's alignment for stack-based pointers
00303   /// FIXME: The defaults need to be removed once all of
00304   /// the backends/clients are updated.
00305   unsigned getPointerPrefAlignment(unsigned AS = 0) const;
00306 
00307   /// Layout pointer size
00308   /// FIXME: The defaults need to be removed once all of
00309   /// the backends/clients are updated.
00310   unsigned getPointerSize(unsigned AS = 0) const;
00311 
00312   /// Layout pointer size, in bits
00313   /// FIXME: The defaults need to be removed once all of
00314   /// the backends/clients are updated.
00315   unsigned getPointerSizeInBits(unsigned AS = 0) const {
00316     return getPointerSize(AS) * 8;
00317   }
00318 
00319   /// Layout pointer size, in bits, based on the type.  If this function is
00320   /// called with a pointer type, then the type size of the pointer is returned.
00321   /// If this function is called with a vector of pointers, then the type size
00322   /// of the pointer is returned.  This should only be called with a pointer or
00323   /// vector of pointers.
00324   unsigned getPointerTypeSizeInBits(Type *) const;
00325 
00326   unsigned getPointerTypeSize(Type *Ty) const {
00327     return getPointerTypeSizeInBits(Ty) / 8;
00328   }
00329 
00330   /// Size examples:
00331   ///
00332   /// Type        SizeInBits  StoreSizeInBits  AllocSizeInBits[*]
00333   /// ----        ----------  ---------------  ---------------
00334   ///  i1            1           8                8
00335   ///  i8            8           8                8
00336   ///  i19          19          24               32
00337   ///  i32          32          32               32
00338   ///  i100        100         104              128
00339   ///  i128        128         128              128
00340   ///  Float        32          32               32
00341   ///  Double       64          64               64
00342   ///  X86_FP80     80          80               96
00343   ///
00344   /// [*] The alloc size depends on the alignment, and thus on the target.
00345   ///     These values are for x86-32 linux.
00346 
00347   /// getTypeSizeInBits - Return the number of bits necessary to hold the
00348   /// specified type.  For example, returns 36 for i36 and 80 for x86_fp80.
00349   /// The type passed must have a size (Type::isSized() must return true).
00350   uint64_t getTypeSizeInBits(Type *Ty) const;
00351 
00352   /// getTypeStoreSize - Return the maximum number of bytes that may be
00353   /// overwritten by storing the specified type.  For example, returns 5
00354   /// for i36 and 10 for x86_fp80.
00355   uint64_t getTypeStoreSize(Type *Ty) const {
00356     return (getTypeSizeInBits(Ty)+7)/8;
00357   }
00358 
00359   /// getTypeStoreSizeInBits - Return the maximum number of bits that may be
00360   /// overwritten by storing the specified type; always a multiple of 8.  For
00361   /// example, returns 40 for i36 and 80 for x86_fp80.
00362   uint64_t getTypeStoreSizeInBits(Type *Ty) const {
00363     return 8*getTypeStoreSize(Ty);
00364   }
00365 
00366   /// getTypeAllocSize - Return the offset in bytes between successive objects
00367   /// of the specified type, including alignment padding.  This is the amount
00368   /// that alloca reserves for this type.  For example, returns 12 or 16 for
00369   /// x86_fp80, depending on alignment.
00370   uint64_t getTypeAllocSize(Type *Ty) const {
00371     // Round up to the next alignment boundary.
00372     return RoundUpAlignment(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
00373   }
00374 
00375   /// getTypeAllocSizeInBits - Return the offset in bits between successive
00376   /// objects of the specified type, including alignment padding; always a
00377   /// multiple of 8.  This is the amount that alloca reserves for this type.
00378   /// For example, returns 96 or 128 for x86_fp80, depending on alignment.
00379   uint64_t getTypeAllocSizeInBits(Type *Ty) const {
00380     return 8*getTypeAllocSize(Ty);
00381   }
00382 
00383   /// getABITypeAlignment - Return the minimum ABI-required alignment for the
00384   /// specified type.
00385   unsigned getABITypeAlignment(Type *Ty) const;
00386 
00387   /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
00388   /// an integer type of the specified bitwidth.
00389   unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const;
00390 
00391   /// getPrefTypeAlignment - Return the preferred stack/global alignment for
00392   /// the specified type.  This is always at least as good as the ABI alignment.
00393   unsigned getPrefTypeAlignment(Type *Ty) const;
00394 
00395   /// getPreferredTypeAlignmentShift - Return the preferred alignment for the
00396   /// specified type, returned as log2 of the value (a shift amount).
00397   unsigned getPreferredTypeAlignmentShift(Type *Ty) const;
00398 
00399   /// getIntPtrType - Return an integer type with size at least as big as that
00400   /// of a pointer in the given address space.
00401   IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
00402 
00403   /// getIntPtrType - Return an integer (vector of integer) type with size at
00404   /// least as big as that of a pointer of the given pointer (vector of pointer)
00405   /// type.
00406   Type *getIntPtrType(Type *) const;
00407 
00408   /// getSmallestLegalIntType - Return the smallest integer type with size at
00409   /// least as big as Width bits.
00410   Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const;
00411 
00412   /// getLargestLegalIntType - Return the largest legal integer type, or null if
00413   /// none are set.
00414   Type *getLargestLegalIntType(LLVMContext &C) const {
00415     unsigned LargestSize = getLargestLegalIntTypeSize();
00416     return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize);
00417   }
00418 
00419   /// getLargestLegalIntTypeSize - Return the size of largest legal integer
00420   /// type size, or 0 if none are set.
00421   unsigned getLargestLegalIntTypeSize() const;
00422 
00423   /// getIndexedOffset - return the offset from the beginning of the type for
00424   /// the specified indices.  This is used to implement getelementptr.
00425   uint64_t getIndexedOffset(Type *Ty, ArrayRef<Value *> Indices) const;
00426 
00427   /// getStructLayout - Return a StructLayout object, indicating the alignment
00428   /// of the struct, its size, and the offsets of its fields.  Note that this
00429   /// information is lazily cached.
00430   const StructLayout *getStructLayout(StructType *Ty) const;
00431 
00432   /// getPreferredAlignment - Return the preferred alignment of the specified
00433   /// global.  This includes an explicitly requested alignment (if the global
00434   /// has one).
00435   unsigned getPreferredAlignment(const GlobalVariable *GV) const;
00436 
00437   /// getPreferredAlignmentLog - Return the preferred alignment of the
00438   /// specified global, returned in log form.  This includes an explicitly
00439   /// requested alignment (if the global has one).
00440   unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
00441 
00442   /// RoundUpAlignment - Round the specified value up to the next alignment
00443   /// boundary specified by Alignment.  For example, 7 rounded up to an
00444   /// alignment boundary of 4 is 8.  8 rounded up to the alignment boundary of 4
00445   /// is 8 because it is already aligned.
00446   template <typename UIntTy>
00447   static UIntTy RoundUpAlignment(UIntTy Val, unsigned Alignment) {
00448     assert((Alignment & (Alignment-1)) == 0 && "Alignment must be power of 2!");
00449     return (Val + (Alignment-1)) & ~UIntTy(Alignment-1);
00450   }
00451 };
00452 
00453 inline DataLayout *unwrap(LLVMTargetDataRef P) {
00454    return reinterpret_cast<DataLayout*>(P);
00455 }
00456 
00457 inline LLVMTargetDataRef wrap(const DataLayout *P) {
00458    return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
00459 }
00460 
00461 class DataLayoutPass : public ImmutablePass {
00462   DataLayout DL;
00463 
00464 public:
00465   /// This has to exist, because this is a pass, but it should never be used.
00466   DataLayoutPass();
00467   ~DataLayoutPass();
00468 
00469   const DataLayout &getDataLayout() const { return DL; }
00470 
00471   static char ID; // Pass identification, replacement for typeid
00472 
00473   bool doFinalization(Module &M) override;
00474   bool doInitialization(Module &M) override;
00475 };
00476 
00477 /// StructLayout - used to lazily calculate structure layout information for a
00478 /// target machine, based on the DataLayout structure.
00479 ///
00480 class StructLayout {
00481   uint64_t StructSize;
00482   unsigned StructAlignment;
00483   unsigned NumElements;
00484   uint64_t MemberOffsets[1];  // variable sized array!
00485 public:
00486 
00487   uint64_t getSizeInBytes() const {
00488     return StructSize;
00489   }
00490 
00491   uint64_t getSizeInBits() const {
00492     return 8*StructSize;
00493   }
00494 
00495   unsigned getAlignment() const {
00496     return StructAlignment;
00497   }
00498 
00499   /// getElementContainingOffset - Given a valid byte offset into the structure,
00500   /// return the structure index that contains it.
00501   ///
00502   unsigned getElementContainingOffset(uint64_t Offset) const;
00503 
00504   uint64_t getElementOffset(unsigned Idx) const {
00505     assert(Idx < NumElements && "Invalid element idx!");
00506     return MemberOffsets[Idx];
00507   }
00508 
00509   uint64_t getElementOffsetInBits(unsigned Idx) const {
00510     return getElementOffset(Idx)*8;
00511   }
00512 
00513 private:
00514   friend class DataLayout;   // Only DataLayout can create this class
00515   StructLayout(StructType *ST, const DataLayout &DL);
00516 };
00517 
00518 
00519 // The implementation of this method is provided inline as it is particularly
00520 // well suited to constant folding when called on a specific Type subclass.
00521 inline uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const {
00522   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
00523   switch (Ty->getTypeID()) {
00524   case Type::LabelTyID:
00525     return getPointerSizeInBits(0);
00526   case Type::PointerTyID:
00527     return getPointerSizeInBits(Ty->getPointerAddressSpace());
00528   case Type::ArrayTyID: {
00529     ArrayType *ATy = cast<ArrayType>(Ty);
00530     return ATy->getNumElements() *
00531            getTypeAllocSizeInBits(ATy->getElementType());
00532   }
00533   case Type::StructTyID:
00534     // Get the layout annotation... which is lazily created on demand.
00535     return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
00536   case Type::IntegerTyID:
00537     return Ty->getIntegerBitWidth();
00538   case Type::HalfTyID:
00539     return 16;
00540   case Type::FloatTyID:
00541     return 32;
00542   case Type::DoubleTyID:
00543   case Type::X86_MMXTyID:
00544     return 64;
00545   case Type::PPC_FP128TyID:
00546   case Type::FP128TyID:
00547     return 128;
00548     // In memory objects this is always aligned to a higher boundary, but
00549   // only 80 bits contain information.
00550   case Type::X86_FP80TyID:
00551     return 80;
00552   case Type::VectorTyID: {
00553     VectorType *VTy = cast<VectorType>(Ty);
00554     return VTy->getNumElements() * getTypeSizeInBits(VTy->getElementType());
00555   }
00556   default:
00557     llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
00558   }
00559 }
00560 
00561 } // End llvm namespace
00562 
00563 #endif