1 //===- llvm/DataLayout.h - Data size & alignment info -----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines layout properties related to datatype size/offset/alignment
10 // information. It uses lazy annotations to cache information about how
11 // structure types are laid out and used.
13 // This structure should be created once, filled in if the defaults are not
14 // correct and then passed around by const&. None of the members functions
15 // require modification to the object.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_IR_DATALAYOUT_H
20 #define LLVM_IR_DATALAYOUT_H
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/MathExtras.h"
36 // This needs to be outside of the namespace, to avoid conflict with llvm-c
38 using LLVMTargetDataRef
= struct LLVMOpaqueTargetData
*;
49 /// Enum used to categorize the alignment types stored by LayoutAlignElem
58 // FIXME: Currently the DataLayout string carries a "preferred alignment"
59 // for types. As the DataLayout is module/global, this should likely be
60 // sunk down to an FTTI element that is queried rather than a global
63 /// Layout alignment element.
65 /// Stores the alignment data associated with a given alignment type (integer,
66 /// vector, float) and type bit width.
68 /// \note The unusual order of elements in the structure attempts to reduce
69 /// padding and make the structure slightly more cache friendly.
70 struct LayoutAlignElem
{
71 /// Alignment type from \c AlignTypeEnum
72 unsigned AlignType
: 8;
73 unsigned TypeBitWidth
: 24;
74 unsigned ABIAlign
: 16;
75 unsigned PrefAlign
: 16;
77 static LayoutAlignElem
get(AlignTypeEnum align_type
, unsigned abi_align
,
78 unsigned pref_align
, uint32_t bit_width
);
80 bool operator==(const LayoutAlignElem
&rhs
) const;
83 /// Layout pointer alignment element.
85 /// Stores the alignment data associated with a given pointer and address space.
87 /// \note The unusual order of elements in the structure attempts to reduce
88 /// padding and make the structure slightly more cache friendly.
89 struct PointerAlignElem
{
92 uint32_t TypeByteWidth
;
93 uint32_t AddressSpace
;
97 static PointerAlignElem
get(uint32_t AddressSpace
, unsigned ABIAlign
,
98 unsigned PrefAlign
, uint32_t TypeByteWidth
,
101 bool operator==(const PointerAlignElem
&rhs
) const;
104 /// A parsed version of the target data layout string in and methods for
107 /// The target data layout string is specified *by the target* - a frontend
108 /// generating LLVM IR is required to generate the right target data for the
109 /// target being codegen'd to.
112 /// Defaults to false.
115 unsigned AllocaAddrSpace
;
116 unsigned StackNaturalAlign
;
117 unsigned ProgramAddrSpace
;
127 ManglingModeT ManglingMode
;
129 SmallVector
<unsigned char, 8> LegalIntWidths
;
131 /// Primitive type alignment data. This is sorted by type and bit
132 /// width during construction.
133 using AlignmentsTy
= SmallVector
<LayoutAlignElem
, 16>;
134 AlignmentsTy Alignments
;
136 AlignmentsTy::const_iterator
137 findAlignmentLowerBound(AlignTypeEnum AlignType
, uint32_t BitWidth
) const {
138 return const_cast<DataLayout
*>(this)->findAlignmentLowerBound(AlignType
,
142 AlignmentsTy::iterator
143 findAlignmentLowerBound(AlignTypeEnum AlignType
, uint32_t BitWidth
);
145 /// The string representation used to create this DataLayout
146 std::string StringRepresentation
;
148 using PointersTy
= SmallVector
<PointerAlignElem
, 8>;
151 PointersTy::const_iterator
152 findPointerLowerBound(uint32_t AddressSpace
) const {
153 return const_cast<DataLayout
*>(this)->findPointerLowerBound(AddressSpace
);
156 PointersTy::iterator
findPointerLowerBound(uint32_t AddressSpace
);
158 // The StructType -> StructLayout map.
159 mutable void *LayoutMap
= nullptr;
161 /// Pointers in these address spaces are non-integral, and don't have a
162 /// well-defined bitwise representation.
163 SmallVector
<unsigned, 8> NonIntegralAddressSpaces
;
165 void setAlignment(AlignTypeEnum align_type
, unsigned abi_align
,
166 unsigned pref_align
, uint32_t bit_width
);
167 unsigned getAlignmentInfo(AlignTypeEnum align_type
, uint32_t bit_width
,
168 bool ABIAlign
, Type
*Ty
) const;
169 void setPointerAlignment(uint32_t AddrSpace
, unsigned ABIAlign
,
170 unsigned PrefAlign
, uint32_t TypeByteWidth
,
171 uint32_t IndexWidth
);
173 /// Internal helper method that returns requested alignment for type.
174 unsigned getAlignment(Type
*Ty
, bool abi_or_pref
) const;
176 /// Parses a target data specification string. Assert if the string is
178 void parseSpecifier(StringRef LayoutDescription
);
180 // Free all internal data structures.
184 /// Constructs a DataLayout from a specification string. See reset().
185 explicit DataLayout(StringRef LayoutDescription
) {
186 reset(LayoutDescription
);
189 /// Initialize target data from properties stored in the module.
190 explicit DataLayout(const Module
*M
);
192 DataLayout(const DataLayout
&DL
) { *this = DL
; }
194 ~DataLayout(); // Not virtual, do not subclass this class
196 DataLayout
&operator=(const DataLayout
&DL
) {
198 StringRepresentation
= DL
.StringRepresentation
;
199 BigEndian
= DL
.isBigEndian();
200 AllocaAddrSpace
= DL
.AllocaAddrSpace
;
201 StackNaturalAlign
= DL
.StackNaturalAlign
;
202 ProgramAddrSpace
= DL
.ProgramAddrSpace
;
203 ManglingMode
= DL
.ManglingMode
;
204 LegalIntWidths
= DL
.LegalIntWidths
;
205 Alignments
= DL
.Alignments
;
206 Pointers
= DL
.Pointers
;
207 NonIntegralAddressSpaces
= DL
.NonIntegralAddressSpaces
;
211 bool operator==(const DataLayout
&Other
) const;
212 bool operator!=(const DataLayout
&Other
) const { return !(*this == Other
); }
214 void init(const Module
*M
);
216 /// Parse a data layout string (with fallback to default values).
217 void reset(StringRef LayoutDescription
);
219 /// Layout endianness...
220 bool isLittleEndian() const { return !BigEndian
; }
221 bool isBigEndian() const { return BigEndian
; }
223 /// Returns the string representation of the DataLayout.
225 /// This representation is in the same format accepted by the string
226 /// constructor above. This should not be used to compare two DataLayout as
227 /// different string can represent the same layout.
228 const std::string
&getStringRepresentation() const {
229 return StringRepresentation
;
232 /// Test if the DataLayout was constructed from an empty string.
233 bool isDefault() const { return StringRepresentation
.empty(); }
235 /// Returns true if the specified type is known to be a native integer
236 /// type supported by the CPU.
238 /// For example, i64 is not native on most 32-bit CPUs and i37 is not native
239 /// on any known one. This returns false if the integer width is not legal.
241 /// The width is specified in bits.
242 bool isLegalInteger(uint64_t Width
) const {
243 for (unsigned LegalIntWidth
: LegalIntWidths
)
244 if (LegalIntWidth
== Width
)
249 bool isIllegalInteger(uint64_t Width
) const { return !isLegalInteger(Width
); }
251 /// Returns true if the given alignment exceeds the natural stack alignment.
252 bool exceedsNaturalStackAlignment(unsigned Align
) const {
253 return (StackNaturalAlign
!= 0) && (Align
> StackNaturalAlign
);
256 unsigned getStackAlignment() const { return StackNaturalAlign
; }
257 unsigned getAllocaAddrSpace() const { return AllocaAddrSpace
; }
259 unsigned getProgramAddressSpace() const { return ProgramAddrSpace
; }
261 bool hasMicrosoftFastStdCallMangling() const {
262 return ManglingMode
== MM_WinCOFFX86
;
265 /// Returns true if symbols with leading question marks should not receive IR
266 /// mangling. True for Windows mangling modes.
267 bool doNotMangleLeadingQuestionMark() const {
268 return ManglingMode
== MM_WinCOFF
|| ManglingMode
== MM_WinCOFFX86
;
271 bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode
== MM_MachO
; }
273 StringRef
getLinkerPrivateGlobalPrefix() const {
274 if (ManglingMode
== MM_MachO
)
279 char getGlobalPrefix() const {
280 switch (ManglingMode
) {
290 llvm_unreachable("invalid mangling mode");
293 StringRef
getPrivateGlobalPrefix() const {
294 switch (ManglingMode
) {
306 llvm_unreachable("invalid mangling mode");
309 static const char *getManglingComponent(const Triple
&T
);
311 /// Returns true if the specified type fits in a native integer type
312 /// supported by the CPU.
314 /// For example, if the CPU only supports i32 as a native integer type, then
315 /// i27 fits in a legal integer type but i45 does not.
316 bool fitsInLegalInteger(unsigned Width
) const {
317 for (unsigned LegalIntWidth
: LegalIntWidths
)
318 if (Width
<= LegalIntWidth
)
323 /// Layout pointer alignment
324 unsigned getPointerABIAlignment(unsigned AS
) const;
326 /// Return target's alignment for stack-based pointers
327 /// FIXME: The defaults need to be removed once all of
328 /// the backends/clients are updated.
329 unsigned getPointerPrefAlignment(unsigned AS
= 0) const;
331 /// Layout pointer size
332 /// FIXME: The defaults need to be removed once all of
333 /// the backends/clients are updated.
334 unsigned getPointerSize(unsigned AS
= 0) const;
336 /// Returns the maximum pointer size over all address spaces.
337 unsigned getMaxPointerSize() const;
339 // Index size used for address calculation.
340 unsigned getIndexSize(unsigned AS
) const;
342 /// Return the address spaces containing non-integral pointers. Pointers in
343 /// this address space don't have a well-defined bitwise representation.
344 ArrayRef
<unsigned> getNonIntegralAddressSpaces() const {
345 return NonIntegralAddressSpaces
;
348 bool isNonIntegralPointerType(PointerType
*PT
) const {
349 ArrayRef
<unsigned> NonIntegralSpaces
= getNonIntegralAddressSpaces();
350 return find(NonIntegralSpaces
, PT
->getAddressSpace()) !=
351 NonIntegralSpaces
.end();
354 bool isNonIntegralPointerType(Type
*Ty
) const {
355 auto *PTy
= dyn_cast
<PointerType
>(Ty
);
356 return PTy
&& isNonIntegralPointerType(PTy
);
359 /// Layout pointer size, in bits
360 /// FIXME: The defaults need to be removed once all of
361 /// the backends/clients are updated.
362 unsigned getPointerSizeInBits(unsigned AS
= 0) const {
363 return getPointerSize(AS
) * 8;
366 /// Returns the maximum pointer size over all address spaces.
367 unsigned getMaxPointerSizeInBits() const {
368 return getMaxPointerSize() * 8;
371 /// Size in bits of index used for address calculation in getelementptr.
372 unsigned getIndexSizeInBits(unsigned AS
) const {
373 return getIndexSize(AS
) * 8;
376 /// Layout pointer size, in bits, based on the type. If this function is
377 /// called with a pointer type, then the type size of the pointer is returned.
378 /// If this function is called with a vector of pointers, then the type size
379 /// of the pointer is returned. This should only be called with a pointer or
380 /// vector of pointers.
381 unsigned getPointerTypeSizeInBits(Type
*) const;
383 /// Layout size of the index used in GEP calculation.
384 /// The function should be called with pointer or vector of pointers type.
385 unsigned getIndexTypeSizeInBits(Type
*Ty
) const;
387 unsigned getPointerTypeSize(Type
*Ty
) const {
388 return getPointerTypeSizeInBits(Ty
) / 8;
393 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*]
394 /// ---- ---------- --------------- ---------------
403 /// X86_FP80 80 80 96
405 /// [*] The alloc size depends on the alignment, and thus on the target.
406 /// These values are for x86-32 linux.
408 /// Returns the number of bits necessary to hold the specified type.
410 /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must
411 /// have a size (Type::isSized() must return true).
412 uint64_t getTypeSizeInBits(Type
*Ty
) const;
414 /// Returns the maximum number of bytes that may be overwritten by
415 /// storing the specified type.
417 /// For example, returns 5 for i36 and 10 for x86_fp80.
418 uint64_t getTypeStoreSize(Type
*Ty
) const {
419 return (getTypeSizeInBits(Ty
) + 7) / 8;
422 /// Returns the maximum number of bits that may be overwritten by
423 /// storing the specified type; always a multiple of 8.
425 /// For example, returns 40 for i36 and 80 for x86_fp80.
426 uint64_t getTypeStoreSizeInBits(Type
*Ty
) const {
427 return 8 * getTypeStoreSize(Ty
);
430 /// Returns the offset in bytes between successive objects of the
431 /// specified type, including alignment padding.
433 /// This is the amount that alloca reserves for this type. For example,
434 /// returns 12 or 16 for x86_fp80, depending on alignment.
435 uint64_t getTypeAllocSize(Type
*Ty
) const {
436 // Round up to the next alignment boundary.
437 return alignTo(getTypeStoreSize(Ty
), getABITypeAlignment(Ty
));
440 /// Returns the offset in bits between successive objects of the
441 /// specified type, including alignment padding; always a multiple of 8.
443 /// This is the amount that alloca reserves for this type. For example,
444 /// returns 96 or 128 for x86_fp80, depending on alignment.
445 uint64_t getTypeAllocSizeInBits(Type
*Ty
) const {
446 return 8 * getTypeAllocSize(Ty
);
449 /// Returns the minimum ABI-required alignment for the specified type.
450 unsigned getABITypeAlignment(Type
*Ty
) const;
452 /// Returns the minimum ABI-required alignment for an integer type of
453 /// the specified bitwidth.
454 unsigned getABIIntegerTypeAlignment(unsigned BitWidth
) const;
456 /// Returns the preferred stack/global alignment for the specified
459 /// This is always at least as good as the ABI alignment.
460 unsigned getPrefTypeAlignment(Type
*Ty
) const;
462 /// Returns the preferred alignment for the specified type, returned as
463 /// log2 of the value (a shift amount).
464 unsigned getPreferredTypeAlignmentShift(Type
*Ty
) const;
466 /// Returns an integer type with size at least as big as that of a
467 /// pointer in the given address space.
468 IntegerType
*getIntPtrType(LLVMContext
&C
, unsigned AddressSpace
= 0) const;
470 /// Returns an integer (vector of integer) type with size at least as
471 /// big as that of a pointer of the given pointer (vector of pointer) type.
472 Type
*getIntPtrType(Type
*) const;
474 /// Returns the smallest integer type with size at least as big as
476 Type
*getSmallestLegalIntType(LLVMContext
&C
, unsigned Width
= 0) const;
478 /// Returns the largest legal integer type, or null if none are set.
479 Type
*getLargestLegalIntType(LLVMContext
&C
) const {
480 unsigned LargestSize
= getLargestLegalIntTypeSizeInBits();
481 return (LargestSize
== 0) ? nullptr : Type::getIntNTy(C
, LargestSize
);
484 /// Returns the size of largest legal integer type size, or 0 if none
486 unsigned getLargestLegalIntTypeSizeInBits() const;
488 /// Returns the type of a GEP index.
489 /// If it was not specified explicitly, it will be the integer type of the
490 /// pointer width - IntPtrType.
491 Type
*getIndexType(Type
*PtrTy
) const;
493 /// Returns the offset from the beginning of the type for the specified
496 /// Note that this takes the element type, not the pointer type.
497 /// This is used to implement getelementptr.
498 int64_t getIndexedOffsetInType(Type
*ElemTy
, ArrayRef
<Value
*> Indices
) const;
500 /// Returns a StructLayout object, indicating the alignment of the
501 /// struct, its size, and the offsets of its fields.
503 /// Note that this information is lazily cached.
504 const StructLayout
*getStructLayout(StructType
*Ty
) const;
506 /// Returns the preferred alignment of the specified global.
508 /// This includes an explicitly requested alignment (if the global has one).
509 unsigned getPreferredAlignment(const GlobalVariable
*GV
) const;
511 /// Returns the preferred alignment of the specified global, returned
514 /// This includes an explicitly requested alignment (if the global has one).
515 unsigned getPreferredAlignmentLog(const GlobalVariable
*GV
) const;
518 inline DataLayout
*unwrap(LLVMTargetDataRef P
) {
519 return reinterpret_cast<DataLayout
*>(P
);
522 inline LLVMTargetDataRef
wrap(const DataLayout
*P
) {
523 return reinterpret_cast<LLVMTargetDataRef
>(const_cast<DataLayout
*>(P
));
526 /// Used to lazily calculate structure layout information for a target machine,
527 /// based on the DataLayout structure.
530 unsigned StructAlignment
;
531 unsigned IsPadded
: 1;
532 unsigned NumElements
: 31;
533 uint64_t MemberOffsets
[1]; // variable sized array!
536 uint64_t getSizeInBytes() const { return StructSize
; }
538 uint64_t getSizeInBits() const { return 8 * StructSize
; }
540 unsigned getAlignment() const { return StructAlignment
; }
542 /// Returns whether the struct has padding or not between its fields.
543 /// NB: Padding in nested element is not taken into account.
544 bool hasPadding() const { return IsPadded
; }
546 /// Given a valid byte offset into the structure, returns the structure
547 /// index that contains it.
548 unsigned getElementContainingOffset(uint64_t Offset
) const;
550 uint64_t getElementOffset(unsigned Idx
) const {
551 assert(Idx
< NumElements
&& "Invalid element idx!");
552 return MemberOffsets
[Idx
];
555 uint64_t getElementOffsetInBits(unsigned Idx
) const {
556 return getElementOffset(Idx
) * 8;
560 friend class DataLayout
; // Only DataLayout can create this class
562 StructLayout(StructType
*ST
, const DataLayout
&DL
);
565 // The implementation of this method is provided inline as it is particularly
566 // well suited to constant folding when called on a specific Type subclass.
567 inline uint64_t DataLayout::getTypeSizeInBits(Type
*Ty
) const {
568 assert(Ty
->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
569 switch (Ty
->getTypeID()) {
570 case Type::LabelTyID
:
571 return getPointerSizeInBits(0);
572 case Type::PointerTyID
:
573 return getPointerSizeInBits(Ty
->getPointerAddressSpace());
574 case Type::ArrayTyID
: {
575 ArrayType
*ATy
= cast
<ArrayType
>(Ty
);
576 return ATy
->getNumElements() *
577 getTypeAllocSizeInBits(ATy
->getElementType());
579 case Type::StructTyID
:
580 // Get the layout annotation... which is lazily created on demand.
581 return getStructLayout(cast
<StructType
>(Ty
))->getSizeInBits();
582 case Type::IntegerTyID
:
583 return Ty
->getIntegerBitWidth();
586 case Type::FloatTyID
:
588 case Type::DoubleTyID
:
589 case Type::X86_MMXTyID
:
591 case Type::PPC_FP128TyID
:
592 case Type::FP128TyID
:
594 // In memory objects this is always aligned to a higher boundary, but
595 // only 80 bits contain information.
596 case Type::X86_FP80TyID
:
598 case Type::VectorTyID
: {
599 VectorType
*VTy
= cast
<VectorType
>(Ty
);
600 return VTy
->getNumElements() * getTypeSizeInBits(VTy
->getElementType());
603 llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
607 } // end namespace llvm
609 #endif // LLVM_IR_DATALAYOUT_H