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