49 : StructSize(
TypeSize::getFixed(0)) {
50 assert(!
ST->isOpaque() &&
"Cannot get layout of opaque structs");
52 NumElements =
ST->getNumElements();
55 for (
unsigned i = 0, e = NumElements; i !=
e; ++i) {
56 Type *Ty =
ST->getElementType(i);
60 const Align TyAlign =
ST->isPacked() ?
Align(1) :
DL.getABITypeAlign(Ty);
69 if (!StructSize.isScalable() && !
isAligned(TyAlign, StructSize)) {
75 StructAlignment = std::max(TyAlign, StructAlignment);
77 getMemberOffsets()[i] = StructSize;
79 StructSize +=
DL.getTypeAllocSize(Ty);
84 if (!StructSize.isScalable() && !
isAligned(StructAlignment, StructSize)) {
94 "Cannot get element at offset for structure containing scalable "
103 assert(SI != MemberOffsets.
begin() &&
"Offset not in structure type!");
108 (SI + 1 == MemberOffsets.
end() ||
110 "Upper bound didn't work!");
117 return SI - MemberOffsets.
begin();
122class StructLayoutMap {
124 LayoutInfoTy LayoutInfo;
129 for (
const auto &
I : LayoutInfo) {
131 Value->~StructLayout();
152 ABIAlign ==
Other.ABIAlign && PrefAlign ==
Other.PrefAlign &&
153 IndexBitWidth ==
Other.IndexBitWidth &&
154 IsNonIntegral ==
Other.IsNonIntegral;
159struct LessPrimitiveBitWidth {
161 unsigned RHSBitWidth)
const {
162 return LHS.BitWidth < RHSBitWidth;
167struct LessPointerAddrSpace {
169 unsigned RHSAddrSpace)
const {
170 return LHS.AddrSpace < RHSAddrSpace;
176 if (
T.isOSBinFormatGOFF())
178 if (
T.isOSBinFormatMachO())
180 if ((
T.isOSWindows() ||
T.isUEFI()) &&
T.isOSBinFormatCOFF())
182 if (
T.isOSBinFormatXCOFF())
190 {1, Align::Constant<1>(), Align::Constant<1>()},
191 {8, Align::Constant<1>(), Align::Constant<1>()},
192 {16, Align::Constant<2>(), Align::Constant<2>()},
193 {32, Align::Constant<4>(), Align::Constant<4>()},
194 {64, Align::Constant<4>(), Align::Constant<8>()},
197 {16, Align::Constant<2>(), Align::Constant<2>()},
198 {32, Align::Constant<4>(), Align::Constant<4>()},
199 {64, Align::Constant<8>(), Align::Constant<8>()},
200 {128, Align::Constant<16>(), Align::Constant<16>()},
203 {64, Align::Constant<8>(), Align::Constant<8>()},
204 {128, Align::Constant<16>(), Align::Constant<16>()},
210 {0, 64, Align::Constant<8>(), Align::Constant<8>(), 64,
false},
220 if (
Error Err = parseLayoutString(LayoutString))
225 delete static_cast<StructLayoutMap *
>(LayoutMap);
227 StringRepresentation =
Other.StringRepresentation;
228 BigEndian =
Other.BigEndian;
229 AllocaAddrSpace =
Other.AllocaAddrSpace;
230 ProgramAddrSpace =
Other.ProgramAddrSpace;
231 DefaultGlobalsAddrSpace =
Other.DefaultGlobalsAddrSpace;
232 StackNaturalAlign =
Other.StackNaturalAlign;
233 FunctionPtrAlign =
Other.FunctionPtrAlign;
234 TheFunctionPtrAlignType =
Other.TheFunctionPtrAlignType;
235 ManglingMode =
Other.ManglingMode;
236 LegalIntWidths =
Other.LegalIntWidths;
237 IntSpecs =
Other.IntSpecs;
238 FloatSpecs =
Other.FloatSpecs;
239 VectorSpecs =
Other.VectorSpecs;
240 PointerSpecs =
Other.PointerSpecs;
241 StructABIAlignment =
Other.StructABIAlignment;
242 StructPrefAlignment =
Other.StructPrefAlignment;
248 return BigEndian ==
Other.BigEndian &&
249 AllocaAddrSpace ==
Other.AllocaAddrSpace &&
250 ProgramAddrSpace ==
Other.ProgramAddrSpace &&
251 DefaultGlobalsAddrSpace ==
Other.DefaultGlobalsAddrSpace &&
252 StackNaturalAlign ==
Other.StackNaturalAlign &&
253 FunctionPtrAlign ==
Other.FunctionPtrAlign &&
254 TheFunctionPtrAlignType ==
Other.TheFunctionPtrAlignType &&
255 ManglingMode ==
Other.ManglingMode &&
256 LegalIntWidths ==
Other.LegalIntWidths && IntSpecs ==
Other.IntSpecs &&
257 FloatSpecs ==
Other.FloatSpecs && VectorSpecs ==
Other.VectorSpecs &&
258 PointerSpecs ==
Other.PointerSpecs &&
259 StructABIAlignment ==
Other.StructABIAlignment &&
260 StructPrefAlignment ==
Other.StructPrefAlignment;
265 if (
Error Err = Layout.parseLayoutString(LayoutString))
266 return std::move(Err);
280 if (!to_integer(Str, AddrSpace, 10) || !isUInt<24>(AddrSpace))
310 bool AllowZero =
false) {
315 if (!to_integer(Str,
Value, 10) || !isUInt<16>(
Value))
321 Alignment =
Align(1);
325 constexpr unsigned ByteWidth = 8;
328 Name +
" alignment must be a power of two times the byte width");
338 assert(Specifier ==
'i' || Specifier ==
'f' || Specifier ==
'v');
339 Spec.drop_front().split(Components,
':');
341 if (Components.
size() < 2 || Components.
size() > 3)
354 if (Specifier ==
'i' &&
BitWidth == 8 && ABIAlign != 1)
358 Align PrefAlign = ABIAlign;
359 if (Components.
size() > 2)
363 if (PrefAlign < ABIAlign)
365 "preferred alignment cannot be less than the ABI alignment");
367 setPrimitiveSpec(Specifier,
BitWidth, ABIAlign, PrefAlign);
375 Spec.drop_front().split(Components,
':');
377 if (Components.
size() < 2 || Components.
size() > 3)
383 if (!Components[0].empty()) {
396 Align PrefAlign = ABIAlign;
397 if (Components.
size() > 2)
401 if (PrefAlign < ABIAlign)
403 "preferred alignment cannot be less than the ABI alignment");
405 StructABIAlignment = ABIAlign;
406 StructPrefAlignment = PrefAlign;
414 Spec.drop_front().split(Components,
':');
416 if (Components.
size() < 3 || Components.
size() > 5)
420 unsigned AddrSpace = 0;
421 if (!Components[0].empty())
437 Align PrefAlign = ABIAlign;
438 if (Components.
size() > 3)
442 if (PrefAlign < ABIAlign)
444 "preferred alignment cannot be less than the ABI alignment");
448 if (Components.
size() > 4)
449 if (
Error Err =
parseSize(Components[4], IndexBitWidth,
"index size"))
454 "index size cannot be larger than the pointer size");
456 setPointerSpec(AddrSpace,
BitWidth, ABIAlign, PrefAlign, IndexBitWidth,
461Error DataLayout::parseSpecification(
464 if (
Spec.starts_with(
"ni")) {
478 NonIntegralAddressSpaces.
push_back(AddrSpace);
484 assert(!
Spec.empty() &&
"Empty specification is handled by the caller");
487 if (Specifier ==
'i' || Specifier ==
'f' || Specifier ==
'v')
488 return parsePrimitiveSpec(
Spec);
490 if (Specifier ==
'a')
491 return parseAggregateSpec(
Spec);
493 if (Specifier ==
'p')
494 return parsePointerSpec(
Spec);
506 "malformed specification, must be just 'e' or 'E'");
525 StackNaturalAlign = Alignment;
548 FunctionPtrAlign = Alignment;
581 ManglingMode = MM_ELF;
584 ManglingMode = MM_GOFF;
587 ManglingMode = MM_MachO;
590 ManglingMode = MM_Mips;
593 ManglingMode = MM_WinCOFF;
596 ManglingMode = MM_WinCOFFX86;
599 ManglingMode = MM_XCOFF;
611 StringRepresentation = std::string(LayoutString);
613 if (LayoutString.
empty())
622 if (
Error Err = parseSpecification(
Spec, NonIntegralAddressSpaces))
628 for (
unsigned AS : NonIntegralAddressSpaces) {
631 const PointerSpec &PS = getPointerSpec(AS);
632 setPointerSpec(AS, PS.BitWidth, PS.ABIAlign, PS.PrefAlign, PS.IndexBitWidth,
652 Specs = &VectorSpecs;
659 I->ABIAlign = ABIAlign;
660 I->PrefAlign = PrefAlign;
668DataLayout::getPointerSpec(
uint32_t AddrSpace)
const {
669 if (AddrSpace != 0) {
670 auto I =
lower_bound(PointerSpecs, AddrSpace, LessPointerAddrSpace());
671 if (
I != PointerSpecs.end() &&
I->AddrSpace == AddrSpace)
675 assert(PointerSpecs[0].AddrSpace == 0);
676 return PointerSpecs[0];
681 uint32_t IndexBitWidth,
bool IsNonIntegral) {
682 auto I =
lower_bound(PointerSpecs, AddrSpace, LessPointerAddrSpace());
683 if (
I == PointerSpecs.end() ||
I->AddrSpace != AddrSpace) {
684 PointerSpecs.insert(
I, PointerSpec{AddrSpace,
BitWidth, ABIAlign, PrefAlign,
685 IndexBitWidth, IsNonIntegral});
688 I->ABIAlign = ABIAlign;
689 I->PrefAlign = PrefAlign;
690 I->IndexBitWidth = IndexBitWidth;
691 I->IsNonIntegral = IsNonIntegral;
696 bool abi_or_pref)
const {
701 if (
I == IntSpecs.end())
703 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
710 LayoutMap =
new StructLayoutMap();
712 StructLayoutMap *STM =
static_cast<StructLayoutMap*
>(LayoutMap);
744 "This should only be called with a pointer or pointer vector type");
750 return divideCeil(getPointerSpec(AS).IndexBitWidth, 8);
755 "This should only be called with a pointer or pointer vector type");
768Align DataLayout::getAlignment(
Type *Ty,
bool abi_or_pref)
const {
769 assert(Ty->
isSized() &&
"Cannot getTypeInfo() on a type that is unsized!");
775 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
780 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
784 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
789 const Align Align = abi_or_pref ? StructABIAlignment : StructPrefAlignment;
805 if (
I != FloatSpecs.end() &&
I->BitWidth ==
BitWidth)
806 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
820 if (
I != VectorSpecs.end() &&
I->BitWidth ==
BitWidth)
821 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
834 Type *LayoutTy = cast<TargetExtType>(Ty)->getLayoutType();
835 return getAlignment(LayoutTy, abi_or_pref);
843 return getAlignment(Ty,
true);
847 return getAlignment(Ty,
false);
857 "Expected a pointer or pointer vector type.");
860 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
866 for (
unsigned LegalIntWidth : LegalIntWidths)
867 if (Width <= LegalIntWidth)
874 return Max != LegalIntWidths.
end() ? *Max : 0;
884 "Expected a pointer or pointer vector type.");
887 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
899 for (; GTI != GTE; ++GTI) {
902 assert(
Idx->getType()->isIntegerTy(32) &&
"Illegal struct idx");
903 unsigned FieldNo = cast<ConstantInt>(
Idx)->getZExtValue();
911 if (int64_t ArrayIdx = cast<ConstantInt>(
Idx)->getSExtValue())
930 Offset -= Index * ElemSize;
931 if (
Offset.isNegative()) {
935 assert(
Offset.isNonNegative() &&
"Remaining offset shouldn't be negative");
942 if (
auto *ArrTy = dyn_cast<ArrayType>(ElemTy)) {
943 ElemTy = ArrTy->getElementType();
947 if (isa<VectorType>(ElemTy)) {
954 if (
auto *STy = dyn_cast<StructType>(ElemTy)) {
962 ElemTy = STy->getElementType(Index);
963 return APInt(32, Index);
1003 if (*GVAlignment >= Alignment)
1004 Alignment = *GVAlignment;
1013 if (Alignment <
Align(16)) {
1017 Alignment =
Align(16);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Error parseSize(StringRef Str, unsigned &BitWidth, StringRef Name="size")
Attempts to parse a size component of a specification.
static APInt getElementIndex(TypeSize ElemSize, APInt &Offset)
static Error parseAddrSpace(StringRef Str, unsigned &AddrSpace)
Attempts to parse an address space component of a specification.
static Error createSpecFormatError(Twine Format)
static Error parseAlignment(StringRef Str, Align &Alignment, StringRef Name, bool AllowZero=false)
Attempts to parse an alignment component of a specification.
constexpr DataLayout::PrimitiveSpec DefaultFloatSpecs[]
constexpr DataLayout::PrimitiveSpec DefaultVectorSpecs[]
constexpr DataLayout::PointerSpec DefaultPointerSpecs[]
constexpr DataLayout::PrimitiveSpec DefaultIntSpecs[]
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
This file defines the DenseMap class.
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
static unsigned getAddressSpace(const Value *V, unsigned MaxLookup)
Class for arbitrary precision integers.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
A parsed version of the target data layout string in and methods for querying it.
static LLVM_ABI const char * getManglingComponent(const Triple &T)
unsigned getPointerSizeInBits(unsigned AS=0) const
The size in bits of the pointer representation in a given address space.
@ MultipleOfFunctionAlign
The function pointer alignment is a multiple of the function alignment.
@ Independent
The function pointer alignment is independent of the function alignment.
LLVM_ABI SmallVector< APInt > getGEPIndicesForOffset(Type *&ElemTy, APInt &Offset) const
Get GEP indices to access Offset inside ElemTy.
LLVM_ABI unsigned getLargestLegalIntTypeSizeInBits() const
Returns the size of largest legal integer type size, or 0 if none are set.
LLVM_ABI unsigned getIndexSize(unsigned AS) const
The index size in bytes used for address calculation, rounded up to a whole number of bytes.
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_ABI DataLayout()
Constructs a DataLayout with default values.
LLVM_ABI 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.
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
LLVM_ABI DataLayout & operator=(const DataLayout &Other)
LLVM_ABI 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...
LLVM_ABI std::optional< APInt > getGEPIndexForOffset(Type *&ElemTy, APInt &Offset) const
Get single GEP index to access Offset inside ElemTy.
LLVM_ABI Type * getSmallestLegalIntType(LLVMContext &C, unsigned Width=0) const
Returns the smallest integer type with size at least as big as Width bits.
LLVM_ABI Align getPreferredAlign(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
LLVM_ABI unsigned getPointerSize(unsigned AS=0) const
The pointer representation size in bytes, rounded up to a whole number of bytes.
LLVM_ABI 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
The size in bits of indices used for address calculation in getelementptr and for addresses in the gi...
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.
LLVM_ABI bool operator==(const DataLayout &Other) const
LLVM_ABI int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef< Value * > Indices) const
Returns the offset from the beginning of the type for the specified indices.
LLVM_ABI Align getPointerABIAlignment(unsigned AS) const
Layout pointer alignment.
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
static LLVM_ABI Expected< DataLayout > parse(StringRef LayoutString)
Parse a data layout string and return the layout.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
bool hasSection() const
Check if this global has a custom object file section.
Type * getValueType() const
bool hasInitializer() const
Definitions have initializers, declarations don't.
MaybeAlign getAlign() const
Returns the alignment of the given variable.
Class to represent integer types.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
This is an important class for using LLVM in a threaded context.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
iterator insert(iterator I, T &&Elt)
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.
constexpr bool empty() const
empty - Check if the string is empty.
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
constexpr size_t size() const
size - Get the string size.
char front() const
front - Get the first character in the string.
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
TypeSize getSizeInBytes() const
MutableArrayRef< TypeSize > getMemberOffsets()
LLVM_ABI 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
Align getAlignment() const
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static constexpr TypeSize getFixed(ScalarTy ExactSize)
static constexpr TypeSize getScalable(ScalarTy MinimumSize)
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
@ 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)
LLVM_ABI bool isScalableTy(SmallPtrSetImpl< const Type * > &Visited) const
Return true if this is a type whose size is a known multiple of vscale.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
TypeID getTypeID() const
Return the type id for the type.
LLVM_ABI unsigned getIntegerBitWidth() const
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
LLVM Value Representation.
Base class of all SIMD vector types.
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
constexpr ScalarTy getFixedValue() const
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
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.
static constexpr bool isKnownGT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
StructType * getStructTypeOrNull() const
TypeSize getSequentialElementStride(const DataLayout &DL) const
Value * getOperand() const
#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.
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
gep_type_iterator gep_type_end(const User *GEP)
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
auto max_element(R &&Range)
Provide wrappers to std::max_element which take ranges instead of having to pass begin/end explicitly...
constexpr unsigned BitWidth
gep_type_iterator gep_type_begin(const User *GEP)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Pointer type specification.
LLVM_ABI bool operator==(const PointerSpec &Other) const
Primitive type specification.
LLVM_ABI bool operator==(const PrimitiveSpec &Other) const
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.