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