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/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/GetElementPtrTypeIterator.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/IR/Value.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/MathExtras.h"
42 //===----------------------------------------------------------------------===//
43 // Support for StructLayout
44 //===----------------------------------------------------------------------===//
46 StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
47 assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
51 NumElements = ST->getNumElements();
53 // Loop over each of the elements, placing them in memory.
54 for (unsigned i = 0, e = NumElements; i != e; ++i) {
55 Type *Ty = ST->getElementType(i);
56 unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
58 // Add padding if necessary to align the data element properly.
59 if ((StructSize & (TyAlign-1)) != 0) {
61 StructSize = alignTo(StructSize, TyAlign);
64 // Keep track of maximum alignment constraint.
65 StructAlignment = std::max(TyAlign, StructAlignment);
67 MemberOffsets[i] = StructSize;
68 StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
71 // Empty structures have alignment of 1 byte.
72 if (StructAlignment == 0) StructAlignment = 1;
74 // Add padding to the end of the struct so that it could be put in an array
75 // and all array elements would be aligned correctly.
76 if ((StructSize & (StructAlignment-1)) != 0) {
78 StructSize = alignTo(StructSize, StructAlignment);
82 /// getElementContainingOffset - Given a valid offset into the structure,
83 /// return the structure index that contains it.
84 unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
86 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
87 assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
89 assert(*SI <= Offset && "upper_bound didn't work");
90 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
91 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
92 "Upper bound didn't work!");
94 // Multiple fields can have the same offset if any of them are zero sized.
95 // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
96 // at the i32 element, because it is the last element at that offset. This is
97 // the right one to return, because anything after it will have a higher
98 // offset, implying that this element is non-empty.
99 return SI-&MemberOffsets[0];
102 //===----------------------------------------------------------------------===//
103 // LayoutAlignElem, LayoutAlign support
104 //===----------------------------------------------------------------------===//
107 LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
108 unsigned pref_align, uint32_t bit_width) {
109 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
110 LayoutAlignElem retval;
111 retval.AlignType = align_type;
112 retval.ABIAlign = abi_align;
113 retval.PrefAlign = pref_align;
114 retval.TypeBitWidth = bit_width;
119 LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
120 return (AlignType == rhs.AlignType
121 && ABIAlign == rhs.ABIAlign
122 && PrefAlign == rhs.PrefAlign
123 && TypeBitWidth == rhs.TypeBitWidth);
126 //===----------------------------------------------------------------------===//
127 // PointerAlignElem, PointerAlign support
128 //===----------------------------------------------------------------------===//
131 PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
132 unsigned PrefAlign, uint32_t TypeByteWidth) {
133 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
134 PointerAlignElem retval;
135 retval.AddressSpace = AddressSpace;
136 retval.ABIAlign = ABIAlign;
137 retval.PrefAlign = PrefAlign;
138 retval.TypeByteWidth = TypeByteWidth;
143 PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
144 return (ABIAlign == rhs.ABIAlign
145 && AddressSpace == rhs.AddressSpace
146 && PrefAlign == rhs.PrefAlign
147 && TypeByteWidth == rhs.TypeByteWidth);
150 //===----------------------------------------------------------------------===//
151 // DataLayout Class Implementation
152 //===----------------------------------------------------------------------===//
154 const char *DataLayout::getManglingComponent(const Triple &T) {
155 if (T.isOSBinFormatMachO())
157 if (T.isOSWindows() && T.isOSBinFormatCOFF())
158 return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
162 static const LayoutAlignElem DefaultAlignments[] = {
163 { INTEGER_ALIGN, 1, 1, 1 }, // i1
164 { INTEGER_ALIGN, 8, 1, 1 }, // i8
165 { INTEGER_ALIGN, 16, 2, 2 }, // i16
166 { INTEGER_ALIGN, 32, 4, 4 }, // i32
167 { INTEGER_ALIGN, 64, 4, 8 }, // i64
168 { FLOAT_ALIGN, 16, 2, 2 }, // half
169 { FLOAT_ALIGN, 32, 4, 4 }, // float
170 { FLOAT_ALIGN, 64, 8, 8 }, // double
171 { FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ...
172 { VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ...
173 { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
174 { AGGREGATE_ALIGN, 0, 0, 8 } // struct
177 void DataLayout::reset(StringRef Desc) {
183 StackNaturalAlign = 0;
184 ManglingMode = MM_None;
185 NonIntegralAddressSpaces.clear();
187 // Default alignments
188 for (const LayoutAlignElem &E : DefaultAlignments) {
189 setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
192 setPointerAlignment(0, 8, 8, 8);
194 parseSpecifier(Desc);
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");
208 /// Get an unsigned integer, including error checks.
209 static unsigned getInt(StringRef R) {
211 bool error = R.getAsInteger(10, Result); (void)error;
213 report_fatal_error("not a number, or does not fit in an unsigned int");
217 /// Convert bits into bytes. Assert if not a byte width multiple.
218 static unsigned inBytes(unsigned Bits) {
220 report_fatal_error("number of bits must be a byte width multiple");
224 void DataLayout::parseSpecifier(StringRef Desc) {
225 StringRepresentation = Desc;
226 while (!Desc.empty()) {
228 std::pair<StringRef, StringRef> Split = split(Desc, '-');
232 Split = split(Split.first, ':');
234 // Aliases used below.
235 StringRef &Tok = Split.first; // Current token.
236 StringRef &Rest = Split.second; // The rest of the string.
240 Split = split(Rest, ':');
242 unsigned AS = getInt(Split.first);
244 report_fatal_error("Address space 0 can never be non-integral");
245 NonIntegralAddressSpaces.push_back(AS);
246 } while (!Rest.empty());
251 char Specifier = Tok.front();
256 // Ignored for backward compatibility.
257 // FIXME: remove this on LLVM 4.0.
267 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
268 if (!isUInt<24>(AddrSpace))
269 report_fatal_error("Invalid address space, must be a 24bit integer");
274 "Missing size specification for pointer in datalayout string");
275 Split = split(Rest, ':');
276 unsigned PointerMemSize = inBytes(getInt(Tok));
278 report_fatal_error("Invalid pointer size of 0 bytes");
283 "Missing alignment specification for pointer in datalayout string");
284 Split = split(Rest, ':');
285 unsigned PointerABIAlign = inBytes(getInt(Tok));
286 if (!isPowerOf2_64(PointerABIAlign))
288 "Pointer ABI alignment must be a power of 2");
290 // Preferred alignment.
291 unsigned PointerPrefAlign = PointerABIAlign;
293 Split = split(Rest, ':');
294 PointerPrefAlign = inBytes(getInt(Tok));
295 if (!isPowerOf2_64(PointerPrefAlign))
297 "Pointer preferred alignment must be a power of 2");
300 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
308 AlignTypeEnum AlignType;
310 default: llvm_unreachable("Unexpected specifier!");
311 case 'i': AlignType = INTEGER_ALIGN; break;
312 case 'v': AlignType = VECTOR_ALIGN; break;
313 case 'f': AlignType = FLOAT_ALIGN; break;
314 case 'a': AlignType = AGGREGATE_ALIGN; break;
318 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
320 if (AlignType == AGGREGATE_ALIGN && Size != 0)
322 "Sized aggregate specification in datalayout string");
327 "Missing alignment specification in datalayout string");
328 Split = split(Rest, ':');
329 unsigned ABIAlign = inBytes(getInt(Tok));
330 if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
332 "ABI alignment specification must be >0 for non-aggregate types");
334 // Preferred alignment.
335 unsigned PrefAlign = ABIAlign;
337 Split = split(Rest, ':');
338 PrefAlign = inBytes(getInt(Tok));
341 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
345 case 'n': // Native integer types.
347 unsigned Width = getInt(Tok);
350 "Zero width native integer type in datalayout string");
351 LegalIntWidths.push_back(Width);
354 Split = split(Rest, ':');
357 case 'S': { // Stack natural alignment.
358 StackNaturalAlign = inBytes(getInt(Tok));
361 case 'A': { // Default stack/alloca address space.
362 AllocaAddrSpace = getInt(Tok);
363 if (!isUInt<24>(AllocaAddrSpace))
364 report_fatal_error("Invalid address space, must be a 24bit integer");
369 report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
371 report_fatal_error("Expected mangling specifier in datalayout string");
373 report_fatal_error("Unknown mangling specifier in datalayout string");
376 report_fatal_error("Unknown mangling in datalayout string");
378 ManglingMode = MM_ELF;
381 ManglingMode = MM_MachO;
384 ManglingMode = MM_Mips;
387 ManglingMode = MM_WinCOFF;
390 ManglingMode = MM_WinCOFFX86;
395 report_fatal_error("Unknown specifier in datalayout string");
401 DataLayout::DataLayout(const Module *M) {
405 void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
407 bool DataLayout::operator==(const DataLayout &Other) const {
408 bool Ret = BigEndian == Other.BigEndian &&
409 AllocaAddrSpace == Other.AllocaAddrSpace &&
410 StackNaturalAlign == Other.StackNaturalAlign &&
411 ManglingMode == Other.ManglingMode &&
412 LegalIntWidths == Other.LegalIntWidths &&
413 Alignments == Other.Alignments && Pointers == Other.Pointers;
414 // Note: getStringRepresentation() might differs, it is not canonicalized
418 DataLayout::AlignmentsTy::iterator
419 DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
421 auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
422 return std::lower_bound(Alignments.begin(), Alignments.end(), Pair,
423 [](const LayoutAlignElem &LHS,
424 const std::pair<unsigned, uint32_t> &RHS) {
425 return std::tie(LHS.AlignType, LHS.TypeBitWidth) <
426 std::tie(RHS.first, RHS.second);
431 DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
432 unsigned pref_align, uint32_t bit_width) {
433 if (!isUInt<24>(bit_width))
434 report_fatal_error("Invalid bit width, must be a 24bit integer");
435 if (!isUInt<16>(abi_align))
436 report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
437 if (!isUInt<16>(pref_align))
438 report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
439 if (abi_align != 0 && !isPowerOf2_64(abi_align))
440 report_fatal_error("Invalid ABI alignment, must be a power of 2");
441 if (pref_align != 0 && !isPowerOf2_64(pref_align))
442 report_fatal_error("Invalid preferred alignment, must be a power of 2");
444 if (pref_align < abi_align)
446 "Preferred alignment cannot be less than the ABI alignment");
448 AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width);
449 if (I != Alignments.end() &&
450 I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) {
451 // Update the abi, preferred alignments.
452 I->ABIAlign = abi_align;
453 I->PrefAlign = pref_align;
455 // Insert before I to keep the vector sorted.
456 Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align,
457 pref_align, bit_width));
461 DataLayout::PointersTy::iterator
462 DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
463 return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
464 [](const PointerAlignElem &A, uint32_t AddressSpace) {
465 return A.AddressSpace < AddressSpace;
469 void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
471 uint32_t TypeByteWidth) {
472 if (PrefAlign < ABIAlign)
474 "Preferred alignment cannot be less than the ABI alignment");
476 PointersTy::iterator I = findPointerLowerBound(AddrSpace);
477 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
478 Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
481 I->ABIAlign = ABIAlign;
482 I->PrefAlign = PrefAlign;
483 I->TypeByteWidth = TypeByteWidth;
487 /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
488 /// preferred if ABIInfo = false) the layout wants for the specified datatype.
489 unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
490 uint32_t BitWidth, bool ABIInfo,
492 AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth);
493 // See if we found an exact match. Of if we are looking for an integer type,
494 // but don't have an exact match take the next largest integer. This is where
495 // the lower_bound will point to when it fails an exact match.
496 if (I != Alignments.end() && I->AlignType == (unsigned)AlignType &&
497 (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN))
498 return ABIInfo ? I->ABIAlign : I->PrefAlign;
500 if (AlignType == INTEGER_ALIGN) {
501 // If we didn't have a larger value try the largest value we have.
502 if (I != Alignments.begin()) {
503 --I; // Go to the previous entry and see if its an integer.
504 if (I->AlignType == INTEGER_ALIGN)
505 return ABIInfo ? I->ABIAlign : I->PrefAlign;
507 } else if (AlignType == VECTOR_ALIGN) {
508 // By default, use natural alignment for vector types. This is consistent
509 // with what clang and llvm-gcc do.
510 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
511 Align *= cast<VectorType>(Ty)->getNumElements();
512 Align = PowerOf2Ceil(Align);
516 // If we still couldn't find a reasonable default alignment, fall back
517 // to a simple heuristic that the alignment is the first power of two
518 // greater-or-equal to the store size of the type. This is a reasonable
519 // approximation of reality, and if the user wanted something less
520 // less conservative, they should have specified it explicitly in the data
522 unsigned Align = getTypeStoreSize(Ty);
523 Align = PowerOf2Ceil(Align);
529 class StructLayoutMap {
530 using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
531 LayoutInfoTy LayoutInfo;
535 // Remove any layouts.
536 for (const auto &I : LayoutInfo) {
537 StructLayout *Value = I.second;
538 Value->~StructLayout();
543 StructLayout *&operator[](StructType *STy) {
544 return LayoutInfo[STy];
548 } // end anonymous namespace
550 void DataLayout::clear() {
551 LegalIntWidths.clear();
554 delete static_cast<StructLayoutMap *>(LayoutMap);
558 DataLayout::~DataLayout() {
562 const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
564 LayoutMap = new StructLayoutMap();
566 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
567 StructLayout *&SL = (*STM)[Ty];
570 // Otherwise, create the struct layout. Because it is variable length, we
571 // malloc it, then use placement new.
572 int NumElts = Ty->getNumElements();
574 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
576 report_bad_alloc_error("Allocation of StructLayout elements failed.");
578 // Set SL before calling StructLayout's ctor. The ctor could cause other
579 // entries to be added to TheMap, invalidating our reference.
582 new (L) StructLayout(Ty, *this);
587 unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
588 PointersTy::const_iterator I = findPointerLowerBound(AS);
589 if (I == Pointers.end() || I->AddressSpace != AS) {
590 I = findPointerLowerBound(0);
591 assert(I->AddressSpace == 0);
596 unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
597 PointersTy::const_iterator I = findPointerLowerBound(AS);
598 if (I == Pointers.end() || I->AddressSpace != AS) {
599 I = findPointerLowerBound(0);
600 assert(I->AddressSpace == 0);
605 unsigned DataLayout::getPointerSize(unsigned AS) const {
606 PointersTy::const_iterator I = findPointerLowerBound(AS);
607 if (I == Pointers.end() || I->AddressSpace != AS) {
608 I = findPointerLowerBound(0);
609 assert(I->AddressSpace == 0);
611 return I->TypeByteWidth;
614 unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
615 assert(Ty->isPtrOrPtrVectorTy() &&
616 "This should only be called with a pointer or pointer vector type");
617 Ty = Ty->getScalarType();
618 return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
622 \param abi_or_pref Flag that determines which alignment is returned. true
623 returns the ABI alignment, false returns the preferred alignment.
624 \param Ty The underlying type for which alignment is determined.
626 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
627 == false) for the requested type \a Ty.
629 unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
630 AlignTypeEnum AlignType;
632 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
633 switch (Ty->getTypeID()) {
634 // Early escape for the non-numeric types.
635 case Type::LabelTyID:
637 ? getPointerABIAlignment(0)
638 : getPointerPrefAlignment(0));
639 case Type::PointerTyID: {
640 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
642 ? getPointerABIAlignment(AS)
643 : getPointerPrefAlignment(AS));
645 case Type::ArrayTyID:
646 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
648 case Type::StructTyID: {
649 // Packed structure types always have an ABI alignment of one.
650 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
653 // Get the layout annotation... which is lazily created on demand.
654 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
655 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
656 return std::max(Align, Layout->getAlignment());
658 case Type::IntegerTyID:
659 AlignType = INTEGER_ALIGN;
662 case Type::FloatTyID:
663 case Type::DoubleTyID:
664 // PPC_FP128TyID and FP128TyID have different data contents, but the
665 // same size and alignment, so they look the same here.
666 case Type::PPC_FP128TyID:
667 case Type::FP128TyID:
668 case Type::X86_FP80TyID:
669 AlignType = FLOAT_ALIGN;
671 case Type::X86_MMXTyID:
672 case Type::VectorTyID:
673 AlignType = VECTOR_ALIGN;
676 llvm_unreachable("Bad type for getAlignment!!!");
679 return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty), abi_or_pref, Ty);
682 unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
683 return getAlignment(Ty, true);
686 /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
687 /// an integer type of the specified bitwidth.
688 unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
689 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
692 unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
693 return getAlignment(Ty, false);
696 unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
697 unsigned Align = getPrefTypeAlignment(Ty);
698 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
699 return Log2_32(Align);
702 IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
703 unsigned AddressSpace) const {
704 return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
707 Type *DataLayout::getIntPtrType(Type *Ty) const {
708 assert(Ty->isPtrOrPtrVectorTy() &&
709 "Expected a pointer or pointer vector type.");
710 unsigned NumBits = getPointerTypeSizeInBits(Ty);
711 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
712 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
713 return VectorType::get(IntTy, VecTy->getNumElements());
717 Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
718 for (unsigned LegalIntWidth : LegalIntWidths)
719 if (Width <= LegalIntWidth)
720 return Type::getIntNTy(C, LegalIntWidth);
724 unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
725 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
726 return Max != LegalIntWidths.end() ? *Max : 0;
729 int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
730 ArrayRef<Value *> Indices) const {
733 generic_gep_type_iterator<Value* const*>
734 GTI = gep_type_begin(ElemTy, Indices),
735 GTE = gep_type_end(ElemTy, Indices);
736 for (; GTI != GTE; ++GTI) {
737 Value *Idx = GTI.getOperand();
738 if (StructType *STy = GTI.getStructTypeOrNull()) {
739 assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
740 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
742 // Get structure layout information...
743 const StructLayout *Layout = getStructLayout(STy);
745 // Add in the offset, as calculated by the structure layout info...
746 Result += Layout->getElementOffset(FieldNo);
748 // Get the array index and the size of each array element.
749 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
750 Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
757 /// getPreferredAlignment - Return the preferred alignment of the specified
758 /// global. This includes an explicitly requested alignment (if the global
760 unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
761 Type *ElemType = GV->getValueType();
762 unsigned Alignment = getPrefTypeAlignment(ElemType);
763 unsigned GVAlignment = GV->getAlignment();
764 if (GVAlignment >= Alignment) {
765 Alignment = GVAlignment;
766 } else if (GVAlignment != 0) {
767 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
770 if (GV->hasInitializer() && GVAlignment == 0) {
771 if (Alignment < 16) {
772 // If the global is not external, see if it is large. If so, give it a
774 if (getTypeSizeInBits(ElemType) > 128)
775 Alignment = 16; // 16-byte alignment.
781 /// getPreferredAlignmentLog - Return the preferred alignment of the
782 /// specified global, returned in log form. This includes an explicitly
783 /// requested alignment (if the global has one).
784 unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
785 return Log2_32(getPreferredAlignment(GV));