blob: 4eb6d78d38161f46215e910f3fbea248e90c27ad [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));
Owen Anderson5bc2bbe2015-03-02 06:00:02 +0000262 if (!PointerMemSize)
263 report_fatal_error("Invalid pointer size of 0 bytes");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000264
265 // ABI alignment.
David Majnemer2dc1b0f2014-12-10 01:38:28 +0000266 if (Rest.empty())
267 report_fatal_error(
268 "Missing alignment specification for pointer in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000269 Split = split(Rest, ':');
270 unsigned PointerABIAlign = inBytes(getInt(Tok));
Owen Anderson040f2f82015-03-02 06:33:51 +0000271 if (!isPowerOf2_64(PointerABIAlign))
272 report_fatal_error(
273 "Pointer ABI alignment must be a power of 2");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000274
275 // Preferred alignment.
276 unsigned PointerPrefAlign = PointerABIAlign;
277 if (!Rest.empty()) {
278 Split = split(Rest, ':');
279 PointerPrefAlign = inBytes(getInt(Tok));
Owen Anderson040f2f82015-03-02 06:33:51 +0000280 if (!isPowerOf2_64(PointerPrefAlign))
281 report_fatal_error(
282 "Pointer preferred alignment must be a power of 2");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000283 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000284
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000285 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
286 PointerMemSize);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000287 break;
288 }
289 case 'i':
290 case 'v':
291 case 'f':
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000292 case 'a': {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000293 AlignTypeEnum AlignType;
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000294 switch (Specifier) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000295 default:
296 case 'i': AlignType = INTEGER_ALIGN; break;
297 case 'v': AlignType = VECTOR_ALIGN; break;
298 case 'f': AlignType = FLOAT_ALIGN; break;
299 case 'a': AlignType = AGGREGATE_ALIGN; break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000300 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000301
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000302 // Bit size.
303 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
304
David Majnemer5330c692014-12-10 01:17:08 +0000305 if (AlignType == AGGREGATE_ALIGN && Size != 0)
306 report_fatal_error(
307 "Sized aggregate specification in datalayout string");
Rafael Espindolaabdd7262014-01-06 21:40:24 +0000308
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000309 // ABI alignment.
David Majnemer612f3122014-12-10 02:36:41 +0000310 if (Rest.empty())
311 report_fatal_error(
312 "Missing alignment specification in datalayout string");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000313 Split = split(Rest, ':');
314 unsigned ABIAlign = inBytes(getInt(Tok));
315
316 // Preferred alignment.
317 unsigned PrefAlign = ABIAlign;
318 if (!Rest.empty()) {
319 Split = split(Rest, ':');
320 PrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000321 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000322
Patrik Hägglund01860a62012-11-14 09:04:56 +0000323 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmowb4faa152012-10-04 23:01:22 +0000324
Micah Villmowac34b5c2012-10-04 22:08:14 +0000325 break;
326 }
327 case 'n': // Native integer types.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000328 for (;;) {
329 unsigned Width = getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000330 if (Width == 0)
331 report_fatal_error(
332 "Zero width native integer type in datalayout string");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000333 LegalIntWidths.push_back(Width);
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000334 if (Rest.empty())
335 break;
336 Split = split(Rest, ':');
337 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000338 break;
339 case 'S': { // Stack natural alignment.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000340 StackNaturalAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000341 break;
342 }
Rafael Espindola58873562014-01-03 19:21:54 +0000343 case 'm':
David Majnemer612f3122014-12-10 02:36:41 +0000344 if (!Tok.empty())
345 report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
346 if (Rest.empty())
347 report_fatal_error("Expected mangling specifier in datalayout string");
348 if (Rest.size() > 1)
349 report_fatal_error("Unknown mangling specifier in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000350 switch(Rest[0]) {
351 default:
David Majnemer5330c692014-12-10 01:17:08 +0000352 report_fatal_error("Unknown mangling in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000353 case 'e':
354 ManglingMode = MM_ELF;
355 break;
356 case 'o':
357 ManglingMode = MM_MachO;
358 break;
359 case 'm':
360 ManglingMode = MM_Mips;
361 break;
Rafael Espindolaaf77e122014-01-10 13:42:12 +0000362 case 'w':
363 ManglingMode = MM_WINCOFF;
Rafael Espindola58873562014-01-03 19:21:54 +0000364 break;
365 }
366 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000367 default:
David Majnemer5330c692014-12-10 01:17:08 +0000368 report_fatal_error("Unknown specifier in datalayout string");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000369 break;
370 }
371 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000372}
373
Craig Topperc6207612014-04-09 06:08:46 +0000374DataLayout::DataLayout(const Module *M) : LayoutMap(nullptr) {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000375 init(M);
376}
377
378void DataLayout::init(const Module *M) {
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000379 const DataLayout *Other = M->getDataLayout();
380 if (Other)
381 *this = *Other;
382 else
Rafael Espindola248ac132014-02-25 22:23:04 +0000383 reset("");
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000384}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000385
Rafael Espindolaae593f12014-02-26 17:02:08 +0000386bool DataLayout::operator==(const DataLayout &Other) const {
Chandler Carruthf67321c2014-10-20 10:41:29 +0000387 bool Ret = BigEndian == Other.BigEndian &&
Rafael Espindolaae593f12014-02-26 17:02:08 +0000388 StackNaturalAlign == Other.StackNaturalAlign &&
389 ManglingMode == Other.ManglingMode &&
390 LegalIntWidths == Other.LegalIntWidths &&
Rafael Espindola89992b02014-04-22 17:47:03 +0000391 Alignments == Other.Alignments && Pointers == Other.Pointers;
Rafael Espindolaae593f12014-02-26 17:02:08 +0000392 assert(Ret == (getStringRepresentation() == Other.getStringRepresentation()));
393 return Ret;
394}
395
Micah Villmowac34b5c2012-10-04 22:08:14 +0000396void
Micah Villmowb4faa152012-10-04 23:01:22 +0000397DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000398 unsigned pref_align, uint32_t bit_width) {
David Majnemer1b9fc3a2015-02-16 05:41:53 +0000399 if (!isUInt<24>(bit_width))
400 report_fatal_error("Invalid bit width, must be a 24bit integer");
401 if (!isUInt<16>(abi_align))
402 report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
403 if (!isUInt<16>(pref_align))
404 report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
405
406 if (pref_align < abi_align)
407 report_fatal_error(
408 "Preferred alignment cannot be less than the ABI alignment");
409
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000410 for (LayoutAlignElem &Elem : Alignments) {
411 if (Elem.AlignType == (unsigned)align_type &&
412 Elem.TypeBitWidth == bit_width) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000413 // Update the abi, preferred alignments.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000414 Elem.ABIAlign = abi_align;
415 Elem.PrefAlign = pref_align;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000416 return;
417 }
418 }
419
Micah Villmowb4faa152012-10-04 23:01:22 +0000420 Alignments.push_back(LayoutAlignElem::get(align_type, abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000421 pref_align, bit_width));
422}
423
Rafael Espindola667fcb82014-02-26 16:58:35 +0000424DataLayout::PointersTy::iterator
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000425DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000426 return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000427 [](const PointerAlignElem &A, uint32_t AddressSpace) {
428 return A.AddressSpace < AddressSpace;
429 });
Rafael Espindola667fcb82014-02-26 16:58:35 +0000430}
431
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000432void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
433 unsigned PrefAlign,
434 uint32_t TypeByteWidth) {
David Majnemer4b042922015-02-16 05:41:55 +0000435 if (PrefAlign < ABIAlign)
436 report_fatal_error(
437 "Preferred alignment cannot be less than the ABI alignment");
438
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000439 PointersTy::iterator I = findPointerLowerBound(AddrSpace);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000440 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
441 Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
442 TypeByteWidth));
Micah Villmow89021e42012-10-09 16:06:12 +0000443 } else {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000444 I->ABIAlign = ABIAlign;
445 I->PrefAlign = PrefAlign;
446 I->TypeByteWidth = TypeByteWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000447 }
448}
449
Micah Villmowac34b5c2012-10-04 22:08:14 +0000450/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000451/// preferred if ABIInfo = false) the layout wants for the specified datatype.
452unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000453 uint32_t BitWidth, bool ABIInfo,
454 Type *Ty) const {
455 // Check to see if we have an exact match and remember the best match we see.
456 int BestMatchIdx = -1;
457 int LargestInt = -1;
458 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmow6d05e692012-10-05 17:02:14 +0000459 if (Alignments[i].AlignType == (unsigned)AlignType &&
Micah Villmowac34b5c2012-10-04 22:08:14 +0000460 Alignments[i].TypeBitWidth == BitWidth)
461 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
462
463 // The best match so far depends on what we're looking for.
464 if (AlignType == INTEGER_ALIGN &&
465 Alignments[i].AlignType == INTEGER_ALIGN) {
466 // The "best match" for integers is the smallest size that is larger than
467 // the BitWidth requested.
468 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
Eli Benderskyfaf5e3e2013-01-30 19:24:23 +0000469 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
Micah Villmowac34b5c2012-10-04 22:08:14 +0000470 BestMatchIdx = i;
471 // However, if there isn't one that's larger, then we must use the
472 // largest one we have (see below)
473 if (LargestInt == -1 ||
474 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
475 LargestInt = i;
476 }
477 }
478
479 // Okay, we didn't find an exact solution. Fall back here depending on what
480 // is being looked for.
481 if (BestMatchIdx == -1) {
482 // If we didn't find an integer alignment, fall back on most conservative.
483 if (AlignType == INTEGER_ALIGN) {
484 BestMatchIdx = LargestInt;
485 } else {
486 assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
487
488 // By default, use natural alignment for vector types. This is consistent
489 // with what clang and llvm-gcc do.
490 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
491 Align *= cast<VectorType>(Ty)->getNumElements();
492 // If the alignment is not a power of 2, round up to the next power of 2.
493 // This happens for non-power-of-2 length vectors.
494 if (Align & (Align-1))
495 Align = NextPowerOf2(Align);
496 return Align;
497 }
498 }
499
500 // Since we got a "best match" index, just return it.
501 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
502 : Alignments[BestMatchIdx].PrefAlign;
503}
504
505namespace {
506
507class StructLayoutMap {
508 typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
509 LayoutInfoTy LayoutInfo;
510
511public:
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000512 ~StructLayoutMap() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000513 // Remove any layouts.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000514 for (const auto &I : LayoutInfo) {
515 StructLayout *Value = I.second;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000516 Value->~StructLayout();
517 free(Value);
518 }
519 }
520
521 StructLayout *&operator[](StructType *STy) {
522 return LayoutInfo[STy];
523 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000524};
525
526} // end anonymous namespace
527
Rafael Espindola248ac132014-02-25 22:23:04 +0000528void DataLayout::clear() {
529 LegalIntWidths.clear();
530 Alignments.clear();
531 Pointers.clear();
532 delete static_cast<StructLayoutMap *>(LayoutMap);
Craig Topperc6207612014-04-09 06:08:46 +0000533 LayoutMap = nullptr;
Rafael Espindola248ac132014-02-25 22:23:04 +0000534}
535
Micah Villmowb4faa152012-10-04 23:01:22 +0000536DataLayout::~DataLayout() {
Rafael Espindola248ac132014-02-25 22:23:04 +0000537 clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000538}
539
Micah Villmowb4faa152012-10-04 23:01:22 +0000540const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000541 if (!LayoutMap)
542 LayoutMap = new StructLayoutMap();
543
544 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
545 StructLayout *&SL = (*STM)[Ty];
546 if (SL) return SL;
547
548 // Otherwise, create the struct layout. Because it is variable length, we
549 // malloc it, then use placement new.
550 int NumElts = Ty->getNumElements();
551 StructLayout *L =
552 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
553
554 // Set SL before calling StructLayout's ctor. The ctor could cause other
555 // entries to be added to TheMap, invalidating our reference.
556 SL = L;
557
558 new (L) StructLayout(Ty, *this);
559
560 return L;
561}
562
Micah Villmowb4faa152012-10-04 23:01:22 +0000563std::string DataLayout::getStringRepresentation() const {
Alp Tokere69170a2014-06-26 22:52:05 +0000564 std::string Result;
565 raw_string_ostream OS(Result);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000566
Chandler Carruthf67321c2014-10-20 10:41:29 +0000567 OS << (BigEndian ? "E" : "e");
Rafael Espindola58873562014-01-03 19:21:54 +0000568
569 switch (ManglingMode) {
570 case MM_None:
571 break;
572 case MM_ELF:
573 OS << "-m:e";
574 break;
575 case MM_MachO:
576 OS << "-m:o";
577 break;
Rafael Espindolaaf77e122014-01-10 13:42:12 +0000578 case MM_WINCOFF:
579 OS << "-m:w";
Rafael Espindola58873562014-01-03 19:21:54 +0000580 break;
581 case MM_Mips:
582 OS << "-m:m";
583 break;
584 }
585
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000586 for (const PointerAlignElem &PI : Pointers) {
Rafael Espindola458a4852013-12-19 23:03:03 +0000587 // Skip default.
588 if (PI.AddressSpace == 0 && PI.ABIAlign == 8 && PI.PrefAlign == 8 &&
589 PI.TypeByteWidth == 8)
590 continue;
591
Micah Villmow89021e42012-10-09 16:06:12 +0000592 OS << "-p";
593 if (PI.AddressSpace) {
594 OS << PI.AddressSpace;
595 }
Rafael Espindola458a4852013-12-19 23:03:03 +0000596 OS << ":" << PI.TypeByteWidth*8 << ':' << PI.ABIAlign*8;
597 if (PI.PrefAlign != PI.ABIAlign)
598 OS << ':' << PI.PrefAlign*8;
Micah Villmow89021e42012-10-09 16:06:12 +0000599 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000600
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000601 for (const LayoutAlignElem &AI : Alignments) {
602 if (std::find(std::begin(DefaultAlignments), std::end(DefaultAlignments),
603 AI) != std::end(DefaultAlignments))
Rafael Espindola458a4852013-12-19 23:03:03 +0000604 continue;
605 OS << '-' << (char)AI.AlignType;
606 if (AI.TypeBitWidth)
607 OS << AI.TypeBitWidth;
608 OS << ':' << AI.ABIAlign*8;
609 if (AI.ABIAlign != AI.PrefAlign)
610 OS << ':' << AI.PrefAlign*8;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000611 }
612
613 if (!LegalIntWidths.empty()) {
614 OS << "-n" << (unsigned)LegalIntWidths[0];
615
616 for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
617 OS << ':' << (unsigned)LegalIntWidths[i];
618 }
Rafael Espindola458a4852013-12-19 23:03:03 +0000619
620 if (StackNaturalAlign)
621 OS << "-S" << StackNaturalAlign*8;
622
Micah Villmowac34b5c2012-10-04 22:08:14 +0000623 return OS.str();
624}
625
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000626unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000627 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000628 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000629 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000630 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000631 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000632 return I->ABIAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000633}
634
635unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000636 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000637 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000638 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000639 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000640 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000641 return I->PrefAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000642}
643
644unsigned DataLayout::getPointerSize(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000645 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000646 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000647 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000648 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000649 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000650 return I->TypeByteWidth;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000651}
652
Matt Arsenault6f4be902013-07-26 17:37:20 +0000653unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
654 assert(Ty->isPtrOrPtrVectorTy() &&
655 "This should only be called with a pointer or pointer vector type");
656
657 if (Ty->isPointerTy())
658 return getTypeSizeInBits(Ty);
659
Matt Arsenault517cf482013-07-27 19:22:28 +0000660 return getTypeSizeInBits(Ty->getScalarType());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000661}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000662
Micah Villmowac34b5c2012-10-04 22:08:14 +0000663/*!
664 \param abi_or_pref Flag that determines which alignment is returned. true
665 returns the ABI alignment, false returns the preferred alignment.
666 \param Ty The underlying type for which alignment is determined.
667
668 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
669 == false) for the requested type \a Ty.
670 */
Micah Villmowb4faa152012-10-04 23:01:22 +0000671unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000672 int AlignType = -1;
673
674 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
675 switch (Ty->getTypeID()) {
676 // Early escape for the non-numeric types.
677 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000678 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000679 ? getPointerABIAlignment(0)
680 : getPointerPrefAlignment(0));
681 case Type::PointerTyID: {
Matt Arsenaultc1728972014-09-18 22:28:56 +0000682 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Micah Villmow89021e42012-10-09 16:06:12 +0000683 return (abi_or_pref
684 ? getPointerABIAlignment(AS)
685 : getPointerPrefAlignment(AS));
686 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000687 case Type::ArrayTyID:
688 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
689
690 case Type::StructTyID: {
691 // Packed structure types always have an ABI alignment of one.
692 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
693 return 1;
694
695 // Get the layout annotation... which is lazily created on demand.
696 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
697 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
698 return std::max(Align, Layout->getAlignment());
699 }
700 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000701 AlignType = INTEGER_ALIGN;
702 break;
703 case Type::HalfTyID:
704 case Type::FloatTyID:
705 case Type::DoubleTyID:
706 // PPC_FP128TyID and FP128TyID have different data contents, but the
707 // same size and alignment, so they look the same here.
708 case Type::PPC_FP128TyID:
709 case Type::FP128TyID:
710 case Type::X86_FP80TyID:
711 AlignType = FLOAT_ALIGN;
712 break;
713 case Type::X86_MMXTyID:
714 case Type::VectorTyID:
715 AlignType = VECTOR_ALIGN;
716 break;
717 default:
718 llvm_unreachable("Bad type for getAlignment!!!");
719 }
720
721 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
722 abi_or_pref, Ty);
723}
724
Micah Villmowb4faa152012-10-04 23:01:22 +0000725unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000726 return getAlignment(Ty, true);
727}
728
729/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
730/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000731unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Craig Topperc6207612014-04-09 06:08:46 +0000732 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000733}
734
Micah Villmowb4faa152012-10-04 23:01:22 +0000735unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000736 return getAlignment(Ty, false);
737}
738
Micah Villmowb4faa152012-10-04 23:01:22 +0000739unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000740 unsigned Align = getPrefTypeAlignment(Ty);
741 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
742 return Log2_32(Align);
743}
744
Micah Villmow89021e42012-10-09 16:06:12 +0000745IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
746 unsigned AddressSpace) const {
747 return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000748}
749
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000750Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000751 assert(Ty->isPtrOrPtrVectorTy() &&
752 "Expected a pointer or pointer vector type.");
Matt Arsenault4dbd4892014-04-23 21:10:15 +0000753 unsigned NumBits = getPointerTypeSizeInBits(Ty);
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000754 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
755 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
756 return VectorType::get(IntTy, VecTy->getNumElements());
757 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000758}
759
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000760Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000761 for (unsigned LegalIntWidth : LegalIntWidths)
762 if (Width <= LegalIntWidth)
763 return Type::getIntNTy(C, LegalIntWidth);
Craig Topperc6207612014-04-09 06:08:46 +0000764 return nullptr;
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000765}
766
Matt Arsenault899f7d22013-09-16 22:43:16 +0000767unsigned DataLayout::getLargestLegalIntTypeSize() const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000768 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
769 return Max != LegalIntWidths.end() ? *Max : 0;
Matt Arsenault899f7d22013-09-16 22:43:16 +0000770}
771
Micah Villmowb4faa152012-10-04 23:01:22 +0000772uint64_t DataLayout::getIndexedOffset(Type *ptrTy,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000773 ArrayRef<Value *> Indices) const {
774 Type *Ty = ptrTy;
775 assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
776 uint64_t Result = 0;
777
778 generic_gep_type_iterator<Value* const*>
779 TI = gep_type_begin(ptrTy, Indices);
780 for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
781 ++CurIDX, ++TI) {
782 if (StructType *STy = dyn_cast<StructType>(*TI)) {
783 assert(Indices[CurIDX]->getType() ==
784 Type::getInt32Ty(ptrTy->getContext()) &&
785 "Illegal struct idx");
786 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
787
788 // Get structure layout information...
789 const StructLayout *Layout = getStructLayout(STy);
790
791 // Add in the offset, as calculated by the structure layout info...
792 Result += Layout->getElementOffset(FieldNo);
793
794 // Update Ty to refer to current element
795 Ty = STy->getElementType(FieldNo);
796 } else {
797 // Update Ty to refer to current element
798 Ty = cast<SequentialType>(Ty)->getElementType();
799
800 // Get the array index and the size of each array element.
801 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
802 Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
803 }
804 }
805
806 return Result;
807}
808
809/// getPreferredAlignment - Return the preferred alignment of the specified
810/// global. This includes an explicitly requested alignment (if the global
811/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000812unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000813 Type *ElemType = GV->getType()->getElementType();
814 unsigned Alignment = getPrefTypeAlignment(ElemType);
815 unsigned GVAlignment = GV->getAlignment();
816 if (GVAlignment >= Alignment) {
817 Alignment = GVAlignment;
818 } else if (GVAlignment != 0) {
819 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
820 }
821
822 if (GV->hasInitializer() && GVAlignment == 0) {
823 if (Alignment < 16) {
824 // If the global is not external, see if it is large. If so, give it a
825 // larger alignment.
826 if (getTypeSizeInBits(ElemType) > 128)
827 Alignment = 16; // 16-byte alignment.
828 }
829 }
830 return Alignment;
831}
832
833/// getPreferredAlignmentLog - Return the preferred alignment of the
834/// specified global, returned in log form. This includes an explicitly
835/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000836unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000837 return Log2_32(getPreferredAlignment(GV));
838}
Rafael Espindola93512512014-02-25 17:30:31 +0000839
840DataLayoutPass::DataLayoutPass() : ImmutablePass(ID), DL("") {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000841 initializeDataLayoutPassPass(*PassRegistry::getPassRegistry());
Rafael Espindola93512512014-02-25 17:30:31 +0000842}
843
844DataLayoutPass::~DataLayoutPass() {}
845
Rafael Espindolac435adc2014-09-10 21:27:43 +0000846bool DataLayoutPass::doInitialization(Module &M) {
847 DL.init(&M);
848 return false;
Rafael Espindola93512512014-02-25 17:30:31 +0000849}
850
Rafael Espindolac435adc2014-09-10 21:27:43 +0000851bool DataLayoutPass::doFinalization(Module &M) {
852 DL.reset("");
853 return false;
Rafael Espindola93512512014-02-25 17:30:31 +0000854}