blob: cde393777a649c93f484b5d0307a3169fef64b02 [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 Villmowb4faa152012-10-04 23:01:22 +000036// Handle the Pass registration stuff necessary to use DataLayout's.
Micah Villmowac34b5c2012-10-04 22:08:14 +000037
Rafael Espindola93512512014-02-25 17:30:31 +000038INITIALIZE_PASS(DataLayoutPass, "datalayout", "Data Layout", false, true)
39char DataLayoutPass::ID = 0;
Micah Villmowac34b5c2012-10-04 22:08:14 +000040
41//===----------------------------------------------------------------------===//
42// Support for StructLayout
43//===----------------------------------------------------------------------===//
44
Eli Bendersky41913c72013-04-16 15:41:18 +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;
49 NumElements = ST->getNumElements();
50
51 // Loop over each of the elements, placing them in memory.
52 for (unsigned i = 0, e = NumElements; i != e; ++i) {
53 Type *Ty = ST->getElementType(i);
Eli Bendersky41913c72013-04-16 15:41:18 +000054 unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
Micah Villmowac34b5c2012-10-04 22:08:14 +000055
56 // Add padding if necessary to align the data element properly.
57 if ((StructSize & (TyAlign-1)) != 0)
David Majnemerf3cadce2014-10-20 06:13:33 +000058 StructSize = RoundUpToAlignment(StructSize, TyAlign);
Micah Villmowac34b5c2012-10-04 22:08:14 +000059
60 // Keep track of maximum alignment constraint.
61 StructAlignment = std::max(TyAlign, StructAlignment);
62
63 MemberOffsets[i] = StructSize;
Eli Bendersky41913c72013-04-16 15:41:18 +000064 StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
Micah Villmowac34b5c2012-10-04 22:08:14 +000065 }
66
67 // Empty structures have alignment of 1 byte.
68 if (StructAlignment == 0) StructAlignment = 1;
69
70 // Add padding to the end of the struct so that it could be put in an array
71 // and all array elements would be aligned correctly.
72 if ((StructSize & (StructAlignment-1)) != 0)
David Majnemerf3cadce2014-10-20 06:13:33 +000073 StructSize = RoundUpToAlignment(StructSize, StructAlignment);
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 Villmowb4faa152012-10-04 23:01:22 +0000121const LayoutAlignElem
Benjamin Kramer058f5b32013-11-19 20:28:04 +0000122DataLayout::InvalidAlignmentElem = { INVALID_ALIGN, 0, 0, 0 };
Micah Villmow89021e42012-10-09 16:06:12 +0000123
124//===----------------------------------------------------------------------===//
125// PointerAlignElem, PointerAlign support
126//===----------------------------------------------------------------------===//
127
128PointerAlignElem
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000129PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
130 unsigned PrefAlign, uint32_t TypeByteWidth) {
131 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
Micah Villmow89021e42012-10-09 16:06:12 +0000132 PointerAlignElem retval;
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000133 retval.AddressSpace = AddressSpace;
134 retval.ABIAlign = ABIAlign;
135 retval.PrefAlign = PrefAlign;
136 retval.TypeByteWidth = TypeByteWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000137 return retval;
138}
139
140bool
141PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
142 return (ABIAlign == rhs.ABIAlign
143 && AddressSpace == rhs.AddressSpace
144 && PrefAlign == rhs.PrefAlign
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000145 && TypeByteWidth == rhs.TypeByteWidth);
Micah Villmow89021e42012-10-09 16:06:12 +0000146}
147
148const PointerAlignElem
Benjamin Kramer058f5b32013-11-19 20:28:04 +0000149DataLayout::InvalidPointerElem = { 0U, 0U, 0U, ~0U };
Micah Villmowac34b5c2012-10-04 22:08:14 +0000150
151//===----------------------------------------------------------------------===//
Micah Villmowb4faa152012-10-04 23:01:22 +0000152// DataLayout Class Implementation
Micah Villmowac34b5c2012-10-04 22:08:14 +0000153//===----------------------------------------------------------------------===//
154
Rafael Espindola58873562014-01-03 19:21:54 +0000155const char *DataLayout::getManglingComponent(const Triple &T) {
156 if (T.isOSBinFormatMachO())
157 return "-m:o";
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000158 if (T.isOSWindows() && T.getArch() == Triple::x86 && T.isOSBinFormatCOFF())
159 return "-m:w";
160 return "-m:e";
Rafael Espindola58873562014-01-03 19:21:54 +0000161}
162
Rafael Espindolae23b8772013-12-20 15:21:32 +0000163static const LayoutAlignElem DefaultAlignments[] = {
Rafael Espindola458a4852013-12-19 23:03:03 +0000164 { INTEGER_ALIGN, 1, 1, 1 }, // i1
165 { INTEGER_ALIGN, 8, 1, 1 }, // i8
166 { INTEGER_ALIGN, 16, 2, 2 }, // i16
167 { INTEGER_ALIGN, 32, 4, 4 }, // i32
168 { INTEGER_ALIGN, 64, 4, 8 }, // i64
169 { FLOAT_ALIGN, 16, 2, 2 }, // half
170 { FLOAT_ALIGN, 32, 4, 4 }, // float
171 { FLOAT_ALIGN, 64, 8, 8 }, // double
172 { FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ...
173 { VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ...
174 { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
175 { AGGREGATE_ALIGN, 0, 0, 8 } // struct
176};
177
Rafael Espindola248ac132014-02-25 22:23:04 +0000178void DataLayout::reset(StringRef Desc) {
179 clear();
180
Craig Topperc6207612014-04-09 06:08:46 +0000181 LayoutMap = nullptr;
Chandler Carruthf67321c2014-10-20 10:41:29 +0000182 BigEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000183 StackNaturalAlign = 0;
Rafael Espindola58873562014-01-03 19:21:54 +0000184 ManglingMode = MM_None;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000185
186 // Default alignments
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000187 for (const LayoutAlignElem &E : DefaultAlignments) {
Rafael Espindola458a4852013-12-19 23:03:03 +0000188 setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
189 E.TypeBitWidth);
190 }
Micah Villmow89021e42012-10-09 16:06:12 +0000191 setPointerAlignment(0, 8, 8, 8);
Patrik Hägglund01860a62012-11-14 09:04:56 +0000192
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000193 parseSpecifier(Desc);
194}
195
196/// Checked version of split, to ensure mandatory subparts.
197static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
198 assert(!Str.empty() && "parse error, string can't be empty here");
199 std::pair<StringRef, StringRef> Split = Str.split(Separator);
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000200 if (Split.second.empty() && Split.first != Str)
201 report_fatal_error("Trailing separator in datalayout string");
David Majnemer612f3122014-12-10 02:36:41 +0000202 if (!Split.second.empty() && Split.first.empty())
203 report_fatal_error("Expected token before separator in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000204 return Split;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000205}
206
Cameron McInally8af9eac2014-01-07 19:51:38 +0000207/// Get an unsigned integer, including error checks.
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000208static unsigned getInt(StringRef R) {
Cameron McInallyf0379fa2014-01-13 22:04:55 +0000209 unsigned Result;
Patrik Hägglund504f4782012-11-28 14:32:52 +0000210 bool error = R.getAsInteger(10, Result); (void)error;
Cameron McInallyf0379fa2014-01-13 22:04:55 +0000211 if (error)
212 report_fatal_error("not a number, or does not fit in an unsigned int");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000213 return Result;
214}
215
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000216/// Convert bits into bytes. Assert if not a byte width multiple.
217static unsigned inBytes(unsigned Bits) {
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000218 if (Bits % 8)
219 report_fatal_error("number of bits must be a byte width multiple");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000220 return Bits / 8;
221}
222
223void DataLayout::parseSpecifier(StringRef Desc) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000224 while (!Desc.empty()) {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000225 // Split at '-'.
226 std::pair<StringRef, StringRef> Split = split(Desc, '-');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000227 Desc = Split.second;
228
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000229 // Split at ':'.
230 Split = split(Split.first, ':');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000231
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000232 // Aliases used below.
233 StringRef &Tok = Split.first; // Current token.
234 StringRef &Rest = Split.second; // The rest of the string.
Micah Villmowac34b5c2012-10-04 22:08:14 +0000235
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000236 char Specifier = Tok.front();
237 Tok = Tok.substr(1);
238
239 switch (Specifier) {
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000240 case 's':
241 // Ignored for backward compatibility.
242 // FIXME: remove this on LLVM 4.0.
243 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000244 case 'E':
Chandler Carruthf67321c2014-10-20 10:41:29 +0000245 BigEndian = true;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000246 break;
247 case 'e':
Chandler Carruthf67321c2014-10-20 10:41:29 +0000248 BigEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000249 break;
250 case 'p': {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000251 // Address space.
252 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000253 if (!isUInt<24>(AddrSpace))
254 report_fatal_error("Invalid address space, must be a 24bit integer");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000255
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000256 // Size.
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000257 if (Rest.empty())
258 report_fatal_error(
259 "Missing size specification for pointer in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000260 Split = split(Rest, ':');
261 unsigned PointerMemSize = inBytes(getInt(Tok));
262
263 // ABI alignment.
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000264 if (Rest.empty())
265 report_fatal_error(
266 "Missing alignment specification for pointer in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000267 Split = split(Rest, ':');
268 unsigned PointerABIAlign = inBytes(getInt(Tok));
269
270 // Preferred alignment.
271 unsigned PointerPrefAlign = PointerABIAlign;
272 if (!Rest.empty()) {
273 Split = split(Rest, ':');
274 PointerPrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000275 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000276
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000277 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
278 PointerMemSize);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000279 break;
280 }
281 case 'i':
282 case 'v':
283 case 'f':
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000284 case 'a': {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000285 AlignTypeEnum AlignType;
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000286 switch (Specifier) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000287 default:
288 case 'i': AlignType = INTEGER_ALIGN; break;
289 case 'v': AlignType = VECTOR_ALIGN; break;
290 case 'f': AlignType = FLOAT_ALIGN; break;
291 case 'a': AlignType = AGGREGATE_ALIGN; break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000292 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000293
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000294 // Bit size.
295 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
296
David Majnemer5330c692014-12-10 01:17:08 +0000297 if (AlignType == AGGREGATE_ALIGN && Size != 0)
298 report_fatal_error(
299 "Sized aggregate specification in datalayout string");
Rafael Espindolaabdd7262014-01-06 21:40:24 +0000300
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000301 // ABI alignment.
David Majnemer612f3122014-12-10 02:36:41 +0000302 if (Rest.empty())
303 report_fatal_error(
304 "Missing alignment specification in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000305 Split = split(Rest, ':');
306 unsigned ABIAlign = inBytes(getInt(Tok));
307
308 // Preferred alignment.
309 unsigned PrefAlign = ABIAlign;
310 if (!Rest.empty()) {
311 Split = split(Rest, ':');
312 PrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000313 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000314
Patrik Hägglund01860a62012-11-14 09:04:56 +0000315 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmowb4faa152012-10-04 23:01:22 +0000316
Micah Villmowac34b5c2012-10-04 22:08:14 +0000317 break;
318 }
319 case 'n': // Native integer types.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000320 for (;;) {
321 unsigned Width = getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000322 if (Width == 0)
323 report_fatal_error(
324 "Zero width native integer type in datalayout string");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000325 LegalIntWidths.push_back(Width);
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000326 if (Rest.empty())
327 break;
328 Split = split(Rest, ':');
329 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000330 break;
331 case 'S': { // Stack natural alignment.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000332 StackNaturalAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000333 break;
334 }
Rafael Espindola58873562014-01-03 19:21:54 +0000335 case 'm':
David Majnemer612f3122014-12-10 02:36:41 +0000336 if (!Tok.empty())
337 report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
338 if (Rest.empty())
339 report_fatal_error("Expected mangling specifier in datalayout string");
340 if (Rest.size() > 1)
341 report_fatal_error("Unknown mangling specifier in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000342 switch(Rest[0]) {
343 default:
David Majnemer5330c692014-12-10 01:17:08 +0000344 report_fatal_error("Unknown mangling in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000345 case 'e':
346 ManglingMode = MM_ELF;
347 break;
348 case 'o':
349 ManglingMode = MM_MachO;
350 break;
351 case 'm':
352 ManglingMode = MM_Mips;
353 break;
Rafael Espindolaaf77e122014-01-10 13:42:12 +0000354 case 'w':
355 ManglingMode = MM_WINCOFF;
Rafael Espindola58873562014-01-03 19:21:54 +0000356 break;
357 }
358 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000359 default:
David Majnemer5330c692014-12-10 01:17:08 +0000360 report_fatal_error("Unknown specifier in datalayout string");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000361 break;
362 }
363 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000364}
365
Craig Topperc6207612014-04-09 06:08:46 +0000366DataLayout::DataLayout(const Module *M) : LayoutMap(nullptr) {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000367 init(M);
368}
369
370void DataLayout::init(const Module *M) {
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000371 const DataLayout *Other = M->getDataLayout();
372 if (Other)
373 *this = *Other;
374 else
Rafael Espindola248ac132014-02-25 22:23:04 +0000375 reset("");
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000376}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000377
Rafael Espindolaae593f12014-02-26 17:02:08 +0000378bool DataLayout::operator==(const DataLayout &Other) const {
Chandler Carruthf67321c2014-10-20 10:41:29 +0000379 bool Ret = BigEndian == Other.BigEndian &&
Rafael Espindolaae593f12014-02-26 17:02:08 +0000380 StackNaturalAlign == Other.StackNaturalAlign &&
381 ManglingMode == Other.ManglingMode &&
382 LegalIntWidths == Other.LegalIntWidths &&
Rafael Espindola89992b02014-04-22 17:47:03 +0000383 Alignments == Other.Alignments && Pointers == Other.Pointers;
Rafael Espindolaae593f12014-02-26 17:02:08 +0000384 assert(Ret == (getStringRepresentation() == Other.getStringRepresentation()));
385 return Ret;
386}
387
Micah Villmowac34b5c2012-10-04 22:08:14 +0000388void
Micah Villmowb4faa152012-10-04 23:01:22 +0000389DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000390 unsigned pref_align, uint32_t bit_width) {
391 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
392 assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield");
393 assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield");
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000394 for (LayoutAlignElem &Elem : Alignments) {
395 if (Elem.AlignType == (unsigned)align_type &&
396 Elem.TypeBitWidth == bit_width) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000397 // Update the abi, preferred alignments.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000398 Elem.ABIAlign = abi_align;
399 Elem.PrefAlign = pref_align;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000400 return;
401 }
402 }
403
Micah Villmowb4faa152012-10-04 23:01:22 +0000404 Alignments.push_back(LayoutAlignElem::get(align_type, abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000405 pref_align, bit_width));
406}
407
Rafael Espindola667fcb82014-02-26 16:58:35 +0000408DataLayout::PointersTy::iterator
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000409DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000410 return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000411 [](const PointerAlignElem &A, uint32_t AddressSpace) {
412 return A.AddressSpace < AddressSpace;
413 });
Rafael Espindola667fcb82014-02-26 16:58:35 +0000414}
415
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000416void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
417 unsigned PrefAlign,
418 uint32_t TypeByteWidth) {
419 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000420 PointersTy::iterator I = findPointerLowerBound(AddrSpace);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000421 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
422 Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
423 TypeByteWidth));
Micah Villmow89021e42012-10-09 16:06:12 +0000424 } else {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000425 I->ABIAlign = ABIAlign;
426 I->PrefAlign = PrefAlign;
427 I->TypeByteWidth = TypeByteWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000428 }
429}
430
Micah Villmowac34b5c2012-10-04 22:08:14 +0000431/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000432/// preferred if ABIInfo = false) the layout wants for the specified datatype.
433unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000434 uint32_t BitWidth, bool ABIInfo,
435 Type *Ty) const {
436 // Check to see if we have an exact match and remember the best match we see.
437 int BestMatchIdx = -1;
438 int LargestInt = -1;
439 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmow6d05e692012-10-05 17:02:14 +0000440 if (Alignments[i].AlignType == (unsigned)AlignType &&
Micah Villmowac34b5c2012-10-04 22:08:14 +0000441 Alignments[i].TypeBitWidth == BitWidth)
442 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
443
444 // The best match so far depends on what we're looking for.
445 if (AlignType == INTEGER_ALIGN &&
446 Alignments[i].AlignType == INTEGER_ALIGN) {
447 // The "best match" for integers is the smallest size that is larger than
448 // the BitWidth requested.
449 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
Eli Benderskyfaf5e3e2013-01-30 19:24:23 +0000450 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
Micah Villmowac34b5c2012-10-04 22:08:14 +0000451 BestMatchIdx = i;
452 // However, if there isn't one that's larger, then we must use the
453 // largest one we have (see below)
454 if (LargestInt == -1 ||
455 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
456 LargestInt = i;
457 }
458 }
459
460 // Okay, we didn't find an exact solution. Fall back here depending on what
461 // is being looked for.
462 if (BestMatchIdx == -1) {
463 // If we didn't find an integer alignment, fall back on most conservative.
464 if (AlignType == INTEGER_ALIGN) {
465 BestMatchIdx = LargestInt;
466 } else {
467 assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
468
469 // By default, use natural alignment for vector types. This is consistent
470 // with what clang and llvm-gcc do.
471 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
472 Align *= cast<VectorType>(Ty)->getNumElements();
473 // If the alignment is not a power of 2, round up to the next power of 2.
474 // This happens for non-power-of-2 length vectors.
475 if (Align & (Align-1))
476 Align = NextPowerOf2(Align);
477 return Align;
478 }
479 }
480
481 // Since we got a "best match" index, just return it.
482 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
483 : Alignments[BestMatchIdx].PrefAlign;
484}
485
486namespace {
487
488class StructLayoutMap {
489 typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
490 LayoutInfoTy LayoutInfo;
491
492public:
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000493 ~StructLayoutMap() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000494 // Remove any layouts.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000495 for (const auto &I : LayoutInfo) {
496 StructLayout *Value = I.second;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000497 Value->~StructLayout();
498 free(Value);
499 }
500 }
501
502 StructLayout *&operator[](StructType *STy) {
503 return LayoutInfo[STy];
504 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000505};
506
507} // end anonymous namespace
508
Rafael Espindola248ac132014-02-25 22:23:04 +0000509void DataLayout::clear() {
510 LegalIntWidths.clear();
511 Alignments.clear();
512 Pointers.clear();
513 delete static_cast<StructLayoutMap *>(LayoutMap);
Craig Topperc6207612014-04-09 06:08:46 +0000514 LayoutMap = nullptr;
Rafael Espindola248ac132014-02-25 22:23:04 +0000515}
516
Micah Villmowb4faa152012-10-04 23:01:22 +0000517DataLayout::~DataLayout() {
Rafael Espindola248ac132014-02-25 22:23:04 +0000518 clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000519}
520
Micah Villmowb4faa152012-10-04 23:01:22 +0000521const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000522 if (!LayoutMap)
523 LayoutMap = new StructLayoutMap();
524
525 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
526 StructLayout *&SL = (*STM)[Ty];
527 if (SL) return SL;
528
529 // Otherwise, create the struct layout. Because it is variable length, we
530 // malloc it, then use placement new.
531 int NumElts = Ty->getNumElements();
532 StructLayout *L =
533 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
534
535 // Set SL before calling StructLayout's ctor. The ctor could cause other
536 // entries to be added to TheMap, invalidating our reference.
537 SL = L;
538
539 new (L) StructLayout(Ty, *this);
540
541 return L;
542}
543
Micah Villmowb4faa152012-10-04 23:01:22 +0000544std::string DataLayout::getStringRepresentation() const {
Alp Tokere69170a2014-06-26 22:52:05 +0000545 std::string Result;
546 raw_string_ostream OS(Result);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000547
Chandler Carruthf67321c2014-10-20 10:41:29 +0000548 OS << (BigEndian ? "E" : "e");
Rafael Espindola58873562014-01-03 19:21:54 +0000549
550 switch (ManglingMode) {
551 case MM_None:
552 break;
553 case MM_ELF:
554 OS << "-m:e";
555 break;
556 case MM_MachO:
557 OS << "-m:o";
558 break;
Rafael Espindolaaf77e122014-01-10 13:42:12 +0000559 case MM_WINCOFF:
560 OS << "-m:w";
Rafael Espindola58873562014-01-03 19:21:54 +0000561 break;
562 case MM_Mips:
563 OS << "-m:m";
564 break;
565 }
566
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000567 for (const PointerAlignElem &PI : Pointers) {
Rafael Espindola458a4852013-12-19 23:03:03 +0000568 // Skip default.
569 if (PI.AddressSpace == 0 && PI.ABIAlign == 8 && PI.PrefAlign == 8 &&
570 PI.TypeByteWidth == 8)
571 continue;
572
Micah Villmow89021e42012-10-09 16:06:12 +0000573 OS << "-p";
574 if (PI.AddressSpace) {
575 OS << PI.AddressSpace;
576 }
Rafael Espindola458a4852013-12-19 23:03:03 +0000577 OS << ":" << PI.TypeByteWidth*8 << ':' << PI.ABIAlign*8;
578 if (PI.PrefAlign != PI.ABIAlign)
579 OS << ':' << PI.PrefAlign*8;
Micah Villmow89021e42012-10-09 16:06:12 +0000580 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000581
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000582 for (const LayoutAlignElem &AI : Alignments) {
583 if (std::find(std::begin(DefaultAlignments), std::end(DefaultAlignments),
584 AI) != std::end(DefaultAlignments))
Rafael Espindola458a4852013-12-19 23:03:03 +0000585 continue;
586 OS << '-' << (char)AI.AlignType;
587 if (AI.TypeBitWidth)
588 OS << AI.TypeBitWidth;
589 OS << ':' << AI.ABIAlign*8;
590 if (AI.ABIAlign != AI.PrefAlign)
591 OS << ':' << AI.PrefAlign*8;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000592 }
593
594 if (!LegalIntWidths.empty()) {
595 OS << "-n" << (unsigned)LegalIntWidths[0];
596
597 for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
598 OS << ':' << (unsigned)LegalIntWidths[i];
599 }
Rafael Espindola458a4852013-12-19 23:03:03 +0000600
601 if (StackNaturalAlign)
602 OS << "-S" << StackNaturalAlign*8;
603
Micah Villmowac34b5c2012-10-04 22:08:14 +0000604 return OS.str();
605}
606
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000607unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000608 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000609 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000610 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000611 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000612 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000613 return I->ABIAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000614}
615
616unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000617 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000618 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000619 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000620 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000621 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000622 return I->PrefAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000623}
624
625unsigned DataLayout::getPointerSize(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000626 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000627 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000628 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000629 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000630 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000631 return I->TypeByteWidth;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000632}
633
Matt Arsenault6f4be902013-07-26 17:37:20 +0000634unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
635 assert(Ty->isPtrOrPtrVectorTy() &&
636 "This should only be called with a pointer or pointer vector type");
637
638 if (Ty->isPointerTy())
639 return getTypeSizeInBits(Ty);
640
Matt Arsenault517cf482013-07-27 19:22:28 +0000641 return getTypeSizeInBits(Ty->getScalarType());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000642}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000643
Micah Villmowac34b5c2012-10-04 22:08:14 +0000644/*!
645 \param abi_or_pref Flag that determines which alignment is returned. true
646 returns the ABI alignment, false returns the preferred alignment.
647 \param Ty The underlying type for which alignment is determined.
648
649 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
650 == false) for the requested type \a Ty.
651 */
Micah Villmowb4faa152012-10-04 23:01:22 +0000652unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000653 int AlignType = -1;
654
655 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
656 switch (Ty->getTypeID()) {
657 // Early escape for the non-numeric types.
658 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000659 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000660 ? getPointerABIAlignment(0)
661 : getPointerPrefAlignment(0));
662 case Type::PointerTyID: {
Matt Arsenaultc1728972014-09-18 22:28:56 +0000663 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Micah Villmow89021e42012-10-09 16:06:12 +0000664 return (abi_or_pref
665 ? getPointerABIAlignment(AS)
666 : getPointerPrefAlignment(AS));
667 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000668 case Type::ArrayTyID:
669 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
670
671 case Type::StructTyID: {
672 // Packed structure types always have an ABI alignment of one.
673 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
674 return 1;
675
676 // Get the layout annotation... which is lazily created on demand.
677 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
678 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
679 return std::max(Align, Layout->getAlignment());
680 }
681 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000682 AlignType = INTEGER_ALIGN;
683 break;
684 case Type::HalfTyID:
685 case Type::FloatTyID:
686 case Type::DoubleTyID:
687 // PPC_FP128TyID and FP128TyID have different data contents, but the
688 // same size and alignment, so they look the same here.
689 case Type::PPC_FP128TyID:
690 case Type::FP128TyID:
691 case Type::X86_FP80TyID:
692 AlignType = FLOAT_ALIGN;
693 break;
694 case Type::X86_MMXTyID:
695 case Type::VectorTyID:
696 AlignType = VECTOR_ALIGN;
697 break;
698 default:
699 llvm_unreachable("Bad type for getAlignment!!!");
700 }
701
702 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
703 abi_or_pref, Ty);
704}
705
Micah Villmowb4faa152012-10-04 23:01:22 +0000706unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000707 return getAlignment(Ty, true);
708}
709
710/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
711/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000712unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Craig Topperc6207612014-04-09 06:08:46 +0000713 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000714}
715
Micah Villmowb4faa152012-10-04 23:01:22 +0000716unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000717 return getAlignment(Ty, false);
718}
719
Micah Villmowb4faa152012-10-04 23:01:22 +0000720unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000721 unsigned Align = getPrefTypeAlignment(Ty);
722 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
723 return Log2_32(Align);
724}
725
Micah Villmow89021e42012-10-09 16:06:12 +0000726IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
727 unsigned AddressSpace) const {
728 return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000729}
730
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000731Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000732 assert(Ty->isPtrOrPtrVectorTy() &&
733 "Expected a pointer or pointer vector type.");
Matt Arsenault4dbd4892014-04-23 21:10:15 +0000734 unsigned NumBits = getPointerTypeSizeInBits(Ty);
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000735 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
736 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
737 return VectorType::get(IntTy, VecTy->getNumElements());
738 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000739}
740
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000741Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000742 for (unsigned LegalIntWidth : LegalIntWidths)
743 if (Width <= LegalIntWidth)
744 return Type::getIntNTy(C, LegalIntWidth);
Craig Topperc6207612014-04-09 06:08:46 +0000745 return nullptr;
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000746}
747
Matt Arsenault899f7d22013-09-16 22:43:16 +0000748unsigned DataLayout::getLargestLegalIntTypeSize() const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000749 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
750 return Max != LegalIntWidths.end() ? *Max : 0;
Matt Arsenault899f7d22013-09-16 22:43:16 +0000751}
752
Micah Villmowb4faa152012-10-04 23:01:22 +0000753uint64_t DataLayout::getIndexedOffset(Type *ptrTy,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000754 ArrayRef<Value *> Indices) const {
755 Type *Ty = ptrTy;
756 assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
757 uint64_t Result = 0;
758
759 generic_gep_type_iterator<Value* const*>
760 TI = gep_type_begin(ptrTy, Indices);
761 for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
762 ++CurIDX, ++TI) {
763 if (StructType *STy = dyn_cast<StructType>(*TI)) {
764 assert(Indices[CurIDX]->getType() ==
765 Type::getInt32Ty(ptrTy->getContext()) &&
766 "Illegal struct idx");
767 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
768
769 // Get structure layout information...
770 const StructLayout *Layout = getStructLayout(STy);
771
772 // Add in the offset, as calculated by the structure layout info...
773 Result += Layout->getElementOffset(FieldNo);
774
775 // Update Ty to refer to current element
776 Ty = STy->getElementType(FieldNo);
777 } else {
778 // Update Ty to refer to current element
779 Ty = cast<SequentialType>(Ty)->getElementType();
780
781 // Get the array index and the size of each array element.
782 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
783 Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
784 }
785 }
786
787 return Result;
788}
789
790/// getPreferredAlignment - Return the preferred alignment of the specified
791/// global. This includes an explicitly requested alignment (if the global
792/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000793unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000794 Type *ElemType = GV->getType()->getElementType();
795 unsigned Alignment = getPrefTypeAlignment(ElemType);
796 unsigned GVAlignment = GV->getAlignment();
797 if (GVAlignment >= Alignment) {
798 Alignment = GVAlignment;
799 } else if (GVAlignment != 0) {
800 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
801 }
802
803 if (GV->hasInitializer() && GVAlignment == 0) {
804 if (Alignment < 16) {
805 // If the global is not external, see if it is large. If so, give it a
806 // larger alignment.
807 if (getTypeSizeInBits(ElemType) > 128)
808 Alignment = 16; // 16-byte alignment.
809 }
810 }
811 return Alignment;
812}
813
814/// getPreferredAlignmentLog - Return the preferred alignment of the
815/// specified global, returned in log form. This includes an explicitly
816/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000817unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000818 return Log2_32(getPreferredAlignment(GV));
819}
Rafael Espindola93512512014-02-25 17:30:31 +0000820
821DataLayoutPass::DataLayoutPass() : ImmutablePass(ID), DL("") {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000822 initializeDataLayoutPassPass(*PassRegistry::getPassRegistry());
Rafael Espindola93512512014-02-25 17:30:31 +0000823}
824
825DataLayoutPass::~DataLayoutPass() {}
826
Rafael Espindolac435adc2014-09-10 21:27:43 +0000827bool DataLayoutPass::doInitialization(Module &M) {
828 DL.init(&M);
829 return false;
Rafael Espindola93512512014-02-25 17:30:31 +0000830}
831
Rafael Espindolac435adc2014-09-10 21:27:43 +0000832bool DataLayoutPass::doFinalization(Module &M) {
833 DL.reset("");
834 return false;
Rafael Espindola93512512014-02-25 17:30:31 +0000835}