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