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"
32 #include "llvm/Support/Alignment.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 /// 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 /// 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 /// 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
;
98 static PointerAlignElem
get(uint32_t AddressSpace
, unsigned ABIAlign
,
99 unsigned PrefAlign
, uint32_t TypeByteWidth
,
100 uint32_t IndexWidth
);
102 bool operator==(const PointerAlignElem
&rhs
) const;
105 /// A parsed version of the target data layout string in and methods for
108 /// The target data layout string is specified *by the target* - a frontend
109 /// generating LLVM IR is required to generate the right target data for the
110 /// target being codegen'd to.
113 enum class FunctionPtrAlignType
{
114 /// The function pointer alignment is independent of the function alignment.
116 /// The function pointer alignment is a multiple of the function alignment.
117 MultipleOfFunctionAlign
,
120 /// Defaults to false.
123 unsigned AllocaAddrSpace
;
124 MaybeAlign StackNaturalAlign
;
125 unsigned ProgramAddrSpace
;
127 MaybeAlign FunctionPtrAlign
;
128 FunctionPtrAlignType TheFunctionPtrAlignType
;
138 ManglingModeT ManglingMode
;
140 SmallVector
<unsigned char, 8> LegalIntWidths
;
142 /// Primitive type alignment data. This is sorted by type and bit
143 /// width during construction.
144 using AlignmentsTy
= SmallVector
<LayoutAlignElem
, 16>;
145 AlignmentsTy Alignments
;
147 AlignmentsTy::const_iterator
148 findAlignmentLowerBound(AlignTypeEnum AlignType
, uint32_t BitWidth
) const {
149 return const_cast<DataLayout
*>(this)->findAlignmentLowerBound(AlignType
,
153 AlignmentsTy::iterator
154 findAlignmentLowerBound(AlignTypeEnum AlignType
, uint32_t BitWidth
);
156 /// The string representation used to create this DataLayout
157 std::string StringRepresentation
;
159 using PointersTy
= SmallVector
<PointerAlignElem
, 8>;
162 PointersTy::const_iterator
163 findPointerLowerBound(uint32_t AddressSpace
) const {
164 return const_cast<DataLayout
*>(this)->findPointerLowerBound(AddressSpace
);
167 PointersTy::iterator
findPointerLowerBound(uint32_t AddressSpace
);
169 // The StructType -> StructLayout map.
170 mutable void *LayoutMap
= nullptr;
172 /// Pointers in these address spaces are non-integral, and don't have a
173 /// well-defined bitwise representation.
174 SmallVector
<unsigned, 8> NonIntegralAddressSpaces
;
176 void setAlignment(AlignTypeEnum align_type
, unsigned abi_align
,
177 unsigned pref_align
, uint32_t bit_width
);
178 unsigned getAlignmentInfo(AlignTypeEnum align_type
, uint32_t bit_width
,
179 bool ABIAlign
, Type
*Ty
) const;
180 void setPointerAlignment(uint32_t AddrSpace
, unsigned ABIAlign
,
181 unsigned PrefAlign
, uint32_t TypeByteWidth
,
182 uint32_t IndexWidth
);
184 /// Internal helper method that returns requested alignment for type.
185 unsigned getAlignment(Type
*Ty
, bool abi_or_pref
) const;
187 /// Parses a target data specification string. Assert if the string is
189 void parseSpecifier(StringRef LayoutDescription
);
191 // Free all internal data structures.
195 /// Constructs a DataLayout from a specification string. See reset().
196 explicit DataLayout(StringRef LayoutDescription
) {
197 reset(LayoutDescription
);
200 /// Initialize target data from properties stored in the module.
201 explicit DataLayout(const Module
*M
);
203 DataLayout(const DataLayout
&DL
) { *this = DL
; }
205 ~DataLayout(); // Not virtual, do not subclass this class
207 DataLayout
&operator=(const DataLayout
&DL
) {
209 StringRepresentation
= DL
.StringRepresentation
;
210 BigEndian
= DL
.isBigEndian();
211 AllocaAddrSpace
= DL
.AllocaAddrSpace
;
212 StackNaturalAlign
= DL
.StackNaturalAlign
;
213 FunctionPtrAlign
= DL
.FunctionPtrAlign
;
214 TheFunctionPtrAlignType
= DL
.TheFunctionPtrAlignType
;
215 ProgramAddrSpace
= DL
.ProgramAddrSpace
;
216 ManglingMode
= DL
.ManglingMode
;
217 LegalIntWidths
= DL
.LegalIntWidths
;
218 Alignments
= DL
.Alignments
;
219 Pointers
= DL
.Pointers
;
220 NonIntegralAddressSpaces
= DL
.NonIntegralAddressSpaces
;
224 bool operator==(const DataLayout
&Other
) const;
225 bool operator!=(const DataLayout
&Other
) const { return !(*this == Other
); }
227 void init(const Module
*M
);
229 /// Parse a data layout string (with fallback to default values).
230 void reset(StringRef LayoutDescription
);
232 /// Layout endianness...
233 bool isLittleEndian() const { return !BigEndian
; }
234 bool isBigEndian() const { return BigEndian
; }
236 /// Returns the string representation of the DataLayout.
238 /// This representation is in the same format accepted by the string
239 /// constructor above. This should not be used to compare two DataLayout as
240 /// different string can represent the same layout.
241 const std::string
&getStringRepresentation() const {
242 return StringRepresentation
;
245 /// Test if the DataLayout was constructed from an empty string.
246 bool isDefault() const { return StringRepresentation
.empty(); }
248 /// Returns true if the specified type is known to be a native integer
249 /// type supported by the CPU.
251 /// For example, i64 is not native on most 32-bit CPUs and i37 is not native
252 /// on any known one. This returns false if the integer width is not legal.
254 /// The width is specified in bits.
255 bool isLegalInteger(uint64_t Width
) const {
256 for (unsigned LegalIntWidth
: LegalIntWidths
)
257 if (LegalIntWidth
== Width
)
262 bool isIllegalInteger(uint64_t Width
) const { return !isLegalInteger(Width
); }
264 /// Returns true if the given alignment exceeds the natural stack alignment.
265 bool exceedsNaturalStackAlignment(llvm::Align Align
) const {
266 return StackNaturalAlign
&& (Align
> StackNaturalAlign
);
269 unsigned getStackAlignment() const { return StackNaturalAlign
? StackNaturalAlign
->value() : 0; }
270 unsigned getAllocaAddrSpace() const { return AllocaAddrSpace
; }
272 /// Returns the alignment of function pointers, which may or may not be
273 /// related to the alignment of functions.
274 /// \see getFunctionPtrAlignType
275 MaybeAlign
getFunctionPtrAlign() const { return FunctionPtrAlign
; }
277 /// Return the type of function pointer alignment.
278 /// \see getFunctionPtrAlign
279 FunctionPtrAlignType
getFunctionPtrAlignType() const {
280 return TheFunctionPtrAlignType
;
283 unsigned getProgramAddressSpace() const { return ProgramAddrSpace
; }
285 bool hasMicrosoftFastStdCallMangling() const {
286 return ManglingMode
== MM_WinCOFFX86
;
289 /// Returns true if symbols with leading question marks should not receive IR
290 /// mangling. True for Windows mangling modes.
291 bool doNotMangleLeadingQuestionMark() const {
292 return ManglingMode
== MM_WinCOFF
|| ManglingMode
== MM_WinCOFFX86
;
295 bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode
== MM_MachO
; }
297 StringRef
getLinkerPrivateGlobalPrefix() const {
298 if (ManglingMode
== MM_MachO
)
303 char getGlobalPrefix() const {
304 switch (ManglingMode
) {
314 llvm_unreachable("invalid mangling mode");
317 StringRef
getPrivateGlobalPrefix() const {
318 switch (ManglingMode
) {
330 llvm_unreachable("invalid mangling mode");
333 static const char *getManglingComponent(const Triple
&T
);
335 /// Returns true if the specified type fits in a native integer type
336 /// supported by the CPU.
338 /// For example, if the CPU only supports i32 as a native integer type, then
339 /// i27 fits in a legal integer type but i45 does not.
340 bool fitsInLegalInteger(unsigned Width
) const {
341 for (unsigned LegalIntWidth
: LegalIntWidths
)
342 if (Width
<= LegalIntWidth
)
347 /// Layout pointer alignment
348 unsigned getPointerABIAlignment(unsigned AS
) const;
350 /// Return target's alignment for stack-based pointers
351 /// FIXME: The defaults need to be removed once all of
352 /// the backends/clients are updated.
353 unsigned getPointerPrefAlignment(unsigned AS
= 0) const;
355 /// Layout pointer size
356 /// FIXME: The defaults need to be removed once all of
357 /// the backends/clients are updated.
358 unsigned getPointerSize(unsigned AS
= 0) const;
360 /// Returns the maximum pointer size over all address spaces.
361 unsigned getMaxPointerSize() const;
363 // Index size used for address calculation.
364 unsigned getIndexSize(unsigned AS
) const;
366 /// Return the address spaces containing non-integral pointers. Pointers in
367 /// this address space don't have a well-defined bitwise representation.
368 ArrayRef
<unsigned> getNonIntegralAddressSpaces() const {
369 return NonIntegralAddressSpaces
;
372 bool isNonIntegralAddressSpace(unsigned AddrSpace
) const {
373 ArrayRef
<unsigned> NonIntegralSpaces
= getNonIntegralAddressSpaces();
374 return find(NonIntegralSpaces
, AddrSpace
) != NonIntegralSpaces
.end();
377 bool isNonIntegralPointerType(PointerType
*PT
) const {
378 return isNonIntegralAddressSpace(PT
->getAddressSpace());
381 bool isNonIntegralPointerType(Type
*Ty
) const {
382 auto *PTy
= dyn_cast
<PointerType
>(Ty
);
383 return PTy
&& isNonIntegralPointerType(PTy
);
386 /// Layout pointer size, in bits
387 /// FIXME: The defaults need to be removed once all of
388 /// the backends/clients are updated.
389 unsigned getPointerSizeInBits(unsigned AS
= 0) const {
390 return getPointerSize(AS
) * 8;
393 /// Returns the maximum pointer size over all address spaces.
394 unsigned getMaxPointerSizeInBits() const {
395 return getMaxPointerSize() * 8;
398 /// Size in bits of index used for address calculation in getelementptr.
399 unsigned getIndexSizeInBits(unsigned AS
) const {
400 return getIndexSize(AS
) * 8;
403 /// Layout pointer size, in bits, based on the type. If this function is
404 /// called with a pointer type, then the type size of the pointer is returned.
405 /// If this function is called with a vector of pointers, then the type size
406 /// of the pointer is returned. This should only be called with a pointer or
407 /// vector of pointers.
408 unsigned getPointerTypeSizeInBits(Type
*) const;
410 /// Layout size of the index used in GEP calculation.
411 /// The function should be called with pointer or vector of pointers type.
412 unsigned getIndexTypeSizeInBits(Type
*Ty
) const;
414 unsigned getPointerTypeSize(Type
*Ty
) const {
415 return getPointerTypeSizeInBits(Ty
) / 8;
420 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*]
421 /// ---- ---------- --------------- ---------------
430 /// X86_FP80 80 80 96
432 /// [*] The alloc size depends on the alignment, and thus on the target.
433 /// These values are for x86-32 linux.
435 /// Returns the number of bits necessary to hold the specified type.
437 /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must
438 /// have a size (Type::isSized() must return true).
439 uint64_t getTypeSizeInBits(Type
*Ty
) const;
441 /// Returns the maximum number of bytes that may be overwritten by
442 /// storing the specified type.
444 /// For example, returns 5 for i36 and 10 for x86_fp80.
445 uint64_t getTypeStoreSize(Type
*Ty
) const {
446 return (getTypeSizeInBits(Ty
) + 7) / 8;
449 /// Returns the maximum number of bits that may be overwritten by
450 /// storing the specified type; always a multiple of 8.
452 /// For example, returns 40 for i36 and 80 for x86_fp80.
453 uint64_t getTypeStoreSizeInBits(Type
*Ty
) const {
454 return 8 * getTypeStoreSize(Ty
);
457 /// Returns true if no extra padding bits are needed when storing the
460 /// For example, returns false for i19 that has a 24-bit store size.
461 bool typeSizeEqualsStoreSize(Type
*Ty
) const {
462 return getTypeSizeInBits(Ty
) == getTypeStoreSizeInBits(Ty
);
465 /// Returns the offset in bytes between successive objects of the
466 /// specified type, including alignment padding.
468 /// This is the amount that alloca reserves for this type. For example,
469 /// returns 12 or 16 for x86_fp80, depending on alignment.
470 uint64_t getTypeAllocSize(Type
*Ty
) const {
471 // Round up to the next alignment boundary.
472 return alignTo(getTypeStoreSize(Ty
), getABITypeAlignment(Ty
));
475 /// Returns the offset in bits between successive objects of the
476 /// specified type, including alignment padding; always a multiple of 8.
478 /// This is the amount that alloca reserves for this type. For example,
479 /// returns 96 or 128 for x86_fp80, depending on alignment.
480 uint64_t getTypeAllocSizeInBits(Type
*Ty
) const {
481 return 8 * getTypeAllocSize(Ty
);
484 /// Returns the minimum ABI-required alignment for the specified type.
485 unsigned getABITypeAlignment(Type
*Ty
) const;
487 /// Returns the minimum ABI-required alignment for an integer type of
488 /// the specified bitwidth.
489 unsigned getABIIntegerTypeAlignment(unsigned BitWidth
) const;
491 /// Returns the preferred stack/global alignment for the specified
494 /// This is always at least as good as the ABI alignment.
495 unsigned getPrefTypeAlignment(Type
*Ty
) const;
497 /// Returns an integer type with size at least as big as that of a
498 /// pointer in the given address space.
499 IntegerType
*getIntPtrType(LLVMContext
&C
, unsigned AddressSpace
= 0) const;
501 /// Returns an integer (vector of integer) type with size at least as
502 /// big as that of a pointer of the given pointer (vector of pointer) type.
503 Type
*getIntPtrType(Type
*) const;
505 /// Returns the smallest integer type with size at least as big as
507 Type
*getSmallestLegalIntType(LLVMContext
&C
, unsigned Width
= 0) const;
509 /// Returns the largest legal integer type, or null if none are set.
510 Type
*getLargestLegalIntType(LLVMContext
&C
) const {
511 unsigned LargestSize
= getLargestLegalIntTypeSizeInBits();
512 return (LargestSize
== 0) ? nullptr : Type::getIntNTy(C
, LargestSize
);
515 /// Returns the size of largest legal integer type size, or 0 if none
517 unsigned getLargestLegalIntTypeSizeInBits() const;
519 /// Returns the type of a GEP index.
520 /// If it was not specified explicitly, it will be the integer type of the
521 /// pointer width - IntPtrType.
522 Type
*getIndexType(Type
*PtrTy
) const;
524 /// Returns the offset from the beginning of the type for the specified
527 /// Note that this takes the element type, not the pointer type.
528 /// This is used to implement getelementptr.
529 int64_t getIndexedOffsetInType(Type
*ElemTy
, ArrayRef
<Value
*> Indices
) const;
531 /// Returns a StructLayout object, indicating the alignment of the
532 /// struct, its size, and the offsets of its fields.
534 /// Note that this information is lazily cached.
535 const StructLayout
*getStructLayout(StructType
*Ty
) const;
537 /// Returns the preferred alignment of the specified global.
539 /// This includes an explicitly requested alignment (if the global has one).
540 unsigned getPreferredAlignment(const GlobalVariable
*GV
) const;
542 /// Returns the preferred alignment of the specified global, returned
545 /// This includes an explicitly requested alignment (if the global has one).
546 unsigned getPreferredAlignmentLog(const GlobalVariable
*GV
) const;
549 inline DataLayout
*unwrap(LLVMTargetDataRef P
) {
550 return reinterpret_cast<DataLayout
*>(P
);
553 inline LLVMTargetDataRef
wrap(const DataLayout
*P
) {
554 return reinterpret_cast<LLVMTargetDataRef
>(const_cast<DataLayout
*>(P
));
557 /// Used to lazily calculate structure layout information for a target machine,
558 /// based on the DataLayout structure.
561 unsigned StructAlignment
;
562 unsigned IsPadded
: 1;
563 unsigned NumElements
: 31;
564 uint64_t MemberOffsets
[1]; // variable sized array!
567 uint64_t getSizeInBytes() const { return StructSize
; }
569 uint64_t getSizeInBits() const { return 8 * StructSize
; }
571 unsigned getAlignment() const { return StructAlignment
; }
573 /// Returns whether the struct has padding or not between its fields.
574 /// NB: Padding in nested element is not taken into account.
575 bool hasPadding() const { return IsPadded
; }
577 /// Given a valid byte offset into the structure, returns the structure
578 /// index that contains it.
579 unsigned getElementContainingOffset(uint64_t Offset
) const;
581 uint64_t getElementOffset(unsigned Idx
) const {
582 assert(Idx
< NumElements
&& "Invalid element idx!");
583 return MemberOffsets
[Idx
];
586 uint64_t getElementOffsetInBits(unsigned Idx
) const {
587 return getElementOffset(Idx
) * 8;
591 friend class DataLayout
; // Only DataLayout can create this class
593 StructLayout(StructType
*ST
, const DataLayout
&DL
);
596 // The implementation of this method is provided inline as it is particularly
597 // well suited to constant folding when called on a specific Type subclass.
598 inline uint64_t DataLayout::getTypeSizeInBits(Type
*Ty
) const {
599 assert(Ty
->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
600 switch (Ty
->getTypeID()) {
601 case Type::LabelTyID
:
602 return getPointerSizeInBits(0);
603 case Type::PointerTyID
:
604 return getPointerSizeInBits(Ty
->getPointerAddressSpace());
605 case Type::ArrayTyID
: {
606 ArrayType
*ATy
= cast
<ArrayType
>(Ty
);
607 return ATy
->getNumElements() *
608 getTypeAllocSizeInBits(ATy
->getElementType());
610 case Type::StructTyID
:
611 // Get the layout annotation... which is lazily created on demand.
612 return getStructLayout(cast
<StructType
>(Ty
))->getSizeInBits();
613 case Type::IntegerTyID
:
614 return Ty
->getIntegerBitWidth();
617 case Type::FloatTyID
:
619 case Type::DoubleTyID
:
620 case Type::X86_MMXTyID
:
622 case Type::PPC_FP128TyID
:
623 case Type::FP128TyID
:
625 // In memory objects this is always aligned to a higher boundary, but
626 // only 80 bits contain information.
627 case Type::X86_FP80TyID
:
629 case Type::VectorTyID
: {
630 VectorType
*VTy
= cast
<VectorType
>(Ty
);
631 return VTy
->getNumElements() * getTypeSizeInBits(VTy
->getElementType());
634 llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
638 } // end namespace llvm
640 #endif // LLVM_IR_DATALAYOUT_H