ARM NEON VTBL/VTBX assembly parsing and encoding.
[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/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 int getInt(StringRef R) {
129   int Result = 0;
130   R.getAsInteger(10, Result);
131   return Result;
132 }
133
134 void TargetData::init() {
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
157 std::string TargetData::parseSpecifier(StringRef Desc, TargetData *td) {
158
159   if (td)
160     td->init();
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       if (td)
179         td->LittleEndian = false;
180       break;
181     case 'e':
182       if (td)
183         td->LittleEndian = true;
184       break;
185     case 'p': {
186       // Pointer size.
187       Split = Token.split(':');
188       int PointerMemSizeBits = getInt(Split.first);
189       if (PointerMemSizeBits < 0 || PointerMemSizeBits % 8 != 0)
190         return "invalid pointer size, must be a positive 8-bit multiple";
191       if (td)
192         td->PointerMemSize = PointerMemSizeBits / 8;
193
194       // Pointer ABI alignment.
195       Split = Split.second.split(':');
196       int PointerABIAlignBits = getInt(Split.first);
197       if (PointerABIAlignBits < 0 || PointerABIAlignBits % 8 != 0) {
198         return "invalid pointer ABI alignment, "
199                "must be a positive 8-bit multiple";
200       }
201       if (td)
202         td->PointerABIAlign = PointerABIAlignBits / 8;
203
204       // Pointer preferred alignment.
205       Split = Split.second.split(':');
206       int PointerPrefAlignBits = getInt(Split.first);
207       if (PointerPrefAlignBits < 0 || PointerPrefAlignBits % 8 != 0) {
208         return "invalid pointer preferred alignment, "
209                "must be a positive 8-bit multiple";
210       }
211       if (td) {
212         td->PointerPrefAlign = PointerPrefAlignBits / 8;
213         if (td->PointerPrefAlign == 0)
214           td->PointerPrefAlign = td->PointerABIAlign;
215       }
216       break;
217     }
218     case 'i':
219     case 'v':
220     case 'f':
221     case 'a':
222     case 's': {
223       AlignTypeEnum AlignType;
224       char field = Specifier[0];
225       switch (field) {
226       default:
227       case 'i': AlignType = INTEGER_ALIGN; break;
228       case 'v': AlignType = VECTOR_ALIGN; break;
229       case 'f': AlignType = FLOAT_ALIGN; break;
230       case 'a': AlignType = AGGREGATE_ALIGN; break;
231       case 's': AlignType = STACK_ALIGN; break;
232       }
233       int Size = getInt(Specifier.substr(1));
234       if (Size < 0) {
235         return std::string("invalid ") + field + "-size field, "
236                "must be positive";
237       }
238
239       Split = Token.split(':');
240       int ABIAlignBits = getInt(Split.first);
241       if (ABIAlignBits < 0 || ABIAlignBits % 8 != 0) {
242         return std::string("invalid ") + field +"-abi-alignment field, "
243                "must be a positive 8-bit multiple";
244       }
245       unsigned ABIAlign = ABIAlignBits / 8;
246
247       Split = Split.second.split(':');
248
249       int PrefAlignBits = getInt(Split.first);
250       if (PrefAlignBits < 0 || PrefAlignBits % 8 != 0) {
251         return std::string("invalid ") + field +"-preferred-alignment field, "
252                "must be a positive 8-bit multiple";
253       }
254       unsigned PrefAlign = PrefAlignBits / 8;
255       if (PrefAlign == 0)
256         PrefAlign = ABIAlign;
257       
258       if (td)
259         td->setAlignment(AlignType, ABIAlign, PrefAlign, Size);
260       break;
261     }
262     case 'n':  // Native integer types.
263       Specifier = Specifier.substr(1);
264       do {
265         int Width = getInt(Specifier);
266         if (Width <= 0) {
267           return std::string("invalid native integer size \'") + Specifier.str() +
268                  "\', must be a positive integer.";
269         }
270         if (td && Width != 0)
271           td->LegalIntWidths.push_back(Width);
272         Split = Token.split(':');
273         Specifier = Split.first;
274         Token = Split.second;
275       } while (!Specifier.empty() || !Token.empty());
276       break;
277     case 'S': { // Stack natural alignment.
278       int StackNaturalAlignBits = getInt(Specifier.substr(1));
279       if (StackNaturalAlignBits < 0 || StackNaturalAlignBits % 8 != 0) {
280         return "invalid natural stack alignment (S-field), "
281                "must be a positive 8-bit multiple";
282       }
283       if (td)
284         td->StackNaturalAlign = StackNaturalAlignBits / 8;
285       break;
286     }
287     default:
288       break;
289     }
290   }
291
292   return "";
293 }
294
295 /// Default ctor.
296 ///
297 /// @note This has to exist, because this is a pass, but it should never be
298 /// used.
299 TargetData::TargetData() : ImmutablePass(ID) {
300   report_fatal_error("Bad TargetData ctor used.  "
301                     "Tool did not specify a TargetData to use?");
302 }
303
304 TargetData::TargetData(const Module *M)
305   : ImmutablePass(ID) {
306   std::string errMsg = parseSpecifier(M->getDataLayout(), this);
307   assert(errMsg == "" && "Module M has malformed target data layout string.");
308   (void)errMsg;
309 }
310
311 void
312 TargetData::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
313                          unsigned pref_align, uint32_t bit_width) {
314   assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
315   for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
316     if (Alignments[i].AlignType == align_type &&
317         Alignments[i].TypeBitWidth == bit_width) {
318       // Update the abi, preferred alignments.
319       Alignments[i].ABIAlign = abi_align;
320       Alignments[i].PrefAlign = pref_align;
321       return;
322     }
323   }
324
325   Alignments.push_back(TargetAlignElem::get(align_type, abi_align,
326                                             pref_align, bit_width));
327 }
328
329 /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
330 /// preferred if ABIInfo = false) the target wants for the specified datatype.
331 unsigned TargetData::getAlignmentInfo(AlignTypeEnum AlignType,
332                                       uint32_t BitWidth, bool ABIInfo,
333                                       Type *Ty) const {
334   // Check to see if we have an exact match and remember the best match we see.
335   int BestMatchIdx = -1;
336   int LargestInt = -1;
337   for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
338     if (Alignments[i].AlignType == AlignType &&
339         Alignments[i].TypeBitWidth == BitWidth)
340       return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
341
342     // The best match so far depends on what we're looking for.
343      if (AlignType == INTEGER_ALIGN &&
344          Alignments[i].AlignType == INTEGER_ALIGN) {
345       // The "best match" for integers is the smallest size that is larger than
346       // the BitWidth requested.
347       if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
348            Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
349         BestMatchIdx = i;
350       // However, if there isn't one that's larger, then we must use the
351       // largest one we have (see below)
352       if (LargestInt == -1 ||
353           Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
354         LargestInt = i;
355     }
356   }
357
358   // Okay, we didn't find an exact solution.  Fall back here depending on what
359   // is being looked for.
360   if (BestMatchIdx == -1) {
361     // If we didn't find an integer alignment, fall back on most conservative.
362     if (AlignType == INTEGER_ALIGN) {
363       BestMatchIdx = LargestInt;
364     } else {
365       assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
366
367       // By default, use natural alignment for vector types. This is consistent
368       // with what clang and llvm-gcc do.
369       unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
370       Align *= cast<VectorType>(Ty)->getNumElements();
371       // If the alignment is not a power of 2, round up to the next power of 2.
372       // This happens for non-power-of-2 length vectors.
373       if (Align & (Align-1))
374         Align = llvm::NextPowerOf2(Align);
375       return Align;
376     }
377   }
378
379   // Since we got a "best match" index, just return it.
380   return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
381                  : Alignments[BestMatchIdx].PrefAlign;
382 }
383
384 namespace {
385
386 class StructLayoutMap {
387   typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
388   LayoutInfoTy LayoutInfo;
389
390 public:
391   virtual ~StructLayoutMap() {
392     // Remove any layouts.
393     for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end();
394          I != E; ++I) {
395       StructLayout *Value = I->second;
396       Value->~StructLayout();
397       free(Value);
398     }
399   }
400
401   StructLayout *&operator[](StructType *STy) {
402     return LayoutInfo[STy];
403   }
404
405   // for debugging...
406   virtual void dump() const {}
407 };
408
409 } // end anonymous namespace
410
411 TargetData::~TargetData() {
412   delete static_cast<StructLayoutMap*>(LayoutMap);
413 }
414
415 const StructLayout *TargetData::getStructLayout(StructType *Ty) const {
416   if (!LayoutMap)
417     LayoutMap = new StructLayoutMap();
418
419   StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
420   StructLayout *&SL = (*STM)[Ty];
421   if (SL) return SL;
422
423   // Otherwise, create the struct layout.  Because it is variable length, we
424   // malloc it, then use placement new.
425   int NumElts = Ty->getNumElements();
426   StructLayout *L =
427     (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
428
429   // Set SL before calling StructLayout's ctor.  The ctor could cause other
430   // entries to be added to TheMap, invalidating our reference.
431   SL = L;
432
433   new (L) StructLayout(Ty, *this);
434
435   return L;
436 }
437
438 std::string TargetData::getStringRepresentation() const {
439   std::string Result;
440   raw_string_ostream OS(Result);
441
442   OS << (LittleEndian ? "e" : "E")
443      << "-p:" << PointerMemSize*8 << ':' << PointerABIAlign*8
444      << ':' << PointerPrefAlign*8
445      << "-S" << StackNaturalAlign*8;
446
447   for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
448     const TargetAlignElem &AI = Alignments[i];
449     OS << '-' << (char)AI.AlignType << AI.TypeBitWidth << ':'
450        << AI.ABIAlign*8 << ':' << AI.PrefAlign*8;
451   }
452
453   if (!LegalIntWidths.empty()) {
454     OS << "-n" << (unsigned)LegalIntWidths[0];
455
456     for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
457       OS << ':' << (unsigned)LegalIntWidths[i];
458   }
459   return OS.str();
460 }
461
462
463 uint64_t TargetData::getTypeSizeInBits(Type *Ty) const {
464   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
465   switch (Ty->getTypeID()) {
466   case Type::LabelTyID:
467   case Type::PointerTyID:
468     return getPointerSizeInBits();
469   case Type::ArrayTyID: {
470     ArrayType *ATy = cast<ArrayType>(Ty);
471     return getTypeAllocSizeInBits(ATy->getElementType())*ATy->getNumElements();
472   }
473   case Type::StructTyID:
474     // Get the layout annotation... which is lazily created on demand.
475     return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
476   case Type::IntegerTyID:
477     return cast<IntegerType>(Ty)->getBitWidth();
478   case Type::VoidTyID:
479     return 8;
480   case Type::FloatTyID:
481     return 32;
482   case Type::DoubleTyID:
483   case Type::X86_MMXTyID:
484     return 64;
485   case Type::PPC_FP128TyID:
486   case Type::FP128TyID:
487     return 128;
488   // In memory objects this is always aligned to a higher boundary, but
489   // only 80 bits contain information.
490   case Type::X86_FP80TyID:
491     return 80;
492   case Type::VectorTyID:
493     return cast<VectorType>(Ty)->getBitWidth();
494   default:
495     llvm_unreachable("TargetData::getTypeSizeInBits(): Unsupported type");
496     break;
497   }
498   return 0;
499 }
500
501 /*!
502   \param abi_or_pref Flag that determines which alignment is returned. true
503   returns the ABI alignment, false returns the preferred alignment.
504   \param Ty The underlying type for which alignment is determined.
505
506   Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
507   == false) for the requested type \a Ty.
508  */
509 unsigned TargetData::getAlignment(Type *Ty, bool abi_or_pref) const {
510   int AlignType = -1;
511
512   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
513   switch (Ty->getTypeID()) {
514   // Early escape for the non-numeric types.
515   case Type::LabelTyID:
516   case Type::PointerTyID:
517     return (abi_or_pref
518             ? getPointerABIAlignment()
519             : getPointerPrefAlignment());
520   case Type::ArrayTyID:
521     return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
522
523   case Type::StructTyID: {
524     // Packed structure types always have an ABI alignment of one.
525     if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
526       return 1;
527
528     // Get the layout annotation... which is lazily created on demand.
529     const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
530     unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
531     return std::max(Align, Layout->getAlignment());
532   }
533   case Type::IntegerTyID:
534   case Type::VoidTyID:
535     AlignType = INTEGER_ALIGN;
536     break;
537   case Type::FloatTyID:
538   case Type::DoubleTyID:
539   // PPC_FP128TyID and FP128TyID have different data contents, but the
540   // same size and alignment, so they look the same here.
541   case Type::PPC_FP128TyID:
542   case Type::FP128TyID:
543   case Type::X86_FP80TyID:
544     AlignType = FLOAT_ALIGN;
545     break;
546   case Type::X86_MMXTyID:
547   case Type::VectorTyID:
548     AlignType = VECTOR_ALIGN;
549     break;
550   default:
551     llvm_unreachable("Bad type for getAlignment!!!");
552     break;
553   }
554
555   return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
556                           abi_or_pref, Ty);
557 }
558
559 unsigned TargetData::getABITypeAlignment(Type *Ty) const {
560   return getAlignment(Ty, true);
561 }
562
563 /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
564 /// an integer type of the specified bitwidth.
565 unsigned TargetData::getABIIntegerTypeAlignment(unsigned BitWidth) const {
566   return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0);
567 }
568
569
570 unsigned TargetData::getCallFrameTypeAlignment(Type *Ty) const {
571   for (unsigned i = 0, e = Alignments.size(); i != e; ++i)
572     if (Alignments[i].AlignType == STACK_ALIGN)
573       return Alignments[i].ABIAlign;
574
575   return getABITypeAlignment(Ty);
576 }
577
578 unsigned TargetData::getPrefTypeAlignment(Type *Ty) const {
579   return getAlignment(Ty, false);
580 }
581
582 unsigned TargetData::getPreferredTypeAlignmentShift(Type *Ty) const {
583   unsigned Align = getPrefTypeAlignment(Ty);
584   assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
585   return Log2_32(Align);
586 }
587
588 /// getIntPtrType - Return an unsigned integer type that is the same size or
589 /// greater to the host pointer size.
590 IntegerType *TargetData::getIntPtrType(LLVMContext &C) const {
591   return IntegerType::get(C, getPointerSizeInBits());
592 }
593
594
595 uint64_t TargetData::getIndexedOffset(Type *ptrTy,
596                                       ArrayRef<Value *> Indices) const {
597   Type *Ty = ptrTy;
598   assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
599   uint64_t Result = 0;
600
601   generic_gep_type_iterator<Value* const*>
602     TI = gep_type_begin(ptrTy, Indices);
603   for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
604        ++CurIDX, ++TI) {
605     if (StructType *STy = dyn_cast<StructType>(*TI)) {
606       assert(Indices[CurIDX]->getType() ==
607              Type::getInt32Ty(ptrTy->getContext()) &&
608              "Illegal struct idx");
609       unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
610
611       // Get structure layout information...
612       const StructLayout *Layout = getStructLayout(STy);
613
614       // Add in the offset, as calculated by the structure layout info...
615       Result += Layout->getElementOffset(FieldNo);
616
617       // Update Ty to refer to current element
618       Ty = STy->getElementType(FieldNo);
619     } else {
620       // Update Ty to refer to current element
621       Ty = cast<SequentialType>(Ty)->getElementType();
622
623       // Get the array index and the size of each array element.
624       if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
625         Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
626     }
627   }
628
629   return Result;
630 }
631
632 /// getPreferredAlignment - Return the preferred alignment of the specified
633 /// global.  This includes an explicitly requested alignment (if the global
634 /// has one).
635 unsigned TargetData::getPreferredAlignment(const GlobalVariable *GV) const {
636   Type *ElemType = GV->getType()->getElementType();
637   unsigned Alignment = getPrefTypeAlignment(ElemType);
638   unsigned GVAlignment = GV->getAlignment();
639   if (GVAlignment >= Alignment) {
640     Alignment = GVAlignment;
641   } else if (GVAlignment != 0) {
642     Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
643   }
644
645   if (GV->hasInitializer() && GVAlignment == 0) {
646     if (Alignment < 16) {
647       // If the global is not external, see if it is large.  If so, give it a
648       // larger alignment.
649       if (getTypeSizeInBits(ElemType) > 128)
650         Alignment = 16;    // 16-byte alignment.
651     }
652   }
653   return Alignment;
654 }
655
656 /// getPreferredAlignmentLog - Return the preferred alignment of the
657 /// specified global, returned in log form.  This includes an explicitly
658 /// requested alignment (if the global has one).
659 unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
660   return Log2_32(getPreferredAlignment(GV));
661 }