Go to the documentation of this file.
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!");
88 assert(*
SI <= Offset &&
"upper_bound didn't work");
90 (
SI + 1 == MemberOffsets.
end() || *(
SI + 1) > Offset) &&
91 "Upper bound didn't work!");
98 return SI - MemberOffsets.
begin();
107 assert(abi_align <= pref_align &&
"Preferred alignment worse than ABI!");
154 if (
T.isOSBinFormatGOFF())
156 if (
T.isOSBinFormatMachO())
158 if (
T.isOSWindows() &&
T.isOSBinFormatCOFF())
160 if (
T.isOSBinFormatXCOFF())
186 StackNaturalAlign.
reset();
187 ProgramAddrSpace = 0;
188 DefaultGlobalsAddrSpace = 0;
189 FunctionPtrAlign.
reset();
191 ManglingMode = MM_None;
192 NonIntegralAddressSpaces.
clear();
197 E.PrefAlign,
E.TypeBitWidth))
200 if (
Error Err = setPointerAlignmentInBits(0,
Align(8),
Align(8), 64, 64))
203 if (
Error Err = parseSpecifier(Desc))
209 if (
Error Err = Layout.parseSpecifier(LayoutDescription))
220 std::pair<StringRef, StringRef> &Split) {
221 assert(!Str.empty() &&
"parse error, string can't be empty here");
222 Split = Str.split(Separator);
223 if (Split.second.empty() && Split.first != Str)
224 return reportError(
"Trailing separator in datalayout string");
225 if (!Split.second.empty() && Split.first.empty())
226 return reportError(
"Expected token before separator in datalayout string");
232 bool error = R.getAsInteger(10, Result); (void)
error;
234 return reportError(
"not a number, or does not fit in an unsigned int");
240 template <
typename IntTy>
242 if (
Error Err = getInt<IntTy>(R, Result))
245 return reportError(
"number of bits must be a byte width multiple");
253 if (!isUInt<24>(AddrSpace))
254 return reportError(
"Invalid address space, must be a 24-bit integer");
259 StringRepresentation = std::string(Desc);
260 while (!Desc.
empty()) {
262 std::pair<StringRef, StringRef> Split;
268 if (
Error Err = ::
split(Split.first,
':', Split))
284 return reportError(
"Address space 0 can never be non-integral");
285 NonIntegralAddressSpaces.push_back(AS);
286 }
while (!Rest.
empty());
291 char Specifier = Tok.
front();
307 unsigned AddrSpace = 0;
311 if (!isUInt<24>(AddrSpace))
312 return reportError(
"Invalid address space, must be a 24bit integer");
317 "Missing size specification for pointer in datalayout string");
320 unsigned PointerMemSize;
324 return reportError(
"Invalid pointer size of 0 bytes");
329 "Missing alignment specification for pointer in datalayout string");
332 unsigned PointerABIAlign;
336 return reportError(
"Pointer ABI alignment must be a power of 2");
340 unsigned IndexSize = PointerMemSize;
343 unsigned PointerPrefAlign = PointerABIAlign;
351 "Pointer preferred alignment must be a power of 2");
360 return reportError(
"Invalid index size of 0 bytes");
363 if (
Error Err = setPointerAlignmentInBits(
390 "Sized aggregate specification in datalayout string");
395 "Missing alignment specification in datalayout string");
403 "ABI alignment specification must be >0 for non-aggregate types");
406 return reportError(
"Invalid ABI alignment, must be a 16bit integer");
408 return reportError(
"Invalid ABI alignment, must be a power of 2");
411 unsigned PrefAlign = ABIAlign;
421 "Invalid preferred alignment, must be a 16bit integer");
423 return reportError(
"Invalid preferred alignment, must be a power of 2");
438 "Zero width native integer type in datalayout string");
439 LegalIntWidths.push_back(
Width);
451 return reportError(
"Alignment is neither 0 nor a power of 2");
456 switch (Tok.
front()) {
464 return reportError(
"Unknown function pointer alignment type in "
465 "datalayout string");
472 return reportError(
"Alignment is neither 0 nor a power of 2");
493 return reportError(
"Unexpected trailing characters after mangling "
494 "specifier in datalayout string");
496 return reportError(
"Expected mangling specifier in datalayout string");
498 return reportError(
"Unknown mangling specifier in datalayout string");
501 return reportError(
"Unknown mangling in datalayout string");
503 ManglingMode = MM_ELF;
506 ManglingMode = MM_GOFF;
509 ManglingMode = MM_MachO;
512 ManglingMode = MM_Mips;
515 ManglingMode = MM_WinCOFF;
518 ManglingMode = MM_WinCOFFX86;
521 ManglingMode = MM_XCOFF;
526 return reportError(
"Unknown specifier in datalayout string");
541 bool Ret = BigEndian ==
Other.BigEndian &&
542 AllocaAddrSpace ==
Other.AllocaAddrSpace &&
543 StackNaturalAlign ==
Other.StackNaturalAlign &&
544 ProgramAddrSpace ==
Other.ProgramAddrSpace &&
545 DefaultGlobalsAddrSpace ==
Other.DefaultGlobalsAddrSpace &&
546 FunctionPtrAlign ==
Other.FunctionPtrAlign &&
547 TheFunctionPtrAlignType ==
Other.TheFunctionPtrAlignType &&
548 ManglingMode ==
Other.ManglingMode &&
549 LegalIntWidths ==
Other.LegalIntWidths &&
550 Alignments ==
Other.Alignments && Pointers ==
Other.Pointers;
558 auto Pair = std::make_pair((
unsigned)AlignType,
BitWidth);
560 return std::make_pair(
E.AlignType,
E.TypeBitWidth) < Pair;
570 assert(
Log2(abi_align) < 16 &&
Log2(pref_align) < 16 &&
"Alignment too big");
571 if (!isUInt<24>(bit_width))
572 return reportError(
"Invalid bit width, must be a 24bit integer");
573 if (pref_align < abi_align)
575 "Preferred alignment cannot be less than the ABI alignment");
578 if (
I != Alignments.end() &&
579 I->AlignType == (
unsigned)align_type &&
I->TypeBitWidth == bit_width) {
581 I->ABIAlign = abi_align;
582 I->PrefAlign = pref_align;
586 pref_align, bit_width));
610 if (PrefAlign < ABIAlign)
612 "Preferred alignment cannot be less than the ABI alignment");
618 if (
I == Pointers.end() ||
I->AddressSpace != AddrSpace) {
621 TypeBitWidth, IndexBitWidth));
623 I->ABIAlign = ABIAlign;
624 I->PrefAlign = PrefAlign;
625 I->TypeBitWidth = TypeBitWidth;
626 I->IndexBitWidth = IndexBitWidth;
632 bool abi_or_pref)
const {
640 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
645 class StructLayoutMap {
647 LayoutInfoTy LayoutInfo;
652 for (
const auto &
I : LayoutInfo) {
654 Value->~StructLayout();
660 return LayoutInfo[STy];
666 void DataLayout::clear() {
667 LegalIntWidths.
clear();
670 delete static_cast<StructLayoutMap *
>(LayoutMap);
680 LayoutMap =
new StructLayoutMap();
682 StructLayoutMap *STM =
static_cast<StructLayoutMap*
>(LayoutMap);
701 return getPointerAlignElem(AS).
ABIAlign;
705 return getPointerAlignElem(AS).
PrefAlign;
709 return divideCeil(getPointerAlignElem(AS).TypeBitWidth, 8);
713 unsigned MaxIndexSize = 0;
714 for (
auto &
P : Pointers)
723 "This should only be called with a pointer or pointer vector type");
729 return divideCeil(getPointerAlignElem(AS).IndexBitWidth, 8);
734 "This should only be called with a pointer or pointer vector type");
747 Align DataLayout::getAlignment(
Type *Ty,
bool abi_or_pref)
const {
748 assert(Ty->
isSized() &&
"Cannot getTypeInfo() on a type that is unsized!");
754 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
759 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
763 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
770 "Aggregate alignment must be first alignment entry");
790 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
807 return abi_or_pref ?
I->ABIAlign :
I->PrefAlign;
830 return getAlignment(Ty,
true);
839 return getAlignment(Ty,
false);
849 "Expected a pointer or pointer vector type.");
852 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
858 for (
unsigned LegalIntWidth : LegalIntWidths)
859 if (
Width <= LegalIntWidth)
865 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
866 return Max != LegalIntWidths.end() ? *Max : 0;
871 "Expected a pointer or pointer vector type.");
874 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
886 for (; GTI != GTE; ++GTI) {
890 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
899 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
911 unsigned BitWidth = Offset.getBitWidth();
917 APInt Index = Offset.sdiv(ElemSize);
918 Offset -= Index * ElemSize;
919 if (Offset.isNegative()) {
923 assert(Offset.isNonNegative() &&
"Remaining offset shouldn't be negative");
929 APInt &Offset)
const {
930 if (
auto *ArrTy = dyn_cast<ArrayType>(ElemTy)) {
931 ElemTy = ArrTy->getElementType();
935 if (
auto *VecTy = dyn_cast<VectorType>(ElemTy)) {
936 ElemTy = VecTy->getElementType();
939 if (ElemSizeInBits % 8 != 0)
945 if (
auto *STy = dyn_cast<StructType>(ElemTy)) {
947 uint64_t IntOffset = Offset.getZExtValue();
953 ElemTy = STy->getElementType(Index);
954 return APInt(32, Index);
962 APInt &Offset)
const {
966 while (Offset != 0) {
970 Indices.push_back(*Index);
994 if (*GVAlignment >= Alignment)
995 Alignment = *GVAlignment;
1004 if (Alignment <
Align(16)) {
1008 Alignment =
Align(16);
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
@ FloatTyID
32-bit floating point type
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
@ DoubleTyID
64-bit floating point type
This is an optimization pass for GlobalISel generic memory operations.
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
A parsed version of the target data layout string in and methods for querying it.
Align getPreferredAlign(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
ScalarTy getFixedSize() const
Layout pointer alignment element.
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
@ Independent
The function pointer alignment is independent of the function alignment.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
MutableArrayRef< uint64_t > getMemberOffsets()
Align getPointerPrefAlignment(unsigned AS=0) const
Return target's alignment for stack-based pointers FIXME: The defaults need to be removed once all of...
static Error getIntInBytes(StringRef R, IntTy &Result)
Get an unsigned integer representing the number of bits and convert it into bytes.
static ErrorSuccess success()
Create a success value.
@ X86_AMXTyID
AMX vectors (8192 bits, X86 specific)
TypeID getTypeID() const
Return the type id for the type.
Triple - Helper class for working with autoconf configuration names.
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
static Error split(StringRef Str, char Separator, std::pair< StringRef, StringRef > &Split)
Checked version of split, to ensure mandatory subparts.
Type * getIndexType(Type *PtrTy) const
Returns the type of a GEP index.
static Error getAddrSpace(StringRef R, unsigned &AddrSpace)
The instances of the Type class are immutable: once they are created, they are never changed.
static const char * getManglingComponent(const Triple &T)
AlignTypeEnum
Enum used to categorize the alignment types stored by LayoutAlignElem.
Tagged union holding either a T or a Error.
static Error getInt(StringRef R, IntTy &Result)
Get an unsigned integer, including error checks.
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
gep_type_iterator gep_type_begin(const User *GEP)
bool isScalable() const
Returns whether the size is scaled by a runtime quantity (vscale).
@ MultipleOfFunctionAlign
The function pointer alignment is a multiple of the function alignment.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Optional< APInt > getGEPIndexForOffset(Type *&ElemTy, APInt &Offset) const
Get single GEP index to access Offset inside ElemTy.
bool hasInitializer() const
Definitions have initializers, declarations don't.
gep_type_iterator gep_type_end(const User *GEP)
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
AddressSpace getAddressSpace(T *V)
unsigned getIndexSizeInBits(unsigned AS) const
Size in bits of index used for address calculation in getelementptr.
static PointerAlignElem getInBits(uint32_t AddressSpace, Align ABIAlign, Align PrefAlign, uint32_t TypeBitWidth, uint32_t IndexBitWidth)
Initializer.
uint64_t getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
(vector float) vec_cmpeq(*A, *B) C
unsigned Log2(Align A)
Returns the log2 of the alignment.
Value * getOperand() const
static TypeSize Fixed(ScalarTy MinVal)
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Class to represent integer types.
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Layout alignment element.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
unsigned getLargestLegalIntTypeSizeInBits() const
Returns the size of largest legal integer type size, or 0 if none are set.
unsigned getIndexSize(unsigned AS) const
rounded up to a whole number of bytes.
This struct is a compact representation of a valid (non-zero power of two) alignment.
StructType * getStructTypeOrNull() const
@ BFloatTyID
16-bit floating point type (7-bit significand)
unsigned getIntegerBitWidth() const
void init(const Module *M)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Base class of all SIMD vector types.
static APInt getElementIndex(TypeSize ElemSize, APInt &Offset)
bool hasSection() const
Check if this global has a custom object file section.
uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator)
Returns the integer ceil(Numerator / Denominator).
constexpr bool isUInt< 16 >(uint64_t x)
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef< Value * > Indices) const
Returns the offset from the beginning of the type for the specified indices.
@ X86_MMXTyID
MMX vectors (64 bits, X86 specific)
unsigned getMaxIndexSize() const
Returns the maximum index size over all address spaces.
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
This is an important class for using LLVM in a threaded context.
Align assumeAligned(uint64_t Value)
Treats the value 0 as a 1, so Align is always at least 1.
LLVM_NODISCARD char front() const
front - Get the first character in the string.
unsigned getElementContainingOffset(uint64_t Offset) const
Given a valid byte offset into the structure, returns the structure index that contains it.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Expected< DataLayout > parse(StringRef LayoutDescription)
Parse a data layout string and return the layout.
uint64_t getPrefTypeAlignment(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Align getPointerABIAlignment(unsigned AS) const
Layout pointer alignment.
StandardInstrumentations SI(Debug, VerifyEach)
Type * getIndexedType() const
A Module instance is used to store all the information related to an LLVM module.
Class for arbitrary precision integers.
@ FP128TyID
128-bit floating point type (112-bit significand)
Class to represent struct types.
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
StringRef - Represent a constant reference to a string, i.e.
uint64_t getSizeInBytes() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Type * getType() const
All values are typed, get the type of this value.
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
unsigned getNumElements() const
Random access to the elements.
Type * getSmallestLegalIntType(LLVMContext &C, unsigned Width=0) const
Returns the smallest integer type with size at least as big as Width bits.
@ IntegerTyID
Arbitrary bit width integers.
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
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.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
bool operator==(const LayoutAlignElem &rhs) const
uint64_t getElementOffset(unsigned Idx) const
uint64_t value() const
This is a hole in the type system and should not be abused.
@ FixedVectorTyID
Fixed width SIMD vector type.
Lightweight error class with error context and mandatory checking.
bool operator==(const PointerAlignElem &rhs) const
constexpr unsigned BitWidth
DataLayout(StringRef LayoutDescription)
Constructs a DataLayout from a specification string. See reset().
static const LayoutAlignElem DefaultAlignments[]
SmallVector< APInt > getGEPIndicesForOffset(Type *&ElemTy, APInt &Offset) const
Get GEP indices to access Offset inside ElemTy.
typename SuperClass::iterator iterator
static LayoutAlignElem get(AlignTypeEnum align_type, Align abi_align, Align pref_align, uint32_t bit_width)
bool operator==(const DataLayout &Other) const
Align max(MaybeAlign Lhs, Align Rhs)
unsigned AlignType
Alignment type from AlignTypeEnum.
void reset(StringRef LayoutDescription)
Parse a data layout string (with fallback to default values).
ScalarTy getKnownMinSize() const
@ ScalableVectorTyID
Scalable SIMD vector type.
Type * getValueType() 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 ...
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Align getAlignment() const
static Error reportError(const Twine &Message)
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
LLVM Value Representation.
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
@ HalfTyID
16-bit floating point type
@ X86_FP80TyID
80-bit floating point type (X87)
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size in bytes, rounded up to a whole number of bytes.
Optional< std::vector< StOtherPiece > > Other
iterator insert(iterator I, T &&Elt)
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...