53 for (
unsigned i = 0,
e = NumElements; i !=
e; ++i) {
60 StructSize =
alignTo(StructSize, TyAlign);
64 StructAlignment =
std::max(TyAlign, StructAlignment);
66 MemberOffsets[i] = StructSize;
72 if (!
isAligned(StructAlignment, StructSize)) {
74 StructSize =
alignTo(StructSize, StructAlignment);
83 assert(SI != &MemberOffsets[0] &&
"Offset not in structure type!");
85 assert(*SI <= Offset &&
"upper_bound didn't work");
86 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
87 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
88 "Upper bound didn't work!");
95 return SI-&MemberOffsets[0];
104 assert(abi_align <= pref_align &&
"Preferred alignment worse than ABI!");
128 assert(ABIAlign <= PrefAlign &&
"Preferred alignment worse than ABI!");
180 StackNaturalAlign.reset();
181 ProgramAddrSpace = 0;
182 FunctionPtrAlign.reset();
183 TheFunctionPtrAlignType = FunctionPtrAlignType::Independent;
184 ManglingMode = MM_None;
185 NonIntegralAddressSpaces.clear();
192 setPointerAlignment(0,
Align(8),
Align(8), 8, 8);
194 parseSpecifier(Desc);
199 assert(!Str.
empty() &&
"parse error, string can't be empty here");
200 std::pair<StringRef, StringRef> Split = Str.
split(Separator);
201 if (Split.second.empty() && Split.first != Str)
203 if (!Split.second.empty() && Split.first.empty())
225 unsigned AddrSpace =
getInt(R);
226 if (!isUInt<24>(AddrSpace))
231 void DataLayout::parseSpecifier(
StringRef Desc) {
232 StringRepresentation = Desc;
233 while (!Desc.
empty()) {
235 std::pair<StringRef, StringRef> Split =
split(Desc,
'-');
239 Split =
split(Split.first,
':');
247 Split =
split(Rest,
':');
249 unsigned AS =
getInt(Split.first);
252 NonIntegralAddressSpaces.push_back(AS);
253 }
while (!Rest.
empty());
258 char Specifier = Tok.
front();
274 unsigned AddrSpace = Tok.
empty() ? 0 :
getInt(Tok);
275 if (!isUInt<24>(AddrSpace))
281 "Missing size specification for pointer in datalayout string");
282 Split =
split(Rest,
':');
290 "Missing alignment specification for pointer in datalayout string");
291 Split =
split(Rest,
':');
295 "Pointer ABI alignment must be a power of 2");
299 unsigned IndexSize = PointerMemSize;
302 unsigned PointerPrefAlign = PointerABIAlign;
304 Split =
split(Rest,
':');
308 "Pointer preferred alignment must be a power of 2");
312 Split =
split(Rest,
':');
318 setPointerAlignment(AddrSpace,
assumeAligned(PointerABIAlign),
341 "Sized aggregate specification in datalayout string");
346 "Missing alignment specification in datalayout string");
347 Split =
split(Rest,
':');
351 "ABI alignment specification must be >0 for non-aggregate types");
359 unsigned PrefAlign = ABIAlign;
361 Split =
split(Rest,
':');
367 "Invalid preferred alignment, must be a 16bit integer");
381 "Zero width native integer type in datalayout string");
382 LegalIntWidths.push_back(Width);
385 Split =
split(Rest,
':');
396 switch (Tok.
front()) {
398 TheFunctionPtrAlignType = FunctionPtrAlignType::Independent;
401 TheFunctionPtrAlignType = FunctionPtrAlignType::MultipleOfFunctionAlign;
405 "datalayout string");
424 report_fatal_error(
"Unexpected trailing characters after mangling specifier in datalayout string");
433 ManglingMode = MM_ELF;
436 ManglingMode = MM_MachO;
439 ManglingMode = MM_Mips;
442 ManglingMode = MM_WinCOFF;
445 ManglingMode = MM_WinCOFFX86;
463 bool Ret = BigEndian == Other.BigEndian &&
464 AllocaAddrSpace == Other.AllocaAddrSpace &&
465 StackNaturalAlign == Other.StackNaturalAlign &&
466 ProgramAddrSpace == Other.ProgramAddrSpace &&
467 FunctionPtrAlign == Other.FunctionPtrAlign &&
468 TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType &&
469 ManglingMode == Other.ManglingMode &&
470 LegalIntWidths == Other.LegalIntWidths &&
471 Alignments == Other.Alignments && Pointers == Other.Pointers;
479 auto Pair = std::make_pair((
unsigned)AlignType, BitWidth);
491 assert(
Log2(abi_align) < 16 &&
Log2(pref_align) < 16 &&
"Alignment too big");
492 if (!isUInt<24>(bit_width))
494 if (pref_align < abi_align)
496 "Preferred alignment cannot be less than the ABI alignment");
499 if (I != Alignments.end() &&
500 I->AlignType == (
unsigned)align_type && I->TypeBitWidth == bit_width) {
502 I->ABIAlign = abi_align;
503 I->PrefAlign = pref_align;
507 pref_align, bit_width));
519 void DataLayout::setPointerAlignment(
uint32_t AddrSpace,
Align ABIAlign,
522 if (PrefAlign < ABIAlign)
524 "Preferred alignment cannot be less than the ABI alignment");
527 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
529 TypeByteWidth, IndexWidth));
531 I->ABIAlign = ABIAlign;
532 I->PrefAlign = PrefAlign;
533 I->TypeByteWidth = TypeByteWidth;
534 I->IndexWidth = IndexWidth;
541 bool ABIInfo,
Type *Ty)
const {
546 if (I != Alignments.end() && I->AlignType == (
unsigned)AlignType &&
548 return ABIInfo ? I->ABIAlign : I->PrefAlign;
552 if (I != Alignments.begin()) {
555 return ABIInfo ? I->ABIAlign : I->PrefAlign;
561 getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
562 Alignment *= cast<VectorType>(Ty)->getNumElements();
564 return Align(Alignment);
573 unsigned Alignment = getTypeStoreSize(Ty);
575 return Align(Alignment);
580 class StructLayoutMap {
582 LayoutInfoTy LayoutInfo;
587 for (
const auto &
I : LayoutInfo) {
589 Value->~StructLayout();
595 return LayoutInfo[STy];
601 void DataLayout::clear() {
602 LegalIntWidths.clear();
605 delete static_cast<StructLayoutMap *
>(LayoutMap);
615 LayoutMap =
new StructLayoutMap();
617 StructLayoutMap *STM =
static_cast<StructLayoutMap*
>(LayoutMap);
638 if (I == Pointers.end() || I->AddressSpace != AS) {
639 I = findPointerLowerBound(0);
640 assert(I->AddressSpace == 0);
647 if (I == Pointers.end() || I->AddressSpace != AS) {
648 I = findPointerLowerBound(0);
649 assert(I->AddressSpace == 0);
656 if (I == Pointers.end() || I->AddressSpace != AS) {
657 I = findPointerLowerBound(0);
658 assert(I->AddressSpace == 0);
660 return I->TypeByteWidth;
664 unsigned MaxPointerSize = 0;
665 for (
auto &
P : Pointers)
666 MaxPointerSize =
std::max(MaxPointerSize,
P.TypeByteWidth);
668 return MaxPointerSize;
673 "This should only be called with a pointer or pointer vector type");
675 return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
680 if (I == Pointers.end() || I->AddressSpace != AS) {
681 I = findPointerLowerBound(0);
682 assert(I->AddressSpace == 0);
684 return I->IndexWidth;
689 "This should only be called with a pointer or pointer vector type");
691 return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
702 Align DataLayout::getAlignment(
Type *Ty,
bool abi_or_pref)
const {
705 assert(Ty->
isSized() &&
"Cannot getTypeInfo() on a type that is unsized!");
709 return abi_or_pref ? getPointerABIAlignment(0) : getPointerPrefAlignment(0);
711 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
712 return abi_or_pref ? getPointerABIAlignment(AS)
713 : getPointerPrefAlignment(AS);
716 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
720 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
724 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
751 return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty).getKnownMinSize(),
762 return getAlignmentInfo(
INTEGER_ALIGN, BitWidth,
true,
nullptr);
770 unsigned AddressSpace)
const {
776 "Expected a pointer or pointer vector type.");
777 unsigned NumBits = getPointerTypeSizeInBits(Ty);
779 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
785 for (
unsigned LegalIntWidth : LegalIntWidths)
786 if (Width <= LegalIntWidth)
792 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
793 return Max != LegalIntWidths.end() ? *Max : 0;
798 "Expected a pointer or pointer vector type.");
799 unsigned NumBits = getIndexTypeSizeInBits(Ty);
801 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
813 for (; GTI != GTE; ++GTI) {
817 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
826 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
851 unsigned Alignment = getPrefTypeAlignment(ElemType);
852 if (GVAlignment >= Alignment) {
853 Alignment = GVAlignment;
854 }
else if (GVAlignment != 0) {
855 Alignment =
std::max(GVAlignment, getABITypeAlignment(ElemType));
862 if (Alignment < 16) {
865 if (getTypeSizeInBits(ElemType) > 128)
876 return Log2_32(getPreferredAlignment(GV));
auto lower_bound(R &&Range, T &&Value) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
unsigned Log2(Align A)
Returns the log2 of the alignment.
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).
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.
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...
static PointerAlignElem get(uint32_t AddressSpace, Align ABIAlign, Align PrefAlign, uint32_t TypeByteWidth, uint32_t IndexWidth)
Initializer.
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)
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.
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...
Align getPointerPrefAlignment(unsigned AS=0) const
Return target's alignment for stack-based pointers FIXME: The defaults need to be removed once all of...
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.
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
auto partition_point(R &&Range, Predicate P) -> decltype(adl_begin(Range))
Binary search for the first iterator in a range where a predicate is false.
Type * getType() const
All values are typed, get the type of this value.
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
LLVM_NODISCARD size_t size() const
size - Get the string size.
uint64_t value() const
This is a hole in the type system and should not be abused.
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...
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
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.
Align getABIIntegerTypeAlignment(unsigned BitWidth) const
Returns the minimum ABI-required alignment for an integer type of the specified bitwidth.
6: 128-bit floating point type (two 64-bits, PowerPC)
static LayoutAlignElem get(AlignTypeEnum align_type, Align abi_align, Align pref_align, uint32_t bit_width)
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
static constexpr const Align None()
Returns a default constructed Align which corresponds to no alignment.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This struct is a compact representation of a valid (non-zero power of two) alignment.
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.
Align getPointerABIAlignment(unsigned AS) const
Layout pointer alignment.
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
Align max(MaybeAlign Lhs, Align Rhs)
16: SIMD 'packed' format, or other vector type
This struct is a compact representation of a valid (power of two) or undefined (0) alignment...
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.
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
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 alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
uint64_t getElementOffset(unsigned Idx) const
Value * getOperand() const
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Layout alignment element.
Type * getValueType() const
unsigned AlignType
Alignment type from AlignTypeEnum.
constexpr bool isUInt< 16 >(uint64_t x)
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.
StringRef - Represent a constant reference to a string, i.e.
Align getAlignment() const
bool operator==(const LayoutAlignElem &rhs) const
9: MMX vectors (64 bits, X86 specific)
Align assumeAligned(uint64_t Value)
Treats the value 0 as a 1, so Align is always at least 1.
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.
auto upper_bound(R &&Range, T &&Value) -> decltype(adl_begin(Range))
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
5: 128-bit floating point type (112-bit mantissa)
gep_type_iterator gep_type_begin(const User *GEP)