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