1 //===-- TargetData.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 target 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/Target/TargetData.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Module.h"
23 #include "llvm/Support/GetElementPtrTypeIterator.h"
24 #include "llvm/Support/MathExtras.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/Mutex.h"
29 #include "llvm/ADT/DenseMap.h"
34 // Handle the Pass registration stuff necessary to use TargetData's.
36 // Register the default SparcV9 implementation...
37 INITIALIZE_PASS(TargetData, "targetdata", "Target Data Layout", false, true)
38 char TargetData::ID = 0;
40 //===----------------------------------------------------------------------===//
41 // Support for StructLayout
42 //===----------------------------------------------------------------------===//
44 StructLayout::StructLayout(StructType *ST, const TargetData &TD) {
45 assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
48 NumElements = ST->getNumElements();
50 // Loop over each of the elements, placing them in memory.
51 for (unsigned i = 0, e = NumElements; i != e; ++i) {
52 Type *Ty = ST->getElementType(i);
53 unsigned TyAlign = ST->isPacked() ? 1 : TD.getABITypeAlignment(Ty);
55 // Add padding if necessary to align the data element properly.
56 if ((StructSize & (TyAlign-1)) != 0)
57 StructSize = TargetData::RoundUpAlignment(StructSize, TyAlign);
59 // Keep track of maximum alignment constraint.
60 StructAlignment = std::max(TyAlign, StructAlignment);
62 MemberOffsets[i] = StructSize;
63 StructSize += TD.getTypeAllocSize(Ty); // Consume space for this data item
66 // Empty structures have alignment of 1 byte.
67 if (StructAlignment == 0) StructAlignment = 1;
69 // Add padding to the end of the struct so that it could be put in an array
70 // and all array elements would be aligned correctly.
71 if ((StructSize & (StructAlignment-1)) != 0)
72 StructSize = TargetData::RoundUpAlignment(StructSize, StructAlignment);
76 /// getElementContainingOffset - Given a valid offset into the structure,
77 /// return the structure index that contains it.
78 unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
80 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
81 assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
83 assert(*SI <= Offset && "upper_bound didn't work");
84 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
85 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
86 "Upper bound didn't work!");
88 // Multiple fields can have the same offset if any of them are zero sized.
89 // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
90 // at the i32 element, because it is the last element at that offset. This is
91 // the right one to return, because anything after it will have a higher
92 // offset, implying that this element is non-empty.
93 return SI-&MemberOffsets[0];
96 //===----------------------------------------------------------------------===//
97 // TargetAlignElem, TargetAlign support
98 //===----------------------------------------------------------------------===//
101 TargetAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
102 unsigned pref_align, uint32_t bit_width) {
103 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
104 TargetAlignElem retval;
105 retval.AlignType = align_type;
106 retval.ABIAlign = abi_align;
107 retval.PrefAlign = pref_align;
108 retval.TypeBitWidth = bit_width;
113 TargetAlignElem::operator==(const TargetAlignElem &rhs) const {
114 return (AlignType == rhs.AlignType
115 && ABIAlign == rhs.ABIAlign
116 && PrefAlign == rhs.PrefAlign
117 && TypeBitWidth == rhs.TypeBitWidth);
120 const TargetAlignElem
121 TargetData::InvalidAlignmentElem = { (AlignTypeEnum)0xFF, 0, 0, 0 };
123 //===----------------------------------------------------------------------===//
124 // TargetData Class Implementation
125 //===----------------------------------------------------------------------===//
127 /// getInt - Get an integer ignoring errors.
128 static int getInt(StringRef R) {
130 R.getAsInteger(10, Result);
134 void TargetData::init() {
135 initializeTargetDataPass(*PassRegistry::getPassRegistry());
138 LittleEndian = false;
141 PointerPrefAlign = PointerABIAlign;
142 StackNaturalAlign = 0;
144 // Default alignments
145 setAlignment(INTEGER_ALIGN, 1, 1, 1); // i1
146 setAlignment(INTEGER_ALIGN, 1, 1, 8); // i8
147 setAlignment(INTEGER_ALIGN, 2, 2, 16); // i16
148 setAlignment(INTEGER_ALIGN, 4, 4, 32); // i32
149 setAlignment(INTEGER_ALIGN, 4, 8, 64); // i64
150 setAlignment(FLOAT_ALIGN, 2, 2, 16); // half
151 setAlignment(FLOAT_ALIGN, 4, 4, 32); // float
152 setAlignment(FLOAT_ALIGN, 8, 8, 64); // double
153 setAlignment(FLOAT_ALIGN, 16, 16, 128); // ppcf128, quad, ...
154 setAlignment(VECTOR_ALIGN, 8, 8, 64); // v2i32, v1i64, ...
155 setAlignment(VECTOR_ALIGN, 16, 16, 128); // v16i8, v8i16, v4i32, ...
156 setAlignment(AGGREGATE_ALIGN, 0, 8, 0); // struct
159 std::string TargetData::parseSpecifier(StringRef Desc, TargetData *td) {
164 while (!Desc.empty()) {
165 std::pair<StringRef, StringRef> Split = Desc.split('-');
166 StringRef Token = Split.first;
172 Split = Token.split(':');
173 StringRef Specifier = Split.first;
174 Token = Split.second;
176 assert(!Specifier.empty() && "Can't be empty here");
178 switch (Specifier[0]) {
181 td->LittleEndian = false;
185 td->LittleEndian = true;
189 Split = Token.split(':');
190 int PointerMemSizeBits = getInt(Split.first);
191 if (PointerMemSizeBits < 0 || PointerMemSizeBits % 8 != 0)
192 return "invalid pointer size, must be a positive 8-bit multiple";
194 td->PointerMemSize = PointerMemSizeBits / 8;
196 // Pointer ABI alignment.
197 Split = Split.second.split(':');
198 int PointerABIAlignBits = getInt(Split.first);
199 if (PointerABIAlignBits < 0 || PointerABIAlignBits % 8 != 0) {
200 return "invalid pointer ABI alignment, "
201 "must be a positive 8-bit multiple";
204 td->PointerABIAlign = PointerABIAlignBits / 8;
206 // Pointer preferred alignment.
207 Split = Split.second.split(':');
208 int PointerPrefAlignBits = getInt(Split.first);
209 if (PointerPrefAlignBits < 0 || PointerPrefAlignBits % 8 != 0) {
210 return "invalid pointer preferred alignment, "
211 "must be a positive 8-bit multiple";
214 td->PointerPrefAlign = PointerPrefAlignBits / 8;
215 if (td->PointerPrefAlign == 0)
216 td->PointerPrefAlign = td->PointerABIAlign;
225 AlignTypeEnum AlignType;
226 char field = Specifier[0];
229 case 'i': AlignType = INTEGER_ALIGN; break;
230 case 'v': AlignType = VECTOR_ALIGN; break;
231 case 'f': AlignType = FLOAT_ALIGN; break;
232 case 'a': AlignType = AGGREGATE_ALIGN; break;
233 case 's': AlignType = STACK_ALIGN; break;
235 int Size = getInt(Specifier.substr(1));
237 return std::string("invalid ") + field + "-size field, "
241 Split = Token.split(':');
242 int ABIAlignBits = getInt(Split.first);
243 if (ABIAlignBits < 0 || ABIAlignBits % 8 != 0) {
244 return std::string("invalid ") + field +"-abi-alignment field, "
245 "must be a positive 8-bit multiple";
247 unsigned ABIAlign = ABIAlignBits / 8;
249 Split = Split.second.split(':');
251 int PrefAlignBits = getInt(Split.first);
252 if (PrefAlignBits < 0 || PrefAlignBits % 8 != 0) {
253 return std::string("invalid ") + field +"-preferred-alignment field, "
254 "must be a positive 8-bit multiple";
256 unsigned PrefAlign = PrefAlignBits / 8;
258 PrefAlign = ABIAlign;
261 td->setAlignment(AlignType, ABIAlign, PrefAlign, Size);
264 case 'n': // Native integer types.
265 Specifier = Specifier.substr(1);
267 int Width = getInt(Specifier);
269 return std::string("invalid native integer size \'") + Specifier.str() +
270 "\', must be a positive integer.";
272 if (td && Width != 0)
273 td->LegalIntWidths.push_back(Width);
274 Split = Token.split(':');
275 Specifier = Split.first;
276 Token = Split.second;
277 } while (!Specifier.empty() || !Token.empty());
279 case 'S': { // Stack natural alignment.
280 int StackNaturalAlignBits = getInt(Specifier.substr(1));
281 if (StackNaturalAlignBits < 0 || StackNaturalAlignBits % 8 != 0) {
282 return "invalid natural stack alignment (S-field), "
283 "must be a positive 8-bit multiple";
286 td->StackNaturalAlign = StackNaturalAlignBits / 8;
299 /// @note This has to exist, because this is a pass, but it should never be
301 TargetData::TargetData() : ImmutablePass(ID) {
302 report_fatal_error("Bad TargetData ctor used. "
303 "Tool did not specify a TargetData to use?");
306 TargetData::TargetData(const Module *M)
307 : ImmutablePass(ID) {
308 std::string errMsg = parseSpecifier(M->getDataLayout(), this);
309 assert(errMsg == "" && "Module M has malformed target data layout string.");
314 TargetData::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
315 unsigned pref_align, uint32_t bit_width) {
316 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
317 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
318 if (Alignments[i].AlignType == align_type &&
319 Alignments[i].TypeBitWidth == bit_width) {
320 // Update the abi, preferred alignments.
321 Alignments[i].ABIAlign = abi_align;
322 Alignments[i].PrefAlign = pref_align;
327 Alignments.push_back(TargetAlignElem::get(align_type, abi_align,
328 pref_align, bit_width));
331 /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
332 /// preferred if ABIInfo = false) the target wants for the specified datatype.
333 unsigned TargetData::getAlignmentInfo(AlignTypeEnum AlignType,
334 uint32_t BitWidth, bool ABIInfo,
336 // Check to see if we have an exact match and remember the best match we see.
337 int BestMatchIdx = -1;
339 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
340 if (Alignments[i].AlignType == AlignType &&
341 Alignments[i].TypeBitWidth == BitWidth)
342 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
344 // The best match so far depends on what we're looking for.
345 if (AlignType == INTEGER_ALIGN &&
346 Alignments[i].AlignType == INTEGER_ALIGN) {
347 // The "best match" for integers is the smallest size that is larger than
348 // the BitWidth requested.
349 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
350 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
352 // However, if there isn't one that's larger, then we must use the
353 // largest one we have (see below)
354 if (LargestInt == -1 ||
355 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
360 // Okay, we didn't find an exact solution. Fall back here depending on what
361 // is being looked for.
362 if (BestMatchIdx == -1) {
363 // If we didn't find an integer alignment, fall back on most conservative.
364 if (AlignType == INTEGER_ALIGN) {
365 BestMatchIdx = LargestInt;
367 assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
369 // By default, use natural alignment for vector types. This is consistent
370 // with what clang and llvm-gcc do.
371 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
372 Align *= cast<VectorType>(Ty)->getNumElements();
373 // If the alignment is not a power of 2, round up to the next power of 2.
374 // This happens for non-power-of-2 length vectors.
375 if (Align & (Align-1))
376 Align = NextPowerOf2(Align);
381 // Since we got a "best match" index, just return it.
382 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
383 : Alignments[BestMatchIdx].PrefAlign;
388 class StructLayoutMap {
389 typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
390 LayoutInfoTy LayoutInfo;
393 virtual ~StructLayoutMap() {
394 // Remove any layouts.
395 for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end();
397 StructLayout *Value = I->second;
398 Value->~StructLayout();
403 StructLayout *&operator[](StructType *STy) {
404 return LayoutInfo[STy];
408 virtual void dump() const {}
411 } // end anonymous namespace
413 TargetData::~TargetData() {
414 delete static_cast<StructLayoutMap*>(LayoutMap);
417 const StructLayout *TargetData::getStructLayout(StructType *Ty) const {
419 LayoutMap = new StructLayoutMap();
421 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
422 StructLayout *&SL = (*STM)[Ty];
425 // Otherwise, create the struct layout. Because it is variable length, we
426 // malloc it, then use placement new.
427 int NumElts = Ty->getNumElements();
429 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
431 // Set SL before calling StructLayout's ctor. The ctor could cause other
432 // entries to be added to TheMap, invalidating our reference.
435 new (L) StructLayout(Ty, *this);
440 std::string TargetData::getStringRepresentation() const {
442 raw_string_ostream OS(Result);
444 OS << (LittleEndian ? "e" : "E")
445 << "-p:" << PointerMemSize*8 << ':' << PointerABIAlign*8
446 << ':' << PointerPrefAlign*8
447 << "-S" << StackNaturalAlign*8;
449 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
450 const TargetAlignElem &AI = Alignments[i];
451 OS << '-' << (char)AI.AlignType << AI.TypeBitWidth << ':'
452 << AI.ABIAlign*8 << ':' << AI.PrefAlign*8;
455 if (!LegalIntWidths.empty()) {
456 OS << "-n" << (unsigned)LegalIntWidths[0];
458 for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
459 OS << ':' << (unsigned)LegalIntWidths[i];
465 uint64_t TargetData::getTypeSizeInBits(Type *Ty) const {
466 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
467 switch (Ty->getTypeID()) {
468 case Type::LabelTyID:
469 case Type::PointerTyID:
470 return getPointerSizeInBits();
471 case Type::ArrayTyID: {
472 ArrayType *ATy = cast<ArrayType>(Ty);
473 return getTypeAllocSizeInBits(ATy->getElementType())*ATy->getNumElements();
475 case Type::StructTyID:
476 // Get the layout annotation... which is lazily created on demand.
477 return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
478 case Type::IntegerTyID:
479 return cast<IntegerType>(Ty)->getBitWidth();
484 case Type::FloatTyID:
486 case Type::DoubleTyID:
487 case Type::X86_MMXTyID:
489 case Type::PPC_FP128TyID:
490 case Type::FP128TyID:
492 // In memory objects this is always aligned to a higher boundary, but
493 // only 80 bits contain information.
494 case Type::X86_FP80TyID:
496 case Type::VectorTyID:
497 return cast<VectorType>(Ty)->getBitWidth();
499 llvm_unreachable("TargetData::getTypeSizeInBits(): Unsupported type");
504 \param abi_or_pref Flag that determines which alignment is returned. true
505 returns the ABI alignment, false returns the preferred alignment.
506 \param Ty The underlying type for which alignment is determined.
508 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
509 == false) for the requested type \a Ty.
511 unsigned TargetData::getAlignment(Type *Ty, bool abi_or_pref) const {
514 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
515 switch (Ty->getTypeID()) {
516 // Early escape for the non-numeric types.
517 case Type::LabelTyID:
518 case Type::PointerTyID:
520 ? getPointerABIAlignment()
521 : getPointerPrefAlignment());
522 case Type::ArrayTyID:
523 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
525 case Type::StructTyID: {
526 // Packed structure types always have an ABI alignment of one.
527 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
530 // Get the layout annotation... which is lazily created on demand.
531 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
532 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
533 return std::max(Align, Layout->getAlignment());
535 case Type::IntegerTyID:
537 AlignType = INTEGER_ALIGN;
540 case Type::FloatTyID:
541 case Type::DoubleTyID:
542 // PPC_FP128TyID and FP128TyID have different data contents, but the
543 // same size and alignment, so they look the same here.
544 case Type::PPC_FP128TyID:
545 case Type::FP128TyID:
546 case Type::X86_FP80TyID:
547 AlignType = FLOAT_ALIGN;
549 case Type::X86_MMXTyID:
550 case Type::VectorTyID:
551 AlignType = VECTOR_ALIGN;
554 llvm_unreachable("Bad type for getAlignment!!!");
557 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
561 unsigned TargetData::getABITypeAlignment(Type *Ty) const {
562 return getAlignment(Ty, true);
565 /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
566 /// an integer type of the specified bitwidth.
567 unsigned TargetData::getABIIntegerTypeAlignment(unsigned BitWidth) const {
568 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0);
572 unsigned TargetData::getCallFrameTypeAlignment(Type *Ty) const {
573 for (unsigned i = 0, e = Alignments.size(); i != e; ++i)
574 if (Alignments[i].AlignType == STACK_ALIGN)
575 return Alignments[i].ABIAlign;
577 return getABITypeAlignment(Ty);
580 unsigned TargetData::getPrefTypeAlignment(Type *Ty) const {
581 return getAlignment(Ty, false);
584 unsigned TargetData::getPreferredTypeAlignmentShift(Type *Ty) const {
585 unsigned Align = getPrefTypeAlignment(Ty);
586 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
587 return Log2_32(Align);
590 /// getIntPtrType - Return an unsigned integer type that is the same size or
591 /// greater to the host pointer size.
592 IntegerType *TargetData::getIntPtrType(LLVMContext &C) const {
593 return IntegerType::get(C, getPointerSizeInBits());
597 uint64_t TargetData::getIndexedOffset(Type *ptrTy,
598 ArrayRef<Value *> Indices) const {
600 assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
603 generic_gep_type_iterator<Value* const*>
604 TI = gep_type_begin(ptrTy, Indices);
605 for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
607 if (StructType *STy = dyn_cast<StructType>(*TI)) {
608 assert(Indices[CurIDX]->getType() ==
609 Type::getInt32Ty(ptrTy->getContext()) &&
610 "Illegal struct idx");
611 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
613 // Get structure layout information...
614 const StructLayout *Layout = getStructLayout(STy);
616 // Add in the offset, as calculated by the structure layout info...
617 Result += Layout->getElementOffset(FieldNo);
619 // Update Ty to refer to current element
620 Ty = STy->getElementType(FieldNo);
622 // Update Ty to refer to current element
623 Ty = cast<SequentialType>(Ty)->getElementType();
625 // Get the array index and the size of each array element.
626 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
627 Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
634 /// getPreferredAlignment - Return the preferred alignment of the specified
635 /// global. This includes an explicitly requested alignment (if the global
637 unsigned TargetData::getPreferredAlignment(const GlobalVariable *GV) const {
638 Type *ElemType = GV->getType()->getElementType();
639 unsigned Alignment = getPrefTypeAlignment(ElemType);
640 unsigned GVAlignment = GV->getAlignment();
641 if (GVAlignment >= Alignment) {
642 Alignment = GVAlignment;
643 } else if (GVAlignment != 0) {
644 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
647 if (GV->hasInitializer() && GVAlignment == 0) {
648 if (Alignment < 16) {
649 // If the global is not external, see if it is large. If so, give it a
651 if (getTypeSizeInBits(ElemType) > 128)
652 Alignment = 16; // 16-byte alignment.
658 /// getPreferredAlignmentLog - Return the preferred alignment of the
659 /// specified global, returned in log form. This includes an explicitly
660 /// requested alignment (if the global has one).
661 unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
662 return Log2_32(getPreferredAlignment(GV));