LLVM API Documentation

TargetData.cpp

Go to the documentation of this file.
00001 //===-- TargetData.cpp - Data size & alignment routines --------------------==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines target properties related to datatype size/offset/alignment
00011 // information.
00012 //
00013 // This structure should be created once, filled in if the defaults are not
00014 // correct and then passed around by const&.  None of the members functions
00015 // require modification to the object.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "llvm/Target/TargetData.h"
00020 #include "llvm/Module.h"
00021 #include "llvm/DerivedTypes.h"
00022 #include "llvm/Constants.h"
00023 #include "llvm/Support/GetElementPtrTypeIterator.h"
00024 #include "llvm/Support/MathExtras.h"
00025 #include "llvm/Support/ManagedStatic.h"
00026 #include "llvm/Support/ErrorHandling.h"
00027 #include "llvm/System/Mutex.h"
00028 #include "llvm/ADT/DenseMap.h"
00029 #include "llvm/ADT/StringExtras.h"
00030 #include <algorithm>
00031 #include <cstdlib>
00032 using namespace llvm;
00033 
00034 // Handle the Pass registration stuff necessary to use TargetData's.
00035 
00036 // Register the default SparcV9 implementation...
00037 static RegisterPass<TargetData> X("targetdata", "Target Data Layout", false, 
00038                                   true);
00039 char TargetData::ID = 0;
00040 
00041 //===----------------------------------------------------------------------===//
00042 // Support for StructLayout
00043 //===----------------------------------------------------------------------===//
00044 
00045 StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
00046   StructAlignment = 0;
00047   StructSize = 0;
00048   NumElements = ST->getNumElements();
00049 
00050   // Loop over each of the elements, placing them in memory.
00051   for (unsigned i = 0, e = NumElements; i != e; ++i) {
00052     const Type *Ty = ST->getElementType(i);
00053     unsigned TyAlign = ST->isPacked() ? 1 : TD.getABITypeAlignment(Ty);
00054 
00055     // Add padding if necessary to align the data element properly.
00056     if ((StructSize & (TyAlign-1)) != 0)
00057       StructSize = TargetData::RoundUpAlignment(StructSize, TyAlign);
00058 
00059     // Keep track of maximum alignment constraint.
00060     StructAlignment = std::max(TyAlign, StructAlignment);
00061 
00062     MemberOffsets[i] = StructSize;
00063     StructSize += TD.getTypeAllocSize(Ty); // Consume space for this data item
00064   }
00065 
00066   // Empty structures have alignment of 1 byte.
00067   if (StructAlignment == 0) StructAlignment = 1;
00068 
00069   // Add padding to the end of the struct so that it could be put in an array
00070   // and all array elements would be aligned correctly.
00071   if ((StructSize & (StructAlignment-1)) != 0)
00072     StructSize = TargetData::RoundUpAlignment(StructSize, StructAlignment);
00073 }
00074 
00075 
00076 /// getElementContainingOffset - Given a valid offset into the structure,
00077 /// return the structure index that contains it.
00078 unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
00079   const uint64_t *SI =
00080     std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
00081   assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
00082   --SI;
00083   assert(*SI <= Offset && "upper_bound didn't work");
00084   assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
00085          (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
00086          "Upper bound didn't work!");
00087   
00088   // Multiple fields can have the same offset if any of them are zero sized.
00089   // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
00090   // at the i32 element, because it is the last element at that offset.  This is
00091   // the right one to return, because anything after it will have a higher
00092   // offset, implying that this element is non-empty.
00093   return SI-&MemberOffsets[0];
00094 }
00095 
00096 //===----------------------------------------------------------------------===//
00097 // TargetAlignElem, TargetAlign support
00098 //===----------------------------------------------------------------------===//
00099 
00100 TargetAlignElem
00101 TargetAlignElem::get(AlignTypeEnum align_type, unsigned char abi_align,
00102                      unsigned char pref_align, uint32_t bit_width) {
00103   assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
00104   TargetAlignElem retval;
00105   retval.AlignType = align_type;
00106   retval.ABIAlign = abi_align;
00107   retval.PrefAlign = pref_align;
00108   retval.TypeBitWidth = bit_width;
00109   return retval;
00110 }
00111 
00112 bool
00113 TargetAlignElem::operator==(const TargetAlignElem &rhs) const {
00114   return (AlignType == rhs.AlignType
00115           && ABIAlign == rhs.ABIAlign
00116           && PrefAlign == rhs.PrefAlign
00117           && TypeBitWidth == rhs.TypeBitWidth);
00118 }
00119 
00120 std::ostream &
00121 TargetAlignElem::dump(std::ostream &os) const {
00122   return os << AlignType
00123             << TypeBitWidth
00124             << ":" << (int) (ABIAlign * 8)
00125             << ":" << (int) (PrefAlign * 8);
00126 }
00127 
00128 const TargetAlignElem TargetData::InvalidAlignmentElem =
00129                 TargetAlignElem::get((AlignTypeEnum) -1, 0, 0, 0);
00130 
00131 //===----------------------------------------------------------------------===//
00132 //                       TargetData Class Implementation
00133 //===----------------------------------------------------------------------===//
00134 
00135 /*!
00136  A TargetDescription string consists of a sequence of hyphen-delimited
00137  specifiers for target endianness, pointer size and alignments, and various
00138  primitive type sizes and alignments. A typical string looks something like:
00139  <br><br>
00140  "E-p:32:32:32-i1:8:8-i8:8:8-i32:32:32-i64:32:64-f32:32:32-f64:32:64"
00141  <br><br>
00142  (note: this string is not fully specified and is only an example.)
00143  \p
00144  Alignments come in two flavors: ABI and preferred. ABI alignment (abi_align,
00145  below) dictates how a type will be aligned within an aggregate and when used
00146  as an argument.  Preferred alignment (pref_align, below) determines a type's
00147  alignment when emitted as a global.
00148  \p
00149  Specifier string details:
00150  <br><br>
00151  <i>[E|e]</i>: Endianness. "E" specifies a big-endian target data model, "e"
00152  specifies a little-endian target data model.
00153  <br><br>
00154  <i>p:@verbatim<size>:<abi_align>:<pref_align>@endverbatim</i>: Pointer size, 
00155  ABI and preferred alignment.
00156  <br><br>
00157  <i>@verbatim<type><size>:<abi_align>:<pref_align>@endverbatim</i>: Numeric type
00158  alignment. Type is
00159  one of <i>i|f|v|a</i>, corresponding to integer, floating point, vector, or
00160  aggregate.  Size indicates the size, e.g., 32 or 64 bits.
00161  \p
00162  The default string, fully specified, is:
00163  <br><br>
00164  "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64"
00165  "-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64"
00166  "-v64:64:64-v128:128:128"
00167  <br><br>
00168  Note that in the case of aggregates, 0 is the default ABI and preferred
00169  alignment. This is a special case, where the aggregate's computed worst-case
00170  alignment will be used.
00171  */ 
00172 void TargetData::init(const std::string &TargetDescription) {
00173   std::string temp = TargetDescription;
00174   
00175   LayoutMap = 0;
00176   LittleEndian = false;
00177   PointerMemSize = 8;
00178   PointerABIAlign   = 8;
00179   PointerPrefAlign = PointerABIAlign;
00180 
00181   // Default alignments
00182   setAlignment(INTEGER_ALIGN,   1,  1, 1);   // i1
00183   setAlignment(INTEGER_ALIGN,   1,  1, 8);   // i8
00184   setAlignment(INTEGER_ALIGN,   2,  2, 16);  // i16
00185   setAlignment(INTEGER_ALIGN,   4,  4, 32);  // i32
00186   setAlignment(INTEGER_ALIGN,   4,  8, 64);  // i64
00187   setAlignment(FLOAT_ALIGN,     4,  4, 32);  // float
00188   setAlignment(FLOAT_ALIGN,     8,  8, 64);  // double
00189   setAlignment(VECTOR_ALIGN,    8,  8, 64);  // v2i32, v1i64, ...
00190   setAlignment(VECTOR_ALIGN,   16, 16, 128); // v16i8, v8i16, v4i32, ...
00191   setAlignment(AGGREGATE_ALIGN, 0,  8,  0);  // struct
00192 
00193   while (!temp.empty()) {
00194     std::string token = getToken(temp, "-");
00195     std::string arg0 = getToken(token, ":");
00196     const char *p = arg0.c_str();
00197     switch(*p) {
00198     case 'E':
00199       LittleEndian = false;
00200       break;
00201     case 'e':
00202       LittleEndian = true;
00203       break;
00204     case 'p':
00205       PointerMemSize = atoi(getToken(token,":").c_str()) / 8;
00206       PointerABIAlign = atoi(getToken(token,":").c_str()) / 8;
00207       PointerPrefAlign = atoi(getToken(token,":").c_str()) / 8;
00208       if (PointerPrefAlign == 0)
00209         PointerPrefAlign = PointerABIAlign;
00210       break;
00211     case 'i':
00212     case 'v':
00213     case 'f':
00214     case 'a':
00215     case 's': {
00216       AlignTypeEnum align_type = STACK_ALIGN; // Dummy init, silence warning
00217       switch(*p) {
00218         case 'i': align_type = INTEGER_ALIGN; break;
00219         case 'v': align_type = VECTOR_ALIGN; break;
00220         case 'f': align_type = FLOAT_ALIGN; break;
00221         case 'a': align_type = AGGREGATE_ALIGN; break;
00222         case 's': align_type = STACK_ALIGN; break;
00223       }
00224       uint32_t size = (uint32_t) atoi(++p);
00225       unsigned char abi_align = atoi(getToken(token, ":").c_str()) / 8;
00226       unsigned char pref_align = atoi(getToken(token, ":").c_str()) / 8;
00227       if (pref_align == 0)
00228         pref_align = abi_align;
00229       setAlignment(align_type, abi_align, pref_align, size);
00230       break;
00231     }
00232     default:
00233       break;
00234     }
00235   }
00236 }
00237 
00238 TargetData::TargetData(const Module *M) 
00239   : ImmutablePass(&ID) {
00240   init(M->getDataLayout());
00241 }
00242 
00243 void
00244 TargetData::setAlignment(AlignTypeEnum align_type, unsigned char abi_align,
00245                          unsigned char pref_align, uint32_t bit_width) {
00246   assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
00247   for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
00248     if (Alignments[i].AlignType == align_type &&
00249         Alignments[i].TypeBitWidth == bit_width) {
00250       // Update the abi, preferred alignments.
00251       Alignments[i].ABIAlign = abi_align;
00252       Alignments[i].PrefAlign = pref_align;
00253       return;
00254     }
00255   }
00256   
00257   Alignments.push_back(TargetAlignElem::get(align_type, abi_align,
00258                                             pref_align, bit_width));
00259 }
00260 
00261 /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or 
00262 /// preferred if ABIInfo = false) the target wants for the specified datatype.
00263 unsigned TargetData::getAlignmentInfo(AlignTypeEnum AlignType, 
00264                                       uint32_t BitWidth, bool ABIInfo,
00265                                       const Type *Ty) const {
00266   // Check to see if we have an exact match and remember the best match we see.
00267   int BestMatchIdx = -1;
00268   int LargestInt = -1;
00269   for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
00270     if (Alignments[i].AlignType == AlignType &&
00271         Alignments[i].TypeBitWidth == BitWidth)
00272       return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
00273     
00274     // The best match so far depends on what we're looking for.
00275     if (AlignType == VECTOR_ALIGN && Alignments[i].AlignType == VECTOR_ALIGN) {
00276       // If this is a specification for a smaller vector type, we will fall back
00277       // to it.  This happens because <128 x double> can be implemented in terms
00278       // of 64 <2 x double>.
00279       if (Alignments[i].TypeBitWidth < BitWidth) {
00280         // Verify that we pick the biggest of the fallbacks.
00281         if (BestMatchIdx == -1 ||
00282             Alignments[BestMatchIdx].TypeBitWidth < Alignments[i].TypeBitWidth)
00283           BestMatchIdx = i;
00284       }
00285     } else if (AlignType == INTEGER_ALIGN && 
00286                Alignments[i].AlignType == INTEGER_ALIGN) {
00287       // The "best match" for integers is the smallest size that is larger than
00288       // the BitWidth requested.
00289       if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 || 
00290            Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
00291         BestMatchIdx = i;
00292       // However, if there isn't one that's larger, then we must use the
00293       // largest one we have (see below)
00294       if (LargestInt == -1 || 
00295           Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
00296         LargestInt = i;
00297     }
00298   }
00299 
00300   // Okay, we didn't find an exact solution.  Fall back here depending on what
00301   // is being looked for.
00302   if (BestMatchIdx == -1) {
00303     // If we didn't find an integer alignment, fall back on most conservative.
00304     if (AlignType == INTEGER_ALIGN) {
00305       BestMatchIdx = LargestInt;
00306     } else {
00307       assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
00308 
00309       // If we didn't find a vector size that is smaller or equal to this type,
00310       // then we will end up scalarizing this to its element type.  Just return
00311       // the alignment of the element.
00312       return getAlignment(cast<VectorType>(Ty)->getElementType(), ABIInfo);
00313     }
00314   }
00315 
00316   // Since we got a "best match" index, just return it.
00317   return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
00318                  : Alignments[BestMatchIdx].PrefAlign;
00319 }
00320 
00321 typedef DenseMap<const StructType*, StructLayout*>LayoutInfoTy;
00322 
00323 TargetData::~TargetData() {
00324   if (!LayoutMap)
00325     return;
00326   
00327   // Remove any layouts for this TD.
00328   LayoutInfoTy &TheMap = *static_cast<LayoutInfoTy*>(LayoutMap);
00329   for (LayoutInfoTy::iterator I = TheMap.begin(), E = TheMap.end(); I != E; ) {
00330     I->second->~StructLayout();
00331     free(I->second);
00332     TheMap.erase(I++);
00333   }
00334   
00335   delete static_cast<LayoutInfoTy*>(LayoutMap);
00336 }
00337 
00338 const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
00339   if (!LayoutMap)
00340     LayoutMap = static_cast<void*>(new LayoutInfoTy());
00341   
00342   LayoutInfoTy &TheMap = *static_cast<LayoutInfoTy*>(LayoutMap);
00343   
00344   StructLayout *&SL = TheMap[Ty];
00345   if (SL) return SL;
00346 
00347   // Otherwise, create the struct layout.  Because it is variable length, we 
00348   // malloc it, then use placement new.
00349   int NumElts = Ty->getNumElements();
00350   StructLayout *L =
00351     (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1)*sizeof(uint64_t));
00352   
00353   // Set SL before calling StructLayout's ctor.  The ctor could cause other
00354   // entries to be added to TheMap, invalidating our reference.
00355   SL = L;
00356   
00357   new (L) StructLayout(Ty, *this);
00358   return L;
00359 }
00360 
00361 /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
00362 /// objects.  If a TargetData object is alive when types are being refined and
00363 /// removed, this method must be called whenever a StructType is removed to
00364 /// avoid a dangling pointer in this cache.
00365 void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const {
00366   if (!LayoutMap) return;  // No cache.
00367   
00368   LayoutInfoTy* LayoutInfo = static_cast<LayoutInfoTy*>(LayoutMap);
00369   LayoutInfoTy::iterator I = LayoutInfo->find(Ty);
00370   if (I == LayoutInfo->end()) return;
00371   
00372   I->second->~StructLayout();
00373   free(I->second);
00374   LayoutInfo->erase(I);
00375 }
00376 
00377 
00378 std::string TargetData::getStringRepresentation() const {
00379   std::string repr;
00380   repr.append(LittleEndian ? "e" : "E");
00381   repr.append("-p:").append(itostr((int64_t) (PointerMemSize * 8))).
00382       append(":").append(itostr((int64_t) (PointerABIAlign * 8))).
00383       append(":").append(itostr((int64_t) (PointerPrefAlign * 8)));
00384   for (align_const_iterator I = Alignments.begin();
00385        I != Alignments.end();
00386        ++I) {
00387     repr.append("-").append(1, (char) I->AlignType).
00388       append(utostr((int64_t) I->TypeBitWidth)).
00389       append(":").append(utostr((uint64_t) (I->ABIAlign * 8))).
00390       append(":").append(utostr((uint64_t) (I->PrefAlign * 8)));
00391   }
00392   return repr;
00393 }
00394 
00395 
00396 uint64_t TargetData::getTypeSizeInBits(const Type *Ty) const {
00397   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
00398   switch (Ty->getTypeID()) {
00399   case Type::LabelTyID:
00400   case Type::PointerTyID:
00401     return getPointerSizeInBits();
00402   case Type::ArrayTyID: {
00403     const ArrayType *ATy = cast<ArrayType>(Ty);
00404     return getTypeAllocSizeInBits(ATy->getElementType())*ATy->getNumElements();
00405   }
00406   case Type::StructTyID:
00407     // Get the layout annotation... which is lazily created on demand.
00408     return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
00409   case Type::IntegerTyID:
00410     return cast<IntegerType>(Ty)->getBitWidth();
00411   case Type::VoidTyID:
00412     return 8;
00413   case Type::FloatTyID:
00414     return 32;
00415   case Type::DoubleTyID:
00416     return 64;
00417   case Type::PPC_FP128TyID:
00418   case Type::FP128TyID:
00419     return 128;
00420   // In memory objects this is always aligned to a higher boundary, but
00421   // only 80 bits contain information.
00422   case Type::X86_FP80TyID:
00423     return 80;
00424   case Type::VectorTyID:
00425     return cast<VectorType>(Ty)->getBitWidth();
00426   default:
00427     llvm_unreachable("TargetData::getTypeSizeInBits(): Unsupported type");
00428     break;
00429   }
00430   return 0;
00431 }
00432 
00433 /*!
00434   \param abi_or_pref Flag that determines which alignment is returned. true
00435   returns the ABI alignment, false returns the preferred alignment.
00436   \param Ty The underlying type for which alignment is determined.
00437 
00438   Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
00439   == false) for the requested type \a Ty.
00440  */
00441 unsigned char TargetData::getAlignment(const Type *Ty, bool abi_or_pref) const {
00442   int AlignType = -1;
00443 
00444   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
00445   switch (Ty->getTypeID()) {
00446   // Early escape for the non-numeric types.
00447   case Type::LabelTyID:
00448   case Type::PointerTyID:
00449     return (abi_or_pref
00450             ? getPointerABIAlignment()
00451             : getPointerPrefAlignment());
00452   case Type::ArrayTyID:
00453     return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
00454 
00455   case Type::StructTyID: {
00456     // Packed structure types always have an ABI alignment of one.
00457     if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
00458       return 1;
00459 
00460     // Get the layout annotation... which is lazily created on demand.
00461     const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
00462     unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
00463     return std::max(Align, (unsigned)Layout->getAlignment());
00464   }
00465   case Type::IntegerTyID:
00466   case Type::VoidTyID:
00467     AlignType = INTEGER_ALIGN;
00468     break;
00469   case Type::FloatTyID:
00470   case Type::DoubleTyID:
00471   // PPC_FP128TyID and FP128TyID have different data contents, but the
00472   // same size and alignment, so they look the same here.
00473   case Type::PPC_FP128TyID:
00474   case Type::FP128TyID:
00475   case Type::X86_FP80TyID:
00476     AlignType = FLOAT_ALIGN;
00477     break;
00478   case Type::VectorTyID:
00479     AlignType = VECTOR_ALIGN;
00480     break;
00481   default:
00482     llvm_unreachable("Bad type for getAlignment!!!");
00483     break;
00484   }
00485 
00486   return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
00487                           abi_or_pref, Ty);
00488 }
00489 
00490 unsigned char TargetData::getABITypeAlignment(const Type *Ty) const {
00491   return getAlignment(Ty, true);
00492 }
00493 
00494 unsigned char TargetData::getCallFrameTypeAlignment(const Type *Ty) const {
00495   for (unsigned i = 0, e = Alignments.size(); i != e; ++i)
00496     if (Alignments[i].AlignType == STACK_ALIGN)
00497       return Alignments[i].ABIAlign;
00498 
00499   return getABITypeAlignment(Ty);
00500 }
00501 
00502 unsigned char TargetData::getPrefTypeAlignment(const Type *Ty) const {
00503   return getAlignment(Ty, false);
00504 }
00505 
00506 unsigned char TargetData::getPreferredTypeAlignmentShift(const Type *Ty) const {
00507   unsigned Align = (unsigned) getPrefTypeAlignment(Ty);
00508   assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
00509   return Log2_32(Align);
00510 }
00511 
00512 /// getIntPtrType - Return an unsigned integer type that is the same size or
00513 /// greater to the host pointer size.
00514 const IntegerType *TargetData::getIntPtrType(LLVMContext &C) const {
00515   return IntegerType::get(C, getPointerSizeInBits());
00516 }
00517 
00518 
00519 uint64_t TargetData::getIndexedOffset(const Type *ptrTy, Value* const* Indices,
00520                                       unsigned NumIndices) const {
00521   const Type *Ty = ptrTy;
00522   assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
00523   uint64_t Result = 0;
00524 
00525   generic_gep_type_iterator<Value* const*>
00526     TI = gep_type_begin(ptrTy, Indices, Indices+NumIndices);
00527   for (unsigned CurIDX = 0; CurIDX != NumIndices; ++CurIDX, ++TI) {
00528     if (const StructType *STy = dyn_cast<StructType>(*TI)) {
00529       assert(Indices[CurIDX]->getType() ==
00530              Type::getInt32Ty(ptrTy->getContext()) &&
00531              "Illegal struct idx");
00532       unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
00533 
00534       // Get structure layout information...
00535       const StructLayout *Layout = getStructLayout(STy);
00536 
00537       // Add in the offset, as calculated by the structure layout info...
00538       Result += Layout->getElementOffset(FieldNo);
00539 
00540       // Update Ty to refer to current element
00541       Ty = STy->getElementType(FieldNo);
00542     } else {
00543       // Update Ty to refer to current element
00544       Ty = cast<SequentialType>(Ty)->getElementType();
00545 
00546       // Get the array index and the size of each array element.
00547       int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue();
00548       Result += arrayIdx * (int64_t)getTypeAllocSize(Ty);
00549     }
00550   }
00551 
00552   return Result;
00553 }
00554 
00555 /// getPreferredAlignment - Return the preferred alignment of the specified
00556 /// global.  This includes an explicitly requested alignment (if the global
00557 /// has one).
00558 unsigned TargetData::getPreferredAlignment(const GlobalVariable *GV) const {
00559   const Type *ElemType = GV->getType()->getElementType();
00560   unsigned Alignment = getPrefTypeAlignment(ElemType);
00561   if (GV->getAlignment() > Alignment)
00562     Alignment = GV->getAlignment();
00563 
00564   if (GV->hasInitializer()) {
00565     if (Alignment < 16) {
00566       // If the global is not external, see if it is large.  If so, give it a
00567       // larger alignment.
00568       if (getTypeSizeInBits(ElemType) > 128)
00569         Alignment = 16;    // 16-byte alignment.
00570     }
00571   }
00572   return Alignment;
00573 }
00574 
00575 /// getPreferredAlignmentLog - Return the preferred alignment of the
00576 /// specified global, returned in log form.  This includes an explicitly
00577 /// requested alignment (if the global has one).
00578 unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
00579   return Log2_32(getPreferredAlignment(GV));
00580 }