blob: fff2f67fe5add147f4b422f7c7111938a88e82ce [file] [log] [blame]
Eugene Zelenkof53a7b42017-05-05 22:30:37 +00001//===- DataLayout.cpp - Data size & alignment routines ---------------------==//
Micah Villmowac34b5c2012-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 Villmowb4faa152012-10-04 23:01:22 +000010// This file defines layout properties related to datatype size/offset/alignment
Micah Villmowac34b5c2012-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 Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/IR/DataLayout.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/DenseMap.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000021#include "llvm/ADT/StringRef.h"
Rafael Espindola58873562014-01-03 19:21:54 +000022#include "llvm/ADT/Triple.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000025#include "llvm/IR/GetElementPtrTypeIterator.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000026#include "llvm/IR/GlobalVariable.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000028#include "llvm/IR/Type.h"
29#include "llvm/IR/Value.h"
30#include "llvm/Support/Casting.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000031#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/MathExtras.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000033#include <algorithm>
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000034#include <cassert>
35#include <cstdint>
Micah Villmowac34b5c2012-10-04 22:08:14 +000036#include <cstdlib>
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000037#include <tuple>
38#include <utility>
39
Micah Villmowac34b5c2012-10-04 22:08:14 +000040using namespace llvm;
41
Micah Villmowac34b5c2012-10-04 22:08:14 +000042//===----------------------------------------------------------------------===//
43// Support for StructLayout
44//===----------------------------------------------------------------------===//
45
Pete Cooper0ae73932015-07-27 17:15:28 +000046StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
Micah Villmowac34b5c2012-10-04 22:08:14 +000047 assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
48 StructAlignment = 0;
49 StructSize = 0;
Mehdi Amini1c131b32015-12-15 01:44:07 +000050 IsPadded = false;
Micah Villmowac34b5c2012-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 Bendersky41913c72013-04-16 15:41:18 +000056 unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
Micah Villmowac34b5c2012-10-04 22:08:14 +000057
58 // Add padding if necessary to align the data element properly.
Mehdi Amini1c131b32015-12-15 01:44:07 +000059 if ((StructSize & (TyAlign-1)) != 0) {
60 IsPadded = true;
Rui Ueyamada00f2f2016-01-14 21:06:47 +000061 StructSize = alignTo(StructSize, TyAlign);
Mehdi Amini1c131b32015-12-15 01:44:07 +000062 }
Micah Villmowac34b5c2012-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 Bendersky41913c72013-04-16 15:41:18 +000068 StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
Micah Villmowac34b5c2012-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 Amini1c131b32015-12-15 01:44:07 +000076 if ((StructSize & (StructAlignment-1)) != 0) {
77 IsPadded = true;
Rui Ueyamada00f2f2016-01-14 21:06:47 +000078 StructSize = alignTo(StructSize, StructAlignment);
Mehdi Amini1c131b32015-12-15 01:44:07 +000079 }
Micah Villmowac34b5c2012-10-04 22:08:14 +000080}
81
Micah Villmowac34b5c2012-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 Villmowb4faa152012-10-04 23:01:22 +0000103// LayoutAlignElem, LayoutAlign support
Micah Villmowac34b5c2012-10-04 22:08:14 +0000104//===----------------------------------------------------------------------===//
105
Micah Villmowb4faa152012-10-04 23:01:22 +0000106LayoutAlignElem
107LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-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 Villmowb4faa152012-10-04 23:01:22 +0000110 LayoutAlignElem retval;
Micah Villmowac34b5c2012-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 Villmowb4faa152012-10-04 23:01:22 +0000119LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
Micah Villmowac34b5c2012-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 Villmow89021e42012-10-09 16:06:12 +0000126//===----------------------------------------------------------------------===//
127// PointerAlignElem, PointerAlign support
128//===----------------------------------------------------------------------===//
129
130PointerAlignElem
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000131PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000132 unsigned PrefAlign, uint32_t TypeByteWidth,
133 uint32_t IndexWidth) {
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000134 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
Micah Villmow89021e42012-10-09 16:06:12 +0000135 PointerAlignElem retval;
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000136 retval.AddressSpace = AddressSpace;
137 retval.ABIAlign = ABIAlign;
138 retval.PrefAlign = PrefAlign;
139 retval.TypeByteWidth = TypeByteWidth;
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000140 retval.IndexWidth = IndexWidth;
Micah Villmow89021e42012-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 Demikhovsky945b7e52018-02-14 06:58:08 +0000149 && TypeByteWidth == rhs.TypeByteWidth
150 && IndexWidth == rhs.IndexWidth);
Micah Villmow89021e42012-10-09 16:06:12 +0000151}
152
Micah Villmowac34b5c2012-10-04 22:08:14 +0000153//===----------------------------------------------------------------------===//
Micah Villmowb4faa152012-10-04 23:01:22 +0000154// DataLayout Class Implementation
Micah Villmowac34b5c2012-10-04 22:08:14 +0000155//===----------------------------------------------------------------------===//
156
Rafael Espindola58873562014-01-03 19:21:54 +0000157const char *DataLayout::getManglingComponent(const Triple &T) {
158 if (T.isOSBinFormatMachO())
159 return "-m:o";
David Majnemer7db449a2015-03-17 23:54:51 +0000160 if (T.isOSWindows() && T.isOSBinFormatCOFF())
161 return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000162 return "-m:e";
Rafael Espindola58873562014-01-03 19:21:54 +0000163}
164
Rafael Espindolae23b8772013-12-20 15:21:32 +0000165static const LayoutAlignElem DefaultAlignments[] = {
Rafael Espindola458a4852013-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 Espindola248ac132014-02-25 22:23:04 +0000180void DataLayout::reset(StringRef Desc) {
181 clear();
182
Craig Topperc6207612014-04-09 06:08:46 +0000183 LayoutMap = nullptr;
Chandler Carruthf67321c2014-10-20 10:41:29 +0000184 BigEndian = false;
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000185 AllocaAddrSpace = 0;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000186 StackNaturalAlign = 0;
Dylan McKayced2fe62018-02-19 09:56:22 +0000187 ProgramAddrSpace = 0;
Rafael Espindola58873562014-01-03 19:21:54 +0000188 ManglingMode = MM_None;
Sanjoy Dasc6af5ea2016-07-28 23:43:38 +0000189 NonIntegralAddressSpaces.clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000190
191 // Default alignments
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000192 for (const LayoutAlignElem &E : DefaultAlignments) {
Rafael Espindola458a4852013-12-19 23:03:03 +0000193 setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
194 E.TypeBitWidth);
195 }
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000196 setPointerAlignment(0, 8, 8, 8, 8);
Patrik Hägglund01860a62012-11-14 09:04:56 +0000197
Patrik Hagglund086ee1e2012-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 Majnemer2dc1b0f2014-12-10 01:38:28 +0000205 if (Split.second.empty() && Split.first != Str)
206 report_fatal_error("Trailing separator in datalayout string");
David Majnemer612f3122014-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 Hagglund086ee1e2012-11-30 10:06:59 +0000209 return Split;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000210}
211
Cameron McInally8af9eac2014-01-07 19:51:38 +0000212/// Get an unsigned integer, including error checks.
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000213static unsigned getInt(StringRef R) {
Cameron McInallyf0379fa2014-01-13 22:04:55 +0000214 unsigned Result;
Patrik Hägglund504f4782012-11-28 14:32:52 +0000215 bool error = R.getAsInteger(10, Result); (void)error;
Cameron McInallyf0379fa2014-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ägglund3eb16c52012-11-28 12:13:12 +0000218 return Result;
219}
220
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000221/// Convert bits into bytes. Assert if not a byte width multiple.
222static unsigned inBytes(unsigned Bits) {
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000223 if (Bits % 8)
224 report_fatal_error("number of bits must be a byte width multiple");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000225 return Bits / 8;
226}
227
Dylan McKayced2fe62018-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 Hagglund086ee1e2012-11-30 10:06:59 +0000235void DataLayout::parseSpecifier(StringRef Desc) {
Mehdi Amini46a43552015-03-04 18:43:29 +0000236 StringRepresentation = Desc;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000237 while (!Desc.empty()) {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000238 // Split at '-'.
239 std::pair<StringRef, StringRef> Split = split(Desc, '-');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000240 Desc = Split.second;
241
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000242 // Split at ':'.
243 Split = split(Split.first, ':');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000244
Patrik Hagglund086ee1e2012-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 Villmowac34b5c2012-10-04 22:08:14 +0000248
Sanjoy Dasc6af5ea2016-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 Hagglund086ee1e2012-11-30 10:06:59 +0000262 char Specifier = Tok.front();
263 Tok = Tok.substr(1);
264
265 switch (Specifier) {
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000266 case 's':
267 // Ignored for backward compatibility.
268 // FIXME: remove this on LLVM 4.0.
269 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000270 case 'E':
Chandler Carruthf67321c2014-10-20 10:41:29 +0000271 BigEndian = true;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000272 break;
273 case 'e':
Chandler Carruthf67321c2014-10-20 10:41:29 +0000274 BigEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000275 break;
276 case 'p': {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000277 // Address space.
278 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000279 if (!isUInt<24>(AddrSpace))
280 report_fatal_error("Invalid address space, must be a 24bit integer");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000281
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000282 // Size.
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000283 if (Rest.empty())
284 report_fatal_error(
285 "Missing size specification for pointer in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000286 Split = split(Rest, ':');
287 unsigned PointerMemSize = inBytes(getInt(Tok));
Owen Anderson5bc2bbe2015-03-02 06:00:02 +0000288 if (!PointerMemSize)
289 report_fatal_error("Invalid pointer size of 0 bytes");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000290
291 // ABI alignment.
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000292 if (Rest.empty())
293 report_fatal_error(
294 "Missing alignment specification for pointer in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000295 Split = split(Rest, ':');
296 unsigned PointerABIAlign = inBytes(getInt(Tok));
Owen Anderson040f2f82015-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 Hagglund086ee1e2012-11-30 10:06:59 +0000300
Elena Demikhovsky945b7e52018-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 Hagglund086ee1e2012-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 Anderson040f2f82015-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 Villmowac34b5c2012-10-04 22:08:14 +0000313
Elena Demikhovsky945b7e52018-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 Hagglund086ee1e2012-11-30 10:06:59 +0000322 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000323 PointerMemSize, IndexSize);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000324 break;
325 }
326 case 'i':
327 case 'v':
328 case 'f':
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000329 case 'a': {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000330 AlignTypeEnum AlignType;
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000331 switch (Specifier) {
Craig Topper64a65ec2017-05-22 19:28:36 +0000332 default: llvm_unreachable("Unexpected specifier!");
Micah Villmowac34b5c2012-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 Villmowac34b5c2012-10-04 22:08:14 +0000337 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000338
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000339 // Bit size.
340 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
341
David Majnemer5330c692014-12-10 01:17:08 +0000342 if (AlignType == AGGREGATE_ALIGN && Size != 0)
343 report_fatal_error(
344 "Sized aggregate specification in datalayout string");
Rafael Espindolaabdd7262014-01-06 21:40:24 +0000345
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000346 // ABI alignment.
David Majnemer612f3122014-12-10 02:36:41 +0000347 if (Rest.empty())
348 report_fatal_error(
349 "Missing alignment specification in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000350 Split = split(Rest, ':');
351 unsigned ABIAlign = inBytes(getInt(Tok));
Owen Andersonab1c7a72015-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 Hagglund086ee1e2012-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 Villmowac34b5c2012-10-04 22:08:14 +0000361 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000362
Patrik Hägglund01860a62012-11-14 09:04:56 +0000363 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmowb4faa152012-10-04 23:01:22 +0000364
Micah Villmowac34b5c2012-10-04 22:08:14 +0000365 break;
366 }
367 case 'n': // Native integer types.
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000368 while (true) {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000369 unsigned Width = getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000370 if (Width == 0)
371 report_fatal_error(
372 "Zero width native integer type in datalayout string");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000373 LegalIntWidths.push_back(Width);
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000374 if (Rest.empty())
375 break;
376 Split = split(Rest, ':');
377 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000378 break;
379 case 'S': { // Stack natural alignment.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000380 StackNaturalAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000381 break;
382 }
Dylan McKayced2fe62018-02-19 09:56:22 +0000383 case 'P': { // Function address space.
384 ProgramAddrSpace = getAddrSpace(Tok);
385 break;
386 }
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000387 case 'A': { // Default stack/alloca address space.
Dylan McKayced2fe62018-02-19 09:56:22 +0000388 AllocaAddrSpace = getAddrSpace(Tok);
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000389 break;
390 }
Rafael Espindola58873562014-01-03 19:21:54 +0000391 case 'm':
David Majnemer612f3122014-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 Espindola58873562014-01-03 19:21:54 +0000398 switch(Rest[0]) {
399 default:
David Majnemer5330c692014-12-10 01:17:08 +0000400 report_fatal_error("Unknown mangling in datalayout string");
Rafael Espindola58873562014-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 Espindolaaf77e122014-01-10 13:42:12 +0000410 case 'w':
David Majnemer7db449a2015-03-17 23:54:51 +0000411 ManglingMode = MM_WinCOFF;
412 break;
413 case 'x':
414 ManglingMode = MM_WinCOFFX86;
Rafael Espindola58873562014-01-03 19:21:54 +0000415 break;
416 }
417 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000418 default:
David Majnemer5330c692014-12-10 01:17:08 +0000419 report_fatal_error("Unknown specifier in datalayout string");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000420 break;
421 }
422 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000423}
424
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000425DataLayout::DataLayout(const Module *M) {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000426 init(M);
427}
428
Mehdi Amini46a43552015-03-04 18:43:29 +0000429void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000430
Rafael Espindolaae593f12014-02-26 17:02:08 +0000431bool DataLayout::operator==(const DataLayout &Other) const {
Chandler Carruthf67321c2014-10-20 10:41:29 +0000432 bool Ret = BigEndian == Other.BigEndian &&
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000433 AllocaAddrSpace == Other.AllocaAddrSpace &&
Rafael Espindolaae593f12014-02-26 17:02:08 +0000434 StackNaturalAlign == Other.StackNaturalAlign &&
Dylan McKayced2fe62018-02-19 09:56:22 +0000435 ProgramAddrSpace == Other.ProgramAddrSpace &&
Rafael Espindolaae593f12014-02-26 17:02:08 +0000436 ManglingMode == Other.ManglingMode &&
437 LegalIntWidths == Other.LegalIntWidths &&
Rafael Espindola89992b02014-04-22 17:47:03 +0000438 Alignments == Other.Alignments && Pointers == Other.Pointers;
Mehdi Amini46a43552015-03-04 18:43:29 +0000439 // Note: getStringRepresentation() might differs, it is not canonicalized
Rafael Espindolaae593f12014-02-26 17:02:08 +0000440 return Ret;
441}
442
Craig Topper490889c2017-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 Villmowac34b5c2012-10-04 22:08:14 +0000455void
Micah Villmowb4faa152012-10-04 23:01:22 +0000456DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000457 unsigned pref_align, uint32_t bit_width) {
David Majnemer1b9fc3a2015-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 Anderson5af4b212015-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 Majnemer1b9fc3a2015-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 Topper490889c2017-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 Villmowac34b5c2012-10-04 22:08:14 +0000483 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000484}
485
Rafael Espindola667fcb82014-02-26 16:58:35 +0000486DataLayout::PointersTy::iterator
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000487DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000488 return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000489 [](const PointerAlignElem &A, uint32_t AddressSpace) {
490 return A.AddressSpace < AddressSpace;
491 });
Rafael Espindola667fcb82014-02-26 16:58:35 +0000492}
493
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000494void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000495 unsigned PrefAlign, uint32_t TypeByteWidth,
496 uint32_t IndexWidth) {
David Majnemer4b042922015-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 Espindolae8ae0db2014-02-26 17:05:38 +0000501 PointersTy::iterator I = findPointerLowerBound(AddrSpace);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000502 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
503 Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000504 TypeByteWidth, IndexWidth));
Micah Villmow89021e42012-10-09 16:06:12 +0000505 } else {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000506 I->ABIAlign = ABIAlign;
507 I->PrefAlign = PrefAlign;
508 I->TypeByteWidth = TypeByteWidth;
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000509 I->IndexWidth = IndexWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000510 }
511}
512
Micah Villmowac34b5c2012-10-04 22:08:14 +0000513/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000514/// preferred if ABIInfo = false) the layout wants for the specified datatype.
515unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000516 uint32_t BitWidth, bool ABIInfo,
Pete Cooper0ae73932015-07-27 17:15:28 +0000517 Type *Ty) const {
Craig Topper490889c2017-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 Villmowac34b5c2012-10-04 22:08:14 +0000525
Craig Topper490889c2017-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 Villmowac34b5c2012-10-04 22:08:14 +0000532 }
Craig Topper490889c2017-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 Villmowac34b5c2012-10-04 22:08:14 +0000541
Owen Anderson7e621e92015-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 Topper490889c2017-03-23 06:15:56 +0000548 unsigned Align = getTypeStoreSize(Ty);
549 Align = PowerOf2Ceil(Align);
550 return Align;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000551}
552
553namespace {
554
555class StructLayoutMap {
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000556 using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000557 LayoutInfoTy LayoutInfo;
558
559public:
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000560 ~StructLayoutMap() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000561 // Remove any layouts.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000562 for (const auto &I : LayoutInfo) {
563 StructLayout *Value = I.second;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000564 Value->~StructLayout();
565 free(Value);
566 }
567 }
568
Pete Cooper0ae73932015-07-27 17:15:28 +0000569 StructLayout *&operator[](StructType *STy) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000570 return LayoutInfo[STy];
571 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000572};
573
574} // end anonymous namespace
575
Rafael Espindola248ac132014-02-25 22:23:04 +0000576void DataLayout::clear() {
577 LegalIntWidths.clear();
578 Alignments.clear();
579 Pointers.clear();
580 delete static_cast<StructLayoutMap *>(LayoutMap);
Craig Topperc6207612014-04-09 06:08:46 +0000581 LayoutMap = nullptr;
Rafael Espindola248ac132014-02-25 22:23:04 +0000582}
583
Micah Villmowb4faa152012-10-04 23:01:22 +0000584DataLayout::~DataLayout() {
Rafael Espindola248ac132014-02-25 22:23:04 +0000585 clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000586}
587
Pete Cooper0ae73932015-07-27 17:15:28 +0000588const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-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 Pavlovc4b6d0e2018-05-30 09:01:12 +0000599 StructLayout *L =
600 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
601 if (L == nullptr)
602 report_bad_alloc_error("Allocation of StructLayout elements failed.");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000603
604 // Set SL before calling StructLayout's ctor. The ctor could cause other
605 // entries to be added to TheMap, invalidating our reference.
606 SL = L;
607
608 new (L) StructLayout(Ty, *this);
609
610 return L;
611}
612
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000613unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000614 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000615 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000616 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000617 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000618 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000619 return I->ABIAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000620}
621
622unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000623 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000624 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000625 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000626 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000627 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000628 return I->PrefAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000629}
630
631unsigned DataLayout::getPointerSize(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000632 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000633 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000634 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000635 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000636 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000637 return I->TypeByteWidth;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000638}
639
Pete Cooper0ae73932015-07-27 17:15:28 +0000640unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
Matt Arsenault6f4be902013-07-26 17:37:20 +0000641 assert(Ty->isPtrOrPtrVectorTy() &&
642 "This should only be called with a pointer or pointer vector type");
Craig Topper5b4f5b02017-04-17 18:22:36 +0000643 Ty = Ty->getScalarType();
644 return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000645}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000646
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000647unsigned DataLayout::getIndexSize(unsigned AS) const {
648 PointersTy::const_iterator I = findPointerLowerBound(AS);
649 if (I == Pointers.end() || I->AddressSpace != AS) {
650 I = findPointerLowerBound(0);
651 assert(I->AddressSpace == 0);
652 }
653 return I->IndexWidth;
654}
655
656unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
657 assert(Ty->isPtrOrPtrVectorTy() &&
658 "This should only be called with a pointer or pointer vector type");
659 Ty = Ty->getScalarType();
660 return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
661}
662
Micah Villmowac34b5c2012-10-04 22:08:14 +0000663/*!
664 \param abi_or_pref Flag that determines which alignment is returned. true
665 returns the ABI alignment, false returns the preferred alignment.
666 \param Ty The underlying type for which alignment is determined.
667
668 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
669 == false) for the requested type \a Ty.
670 */
Pete Cooper0ae73932015-07-27 17:15:28 +0000671unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Craig Topperff6922a2017-04-19 00:31:38 +0000672 AlignTypeEnum AlignType;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000673
674 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
675 switch (Ty->getTypeID()) {
676 // Early escape for the non-numeric types.
677 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000678 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000679 ? getPointerABIAlignment(0)
680 : getPointerPrefAlignment(0));
681 case Type::PointerTyID: {
Matt Arsenaultc1728972014-09-18 22:28:56 +0000682 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Micah Villmow89021e42012-10-09 16:06:12 +0000683 return (abi_or_pref
684 ? getPointerABIAlignment(AS)
685 : getPointerPrefAlignment(AS));
686 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000687 case Type::ArrayTyID:
688 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
689
690 case Type::StructTyID: {
691 // Packed structure types always have an ABI alignment of one.
692 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
693 return 1;
694
695 // Get the layout annotation... which is lazily created on demand.
696 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
697 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
698 return std::max(Align, Layout->getAlignment());
699 }
700 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000701 AlignType = INTEGER_ALIGN;
702 break;
703 case Type::HalfTyID:
704 case Type::FloatTyID:
705 case Type::DoubleTyID:
706 // PPC_FP128TyID and FP128TyID have different data contents, but the
707 // same size and alignment, so they look the same here.
708 case Type::PPC_FP128TyID:
709 case Type::FP128TyID:
710 case Type::X86_FP80TyID:
711 AlignType = FLOAT_ALIGN;
712 break;
713 case Type::X86_MMXTyID:
714 case Type::VectorTyID:
715 AlignType = VECTOR_ALIGN;
716 break;
717 default:
718 llvm_unreachable("Bad type for getAlignment!!!");
719 }
720
Craig Topperff6922a2017-04-19 00:31:38 +0000721 return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty), abi_or_pref, Ty);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000722}
723
Pete Cooper0ae73932015-07-27 17:15:28 +0000724unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000725 return getAlignment(Ty, true);
726}
727
728/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
729/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000730unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Craig Topperc6207612014-04-09 06:08:46 +0000731 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000732}
733
Pete Cooper0ae73932015-07-27 17:15:28 +0000734unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000735 return getAlignment(Ty, false);
736}
737
Pete Cooper0ae73932015-07-27 17:15:28 +0000738unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000739 unsigned Align = getPrefTypeAlignment(Ty);
740 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
741 return Log2_32(Align);
742}
743
Micah Villmow89021e42012-10-09 16:06:12 +0000744IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
745 unsigned AddressSpace) const {
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000746 return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000747}
748
Pete Cooper0ae73932015-07-27 17:15:28 +0000749Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000750 assert(Ty->isPtrOrPtrVectorTy() &&
751 "Expected a pointer or pointer vector type.");
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000752 unsigned NumBits = getIndexTypeSizeInBits(Ty);
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000753 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
Pete Cooper0ae73932015-07-27 17:15:28 +0000754 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000755 return VectorType::get(IntTy, VecTy->getNumElements());
756 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000757}
758
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000759Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000760 for (unsigned LegalIntWidth : LegalIntWidths)
761 if (Width <= LegalIntWidth)
762 return Type::getIntNTy(C, LegalIntWidth);
Craig Topperc6207612014-04-09 06:08:46 +0000763 return nullptr;
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000764}
765
Jun Bum Limbe11bdc2016-05-13 18:38:35 +0000766unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000767 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
768 return Max != LegalIntWidths.end() ? *Max : 0;
Matt Arsenault899f7d22013-09-16 22:43:16 +0000769}
770
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000771Type *DataLayout::getIndexType(Type *Ty) const {
772 assert(Ty->isPtrOrPtrVectorTy() &&
773 "Expected a pointer or pointer vector type.");
774 unsigned NumBits = getIndexTypeSizeInBits(Ty);
775 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
776 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
777 return VectorType::get(IntTy, VecTy->getNumElements());
778 return IntTy;
779}
780
David Majnemer17bdf442016-07-13 03:42:38 +0000781int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
782 ArrayRef<Value *> Indices) const {
783 int64_t Result = 0;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000784
785 generic_gep_type_iterator<Value* const*>
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000786 GTI = gep_type_begin(ElemTy, Indices),
787 GTE = gep_type_end(ElemTy, Indices);
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000788 for (; GTI != GTE; ++GTI) {
789 Value *Idx = GTI.getOperand();
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000790 if (StructType *STy = GTI.getStructTypeOrNull()) {
Manuel Jacobcc13c2c2016-01-22 03:30:27 +0000791 assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000792 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000793
794 // Get structure layout information...
795 const StructLayout *Layout = getStructLayout(STy);
796
797 // Add in the offset, as calculated by the structure layout info...
798 Result += Layout->getElementOffset(FieldNo);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000799 } else {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000800 // Get the array index and the size of each array element.
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000801 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
David Majnemer17bdf442016-07-13 03:42:38 +0000802 Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000803 }
804 }
805
806 return Result;
807}
808
809/// getPreferredAlignment - Return the preferred alignment of the specified
810/// global. This includes an explicitly requested alignment (if the global
811/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000812unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000813 Type *ElemType = GV->getValueType();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000814 unsigned Alignment = getPrefTypeAlignment(ElemType);
815 unsigned GVAlignment = GV->getAlignment();
816 if (GVAlignment >= Alignment) {
817 Alignment = GVAlignment;
818 } else if (GVAlignment != 0) {
819 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
820 }
821
822 if (GV->hasInitializer() && GVAlignment == 0) {
823 if (Alignment < 16) {
824 // If the global is not external, see if it is large. If so, give it a
825 // larger alignment.
826 if (getTypeSizeInBits(ElemType) > 128)
827 Alignment = 16; // 16-byte alignment.
828 }
829 }
830 return Alignment;
831}
832
833/// getPreferredAlignmentLog - Return the preferred alignment of the
834/// specified global, returned in log form. This includes an explicitly
835/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000836unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000837 return Log2_32(getPreferredAlignment(GV));
838}