TargetData.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. #include "llvm/Target/TargetData.h"
  19. #include "llvm/Module.h"
  20. #include "llvm/DerivedTypes.h"
  21. #include "llvm/Constants.h"
  22. #include "llvm/Support/GetElementPtrTypeIterator.h"
  23. #include "llvm/Support/MathExtras.h"
  24. #include "llvm/Support/ManagedStatic.h"
  25. #include "llvm/ADT/DenseMap.h"
  26. #include "llvm/ADT/StringExtras.h"
  27. #include <algorithm>
  28. #include <cstdlib>
  29. using namespace llvm;
  30. // Handle the Pass registration stuff necessary to use TargetData's.
  31. // Register the default SparcV9 implementation...
  32. static RegisterPass<TargetData> X("targetdata", "Target Data Layout", false,
  33. true);
  34. char TargetData::ID = 0;
  35. //===----------------------------------------------------------------------===//
  36. // Support for StructLayout
  37. //===----------------------------------------------------------------------===//
  38. StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
  39. StructAlignment = 0;
  40. StructSize = 0;
  41. NumElements = ST->getNumElements();
  42. // Loop over each of the elements, placing them in memory...
  43. for (unsigned i = 0, e = NumElements; i != e; ++i) {
  44. const Type *Ty = ST->getElementType(i);
  45. unsigned TyAlign = ST->isPacked() ? 1 : TD.getABITypeAlignment(Ty);
  46. // Add padding if necessary to align the data element properly...
  47. StructSize = (StructSize + TyAlign - 1)/TyAlign * TyAlign;
  48. // Keep track of maximum alignment constraint
  49. StructAlignment = std::max(TyAlign, StructAlignment);
  50. MemberOffsets[i] = StructSize;
  51. StructSize += TD.getABITypeSize(Ty); // Consume space for this data item
  52. }
  53. // Empty structures have alignment of 1 byte.
  54. if (StructAlignment == 0) StructAlignment = 1;
  55. // Add padding to the end of the struct so that it could be put in an array
  56. // and all array elements would be aligned correctly.
  57. if (StructSize % StructAlignment != 0)
  58. StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
  59. }
  60. /// getElementContainingOffset - Given a valid offset into the structure,
  61. /// return the structure index that contains it.
  62. unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
  63. const uint64_t *SI =
  64. std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
  65. assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
  66. --SI;
  67. assert(*SI <= Offset && "upper_bound didn't work");
  68. assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
  69. (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
  70. "Upper bound didn't work!");
  71. // Multiple fields can have the same offset if any of them are zero sized.
  72. // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
  73. // at the i32 element, because it is the last element at that offset. This is
  74. // the right one to return, because anything after it will have a higher
  75. // offset, implying that this element is non-empty.
  76. return SI-&MemberOffsets[0];
  77. }
  78. //===----------------------------------------------------------------------===//
  79. // TargetAlignElem, TargetAlign support
  80. //===----------------------------------------------------------------------===//
  81. TargetAlignElem
  82. TargetAlignElem::get(AlignTypeEnum align_type, unsigned char abi_align,
  83. unsigned char pref_align, uint32_t bit_width) {
  84. assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
  85. TargetAlignElem retval;
  86. retval.AlignType = align_type;
  87. retval.ABIAlign = abi_align;
  88. retval.PrefAlign = pref_align;
  89. retval.TypeBitWidth = bit_width;
  90. return retval;
  91. }
  92. bool
  93. TargetAlignElem::operator==(const TargetAlignElem &rhs) const {
  94. return (AlignType == rhs.AlignType
  95. && ABIAlign == rhs.ABIAlign
  96. && PrefAlign == rhs.PrefAlign
  97. && TypeBitWidth == rhs.TypeBitWidth);
  98. }
  99. std::ostream &
  100. TargetAlignElem::dump(std::ostream &os) const {
  101. return os << AlignType
  102. << TypeBitWidth
  103. << ":" << (int) (ABIAlign * 8)
  104. << ":" << (int) (PrefAlign * 8);
  105. }
  106. const TargetAlignElem TargetData::InvalidAlignmentElem =
  107. TargetAlignElem::get((AlignTypeEnum) -1, 0, 0, 0);
  108. //===----------------------------------------------------------------------===//
  109. // TargetData Class Implementation
  110. //===----------------------------------------------------------------------===//
  111. /*!
  112. A TargetDescription string consists of a sequence of hyphen-delimited
  113. specifiers for target endianness, pointer size and alignments, and various
  114. primitive type sizes and alignments. A typical string looks something like:
  115. <br><br>
  116. "E-p:32:32:32-i1:8:8-i8:8:8-i32:32:32-i64:32:64-f32:32:32-f64:32:64"
  117. <br><br>
  118. (note: this string is not fully specified and is only an example.)
  119. \p
  120. Alignments come in two flavors: ABI and preferred. ABI alignment (abi_align,
  121. below) dictates how a type will be aligned within an aggregate and when used
  122. as an argument. Preferred alignment (pref_align, below) determines a type's
  123. alignment when emitted as a global.
  124. \p
  125. Specifier string details:
  126. <br><br>
  127. <i>[E|e]</i>: Endianness. "E" specifies a big-endian target data model, "e"
  128. specifies a little-endian target data model.
  129. <br><br>
  130. <i>p:@verbatim<size>:<abi_align>:<pref_align>@endverbatim</i>: Pointer size,
  131. ABI and preferred alignment.
  132. <br><br>
  133. <i>@verbatim<type><size>:<abi_align>:<pref_align>@endverbatim</i>: Numeric type
  134. alignment. Type is
  135. one of <i>i|f|v|a</i>, corresponding to integer, floating point, vector (aka
  136. packed) or aggregate. Size indicates the size, e.g., 32 or 64 bits.
  137. \p
  138. The default string, fully specified is:
  139. <br><br>
  140. "E-p:64:64:64-a0:0:0-f32:32:32-f64:0:64"
  141. "-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:0:64"
  142. "-v64:64:64-v128:128:128"
  143. <br><br>
  144. Note that in the case of aggregates, 0 is the default ABI and preferred
  145. alignment. This is a special case, where the aggregate's computed worst-case
  146. alignment will be used.
  147. */
  148. void TargetData::init(const std::string &TargetDescription) {
  149. std::string temp = TargetDescription;
  150. LittleEndian = false;
  151. PointerMemSize = 8;
  152. PointerABIAlign = 8;
  153. PointerPrefAlign = PointerABIAlign;
  154. // Default alignments
  155. setAlignment(INTEGER_ALIGN, 1, 1, 1); // Bool
  156. setAlignment(INTEGER_ALIGN, 1, 1, 8); // Byte
  157. setAlignment(INTEGER_ALIGN, 2, 2, 16); // short
  158. setAlignment(INTEGER_ALIGN, 4, 4, 32); // int
  159. setAlignment(INTEGER_ALIGN, 4, 8, 64); // long
  160. setAlignment(FLOAT_ALIGN, 4, 4, 32); // float
  161. setAlignment(FLOAT_ALIGN, 8, 8, 64); // double
  162. setAlignment(VECTOR_ALIGN, 8, 8, 64); // v2i32
  163. setAlignment(VECTOR_ALIGN, 16, 16, 128); // v16i8, v8i16, v4i32, ...
  164. setAlignment(AGGREGATE_ALIGN, 0, 8, 0); // struct, union, class, ...
  165. while (!temp.empty()) {
  166. std::string token = getToken(temp, "-");
  167. std::string arg0 = getToken(token, ":");
  168. const char *p = arg0.c_str();
  169. switch(*p) {
  170. case 'E':
  171. LittleEndian = false;
  172. break;
  173. case 'e':
  174. LittleEndian = true;
  175. break;
  176. case 'p':
  177. PointerMemSize = atoi(getToken(token,":").c_str()) / 8;
  178. PointerABIAlign = atoi(getToken(token,":").c_str()) / 8;
  179. PointerPrefAlign = atoi(getToken(token,":").c_str()) / 8;
  180. if (PointerPrefAlign == 0)
  181. PointerPrefAlign = PointerABIAlign;
  182. break;
  183. case 'i':
  184. case 'v':
  185. case 'f':
  186. case 'a':
  187. case 's': {
  188. AlignTypeEnum align_type = STACK_ALIGN; // Dummy init, silence warning
  189. switch(*p) {
  190. case 'i': align_type = INTEGER_ALIGN; break;
  191. case 'v': align_type = VECTOR_ALIGN; break;
  192. case 'f': align_type = FLOAT_ALIGN; break;
  193. case 'a': align_type = AGGREGATE_ALIGN; break;
  194. case 's': align_type = STACK_ALIGN; break;
  195. }
  196. uint32_t size = (uint32_t) atoi(++p);
  197. unsigned char abi_align = atoi(getToken(token, ":").c_str()) / 8;
  198. unsigned char pref_align = atoi(getToken(token, ":").c_str()) / 8;
  199. if (pref_align == 0)
  200. pref_align = abi_align;
  201. setAlignment(align_type, abi_align, pref_align, size);
  202. break;
  203. }
  204. default:
  205. break;
  206. }
  207. }
  208. }
  209. TargetData::TargetData(const Module *M)
  210. : ImmutablePass(&ID) {
  211. init(M->getDataLayout());
  212. }
  213. void
  214. TargetData::setAlignment(AlignTypeEnum align_type, unsigned char abi_align,
  215. unsigned char pref_align, uint32_t bit_width) {
  216. assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
  217. for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
  218. if (Alignments[i].AlignType == align_type &&
  219. Alignments[i].TypeBitWidth == bit_width) {
  220. // Update the abi, preferred alignments.
  221. Alignments[i].ABIAlign = abi_align;
  222. Alignments[i].PrefAlign = pref_align;
  223. return;
  224. }
  225. }
  226. Alignments.push_back(TargetAlignElem::get(align_type, abi_align,
  227. pref_align, bit_width));
  228. }
  229. /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
  230. /// preferred if ABIInfo = false) the target wants for the specified datatype.
  231. unsigned TargetData::getAlignmentInfo(AlignTypeEnum AlignType,
  232. uint32_t BitWidth, bool ABIInfo,
  233. const Type *Ty) const {
  234. // Check to see if we have an exact match and remember the best match we see.
  235. int BestMatchIdx = -1;
  236. int LargestInt = -1;
  237. for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
  238. if (Alignments[i].AlignType == AlignType &&
  239. Alignments[i].TypeBitWidth == BitWidth)
  240. return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
  241. // The best match so far depends on what we're looking for.
  242. if (AlignType == VECTOR_ALIGN && Alignments[i].AlignType == VECTOR_ALIGN) {
  243. // If this is a specification for a smaller vector type, we will fall back
  244. // to it. This happens because <128 x double> can be implemented in terms
  245. // of 64 <2 x double>.
  246. if (Alignments[i].TypeBitWidth < BitWidth) {
  247. // Verify that we pick the biggest of the fallbacks.
  248. if (BestMatchIdx == -1 ||
  249. Alignments[BestMatchIdx].TypeBitWidth < Alignments[i].TypeBitWidth)
  250. BestMatchIdx = i;
  251. }
  252. } else if (AlignType == INTEGER_ALIGN &&
  253. Alignments[i].AlignType == INTEGER_ALIGN) {
  254. // The "best match" for integers is the smallest size that is larger than
  255. // the BitWidth requested.
  256. if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
  257. Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
  258. BestMatchIdx = i;
  259. // However, if there isn't one that's larger, then we must use the
  260. // largest one we have (see below)
  261. if (LargestInt == -1 ||
  262. Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
  263. LargestInt = i;
  264. }
  265. }
  266. // Okay, we didn't find an exact solution. Fall back here depending on what
  267. // is being looked for.
  268. if (BestMatchIdx == -1) {
  269. // If we didn't find an integer alignment, fall back on most conservative.
  270. if (AlignType == INTEGER_ALIGN) {
  271. BestMatchIdx = LargestInt;
  272. } else {
  273. assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
  274. // If we didn't find a vector size that is smaller or equal to this type,
  275. // then we will end up scalarizing this to its element type. Just return
  276. // the alignment of the element.
  277. return getAlignment(cast<VectorType>(Ty)->getElementType(), ABIInfo);
  278. }
  279. }
  280. // Since we got a "best match" index, just return it.
  281. return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
  282. : Alignments[BestMatchIdx].PrefAlign;
  283. }
  284. namespace {
  285. /// LayoutInfo - The lazy cache of structure layout information maintained by
  286. /// TargetData. Note that the struct types must have been free'd before
  287. /// llvm_shutdown is called (and thus this is deallocated) because all the
  288. /// targets with cached elements should have been destroyed.
  289. ///
  290. typedef std::pair<const TargetData*,const StructType*> LayoutKey;
  291. struct DenseMapLayoutKeyInfo {
  292. static inline LayoutKey getEmptyKey() { return LayoutKey(0, 0); }
  293. static inline LayoutKey getTombstoneKey() {
  294. return LayoutKey((TargetData*)(intptr_t)-1, 0);
  295. }
  296. static unsigned getHashValue(const LayoutKey &Val) {
  297. return DenseMapInfo<void*>::getHashValue(Val.first) ^
  298. DenseMapInfo<void*>::getHashValue(Val.second);
  299. }
  300. static bool isEqual(const LayoutKey &LHS, const LayoutKey &RHS) {
  301. return LHS == RHS;
  302. }
  303. static bool isPod() { return true; }
  304. };
  305. typedef DenseMap<LayoutKey, StructLayout*, DenseMapLayoutKeyInfo> LayoutInfoTy;
  306. }
  307. static ManagedStatic<LayoutInfoTy> LayoutInfo;
  308. TargetData::~TargetData() {
  309. if (LayoutInfo.isConstructed()) {
  310. // Remove any layouts for this TD.
  311. LayoutInfoTy &TheMap = *LayoutInfo;
  312. for (LayoutInfoTy::iterator I = TheMap.begin(), E = TheMap.end();
  313. I != E; ) {
  314. if (I->first.first == this) {
  315. I->second->~StructLayout();
  316. free(I->second);
  317. TheMap.erase(I++);
  318. } else {
  319. ++I;
  320. }
  321. }
  322. }
  323. }
  324. const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
  325. LayoutInfoTy &TheMap = *LayoutInfo;
  326. StructLayout *&SL = TheMap[LayoutKey(this, Ty)];
  327. if (SL) return SL;
  328. // Otherwise, create the struct layout. Because it is variable length, we
  329. // malloc it, then use placement new.
  330. int NumElts = Ty->getNumElements();
  331. StructLayout *L =
  332. (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1)*sizeof(uint64_t));
  333. // Set SL before calling StructLayout's ctor. The ctor could cause other
  334. // entries to be added to TheMap, invalidating our reference.
  335. SL = L;
  336. new (L) StructLayout(Ty, *this);
  337. return L;
  338. }
  339. /// InvalidateStructLayoutInfo - TargetData speculatively caches StructLayout
  340. /// objects. If a TargetData object is alive when types are being refined and
  341. /// removed, this method must be called whenever a StructType is removed to
  342. /// avoid a dangling pointer in this cache.
  343. void TargetData::InvalidateStructLayoutInfo(const StructType *Ty) const {
  344. if (!LayoutInfo.isConstructed()) return; // No cache.
  345. LayoutInfoTy::iterator I = LayoutInfo->find(LayoutKey(this, Ty));
  346. if (I != LayoutInfo->end()) {
  347. I->second->~StructLayout();
  348. free(I->second);
  349. LayoutInfo->erase(I);
  350. }
  351. }
  352. std::string TargetData::getStringRepresentation() const {
  353. std::string repr;
  354. repr.append(LittleEndian ? "e" : "E");
  355. repr.append("-p:").append(itostr((int64_t) (PointerMemSize * 8))).
  356. append(":").append(itostr((int64_t) (PointerABIAlign * 8))).
  357. append(":").append(itostr((int64_t) (PointerPrefAlign * 8)));
  358. for (align_const_iterator I = Alignments.begin();
  359. I != Alignments.end();
  360. ++I) {
  361. repr.append("-").append(1, (char) I->AlignType).
  362. append(utostr((int64_t) I->TypeBitWidth)).
  363. append(":").append(utostr((uint64_t) (I->ABIAlign * 8))).
  364. append(":").append(utostr((uint64_t) (I->PrefAlign * 8)));
  365. }
  366. return repr;
  367. }
  368. uint64_t TargetData::getTypeSizeInBits(const Type *Ty) const {
  369. assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
  370. switch (Ty->getTypeID()) {
  371. case Type::LabelTyID:
  372. case Type::PointerTyID:
  373. return getPointerSizeInBits();
  374. case Type::ArrayTyID: {
  375. const ArrayType *ATy = cast<ArrayType>(Ty);
  376. return getABITypeSizeInBits(ATy->getElementType())*ATy->getNumElements();
  377. }
  378. case Type::StructTyID: {
  379. // Get the layout annotation... which is lazily created on demand.
  380. const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
  381. return Layout->getSizeInBits();
  382. }
  383. case Type::IntegerTyID:
  384. return cast<IntegerType>(Ty)->getBitWidth();
  385. case Type::VoidTyID:
  386. return 8;
  387. case Type::FloatTyID:
  388. return 32;
  389. case Type::DoubleTyID:
  390. return 64;
  391. case Type::PPC_FP128TyID:
  392. case Type::FP128TyID:
  393. return 128;
  394. // In memory objects this is always aligned to a higher boundary, but
  395. // only 80 bits contain information.
  396. case Type::X86_FP80TyID:
  397. return 80;
  398. case Type::VectorTyID: {
  399. const VectorType *PTy = cast<VectorType>(Ty);
  400. return PTy->getBitWidth();
  401. }
  402. default:
  403. assert(0 && "TargetData::getTypeSizeInBits(): Unsupported type");
  404. break;
  405. }
  406. return 0;
  407. }
  408. /*!
  409. \param abi_or_pref Flag that determines which alignment is returned. true
  410. returns the ABI alignment, false returns the preferred alignment.
  411. \param Ty The underlying type for which alignment is determined.
  412. Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
  413. == false) for the requested type \a Ty.
  414. */
  415. unsigned char TargetData::getAlignment(const Type *Ty, bool abi_or_pref) const {
  416. int AlignType = -1;
  417. assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
  418. switch (Ty->getTypeID()) {
  419. /* Early escape for the non-numeric types */
  420. case Type::LabelTyID:
  421. case Type::PointerTyID:
  422. return (abi_or_pref
  423. ? getPointerABIAlignment()
  424. : getPointerPrefAlignment());
  425. case Type::ArrayTyID:
  426. return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
  427. case Type::StructTyID: {
  428. // Packed structure types always have an ABI alignment of one.
  429. if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
  430. return 1;
  431. // Get the layout annotation... which is lazily created on demand.
  432. const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
  433. unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
  434. return std::max(Align, (unsigned)Layout->getAlignment());
  435. }
  436. case Type::IntegerTyID:
  437. case Type::VoidTyID:
  438. AlignType = INTEGER_ALIGN;
  439. break;
  440. case Type::FloatTyID:
  441. case Type::DoubleTyID:
  442. // PPC_FP128TyID and FP128TyID have different data contents, but the
  443. // same size and alignment, so they look the same here.
  444. case Type::PPC_FP128TyID:
  445. case Type::FP128TyID:
  446. case Type::X86_FP80TyID:
  447. AlignType = FLOAT_ALIGN;
  448. break;
  449. case Type::VectorTyID:
  450. AlignType = VECTOR_ALIGN;
  451. break;
  452. default:
  453. assert(0 && "Bad type for getAlignment!!!");
  454. break;
  455. }
  456. return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
  457. abi_or_pref, Ty);
  458. }
  459. unsigned char TargetData::getABITypeAlignment(const Type *Ty) const {
  460. return getAlignment(Ty, true);
  461. }
  462. unsigned char TargetData::getCallFrameTypeAlignment(const Type *Ty) const {
  463. for (unsigned i = 0, e = Alignments.size(); i != e; ++i)
  464. if (Alignments[i].AlignType == STACK_ALIGN)
  465. return Alignments[i].ABIAlign;
  466. return getABITypeAlignment(Ty);
  467. }
  468. unsigned char TargetData::getPrefTypeAlignment(const Type *Ty) const {
  469. return getAlignment(Ty, false);
  470. }
  471. unsigned char TargetData::getPreferredTypeAlignmentShift(const Type *Ty) const {
  472. unsigned Align = (unsigned) getPrefTypeAlignment(Ty);
  473. assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
  474. return Log2_32(Align);
  475. }
  476. /// getIntPtrType - Return an unsigned integer type that is the same size or
  477. /// greater to the host pointer size.
  478. const Type *TargetData::getIntPtrType() const {
  479. return IntegerType::get(getPointerSizeInBits());
  480. }
  481. uint64_t TargetData::getIndexedOffset(const Type *ptrTy, Value* const* Indices,
  482. unsigned NumIndices) const {
  483. const Type *Ty = ptrTy;
  484. assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
  485. uint64_t Result = 0;
  486. generic_gep_type_iterator<Value* const*>
  487. TI = gep_type_begin(ptrTy, Indices, Indices+NumIndices);
  488. for (unsigned CurIDX = 0; CurIDX != NumIndices; ++CurIDX, ++TI) {
  489. if (const StructType *STy = dyn_cast<StructType>(*TI)) {
  490. assert(Indices[CurIDX]->getType() == Type::Int32Ty &&
  491. "Illegal struct idx");
  492. unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
  493. // Get structure layout information...
  494. const StructLayout *Layout = getStructLayout(STy);
  495. // Add in the offset, as calculated by the structure layout info...
  496. Result += Layout->getElementOffset(FieldNo);
  497. // Update Ty to refer to current element
  498. Ty = STy->getElementType(FieldNo);
  499. } else {
  500. // Update Ty to refer to current element
  501. Ty = cast<SequentialType>(Ty)->getElementType();
  502. // Get the array index and the size of each array element.
  503. int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue();
  504. Result += arrayIdx * (int64_t)getABITypeSize(Ty);
  505. }
  506. }
  507. return Result;
  508. }
  509. /// getPreferredAlignment - Return the preferred alignment of the specified
  510. /// global. This includes an explicitly requested alignment (if the global
  511. /// has one).
  512. unsigned TargetData::getPreferredAlignment(const GlobalVariable *GV) const {
  513. const Type *ElemType = GV->getType()->getElementType();
  514. unsigned Alignment = getPrefTypeAlignment(ElemType);
  515. if (GV->getAlignment() > Alignment)
  516. Alignment = GV->getAlignment();
  517. if (GV->hasInitializer()) {
  518. if (Alignment < 16) {
  519. // If the global is not external, see if it is large. If so, give it a
  520. // larger alignment.
  521. if (getTypeSizeInBits(ElemType) > 128)
  522. Alignment = 16; // 16-byte alignment.
  523. }
  524. }
  525. return Alignment;
  526. }
  527. /// getPreferredAlignmentLog - Return the preferred alignment of the
  528. /// specified global, returned in log form. This includes an explicitly
  529. /// requested alignment (if the global has one).
  530. unsigned TargetData::getPreferredAlignmentLog(const GlobalVariable *GV) const {
  531. return Log2_32(getPreferredAlignment(GV));
  532. }