LLVM  7.0.0svn
DataLayout.cpp
Go to the documentation of this file.
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/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/IR/Value.h"
30 #include "llvm/Support/Casting.h"
33 #include <algorithm>
34 #include <cassert>
35 #include <cstdint>
36 #include <cstdlib>
37 #include <tuple>
38 #include <utility>
39 
40 using namespace llvm;
41 
42 //===----------------------------------------------------------------------===//
43 // Support for StructLayout
44 //===----------------------------------------------------------------------===//
45 
46 StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
47  assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
48  StructAlignment = 0;
49  StructSize = 0;
50  IsPadded = false;
51  NumElements = ST->getNumElements();
52 
53  // Loop over each of the elements, placing them in memory.
54  for (unsigned i = 0, e = NumElements; i != e; ++i) {
55  Type *Ty = ST->getElementType(i);
56  unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
57 
58  // Add padding if necessary to align the data element properly.
59  if ((StructSize & (TyAlign-1)) != 0) {
60  IsPadded = true;
61  StructSize = alignTo(StructSize, TyAlign);
62  }
63 
64  // Keep track of maximum alignment constraint.
65  StructAlignment = std::max(TyAlign, StructAlignment);
66 
67  MemberOffsets[i] = StructSize;
68  StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
69  }
70 
71  // Empty structures have alignment of 1 byte.
72  if (StructAlignment == 0) StructAlignment = 1;
73 
74  // Add padding to the end of the struct so that it could be put in an array
75  // and all array elements would be aligned correctly.
76  if ((StructSize & (StructAlignment-1)) != 0) {
77  IsPadded = true;
78  StructSize = alignTo(StructSize, StructAlignment);
79  }
80 }
81 
82 /// getElementContainingOffset - Given a valid offset into the structure,
83 /// return the structure index that contains it.
85  const uint64_t *SI =
86  std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
87  assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
88  --SI;
89  assert(*SI <= Offset && "upper_bound didn't work");
90  assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
91  (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
92  "Upper bound didn't work!");
93 
94  // Multiple fields can have the same offset if any of them are zero sized.
95  // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
96  // at the i32 element, because it is the last element at that offset. This is
97  // the right one to return, because anything after it will have a higher
98  // offset, implying that this element is non-empty.
99  return SI-&MemberOffsets[0];
100 }
101 
102 //===----------------------------------------------------------------------===//
103 // LayoutAlignElem, LayoutAlign support
104 //===----------------------------------------------------------------------===//
105 
107 LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
108  unsigned pref_align, uint32_t bit_width) {
109  assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
110  LayoutAlignElem retval;
111  retval.AlignType = align_type;
112  retval.ABIAlign = abi_align;
113  retval.PrefAlign = pref_align;
114  retval.TypeBitWidth = bit_width;
115  return retval;
116 }
117 
118 bool
120  return (AlignType == rhs.AlignType
121  && ABIAlign == rhs.ABIAlign
122  && PrefAlign == rhs.PrefAlign
123  && TypeBitWidth == rhs.TypeBitWidth);
124 }
125 
126 //===----------------------------------------------------------------------===//
127 // PointerAlignElem, PointerAlign support
128 //===----------------------------------------------------------------------===//
129 
132  unsigned PrefAlign, uint32_t TypeByteWidth,
133  uint32_t IndexWidth) {
134  assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
135  PointerAlignElem retval;
136  retval.AddressSpace = AddressSpace;
137  retval.ABIAlign = ABIAlign;
138  retval.PrefAlign = PrefAlign;
139  retval.TypeByteWidth = TypeByteWidth;
140  retval.IndexWidth = IndexWidth;
141  return retval;
142 }
143 
144 bool
146  return (ABIAlign == rhs.ABIAlign
147  && AddressSpace == rhs.AddressSpace
148  && PrefAlign == rhs.PrefAlign
149  && TypeByteWidth == rhs.TypeByteWidth
150  && IndexWidth == rhs.IndexWidth);
151 }
152 
153 //===----------------------------------------------------------------------===//
154 // DataLayout Class Implementation
155 //===----------------------------------------------------------------------===//
156 
158  if (T.isOSBinFormatMachO())
159  return "-m:o";
160  if (T.isOSWindows() && T.isOSBinFormatCOFF())
161  return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
162  return "-m:e";
163 }
164 
166  { INTEGER_ALIGN, 1, 1, 1 }, // i1
167  { INTEGER_ALIGN, 8, 1, 1 }, // i8
168  { INTEGER_ALIGN, 16, 2, 2 }, // i16
169  { INTEGER_ALIGN, 32, 4, 4 }, // i32
170  { INTEGER_ALIGN, 64, 4, 8 }, // i64
171  { FLOAT_ALIGN, 16, 2, 2 }, // half
172  { FLOAT_ALIGN, 32, 4, 4 }, // float
173  { FLOAT_ALIGN, 64, 8, 8 }, // double
174  { FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ...
175  { VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ...
176  { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
177  { AGGREGATE_ALIGN, 0, 0, 8 } // struct
178 };
179 
181  clear();
182 
183  LayoutMap = nullptr;
184  BigEndian = false;
185  AllocaAddrSpace = 0;
186  StackNaturalAlign = 0;
187  ProgramAddrSpace = 0;
188  ManglingMode = MM_None;
189  NonIntegralAddressSpaces.clear();
190 
191  // Default alignments
192  for (const LayoutAlignElem &E : DefaultAlignments) {
193  setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
194  E.TypeBitWidth);
195  }
196  setPointerAlignment(0, 8, 8, 8, 8);
197 
198  parseSpecifier(Desc);
199 }
200 
201 /// Checked version of split, to ensure mandatory subparts.
202 static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
203  assert(!Str.empty() && "parse error, string can't be empty here");
204  std::pair<StringRef, StringRef> Split = Str.split(Separator);
205  if (Split.second.empty() && Split.first != Str)
206  report_fatal_error("Trailing separator in datalayout string");
207  if (!Split.second.empty() && Split.first.empty())
208  report_fatal_error("Expected token before separator in datalayout string");
209  return Split;
210 }
211 
212 /// Get an unsigned integer, including error checks.
213 static unsigned getInt(StringRef R) {
214  unsigned Result;
215  bool error = R.getAsInteger(10, Result); (void)error;
216  if (error)
217  report_fatal_error("not a number, or does not fit in an unsigned int");
218  return Result;
219 }
220 
221 /// Convert bits into bytes. Assert if not a byte width multiple.
222 static unsigned inBytes(unsigned Bits) {
223  if (Bits % 8)
224  report_fatal_error("number of bits must be a byte width multiple");
225  return Bits / 8;
226 }
227 
228 static unsigned getAddrSpace(StringRef R) {
229  unsigned AddrSpace = getInt(R);
230  if (!isUInt<24>(AddrSpace))
231  report_fatal_error("Invalid address space, must be a 24-bit integer");
232  return AddrSpace;
233 }
234 
235 void DataLayout::parseSpecifier(StringRef Desc) {
236  StringRepresentation = Desc;
237  while (!Desc.empty()) {
238  // Split at '-'.
239  std::pair<StringRef, StringRef> Split = split(Desc, '-');
240  Desc = Split.second;
241 
242  // Split at ':'.
243  Split = split(Split.first, ':');
244 
245  // Aliases used below.
246  StringRef &Tok = Split.first; // Current token.
247  StringRef &Rest = Split.second; // The rest of the string.
248 
249  if (Tok == "ni") {
250  do {
251  Split = split(Rest, ':');
252  Rest = Split.second;
253  unsigned AS = getInt(Split.first);
254  if (AS == 0)
255  report_fatal_error("Address space 0 can never be non-integral");
256  NonIntegralAddressSpaces.push_back(AS);
257  } while (!Rest.empty());
258 
259  continue;
260  }
261 
262  char Specifier = Tok.front();
263  Tok = Tok.substr(1);
264 
265  switch (Specifier) {
266  case 's':
267  // Ignored for backward compatibility.
268  // FIXME: remove this on LLVM 4.0.
269  break;
270  case 'E':
271  BigEndian = true;
272  break;
273  case 'e':
274  BigEndian = false;
275  break;
276  case 'p': {
277  // Address space.
278  unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
279  if (!isUInt<24>(AddrSpace))
280  report_fatal_error("Invalid address space, must be a 24bit integer");
281 
282  // Size.
283  if (Rest.empty())
285  "Missing size specification for pointer in datalayout string");
286  Split = split(Rest, ':');
287  unsigned PointerMemSize = inBytes(getInt(Tok));
288  if (!PointerMemSize)
289  report_fatal_error("Invalid pointer size of 0 bytes");
290 
291  // ABI alignment.
292  if (Rest.empty())
294  "Missing alignment specification for pointer in datalayout string");
295  Split = split(Rest, ':');
296  unsigned PointerABIAlign = inBytes(getInt(Tok));
297  if (!isPowerOf2_64(PointerABIAlign))
299  "Pointer ABI alignment must be a power of 2");
300 
301  // Size of index used in GEP for address calculation.
302  // The parameter is optional. By default it is equal to size of pointer.
303  unsigned IndexSize = PointerMemSize;
304 
305  // Preferred alignment.
306  unsigned PointerPrefAlign = PointerABIAlign;
307  if (!Rest.empty()) {
308  Split = split(Rest, ':');
309  PointerPrefAlign = inBytes(getInt(Tok));
310  if (!isPowerOf2_64(PointerPrefAlign))
312  "Pointer preferred alignment must be a power of 2");
313 
314  // Now read the index. It is the second optional parameter here.
315  if (!Rest.empty()) {
316  Split = split(Rest, ':');
317  IndexSize = inBytes(getInt(Tok));
318  if (!IndexSize)
319  report_fatal_error("Invalid index size of 0 bytes");
320  }
321  }
322  setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
323  PointerMemSize, IndexSize);
324  break;
325  }
326  case 'i':
327  case 'v':
328  case 'f':
329  case 'a': {
330  AlignTypeEnum AlignType;
331  switch (Specifier) {
332  default: llvm_unreachable("Unexpected specifier!");
333  case 'i': AlignType = INTEGER_ALIGN; break;
334  case 'v': AlignType = VECTOR_ALIGN; break;
335  case 'f': AlignType = FLOAT_ALIGN; break;
336  case 'a': AlignType = AGGREGATE_ALIGN; break;
337  }
338 
339  // Bit size.
340  unsigned Size = Tok.empty() ? 0 : getInt(Tok);
341 
342  if (AlignType == AGGREGATE_ALIGN && Size != 0)
344  "Sized aggregate specification in datalayout string");
345 
346  // ABI alignment.
347  if (Rest.empty())
349  "Missing alignment specification in datalayout string");
350  Split = split(Rest, ':');
351  unsigned ABIAlign = inBytes(getInt(Tok));
352  if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
354  "ABI alignment specification must be >0 for non-aggregate types");
355 
356  // Preferred alignment.
357  unsigned PrefAlign = ABIAlign;
358  if (!Rest.empty()) {
359  Split = split(Rest, ':');
360  PrefAlign = inBytes(getInt(Tok));
361  }
362 
363  setAlignment(AlignType, ABIAlign, PrefAlign, Size);
364 
365  break;
366  }
367  case 'n': // Native integer types.
368  while (true) {
369  unsigned Width = getInt(Tok);
370  if (Width == 0)
372  "Zero width native integer type in datalayout string");
373  LegalIntWidths.push_back(Width);
374  if (Rest.empty())
375  break;
376  Split = split(Rest, ':');
377  }
378  break;
379  case 'S': { // Stack natural alignment.
380  StackNaturalAlign = inBytes(getInt(Tok));
381  break;
382  }
383  case 'P': { // Function address space.
384  ProgramAddrSpace = getAddrSpace(Tok);
385  break;
386  }
387  case 'A': { // Default stack/alloca address space.
388  AllocaAddrSpace = getAddrSpace(Tok);
389  break;
390  }
391  case 'm':
392  if (!Tok.empty())
393  report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
394  if (Rest.empty())
395  report_fatal_error("Expected mangling specifier in datalayout string");
396  if (Rest.size() > 1)
397  report_fatal_error("Unknown mangling specifier in datalayout string");
398  switch(Rest[0]) {
399  default:
400  report_fatal_error("Unknown mangling in datalayout string");
401  case 'e':
402  ManglingMode = MM_ELF;
403  break;
404  case 'o':
405  ManglingMode = MM_MachO;
406  break;
407  case 'm':
408  ManglingMode = MM_Mips;
409  break;
410  case 'w':
411  ManglingMode = MM_WinCOFF;
412  break;
413  case 'x':
414  ManglingMode = MM_WinCOFFX86;
415  break;
416  }
417  break;
418  default:
419  report_fatal_error("Unknown specifier in datalayout string");
420  break;
421  }
422  }
423 }
424 
426  init(M);
427 }
428 
429 void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
430 
431 bool DataLayout::operator==(const DataLayout &Other) const {
432  bool Ret = BigEndian == Other.BigEndian &&
433  AllocaAddrSpace == Other.AllocaAddrSpace &&
434  StackNaturalAlign == Other.StackNaturalAlign &&
435  ProgramAddrSpace == Other.ProgramAddrSpace &&
436  ManglingMode == Other.ManglingMode &&
437  LegalIntWidths == Other.LegalIntWidths &&
438  Alignments == Other.Alignments && Pointers == Other.Pointers;
439  // Note: getStringRepresentation() might differs, it is not canonicalized
440  return Ret;
441 }
442 
444 DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
445  uint32_t BitWidth) {
446  auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
447  return std::lower_bound(Alignments.begin(), Alignments.end(), Pair,
448  [](const LayoutAlignElem &LHS,
449  const std::pair<unsigned, uint32_t> &RHS) {
450  return std::tie(LHS.AlignType, LHS.TypeBitWidth) <
451  std::tie(RHS.first, RHS.second);
452  });
453 }
454 
455 void
456 DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
457  unsigned pref_align, uint32_t bit_width) {
458  if (!isUInt<24>(bit_width))
459  report_fatal_error("Invalid bit width, must be a 24bit integer");
460  if (!isUInt<16>(abi_align))
461  report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
462  if (!isUInt<16>(pref_align))
463  report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
464  if (abi_align != 0 && !isPowerOf2_64(abi_align))
465  report_fatal_error("Invalid ABI alignment, must be a power of 2");
466  if (pref_align != 0 && !isPowerOf2_64(pref_align))
467  report_fatal_error("Invalid preferred alignment, must be a power of 2");
468 
469  if (pref_align < abi_align)
471  "Preferred alignment cannot be less than the ABI alignment");
472 
473  AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width);
474  if (I != Alignments.end() &&
475  I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) {
476  // Update the abi, preferred alignments.
477  I->ABIAlign = abi_align;
478  I->PrefAlign = pref_align;
479  } else {
480  // Insert before I to keep the vector sorted.
481  Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align,
482  pref_align, bit_width));
483  }
484 }
485 
487 DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
488  return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
489  [](const PointerAlignElem &A, uint32_t AddressSpace) {
490  return A.AddressSpace < AddressSpace;
491  });
492 }
493 
494 void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
495  unsigned PrefAlign, uint32_t TypeByteWidth,
496  uint32_t IndexWidth) {
497  if (PrefAlign < ABIAlign)
499  "Preferred alignment cannot be less than the ABI alignment");
500 
501  PointersTy::iterator I = findPointerLowerBound(AddrSpace);
502  if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
503  Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
504  TypeByteWidth, IndexWidth));
505  } else {
506  I->ABIAlign = ABIAlign;
507  I->PrefAlign = PrefAlign;
508  I->TypeByteWidth = TypeByteWidth;
509  I->IndexWidth = IndexWidth;
510  }
511 }
512 
513 /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
514 /// preferred if ABIInfo = false) the layout wants for the specified datatype.
515 unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
516  uint32_t BitWidth, bool ABIInfo,
517  Type *Ty) const {
518  AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth);
519  // See if we found an exact match. Of if we are looking for an integer type,
520  // but don't have an exact match take the next largest integer. This is where
521  // the lower_bound will point to when it fails an exact match.
522  if (I != Alignments.end() && I->AlignType == (unsigned)AlignType &&
523  (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN))
524  return ABIInfo ? I->ABIAlign : I->PrefAlign;
525 
526  if (AlignType == INTEGER_ALIGN) {
527  // If we didn't have a larger value try the largest value we have.
528  if (I != Alignments.begin()) {
529  --I; // Go to the previous entry and see if its an integer.
530  if (I->AlignType == INTEGER_ALIGN)
531  return ABIInfo ? I->ABIAlign : I->PrefAlign;
532  }
533  } else if (AlignType == VECTOR_ALIGN) {
534  // By default, use natural alignment for vector types. This is consistent
535  // with what clang and llvm-gcc do.
536  unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
537  Align *= cast<VectorType>(Ty)->getNumElements();
538  Align = PowerOf2Ceil(Align);
539  return Align;
540  }
541 
542  // If we still couldn't find a reasonable default alignment, fall back
543  // to a simple heuristic that the alignment is the first power of two
544  // greater-or-equal to the store size of the type. This is a reasonable
545  // approximation of reality, and if the user wanted something less
546  // less conservative, they should have specified it explicitly in the data
547  // layout.
548  unsigned Align = getTypeStoreSize(Ty);
549  Align = PowerOf2Ceil(Align);
550  return Align;
551 }
552 
553 namespace {
554 
555 class StructLayoutMap {
556  using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
557  LayoutInfoTy LayoutInfo;
558 
559 public:
560  ~StructLayoutMap() {
561  // Remove any layouts.
562  for (const auto &I : LayoutInfo) {
563  StructLayout *Value = I.second;
564  Value->~StructLayout();
565  free(Value);
566  }
567  }
568 
569  StructLayout *&operator[](StructType *STy) {
570  return LayoutInfo[STy];
571  }
572 };
573 
574 } // end anonymous namespace
575 
576 void DataLayout::clear() {
577  LegalIntWidths.clear();
578  Alignments.clear();
579  Pointers.clear();
580  delete static_cast<StructLayoutMap *>(LayoutMap);
581  LayoutMap = nullptr;
582 }
583 
585  clear();
586 }
587 
589  if (!LayoutMap)
590  LayoutMap = new StructLayoutMap();
591 
592  StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
593  StructLayout *&SL = (*STM)[Ty];
594  if (SL) return SL;
595 
596  // Otherwise, create the struct layout. Because it is variable length, we
597  // malloc it, then use placement new.
598  int NumElts = Ty->getNumElements();
599  StructLayout *L =
600  (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
601  if (L == nullptr)
602  report_bad_alloc_error("Allocation of StructLayout elements failed.");
603 
604  // Set SL before calling StructLayout's ctor. The ctor could cause other
605  // entries to be added to TheMap, invalidating our reference.
606  SL = L;
607 
608  new (L) StructLayout(Ty, *this);
609 
610  return L;
611 }
612 
613 unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
614  PointersTy::const_iterator I = findPointerLowerBound(AS);
615  if (I == Pointers.end() || I->AddressSpace != AS) {
616  I = findPointerLowerBound(0);
617  assert(I->AddressSpace == 0);
618  }
619  return I->ABIAlign;
620 }
621 
622 unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
623  PointersTy::const_iterator I = findPointerLowerBound(AS);
624  if (I == Pointers.end() || I->AddressSpace != AS) {
625  I = findPointerLowerBound(0);
626  assert(I->AddressSpace == 0);
627  }
628  return I->PrefAlign;
629 }
630 
631 unsigned DataLayout::getPointerSize(unsigned AS) const {
632  PointersTy::const_iterator I = findPointerLowerBound(AS);
633  if (I == Pointers.end() || I->AddressSpace != AS) {
634  I = findPointerLowerBound(0);
635  assert(I->AddressSpace == 0);
636  }
637  return I->TypeByteWidth;
638 }
639 
641  assert(Ty->isPtrOrPtrVectorTy() &&
642  "This should only be called with a pointer or pointer vector type");
643  Ty = Ty->getScalarType();
644  return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
645 }
646 
647 unsigned DataLayout::getIndexSize(unsigned AS) const {
648  PointersTy::const_iterator I = findPointerLowerBound(AS);
649  if (I == Pointers.end() || I->AddressSpace != AS) {
650  I = findPointerLowerBound(0);
651  assert(I->AddressSpace == 0);
652  }
653  return I->IndexWidth;
654 }
655 
657  assert(Ty->isPtrOrPtrVectorTy() &&
658  "This should only be called with a pointer or pointer vector type");
659  Ty = Ty->getScalarType();
660  return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
661 }
662 
663 /*!
664  \param abi_or_pref Flag that determines which alignment is returned. true
665  returns the ABI alignment, false returns the preferred alignment.
666  \param Ty The underlying type for which alignment is determined.
667 
668  Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
669  == false) for the requested type \a Ty.
670  */
671 unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
672  AlignTypeEnum AlignType;
673 
674  assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
675  switch (Ty->getTypeID()) {
676  // Early escape for the non-numeric types.
677  case Type::LabelTyID:
678  return (abi_or_pref
679  ? getPointerABIAlignment(0)
680  : getPointerPrefAlignment(0));
681  case Type::PointerTyID: {
682  unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
683  return (abi_or_pref
684  ? getPointerABIAlignment(AS)
685  : getPointerPrefAlignment(AS));
686  }
687  case Type::ArrayTyID:
688  return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
689 
690  case Type::StructTyID: {
691  // Packed structure types always have an ABI alignment of one.
692  if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
693  return 1;
694 
695  // Get the layout annotation... which is lazily created on demand.
696  const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
697  unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
698  return std::max(Align, Layout->getAlignment());
699  }
700  case Type::IntegerTyID:
701  AlignType = INTEGER_ALIGN;
702  break;
703  case Type::HalfTyID:
704  case Type::FloatTyID:
705  case Type::DoubleTyID:
706  // PPC_FP128TyID and FP128TyID have different data contents, but the
707  // same size and alignment, so they look the same here.
708  case Type::PPC_FP128TyID:
709  case Type::FP128TyID:
710  case Type::X86_FP80TyID:
711  AlignType = FLOAT_ALIGN;
712  break;
713  case Type::X86_MMXTyID:
714  case Type::VectorTyID:
715  AlignType = VECTOR_ALIGN;
716  break;
717  default:
718  llvm_unreachable("Bad type for getAlignment!!!");
719  }
720 
721  return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty), abi_or_pref, Ty);
722 }
723 
725  return getAlignment(Ty, true);
726 }
727 
728 /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
729 /// an integer type of the specified bitwidth.
730 unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
731  return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
732 }
733 
735  return getAlignment(Ty, false);
736 }
737 
739  unsigned Align = getPrefTypeAlignment(Ty);
740  assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
741  return Log2_32(Align);
742 }
743 
745  unsigned AddressSpace) const {
746  return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
747 }
748 
750  assert(Ty->isPtrOrPtrVectorTy() &&
751  "Expected a pointer or pointer vector type.");
752  unsigned NumBits = getIndexTypeSizeInBits(Ty);
753  IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
754  if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
755  return VectorType::get(IntTy, VecTy->getNumElements());
756  return IntTy;
757 }
758 
760  for (unsigned LegalIntWidth : LegalIntWidths)
761  if (Width <= LegalIntWidth)
762  return Type::getIntNTy(C, LegalIntWidth);
763  return nullptr;
764 }
765 
767  auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
768  return Max != LegalIntWidths.end() ? *Max : 0;
769 }
770 
772  assert(Ty->isPtrOrPtrVectorTy() &&
773  "Expected a pointer or pointer vector type.");
774  unsigned NumBits = getIndexTypeSizeInBits(Ty);
775  IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
776  if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
777  return VectorType::get(IntTy, VecTy->getNumElements());
778  return IntTy;
779 }
780 
782  ArrayRef<Value *> Indices) const {
783  int64_t Result = 0;
784 
786  GTI = gep_type_begin(ElemTy, Indices),
787  GTE = gep_type_end(ElemTy, Indices);
788  for (; GTI != GTE; ++GTI) {
789  Value *Idx = GTI.getOperand();
790  if (StructType *STy = GTI.getStructTypeOrNull()) {
791  assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
792  unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
793 
794  // Get structure layout information...
795  const StructLayout *Layout = getStructLayout(STy);
796 
797  // Add in the offset, as calculated by the structure layout info...
798  Result += Layout->getElementOffset(FieldNo);
799  } else {
800  // Get the array index and the size of each array element.
801  if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
802  Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
803  }
804  }
805 
806  return Result;
807 }
808 
809 /// getPreferredAlignment - Return the preferred alignment of the specified
810 /// global. This includes an explicitly requested alignment (if the global
811 /// has one).
813  Type *ElemType = GV->getValueType();
814  unsigned Alignment = getPrefTypeAlignment(ElemType);
815  unsigned GVAlignment = GV->getAlignment();
816  if (GVAlignment >= Alignment) {
817  Alignment = GVAlignment;
818  } else if (GVAlignment != 0) {
819  Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
820  }
821 
822  if (GV->hasInitializer() && GVAlignment == 0) {
823  if (Alignment < 16) {
824  // If the global is not external, see if it is large. If so, give it a
825  // larger alignment.
826  if (getTypeSizeInBits(ElemType) > 128)
827  Alignment = 16; // 16-byte alignment.
828  }
829  }
830  return Alignment;
831 }
832 
833 /// getPreferredAlignmentLog - Return the preferred alignment of the
834 /// specified global, returned in log form. This includes an explicitly
835 /// requested alignment (if the global has one).
837  return Log2_32(getPreferredAlignment(GV));
838 }
uint64_t CallInst * C
unsigned getAlignment() const
Definition: GlobalObject.h:59
7: Labels
Definition: Type.h:64
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
void reset(StringRef LayoutDescription)
Parse a data layout string (with fallback to default values).
Definition: DataLayout.cpp:180
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:329
AlignTypeEnum
Enum used to categorize the alignment types stored by LayoutAlignElem.
Definition: DataLayout.h:51
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:115
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:314
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:262
unsigned getPointerPrefAlignment(unsigned AS=0) const
Return target&#39;s alignment for stack-based pointers FIXME: The defaults need to be removed once all of...
Definition: DataLayout.cpp:622
2: 32-bit floating point type
Definition: Type.h:59
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:313
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:588
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
gep_type_iterator gep_type_end(const User *GEP)
#define error(X)
13: Structures
Definition: Type.h:73
static std::pair< StringRef, StringRef > split(StringRef Str, char Separator)
Checked version of split, to ensure mandatory subparts.
Definition: DataLayout.cpp:202
4: 80-bit floating point type (X87)
Definition: Type.h:61
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:677
1: 16-bit floating point type
Definition: Type.h:58
bool isOpaque() const
Return true if this is a type with an identity that has no body specified yet.
Definition: DerivedTypes.h:269
15: Pointers
Definition: Type.h:75
Type * getSmallestLegalIntType(LLVMContext &C, unsigned Width=0) const
Returns the smallest integer type with size at least as big as Width bits.
Definition: DataLayout.cpp:759
unsigned getPointerABIAlignment(unsigned AS) const
Layout pointer alignment.
Definition: DataLayout.cpp:613
static const char * getManglingComponent(const Triple &T)
Definition: DataLayout.cpp:157
unsigned getElementContainingOffset(uint64_t Offset) const
Given a valid byte offset into the structure, returns the structure index that contains it...
Definition: DataLayout.cpp:84
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:130
static unsigned getAddrSpace(StringRef R)
Definition: DataLayout.cpp:228
unsigned getIndexSize(unsigned AS) const
Definition: DataLayout.cpp:647
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:515
Type * getIndexType(Type *PtrTy) const
Returns the type of a GEP index.
Definition: DataLayout.cpp:771
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:361
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:640
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:138
static unsigned getInt(StringRef R)
Get an unsigned integer, including error checks.
Definition: DataLayout.cpp:213
Class to represent struct types.
Definition: DerivedTypes.h:201
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
unsigned getPreferredTypeAlignmentShift(Type *Ty) const
Returns the preferred alignment for the specified type, returned as log2 of the value (a shift amount...
Definition: DataLayout.cpp:738
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:285
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align, unsigned pref_align, uint32_t bit_width)
Definition: DataLayout.cpp:107
auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:910
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
static const LayoutAlignElem DefaultAlignments[]
Definition: DataLayout.cpp:165
Layout pointer alignment element.
Definition: DataLayout.h:90
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return &#39;this&#39;.
Definition: Type.h:301
11: Arbitrary bit width integers
Definition: Type.h:71
bool isOSWindows() const
Tests whether the OS is Windows.
Definition: Triple.h:562
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:744
unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global, returned in log form.
Definition: DataLayout.cpp:836
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
bool isOSBinFormatCOFF() const
Tests whether the OS uses the COFF binary format.
Definition: Triple.h:593
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size FIXME: The defaults need to be removed once all of the backends/clients are updat...
Definition: DataLayout.cpp:631
This file contains the declarations for the subclasses of Constant, which represent the different fla...
unsigned getPrefTypeAlignment(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:734
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:426
bool operator==(const PointerAlignElem &rhs) const
Definition: DataLayout.cpp:145
unsigned getLargestLegalIntTypeSizeInBits() const
Returns the size of largest legal integer type size, or 0 if none are set.
Definition: DataLayout.cpp:766
6: 128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:63
Class to represent integer types.
Definition: DerivedTypes.h:40
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
Definition: DataLayout.cpp:656
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:598
const AMDGPUAS & AS
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool operator==(const DataLayout &Other) const
Definition: DataLayout.cpp:431
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:497
14: Arrays
Definition: Type.h:74
void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:224
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:240
16: SIMD &#39;packed&#39; format, or other vector type
Definition: Type.h:76
Module.h This file contains the declarations for the Module class.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
AddressSpace
Definition: NVPTXBaseInfo.h:22
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:724
unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const
Returns the minimum ABI-required alignment for an integer type of the specified bitwidth.
Definition: DataLayout.cpp:730
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition: Type.cpp:180
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:531
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
Definition: DataLayout.cpp:812
Class to represent vector types.
Definition: DerivedTypes.h:393
typename SuperClass::iterator iterator
Definition: SmallVector.h:328
DataLayout(StringRef LayoutDescription)
Constructs a DataLayout from a specification string. See reset().
Definition: DataLayout.h:186
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:210
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:422
bool isPacked() const
Definition: DerivedTypes.h:261
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:537
Layout alignment element.
Definition: DataLayout.h:71
#define I(x, y, z)
Definition: MD5.cpp:58
static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign, unsigned PrefAlign, uint32_t TypeByteWidth, uint32_t IndexWidth)
Initializer.
Definition: DataLayout.cpp:131
Type * getValueType() const
Definition: GlobalValue.h:275
unsigned AlignType
Alignment type from AlignTypeEnum.
Definition: DataLayout.h:73
constexpr bool isUInt< 16 >(uint64_t x)
Definition: MathExtras.h:338
static unsigned inBytes(unsigned Bits)
Convert bits into bytes. Assert if not a byte width multiple.
Definition: DataLayout.cpp:222
3: 64-bit floating point type
Definition: Type.h:60
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM_NODISCARD char front() const
front - Get the first character in the string.
Definition: StringRef.h:142
LLVM Value Representation.
Definition: Value.h:73
bool hasInitializer() const
Definitions have initializers, declarations don&#39;t.
unsigned getAlignment() const
Definition: DataLayout.h:527
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:593
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool operator==(const LayoutAlignElem &rhs) const
Definition: DataLayout.cpp:119
9: MMX vectors (64 bits, X86 specific)
Definition: Type.h:66
static void Split(std::vector< std::string > &V, StringRef S)
Splits a string of comma separated items in to a vector of strings.
int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef< Value *> Indices) const
Returns the offset from the beginning of the type for the specified indices.
Definition: DataLayout.cpp:781
void init(const Module *M)
Definition: DataLayout.cpp:429
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition: MathExtras.h:651
5: 128-bit floating point type (112-bit mantissa)
Definition: Type.h:62
gep_type_iterator gep_type_begin(const User *GEP)