blob: 2644214a49c0d168b694a516b30d7350c553d5d4 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Micah Villmowac34b5c2012-10-04 22:08:14 +00006//
7//===----------------------------------------------------------------------===//
8//
Micah Villmowb4faa152012-10-04 23:01:22 +00009// This file defines layout properties related to datatype size/offset/alignment
Micah Villmowac34b5c2012-10-04 22:08:14 +000010// information.
11//
12// This structure should be created once, filled in if the defaults are not
13// correct and then passed around by const&. None of the members functions
14// require modification to the object.
15//
16//===----------------------------------------------------------------------===//
17
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/IR/DataLayout.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/DenseMap.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000020#include "llvm/ADT/StringRef.h"
Rafael Espindola58873562014-01-03 19:21:54 +000021#include "llvm/ADT/Triple.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Constants.h"
23#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000024#include "llvm/IR/GetElementPtrTypeIterator.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000025#include "llvm/IR/GlobalVariable.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Module.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000027#include "llvm/IR/Type.h"
28#include "llvm/IR/Value.h"
29#include "llvm/Support/Casting.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000030#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/MathExtras.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000032#include <algorithm>
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000033#include <cassert>
34#include <cstdint>
Micah Villmowac34b5c2012-10-04 22:08:14 +000035#include <cstdlib>
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000036#include <tuple>
37#include <utility>
38
Micah Villmowac34b5c2012-10-04 22:08:14 +000039using namespace llvm;
40
Micah Villmowac34b5c2012-10-04 22:08:14 +000041//===----------------------------------------------------------------------===//
42// Support for StructLayout
43//===----------------------------------------------------------------------===//
44
Pete Cooper0ae73932015-07-27 17:15:28 +000045StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
Micah Villmowac34b5c2012-10-04 22:08:14 +000046 assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
47 StructAlignment = 0;
48 StructSize = 0;
Mehdi Amini1c131b32015-12-15 01:44:07 +000049 IsPadded = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +000050 NumElements = ST->getNumElements();
51
52 // Loop over each of the elements, placing them in memory.
53 for (unsigned i = 0, e = NumElements; i != e; ++i) {
54 Type *Ty = ST->getElementType(i);
Eli Bendersky41913c72013-04-16 15:41:18 +000055 unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
Micah Villmowac34b5c2012-10-04 22:08:14 +000056
57 // Add padding if necessary to align the data element properly.
Mehdi Amini1c131b32015-12-15 01:44:07 +000058 if ((StructSize & (TyAlign-1)) != 0) {
59 IsPadded = true;
Rui Ueyamada00f2f2016-01-14 21:06:47 +000060 StructSize = alignTo(StructSize, TyAlign);
Mehdi Amini1c131b32015-12-15 01:44:07 +000061 }
Micah Villmowac34b5c2012-10-04 22:08:14 +000062
63 // Keep track of maximum alignment constraint.
64 StructAlignment = std::max(TyAlign, StructAlignment);
65
66 MemberOffsets[i] = StructSize;
Eli Bendersky41913c72013-04-16 15:41:18 +000067 StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
Micah Villmowac34b5c2012-10-04 22:08:14 +000068 }
69
70 // Empty structures have alignment of 1 byte.
71 if (StructAlignment == 0) StructAlignment = 1;
72
73 // Add padding to the end of the struct so that it could be put in an array
74 // and all array elements would be aligned correctly.
Mehdi Amini1c131b32015-12-15 01:44:07 +000075 if ((StructSize & (StructAlignment-1)) != 0) {
76 IsPadded = true;
Rui Ueyamada00f2f2016-01-14 21:06:47 +000077 StructSize = alignTo(StructSize, StructAlignment);
Mehdi Amini1c131b32015-12-15 01:44:07 +000078 }
Micah Villmowac34b5c2012-10-04 22:08:14 +000079}
80
Micah Villmowac34b5c2012-10-04 22:08:14 +000081/// getElementContainingOffset - Given a valid offset into the structure,
82/// return the structure index that contains it.
83unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
84 const uint64_t *SI =
85 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
86 assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
87 --SI;
88 assert(*SI <= Offset && "upper_bound didn't work");
89 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
90 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
91 "Upper bound didn't work!");
92
93 // Multiple fields can have the same offset if any of them are zero sized.
94 // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
95 // at the i32 element, because it is the last element at that offset. This is
96 // the right one to return, because anything after it will have a higher
97 // offset, implying that this element is non-empty.
98 return SI-&MemberOffsets[0];
99}
100
101//===----------------------------------------------------------------------===//
Micah Villmowb4faa152012-10-04 23:01:22 +0000102// LayoutAlignElem, LayoutAlign support
Micah Villmowac34b5c2012-10-04 22:08:14 +0000103//===----------------------------------------------------------------------===//
104
Micah Villmowb4faa152012-10-04 23:01:22 +0000105LayoutAlignElem
106LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000107 unsigned pref_align, uint32_t bit_width) {
108 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
Micah Villmowb4faa152012-10-04 23:01:22 +0000109 LayoutAlignElem retval;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000110 retval.AlignType = align_type;
111 retval.ABIAlign = abi_align;
112 retval.PrefAlign = pref_align;
113 retval.TypeBitWidth = bit_width;
114 return retval;
115}
116
117bool
Micah Villmowb4faa152012-10-04 23:01:22 +0000118LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000119 return (AlignType == rhs.AlignType
120 && ABIAlign == rhs.ABIAlign
121 && PrefAlign == rhs.PrefAlign
122 && TypeBitWidth == rhs.TypeBitWidth);
123}
124
Micah Villmow89021e42012-10-09 16:06:12 +0000125//===----------------------------------------------------------------------===//
126// PointerAlignElem, PointerAlign support
127//===----------------------------------------------------------------------===//
128
129PointerAlignElem
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000130PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000131 unsigned PrefAlign, uint32_t TypeByteWidth,
132 uint32_t IndexWidth) {
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000133 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
Micah Villmow89021e42012-10-09 16:06:12 +0000134 PointerAlignElem retval;
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000135 retval.AddressSpace = AddressSpace;
136 retval.ABIAlign = ABIAlign;
137 retval.PrefAlign = PrefAlign;
138 retval.TypeByteWidth = TypeByteWidth;
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000139 retval.IndexWidth = IndexWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000140 return retval;
141}
142
143bool
144PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
145 return (ABIAlign == rhs.ABIAlign
146 && AddressSpace == rhs.AddressSpace
147 && PrefAlign == rhs.PrefAlign
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000148 && TypeByteWidth == rhs.TypeByteWidth
149 && IndexWidth == rhs.IndexWidth);
Micah Villmow89021e42012-10-09 16:06:12 +0000150}
151
Micah Villmowac34b5c2012-10-04 22:08:14 +0000152//===----------------------------------------------------------------------===//
Micah Villmowb4faa152012-10-04 23:01:22 +0000153// DataLayout Class Implementation
Micah Villmowac34b5c2012-10-04 22:08:14 +0000154//===----------------------------------------------------------------------===//
155
Rafael Espindola58873562014-01-03 19:21:54 +0000156const char *DataLayout::getManglingComponent(const Triple &T) {
157 if (T.isOSBinFormatMachO())
158 return "-m:o";
David Majnemer7db449a2015-03-17 23:54:51 +0000159 if (T.isOSWindows() && T.isOSBinFormatCOFF())
160 return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000161 return "-m:e";
Rafael Espindola58873562014-01-03 19:21:54 +0000162}
163
Rafael Espindolae23b8772013-12-20 15:21:32 +0000164static const LayoutAlignElem DefaultAlignments[] = {
Rafael Espindola458a4852013-12-19 23:03:03 +0000165 { INTEGER_ALIGN, 1, 1, 1 }, // i1
166 { INTEGER_ALIGN, 8, 1, 1 }, // i8
167 { INTEGER_ALIGN, 16, 2, 2 }, // i16
168 { INTEGER_ALIGN, 32, 4, 4 }, // i32
169 { INTEGER_ALIGN, 64, 4, 8 }, // i64
170 { FLOAT_ALIGN, 16, 2, 2 }, // half
171 { FLOAT_ALIGN, 32, 4, 4 }, // float
172 { FLOAT_ALIGN, 64, 8, 8 }, // double
173 { FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ...
174 { VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ...
175 { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
176 { AGGREGATE_ALIGN, 0, 0, 8 } // struct
177};
178
Rafael Espindola248ac132014-02-25 22:23:04 +0000179void DataLayout::reset(StringRef Desc) {
180 clear();
181
Craig Topperc6207612014-04-09 06:08:46 +0000182 LayoutMap = nullptr;
Chandler Carruthf67321c2014-10-20 10:41:29 +0000183 BigEndian = false;
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000184 AllocaAddrSpace = 0;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000185 StackNaturalAlign = 0;
Dylan McKayced2fe62018-02-19 09:56:22 +0000186 ProgramAddrSpace = 0;
Rafael Espindola58873562014-01-03 19:21:54 +0000187 ManglingMode = MM_None;
Sanjoy Dasc6af5ea2016-07-28 23:43:38 +0000188 NonIntegralAddressSpaces.clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000189
190 // Default alignments
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000191 for (const LayoutAlignElem &E : DefaultAlignments) {
Rafael Espindola458a4852013-12-19 23:03:03 +0000192 setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
193 E.TypeBitWidth);
194 }
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000195 setPointerAlignment(0, 8, 8, 8, 8);
Patrik Hägglund01860a62012-11-14 09:04:56 +0000196
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000197 parseSpecifier(Desc);
198}
199
200/// Checked version of split, to ensure mandatory subparts.
201static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
202 assert(!Str.empty() && "parse error, string can't be empty here");
203 std::pair<StringRef, StringRef> Split = Str.split(Separator);
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000204 if (Split.second.empty() && Split.first != Str)
205 report_fatal_error("Trailing separator in datalayout string");
David Majnemer612f3122014-12-10 02:36:41 +0000206 if (!Split.second.empty() && Split.first.empty())
207 report_fatal_error("Expected token before separator in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000208 return Split;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000209}
210
Cameron McInally8af9eac2014-01-07 19:51:38 +0000211/// Get an unsigned integer, including error checks.
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000212static unsigned getInt(StringRef R) {
Cameron McInallyf0379fa2014-01-13 22:04:55 +0000213 unsigned Result;
Patrik Hägglund504f4782012-11-28 14:32:52 +0000214 bool error = R.getAsInteger(10, Result); (void)error;
Cameron McInallyf0379fa2014-01-13 22:04:55 +0000215 if (error)
216 report_fatal_error("not a number, or does not fit in an unsigned int");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000217 return Result;
218}
219
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000220/// Convert bits into bytes. Assert if not a byte width multiple.
221static unsigned inBytes(unsigned Bits) {
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000222 if (Bits % 8)
223 report_fatal_error("number of bits must be a byte width multiple");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000224 return Bits / 8;
225}
226
Dylan McKayced2fe62018-02-19 09:56:22 +0000227static unsigned getAddrSpace(StringRef R) {
228 unsigned AddrSpace = getInt(R);
229 if (!isUInt<24>(AddrSpace))
230 report_fatal_error("Invalid address space, must be a 24-bit integer");
231 return AddrSpace;
232}
233
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000234void DataLayout::parseSpecifier(StringRef Desc) {
Mehdi Amini46a43552015-03-04 18:43:29 +0000235 StringRepresentation = Desc;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000236 while (!Desc.empty()) {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000237 // Split at '-'.
238 std::pair<StringRef, StringRef> Split = split(Desc, '-');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000239 Desc = Split.second;
240
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000241 // Split at ':'.
242 Split = split(Split.first, ':');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000243
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000244 // Aliases used below.
245 StringRef &Tok = Split.first; // Current token.
246 StringRef &Rest = Split.second; // The rest of the string.
Micah Villmowac34b5c2012-10-04 22:08:14 +0000247
Sanjoy Dasc6af5ea2016-07-28 23:43:38 +0000248 if (Tok == "ni") {
249 do {
250 Split = split(Rest, ':');
251 Rest = Split.second;
252 unsigned AS = getInt(Split.first);
253 if (AS == 0)
254 report_fatal_error("Address space 0 can never be non-integral");
255 NonIntegralAddressSpaces.push_back(AS);
256 } while (!Rest.empty());
257
258 continue;
259 }
260
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000261 char Specifier = Tok.front();
262 Tok = Tok.substr(1);
263
264 switch (Specifier) {
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000265 case 's':
266 // Ignored for backward compatibility.
267 // FIXME: remove this on LLVM 4.0.
268 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000269 case 'E':
Chandler Carruthf67321c2014-10-20 10:41:29 +0000270 BigEndian = true;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000271 break;
272 case 'e':
Chandler Carruthf67321c2014-10-20 10:41:29 +0000273 BigEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000274 break;
275 case 'p': {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000276 // Address space.
277 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000278 if (!isUInt<24>(AddrSpace))
279 report_fatal_error("Invalid address space, must be a 24bit integer");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000280
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000281 // Size.
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000282 if (Rest.empty())
283 report_fatal_error(
284 "Missing size specification for pointer in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000285 Split = split(Rest, ':');
286 unsigned PointerMemSize = inBytes(getInt(Tok));
Owen Anderson5bc2bbe2015-03-02 06:00:02 +0000287 if (!PointerMemSize)
288 report_fatal_error("Invalid pointer size of 0 bytes");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000289
290 // ABI alignment.
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000291 if (Rest.empty())
292 report_fatal_error(
293 "Missing alignment specification for pointer in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000294 Split = split(Rest, ':');
295 unsigned PointerABIAlign = inBytes(getInt(Tok));
Owen Anderson040f2f82015-03-02 06:33:51 +0000296 if (!isPowerOf2_64(PointerABIAlign))
297 report_fatal_error(
298 "Pointer ABI alignment must be a power of 2");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000299
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000300 // Size of index used in GEP for address calculation.
301 // The parameter is optional. By default it is equal to size of pointer.
302 unsigned IndexSize = PointerMemSize;
303
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000304 // Preferred alignment.
305 unsigned PointerPrefAlign = PointerABIAlign;
306 if (!Rest.empty()) {
307 Split = split(Rest, ':');
308 PointerPrefAlign = inBytes(getInt(Tok));
Owen Anderson040f2f82015-03-02 06:33:51 +0000309 if (!isPowerOf2_64(PointerPrefAlign))
310 report_fatal_error(
311 "Pointer preferred alignment must be a power of 2");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000312
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000313 // Now read the index. It is the second optional parameter here.
314 if (!Rest.empty()) {
315 Split = split(Rest, ':');
316 IndexSize = inBytes(getInt(Tok));
317 if (!IndexSize)
318 report_fatal_error("Invalid index size of 0 bytes");
319 }
320 }
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000321 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000322 PointerMemSize, IndexSize);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000323 break;
324 }
325 case 'i':
326 case 'v':
327 case 'f':
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000328 case 'a': {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000329 AlignTypeEnum AlignType;
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000330 switch (Specifier) {
Craig Topper64a65ec2017-05-22 19:28:36 +0000331 default: llvm_unreachable("Unexpected specifier!");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000332 case 'i': AlignType = INTEGER_ALIGN; break;
333 case 'v': AlignType = VECTOR_ALIGN; break;
334 case 'f': AlignType = FLOAT_ALIGN; break;
335 case 'a': AlignType = AGGREGATE_ALIGN; break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000336 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000337
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000338 // Bit size.
339 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
340
David Majnemer5330c692014-12-10 01:17:08 +0000341 if (AlignType == AGGREGATE_ALIGN && Size != 0)
342 report_fatal_error(
343 "Sized aggregate specification in datalayout string");
Rafael Espindolaabdd7262014-01-06 21:40:24 +0000344
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000345 // ABI alignment.
David Majnemer612f3122014-12-10 02:36:41 +0000346 if (Rest.empty())
347 report_fatal_error(
348 "Missing alignment specification in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000349 Split = split(Rest, ':');
350 unsigned ABIAlign = inBytes(getInt(Tok));
Owen Andersonab1c7a72015-03-02 09:34:59 +0000351 if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
352 report_fatal_error(
353 "ABI alignment specification must be >0 for non-aggregate types");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000354
355 // Preferred alignment.
356 unsigned PrefAlign = ABIAlign;
357 if (!Rest.empty()) {
358 Split = split(Rest, ':');
359 PrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000360 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000361
Patrik Hägglund01860a62012-11-14 09:04:56 +0000362 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmowb4faa152012-10-04 23:01:22 +0000363
Micah Villmowac34b5c2012-10-04 22:08:14 +0000364 break;
365 }
366 case 'n': // Native integer types.
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000367 while (true) {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000368 unsigned Width = getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000369 if (Width == 0)
370 report_fatal_error(
371 "Zero width native integer type in datalayout string");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000372 LegalIntWidths.push_back(Width);
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000373 if (Rest.empty())
374 break;
375 Split = split(Rest, ':');
376 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000377 break;
378 case 'S': { // Stack natural alignment.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000379 StackNaturalAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000380 break;
381 }
Dylan McKayced2fe62018-02-19 09:56:22 +0000382 case 'P': { // Function address space.
383 ProgramAddrSpace = getAddrSpace(Tok);
384 break;
385 }
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000386 case 'A': { // Default stack/alloca address space.
Dylan McKayced2fe62018-02-19 09:56:22 +0000387 AllocaAddrSpace = getAddrSpace(Tok);
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000388 break;
389 }
Rafael Espindola58873562014-01-03 19:21:54 +0000390 case 'm':
David Majnemer612f3122014-12-10 02:36:41 +0000391 if (!Tok.empty())
392 report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
393 if (Rest.empty())
394 report_fatal_error("Expected mangling specifier in datalayout string");
395 if (Rest.size() > 1)
396 report_fatal_error("Unknown mangling specifier in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000397 switch(Rest[0]) {
398 default:
David Majnemer5330c692014-12-10 01:17:08 +0000399 report_fatal_error("Unknown mangling in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000400 case 'e':
401 ManglingMode = MM_ELF;
402 break;
403 case 'o':
404 ManglingMode = MM_MachO;
405 break;
406 case 'm':
407 ManglingMode = MM_Mips;
408 break;
Rafael Espindolaaf77e122014-01-10 13:42:12 +0000409 case 'w':
David Majnemer7db449a2015-03-17 23:54:51 +0000410 ManglingMode = MM_WinCOFF;
411 break;
412 case 'x':
413 ManglingMode = MM_WinCOFFX86;
Rafael Espindola58873562014-01-03 19:21:54 +0000414 break;
415 }
416 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000417 default:
David Majnemer5330c692014-12-10 01:17:08 +0000418 report_fatal_error("Unknown specifier in datalayout string");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000419 break;
420 }
421 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000422}
423
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000424DataLayout::DataLayout(const Module *M) {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000425 init(M);
426}
427
Mehdi Amini46a43552015-03-04 18:43:29 +0000428void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000429
Rafael Espindolaae593f12014-02-26 17:02:08 +0000430bool DataLayout::operator==(const DataLayout &Other) const {
Chandler Carruthf67321c2014-10-20 10:41:29 +0000431 bool Ret = BigEndian == Other.BigEndian &&
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000432 AllocaAddrSpace == Other.AllocaAddrSpace &&
Rafael Espindolaae593f12014-02-26 17:02:08 +0000433 StackNaturalAlign == Other.StackNaturalAlign &&
Dylan McKayced2fe62018-02-19 09:56:22 +0000434 ProgramAddrSpace == Other.ProgramAddrSpace &&
Rafael Espindolaae593f12014-02-26 17:02:08 +0000435 ManglingMode == Other.ManglingMode &&
436 LegalIntWidths == Other.LegalIntWidths &&
Rafael Espindola89992b02014-04-22 17:47:03 +0000437 Alignments == Other.Alignments && Pointers == Other.Pointers;
Mehdi Amini46a43552015-03-04 18:43:29 +0000438 // Note: getStringRepresentation() might differs, it is not canonicalized
Rafael Espindolaae593f12014-02-26 17:02:08 +0000439 return Ret;
440}
441
Craig Topper490889c2017-03-23 06:15:56 +0000442DataLayout::AlignmentsTy::iterator
443DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
444 uint32_t BitWidth) {
445 auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
446 return std::lower_bound(Alignments.begin(), Alignments.end(), Pair,
447 [](const LayoutAlignElem &LHS,
448 const std::pair<unsigned, uint32_t> &RHS) {
449 return std::tie(LHS.AlignType, LHS.TypeBitWidth) <
450 std::tie(RHS.first, RHS.second);
451 });
452}
453
Micah Villmowac34b5c2012-10-04 22:08:14 +0000454void
Micah Villmowb4faa152012-10-04 23:01:22 +0000455DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000456 unsigned pref_align, uint32_t bit_width) {
David Majnemer1b9fc3a2015-02-16 05:41:53 +0000457 if (!isUInt<24>(bit_width))
458 report_fatal_error("Invalid bit width, must be a 24bit integer");
459 if (!isUInt<16>(abi_align))
460 report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
461 if (!isUInt<16>(pref_align))
462 report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
Owen Anderson5af4b212015-03-02 09:35:03 +0000463 if (abi_align != 0 && !isPowerOf2_64(abi_align))
464 report_fatal_error("Invalid ABI alignment, must be a power of 2");
465 if (pref_align != 0 && !isPowerOf2_64(pref_align))
466 report_fatal_error("Invalid preferred alignment, must be a power of 2");
David Majnemer1b9fc3a2015-02-16 05:41:53 +0000467
468 if (pref_align < abi_align)
469 report_fatal_error(
470 "Preferred alignment cannot be less than the ABI alignment");
471
Craig Topper490889c2017-03-23 06:15:56 +0000472 AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width);
473 if (I != Alignments.end() &&
474 I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) {
475 // Update the abi, preferred alignments.
476 I->ABIAlign = abi_align;
477 I->PrefAlign = pref_align;
478 } else {
479 // Insert before I to keep the vector sorted.
480 Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align,
481 pref_align, bit_width));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000482 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000483}
484
Rafael Espindola667fcb82014-02-26 16:58:35 +0000485DataLayout::PointersTy::iterator
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000486DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000487 return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000488 [](const PointerAlignElem &A, uint32_t AddressSpace) {
489 return A.AddressSpace < AddressSpace;
490 });
Rafael Espindola667fcb82014-02-26 16:58:35 +0000491}
492
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000493void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000494 unsigned PrefAlign, uint32_t TypeByteWidth,
495 uint32_t IndexWidth) {
David Majnemer4b042922015-02-16 05:41:55 +0000496 if (PrefAlign < ABIAlign)
497 report_fatal_error(
498 "Preferred alignment cannot be less than the ABI alignment");
499
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000500 PointersTy::iterator I = findPointerLowerBound(AddrSpace);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000501 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
502 Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000503 TypeByteWidth, IndexWidth));
Micah Villmow89021e42012-10-09 16:06:12 +0000504 } else {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000505 I->ABIAlign = ABIAlign;
506 I->PrefAlign = PrefAlign;
507 I->TypeByteWidth = TypeByteWidth;
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000508 I->IndexWidth = IndexWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000509 }
510}
511
Micah Villmowac34b5c2012-10-04 22:08:14 +0000512/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000513/// preferred if ABIInfo = false) the layout wants for the specified datatype.
514unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000515 uint32_t BitWidth, bool ABIInfo,
Pete Cooper0ae73932015-07-27 17:15:28 +0000516 Type *Ty) const {
Craig Topper490889c2017-03-23 06:15:56 +0000517 AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth);
518 // See if we found an exact match. Of if we are looking for an integer type,
519 // but don't have an exact match take the next largest integer. This is where
520 // the lower_bound will point to when it fails an exact match.
521 if (I != Alignments.end() && I->AlignType == (unsigned)AlignType &&
522 (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN))
523 return ABIInfo ? I->ABIAlign : I->PrefAlign;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000524
Craig Topper490889c2017-03-23 06:15:56 +0000525 if (AlignType == INTEGER_ALIGN) {
526 // If we didn't have a larger value try the largest value we have.
527 if (I != Alignments.begin()) {
528 --I; // Go to the previous entry and see if its an integer.
529 if (I->AlignType == INTEGER_ALIGN)
530 return ABIInfo ? I->ABIAlign : I->PrefAlign;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000531 }
Craig Topper490889c2017-03-23 06:15:56 +0000532 } else if (AlignType == VECTOR_ALIGN) {
533 // By default, use natural alignment for vector types. This is consistent
534 // with what clang and llvm-gcc do.
535 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
536 Align *= cast<VectorType>(Ty)->getNumElements();
537 Align = PowerOf2Ceil(Align);
538 return Align;
539 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000540
Owen Anderson7e621e92015-03-08 21:53:59 +0000541 // If we still couldn't find a reasonable default alignment, fall back
542 // to a simple heuristic that the alignment is the first power of two
543 // greater-or-equal to the store size of the type. This is a reasonable
544 // approximation of reality, and if the user wanted something less
545 // less conservative, they should have specified it explicitly in the data
546 // layout.
Craig Topper490889c2017-03-23 06:15:56 +0000547 unsigned Align = getTypeStoreSize(Ty);
548 Align = PowerOf2Ceil(Align);
549 return Align;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000550}
551
552namespace {
553
554class StructLayoutMap {
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000555 using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000556 LayoutInfoTy LayoutInfo;
557
558public:
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000559 ~StructLayoutMap() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000560 // Remove any layouts.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000561 for (const auto &I : LayoutInfo) {
562 StructLayout *Value = I.second;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000563 Value->~StructLayout();
564 free(Value);
565 }
566 }
567
Pete Cooper0ae73932015-07-27 17:15:28 +0000568 StructLayout *&operator[](StructType *STy) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000569 return LayoutInfo[STy];
570 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000571};
572
573} // end anonymous namespace
574
Rafael Espindola248ac132014-02-25 22:23:04 +0000575void DataLayout::clear() {
576 LegalIntWidths.clear();
577 Alignments.clear();
578 Pointers.clear();
579 delete static_cast<StructLayoutMap *>(LayoutMap);
Craig Topperc6207612014-04-09 06:08:46 +0000580 LayoutMap = nullptr;
Rafael Espindola248ac132014-02-25 22:23:04 +0000581}
582
Micah Villmowb4faa152012-10-04 23:01:22 +0000583DataLayout::~DataLayout() {
Rafael Espindola248ac132014-02-25 22:23:04 +0000584 clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000585}
586
Pete Cooper0ae73932015-07-27 17:15:28 +0000587const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000588 if (!LayoutMap)
589 LayoutMap = new StructLayoutMap();
590
591 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
592 StructLayout *&SL = (*STM)[Ty];
593 if (SL) return SL;
594
595 // Otherwise, create the struct layout. Because it is variable length, we
596 // malloc it, then use placement new.
597 int NumElts = Ty->getNumElements();
Serge Pavlov15681ad2018-06-09 05:19:45 +0000598 StructLayout *L = (StructLayout *)
599 safe_malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000600
601 // Set SL before calling StructLayout's ctor. The ctor could cause other
602 // entries to be added to TheMap, invalidating our reference.
603 SL = L;
604
605 new (L) StructLayout(Ty, *this);
606
607 return L;
608}
609
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000610unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000611 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000612 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000613 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000614 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000615 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000616 return I->ABIAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000617}
618
619unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000620 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000621 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000622 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000623 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000624 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000625 return I->PrefAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000626}
627
628unsigned DataLayout::getPointerSize(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000629 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000630 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000631 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000632 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000633 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000634 return I->TypeByteWidth;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000635}
636
Hal Finkel4f238142019-01-02 16:28:09 +0000637unsigned DataLayout::getMaxPointerSize() const {
638 unsigned MaxPointerSize = 0;
639 for (auto &P : Pointers)
640 MaxPointerSize = std::max(MaxPointerSize, P.TypeByteWidth);
641
642 return MaxPointerSize;
643}
644
Pete Cooper0ae73932015-07-27 17:15:28 +0000645unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
Matt Arsenault6f4be902013-07-26 17:37:20 +0000646 assert(Ty->isPtrOrPtrVectorTy() &&
647 "This should only be called with a pointer or pointer vector type");
Craig Topper5b4f5b02017-04-17 18:22:36 +0000648 Ty = Ty->getScalarType();
649 return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000650}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000651
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000652unsigned DataLayout::getIndexSize(unsigned AS) const {
653 PointersTy::const_iterator I = findPointerLowerBound(AS);
654 if (I == Pointers.end() || I->AddressSpace != AS) {
655 I = findPointerLowerBound(0);
656 assert(I->AddressSpace == 0);
657 }
658 return I->IndexWidth;
659}
660
661unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
662 assert(Ty->isPtrOrPtrVectorTy() &&
663 "This should only be called with a pointer or pointer vector type");
664 Ty = Ty->getScalarType();
665 return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
666}
667
Micah Villmowac34b5c2012-10-04 22:08:14 +0000668/*!
669 \param abi_or_pref Flag that determines which alignment is returned. true
670 returns the ABI alignment, false returns the preferred alignment.
671 \param Ty The underlying type for which alignment is determined.
672
673 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
674 == false) for the requested type \a Ty.
675 */
Pete Cooper0ae73932015-07-27 17:15:28 +0000676unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Craig Topperff6922a2017-04-19 00:31:38 +0000677 AlignTypeEnum AlignType;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000678
679 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
680 switch (Ty->getTypeID()) {
681 // Early escape for the non-numeric types.
682 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000683 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000684 ? getPointerABIAlignment(0)
685 : getPointerPrefAlignment(0));
686 case Type::PointerTyID: {
Matt Arsenaultc1728972014-09-18 22:28:56 +0000687 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Micah Villmow89021e42012-10-09 16:06:12 +0000688 return (abi_or_pref
689 ? getPointerABIAlignment(AS)
690 : getPointerPrefAlignment(AS));
691 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000692 case Type::ArrayTyID:
693 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
694
695 case Type::StructTyID: {
696 // Packed structure types always have an ABI alignment of one.
697 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
698 return 1;
699
700 // Get the layout annotation... which is lazily created on demand.
701 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
702 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
703 return std::max(Align, Layout->getAlignment());
704 }
705 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000706 AlignType = INTEGER_ALIGN;
707 break;
708 case Type::HalfTyID:
709 case Type::FloatTyID:
710 case Type::DoubleTyID:
711 // PPC_FP128TyID and FP128TyID have different data contents, but the
712 // same size and alignment, so they look the same here.
713 case Type::PPC_FP128TyID:
714 case Type::FP128TyID:
715 case Type::X86_FP80TyID:
716 AlignType = FLOAT_ALIGN;
717 break;
718 case Type::X86_MMXTyID:
719 case Type::VectorTyID:
720 AlignType = VECTOR_ALIGN;
721 break;
722 default:
723 llvm_unreachable("Bad type for getAlignment!!!");
724 }
725
Craig Topperff6922a2017-04-19 00:31:38 +0000726 return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty), abi_or_pref, Ty);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000727}
728
Pete Cooper0ae73932015-07-27 17:15:28 +0000729unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000730 return getAlignment(Ty, true);
731}
732
733/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
734/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000735unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Craig Topperc6207612014-04-09 06:08:46 +0000736 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000737}
738
Pete Cooper0ae73932015-07-27 17:15:28 +0000739unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000740 return getAlignment(Ty, false);
741}
742
Pete Cooper0ae73932015-07-27 17:15:28 +0000743unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000744 unsigned Align = getPrefTypeAlignment(Ty);
745 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
746 return Log2_32(Align);
747}
748
Micah Villmow89021e42012-10-09 16:06:12 +0000749IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
750 unsigned AddressSpace) const {
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000751 return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000752}
753
Pete Cooper0ae73932015-07-27 17:15:28 +0000754Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000755 assert(Ty->isPtrOrPtrVectorTy() &&
756 "Expected a pointer or pointer vector type.");
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000757 unsigned NumBits = getIndexTypeSizeInBits(Ty);
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000758 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
Pete Cooper0ae73932015-07-27 17:15:28 +0000759 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000760 return VectorType::get(IntTy, VecTy->getNumElements());
761 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000762}
763
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000764Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000765 for (unsigned LegalIntWidth : LegalIntWidths)
766 if (Width <= LegalIntWidth)
767 return Type::getIntNTy(C, LegalIntWidth);
Craig Topperc6207612014-04-09 06:08:46 +0000768 return nullptr;
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000769}
770
Jun Bum Limbe11bdc2016-05-13 18:38:35 +0000771unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000772 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
773 return Max != LegalIntWidths.end() ? *Max : 0;
Matt Arsenault899f7d22013-09-16 22:43:16 +0000774}
775
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000776Type *DataLayout::getIndexType(Type *Ty) const {
777 assert(Ty->isPtrOrPtrVectorTy() &&
778 "Expected a pointer or pointer vector type.");
779 unsigned NumBits = getIndexTypeSizeInBits(Ty);
780 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
781 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
782 return VectorType::get(IntTy, VecTy->getNumElements());
783 return IntTy;
784}
785
David Majnemer17bdf442016-07-13 03:42:38 +0000786int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
787 ArrayRef<Value *> Indices) const {
788 int64_t Result = 0;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000789
790 generic_gep_type_iterator<Value* const*>
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000791 GTI = gep_type_begin(ElemTy, Indices),
792 GTE = gep_type_end(ElemTy, Indices);
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000793 for (; GTI != GTE; ++GTI) {
794 Value *Idx = GTI.getOperand();
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000795 if (StructType *STy = GTI.getStructTypeOrNull()) {
Manuel Jacobcc13c2c2016-01-22 03:30:27 +0000796 assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000797 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000798
799 // Get structure layout information...
800 const StructLayout *Layout = getStructLayout(STy);
801
802 // Add in the offset, as calculated by the structure layout info...
803 Result += Layout->getElementOffset(FieldNo);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000804 } else {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000805 // Get the array index and the size of each array element.
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000806 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
David Majnemer17bdf442016-07-13 03:42:38 +0000807 Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000808 }
809 }
810
811 return Result;
812}
813
814/// getPreferredAlignment - Return the preferred alignment of the specified
815/// global. This includes an explicitly requested alignment (if the global
816/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000817unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Eli Friedman37696392018-08-29 23:46:26 +0000818 unsigned GVAlignment = GV->getAlignment();
819 // If a section is specified, always precisely honor explicit alignment,
820 // so we don't insert padding into a section we don't control.
821 if (GVAlignment && GV->hasSection())
822 return GVAlignment;
823
824 // If no explicit alignment is specified, compute the alignment based on
825 // the IR type. If an alignment is specified, increase it to match the ABI
826 // alignment of the IR type.
827 //
828 // FIXME: Not sure it makes sense to use the alignment of the type if
829 // there's already an explicit alignment specification.
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000830 Type *ElemType = GV->getValueType();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000831 unsigned Alignment = getPrefTypeAlignment(ElemType);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000832 if (GVAlignment >= Alignment) {
833 Alignment = GVAlignment;
834 } else if (GVAlignment != 0) {
835 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
836 }
837
Eli Friedman37696392018-08-29 23:46:26 +0000838 // If no explicit alignment is specified, and the global is large, increase
839 // the alignment to 16.
840 // FIXME: Why 16, specifically?
Micah Villmowac34b5c2012-10-04 22:08:14 +0000841 if (GV->hasInitializer() && GVAlignment == 0) {
842 if (Alignment < 16) {
843 // If the global is not external, see if it is large. If so, give it a
844 // larger alignment.
845 if (getTypeSizeInBits(ElemType) > 128)
846 Alignment = 16; // 16-byte alignment.
847 }
848 }
849 return Alignment;
850}
851
852/// getPreferredAlignmentLog - Return the preferred alignment of the
853/// specified global, returned in log form. This includes an explicitly
854/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000855unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000856 return Log2_32(getPreferredAlignment(GV));
857}