xref: /llvm-project/llvm/lib/IR/DataLayout.cpp (revision 75c7bca740935a0cca462e28475dd6b046a6872c)
1 //===- DataLayout.cpp - Data size & alignment routines ---------------------==//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines layout properties related to datatype size/offset/alignment
10 // information.
11 //
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.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/GetElementPtrTypeIterator.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/IR/Value.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/Error.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/MemAlloc.h"
32 #include "llvm/Support/TypeSize.h"
33 #include "llvm/TargetParser/Triple.h"
34 #include <algorithm>
35 #include <cassert>
36 #include <cstdint>
37 #include <cstdlib>
38 #include <new>
39 #include <utility>
40 
41 using namespace llvm;
42 
43 //===----------------------------------------------------------------------===//
44 // Support for StructLayout
45 //===----------------------------------------------------------------------===//
46 
47 StructLayout::StructLayout(StructType *ST, const DataLayout &DL)
48     : StructSize(TypeSize::getFixed(0)) {
49   assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
50   IsPadded = false;
51   NumElements = ST->getNumElements();
52 
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     if (i == 0 && Ty->isScalableTy())
57       StructSize = TypeSize::getScalable(0);
58 
59     const Align TyAlign = ST->isPacked() ? Align(1) : DL.getABITypeAlign(Ty);
60 
61     // Add padding if necessary to align the data element properly.
62     // Currently the only structure with scalable size will be the homogeneous
63     // scalable vector types. Homogeneous scalable vector types have members of
64     // the same data type so no alignment issue will happen. The condition here
65     // assumes so and needs to be adjusted if this assumption changes (e.g. we
66     // support structures with arbitrary scalable data type, or structure that
67     // contains both fixed size and scalable size data type members).
68     if (!StructSize.isScalable() && !isAligned(TyAlign, StructSize)) {
69       IsPadded = true;
70       StructSize = TypeSize::getFixed(alignTo(StructSize, TyAlign));
71     }
72 
73     // Keep track of maximum alignment constraint.
74     StructAlignment = std::max(TyAlign, StructAlignment);
75 
76     getMemberOffsets()[i] = StructSize;
77     // Consume space for this data item
78     StructSize += DL.getTypeAllocSize(Ty);
79   }
80 
81   // Add padding to the end of the struct so that it could be put in an array
82   // and all array elements would be aligned correctly.
83   if (!StructSize.isScalable() && !isAligned(StructAlignment, StructSize)) {
84     IsPadded = true;
85     StructSize = TypeSize::getFixed(alignTo(StructSize, StructAlignment));
86   }
87 }
88 
89 /// getElementContainingOffset - Given a valid offset into the structure,
90 /// return the structure index that contains it.
91 unsigned StructLayout::getElementContainingOffset(uint64_t FixedOffset) const {
92   assert(!StructSize.isScalable() &&
93          "Cannot get element at offset for structure containing scalable "
94          "vector types");
95   TypeSize Offset = TypeSize::getFixed(FixedOffset);
96   ArrayRef<TypeSize> MemberOffsets = getMemberOffsets();
97 
98   const auto *SI =
99       std::upper_bound(MemberOffsets.begin(), MemberOffsets.end(), Offset,
100                        [](TypeSize LHS, TypeSize RHS) -> bool {
101                          return TypeSize::isKnownLT(LHS, RHS);
102                        });
103   assert(SI != MemberOffsets.begin() && "Offset not in structure type!");
104   --SI;
105   assert(TypeSize::isKnownLE(*SI, Offset) && "upper_bound didn't work");
106   assert(
107       (SI == MemberOffsets.begin() || TypeSize::isKnownLE(*(SI - 1), Offset)) &&
108       (SI + 1 == MemberOffsets.end() ||
109        TypeSize::isKnownGT(*(SI + 1), Offset)) &&
110       "Upper bound didn't work!");
111 
112   // Multiple fields can have the same offset if any of them are zero sized.
113   // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
114   // at the i32 element, because it is the last element at that offset.  This is
115   // the right one to return, because anything after it will have a higher
116   // offset, implying that this element is non-empty.
117   return SI - MemberOffsets.begin();
118 }
119 
120 //===----------------------------------------------------------------------===//
121 // LayoutAlignElem, LayoutAlign support
122 //===----------------------------------------------------------------------===//
123 
124 LayoutAlignElem LayoutAlignElem::get(Align ABIAlign, Align PrefAlign,
125                                      uint32_t BitWidth) {
126   assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
127   LayoutAlignElem retval;
128   retval.ABIAlign = ABIAlign;
129   retval.PrefAlign = PrefAlign;
130   retval.TypeBitWidth = BitWidth;
131   return retval;
132 }
133 
134 bool LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
135   return ABIAlign == rhs.ABIAlign && PrefAlign == rhs.PrefAlign &&
136          TypeBitWidth == rhs.TypeBitWidth;
137 }
138 
139 //===----------------------------------------------------------------------===//
140 // PointerAlignElem, PointerAlign support
141 //===----------------------------------------------------------------------===//
142 
143 PointerAlignElem PointerAlignElem::getInBits(uint32_t AddressSpace,
144                                              Align ABIAlign, Align PrefAlign,
145                                              uint32_t TypeBitWidth,
146                                              uint32_t IndexBitWidth) {
147   assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
148   PointerAlignElem retval;
149   retval.AddressSpace = AddressSpace;
150   retval.ABIAlign = ABIAlign;
151   retval.PrefAlign = PrefAlign;
152   retval.TypeBitWidth = TypeBitWidth;
153   retval.IndexBitWidth = IndexBitWidth;
154   return retval;
155 }
156 
157 bool
158 PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
159   return (ABIAlign == rhs.ABIAlign && AddressSpace == rhs.AddressSpace &&
160           PrefAlign == rhs.PrefAlign && TypeBitWidth == rhs.TypeBitWidth &&
161           IndexBitWidth == rhs.IndexBitWidth);
162 }
163 
164 //===----------------------------------------------------------------------===//
165 //                       DataLayout Class Implementation
166 //===----------------------------------------------------------------------===//
167 
168 const char *DataLayout::getManglingComponent(const Triple &T) {
169   if (T.isOSBinFormatGOFF())
170     return "-m:l";
171   if (T.isOSBinFormatMachO())
172     return "-m:o";
173   if ((T.isOSWindows() || T.isUEFI()) && T.isOSBinFormatCOFF())
174     return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
175   if (T.isOSBinFormatXCOFF())
176     return "-m:a";
177   return "-m:e";
178 }
179 
180 static const std::pair<AlignTypeEnum, LayoutAlignElem> DefaultAlignments[] = {
181     {INTEGER_ALIGN, {1, Align(1), Align(1)}},    // i1
182     {INTEGER_ALIGN, {8, Align(1), Align(1)}},    // i8
183     {INTEGER_ALIGN, {16, Align(2), Align(2)}},   // i16
184     {INTEGER_ALIGN, {32, Align(4), Align(4)}},   // i32
185     {INTEGER_ALIGN, {64, Align(4), Align(8)}},   // i64
186     {FLOAT_ALIGN, {16, Align(2), Align(2)}},     // half, bfloat
187     {FLOAT_ALIGN, {32, Align(4), Align(4)}},     // float
188     {FLOAT_ALIGN, {64, Align(8), Align(8)}},     // double
189     {FLOAT_ALIGN, {128, Align(16), Align(16)}},  // ppcf128, quad, ...
190     {VECTOR_ALIGN, {64, Align(8), Align(8)}},    // v2i32, v1i64, ...
191     {VECTOR_ALIGN, {128, Align(16), Align(16)}}, // v16i8, v8i16, v4i32, ...
192 };
193 
194 void DataLayout::reset(StringRef Desc) {
195   clear();
196 
197   LayoutMap = nullptr;
198   BigEndian = false;
199   AllocaAddrSpace = 0;
200   StackNaturalAlign.reset();
201   ProgramAddrSpace = 0;
202   DefaultGlobalsAddrSpace = 0;
203   FunctionPtrAlign.reset();
204   TheFunctionPtrAlignType = FunctionPtrAlignType::Independent;
205   ManglingMode = MM_None;
206   NonIntegralAddressSpaces.clear();
207   StructAlignment = LayoutAlignElem::get(Align(1), Align(8), 0);
208 
209   // Default alignments
210   for (const auto &[Kind, Layout] : DefaultAlignments) {
211     if (Error Err = setAlignment(Kind, Layout.ABIAlign, Layout.PrefAlign,
212                                  Layout.TypeBitWidth))
213       return report_fatal_error(std::move(Err));
214   }
215   if (Error Err = setPointerAlignmentInBits(0, Align(8), Align(8), 64, 64))
216     return report_fatal_error(std::move(Err));
217 
218   if (Error Err = parseSpecifier(Desc))
219     return report_fatal_error(std::move(Err));
220 }
221 
222 DataLayout &DataLayout::operator=(const DataLayout &Other) {
223   clear();
224   StringRepresentation = Other.StringRepresentation;
225   BigEndian = Other.BigEndian;
226   AllocaAddrSpace = Other.AllocaAddrSpace;
227   ProgramAddrSpace = Other.ProgramAddrSpace;
228   DefaultGlobalsAddrSpace = Other.DefaultGlobalsAddrSpace;
229   StackNaturalAlign = Other.StackNaturalAlign;
230   FunctionPtrAlign = Other.FunctionPtrAlign;
231   TheFunctionPtrAlignType = Other.TheFunctionPtrAlignType;
232   ManglingMode = Other.ManglingMode;
233   LegalIntWidths = Other.LegalIntWidths;
234   IntAlignments = Other.IntAlignments;
235   FloatAlignments = Other.FloatAlignments;
236   VectorAlignments = Other.VectorAlignments;
237   StructAlignment = Other.StructAlignment;
238   Pointers = Other.Pointers;
239   NonIntegralAddressSpaces = Other.NonIntegralAddressSpaces;
240   return *this;
241 }
242 
243 bool DataLayout::operator==(const DataLayout &Other) const {
244   // NOTE: StringRepresentation might differ, it is not canonicalized.
245   // FIXME: NonIntegralAddressSpaces isn't compared.
246   return BigEndian == Other.BigEndian &&
247          AllocaAddrSpace == Other.AllocaAddrSpace &&
248          ProgramAddrSpace == Other.ProgramAddrSpace &&
249          DefaultGlobalsAddrSpace == Other.DefaultGlobalsAddrSpace &&
250          StackNaturalAlign == Other.StackNaturalAlign &&
251          FunctionPtrAlign == Other.FunctionPtrAlign &&
252          TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType &&
253          ManglingMode == Other.ManglingMode &&
254          LegalIntWidths == Other.LegalIntWidths &&
255          IntAlignments == Other.IntAlignments &&
256          FloatAlignments == Other.FloatAlignments &&
257          VectorAlignments == Other.VectorAlignments &&
258          StructAlignment == Other.StructAlignment && Pointers == Other.Pointers;
259 }
260 
261 Expected<DataLayout> DataLayout::parse(StringRef LayoutDescription) {
262   DataLayout Layout("");
263   if (Error Err = Layout.parseSpecifier(LayoutDescription))
264     return std::move(Err);
265   return Layout;
266 }
267 
268 static Error reportError(const Twine &Message) {
269   return createStringError(inconvertibleErrorCode(), Message);
270 }
271 
272 /// Checked version of split, to ensure mandatory subparts.
273 static Error split(StringRef Str, char Separator,
274                    std::pair<StringRef, StringRef> &Split) {
275   assert(!Str.empty() && "parse error, string can't be empty here");
276   Split = Str.split(Separator);
277   if (Split.second.empty() && Split.first != Str)
278     return reportError("Trailing separator in datalayout string");
279   if (!Split.second.empty() && Split.first.empty())
280     return reportError("Expected token before separator in datalayout string");
281   return Error::success();
282 }
283 
284 /// Get an unsigned integer, including error checks.
285 template <typename IntTy> static Error getInt(StringRef R, IntTy &Result) {
286   bool error = R.getAsInteger(10, Result); (void)error;
287   if (error)
288     return reportError("not a number, or does not fit in an unsigned int");
289   return Error::success();
290 }
291 
292 /// Get an unsigned integer representing the number of bits and convert it into
293 /// bytes. Error out of not a byte width multiple.
294 template <typename IntTy>
295 static Error getIntInBytes(StringRef R, IntTy &Result) {
296   if (Error Err = getInt<IntTy>(R, Result))
297     return Err;
298   if (Result % 8)
299     return reportError("number of bits must be a byte width multiple");
300   Result /= 8;
301   return Error::success();
302 }
303 
304 static Error getAddrSpace(StringRef R, unsigned &AddrSpace) {
305   if (Error Err = getInt(R, AddrSpace))
306     return Err;
307   if (!isUInt<24>(AddrSpace))
308     return reportError("Invalid address space, must be a 24-bit integer");
309   return Error::success();
310 }
311 
312 Error DataLayout::parseSpecifier(StringRef Desc) {
313   StringRepresentation = std::string(Desc);
314   while (!Desc.empty()) {
315     // Split at '-'.
316     std::pair<StringRef, StringRef> Split;
317     if (Error Err = ::split(Desc, '-', Split))
318       return Err;
319     Desc = Split.second;
320 
321     // Split at ':'.
322     if (Error Err = ::split(Split.first, ':', Split))
323       return Err;
324 
325     // Aliases used below.
326     StringRef &Tok  = Split.first;  // Current token.
327     StringRef &Rest = Split.second; // The rest of the string.
328 
329     if (Tok == "ni") {
330       do {
331         if (Error Err = ::split(Rest, ':', Split))
332           return Err;
333         Rest = Split.second;
334         unsigned AS;
335         if (Error Err = getInt(Split.first, AS))
336           return Err;
337         if (AS == 0)
338           return reportError("Address space 0 can never be non-integral");
339         NonIntegralAddressSpaces.push_back(AS);
340       } while (!Rest.empty());
341 
342       continue;
343     }
344 
345     char Specifier = Tok.front();
346     Tok = Tok.substr(1);
347 
348     switch (Specifier) {
349     case 's':
350       // Deprecated, but ignoring here to preserve loading older textual llvm
351       // ASM file
352       break;
353     case 'E':
354       BigEndian = true;
355       break;
356     case 'e':
357       BigEndian = false;
358       break;
359     case 'p': {
360       // Address space.
361       unsigned AddrSpace = 0;
362       if (!Tok.empty())
363         if (Error Err = getInt(Tok, AddrSpace))
364           return Err;
365       if (!isUInt<24>(AddrSpace))
366         return reportError("Invalid address space, must be a 24-bit integer");
367 
368       // Size.
369       if (Rest.empty())
370         return reportError(
371             "Missing size specification for pointer in datalayout string");
372       if (Error Err = ::split(Rest, ':', Split))
373         return Err;
374       unsigned PointerMemSize;
375       if (Error Err = getInt(Tok, PointerMemSize))
376         return Err;
377       if (!PointerMemSize)
378         return reportError("Invalid pointer size of 0 bytes");
379 
380       // ABI alignment.
381       if (Rest.empty())
382         return reportError(
383             "Missing alignment specification for pointer in datalayout string");
384       if (Error Err = ::split(Rest, ':', Split))
385         return Err;
386       unsigned PointerABIAlign;
387       if (Error Err = getIntInBytes(Tok, PointerABIAlign))
388         return Err;
389       if (!isPowerOf2_64(PointerABIAlign))
390         return reportError("Pointer ABI alignment must be a power of 2");
391 
392       // Size of index used in GEP for address calculation.
393       // The parameter is optional. By default it is equal to size of pointer.
394       unsigned IndexSize = PointerMemSize;
395 
396       // Preferred alignment.
397       unsigned PointerPrefAlign = PointerABIAlign;
398       if (!Rest.empty()) {
399         if (Error Err = ::split(Rest, ':', Split))
400           return Err;
401         if (Error Err = getIntInBytes(Tok, PointerPrefAlign))
402           return Err;
403         if (!isPowerOf2_64(PointerPrefAlign))
404           return reportError(
405               "Pointer preferred alignment must be a power of 2");
406 
407         // Now read the index. It is the second optional parameter here.
408         if (!Rest.empty()) {
409           if (Error Err = ::split(Rest, ':', Split))
410             return Err;
411           if (Error Err = getInt(Tok, IndexSize))
412             return Err;
413           if (!IndexSize)
414             return reportError("Invalid index size of 0 bytes");
415         }
416       }
417       if (Error Err = setPointerAlignmentInBits(
418               AddrSpace, assumeAligned(PointerABIAlign),
419               assumeAligned(PointerPrefAlign), PointerMemSize, IndexSize))
420         return Err;
421       break;
422     }
423     case 'i':
424     case 'v':
425     case 'f':
426     case 'a': {
427       AlignTypeEnum AlignType;
428       switch (Specifier) {
429       default: llvm_unreachable("Unexpected specifier!");
430       case 'i': AlignType = INTEGER_ALIGN; break;
431       case 'v': AlignType = VECTOR_ALIGN; break;
432       case 'f': AlignType = FLOAT_ALIGN; break;
433       case 'a': AlignType = AGGREGATE_ALIGN; break;
434       }
435 
436       // Bit size.
437       unsigned Size = 0;
438       if (!Tok.empty())
439         if (Error Err = getInt(Tok, Size))
440           return Err;
441 
442       if (AlignType == AGGREGATE_ALIGN && Size != 0)
443         return reportError(
444             "Sized aggregate specification in datalayout string");
445 
446       // ABI alignment.
447       if (Rest.empty())
448         return reportError(
449             "Missing alignment specification in datalayout string");
450       if (Error Err = ::split(Rest, ':', Split))
451         return Err;
452       unsigned ABIAlign;
453       if (Error Err = getIntInBytes(Tok, ABIAlign))
454         return Err;
455       if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
456         return reportError(
457             "ABI alignment specification must be >0 for non-aggregate types");
458 
459       if (!isUInt<16>(ABIAlign))
460         return reportError("Invalid ABI alignment, must be a 16bit integer");
461       if (ABIAlign != 0 && !isPowerOf2_64(ABIAlign))
462         return reportError("Invalid ABI alignment, must be a power of 2");
463       if (AlignType == INTEGER_ALIGN && Size == 8 && ABIAlign != 1)
464         return reportError(
465             "Invalid ABI alignment, i8 must be naturally aligned");
466 
467       // Preferred alignment.
468       unsigned PrefAlign = ABIAlign;
469       if (!Rest.empty()) {
470         if (Error Err = ::split(Rest, ':', Split))
471           return Err;
472         if (Error Err = getIntInBytes(Tok, PrefAlign))
473           return Err;
474       }
475 
476       if (!isUInt<16>(PrefAlign))
477         return reportError(
478             "Invalid preferred alignment, must be a 16bit integer");
479       if (PrefAlign != 0 && !isPowerOf2_64(PrefAlign))
480         return reportError("Invalid preferred alignment, must be a power of 2");
481 
482       if (Error Err = setAlignment(AlignType, assumeAligned(ABIAlign),
483                                    assumeAligned(PrefAlign), Size))
484         return Err;
485 
486       break;
487     }
488     case 'n':  // Native integer types.
489       while (true) {
490         unsigned Width;
491         if (Error Err = getInt(Tok, Width))
492           return Err;
493         if (Width == 0)
494           return reportError(
495               "Zero width native integer type in datalayout string");
496         LegalIntWidths.push_back(Width);
497         if (Rest.empty())
498           break;
499         if (Error Err = ::split(Rest, ':', Split))
500           return Err;
501       }
502       break;
503     case 'S': { // Stack natural alignment.
504       uint64_t Alignment;
505       if (Error Err = getIntInBytes(Tok, Alignment))
506         return Err;
507       if (Alignment != 0 && !llvm::isPowerOf2_64(Alignment))
508         return reportError("Alignment is neither 0 nor a power of 2");
509       StackNaturalAlign = MaybeAlign(Alignment);
510       break;
511     }
512     case 'F': {
513       switch (Tok.front()) {
514       case 'i':
515         TheFunctionPtrAlignType = FunctionPtrAlignType::Independent;
516         break;
517       case 'n':
518         TheFunctionPtrAlignType = FunctionPtrAlignType::MultipleOfFunctionAlign;
519         break;
520       default:
521         return reportError("Unknown function pointer alignment type in "
522                            "datalayout string");
523       }
524       Tok = Tok.substr(1);
525       uint64_t Alignment;
526       if (Error Err = getIntInBytes(Tok, Alignment))
527         return Err;
528       if (Alignment != 0 && !llvm::isPowerOf2_64(Alignment))
529         return reportError("Alignment is neither 0 nor a power of 2");
530       FunctionPtrAlign = MaybeAlign(Alignment);
531       break;
532     }
533     case 'P': { // Function address space.
534       if (Error Err = getAddrSpace(Tok, ProgramAddrSpace))
535         return Err;
536       break;
537     }
538     case 'A': { // Default stack/alloca address space.
539       if (Error Err = getAddrSpace(Tok, AllocaAddrSpace))
540         return Err;
541       break;
542     }
543     case 'G': { // Default address space for global variables.
544       if (Error Err = getAddrSpace(Tok, DefaultGlobalsAddrSpace))
545         return Err;
546       break;
547     }
548     case 'm':
549       if (!Tok.empty())
550         return reportError("Unexpected trailing characters after mangling "
551                            "specifier in datalayout string");
552       if (Rest.empty())
553         return reportError("Expected mangling specifier in datalayout string");
554       if (Rest.size() > 1)
555         return reportError("Unknown mangling specifier in datalayout string");
556       switch(Rest[0]) {
557       default:
558         return reportError("Unknown mangling in datalayout string");
559       case 'e':
560         ManglingMode = MM_ELF;
561         break;
562       case 'l':
563         ManglingMode = MM_GOFF;
564         break;
565       case 'o':
566         ManglingMode = MM_MachO;
567         break;
568       case 'm':
569         ManglingMode = MM_Mips;
570         break;
571       case 'w':
572         ManglingMode = MM_WinCOFF;
573         break;
574       case 'x':
575         ManglingMode = MM_WinCOFFX86;
576         break;
577       case 'a':
578         ManglingMode = MM_XCOFF;
579         break;
580       }
581       break;
582     default:
583       return reportError("Unknown specifier in datalayout string");
584       break;
585     }
586   }
587 
588   return Error::success();
589 }
590 
591 static SmallVectorImpl<LayoutAlignElem>::const_iterator
592 findAlignmentLowerBound(const SmallVectorImpl<LayoutAlignElem> &Alignments,
593                         uint32_t BitWidth) {
594   return partition_point(Alignments, [BitWidth](const LayoutAlignElem &E) {
595     return E.TypeBitWidth < BitWidth;
596   });
597 }
598 
599 Error DataLayout::setAlignment(AlignTypeEnum AlignType, Align ABIAlign,
600                                Align PrefAlign, uint32_t BitWidth) {
601   // AlignmentsTy::ABIAlign and AlignmentsTy::PrefAlign were once stored as
602   // uint16_t, it is unclear if there are requirements for alignment to be less
603   // than 2^16 other than storage. In the meantime we leave the restriction as
604   // an assert. See D67400 for context.
605   assert(Log2(ABIAlign) < 16 && Log2(PrefAlign) < 16 && "Alignment too big");
606   if (!isUInt<24>(BitWidth))
607     return reportError("Invalid bit width, must be a 24-bit integer");
608   if (PrefAlign < ABIAlign)
609     return reportError(
610         "Preferred alignment cannot be less than the ABI alignment");
611 
612   SmallVectorImpl<LayoutAlignElem> *Alignments;
613   switch (AlignType) {
614   case AGGREGATE_ALIGN:
615     StructAlignment.ABIAlign = ABIAlign;
616     StructAlignment.PrefAlign = PrefAlign;
617     return Error::success();
618   case INTEGER_ALIGN:
619     Alignments = &IntAlignments;
620     break;
621   case FLOAT_ALIGN:
622     Alignments = &FloatAlignments;
623     break;
624   case VECTOR_ALIGN:
625     Alignments = &VectorAlignments;
626     break;
627   }
628 
629   auto I = partition_point(*Alignments, [BitWidth](const LayoutAlignElem &E) {
630     return E.TypeBitWidth < BitWidth;
631   });
632   if (I != Alignments->end() && I->TypeBitWidth == BitWidth) {
633     // Update the abi, preferred alignments.
634     I->ABIAlign = ABIAlign;
635     I->PrefAlign = PrefAlign;
636   } else {
637     // Insert before I to keep the vector sorted.
638     Alignments->insert(I, LayoutAlignElem::get(ABIAlign, PrefAlign, BitWidth));
639   }
640   return Error::success();
641 }
642 
643 const PointerAlignElem &
644 DataLayout::getPointerAlignElem(uint32_t AddressSpace) const {
645   if (AddressSpace != 0) {
646     auto I = lower_bound(Pointers, AddressSpace,
647                          [](const PointerAlignElem &A, uint32_t AddressSpace) {
648       return A.AddressSpace < AddressSpace;
649     });
650     if (I != Pointers.end() && I->AddressSpace == AddressSpace)
651       return *I;
652   }
653 
654   assert(Pointers[0].AddressSpace == 0);
655   return Pointers[0];
656 }
657 
658 Error DataLayout::setPointerAlignmentInBits(uint32_t AddrSpace, Align ABIAlign,
659                                             Align PrefAlign,
660                                             uint32_t TypeBitWidth,
661                                             uint32_t IndexBitWidth) {
662   if (PrefAlign < ABIAlign)
663     return reportError(
664         "Preferred alignment cannot be less than the ABI alignment");
665   if (IndexBitWidth > TypeBitWidth)
666     return reportError("Index width cannot be larger than pointer width");
667 
668   auto I = lower_bound(Pointers, AddrSpace,
669                        [](const PointerAlignElem &A, uint32_t AddressSpace) {
670     return A.AddressSpace < AddressSpace;
671   });
672   if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
673     Pointers.insert(I,
674                     PointerAlignElem::getInBits(AddrSpace, ABIAlign, PrefAlign,
675                                                 TypeBitWidth, IndexBitWidth));
676   } else {
677     I->ABIAlign = ABIAlign;
678     I->PrefAlign = PrefAlign;
679     I->TypeBitWidth = TypeBitWidth;
680     I->IndexBitWidth = IndexBitWidth;
681   }
682   return Error::success();
683 }
684 
685 Align DataLayout::getIntegerAlignment(uint32_t BitWidth,
686                                       bool abi_or_pref) const {
687   auto I = findAlignmentLowerBound(IntAlignments, BitWidth);
688   // If we don't have an exact match, use alignment of next larger integer
689   // type. If there is none, use alignment of largest integer type by going
690   // back one element.
691   if (I == IntAlignments.end())
692     --I;
693   return abi_or_pref ? I->ABIAlign : I->PrefAlign;
694 }
695 
696 namespace {
697 
698 class StructLayoutMap {
699   using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
700   LayoutInfoTy LayoutInfo;
701 
702 public:
703   ~StructLayoutMap() {
704     // Remove any layouts.
705     for (const auto &I : LayoutInfo) {
706       StructLayout *Value = I.second;
707       Value->~StructLayout();
708       free(Value);
709     }
710   }
711 
712   StructLayout *&operator[](StructType *STy) {
713     return LayoutInfo[STy];
714   }
715 };
716 
717 } // end anonymous namespace
718 
719 void DataLayout::clear() {
720   LegalIntWidths.clear();
721   IntAlignments.clear();
722   FloatAlignments.clear();
723   VectorAlignments.clear();
724   Pointers.clear();
725   delete static_cast<StructLayoutMap *>(LayoutMap);
726   LayoutMap = nullptr;
727 }
728 
729 DataLayout::~DataLayout() {
730   clear();
731 }
732 
733 const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
734   if (!LayoutMap)
735     LayoutMap = new StructLayoutMap();
736 
737   StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
738   StructLayout *&SL = (*STM)[Ty];
739   if (SL) return SL;
740 
741   // Otherwise, create the struct layout.  Because it is variable length, we
742   // malloc it, then use placement new.
743   StructLayout *L = (StructLayout *)safe_malloc(
744       StructLayout::totalSizeToAlloc<TypeSize>(Ty->getNumElements()));
745 
746   // Set SL before calling StructLayout's ctor.  The ctor could cause other
747   // entries to be added to TheMap, invalidating our reference.
748   SL = L;
749 
750   new (L) StructLayout(Ty, *this);
751 
752   return L;
753 }
754 
755 Align DataLayout::getPointerABIAlignment(unsigned AS) const {
756   return getPointerAlignElem(AS).ABIAlign;
757 }
758 
759 Align DataLayout::getPointerPrefAlignment(unsigned AS) const {
760   return getPointerAlignElem(AS).PrefAlign;
761 }
762 
763 unsigned DataLayout::getPointerSize(unsigned AS) const {
764   return divideCeil(getPointerAlignElem(AS).TypeBitWidth, 8);
765 }
766 
767 unsigned DataLayout::getMaxIndexSize() const {
768   unsigned MaxIndexSize = 0;
769   for (auto &P : Pointers)
770     MaxIndexSize =
771         std::max(MaxIndexSize, (unsigned)divideCeil(P.TypeBitWidth, 8));
772 
773   return MaxIndexSize;
774 }
775 
776 unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
777   assert(Ty->isPtrOrPtrVectorTy() &&
778          "This should only be called with a pointer or pointer vector type");
779   Ty = Ty->getScalarType();
780   return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
781 }
782 
783 unsigned DataLayout::getIndexSize(unsigned AS) const {
784   return divideCeil(getPointerAlignElem(AS).IndexBitWidth, 8);
785 }
786 
787 unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
788   assert(Ty->isPtrOrPtrVectorTy() &&
789          "This should only be called with a pointer or pointer vector type");
790   Ty = Ty->getScalarType();
791   return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
792 }
793 
794 /*!
795   \param abi_or_pref Flag that determines which alignment is returned. true
796   returns the ABI alignment, false returns the preferred alignment.
797   \param Ty The underlying type for which alignment is determined.
798 
799   Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
800   == false) for the requested type \a Ty.
801  */
802 Align DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
803   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
804   switch (Ty->getTypeID()) {
805   // Early escape for the non-numeric types.
806   case Type::LabelTyID:
807     return abi_or_pref ? getPointerABIAlignment(0) : getPointerPrefAlignment(0);
808   case Type::PointerTyID: {
809     unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
810     return abi_or_pref ? getPointerABIAlignment(AS)
811                        : getPointerPrefAlignment(AS);
812     }
813   case Type::ArrayTyID:
814     return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
815 
816   case Type::StructTyID: {
817     // Packed structure types always have an ABI alignment of one.
818     if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
819       return Align(1);
820 
821     // Get the layout annotation... which is lazily created on demand.
822     const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
823     const Align Align =
824         abi_or_pref ? StructAlignment.ABIAlign : StructAlignment.PrefAlign;
825     return std::max(Align, Layout->getAlignment());
826   }
827   case Type::IntegerTyID:
828     return getIntegerAlignment(Ty->getIntegerBitWidth(), abi_or_pref);
829   case Type::HalfTyID:
830   case Type::BFloatTyID:
831   case Type::FloatTyID:
832   case Type::DoubleTyID:
833   // PPC_FP128TyID and FP128TyID have different data contents, but the
834   // same size and alignment, so they look the same here.
835   case Type::PPC_FP128TyID:
836   case Type::FP128TyID:
837   case Type::X86_FP80TyID: {
838     unsigned BitWidth = getTypeSizeInBits(Ty).getFixedValue();
839     auto I = findAlignmentLowerBound(FloatAlignments, BitWidth);
840     if (I != FloatAlignments.end() && I->TypeBitWidth == BitWidth)
841       return abi_or_pref ? I->ABIAlign : I->PrefAlign;
842 
843     // If we still couldn't find a reasonable default alignment, fall back
844     // to a simple heuristic that the alignment is the first power of two
845     // greater-or-equal to the store size of the type.  This is a reasonable
846     // approximation of reality, and if the user wanted something less
847     // less conservative, they should have specified it explicitly in the data
848     // layout.
849     return Align(PowerOf2Ceil(BitWidth / 8));
850   }
851   case Type::FixedVectorTyID:
852   case Type::ScalableVectorTyID: {
853     unsigned BitWidth = getTypeSizeInBits(Ty).getKnownMinValue();
854     auto I = findAlignmentLowerBound(VectorAlignments, BitWidth);
855     if (I != VectorAlignments.end() && I->TypeBitWidth == BitWidth)
856       return abi_or_pref ? I->ABIAlign : I->PrefAlign;
857 
858     // By default, use natural alignment for vector types. This is consistent
859     // with what clang and llvm-gcc do.
860     //
861     // We're only calculating a natural alignment, so it doesn't have to be
862     // based on the full size for scalable vectors. Using the minimum element
863     // count should be enough here.
864     return Align(PowerOf2Ceil(getTypeStoreSize(Ty).getKnownMinValue()));
865   }
866   case Type::X86_AMXTyID:
867     return Align(64);
868   case Type::TargetExtTyID: {
869     Type *LayoutTy = cast<TargetExtType>(Ty)->getLayoutType();
870     return getAlignment(LayoutTy, abi_or_pref);
871   }
872   default:
873     llvm_unreachable("Bad type for getAlignment!!!");
874   }
875 }
876 
877 Align DataLayout::getABITypeAlign(Type *Ty) const {
878   return getAlignment(Ty, true);
879 }
880 
881 Align DataLayout::getPrefTypeAlign(Type *Ty) const {
882   return getAlignment(Ty, false);
883 }
884 
885 IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
886                                        unsigned AddressSpace) const {
887   return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
888 }
889 
890 Type *DataLayout::getIntPtrType(Type *Ty) const {
891   assert(Ty->isPtrOrPtrVectorTy() &&
892          "Expected a pointer or pointer vector type.");
893   unsigned NumBits = getPointerTypeSizeInBits(Ty);
894   IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
895   if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
896     return VectorType::get(IntTy, VecTy);
897   return IntTy;
898 }
899 
900 Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
901   for (unsigned LegalIntWidth : LegalIntWidths)
902     if (Width <= LegalIntWidth)
903       return Type::getIntNTy(C, LegalIntWidth);
904   return nullptr;
905 }
906 
907 unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
908   auto Max = llvm::max_element(LegalIntWidths);
909   return Max != LegalIntWidths.end() ? *Max : 0;
910 }
911 
912 IntegerType *DataLayout::getIndexType(LLVMContext &C,
913                                       unsigned AddressSpace) const {
914   return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
915 }
916 
917 Type *DataLayout::getIndexType(Type *Ty) const {
918   assert(Ty->isPtrOrPtrVectorTy() &&
919          "Expected a pointer or pointer vector type.");
920   unsigned NumBits = getIndexTypeSizeInBits(Ty);
921   IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
922   if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
923     return VectorType::get(IntTy, VecTy);
924   return IntTy;
925 }
926 
927 int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
928                                            ArrayRef<Value *> Indices) const {
929   int64_t Result = 0;
930 
931   generic_gep_type_iterator<Value* const*>
932     GTI = gep_type_begin(ElemTy, Indices),
933     GTE = gep_type_end(ElemTy, Indices);
934   for (; GTI != GTE; ++GTI) {
935     Value *Idx = GTI.getOperand();
936     if (StructType *STy = GTI.getStructTypeOrNull()) {
937       assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
938       unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
939 
940       // Get structure layout information...
941       const StructLayout *Layout = getStructLayout(STy);
942 
943       // Add in the offset, as calculated by the structure layout info...
944       Result += Layout->getElementOffset(FieldNo);
945     } else {
946       if (int64_t ArrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
947         Result += ArrayIdx * GTI.getSequentialElementStride(*this);
948     }
949   }
950 
951   return Result;
952 }
953 
954 static APInt getElementIndex(TypeSize ElemSize, APInt &Offset) {
955   // Skip over scalable or zero size elements. Also skip element sizes larger
956   // than the positive index space, because the arithmetic below may not be
957   // correct in that case.
958   unsigned BitWidth = Offset.getBitWidth();
959   if (ElemSize.isScalable() || ElemSize == 0 ||
960       !isUIntN(BitWidth - 1, ElemSize)) {
961     return APInt::getZero(BitWidth);
962   }
963 
964   APInt Index = Offset.sdiv(ElemSize);
965   Offset -= Index * ElemSize;
966   if (Offset.isNegative()) {
967     // Prefer a positive remaining offset to allow struct indexing.
968     --Index;
969     Offset += ElemSize;
970     assert(Offset.isNonNegative() && "Remaining offset shouldn't be negative");
971   }
972   return Index;
973 }
974 
975 std::optional<APInt> DataLayout::getGEPIndexForOffset(Type *&ElemTy,
976                                                       APInt &Offset) const {
977   if (auto *ArrTy = dyn_cast<ArrayType>(ElemTy)) {
978     ElemTy = ArrTy->getElementType();
979     return getElementIndex(getTypeAllocSize(ElemTy), Offset);
980   }
981 
982   if (isa<VectorType>(ElemTy)) {
983     // Vector GEPs are partially broken (e.g. for overaligned element types),
984     // and may be forbidden in the future, so avoid generating GEPs into
985     // vectors. See https://discourse.llvm.org/t/67497
986     return std::nullopt;
987   }
988 
989   if (auto *STy = dyn_cast<StructType>(ElemTy)) {
990     const StructLayout *SL = getStructLayout(STy);
991     uint64_t IntOffset = Offset.getZExtValue();
992     if (IntOffset >= SL->getSizeInBytes())
993       return std::nullopt;
994 
995     unsigned Index = SL->getElementContainingOffset(IntOffset);
996     Offset -= SL->getElementOffset(Index);
997     ElemTy = STy->getElementType(Index);
998     return APInt(32, Index);
999   }
1000 
1001   // Non-aggregate type.
1002   return std::nullopt;
1003 }
1004 
1005 SmallVector<APInt> DataLayout::getGEPIndicesForOffset(Type *&ElemTy,
1006                                                       APInt &Offset) const {
1007   assert(ElemTy->isSized() && "Element type must be sized");
1008   SmallVector<APInt> Indices;
1009   Indices.push_back(getElementIndex(getTypeAllocSize(ElemTy), Offset));
1010   while (Offset != 0) {
1011     std::optional<APInt> Index = getGEPIndexForOffset(ElemTy, Offset);
1012     if (!Index)
1013       break;
1014     Indices.push_back(*Index);
1015   }
1016 
1017   return Indices;
1018 }
1019 
1020 /// getPreferredAlign - Return the preferred alignment of the specified global.
1021 /// This includes an explicitly requested alignment (if the global has one).
1022 Align DataLayout::getPreferredAlign(const GlobalVariable *GV) const {
1023   MaybeAlign GVAlignment = GV->getAlign();
1024   // If a section is specified, always precisely honor explicit alignment,
1025   // so we don't insert padding into a section we don't control.
1026   if (GVAlignment && GV->hasSection())
1027     return *GVAlignment;
1028 
1029   // If no explicit alignment is specified, compute the alignment based on
1030   // the IR type. If an alignment is specified, increase it to match the ABI
1031   // alignment of the IR type.
1032   //
1033   // FIXME: Not sure it makes sense to use the alignment of the type if
1034   // there's already an explicit alignment specification.
1035   Type *ElemType = GV->getValueType();
1036   Align Alignment = getPrefTypeAlign(ElemType);
1037   if (GVAlignment) {
1038     if (*GVAlignment >= Alignment)
1039       Alignment = *GVAlignment;
1040     else
1041       Alignment = std::max(*GVAlignment, getABITypeAlign(ElemType));
1042   }
1043 
1044   // If no explicit alignment is specified, and the global is large, increase
1045   // the alignment to 16.
1046   // FIXME: Why 16, specifically?
1047   if (GV->hasInitializer() && !GVAlignment) {
1048     if (Alignment < Align(16)) {
1049       // If the global is not external, see if it is large.  If so, give it a
1050       // larger alignment.
1051       if (getTypeSizeInBits(ElemType) > 128)
1052         Alignment = Align(16); // 16-byte alignment.
1053     }
1054   }
1055   return Alignment;
1056 }
1057