DataLayout.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. //===- DataLayout.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 layout 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/IR/DataLayout.h"
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/ADT/Triple.h"
  22. #include "llvm/IR/Constants.h"
  23. #include "llvm/IR/DerivedTypes.h"
  24. #include "llvm/IR/GetElementPtrTypeIterator.h"
  25. #include "llvm/IR/GlobalVariable.h"
  26. #include "llvm/IR/Module.h"
  27. #include "llvm/IR/Type.h"
  28. #include "llvm/IR/Value.h"
  29. #include "llvm/Support/Casting.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/MathExtras.h"
  32. #include <algorithm>
  33. #include <cassert>
  34. #include <cstdint>
  35. #include <cstdlib>
  36. #include <tuple>
  37. #include <utility>
  38. using namespace llvm;
  39. //===----------------------------------------------------------------------===//
  40. // Support for StructLayout
  41. //===----------------------------------------------------------------------===//
  42. StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
  43. assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
  44. StructAlignment = 0;
  45. StructSize = 0;
  46. IsPadded = false;
  47. NumElements = ST->getNumElements();
  48. // Loop over each of the elements, placing them in memory.
  49. for (unsigned i = 0, e = NumElements; i != e; ++i) {
  50. Type *Ty = ST->getElementType(i);
  51. unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
  52. // Add padding if necessary to align the data element properly.
  53. if ((StructSize & (TyAlign-1)) != 0) {
  54. IsPadded = true;
  55. StructSize = alignTo(StructSize, TyAlign);
  56. }
  57. // Keep track of maximum alignment constraint.
  58. StructAlignment = std::max(TyAlign, StructAlignment);
  59. MemberOffsets[i] = StructSize;
  60. StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
  61. }
  62. // Empty structures have alignment of 1 byte.
  63. if (StructAlignment == 0) StructAlignment = 1;
  64. // Add padding to the end of the struct so that it could be put in an array
  65. // and all array elements would be aligned correctly.
  66. if ((StructSize & (StructAlignment-1)) != 0) {
  67. IsPadded = true;
  68. StructSize = alignTo(StructSize, StructAlignment);
  69. }
  70. }
  71. /// getElementContainingOffset - Given a valid offset into the structure,
  72. /// return the structure index that contains it.
  73. unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
  74. const uint64_t *SI =
  75. std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
  76. assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
  77. --SI;
  78. assert(*SI <= Offset && "upper_bound didn't work");
  79. assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
  80. (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
  81. "Upper bound didn't work!");
  82. // Multiple fields can have the same offset if any of them are zero sized.
  83. // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
  84. // at the i32 element, because it is the last element at that offset. This is
  85. // the right one to return, because anything after it will have a higher
  86. // offset, implying that this element is non-empty.
  87. return SI-&MemberOffsets[0];
  88. }
  89. //===----------------------------------------------------------------------===//
  90. // LayoutAlignElem, LayoutAlign support
  91. //===----------------------------------------------------------------------===//
  92. LayoutAlignElem
  93. LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
  94. unsigned pref_align, uint32_t bit_width) {
  95. assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
  96. LayoutAlignElem retval;
  97. retval.AlignType = align_type;
  98. retval.ABIAlign = abi_align;
  99. retval.PrefAlign = pref_align;
  100. retval.TypeBitWidth = bit_width;
  101. return retval;
  102. }
  103. bool
  104. LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
  105. return (AlignType == rhs.AlignType
  106. && ABIAlign == rhs.ABIAlign
  107. && PrefAlign == rhs.PrefAlign
  108. && TypeBitWidth == rhs.TypeBitWidth);
  109. }
  110. //===----------------------------------------------------------------------===//
  111. // PointerAlignElem, PointerAlign support
  112. //===----------------------------------------------------------------------===//
  113. PointerAlignElem
  114. PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
  115. unsigned PrefAlign, uint32_t TypeByteWidth,
  116. uint32_t IndexWidth) {
  117. assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
  118. PointerAlignElem retval;
  119. retval.AddressSpace = AddressSpace;
  120. retval.ABIAlign = ABIAlign;
  121. retval.PrefAlign = PrefAlign;
  122. retval.TypeByteWidth = TypeByteWidth;
  123. retval.IndexWidth = IndexWidth;
  124. return retval;
  125. }
  126. bool
  127. PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
  128. return (ABIAlign == rhs.ABIAlign
  129. && AddressSpace == rhs.AddressSpace
  130. && PrefAlign == rhs.PrefAlign
  131. && TypeByteWidth == rhs.TypeByteWidth
  132. && IndexWidth == rhs.IndexWidth);
  133. }
  134. //===----------------------------------------------------------------------===//
  135. // DataLayout Class Implementation
  136. //===----------------------------------------------------------------------===//
  137. const char *DataLayout::getManglingComponent(const Triple &T) {
  138. if (T.isOSBinFormatMachO())
  139. return "-m:o";
  140. if (T.isOSWindows() && T.isOSBinFormatCOFF())
  141. return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
  142. return "-m:e";
  143. }
  144. static const LayoutAlignElem DefaultAlignments[] = {
  145. { INTEGER_ALIGN, 1, 1, 1 }, // i1
  146. { INTEGER_ALIGN, 8, 1, 1 }, // i8
  147. { INTEGER_ALIGN, 16, 2, 2 }, // i16
  148. { INTEGER_ALIGN, 32, 4, 4 }, // i32
  149. { INTEGER_ALIGN, 64, 4, 8 }, // i64
  150. { FLOAT_ALIGN, 16, 2, 2 }, // half
  151. { FLOAT_ALIGN, 32, 4, 4 }, // float
  152. { FLOAT_ALIGN, 64, 8, 8 }, // double
  153. { FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ...
  154. { VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ...
  155. { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
  156. { AGGREGATE_ALIGN, 0, 0, 8 } // struct
  157. };
  158. void DataLayout::reset(StringRef Desc) {
  159. clear();
  160. LayoutMap = nullptr;
  161. BigEndian = false;
  162. AllocaAddrSpace = 0;
  163. StackNaturalAlign = 0;
  164. ProgramAddrSpace = 0;
  165. ManglingMode = MM_None;
  166. NonIntegralAddressSpaces.clear();
  167. // Default alignments
  168. for (const LayoutAlignElem &E : DefaultAlignments) {
  169. setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
  170. E.TypeBitWidth);
  171. }
  172. setPointerAlignment(0, 8, 8, 8, 8);
  173. parseSpecifier(Desc);
  174. }
  175. /// Checked version of split, to ensure mandatory subparts.
  176. static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
  177. assert(!Str.empty() && "parse error, string can't be empty here");
  178. std::pair<StringRef, StringRef> Split = Str.split(Separator);
  179. if (Split.second.empty() && Split.first != Str)
  180. report_fatal_error("Trailing separator in datalayout string");
  181. if (!Split.second.empty() && Split.first.empty())
  182. report_fatal_error("Expected token before separator in datalayout string");
  183. return Split;
  184. }
  185. /// Get an unsigned integer, including error checks.
  186. static unsigned getInt(StringRef R) {
  187. unsigned Result;
  188. bool error = R.getAsInteger(10, Result); (void)error;
  189. if (error)
  190. report_fatal_error("not a number, or does not fit in an unsigned int");
  191. return Result;
  192. }
  193. /// Convert bits into bytes. Assert if not a byte width multiple.
  194. static unsigned inBytes(unsigned Bits) {
  195. if (Bits % 8)
  196. report_fatal_error("number of bits must be a byte width multiple");
  197. return Bits / 8;
  198. }
  199. static unsigned getAddrSpace(StringRef R) {
  200. unsigned AddrSpace = getInt(R);
  201. if (!isUInt<24>(AddrSpace))
  202. report_fatal_error("Invalid address space, must be a 24-bit integer");
  203. return AddrSpace;
  204. }
  205. void DataLayout::parseSpecifier(StringRef Desc) {
  206. StringRepresentation = Desc;
  207. while (!Desc.empty()) {
  208. // Split at '-'.
  209. std::pair<StringRef, StringRef> Split = split(Desc, '-');
  210. Desc = Split.second;
  211. // Split at ':'.
  212. Split = split(Split.first, ':');
  213. // Aliases used below.
  214. StringRef &Tok = Split.first; // Current token.
  215. StringRef &Rest = Split.second; // The rest of the string.
  216. if (Tok == "ni") {
  217. do {
  218. Split = split(Rest, ':');
  219. Rest = Split.second;
  220. unsigned AS = getInt(Split.first);
  221. if (AS == 0)
  222. report_fatal_error("Address space 0 can never be non-integral");
  223. NonIntegralAddressSpaces.push_back(AS);
  224. } while (!Rest.empty());
  225. continue;
  226. }
  227. char Specifier = Tok.front();
  228. Tok = Tok.substr(1);
  229. switch (Specifier) {
  230. case 's':
  231. // Ignored for backward compatibility.
  232. // FIXME: remove this on LLVM 4.0.
  233. break;
  234. case 'E':
  235. BigEndian = true;
  236. break;
  237. case 'e':
  238. BigEndian = false;
  239. break;
  240. case 'p': {
  241. // Address space.
  242. unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
  243. if (!isUInt<24>(AddrSpace))
  244. report_fatal_error("Invalid address space, must be a 24bit integer");
  245. // Size.
  246. if (Rest.empty())
  247. report_fatal_error(
  248. "Missing size specification for pointer in datalayout string");
  249. Split = split(Rest, ':');
  250. unsigned PointerMemSize = inBytes(getInt(Tok));
  251. if (!PointerMemSize)
  252. report_fatal_error("Invalid pointer size of 0 bytes");
  253. // ABI alignment.
  254. if (Rest.empty())
  255. report_fatal_error(
  256. "Missing alignment specification for pointer in datalayout string");
  257. Split = split(Rest, ':');
  258. unsigned PointerABIAlign = inBytes(getInt(Tok));
  259. if (!isPowerOf2_64(PointerABIAlign))
  260. report_fatal_error(
  261. "Pointer ABI alignment must be a power of 2");
  262. // Size of index used in GEP for address calculation.
  263. // The parameter is optional. By default it is equal to size of pointer.
  264. unsigned IndexSize = PointerMemSize;
  265. // Preferred alignment.
  266. unsigned PointerPrefAlign = PointerABIAlign;
  267. if (!Rest.empty()) {
  268. Split = split(Rest, ':');
  269. PointerPrefAlign = inBytes(getInt(Tok));
  270. if (!isPowerOf2_64(PointerPrefAlign))
  271. report_fatal_error(
  272. "Pointer preferred alignment must be a power of 2");
  273. // Now read the index. It is the second optional parameter here.
  274. if (!Rest.empty()) {
  275. Split = split(Rest, ':');
  276. IndexSize = inBytes(getInt(Tok));
  277. if (!IndexSize)
  278. report_fatal_error("Invalid index size of 0 bytes");
  279. }
  280. }
  281. setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
  282. PointerMemSize, IndexSize);
  283. break;
  284. }
  285. case 'i':
  286. case 'v':
  287. case 'f':
  288. case 'a': {
  289. AlignTypeEnum AlignType;
  290. switch (Specifier) {
  291. default: llvm_unreachable("Unexpected specifier!");
  292. case 'i': AlignType = INTEGER_ALIGN; break;
  293. case 'v': AlignType = VECTOR_ALIGN; break;
  294. case 'f': AlignType = FLOAT_ALIGN; break;
  295. case 'a': AlignType = AGGREGATE_ALIGN; break;
  296. }
  297. // Bit size.
  298. unsigned Size = Tok.empty() ? 0 : getInt(Tok);
  299. if (AlignType == AGGREGATE_ALIGN && Size != 0)
  300. report_fatal_error(
  301. "Sized aggregate specification in datalayout string");
  302. // ABI alignment.
  303. if (Rest.empty())
  304. report_fatal_error(
  305. "Missing alignment specification in datalayout string");
  306. Split = split(Rest, ':');
  307. unsigned ABIAlign = inBytes(getInt(Tok));
  308. if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
  309. report_fatal_error(
  310. "ABI alignment specification must be >0 for non-aggregate types");
  311. // Preferred alignment.
  312. unsigned PrefAlign = ABIAlign;
  313. if (!Rest.empty()) {
  314. Split = split(Rest, ':');
  315. PrefAlign = inBytes(getInt(Tok));
  316. }
  317. setAlignment(AlignType, ABIAlign, PrefAlign, Size);
  318. break;
  319. }
  320. case 'n': // Native integer types.
  321. while (true) {
  322. unsigned Width = getInt(Tok);
  323. if (Width == 0)
  324. report_fatal_error(
  325. "Zero width native integer type in datalayout string");
  326. LegalIntWidths.push_back(Width);
  327. if (Rest.empty())
  328. break;
  329. Split = split(Rest, ':');
  330. }
  331. break;
  332. case 'S': { // Stack natural alignment.
  333. StackNaturalAlign = inBytes(getInt(Tok));
  334. break;
  335. }
  336. case 'P': { // Function address space.
  337. ProgramAddrSpace = getAddrSpace(Tok);
  338. break;
  339. }
  340. case 'A': { // Default stack/alloca address space.
  341. AllocaAddrSpace = getAddrSpace(Tok);
  342. break;
  343. }
  344. case 'm':
  345. if (!Tok.empty())
  346. report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
  347. if (Rest.empty())
  348. report_fatal_error("Expected mangling specifier in datalayout string");
  349. if (Rest.size() > 1)
  350. report_fatal_error("Unknown mangling specifier in datalayout string");
  351. switch(Rest[0]) {
  352. default:
  353. report_fatal_error("Unknown mangling in datalayout string");
  354. case 'e':
  355. ManglingMode = MM_ELF;
  356. break;
  357. case 'o':
  358. ManglingMode = MM_MachO;
  359. break;
  360. case 'm':
  361. ManglingMode = MM_Mips;
  362. break;
  363. case 'w':
  364. ManglingMode = MM_WinCOFF;
  365. break;
  366. case 'x':
  367. ManglingMode = MM_WinCOFFX86;
  368. break;
  369. }
  370. break;
  371. default:
  372. report_fatal_error("Unknown specifier in datalayout string");
  373. break;
  374. }
  375. }
  376. }
  377. DataLayout::DataLayout(const Module *M) {
  378. init(M);
  379. }
  380. void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
  381. bool DataLayout::operator==(const DataLayout &Other) const {
  382. bool Ret = BigEndian == Other.BigEndian &&
  383. AllocaAddrSpace == Other.AllocaAddrSpace &&
  384. StackNaturalAlign == Other.StackNaturalAlign &&
  385. ProgramAddrSpace == Other.ProgramAddrSpace &&
  386. ManglingMode == Other.ManglingMode &&
  387. LegalIntWidths == Other.LegalIntWidths &&
  388. Alignments == Other.Alignments && Pointers == Other.Pointers;
  389. // Note: getStringRepresentation() might differs, it is not canonicalized
  390. return Ret;
  391. }
  392. DataLayout::AlignmentsTy::iterator
  393. DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
  394. uint32_t BitWidth) {
  395. auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
  396. return std::lower_bound(Alignments.begin(), Alignments.end(), Pair,
  397. [](const LayoutAlignElem &LHS,
  398. const std::pair<unsigned, uint32_t> &RHS) {
  399. return std::tie(LHS.AlignType, LHS.TypeBitWidth) <
  400. std::tie(RHS.first, RHS.second);
  401. });
  402. }
  403. void
  404. DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
  405. unsigned pref_align, uint32_t bit_width) {
  406. if (!isUInt<24>(bit_width))
  407. report_fatal_error("Invalid bit width, must be a 24bit integer");
  408. if (!isUInt<16>(abi_align))
  409. report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
  410. if (!isUInt<16>(pref_align))
  411. report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
  412. if (abi_align != 0 && !isPowerOf2_64(abi_align))
  413. report_fatal_error("Invalid ABI alignment, must be a power of 2");
  414. if (pref_align != 0 && !isPowerOf2_64(pref_align))
  415. report_fatal_error("Invalid preferred alignment, must be a power of 2");
  416. if (pref_align < abi_align)
  417. report_fatal_error(
  418. "Preferred alignment cannot be less than the ABI alignment");
  419. AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width);
  420. if (I != Alignments.end() &&
  421. I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) {
  422. // Update the abi, preferred alignments.
  423. I->ABIAlign = abi_align;
  424. I->PrefAlign = pref_align;
  425. } else {
  426. // Insert before I to keep the vector sorted.
  427. Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align,
  428. pref_align, bit_width));
  429. }
  430. }
  431. DataLayout::PointersTy::iterator
  432. DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
  433. return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
  434. [](const PointerAlignElem &A, uint32_t AddressSpace) {
  435. return A.AddressSpace < AddressSpace;
  436. });
  437. }
  438. void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
  439. unsigned PrefAlign, uint32_t TypeByteWidth,
  440. uint32_t IndexWidth) {
  441. if (PrefAlign < ABIAlign)
  442. report_fatal_error(
  443. "Preferred alignment cannot be less than the ABI alignment");
  444. PointersTy::iterator I = findPointerLowerBound(AddrSpace);
  445. if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
  446. Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
  447. TypeByteWidth, IndexWidth));
  448. } else {
  449. I->ABIAlign = ABIAlign;
  450. I->PrefAlign = PrefAlign;
  451. I->TypeByteWidth = TypeByteWidth;
  452. I->IndexWidth = IndexWidth;
  453. }
  454. }
  455. /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
  456. /// preferred if ABIInfo = false) the layout wants for the specified datatype.
  457. unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
  458. uint32_t BitWidth, bool ABIInfo,
  459. Type *Ty) const {
  460. AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth);
  461. // See if we found an exact match. Of if we are looking for an integer type,
  462. // but don't have an exact match take the next largest integer. This is where
  463. // the lower_bound will point to when it fails an exact match.
  464. if (I != Alignments.end() && I->AlignType == (unsigned)AlignType &&
  465. (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN))
  466. return ABIInfo ? I->ABIAlign : I->PrefAlign;
  467. if (AlignType == INTEGER_ALIGN) {
  468. // If we didn't have a larger value try the largest value we have.
  469. if (I != Alignments.begin()) {
  470. --I; // Go to the previous entry and see if its an integer.
  471. if (I->AlignType == INTEGER_ALIGN)
  472. return ABIInfo ? I->ABIAlign : I->PrefAlign;
  473. }
  474. } else if (AlignType == VECTOR_ALIGN) {
  475. // By default, use natural alignment for vector types. This is consistent
  476. // with what clang and llvm-gcc do.
  477. unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
  478. Align *= cast<VectorType>(Ty)->getNumElements();
  479. Align = PowerOf2Ceil(Align);
  480. return Align;
  481. }
  482. // If we still couldn't find a reasonable default alignment, fall back
  483. // to a simple heuristic that the alignment is the first power of two
  484. // greater-or-equal to the store size of the type. This is a reasonable
  485. // approximation of reality, and if the user wanted something less
  486. // less conservative, they should have specified it explicitly in the data
  487. // layout.
  488. unsigned Align = getTypeStoreSize(Ty);
  489. Align = PowerOf2Ceil(Align);
  490. return Align;
  491. }
  492. namespace {
  493. class StructLayoutMap {
  494. using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
  495. LayoutInfoTy LayoutInfo;
  496. public:
  497. ~StructLayoutMap() {
  498. // Remove any layouts.
  499. for (const auto &I : LayoutInfo) {
  500. StructLayout *Value = I.second;
  501. Value->~StructLayout();
  502. free(Value);
  503. }
  504. }
  505. StructLayout *&operator[](StructType *STy) {
  506. return LayoutInfo[STy];
  507. }
  508. };
  509. } // end anonymous namespace
  510. void DataLayout::clear() {
  511. LegalIntWidths.clear();
  512. Alignments.clear();
  513. Pointers.clear();
  514. delete static_cast<StructLayoutMap *>(LayoutMap);
  515. LayoutMap = nullptr;
  516. }
  517. DataLayout::~DataLayout() {
  518. clear();
  519. }
  520. const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
  521. if (!LayoutMap)
  522. LayoutMap = new StructLayoutMap();
  523. StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
  524. StructLayout *&SL = (*STM)[Ty];
  525. if (SL) return SL;
  526. // Otherwise, create the struct layout. Because it is variable length, we
  527. // malloc it, then use placement new.
  528. int NumElts = Ty->getNumElements();
  529. StructLayout *L = (StructLayout *)
  530. safe_malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
  531. // Set SL before calling StructLayout's ctor. The ctor could cause other
  532. // entries to be added to TheMap, invalidating our reference.
  533. SL = L;
  534. new (L) StructLayout(Ty, *this);
  535. return L;
  536. }
  537. unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
  538. PointersTy::const_iterator I = findPointerLowerBound(AS);
  539. if (I == Pointers.end() || I->AddressSpace != AS) {
  540. I = findPointerLowerBound(0);
  541. assert(I->AddressSpace == 0);
  542. }
  543. return I->ABIAlign;
  544. }
  545. unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
  546. PointersTy::const_iterator I = findPointerLowerBound(AS);
  547. if (I == Pointers.end() || I->AddressSpace != AS) {
  548. I = findPointerLowerBound(0);
  549. assert(I->AddressSpace == 0);
  550. }
  551. return I->PrefAlign;
  552. }
  553. unsigned DataLayout::getPointerSize(unsigned AS) const {
  554. PointersTy::const_iterator I = findPointerLowerBound(AS);
  555. if (I == Pointers.end() || I->AddressSpace != AS) {
  556. I = findPointerLowerBound(0);
  557. assert(I->AddressSpace == 0);
  558. }
  559. return I->TypeByteWidth;
  560. }
  561. unsigned DataLayout::getMaxPointerSize() const {
  562. unsigned MaxPointerSize = 0;
  563. for (auto &P : Pointers)
  564. MaxPointerSize = std::max(MaxPointerSize, P.TypeByteWidth);
  565. return MaxPointerSize;
  566. }
  567. unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
  568. assert(Ty->isPtrOrPtrVectorTy() &&
  569. "This should only be called with a pointer or pointer vector type");
  570. Ty = Ty->getScalarType();
  571. return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
  572. }
  573. unsigned DataLayout::getIndexSize(unsigned AS) const {
  574. PointersTy::const_iterator I = findPointerLowerBound(AS);
  575. if (I == Pointers.end() || I->AddressSpace != AS) {
  576. I = findPointerLowerBound(0);
  577. assert(I->AddressSpace == 0);
  578. }
  579. return I->IndexWidth;
  580. }
  581. unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
  582. assert(Ty->isPtrOrPtrVectorTy() &&
  583. "This should only be called with a pointer or pointer vector type");
  584. Ty = Ty->getScalarType();
  585. return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
  586. }
  587. /*!
  588. \param abi_or_pref Flag that determines which alignment is returned. true
  589. returns the ABI alignment, false returns the preferred alignment.
  590. \param Ty The underlying type for which alignment is determined.
  591. Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
  592. == false) for the requested type \a Ty.
  593. */
  594. unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
  595. AlignTypeEnum AlignType;
  596. assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
  597. switch (Ty->getTypeID()) {
  598. // Early escape for the non-numeric types.
  599. case Type::LabelTyID:
  600. return (abi_or_pref
  601. ? getPointerABIAlignment(0)
  602. : getPointerPrefAlignment(0));
  603. case Type::PointerTyID: {
  604. unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
  605. return (abi_or_pref
  606. ? getPointerABIAlignment(AS)
  607. : getPointerPrefAlignment(AS));
  608. }
  609. case Type::ArrayTyID:
  610. return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
  611. case Type::StructTyID: {
  612. // Packed structure types always have an ABI alignment of one.
  613. if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
  614. return 1;
  615. // Get the layout annotation... which is lazily created on demand.
  616. const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
  617. unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
  618. return std::max(Align, Layout->getAlignment());
  619. }
  620. case Type::IntegerTyID:
  621. AlignType = INTEGER_ALIGN;
  622. break;
  623. case Type::HalfTyID:
  624. case Type::FloatTyID:
  625. case Type::DoubleTyID:
  626. // PPC_FP128TyID and FP128TyID have different data contents, but the
  627. // same size and alignment, so they look the same here.
  628. case Type::PPC_FP128TyID:
  629. case Type::FP128TyID:
  630. case Type::X86_FP80TyID:
  631. AlignType = FLOAT_ALIGN;
  632. break;
  633. case Type::X86_MMXTyID:
  634. case Type::VectorTyID:
  635. AlignType = VECTOR_ALIGN;
  636. break;
  637. default:
  638. llvm_unreachable("Bad type for getAlignment!!!");
  639. }
  640. return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty), abi_or_pref, Ty);
  641. }
  642. unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
  643. return getAlignment(Ty, true);
  644. }
  645. /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
  646. /// an integer type of the specified bitwidth.
  647. unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
  648. return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
  649. }
  650. unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
  651. return getAlignment(Ty, false);
  652. }
  653. unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
  654. unsigned Align = getPrefTypeAlignment(Ty);
  655. assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
  656. return Log2_32(Align);
  657. }
  658. IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
  659. unsigned AddressSpace) const {
  660. return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
  661. }
  662. Type *DataLayout::getIntPtrType(Type *Ty) const {
  663. assert(Ty->isPtrOrPtrVectorTy() &&
  664. "Expected a pointer or pointer vector type.");
  665. unsigned NumBits = getIndexTypeSizeInBits(Ty);
  666. IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
  667. if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
  668. return VectorType::get(IntTy, VecTy->getNumElements());
  669. return IntTy;
  670. }
  671. Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
  672. for (unsigned LegalIntWidth : LegalIntWidths)
  673. if (Width <= LegalIntWidth)
  674. return Type::getIntNTy(C, LegalIntWidth);
  675. return nullptr;
  676. }
  677. unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
  678. auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
  679. return Max != LegalIntWidths.end() ? *Max : 0;
  680. }
  681. Type *DataLayout::getIndexType(Type *Ty) const {
  682. assert(Ty->isPtrOrPtrVectorTy() &&
  683. "Expected a pointer or pointer vector type.");
  684. unsigned NumBits = getIndexTypeSizeInBits(Ty);
  685. IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
  686. if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
  687. return VectorType::get(IntTy, VecTy->getNumElements());
  688. return IntTy;
  689. }
  690. int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
  691. ArrayRef<Value *> Indices) const {
  692. int64_t Result = 0;
  693. generic_gep_type_iterator<Value* const*>
  694. GTI = gep_type_begin(ElemTy, Indices),
  695. GTE = gep_type_end(ElemTy, Indices);
  696. for (; GTI != GTE; ++GTI) {
  697. Value *Idx = GTI.getOperand();
  698. if (StructType *STy = GTI.getStructTypeOrNull()) {
  699. assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
  700. unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
  701. // Get structure layout information...
  702. const StructLayout *Layout = getStructLayout(STy);
  703. // Add in the offset, as calculated by the structure layout info...
  704. Result += Layout->getElementOffset(FieldNo);
  705. } else {
  706. // Get the array index and the size of each array element.
  707. if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
  708. Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
  709. }
  710. }
  711. return Result;
  712. }
  713. /// getPreferredAlignment - Return the preferred alignment of the specified
  714. /// global. This includes an explicitly requested alignment (if the global
  715. /// has one).
  716. unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
  717. unsigned GVAlignment = GV->getAlignment();
  718. // If a section is specified, always precisely honor explicit alignment,
  719. // so we don't insert padding into a section we don't control.
  720. if (GVAlignment && GV->hasSection())
  721. return GVAlignment;
  722. // If no explicit alignment is specified, compute the alignment based on
  723. // the IR type. If an alignment is specified, increase it to match the ABI
  724. // alignment of the IR type.
  725. //
  726. // FIXME: Not sure it makes sense to use the alignment of the type if
  727. // there's already an explicit alignment specification.
  728. Type *ElemType = GV->getValueType();
  729. unsigned Alignment = getPrefTypeAlignment(ElemType);
  730. if (GVAlignment >= Alignment) {
  731. Alignment = GVAlignment;
  732. } else if (GVAlignment != 0) {
  733. Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
  734. }
  735. // If no explicit alignment is specified, and the global is large, increase
  736. // the alignment to 16.
  737. // FIXME: Why 16, specifically?
  738. if (GV->hasInitializer() && GVAlignment == 0) {
  739. if (Alignment < 16) {
  740. // If the global is not external, see if it is large. If so, give it a
  741. // larger alignment.
  742. if (getTypeSizeInBits(ElemType) > 128)
  743. Alignment = 16; // 16-byte alignment.
  744. }
  745. }
  746. return Alignment;
  747. }
  748. /// getPreferredAlignmentLog - Return the preferred alignment of the
  749. /// specified global, returned in log form. This includes an explicitly
  750. /// requested alignment (if the global has one).
  751. unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
  752. return Log2_32(getPreferredAlignment(GV));
  753. }