1 //===- DataLayout.cpp - Data size & alignment routines ---------------------==//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines layout properties related to datatype size/offset/alignment
12 // This structure should be created once, filled in if the defaults are not
13 // correct and then passed around by const&. None of the members functions
14 // require modification to the object.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/GetElementPtrTypeIterator.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/IR/Value.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/Error.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/TypeSize.h"
43 //===----------------------------------------------------------------------===//
44 // Support for StructLayout
45 //===----------------------------------------------------------------------===//
47 StructLayout::StructLayout(StructType
*ST
, const DataLayout
&DL
) {
48 assert(!ST
->isOpaque() && "Cannot get layout of opaque structs");
51 NumElements
= ST
->getNumElements();
53 // Loop over each of the elements, placing them in memory.
54 for (unsigned i
= 0, e
= NumElements
; i
!= e
; ++i
) {
55 Type
*Ty
= ST
->getElementType(i
);
56 const Align TyAlign
= ST
->isPacked() ? Align(1) : DL
.getABITypeAlign(Ty
);
58 // Add padding if necessary to align the data element properly.
59 if (!isAligned(TyAlign
, StructSize
)) {
61 StructSize
= alignTo(StructSize
, TyAlign
);
64 // Keep track of maximum alignment constraint.
65 StructAlignment
= std::max(TyAlign
, StructAlignment
);
67 getMemberOffsets()[i
] = StructSize
;
68 // Consume space for this data item
69 StructSize
+= DL
.getTypeAllocSize(Ty
).getFixedValue();
72 // Add padding to the end of the struct so that it could be put in an array
73 // and all array elements would be aligned correctly.
74 if (!isAligned(StructAlignment
, StructSize
)) {
76 StructSize
= alignTo(StructSize
, StructAlignment
);
80 /// getElementContainingOffset - Given a valid offset into the structure,
81 /// return the structure index that contains it.
82 unsigned StructLayout::getElementContainingOffset(uint64_t Offset
) const {
83 ArrayRef
<uint64_t> MemberOffsets
= getMemberOffsets();
84 auto SI
= llvm::upper_bound(MemberOffsets
, Offset
);
85 assert(SI
!= MemberOffsets
.begin() && "Offset not in structure type!");
87 assert(*SI
<= Offset
&& "upper_bound didn't work");
88 assert((SI
== MemberOffsets
.begin() || *(SI
- 1) <= Offset
) &&
89 (SI
+ 1 == MemberOffsets
.end() || *(SI
+ 1) > Offset
) &&
90 "Upper bound didn't work!");
92 // Multiple fields can have the same offset if any of them are zero sized.
93 // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
94 // at the i32 element, because it is the last element at that offset. This is
95 // the right one to return, because anything after it will have a higher
96 // offset, implying that this element is non-empty.
97 return SI
- MemberOffsets
.begin();
100 //===----------------------------------------------------------------------===//
101 // LayoutAlignElem, LayoutAlign support
102 //===----------------------------------------------------------------------===//
104 LayoutAlignElem
LayoutAlignElem::get(AlignTypeEnum align_type
, Align abi_align
,
105 Align pref_align
, uint32_t bit_width
) {
106 assert(abi_align
<= pref_align
&& "Preferred alignment worse than ABI!");
107 LayoutAlignElem retval
;
108 retval
.AlignType
= align_type
;
109 retval
.ABIAlign
= abi_align
;
110 retval
.PrefAlign
= pref_align
;
111 retval
.TypeBitWidth
= bit_width
;
116 LayoutAlignElem::operator==(const LayoutAlignElem
&rhs
) const {
117 return (AlignType
== rhs
.AlignType
118 && ABIAlign
== rhs
.ABIAlign
119 && PrefAlign
== rhs
.PrefAlign
120 && TypeBitWidth
== rhs
.TypeBitWidth
);
123 //===----------------------------------------------------------------------===//
124 // PointerAlignElem, PointerAlign support
125 //===----------------------------------------------------------------------===//
127 PointerAlignElem
PointerAlignElem::get(uint32_t AddressSpace
, Align ABIAlign
,
128 Align PrefAlign
, uint32_t TypeByteWidth
,
129 uint32_t IndexWidth
) {
130 assert(ABIAlign
<= PrefAlign
&& "Preferred alignment worse than ABI!");
131 PointerAlignElem retval
;
132 retval
.AddressSpace
= AddressSpace
;
133 retval
.ABIAlign
= ABIAlign
;
134 retval
.PrefAlign
= PrefAlign
;
135 retval
.TypeByteWidth
= TypeByteWidth
;
136 retval
.IndexWidth
= IndexWidth
;
141 PointerAlignElem::operator==(const PointerAlignElem
&rhs
) const {
142 return (ABIAlign
== rhs
.ABIAlign
143 && AddressSpace
== rhs
.AddressSpace
144 && PrefAlign
== rhs
.PrefAlign
145 && TypeByteWidth
== rhs
.TypeByteWidth
146 && IndexWidth
== rhs
.IndexWidth
);
149 //===----------------------------------------------------------------------===//
150 // DataLayout Class Implementation
151 //===----------------------------------------------------------------------===//
153 const char *DataLayout::getManglingComponent(const Triple
&T
) {
154 if (T
.isOSBinFormatMachO())
156 if (T
.isOSWindows() && T
.isOSBinFormatCOFF())
157 return T
.getArch() == Triple::x86
? "-m:x" : "-m:w";
158 if (T
.isOSBinFormatXCOFF())
163 static const LayoutAlignElem DefaultAlignments
[] = {
164 {INTEGER_ALIGN
, 1, Align(1), Align(1)}, // i1
165 {INTEGER_ALIGN
, 8, Align(1), Align(1)}, // i8
166 {INTEGER_ALIGN
, 16, Align(2), Align(2)}, // i16
167 {INTEGER_ALIGN
, 32, Align(4), Align(4)}, // i32
168 {INTEGER_ALIGN
, 64, Align(4), Align(8)}, // i64
169 {FLOAT_ALIGN
, 16, Align(2), Align(2)}, // half, bfloat
170 {FLOAT_ALIGN
, 32, Align(4), Align(4)}, // float
171 {FLOAT_ALIGN
, 64, Align(8), Align(8)}, // double
172 {FLOAT_ALIGN
, 128, Align(16), Align(16)}, // ppcf128, quad, ...
173 {VECTOR_ALIGN
, 64, Align(8), Align(8)}, // v2i32, v1i64, ...
174 {VECTOR_ALIGN
, 128, Align(16), Align(16)}, // v16i8, v8i16, v4i32, ...
175 {AGGREGATE_ALIGN
, 0, Align(1), Align(8)} // struct
178 void DataLayout::reset(StringRef Desc
) {
184 StackNaturalAlign
.reset();
185 ProgramAddrSpace
= 0;
186 DefaultGlobalsAddrSpace
= 0;
187 FunctionPtrAlign
.reset();
188 TheFunctionPtrAlignType
= FunctionPtrAlignType::Independent
;
189 ManglingMode
= MM_None
;
190 NonIntegralAddressSpaces
.clear();
192 // Default alignments
193 for (const LayoutAlignElem
&E
: DefaultAlignments
) {
194 if (Error Err
= setAlignment((AlignTypeEnum
)E
.AlignType
, E
.ABIAlign
,
195 E
.PrefAlign
, E
.TypeBitWidth
))
196 return report_fatal_error(std::move(Err
));
198 if (Error Err
= setPointerAlignment(0, Align(8), Align(8), 8, 8))
199 return report_fatal_error(std::move(Err
));
201 if (Error Err
= parseSpecifier(Desc
))
202 return report_fatal_error(std::move(Err
));
205 Expected
<DataLayout
> DataLayout::parse(StringRef LayoutDescription
) {
206 DataLayout
Layout("");
207 if (Error Err
= Layout
.parseSpecifier(LayoutDescription
))
208 return std::move(Err
);
212 static Error
reportError(const Twine
&Message
) {
213 return createStringError(inconvertibleErrorCode(), Message
);
216 /// Checked version of split, to ensure mandatory subparts.
217 static Error
split(StringRef Str
, char Separator
,
218 std::pair
<StringRef
, StringRef
> &Split
) {
219 assert(!Str
.empty() && "parse error, string can't be empty here");
220 Split
= Str
.split(Separator
);
221 if (Split
.second
.empty() && Split
.first
!= Str
)
222 return reportError("Trailing separator in datalayout string");
223 if (!Split
.second
.empty() && Split
.first
.empty())
224 return reportError("Expected token before separator in datalayout string");
225 return Error::success();
228 /// Get an unsigned integer, including error checks.
229 template <typename IntTy
> static Error
getInt(StringRef R
, IntTy
&Result
) {
230 bool error
= R
.getAsInteger(10, Result
); (void)error
;
232 return reportError("not a number, or does not fit in an unsigned int");
233 return Error::success();
236 /// Get an unsigned integer representing the number of bits and convert it into
237 /// bytes. Error out of not a byte width multiple.
238 template <typename IntTy
>
239 static Error
getIntInBytes(StringRef R
, IntTy
&Result
) {
240 if (Error Err
= getInt
<IntTy
>(R
, Result
))
243 return reportError("number of bits must be a byte width multiple");
245 return Error::success();
248 static Error
getAddrSpace(StringRef R
, unsigned &AddrSpace
) {
249 if (Error Err
= getInt(R
, AddrSpace
))
251 if (!isUInt
<24>(AddrSpace
))
252 return reportError("Invalid address space, must be a 24-bit integer");
253 return Error::success();
256 Error
DataLayout::parseSpecifier(StringRef Desc
) {
257 StringRepresentation
= std::string(Desc
);
258 while (!Desc
.empty()) {
260 std::pair
<StringRef
, StringRef
> Split
;
261 if (Error Err
= split(Desc
, '-', Split
))
266 if (Error Err
= split(Split
.first
, ':', Split
))
269 // Aliases used below.
270 StringRef
&Tok
= Split
.first
; // Current token.
271 StringRef
&Rest
= Split
.second
; // The rest of the string.
275 if (Error Err
= split(Rest
, ':', Split
))
279 if (Error Err
= getInt(Split
.first
, AS
))
282 return reportError("Address space 0 can never be non-integral");
283 NonIntegralAddressSpaces
.push_back(AS
);
284 } while (!Rest
.empty());
289 char Specifier
= Tok
.front();
294 // Deprecated, but ignoring here to preserve loading older textual llvm
305 unsigned AddrSpace
= 0;
307 if (Error Err
= getInt(Tok
, AddrSpace
))
309 if (!isUInt
<24>(AddrSpace
))
310 return reportError("Invalid address space, must be a 24bit integer");
315 "Missing size specification for pointer in datalayout string");
316 if (Error Err
= split(Rest
, ':', Split
))
318 unsigned PointerMemSize
;
319 if (Error Err
= getIntInBytes(Tok
, PointerMemSize
))
322 return reportError("Invalid pointer size of 0 bytes");
327 "Missing alignment specification for pointer in datalayout string");
328 if (Error Err
= split(Rest
, ':', Split
))
330 unsigned PointerABIAlign
;
331 if (Error Err
= getIntInBytes(Tok
, PointerABIAlign
))
333 if (!isPowerOf2_64(PointerABIAlign
))
334 return reportError("Pointer ABI alignment must be a power of 2");
336 // Size of index used in GEP for address calculation.
337 // The parameter is optional. By default it is equal to size of pointer.
338 unsigned IndexSize
= PointerMemSize
;
340 // Preferred alignment.
341 unsigned PointerPrefAlign
= PointerABIAlign
;
343 if (Error Err
= split(Rest
, ':', Split
))
345 if (Error Err
= getIntInBytes(Tok
, PointerPrefAlign
))
347 if (!isPowerOf2_64(PointerPrefAlign
))
349 "Pointer preferred alignment must be a power of 2");
351 // Now read the index. It is the second optional parameter here.
353 if (Error Err
= split(Rest
, ':', Split
))
355 if (Error Err
= getIntInBytes(Tok
, IndexSize
))
358 return reportError("Invalid index size of 0 bytes");
361 if (Error Err
= setPointerAlignment(
362 AddrSpace
, assumeAligned(PointerABIAlign
),
363 assumeAligned(PointerPrefAlign
), PointerMemSize
, IndexSize
))
371 AlignTypeEnum AlignType
;
373 default: llvm_unreachable("Unexpected specifier!");
374 case 'i': AlignType
= INTEGER_ALIGN
; break;
375 case 'v': AlignType
= VECTOR_ALIGN
; break;
376 case 'f': AlignType
= FLOAT_ALIGN
; break;
377 case 'a': AlignType
= AGGREGATE_ALIGN
; break;
383 if (Error Err
= getInt(Tok
, Size
))
386 if (AlignType
== AGGREGATE_ALIGN
&& Size
!= 0)
388 "Sized aggregate specification in datalayout string");
393 "Missing alignment specification in datalayout string");
394 if (Error Err
= split(Rest
, ':', Split
))
397 if (Error Err
= getIntInBytes(Tok
, ABIAlign
))
399 if (AlignType
!= AGGREGATE_ALIGN
&& !ABIAlign
)
401 "ABI alignment specification must be >0 for non-aggregate types");
403 if (!isUInt
<16>(ABIAlign
))
404 return reportError("Invalid ABI alignment, must be a 16bit integer");
405 if (ABIAlign
!= 0 && !isPowerOf2_64(ABIAlign
))
406 return reportError("Invalid ABI alignment, must be a power of 2");
408 // Preferred alignment.
409 unsigned PrefAlign
= ABIAlign
;
411 if (Error Err
= split(Rest
, ':', Split
))
413 if (Error Err
= getIntInBytes(Tok
, PrefAlign
))
417 if (!isUInt
<16>(PrefAlign
))
419 "Invalid preferred alignment, must be a 16bit integer");
420 if (PrefAlign
!= 0 && !isPowerOf2_64(PrefAlign
))
421 return reportError("Invalid preferred alignment, must be a power of 2");
423 if (Error Err
= setAlignment(AlignType
, assumeAligned(ABIAlign
),
424 assumeAligned(PrefAlign
), Size
))
429 case 'n': // Native integer types.
432 if (Error Err
= getInt(Tok
, Width
))
436 "Zero width native integer type in datalayout string");
437 LegalIntWidths
.push_back(Width
);
440 if (Error Err
= split(Rest
, ':', Split
))
444 case 'S': { // Stack natural alignment.
446 if (Error Err
= getIntInBytes(Tok
, Alignment
))
448 if (Alignment
!= 0 && !llvm::isPowerOf2_64(Alignment
))
449 return reportError("Alignment is neither 0 nor a power of 2");
450 StackNaturalAlign
= MaybeAlign(Alignment
);
454 switch (Tok
.front()) {
456 TheFunctionPtrAlignType
= FunctionPtrAlignType::Independent
;
459 TheFunctionPtrAlignType
= FunctionPtrAlignType::MultipleOfFunctionAlign
;
462 return reportError("Unknown function pointer alignment type in "
463 "datalayout string");
467 if (Error Err
= getIntInBytes(Tok
, Alignment
))
469 if (Alignment
!= 0 && !llvm::isPowerOf2_64(Alignment
))
470 return reportError("Alignment is neither 0 nor a power of 2");
471 FunctionPtrAlign
= MaybeAlign(Alignment
);
474 case 'P': { // Function address space.
475 if (Error Err
= getAddrSpace(Tok
, ProgramAddrSpace
))
479 case 'A': { // Default stack/alloca address space.
480 if (Error Err
= getAddrSpace(Tok
, AllocaAddrSpace
))
484 case 'G': { // Default address space for global variables.
485 if (Error Err
= getAddrSpace(Tok
, DefaultGlobalsAddrSpace
))
491 return reportError("Unexpected trailing characters after mangling "
492 "specifier in datalayout string");
494 return reportError("Expected mangling specifier in datalayout string");
496 return reportError("Unknown mangling specifier in datalayout string");
499 return reportError("Unknown mangling in datalayout string");
501 ManglingMode
= MM_ELF
;
504 ManglingMode
= MM_MachO
;
507 ManglingMode
= MM_Mips
;
510 ManglingMode
= MM_WinCOFF
;
513 ManglingMode
= MM_WinCOFFX86
;
516 ManglingMode
= MM_XCOFF
;
521 return reportError("Unknown specifier in datalayout string");
526 return Error::success();
529 DataLayout::DataLayout(const Module
*M
) {
533 void DataLayout::init(const Module
*M
) { *this = M
->getDataLayout(); }
535 bool DataLayout::operator==(const DataLayout
&Other
) const {
536 bool Ret
= BigEndian
== Other
.BigEndian
&&
537 AllocaAddrSpace
== Other
.AllocaAddrSpace
&&
538 StackNaturalAlign
== Other
.StackNaturalAlign
&&
539 ProgramAddrSpace
== Other
.ProgramAddrSpace
&&
540 DefaultGlobalsAddrSpace
== Other
.DefaultGlobalsAddrSpace
&&
541 FunctionPtrAlign
== Other
.FunctionPtrAlign
&&
542 TheFunctionPtrAlignType
== Other
.TheFunctionPtrAlignType
&&
543 ManglingMode
== Other
.ManglingMode
&&
544 LegalIntWidths
== Other
.LegalIntWidths
&&
545 Alignments
== Other
.Alignments
&& Pointers
== Other
.Pointers
;
546 // Note: getStringRepresentation() might differs, it is not canonicalized
550 DataLayout::AlignmentsTy::iterator
551 DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType
,
553 auto Pair
= std::make_pair((unsigned)AlignType
, BitWidth
);
554 return partition_point(Alignments
, [=](const LayoutAlignElem
&E
) {
555 return std::make_pair(E
.AlignType
, E
.TypeBitWidth
) < Pair
;
559 Error
DataLayout::setAlignment(AlignTypeEnum align_type
, Align abi_align
,
560 Align pref_align
, uint32_t bit_width
) {
561 // AlignmentsTy::ABIAlign and AlignmentsTy::PrefAlign were once stored as
562 // uint16_t, it is unclear if there are requirements for alignment to be less
563 // than 2^16 other than storage. In the meantime we leave the restriction as
564 // an assert. See D67400 for context.
565 assert(Log2(abi_align
) < 16 && Log2(pref_align
) < 16 && "Alignment too big");
566 if (!isUInt
<24>(bit_width
))
567 return reportError("Invalid bit width, must be a 24bit integer");
568 if (pref_align
< abi_align
)
570 "Preferred alignment cannot be less than the ABI alignment");
572 AlignmentsTy::iterator I
= findAlignmentLowerBound(align_type
, bit_width
);
573 if (I
!= Alignments
.end() &&
574 I
->AlignType
== (unsigned)align_type
&& I
->TypeBitWidth
== bit_width
) {
575 // Update the abi, preferred alignments.
576 I
->ABIAlign
= abi_align
;
577 I
->PrefAlign
= pref_align
;
579 // Insert before I to keep the vector sorted.
580 Alignments
.insert(I
, LayoutAlignElem::get(align_type
, abi_align
,
581 pref_align
, bit_width
));
583 return Error::success();
586 const PointerAlignElem
&
587 DataLayout::getPointerAlignElem(uint32_t AddressSpace
) const {
588 if (AddressSpace
!= 0) {
589 auto I
= lower_bound(Pointers
, AddressSpace
,
590 [](const PointerAlignElem
&A
, uint32_t AddressSpace
) {
591 return A
.AddressSpace
< AddressSpace
;
593 if (I
!= Pointers
.end() && I
->AddressSpace
== AddressSpace
)
597 assert(Pointers
[0].AddressSpace
== 0);
601 Error
DataLayout::setPointerAlignment(uint32_t AddrSpace
, Align ABIAlign
,
602 Align PrefAlign
, uint32_t TypeByteWidth
,
603 uint32_t IndexWidth
) {
604 if (PrefAlign
< ABIAlign
)
606 "Preferred alignment cannot be less than the ABI alignment");
608 auto I
= lower_bound(Pointers
, AddrSpace
,
609 [](const PointerAlignElem
&A
, uint32_t AddressSpace
) {
610 return A
.AddressSpace
< AddressSpace
;
612 if (I
== Pointers
.end() || I
->AddressSpace
!= AddrSpace
) {
613 Pointers
.insert(I
, PointerAlignElem::get(AddrSpace
, ABIAlign
, PrefAlign
,
614 TypeByteWidth
, IndexWidth
));
616 I
->ABIAlign
= ABIAlign
;
617 I
->PrefAlign
= PrefAlign
;
618 I
->TypeByteWidth
= TypeByteWidth
;
619 I
->IndexWidth
= IndexWidth
;
621 return Error::success();
624 Align
DataLayout::getIntegerAlignment(uint32_t BitWidth
,
625 bool abi_or_pref
) const {
626 auto I
= findAlignmentLowerBound(INTEGER_ALIGN
, BitWidth
);
627 // If we don't have an exact match, use alignment of next larger integer
628 // type. If there is none, use alignment of largest integer type by going
630 if (I
== Alignments
.end() || I
->AlignType
!= INTEGER_ALIGN
)
632 assert(I
->AlignType
== INTEGER_ALIGN
&& "Must be integer alignment");
633 return abi_or_pref
? I
->ABIAlign
: I
->PrefAlign
;
638 class StructLayoutMap
{
639 using LayoutInfoTy
= DenseMap
<StructType
*, StructLayout
*>;
640 LayoutInfoTy LayoutInfo
;
644 // Remove any layouts.
645 for (const auto &I
: LayoutInfo
) {
646 StructLayout
*Value
= I
.second
;
647 Value
->~StructLayout();
652 StructLayout
*&operator[](StructType
*STy
) {
653 return LayoutInfo
[STy
];
657 } // end anonymous namespace
659 void DataLayout::clear() {
660 LegalIntWidths
.clear();
663 delete static_cast<StructLayoutMap
*>(LayoutMap
);
667 DataLayout::~DataLayout() {
671 const StructLayout
*DataLayout::getStructLayout(StructType
*Ty
) const {
673 LayoutMap
= new StructLayoutMap();
675 StructLayoutMap
*STM
= static_cast<StructLayoutMap
*>(LayoutMap
);
676 StructLayout
*&SL
= (*STM
)[Ty
];
679 // Otherwise, create the struct layout. Because it is variable length, we
680 // malloc it, then use placement new.
681 StructLayout
*L
= (StructLayout
*)safe_malloc(
682 StructLayout::totalSizeToAlloc
<uint64_t>(Ty
->getNumElements()));
684 // Set SL before calling StructLayout's ctor. The ctor could cause other
685 // entries to be added to TheMap, invalidating our reference.
688 new (L
) StructLayout(Ty
, *this);
693 Align
DataLayout::getPointerABIAlignment(unsigned AS
) const {
694 return getPointerAlignElem(AS
).ABIAlign
;
697 Align
DataLayout::getPointerPrefAlignment(unsigned AS
) const {
698 return getPointerAlignElem(AS
).PrefAlign
;
701 unsigned DataLayout::getPointerSize(unsigned AS
) const {
702 return getPointerAlignElem(AS
).TypeByteWidth
;
705 unsigned DataLayout::getMaxPointerSize() const {
706 unsigned MaxPointerSize
= 0;
707 for (auto &P
: Pointers
)
708 MaxPointerSize
= std::max(MaxPointerSize
, P
.TypeByteWidth
);
710 return MaxPointerSize
;
713 unsigned DataLayout::getPointerTypeSizeInBits(Type
*Ty
) const {
714 assert(Ty
->isPtrOrPtrVectorTy() &&
715 "This should only be called with a pointer or pointer vector type");
716 Ty
= Ty
->getScalarType();
717 return getPointerSizeInBits(cast
<PointerType
>(Ty
)->getAddressSpace());
720 unsigned DataLayout::getIndexSize(unsigned AS
) const {
721 return getPointerAlignElem(AS
).IndexWidth
;
724 unsigned DataLayout::getIndexTypeSizeInBits(Type
*Ty
) const {
725 assert(Ty
->isPtrOrPtrVectorTy() &&
726 "This should only be called with a pointer or pointer vector type");
727 Ty
= Ty
->getScalarType();
728 return getIndexSizeInBits(cast
<PointerType
>(Ty
)->getAddressSpace());
732 \param abi_or_pref Flag that determines which alignment is returned. true
733 returns the ABI alignment, false returns the preferred alignment.
734 \param Ty The underlying type for which alignment is determined.
736 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
737 == false) for the requested type \a Ty.
739 Align
DataLayout::getAlignment(Type
*Ty
, bool abi_or_pref
) const {
740 assert(Ty
->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
741 switch (Ty
->getTypeID()) {
742 // Early escape for the non-numeric types.
743 case Type::LabelTyID
:
744 return abi_or_pref
? getPointerABIAlignment(0) : getPointerPrefAlignment(0);
745 case Type::PointerTyID
: {
746 unsigned AS
= cast
<PointerType
>(Ty
)->getAddressSpace();
747 return abi_or_pref
? getPointerABIAlignment(AS
)
748 : getPointerPrefAlignment(AS
);
750 case Type::ArrayTyID
:
751 return getAlignment(cast
<ArrayType
>(Ty
)->getElementType(), abi_or_pref
);
753 case Type::StructTyID
: {
754 // Packed structure types always have an ABI alignment of one.
755 if (cast
<StructType
>(Ty
)->isPacked() && abi_or_pref
)
758 // Get the layout annotation... which is lazily created on demand.
759 const StructLayout
*Layout
= getStructLayout(cast
<StructType
>(Ty
));
760 const LayoutAlignElem
&AggregateAlign
= Alignments
[0];
761 assert(AggregateAlign
.AlignType
== AGGREGATE_ALIGN
&&
762 "Aggregate alignment must be first alignment entry");
764 abi_or_pref
? AggregateAlign
.ABIAlign
: AggregateAlign
.PrefAlign
;
765 return std::max(Align
, Layout
->getAlignment());
767 case Type::IntegerTyID
:
768 return getIntegerAlignment(Ty
->getIntegerBitWidth(), abi_or_pref
);
770 case Type::BFloatTyID
:
771 case Type::FloatTyID
:
772 case Type::DoubleTyID
:
773 // PPC_FP128TyID and FP128TyID have different data contents, but the
774 // same size and alignment, so they look the same here.
775 case Type::PPC_FP128TyID
:
776 case Type::FP128TyID
:
777 case Type::X86_FP80TyID
: {
778 unsigned BitWidth
= getTypeSizeInBits(Ty
).getFixedSize();
779 auto I
= findAlignmentLowerBound(FLOAT_ALIGN
, BitWidth
);
780 if (I
!= Alignments
.end() && I
->AlignType
== FLOAT_ALIGN
&&
781 I
->TypeBitWidth
== BitWidth
)
782 return abi_or_pref
? I
->ABIAlign
: I
->PrefAlign
;
784 // If we still couldn't find a reasonable default alignment, fall back
785 // to a simple heuristic that the alignment is the first power of two
786 // greater-or-equal to the store size of the type. This is a reasonable
787 // approximation of reality, and if the user wanted something less
788 // less conservative, they should have specified it explicitly in the data
790 return Align(PowerOf2Ceil(BitWidth
/ 8));
792 case Type::X86_MMXTyID
:
793 case Type::FixedVectorTyID
:
794 case Type::ScalableVectorTyID
: {
795 unsigned BitWidth
= getTypeSizeInBits(Ty
).getKnownMinSize();
796 auto I
= findAlignmentLowerBound(VECTOR_ALIGN
, BitWidth
);
797 if (I
!= Alignments
.end() && I
->AlignType
== VECTOR_ALIGN
&&
798 I
->TypeBitWidth
== BitWidth
)
799 return abi_or_pref
? I
->ABIAlign
: I
->PrefAlign
;
801 // By default, use natural alignment for vector types. This is consistent
802 // with what clang and llvm-gcc do.
804 // We're only calculating a natural alignment, so it doesn't have to be
805 // based on the full size for scalable vectors. Using the minimum element
806 // count should be enough here.
807 return Align(PowerOf2Ceil(getTypeStoreSize(Ty
).getKnownMinSize()));
809 case Type::X86_AMXTyID
:
812 llvm_unreachable("Bad type for getAlignment!!!");
816 /// TODO: Remove this function once the transition to Align is over.
817 unsigned DataLayout::getABITypeAlignment(Type
*Ty
) const {
818 return getABITypeAlign(Ty
).value();
821 Align
DataLayout::getABITypeAlign(Type
*Ty
) const {
822 return getAlignment(Ty
, true);
825 /// TODO: Remove this function once the transition to Align is over.
826 unsigned DataLayout::getPrefTypeAlignment(Type
*Ty
) const {
827 return getPrefTypeAlign(Ty
).value();
830 Align
DataLayout::getPrefTypeAlign(Type
*Ty
) const {
831 return getAlignment(Ty
, false);
834 IntegerType
*DataLayout::getIntPtrType(LLVMContext
&C
,
835 unsigned AddressSpace
) const {
836 return IntegerType::get(C
, getPointerSizeInBits(AddressSpace
));
839 Type
*DataLayout::getIntPtrType(Type
*Ty
) const {
840 assert(Ty
->isPtrOrPtrVectorTy() &&
841 "Expected a pointer or pointer vector type.");
842 unsigned NumBits
= getPointerTypeSizeInBits(Ty
);
843 IntegerType
*IntTy
= IntegerType::get(Ty
->getContext(), NumBits
);
844 if (VectorType
*VecTy
= dyn_cast
<VectorType
>(Ty
))
845 return VectorType::get(IntTy
, VecTy
);
849 Type
*DataLayout::getSmallestLegalIntType(LLVMContext
&C
, unsigned Width
) const {
850 for (unsigned LegalIntWidth
: LegalIntWidths
)
851 if (Width
<= LegalIntWidth
)
852 return Type::getIntNTy(C
, LegalIntWidth
);
856 unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
857 auto Max
= std::max_element(LegalIntWidths
.begin(), LegalIntWidths
.end());
858 return Max
!= LegalIntWidths
.end() ? *Max
: 0;
861 Type
*DataLayout::getIndexType(Type
*Ty
) const {
862 assert(Ty
->isPtrOrPtrVectorTy() &&
863 "Expected a pointer or pointer vector type.");
864 unsigned NumBits
= getIndexTypeSizeInBits(Ty
);
865 IntegerType
*IntTy
= IntegerType::get(Ty
->getContext(), NumBits
);
866 if (VectorType
*VecTy
= dyn_cast
<VectorType
>(Ty
))
867 return VectorType::get(IntTy
, VecTy
);
871 int64_t DataLayout::getIndexedOffsetInType(Type
*ElemTy
,
872 ArrayRef
<Value
*> Indices
) const {
875 generic_gep_type_iterator
<Value
* const*>
876 GTI
= gep_type_begin(ElemTy
, Indices
),
877 GTE
= gep_type_end(ElemTy
, Indices
);
878 for (; GTI
!= GTE
; ++GTI
) {
879 Value
*Idx
= GTI
.getOperand();
880 if (StructType
*STy
= GTI
.getStructTypeOrNull()) {
881 assert(Idx
->getType()->isIntegerTy(32) && "Illegal struct idx");
882 unsigned FieldNo
= cast
<ConstantInt
>(Idx
)->getZExtValue();
884 // Get structure layout information...
885 const StructLayout
*Layout
= getStructLayout(STy
);
887 // Add in the offset, as calculated by the structure layout info...
888 Result
+= Layout
->getElementOffset(FieldNo
);
890 // Get the array index and the size of each array element.
891 if (int64_t arrayIdx
= cast
<ConstantInt
>(Idx
)->getSExtValue())
892 Result
+= arrayIdx
* getTypeAllocSize(GTI
.getIndexedType());
899 /// getPreferredAlign - Return the preferred alignment of the specified global.
900 /// This includes an explicitly requested alignment (if the global has one).
901 Align
DataLayout::getPreferredAlign(const GlobalVariable
*GV
) const {
902 MaybeAlign GVAlignment
= GV
->getAlign();
903 // If a section is specified, always precisely honor explicit alignment,
904 // so we don't insert padding into a section we don't control.
905 if (GVAlignment
&& GV
->hasSection())
908 // If no explicit alignment is specified, compute the alignment based on
909 // the IR type. If an alignment is specified, increase it to match the ABI
910 // alignment of the IR type.
912 // FIXME: Not sure it makes sense to use the alignment of the type if
913 // there's already an explicit alignment specification.
914 Type
*ElemType
= GV
->getValueType();
915 Align Alignment
= getPrefTypeAlign(ElemType
);
917 if (*GVAlignment
>= Alignment
)
918 Alignment
= *GVAlignment
;
920 Alignment
= std::max(*GVAlignment
, getABITypeAlign(ElemType
));
923 // If no explicit alignment is specified, and the global is large, increase
924 // the alignment to 16.
925 // FIXME: Why 16, specifically?
926 if (GV
->hasInitializer() && !GVAlignment
) {
927 if (Alignment
< Align(16)) {
928 // If the global is not external, see if it is large. If so, give it a
930 if (getTypeSizeInBits(ElemType
) > 128)
931 Alignment
= Align(16); // 16-byte alignment.