DataLayout.h revision 309124
1//===--------- llvm/DataLayout.h - Data size & alignment info ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines layout properties related to datatype size/offset/alignment
11// information.  It uses lazy annotations to cache information about how
12// structure types are laid out and used.
13//
14// This structure should be created once, filled in if the defaults are not
15// correct and then passed around by const&.  None of the members functions
16// require modification to the object.
17//
18//===----------------------------------------------------------------------===//
19
20#ifndef LLVM_IR_DATALAYOUT_H
21#define LLVM_IR_DATALAYOUT_H
22
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/IR/DerivedTypes.h"
25#include "llvm/IR/Type.h"
26#include "llvm/Pass.h"
27#include "llvm/Support/DataTypes.h"
28
29// This needs to be outside of the namespace, to avoid conflict with llvm-c
30// decl.
31typedef struct LLVMOpaqueTargetData *LLVMTargetDataRef;
32
33namespace llvm {
34
35class Value;
36class StructType;
37class StructLayout;
38class Triple;
39class GlobalVariable;
40class LLVMContext;
41template<typename T>
42class ArrayRef;
43
44/// Enum used to categorize the alignment types stored by LayoutAlignElem
45enum AlignTypeEnum {
46  INVALID_ALIGN = 0,
47  INTEGER_ALIGN = 'i',
48  VECTOR_ALIGN = 'v',
49  FLOAT_ALIGN = 'f',
50  AGGREGATE_ALIGN = 'a'
51};
52
53// FIXME: Currently the DataLayout string carries a "preferred alignment"
54// for types. As the DataLayout is module/global, this should likely be
55// sunk down to an FTTI element that is queried rather than a global
56// preference.
57
58/// \brief Layout alignment element.
59///
60/// Stores the alignment data associated with a given alignment type (integer,
61/// vector, float) and type bit width.
62///
63/// \note The unusual order of elements in the structure attempts to reduce
64/// padding and make the structure slightly more cache friendly.
65struct LayoutAlignElem {
66  /// \brief Alignment type from \c AlignTypeEnum
67  unsigned AlignType : 8;
68  unsigned TypeBitWidth : 24;
69  unsigned ABIAlign : 16;
70  unsigned PrefAlign : 16;
71
72  static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align,
73                             unsigned pref_align, uint32_t bit_width);
74  bool operator==(const LayoutAlignElem &rhs) const;
75};
76
77/// \brief Layout pointer alignment element.
78///
79/// Stores the alignment data associated with a given pointer and address space.
80///
81/// \note The unusual order of elements in the structure attempts to reduce
82/// padding and make the structure slightly more cache friendly.
83struct PointerAlignElem {
84  unsigned ABIAlign;
85  unsigned PrefAlign;
86  uint32_t TypeByteWidth;
87  uint32_t AddressSpace;
88
89  /// Initializer
90  static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign,
91                              unsigned PrefAlign, uint32_t TypeByteWidth);
92  bool operator==(const PointerAlignElem &rhs) const;
93};
94
95/// \brief A parsed version of the target data layout string in and methods for
96/// querying it.
97///
98/// The target data layout string is specified *by the target* - a frontend
99/// generating LLVM IR is required to generate the right target data for the
100/// target being codegen'd to.
101class DataLayout {
102private:
103  /// Defaults to false.
104  bool BigEndian;
105
106  unsigned StackNaturalAlign;
107
108  enum ManglingModeT {
109    MM_None,
110    MM_ELF,
111    MM_MachO,
112    MM_WinCOFF,
113    MM_WinCOFFX86,
114    MM_Mips
115  };
116  ManglingModeT ManglingMode;
117
118  SmallVector<unsigned char, 8> LegalIntWidths;
119
120  /// \brief Primitive type alignment data.
121  SmallVector<LayoutAlignElem, 16> Alignments;
122
123  /// \brief The string representation used to create this DataLayout
124  std::string StringRepresentation;
125
126  typedef SmallVector<PointerAlignElem, 8> PointersTy;
127  PointersTy Pointers;
128
129  PointersTy::const_iterator
130  findPointerLowerBound(uint32_t AddressSpace) const {
131    return const_cast<DataLayout *>(this)->findPointerLowerBound(AddressSpace);
132  }
133
134  PointersTy::iterator findPointerLowerBound(uint32_t AddressSpace);
135
136  /// This member is a signal that a requested alignment type and bit width were
137  /// not found in the SmallVector.
138  static const LayoutAlignElem InvalidAlignmentElem;
139
140  /// This member is a signal that a requested pointer type and bit width were
141  /// not found in the DenseSet.
142  static const PointerAlignElem InvalidPointerElem;
143
144  // The StructType -> StructLayout map.
145  mutable void *LayoutMap;
146
147  void setAlignment(AlignTypeEnum align_type, unsigned abi_align,
148                    unsigned pref_align, uint32_t bit_width);
149  unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
150                            bool ABIAlign, Type *Ty) const;
151  void setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
152                           unsigned PrefAlign, uint32_t TypeByteWidth);
153
154  /// Internal helper method that returns requested alignment for type.
155  unsigned getAlignment(Type *Ty, bool abi_or_pref) const;
156
157  /// \brief Valid alignment predicate.
158  ///
159  /// Predicate that tests a LayoutAlignElem reference returned by get() against
160  /// InvalidAlignmentElem.
161  bool validAlignment(const LayoutAlignElem &align) const {
162    return &align != &InvalidAlignmentElem;
163  }
164
165  /// \brief Valid pointer predicate.
166  ///
167  /// Predicate that tests a PointerAlignElem reference returned by get()
168  /// against \c InvalidPointerElem.
169  bool validPointer(const PointerAlignElem &align) const {
170    return &align != &InvalidPointerElem;
171  }
172
173  /// Parses a target data specification string. Assert if the string is
174  /// malformed.
175  void parseSpecifier(StringRef LayoutDescription);
176
177  // Free all internal data structures.
178  void clear();
179
180public:
181  /// Constructs a DataLayout from a specification string. See reset().
182  explicit DataLayout(StringRef LayoutDescription) : LayoutMap(nullptr) {
183    reset(LayoutDescription);
184  }
185
186  /// Initialize target data from properties stored in the module.
187  explicit DataLayout(const Module *M);
188
189  void init(const Module *M);
190
191  DataLayout(const DataLayout &DL) : LayoutMap(nullptr) { *this = DL; }
192
193  DataLayout &operator=(const DataLayout &DL) {
194    clear();
195    StringRepresentation = DL.StringRepresentation;
196    BigEndian = DL.isBigEndian();
197    StackNaturalAlign = DL.StackNaturalAlign;
198    ManglingMode = DL.ManglingMode;
199    LegalIntWidths = DL.LegalIntWidths;
200    Alignments = DL.Alignments;
201    Pointers = DL.Pointers;
202    return *this;
203  }
204
205  bool operator==(const DataLayout &Other) const;
206  bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
207
208  ~DataLayout(); // Not virtual, do not subclass this class
209
210  /// Parse a data layout string (with fallback to default values).
211  void reset(StringRef LayoutDescription);
212
213  /// Layout endianness...
214  bool isLittleEndian() const { return !BigEndian; }
215  bool isBigEndian() const { return BigEndian; }
216
217  /// \brief Returns the string representation of the DataLayout.
218  ///
219  /// This representation is in the same format accepted by the string
220  /// constructor above. This should not be used to compare two DataLayout as
221  /// different string can represent the same layout.
222  const std::string &getStringRepresentation() const {
223    return StringRepresentation;
224  }
225
226  /// \brief Test if the DataLayout was constructed from an empty string.
227  bool isDefault() const { return StringRepresentation.empty(); }
228
229  /// \brief Returns true if the specified type is known to be a native integer
230  /// type supported by the CPU.
231  ///
232  /// For example, i64 is not native on most 32-bit CPUs and i37 is not native
233  /// on any known one. This returns false if the integer width is not legal.
234  ///
235  /// The width is specified in bits.
236  bool isLegalInteger(uint64_t Width) const {
237    for (unsigned LegalIntWidth : LegalIntWidths)
238      if (LegalIntWidth == Width)
239        return true;
240    return false;
241  }
242
243  bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); }
244
245  /// Returns true if the given alignment exceeds the natural stack alignment.
246  bool exceedsNaturalStackAlignment(unsigned Align) const {
247    return (StackNaturalAlign != 0) && (Align > StackNaturalAlign);
248  }
249
250  unsigned getStackAlignment() const { return StackNaturalAlign; }
251
252  bool hasMicrosoftFastStdCallMangling() const {
253    return ManglingMode == MM_WinCOFFX86;
254  }
255
256  bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; }
257
258  const char *getLinkerPrivateGlobalPrefix() const {
259    if (ManglingMode == MM_MachO)
260      return "l";
261    return "";
262  }
263
264  char getGlobalPrefix() const {
265    switch (ManglingMode) {
266    case MM_None:
267    case MM_ELF:
268    case MM_Mips:
269    case MM_WinCOFF:
270      return '\0';
271    case MM_MachO:
272    case MM_WinCOFFX86:
273      return '_';
274    }
275    llvm_unreachable("invalid mangling mode");
276  }
277
278  const char *getPrivateGlobalPrefix() const {
279    switch (ManglingMode) {
280    case MM_None:
281      return "";
282    case MM_ELF:
283      return ".L";
284    case MM_Mips:
285      return "$";
286    case MM_MachO:
287    case MM_WinCOFF:
288    case MM_WinCOFFX86:
289      return "L";
290    }
291    llvm_unreachable("invalid mangling mode");
292  }
293
294  static const char *getManglingComponent(const Triple &T);
295
296  /// \brief Returns true if the specified type fits in a native integer type
297  /// supported by the CPU.
298  ///
299  /// For example, if the CPU only supports i32 as a native integer type, then
300  /// i27 fits in a legal integer type but i45 does not.
301  bool fitsInLegalInteger(unsigned Width) const {
302    for (unsigned LegalIntWidth : LegalIntWidths)
303      if (Width <= LegalIntWidth)
304        return true;
305    return false;
306  }
307
308  /// Layout pointer alignment
309  /// FIXME: The defaults need to be removed once all of
310  /// the backends/clients are updated.
311  unsigned getPointerABIAlignment(unsigned AS = 0) const;
312
313  /// Return target's alignment for stack-based pointers
314  /// FIXME: The defaults need to be removed once all of
315  /// the backends/clients are updated.
316  unsigned getPointerPrefAlignment(unsigned AS = 0) const;
317
318  /// Layout pointer size
319  /// FIXME: The defaults need to be removed once all of
320  /// the backends/clients are updated.
321  unsigned getPointerSize(unsigned AS = 0) const;
322
323  /// Layout pointer size, in bits
324  /// FIXME: The defaults need to be removed once all of
325  /// the backends/clients are updated.
326  unsigned getPointerSizeInBits(unsigned AS = 0) const {
327    return getPointerSize(AS) * 8;
328  }
329
330  /// Layout pointer size, in bits, based on the type.  If this function is
331  /// called with a pointer type, then the type size of the pointer is returned.
332  /// If this function is called with a vector of pointers, then the type size
333  /// of the pointer is returned.  This should only be called with a pointer or
334  /// vector of pointers.
335  unsigned getPointerTypeSizeInBits(Type *) const;
336
337  unsigned getPointerTypeSize(Type *Ty) const {
338    return getPointerTypeSizeInBits(Ty) / 8;
339  }
340
341  /// Size examples:
342  ///
343  /// Type        SizeInBits  StoreSizeInBits  AllocSizeInBits[*]
344  /// ----        ----------  ---------------  ---------------
345  ///  i1            1           8                8
346  ///  i8            8           8                8
347  ///  i19          19          24               32
348  ///  i32          32          32               32
349  ///  i100        100         104              128
350  ///  i128        128         128              128
351  ///  Float        32          32               32
352  ///  Double       64          64               64
353  ///  X86_FP80     80          80               96
354  ///
355  /// [*] The alloc size depends on the alignment, and thus on the target.
356  ///     These values are for x86-32 linux.
357
358  /// \brief Returns the number of bits necessary to hold the specified type.
359  ///
360  /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must
361  /// have a size (Type::isSized() must return true).
362  uint64_t getTypeSizeInBits(Type *Ty) const;
363
364  /// \brief Returns the maximum number of bytes that may be overwritten by
365  /// storing the specified type.
366  ///
367  /// For example, returns 5 for i36 and 10 for x86_fp80.
368  uint64_t getTypeStoreSize(Type *Ty) const {
369    return (getTypeSizeInBits(Ty) + 7) / 8;
370  }
371
372  /// \brief Returns the maximum number of bits that may be overwritten by
373  /// storing the specified type; always a multiple of 8.
374  ///
375  /// For example, returns 40 for i36 and 80 for x86_fp80.
376  uint64_t getTypeStoreSizeInBits(Type *Ty) const {
377    return 8 * getTypeStoreSize(Ty);
378  }
379
380  /// \brief Returns the offset in bytes between successive objects of the
381  /// specified type, including alignment padding.
382  ///
383  /// This is the amount that alloca reserves for this type. For example,
384  /// returns 12 or 16 for x86_fp80, depending on alignment.
385  uint64_t getTypeAllocSize(Type *Ty) const {
386    // Round up to the next alignment boundary.
387    return alignTo(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
388  }
389
390  /// \brief Returns the offset in bits between successive objects of the
391  /// specified type, including alignment padding; always a multiple of 8.
392  ///
393  /// This is the amount that alloca reserves for this type. For example,
394  /// returns 96 or 128 for x86_fp80, depending on alignment.
395  uint64_t getTypeAllocSizeInBits(Type *Ty) const {
396    return 8 * getTypeAllocSize(Ty);
397  }
398
399  /// \brief Returns the minimum ABI-required alignment for the specified type.
400  unsigned getABITypeAlignment(Type *Ty) const;
401
402  /// \brief Returns the minimum ABI-required alignment for an integer type of
403  /// the specified bitwidth.
404  unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const;
405
406  /// \brief Returns the preferred stack/global alignment for the specified
407  /// type.
408  ///
409  /// This is always at least as good as the ABI alignment.
410  unsigned getPrefTypeAlignment(Type *Ty) const;
411
412  /// \brief Returns the preferred alignment for the specified type, returned as
413  /// log2 of the value (a shift amount).
414  unsigned getPreferredTypeAlignmentShift(Type *Ty) const;
415
416  /// \brief Returns an integer type with size at least as big as that of a
417  /// pointer in the given address space.
418  IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
419
420  /// \brief Returns an integer (vector of integer) type with size at least as
421  /// big as that of a pointer of the given pointer (vector of pointer) type.
422  Type *getIntPtrType(Type *) const;
423
424  /// \brief Returns the smallest integer type with size at least as big as
425  /// Width bits.
426  Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const;
427
428  /// \brief Returns the largest legal integer type, or null if none are set.
429  Type *getLargestLegalIntType(LLVMContext &C) const {
430    unsigned LargestSize = getLargestLegalIntTypeSizeInBits();
431    return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize);
432  }
433
434  /// \brief Returns the size of largest legal integer type size, or 0 if none
435  /// are set.
436  unsigned getLargestLegalIntTypeSizeInBits() const;
437
438  /// \brief Returns the offset from the beginning of the type for the specified
439  /// indices.
440  ///
441  /// Note that this takes the element type, not the pointer type.
442  /// This is used to implement getelementptr.
443  int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef<Value *> Indices) const;
444
445  /// \brief Returns a StructLayout object, indicating the alignment of the
446  /// struct, its size, and the offsets of its fields.
447  ///
448  /// Note that this information is lazily cached.
449  const StructLayout *getStructLayout(StructType *Ty) const;
450
451  /// \brief Returns the preferred alignment of the specified global.
452  ///
453  /// This includes an explicitly requested alignment (if the global has one).
454  unsigned getPreferredAlignment(const GlobalVariable *GV) const;
455
456  /// \brief Returns the preferred alignment of the specified global, returned
457  /// in log form.
458  ///
459  /// This includes an explicitly requested alignment (if the global has one).
460  unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
461};
462
463inline DataLayout *unwrap(LLVMTargetDataRef P) {
464  return reinterpret_cast<DataLayout *>(P);
465}
466
467inline LLVMTargetDataRef wrap(const DataLayout *P) {
468  return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P));
469}
470
471/// Used to lazily calculate structure layout information for a target machine,
472/// based on the DataLayout structure.
473class StructLayout {
474  uint64_t StructSize;
475  unsigned StructAlignment;
476  unsigned IsPadded : 1;
477  unsigned NumElements : 31;
478  uint64_t MemberOffsets[1]; // variable sized array!
479public:
480  uint64_t getSizeInBytes() const { return StructSize; }
481
482  uint64_t getSizeInBits() const { return 8 * StructSize; }
483
484  unsigned getAlignment() const { return StructAlignment; }
485
486  /// Returns whether the struct has padding or not between its fields.
487  /// NB: Padding in nested element is not taken into account.
488  bool hasPadding() const { return IsPadded; }
489
490  /// \brief Given a valid byte offset into the structure, returns the structure
491  /// index that contains it.
492  unsigned getElementContainingOffset(uint64_t Offset) const;
493
494  uint64_t getElementOffset(unsigned Idx) const {
495    assert(Idx < NumElements && "Invalid element idx!");
496    return MemberOffsets[Idx];
497  }
498
499  uint64_t getElementOffsetInBits(unsigned Idx) const {
500    return getElementOffset(Idx) * 8;
501  }
502
503private:
504  friend class DataLayout; // Only DataLayout can create this class
505  StructLayout(StructType *ST, const DataLayout &DL);
506};
507
508// The implementation of this method is provided inline as it is particularly
509// well suited to constant folding when called on a specific Type subclass.
510inline uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const {
511  assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
512  switch (Ty->getTypeID()) {
513  case Type::LabelTyID:
514    return getPointerSizeInBits(0);
515  case Type::PointerTyID:
516    return getPointerSizeInBits(Ty->getPointerAddressSpace());
517  case Type::ArrayTyID: {
518    ArrayType *ATy = cast<ArrayType>(Ty);
519    return ATy->getNumElements() *
520           getTypeAllocSizeInBits(ATy->getElementType());
521  }
522  case Type::StructTyID:
523    // Get the layout annotation... which is lazily created on demand.
524    return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
525  case Type::IntegerTyID:
526    return Ty->getIntegerBitWidth();
527  case Type::HalfTyID:
528    return 16;
529  case Type::FloatTyID:
530    return 32;
531  case Type::DoubleTyID:
532  case Type::X86_MMXTyID:
533    return 64;
534  case Type::PPC_FP128TyID:
535  case Type::FP128TyID:
536    return 128;
537  // In memory objects this is always aligned to a higher boundary, but
538  // only 80 bits contain information.
539  case Type::X86_FP80TyID:
540    return 80;
541  case Type::VectorTyID: {
542    VectorType *VTy = cast<VectorType>(Ty);
543    return VTy->getNumElements() * getTypeSizeInBits(VTy->getElementType());
544  }
545  default:
546    llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
547  }
548}
549
550} // End llvm namespace
551
552#endif
553