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/ErrorHandling.h"
31 #include "llvm/Support/MathExtras.h"
32 #include <algorithm>
33 #include <cassert>
34 #include <cstdint>
35 #include <cstdlib>
36 #include <tuple>
37 #include <utility>
38 
39 using namespace llvm;
40 
41 //===----------------------------------------------------------------------===//
42 // Support for StructLayout
43 //===----------------------------------------------------------------------===//
44 
45 StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
46   assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
47   StructAlignment = 0;
48   StructSize = 0;
49   IsPadded = false;
50   NumElements = ST->getNumElements();
51 
52   // Loop over each of the elements, placing them in memory.
53   for (unsigned i = 0, e = NumElements; i != e; ++i) {
54     Type *Ty = ST->getElementType(i);
55     unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
56 
57     // Add padding if necessary to align the data element properly.
58     if ((StructSize & (TyAlign-1)) != 0) {
59       IsPadded = true;
60       StructSize = alignTo(StructSize, TyAlign);
61     }
62 
63     // Keep track of maximum alignment constraint.
64     StructAlignment = std::max(TyAlign, StructAlignment);
65 
66     MemberOffsets[i] = StructSize;
67     StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
68   }
69 
70   // Empty structures have alignment of 1 byte.
71   if (StructAlignment == 0) StructAlignment = 1;
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 ((StructSize & (StructAlignment-1)) != 0) {
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   const uint64_t *SI =
85     std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
86   assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
87   --SI;
88   assert(*SI <= Offset && "upper_bound didn't work");
89   assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
90          (SI+1 == &MemberOffsets[NumElements] || *(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[0];
99 }
100 
101 //===----------------------------------------------------------------------===//
102 // LayoutAlignElem, LayoutAlign support
103 //===----------------------------------------------------------------------===//
104 
105 LayoutAlignElem
106 LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
107                      unsigned pref_align, uint32_t bit_width) {
108   assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
109   LayoutAlignElem retval;
110   retval.AlignType = align_type;
111   retval.ABIAlign = abi_align;
112   retval.PrefAlign = pref_align;
113   retval.TypeBitWidth = bit_width;
114   return retval;
115 }
116 
117 bool
118 LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
119   return (AlignType == rhs.AlignType
120           && ABIAlign == rhs.ABIAlign
121           && PrefAlign == rhs.PrefAlign
122           && TypeBitWidth == rhs.TypeBitWidth);
123 }
124 
125 //===----------------------------------------------------------------------===//
126 // PointerAlignElem, PointerAlign support
127 //===----------------------------------------------------------------------===//
128 
129 PointerAlignElem
130 PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
131                       unsigned PrefAlign, uint32_t TypeByteWidth,
132                       uint32_t IndexWidth) {
133   assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
134   PointerAlignElem retval;
135   retval.AddressSpace = AddressSpace;
136   retval.ABIAlign = ABIAlign;
137   retval.PrefAlign = PrefAlign;
138   retval.TypeByteWidth = TypeByteWidth;
139   retval.IndexWidth = IndexWidth;
140   return retval;
141 }
142 
143 bool
144 PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
145   return (ABIAlign == rhs.ABIAlign
146           && AddressSpace == rhs.AddressSpace
147           && PrefAlign == rhs.PrefAlign
148           && TypeByteWidth == rhs.TypeByteWidth
149           && IndexWidth == rhs.IndexWidth);
150 }
151 
152 //===----------------------------------------------------------------------===//
153 //                       DataLayout Class Implementation
154 //===----------------------------------------------------------------------===//
155 
156 const char *DataLayout::getManglingComponent(const Triple &T) {
157   if (T.isOSBinFormatMachO())
158     return "-m:o";
159   if (T.isOSWindows() && T.isOSBinFormatCOFF())
160     return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
161   return "-m:e";
162 }
163 
164 static const LayoutAlignElem DefaultAlignments[] = {
165   { INTEGER_ALIGN, 1, 1, 1 },    // i1
166   { INTEGER_ALIGN, 8, 1, 1 },    // i8
167   { INTEGER_ALIGN, 16, 2, 2 },   // i16
168   { INTEGER_ALIGN, 32, 4, 4 },   // i32
169   { INTEGER_ALIGN, 64, 4, 8 },   // i64
170   { FLOAT_ALIGN, 16, 2, 2 },     // half
171   { FLOAT_ALIGN, 32, 4, 4 },     // float
172   { FLOAT_ALIGN, 64, 8, 8 },     // double
173   { FLOAT_ALIGN, 128, 16, 16 },  // ppcf128, quad, ...
174   { VECTOR_ALIGN, 64, 8, 8 },    // v2i32, v1i64, ...
175   { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
176   { AGGREGATE_ALIGN, 0, 0, 8 }   // struct
177 };
178 
179 void DataLayout::reset(StringRef Desc) {
180   clear();
181 
182   LayoutMap = nullptr;
183   BigEndian = false;
184   AllocaAddrSpace = 0;
185   StackNaturalAlign = 0;
186   ProgramAddrSpace = 0;
187   ManglingMode = MM_None;
188   NonIntegralAddressSpaces.clear();
189 
190   // Default alignments
191   for (const LayoutAlignElem &E : DefaultAlignments) {
192     setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
193                  E.TypeBitWidth);
194   }
195   setPointerAlignment(0, 8, 8, 8, 8);
196 
197   parseSpecifier(Desc);
198 }
199 
200 /// Checked version of split, to ensure mandatory subparts.
201 static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
202   assert(!Str.empty() && "parse error, string can't be empty here");
203   std::pair<StringRef, StringRef> Split = Str.split(Separator);
204   if (Split.second.empty() && Split.first != Str)
205     report_fatal_error("Trailing separator in datalayout string");
206   if (!Split.second.empty() && Split.first.empty())
207     report_fatal_error("Expected token before separator in datalayout string");
208   return Split;
209 }
210 
211 /// Get an unsigned integer, including error checks.
212 static unsigned getInt(StringRef R) {
213   unsigned Result;
214   bool error = R.getAsInteger(10, Result); (void)error;
215   if (error)
216     report_fatal_error("not a number, or does not fit in an unsigned int");
217   return Result;
218 }
219 
220 /// Convert bits into bytes. Assert if not a byte width multiple.
221 static unsigned inBytes(unsigned Bits) {
222   if (Bits % 8)
223     report_fatal_error("number of bits must be a byte width multiple");
224   return Bits / 8;
225 }
226 
227 static unsigned getAddrSpace(StringRef R) {
228   unsigned AddrSpace = getInt(R);
229   if (!isUInt<24>(AddrSpace))
230     report_fatal_error("Invalid address space, must be a 24-bit integer");
231   return AddrSpace;
232 }
233 
234 void DataLayout::parseSpecifier(StringRef Desc) {
235   StringRepresentation = Desc;
236   while (!Desc.empty()) {
237     // Split at '-'.
238     std::pair<StringRef, StringRef> Split = split(Desc, '-');
239     Desc = Split.second;
240 
241     // Split at ':'.
242     Split = split(Split.first, ':');
243 
244     // Aliases used below.
245     StringRef &Tok  = Split.first;  // Current token.
246     StringRef &Rest = Split.second; // The rest of the string.
247 
248     if (Tok == "ni") {
249       do {
250         Split = split(Rest, ':');
251         Rest = Split.second;
252         unsigned AS = getInt(Split.first);
253         if (AS == 0)
254           report_fatal_error("Address space 0 can never be non-integral");
255         NonIntegralAddressSpaces.push_back(AS);
256       } while (!Rest.empty());
257 
258       continue;
259     }
260 
261     char Specifier = Tok.front();
262     Tok = Tok.substr(1);
263 
264     switch (Specifier) {
265     case 's':
266       // Ignored for backward compatibility.
267       // FIXME: remove this on LLVM 4.0.
268       break;
269     case 'E':
270       BigEndian = true;
271       break;
272     case 'e':
273       BigEndian = false;
274       break;
275     case 'p': {
276       // Address space.
277       unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
278       if (!isUInt<24>(AddrSpace))
279         report_fatal_error("Invalid address space, must be a 24bit integer");
280 
281       // Size.
282       if (Rest.empty())
283         report_fatal_error(
284             "Missing size specification for pointer in datalayout string");
285       Split = split(Rest, ':');
286       unsigned PointerMemSize = inBytes(getInt(Tok));
287       if (!PointerMemSize)
288         report_fatal_error("Invalid pointer size of 0 bytes");
289 
290       // ABI alignment.
291       if (Rest.empty())
292         report_fatal_error(
293             "Missing alignment specification for pointer in datalayout string");
294       Split = split(Rest, ':');
295       unsigned PointerABIAlign = inBytes(getInt(Tok));
296       if (!isPowerOf2_64(PointerABIAlign))
297         report_fatal_error(
298             "Pointer ABI alignment must be a power of 2");
299 
300       // Size of index used in GEP for address calculation.
301       // The parameter is optional. By default it is equal to size of pointer.
302       unsigned IndexSize = PointerMemSize;
303 
304       // Preferred alignment.
305       unsigned PointerPrefAlign = PointerABIAlign;
306       if (!Rest.empty()) {
307         Split = split(Rest, ':');
308         PointerPrefAlign = inBytes(getInt(Tok));
309         if (!isPowerOf2_64(PointerPrefAlign))
310           report_fatal_error(
311             "Pointer preferred alignment must be a power of 2");
312 
313         // Now read the index. It is the second optional parameter here.
314         if (!Rest.empty()) {
315           Split = split(Rest, ':');
316           IndexSize = inBytes(getInt(Tok));
317           if (!IndexSize)
318             report_fatal_error("Invalid index size of 0 bytes");
319         }
320       }
321       setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
322                           PointerMemSize, IndexSize);
323       break;
324     }
325     case 'i':
326     case 'v':
327     case 'f':
328     case 'a': {
329       AlignTypeEnum AlignType;
330       switch (Specifier) {
331       default: llvm_unreachable("Unexpected specifier!");
332       case 'i': AlignType = INTEGER_ALIGN; break;
333       case 'v': AlignType = VECTOR_ALIGN; break;
334       case 'f': AlignType = FLOAT_ALIGN; break;
335       case 'a': AlignType = AGGREGATE_ALIGN; break;
336       }
337 
338       // Bit size.
339       unsigned Size = Tok.empty() ? 0 : getInt(Tok);
340 
341       if (AlignType == AGGREGATE_ALIGN && Size != 0)
342         report_fatal_error(
343             "Sized aggregate specification in datalayout string");
344 
345       // ABI alignment.
346       if (Rest.empty())
347         report_fatal_error(
348             "Missing alignment specification in datalayout string");
349       Split = split(Rest, ':');
350       unsigned ABIAlign = inBytes(getInt(Tok));
351       if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
352         report_fatal_error(
353             "ABI alignment specification must be >0 for non-aggregate types");
354 
355       // Preferred alignment.
356       unsigned PrefAlign = ABIAlign;
357       if (!Rest.empty()) {
358         Split = split(Rest, ':');
359         PrefAlign = inBytes(getInt(Tok));
360       }
361 
362       setAlignment(AlignType, ABIAlign, PrefAlign, Size);
363 
364       break;
365     }
366     case 'n':  // Native integer types.
367       while (true) {
368         unsigned Width = getInt(Tok);
369         if (Width == 0)
370           report_fatal_error(
371               "Zero width native integer type in datalayout string");
372         LegalIntWidths.push_back(Width);
373         if (Rest.empty())
374           break;
375         Split = split(Rest, ':');
376       }
377       break;
378     case 'S': { // Stack natural alignment.
379       StackNaturalAlign = inBytes(getInt(Tok));
380       break;
381     }
382     case 'P': { // Function address space.
383       ProgramAddrSpace = getAddrSpace(Tok);
384       break;
385     }
386     case 'A': { // Default stack/alloca address space.
387       AllocaAddrSpace = getAddrSpace(Tok);
388       break;
389     }
390     case 'm':
391       if (!Tok.empty())
392         report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
393       if (Rest.empty())
394         report_fatal_error("Expected mangling specifier in datalayout string");
395       if (Rest.size() > 1)
396         report_fatal_error("Unknown mangling specifier in datalayout string");
397       switch(Rest[0]) {
398       default:
399         report_fatal_error("Unknown mangling in datalayout string");
400       case 'e':
401         ManglingMode = MM_ELF;
402         break;
403       case 'o':
404         ManglingMode = MM_MachO;
405         break;
406       case 'm':
407         ManglingMode = MM_Mips;
408         break;
409       case 'w':
410         ManglingMode = MM_WinCOFF;
411         break;
412       case 'x':
413         ManglingMode = MM_WinCOFFX86;
414         break;
415       }
416       break;
417     default:
418       report_fatal_error("Unknown specifier in datalayout string");
419       break;
420     }
421   }
422 }
423 
424 DataLayout::DataLayout(const Module *M) {
425   init(M);
426 }
427 
428 void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
429 
430 bool DataLayout::operator==(const DataLayout &Other) const {
431   bool Ret = BigEndian == Other.BigEndian &&
432              AllocaAddrSpace == Other.AllocaAddrSpace &&
433              StackNaturalAlign == Other.StackNaturalAlign &&
434              ProgramAddrSpace == Other.ProgramAddrSpace &&
435              ManglingMode == Other.ManglingMode &&
436              LegalIntWidths == Other.LegalIntWidths &&
437              Alignments == Other.Alignments && Pointers == Other.Pointers;
438   // Note: getStringRepresentation() might differs, it is not canonicalized
439   return Ret;
440 }
441 
442 DataLayout::AlignmentsTy::iterator
443 DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
444                                     uint32_t BitWidth) {
445   auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
446   return std::lower_bound(Alignments.begin(), Alignments.end(), Pair,
447                           [](const LayoutAlignElem &LHS,
448                              const std::pair<unsigned, uint32_t> &RHS) {
449                             return std::tie(LHS.AlignType, LHS.TypeBitWidth) <
450                                    std::tie(RHS.first, RHS.second);
451                           });
452 }
453 
454 void
455 DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
456                          unsigned pref_align, uint32_t bit_width) {
457   if (!isUInt<24>(bit_width))
458     report_fatal_error("Invalid bit width, must be a 24bit integer");
459   if (!isUInt<16>(abi_align))
460     report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
461   if (!isUInt<16>(pref_align))
462     report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
463   if (abi_align != 0 && !isPowerOf2_64(abi_align))
464     report_fatal_error("Invalid ABI alignment, must be a power of 2");
465   if (pref_align != 0 && !isPowerOf2_64(pref_align))
466     report_fatal_error("Invalid preferred alignment, must be a power of 2");
467 
468   if (pref_align < abi_align)
469     report_fatal_error(
470         "Preferred alignment cannot be less than the ABI alignment");
471 
472   AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width);
473   if (I != Alignments.end() &&
474       I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) {
475     // Update the abi, preferred alignments.
476     I->ABIAlign = abi_align;
477     I->PrefAlign = pref_align;
478   } else {
479     // Insert before I to keep the vector sorted.
480     Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align,
481                                               pref_align, bit_width));
482   }
483 }
484 
485 DataLayout::PointersTy::iterator
486 DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
487   return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
488                           [](const PointerAlignElem &A, uint32_t AddressSpace) {
489     return A.AddressSpace < AddressSpace;
490   });
491 }
492 
493 void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
494                                      unsigned PrefAlign, uint32_t TypeByteWidth,
495                                      uint32_t IndexWidth) {
496   if (PrefAlign < ABIAlign)
497     report_fatal_error(
498         "Preferred alignment cannot be less than the ABI alignment");
499 
500   PointersTy::iterator I = findPointerLowerBound(AddrSpace);
501   if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
502     Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
503                                              TypeByteWidth, IndexWidth));
504   } else {
505     I->ABIAlign = ABIAlign;
506     I->PrefAlign = PrefAlign;
507     I->TypeByteWidth = TypeByteWidth;
508     I->IndexWidth = IndexWidth;
509   }
510 }
511 
512 /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
513 /// preferred if ABIInfo = false) the layout wants for the specified datatype.
514 unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
515                                       uint32_t BitWidth, bool ABIInfo,
516                                       Type *Ty) const {
517   AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth);
518   // See if we found an exact match. Of if we are looking for an integer type,
519   // but don't have an exact match take the next largest integer. This is where
520   // the lower_bound will point to when it fails an exact match.
521   if (I != Alignments.end() && I->AlignType == (unsigned)AlignType &&
522       (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN))
523     return ABIInfo ? I->ABIAlign : I->PrefAlign;
524 
525   if (AlignType == INTEGER_ALIGN) {
526     // If we didn't have a larger value try the largest value we have.
527     if (I != Alignments.begin()) {
528       --I; // Go to the previous entry and see if its an integer.
529       if (I->AlignType == INTEGER_ALIGN)
530         return ABIInfo ? I->ABIAlign : I->PrefAlign;
531     }
532   } else if (AlignType == VECTOR_ALIGN) {
533     // By default, use natural alignment for vector types. This is consistent
534     // with what clang and llvm-gcc do.
535     unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
536     Align *= cast<VectorType>(Ty)->getNumElements();
537     Align = PowerOf2Ceil(Align);
538     return Align;
539    }
540 
541   // If we still couldn't find a reasonable default alignment, fall back
542   // to a simple heuristic that the alignment is the first power of two
543   // greater-or-equal to the store size of the type.  This is a reasonable
544   // approximation of reality, and if the user wanted something less
545   // less conservative, they should have specified it explicitly in the data
546   // layout.
547   unsigned Align = getTypeStoreSize(Ty);
548   Align = PowerOf2Ceil(Align);
549   return Align;
550 }
551 
552 namespace {
553 
554 class StructLayoutMap {
555   using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
556   LayoutInfoTy LayoutInfo;
557 
558 public:
559   ~StructLayoutMap() {
560     // Remove any layouts.
561     for (const auto &I : LayoutInfo) {
562       StructLayout *Value = I.second;
563       Value->~StructLayout();
564       free(Value);
565     }
566   }
567 
568   StructLayout *&operator[](StructType *STy) {
569     return LayoutInfo[STy];
570   }
571 };
572 
573 } // end anonymous namespace
574 
575 void DataLayout::clear() {
576   LegalIntWidths.clear();
577   Alignments.clear();
578   Pointers.clear();
579   delete static_cast<StructLayoutMap *>(LayoutMap);
580   LayoutMap = nullptr;
581 }
582 
583 DataLayout::~DataLayout() {
584   clear();
585 }
586 
587 const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
588   if (!LayoutMap)
589     LayoutMap = new StructLayoutMap();
590 
591   StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
592   StructLayout *&SL = (*STM)[Ty];
593   if (SL) return SL;
594 
595   // Otherwise, create the struct layout.  Because it is variable length, we
596   // malloc it, then use placement new.
597   int NumElts = Ty->getNumElements();
598   StructLayout *L = (StructLayout *)
599       safe_malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
600 
601   // Set SL before calling StructLayout's ctor.  The ctor could cause other
602   // entries to be added to TheMap, invalidating our reference.
603   SL = L;
604 
605   new (L) StructLayout(Ty, *this);
606 
607   return L;
608 }
609 
610 unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
611   PointersTy::const_iterator I = findPointerLowerBound(AS);
612   if (I == Pointers.end() || I->AddressSpace != AS) {
613     I = findPointerLowerBound(0);
614     assert(I->AddressSpace == 0);
615   }
616   return I->ABIAlign;
617 }
618 
619 unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
620   PointersTy::const_iterator I = findPointerLowerBound(AS);
621   if (I == Pointers.end() || I->AddressSpace != AS) {
622     I = findPointerLowerBound(0);
623     assert(I->AddressSpace == 0);
624   }
625   return I->PrefAlign;
626 }
627 
628 unsigned DataLayout::getPointerSize(unsigned AS) const {
629   PointersTy::const_iterator I = findPointerLowerBound(AS);
630   if (I == Pointers.end() || I->AddressSpace != AS) {
631     I = findPointerLowerBound(0);
632     assert(I->AddressSpace == 0);
633   }
634   return I->TypeByteWidth;
635 }
636 
637 unsigned DataLayout::getMaxPointerSize() const {
638   unsigned MaxPointerSize = 0;
639   for (auto &P : Pointers)
640     MaxPointerSize = std::max(MaxPointerSize, P.TypeByteWidth);
641 
642   return MaxPointerSize;
643 }
644 
645 unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
646   assert(Ty->isPtrOrPtrVectorTy() &&
647          "This should only be called with a pointer or pointer vector type");
648   Ty = Ty->getScalarType();
649   return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
650 }
651 
652 unsigned DataLayout::getIndexSize(unsigned AS) const {
653   PointersTy::const_iterator I = findPointerLowerBound(AS);
654   if (I == Pointers.end() || I->AddressSpace != AS) {
655     I = findPointerLowerBound(0);
656     assert(I->AddressSpace == 0);
657   }
658   return I->IndexWidth;
659 }
660 
661 unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
662   assert(Ty->isPtrOrPtrVectorTy() &&
663          "This should only be called with a pointer or pointer vector type");
664   Ty = Ty->getScalarType();
665   return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
666 }
667 
668 /*!
669   \param abi_or_pref Flag that determines which alignment is returned. true
670   returns the ABI alignment, false returns the preferred alignment.
671   \param Ty The underlying type for which alignment is determined.
672 
673   Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
674   == false) for the requested type \a Ty.
675  */
676 unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
677   AlignTypeEnum AlignType;
678 
679   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
680   switch (Ty->getTypeID()) {
681   // Early escape for the non-numeric types.
682   case Type::LabelTyID:
683     return (abi_or_pref
684             ? getPointerABIAlignment(0)
685             : getPointerPrefAlignment(0));
686   case Type::PointerTyID: {
687     unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
688     return (abi_or_pref
689             ? getPointerABIAlignment(AS)
690             : getPointerPrefAlignment(AS));
691     }
692   case Type::ArrayTyID:
693     return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
694 
695   case Type::StructTyID: {
696     // Packed structure types always have an ABI alignment of one.
697     if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
698       return 1;
699 
700     // Get the layout annotation... which is lazily created on demand.
701     const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
702     unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
703     return std::max(Align, Layout->getAlignment());
704   }
705   case Type::IntegerTyID:
706     AlignType = INTEGER_ALIGN;
707     break;
708   case Type::HalfTyID:
709   case Type::FloatTyID:
710   case Type::DoubleTyID:
711   // PPC_FP128TyID and FP128TyID have different data contents, but the
712   // same size and alignment, so they look the same here.
713   case Type::PPC_FP128TyID:
714   case Type::FP128TyID:
715   case Type::X86_FP80TyID:
716     AlignType = FLOAT_ALIGN;
717     break;
718   case Type::X86_MMXTyID:
719   case Type::VectorTyID:
720     AlignType = VECTOR_ALIGN;
721     break;
722   default:
723     llvm_unreachable("Bad type for getAlignment!!!");
724   }
725 
726   return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty), abi_or_pref, Ty);
727 }
728 
729 unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
730   return getAlignment(Ty, true);
731 }
732 
733 /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
734 /// an integer type of the specified bitwidth.
735 unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
736   return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
737 }
738 
739 unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
740   return getAlignment(Ty, false);
741 }
742 
743 unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
744   unsigned Align = getPrefTypeAlignment(Ty);
745   assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
746   return Log2_32(Align);
747 }
748 
749 IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
750                                        unsigned AddressSpace) const {
751   return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
752 }
753 
754 Type *DataLayout::getIntPtrType(Type *Ty) const {
755   assert(Ty->isPtrOrPtrVectorTy() &&
756          "Expected a pointer or pointer vector type.");
757   unsigned NumBits = getIndexTypeSizeInBits(Ty);
758   IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
759   if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
760     return VectorType::get(IntTy, VecTy->getNumElements());
761   return IntTy;
762 }
763 
764 Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
765   for (unsigned LegalIntWidth : LegalIntWidths)
766     if (Width <= LegalIntWidth)
767       return Type::getIntNTy(C, LegalIntWidth);
768   return nullptr;
769 }
770 
771 unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
772   auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
773   return Max != LegalIntWidths.end() ? *Max : 0;
774 }
775 
776 Type *DataLayout::getIndexType(Type *Ty) const {
777   assert(Ty->isPtrOrPtrVectorTy() &&
778          "Expected a pointer or pointer vector type.");
779   unsigned NumBits = getIndexTypeSizeInBits(Ty);
780   IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
781   if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
782     return VectorType::get(IntTy, VecTy->getNumElements());
783   return IntTy;
784 }
785 
786 int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
787                                            ArrayRef<Value *> Indices) const {
788   int64_t Result = 0;
789 
790   generic_gep_type_iterator<Value* const*>
791     GTI = gep_type_begin(ElemTy, Indices),
792     GTE = gep_type_end(ElemTy, Indices);
793   for (; GTI != GTE; ++GTI) {
794     Value *Idx = GTI.getOperand();
795     if (StructType *STy = GTI.getStructTypeOrNull()) {
796       assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
797       unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
798 
799       // Get structure layout information...
800       const StructLayout *Layout = getStructLayout(STy);
801 
802       // Add in the offset, as calculated by the structure layout info...
803       Result += Layout->getElementOffset(FieldNo);
804     } else {
805       // Get the array index and the size of each array element.
806       if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
807         Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
808     }
809   }
810 
811   return Result;
812 }
813 
814 /// getPreferredAlignment - Return the preferred alignment of the specified
815 /// global.  This includes an explicitly requested alignment (if the global
816 /// has one).
817 unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
818   unsigned GVAlignment = GV->getAlignment();
819   // If a section is specified, always precisely honor explicit alignment,
820   // so we don't insert padding into a section we don't control.
821   if (GVAlignment && GV->hasSection())
822     return GVAlignment;
823 
824   // If no explicit alignment is specified, compute the alignment based on
825   // the IR type. If an alignment is specified, increase it to match the ABI
826   // alignment of the IR type.
827   //
828   // FIXME: Not sure it makes sense to use the alignment of the type if
829   // there's already an explicit alignment specification.
830   Type *ElemType = GV->getValueType();
831   unsigned Alignment = getPrefTypeAlignment(ElemType);
832   if (GVAlignment >= Alignment) {
833     Alignment = GVAlignment;
834   } else if (GVAlignment != 0) {
835     Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
836   }
837 
838   // If no explicit alignment is specified, and the global is large, increase
839   // the alignment to 16.
840   // FIXME: Why 16, specifically?
841   if (GV->hasInitializer() && GVAlignment == 0) {
842     if (Alignment < 16) {
843       // If the global is not external, see if it is large.  If so, give it a
844       // larger alignment.
845       if (getTypeSizeInBits(ElemType) > 128)
846         Alignment = 16;    // 16-byte alignment.
847     }
848   }
849   return Alignment;
850 }
851 
852 /// getPreferredAlignmentLog - Return the preferred alignment of the
853 /// specified global, returned in log form.  This includes an explicitly
854 /// requested alignment (if the global has one).
855 unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
856   return Log2_32(getPreferredAlignment(GV));
857 }
858