improve portability to avoid conflicting with std::next in c++'0x.
[oota-llvm.git] / 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/System/Mutex.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include <algorithm>
31 #include <cstdlib>
32 #include <ostream>
33 using namespace llvm;
34
35 // Handle the Pass registration stuff necessary to use TargetData's.
36
37 // Register the default SparcV9 implementation...
38 static RegisterPass<TargetData> X("targetdata", "Target Data Layout", false, 
39                                   true);
40 char TargetData::ID = 0;
41
42 //===----------------------------------------------------------------------===//
43 // Support for StructLayout
44 //===----------------------------------------------------------------------===//
45
46 StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
47   StructAlignment = 0;
48   StructSize = 0;
49   NumElements = ST->getNumElements();
50
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);
55
56     // Add padding if necessary to align the data element properly.
57     if ((StructSize & (TyAlign-1)) != 0)
58       StructSize = TargetData::RoundUpAlignment(StructSize, TyAlign);
59
60     // Keep track of maximum alignment constraint.
61     StructAlignment = std::max(TyAlign, StructAlignment);
62
63     MemberOffsets[i] = StructSize;
64     StructSize += TD.getTypeAllocSize(Ty); // Consume space for this data item
65   }
66
67   // Empty structures have alignment of 1 byte.
68   if (StructAlignment == 0) StructAlignment = 1;
69
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);
74 }
75
76
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 {
80   const uint64_t *SI =
81     std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
82   assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
83   --SI;
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!");
88   
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];
95 }
96
97 //===----------------------------------------------------------------------===//
98 // TargetAlignElem, TargetAlign support
99 //===----------------------------------------------------------------------===//
100
101 TargetAlignElem
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;
110   return retval;
111 }
112
113 bool
114 TargetAlignElem::operator==(const TargetAlignElem &rhs) const {
115   return (AlignType == rhs.AlignType
116           && ABIAlign == rhs.ABIAlign
117           && PrefAlign == rhs.PrefAlign
118           && TypeBitWidth == rhs.TypeBitWidth);
119 }
120
121 std::ostream &
122 TargetAlignElem::dump(std::ostream &os) const {
123   return os << AlignType
124             << TypeBitWidth
125             << ":" << (int) (ABIAlign * 8)
126             << ":" << (int) (PrefAlign * 8);
127 }
128
129 const TargetAlignElem TargetData::InvalidAlignmentElem =
130                 TargetAlignElem::get((AlignTypeEnum) -1, 0, 0, 0);
131
132 //===----------------------------------------------------------------------===//
133 //                       TargetData Class Implementation
134 //===----------------------------------------------------------------------===//
135
136 /// getInt - Get an integer ignoring errors.
137 static unsigned getInt(StringRef R) {
138   unsigned Result = 0;
139   R.getAsInteger(10, Result);
140   return Result;
141 }
142
143 void TargetData::init(StringRef Desc) {
144   LayoutMap = 0;
145   LittleEndian = false;
146   PointerMemSize = 8;
147   PointerABIAlign = 8;
148   PointerPrefAlign = PointerABIAlign;
149
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
161
162   while (!Desc.empty()) {
163     std::pair<StringRef, StringRef> Split = Desc.split('-');
164     StringRef Token = Split.first;
165     Desc = Split.second;
166     
167     if (Token.empty())
168       continue;
169     
170     Split = Token.split(':');
171     StringRef Specifier = Split.first;
172     Token = Split.second;
173     
174     assert(!Specifier.empty() && "Can't be empty here");
175     
176     switch (Specifier[0]) {
177     case 'E':
178       LittleEndian = false;
179       break;
180     case 'e':
181       LittleEndian = true;
182       break;
183     case 'p':
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;
192       break;
193     case 'i':
194     case 'v':
195     case 'f':
196     case 'a':
197     case 's': {
198       AlignTypeEnum AlignType;
199       switch (Specifier[0]) {
200       default:
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;
206       }
207       unsigned Size = getInt(Specifier.substr(1));
208       Split = Token.split(':');
209       unsigned char ABIAlign = getInt(Split.first) / 8;
210       
211       Split = Split.second.split(':');
212       unsigned char PrefAlign = getInt(Split.first) / 8;
213       if (PrefAlign == 0)
214         PrefAlign = ABIAlign;
215       setAlignment(AlignType, ABIAlign, PrefAlign, Size);
216       break;
217     }
218     case 'n':  // Native integer types.
219       Specifier = Specifier.substr(1);
220       do {
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());
227       break;
228         
229     default:
230       break;
231     }
232   }
233 }
234
235 /// Default ctor.
236 ///
237 /// @note This has to exist, because this is a pass, but it should never be
238 /// used.
239 TargetData::TargetData() : ImmutablePass(&ID) {
240   llvm_report_error("Bad TargetData ctor used.  "
241                     "Tool did not specify a TargetData to use?");
242 }
243
244 TargetData::TargetData(const Module *M) 
245   : ImmutablePass(&ID) {
246   init(M->getDataLayout());
247 }
248
249 void
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;
259       return;
260     }
261   }
262   
263   Alignments.push_back(TargetAlignElem::get(align_type, abi_align,
264                                             pref_align, bit_width));
265 }
266
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;
274   int LargestInt = -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;
279     
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)
289           BestMatchIdx = i;
290       }
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))
297         BestMatchIdx = i;
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)
302         LargestInt = i;
303     }
304   }
305
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;
312     } else {
313       assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
314
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);
319     }
320   }
321
322   // Since we got a "best match" index, just return it.
323   return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
324                  : Alignments[BestMatchIdx].PrefAlign;
325 }
326
327 typedef DenseMap<const StructType*, StructLayout*> LayoutInfoTy;
328
329 namespace {
330
331 class StructLayoutMap : public AbstractTypeUser {
332   LayoutInfoTy LayoutInfo;
333
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.
337   ///
338   virtual void refineAbstractType(const DerivedType *OldTy,
339                                   const Type *) {
340     const StructType *STy = dyn_cast<const StructType>(OldTy);
341     assert(STy && "This can only track struct types.");
342
343     LayoutInfoTy::iterator Iter = LayoutInfo.find(STy);
344     Iter->second->~StructLayout();
345     free(Iter->second);
346     LayoutInfo.erase(Iter);
347     OldTy->removeAbstractTypeUser(this);
348   }
349
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.
354   ///
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.");
358
359     LayoutInfoTy::iterator Iter = LayoutInfo.find(STy);
360     Iter->second->~StructLayout();
361     free(Iter->second);
362     LayoutInfo.erase(Iter);
363     AbsTy->removeAbstractTypeUser(this);
364   }
365
366 public:
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;
373
374       if (Key && Key->isAbstract())
375         Key->removeAbstractTypeUser(this);
376
377       if (Value) {
378         Value->~StructLayout();
379         free(Value);
380       }
381     }
382   }
383
384   LayoutInfoTy::iterator end() {
385     return LayoutInfo.end();
386   }
387
388   LayoutInfoTy::iterator find(const StructType *&Val) {
389     return LayoutInfo.find(Val);
390   }
391
392   bool erase(LayoutInfoTy::iterator I) {
393     return LayoutInfo.erase(I);
394   }
395
396   StructLayout *&operator[](const StructType *STy) {
397     return LayoutInfo[STy];
398   }
399
400   // for debugging...
401   virtual void dump() const {}
402 };
403
404 } // end namespace llvm
405
406 TargetData::~TargetData() {
407   delete static_cast<StructLayoutMap*>(LayoutMap);
408 }
409
410 const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
411   if (!LayoutMap)
412     LayoutMap = new StructLayoutMap();
413   
414   StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
415   StructLayout *&SL = (*STM)[Ty];
416   if (SL) return SL;
417
418   // Otherwise, create the struct layout.  Because it is variable length, we 
419   // malloc it, then use placement new.
420   int NumElts = Ty->getNumElements();
421   StructLayout *L =
422     (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
423   
424   // Set SL before calling StructLayout's ctor.  The ctor could cause other
425   // entries to be added to TheMap, invalidating our reference.
426   SL = L;
427   
428   new (L) StructLayout(Ty, *this);
429
430   if (Ty->isAbstract())
431     Ty->addAbstractTypeUser(STM);
432
433   return L;
434 }
435
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.
442   
443   StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
444   LayoutInfoTy::iterator I = STM->find(Ty);
445   if (I == STM->end()) return;
446   
447   I->second->~StructLayout();
448   free(I->second);
449   STM->erase(I);
450
451   if (Ty->isAbstract())
452     Ty->removeAbstractTypeUser(STM);
453 }
454
455 std::string TargetData::getStringRepresentation() const {
456   std::string Result;
457   raw_string_ostream OS(Result);
458   
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;
466   }
467   
468   if (!LegalIntWidths.empty()) {
469     OS << "-n" << (unsigned)LegalIntWidths[0];
470     
471     for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
472       OS << ':' << (unsigned)LegalIntWidths[i];
473   }
474   return OS.str();
475 }
476
477
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();
487   }
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();
493   case Type::VoidTyID:
494     return 8;
495   case Type::FloatTyID:
496     return 32;
497   case Type::DoubleTyID:
498     return 64;
499   case Type::PPC_FP128TyID:
500   case Type::FP128TyID:
501     return 128;
502   // In memory objects this is always aligned to a higher boundary, but
503   // only 80 bits contain information.
504   case Type::X86_FP80TyID:
505     return 80;
506   case Type::VectorTyID:
507     return cast<VectorType>(Ty)->getBitWidth();
508   default:
509     llvm_unreachable("TargetData::getTypeSizeInBits(): Unsupported type");
510     break;
511   }
512   return 0;
513 }
514
515 /*!
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.
519
520   Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
521   == false) for the requested type \a Ty.
522  */
523 unsigned char TargetData::getAlignment(const Type *Ty, bool abi_or_pref) const {
524   int AlignType = -1;
525
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:
531     return (abi_or_pref
532             ? getPointerABIAlignment()
533             : getPointerPrefAlignment());
534   case Type::ArrayTyID:
535     return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
536
537   case Type::StructTyID: {
538     // Packed structure types always have an ABI alignment of one.
539     if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
540       return 1;
541
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());
546   }
547   case Type::IntegerTyID:
548   case Type::VoidTyID:
549     AlignType = INTEGER_ALIGN;
550     break;
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;
559     break;
560   case Type::VectorTyID:
561     AlignType = VECTOR_ALIGN;
562     break;
563   default:
564     llvm_unreachable("Bad type for getAlignment!!!");
565     break;
566   }
567
568   return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
569                           abi_or_pref, Ty);
570 }
571
572 unsigned char TargetData::getABITypeAlignment(const Type *Ty) const {
573   return getAlignment(Ty, true);
574 }
575
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;
580
581   return getABITypeAlignment(Ty);
582 }
583
584 unsigned char TargetData::getPrefTypeAlignment(const Type *Ty) const {
585   return getAlignment(Ty, false);
586 }
587
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);
592 }
593
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());
598 }
599
600
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()");
605   uint64_t Result = 0;
606
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();
615
616       // Get structure layout information...
617       const StructLayout *Layout = getStructLayout(STy);
618
619       // Add in the offset, as calculated by the structure layout info...
620       Result += Layout->getElementOffset(FieldNo);
621
622       // Update Ty to refer to current element
623       Ty = STy->getElementType(FieldNo);
624     } else {
625       // Update Ty to refer to current element
626       Ty = cast<SequentialType>(Ty)->getElementType();
627
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);
631     }
632   }
633
634   return Result;
635 }
636
637 /// getPreferredAlignment - Return the preferred alignment of the specified
638 /// global.  This includes an explicitly requested alignment (if the global
639 /// has one).
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();
645
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
649       // larger alignment.
650       if (getTypeSizeInBits(ElemType) > 128)
651         Alignment = 16;    // 16-byte alignment.
652     }
653   }
654   return Alignment;
655 }
656
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));
662 }