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