blob: 63c24b5ee7af23b7ab1270c8f87ef5598a524528 [file] [log] [blame]
Eugene Zelenkoe5dc33d2017-05-05 22:30:37 +00001//===- DataLayout.cpp - Data size & alignment routines ---------------------==//
Micah Villmowe18c2ae2012-10-04 22:08:14 +00002//
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//
Micah Villmow99b11482012-10-04 23:01:22 +000010// This file defines layout properties related to datatype size/offset/alignment
Micah Villmowe18c2ae2012-10-04 22:08:14 +000011// 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
Chandler Carruthe3e43d92017-06-06 11:49:48 +000019#include "llvm/IR/DataLayout.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/ADT/DenseMap.h"
Eugene Zelenkoe5dc33d2017-05-05 22:30:37 +000021#include "llvm/ADT/StringRef.h"
Rafael Espindola8e0f67d2014-01-03 19:21:54 +000022#include "llvm/ADT/Triple.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/DerivedTypes.h"
Chandler Carruthbd7cba02014-03-04 10:40:04 +000025#include "llvm/IR/GetElementPtrTypeIterator.h"
Eugene Zelenkoe5dc33d2017-05-05 22:30:37 +000026#include "llvm/IR/GlobalVariable.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
Eugene Zelenkoe5dc33d2017-05-05 22:30:37 +000028#include "llvm/IR/Type.h"
29#include "llvm/IR/Value.h"
30#include "llvm/Support/Casting.h"
Micah Villmowe18c2ae2012-10-04 22:08:14 +000031#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000032#include "llvm/Support/MathExtras.h"
Micah Villmowe18c2ae2012-10-04 22:08:14 +000033#include <algorithm>
Eugene Zelenkoe5dc33d2017-05-05 22:30:37 +000034#include <cassert>
35#include <cstdint>
Micah Villmowe18c2ae2012-10-04 22:08:14 +000036#include <cstdlib>
Eugene Zelenkoe5dc33d2017-05-05 22:30:37 +000037#include <tuple>
38#include <utility>
39
Micah Villmowe18c2ae2012-10-04 22:08:14 +000040using namespace llvm;
41
Micah Villmowe18c2ae2012-10-04 22:08:14 +000042//===----------------------------------------------------------------------===//
43// Support for StructLayout
44//===----------------------------------------------------------------------===//
45
Pete Cooper80ec0f82015-07-27 17:15:28 +000046StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
Micah Villmowe18c2ae2012-10-04 22:08:14 +000047 assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
48 StructAlignment = 0;
49 StructSize = 0;
Mehdi Amini257fee72015-12-15 01:44:07 +000050 IsPadded = false;
Micah Villmowe18c2ae2012-10-04 22:08:14 +000051 NumElements = ST->getNumElements();
52
53 // Loop over each of the elements, placing them in memory.
54 for (unsigned i = 0, e = NumElements; i != e; ++i) {
55 Type *Ty = ST->getElementType(i);
Eli Bendersky6b51f752013-04-16 15:41:18 +000056 unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
Micah Villmowe18c2ae2012-10-04 22:08:14 +000057
58 // Add padding if necessary to align the data element properly.
Mehdi Amini257fee72015-12-15 01:44:07 +000059 if ((StructSize & (TyAlign-1)) != 0) {
60 IsPadded = true;
Rui Ueyama3edb0ec2016-01-14 21:06:47 +000061 StructSize = alignTo(StructSize, TyAlign);
Mehdi Amini257fee72015-12-15 01:44:07 +000062 }
Micah Villmowe18c2ae2012-10-04 22:08:14 +000063
64 // Keep track of maximum alignment constraint.
65 StructAlignment = std::max(TyAlign, StructAlignment);
66
67 MemberOffsets[i] = StructSize;
Eli Bendersky6b51f752013-04-16 15:41:18 +000068 StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
Micah Villmowe18c2ae2012-10-04 22:08:14 +000069 }
70
71 // Empty structures have alignment of 1 byte.
72 if (StructAlignment == 0) StructAlignment = 1;
73
74 // Add padding to the end of the struct so that it could be put in an array
75 // and all array elements would be aligned correctly.
Mehdi Amini257fee72015-12-15 01:44:07 +000076 if ((StructSize & (StructAlignment-1)) != 0) {
77 IsPadded = true;
Rui Ueyama3edb0ec2016-01-14 21:06:47 +000078 StructSize = alignTo(StructSize, StructAlignment);
Mehdi Amini257fee72015-12-15 01:44:07 +000079 }
Micah Villmowe18c2ae2012-10-04 22:08:14 +000080}
81
Micah Villmowe18c2ae2012-10-04 22:08:14 +000082/// getElementContainingOffset - Given a valid offset into the structure,
83/// return the structure index that contains it.
84unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
85 const uint64_t *SI =
86 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
87 assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
88 --SI;
89 assert(*SI <= Offset && "upper_bound didn't work");
90 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
91 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
92 "Upper bound didn't work!");
93
94 // Multiple fields can have the same offset if any of them are zero sized.
95 // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
96 // at the i32 element, because it is the last element at that offset. This is
97 // the right one to return, because anything after it will have a higher
98 // offset, implying that this element is non-empty.
99 return SI-&MemberOffsets[0];
100}
101
102//===----------------------------------------------------------------------===//
Micah Villmow99b11482012-10-04 23:01:22 +0000103// LayoutAlignElem, LayoutAlign support
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000104//===----------------------------------------------------------------------===//
105
Micah Villmow99b11482012-10-04 23:01:22 +0000106LayoutAlignElem
107LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000108 unsigned pref_align, uint32_t bit_width) {
109 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
Micah Villmow99b11482012-10-04 23:01:22 +0000110 LayoutAlignElem retval;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000111 retval.AlignType = align_type;
112 retval.ABIAlign = abi_align;
113 retval.PrefAlign = pref_align;
114 retval.TypeBitWidth = bit_width;
115 return retval;
116}
117
118bool
Micah Villmow99b11482012-10-04 23:01:22 +0000119LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000120 return (AlignType == rhs.AlignType
121 && ABIAlign == rhs.ABIAlign
122 && PrefAlign == rhs.PrefAlign
123 && TypeBitWidth == rhs.TypeBitWidth);
124}
125
Micah Villmow7d661462012-10-09 16:06:12 +0000126//===----------------------------------------------------------------------===//
127// PointerAlignElem, PointerAlign support
128//===----------------------------------------------------------------------===//
129
130PointerAlignElem
Rafael Espindola5e7d2412013-12-13 23:15:20 +0000131PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000132 unsigned PrefAlign, uint32_t TypeByteWidth,
133 uint32_t IndexWidth) {
Rafael Espindola5e7d2412013-12-13 23:15:20 +0000134 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
Micah Villmow7d661462012-10-09 16:06:12 +0000135 PointerAlignElem retval;
Rafael Espindola5e7d2412013-12-13 23:15:20 +0000136 retval.AddressSpace = AddressSpace;
137 retval.ABIAlign = ABIAlign;
138 retval.PrefAlign = PrefAlign;
139 retval.TypeByteWidth = TypeByteWidth;
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000140 retval.IndexWidth = IndexWidth;
Micah Villmow7d661462012-10-09 16:06:12 +0000141 return retval;
142}
143
144bool
145PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
146 return (ABIAlign == rhs.ABIAlign
147 && AddressSpace == rhs.AddressSpace
148 && PrefAlign == rhs.PrefAlign
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000149 && TypeByteWidth == rhs.TypeByteWidth
150 && IndexWidth == rhs.IndexWidth);
Micah Villmow7d661462012-10-09 16:06:12 +0000151}
152
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000153//===----------------------------------------------------------------------===//
Micah Villmow99b11482012-10-04 23:01:22 +0000154// DataLayout Class Implementation
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000155//===----------------------------------------------------------------------===//
156
Rafael Espindola8e0f67d2014-01-03 19:21:54 +0000157const char *DataLayout::getManglingComponent(const Triple &T) {
158 if (T.isOSBinFormatMachO())
159 return "-m:o";
David Majnemer7605cdd2015-03-17 23:54:51 +0000160 if (T.isOSWindows() && T.isOSBinFormatCOFF())
161 return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
Saleem Abdulrasool396e5e32014-04-02 20:32:05 +0000162 return "-m:e";
Rafael Espindola8e0f67d2014-01-03 19:21:54 +0000163}
164
Rafael Espindola2c6e51f2013-12-20 15:21:32 +0000165static const LayoutAlignElem DefaultAlignments[] = {
Rafael Espindolab9df9e62013-12-19 23:03:03 +0000166 { INTEGER_ALIGN, 1, 1, 1 }, // i1
167 { INTEGER_ALIGN, 8, 1, 1 }, // i8
168 { INTEGER_ALIGN, 16, 2, 2 }, // i16
169 { INTEGER_ALIGN, 32, 4, 4 }, // i32
170 { INTEGER_ALIGN, 64, 4, 8 }, // i64
171 { FLOAT_ALIGN, 16, 2, 2 }, // half
172 { FLOAT_ALIGN, 32, 4, 4 }, // float
173 { FLOAT_ALIGN, 64, 8, 8 }, // double
174 { FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ...
175 { VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ...
176 { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
177 { AGGREGATE_ALIGN, 0, 0, 8 } // struct
178};
179
Rafael Espindola3f0a9af2014-02-25 22:23:04 +0000180void DataLayout::reset(StringRef Desc) {
181 clear();
182
Craig Topperec0f0bc2014-04-09 06:08:46 +0000183 LayoutMap = nullptr;
Chandler Carruth34b45cd2014-10-20 10:41:29 +0000184 BigEndian = false;
Matt Arsenaulte0b3c332017-04-10 22:27:50 +0000185 AllocaAddrSpace = 0;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000186 StackNaturalAlign = 0;
Dylan McKaya78fde82018-02-19 09:56:22 +0000187 ProgramAddrSpace = 0;
Rafael Espindola8e0f67d2014-01-03 19:21:54 +0000188 ManglingMode = MM_None;
Sanjoy Das1c20b712016-07-28 23:43:38 +0000189 NonIntegralAddressSpaces.clear();
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000190
191 // Default alignments
Benjamin Kramer6e9eeab2014-03-10 15:03:06 +0000192 for (const LayoutAlignElem &E : DefaultAlignments) {
Rafael Espindolab9df9e62013-12-19 23:03:03 +0000193 setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
194 E.TypeBitWidth);
195 }
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000196 setPointerAlignment(0, 8, 8, 8, 8);
Patrik Hägglund1e656762012-11-14 09:04:56 +0000197
Patrik Hagglund58b45532012-11-30 10:06:59 +0000198 parseSpecifier(Desc);
199}
200
201/// Checked version of split, to ensure mandatory subparts.
202static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
203 assert(!Str.empty() && "parse error, string can't be empty here");
204 std::pair<StringRef, StringRef> Split = Str.split(Separator);
David Majnemerbf139272014-12-10 01:38:28 +0000205 if (Split.second.empty() && Split.first != Str)
206 report_fatal_error("Trailing separator in datalayout string");
David Majnemerfda17192014-12-10 02:36:41 +0000207 if (!Split.second.empty() && Split.first.empty())
208 report_fatal_error("Expected token before separator in datalayout string");
Patrik Hagglund58b45532012-11-30 10:06:59 +0000209 return Split;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000210}
211
Cameron McInally67cc1dc2014-01-07 19:51:38 +0000212/// Get an unsigned integer, including error checks.
Patrik Hägglund37d5be72012-11-28 12:13:12 +0000213static unsigned getInt(StringRef R) {
Cameron McInally486fdf22014-01-13 22:04:55 +0000214 unsigned Result;
Patrik Hägglunddacb9a52012-11-28 14:32:52 +0000215 bool error = R.getAsInteger(10, Result); (void)error;
Cameron McInally486fdf22014-01-13 22:04:55 +0000216 if (error)
217 report_fatal_error("not a number, or does not fit in an unsigned int");
Patrik Hägglund37d5be72012-11-28 12:13:12 +0000218 return Result;
219}
220
Patrik Hagglund58b45532012-11-30 10:06:59 +0000221/// Convert bits into bytes. Assert if not a byte width multiple.
222static unsigned inBytes(unsigned Bits) {
David Majnemerbf139272014-12-10 01:38:28 +0000223 if (Bits % 8)
224 report_fatal_error("number of bits must be a byte width multiple");
Patrik Hagglund58b45532012-11-30 10:06:59 +0000225 return Bits / 8;
226}
227
Dylan McKaya78fde82018-02-19 09:56:22 +0000228static unsigned getAddrSpace(StringRef R) {
229 unsigned AddrSpace = getInt(R);
230 if (!isUInt<24>(AddrSpace))
231 report_fatal_error("Invalid address space, must be a 24-bit integer");
232 return AddrSpace;
233}
234
Patrik Hagglund58b45532012-11-30 10:06:59 +0000235void DataLayout::parseSpecifier(StringRef Desc) {
Mehdi Aminic94da202015-03-04 18:43:29 +0000236 StringRepresentation = Desc;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000237 while (!Desc.empty()) {
Patrik Hagglund58b45532012-11-30 10:06:59 +0000238 // Split at '-'.
239 std::pair<StringRef, StringRef> Split = split(Desc, '-');
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000240 Desc = Split.second;
241
Patrik Hagglund58b45532012-11-30 10:06:59 +0000242 // Split at ':'.
243 Split = split(Split.first, ':');
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000244
Patrik Hagglund58b45532012-11-30 10:06:59 +0000245 // Aliases used below.
246 StringRef &Tok = Split.first; // Current token.
247 StringRef &Rest = Split.second; // The rest of the string.
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000248
Sanjoy Das1c20b712016-07-28 23:43:38 +0000249 if (Tok == "ni") {
250 do {
251 Split = split(Rest, ':');
252 Rest = Split.second;
253 unsigned AS = getInt(Split.first);
254 if (AS == 0)
255 report_fatal_error("Address space 0 can never be non-integral");
256 NonIntegralAddressSpaces.push_back(AS);
257 } while (!Rest.empty());
258
259 continue;
260 }
261
Patrik Hagglund58b45532012-11-30 10:06:59 +0000262 char Specifier = Tok.front();
263 Tok = Tok.substr(1);
264
265 switch (Specifier) {
Rafael Espindola33cc3f82014-01-01 22:29:43 +0000266 case 's':
267 // Ignored for backward compatibility.
268 // FIXME: remove this on LLVM 4.0.
269 break;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000270 case 'E':
Chandler Carruth34b45cd2014-10-20 10:41:29 +0000271 BigEndian = true;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000272 break;
273 case 'e':
Chandler Carruth34b45cd2014-10-20 10:41:29 +0000274 BigEndian = false;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000275 break;
276 case 'p': {
Patrik Hagglund58b45532012-11-30 10:06:59 +0000277 // Address space.
278 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
David Majnemeref6e5492014-12-10 01:17:08 +0000279 if (!isUInt<24>(AddrSpace))
280 report_fatal_error("Invalid address space, must be a 24bit integer");
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000281
Patrik Hagglund58b45532012-11-30 10:06:59 +0000282 // Size.
David Majnemerbf139272014-12-10 01:38:28 +0000283 if (Rest.empty())
284 report_fatal_error(
285 "Missing size specification for pointer in datalayout string");
Patrik Hagglund58b45532012-11-30 10:06:59 +0000286 Split = split(Rest, ':');
287 unsigned PointerMemSize = inBytes(getInt(Tok));
Owen Andersonf2128562015-03-02 06:00:02 +0000288 if (!PointerMemSize)
289 report_fatal_error("Invalid pointer size of 0 bytes");
Patrik Hagglund58b45532012-11-30 10:06:59 +0000290
291 // ABI alignment.
David Majnemerbf139272014-12-10 01:38:28 +0000292 if (Rest.empty())
293 report_fatal_error(
294 "Missing alignment specification for pointer in datalayout string");
Patrik Hagglund58b45532012-11-30 10:06:59 +0000295 Split = split(Rest, ':');
296 unsigned PointerABIAlign = inBytes(getInt(Tok));
Owen Andersonddfdffb2015-03-02 06:33:51 +0000297 if (!isPowerOf2_64(PointerABIAlign))
298 report_fatal_error(
299 "Pointer ABI alignment must be a power of 2");
Patrik Hagglund58b45532012-11-30 10:06:59 +0000300
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000301 // Size of index used in GEP for address calculation.
302 // The parameter is optional. By default it is equal to size of pointer.
303 unsigned IndexSize = PointerMemSize;
304
Patrik Hagglund58b45532012-11-30 10:06:59 +0000305 // Preferred alignment.
306 unsigned PointerPrefAlign = PointerABIAlign;
307 if (!Rest.empty()) {
308 Split = split(Rest, ':');
309 PointerPrefAlign = inBytes(getInt(Tok));
Owen Andersonddfdffb2015-03-02 06:33:51 +0000310 if (!isPowerOf2_64(PointerPrefAlign))
311 report_fatal_error(
312 "Pointer preferred alignment must be a power of 2");
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000313
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000314 // Now read the index. It is the second optional parameter here.
315 if (!Rest.empty()) {
316 Split = split(Rest, ':');
317 IndexSize = inBytes(getInt(Tok));
318 if (!IndexSize)
319 report_fatal_error("Invalid index size of 0 bytes");
320 }
321 }
Patrik Hagglund58b45532012-11-30 10:06:59 +0000322 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000323 PointerMemSize, IndexSize);
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000324 break;
325 }
326 case 'i':
327 case 'v':
328 case 'f':
Rafael Espindola33cc3f82014-01-01 22:29:43 +0000329 case 'a': {
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000330 AlignTypeEnum AlignType;
Patrik Hagglund58b45532012-11-30 10:06:59 +0000331 switch (Specifier) {
Craig Topper326f38f2017-05-22 19:28:36 +0000332 default: llvm_unreachable("Unexpected specifier!");
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000333 case 'i': AlignType = INTEGER_ALIGN; break;
334 case 'v': AlignType = VECTOR_ALIGN; break;
335 case 'f': AlignType = FLOAT_ALIGN; break;
336 case 'a': AlignType = AGGREGATE_ALIGN; break;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000337 }
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000338
Patrik Hagglund58b45532012-11-30 10:06:59 +0000339 // Bit size.
340 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
341
David Majnemeref6e5492014-12-10 01:17:08 +0000342 if (AlignType == AGGREGATE_ALIGN && Size != 0)
343 report_fatal_error(
344 "Sized aggregate specification in datalayout string");
Rafael Espindola17764832014-01-06 21:40:24 +0000345
Patrik Hagglund58b45532012-11-30 10:06:59 +0000346 // ABI alignment.
David Majnemerfda17192014-12-10 02:36:41 +0000347 if (Rest.empty())
348 report_fatal_error(
349 "Missing alignment specification in datalayout string");
Patrik Hagglund58b45532012-11-30 10:06:59 +0000350 Split = split(Rest, ':');
351 unsigned ABIAlign = inBytes(getInt(Tok));
Owen Andersonc7292fd2015-03-02 09:34:59 +0000352 if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
353 report_fatal_error(
354 "ABI alignment specification must be >0 for non-aggregate types");
Patrik Hagglund58b45532012-11-30 10:06:59 +0000355
356 // Preferred alignment.
357 unsigned PrefAlign = ABIAlign;
358 if (!Rest.empty()) {
359 Split = split(Rest, ':');
360 PrefAlign = inBytes(getInt(Tok));
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000361 }
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000362
Patrik Hägglund1e656762012-11-14 09:04:56 +0000363 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmow99b11482012-10-04 23:01:22 +0000364
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000365 break;
366 }
367 case 'n': // Native integer types.
Eugene Zelenkoe5dc33d2017-05-05 22:30:37 +0000368 while (true) {
Patrik Hagglund58b45532012-11-30 10:06:59 +0000369 unsigned Width = getInt(Tok);
David Majnemeref6e5492014-12-10 01:17:08 +0000370 if (Width == 0)
371 report_fatal_error(
372 "Zero width native integer type in datalayout string");
Patrik Hägglund37d5be72012-11-28 12:13:12 +0000373 LegalIntWidths.push_back(Width);
Patrik Hagglund58b45532012-11-30 10:06:59 +0000374 if (Rest.empty())
375 break;
376 Split = split(Rest, ':');
377 }
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000378 break;
379 case 'S': { // Stack natural alignment.
Patrik Hagglund58b45532012-11-30 10:06:59 +0000380 StackNaturalAlign = inBytes(getInt(Tok));
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000381 break;
382 }
Dylan McKaya78fde82018-02-19 09:56:22 +0000383 case 'P': { // Function address space.
384 ProgramAddrSpace = getAddrSpace(Tok);
385 break;
386 }
Matt Arsenaulte0b3c332017-04-10 22:27:50 +0000387 case 'A': { // Default stack/alloca address space.
Dylan McKaya78fde82018-02-19 09:56:22 +0000388 AllocaAddrSpace = getAddrSpace(Tok);
Matt Arsenaulte0b3c332017-04-10 22:27:50 +0000389 break;
390 }
Rafael Espindola8e0f67d2014-01-03 19:21:54 +0000391 case 'm':
David Majnemerfda17192014-12-10 02:36:41 +0000392 if (!Tok.empty())
393 report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
394 if (Rest.empty())
395 report_fatal_error("Expected mangling specifier in datalayout string");
396 if (Rest.size() > 1)
397 report_fatal_error("Unknown mangling specifier in datalayout string");
Rafael Espindola8e0f67d2014-01-03 19:21:54 +0000398 switch(Rest[0]) {
399 default:
David Majnemeref6e5492014-12-10 01:17:08 +0000400 report_fatal_error("Unknown mangling in datalayout string");
Rafael Espindola8e0f67d2014-01-03 19:21:54 +0000401 case 'e':
402 ManglingMode = MM_ELF;
403 break;
404 case 'o':
405 ManglingMode = MM_MachO;
406 break;
407 case 'm':
408 ManglingMode = MM_Mips;
409 break;
Rafael Espindolaa9ad60c2014-01-10 13:42:12 +0000410 case 'w':
David Majnemer7605cdd2015-03-17 23:54:51 +0000411 ManglingMode = MM_WinCOFF;
412 break;
413 case 'x':
414 ManglingMode = MM_WinCOFFX86;
Rafael Espindola8e0f67d2014-01-03 19:21:54 +0000415 break;
416 }
417 break;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000418 default:
David Majnemeref6e5492014-12-10 01:17:08 +0000419 report_fatal_error("Unknown specifier in datalayout string");
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000420 break;
421 }
422 }
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000423}
424
Eugene Zelenkoe5dc33d2017-05-05 22:30:37 +0000425DataLayout::DataLayout(const Module *M) {
Rafael Espindola3b670552014-09-10 21:27:43 +0000426 init(M);
427}
428
Mehdi Aminic94da202015-03-04 18:43:29 +0000429void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000430
Rafael Espindolac4bdb932014-02-26 17:02:08 +0000431bool DataLayout::operator==(const DataLayout &Other) const {
Chandler Carruth34b45cd2014-10-20 10:41:29 +0000432 bool Ret = BigEndian == Other.BigEndian &&
Matt Arsenaulte0b3c332017-04-10 22:27:50 +0000433 AllocaAddrSpace == Other.AllocaAddrSpace &&
Rafael Espindolac4bdb932014-02-26 17:02:08 +0000434 StackNaturalAlign == Other.StackNaturalAlign &&
Dylan McKaya78fde82018-02-19 09:56:22 +0000435 ProgramAddrSpace == Other.ProgramAddrSpace &&
Rafael Espindolac4bdb932014-02-26 17:02:08 +0000436 ManglingMode == Other.ManglingMode &&
437 LegalIntWidths == Other.LegalIntWidths &&
Rafael Espindola10210142014-04-22 17:47:03 +0000438 Alignments == Other.Alignments && Pointers == Other.Pointers;
Mehdi Aminic94da202015-03-04 18:43:29 +0000439 // Note: getStringRepresentation() might differs, it is not canonicalized
Rafael Espindolac4bdb932014-02-26 17:02:08 +0000440 return Ret;
441}
442
Craig Topperba5e4202017-03-23 06:15:56 +0000443DataLayout::AlignmentsTy::iterator
444DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
445 uint32_t BitWidth) {
446 auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
447 return std::lower_bound(Alignments.begin(), Alignments.end(), Pair,
448 [](const LayoutAlignElem &LHS,
449 const std::pair<unsigned, uint32_t> &RHS) {
450 return std::tie(LHS.AlignType, LHS.TypeBitWidth) <
451 std::tie(RHS.first, RHS.second);
452 });
453}
454
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000455void
Micah Villmow99b11482012-10-04 23:01:22 +0000456DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000457 unsigned pref_align, uint32_t bit_width) {
David Majnemerc859e732015-02-16 05:41:53 +0000458 if (!isUInt<24>(bit_width))
459 report_fatal_error("Invalid bit width, must be a 24bit integer");
460 if (!isUInt<16>(abi_align))
461 report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
462 if (!isUInt<16>(pref_align))
463 report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
Owen Anderson8fae2842015-03-02 09:35:03 +0000464 if (abi_align != 0 && !isPowerOf2_64(abi_align))
465 report_fatal_error("Invalid ABI alignment, must be a power of 2");
466 if (pref_align != 0 && !isPowerOf2_64(pref_align))
467 report_fatal_error("Invalid preferred alignment, must be a power of 2");
David Majnemerc859e732015-02-16 05:41:53 +0000468
469 if (pref_align < abi_align)
470 report_fatal_error(
471 "Preferred alignment cannot be less than the ABI alignment");
472
Craig Topperba5e4202017-03-23 06:15:56 +0000473 AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width);
474 if (I != Alignments.end() &&
475 I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) {
476 // Update the abi, preferred alignments.
477 I->ABIAlign = abi_align;
478 I->PrefAlign = pref_align;
479 } else {
480 // Insert before I to keep the vector sorted.
481 Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align,
482 pref_align, bit_width));
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000483 }
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000484}
485
Rafael Espindolae3561972014-02-26 16:58:35 +0000486DataLayout::PointersTy::iterator
Rafael Espindolaf985aec2014-02-26 17:05:38 +0000487DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
Rafael Espindolae3561972014-02-26 16:58:35 +0000488 return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
Benjamin Kramer6e9eeab2014-03-10 15:03:06 +0000489 [](const PointerAlignElem &A, uint32_t AddressSpace) {
490 return A.AddressSpace < AddressSpace;
491 });
Rafael Espindolae3561972014-02-26 16:58:35 +0000492}
493
Rafael Espindola5e7d2412013-12-13 23:15:20 +0000494void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000495 unsigned PrefAlign, uint32_t TypeByteWidth,
496 uint32_t IndexWidth) {
David Majnemer2b022d52015-02-16 05:41:55 +0000497 if (PrefAlign < ABIAlign)
498 report_fatal_error(
499 "Preferred alignment cannot be less than the ABI alignment");
500
Rafael Espindolaf985aec2014-02-26 17:05:38 +0000501 PointersTy::iterator I = findPointerLowerBound(AddrSpace);
Rafael Espindolae3561972014-02-26 16:58:35 +0000502 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
503 Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000504 TypeByteWidth, IndexWidth));
Micah Villmow7d661462012-10-09 16:06:12 +0000505 } else {
Rafael Espindolae3561972014-02-26 16:58:35 +0000506 I->ABIAlign = ABIAlign;
507 I->PrefAlign = PrefAlign;
508 I->TypeByteWidth = TypeByteWidth;
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000509 I->IndexWidth = IndexWidth;
Micah Villmow7d661462012-10-09 16:06:12 +0000510 }
511}
512
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000513/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmow99b11482012-10-04 23:01:22 +0000514/// preferred if ABIInfo = false) the layout wants for the specified datatype.
515unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000516 uint32_t BitWidth, bool ABIInfo,
Pete Cooper80ec0f82015-07-27 17:15:28 +0000517 Type *Ty) const {
Craig Topperba5e4202017-03-23 06:15:56 +0000518 AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth);
519 // See if we found an exact match. Of if we are looking for an integer type,
520 // but don't have an exact match take the next largest integer. This is where
521 // the lower_bound will point to when it fails an exact match.
522 if (I != Alignments.end() && I->AlignType == (unsigned)AlignType &&
523 (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN))
524 return ABIInfo ? I->ABIAlign : I->PrefAlign;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000525
Craig Topperba5e4202017-03-23 06:15:56 +0000526 if (AlignType == INTEGER_ALIGN) {
527 // If we didn't have a larger value try the largest value we have.
528 if (I != Alignments.begin()) {
529 --I; // Go to the previous entry and see if its an integer.
530 if (I->AlignType == INTEGER_ALIGN)
531 return ABIInfo ? I->ABIAlign : I->PrefAlign;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000532 }
Craig Topperba5e4202017-03-23 06:15:56 +0000533 } else if (AlignType == VECTOR_ALIGN) {
534 // By default, use natural alignment for vector types. This is consistent
535 // with what clang and llvm-gcc do.
536 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
537 Align *= cast<VectorType>(Ty)->getNumElements();
538 Align = PowerOf2Ceil(Align);
539 return Align;
540 }
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000541
Owen Andersonc03496d2015-03-08 21:53:59 +0000542 // If we still couldn't find a reasonable default alignment, fall back
543 // to a simple heuristic that the alignment is the first power of two
544 // greater-or-equal to the store size of the type. This is a reasonable
545 // approximation of reality, and if the user wanted something less
546 // less conservative, they should have specified it explicitly in the data
547 // layout.
Craig Topperba5e4202017-03-23 06:15:56 +0000548 unsigned Align = getTypeStoreSize(Ty);
549 Align = PowerOf2Ceil(Align);
550 return Align;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000551}
552
553namespace {
554
555class StructLayoutMap {
Eugene Zelenkoe5dc33d2017-05-05 22:30:37 +0000556 using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000557 LayoutInfoTy LayoutInfo;
558
559public:
Benjamin Kramer6e9eeab2014-03-10 15:03:06 +0000560 ~StructLayoutMap() {
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000561 // Remove any layouts.
Benjamin Kramer6e9eeab2014-03-10 15:03:06 +0000562 for (const auto &I : LayoutInfo) {
563 StructLayout *Value = I.second;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000564 Value->~StructLayout();
565 free(Value);
566 }
567 }
568
Pete Cooper80ec0f82015-07-27 17:15:28 +0000569 StructLayout *&operator[](StructType *STy) {
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000570 return LayoutInfo[STy];
571 }
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000572};
573
574} // end anonymous namespace
575
Rafael Espindola3f0a9af2014-02-25 22:23:04 +0000576void DataLayout::clear() {
577 LegalIntWidths.clear();
578 Alignments.clear();
579 Pointers.clear();
580 delete static_cast<StructLayoutMap *>(LayoutMap);
Craig Topperec0f0bc2014-04-09 06:08:46 +0000581 LayoutMap = nullptr;
Rafael Espindola3f0a9af2014-02-25 22:23:04 +0000582}
583
Micah Villmow99b11482012-10-04 23:01:22 +0000584DataLayout::~DataLayout() {
Rafael Espindola3f0a9af2014-02-25 22:23:04 +0000585 clear();
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000586}
587
Pete Cooper80ec0f82015-07-27 17:15:28 +0000588const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000589 if (!LayoutMap)
590 LayoutMap = new StructLayoutMap();
591
592 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
593 StructLayout *&SL = (*STM)[Ty];
594 if (SL) return SL;
595
596 // Otherwise, create the struct layout. Because it is variable length, we
597 // malloc it, then use placement new.
598 int NumElts = Ty->getNumElements();
Serge Pavlovc71a2122018-06-09 05:19:45 +0000599 StructLayout *L = (StructLayout *)
600 safe_malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000601
602 // Set SL before calling StructLayout's ctor. The ctor could cause other
603 // entries to be added to TheMap, invalidating our reference.
604 SL = L;
605
606 new (L) StructLayout(Ty, *this);
607
608 return L;
609}
610
Rafael Espindola3c4c95e2014-02-26 16:49:40 +0000611unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
Rafael Espindolaf985aec2014-02-26 17:05:38 +0000612 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindolae3561972014-02-26 16:58:35 +0000613 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolaf985aec2014-02-26 17:05:38 +0000614 I = findPointerLowerBound(0);
Rafael Espindolae3561972014-02-26 16:58:35 +0000615 assert(I->AddressSpace == 0);
Rafael Espindola3c4c95e2014-02-26 16:49:40 +0000616 }
Rafael Espindolae3561972014-02-26 16:58:35 +0000617 return I->ABIAlign;
Rafael Espindola3c4c95e2014-02-26 16:49:40 +0000618}
619
620unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
Rafael Espindolaf985aec2014-02-26 17:05:38 +0000621 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindolae3561972014-02-26 16:58:35 +0000622 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolaf985aec2014-02-26 17:05:38 +0000623 I = findPointerLowerBound(0);
Rafael Espindolae3561972014-02-26 16:58:35 +0000624 assert(I->AddressSpace == 0);
Rafael Espindola3c4c95e2014-02-26 16:49:40 +0000625 }
Rafael Espindolae3561972014-02-26 16:58:35 +0000626 return I->PrefAlign;
Rafael Espindola3c4c95e2014-02-26 16:49:40 +0000627}
628
629unsigned DataLayout::getPointerSize(unsigned AS) const {
Rafael Espindolaf985aec2014-02-26 17:05:38 +0000630 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindolae3561972014-02-26 16:58:35 +0000631 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolaf985aec2014-02-26 17:05:38 +0000632 I = findPointerLowerBound(0);
Rafael Espindolae3561972014-02-26 16:58:35 +0000633 assert(I->AddressSpace == 0);
Rafael Espindola3c4c95e2014-02-26 16:49:40 +0000634 }
Rafael Espindolae3561972014-02-26 16:58:35 +0000635 return I->TypeByteWidth;
Rafael Espindola3c4c95e2014-02-26 16:49:40 +0000636}
637
Hal Finkelccb51b92019-01-02 16:28:09 +0000638unsigned DataLayout::getMaxPointerSize() const {
639 unsigned MaxPointerSize = 0;
640 for (auto &P : Pointers)
641 MaxPointerSize = std::max(MaxPointerSize, P.TypeByteWidth);
642
643 return MaxPointerSize;
644}
645
Pete Cooper80ec0f82015-07-27 17:15:28 +0000646unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
Matt Arsenault58376d82013-07-26 17:37:20 +0000647 assert(Ty->isPtrOrPtrVectorTy() &&
648 "This should only be called with a pointer or pointer vector type");
Craig Topper7b19c162017-04-17 18:22:36 +0000649 Ty = Ty->getScalarType();
650 return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
Matt Arsenault58376d82013-07-26 17:37:20 +0000651}
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000652
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000653unsigned DataLayout::getIndexSize(unsigned AS) const {
654 PointersTy::const_iterator I = findPointerLowerBound(AS);
655 if (I == Pointers.end() || I->AddressSpace != AS) {
656 I = findPointerLowerBound(0);
657 assert(I->AddressSpace == 0);
658 }
659 return I->IndexWidth;
660}
661
662unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
663 assert(Ty->isPtrOrPtrVectorTy() &&
664 "This should only be called with a pointer or pointer vector type");
665 Ty = Ty->getScalarType();
666 return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
667}
668
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000669/*!
670 \param abi_or_pref Flag that determines which alignment is returned. true
671 returns the ABI alignment, false returns the preferred alignment.
672 \param Ty The underlying type for which alignment is determined.
673
674 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
675 == false) for the requested type \a Ty.
676 */
Pete Cooper80ec0f82015-07-27 17:15:28 +0000677unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Craig Topperfb21a1a2017-04-19 00:31:38 +0000678 AlignTypeEnum AlignType;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000679
680 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
681 switch (Ty->getTypeID()) {
682 // Early escape for the non-numeric types.
683 case Type::LabelTyID:
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000684 return (abi_or_pref
Micah Villmow7d661462012-10-09 16:06:12 +0000685 ? getPointerABIAlignment(0)
686 : getPointerPrefAlignment(0));
687 case Type::PointerTyID: {
Matt Arsenault76f0a922014-09-18 22:28:56 +0000688 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Micah Villmow7d661462012-10-09 16:06:12 +0000689 return (abi_or_pref
690 ? getPointerABIAlignment(AS)
691 : getPointerPrefAlignment(AS));
692 }
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000693 case Type::ArrayTyID:
694 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
695
696 case Type::StructTyID: {
697 // Packed structure types always have an ABI alignment of one.
698 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
699 return 1;
700
701 // Get the layout annotation... which is lazily created on demand.
702 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
703 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
704 return std::max(Align, Layout->getAlignment());
705 }
706 case Type::IntegerTyID:
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000707 AlignType = INTEGER_ALIGN;
708 break;
709 case Type::HalfTyID:
710 case Type::FloatTyID:
711 case Type::DoubleTyID:
712 // PPC_FP128TyID and FP128TyID have different data contents, but the
713 // same size and alignment, so they look the same here.
714 case Type::PPC_FP128TyID:
715 case Type::FP128TyID:
716 case Type::X86_FP80TyID:
717 AlignType = FLOAT_ALIGN;
718 break;
719 case Type::X86_MMXTyID:
720 case Type::VectorTyID:
721 AlignType = VECTOR_ALIGN;
722 break;
723 default:
724 llvm_unreachable("Bad type for getAlignment!!!");
725 }
726
Craig Topperfb21a1a2017-04-19 00:31:38 +0000727 return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty), abi_or_pref, Ty);
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000728}
729
Pete Cooper80ec0f82015-07-27 17:15:28 +0000730unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000731 return getAlignment(Ty, true);
732}
733
734/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
735/// an integer type of the specified bitwidth.
Micah Villmow99b11482012-10-04 23:01:22 +0000736unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Craig Topperec0f0bc2014-04-09 06:08:46 +0000737 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000738}
739
Pete Cooper80ec0f82015-07-27 17:15:28 +0000740unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000741 return getAlignment(Ty, false);
742}
743
Pete Cooper80ec0f82015-07-27 17:15:28 +0000744unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000745 unsigned Align = getPrefTypeAlignment(Ty);
746 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
747 return Log2_32(Align);
748}
749
Micah Villmow7d661462012-10-09 16:06:12 +0000750IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
751 unsigned AddressSpace) const {
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000752 return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000753}
754
Pete Cooper80ec0f82015-07-27 17:15:28 +0000755Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000756 assert(Ty->isPtrOrPtrVectorTy() &&
757 "Expected a pointer or pointer vector type.");
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000758 unsigned NumBits = getIndexTypeSizeInBits(Ty);
Duncan Sands7ed4f942012-10-29 17:31:46 +0000759 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
Pete Cooper80ec0f82015-07-27 17:15:28 +0000760 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
Duncan Sands7ed4f942012-10-29 17:31:46 +0000761 return VectorType::get(IntTy, VecTy->getNumElements());
762 return IntTy;
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000763}
764
Arnaud A. de Grandmaison2be921a2013-03-22 08:25:01 +0000765Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
Benjamin Kramer6e9eeab2014-03-10 15:03:06 +0000766 for (unsigned LegalIntWidth : LegalIntWidths)
767 if (Width <= LegalIntWidth)
768 return Type::getIntNTy(C, LegalIntWidth);
Craig Topperec0f0bc2014-04-09 06:08:46 +0000769 return nullptr;
Arnaud A. de Grandmaison2be921a2013-03-22 08:25:01 +0000770}
771
Jun Bum Lim1bbd21b2016-05-13 18:38:35 +0000772unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
Benjamin Kramer6e9eeab2014-03-10 15:03:06 +0000773 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
774 return Max != LegalIntWidths.end() ? *Max : 0;
Matt Arsenault4b28ee22013-09-16 22:43:16 +0000775}
776
Elena Demikhovsky8e229ec2018-02-14 06:58:08 +0000777Type *DataLayout::getIndexType(Type *Ty) const {
778 assert(Ty->isPtrOrPtrVectorTy() &&
779 "Expected a pointer or pointer vector type.");
780 unsigned NumBits = getIndexTypeSizeInBits(Ty);
781 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
782 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
783 return VectorType::get(IntTy, VecTy->getNumElements());
784 return IntTy;
785}
786
David Majnemer17c5ce912016-07-13 03:42:38 +0000787int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
788 ArrayRef<Value *> Indices) const {
789 int64_t Result = 0;
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000790
791 generic_gep_type_iterator<Value* const*>
Peter Collingbourne06115802016-12-02 02:24:42 +0000792 GTI = gep_type_begin(ElemTy, Indices),
793 GTE = gep_type_end(ElemTy, Indices);
Eduard Burtescua2694782016-01-22 03:08:27 +0000794 for (; GTI != GTE; ++GTI) {
795 Value *Idx = GTI.getOperand();
Peter Collingbourne06115802016-12-02 02:24:42 +0000796 if (StructType *STy = GTI.getStructTypeOrNull()) {
Manuel Jacob96905162016-01-22 03:30:27 +0000797 assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
Eduard Burtescua2694782016-01-22 03:08:27 +0000798 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000799
800 // Get structure layout information...
801 const StructLayout *Layout = getStructLayout(STy);
802
803 // Add in the offset, as calculated by the structure layout info...
804 Result += Layout->getElementOffset(FieldNo);
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000805 } else {
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000806 // Get the array index and the size of each array element.
Eduard Burtescua2694782016-01-22 03:08:27 +0000807 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
David Majnemer17c5ce912016-07-13 03:42:38 +0000808 Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000809 }
810 }
811
812 return Result;
813}
814
815/// getPreferredAlignment - Return the preferred alignment of the specified
816/// global. This includes an explicitly requested alignment (if the global
817/// has one).
Micah Villmow99b11482012-10-04 23:01:22 +0000818unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Eli Friedman47dc0192018-08-29 23:46:26 +0000819 unsigned GVAlignment = GV->getAlignment();
820 // If a section is specified, always precisely honor explicit alignment,
821 // so we don't insert padding into a section we don't control.
822 if (GVAlignment && GV->hasSection())
823 return GVAlignment;
824
825 // If no explicit alignment is specified, compute the alignment based on
826 // the IR type. If an alignment is specified, increase it to match the ABI
827 // alignment of the IR type.
828 //
829 // FIXME: Not sure it makes sense to use the alignment of the type if
830 // there's already an explicit alignment specification.
Manuel Jacob75e1cfb2016-01-16 20:30:46 +0000831 Type *ElemType = GV->getValueType();
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000832 unsigned Alignment = getPrefTypeAlignment(ElemType);
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000833 if (GVAlignment >= Alignment) {
834 Alignment = GVAlignment;
835 } else if (GVAlignment != 0) {
836 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
837 }
838
Eli Friedman47dc0192018-08-29 23:46:26 +0000839 // If no explicit alignment is specified, and the global is large, increase
840 // the alignment to 16.
841 // FIXME: Why 16, specifically?
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000842 if (GV->hasInitializer() && GVAlignment == 0) {
843 if (Alignment < 16) {
844 // If the global is not external, see if it is large. If so, give it a
845 // larger alignment.
846 if (getTypeSizeInBits(ElemType) > 128)
847 Alignment = 16; // 16-byte alignment.
848 }
849 }
850 return Alignment;
851}
852
853/// getPreferredAlignmentLog - Return the preferred alignment of the
854/// specified global, returned in log form. This includes an explicitly
855/// requested alignment (if the global has one).
Micah Villmow99b11482012-10-04 23:01:22 +0000856unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowe18c2ae2012-10-04 22:08:14 +0000857 return Log2_32(getPreferredAlignment(GV));
858}