48 for (
unsigned i = 0, e = NumElements;
i != e; ++
i) {
53 if ((StructSize & (TyAlign-1)) != 0) {
55 StructSize =
alignTo(StructSize, TyAlign);
59 StructAlignment = std::max(TyAlign, StructAlignment);
61 MemberOffsets[
i] = StructSize;
66 if (StructAlignment == 0) StructAlignment = 1;
70 if ((StructSize & (StructAlignment-1)) != 0) {
72 StructSize =
alignTo(StructSize, StructAlignment);
81 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
82 assert(SI != &MemberOffsets[0] &&
"Offset not in structure type!");
84 assert(*SI <= Offset &&
"upper_bound didn't work");
85 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
86 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
87 "Upper bound didn't work!");
94 return SI-&MemberOffsets[0];
103 unsigned pref_align,
uint32_t bit_width) {
104 assert(abi_align <= pref_align &&
"Preferred alignment worse than ABI!");
122 DataLayout::InvalidAlignmentElem = {
INVALID_ALIGN, 0, 0, 0 };
130 unsigned PrefAlign,
uint32_t TypeByteWidth) {
131 assert(ABIAlign <= PrefAlign &&
"Preferred alignment worse than ABI!");
149 DataLayout::InvalidPointerElem = { 0U, 0U, 0U, ~0U };
183 StackNaturalAlign = 0;
184 ManglingMode = MM_None;
185 NonIntegralAddressSpaces.
clear();
192 setPointerAlignment(0, 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 StringRepresentation =
Desc;
226 while (!Desc.
empty()) {
228 std::pair<StringRef, StringRef>
Split =
split(Desc,
'-');
232 Split =
split(Split.first,
':');
240 Split =
split(Rest,
':');
242 unsigned AS =
getInt(Split.first);
246 }
while (!Rest.
empty());
251 char Specifier = Tok.
front();
267 unsigned AddrSpace = Tok.
empty() ? 0 :
getInt(Tok);
268 if (!isUInt<24>(AddrSpace))
274 "Missing size specification for pointer in datalayout string");
275 Split =
split(Rest,
':');
283 "Missing alignment specification for pointer in datalayout string");
284 Split =
split(Rest,
':');
288 "Pointer ABI alignment must be a power of 2");
291 unsigned PointerPrefAlign = PointerABIAlign;
293 Split =
split(Rest,
':');
297 "Pointer preferred alignment must be a power of 2");
300 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
322 "Sized aggregate specification in datalayout string");
327 "Missing alignment specification in datalayout string");
328 Split =
split(Rest,
':');
332 "ABI alignment specification must be >0 for non-aggregate types");
335 unsigned PrefAlign = ABIAlign;
337 Split =
split(Rest,
':');
341 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
347 unsigned Width =
getInt(Tok);
350 "Zero width native integer type in datalayout string");
354 Split =
split(Rest,
':');
363 report_fatal_error(
"Unexpected trailing characters after mangling specifier in datalayout string");
372 ManglingMode = MM_ELF;
375 ManglingMode = MM_MachO;
378 ManglingMode = MM_Mips;
381 ManglingMode = MM_WinCOFF;
384 ManglingMode = MM_WinCOFFX86;
402 bool Ret = BigEndian == Other.BigEndian &&
403 StackNaturalAlign == Other.StackNaturalAlign &&
404 ManglingMode == Other.ManglingMode &&
405 LegalIntWidths == Other.LegalIntWidths &&
406 Alignments == Other.Alignments && Pointers == Other.Pointers;
412 DataLayout::setAlignment(
AlignTypeEnum align_type,
unsigned abi_align,
413 unsigned pref_align,
uint32_t bit_width) {
414 if (!isUInt<24>(bit_width))
425 if (pref_align < abi_align)
427 "Preferred alignment cannot be less than the ABI alignment");
430 if (Elem.AlignType == (
unsigned)align_type &&
431 Elem.TypeBitWidth == bit_width) {
433 Elem.ABIAlign = abi_align;
434 Elem.PrefAlign = pref_align;
440 pref_align, bit_width));
451 void DataLayout::setPointerAlignment(
uint32_t AddrSpace,
unsigned ABIAlign,
454 if (PrefAlign < ABIAlign)
456 "Preferred alignment cannot be less than the ABI alignment");
459 if (I == Pointers.
end() || I->AddressSpace != AddrSpace) {
463 I->ABIAlign = ABIAlign;
464 I->PrefAlign = PrefAlign;
465 I->TypeByteWidth = TypeByteWidth;
471 unsigned DataLayout::getAlignmentInfo(
AlignTypeEnum AlignType,
475 int BestMatchIdx = -1;
477 for (
unsigned i = 0, e = Alignments.size();
i != e; ++
i) {
478 if (Alignments[
i].AlignType == (
unsigned)AlignType &&
479 Alignments[
i].TypeBitWidth == BitWidth)
480 return ABIInfo ? Alignments[
i].ABIAlign : Alignments[
i].PrefAlign;
487 if (Alignments[
i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
488 Alignments[
i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
492 if (LargestInt == -1 ||
493 Alignments[
i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
500 if (BestMatchIdx == -1) {
503 BestMatchIdx = LargestInt;
508 Align *= cast<VectorType>(Ty)->getNumElements();
520 if (BestMatchIdx == -1) {
527 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
528 : Alignments[BestMatchIdx].PrefAlign;
533 class StructLayoutMap {
535 LayoutInfoTy LayoutInfo;
540 for (
const auto &I : LayoutInfo) {
542 Value->~StructLayout();
548 return LayoutInfo[STy];
554 void DataLayout::clear() {
555 LegalIntWidths.
clear();
558 delete static_cast<StructLayoutMap *
>(LayoutMap);
568 LayoutMap =
new StructLayoutMap();
570 StructLayoutMap *STM =
static_cast<StructLayoutMap*
>(LayoutMap);
592 if (I == Pointers.
end() || I->AddressSpace != AS) {
593 I = findPointerLowerBound(0);
594 assert(I->AddressSpace == 0);
601 if (I == Pointers.
end() || I->AddressSpace != AS) {
602 I = findPointerLowerBound(0);
603 assert(I->AddressSpace == 0);
610 if (I == Pointers.
end() || I->AddressSpace != AS) {
611 I = findPointerLowerBound(0);
612 assert(I->AddressSpace == 0);
614 return I->TypeByteWidth;
619 "This should only be called with a pointer or pointer vector type");
635 unsigned DataLayout::getAlignment(
Type *Ty,
bool abi_or_pref)
const {
638 assert(Ty->
isSized() &&
"Cannot getTypeInfo() on a type that is unsized!");
646 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
652 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
656 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
661 unsigned Align = getAlignmentInfo(
AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
690 return getAlignment(Ty,
true);
696 return getAlignmentInfo(
INTEGER_ALIGN, BitWidth,
true,
nullptr);
700 return getAlignment(Ty,
false);
705 assert(!(Align & (Align-1)) &&
"Alignment is not a power of two!");
710 unsigned AddressSpace)
const {
716 "Expected a pointer or pointer vector type.");
719 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
725 for (
unsigned LegalIntWidth : LegalIntWidths)
726 if (Width <= LegalIntWidth)
732 auto Max = std::max_element(LegalIntWidths.
begin(), LegalIntWidths.
end());
733 return Max != LegalIntWidths.
end() ? *
Max : 0;
743 for (; GTI != GTE; ++GTI) {
747 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
756 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
771 if (GVAlignment >= Alignment) {
772 Alignment = GVAlignment;
773 }
else if (GVAlignment != 0) {
778 if (Alignment < 16) {
SuperClass::iterator iterator
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.
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
Type * getIndexedType() const
void reset(StringRef LayoutDescription)
Parse a data layout string (with fallback to default values).
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
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.
bool isOpaque() const
Return true if this is a type with an identity that has no body specified yet.
A Module instance is used to store all the information related to an LLVM module. ...
2: 32-bit floating point type
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
unsigned getPrefTypeAlignment(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
Type * getValueType() const
unsigned getPointerPrefAlignment(unsigned AS=0) const
Return target's alignment for stack-based pointers FIXME: The defaults need to be removed once all of...
gep_type_iterator gep_type_end(const User *GEP)
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
unsigned getAlignment() const
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 operator==(const DataLayout &Other) const
static const char * getManglingComponent(const Triple &T)
bool isOSWindows() const
Tests whether the OS is Windows.
StructType * getStructTypeOrNull() const
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
unsigned getPreferredTypeAlignmentShift(Type *Ty) const
Returns the preferred alignment for the specified type, returned as log2 of the value (a shift amount...
unsigned getPointerABIAlignment(unsigned AS=0) const
Layout pointer alignment FIXME: The defaults need to be removed once all of the backends/clients are ...
static unsigned getInt(StringRef R)
Get an unsigned integer, including error checks.
Class to represent struct types.
Type * getSmallestLegalIntType(LLVMContext &C, unsigned Width=0) const
Returns the smallest integer type with size at least as big as Width bits.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
unsigned getAlignment() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
TypeID getTypeID() const
Return the type id for the type.
static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align, unsigned pref_align, uint32_t bit_width)
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
LLVM_NODISCARD char front() const
front - Get the first character in the string.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Type * getScalarType() const LLVM_READONLY
If this is a vector type, return the element type, otherwise return 'this'.
static const LayoutAlignElem DefaultAlignments[]
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Layout pointer alignment element.
uint64_t getElementOffset(unsigned Idx) const
11: Arbitrary bit width integers
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const
Returns the minimum ABI-required alignment for an integer type of the specified bitwidth.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
Type * getElementType(unsigned N) const
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
This file contains the declarations for the subclasses of Constant, which represent the different fla...
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).
constexpr bool isPowerOf2_64(uint64_t Value)
isPowerOf2_64 - This function returns true if the argument is a power of two 0 (64 bit edition...
6: 128-bit floating point type (two 64-bits, PowerPC)
int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef< Value * > Indices) const
Returns the offset from the beginning of the type for the specified indices.
Class to represent integer types.
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
bool isPointerTy() const
True if this is an instance of PointerType.
bool operator==(const LayoutAlignElem &rhs) const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
Triple - Helper class for working with autoconf configuration names.
unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global, returned in log form.
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 getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
16: SIMD 'packed' format, or other vector type
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
static const char * Separator
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getElementContainingOffset(uint64_t Offset) const
Given a valid byte offset into the structure, returns the structure index that contains it...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
unsigned Log2_32(uint32_t Value)
Log2_32 - This function returns the floor log base 2 of the specified value, -1 if the value is zero...
Class to represent vector types.
bool hasInitializer() const
Definitions have initializers, declarations don't.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
bool isIntegerTy() const
True if this is an instance of IntegerType.
DataLayout(StringRef LayoutDescription)
Constructs a DataLayout from a specification string. See reset().
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
iterator insert(iterator I, T &&Elt)
bool operator==(const PointerAlignElem &rhs) const
Value * getOperand() const
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Layout alignment element.
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 AlignType
Alignment type from AlignTypeEnum.
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
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 Value Representation.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
StringRef - Represent a constant reference to a string, i.e.
unsigned getLargestLegalIntTypeSizeInBits() const
Returns the size of largest legal integer type size, or 0 if none are set.
9: MMX vectors (64 bits, X86 specific)
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size FIXME: The defaults need to be removed once all of the backends/clients are updat...
static void Split(std::vector< std::string > &V, StringRef S)
Split - Splits a string of comma separated items in to a vector of strings.
unsigned getNumElements() const
Random access to the elements.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign, unsigned PrefAlign, uint32_t TypeByteWidth)
Initializer.
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)