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