blob: 573e904f2ee820f4d81ea0df7a152181421c4992 [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));
Owen Andersonab1c7a72015-03-02 09:34:59 +0000315 if (AlignType != AGGREGATE_ALIGN && !ABIAlign)
316 report_fatal_error(
317 "ABI alignment specification must be >0 for non-aggregate types");
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000318
319 // Preferred alignment.
320 unsigned PrefAlign = ABIAlign;
321 if (!Rest.empty()) {
322 Split = split(Rest, ':');
323 PrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000324 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000325
Patrik Hägglund01860a62012-11-14 09:04:56 +0000326 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmowb4faa152012-10-04 23:01:22 +0000327
Micah Villmowac34b5c2012-10-04 22:08:14 +0000328 break;
329 }
330 case 'n': // Native integer types.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000331 for (;;) {
332 unsigned Width = getInt(Tok);
David Majnemer5330c692014-12-10 01:17:08 +0000333 if (Width == 0)
334 report_fatal_error(
335 "Zero width native integer type in datalayout string");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000336 LegalIntWidths.push_back(Width);
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000337 if (Rest.empty())
338 break;
339 Split = split(Rest, ':');
340 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000341 break;
342 case 'S': { // Stack natural alignment.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000343 StackNaturalAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000344 break;
345 }
Rafael Espindola58873562014-01-03 19:21:54 +0000346 case 'm':
David Majnemer612f3122014-12-10 02:36:41 +0000347 if (!Tok.empty())
348 report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string");
349 if (Rest.empty())
350 report_fatal_error("Expected mangling specifier in datalayout string");
351 if (Rest.size() > 1)
352 report_fatal_error("Unknown mangling specifier in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000353 switch(Rest[0]) {
354 default:
David Majnemer5330c692014-12-10 01:17:08 +0000355 report_fatal_error("Unknown mangling in datalayout string");
Rafael Espindola58873562014-01-03 19:21:54 +0000356 case 'e':
357 ManglingMode = MM_ELF;
358 break;
359 case 'o':
360 ManglingMode = MM_MachO;
361 break;
362 case 'm':
363 ManglingMode = MM_Mips;
364 break;
Rafael Espindolaaf77e122014-01-10 13:42:12 +0000365 case 'w':
366 ManglingMode = MM_WINCOFF;
Rafael Espindola58873562014-01-03 19:21:54 +0000367 break;
368 }
369 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000370 default:
David Majnemer5330c692014-12-10 01:17:08 +0000371 report_fatal_error("Unknown specifier in datalayout string");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000372 break;
373 }
374 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000375}
376
Craig Topperc6207612014-04-09 06:08:46 +0000377DataLayout::DataLayout(const Module *M) : LayoutMap(nullptr) {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000378 init(M);
379}
380
381void DataLayout::init(const Module *M) {
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000382 const DataLayout *Other = M->getDataLayout();
383 if (Other)
384 *this = *Other;
385 else
Rafael Espindola248ac132014-02-25 22:23:04 +0000386 reset("");
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000387}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000388
Rafael Espindolaae593f12014-02-26 17:02:08 +0000389bool DataLayout::operator==(const DataLayout &Other) const {
Chandler Carruthf67321c2014-10-20 10:41:29 +0000390 bool Ret = BigEndian == Other.BigEndian &&
Rafael Espindolaae593f12014-02-26 17:02:08 +0000391 StackNaturalAlign == Other.StackNaturalAlign &&
392 ManglingMode == Other.ManglingMode &&
393 LegalIntWidths == Other.LegalIntWidths &&
Rafael Espindola89992b02014-04-22 17:47:03 +0000394 Alignments == Other.Alignments && Pointers == Other.Pointers;
Rafael Espindolaae593f12014-02-26 17:02:08 +0000395 assert(Ret == (getStringRepresentation() == Other.getStringRepresentation()));
396 return Ret;
397}
398
Micah Villmowac34b5c2012-10-04 22:08:14 +0000399void
Micah Villmowb4faa152012-10-04 23:01:22 +0000400DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000401 unsigned pref_align, uint32_t bit_width) {
David Majnemer1b9fc3a2015-02-16 05:41:53 +0000402 if (!isUInt<24>(bit_width))
403 report_fatal_error("Invalid bit width, must be a 24bit integer");
404 if (!isUInt<16>(abi_align))
405 report_fatal_error("Invalid ABI alignment, must be a 16bit integer");
406 if (!isUInt<16>(pref_align))
407 report_fatal_error("Invalid preferred alignment, must be a 16bit integer");
Owen Anderson5af4b212015-03-02 09:35:03 +0000408 if (abi_align != 0 && !isPowerOf2_64(abi_align))
409 report_fatal_error("Invalid ABI alignment, must be a power of 2");
410 if (pref_align != 0 && !isPowerOf2_64(pref_align))
411 report_fatal_error("Invalid preferred alignment, must be a power of 2");
David Majnemer1b9fc3a2015-02-16 05:41:53 +0000412
413 if (pref_align < abi_align)
414 report_fatal_error(
415 "Preferred alignment cannot be less than the ABI alignment");
416
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000417 for (LayoutAlignElem &Elem : Alignments) {
418 if (Elem.AlignType == (unsigned)align_type &&
419 Elem.TypeBitWidth == bit_width) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000420 // Update the abi, preferred alignments.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000421 Elem.ABIAlign = abi_align;
422 Elem.PrefAlign = pref_align;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000423 return;
424 }
425 }
426
Micah Villmowb4faa152012-10-04 23:01:22 +0000427 Alignments.push_back(LayoutAlignElem::get(align_type, abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000428 pref_align, bit_width));
429}
430
Rafael Espindola667fcb82014-02-26 16:58:35 +0000431DataLayout::PointersTy::iterator
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000432DataLayout::findPointerLowerBound(uint32_t AddressSpace) {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000433 return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace,
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000434 [](const PointerAlignElem &A, uint32_t AddressSpace) {
435 return A.AddressSpace < AddressSpace;
436 });
Rafael Espindola667fcb82014-02-26 16:58:35 +0000437}
438
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000439void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
440 unsigned PrefAlign,
441 uint32_t TypeByteWidth) {
David Majnemer4b042922015-02-16 05:41:55 +0000442 if (PrefAlign < ABIAlign)
443 report_fatal_error(
444 "Preferred alignment cannot be less than the ABI alignment");
445
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000446 PointersTy::iterator I = findPointerLowerBound(AddrSpace);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000447 if (I == Pointers.end() || I->AddressSpace != AddrSpace) {
448 Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign,
449 TypeByteWidth));
Micah Villmow89021e42012-10-09 16:06:12 +0000450 } else {
Rafael Espindola667fcb82014-02-26 16:58:35 +0000451 I->ABIAlign = ABIAlign;
452 I->PrefAlign = PrefAlign;
453 I->TypeByteWidth = TypeByteWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000454 }
455}
456
Micah Villmowac34b5c2012-10-04 22:08:14 +0000457/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000458/// preferred if ABIInfo = false) the layout wants for the specified datatype.
459unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000460 uint32_t BitWidth, bool ABIInfo,
461 Type *Ty) const {
462 // Check to see if we have an exact match and remember the best match we see.
463 int BestMatchIdx = -1;
464 int LargestInt = -1;
465 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmow6d05e692012-10-05 17:02:14 +0000466 if (Alignments[i].AlignType == (unsigned)AlignType &&
Micah Villmowac34b5c2012-10-04 22:08:14 +0000467 Alignments[i].TypeBitWidth == BitWidth)
468 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
469
470 // The best match so far depends on what we're looking for.
471 if (AlignType == INTEGER_ALIGN &&
472 Alignments[i].AlignType == INTEGER_ALIGN) {
473 // The "best match" for integers is the smallest size that is larger than
474 // the BitWidth requested.
475 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
Eli Benderskyfaf5e3e2013-01-30 19:24:23 +0000476 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
Micah Villmowac34b5c2012-10-04 22:08:14 +0000477 BestMatchIdx = i;
478 // However, if there isn't one that's larger, then we must use the
479 // largest one we have (see below)
480 if (LargestInt == -1 ||
481 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
482 LargestInt = i;
483 }
484 }
485
486 // Okay, we didn't find an exact solution. Fall back here depending on what
487 // is being looked for.
488 if (BestMatchIdx == -1) {
489 // If we didn't find an integer alignment, fall back on most conservative.
490 if (AlignType == INTEGER_ALIGN) {
491 BestMatchIdx = LargestInt;
492 } else {
493 assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
494
495 // By default, use natural alignment for vector types. This is consistent
496 // with what clang and llvm-gcc do.
497 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
498 Align *= cast<VectorType>(Ty)->getNumElements();
499 // If the alignment is not a power of 2, round up to the next power of 2.
500 // This happens for non-power-of-2 length vectors.
501 if (Align & (Align-1))
502 Align = NextPowerOf2(Align);
503 return Align;
504 }
505 }
506
507 // Since we got a "best match" index, just return it.
508 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
509 : Alignments[BestMatchIdx].PrefAlign;
510}
511
512namespace {
513
514class StructLayoutMap {
515 typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
516 LayoutInfoTy LayoutInfo;
517
518public:
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000519 ~StructLayoutMap() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000520 // Remove any layouts.
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000521 for (const auto &I : LayoutInfo) {
522 StructLayout *Value = I.second;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000523 Value->~StructLayout();
524 free(Value);
525 }
526 }
527
528 StructLayout *&operator[](StructType *STy) {
529 return LayoutInfo[STy];
530 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000531};
532
533} // end anonymous namespace
534
Rafael Espindola248ac132014-02-25 22:23:04 +0000535void DataLayout::clear() {
536 LegalIntWidths.clear();
537 Alignments.clear();
538 Pointers.clear();
539 delete static_cast<StructLayoutMap *>(LayoutMap);
Craig Topperc6207612014-04-09 06:08:46 +0000540 LayoutMap = nullptr;
Rafael Espindola248ac132014-02-25 22:23:04 +0000541}
542
Micah Villmowb4faa152012-10-04 23:01:22 +0000543DataLayout::~DataLayout() {
Rafael Espindola248ac132014-02-25 22:23:04 +0000544 clear();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000545}
546
Micah Villmowb4faa152012-10-04 23:01:22 +0000547const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000548 if (!LayoutMap)
549 LayoutMap = new StructLayoutMap();
550
551 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
552 StructLayout *&SL = (*STM)[Ty];
553 if (SL) return SL;
554
555 // Otherwise, create the struct layout. Because it is variable length, we
556 // malloc it, then use placement new.
557 int NumElts = Ty->getNumElements();
558 StructLayout *L =
559 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
560
561 // Set SL before calling StructLayout's ctor. The ctor could cause other
562 // entries to be added to TheMap, invalidating our reference.
563 SL = L;
564
565 new (L) StructLayout(Ty, *this);
566
567 return L;
568}
569
Micah Villmowb4faa152012-10-04 23:01:22 +0000570std::string DataLayout::getStringRepresentation() const {
Alp Tokere69170a2014-06-26 22:52:05 +0000571 std::string Result;
572 raw_string_ostream OS(Result);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000573
Chandler Carruthf67321c2014-10-20 10:41:29 +0000574 OS << (BigEndian ? "E" : "e");
Rafael Espindola58873562014-01-03 19:21:54 +0000575
576 switch (ManglingMode) {
577 case MM_None:
578 break;
579 case MM_ELF:
580 OS << "-m:e";
581 break;
582 case MM_MachO:
583 OS << "-m:o";
584 break;
Rafael Espindolaaf77e122014-01-10 13:42:12 +0000585 case MM_WINCOFF:
586 OS << "-m:w";
Rafael Espindola58873562014-01-03 19:21:54 +0000587 break;
588 case MM_Mips:
589 OS << "-m:m";
590 break;
591 }
592
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000593 for (const PointerAlignElem &PI : Pointers) {
Rafael Espindola458a4852013-12-19 23:03:03 +0000594 // Skip default.
595 if (PI.AddressSpace == 0 && PI.ABIAlign == 8 && PI.PrefAlign == 8 &&
596 PI.TypeByteWidth == 8)
597 continue;
598
Micah Villmow89021e42012-10-09 16:06:12 +0000599 OS << "-p";
600 if (PI.AddressSpace) {
601 OS << PI.AddressSpace;
602 }
Rafael Espindola458a4852013-12-19 23:03:03 +0000603 OS << ":" << PI.TypeByteWidth*8 << ':' << PI.ABIAlign*8;
604 if (PI.PrefAlign != PI.ABIAlign)
605 OS << ':' << PI.PrefAlign*8;
Micah Villmow89021e42012-10-09 16:06:12 +0000606 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000607
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000608 for (const LayoutAlignElem &AI : Alignments) {
609 if (std::find(std::begin(DefaultAlignments), std::end(DefaultAlignments),
610 AI) != std::end(DefaultAlignments))
Rafael Espindola458a4852013-12-19 23:03:03 +0000611 continue;
612 OS << '-' << (char)AI.AlignType;
613 if (AI.TypeBitWidth)
614 OS << AI.TypeBitWidth;
615 OS << ':' << AI.ABIAlign*8;
616 if (AI.ABIAlign != AI.PrefAlign)
617 OS << ':' << AI.PrefAlign*8;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000618 }
619
620 if (!LegalIntWidths.empty()) {
621 OS << "-n" << (unsigned)LegalIntWidths[0];
622
623 for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
624 OS << ':' << (unsigned)LegalIntWidths[i];
625 }
Rafael Espindola458a4852013-12-19 23:03:03 +0000626
627 if (StackNaturalAlign)
628 OS << "-S" << StackNaturalAlign*8;
629
Micah Villmowac34b5c2012-10-04 22:08:14 +0000630 return OS.str();
631}
632
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000633unsigned DataLayout::getPointerABIAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000634 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000635 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000636 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000637 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000638 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000639 return I->ABIAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000640}
641
642unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000643 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000644 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000645 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000646 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000647 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000648 return I->PrefAlign;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000649}
650
651unsigned DataLayout::getPointerSize(unsigned AS) const {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000652 PointersTy::const_iterator I = findPointerLowerBound(AS);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000653 if (I == Pointers.end() || I->AddressSpace != AS) {
Rafael Espindolae8ae0db2014-02-26 17:05:38 +0000654 I = findPointerLowerBound(0);
Rafael Espindola667fcb82014-02-26 16:58:35 +0000655 assert(I->AddressSpace == 0);
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000656 }
Rafael Espindola667fcb82014-02-26 16:58:35 +0000657 return I->TypeByteWidth;
Rafael Espindola5109fcc2014-02-26 16:49:40 +0000658}
659
Matt Arsenault6f4be902013-07-26 17:37:20 +0000660unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
661 assert(Ty->isPtrOrPtrVectorTy() &&
662 "This should only be called with a pointer or pointer vector type");
663
664 if (Ty->isPointerTy())
665 return getTypeSizeInBits(Ty);
666
Matt Arsenault517cf482013-07-27 19:22:28 +0000667 return getTypeSizeInBits(Ty->getScalarType());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000668}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000669
Micah Villmowac34b5c2012-10-04 22:08:14 +0000670/*!
671 \param abi_or_pref Flag that determines which alignment is returned. true
672 returns the ABI alignment, false returns the preferred alignment.
673 \param Ty The underlying type for which alignment is determined.
674
675 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
676 == false) for the requested type \a Ty.
677 */
Micah Villmowb4faa152012-10-04 23:01:22 +0000678unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000679 int AlignType = -1;
680
681 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
682 switch (Ty->getTypeID()) {
683 // Early escape for the non-numeric types.
684 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000685 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000686 ? getPointerABIAlignment(0)
687 : getPointerPrefAlignment(0));
688 case Type::PointerTyID: {
Matt Arsenaultc1728972014-09-18 22:28:56 +0000689 unsigned AS = cast<PointerType>(Ty)->getAddressSpace();
Micah Villmow89021e42012-10-09 16:06:12 +0000690 return (abi_or_pref
691 ? getPointerABIAlignment(AS)
692 : getPointerPrefAlignment(AS));
693 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000694 case Type::ArrayTyID:
695 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
696
697 case Type::StructTyID: {
698 // Packed structure types always have an ABI alignment of one.
699 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
700 return 1;
701
702 // Get the layout annotation... which is lazily created on demand.
703 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
704 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
705 return std::max(Align, Layout->getAlignment());
706 }
707 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000708 AlignType = INTEGER_ALIGN;
709 break;
710 case Type::HalfTyID:
711 case Type::FloatTyID:
712 case Type::DoubleTyID:
713 // PPC_FP128TyID and FP128TyID have different data contents, but the
714 // same size and alignment, so they look the same here.
715 case Type::PPC_FP128TyID:
716 case Type::FP128TyID:
717 case Type::X86_FP80TyID:
718 AlignType = FLOAT_ALIGN;
719 break;
720 case Type::X86_MMXTyID:
721 case Type::VectorTyID:
722 AlignType = VECTOR_ALIGN;
723 break;
724 default:
725 llvm_unreachable("Bad type for getAlignment!!!");
726 }
727
728 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
729 abi_or_pref, Ty);
730}
731
Micah Villmowb4faa152012-10-04 23:01:22 +0000732unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000733 return getAlignment(Ty, true);
734}
735
736/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
737/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000738unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Craig Topperc6207612014-04-09 06:08:46 +0000739 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000740}
741
Micah Villmowb4faa152012-10-04 23:01:22 +0000742unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000743 return getAlignment(Ty, false);
744}
745
Micah Villmowb4faa152012-10-04 23:01:22 +0000746unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000747 unsigned Align = getPrefTypeAlignment(Ty);
748 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
749 return Log2_32(Align);
750}
751
Micah Villmow89021e42012-10-09 16:06:12 +0000752IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
753 unsigned AddressSpace) const {
754 return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000755}
756
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000757Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000758 assert(Ty->isPtrOrPtrVectorTy() &&
759 "Expected a pointer or pointer vector type.");
Matt Arsenault4dbd4892014-04-23 21:10:15 +0000760 unsigned NumBits = getPointerTypeSizeInBits(Ty);
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000761 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
762 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
763 return VectorType::get(IntTy, VecTy->getNumElements());
764 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000765}
766
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000767Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000768 for (unsigned LegalIntWidth : LegalIntWidths)
769 if (Width <= LegalIntWidth)
770 return Type::getIntNTy(C, LegalIntWidth);
Craig Topperc6207612014-04-09 06:08:46 +0000771 return nullptr;
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000772}
773
Matt Arsenault899f7d22013-09-16 22:43:16 +0000774unsigned DataLayout::getLargestLegalIntTypeSize() const {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000775 auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end());
776 return Max != LegalIntWidths.end() ? *Max : 0;
Matt Arsenault899f7d22013-09-16 22:43:16 +0000777}
778
Micah Villmowb4faa152012-10-04 23:01:22 +0000779uint64_t DataLayout::getIndexedOffset(Type *ptrTy,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000780 ArrayRef<Value *> Indices) const {
781 Type *Ty = ptrTy;
782 assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
783 uint64_t Result = 0;
784
785 generic_gep_type_iterator<Value* const*>
786 TI = gep_type_begin(ptrTy, Indices);
787 for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
788 ++CurIDX, ++TI) {
789 if (StructType *STy = dyn_cast<StructType>(*TI)) {
790 assert(Indices[CurIDX]->getType() ==
791 Type::getInt32Ty(ptrTy->getContext()) &&
792 "Illegal struct idx");
793 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
794
795 // Get structure layout information...
796 const StructLayout *Layout = getStructLayout(STy);
797
798 // Add in the offset, as calculated by the structure layout info...
799 Result += Layout->getElementOffset(FieldNo);
800
801 // Update Ty to refer to current element
802 Ty = STy->getElementType(FieldNo);
803 } else {
804 // Update Ty to refer to current element
805 Ty = cast<SequentialType>(Ty)->getElementType();
806
807 // Get the array index and the size of each array element.
808 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
809 Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
810 }
811 }
812
813 return Result;
814}
815
816/// getPreferredAlignment - Return the preferred alignment of the specified
817/// global. This includes an explicitly requested alignment (if the global
818/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000819unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000820 Type *ElemType = GV->getType()->getElementType();
821 unsigned Alignment = getPrefTypeAlignment(ElemType);
822 unsigned GVAlignment = GV->getAlignment();
823 if (GVAlignment >= Alignment) {
824 Alignment = GVAlignment;
825 } else if (GVAlignment != 0) {
826 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
827 }
828
829 if (GV->hasInitializer() && GVAlignment == 0) {
830 if (Alignment < 16) {
831 // If the global is not external, see if it is large. If so, give it a
832 // larger alignment.
833 if (getTypeSizeInBits(ElemType) > 128)
834 Alignment = 16; // 16-byte alignment.
835 }
836 }
837 return Alignment;
838}
839
840/// getPreferredAlignmentLog - Return the preferred alignment of the
841/// specified global, returned in log form. This includes an explicitly
842/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000843unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000844 return Log2_32(getPreferredAlignment(GV));
845}
Rafael Espindola93512512014-02-25 17:30:31 +0000846
847DataLayoutPass::DataLayoutPass() : ImmutablePass(ID), DL("") {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000848 initializeDataLayoutPassPass(*PassRegistry::getPassRegistry());
Rafael Espindola93512512014-02-25 17:30:31 +0000849}
850
851DataLayoutPass::~DataLayoutPass() {}
852
Rafael Espindolac435adc2014-09-10 21:27:43 +0000853bool DataLayoutPass::doInitialization(Module &M) {
854 DL.init(&M);
855 return false;
Rafael Espindola93512512014-02-25 17:30:31 +0000856}
857
Rafael Espindolac435adc2014-09-10 21:27:43 +0000858bool DataLayoutPass::doFinalization(Module &M) {
859 DL.reset("");
860 return false;
Rafael Espindola93512512014-02-25 17:30:31 +0000861}