1 //===- llvm/DataLayout.h - Data size & alignment info -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_IR_DATALAYOUT_H
21 #define LLVM_IR_DATALAYOUT_H
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/Pass.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/MathExtras.h"
37 // This needs to be outside of the namespace, to avoid conflict with llvm-c
39 using LLVMTargetDataRef = struct LLVMOpaqueTargetData *;
50 /// Enum used to categorize the alignment types stored by LayoutAlignElem
59 // FIXME: Currently the DataLayout string carries a "preferred alignment"
60 // for types. As the DataLayout is module/global, this should likely be
61 // sunk down to an FTTI element that is queried rather than a global
64 /// \brief Layout alignment element.
66 /// Stores the alignment data associated with a given alignment type (integer,
67 /// vector, float) and type bit width.
69 /// \note The unusual order of elements in the structure attempts to reduce
70 /// padding and make the structure slightly more cache friendly.
71 struct LayoutAlignElem {
72 /// \brief Alignment type from \c AlignTypeEnum
73 unsigned AlignType : 8;
74 unsigned TypeBitWidth : 24;
75 unsigned ABIAlign : 16;
76 unsigned PrefAlign : 16;
78 static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align,
79 unsigned pref_align, uint32_t bit_width);
81 bool operator==(const LayoutAlignElem &rhs) const;
84 /// \brief Layout pointer alignment element.
86 /// Stores the alignment data associated with a given pointer and address space.
88 /// \note The unusual order of elements in the structure attempts to reduce
89 /// padding and make the structure slightly more cache friendly.
90 struct PointerAlignElem {
93 uint32_t TypeByteWidth;
94 uint32_t AddressSpace;
97 static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign,
98 unsigned PrefAlign, uint32_t TypeByteWidth);
100 bool operator==(const PointerAlignElem &rhs) const;
103 /// \brief A parsed version of the target data layout string in and methods for
106 /// The target data layout string is specified *by the target* - a frontend
107 /// generating LLVM IR is required to generate the right target data for the
108 /// target being codegen'd to.
111 /// Defaults to false.
114 unsigned AllocaAddrSpace;
115 unsigned StackNaturalAlign;
125 ManglingModeT ManglingMode;
127 SmallVector<unsigned char, 8> LegalIntWidths;
129 /// \brief Primitive type alignment data. This is sorted by type and bit
130 /// width during construction.
131 using AlignmentsTy = SmallVector<LayoutAlignElem, 16>;
132 AlignmentsTy Alignments;
134 AlignmentsTy::const_iterator
135 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth) const {
136 return const_cast<DataLayout *>(this)->findAlignmentLowerBound(AlignType,
140 AlignmentsTy::iterator
141 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth);
143 /// \brief The string representation used to create this DataLayout
144 std::string StringRepresentation;
146 using PointersTy = SmallVector<PointerAlignElem, 8>;
149 PointersTy::const_iterator
150 findPointerLowerBound(uint32_t AddressSpace) const {
151 return const_cast<DataLayout *>(this)->findPointerLowerBound(AddressSpace);
154 PointersTy::iterator findPointerLowerBound(uint32_t AddressSpace);
156 // The StructType -> StructLayout map.
157 mutable void *LayoutMap = nullptr;
159 /// Pointers in these address spaces are non-integral, and don't have a
160 /// well-defined bitwise representation.
161 SmallVector<unsigned, 8> NonIntegralAddressSpaces;
163 void setAlignment(AlignTypeEnum align_type, unsigned abi_align,
164 unsigned pref_align, uint32_t bit_width);
165 unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
166 bool ABIAlign, Type *Ty) const;
167 void setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
168 unsigned PrefAlign, uint32_t TypeByteWidth);
170 /// Internal helper method that returns requested alignment for type.
171 unsigned getAlignment(Type *Ty, bool abi_or_pref) const;
173 /// Parses a target data specification string. Assert if the string is
175 void parseSpecifier(StringRef LayoutDescription);
177 // Free all internal data structures.
181 /// Constructs a DataLayout from a specification string. See reset().
182 explicit DataLayout(StringRef LayoutDescription) {
183 reset(LayoutDescription);
186 /// Initialize target data from properties stored in the module.
187 explicit DataLayout(const Module *M);
189 DataLayout(const DataLayout &DL) { *this = DL; }
191 ~DataLayout(); // Not virtual, do not subclass this class
193 DataLayout &operator=(const DataLayout &DL) {
195 StringRepresentation = DL.StringRepresentation;
196 BigEndian = DL.isBigEndian();
197 AllocaAddrSpace = DL.AllocaAddrSpace;
198 StackNaturalAlign = DL.StackNaturalAlign;
199 ManglingMode = DL.ManglingMode;
200 LegalIntWidths = DL.LegalIntWidths;
201 Alignments = DL.Alignments;
202 Pointers = DL.Pointers;
203 NonIntegralAddressSpaces = DL.NonIntegralAddressSpaces;
207 bool operator==(const DataLayout &Other) const;
208 bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
210 void init(const Module *M);
212 /// Parse a data layout string (with fallback to default values).
213 void reset(StringRef LayoutDescription);
215 /// Layout endianness...
216 bool isLittleEndian() const { return !BigEndian; }
217 bool isBigEndian() const { return BigEndian; }
219 /// \brief Returns the string representation of the DataLayout.
221 /// This representation is in the same format accepted by the string
222 /// constructor above. This should not be used to compare two DataLayout as
223 /// different string can represent the same layout.
224 const std::string &getStringRepresentation() const {
225 return StringRepresentation;
228 /// \brief Test if the DataLayout was constructed from an empty string.
229 bool isDefault() const { return StringRepresentation.empty(); }
231 /// \brief Returns true if the specified type is known to be a native integer
232 /// type supported by the CPU.
234 /// For example, i64 is not native on most 32-bit CPUs and i37 is not native
235 /// on any known one. This returns false if the integer width is not legal.
237 /// The width is specified in bits.
238 bool isLegalInteger(uint64_t Width) const {
239 for (unsigned LegalIntWidth : LegalIntWidths)
240 if (LegalIntWidth == Width)
245 bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); }
247 /// Returns true if the given alignment exceeds the natural stack alignment.
248 bool exceedsNaturalStackAlignment(unsigned Align) const {
249 return (StackNaturalAlign != 0) && (Align > StackNaturalAlign);
252 unsigned getStackAlignment() const { return StackNaturalAlign; }
253 unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; }
255 bool hasMicrosoftFastStdCallMangling() const {
256 return ManglingMode == MM_WinCOFFX86;
259 bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; }
261 StringRef getLinkerPrivateGlobalPrefix() const {
262 if (ManglingMode == MM_MachO)
267 char getGlobalPrefix() const {
268 switch (ManglingMode) {
278 llvm_unreachable("invalid mangling mode");
281 StringRef getPrivateGlobalPrefix() const {
282 switch (ManglingMode) {
294 llvm_unreachable("invalid mangling mode");
297 static const char *getManglingComponent(const Triple &T);
299 /// \brief Returns true if the specified type fits in a native integer type
300 /// supported by the CPU.
302 /// For example, if the CPU only supports i32 as a native integer type, then
303 /// i27 fits in a legal integer type but i45 does not.
304 bool fitsInLegalInteger(unsigned Width) const {
305 for (unsigned LegalIntWidth : LegalIntWidths)
306 if (Width <= LegalIntWidth)
311 /// Layout pointer alignment
312 /// FIXME: The defaults need to be removed once all of
313 /// the backends/clients are updated.
314 unsigned getPointerABIAlignment(unsigned AS = 0) const;
316 /// Return target's alignment for stack-based pointers
317 /// FIXME: The defaults need to be removed once all of
318 /// the backends/clients are updated.
319 unsigned getPointerPrefAlignment(unsigned AS = 0) const;
321 /// Layout pointer size
322 /// FIXME: The defaults need to be removed once all of
323 /// the backends/clients are updated.
324 unsigned getPointerSize(unsigned AS = 0) const;
326 /// Return the address spaces containing non-integral pointers. Pointers in
327 /// this address space don't have a well-defined bitwise representation.
328 ArrayRef<unsigned> getNonIntegralAddressSpaces() const {
329 return NonIntegralAddressSpaces;
332 bool isNonIntegralPointerType(PointerType *PT) const {
333 ArrayRef<unsigned> NonIntegralSpaces = getNonIntegralAddressSpaces();
334 return find(NonIntegralSpaces, PT->getAddressSpace()) !=
335 NonIntegralSpaces.end();
338 bool isNonIntegralPointerType(Type *Ty) const {
339 auto *PTy = dyn_cast<PointerType>(Ty);
340 return PTy && isNonIntegralPointerType(PTy);
343 /// Layout pointer size, in bits
344 /// FIXME: The defaults need to be removed once all of
345 /// the backends/clients are updated.
346 unsigned getPointerSizeInBits(unsigned AS = 0) const {
347 return getPointerSize(AS) * 8;
350 /// Layout pointer size, in bits, based on the type. If this function is
351 /// called with a pointer type, then the type size of the pointer is returned.
352 /// If this function is called with a vector of pointers, then the type size
353 /// of the pointer is returned. This should only be called with a pointer or
354 /// vector of pointers.
355 unsigned getPointerTypeSizeInBits(Type *) const;
357 unsigned getPointerTypeSize(Type *Ty) const {
358 return getPointerTypeSizeInBits(Ty) / 8;
363 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*]
364 /// ---- ---------- --------------- ---------------
373 /// X86_FP80 80 80 96
375 /// [*] The alloc size depends on the alignment, and thus on the target.
376 /// These values are for x86-32 linux.
378 /// \brief Returns the number of bits necessary to hold the specified type.
380 /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must
381 /// have a size (Type::isSized() must return true).
382 uint64_t getTypeSizeInBits(Type *Ty) const;
384 /// \brief Returns the maximum number of bytes that may be overwritten by
385 /// storing the specified type.
387 /// For example, returns 5 for i36 and 10 for x86_fp80.
388 uint64_t getTypeStoreSize(Type *Ty) const {
389 return (getTypeSizeInBits(Ty) + 7) / 8;
392 /// \brief Returns the maximum number of bits that may be overwritten by
393 /// storing the specified type; always a multiple of 8.
395 /// For example, returns 40 for i36 and 80 for x86_fp80.
396 uint64_t getTypeStoreSizeInBits(Type *Ty) const {
397 return 8 * getTypeStoreSize(Ty);
400 /// \brief Returns the offset in bytes between successive objects of the
401 /// specified type, including alignment padding.
403 /// This is the amount that alloca reserves for this type. For example,
404 /// returns 12 or 16 for x86_fp80, depending on alignment.
405 uint64_t getTypeAllocSize(Type *Ty) const {
406 // Round up to the next alignment boundary.
407 return alignTo(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
410 /// \brief Returns the offset in bits between successive objects of the
411 /// specified type, including alignment padding; always a multiple of 8.
413 /// This is the amount that alloca reserves for this type. For example,
414 /// returns 96 or 128 for x86_fp80, depending on alignment.
415 uint64_t getTypeAllocSizeInBits(Type *Ty) const {
416 return 8 * getTypeAllocSize(Ty);
419 /// \brief Returns the minimum ABI-required alignment for the specified type.
420 unsigned getABITypeAlignment(Type *Ty) const;
422 /// \brief Returns the minimum ABI-required alignment for an integer type of
423 /// the specified bitwidth.
424 unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const;
426 /// \brief Returns the preferred stack/global alignment for the specified
429 /// This is always at least as good as the ABI alignment.
430 unsigned getPrefTypeAlignment(Type *Ty) const;
432 /// \brief Returns the preferred alignment for the specified type, returned as
433 /// log2 of the value (a shift amount).
434 unsigned getPreferredTypeAlignmentShift(Type *Ty) const;
436 /// \brief Returns an integer type with size at least as big as that of a
437 /// pointer in the given address space.
438 IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
440 /// \brief Returns an integer (vector of integer) type with size at least as
441 /// big as that of a pointer of the given pointer (vector of pointer) type.
442 Type *getIntPtrType(Type *) const;
444 /// \brief Returns the smallest integer type with size at least as big as
446 Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const;
448 /// \brief Returns the largest legal integer type, or null if none are set.
449 Type *getLargestLegalIntType(LLVMContext &C) const {
450 unsigned LargestSize = getLargestLegalIntTypeSizeInBits();
451 return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize);
454 /// \brief Returns the size of largest legal integer type size, or 0 if none
456 unsigned getLargestLegalIntTypeSizeInBits() const;
458 /// \brief Returns the offset from the beginning of the type for the specified
461 /// Note that this takes the element type, not the pointer type.
462 /// This is used to implement getelementptr.
463 int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef<Value *> Indices) const;
465 /// \brief Returns a StructLayout object, indicating the alignment of the
466 /// struct, its size, and the offsets of its fields.
468 /// Note that this information is lazily cached.
469 const StructLayout *getStructLayout(StructType *Ty) const;
471 /// \brief Returns the preferred alignment of the specified global.
473 /// This includes an explicitly requested alignment (if the global has one).
474 unsigned getPreferredAlignment(const GlobalVariable *GV) const;
476 /// \brief Returns the preferred alignment of the specified global, returned
479 /// This includes an explicitly requested alignment (if the global has one).
480 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
483 inline DataLayout *unwrap(LLVMTargetDataRef P) {
484 return reinterpret_cast<DataLayout *>(P);
487 inline LLVMTargetDataRef wrap(const DataLayout *P) {
488 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P));
491 /// Used to lazily calculate structure layout information for a target machine,
492 /// based on the DataLayout structure.
495 unsigned StructAlignment;
496 unsigned IsPadded : 1;
497 unsigned NumElements : 31;
498 uint64_t MemberOffsets[1]; // variable sized array!
501 uint64_t getSizeInBytes() const { return StructSize; }
503 uint64_t getSizeInBits() const { return 8 * StructSize; }
505 unsigned getAlignment() const { return StructAlignment; }
507 /// Returns whether the struct has padding or not between its fields.
508 /// NB: Padding in nested element is not taken into account.
509 bool hasPadding() const { return IsPadded; }
511 /// \brief Given a valid byte offset into the structure, returns the structure
512 /// index that contains it.
513 unsigned getElementContainingOffset(uint64_t Offset) const;
515 uint64_t getElementOffset(unsigned Idx) const {
516 assert(Idx < NumElements && "Invalid element idx!");
517 return MemberOffsets[Idx];
520 uint64_t getElementOffsetInBits(unsigned Idx) const {
521 return getElementOffset(Idx) * 8;
525 friend class DataLayout; // Only DataLayout can create this class
527 StructLayout(StructType *ST, const DataLayout &DL);
530 // The implementation of this method is provided inline as it is particularly
531 // well suited to constant folding when called on a specific Type subclass.
532 inline uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const {
533 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
534 switch (Ty->getTypeID()) {
535 case Type::LabelTyID:
536 return getPointerSizeInBits(0);
537 case Type::PointerTyID:
538 return getPointerSizeInBits(Ty->getPointerAddressSpace());
539 case Type::ArrayTyID: {
540 ArrayType *ATy = cast<ArrayType>(Ty);
541 return ATy->getNumElements() *
542 getTypeAllocSizeInBits(ATy->getElementType());
544 case Type::StructTyID:
545 // Get the layout annotation... which is lazily created on demand.
546 return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
547 case Type::IntegerTyID:
548 return Ty->getIntegerBitWidth();
551 case Type::FloatTyID:
553 case Type::DoubleTyID:
554 case Type::X86_MMXTyID:
556 case Type::PPC_FP128TyID:
557 case Type::FP128TyID:
559 // In memory objects this is always aligned to a higher boundary, but
560 // only 80 bits contain information.
561 case Type::X86_FP80TyID:
563 case Type::VectorTyID: {
564 VectorType *VTy = cast<VectorType>(Ty);
565 return VTy->getNumElements() * getTypeSizeInBits(VTy->getElementType());
568 llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
572 } // end namespace llvm
574 #endif // LLVM_IR_DATALAYOUT_H