blob: 6e0ebbd4a730ba5cdbdb41328129119f224a84af [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);
Fangrui Song78ee2fb2019-06-30 11:19:56 +0000466 return partition_point(Alignments, [=](const LayoutAlignElem &E) {
467 return std::make_pair(E.AlignType, E.TypeBitWidth) < Pair;
Fangrui Songdc8de602019-06-21 05:40:31 +0000468 });
Craig Topper490889c2017-03-23 06:15:56 +0000469}
470
Micah Villmowac34b5c2012-10-04 22:08:14 +0000471void
Micah Villmowb4faa152012-10-04 23:01:22 +0000472DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000473 unsigned pref_align, uint32_t bit_width) {
David Majnemer1b9fc3a2015-02-16 05:41:53 +0000474 if (!isUInt<24>(bit_width))
475 report_fatal_error("Invalid bit width, must be a 24bit integer");
476 if (!isUInt<16>(abi_align))
477 report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
478 if (!isUInt<16>(pref_align))
479 report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
Owen Anderson5af4b212015-03-02 09:35:03 +0000480 if (abi_align != 0 && !isPowerOf2_64(abi_align))
481 report_fatal_error("Invalid ABI alignment, must be a power of 2");
482 if (pref_align != 0 && !isPowerOf2_64(pref_align))
483 report_fatal_error("Invalid preferred alignment, must be a power of 2");
David Majnemer1b9fc3a2015-02-16 05:41:53 +0000484
485 if (pref_align < abi_align)
486 report_fatal_error(
487 "Preferred alignment cannot be less than the ABI alignment");
488
Craig Topper490889c2017-03-23 06:15:56 +0000489 AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width);
490 if (I != Alignments.end() &&
491 I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) {
492 // Update the abi, preferred alignments.
493 I->ABIAlign = abi_align;
494 I->PrefAlign = pref_align;
495 } else {
496 // Insert before I to keep the vector sorted.
497 Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align,
498 pref_align, bit_width));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000499 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000500}
501
Rafael Espindola667fcb82014-02-26 16:58:35 +0000502DataLayout::PointersTy::iterator
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000503DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000504 return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000505 [](const PointerAlignElem &A, uint32_t AddressSpace) {
506 return A.AddressSpace < AddressSpace;
507 });
Rafael Espindola667fcb82014-02-26 16:58:35 +0000508}
509
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000510void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000511 unsigned PrefAlign, uint32_t TypeByteWidth,
512 uint32_t IndexWidth) {
David Majnemer4b042922015-02-16 05:41:55 +0000513 if (PrefAlign < ABIAlign)
514 report_fatal_error(
515 "Preferred alignment cannot be less than the ABI alignment");
516
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000517 PointersTy::iterator I = findPointerLowerBound(AddrSpace);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000518 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
519 Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000520 TypeByteWidth, IndexWidth));
Micah Villmow89021e42012-10-09 16:06:12 +0000521 } else {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000522 I->ABIAlign = ABIAlign;
523 I->PrefAlign = PrefAlign;
524 I->TypeByteWidth = TypeByteWidth;
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000525 I->IndexWidth = IndexWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000526 }
527}
528
Micah Villmowac34b5c2012-10-04 22:08:14 +0000529/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000530/// preferred if ABIInfo = false) the layout wants for the specified datatype.
531unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000532 uint32_t BitWidth, bool ABIInfo,
Pete Cooper0ae73932015-07-27 17:15:28 +0000533 Type *Ty) const {
Craig Topper490889c2017-03-23 06:15:56 +0000534 AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth);
535 // See if we found an exact match. Of if we are looking for an integer type,
536 // but don't have an exact match take the next largest integer. This is where
537 // the lower_bound will point to when it fails an exact match.
538 if (I != Alignments.end() && I->AlignType == (unsigned)AlignType &&
539 (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN))
540 return ABIInfo ? I->ABIAlign : I->PrefAlign;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000541
Craig Topper490889c2017-03-23 06:15:56 +0000542 if (AlignType == INTEGER_ALIGN) {
543 // If we didn't have a larger value try the largest value we have.
544 if (I != Alignments.begin()) {
545 --I; // Go to the previous entry and see if its an integer.
546 if (I->AlignType == INTEGER_ALIGN)
547 return ABIInfo ? I->ABIAlign : I->PrefAlign;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000548 }
Craig Topper490889c2017-03-23 06:15:56 +0000549 } else if (AlignType == VECTOR_ALIGN) {
550 // By default, use natural alignment for vector types. This is consistent
551 // with what clang and llvm-gcc do.
552 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
553 Align *= cast<VectorType>(Ty)->getNumElements();
554 Align = PowerOf2Ceil(Align);
555 return Align;
556 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000557
Owen Anderson7e621e92015-03-08 21:53:59 +0000558 // If we still couldn't find a reasonable default alignment, fall back
559 // to a simple heuristic that the alignment is the first power of two
560 // greater-or-equal to the store size of the type. This is a reasonable
561 // approximation of reality, and if the user wanted something less
562 // less conservative, they should have specified it explicitly in the data
563 // layout.
Craig Topper490889c2017-03-23 06:15:56 +0000564 unsigned Align = getTypeStoreSize(Ty);
565 Align = PowerOf2Ceil(Align);
566 return Align;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000567}
568
569namespace {
570
571class StructLayoutMap {
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000572 using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000573 LayoutInfoTy LayoutInfo;
574
575public:
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000576 ~StructLayoutMap() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000577 // Remove any layouts.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000578 for (const auto &I : LayoutInfo) {
579 StructLayout *Value = I.second;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000580 Value->~StructLayout();
581 free(Value);
582 }
583 }
584
Pete Cooper0ae73932015-07-27 17:15:28 +0000585 StructLayout *&operator[](StructType *STy) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000586 return LayoutInfo[STy];
587 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000588};
589
590} // end anonymous namespace
591
Rafael Espindola248ac132014-02-25 22:23:04 +0000592void DataLayout::clear() {
593 LegalIntWidths.clear();
594 Alignments.clear();
595 Pointers.clear();
596 delete static_cast<StructLayoutMap *>(LayoutMap);
Craig Topperc6207612014-04-09 06:08:46 +0000597 LayoutMap = nullptr;
Rafael Espindola248ac132014-02-25 22:23:04 +0000598}
599
Micah Villmowb4faa152012-10-04 23:01:22 +0000600DataLayout::~DataLayout() {
Rafael Espindola248ac132014-02-25 22:23:04 +0000601 clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000602}
603
Pete Cooper0ae73932015-07-27 17:15:28 +0000604const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000605 if (!LayoutMap)
606 LayoutMap = new StructLayoutMap();
607
608 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
609 StructLayout *&SL = (*STM)[Ty];
610 if (SL) return SL;
611
612 // Otherwise, create the struct layout. Because it is variable length, we
613 // malloc it, then use placement new.
614 int NumElts = Ty->getNumElements();
Serge Pavlov15681ad2018-06-09 05:19:45 +0000615 StructLayout *L = (StructLayout *)
616 safe_malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000617
618 // Set SL before calling StructLayout's ctor. The ctor could cause other
619 // entries to be added to TheMap, invalidating our reference.
620 SL = L;
621
622 new (L) StructLayout(Ty, *this);
623
624 return L;
625}
626
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000627unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000628 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000629 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000630 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000631 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000632 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000633 return I->ABIAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000634}
635
636unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000637 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000638 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000639 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000640 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000641 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000642 return I->PrefAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000643}
644
645unsigned DataLayout::getPointerSize(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000646 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000647 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000648 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000649 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000650 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000651 return I->TypeByteWidth;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000652}
653
Hal Finkel4f238142019-01-02 16:28:09 +0000654unsigned DataLayout::getMaxPointerSize() const {
655 unsigned MaxPointerSize = 0;
656 for (auto &P : Pointers)
657 MaxPointerSize = std::max(MaxPointerSize, P.TypeByteWidth);
658
659 return MaxPointerSize;
660}
661
Pete Cooper0ae73932015-07-27 17:15:28 +0000662unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
Matt Arsenault6f4be902013-07-26 17:37:20 +0000663 assert(Ty->isPtrOrPtrVectorTy() &&
664 "This should only be called with a pointer or pointer vector type");
Craig Topper5b4f5b02017-04-17 18:22:36 +0000665 Ty = Ty->getScalarType();
666 return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000667}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000668
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000669unsigned DataLayout::getIndexSize(unsigned AS) const {
670 PointersTy::const_iterator I = findPointerLowerBound(AS);
671 if (I == Pointers.end() || I->AddressSpace != AS) {
672 I = findPointerLowerBound(0);
673 assert(I->AddressSpace == 0);
674 }
675 return I->IndexWidth;
676}
677
678unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const {
679 assert(Ty->isPtrOrPtrVectorTy() &&
680 "This should only be called with a pointer or pointer vector type");
681 Ty = Ty->getScalarType();
682 return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
683}
684
Micah Villmowac34b5c2012-10-04 22:08:14 +0000685/*!
686 \param abi_or_pref Flag that determines which alignment is returned. true
687 returns the ABI alignment, false returns the preferred alignment.
688 \param Ty The underlying type for which alignment is determined.
689
690 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
691 == false) for the requested type \a Ty.
692 */
Pete Cooper0ae73932015-07-27 17:15:28 +0000693unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Craig Topperff6922a2017-04-19 00:31:38 +0000694 AlignTypeEnum AlignType;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000695
696 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
697 switch (Ty->getTypeID()) {
698 // Early escape for the non-numeric types.
699 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000700 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000701 ? getPointerABIAlignment(0)
702 : getPointerPrefAlignment(0));
703 case Type::PointerTyID: {
Matt Arsenaultc1728972014-09-18 22:28:56 +0000704 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Micah Villmow89021e42012-10-09 16:06:12 +0000705 return (abi_or_pref
706 ? getPointerABIAlignment(AS)
707 : getPointerPrefAlignment(AS));
708 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000709 case Type::ArrayTyID:
710 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
711
712 case Type::StructTyID: {
713 // Packed structure types always have an ABI alignment of one.
714 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
715 return 1;
716
717 // Get the layout annotation... which is lazily created on demand.
718 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
719 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
720 return std::max(Align, Layout->getAlignment());
721 }
722 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000723 AlignType = INTEGER_ALIGN;
724 break;
725 case Type::HalfTyID:
726 case Type::FloatTyID:
727 case Type::DoubleTyID:
728 // PPC_FP128TyID and FP128TyID have different data contents, but the
729 // same size and alignment, so they look the same here.
730 case Type::PPC_FP128TyID:
731 case Type::FP128TyID:
732 case Type::X86_FP80TyID:
733 AlignType = FLOAT_ALIGN;
734 break;
735 case Type::X86_MMXTyID:
736 case Type::VectorTyID:
737 AlignType = VECTOR_ALIGN;
738 break;
739 default:
740 llvm_unreachable("Bad type for getAlignment!!!");
741 }
742
Craig Topperff6922a2017-04-19 00:31:38 +0000743 return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty), abi_or_pref, Ty);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000744}
745
Pete Cooper0ae73932015-07-27 17:15:28 +0000746unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000747 return getAlignment(Ty, true);
748}
749
750/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
751/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000752unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Craig Topperc6207612014-04-09 06:08:46 +0000753 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000754}
755
Pete Cooper0ae73932015-07-27 17:15:28 +0000756unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000757 return getAlignment(Ty, false);
758}
759
Pete Cooper0ae73932015-07-27 17:15:28 +0000760unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000761 unsigned Align = getPrefTypeAlignment(Ty);
762 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
763 return Log2_32(Align);
764}
765
Micah Villmow89021e42012-10-09 16:06:12 +0000766IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
767 unsigned AddressSpace) const {
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000768 return IntegerType::get(C, getIndexSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000769}
770
Pete Cooper0ae73932015-07-27 17:15:28 +0000771Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000772 assert(Ty->isPtrOrPtrVectorTy() &&
773 "Expected a pointer or pointer vector type.");
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000774 unsigned NumBits = getIndexTypeSizeInBits(Ty);
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000775 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
Pete Cooper0ae73932015-07-27 17:15:28 +0000776 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000777 return VectorType::get(IntTy, VecTy->getNumElements());
778 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000779}
780
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000781Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000782 for (unsigned LegalIntWidth : LegalIntWidths)
783 if (Width <= LegalIntWidth)
784 return Type::getIntNTy(C, LegalIntWidth);
Craig Topperc6207612014-04-09 06:08:46 +0000785 return nullptr;
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000786}
787
Jun Bum Limbe11bdc2016-05-13 18:38:35 +0000788unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000789 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
790 return Max != LegalIntWidths.end() ? *Max : 0;
Matt Arsenault899f7d22013-09-16 22:43:16 +0000791}
792
Elena Demikhovsky945b7e52018-02-14 06:58:08 +0000793Type *DataLayout::getIndexType(Type *Ty) const {
794 assert(Ty->isPtrOrPtrVectorTy() &&
795 "Expected a pointer or pointer vector type.");
796 unsigned NumBits = getIndexTypeSizeInBits(Ty);
797 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
798 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
799 return VectorType::get(IntTy, VecTy->getNumElements());
800 return IntTy;
801}
802
David Majnemer17bdf442016-07-13 03:42:38 +0000803int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
804 ArrayRef<Value *> Indices) const {
805 int64_t Result = 0;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000806
807 generic_gep_type_iterator<Value* const*>
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000808 GTI = gep_type_begin(ElemTy, Indices),
809 GTE = gep_type_end(ElemTy, Indices);
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000810 for (; GTI != GTE; ++GTI) {
811 Value *Idx = GTI.getOperand();
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000812 if (StructType *STy = GTI.getStructTypeOrNull()) {
Manuel Jacobcc13c2c2016-01-22 03:30:27 +0000813 assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000814 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000815
816 // Get structure layout information...
817 const StructLayout *Layout = getStructLayout(STy);
818
819 // Add in the offset, as calculated by the structure layout info...
820 Result += Layout->getElementOffset(FieldNo);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000821 } else {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000822 // Get the array index and the size of each array element.
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000823 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
David Majnemer17bdf442016-07-13 03:42:38 +0000824 Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000825 }
826 }
827
828 return Result;
829}
830
831/// getPreferredAlignment - Return the preferred alignment of the specified
832/// global. This includes an explicitly requested alignment (if the global
833/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000834unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Eli Friedman37696392018-08-29 23:46:26 +0000835 unsigned GVAlignment = GV->getAlignment();
836 // If a section is specified, always precisely honor explicit alignment,
837 // so we don't insert padding into a section we don't control.
838 if (GVAlignment && GV->hasSection())
839 return GVAlignment;
840
841 // If no explicit alignment is specified, compute the alignment based on
842 // the IR type. If an alignment is specified, increase it to match the ABI
843 // alignment of the IR type.
844 //
845 // FIXME: Not sure it makes sense to use the alignment of the type if
846 // there's already an explicit alignment specification.
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000847 Type *ElemType = GV->getValueType();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000848 unsigned Alignment = getPrefTypeAlignment(ElemType);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000849 if (GVAlignment >= Alignment) {
850 Alignment = GVAlignment;
851 } else if (GVAlignment != 0) {
852 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
853 }
854
Eli Friedman37696392018-08-29 23:46:26 +0000855 // If no explicit alignment is specified, and the global is large, increase
856 // the alignment to 16.
857 // FIXME: Why 16, specifically?
Micah Villmowac34b5c2012-10-04 22:08:14 +0000858 if (GV->hasInitializer() && GVAlignment == 0) {
859 if (Alignment < 16) {
860 // If the global is not external, see if it is large. If so, give it a
861 // larger alignment.
862 if (getTypeSizeInBits(ElemType) > 128)
863 Alignment = 16; // 16-byte alignment.
864 }
865 }
866 return Alignment;
867}
868
869/// getPreferredAlignmentLog - Return the preferred alignment of the
870/// specified global, returned in log form. This includes an explicitly
871/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000872unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000873 return Log2_32(getPreferredAlignment(GV));
874}