LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.