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