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