41 assert(!ST->
isOpaque() &&
"Cannot get layout of opaque structs");
47 for (
unsigned i = 0, e = NumElements; i != e; ++i) {
52 if ((StructSize & (TyAlign-1)) != 0)
56 StructAlignment = std::max(TyAlign, StructAlignment);
58 MemberOffsets[i] = StructSize;
63 if (StructAlignment == 0) StructAlignment = 1;
67 if ((StructSize & (StructAlignment-1)) != 0)
76 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
77 assert(SI != &MemberOffsets[0] &&
"Offset not in structure type!");
79 assert(*SI <= Offset &&
"upper_bound didn't work");
80 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
81 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
82 "Upper bound didn't work!");
89 return SI-&MemberOffsets[0];
98 unsigned pref_align, uint32_t bit_width) {
99 assert(abi_align <= pref_align &&
"Preferred alignment worse than ABI!");
117 DataLayout::InvalidAlignmentElem = {
INVALID_ALIGN, 0, 0, 0 };
125 unsigned PrefAlign, uint32_t TypeByteWidth) {
126 assert(ABIAlign <= PrefAlign &&
"Preferred alignment worse than ABI!");
144 DataLayout::InvalidPointerElem = { 0U, 0U, 0U, ~0U };
178 StackNaturalAlign = 0;
179 ManglingMode = MM_None;
183 setAlignment((
AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
186 setPointerAlignment(0, 8, 8, 8);
188 parseSpecifier(Desc);
193 assert(!Str.
empty() &&
"parse error, string can't be empty here");
194 std::pair<StringRef, StringRef>
Split = Str.
split(Separator);
195 if (Split.second.empty() && Split.first != Str)
197 if (!Split.second.empty() && Split.first.empty())
218 void DataLayout::parseSpecifier(
StringRef Desc) {
219 StringRepresentation = Desc;
220 while (!Desc.
empty()) {
222 std::pair<StringRef, StringRef>
Split =
split(Desc,
'-');
226 Split =
split(Split.first,
':');
232 char Specifier = Tok.
front();
248 unsigned AddrSpace = Tok.
empty() ? 0 :
getInt(Tok);
249 if (!isUInt<24>(AddrSpace))
255 "Missing size specification for pointer in datalayout string");
256 Split =
split(Rest,
':');
264 "Missing alignment specification for pointer in datalayout string");
265 Split =
split(Rest,
':');
269 "Pointer ABI alignment must be a power of 2");
272 unsigned PointerPrefAlign = PointerABIAlign;
274 Split =
split(Rest,
':');
278 "Pointer preferred alignment must be a power of 2");
281 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
303 "Sized aggregate specification in datalayout string");
308 "Missing alignment specification in datalayout string");
309 Split =
split(Rest,
':');
313 "ABI alignment specification must be >0 for non-aggregate types");
316 unsigned PrefAlign = ABIAlign;
318 Split =
split(Rest,
':');
322 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
328 unsigned Width =
getInt(Tok);
331 "Zero width native integer type in datalayout string");
335 Split =
split(Rest,
':');
344 report_fatal_error(
"Unexpected trailing characters after mangling specifier in datalayout string");
353 ManglingMode = MM_ELF;
356 ManglingMode = MM_MachO;
359 ManglingMode = MM_Mips;
362 ManglingMode = MM_WinCOFF;
365 ManglingMode = MM_WinCOFFX86;
383 bool Ret = BigEndian == Other.BigEndian &&
384 StackNaturalAlign == Other.StackNaturalAlign &&
385 ManglingMode == Other.ManglingMode &&
386 LegalIntWidths == Other.LegalIntWidths &&
387 Alignments == Other.Alignments && Pointers == Other.Pointers;
393 DataLayout::setAlignment(
AlignTypeEnum align_type,
unsigned abi_align,
394 unsigned pref_align, uint32_t bit_width) {
395 if (!isUInt<24>(bit_width))
406 if (pref_align < abi_align)
408 "Preferred alignment cannot be less than the ABI alignment");
411 if (Elem.AlignType == (
unsigned)align_type &&
412 Elem.TypeBitWidth == bit_width) {
414 Elem.ABIAlign = abi_align;
415 Elem.PrefAlign = pref_align;
421 pref_align, bit_width));
425 DataLayout::findPointerLowerBound(uint32_t
AddressSpace) {
432 void DataLayout::setPointerAlignment(uint32_t AddrSpace,
unsigned ABIAlign,
434 uint32_t TypeByteWidth) {
435 if (PrefAlign < ABIAlign)
437 "Preferred alignment cannot be less than the ABI alignment");
440 if (I == Pointers.
end() || I->AddressSpace != AddrSpace) {
444 I->ABIAlign = ABIAlign;
445 I->PrefAlign = PrefAlign;
446 I->TypeByteWidth = TypeByteWidth;
452 unsigned DataLayout::getAlignmentInfo(
AlignTypeEnum AlignType,
453 uint32_t BitWidth,
bool ABIInfo,
456 int BestMatchIdx = -1;
458 for (
unsigned i = 0, e = Alignments.size(); i != e; ++i) {
459 if (Alignments[i].AlignType == (
unsigned)AlignType &&
460 Alignments[i].TypeBitWidth == BitWidth)
461 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
468 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
469 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
473 if (LargestInt == -1 ||
474 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
481 if (BestMatchIdx == -1) {
484 BestMatchIdx = LargestInt;
489 Align *= cast<VectorType>(Ty)->getNumElements();
492 if (Align & (Align-1))
504 if (BestMatchIdx == -1) {
506 if (Align & (Align-1))
512 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
513 : Alignments[BestMatchIdx].PrefAlign;
518 class StructLayoutMap {
520 LayoutInfoTy LayoutInfo;
525 for (
const auto &I : LayoutInfo) {
527 Value->~StructLayout();
533 return LayoutInfo[STy];
539 void DataLayout::clear() {
540 LegalIntWidths.
clear();
543 delete static_cast<StructLayoutMap *
>(LayoutMap);
553 LayoutMap =
new StructLayoutMap();
555 StructLayoutMap *STM =
static_cast<StructLayoutMap*
>(LayoutMap);
577 if (I == Pointers.
end() || I->AddressSpace != AS) {
578 I = findPointerLowerBound(0);
579 assert(I->AddressSpace == 0);
586 if (I == Pointers.
end() || I->AddressSpace != AS) {
587 I = findPointerLowerBound(0);
588 assert(I->AddressSpace == 0);
595 if (I == Pointers.
end() || I->AddressSpace != AS) {
596 I = findPointerLowerBound(0);
597 assert(I->AddressSpace == 0);
599 return I->TypeByteWidth;
604 "This should only be called with a pointer or pointer vector type");
620 unsigned DataLayout::getAlignment(
Type *Ty,
bool abi_or_pref)
const {
623 assert(Ty->
isSized() &&
"Cannot getTypeInfo() on a type that is unsized!");
631 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
637 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
641 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
646 unsigned Align = getAlignmentInfo(
AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
675 return getAlignment(Ty,
true);
681 return getAlignmentInfo(
INTEGER_ALIGN, BitWidth,
true,
nullptr);
685 return getAlignment(Ty,
false);
690 assert(!(Align & (Align-1)) &&
"Alignment is not a power of two!");
695 unsigned AddressSpace)
const {
701 "Expected a pointer or pointer vector type.");
704 if (
VectorType *VecTy = dyn_cast<VectorType>(Ty))
710 for (
unsigned LegalIntWidth : LegalIntWidths)
711 if (Width <= LegalIntWidth)
717 auto Max = std::max_element(LegalIntWidths.
begin(), LegalIntWidths.
end());
718 return Max != LegalIntWidths.
end() ? *
Max : 0;
724 assert(Ty->
isPointerTy() &&
"Illegal argument for getIndexedOffset()");
729 for (
unsigned CurIDX = 0, EndIDX = Indices.
size(); CurIDX != EndIDX;
731 if (
StructType *STy = dyn_cast<StructType>(*TI)) {
732 assert(Indices[CurIDX]->
getType() ==
734 "Illegal struct idx");
735 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
744 Ty = STy->getElementType(FieldNo);
747 Ty = cast<SequentialType>(Ty)->getElementType();
750 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
765 if (GVAlignment >= Alignment) {
766 Alignment = GVAlignment;
767 }
else if (GVAlignment != 0) {
772 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. ...
void reset(StringRef LayoutDescription)
Parse a data layout string (with fallback to default values).
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
size_t size() const
size - Get the string size.
AlignTypeEnum
Enum used to categorize the alignment types stored by LayoutAlignElem.
bool isOpaque() const
isOpaque - 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.
unsigned getPointerPrefAlignment(unsigned AS=0) const
Return target's alignment for stack-based pointers FIXME: The defaults need to be removed once all of...
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
bool isPtrOrPtrVectorTy() const
isPtrOrPtrVectorTy - 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)
1: 16-bit floating point type
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
bool operator==(const DataLayout &Other) const
static const char * getManglingComponent(const Triple &T)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
bool isOSWindows() const
Tests whether the OS is Windows.
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.
StructType - Class to represent struct types.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isSized(SmallPtrSetImpl< const Type * > *Visited=nullptr) const
isSized - Return true if it makes sense to take the size of this type.
uint64_t getIndexedOffset(Type *Ty, ArrayRef< Value * > Indices) const
Returns the offset from the beginning of the type for the specified indices.
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
getContext - 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
getTypeID - 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.
static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler, std::error_code EC, const Twine &Message)
static const LayoutAlignElem DefaultAlignments[]
Type * getElementType() const
size_t size() const
size - Get the array size.
Layout pointer alignment element.
uint64_t getElementOffset(unsigned Idx) const
10: Arbitrary bit width integers
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
This file contains the declarations for the subclasses of Constant, which represent the different fla...
6: 128-bit floating point type (two 64-bits, PowerPC)
Class to represent integer types.
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
uint64_t NextPowerOf2(uint64_t A)
NextPowerOf2 - Returns the next power of two (in 64-bits) that is strictly greater than A...
bool operator==(const LayoutAlignElem &rhs) const
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.
15: 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...
Module.h This file contains the declarations for the Module class.
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 cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(NoStrictAlign), cl::values(clEnumValN(StrictAlign,"aarch64-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"aarch64-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
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...
VectorType - Class to represent vector types.
bool hasInitializer() const
Definitions have initializers, declarations don't.
DataLayout(StringRef LayoutDescription)
Constructs a DataLayout from a specification string. See reset().
unsigned getLargestLegalIntTypeSize() const
Returns the size of largest legal integer type size, or 0 if none are set.
bool isPowerOf2_64(uint64_t Value)
isPowerOf2_64 - This function returns true if the argument is a power of two 0 (64 bit edition...
iterator insert(iterator I, T &&Elt)
uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
bool operator==(const PointerAlignElem &rhs) const
const Type * getScalarType() const LLVM_READONLY
getScalarType - If this is a vector type, return the element type, otherwise return 'this'...
PointerType * getType() const
Global values are always pointers.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
static IntegerType * getInt32Ty(LLVMContext &C)
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...
static unsigned inBytes(unsigned Bits)
Convert bits into bytes. Assert if not a byte width multiple.
3: 64-bit floating point type
char front() const
front - Get the first character in the string.
LLVM Value Representation.
static VectorType * get(Type *ElementType, unsigned NumElements)
VectorType::get - This static method is the primary way to construct an VectorType.
bool isUInt< 16 >(uint64_t x)
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
StringRef - Represent a constant reference to a string, i.e.
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 PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign, unsigned PrefAlign, uint32_t TypeByteWidth)
Initializer.
void init(const Module *M)
bool empty() const
empty - Check if the string is empty.
5: 128-bit floating point type (112-bit mantissa)
gep_type_iterator gep_type_begin(const User *GEP)