49 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);
57 const Align TyAlign =
ST->isPacked() ?
Align(1) :
DL.getABITypeAlign(Ty);
62 StructSize =
alignTo(StructSize, TyAlign);
66 StructAlignment = std::max(TyAlign, StructAlignment);
70 StructSize +=
DL.getTypeAllocSize(Ty).getFixedValue();
75 if (!
isAligned(StructAlignment, StructSize)) {
77 StructSize =
alignTo(StructSize, StructAlignment);
86 assert(
SI != MemberOffsets.
begin() &&
"Offset not in structure type!");
91 "Upper bound didn't work!");
98 return SI - MemberOffsets.
begin();
150 if (
T.isOSBinFormatGOFF())
152 if (
T.isOSBinFormatMachO())
154 if (
T.isOSWindows() &&
T.isOSBinFormatCOFF())
156 if (
T.isOSBinFormatXCOFF())
181 StackNaturalAlign.reset();
182 ProgramAddrSpace = 0;
183 DefaultGlobalsAddrSpace = 0;
184 FunctionPtrAlign.reset();
186 ManglingMode = MM_None;
187 NonIntegralAddressSpaces.
clear();
192 if (
Error Err = setAlignment(Kind, Layout.ABIAlign, Layout.PrefAlign,
193 Layout.TypeBitWidth))
196 if (
Error Err = setPointerAlignmentInBits(0,
Align(8),
Align(8), 64, 64))
199 if (
Error Err = parseSpecifier(Desc))
205 if (
Error Err = Layout.parseSpecifier(LayoutDescription))
206 return std::move(Err);
216 std::pair<StringRef, StringRef> &Split) {
217 assert(!Str.empty() &&
"parse error, string can't be empty here");
218 Split = Str.split(Separator);
219 if (Split.second.empty() && Split.first != Str)
220 return reportError(
"Trailing separator in datalayout string");
221 if (!Split.second.empty() && Split.first.empty())
222 return reportError(
"Expected token before separator in datalayout string");
228 bool error = R.getAsInteger(10, Result); (void)
error;
230 return reportError(
"not a number, or does not fit in an unsigned int");
236template <
typename IntTy>
238 if (
Error Err = getInt<IntTy>(R, Result))
241 return reportError(
"number of bits must be a byte width multiple");
249 if (!isUInt<24>(AddrSpace))
250 return reportError(
"Invalid address space, must be a 24-bit integer");
255 StringRepresentation = std::string(Desc);
256 while (!Desc.
empty()) {
258 std::pair<StringRef, StringRef>
Split;
280 return reportError(
"Address space 0 can never be non-integral");
282 }
while (!Rest.
empty());
287 char Specifier = Tok.
front();
303 unsigned AddrSpace = 0;
307 if (!isUInt<24>(AddrSpace))
308 return reportError(
"Invalid address space, must be a 24-bit integer");
313 "Missing size specification for pointer in datalayout string");
316 unsigned PointerMemSize;
320 return reportError(
"Invalid pointer size of 0 bytes");
325 "Missing alignment specification for pointer in datalayout string");
328 unsigned PointerABIAlign;
332 return reportError(
"Pointer ABI alignment must be a power of 2");
336 unsigned IndexSize = PointerMemSize;
339 unsigned PointerPrefAlign = PointerABIAlign;
347 "Pointer preferred alignment must be a power of 2");
356 return reportError(
"Invalid index size of 0 bytes");
359 if (
Error Err = setPointerAlignmentInBits(
386 "Sized aggregate specification in datalayout string");
391 "Missing alignment specification in datalayout string");
399 "ABI alignment specification must be >0 for non-aggregate types");
401 if (!isUInt<16>(ABIAlign))
402 return reportError(
"Invalid ABI alignment, must be a 16bit integer");
404 return reportError(
"Invalid ABI alignment, must be a power of 2");
407 "Invalid ABI alignment, i8 must be naturally aligned");
410 unsigned PrefAlign = ABIAlign;
418 if (!isUInt<16>(PrefAlign))
420 "Invalid preferred alignment, must be a 16bit integer");
422 return reportError(
"Invalid preferred alignment, must be a power of 2");
437 "Zero width native integer type in datalayout string");
450 return reportError(
"Alignment is neither 0 nor a power of 2");
455 switch (Tok.
front()) {
463 return reportError(
"Unknown function pointer alignment type in "
464 "datalayout string");
471 return reportError(
"Alignment is neither 0 nor a power of 2");
492 return reportError(
"Unexpected trailing characters after mangling "
493 "specifier in datalayout string");
495 return reportError(
"Expected mangling specifier in datalayout string");
497 return reportError(
"Unknown mangling specifier in datalayout string");
500 return reportError(
"Unknown mangling in datalayout string");
502 ManglingMode = MM_ELF;
505 ManglingMode = MM_GOFF;
508 ManglingMode = MM_MachO;
511 ManglingMode = MM_Mips;
514 ManglingMode = MM_WinCOFF;
517 ManglingMode = MM_WinCOFFX86;
520 ManglingMode = MM_XCOFF;
525 return reportError(
"Unknown specifier in datalayout string");
540 bool Ret = BigEndian ==
Other.BigEndian &&
541 AllocaAddrSpace ==
Other.AllocaAddrSpace &&
542 StackNaturalAlign ==
Other.StackNaturalAlign &&
543 ProgramAddrSpace ==
Other.ProgramAddrSpace &&
544 DefaultGlobalsAddrSpace ==
Other.DefaultGlobalsAddrSpace &&
545 FunctionPtrAlign ==
Other.FunctionPtrAlign &&
546 TheFunctionPtrAlignType ==
Other.TheFunctionPtrAlignType &&
547 ManglingMode ==
Other.ManglingMode &&
548 LegalIntWidths ==
Other.LegalIntWidths &&
549 IntAlignments ==
Other.IntAlignments &&
550 FloatAlignments ==
Other.FloatAlignments &&
551 VectorAlignments ==
Other.VectorAlignments &&
552 StructAlignment ==
Other.StructAlignment &&
553 Pointers ==
Other.Pointers;
572 assert(
Log2(ABIAlign) < 16 &&
Log2(PrefAlign) < 16 &&
"Alignment too big");
574 return reportError(
"Invalid bit width, must be a 24-bit integer");
575 if (PrefAlign < ABIAlign)
577 "Preferred alignment cannot be less than the ABI alignment");
582 StructAlignment.
ABIAlign = ABIAlign;
586 Alignments = &IntAlignments;
589 Alignments = &FloatAlignments;
592 Alignments = &VectorAlignments;
601 I->ABIAlign = ABIAlign;
602 I->PrefAlign = PrefAlign;
629 if (PrefAlign < ABIAlign)
631 "Preferred alignment cannot be less than the ABI alignment");
637 if (
I == Pointers.
end() ||
I->AddressSpace != AddrSpace) {
640 TypeBitWidth, IndexBitWidth));
642 I->ABIAlign = ABIAlign;
643 I->PrefAlign = PrefAlign;
644 I->TypeBitWidth = TypeBitWidth;
645 I->IndexBitWidth = IndexBitWidth;
651 bool abi_or_pref)
const {
656 if (
I == IntAlignments.
end())
658 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
663class StructLayoutMap {
665 LayoutInfoTy LayoutInfo;
670 for (
const auto &
I : LayoutInfo) {
672 Value->~StructLayout();
678 return LayoutInfo[STy];
684void DataLayout::clear() {
685 LegalIntWidths.
clear();
686 IntAlignments.
clear();
687 FloatAlignments.
clear();
688 VectorAlignments.
clear();
690 delete static_cast<StructLayoutMap *
>(LayoutMap);
700 LayoutMap =
new StructLayoutMap();
702 StructLayoutMap *STM =
static_cast<StructLayoutMap*
>(LayoutMap);
721 return getPointerAlignElem(AS).
ABIAlign;
725 return getPointerAlignElem(AS).
PrefAlign;
729 return divideCeil(getPointerAlignElem(AS).TypeBitWidth, 8);
733 unsigned MaxIndexSize = 0;
734 for (
auto &
P : Pointers)
736 std::max(MaxIndexSize, (
unsigned)
divideCeil(
P.TypeBitWidth, 8));
743 "This should only be called with a pointer or pointer vector type");
749 return divideCeil(getPointerAlignElem(AS).IndexBitWidth, 8);
754 "This should only be called with a pointer or pointer vector type");
767Align DataLayout::getAlignment(
Type *Ty,
bool abi_or_pref)
const {
768 assert(Ty->
isSized() &&
"Cannot getTypeInfo() on a type that is unsized!");
774 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
779 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
783 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
805 if (
I != FloatAlignments.
end() &&
I->TypeBitWidth ==
BitWidth)
806 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
821 if (
I != VectorAlignments.
end() &&
I->TypeBitWidth ==
BitWidth)
822 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
835 Type *LayoutTy = cast<TargetExtType>(Ty)->getLayoutType();
836 return getAlignment(LayoutTy, abi_or_pref);
849 return getAlignment(Ty,
true);
858 return getAlignment(Ty,
false);
868 "Expected a pointer or pointer vector type.");
871 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
877 for (
unsigned LegalIntWidth : LegalIntWidths)
878 if (Width <= LegalIntWidth)
884 auto Max = std::max_element(LegalIntWidths.
begin(), LegalIntWidths.
end());
885 return Max != LegalIntWidths.
end() ? *Max : 0;
890 "Expected a pointer or pointer vector type.");
893 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
905 for (; GTI != GTE; ++GTI) {
908 assert(
Idx->getType()->isIntegerTy(32) &&
"Illegal struct idx");
909 unsigned FieldNo = cast<ConstantInt>(
Idx)->getZExtValue();
918 if (int64_t arrayIdx = cast<ConstantInt>(
Idx)->getSExtValue())
938 if (
Offset.isNegative()) {
942 assert(
Offset.isNonNegative() &&
"Remaining offset shouldn't be negative");
949 if (
auto *ArrTy = dyn_cast<ArrayType>(ElemTy)) {
950 ElemTy = ArrTy->getElementType();
954 if (isa<VectorType>(ElemTy)) {
961 if (
auto *STy = dyn_cast<StructType>(ElemTy)) {
969 ElemTy = STy->getElementType(
Index);
1010 if (*GVAlignment >= Alignment)
1011 Alignment = *GVAlignment;
1020 if (Alignment <
Align(16)) {
1024 Alignment =
Align(16);
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static Error reportError(StringRef Message)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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.
uint64_t getPrefTypeAlignment(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
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).
uint64_t getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
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.
Type * getIndexType(Type *PtrTy) const
Returns the type of a GEP index.
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...
MutableArrayRef< uint64_t > getMemberOffsets()
uint64_t getSizeInBytes() const
uint64_t getElementOffset(unsigned Idx) const
Align getAlignment() const
unsigned getElementContainingOffset(uint64_t Offset) const
Given a valid byte offset into the structure, returns the structure index that contains it.
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...
The instances of the Type class are immutable: once they are created, they are never changed.
unsigned getIntegerBitWidth() const
@ X86_MMXTyID
MMX vectors (64 bits, X86 specific)
@ 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.
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
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.
StructType * getStructTypeOrNull() const
Value * getOperand() const
Type * getIndexedType() 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.
uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator)
Returns the integer ceil(Numerator / Denominator).
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)
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.
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)
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.
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.
uint64_t value() const
This is a hole in the type system and should not be abused.
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.