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 "
100 std::upper_bound(MemberOffsets.
begin(), MemberOffsets.
end(),
Offset,
102 return TypeSize::isKnownLT(LHS, RHS);
104 assert(SI != MemberOffsets.
begin() &&
"Offset not in structure type!");
109 (SI + 1 == MemberOffsets.
end() ||
111 "Upper bound didn't work!");
118 return SI - MemberOffsets.
begin();
170 if (
T.isOSBinFormatGOFF())
172 if (
T.isOSBinFormatMachO())
174 if ((
T.isOSWindows() ||
T.isUEFI()) &&
T.isOSBinFormatCOFF())
176 if (
T.isOSBinFormatXCOFF())
201 StackNaturalAlign.reset();
202 ProgramAddrSpace = 0;
203 DefaultGlobalsAddrSpace = 0;
204 FunctionPtrAlign.reset();
206 ManglingMode = MM_None;
207 NonIntegralAddressSpaces.
clear();
212 if (
Error Err = setAlignment(Kind, Layout.ABIAlign, Layout.PrefAlign,
213 Layout.TypeBitWidth))
216 if (
Error Err = setPointerAlignmentInBits(0,
Align(8),
Align(8), 64, 64))
225 if (
Error Err = Layout.parseSpecifier(LayoutDescription))
226 return std::move(Err);
236 std::pair<StringRef, StringRef> &Split) {
237 assert(!Str.empty() &&
"parse error, string can't be empty here");
238 Split = Str.split(Separator);
239 if (Split.second.empty() && Split.first != Str)
240 return reportError(
"Trailing separator in datalayout string");
241 if (!Split.second.empty() && Split.first.empty())
242 return reportError(
"Expected token before separator in datalayout string");
248 bool error = R.getAsInteger(10, Result); (void)
error;
250 return reportError(
"not a number, or does not fit in an unsigned int");
256template <
typename IntTy>
258 if (
Error Err = getInt<IntTy>(R, Result))
261 return reportError(
"number of bits must be a byte width multiple");
269 if (!isUInt<24>(AddrSpace))
270 return reportError(
"Invalid address space, must be a 24-bit integer");
275 StringRepresentation = std::string(
Desc);
276 while (!
Desc.empty()) {
278 std::pair<StringRef, StringRef>
Split;
300 return reportError(
"Address space 0 can never be non-integral");
302 }
while (!Rest.
empty());
307 char Specifier = Tok.
front();
323 unsigned AddrSpace = 0;
327 if (!isUInt<24>(AddrSpace))
328 return reportError(
"Invalid address space, must be a 24-bit integer");
333 "Missing size specification for pointer in datalayout string");
336 unsigned PointerMemSize;
340 return reportError(
"Invalid pointer size of 0 bytes");
345 "Missing alignment specification for pointer in datalayout string");
348 unsigned PointerABIAlign;
352 return reportError(
"Pointer ABI alignment must be a power of 2");
356 unsigned IndexSize = PointerMemSize;
359 unsigned PointerPrefAlign = PointerABIAlign;
367 "Pointer preferred alignment must be a power of 2");
376 return reportError(
"Invalid index size of 0 bytes");
379 if (
Error Err = setPointerAlignmentInBits(
406 "Sized aggregate specification in datalayout string");
411 "Missing alignment specification in datalayout string");
419 "ABI alignment specification must be >0 for non-aggregate types");
421 if (!isUInt<16>(ABIAlign))
422 return reportError(
"Invalid ABI alignment, must be a 16bit integer");
424 return reportError(
"Invalid ABI alignment, must be a power of 2");
427 "Invalid ABI alignment, i8 must be naturally aligned");
430 unsigned PrefAlign = ABIAlign;
438 if (!isUInt<16>(PrefAlign))
440 "Invalid preferred alignment, must be a 16bit integer");
442 return reportError(
"Invalid preferred alignment, must be a power of 2");
457 "Zero width native integer type in datalayout string");
470 return reportError(
"Alignment is neither 0 nor a power of 2");
475 switch (Tok.
front()) {
483 return reportError(
"Unknown function pointer alignment type in "
484 "datalayout string");
491 return reportError(
"Alignment is neither 0 nor a power of 2");
512 return reportError(
"Unexpected trailing characters after mangling "
513 "specifier in datalayout string");
515 return reportError(
"Expected mangling specifier in datalayout string");
517 return reportError(
"Unknown mangling specifier in datalayout string");
520 return reportError(
"Unknown mangling in datalayout string");
522 ManglingMode = MM_ELF;
525 ManglingMode = MM_GOFF;
528 ManglingMode = MM_MachO;
531 ManglingMode = MM_Mips;
534 ManglingMode = MM_WinCOFF;
537 ManglingMode = MM_WinCOFFX86;
540 ManglingMode = MM_XCOFF;
545 return reportError(
"Unknown specifier in datalayout string");
560 bool Ret = BigEndian ==
Other.BigEndian &&
561 AllocaAddrSpace ==
Other.AllocaAddrSpace &&
562 StackNaturalAlign ==
Other.StackNaturalAlign &&
563 ProgramAddrSpace ==
Other.ProgramAddrSpace &&
564 DefaultGlobalsAddrSpace ==
Other.DefaultGlobalsAddrSpace &&
565 FunctionPtrAlign ==
Other.FunctionPtrAlign &&
566 TheFunctionPtrAlignType ==
Other.TheFunctionPtrAlignType &&
567 ManglingMode ==
Other.ManglingMode &&
568 LegalIntWidths ==
Other.LegalIntWidths &&
569 IntAlignments ==
Other.IntAlignments &&
570 FloatAlignments ==
Other.FloatAlignments &&
571 VectorAlignments ==
Other.VectorAlignments &&
572 StructAlignment ==
Other.StructAlignment &&
573 Pointers ==
Other.Pointers;
592 assert(
Log2(ABIAlign) < 16 &&
Log2(PrefAlign) < 16 &&
"Alignment too big");
594 return reportError(
"Invalid bit width, must be a 24-bit integer");
595 if (PrefAlign < ABIAlign)
597 "Preferred alignment cannot be less than the ABI alignment");
602 StructAlignment.
ABIAlign = ABIAlign;
606 Alignments = &IntAlignments;
609 Alignments = &FloatAlignments;
612 Alignments = &VectorAlignments;
621 I->ABIAlign = ABIAlign;
622 I->PrefAlign = PrefAlign;
649 if (PrefAlign < ABIAlign)
651 "Preferred alignment cannot be less than the ABI alignment");
652 if (IndexBitWidth > TypeBitWidth)
653 return reportError(
"Index width cannot be larger than pointer width");
659 if (
I == Pointers.
end() ||
I->AddressSpace != AddrSpace) {
662 TypeBitWidth, IndexBitWidth));
664 I->ABIAlign = ABIAlign;
665 I->PrefAlign = PrefAlign;
666 I->TypeBitWidth = TypeBitWidth;
667 I->IndexBitWidth = IndexBitWidth;
673 bool abi_or_pref)
const {
678 if (
I == IntAlignments.
end())
680 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
685class StructLayoutMap {
687 LayoutInfoTy LayoutInfo;
692 for (
const auto &
I : LayoutInfo) {
694 Value->~StructLayout();
700 return LayoutInfo[STy];
706void DataLayout::clear() {
707 LegalIntWidths.
clear();
708 IntAlignments.
clear();
709 FloatAlignments.
clear();
710 VectorAlignments.
clear();
712 delete static_cast<StructLayoutMap *
>(LayoutMap);
722 LayoutMap =
new StructLayoutMap();
724 StructLayoutMap *STM =
static_cast<StructLayoutMap*
>(LayoutMap);
743 return getPointerAlignElem(AS).
ABIAlign;
747 return getPointerAlignElem(AS).
PrefAlign;
751 return divideCeil(getPointerAlignElem(AS).TypeBitWidth, 8);
755 unsigned MaxIndexSize = 0;
756 for (
auto &
P : Pointers)
758 std::max(MaxIndexSize, (
unsigned)
divideCeil(
P.TypeBitWidth, 8));
765 "This should only be called with a pointer or pointer vector type");
771 return divideCeil(getPointerAlignElem(AS).IndexBitWidth, 8);
776 "This should only be called with a pointer or pointer vector type");
789Align DataLayout::getAlignment(
Type *Ty,
bool abi_or_pref)
const {
790 assert(Ty->
isSized() &&
"Cannot getTypeInfo() on a type that is unsized!");
796 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
801 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
805 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
827 if (
I != FloatAlignments.
end() &&
I->TypeBitWidth ==
BitWidth)
828 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
842 if (
I != VectorAlignments.
end() &&
I->TypeBitWidth ==
BitWidth)
843 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
856 Type *LayoutTy = cast<TargetExtType>(Ty)->getLayoutType();
857 return getAlignment(LayoutTy, abi_or_pref);
865 return getAlignment(Ty,
true);
869 return getAlignment(Ty,
false);
879 "Expected a pointer or pointer vector type.");
882 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
888 for (
unsigned LegalIntWidth : LegalIntWidths)
889 if (Width <= LegalIntWidth)
896 return Max != LegalIntWidths.
end() ? *Max : 0;
906 "Expected a pointer or pointer vector type.");
909 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
921 for (; GTI != GTE; ++GTI) {
924 assert(
Idx->getType()->isIntegerTy(32) &&
"Illegal struct idx");
925 unsigned FieldNo = cast<ConstantInt>(
Idx)->getZExtValue();
933 if (int64_t ArrayIdx = cast<ConstantInt>(
Idx)->getSExtValue())
953 if (
Offset.isNegative()) {
957 assert(
Offset.isNonNegative() &&
"Remaining offset shouldn't be negative");
964 if (
auto *ArrTy = dyn_cast<ArrayType>(ElemTy)) {
965 ElemTy = ArrTy->getElementType();
969 if (isa<VectorType>(ElemTy)) {
976 if (
auto *STy = dyn_cast<StructType>(ElemTy)) {
984 ElemTy = STy->getElementType(
Index);
1014 return *GVAlignment;
1025 if (*GVAlignment >= Alignment)
1026 Alignment = *GVAlignment;
1035 if (Alignment <
Align(16)) {
1039 Alignment =
Align(16);
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static Error reportError(StringRef Message)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Error getInt(StringRef R, IntTy &Result)
Get an unsigned integer, including error checks.
static APInt getElementIndex(TypeSize ElemSize, APInt &Offset)
static Error getIntInBytes(StringRef R, IntTy &Result)
Get an unsigned integer representing the number of bits and convert it into bytes.
static SmallVectorImpl< LayoutAlignElem >::const_iterator findAlignmentLowerBound(const SmallVectorImpl< LayoutAlignElem > &Alignments, uint32_t BitWidth)
static Error getAddrSpace(StringRef R, unsigned &AddrSpace)
static Error split(StringRef Str, char Separator, std::pair< StringRef, StringRef > &Split)
Checked version of split, to ensure mandatory subparts.
static const std::pair< AlignTypeEnum, LayoutAlignElem > DefaultAlignments[]
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'.
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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 const char * getManglingComponent(const Triple &T)
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
unsigned getMaxIndexSize() const
Returns the maximum index size over all address spaces.
@ 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.
unsigned getLargestLegalIntTypeSizeInBits() const
Returns the size of largest legal integer type size, or 0 if none are set.
void init(const Module *M)
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(StringRef LayoutDescription)
Constructs a DataLayout from a specification string. See reset().
static Expected< DataLayout > parse(StringRef LayoutDescription)
Parse a data layout string and return the layout.
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.
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
void reset(StringRef LayoutDescription)
Parse a data layout string (with fallback to default values).
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.
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size in bytes, rounded up to a whole number of bytes.
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.
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
int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef< Value * > Indices) const
Returns the offset from the beginning of the type for the specified indices.
Align getPointerABIAlignment(unsigned AS) const
Layout pointer alignment.
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
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.
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
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.
Class to represent integer types.
static 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.
A Module instance is used to store all the information related to an LLVM module.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
typename SuperClass::const_iterator const_iterator
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 StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr bool empty() const
empty - Check if the string is empty.
constexpr size_t size() const
size - Get the string size.
char front() const
front - Get the first character in the string.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
TypeSize getSizeInBytes() const
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
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.
unsigned getIntegerBitWidth() const
@ 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.
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.
bool isScalableTy() const
Return true if this is a type whose size is a known multiple of vscale.
TypeID getTypeID() const
Return the type id for the type.
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 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)
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 isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
gep_type_iterator gep_type_end(const User *GEP)
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.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
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)
constexpr unsigned BitWidth
gep_type_iterator gep_type_begin(const User *GEP)
Align assumeAligned(uint64_t Value)
Treats the value 0 as a 1, so Align is always at least 1.
unsigned Log2(Align A)
Returns the log2 of the alignment.
AlignTypeEnum
Enum used to categorize the alignment types stored by LayoutAlignElem.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Description of the encoding of one expression Op.
Layout alignment element.
static LayoutAlignElem get(Align ABIAlign, Align PrefAlign, uint32_t BitWidth)
bool operator==(const LayoutAlignElem &rhs) const
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Layout pointer alignment element.
bool operator==(const PointerAlignElem &rhs) const
static PointerAlignElem getInBits(uint32_t AddressSpace, Align ABIAlign, Align PrefAlign, uint32_t TypeBitWidth, uint32_t IndexBitWidth)
Initializer.