54 for (
unsigned i = 0, e = NumElements; i != e; ++i) {
59 if ((StructSize & (TyAlign-1)) != 0) {
61 StructSize =
alignTo(StructSize, TyAlign);
65 StructAlignment =
std::max(TyAlign, StructAlignment);
67 MemberOffsets[i] = StructSize;
72 if (StructAlignment == 0) StructAlignment = 1;
76 if ((StructSize & (StructAlignment-1)) != 0) {
78 StructSize =
alignTo(StructSize, StructAlignment);
87 assert(SI != &MemberOffsets[0] &&
"Offset not in structure type!");
89 assert(*SI <= Offset &&
"upper_bound didn't work");
90 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
91 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
92 "Upper bound didn't work!");
99 return SI-&MemberOffsets[0];
108 unsigned pref_align,
uint32_t bit_width) {
109 assert(abi_align <= pref_align &&
"Preferred alignment worse than ABI!");
132 unsigned PrefAlign,
uint32_t TypeByteWidth,
134 assert(ABIAlign <= PrefAlign &&
"Preferred alignment worse than ABI!");
186 StackNaturalAlign = 0;
187 ProgramAddrSpace = 0;
188 ManglingMode = MM_None;
189 NonIntegralAddressSpaces.clear();
196 setPointerAlignment(0, 8, 8, 8, 8);
198 parseSpecifier(Desc);
203 assert(!Str.
empty() &&
"parse error, string can't be empty here");
204 std::pair<StringRef, StringRef>
Split = Str.
split(Separator);
205 if (Split.second.empty() && Split.first != Str)
207 if (!Split.second.empty() && Split.first.empty())
229 unsigned AddrSpace =
getInt(R);
230 if (!isUInt<24>(AddrSpace))
235 void DataLayout::parseSpecifier(
StringRef Desc) {
236 StringRepresentation = Desc;
237 while (!Desc.
empty()) {
239 std::pair<StringRef, StringRef>
Split =
split(Desc,
'-');
243 Split =
split(Split.first,
':');
251 Split =
split(Rest,
':');
253 unsigned AS =
getInt(Split.first);
256 NonIntegralAddressSpaces.push_back(AS);
257 }
while (!Rest.
empty());
262 char Specifier = Tok.
front();
278 unsigned AddrSpace = Tok.
empty() ? 0 :
getInt(Tok);
279 if (!isUInt<24>(AddrSpace))
285 "Missing size specification for pointer in datalayout string");
286 Split =
split(Rest,
':');
294 "Missing alignment specification for pointer in datalayout string");
295 Split =
split(Rest,
':');
299 "Pointer ABI alignment must be a power of 2");
303 unsigned IndexSize = PointerMemSize;
306 unsigned PointerPrefAlign = PointerABIAlign;
308 Split =
split(Rest,
':');
312 "Pointer preferred alignment must be a power of 2");
316 Split =
split(Rest,
':');
322 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
323 PointerMemSize, IndexSize);
344 "Sized aggregate specification in datalayout string");
349 "Missing alignment specification in datalayout string");
350 Split =
split(Rest,
':');
354 "ABI alignment specification must be >0 for non-aggregate types");
357 unsigned PrefAlign = ABIAlign;
359 Split =
split(Rest,
':');
363 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
369 unsigned Width =
getInt(Tok);
372 "Zero width native integer type in datalayout string");
373 LegalIntWidths.push_back(Width);
376 Split =
split(Rest,
':');
393 report_fatal_error(
"Unexpected trailing characters after mangling specifier in datalayout string");
402 ManglingMode = MM_ELF;
405 ManglingMode = MM_MachO;
408 ManglingMode = MM_Mips;
411 ManglingMode = MM_WinCOFF;
414 ManglingMode = MM_WinCOFFX86;
432 bool Ret = BigEndian == Other.BigEndian &&
433 AllocaAddrSpace == Other.AllocaAddrSpace &&
434 StackNaturalAlign == Other.StackNaturalAlign &&
435 ProgramAddrSpace == Other.ProgramAddrSpace &&
436 ManglingMode == Other.ManglingMode &&
437 LegalIntWidths == Other.LegalIntWidths &&
438 Alignments == Other.Alignments && Pointers == Other.Pointers;
446 auto Pair = std::make_pair((
unsigned)AlignType, BitWidth);
449 const std::pair<unsigned, uint32_t> &RHS) {
450 return std::tie(LHS.AlignType, LHS.TypeBitWidth) <
451 std::tie(RHS.first, RHS.second);
456 DataLayout::setAlignment(
AlignTypeEnum align_type,
unsigned abi_align,
457 unsigned pref_align,
uint32_t bit_width) {
458 if (!isUInt<24>(bit_width))
469 if (pref_align < abi_align)
471 "Preferred alignment cannot be less than the ABI alignment");
474 if (I != Alignments.end() &&
475 I->AlignType == (
unsigned)align_type && I->TypeBitWidth == bit_width) {
477 I->ABIAlign = abi_align;
478 I->PrefAlign = pref_align;
482 pref_align, bit_width));
494 void DataLayout::setPointerAlignment(
uint32_t AddrSpace,
unsigned ABIAlign,
495 unsigned PrefAlign,
uint32_t TypeByteWidth,
497 if (PrefAlign < ABIAlign)
499 "Preferred alignment cannot be less than the ABI alignment");
502 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
504 TypeByteWidth, IndexWidth));
506 I->ABIAlign = ABIAlign;
507 I->PrefAlign = PrefAlign;
508 I->TypeByteWidth = TypeByteWidth;
509 I->IndexWidth = IndexWidth;
515 unsigned DataLayout::getAlignmentInfo(
AlignTypeEnum AlignType,
522 if (I != Alignments.end() && I->AlignType == (
unsigned)AlignType &&
524 return ABIInfo ? I->ABIAlign : I->PrefAlign;
528 if (I != Alignments.begin()) {
531 return ABIInfo ? I->ABIAlign : I->PrefAlign;
536 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
537 Align *= cast<VectorType>(Ty)->getNumElements();
548 unsigned Align = getTypeStoreSize(Ty);
555 class StructLayoutMap {
557 LayoutInfoTy LayoutInfo;
562 for (
const auto &
I : LayoutInfo) {
564 Value->~StructLayout();
570 return LayoutInfo[STy];
576 void DataLayout::clear() {
577 LegalIntWidths.clear();
580 delete static_cast<StructLayoutMap *
>(LayoutMap);
590 LayoutMap =
new StructLayoutMap();
592 StructLayoutMap *STM =
static_cast<StructLayoutMap*
>(LayoutMap);
613 if (I == Pointers.end() || I->AddressSpace != AS) {
614 I = findPointerLowerBound(0);
615 assert(I->AddressSpace == 0);
622 if (I == Pointers.end() || I->AddressSpace != AS) {
623 I = findPointerLowerBound(0);
624 assert(I->AddressSpace == 0);
631 if (I == Pointers.end() || I->AddressSpace != AS) {
632 I = findPointerLowerBound(0);
633 assert(I->AddressSpace == 0);
635 return I->TypeByteWidth;
639 unsigned MaxPointerSize = 0;
640 for (
auto &
P : Pointers)
641 MaxPointerSize =
std::max(MaxPointerSize,
P.TypeByteWidth);
643 return MaxPointerSize;
648 "This should only be called with a pointer or pointer vector type");
650 return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
655 if (I == Pointers.end() || I->AddressSpace != AS) {
656 I = findPointerLowerBound(0);
657 assert(I->AddressSpace == 0);
659 return I->IndexWidth;
664 "This should only be called with a pointer or pointer vector type");
666 return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
677 unsigned DataLayout::getAlignment(
Type *Ty,
bool abi_or_pref)
const {
680 assert(Ty->
isSized() &&
"Cannot getTypeInfo() on a type that is unsized!");
685 ? getPointerABIAlignment(0)
686 : getPointerPrefAlignment(0));
688 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
690 ? getPointerABIAlignment(AS)
691 : getPointerPrefAlignment(AS));
694 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
698 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
702 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
727 return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty), abi_or_pref, Ty);
737 return getAlignmentInfo(
INTEGER_ALIGN, BitWidth,
true,
nullptr);
745 unsigned Align = getPrefTypeAlignment(Ty);
746 assert(!(Align & (Align-1)) &&
"Alignment is not a power of two!");
751 unsigned AddressSpace)
const {
757 "Expected a pointer or pointer vector type.");
758 unsigned NumBits = getIndexTypeSizeInBits(Ty);
760 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
766 for (
unsigned LegalIntWidth : LegalIntWidths)
767 if (Width <= LegalIntWidth)
773 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
774 return Max != LegalIntWidths.end() ? *Max : 0;
779 "Expected a pointer or pointer vector type.");
780 unsigned NumBits = getIndexTypeSizeInBits(Ty);
782 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
794 for (; GTI != GTE; ++GTI) {
798 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
807 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
832 unsigned Alignment = getPrefTypeAlignment(ElemType);
833 if (GVAlignment >= Alignment) {
834 Alignment = GVAlignment;
835 }
else if (GVAlignment != 0) {
836 Alignment =
std::max(GVAlignment, getABITypeAlignment(ElemType));
843 if (Alignment < 16) {
846 if (getTypeSizeInBits(ElemType) > 128)
857 return Log2_32(getPreferredAlignment(GV));
unsigned getAlignment() const
A parsed version of the target data layout string in and methods for querying it. ...
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
void reset(StringRef LayoutDescription)
Parse a data layout string (with fallback to default values).
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
typename SuperClass::const_iterator const_iterator
AlignTypeEnum
Enum used to categorize the alignment types stored by LayoutAlignElem.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
Type * getElementType(unsigned N) const
A Module instance is used to store all the information related to an LLVM module. ...
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
unsigned getPointerPrefAlignment(unsigned AS=0) const
Return target's alignment for stack-based pointers FIXME: The defaults need to be removed once all of...
2: 32-bit floating point type
unsigned getNumElements() const
Random access to the elements.
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
gep_type_iterator gep_type_end(const User *GEP)
static std::pair< StringRef, StringRef > split(StringRef Str, char Separator)
Checked version of split, to ensure mandatory subparts.
4: 80-bit floating point type (X87)
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
1: 16-bit floating point type
bool isOpaque() const
Return true if this is a type with an identity that has no body specified yet.
Type * getSmallestLegalIntType(LLVMContext &C, unsigned Width=0) const
Returns the smallest integer type with size at least as big as Width bits.
unsigned getPointerABIAlignment(unsigned AS) const
Layout pointer alignment.
static const char * getManglingComponent(const Triple &T)
unsigned getElementContainingOffset(uint64_t Offset) const
Given a valid byte offset into the structure, returns the structure index that contains it...
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static unsigned getAddrSpace(StringRef R)
unsigned getIndexSize(unsigned AS) const
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Type * getIndexType(Type *PtrTy) const
Returns the type of a GEP index.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
TypeID getTypeID() const
Return the type id for the type.
static unsigned getInt(StringRef R)
Get an unsigned integer, including error checks.
Class to represent struct types.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Type * getType() const
All values are typed, get the type of this value.
unsigned getPreferredTypeAlignmentShift(Type *Ty) const
Returns the preferred alignment for the specified type, returned as log2 of the value (a shift amount...
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align, unsigned pref_align, uint32_t bit_width)
auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
static const LayoutAlignElem DefaultAlignments[]
Layout pointer alignment element.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
11: Arbitrary bit width integers
bool isOSWindows() const
Tests whether the OS is Windows.
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 getPreferredAlignmentLog(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global, returned in log form.
initializer< Ty > init(const Ty &Val)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
The instances of the Type class are immutable: once they are created, they are never changed...
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
This is an important class for using LLVM in a threaded context.
unsigned getMaxPointerSize() const
Returns the maximum pointer size over all address spaces.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size FIXME: The defaults need to be removed once all of the backends/clients are updat...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
unsigned getPrefTypeAlignment(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
bool operator==(const PointerAlignElem &rhs) const
unsigned getLargestLegalIntTypeSizeInBits() const
Returns the size of largest legal integer type size, or 0 if none are set.
6: 128-bit floating point type (two 64-bits, PowerPC)
Class to represent integer types.
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Type * getIndexedType() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
bool operator==(const DataLayout &Other) const
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
StructType * getStructTypeOrNull() const
16: SIMD 'packed' format, or other vector type
bool hasSection() const
Check if this global has a custom object file section.
Module.h This file contains the declarations for the Module class.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const
Returns the minimum ABI-required alignment for an integer type of the specified bitwidth.
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
Class to represent vector types.
typename SuperClass::iterator iterator
DataLayout(StringRef LayoutDescription)
Constructs a DataLayout from a specification string. See reset().
static void clear(coro::Shape &Shape)
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
uint64_t getElementOffset(unsigned Idx) const
Value * getOperand() const
Layout alignment element.
static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign, unsigned PrefAlign, uint32_t TypeByteWidth, uint32_t IndexWidth)
Initializer.
Type * getValueType() const
unsigned AlignType
Alignment type from AlignTypeEnum.
constexpr bool isUInt< 16 >(uint64_t x)
auto upper_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range))
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
static unsigned inBytes(unsigned Bits)
Convert bits into bytes. Assert if not a byte width multiple.
3: 64-bit floating point type
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM_NODISCARD char front() const
front - Get the first character in the string.
LLVM Value Representation.
bool hasInitializer() const
Definitions have initializers, declarations don't.
unsigned getAlignment() const
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
StringRef - Represent a constant reference to a string, i.e.
bool operator==(const LayoutAlignElem &rhs) const
9: MMX vectors (64 bits, X86 specific)
static void Split(std::vector< std::string > &V, StringRef S)
Splits a string of comma separated items in to a vector of strings.
int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef< Value *> Indices) const
Returns the offset from the beginning of the type for the specified indices.
void init(const Module *M)
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
5: 128-bit floating point type (112-bit mantissa)
gep_type_iterator gep_type_begin(const User *GEP)