1 //===-- DataLayout.cpp - Data size & alignment routines --------------------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines layout properties related to datatype size/offset/alignment
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.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/GetElementPtrTypeIterator.h"
27 #include "llvm/Support/ManagedStatic.h"
28 #include "llvm/Support/MathExtras.h"
29 #include "llvm/Support/Mutex.h"
30 #include "llvm/Support/raw_ostream.h"
35 // Handle the Pass registration stuff necessary to use DataLayout's.
37 // Register the default SparcV9 implementation...
38 INITIALIZE_PASS(DataLayout, "datalayout", "Data Layout", false, true)
39 char DataLayout::ID = 0;
41 //===----------------------------------------------------------------------===//
42 // Support for StructLayout
43 //===----------------------------------------------------------------------===//
45 StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
46 assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
49 NumElements = ST->getNumElements();
51 // Loop over each of the elements, placing them in memory.
52 for (unsigned i = 0, e = NumElements; i != e; ++i) {
53 Type *Ty = ST->getElementType(i);
54 unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
56 // Add padding if necessary to align the data element properly.
57 if ((StructSize & (TyAlign-1)) != 0)
58 StructSize = DataLayout::RoundUpAlignment(StructSize, TyAlign);
60 // Keep track of maximum alignment constraint.
61 StructAlignment = std::max(TyAlign, StructAlignment);
63 MemberOffsets[i] = StructSize;
64 StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
67 // Empty structures have alignment of 1 byte.
68 if (StructAlignment == 0) StructAlignment = 1;
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 ((StructSize & (StructAlignment-1)) != 0)
73 StructSize = DataLayout::RoundUpAlignment(StructSize, StructAlignment);
77 /// getElementContainingOffset - Given a valid offset into the structure,
78 /// return the structure index that contains it.
79 unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
81 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
82 assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
84 assert(*SI <= Offset && "upper_bound didn't work");
85 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
86 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
87 "Upper bound didn't work!");
89 // Multiple fields can have the same offset if any of them are zero sized.
90 // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
91 // at the i32 element, because it is the last element at that offset. This is
92 // the right one to return, because anything after it will have a higher
93 // offset, implying that this element is non-empty.
94 return SI-&MemberOffsets[0];
97 //===----------------------------------------------------------------------===//
98 // LayoutAlignElem, LayoutAlign support
99 //===----------------------------------------------------------------------===//
102 LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
103 unsigned pref_align, uint32_t bit_width) {
104 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
105 LayoutAlignElem retval;
106 retval.AlignType = align_type;
107 retval.ABIAlign = abi_align;
108 retval.PrefAlign = pref_align;
109 retval.TypeBitWidth = bit_width;
114 LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
115 return (AlignType == rhs.AlignType
116 && ABIAlign == rhs.ABIAlign
117 && PrefAlign == rhs.PrefAlign
118 && TypeBitWidth == rhs.TypeBitWidth);
121 const LayoutAlignElem
122 DataLayout::InvalidAlignmentElem = { INVALID_ALIGN, 0, 0, 0 };
124 //===----------------------------------------------------------------------===//
125 // PointerAlignElem, PointerAlign support
126 //===----------------------------------------------------------------------===//
129 PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
130 unsigned PrefAlign, uint32_t TypeByteWidth) {
131 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
132 PointerAlignElem retval;
133 retval.AddressSpace = AddressSpace;
134 retval.ABIAlign = ABIAlign;
135 retval.PrefAlign = PrefAlign;
136 retval.TypeByteWidth = TypeByteWidth;
141 PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
142 return (ABIAlign == rhs.ABIAlign
143 && AddressSpace == rhs.AddressSpace
144 && PrefAlign == rhs.PrefAlign
145 && TypeByteWidth == rhs.TypeByteWidth);
148 const PointerAlignElem
149 DataLayout::InvalidPointerElem = { 0U, 0U, 0U, ~0U };
151 //===----------------------------------------------------------------------===//
152 // DataLayout Class Implementation
153 //===----------------------------------------------------------------------===//
155 static const LayoutAlignElem DefaultAlignments[] = {
156 { INTEGER_ALIGN, 1, 1, 1 }, // i1
157 { INTEGER_ALIGN, 8, 1, 1 }, // i8
158 { INTEGER_ALIGN, 16, 2, 2 }, // i16
159 { INTEGER_ALIGN, 32, 4, 4 }, // i32
160 { INTEGER_ALIGN, 64, 4, 8 }, // i64
161 { FLOAT_ALIGN, 16, 2, 2 }, // half
162 { FLOAT_ALIGN, 32, 4, 4 }, // float
163 { FLOAT_ALIGN, 64, 8, 8 }, // double
164 { FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ...
165 { VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ...
166 { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
167 { AGGREGATE_ALIGN, 0, 0, 8 } // struct
170 void DataLayout::init(StringRef Desc) {
171 initializeDataLayoutPass(*PassRegistry::getPassRegistry());
174 LittleEndian = false;
175 StackNaturalAlign = 0;
177 // Default alignments
178 for (int I = 0, N = array_lengthof(DefaultAlignments); I < N; ++I) {
179 const LayoutAlignElem &E = DefaultAlignments[I];
180 setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
183 setPointerAlignment(0, 8, 8, 8);
185 parseSpecifier(Desc);
188 /// Checked version of split, to ensure mandatory subparts.
189 static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
190 assert(!Str.empty() && "parse error, string can't be empty here");
191 std::pair<StringRef, StringRef> Split = Str.split(Separator);
192 assert((!Split.second.empty() || Split.first == Str) &&
193 "a trailing separator is not allowed");
197 /// Get an unsinged integer, including error checks.
198 static unsigned getInt(StringRef R) {
200 bool error = R.getAsInteger(10, Result); (void)error;
201 assert(!error && "not a number, or does not fit in an unsigned int");
205 /// Convert bits into bytes. Assert if not a byte width multiple.
206 static unsigned inBytes(unsigned Bits) {
207 assert(Bits % 8 == 0 && "number of bits must be a byte width multiple");
211 void DataLayout::parseSpecifier(StringRef Desc) {
212 while (!Desc.empty()) {
214 std::pair<StringRef, StringRef> Split = split(Desc, '-');
218 Split = split(Split.first, ':');
220 // Aliases used below.
221 StringRef &Tok = Split.first; // Current token.
222 StringRef &Rest = Split.second; // The rest of the string.
224 char Specifier = Tok.front();
229 LittleEndian = false;
236 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
237 assert(AddrSpace < 1 << 24 &&
238 "Invalid address space, must be a 24bit integer");
241 Split = split(Rest, ':');
242 unsigned PointerMemSize = inBytes(getInt(Tok));
245 Split = split(Rest, ':');
246 unsigned PointerABIAlign = inBytes(getInt(Tok));
248 // Preferred alignment.
249 unsigned PointerPrefAlign = PointerABIAlign;
251 Split = split(Rest, ':');
252 PointerPrefAlign = inBytes(getInt(Tok));
255 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
264 AlignTypeEnum AlignType;
267 case 'i': AlignType = INTEGER_ALIGN; break;
268 case 'v': AlignType = VECTOR_ALIGN; break;
269 case 'f': AlignType = FLOAT_ALIGN; break;
270 case 'a': AlignType = AGGREGATE_ALIGN; break;
271 case 's': AlignType = STACK_ALIGN; break;
275 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
278 Split = split(Rest, ':');
279 unsigned ABIAlign = inBytes(getInt(Tok));
281 // Preferred alignment.
282 unsigned PrefAlign = ABIAlign;
284 Split = split(Rest, ':');
285 PrefAlign = inBytes(getInt(Tok));
288 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
292 case 'n': // Native integer types.
294 unsigned Width = getInt(Tok);
295 assert(Width != 0 && "width must be non-zero");
296 LegalIntWidths.push_back(Width);
299 Split = split(Rest, ':');
302 case 'S': { // Stack natural alignment.
303 StackNaturalAlign = inBytes(getInt(Tok));
307 llvm_unreachable("Unknown specifier in datalayout string");
315 /// @note This has to exist, because this is a pass, but it should never be
317 DataLayout::DataLayout() : ImmutablePass(ID) {
318 report_fatal_error("Bad DataLayout ctor used. "
319 "Tool did not specify a DataLayout to use?");
322 DataLayout::DataLayout(const Module *M)
323 : ImmutablePass(ID) {
324 init(M->getDataLayout());
328 DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
329 unsigned pref_align, uint32_t bit_width) {
330 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
331 assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield");
332 assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield");
333 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
334 if (Alignments[i].AlignType == (unsigned)align_type &&
335 Alignments[i].TypeBitWidth == bit_width) {
336 // Update the abi, preferred alignments.
337 Alignments[i].ABIAlign = abi_align;
338 Alignments[i].PrefAlign = pref_align;
343 Alignments.push_back(LayoutAlignElem::get(align_type, abi_align,
344 pref_align, bit_width));
347 void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
349 uint32_t TypeByteWidth) {
350 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
351 DenseMap<unsigned,PointerAlignElem>::iterator val = Pointers.find(AddrSpace);
352 if (val == Pointers.end()) {
353 Pointers[AddrSpace] =
354 PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign, TypeByteWidth);
356 val->second.ABIAlign = ABIAlign;
357 val->second.PrefAlign = PrefAlign;
358 val->second.TypeByteWidth = TypeByteWidth;
362 /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
363 /// preferred if ABIInfo = false) the layout wants for the specified datatype.
364 unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
365 uint32_t BitWidth, bool ABIInfo,
367 // Check to see if we have an exact match and remember the best match we see.
368 int BestMatchIdx = -1;
370 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
371 if (Alignments[i].AlignType == (unsigned)AlignType &&
372 Alignments[i].TypeBitWidth == BitWidth)
373 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
375 // The best match so far depends on what we're looking for.
376 if (AlignType == INTEGER_ALIGN &&
377 Alignments[i].AlignType == INTEGER_ALIGN) {
378 // The "best match" for integers is the smallest size that is larger than
379 // the BitWidth requested.
380 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
381 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
383 // However, if there isn't one that's larger, then we must use the
384 // largest one we have (see below)
385 if (LargestInt == -1 ||
386 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
391 // Okay, we didn't find an exact solution. Fall back here depending on what
392 // is being looked for.
393 if (BestMatchIdx == -1) {
394 // If we didn't find an integer alignment, fall back on most conservative.
395 if (AlignType == INTEGER_ALIGN) {
396 BestMatchIdx = LargestInt;
398 assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
400 // By default, use natural alignment for vector types. This is consistent
401 // with what clang and llvm-gcc do.
402 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
403 Align *= cast<VectorType>(Ty)->getNumElements();
404 // If the alignment is not a power of 2, round up to the next power of 2.
405 // This happens for non-power-of-2 length vectors.
406 if (Align & (Align-1))
407 Align = NextPowerOf2(Align);
412 // Since we got a "best match" index, just return it.
413 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
414 : Alignments[BestMatchIdx].PrefAlign;
419 class StructLayoutMap {
420 typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
421 LayoutInfoTy LayoutInfo;
424 virtual ~StructLayoutMap() {
425 // Remove any layouts.
426 for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end();
428 StructLayout *Value = I->second;
429 Value->~StructLayout();
434 StructLayout *&operator[](StructType *STy) {
435 return LayoutInfo[STy];
439 virtual void dump() const {}
442 } // end anonymous namespace
444 DataLayout::~DataLayout() {
445 delete static_cast<StructLayoutMap*>(LayoutMap);
448 bool DataLayout::doFinalization(Module &M) {
449 delete static_cast<StructLayoutMap*>(LayoutMap);
454 const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
456 LayoutMap = new StructLayoutMap();
458 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
459 StructLayout *&SL = (*STM)[Ty];
462 // Otherwise, create the struct layout. Because it is variable length, we
463 // malloc it, then use placement new.
464 int NumElts = Ty->getNumElements();
466 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
468 // Set SL before calling StructLayout's ctor. The ctor could cause other
469 // entries to be added to TheMap, invalidating our reference.
472 new (L) StructLayout(Ty, *this);
477 std::string DataLayout::getStringRepresentation() const {
479 raw_string_ostream OS(Result);
481 OS << (LittleEndian ? "e" : "E");
482 SmallVector<unsigned, 8> addrSpaces;
483 // Lets get all of the known address spaces and sort them
484 // into increasing order so that we can emit the string
485 // in a cleaner format.
486 for (DenseMap<unsigned, PointerAlignElem>::const_iterator
487 pib = Pointers.begin(), pie = Pointers.end();
489 addrSpaces.push_back(pib->first);
491 std::sort(addrSpaces.begin(), addrSpaces.end());
492 for (SmallVectorImpl<unsigned>::iterator asb = addrSpaces.begin(),
493 ase = addrSpaces.end(); asb != ase; ++asb) {
494 const PointerAlignElem &PI = Pointers.find(*asb)->second;
497 if (PI.AddressSpace == 0 && PI.ABIAlign == 8 && PI.PrefAlign == 8 &&
498 PI.TypeByteWidth == 8)
502 if (PI.AddressSpace) {
503 OS << PI.AddressSpace;
505 OS << ":" << PI.TypeByteWidth*8 << ':' << PI.ABIAlign*8;
506 if (PI.PrefAlign != PI.ABIAlign)
507 OS << ':' << PI.PrefAlign*8;
510 const LayoutAlignElem *DefaultStart = DefaultAlignments;
511 const LayoutAlignElem *DefaultEnd =
512 DefaultStart + array_lengthof(DefaultAlignments);
513 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
514 const LayoutAlignElem &AI = Alignments[i];
515 if (std::find(DefaultStart, DefaultEnd, AI) != DefaultEnd)
517 OS << '-' << (char)AI.AlignType;
519 OS << AI.TypeBitWidth;
520 OS << ':' << AI.ABIAlign*8;
521 if (AI.ABIAlign != AI.PrefAlign)
522 OS << ':' << AI.PrefAlign*8;
525 if (!LegalIntWidths.empty()) {
526 OS << "-n" << (unsigned)LegalIntWidths[0];
528 for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
529 OS << ':' << (unsigned)LegalIntWidths[i];
532 if (StackNaturalAlign)
533 OS << "-S" << StackNaturalAlign*8;
538 unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
539 assert(Ty->isPtrOrPtrVectorTy() &&
540 "This should only be called with a pointer or pointer vector type");
542 if (Ty->isPointerTy())
543 return getTypeSizeInBits(Ty);
545 return getTypeSizeInBits(Ty->getScalarType());
549 \param abi_or_pref Flag that determines which alignment is returned. true
550 returns the ABI alignment, false returns the preferred alignment.
551 \param Ty The underlying type for which alignment is determined.
553 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
554 == false) for the requested type \a Ty.
556 unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
559 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
560 switch (Ty->getTypeID()) {
561 // Early escape for the non-numeric types.
562 case Type::LabelTyID:
564 ? getPointerABIAlignment(0)
565 : getPointerPrefAlignment(0));
566 case Type::PointerTyID: {
567 unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace();
569 ? getPointerABIAlignment(AS)
570 : getPointerPrefAlignment(AS));
572 case Type::ArrayTyID:
573 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
575 case Type::StructTyID: {
576 // Packed structure types always have an ABI alignment of one.
577 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
580 // Get the layout annotation... which is lazily created on demand.
581 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
582 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
583 return std::max(Align, Layout->getAlignment());
585 case Type::IntegerTyID:
586 AlignType = INTEGER_ALIGN;
589 case Type::FloatTyID:
590 case Type::DoubleTyID:
591 // PPC_FP128TyID and FP128TyID have different data contents, but the
592 // same size and alignment, so they look the same here.
593 case Type::PPC_FP128TyID:
594 case Type::FP128TyID:
595 case Type::X86_FP80TyID:
596 AlignType = FLOAT_ALIGN;
598 case Type::X86_MMXTyID:
599 case Type::VectorTyID:
600 AlignType = VECTOR_ALIGN;
603 llvm_unreachable("Bad type for getAlignment!!!");
606 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
610 unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
611 return getAlignment(Ty, true);
614 /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
615 /// an integer type of the specified bitwidth.
616 unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
617 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0);
620 unsigned DataLayout::getCallFrameTypeAlignment(Type *Ty) const {
621 for (unsigned i = 0, e = Alignments.size(); i != e; ++i)
622 if (Alignments[i].AlignType == STACK_ALIGN)
623 return Alignments[i].ABIAlign;
625 return getABITypeAlignment(Ty);
628 unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
629 return getAlignment(Ty, false);
632 unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
633 unsigned Align = getPrefTypeAlignment(Ty);
634 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
635 return Log2_32(Align);
638 IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
639 unsigned AddressSpace) const {
640 return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
643 Type *DataLayout::getIntPtrType(Type *Ty) const {
644 assert(Ty->isPtrOrPtrVectorTy() &&
645 "Expected a pointer or pointer vector type.");
646 unsigned NumBits = getTypeSizeInBits(Ty->getScalarType());
647 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
648 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
649 return VectorType::get(IntTy, VecTy->getNumElements());
653 Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
654 for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i)
655 if (Width <= LegalIntWidths[i])
656 return Type::getIntNTy(C, LegalIntWidths[i]);
660 unsigned DataLayout::getLargestLegalIntTypeSize() const {
661 unsigned MaxWidth = 0;
662 for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i)
663 MaxWidth = std::max<unsigned>(MaxWidth, LegalIntWidths[i]);
667 uint64_t DataLayout::getIndexedOffset(Type *ptrTy,
668 ArrayRef<Value *> Indices) const {
670 assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
673 generic_gep_type_iterator<Value* const*>
674 TI = gep_type_begin(ptrTy, Indices);
675 for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
677 if (StructType *STy = dyn_cast<StructType>(*TI)) {
678 assert(Indices[CurIDX]->getType() ==
679 Type::getInt32Ty(ptrTy->getContext()) &&
680 "Illegal struct idx");
681 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
683 // Get structure layout information...
684 const StructLayout *Layout = getStructLayout(STy);
686 // Add in the offset, as calculated by the structure layout info...
687 Result += Layout->getElementOffset(FieldNo);
689 // Update Ty to refer to current element
690 Ty = STy->getElementType(FieldNo);
692 // Update Ty to refer to current element
693 Ty = cast<SequentialType>(Ty)->getElementType();
695 // Get the array index and the size of each array element.
696 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
697 Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
704 /// getPreferredAlignment - Return the preferred alignment of the specified
705 /// global. This includes an explicitly requested alignment (if the global
707 unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
708 Type *ElemType = GV->getType()->getElementType();
709 unsigned Alignment = getPrefTypeAlignment(ElemType);
710 unsigned GVAlignment = GV->getAlignment();
711 if (GVAlignment >= Alignment) {
712 Alignment = GVAlignment;
713 } else if (GVAlignment != 0) {
714 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
717 if (GV->hasInitializer() && GVAlignment == 0) {
718 if (Alignment < 16) {
719 // If the global is not external, see if it is large. If so, give it a
721 if (getTypeSizeInBits(ElemType) > 128)
722 Alignment = 16; // 16-byte alignment.
728 /// getPreferredAlignmentLog - Return the preferred alignment of the
729 /// specified global, returned in log form. This includes an explicitly
730 /// requested alignment (if the global has one).
731 unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
732 return Log2_32(getPreferredAlignment(GV));