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