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