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/ErrorHandling.h"
31 #include "llvm/Support/MathExtras.h"
41 //===----------------------------------------------------------------------===//
42 // Support for StructLayout
43 //===----------------------------------------------------------------------===//
45 StructLayout::StructLayout(StructType
*ST
, const DataLayout
&DL
) {
46 assert(!ST
->isOpaque() && "Cannot get layout of opaque structs");
50 NumElements
= ST
->getNumElements();
52 // Loop over each of the elements, placing them in memory.
53 for (unsigned i
= 0, e
= NumElements
; i
!= e
; ++i
) {
54 Type
*Ty
= ST
->getElementType(i
);
55 unsigned TyAlign
= ST
->isPacked() ? 1 : DL
.getABITypeAlignment(Ty
);
57 // Add padding if necessary to align the data element properly.
58 if ((StructSize
& (TyAlign
-1)) != 0) {
60 StructSize
= alignTo(StructSize
, TyAlign
);
63 // Keep track of maximum alignment constraint.
64 StructAlignment
= std::max(TyAlign
, StructAlignment
);
66 MemberOffsets
[i
] = StructSize
;
67 StructSize
+= DL
.getTypeAllocSize(Ty
); // Consume space for this data item
70 // Empty structures have alignment of 1 byte.
71 if (StructAlignment
== 0) StructAlignment
= 1;
73 // Add padding to the end of the struct so that it could be put in an array
74 // and all array elements would be aligned correctly.
75 if ((StructSize
& (StructAlignment
-1)) != 0) {
77 StructSize
= alignTo(StructSize
, StructAlignment
);
81 /// getElementContainingOffset - Given a valid offset into the structure,
82 /// return the structure index that contains it.
83 unsigned StructLayout::getElementContainingOffset(uint64_t Offset
) const {
85 std::upper_bound(&MemberOffsets
[0], &MemberOffsets
[NumElements
], Offset
);
86 assert(SI
!= &MemberOffsets
[0] && "Offset not in structure type!");
88 assert(*SI
<= Offset
&& "upper_bound didn't work");
89 assert((SI
== &MemberOffsets
[0] || *(SI
-1) <= Offset
) &&
90 (SI
+1 == &MemberOffsets
[NumElements
] || *(SI
+1) > Offset
) &&
91 "Upper bound didn't work!");
93 // Multiple fields can have the same offset if any of them are zero sized.
94 // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
95 // at the i32 element, because it is the last element at that offset. This is
96 // the right one to return, because anything after it will have a higher
97 // offset, implying that this element is non-empty.
98 return SI
-&MemberOffsets
[0];
101 //===----------------------------------------------------------------------===//
102 // LayoutAlignElem, LayoutAlign support
103 //===----------------------------------------------------------------------===//
106 LayoutAlignElem::get(AlignTypeEnum align_type
, unsigned abi_align
,
107 unsigned pref_align
, uint32_t bit_width
) {
108 assert(abi_align
<= pref_align
&& "Preferred alignment worse than ABI!");
109 LayoutAlignElem retval
;
110 retval
.AlignType
= align_type
;
111 retval
.ABIAlign
= abi_align
;
112 retval
.PrefAlign
= pref_align
;
113 retval
.TypeBitWidth
= bit_width
;
118 LayoutAlignElem::operator==(const LayoutAlignElem
&rhs
) const {
119 return (AlignType
== rhs
.AlignType
120 && ABIAlign
== rhs
.ABIAlign
121 && PrefAlign
== rhs
.PrefAlign
122 && TypeBitWidth
== rhs
.TypeBitWidth
);
125 //===----------------------------------------------------------------------===//
126 // PointerAlignElem, PointerAlign support
127 //===----------------------------------------------------------------------===//
130 PointerAlignElem::get(uint32_t AddressSpace
, unsigned ABIAlign
,
131 unsigned PrefAlign
, uint32_t TypeByteWidth
,
132 uint32_t IndexWidth
) {
133 assert(ABIAlign
<= PrefAlign
&& "Preferred alignment worse than ABI!");
134 PointerAlignElem retval
;
135 retval
.AddressSpace
= AddressSpace
;
136 retval
.ABIAlign
= ABIAlign
;
137 retval
.PrefAlign
= PrefAlign
;
138 retval
.TypeByteWidth
= TypeByteWidth
;
139 retval
.IndexWidth
= IndexWidth
;
144 PointerAlignElem::operator==(const PointerAlignElem
&rhs
) const {
145 return (ABIAlign
== rhs
.ABIAlign
146 && AddressSpace
== rhs
.AddressSpace
147 && PrefAlign
== rhs
.PrefAlign
148 && TypeByteWidth
== rhs
.TypeByteWidth
149 && IndexWidth
== rhs
.IndexWidth
);
152 //===----------------------------------------------------------------------===//
153 // DataLayout Class Implementation
154 //===----------------------------------------------------------------------===//
156 const char *DataLayout::getManglingComponent(const Triple
&T
) {
157 if (T
.isOSBinFormatMachO())
159 if (T
.isOSWindows() && T
.isOSBinFormatCOFF())
160 return T
.getArch() == Triple::x86
? "-m:x" : "-m:w";
164 static const LayoutAlignElem DefaultAlignments
[] = {
165 { INTEGER_ALIGN
, 1, 1, 1 }, // i1
166 { INTEGER_ALIGN
, 8, 1, 1 }, // i8
167 { INTEGER_ALIGN
, 16, 2, 2 }, // i16
168 { INTEGER_ALIGN
, 32, 4, 4 }, // i32
169 { INTEGER_ALIGN
, 64, 4, 8 }, // i64
170 { FLOAT_ALIGN
, 16, 2, 2 }, // half
171 { FLOAT_ALIGN
, 32, 4, 4 }, // float
172 { FLOAT_ALIGN
, 64, 8, 8 }, // double
173 { FLOAT_ALIGN
, 128, 16, 16 }, // ppcf128, quad, ...
174 { VECTOR_ALIGN
, 64, 8, 8 }, // v2i32, v1i64, ...
175 { VECTOR_ALIGN
, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
176 { AGGREGATE_ALIGN
, 0, 0, 8 } // struct
179 void DataLayout::reset(StringRef Desc
) {
185 StackNaturalAlign
= 0;
186 ProgramAddrSpace
= 0;
187 FunctionPtrAlign
= 0;
188 TheFunctionPtrAlignType
= FunctionPtrAlignType::Independent
;
189 ManglingMode
= MM_None
;
190 NonIntegralAddressSpaces
.clear();
192 // Default alignments
193 for (const LayoutAlignElem
&E
: DefaultAlignments
) {
194 setAlignment((AlignTypeEnum
)E
.AlignType
, E
.ABIAlign
, E
.PrefAlign
,
197 setPointerAlignment(0, 8, 8, 8, 8);
199 parseSpecifier(Desc
);
202 /// Checked version of split, to ensure mandatory subparts.
203 static std::pair
<StringRef
, StringRef
> split(StringRef Str
, char Separator
) {
204 assert(!Str
.empty() && "parse error, string can't be empty here");
205 std::pair
<StringRef
, StringRef
> Split
= Str
.split(Separator
);
206 if (Split
.second
.empty() && Split
.first
!= Str
)
207 report_fatal_error("Trailing separator in datalayout string");
208 if (!Split
.second
.empty() && Split
.first
.empty())
209 report_fatal_error("Expected token before separator in datalayout string");
213 /// Get an unsigned integer, including error checks.
214 static unsigned getInt(StringRef R
) {
216 bool error
= R
.getAsInteger(10, Result
); (void)error
;
218 report_fatal_error("not a number, or does not fit in an unsigned int");
222 /// Convert bits into bytes. Assert if not a byte width multiple.
223 static unsigned inBytes(unsigned Bits
) {
225 report_fatal_error("number of bits must be a byte width multiple");
229 static unsigned getAddrSpace(StringRef R
) {
230 unsigned AddrSpace
= getInt(R
);
231 if (!isUInt
<24>(AddrSpace
))
232 report_fatal_error("Invalid address space, must be a 24-bit integer");
236 void DataLayout::parseSpecifier(StringRef Desc
) {
237 StringRepresentation
= Desc
;
238 while (!Desc
.empty()) {
240 std::pair
<StringRef
, StringRef
> Split
= split(Desc
, '-');
244 Split
= split(Split
.first
, ':');
246 // Aliases used below.
247 StringRef
&Tok
= Split
.first
; // Current token.
248 StringRef
&Rest
= Split
.second
; // The rest of the string.
252 Split
= split(Rest
, ':');
254 unsigned AS
= getInt(Split
.first
);
256 report_fatal_error("Address space 0 can never be non-integral");
257 NonIntegralAddressSpaces
.push_back(AS
);
258 } while (!Rest
.empty());
263 char Specifier
= Tok
.front();
268 // Ignored for backward compatibility.
269 // FIXME: remove this on LLVM 4.0.
279 unsigned AddrSpace
= Tok
.empty() ? 0 : getInt(Tok
);
280 if (!isUInt
<24>(AddrSpace
))
281 report_fatal_error("Invalid address space, must be a 24bit integer");
286 "Missing size specification for pointer in datalayout string");
287 Split
= split(Rest
, ':');
288 unsigned PointerMemSize
= inBytes(getInt(Tok
));
290 report_fatal_error("Invalid pointer size of 0 bytes");
295 "Missing alignment specification for pointer in datalayout string");
296 Split
= split(Rest
, ':');
297 unsigned PointerABIAlign
= inBytes(getInt(Tok
));
298 if (!isPowerOf2_64(PointerABIAlign
))
300 "Pointer ABI alignment must be a power of 2");
302 // Size of index used in GEP for address calculation.
303 // The parameter is optional. By default it is equal to size of pointer.
304 unsigned IndexSize
= PointerMemSize
;
306 // Preferred alignment.
307 unsigned PointerPrefAlign
= PointerABIAlign
;
309 Split
= split(Rest
, ':');
310 PointerPrefAlign
= inBytes(getInt(Tok
));
311 if (!isPowerOf2_64(PointerPrefAlign
))
313 "Pointer preferred alignment must be a power of 2");
315 // Now read the index. It is the second optional parameter here.
317 Split
= split(Rest
, ':');
318 IndexSize
= inBytes(getInt(Tok
));
320 report_fatal_error("Invalid index size of 0 bytes");
323 setPointerAlignment(AddrSpace
, PointerABIAlign
, PointerPrefAlign
,
324 PointerMemSize
, IndexSize
);
331 AlignTypeEnum AlignType
;
333 default: llvm_unreachable("Unexpected specifier!");
334 case 'i': AlignType
= INTEGER_ALIGN
; break;
335 case 'v': AlignType
= VECTOR_ALIGN
; break;
336 case 'f': AlignType
= FLOAT_ALIGN
; break;
337 case 'a': AlignType
= AGGREGATE_ALIGN
; break;
341 unsigned Size
= Tok
.empty() ? 0 : getInt(Tok
);
343 if (AlignType
== AGGREGATE_ALIGN
&& Size
!= 0)
345 "Sized aggregate specification in datalayout string");
350 "Missing alignment specification in datalayout string");
351 Split
= split(Rest
, ':');
352 unsigned ABIAlign
= inBytes(getInt(Tok
));
353 if (AlignType
!= AGGREGATE_ALIGN
&& !ABIAlign
)
355 "ABI alignment specification must be >0 for non-aggregate types");
357 // Preferred alignment.
358 unsigned PrefAlign
= ABIAlign
;
360 Split
= split(Rest
, ':');
361 PrefAlign
= inBytes(getInt(Tok
));
364 setAlignment(AlignType
, ABIAlign
, PrefAlign
, Size
);
368 case 'n': // Native integer types.
370 unsigned Width
= getInt(Tok
);
373 "Zero width native integer type in datalayout string");
374 LegalIntWidths
.push_back(Width
);
377 Split
= split(Rest
, ':');
380 case 'S': { // Stack natural alignment.
381 StackNaturalAlign
= inBytes(getInt(Tok
));
385 switch (Tok
.front()) {
387 TheFunctionPtrAlignType
= FunctionPtrAlignType::Independent
;
390 TheFunctionPtrAlignType
= FunctionPtrAlignType::MultipleOfFunctionAlign
;
393 report_fatal_error("Unknown function pointer alignment type in "
394 "datalayout string");
397 FunctionPtrAlign
= inBytes(getInt(Tok
));
400 case 'P': { // Function address space.
401 ProgramAddrSpace
= getAddrSpace(Tok
);
404 case 'A': { // Default stack/alloca address space.
405 AllocaAddrSpace
= getAddrSpace(Tok
);
410 report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
412 report_fatal_error("Expected mangling specifier in datalayout string");
414 report_fatal_error("Unknown mangling specifier in datalayout string");
417 report_fatal_error("Unknown mangling in datalayout string");
419 ManglingMode
= MM_ELF
;
422 ManglingMode
= MM_MachO
;
425 ManglingMode
= MM_Mips
;
428 ManglingMode
= MM_WinCOFF
;
431 ManglingMode
= MM_WinCOFFX86
;
436 report_fatal_error("Unknown specifier in datalayout string");
442 DataLayout::DataLayout(const Module
*M
) {
446 void DataLayout::init(const Module
*M
) { *this = M
->getDataLayout(); }
448 bool DataLayout::operator==(const DataLayout
&Other
) const {
449 bool Ret
= BigEndian
== Other
.BigEndian
&&
450 AllocaAddrSpace
== Other
.AllocaAddrSpace
&&
451 StackNaturalAlign
== Other
.StackNaturalAlign
&&
452 ProgramAddrSpace
== Other
.ProgramAddrSpace
&&
453 FunctionPtrAlign
== Other
.FunctionPtrAlign
&&
454 TheFunctionPtrAlignType
== Other
.TheFunctionPtrAlignType
&&
455 ManglingMode
== Other
.ManglingMode
&&
456 LegalIntWidths
== Other
.LegalIntWidths
&&
457 Alignments
== Other
.Alignments
&& Pointers
== Other
.Pointers
;
458 // Note: getStringRepresentation() might differs, it is not canonicalized
462 DataLayout::AlignmentsTy::iterator
463 DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType
,
465 auto Pair
= std::make_pair((unsigned)AlignType
, BitWidth
);
466 return std::lower_bound(Alignments
.begin(), Alignments
.end(), Pair
,
467 [](const LayoutAlignElem
&LHS
,
468 const std::pair
<unsigned, uint32_t> &RHS
) {
469 return std::tie(LHS
.AlignType
, LHS
.TypeBitWidth
) <
470 std::tie(RHS
.first
, RHS
.second
);
475 DataLayout::setAlignment(AlignTypeEnum align_type
, unsigned abi_align
,
476 unsigned pref_align
, uint32_t bit_width
) {
477 if (!isUInt
<24>(bit_width
))
478 report_fatal_error("Invalid bit width, must be a 24bit integer");
479 if (!isUInt
<16>(abi_align
))
480 report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
481 if (!isUInt
<16>(pref_align
))
482 report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
483 if (abi_align
!= 0 && !isPowerOf2_64(abi_align
))
484 report_fatal_error("Invalid ABI alignment, must be a power of 2");
485 if (pref_align
!= 0 && !isPowerOf2_64(pref_align
))
486 report_fatal_error("Invalid preferred alignment, must be a power of 2");
488 if (pref_align
< abi_align
)
490 "Preferred alignment cannot be less than the ABI alignment");
492 AlignmentsTy::iterator I
= findAlignmentLowerBound(align_type
, bit_width
);
493 if (I
!= Alignments
.end() &&
494 I
->AlignType
== (unsigned)align_type
&& I
->TypeBitWidth
== bit_width
) {
495 // Update the abi, preferred alignments.
496 I
->ABIAlign
= abi_align
;
497 I
->PrefAlign
= pref_align
;
499 // Insert before I to keep the vector sorted.
500 Alignments
.insert(I
, LayoutAlignElem::get(align_type
, abi_align
,
501 pref_align
, bit_width
));
505 DataLayout::PointersTy::iterator
506 DataLayout::findPointerLowerBound(uint32_t AddressSpace
) {
507 return std::lower_bound(Pointers
.begin(), Pointers
.end(), AddressSpace
,
508 [](const PointerAlignElem
&A
, uint32_t AddressSpace
) {
509 return A
.AddressSpace
< AddressSpace
;
513 void DataLayout::setPointerAlignment(uint32_t AddrSpace
, unsigned ABIAlign
,
514 unsigned PrefAlign
, uint32_t TypeByteWidth
,
515 uint32_t IndexWidth
) {
516 if (PrefAlign
< ABIAlign
)
518 "Preferred alignment cannot be less than the ABI alignment");
520 PointersTy::iterator I
= findPointerLowerBound(AddrSpace
);
521 if (I
== Pointers
.end() || I
->AddressSpace
!= AddrSpace
) {
522 Pointers
.insert(I
, PointerAlignElem::get(AddrSpace
, ABIAlign
, PrefAlign
,
523 TypeByteWidth
, IndexWidth
));
525 I
->ABIAlign
= ABIAlign
;
526 I
->PrefAlign
= PrefAlign
;
527 I
->TypeByteWidth
= TypeByteWidth
;
528 I
->IndexWidth
= IndexWidth
;
532 /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
533 /// preferred if ABIInfo = false) the layout wants for the specified datatype.
534 unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType
,
535 uint32_t BitWidth
, bool ABIInfo
,
537 AlignmentsTy::const_iterator I
= findAlignmentLowerBound(AlignType
, BitWidth
);
538 // See if we found an exact match. Of if we are looking for an integer type,
539 // but don't have an exact match take the next largest integer. This is where
540 // the lower_bound will point to when it fails an exact match.
541 if (I
!= Alignments
.end() && I
->AlignType
== (unsigned)AlignType
&&
542 (I
->TypeBitWidth
== BitWidth
|| AlignType
== INTEGER_ALIGN
))
543 return ABIInfo
? I
->ABIAlign
: I
->PrefAlign
;
545 if (AlignType
== INTEGER_ALIGN
) {
546 // If we didn't have a larger value try the largest value we have.
547 if (I
!= Alignments
.begin()) {
548 --I
; // Go to the previous entry and see if its an integer.
549 if (I
->AlignType
== INTEGER_ALIGN
)
550 return ABIInfo
? I
->ABIAlign
: I
->PrefAlign
;
552 } else if (AlignType
== VECTOR_ALIGN
) {
553 // By default, use natural alignment for vector types. This is consistent
554 // with what clang and llvm-gcc do.
555 unsigned Align
= getTypeAllocSize(cast
<VectorType
>(Ty
)->getElementType());
556 Align
*= cast
<VectorType
>(Ty
)->getNumElements();
557 Align
= PowerOf2Ceil(Align
);
561 // If we still couldn't find a reasonable default alignment, fall back
562 // to a simple heuristic that the alignment is the first power of two
563 // greater-or-equal to the store size of the type. This is a reasonable
564 // approximation of reality, and if the user wanted something less
565 // less conservative, they should have specified it explicitly in the data
567 unsigned Align
= getTypeStoreSize(Ty
);
568 Align
= PowerOf2Ceil(Align
);
574 class StructLayoutMap
{
575 using LayoutInfoTy
= DenseMap
<StructType
*, StructLayout
*>;
576 LayoutInfoTy LayoutInfo
;
580 // Remove any layouts.
581 for (const auto &I
: LayoutInfo
) {
582 StructLayout
*Value
= I
.second
;
583 Value
->~StructLayout();
588 StructLayout
*&operator[](StructType
*STy
) {
589 return LayoutInfo
[STy
];
593 } // end anonymous namespace
595 void DataLayout::clear() {
596 LegalIntWidths
.clear();
599 delete static_cast<StructLayoutMap
*>(LayoutMap
);
603 DataLayout::~DataLayout() {
607 const StructLayout
*DataLayout::getStructLayout(StructType
*Ty
) const {
609 LayoutMap
= new StructLayoutMap();
611 StructLayoutMap
*STM
= static_cast<StructLayoutMap
*>(LayoutMap
);
612 StructLayout
*&SL
= (*STM
)[Ty
];
615 // Otherwise, create the struct layout. Because it is variable length, we
616 // malloc it, then use placement new.
617 int NumElts
= Ty
->getNumElements();
618 StructLayout
*L
= (StructLayout
*)
619 safe_malloc(sizeof(StructLayout
)+(NumElts
-1) * sizeof(uint64_t));
621 // Set SL before calling StructLayout's ctor. The ctor could cause other
622 // entries to be added to TheMap, invalidating our reference.
625 new (L
) StructLayout(Ty
, *this);
630 unsigned DataLayout::getPointerABIAlignment(unsigned AS
) const {
631 PointersTy::const_iterator I
= findPointerLowerBound(AS
);
632 if (I
== Pointers
.end() || I
->AddressSpace
!= AS
) {
633 I
= findPointerLowerBound(0);
634 assert(I
->AddressSpace
== 0);
639 unsigned DataLayout::getPointerPrefAlignment(unsigned AS
) const {
640 PointersTy::const_iterator I
= findPointerLowerBound(AS
);
641 if (I
== Pointers
.end() || I
->AddressSpace
!= AS
) {
642 I
= findPointerLowerBound(0);
643 assert(I
->AddressSpace
== 0);
648 unsigned DataLayout::getPointerSize(unsigned AS
) const {
649 PointersTy::const_iterator I
= findPointerLowerBound(AS
);
650 if (I
== Pointers
.end() || I
->AddressSpace
!= AS
) {
651 I
= findPointerLowerBound(0);
652 assert(I
->AddressSpace
== 0);
654 return I
->TypeByteWidth
;
657 unsigned DataLayout::getMaxPointerSize() const {
658 unsigned MaxPointerSize
= 0;
659 for (auto &P
: Pointers
)
660 MaxPointerSize
= std::max(MaxPointerSize
, P
.TypeByteWidth
);
662 return MaxPointerSize
;
665 unsigned DataLayout::getPointerTypeSizeInBits(Type
*Ty
) const {
666 assert(Ty
->isPtrOrPtrVectorTy() &&
667 "This should only be called with a pointer or pointer vector type");
668 Ty
= Ty
->getScalarType();
669 return getPointerSizeInBits(cast
<PointerType
>(Ty
)->getAddressSpace());
672 unsigned DataLayout::getIndexSize(unsigned AS
) const {
673 PointersTy::const_iterator I
= findPointerLowerBound(AS
);
674 if (I
== Pointers
.end() || I
->AddressSpace
!= AS
) {
675 I
= findPointerLowerBound(0);
676 assert(I
->AddressSpace
== 0);
678 return I
->IndexWidth
;
681 unsigned DataLayout::getIndexTypeSizeInBits(Type
*Ty
) const {
682 assert(Ty
->isPtrOrPtrVectorTy() &&
683 "This should only be called with a pointer or pointer vector type");
684 Ty
= Ty
->getScalarType();
685 return getIndexSizeInBits(cast
<PointerType
>(Ty
)->getAddressSpace());
689 \param abi_or_pref Flag that determines which alignment is returned. true
690 returns the ABI alignment, false returns the preferred alignment.
691 \param Ty The underlying type for which alignment is determined.
693 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
694 == false) for the requested type \a Ty.
696 unsigned DataLayout::getAlignment(Type
*Ty
, bool abi_or_pref
) const {
697 AlignTypeEnum AlignType
;
699 assert(Ty
->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
700 switch (Ty
->getTypeID()) {
701 // Early escape for the non-numeric types.
702 case Type::LabelTyID
:
704 ? getPointerABIAlignment(0)
705 : getPointerPrefAlignment(0));
706 case Type::PointerTyID
: {
707 unsigned AS
= cast
<PointerType
>(Ty
)->getAddressSpace();
709 ? getPointerABIAlignment(AS
)
710 : getPointerPrefAlignment(AS
));
712 case Type::ArrayTyID
:
713 return getAlignment(cast
<ArrayType
>(Ty
)->getElementType(), abi_or_pref
);
715 case Type::StructTyID
: {
716 // Packed structure types always have an ABI alignment of one.
717 if (cast
<StructType
>(Ty
)->isPacked() && abi_or_pref
)
720 // Get the layout annotation... which is lazily created on demand.
721 const StructLayout
*Layout
= getStructLayout(cast
<StructType
>(Ty
));
722 unsigned Align
= getAlignmentInfo(AGGREGATE_ALIGN
, 0, abi_or_pref
, Ty
);
723 return std::max(Align
, Layout
->getAlignment());
725 case Type::IntegerTyID
:
726 AlignType
= INTEGER_ALIGN
;
729 case Type::FloatTyID
:
730 case Type::DoubleTyID
:
731 // PPC_FP128TyID and FP128TyID have different data contents, but the
732 // same size and alignment, so they look the same here.
733 case Type::PPC_FP128TyID
:
734 case Type::FP128TyID
:
735 case Type::X86_FP80TyID
:
736 AlignType
= FLOAT_ALIGN
;
738 case Type::X86_MMXTyID
:
739 case Type::VectorTyID
:
740 AlignType
= VECTOR_ALIGN
;
743 llvm_unreachable("Bad type for getAlignment!!!");
746 return getAlignmentInfo(AlignType
, getTypeSizeInBits(Ty
), abi_or_pref
, Ty
);
749 unsigned DataLayout::getABITypeAlignment(Type
*Ty
) const {
750 return getAlignment(Ty
, true);
753 /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
754 /// an integer type of the specified bitwidth.
755 unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth
) const {
756 return getAlignmentInfo(INTEGER_ALIGN
, BitWidth
, true, nullptr);
759 unsigned DataLayout::getPrefTypeAlignment(Type
*Ty
) const {
760 return getAlignment(Ty
, false);
763 unsigned DataLayout::getPreferredTypeAlignmentShift(Type
*Ty
) const {
764 unsigned Align
= getPrefTypeAlignment(Ty
);
765 assert(!(Align
& (Align
-1)) && "Alignment is not a power of two!");
766 return Log2_32(Align
);
769 IntegerType
*DataLayout::getIntPtrType(LLVMContext
&C
,
770 unsigned AddressSpace
) const {
771 return IntegerType::get(C
, getIndexSizeInBits(AddressSpace
));
774 Type
*DataLayout::getIntPtrType(Type
*Ty
) const {
775 assert(Ty
->isPtrOrPtrVectorTy() &&
776 "Expected a pointer or pointer vector type.");
777 unsigned NumBits
= getIndexTypeSizeInBits(Ty
);
778 IntegerType
*IntTy
= IntegerType::get(Ty
->getContext(), NumBits
);
779 if (VectorType
*VecTy
= dyn_cast
<VectorType
>(Ty
))
780 return VectorType::get(IntTy
, VecTy
->getNumElements());
784 Type
*DataLayout::getSmallestLegalIntType(LLVMContext
&C
, unsigned Width
) const {
785 for (unsigned LegalIntWidth
: LegalIntWidths
)
786 if (Width
<= LegalIntWidth
)
787 return Type::getIntNTy(C
, LegalIntWidth
);
791 unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
792 auto Max
= std::max_element(LegalIntWidths
.begin(), LegalIntWidths
.end());
793 return Max
!= LegalIntWidths
.end() ? *Max
: 0;
796 Type
*DataLayout::getIndexType(Type
*Ty
) const {
797 assert(Ty
->isPtrOrPtrVectorTy() &&
798 "Expected a pointer or pointer vector type.");
799 unsigned NumBits
= getIndexTypeSizeInBits(Ty
);
800 IntegerType
*IntTy
= IntegerType::get(Ty
->getContext(), NumBits
);
801 if (VectorType
*VecTy
= dyn_cast
<VectorType
>(Ty
))
802 return VectorType::get(IntTy
, VecTy
->getNumElements());
806 int64_t DataLayout::getIndexedOffsetInType(Type
*ElemTy
,
807 ArrayRef
<Value
*> Indices
) const {
810 generic_gep_type_iterator
<Value
* const*>
811 GTI
= gep_type_begin(ElemTy
, Indices
),
812 GTE
= gep_type_end(ElemTy
, Indices
);
813 for (; GTI
!= GTE
; ++GTI
) {
814 Value
*Idx
= GTI
.getOperand();
815 if (StructType
*STy
= GTI
.getStructTypeOrNull()) {
816 assert(Idx
->getType()->isIntegerTy(32) && "Illegal struct idx");
817 unsigned FieldNo
= cast
<ConstantInt
>(Idx
)->getZExtValue();
819 // Get structure layout information...
820 const StructLayout
*Layout
= getStructLayout(STy
);
822 // Add in the offset, as calculated by the structure layout info...
823 Result
+= Layout
->getElementOffset(FieldNo
);
825 // Get the array index and the size of each array element.
826 if (int64_t arrayIdx
= cast
<ConstantInt
>(Idx
)->getSExtValue())
827 Result
+= arrayIdx
* getTypeAllocSize(GTI
.getIndexedType());
834 /// getPreferredAlignment - Return the preferred alignment of the specified
835 /// global. This includes an explicitly requested alignment (if the global
837 unsigned DataLayout::getPreferredAlignment(const GlobalVariable
*GV
) const {
838 unsigned GVAlignment
= GV
->getAlignment();
839 // If a section is specified, always precisely honor explicit alignment,
840 // so we don't insert padding into a section we don't control.
841 if (GVAlignment
&& GV
->hasSection())
844 // If no explicit alignment is specified, compute the alignment based on
845 // the IR type. If an alignment is specified, increase it to match the ABI
846 // alignment of the IR type.
848 // FIXME: Not sure it makes sense to use the alignment of the type if
849 // there's already an explicit alignment specification.
850 Type
*ElemType
= GV
->getValueType();
851 unsigned Alignment
= getPrefTypeAlignment(ElemType
);
852 if (GVAlignment
>= Alignment
) {
853 Alignment
= GVAlignment
;
854 } else if (GVAlignment
!= 0) {
855 Alignment
= std::max(GVAlignment
, getABITypeAlignment(ElemType
));
858 // If no explicit alignment is specified, and the global is large, increase
859 // the alignment to 16.
860 // FIXME: Why 16, specifically?
861 if (GV
->hasInitializer() && GVAlignment
== 0) {
862 if (Alignment
< 16) {
863 // If the global is not external, see if it is large. If so, give it a
865 if (getTypeSizeInBits(ElemType
) > 128)
866 Alignment
= 16; // 16-byte alignment.
872 /// getPreferredAlignmentLog - Return the preferred alignment of the
873 /// specified global, returned in log form. This includes an explicitly
874 /// requested alignment (if the global has one).
875 unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable
*GV
) const {
876 return Log2_32(getPreferredAlignment(GV
));