blob: 943f5381c64971eedea7018dfbe4944b824ff7eb [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;
Michael Platings308e82e2019-03-08 10:44:06 +0000187 FunctionPtrAlign = 0;
188 TheFunctionPtrAlignType = FunctionPtrAlignType::Independent;
Rafael Espindola58873562014-01-03 19:21:54 +0000189 ManglingMode = MM_None;
Sanjoy Dasc6af5ea2016-07-28 23:43:38 +0000190 NonIntegralAddressSpaces.clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000191
192 // Default alignments
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000193 for (const LayoutAlignElem &E : DefaultAlignments) {
Rafael Espindola458a4852013-12-19 23:03:03 +0000194 setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
195 E.TypeBitWidth);
196 }
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000197 setPointerAlignment(0, 8, 8, 8, 8);
Patrik Hägglund01860a62012-11-14 09:04:56 +0000198
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000199 parseSpecifier(Desc);
200}
201
202/// Checked version of split, to ensure mandatory subparts.
203static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
204 assert(!Str.empty() && "parse error, string can't be empty here");
205 std::pair<StringRef, StringRef> Split = Str.split(Separator);
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000206 if (Split.second.empty() && Split.first != Str)
207 report_fatal_error("Trailing separator in datalayout string");
David Majnemer612f3122014-12-10 02:36:41 +0000208 if (!Split.second.empty() && Split.first.empty())
209 report_fatal_error("Expected token before separator in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000210 return Split;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000211}
212
Cameron McInally8af9eac2014-01-07 19:51:38 +0000213/// Get an unsigned integer, including error checks.
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000214static unsigned getInt(StringRef R) {
Cameron McInallyf0379fa2014-01-13 22:04:55 +0000215 unsigned Result;
Patrik Hägglund504f4782012-11-28 14:32:52 +0000216 bool error = R.getAsInteger(10, Result); (void)error;
Cameron McInallyf0379fa2014-01-13 22:04:55 +0000217 if (error)
218 report_fatal_error("not a number, or does not fit in an unsigned int");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000219 return Result;
220}
221
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000222/// Convert bits into bytes. Assert if not a byte width multiple.
223static unsigned inBytes(unsigned Bits) {
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000224 if (Bits % 8)
225 report_fatal_error("number of bits must be a byte width multiple");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000226 return Bits / 8;
227}
228
Dylan McKayced2fe62018-02-19 09:56:22 +0000229static unsigned getAddrSpace(StringRef R) {
230 unsigned AddrSpace = getInt(R);
231 if (!isUInt<24>(AddrSpace))
232 report_fatal_error("Invalid address space, must be a 24-bit integer");
233 return AddrSpace;
234}
235
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000236void DataLayout::parseSpecifier(StringRef Desc) {
Mehdi Amini46a43552015-03-04 18:43:29 +0000237 StringRepresentation = Desc;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000238 while (!Desc.empty()) {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000239 // Split at '-'.
240 std::pair<StringRef, StringRef> Split = split(Desc, '-');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000241 Desc = Split.second;
242
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000243 // Split at ':'.
244 Split = split(Split.first, ':');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000245
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000246 // Aliases used below.
247 StringRef &Tok = Split.first; // Current token.
248 StringRef &Rest = Split.second; // The rest of the string.
Micah Villmowac34b5c2012-10-04 22:08:14 +0000249
Sanjoy Dasc6af5ea2016-07-28 23:43:38 +0000250 if (Tok == "ni") {
251 do {
252 Split = split(Rest, ':');
253 Rest = Split.second;
254 unsigned AS = getInt(Split.first);
255 if (AS == 0)
256 report_fatal_error("Address space 0 can never be non-integral");
257 NonIntegralAddressSpaces.push_back(AS);
258 } while (!Rest.empty());
259
260 continue;
261 }
262
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000263 char Specifier = Tok.front();
264 Tok = Tok.substr(1);
265
266 switch (Specifier) {
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000267 case 's':
268 // Ignored for backward compatibility.
269 // FIXME: remove this on LLVM 4.0.
270 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000271 case 'E':
Chandler Carruthf67321c2014-10-20 10:41:29 +0000272 BigEndian = true;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000273 break;
274 case 'e':
Chandler Carruthf67321c2014-10-20 10:41:29 +0000275 BigEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000276 break;
277 case 'p': {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000278 // Address space.
279 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000280 if (!isUInt<24>(AddrSpace))
281 report_fatal_error("Invalid address space, must be a 24bit integer");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000282
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000283 // Size.
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000284 if (Rest.empty())
285 report_fatal_error(
286 "Missing size specification for pointer in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000287 Split = split(Rest, ':');
288 unsigned PointerMemSize = inBytes(getInt(Tok));
Owen Anderson5bc2bbe2015-03-02 06:00:02 +0000289 if (!PointerMemSize)
290 report_fatal_error("Invalid pointer size of 0 bytes");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000291
292 // ABI alignment.
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000293 if (Rest.empty())
294 report_fatal_error(
295 "Missing alignment specification for pointer in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000296 Split = split(Rest, ':');
297 unsigned PointerABIAlign = inBytes(getInt(Tok));
Owen Anderson040f2f82015-03-02 06:33:51 +0000298 if (!isPowerOf2_64(PointerABIAlign))
299 report_fatal_error(
300 "Pointer ABI alignment must be a power of 2");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000301
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000302 // Size of index used in GEP for address calculation.
303 // The parameter is optional. By default it is equal to size of pointer.
304 unsigned IndexSize = PointerMemSize;
305
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000306 // Preferred alignment.
307 unsigned PointerPrefAlign = PointerABIAlign;
308 if (!Rest.empty()) {
309 Split = split(Rest, ':');
310 PointerPrefAlign = inBytes(getInt(Tok));
Owen Anderson040f2f82015-03-02 06:33:51 +0000311 if (!isPowerOf2_64(PointerPrefAlign))
312 report_fatal_error(
313 "Pointer preferred alignment must be a power of 2");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000314
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000315 // Now read the index. It is the second optional parameter here.
316 if (!Rest.empty()) {
317 Split = split(Rest, ':');
318 IndexSize = inBytes(getInt(Tok));
319 if (!IndexSize)
320 report_fatal_error("Invalid index size of 0 bytes");
321 }
322 }
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000323 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000324 PointerMemSize, IndexSize);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000325 break;
326 }
327 case 'i':
328 case 'v':
329 case 'f':
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000330 case 'a': {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000331 AlignTypeEnum AlignType;
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000332 switch (Specifier) {
Craig Topper64a65ec2017-05-22 19:28:36 +0000333 default: llvm_unreachable("Unexpected specifier!");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000334 case 'i': AlignType = INTEGER_ALIGN; break;
335 case 'v': AlignType = VECTOR_ALIGN; break;
336 case 'f': AlignType = FLOAT_ALIGN; break;
337 case 'a': AlignType = AGGREGATE_ALIGN; break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000338 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000339
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000340 // Bit size.
341 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
342
David Majnemer5330c692014-12-10 01:17:08 +0000343 if (AlignType == AGGREGATE_ALIGN && Size != 0)
344 report_fatal_error(
345 "Sized aggregate specification in datalayout string");
Rafael Espindolaabdd7262014-01-06 21:40:24 +0000346
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000347 // ABI alignment.
David Majnemer612f3122014-12-10 02:36:41 +0000348 if (Rest.empty())
349 report_fatal_error(
350 "Missing alignment specification in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000351 Split = split(Rest, ':');
352 unsigned ABIAlign = inBytes(getInt(Tok));
Owen Andersonab1c7a72015-03-02 09:34:59 +0000353 if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
354 report_fatal_error(
355 "ABI alignment specification must be >0 for non-aggregate types");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000356
357 // Preferred alignment.
358 unsigned PrefAlign = ABIAlign;
359 if (!Rest.empty()) {
360 Split = split(Rest, ':');
361 PrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000362 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000363
Patrik Hägglund01860a62012-11-14 09:04:56 +0000364 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmowb4faa152012-10-04 23:01:22 +0000365
Micah Villmowac34b5c2012-10-04 22:08:14 +0000366 break;
367 }
368 case 'n': // Native integer types.
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000369 while (true) {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000370 unsigned Width = getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000371 if (Width == 0)
372 report_fatal_error(
373 "Zero width native integer type in datalayout string");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000374 LegalIntWidths.push_back(Width);
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000375 if (Rest.empty())
376 break;
377 Split = split(Rest, ':');
378 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000379 break;
380 case 'S': { // Stack natural alignment.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000381 StackNaturalAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000382 break;
383 }
Michael Platings308e82e2019-03-08 10:44:06 +0000384 case 'F': {
385 switch (Tok.front()) {
386 case 'i':
387 TheFunctionPtrAlignType = FunctionPtrAlignType::Independent;
388 break;
389 case 'n':
390 TheFunctionPtrAlignType = FunctionPtrAlignType::MultipleOfFunctionAlign;
391 break;
392 default:
393 report_fatal_error("Unknown function pointer alignment type in "
394 "datalayout string");
395 }
396 Tok = Tok.substr(1);
397 FunctionPtrAlign = inBytes(getInt(Tok));
398 break;
399 }
Dylan McKayced2fe62018-02-19 09:56:22 +0000400 case 'P': { // Function address space.
401 ProgramAddrSpace = getAddrSpace(Tok);
402 break;
403 }
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000404 case 'A': { // Default stack/alloca address space.
Dylan McKayced2fe62018-02-19 09:56:22 +0000405 AllocaAddrSpace = getAddrSpace(Tok);
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000406 break;
407 }
Rafael Espindola58873562014-01-03 19:21:54 +0000408 case 'm':
David Majnemer612f3122014-12-10 02:36:41 +0000409 if (!Tok.empty())
410 report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
411 if (Rest.empty())
412 report_fatal_error("Expected mangling specifier in datalayout string");
413 if (Rest.size() > 1)
414 report_fatal_error("Unknown mangling specifier in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000415 switch(Rest[0]) {
416 default:
David Majnemer5330c692014-12-10 01:17:08 +0000417 report_fatal_error("Unknown mangling in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000418 case 'e':
419 ManglingMode = MM_ELF;
420 break;
421 case 'o':
422 ManglingMode = MM_MachO;
423 break;
424 case 'm':
425 ManglingMode = MM_Mips;
426 break;
Rafael Espindolaaf77e122014-01-10 13:42:12 +0000427 case 'w':
David Majnemer7db449a2015-03-17 23:54:51 +0000428 ManglingMode = MM_WinCOFF;
429 break;
430 case 'x':
431 ManglingMode = MM_WinCOFFX86;
Rafael Espindola58873562014-01-03 19:21:54 +0000432 break;
433 }
434 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000435 default:
David Majnemer5330c692014-12-10 01:17:08 +0000436 report_fatal_error("Unknown specifier in datalayout string");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000437 break;
438 }
439 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000440}
441
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000442DataLayout::DataLayout(const Module *M) {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000443 init(M);
444}
445
Mehdi Amini46a43552015-03-04 18:43:29 +0000446void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000447
Rafael Espindolaae593f12014-02-26 17:02:08 +0000448bool DataLayout::operator==(const DataLayout &Other) const {
Chandler Carruthf67321c2014-10-20 10:41:29 +0000449 bool Ret = BigEndian == Other.BigEndian &&
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000450 AllocaAddrSpace == Other.AllocaAddrSpace &&
Rafael Espindolaae593f12014-02-26 17:02:08 +0000451 StackNaturalAlign == Other.StackNaturalAlign &&
Dylan McKayced2fe62018-02-19 09:56:22 +0000452 ProgramAddrSpace == Other.ProgramAddrSpace &&
Michael Platings308e82e2019-03-08 10:44:06 +0000453 FunctionPtrAlign == Other.FunctionPtrAlign &&
454 TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType &&
Rafael Espindolaae593f12014-02-26 17:02:08 +0000455 ManglingMode == Other.ManglingMode &&
456 LegalIntWidths == Other.LegalIntWidths &&
Rafael Espindola89992b02014-04-22 17:47:03 +0000457 Alignments == Other.Alignments && Pointers == Other.Pointers;
Mehdi Amini46a43552015-03-04 18:43:29 +0000458 // Note: getStringRepresentation() might differs, it is not canonicalized
Rafael Espindolaae593f12014-02-26 17:02:08 +0000459 return Ret;
460}
461
Craig Topper490889c2017-03-23 06:15:56 +0000462DataLayout::AlignmentsTy::iterator
463DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
464 uint32_t BitWidth) {
465 auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
466 return std::lower_bound(Alignments.begin(), Alignments.end(), Pair,
467 [](const LayoutAlignElem &LHS,
468 const std::pair<unsigned, uint32_t> &RHS) {
469 return std::tie(LHS.AlignType, LHS.TypeBitWidth) <
470 std::tie(RHS.first, RHS.second);
471 });
472}
473
Micah Villmowac34b5c2012-10-04 22:08:14 +0000474void
Micah Villmowb4faa152012-10-04 23:01:22 +0000475DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000476 unsigned pref_align, uint32_t bit_width) {
David Majnemer1b9fc3a2015-02-16 05:41:53 +0000477 if (!isUInt<24>(bit_width))
478 report_fatal_error("Invalid bit width, must be a 24bit integer");
479 if (!isUInt<16>(abi_align))
480 report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
481 if (!isUInt<16>(pref_align))
482 report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
Owen Anderson5af4b212015-03-02 09:35:03 +0000483 if (abi_align != 0 && !isPowerOf2_64(abi_align))
484 report_fatal_error("Invalid ABI alignment, must be a power of 2");
485 if (pref_align != 0 && !isPowerOf2_64(pref_align))
486 report_fatal_error("Invalid preferred alignment, must be a power of 2");
David Majnemer1b9fc3a2015-02-16 05:41:53 +0000487
488 if (pref_align < abi_align)
489 report_fatal_error(
490 "Preferred alignment cannot be less than the ABI alignment");
491
Craig Topper490889c2017-03-23 06:15:56 +0000492 AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width);
493 if (I != Alignments.end() &&
494 I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) {
495 // Update the abi, preferred alignments.
496 I->ABIAlign = abi_align;
497 I->PrefAlign = pref_align;
498 } else {
499 // Insert before I to keep the vector sorted.
500 Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align,
501 pref_align, bit_width));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000502 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000503}
504
Rafael Espindola667fcb82014-02-26 16:58:35 +0000505DataLayout::PointersTy::iterator
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000506DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000507 return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000508 [](const PointerAlignElem &A, uint32_t AddressSpace) {
509 return A.AddressSpace < AddressSpace;
510 });
Rafael Espindola667fcb82014-02-26 16:58:35 +0000511}
512
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000513void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000514 unsigned PrefAlign, uint32_t TypeByteWidth,
515 uint32_t IndexWidth) {
David Majnemer4b042922015-02-16 05:41:55 +0000516 if (PrefAlign < ABIAlign)
517 report_fatal_error(
518 "Preferred alignment cannot be less than the ABI alignment");
519
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000520 PointersTy::iterator I = findPointerLowerBound(AddrSpace);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000521 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
522 Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000523 TypeByteWidth, IndexWidth));
Micah Villmow89021e42012-10-09 16:06:12 +0000524 } else {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000525 I->ABIAlign = ABIAlign;
526 I->PrefAlign = PrefAlign;
527 I->TypeByteWidth = TypeByteWidth;
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000528 I->IndexWidth = IndexWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000529 }
530}
531
Micah Villmowac34b5c2012-10-04 22:08:14 +0000532/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000533/// preferred if ABIInfo = false) the layout wants for the specified datatype.
534unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000535 uint32_t BitWidth, bool ABIInfo,
Pete Cooper0ae73932015-07-27 17:15:28 +0000536 Type *Ty) const {
Craig Topper490889c2017-03-23 06:15:56 +0000537 AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth);
538 // See if we found an exact match. Of if we are looking for an integer type,
539 // but don't have an exact match take the next largest integer. This is where
540 // the lower_bound will point to when it fails an exact match.
541 if (I != Alignments.end() && I->AlignType == (unsigned)AlignType &&
542 (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN))
543 return ABIInfo ? I->ABIAlign : I->PrefAlign;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000544
Craig Topper490889c2017-03-23 06:15:56 +0000545 if (AlignType == INTEGER_ALIGN) {
546 // If we didn't have a larger value try the largest value we have.
547 if (I != Alignments.begin()) {
548 --I; // Go to the previous entry and see if its an integer.
549 if (I->AlignType == INTEGER_ALIGN)
550 return ABIInfo ? I->ABIAlign : I->PrefAlign;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000551 }
Craig Topper490889c2017-03-23 06:15:56 +0000552 } else if (AlignType == VECTOR_ALIGN) {
553 // By default, use natural alignment for vector types. This is consistent
554 // with what clang and llvm-gcc do.
555 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
556 Align *= cast<VectorType>(Ty)->getNumElements();
557 Align = PowerOf2Ceil(Align);
558 return Align;
559 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000560
Owen Anderson7e621e92015-03-08 21:53:59 +0000561 // If we still couldn't find a reasonable default alignment, fall back
562 // to a simple heuristic that the alignment is the first power of two
563 // greater-or-equal to the store size of the type. This is a reasonable
564 // approximation of reality, and if the user wanted something less
565 // less conservative, they should have specified it explicitly in the data
566 // layout.
Craig Topper490889c2017-03-23 06:15:56 +0000567 unsigned Align = getTypeStoreSize(Ty);
568 Align = PowerOf2Ceil(Align);
569 return Align;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000570}
571
572namespace {
573
574class StructLayoutMap {
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000575 using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000576 LayoutInfoTy LayoutInfo;
577
578public:
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000579 ~StructLayoutMap() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000580 // Remove any layouts.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000581 for (const auto &I : LayoutInfo) {
582 StructLayout *Value = I.second;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000583 Value->~StructLayout();
584 free(Value);
585 }
586 }
587
Pete Cooper0ae73932015-07-27 17:15:28 +0000588 StructLayout *&operator[](StructType *STy) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000589 return LayoutInfo[STy];
590 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000591};
592
593} // end anonymous namespace
594
Rafael Espindola248ac132014-02-25 22:23:04 +0000595void DataLayout::clear() {
596 LegalIntWidths.clear();
597 Alignments.clear();
598 Pointers.clear();
599 delete static_cast<StructLayoutMap *>(LayoutMap);
Craig Topperc6207612014-04-09 06:08:46 +0000600 LayoutMap = nullptr;
Rafael Espindola248ac132014-02-25 22:23:04 +0000601}
602
Micah Villmowb4faa152012-10-04 23:01:22 +0000603DataLayout::~DataLayout() {
Rafael Espindola248ac132014-02-25 22:23:04 +0000604 clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000605}
606
Pete Cooper0ae73932015-07-27 17:15:28 +0000607const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000608 if (!LayoutMap)
609 LayoutMap = new StructLayoutMap();
610
611 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
612 StructLayout *&SL = (*STM)[Ty];
613 if (SL) return SL;
614
615 // Otherwise, create the struct layout. Because it is variable length, we
616 // malloc it, then use placement new.
617 int NumElts = Ty->getNumElements();
Serge Pavlov15681ad2018-06-09 05:19:45 +0000618 StructLayout *L = (StructLayout *)
619 safe_malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000620
621 // Set SL before calling StructLayout's ctor. The ctor could cause other
622 // entries to be added to TheMap, invalidating our reference.
623 SL = L;
624
625 new (L) StructLayout(Ty, *this);
626
627 return L;
628}
629
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000630unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000631 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000632 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000633 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000634 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000635 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000636 return I->ABIAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000637}
638
639unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000640 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000641 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000642 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000643 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000644 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000645 return I->PrefAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000646}
647
648unsigned DataLayout::getPointerSize(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000649 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000650 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000651 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000652 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000653 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000654 return I->TypeByteWidth;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000655}
656
Hal Finkel4f238142019-01-02 16:28:09 +0000657unsigned DataLayout::getMaxPointerSize() const {
658 unsigned MaxPointerSize = 0;
659 for (auto &P : Pointers)
660 MaxPointerSize = std::max(MaxPointerSize, P.TypeByteWidth);
661
662 return MaxPointerSize;
663}
664
Pete Cooper0ae73932015-07-27 17:15:28 +0000665unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
Matt Arsenault6f4be902013-07-26 17:37:20 +0000666 assert(Ty->isPtrOrPtrVectorTy() &&
667 "This should only be called with a pointer or pointer vector type");
Craig Topper5b4f5b02017-04-17 18:22:36 +0000668 Ty = Ty->getScalarType();
669 return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000670}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000671
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000672unsigned DataLayout::getIndexSize(unsigned AS) const {
673 PointersTy::const_iterator I = findPointerLowerBound(AS);
674 if (I == Pointers.end() || I->AddressSpace != AS) {
675 I = findPointerLowerBound(0);
676 assert(I->AddressSpace == 0);
677 }
678 return I->IndexWidth;
679}
680
681unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
682 assert(Ty->isPtrOrPtrVectorTy() &&
683 "This should only be called with a pointer or pointer vector type");
684 Ty = Ty->getScalarType();
685 return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
686}
687
Micah Villmowac34b5c2012-10-04 22:08:14 +0000688/*!
689 \param abi_or_pref Flag that determines which alignment is returned. true
690 returns the ABI alignment, false returns the preferred alignment.
691 \param Ty The underlying type for which alignment is determined.
692
693 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
694 == false) for the requested type \a Ty.
695 */
Pete Cooper0ae73932015-07-27 17:15:28 +0000696unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Craig Topperff6922a2017-04-19 00:31:38 +0000697 AlignTypeEnum AlignType;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000698
699 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
700 switch (Ty->getTypeID()) {
701 // Early escape for the non-numeric types.
702 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000703 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000704 ? getPointerABIAlignment(0)
705 : getPointerPrefAlignment(0));
706 case Type::PointerTyID: {
Matt Arsenaultc1728972014-09-18 22:28:56 +0000707 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Micah Villmow89021e42012-10-09 16:06:12 +0000708 return (abi_or_pref
709 ? getPointerABIAlignment(AS)
710 : getPointerPrefAlignment(AS));
711 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000712 case Type::ArrayTyID:
713 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
714
715 case Type::StructTyID: {
716 // Packed structure types always have an ABI alignment of one.
717 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
718 return 1;
719
720 // Get the layout annotation... which is lazily created on demand.
721 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
722 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
723 return std::max(Align, Layout->getAlignment());
724 }
725 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000726 AlignType = INTEGER_ALIGN;
727 break;
728 case Type::HalfTyID:
729 case Type::FloatTyID:
730 case Type::DoubleTyID:
731 // PPC_FP128TyID and FP128TyID have different data contents, but the
732 // same size and alignment, so they look the same here.
733 case Type::PPC_FP128TyID:
734 case Type::FP128TyID:
735 case Type::X86_FP80TyID:
736 AlignType = FLOAT_ALIGN;
737 break;
738 case Type::X86_MMXTyID:
739 case Type::VectorTyID:
740 AlignType = VECTOR_ALIGN;
741 break;
742 default:
743 llvm_unreachable("Bad type for getAlignment!!!");
744 }
745
Craig Topperff6922a2017-04-19 00:31:38 +0000746 return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty), abi_or_pref, Ty);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000747}
748
Pete Cooper0ae73932015-07-27 17:15:28 +0000749unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000750 return getAlignment(Ty, true);
751}
752
753/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
754/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000755unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Craig Topperc6207612014-04-09 06:08:46 +0000756 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000757}
758
Pete Cooper0ae73932015-07-27 17:15:28 +0000759unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000760 return getAlignment(Ty, false);
761}
762
Pete Cooper0ae73932015-07-27 17:15:28 +0000763unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000764 unsigned Align = getPrefTypeAlignment(Ty);
765 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
766 return Log2_32(Align);
767}
768
Micah Villmow89021e42012-10-09 16:06:12 +0000769IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
770 unsigned AddressSpace) const {
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000771 return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000772}
773
Pete Cooper0ae73932015-07-27 17:15:28 +0000774Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000775 assert(Ty->isPtrOrPtrVectorTy() &&
776 "Expected a pointer or pointer vector type.");
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000777 unsigned NumBits = getIndexTypeSizeInBits(Ty);
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000778 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
Pete Cooper0ae73932015-07-27 17:15:28 +0000779 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000780 return VectorType::get(IntTy, VecTy->getNumElements());
781 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000782}
783
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000784Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000785 for (unsigned LegalIntWidth : LegalIntWidths)
786 if (Width <= LegalIntWidth)
787 return Type::getIntNTy(C, LegalIntWidth);
Craig Topperc6207612014-04-09 06:08:46 +0000788 return nullptr;
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000789}
790
Jun Bum Limbe11bdc2016-05-13 18:38:35 +0000791unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000792 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
793 return Max != LegalIntWidths.end() ? *Max : 0;
Matt Arsenault899f7d22013-09-16 22:43:16 +0000794}
795
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000796Type *DataLayout::getIndexType(Type *Ty) const {
797 assert(Ty->isPtrOrPtrVectorTy() &&
798 "Expected a pointer or pointer vector type.");
799 unsigned NumBits = getIndexTypeSizeInBits(Ty);
800 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
801 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
802 return VectorType::get(IntTy, VecTy->getNumElements());
803 return IntTy;
804}
805
David Majnemer17bdf442016-07-13 03:42:38 +0000806int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
807 ArrayRef<Value *> Indices) const {
808 int64_t Result = 0;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000809
810 generic_gep_type_iterator<Value* const*>
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000811 GTI = gep_type_begin(ElemTy, Indices),
812 GTE = gep_type_end(ElemTy, Indices);
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000813 for (; GTI != GTE; ++GTI) {
814 Value *Idx = GTI.getOperand();
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000815 if (StructType *STy = GTI.getStructTypeOrNull()) {
Manuel Jacobcc13c2c2016-01-22 03:30:27 +0000816 assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000817 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000818
819 // Get structure layout information...
820 const StructLayout *Layout = getStructLayout(STy);
821
822 // Add in the offset, as calculated by the structure layout info...
823 Result += Layout->getElementOffset(FieldNo);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000824 } else {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000825 // Get the array index and the size of each array element.
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000826 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
David Majnemer17bdf442016-07-13 03:42:38 +0000827 Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000828 }
829 }
830
831 return Result;
832}
833
834/// getPreferredAlignment - Return the preferred alignment of the specified
835/// global. This includes an explicitly requested alignment (if the global
836/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000837unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Eli Friedman37696392018-08-29 23:46:26 +0000838 unsigned GVAlignment = GV->getAlignment();
839 // If a section is specified, always precisely honor explicit alignment,
840 // so we don't insert padding into a section we don't control.
841 if (GVAlignment && GV->hasSection())
842 return GVAlignment;
843
844 // If no explicit alignment is specified, compute the alignment based on
845 // the IR type. If an alignment is specified, increase it to match the ABI
846 // alignment of the IR type.
847 //
848 // FIXME: Not sure it makes sense to use the alignment of the type if
849 // there's already an explicit alignment specification.
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000850 Type *ElemType = GV->getValueType();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000851 unsigned Alignment = getPrefTypeAlignment(ElemType);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000852 if (GVAlignment >= Alignment) {
853 Alignment = GVAlignment;
854 } else if (GVAlignment != 0) {
855 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
856 }
857
Eli Friedman37696392018-08-29 23:46:26 +0000858 // If no explicit alignment is specified, and the global is large, increase
859 // the alignment to 16.
860 // FIXME: Why 16, specifically?
Micah Villmowac34b5c2012-10-04 22:08:14 +0000861 if (GV->hasInitializer() && GVAlignment == 0) {
862 if (Alignment < 16) {
863 // If the global is not external, see if it is large. If so, give it a
864 // larger alignment.
865 if (getTypeSizeInBits(ElemType) > 128)
866 Alignment = 16; // 16-byte alignment.
867 }
868 }
869 return Alignment;
870}
871
872/// getPreferredAlignmentLog - Return the preferred alignment of the
873/// specified global, returned in log form. This includes an explicitly
874/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000875unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000876 return Log2_32(getPreferredAlignment(GV));
877}