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