blob: c72057ed146b7c80c87038c53def6363bbe85b0b [file] [log] [blame]
Micah Villmowb4faa152012-10-04 23:01:22 +00001//===-- DataLayout.cpp - Data size & alignment routines --------------------==//
Micah Villmowac34b5c2012-10-04 22:08:14 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Micah Villmowb4faa152012-10-04 23:01:22 +000010// This file defines layout properties related to datatype size/offset/alignment
Micah Villmowac34b5c2012-10-04 22:08:14 +000011// information.
12//
13// This structure should be created once, filled in if the defaults are not
14// correct and then passed around by const&. None of the members functions
15// require modification to the object.
16//
17//===----------------------------------------------------------------------===//
18
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/DataLayout.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/DenseMap.h"
Rafael Espindola458a4852013-12-19 23:03:03 +000021#include "llvm/ADT/STLExtras.h"
Rafael Espindola58873562014-01-03 19:21:54 +000022#include "llvm/ADT/Triple.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000025#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Module.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000027#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/MathExtras.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000030#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/raw_ostream.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000032#include <algorithm>
33#include <cstdlib>
34using namespace llvm;
35
Micah Villmowac34b5c2012-10-04 22:08:14 +000036//===----------------------------------------------------------------------===//
37// Support for StructLayout
38//===----------------------------------------------------------------------===//
39
Pete Cooper0ae73932015-07-27 17:15:28 +000040StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
Micah Villmowac34b5c2012-10-04 22:08:14 +000041 assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
42 StructAlignment = 0;
43 StructSize = 0;
Mehdi Amini1c131b32015-12-15 01:44:07 +000044 IsPadded = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +000045 NumElements = ST->getNumElements();
46
47 // Loop over each of the elements, placing them in memory.
48 for (unsigned i = 0, e = NumElements; i != e; ++i) {
49 Type *Ty = ST->getElementType(i);
Eli Bendersky41913c72013-04-16 15:41:18 +000050 unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
Micah Villmowac34b5c2012-10-04 22:08:14 +000051
52 // Add padding if necessary to align the data element properly.
Mehdi Amini1c131b32015-12-15 01:44:07 +000053 if ((StructSize & (TyAlign-1)) != 0) {
54 IsPadded = true;
Rui Ueyamada00f2f2016-01-14 21:06:47 +000055 StructSize = alignTo(StructSize, TyAlign);
Mehdi Amini1c131b32015-12-15 01:44:07 +000056 }
Micah Villmowac34b5c2012-10-04 22:08:14 +000057
58 // Keep track of maximum alignment constraint.
59 StructAlignment = std::max(TyAlign, StructAlignment);
60
61 MemberOffsets[i] = StructSize;
Eli Bendersky41913c72013-04-16 15:41:18 +000062 StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
Micah Villmowac34b5c2012-10-04 22:08:14 +000063 }
64
65 // Empty structures have alignment of 1 byte.
66 if (StructAlignment == 0) StructAlignment = 1;
67
68 // Add padding to the end of the struct so that it could be put in an array
69 // and all array elements would be aligned correctly.
Mehdi Amini1c131b32015-12-15 01:44:07 +000070 if ((StructSize & (StructAlignment-1)) != 0) {
71 IsPadded = true;
Rui Ueyamada00f2f2016-01-14 21:06:47 +000072 StructSize = alignTo(StructSize, StructAlignment);
Mehdi Amini1c131b32015-12-15 01:44:07 +000073 }
Micah Villmowac34b5c2012-10-04 22:08:14 +000074}
75
76
77/// getElementContainingOffset - Given a valid offset into the structure,
78/// return the structure index that contains it.
79unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
80 const uint64_t *SI =
81 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
82 assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
83 --SI;
84 assert(*SI <= Offset && "upper_bound didn't work");
85 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
86 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
87 "Upper bound didn't work!");
88
89 // Multiple fields can have the same offset if any of them are zero sized.
90 // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
91 // at the i32 element, because it is the last element at that offset. This is
92 // the right one to return, because anything after it will have a higher
93 // offset, implying that this element is non-empty.
94 return SI-&MemberOffsets[0];
95}
96
97//===----------------------------------------------------------------------===//
Micah Villmowb4faa152012-10-04 23:01:22 +000098// LayoutAlignElem, LayoutAlign support
Micah Villmowac34b5c2012-10-04 22:08:14 +000099//===----------------------------------------------------------------------===//
100
Micah Villmowb4faa152012-10-04 23:01:22 +0000101LayoutAlignElem
102LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000103 unsigned pref_align, uint32_t bit_width) {
104 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
Micah Villmowb4faa152012-10-04 23:01:22 +0000105 LayoutAlignElem retval;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000106 retval.AlignType = align_type;
107 retval.ABIAlign = abi_align;
108 retval.PrefAlign = pref_align;
109 retval.TypeBitWidth = bit_width;
110 return retval;
111}
112
113bool
Micah Villmowb4faa152012-10-04 23:01:22 +0000114LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000115 return (AlignType == rhs.AlignType
116 && ABIAlign == rhs.ABIAlign
117 && PrefAlign == rhs.PrefAlign
118 && TypeBitWidth == rhs.TypeBitWidth);
119}
120
Micah Villmow89021e42012-10-09 16:06:12 +0000121//===----------------------------------------------------------------------===//
122// PointerAlignElem, PointerAlign support
123//===----------------------------------------------------------------------===//
124
125PointerAlignElem
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000126PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
127 unsigned PrefAlign, uint32_t TypeByteWidth) {
128 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
Micah Villmow89021e42012-10-09 16:06:12 +0000129 PointerAlignElem retval;
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000130 retval.AddressSpace = AddressSpace;
131 retval.ABIAlign = ABIAlign;
132 retval.PrefAlign = PrefAlign;
133 retval.TypeByteWidth = TypeByteWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000134 return retval;
135}
136
137bool
138PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
139 return (ABIAlign == rhs.ABIAlign
140 && AddressSpace == rhs.AddressSpace
141 && PrefAlign == rhs.PrefAlign
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000142 && TypeByteWidth == rhs.TypeByteWidth);
Micah Villmow89021e42012-10-09 16:06:12 +0000143}
144
Micah Villmowac34b5c2012-10-04 22:08:14 +0000145//===----------------------------------------------------------------------===//
Micah Villmowb4faa152012-10-04 23:01:22 +0000146// DataLayout Class Implementation
Micah Villmowac34b5c2012-10-04 22:08:14 +0000147//===----------------------------------------------------------------------===//
148
Rafael Espindola58873562014-01-03 19:21:54 +0000149const char *DataLayout::getManglingComponent(const Triple &T) {
150 if (T.isOSBinFormatMachO())
151 return "-m:o";
David Majnemer7db449a2015-03-17 23:54:51 +0000152 if (T.isOSWindows() && T.isOSBinFormatCOFF())
153 return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000154 return "-m:e";
Rafael Espindola58873562014-01-03 19:21:54 +0000155}
156
Rafael Espindolae23b8772013-12-20 15:21:32 +0000157static const LayoutAlignElem DefaultAlignments[] = {
Rafael Espindola458a4852013-12-19 23:03:03 +0000158 { INTEGER_ALIGN, 1, 1, 1 }, // i1
159 { INTEGER_ALIGN, 8, 1, 1 }, // i8
160 { INTEGER_ALIGN, 16, 2, 2 }, // i16
161 { INTEGER_ALIGN, 32, 4, 4 }, // i32
162 { INTEGER_ALIGN, 64, 4, 8 }, // i64
163 { FLOAT_ALIGN, 16, 2, 2 }, // half
164 { FLOAT_ALIGN, 32, 4, 4 }, // float
165 { FLOAT_ALIGN, 64, 8, 8 }, // double
166 { FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ...
167 { VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ...
168 { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
169 { AGGREGATE_ALIGN, 0, 0, 8 } // struct
170};
171
Rafael Espindola248ac132014-02-25 22:23:04 +0000172void DataLayout::reset(StringRef Desc) {
173 clear();
174
Craig Topperc6207612014-04-09 06:08:46 +0000175 LayoutMap = nullptr;
Chandler Carruthf67321c2014-10-20 10:41:29 +0000176 BigEndian = false;
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000177 AllocaAddrSpace = 0;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000178 StackNaturalAlign = 0;
Rafael Espindola58873562014-01-03 19:21:54 +0000179 ManglingMode = MM_None;
Sanjoy Dasc6af5ea2016-07-28 23:43:38 +0000180 NonIntegralAddressSpaces.clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000181
182 // Default alignments
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000183 for (const LayoutAlignElem &E : DefaultAlignments) {
Rafael Espindola458a4852013-12-19 23:03:03 +0000184 setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
185 E.TypeBitWidth);
186 }
Micah Villmow89021e42012-10-09 16:06:12 +0000187 setPointerAlignment(0, 8, 8, 8);
Patrik Hägglund01860a62012-11-14 09:04:56 +0000188
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000189 parseSpecifier(Desc);
190}
191
192/// Checked version of split, to ensure mandatory subparts.
193static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
194 assert(!Str.empty() && "parse error, string can't be empty here");
195 std::pair<StringRef, StringRef> Split = Str.split(Separator);
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000196 if (Split.second.empty() && Split.first != Str)
197 report_fatal_error("Trailing separator in datalayout string");
David Majnemer612f3122014-12-10 02:36:41 +0000198 if (!Split.second.empty() && Split.first.empty())
199 report_fatal_error("Expected token before separator in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000200 return Split;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000201}
202
Cameron McInally8af9eac2014-01-07 19:51:38 +0000203/// Get an unsigned integer, including error checks.
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000204static unsigned getInt(StringRef R) {
Cameron McInallyf0379fa2014-01-13 22:04:55 +0000205 unsigned Result;
Patrik Hägglund504f4782012-11-28 14:32:52 +0000206 bool error = R.getAsInteger(10, Result); (void)error;
Cameron McInallyf0379fa2014-01-13 22:04:55 +0000207 if (error)
208 report_fatal_error("not a number, or does not fit in an unsigned int");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000209 return Result;
210}
211
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000212/// Convert bits into bytes. Assert if not a byte width multiple.
213static unsigned inBytes(unsigned Bits) {
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000214 if (Bits % 8)
215 report_fatal_error("number of bits must be a byte width multiple");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000216 return Bits / 8;
217}
218
219void DataLayout::parseSpecifier(StringRef Desc) {
Mehdi Amini46a43552015-03-04 18:43:29 +0000220 StringRepresentation = Desc;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000221 while (!Desc.empty()) {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000222 // Split at '-'.
223 std::pair<StringRef, StringRef> Split = split(Desc, '-');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000224 Desc = Split.second;
225
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000226 // Split at ':'.
227 Split = split(Split.first, ':');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000228
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000229 // Aliases used below.
230 StringRef &Tok = Split.first; // Current token.
231 StringRef &Rest = Split.second; // The rest of the string.
Micah Villmowac34b5c2012-10-04 22:08:14 +0000232
Sanjoy Dasc6af5ea2016-07-28 23:43:38 +0000233 if (Tok == "ni") {
234 do {
235 Split = split(Rest, ':');
236 Rest = Split.second;
237 unsigned AS = getInt(Split.first);
238 if (AS == 0)
239 report_fatal_error("Address space 0 can never be non-integral");
240 NonIntegralAddressSpaces.push_back(AS);
241 } while (!Rest.empty());
242
243 continue;
244 }
245
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000246 char Specifier = Tok.front();
247 Tok = Tok.substr(1);
248
249 switch (Specifier) {
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000250 case 's':
251 // Ignored for backward compatibility.
252 // FIXME: remove this on LLVM 4.0.
253 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000254 case 'E':
Chandler Carruthf67321c2014-10-20 10:41:29 +0000255 BigEndian = true;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000256 break;
257 case 'e':
Chandler Carruthf67321c2014-10-20 10:41:29 +0000258 BigEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000259 break;
260 case 'p': {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000261 // Address space.
262 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000263 if (!isUInt<24>(AddrSpace))
264 report_fatal_error("Invalid address space, must be a 24bit integer");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000265
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000266 // Size.
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000267 if (Rest.empty())
268 report_fatal_error(
269 "Missing size specification for pointer in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000270 Split = split(Rest, ':');
271 unsigned PointerMemSize = inBytes(getInt(Tok));
Owen Anderson5bc2bbe2015-03-02 06:00:02 +0000272 if (!PointerMemSize)
273 report_fatal_error("Invalid pointer size of 0 bytes");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000274
275 // ABI alignment.
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000276 if (Rest.empty())
277 report_fatal_error(
278 "Missing alignment specification for pointer in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000279 Split = split(Rest, ':');
280 unsigned PointerABIAlign = inBytes(getInt(Tok));
Owen Anderson040f2f82015-03-02 06:33:51 +0000281 if (!isPowerOf2_64(PointerABIAlign))
282 report_fatal_error(
283 "Pointer ABI alignment must be a power of 2");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000284
285 // Preferred alignment.
286 unsigned PointerPrefAlign = PointerABIAlign;
287 if (!Rest.empty()) {
288 Split = split(Rest, ':');
289 PointerPrefAlign = inBytes(getInt(Tok));
Owen Anderson040f2f82015-03-02 06:33:51 +0000290 if (!isPowerOf2_64(PointerPrefAlign))
291 report_fatal_error(
292 "Pointer preferred alignment must be a power of 2");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000293 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000294
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000295 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
296 PointerMemSize);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000297 break;
298 }
299 case 'i':
300 case 'v':
301 case 'f':
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000302 case 'a': {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000303 AlignTypeEnum AlignType;
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000304 switch (Specifier) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000305 default:
306 case 'i': AlignType = INTEGER_ALIGN; break;
307 case 'v': AlignType = VECTOR_ALIGN; break;
308 case 'f': AlignType = FLOAT_ALIGN; break;
309 case 'a': AlignType = AGGREGATE_ALIGN; break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000310 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000311
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000312 // Bit size.
313 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
314
David Majnemer5330c692014-12-10 01:17:08 +0000315 if (AlignType == AGGREGATE_ALIGN && Size != 0)
316 report_fatal_error(
317 "Sized aggregate specification in datalayout string");
Rafael Espindolaabdd7262014-01-06 21:40:24 +0000318
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000319 // ABI alignment.
David Majnemer612f3122014-12-10 02:36:41 +0000320 if (Rest.empty())
321 report_fatal_error(
322 "Missing alignment specification in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000323 Split = split(Rest, ':');
324 unsigned ABIAlign = inBytes(getInt(Tok));
Owen Andersonab1c7a72015-03-02 09:34:59 +0000325 if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
326 report_fatal_error(
327 "ABI alignment specification must be >0 for non-aggregate types");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000328
329 // Preferred alignment.
330 unsigned PrefAlign = ABIAlign;
331 if (!Rest.empty()) {
332 Split = split(Rest, ':');
333 PrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000334 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000335
Patrik Hägglund01860a62012-11-14 09:04:56 +0000336 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmowb4faa152012-10-04 23:01:22 +0000337
Micah Villmowac34b5c2012-10-04 22:08:14 +0000338 break;
339 }
340 case 'n': // Native integer types.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000341 for (;;) {
342 unsigned Width = getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000343 if (Width == 0)
344 report_fatal_error(
345 "Zero width native integer type in datalayout string");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000346 LegalIntWidths.push_back(Width);
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000347 if (Rest.empty())
348 break;
349 Split = split(Rest, ':');
350 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000351 break;
352 case 'S': { // Stack natural alignment.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000353 StackNaturalAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000354 break;
355 }
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000356 case 'A': { // Default stack/alloca address space.
357 AllocaAddrSpace = getInt(Tok);
358 if (!isUInt<24>(AllocaAddrSpace))
359 report_fatal_error("Invalid address space, must be a 24bit integer");
360 break;
361 }
Rafael Espindola58873562014-01-03 19:21:54 +0000362 case 'm':
David Majnemer612f3122014-12-10 02:36:41 +0000363 if (!Tok.empty())
364 report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
365 if (Rest.empty())
366 report_fatal_error("Expected mangling specifier in datalayout string");
367 if (Rest.size() > 1)
368 report_fatal_error("Unknown mangling specifier in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000369 switch(Rest[0]) {
370 default:
David Majnemer5330c692014-12-10 01:17:08 +0000371 report_fatal_error("Unknown mangling in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000372 case 'e':
373 ManglingMode = MM_ELF;
374 break;
375 case 'o':
376 ManglingMode = MM_MachO;
377 break;
378 case 'm':
379 ManglingMode = MM_Mips;
380 break;
Rafael Espindolaaf77e122014-01-10 13:42:12 +0000381 case 'w':
David Majnemer7db449a2015-03-17 23:54:51 +0000382 ManglingMode = MM_WinCOFF;
383 break;
384 case 'x':
385 ManglingMode = MM_WinCOFFX86;
Rafael Espindola58873562014-01-03 19:21:54 +0000386 break;
387 }
388 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000389 default:
David Majnemer5330c692014-12-10 01:17:08 +0000390 report_fatal_error("Unknown specifier in datalayout string");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000391 break;
392 }
393 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000394}
395
Craig Topperc6207612014-04-09 06:08:46 +0000396DataLayout::DataLayout(const Module *M) : LayoutMap(nullptr) {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000397 init(M);
398}
399
Mehdi Amini46a43552015-03-04 18:43:29 +0000400void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000401
Rafael Espindolaae593f12014-02-26 17:02:08 +0000402bool DataLayout::operator==(const DataLayout &Other) const {
Chandler Carruthf67321c2014-10-20 10:41:29 +0000403 bool Ret = BigEndian == Other.BigEndian &&
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000404 AllocaAddrSpace == Other.AllocaAddrSpace &&
Rafael Espindolaae593f12014-02-26 17:02:08 +0000405 StackNaturalAlign == Other.StackNaturalAlign &&
406 ManglingMode == Other.ManglingMode &&
407 LegalIntWidths == Other.LegalIntWidths &&
Rafael Espindola89992b02014-04-22 17:47:03 +0000408 Alignments == Other.Alignments && Pointers == Other.Pointers;
Mehdi Amini46a43552015-03-04 18:43:29 +0000409 // Note: getStringRepresentation() might differs, it is not canonicalized
Rafael Espindolaae593f12014-02-26 17:02:08 +0000410 return Ret;
411}
412
Craig Topper490889c2017-03-23 06:15:56 +0000413DataLayout::AlignmentsTy::iterator
414DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
415 uint32_t BitWidth) {
416 auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
417 return std::lower_bound(Alignments.begin(), Alignments.end(), Pair,
418 [](const LayoutAlignElem &LHS,
419 const std::pair<unsigned, uint32_t> &RHS) {
420 return std::tie(LHS.AlignType, LHS.TypeBitWidth) <
421 std::tie(RHS.first, RHS.second);
422 });
423}
424
Micah Villmowac34b5c2012-10-04 22:08:14 +0000425void
Micah Villmowb4faa152012-10-04 23:01:22 +0000426DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000427 unsigned pref_align, uint32_t bit_width) {
David Majnemer1b9fc3a2015-02-16 05:41:53 +0000428 if (!isUInt<24>(bit_width))
429 report_fatal_error("Invalid bit width, must be a 24bit integer");
430 if (!isUInt<16>(abi_align))
431 report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
432 if (!isUInt<16>(pref_align))
433 report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
Owen Anderson5af4b212015-03-02 09:35:03 +0000434 if (abi_align != 0 && !isPowerOf2_64(abi_align))
435 report_fatal_error("Invalid ABI alignment, must be a power of 2");
436 if (pref_align != 0 && !isPowerOf2_64(pref_align))
437 report_fatal_error("Invalid preferred alignment, must be a power of 2");
David Majnemer1b9fc3a2015-02-16 05:41:53 +0000438
439 if (pref_align < abi_align)
440 report_fatal_error(
441 "Preferred alignment cannot be less than the ABI alignment");
442
Craig Topper490889c2017-03-23 06:15:56 +0000443 AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width);
444 if (I != Alignments.end() &&
445 I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) {
446 // Update the abi, preferred alignments.
447 I->ABIAlign = abi_align;
448 I->PrefAlign = pref_align;
449 } else {
450 // Insert before I to keep the vector sorted.
451 Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align,
452 pref_align, bit_width));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000453 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000454}
455
Rafael Espindola667fcb82014-02-26 16:58:35 +0000456DataLayout::PointersTy::iterator
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000457DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000458 return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000459 [](const PointerAlignElem &A, uint32_t AddressSpace) {
460 return A.AddressSpace < AddressSpace;
461 });
Rafael Espindola667fcb82014-02-26 16:58:35 +0000462}
463
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000464void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
465 unsigned PrefAlign,
466 uint32_t TypeByteWidth) {
David Majnemer4b042922015-02-16 05:41:55 +0000467 if (PrefAlign < ABIAlign)
468 report_fatal_error(
469 "Preferred alignment cannot be less than the ABI alignment");
470
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000471 PointersTy::iterator I = findPointerLowerBound(AddrSpace);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000472 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
473 Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
474 TypeByteWidth));
Micah Villmow89021e42012-10-09 16:06:12 +0000475 } else {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000476 I->ABIAlign = ABIAlign;
477 I->PrefAlign = PrefAlign;
478 I->TypeByteWidth = TypeByteWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000479 }
480}
481
Micah Villmowac34b5c2012-10-04 22:08:14 +0000482/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000483/// preferred if ABIInfo = false) the layout wants for the specified datatype.
484unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000485 uint32_t BitWidth, bool ABIInfo,
Pete Cooper0ae73932015-07-27 17:15:28 +0000486 Type *Ty) const {
Craig Topper490889c2017-03-23 06:15:56 +0000487 AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth);
488 // See if we found an exact match. Of if we are looking for an integer type,
489 // but don't have an exact match take the next largest integer. This is where
490 // the lower_bound will point to when it fails an exact match.
491 if (I != Alignments.end() && I->AlignType == (unsigned)AlignType &&
492 (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN))
493 return ABIInfo ? I->ABIAlign : I->PrefAlign;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000494
Craig Topper490889c2017-03-23 06:15:56 +0000495 if (AlignType == INTEGER_ALIGN) {
496 // If we didn't have a larger value try the largest value we have.
497 if (I != Alignments.begin()) {
498 --I; // Go to the previous entry and see if its an integer.
499 if (I->AlignType == INTEGER_ALIGN)
500 return ABIInfo ? I->ABIAlign : I->PrefAlign;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000501 }
Craig Topper490889c2017-03-23 06:15:56 +0000502 } else if (AlignType == VECTOR_ALIGN) {
503 // By default, use natural alignment for vector types. This is consistent
504 // with what clang and llvm-gcc do.
505 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
506 Align *= cast<VectorType>(Ty)->getNumElements();
507 Align = PowerOf2Ceil(Align);
508 return Align;
509 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000510
Owen Anderson7e621e92015-03-08 21:53:59 +0000511 // If we still couldn't find a reasonable default alignment, fall back
512 // to a simple heuristic that the alignment is the first power of two
513 // greater-or-equal to the store size of the type. This is a reasonable
514 // approximation of reality, and if the user wanted something less
515 // less conservative, they should have specified it explicitly in the data
516 // layout.
Craig Topper490889c2017-03-23 06:15:56 +0000517 unsigned Align = getTypeStoreSize(Ty);
518 Align = PowerOf2Ceil(Align);
519 return Align;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000520}
521
522namespace {
523
524class StructLayoutMap {
Pete Cooper0ae73932015-07-27 17:15:28 +0000525 typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000526 LayoutInfoTy LayoutInfo;
527
528public:
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000529 ~StructLayoutMap() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000530 // Remove any layouts.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000531 for (const auto &I : LayoutInfo) {
532 StructLayout *Value = I.second;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000533 Value->~StructLayout();
534 free(Value);
535 }
536 }
537
Pete Cooper0ae73932015-07-27 17:15:28 +0000538 StructLayout *&operator[](StructType *STy) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000539 return LayoutInfo[STy];
540 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000541};
542
543} // end anonymous namespace
544
Rafael Espindola248ac132014-02-25 22:23:04 +0000545void DataLayout::clear() {
546 LegalIntWidths.clear();
547 Alignments.clear();
548 Pointers.clear();
549 delete static_cast<StructLayoutMap *>(LayoutMap);
Craig Topperc6207612014-04-09 06:08:46 +0000550 LayoutMap = nullptr;
Rafael Espindola248ac132014-02-25 22:23:04 +0000551}
552
Micah Villmowb4faa152012-10-04 23:01:22 +0000553DataLayout::~DataLayout() {
Rafael Espindola248ac132014-02-25 22:23:04 +0000554 clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000555}
556
Pete Cooper0ae73932015-07-27 17:15:28 +0000557const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000558 if (!LayoutMap)
559 LayoutMap = new StructLayoutMap();
560
561 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
562 StructLayout *&SL = (*STM)[Ty];
563 if (SL) return SL;
564
565 // Otherwise, create the struct layout. Because it is variable length, we
566 // malloc it, then use placement new.
567 int NumElts = Ty->getNumElements();
568 StructLayout *L =
569 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
570
571 // Set SL before calling StructLayout's ctor. The ctor could cause other
572 // entries to be added to TheMap, invalidating our reference.
573 SL = L;
574
575 new (L) StructLayout(Ty, *this);
576
577 return L;
578}
579
Micah Villmowac34b5c2012-10-04 22:08:14 +0000580
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000581unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000582 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000583 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000584 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000585 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000586 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000587 return I->ABIAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000588}
589
590unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000591 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000592 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000593 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000594 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000595 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000596 return I->PrefAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000597}
598
599unsigned DataLayout::getPointerSize(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000600 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000601 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000602 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000603 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000604 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000605 return I->TypeByteWidth;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000606}
607
Pete Cooper0ae73932015-07-27 17:15:28 +0000608unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
Matt Arsenault6f4be902013-07-26 17:37:20 +0000609 assert(Ty->isPtrOrPtrVectorTy() &&
610 "This should only be called with a pointer or pointer vector type");
Craig Topper5b4f5b02017-04-17 18:22:36 +0000611 Ty = Ty->getScalarType();
612 return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000613}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000614
Micah Villmowac34b5c2012-10-04 22:08:14 +0000615/*!
616 \param abi_or_pref Flag that determines which alignment is returned. true
617 returns the ABI alignment, false returns the preferred alignment.
618 \param Ty The underlying type for which alignment is determined.
619
620 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
621 == false) for the requested type \a Ty.
622 */
Pete Cooper0ae73932015-07-27 17:15:28 +0000623unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000624 int AlignType = -1;
625
626 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
627 switch (Ty->getTypeID()) {
628 // Early escape for the non-numeric types.
629 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000630 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000631 ? getPointerABIAlignment(0)
632 : getPointerPrefAlignment(0));
633 case Type::PointerTyID: {
Matt Arsenaultc1728972014-09-18 22:28:56 +0000634 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Micah Villmow89021e42012-10-09 16:06:12 +0000635 return (abi_or_pref
636 ? getPointerABIAlignment(AS)
637 : getPointerPrefAlignment(AS));
638 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000639 case Type::ArrayTyID:
640 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
641
642 case Type::StructTyID: {
643 // Packed structure types always have an ABI alignment of one.
644 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
645 return 1;
646
647 // Get the layout annotation... which is lazily created on demand.
648 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
649 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
650 return std::max(Align, Layout->getAlignment());
651 }
652 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000653 AlignType = INTEGER_ALIGN;
654 break;
655 case Type::HalfTyID:
656 case Type::FloatTyID:
657 case Type::DoubleTyID:
658 // PPC_FP128TyID and FP128TyID have different data contents, but the
659 // same size and alignment, so they look the same here.
660 case Type::PPC_FP128TyID:
661 case Type::FP128TyID:
662 case Type::X86_FP80TyID:
663 AlignType = FLOAT_ALIGN;
664 break;
665 case Type::X86_MMXTyID:
666 case Type::VectorTyID:
667 AlignType = VECTOR_ALIGN;
668 break;
669 default:
670 llvm_unreachable("Bad type for getAlignment!!!");
671 }
672
673 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
674 abi_or_pref, Ty);
675}
676
Pete Cooper0ae73932015-07-27 17:15:28 +0000677unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000678 return getAlignment(Ty, true);
679}
680
681/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
682/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000683unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Craig Topperc6207612014-04-09 06:08:46 +0000684 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000685}
686
Pete Cooper0ae73932015-07-27 17:15:28 +0000687unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000688 return getAlignment(Ty, false);
689}
690
Pete Cooper0ae73932015-07-27 17:15:28 +0000691unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000692 unsigned Align = getPrefTypeAlignment(Ty);
693 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
694 return Log2_32(Align);
695}
696
Micah Villmow89021e42012-10-09 16:06:12 +0000697IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
698 unsigned AddressSpace) const {
699 return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000700}
701
Pete Cooper0ae73932015-07-27 17:15:28 +0000702Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000703 assert(Ty->isPtrOrPtrVectorTy() &&
704 "Expected a pointer or pointer vector type.");
Matt Arsenault4dbd4892014-04-23 21:10:15 +0000705 unsigned NumBits = getPointerTypeSizeInBits(Ty);
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000706 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
Pete Cooper0ae73932015-07-27 17:15:28 +0000707 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000708 return VectorType::get(IntTy, VecTy->getNumElements());
709 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000710}
711
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000712Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000713 for (unsigned LegalIntWidth : LegalIntWidths)
714 if (Width <= LegalIntWidth)
715 return Type::getIntNTy(C, LegalIntWidth);
Craig Topperc6207612014-04-09 06:08:46 +0000716 return nullptr;
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000717}
718
Jun Bum Limbe11bdc2016-05-13 18:38:35 +0000719unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000720 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
721 return Max != LegalIntWidths.end() ? *Max : 0;
Matt Arsenault899f7d22013-09-16 22:43:16 +0000722}
723
David Majnemer17bdf442016-07-13 03:42:38 +0000724int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
725 ArrayRef<Value *> Indices) const {
726 int64_t Result = 0;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000727
728 generic_gep_type_iterator<Value* const*>
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000729 GTI = gep_type_begin(ElemTy, Indices),
730 GTE = gep_type_end(ElemTy, Indices);
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000731 for (; GTI != GTE; ++GTI) {
732 Value *Idx = GTI.getOperand();
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000733 if (StructType *STy = GTI.getStructTypeOrNull()) {
Manuel Jacobcc13c2c2016-01-22 03:30:27 +0000734 assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000735 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000736
737 // Get structure layout information...
738 const StructLayout *Layout = getStructLayout(STy);
739
740 // Add in the offset, as calculated by the structure layout info...
741 Result += Layout->getElementOffset(FieldNo);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000742 } else {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000743 // Get the array index and the size of each array element.
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000744 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
David Majnemer17bdf442016-07-13 03:42:38 +0000745 Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000746 }
747 }
748
749 return Result;
750}
751
752/// getPreferredAlignment - Return the preferred alignment of the specified
753/// global. This includes an explicitly requested alignment (if the global
754/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000755unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000756 Type *ElemType = GV->getValueType();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000757 unsigned Alignment = getPrefTypeAlignment(ElemType);
758 unsigned GVAlignment = GV->getAlignment();
759 if (GVAlignment >= Alignment) {
760 Alignment = GVAlignment;
761 } else if (GVAlignment != 0) {
762 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
763 }
764
765 if (GV->hasInitializer() && GVAlignment == 0) {
766 if (Alignment < 16) {
767 // If the global is not external, see if it is large. If so, give it a
768 // larger alignment.
769 if (getTypeSizeInBits(ElemType) > 128)
770 Alignment = 16; // 16-byte alignment.
771 }
772 }
773 return Alignment;
774}
775
776/// getPreferredAlignmentLog - Return the preferred alignment of the
777/// specified global, returned in log form. This includes an explicitly
778/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000779unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000780 return Log2_32(getPreferredAlignment(GV));
781}
Rafael Espindola93512512014-02-25 17:30:31 +0000782