]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/lib/Target/TargetData.cpp
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / lib / Target / TargetData.cpp
1 //===-- TargetData.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 target 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/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"
30 #include <algorithm>
31 #include <cstdlib>
32 using namespace llvm;
33
34 // Handle the Pass registration stuff necessary to use TargetData's.
35
36 // Register the default SparcV9 implementation...
37 INITIALIZE_PASS(TargetData, "targetdata", "Target Data Layout", false, true)
38 char TargetData::ID = 0;
39
40 //===----------------------------------------------------------------------===//
41 // Support for StructLayout
42 //===----------------------------------------------------------------------===//
43
44 StructLayout::StructLayout(StructType *ST, const TargetData &TD) {
45   assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
46   StructAlignment = 0;
47   StructSize = 0;
48   NumElements = ST->getNumElements();
49
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);
54
55     // Add padding if necessary to align the data element properly.
56     if ((StructSize & (TyAlign-1)) != 0)
57       StructSize = TargetData::RoundUpAlignment(StructSize, TyAlign);
58
59     // Keep track of maximum alignment constraint.
60     StructAlignment = std::max(TyAlign, StructAlignment);
61
62     MemberOffsets[i] = StructSize;
63     StructSize += TD.getTypeAllocSize(Ty); // Consume space for this data item
64   }
65
66   // Empty structures have alignment of 1 byte.
67   if (StructAlignment == 0) StructAlignment = 1;
68
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);
73 }
74
75
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 {
79   const uint64_t *SI =
80     std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
81   assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
82   --SI;
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!");
87
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];
94 }
95
96 //===----------------------------------------------------------------------===//
97 // TargetAlignElem, TargetAlign support
98 //===----------------------------------------------------------------------===//
99
100 TargetAlignElem
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;
109   return retval;
110 }
111
112 bool
113 TargetAlignElem::operator==(const TargetAlignElem &rhs) const {
114   return (AlignType == rhs.AlignType
115           && ABIAlign == rhs.ABIAlign
116           && PrefAlign == rhs.PrefAlign
117           && TypeBitWidth == rhs.TypeBitWidth);
118 }
119
120 const TargetAlignElem TargetData::InvalidAlignmentElem =
121                 TargetAlignElem::get((AlignTypeEnum) -1, 0, 0, 0);
122
123 //===----------------------------------------------------------------------===//
124 //                       TargetData Class Implementation
125 //===----------------------------------------------------------------------===//
126
127 /// getInt - Get an integer ignoring errors.
128 static unsigned getInt(StringRef R) {
129   unsigned Result = 0;
130   R.getAsInteger(10, Result);
131   return Result;
132 }
133
134 void TargetData::init(StringRef Desc) {
135   initializeTargetDataPass(*PassRegistry::getPassRegistry());
136   
137   LayoutMap = 0;
138   LittleEndian = false;
139   PointerMemSize = 8;
140   PointerABIAlign = 8;
141   PointerPrefAlign = PointerABIAlign;
142   StackNaturalAlign = 0;
143
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,     4,  4, 32);  // float
151   setAlignment(FLOAT_ALIGN,     8,  8, 64);  // double
152   setAlignment(VECTOR_ALIGN,    8,  8, 64);  // v2i32, v1i64, ...
153   setAlignment(VECTOR_ALIGN,   16, 16, 128); // v16i8, v8i16, v4i32, ...
154   setAlignment(AGGREGATE_ALIGN, 0,  8,  0);  // struct
155
156   while (!Desc.empty()) {
157     std::pair<StringRef, StringRef> Split = Desc.split('-');
158     StringRef Token = Split.first;
159     Desc = Split.second;
160
161     if (Token.empty())
162       continue;
163
164     Split = Token.split(':');
165     StringRef Specifier = Split.first;
166     Token = Split.second;
167
168     assert(!Specifier.empty() && "Can't be empty here");
169
170     switch (Specifier[0]) {
171     case 'E':
172       LittleEndian = false;
173       break;
174     case 'e':
175       LittleEndian = true;
176       break;
177     case 'p':
178       Split = Token.split(':');
179       PointerMemSize = getInt(Split.first) / 8;
180       Split = Split.second.split(':');
181       PointerABIAlign = getInt(Split.first) / 8;
182       Split = Split.second.split(':');
183       PointerPrefAlign = getInt(Split.first) / 8;
184       if (PointerPrefAlign == 0)
185         PointerPrefAlign = PointerABIAlign;
186       break;
187     case 'i':
188     case 'v':
189     case 'f':
190     case 'a':
191     case 's': {
192       AlignTypeEnum AlignType;
193       switch (Specifier[0]) {
194       default:
195       case 'i': AlignType = INTEGER_ALIGN; break;
196       case 'v': AlignType = VECTOR_ALIGN; break;
197       case 'f': AlignType = FLOAT_ALIGN; break;
198       case 'a': AlignType = AGGREGATE_ALIGN; break;
199       case 's': AlignType = STACK_ALIGN; break;
200       }
201       unsigned Size = getInt(Specifier.substr(1));
202       Split = Token.split(':');
203       unsigned ABIAlign = getInt(Split.first) / 8;
204
205       Split = Split.second.split(':');
206       unsigned PrefAlign = getInt(Split.first) / 8;
207       if (PrefAlign == 0)
208         PrefAlign = ABIAlign;
209       setAlignment(AlignType, ABIAlign, PrefAlign, Size);
210       break;
211     }
212     case 'n':  // Native integer types.
213       Specifier = Specifier.substr(1);
214       do {
215         if (unsigned Width = getInt(Specifier))
216           LegalIntWidths.push_back(Width);
217         Split = Token.split(':');
218         Specifier = Split.first;
219         Token = Split.second;
220       } while (!Specifier.empty() || !Token.empty());
221       break;
222     case 'S': // Stack natural alignment.
223       StackNaturalAlign = getInt(Specifier.substr(1));
224       StackNaturalAlign /= 8;
225       // FIXME: Should we really be truncating these alingments and
226       // sizes silently?
227       break;
228     default:
229       break;
230     }
231   }
232 }
233
234 /// Default ctor.
235 ///
236 /// @note This has to exist, because this is a pass, but it should never be
237 /// used.
238 TargetData::TargetData() : ImmutablePass(ID) {
239   report_fatal_error("Bad TargetData ctor used.  "
240                     "Tool did not specify a TargetData to use?");
241 }
242
243 TargetData::TargetData(const Module *M)
244   : ImmutablePass(ID) {
245   init(M->getDataLayout());
246 }
247
248 void
249 TargetData::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
250                          unsigned pref_align, uint32_t bit_width) {
251   assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
252   for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
253     if (Alignments[i].AlignType == align_type &&
254         Alignments[i].TypeBitWidth == bit_width) {
255       // Update the abi, preferred alignments.
256       Alignments[i].ABIAlign = abi_align;
257       Alignments[i].PrefAlign = pref_align;
258       return;
259     }
260   }
261
262   Alignments.push_back(TargetAlignElem::get(align_type, abi_align,
263                                             pref_align, bit_width));
264 }
265
266 /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
267 /// preferred if ABIInfo = false) the target wants for the specified datatype.
268 unsigned TargetData::getAlignmentInfo(AlignTypeEnum AlignType,
269                                       uint32_t BitWidth, bool ABIInfo,
270                                       Type *Ty) const {
271   // Check to see if we have an exact match and remember the best match we see.
272   int BestMatchIdx = -1;
273   int LargestInt = -1;
274   for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
275     if (Alignments[i].AlignType == AlignType &&
276         Alignments[i].TypeBitWidth == BitWidth)
277       return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
278
279     // The best match so far depends on what we're looking for.
280      if (AlignType == INTEGER_ALIGN &&
281          Alignments[i].AlignType == INTEGER_ALIGN) {
282       // The "best match" for integers is the smallest size that is larger than
283       // the BitWidth requested.
284       if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
285            Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
286         BestMatchIdx = i;
287       // However, if there isn't one that's larger, then we must use the
288       // largest one we have (see below)
289       if (LargestInt == -1 ||
290           Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
291         LargestInt = i;
292     }
293   }
294
295   // Okay, we didn't find an exact solution.  Fall back here depending on what
296   // is being looked for.
297   if (BestMatchIdx == -1) {
298     // If we didn't find an integer alignment, fall back on most conservative.
299     if (AlignType == INTEGER_ALIGN) {
300       BestMatchIdx = LargestInt;
301     } else {
302       assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
303
304       // By default, use natural alignment for vector types. This is consistent
305       // with what clang and llvm-gcc do.
306       unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
307       Align *= cast<VectorType>(Ty)->getNumElements();
308       // If the alignment is not a power of 2, round up to the next power of 2.
309       // This happens for non-power-of-2 length vectors.
310       if (Align & (Align-1))
311         Align = llvm::NextPowerOf2(Align);
312       return Align;
313     }
314   }
315
316   // Since we got a "best match" index, just return it.
317   return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
318                  : Alignments[BestMatchIdx].PrefAlign;
319 }
320
321 namespace {
322
323 class StructLayoutMap {
324   typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
325   LayoutInfoTy LayoutInfo;
326
327 public:
328   virtual ~StructLayoutMap() {
329     // Remove any layouts.
330     for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end();
331          I != E; ++I) {
332       StructLayout *Value = I->second;
333       Value->~StructLayout();
334       free(Value);
335     }
336   }
337
338   StructLayout *&operator[](StructType *STy) {
339     return LayoutInfo[STy];
340   }
341
342   // for debugging...
343   virtual void dump() const {}
344 };
345
346 } // end anonymous namespace
347
348 TargetData::~TargetData() {
349   delete static_cast<StructLayoutMap*>(LayoutMap);
350 }
351
352 const StructLayout *TargetData::getStructLayout(StructType *Ty) const {
353   if (!LayoutMap)
354     LayoutMap = new StructLayoutMap();
355
356   StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
357   StructLayout *&SL = (*STM)[Ty];
358   if (SL) return SL;
359
360   // Otherwise, create the struct layout.  Because it is variable length, we
361   // malloc it, then use placement new.
362   int NumElts = Ty->getNumElements();
363   StructLayout *L =
364     (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
365
366   // Set SL before calling StructLayout's ctor.  The ctor could cause other
367   // entries to be added to TheMap, invalidating our reference.
368   SL = L;
369
370   new (L) StructLayout(Ty, *this);
371
372   return L;
373 }
374
375 std::string TargetData::getStringRepresentation() const {
376   std::string Result;
377   raw_string_ostream OS(Result);
378
379   OS << (LittleEndian ? "e" : "E")
380      << "-p:" << PointerMemSize*8 << ':' << PointerABIAlign*8
381      << ':' << PointerPrefAlign*8
382      << "-S" << StackNaturalAlign*8;
383
384   for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
385     const TargetAlignElem &AI = Alignments[i];
386     OS << '-' << (char)AI.AlignType << AI.TypeBitWidth << ':'
387        << AI.ABIAlign*8 << ':' << AI.PrefAlign*8;
388   }
389
390   if (!LegalIntWidths.empty()) {
391     OS << "-n" << (unsigned)LegalIntWidths[0];
392
393     for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
394       OS << ':' << (unsigned)LegalIntWidths[i];
395   }
396   return OS.str();
397 }
398
399
400 uint64_t TargetData::getTypeSizeInBits(Type *Ty) const {
401   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
402   switch (Ty->getTypeID()) {
403   case Type::LabelTyID:
404   case Type::PointerTyID:
405     return getPointerSizeInBits();
406   case Type::ArrayTyID: {
407     ArrayType *ATy = cast<ArrayType>(Ty);
408     return getTypeAllocSizeInBits(ATy->getElementType())*ATy->getNumElements();
409   }
410   case Type::StructTyID:
411     // Get the layout annotation... which is lazily created on demand.
412     return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
413   case Type::IntegerTyID:
414     return cast<IntegerType>(Ty)->getBitWidth();
415   case Type::VoidTyID:
416     return 8;
417   case Type::FloatTyID:
418     return 32;
419   case Type::DoubleTyID:
420   case Type::X86_MMXTyID:
421     return 64;
422   case Type::PPC_FP128TyID:
423   case Type::FP128TyID:
424     return 128;
425   // In memory objects this is always aligned to a higher boundary, but
426   // only 80 bits contain information.
427   case Type::X86_FP80TyID:
428     return 80;
429   case Type::VectorTyID:
430     return cast<VectorType>(Ty)->getBitWidth();
431   default:
432     llvm_unreachable("TargetData::getTypeSizeInBits(): Unsupported type");
433     break;
434   }
435   return 0;
436 }
437
438 /*!
439   \param abi_or_pref Flag that determines which alignment is returned. true
440   returns the ABI alignment, false returns the preferred alignment.
441   \param Ty The underlying type for which alignment is determined.
442
443   Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
444   == false) for the requested type \a Ty.
445  */
446 unsigned TargetData::getAlignment(Type *Ty, bool abi_or_pref) const {
447   int AlignType = -1;
448
449   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
450   switch (Ty->getTypeID()) {
451   // Early escape for the non-numeric types.
452   case Type::LabelTyID:
453   case Type::PointerTyID:
454     return (abi_or_pref
455             ? getPointerABIAlignment()
456             : getPointerPrefAlignment());
457   case Type::ArrayTyID:
458     return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
459
460   case Type::StructTyID: {
461     // Packed structure types always have an ABI alignment of one.
462     if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
463       return 1;
464
465     // Get the layout annotation... which is lazily created on demand.
466     const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
467     unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
468     return std::max(Align, Layout->getAlignment());
469   }
470   case Type::IntegerTyID:
471   case Type::VoidTyID:
472     AlignType = INTEGER_ALIGN;
473     break;
474   case Type::FloatTyID:
475   case Type::DoubleTyID:
476   // PPC_FP128TyID and FP128TyID have different data contents, but the
477   // same size and alignment, so they look the same here.
478   case Type::PPC_FP128TyID:
479   case Type::FP128TyID:
480   case Type::X86_FP80TyID:
481     AlignType = FLOAT_ALIGN;
482     break;
483   case Type::X86_MMXTyID:
484   case Type::VectorTyID:
485     AlignType = VECTOR_ALIGN;
486     break;
487   default:
488     llvm_unreachable("Bad type for getAlignment!!!");
489     break;
490   }
491
492   return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
493                           abi_or_pref, Ty);
494 }
495
496 unsigned TargetData::getABITypeAlignment(Type *Ty) const {
497   return getAlignment(Ty, true);
498 }
499
500 /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
501 /// an integer type of the specified bitwidth.
502 unsigned TargetData::getABIIntegerTypeAlignment(unsigned BitWidth) const {
503   return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0);
504 }
505
506
507 unsigned TargetData::getCallFrameTypeAlignment(Type *Ty) const {
508   for (unsigned i = 0, e = Alignments.size(); i != e; ++i)
509     if (Alignments[i].AlignType == STACK_ALIGN)
510       return Alignments[i].ABIAlign;
511
512   return getABITypeAlignment(Ty);
513 }
514
515 unsigned TargetData::getPrefTypeAlignment(Type *Ty) const {
516   return getAlignment(Ty, false);
517 }
518
519 unsigned TargetData::getPreferredTypeAlignmentShift(Type *Ty) const {
520   unsigned Align = getPrefTypeAlignment(Ty);
521   assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
522   return Log2_32(Align);
523 }
524
525 /// getIntPtrType - Return an unsigned integer type that is the same size or
526 /// greater to the host pointer size.
527 IntegerType *TargetData::getIntPtrType(LLVMContext &C) const {
528   return IntegerType::get(C, getPointerSizeInBits());
529 }
530
531
532 uint64_t TargetData::getIndexedOffset(Type *ptrTy,
533                                       ArrayRef<Value *> Indices) const {
534   Type *Ty = ptrTy;
535   assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
536   uint64_t Result = 0;
537
538   generic_gep_type_iterator<Value* const*>
539     TI = gep_type_begin(ptrTy, Indices);
540   for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
541        ++CurIDX, ++TI) {
542     if (StructType *STy = dyn_cast<StructType>(*TI)) {
543       assert(Indices[CurIDX]->getType() ==
544              Type::getInt32Ty(ptrTy->getContext()) &&
545              "Illegal struct idx");
546       unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
547
548       // Get structure layout information...
549       const StructLayout *Layout = getStructLayout(STy);
550
551       // Add in the offset, as calculated by the structure layout info...
552       Result += Layout->getElementOffset(FieldNo);
553
554       // Update Ty to refer to current element
555       Ty = STy->getElementType(FieldNo);
556     } else {
557       // Update Ty to refer to current element
558       Ty = cast<SequentialType>(Ty)->getElementType();
559
560       // Get the array index and the size of each array element.
561       if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
562         Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
563     }
564   }
565
566   return Result;
567 }
568
569 /// getPreferredAlignment - Return the preferred alignment of the specified
570 /// global.  This includes an explicitly requested alignment (if the global
571 /// has one).
572 unsigned TargetData::getPreferredAlignment(const GlobalVariable *GV) const {
573   Type *ElemType = GV->getType()->getElementType();
574   unsigned Alignment = getPrefTypeAlignment(ElemType);
575   unsigned GVAlignment = GV->getAlignment();
576   if (GVAlignment >= Alignment) {
577     Alignment = GVAlignment;
578   } else if (GVAlignment != 0) {
579     Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
580   }
581
582   if (GV->hasInitializer() && GVAlignment == 0) {
583     if (Alignment < 16) {
584       // If the global is not external, see if it is large.  If so, give it a
585       // larger alignment.
586       if (getTypeSizeInBits(ElemType) > 128)
587         Alignment = 16;    // 16-byte alignment.
588     }
589   }
590   return Alignment;
591 }
592
593 /// getPreferredAlignmentLog - Return the preferred alignment of the
594 /// specified global, returned in log form.  This includes an explicitly
595 /// requested alignment (if the global has one).
596 unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
597   return Log2_32(getPreferredAlignment(GV));
598 }