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