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/System/Mutex.h"
29 #include "llvm/ADT/DenseMap.h"
35 // Handle the Pass registration stuff necessary to use TargetData's.
37 // Register the default SparcV9 implementation...
38 static RegisterPass<TargetData> X("targetdata", "Target Data Layout", false,
40 char TargetData::ID = 0;
42 //===----------------------------------------------------------------------===//
43 // Support for StructLayout
44 //===----------------------------------------------------------------------===//
46 StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
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 const Type *Ty = ST->getElementType(i);
54 unsigned TyAlign = ST->isPacked() ? 1 : TD.getABITypeAlignment(Ty);
56 // Add padding if necessary to align the data element properly.
57 if ((StructSize & (TyAlign-1)) != 0)
58 StructSize = TargetData::RoundUpAlignment(StructSize, TyAlign);
60 // Keep track of maximum alignment constraint.
61 StructAlignment = std::max(TyAlign, StructAlignment);
63 MemberOffsets[i] = StructSize;
64 StructSize += TD.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 = TargetData::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 // TargetAlignElem, TargetAlign support
99 //===----------------------------------------------------------------------===//
102 TargetAlignElem::get(AlignTypeEnum align_type, unsigned char abi_align,
103 unsigned char pref_align, uint32_t bit_width) {
104 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
105 TargetAlignElem retval;
106 retval.AlignType = align_type;
107 retval.ABIAlign = abi_align;
108 retval.PrefAlign = pref_align;
109 retval.TypeBitWidth = bit_width;
114 TargetAlignElem::operator==(const TargetAlignElem &rhs) const {
115 return (AlignType == rhs.AlignType
116 && ABIAlign == rhs.ABIAlign
117 && PrefAlign == rhs.PrefAlign
118 && TypeBitWidth == rhs.TypeBitWidth);
122 TargetAlignElem::dump(std::ostream &os) const {
123 return os << AlignType
125 << ":" << (int) (ABIAlign * 8)
126 << ":" << (int) (PrefAlign * 8);
129 const TargetAlignElem TargetData::InvalidAlignmentElem =
130 TargetAlignElem::get((AlignTypeEnum) -1, 0, 0, 0);
132 //===----------------------------------------------------------------------===//
133 // TargetData Class Implementation
134 //===----------------------------------------------------------------------===//
136 /// getInt - Get an integer ignoring errors.
137 static unsigned getInt(StringRef R) {
139 R.getAsInteger(10, Result);
143 void TargetData::init(StringRef Desc) {
145 LittleEndian = false;
148 PointerPrefAlign = PointerABIAlign;
150 // Default alignments
151 setAlignment(INTEGER_ALIGN, 1, 1, 1); // i1
152 setAlignment(INTEGER_ALIGN, 1, 1, 8); // i8
153 setAlignment(INTEGER_ALIGN, 2, 2, 16); // i16
154 setAlignment(INTEGER_ALIGN, 4, 4, 32); // i32
155 setAlignment(INTEGER_ALIGN, 4, 8, 64); // i64
156 setAlignment(FLOAT_ALIGN, 4, 4, 32); // float
157 setAlignment(FLOAT_ALIGN, 8, 8, 64); // double
158 setAlignment(VECTOR_ALIGN, 8, 8, 64); // v2i32, v1i64, ...
159 setAlignment(VECTOR_ALIGN, 16, 16, 128); // v16i8, v8i16, v4i32, ...
160 setAlignment(AGGREGATE_ALIGN, 0, 8, 0); // struct
162 while (!Desc.empty()) {
163 std::pair<StringRef, StringRef> Split = Desc.split('-');
164 StringRef Token = Split.first;
170 Split = Token.split(':');
171 StringRef Specifier = Split.first;
172 Token = Split.second;
174 assert(!Specifier.empty() && "Can't be empty here");
176 switch (Specifier[0]) {
178 LittleEndian = false;
184 Split = Token.split(':');
185 PointerMemSize = getInt(Split.first) / 8;
186 Split = Split.second.split(':');
187 PointerABIAlign = getInt(Split.first) / 8;
188 Split = Split.second.split(':');
189 PointerPrefAlign = getInt(Split.first) / 8;
190 if (PointerPrefAlign == 0)
191 PointerPrefAlign = PointerABIAlign;
198 AlignTypeEnum AlignType;
199 switch (Specifier[0]) {
201 case 'i': AlignType = INTEGER_ALIGN; break;
202 case 'v': AlignType = VECTOR_ALIGN; break;
203 case 'f': AlignType = FLOAT_ALIGN; break;
204 case 'a': AlignType = AGGREGATE_ALIGN; break;
205 case 's': AlignType = STACK_ALIGN; break;
207 unsigned Size = getInt(Specifier.substr(1));
208 Split = Token.split(':');
209 unsigned char ABIAlign = getInt(Split.first) / 8;
211 Split = Split.second.split(':');
212 unsigned char PrefAlign = getInt(Split.first) / 8;
214 PrefAlign = ABIAlign;
215 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
218 case 'n': // Native integer types.
219 Specifier = Specifier.substr(1);
221 if (unsigned Width = getInt(Specifier))
222 LegalIntWidths.push_back(Width);
223 Split = Token.split(':');
224 Specifier = Split.first;
225 Token = Split.second;
226 } while (!Specifier.empty() || !Token.empty());
237 /// @note This has to exist, because this is a pass, but it should never be
239 TargetData::TargetData() : ImmutablePass(&ID) {
240 llvm_report_error("Bad TargetData ctor used. "
241 "Tool did not specify a TargetData to use?");
244 TargetData::TargetData(const Module *M)
245 : ImmutablePass(&ID) {
246 init(M->getDataLayout());
250 TargetData::setAlignment(AlignTypeEnum align_type, unsigned char abi_align,
251 unsigned char pref_align, uint32_t bit_width) {
252 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
253 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
254 if (Alignments[i].AlignType == align_type &&
255 Alignments[i].TypeBitWidth == bit_width) {
256 // Update the abi, preferred alignments.
257 Alignments[i].ABIAlign = abi_align;
258 Alignments[i].PrefAlign = pref_align;
263 Alignments.push_back(TargetAlignElem::get(align_type, abi_align,
264 pref_align, bit_width));
267 /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
268 /// preferred if ABIInfo = false) the target wants for the specified datatype.
269 unsigned TargetData::getAlignmentInfo(AlignTypeEnum AlignType,
270 uint32_t BitWidth, bool ABIInfo,
271 const Type *Ty) const {
272 // Check to see if we have an exact match and remember the best match we see.
273 int BestMatchIdx = -1;
275 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
276 if (Alignments[i].AlignType == AlignType &&
277 Alignments[i].TypeBitWidth == BitWidth)
278 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
280 // The best match so far depends on what we're looking for.
281 if (AlignType == VECTOR_ALIGN && Alignments[i].AlignType == VECTOR_ALIGN) {
282 // If this is a specification for a smaller vector type, we will fall back
283 // to it. This happens because <128 x double> can be implemented in terms
284 // of 64 <2 x double>.
285 if (Alignments[i].TypeBitWidth < BitWidth) {
286 // Verify that we pick the biggest of the fallbacks.
287 if (BestMatchIdx == -1 ||
288 Alignments[BestMatchIdx].TypeBitWidth < Alignments[i].TypeBitWidth)
291 } else if (AlignType == INTEGER_ALIGN &&
292 Alignments[i].AlignType == INTEGER_ALIGN) {
293 // The "best match" for integers is the smallest size that is larger than
294 // the BitWidth requested.
295 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
296 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
298 // However, if there isn't one that's larger, then we must use the
299 // largest one we have (see below)
300 if (LargestInt == -1 ||
301 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
306 // Okay, we didn't find an exact solution. Fall back here depending on what
307 // is being looked for.
308 if (BestMatchIdx == -1) {
309 // If we didn't find an integer alignment, fall back on most conservative.
310 if (AlignType == INTEGER_ALIGN) {
311 BestMatchIdx = LargestInt;
313 assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
315 // If we didn't find a vector size that is smaller or equal to this type,
316 // then we will end up scalarizing this to its element type. Just return
317 // the alignment of the element.
318 return getAlignment(cast<VectorType>(Ty)->getElementType(), ABIInfo);
322 // Since we got a "best match" index, just return it.
323 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
324 : Alignments[BestMatchIdx].PrefAlign;
327 typedef DenseMap<const StructType*, StructLayout*> LayoutInfoTy;
331 class StructLayoutMap : public AbstractTypeUser {
332 LayoutInfoTy LayoutInfo;
334 /// refineAbstractType - The callback method invoked when an abstract type is
335 /// resolved to another type. An object must override this method to update
336 /// its internal state to reference NewType instead of OldType.
338 virtual void refineAbstractType(const DerivedType *OldTy,
340 const StructType *STy = dyn_cast<const StructType>(OldTy);
341 assert(STy && "This can only track struct types.");
343 LayoutInfoTy::iterator Iter = LayoutInfo.find(STy);
344 Iter->second->~StructLayout();
346 LayoutInfo.erase(Iter);
347 OldTy->removeAbstractTypeUser(this);
350 /// typeBecameConcrete - The other case which AbstractTypeUsers must be aware
351 /// of is when a type makes the transition from being abstract (where it has
352 /// clients on its AbstractTypeUsers list) to concrete (where it does not).
353 /// This method notifies ATU's when this occurs for a type.
355 virtual void typeBecameConcrete(const DerivedType *AbsTy) {
356 const StructType *STy = dyn_cast<const StructType>(AbsTy);
357 assert(STy && "This can only track struct types.");
359 LayoutInfoTy::iterator Iter = LayoutInfo.find(STy);
360 Iter->second->~StructLayout();
362 LayoutInfo.erase(Iter);
363 AbsTy->removeAbstractTypeUser(this);
367 virtual ~StructLayoutMap() {
368 // Remove any layouts.
369 for (LayoutInfoTy::iterator
370 I = LayoutInfo.begin(), E = LayoutInfo.end(); I != E; ++I) {
371 const Type *Key = I->first;
372 StructLayout *Value = I->second;
374 if (Key && Key->isAbstract())
375 Key->removeAbstractTypeUser(this);
378 Value->~StructLayout();
384 LayoutInfoTy::iterator end() {
385 return LayoutInfo.end();
388 LayoutInfoTy::iterator find(const StructType *&Val) {
389 return LayoutInfo.find(Val);
392 bool erase(LayoutInfoTy::iterator I) {
393 return LayoutInfo.erase(I);
396 StructLayout *&operator[](const StructType *STy) {
397 return LayoutInfo[STy];
401 virtual void dump() const {}
404 } // end namespace llvm
406 TargetData::~TargetData() {
407 delete static_cast<StructLayoutMap*>(LayoutMap);
410 const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
412 LayoutMap = new StructLayoutMap();
414 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
415 StructLayout *&SL = (*STM)[Ty];
418 // Otherwise, create the struct layout. Because it is variable length, we
419 // malloc it, then use placement new.
420 int NumElts = Ty->getNumElements();
422 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
424 // Set SL before calling StructLayout's ctor. The ctor could cause other
425 // entries to be added to TheMap, invalidating our reference.
428 new (L) StructLayout(Ty, *this);
430 if (Ty->isAbstract())
431 Ty->addAbstractTypeUser(STM);
436 /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
437 /// objects. If a TargetData object is alive when types are being refined and
438 /// removed, this method must be called whenever a StructType is removed to
439 /// avoid a dangling pointer in this cache.
440 void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const {
441 if (!LayoutMap) return; // No cache.
443 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
444 LayoutInfoTy::iterator I = STM->find(Ty);
445 if (I == STM->end()) return;
447 I->second->~StructLayout();
451 if (Ty->isAbstract())
452 Ty->removeAbstractTypeUser(STM);
455 std::string TargetData::getStringRepresentation() const {
457 raw_string_ostream OS(Result);
459 OS << (LittleEndian ? "e" : "E")
460 << "-p:" << PointerMemSize*8 << ':' << PointerABIAlign*8
461 << ':' << PointerPrefAlign*8;
462 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
463 const TargetAlignElem &AI = Alignments[i];
464 OS << '-' << (char)AI.AlignType << AI.TypeBitWidth << ':'
465 << AI.ABIAlign*8 << ':' << AI.PrefAlign*8;
468 if (!LegalIntWidths.empty()) {
469 OS << "-n" << (unsigned)LegalIntWidths[0];
471 for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
472 OS << ':' << (unsigned)LegalIntWidths[i];
478 uint64_t TargetData::getTypeSizeInBits(const Type *Ty) const {
479 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
480 switch (Ty->getTypeID()) {
481 case Type::LabelTyID:
482 case Type::PointerTyID:
483 return getPointerSizeInBits();
484 case Type::ArrayTyID: {
485 const ArrayType *ATy = cast<ArrayType>(Ty);
486 return getTypeAllocSizeInBits(ATy->getElementType())*ATy->getNumElements();
488 case Type::StructTyID:
489 // Get the layout annotation... which is lazily created on demand.
490 return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
491 case Type::IntegerTyID:
492 return cast<IntegerType>(Ty)->getBitWidth();
495 case Type::FloatTyID:
497 case Type::DoubleTyID:
499 case Type::PPC_FP128TyID:
500 case Type::FP128TyID:
502 // In memory objects this is always aligned to a higher boundary, but
503 // only 80 bits contain information.
504 case Type::X86_FP80TyID:
506 case Type::VectorTyID:
507 return cast<VectorType>(Ty)->getBitWidth();
509 llvm_unreachable("TargetData::getTypeSizeInBits(): Unsupported type");
516 \param abi_or_pref Flag that determines which alignment is returned. true
517 returns the ABI alignment, false returns the preferred alignment.
518 \param Ty The underlying type for which alignment is determined.
520 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
521 == false) for the requested type \a Ty.
523 unsigned char TargetData::getAlignment(const Type *Ty, bool abi_or_pref) const {
526 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
527 switch (Ty->getTypeID()) {
528 // Early escape for the non-numeric types.
529 case Type::LabelTyID:
530 case Type::PointerTyID:
532 ? getPointerABIAlignment()
533 : getPointerPrefAlignment());
534 case Type::ArrayTyID:
535 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
537 case Type::StructTyID: {
538 // Packed structure types always have an ABI alignment of one.
539 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
542 // Get the layout annotation... which is lazily created on demand.
543 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
544 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
545 return std::max(Align, (unsigned)Layout->getAlignment());
547 case Type::IntegerTyID:
549 AlignType = INTEGER_ALIGN;
551 case Type::FloatTyID:
552 case Type::DoubleTyID:
553 // PPC_FP128TyID and FP128TyID have different data contents, but the
554 // same size and alignment, so they look the same here.
555 case Type::PPC_FP128TyID:
556 case Type::FP128TyID:
557 case Type::X86_FP80TyID:
558 AlignType = FLOAT_ALIGN;
560 case Type::VectorTyID:
561 AlignType = VECTOR_ALIGN;
564 llvm_unreachable("Bad type for getAlignment!!!");
568 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
572 unsigned char TargetData::getABITypeAlignment(const Type *Ty) const {
573 return getAlignment(Ty, true);
576 unsigned char TargetData::getCallFrameTypeAlignment(const Type *Ty) const {
577 for (unsigned i = 0, e = Alignments.size(); i != e; ++i)
578 if (Alignments[i].AlignType == STACK_ALIGN)
579 return Alignments[i].ABIAlign;
581 return getABITypeAlignment(Ty);
584 unsigned char TargetData::getPrefTypeAlignment(const Type *Ty) const {
585 return getAlignment(Ty, false);
588 unsigned char TargetData::getPreferredTypeAlignmentShift(const Type *Ty) const {
589 unsigned Align = (unsigned) getPrefTypeAlignment(Ty);
590 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
591 return Log2_32(Align);
594 /// getIntPtrType - Return an unsigned integer type that is the same size or
595 /// greater to the host pointer size.
596 const IntegerType *TargetData::getIntPtrType(LLVMContext &C) const {
597 return IntegerType::get(C, getPointerSizeInBits());
601 uint64_t TargetData::getIndexedOffset(const Type *ptrTy, Value* const* Indices,
602 unsigned NumIndices) const {
603 const Type *Ty = ptrTy;
604 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
607 generic_gep_type_iterator<Value* const*>
608 TI = gep_type_begin(ptrTy, Indices, Indices+NumIndices);
609 for (unsigned CurIDX = 0; CurIDX != NumIndices; ++CurIDX, ++TI) {
610 if (const StructType *STy = dyn_cast<StructType>(*TI)) {
611 assert(Indices[CurIDX]->getType() ==
612 Type::getInt32Ty(ptrTy->getContext()) &&
613 "Illegal struct idx");
614 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
616 // Get structure layout information...
617 const StructLayout *Layout = getStructLayout(STy);
619 // Add in the offset, as calculated by the structure layout info...
620 Result += Layout->getElementOffset(FieldNo);
622 // Update Ty to refer to current element
623 Ty = STy->getElementType(FieldNo);
625 // Update Ty to refer to current element
626 Ty = cast<SequentialType>(Ty)->getElementType();
628 // Get the array index and the size of each array element.
629 int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue();
630 Result += arrayIdx * (int64_t)getTypeAllocSize(Ty);
637 /// getPreferredAlignment - Return the preferred alignment of the specified
638 /// global. This includes an explicitly requested alignment (if the global
640 unsigned TargetData::getPreferredAlignment(const GlobalVariable *GV) const {
641 const Type *ElemType = GV->getType()->getElementType();
642 unsigned Alignment = getPrefTypeAlignment(ElemType);
643 if (GV->getAlignment() > Alignment)
644 Alignment = GV->getAlignment();
646 if (GV->hasInitializer()) {
647 if (Alignment < 16) {
648 // If the global is not external, see if it is large. If so, give it a
650 if (getTypeSizeInBits(ElemType) > 128)
651 Alignment = 16; // 16-byte alignment.
657 /// getPreferredAlignmentLog - Return the preferred alignment of the
658 /// specified global, returned in log form. This includes an explicitly
659 /// requested alignment (if the global has one).
660 unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
661 return Log2_32(getPreferredAlignment(GV));