blob: 6f90ce598568628478e4a669da60d103f9a644b5 [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");
611
612 if (Ty->isPointerTy())
613 return getTypeSizeInBits(Ty);
614
Matt Arsenault517cf482013-07-27 19:22:28 +0000615 return getTypeSizeInBits(Ty->getScalarType());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000616}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000617
Micah Villmowac34b5c2012-10-04 22:08:14 +0000618/*!
619 \param abi_or_pref Flag that determines which alignment is returned. true
620 returns the ABI alignment, false returns the preferred alignment.
621 \param Ty The underlying type for which alignment is determined.
622
623 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
624 == false) for the requested type \a Ty.
625 */
Pete Cooper0ae73932015-07-27 17:15:28 +0000626unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000627 int AlignType = -1;
628
629 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
630 switch (Ty->getTypeID()) {
631 // Early escape for the non-numeric types.
632 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000633 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000634 ? getPointerABIAlignment(0)
635 : getPointerPrefAlignment(0));
636 case Type::PointerTyID: {
Matt Arsenaultc1728972014-09-18 22:28:56 +0000637 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Micah Villmow89021e42012-10-09 16:06:12 +0000638 return (abi_or_pref
639 ? getPointerABIAlignment(AS)
640 : getPointerPrefAlignment(AS));
641 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000642 case Type::ArrayTyID:
643 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
644
645 case Type::StructTyID: {
646 // Packed structure types always have an ABI alignment of one.
647 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
648 return 1;
649
650 // Get the layout annotation... which is lazily created on demand.
651 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
652 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
653 return std::max(Align, Layout->getAlignment());
654 }
655 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000656 AlignType = INTEGER_ALIGN;
657 break;
658 case Type::HalfTyID:
659 case Type::FloatTyID:
660 case Type::DoubleTyID:
661 // PPC_FP128TyID and FP128TyID have different data contents, but the
662 // same size and alignment, so they look the same here.
663 case Type::PPC_FP128TyID:
664 case Type::FP128TyID:
665 case Type::X86_FP80TyID:
666 AlignType = FLOAT_ALIGN;
667 break;
668 case Type::X86_MMXTyID:
669 case Type::VectorTyID:
670 AlignType = VECTOR_ALIGN;
671 break;
672 default:
673 llvm_unreachable("Bad type for getAlignment!!!");
674 }
675
676 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
677 abi_or_pref, Ty);
678}
679
Pete Cooper0ae73932015-07-27 17:15:28 +0000680unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000681 return getAlignment(Ty, true);
682}
683
684/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
685/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000686unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Craig Topperc6207612014-04-09 06:08:46 +0000687 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000688}
689
Pete Cooper0ae73932015-07-27 17:15:28 +0000690unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000691 return getAlignment(Ty, false);
692}
693
Pete Cooper0ae73932015-07-27 17:15:28 +0000694unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000695 unsigned Align = getPrefTypeAlignment(Ty);
696 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
697 return Log2_32(Align);
698}
699
Micah Villmow89021e42012-10-09 16:06:12 +0000700IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
701 unsigned AddressSpace) const {
702 return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000703}
704
Pete Cooper0ae73932015-07-27 17:15:28 +0000705Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000706 assert(Ty->isPtrOrPtrVectorTy() &&
707 "Expected a pointer or pointer vector type.");
Matt Arsenault4dbd4892014-04-23 21:10:15 +0000708 unsigned NumBits = getPointerTypeSizeInBits(Ty);
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000709 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
Pete Cooper0ae73932015-07-27 17:15:28 +0000710 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000711 return VectorType::get(IntTy, VecTy->getNumElements());
712 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000713}
714
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000715Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000716 for (unsigned LegalIntWidth : LegalIntWidths)
717 if (Width <= LegalIntWidth)
718 return Type::getIntNTy(C, LegalIntWidth);
Craig Topperc6207612014-04-09 06:08:46 +0000719 return nullptr;
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000720}
721
Jun Bum Limbe11bdc2016-05-13 18:38:35 +0000722unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000723 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
724 return Max != LegalIntWidths.end() ? *Max : 0;
Matt Arsenault899f7d22013-09-16 22:43:16 +0000725}
726
David Majnemer17bdf442016-07-13 03:42:38 +0000727int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
728 ArrayRef<Value *> Indices) const {
729 int64_t Result = 0;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000730
731 generic_gep_type_iterator<Value* const*>
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000732 GTI = gep_type_begin(ElemTy, Indices),
733 GTE = gep_type_end(ElemTy, Indices);
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000734 for (; GTI != GTE; ++GTI) {
735 Value *Idx = GTI.getOperand();
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000736 if (StructType *STy = GTI.getStructTypeOrNull()) {
Manuel Jacobcc13c2c2016-01-22 03:30:27 +0000737 assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx");
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000738 unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000739
740 // Get structure layout information...
741 const StructLayout *Layout = getStructLayout(STy);
742
743 // Add in the offset, as calculated by the structure layout info...
744 Result += Layout->getElementOffset(FieldNo);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000745 } else {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000746 // Get the array index and the size of each array element.
Eduard Burtescu68e7f492016-01-22 03:08:27 +0000747 if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue())
David Majnemer17bdf442016-07-13 03:42:38 +0000748 Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000749 }
750 }
751
752 return Result;
753}
754
755/// getPreferredAlignment - Return the preferred alignment of the specified
756/// global. This includes an explicitly requested alignment (if the global
757/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000758unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000759 Type *ElemType = GV->getValueType();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000760 unsigned Alignment = getPrefTypeAlignment(ElemType);
761 unsigned GVAlignment = GV->getAlignment();
762 if (GVAlignment >= Alignment) {
763 Alignment = GVAlignment;
764 } else if (GVAlignment != 0) {
765 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
766 }
767
768 if (GV->hasInitializer() && GVAlignment == 0) {
769 if (Alignment < 16) {
770 // If the global is not external, see if it is large. If so, give it a
771 // larger alignment.
772 if (getTypeSizeInBits(ElemType) > 128)
773 Alignment = 16; // 16-byte alignment.
774 }
775 }
776 return Alignment;
777}
778
779/// getPreferredAlignmentLog - Return the preferred alignment of the
780/// specified global, returned in log form. This includes an explicitly
781/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000782unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000783 return Log2_32(getPreferredAlignment(GV));
784}
Rafael Espindola93512512014-02-25 17:30:31 +0000785