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 40// This needs to be outside of the namespace, to avoid conflict with llvm-c 52// FIXME: Currently the DataLayout string carries a "preferred alignment" 53// for types. As the DataLayout is module/global, this should likely be 54// sunk down to an FTTI element that is queried rather than a global 57/// A parsed version of the target data layout string in and methods for 60/// The target data layout string is specified *by the target* - a frontend 61/// generating LLVM IR is required to generate the right target data for the 62/// target being codegen'd to. 65 /// Primitive type specification. 74 /// Pointer type specification. 81 /// Pointers in this address space don't have a well-defined bitwise 82 /// representation (e.g. may be relocated by a copying garbage collector). 83 /// Additionally, they may also be non-integral (i.e. containing additional 84 /// metadata such as bounds information/permissions). 90 /// The function pointer alignment is independent of the function alignment. 92 /// The function pointer alignment is a multiple of the function alignment. 98unsigned AllocaAddrSpace = 0;
99unsigned ProgramAddrSpace = 0;
100unsigned DefaultGlobalsAddrSpace = 0;
117 ManglingModeT ManglingMode = MM_None;
119// FIXME: `unsigned char` truncates the value parsed by `parseSpecifier`. 120 SmallVector<unsigned char, 8> LegalIntWidths;
122 /// Primitive type specifications. Sorted and uniqued by type bit width. 123 SmallVector<PrimitiveSpec, 6> IntSpecs;
124 SmallVector<PrimitiveSpec, 4> FloatSpecs;
125 SmallVector<PrimitiveSpec, 10> VectorSpecs;
127 /// Pointer type specifications. Sorted and uniqued by address space number. 128 SmallVector<PointerSpec, 8> PointerSpecs;
130 /// The string representation used to create this DataLayout 131 std::string StringRepresentation;
133 /// Struct type ABI and preferred alignments. The default spec is "a:8:64". 134 Align StructABIAlignment = Align::Constant<1>();
135 Align StructPrefAlignment = Align::Constant<8>();
137// The StructType -> StructLayout map. 138mutablevoid *LayoutMap =
nullptr;
140 /// Sets or updates the specification for the given primitive type. 144 /// Searches for a pointer specification that matches the given address space. 145 /// Returns the default address space specification if not found. 146const PointerSpec &getPointerSpec(
uint32_t AddrSpace)
const;
148 /// Sets or updates the specification for pointer in the given address space. 150 Align PrefAlign,
uint32_t IndexBitWidth,
153 /// Internal helper to get alignment for integer of given bitwidth. 156 /// Internal helper method that returns requested alignment for type. 157 Align getAlignment(Type *Ty,
bool abi_or_pref)
const;
159 /// Attempts to parse primitive specification ('i', 'f', or 'v'). 160 Error parsePrimitiveSpec(StringRef Spec);
162 /// Attempts to parse aggregate specification ('a'). 163 Error parseAggregateSpec(StringRef Spec);
165 /// Attempts to parse pointer specification ('p'). 166 Error parsePointerSpec(StringRef Spec);
168 /// Attempts to parse a single specification. 169 Error parseSpecification(StringRef Spec,
170 SmallVectorImpl<unsigned> &NonIntegralAddressSpaces);
172 /// Attempts to parse a data layout string. 173 Error parseLayoutString(StringRef LayoutString);
176 /// Constructs a DataLayout with default values. 179 /// Constructs a DataLayout from a specification string. 180 /// WARNING: Aborts execution if the string is malformed. Use parse() instead. 185~DataLayout();
// Not virtual, do not subclass this class 192 /// Parse a data layout string and return the layout. Return an error 193 /// description on failure. 196 /// Layout endianness... 200 /// Returns the string representation of the DataLayout. 202 /// This representation is in the same format accepted by the string 203 /// constructor above. This should not be used to compare two DataLayout as 204 /// different string can represent the same layout. 206return StringRepresentation;
209 /// Test if the DataLayout was constructed from an empty string. 210boolisDefault()
const{
return StringRepresentation.empty(); }
212 /// Returns true if the specified type is known to be a native integer 213 /// type supported by the CPU. 215 /// For example, i64 is not native on most 32-bit CPUs and i37 is not native 216 /// on any known one. This returns false if the integer width is not legal. 218 /// The width is specified in bits. 225 /// Returns the natural stack alignment, or MaybeAlign() if one wasn't 235 /// Returns the alignment of function pointers, which may or may not be 236 /// related to the alignment of functions. 237 /// \see getFunctionPtrAlignType 240 /// Return the type of function pointer alignment. 241 /// \see getFunctionPtrAlign 243return TheFunctionPtrAlignType;
248return DefaultGlobalsAddrSpace;
252return ManglingMode == MM_WinCOFFX86;
255 /// Returns true if symbols with leading question marks should not receive IR 256 /// mangling. True for Windows mangling modes. 258return ManglingMode == MM_WinCOFF || ManglingMode == MM_WinCOFFX86;
264if (ManglingMode == MM_MachO)
270switch (ManglingMode) {
286switch (ManglingMode) {
307 /// Returns true if the specified type fits in a native integer type 308 /// supported by the CPU. 310 /// For example, if the CPU only supports i32 as a native integer type, then 311 /// i27 fits in a legal integer type but i45 does not. 313for (
unsigned LegalIntWidth : LegalIntWidths)
314if (Width <= LegalIntWidth)
319 /// Layout pointer alignment 322 /// Return target's alignment for stack-based pointers 323 /// FIXME: The defaults need to be removed once all of 324 /// the backends/clients are updated. 327 /// Layout pointer size in bytes, rounded up to a whole 329 /// FIXME: The defaults need to be removed once all of 330 /// the backends/clients are updated. 333// Index size in bytes used for address calculation, 334 /// rounded up to a whole number of bytes. 337 /// Return the address spaces containing non-integral pointers. Pointers in 338 /// this address space don't have a well-defined bitwise representation. 357auto *PTy = dyn_cast<PointerType>(Ty);
361 /// Layout pointer size, in bits 362 /// FIXME: The defaults need to be removed once all of 363 /// the backends/clients are updated. 368 /// Size in bits of index used for address calculation in getelementptr. 373 /// Layout pointer size, in bits, based on the type. If this function is 374 /// called with a pointer type, then the type size of the pointer is returned. 375 /// If this function is called with a vector of pointers, then the type size 376 /// of the pointer is returned. This should only be called with a pointer or 377 /// vector of pointers. 380 /// Layout size of the index used in GEP calculation. 381 /// The function should be called with pointer or vector of pointers type. 390 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*] 391 /// ---- ---------- --------------- --------------- 400 /// X86_FP80 80 80 96 402 /// [*] The alloc size depends on the alignment, and thus on the target. 403 /// These values are for x86-32 linux. 405 /// Returns the number of bits necessary to hold the specified type. 407 /// If Ty is a scalable vector type, the scalable property will be set and 408 /// the runtime size will be a positive integer multiple of the base size. 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). 414 /// Returns the maximum number of bytes that may be overwritten by 415 /// storing the specified type. 417 /// If Ty is a scalable vector type, the scalable property will be set and 418 /// the runtime size will be a positive integer multiple of the base size. 420 /// For example, returns 5 for i36 and 10 for x86_fp80. 427 /// Returns the maximum number of bits that may be overwritten by 428 /// storing the specified type; always a multiple of 8. 430 /// If Ty is a scalable vector type, the scalable property will be set and 431 /// the runtime size will be a positive integer multiple of the base size. 433 /// For example, returns 40 for i36 and 80 for x86_fp80. 438return {AlignedSizeInBits, BaseSize.
isScalable()};
441 /// Returns true if no extra padding bits are needed when storing the 444 /// For example, returns false for i19 that has a 24-bit store size. 449 /// Returns the offset in bytes between successive objects of the 450 /// specified type, including alignment padding. 452 /// If Ty is a scalable vector type, the scalable property will be set and 453 /// the runtime size will be a positive integer multiple of the base size. 455 /// This is the amount that alloca reserves for this type. For example, 456 /// returns 12 or 16 for x86_fp80, depending on alignment. 458// Round up to the next alignment boundary. 462 /// Returns the offset in bits between successive objects of the 463 /// specified type, including alignment padding; always a multiple of 8. 465 /// If Ty is a scalable vector type, the scalable property will be set and 466 /// the runtime size will be a positive integer multiple of the base size. 468 /// This is the amount that alloca reserves for this type. For example, 469 /// returns 96 or 128 for x86_fp80, depending on alignment. 474 /// Returns the minimum ABI-required alignment for the specified type. 477 /// Helper function to return `Alignment` if it's set or the result of 478 /// `getABITypeAlign(Ty)`, in any case the result is a valid alignment. 484 /// Returns the minimum ABI-required alignment for an integer type of 485 /// the specified bitwidth. 487return getIntegerAlignment(
BitWidth,
/* abi_or_pref */true);
490 /// Returns the preferred stack/global alignment for the specified 493 /// This is always at least as good as the ABI alignment. 496 /// Returns an integer type with size at least as big as that of a 497 /// pointer in the given address space. 500 /// Returns an integer (vector of integer) type with size at least as 501 /// big as that of a pointer of the given pointer (vector of pointer) type. 504 /// Returns the smallest integer type with size at least as big as 508 /// Returns the largest legal integer type, or null if none are set. 514 /// Returns the size of largest legal integer type size, or 0 if none 518 /// Returns the type of a GEP index in AddressSpace. 519 /// If it was not specified explicitly, it will be the integer type of the 520 /// pointer width - IntPtrType. 523 /// Returns the type of a GEP index. 524 /// If it was not specified explicitly, it will be the integer type of the 525 /// pointer width - IntPtrType. 528 /// Returns the offset from the beginning of the type for the specified 531 /// Note that this takes the element type, not the pointer type. 532 /// This is used to implement getelementptr. 535 /// Get GEP indices to access Offset inside ElemTy. ElemTy is updated to be 536 /// the result element type and Offset to be the residual offset. 539 /// Get single GEP index to access Offset inside ElemTy. Returns std::nullopt 540 /// if index cannot be computed, e.g. because the type is not an aggregate. 541 /// ElemTy is updated to be the result element type and Offset to be the 545 /// Returns a StructLayout object, indicating the alignment of the 546 /// struct, its size, and the offsets of its fields. 548 /// Note that this information is lazily cached. 551 /// Returns the preferred alignment of the specified global. 553 /// This includes an explicitly requested alignment (if the global has one). 565/// Used to lazily calculate structure layout information for a target machine, 566/// based on the DataLayout structure. 569Align StructAlignment;
570unsigned IsPadded : 1;
571unsigned NumElements : 31;
580 /// Returns whether the struct has padding or not between its fields. 581 /// NB: Padding in nested element is not taken into account. 584 /// Given a valid byte offset into the structure, returns the structure 585 /// index that contains it. 597assert(
Idx < NumElements &&
"Invalid element idx!");
606friendclassDataLayout;
// Only DataLayout can create this class 610size_t numTrailingObjects(OverloadToken<TypeSize>)
const{
615// The implementation of this method is provided inline as it is particularly 616// well suited to constant folding when called on a specific Type subclass. 618assert(Ty->
isSized() &&
"Cannot getTypeInfo() on a type that is unsized!");
631// Get the layout annotation... which is lazily created on demand. 647// In memory objects this is always aligned to a higher boundary, but 648// only 80 bits contain information. 655uint64_t MinBits = EltCnt.getKnownMinValue() *
657returnTypeSize(MinBits, EltCnt.isScalable());
660Type *LayoutTy = cast<TargetExtType>(Ty)->getLayoutType();
668}
// end namespace llvm 670#endif// LLVM_IR_DATALAYOUT_H This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Given that RA is a live value
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
This header defines support for implementing classes that have some trailing object (or arrays of obj...
Class for arbitrary precision integers.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Class to represent array types.
uint64_t getNumElements() const
Type * getElementType() const
A parsed version of the target data layout string in and methods for querying it.
unsigned getProgramAddressSpace() const
bool typeSizeEqualsStoreSize(Type *Ty) const
Returns true if no extra padding bits are needed when storing the specified type.
bool hasLinkerPrivateGlobalPrefix() const
static const char * getManglingComponent(const Triple &T)
StringRef getLinkerPrivateGlobalPrefix() const
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
bool isNonIntegralPointerType(Type *Ty) const
@ MultipleOfFunctionAlign
The function pointer alignment is a multiple of the function alignment.
@ Independent
The function pointer alignment is independent of the function alignment.
SmallVector< APInt > getGEPIndicesForOffset(Type *&ElemTy, APInt &Offset) const
Get GEP indices to access Offset inside ElemTy.
bool isLittleEndian() const
Layout endianness...
bool isDefault() const
Test if the DataLayout was constructed from an empty string.
TypeSize getTypeStoreSizeInBits(Type *Ty) const
Returns the maximum number of bits that may be overwritten by storing the specified type; always a mu...
unsigned getLargestLegalIntTypeSizeInBits() const
Returns the size of largest legal integer type size, or 0 if none are set.
bool isLegalInteger(uint64_t Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU.
unsigned getDefaultGlobalsAddressSpace() const
FunctionPtrAlignType getFunctionPtrAlignType() const
Return the type of function pointer alignment.
Align getABIIntegerTypeAlignment(unsigned BitWidth) const
Returns the minimum ABI-required alignment for an integer type of the specified bitwidth.
bool doNotMangleLeadingQuestionMark() const
Returns true if symbols with leading question marks should not receive IR mangling.
unsigned getIndexSize(unsigned AS) const
rounded up to a whole number of bytes.
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
DataLayout()
Constructs a DataLayout with default values.
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
unsigned getPointerTypeSize(Type *Ty) const
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
bool isNonIntegralAddressSpace(unsigned AddrSpace) const
bool isIllegalInteger(uint64_t Width) const
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
PointerType * getAllocaPtrType(LLVMContext &Ctx) const
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
MaybeAlign getStackAlignment() const
Returns the natural stack alignment, or MaybeAlign() if one wasn't specified.
unsigned getAllocaAddrSpace() const
DataLayout & operator=(const DataLayout &Other)
IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
std::optional< APInt > getGEPIndexForOffset(Type *&ElemTy, APInt &Offset) const
Get single GEP index to access Offset inside ElemTy.
Type * getSmallestLegalIntType(LLVMContext &C, unsigned Width=0) const
Returns the smallest integer type with size at least as big as Width bits.
Align getPreferredAlign(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
bool fitsInLegalInteger(unsigned Width) const
Returns true if the specified type fits in a native integer type supported by the CPU.
bool hasMicrosoftFastStdCallMangling() const
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size in bytes, rounded up to a whole number of bytes.
bool isNonIntegralPointerType(PointerType *PT) const
Align getPointerPrefAlignment(unsigned AS=0) const
Return target's alignment for stack-based pointers FIXME: The defaults need to be removed once all of...
unsigned getIndexSizeInBits(unsigned AS) const
Size in bits of index used for address calculation in getelementptr.
Type * getLargestLegalIntType(LLVMContext &C) const
Returns the largest legal integer type, or null if none are set.
StringRef getPrivateGlobalPrefix() const
MaybeAlign getFunctionPtrAlign() const
Returns the alignment of function pointers, which may or may not be related to the alignment of funct...
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
bool operator!=(const DataLayout &Other) const
DataLayout(const DataLayout &DL)
TypeSize getTypeAllocSizeInBits(Type *Ty) const
Returns the offset in bits between successive objects of the specified type, including alignment padd...
char getGlobalPrefix() const
int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef< Value * > Indices) const
Returns the offset from the beginning of the type for the specified indices.
const std::string & getStringRepresentation() const
Returns the string representation of the DataLayout.
Align getValueOrABITypeAlignment(MaybeAlign Alignment, Type *Ty) const
Helper function to return Alignment if it's set or the result of getABITypeAlign(Ty),...
Align getPointerABIAlignment(unsigned AS) const
Layout pointer alignment.
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
SmallVector< unsigned, 8 > getNonIntegralAddressSpaces() const
Return the address spaces containing non-integral pointers.
Tagged union holding either a T or a Error.
Class to represent integer types.
This is an important class for using LLVM in a threaded context.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Class to represent pointers.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
TypeSize getSizeInBytes() const
bool hasPadding() const
Returns whether the struct has padding or not between its fields.
MutableArrayRef< TypeSize > getMemberOffsets()
unsigned getElementContainingOffset(uint64_t FixedOffset) const
Given a valid byte offset into the structure, returns the structure index that contains it.
TypeSize getElementOffset(unsigned Idx) const
ArrayRef< TypeSize > getMemberOffsets() const
TypeSize getSizeInBits() const
TypeSize getElementOffsetInBits(unsigned Idx) const
Align getAlignment() const
Class to represent struct types.
See the file comment for details on the usage of the TrailingObjects type.
Triple - Helper class for working with autoconf configuration names.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
The instances of the Type class are immutable: once they are created, they are never changed.
unsigned getIntegerBitWidth() const
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
@ X86_AMXTyID
AMX vectors (8192 bits, X86 specific)
@ HalfTyID
16-bit floating point type
@ TargetExtTyID
Target extension type.
@ ScalableVectorTyID
Scalable SIMD vector type.
@ FloatTyID
32-bit floating point type
@ IntegerTyID
Arbitrary bit width integers.
@ FixedVectorTyID
Fixed width SIMD vector type.
@ BFloatTyID
16-bit floating point type (7-bit significand)
@ DoubleTyID
64-bit floating point type
@ X86_FP80TyID
80-bit floating point type (X87)
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
@ FP128TyID
128-bit floating point type (112-bit significand)
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
TypeID getTypeID() const
Return the type id for the type.
Base class of all SIMD vector types.
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
struct LLVMOpaqueTargetData * LLVMTargetDataRef
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
Attribute unwrap(LLVMAttributeRef Attr)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
constexpr unsigned BitWidth
LLVMAttributeRef wrap(Attribute Attr)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Pointer type specification.
bool operator==(const PointerSpec &Other) const
bool IsNonIntegral
Pointers in this address space don't have a well-defined bitwise representation (e....
Primitive type specification.
bool operator==(const PrimitiveSpec &Other) const
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.