blob: a22588628f752c390fd1ac6418576a1f83d1ec16 [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Constants.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Module.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000025#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Support/GetElementPtrTypeIterator.h"
27#include "llvm/Support/ManagedStatic.h"
28#include "llvm/Support/MathExtras.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000029#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Support/raw_ostream.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000031#include <algorithm>
32#include <cstdlib>
33using namespace llvm;
34
Micah Villmowb4faa152012-10-04 23:01:22 +000035// Handle the Pass registration stuff necessary to use DataLayout's.
Micah Villmowac34b5c2012-10-04 22:08:14 +000036
37// Register the default SparcV9 implementation...
Micah Villmowb4faa152012-10-04 23:01:22 +000038INITIALIZE_PASS(DataLayout, "datalayout", "Data Layout", false, true)
39char DataLayout::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)
Micah Villmowb4faa152012-10-04 23:01:22 +000058 StructSize = DataLayout::RoundUpAlignment(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)
Micah Villmowb4faa152012-10-04 23:01:22 +000073 StructSize = DataLayout::RoundUpAlignment(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 Espindolae23b8772013-12-20 15:21:32 +0000155static const LayoutAlignElem DefaultAlignments[] = {
Rafael Espindola458a4852013-12-19 23:03:03 +0000156 { INTEGER_ALIGN, 1, 1, 1 }, // i1
157 { INTEGER_ALIGN, 8, 1, 1 }, // i8
158 { INTEGER_ALIGN, 16, 2, 2 }, // i16
159 { INTEGER_ALIGN, 32, 4, 4 }, // i32
160 { INTEGER_ALIGN, 64, 4, 8 }, // i64
161 { FLOAT_ALIGN, 16, 2, 2 }, // half
162 { FLOAT_ALIGN, 32, 4, 4 }, // float
163 { FLOAT_ALIGN, 64, 8, 8 }, // double
164 { FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ...
165 { VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ...
166 { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ...
167 { AGGREGATE_ALIGN, 0, 0, 8 } // struct
168};
169
Patrik Hägglund01860a62012-11-14 09:04:56 +0000170void DataLayout::init(StringRef Desc) {
Micah Villmowb4faa152012-10-04 23:01:22 +0000171 initializeDataLayoutPass(*PassRegistry::getPassRegistry());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000172
173 LayoutMap = 0;
174 LittleEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000175 StackNaturalAlign = 0;
176
177 // Default alignments
Rafael Espindola458a4852013-12-19 23:03:03 +0000178 for (int I = 0, N = array_lengthof(DefaultAlignments); I < N; ++I) {
Rafael Espindolae23b8772013-12-20 15:21:32 +0000179 const LayoutAlignElem &E = DefaultAlignments[I];
Rafael Espindola458a4852013-12-19 23:03:03 +0000180 setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign,
181 E.TypeBitWidth);
182 }
Micah Villmow89021e42012-10-09 16:06:12 +0000183 setPointerAlignment(0, 8, 8, 8);
Patrik Hägglund01860a62012-11-14 09:04:56 +0000184
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000185 parseSpecifier(Desc);
186}
187
188/// Checked version of split, to ensure mandatory subparts.
189static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
190 assert(!Str.empty() && "parse error, string can't be empty here");
191 std::pair<StringRef, StringRef> Split = Str.split(Separator);
192 assert((!Split.second.empty() || Split.first == Str) &&
193 "a trailing separator is not allowed");
194 return Split;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000195}
196
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000197/// Get an unsinged integer, including error checks.
198static unsigned getInt(StringRef R) {
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000199 unsigned Result;
Patrik Hägglund504f4782012-11-28 14:32:52 +0000200 bool error = R.getAsInteger(10, Result); (void)error;
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000201 assert(!error && "not a number, or does not fit in an unsigned int");
202 return Result;
203}
204
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000205/// Convert bits into bytes. Assert if not a byte width multiple.
206static unsigned inBytes(unsigned Bits) {
207 assert(Bits % 8 == 0 && "number of bits must be a byte width multiple");
208 return Bits / 8;
209}
210
211void DataLayout::parseSpecifier(StringRef Desc) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000212 while (!Desc.empty()) {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000213 // Split at '-'.
214 std::pair<StringRef, StringRef> Split = split(Desc, '-');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000215 Desc = Split.second;
216
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000217 // Split at ':'.
218 Split = split(Split.first, ':');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000219
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000220 // Aliases used below.
221 StringRef &Tok = Split.first; // Current token.
222 StringRef &Rest = Split.second; // The rest of the string.
Micah Villmowac34b5c2012-10-04 22:08:14 +0000223
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000224 char Specifier = Tok.front();
225 Tok = Tok.substr(1);
226
227 switch (Specifier) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000228 case 'E':
Patrik Hägglund01860a62012-11-14 09:04:56 +0000229 LittleEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000230 break;
231 case 'e':
Patrik Hägglund01860a62012-11-14 09:04:56 +0000232 LittleEndian = true;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000233 break;
234 case 'p': {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000235 // Address space.
236 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
237 assert(AddrSpace < 1 << 24 &&
238 "Invalid address space, must be a 24bit integer");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000239
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000240 // Size.
241 Split = split(Rest, ':');
242 unsigned PointerMemSize = inBytes(getInt(Tok));
243
244 // ABI alignment.
245 Split = split(Rest, ':');
246 unsigned PointerABIAlign = inBytes(getInt(Tok));
247
248 // Preferred alignment.
249 unsigned PointerPrefAlign = PointerABIAlign;
250 if (!Rest.empty()) {
251 Split = split(Rest, ':');
252 PointerPrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000253 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000254
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000255 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
256 PointerMemSize);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000257 break;
258 }
259 case 'i':
260 case 'v':
261 case 'f':
262 case 'a':
263 case 's': {
264 AlignTypeEnum AlignType;
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000265 switch (Specifier) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000266 default:
267 case 'i': AlignType = INTEGER_ALIGN; break;
268 case 'v': AlignType = VECTOR_ALIGN; break;
269 case 'f': AlignType = FLOAT_ALIGN; break;
270 case 'a': AlignType = AGGREGATE_ALIGN; break;
271 case 's': AlignType = STACK_ALIGN; break;
272 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000273
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000274 // Bit size.
275 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
276
277 // ABI alignment.
278 Split = split(Rest, ':');
279 unsigned ABIAlign = inBytes(getInt(Tok));
280
281 // Preferred alignment.
282 unsigned PrefAlign = ABIAlign;
283 if (!Rest.empty()) {
284 Split = split(Rest, ':');
285 PrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000286 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000287
Patrik Hägglund01860a62012-11-14 09:04:56 +0000288 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmowb4faa152012-10-04 23:01:22 +0000289
Micah Villmowac34b5c2012-10-04 22:08:14 +0000290 break;
291 }
292 case 'n': // Native integer types.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000293 for (;;) {
294 unsigned Width = getInt(Tok);
295 assert(Width != 0 && "width must be non-zero");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000296 LegalIntWidths.push_back(Width);
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000297 if (Rest.empty())
298 break;
299 Split = split(Rest, ':');
300 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000301 break;
302 case 'S': { // Stack natural alignment.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000303 StackNaturalAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000304 break;
305 }
306 default:
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000307 llvm_unreachable("Unknown specifier in datalayout string");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000308 break;
309 }
310 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000311}
312
313/// Default ctor.
314///
315/// @note This has to exist, because this is a pass, but it should never be
316/// used.
Micah Villmowb4faa152012-10-04 23:01:22 +0000317DataLayout::DataLayout() : ImmutablePass(ID) {
318 report_fatal_error("Bad DataLayout ctor used. "
Eli Benderskyfaf5e3e2013-01-30 19:24:23 +0000319 "Tool did not specify a DataLayout to use?");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000320}
321
Micah Villmowb4faa152012-10-04 23:01:22 +0000322DataLayout::DataLayout(const Module *M)
Micah Villmowac34b5c2012-10-04 22:08:14 +0000323 : ImmutablePass(ID) {
Patrik Hägglund01860a62012-11-14 09:04:56 +0000324 init(M->getDataLayout());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000325}
326
327void
Micah Villmowb4faa152012-10-04 23:01:22 +0000328DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000329 unsigned pref_align, uint32_t bit_width) {
330 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
331 assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield");
332 assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield");
333 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmow6d05e692012-10-05 17:02:14 +0000334 if (Alignments[i].AlignType == (unsigned)align_type &&
Micah Villmowac34b5c2012-10-04 22:08:14 +0000335 Alignments[i].TypeBitWidth == bit_width) {
336 // Update the abi, preferred alignments.
337 Alignments[i].ABIAlign = abi_align;
338 Alignments[i].PrefAlign = pref_align;
339 return;
340 }
341 }
342
Micah Villmowb4faa152012-10-04 23:01:22 +0000343 Alignments.push_back(LayoutAlignElem::get(align_type, abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000344 pref_align, bit_width));
345}
346
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000347void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
348 unsigned PrefAlign,
349 uint32_t TypeByteWidth) {
350 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
351 DenseMap<unsigned,PointerAlignElem>::iterator val = Pointers.find(AddrSpace);
Micah Villmow89021e42012-10-09 16:06:12 +0000352 if (val == Pointers.end()) {
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000353 Pointers[AddrSpace] =
354 PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign, TypeByteWidth);
Micah Villmow89021e42012-10-09 16:06:12 +0000355 } else {
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000356 val->second.ABIAlign = ABIAlign;
357 val->second.PrefAlign = PrefAlign;
358 val->second.TypeByteWidth = TypeByteWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000359 }
360}
361
Micah Villmowac34b5c2012-10-04 22:08:14 +0000362/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000363/// preferred if ABIInfo = false) the layout wants for the specified datatype.
364unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000365 uint32_t BitWidth, bool ABIInfo,
366 Type *Ty) const {
367 // Check to see if we have an exact match and remember the best match we see.
368 int BestMatchIdx = -1;
369 int LargestInt = -1;
370 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmow6d05e692012-10-05 17:02:14 +0000371 if (Alignments[i].AlignType == (unsigned)AlignType &&
Micah Villmowac34b5c2012-10-04 22:08:14 +0000372 Alignments[i].TypeBitWidth == BitWidth)
373 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
374
375 // The best match so far depends on what we're looking for.
376 if (AlignType == INTEGER_ALIGN &&
377 Alignments[i].AlignType == INTEGER_ALIGN) {
378 // The "best match" for integers is the smallest size that is larger than
379 // the BitWidth requested.
380 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
Eli Benderskyfaf5e3e2013-01-30 19:24:23 +0000381 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
Micah Villmowac34b5c2012-10-04 22:08:14 +0000382 BestMatchIdx = i;
383 // However, if there isn't one that's larger, then we must use the
384 // largest one we have (see below)
385 if (LargestInt == -1 ||
386 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
387 LargestInt = i;
388 }
389 }
390
391 // Okay, we didn't find an exact solution. Fall back here depending on what
392 // is being looked for.
393 if (BestMatchIdx == -1) {
394 // If we didn't find an integer alignment, fall back on most conservative.
395 if (AlignType == INTEGER_ALIGN) {
396 BestMatchIdx = LargestInt;
397 } else {
398 assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
399
400 // By default, use natural alignment for vector types. This is consistent
401 // with what clang and llvm-gcc do.
402 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
403 Align *= cast<VectorType>(Ty)->getNumElements();
404 // If the alignment is not a power of 2, round up to the next power of 2.
405 // This happens for non-power-of-2 length vectors.
406 if (Align & (Align-1))
407 Align = NextPowerOf2(Align);
408 return Align;
409 }
410 }
411
412 // Since we got a "best match" index, just return it.
413 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
414 : Alignments[BestMatchIdx].PrefAlign;
415}
416
417namespace {
418
419class StructLayoutMap {
420 typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
421 LayoutInfoTy LayoutInfo;
422
423public:
424 virtual ~StructLayoutMap() {
425 // Remove any layouts.
426 for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end();
427 I != E; ++I) {
428 StructLayout *Value = I->second;
429 Value->~StructLayout();
430 free(Value);
431 }
432 }
433
434 StructLayout *&operator[](StructType *STy) {
435 return LayoutInfo[STy];
436 }
437
438 // for debugging...
439 virtual void dump() const {}
440};
441
442} // end anonymous namespace
443
Micah Villmowb4faa152012-10-04 23:01:22 +0000444DataLayout::~DataLayout() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000445 delete static_cast<StructLayoutMap*>(LayoutMap);
446}
447
Pete Cooper6308a822013-03-12 17:37:31 +0000448bool DataLayout::doFinalization(Module &M) {
449 delete static_cast<StructLayoutMap*>(LayoutMap);
450 LayoutMap = 0;
451 return false;
452}
453
Micah Villmowb4faa152012-10-04 23:01:22 +0000454const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000455 if (!LayoutMap)
456 LayoutMap = new StructLayoutMap();
457
458 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
459 StructLayout *&SL = (*STM)[Ty];
460 if (SL) return SL;
461
462 // Otherwise, create the struct layout. Because it is variable length, we
463 // malloc it, then use placement new.
464 int NumElts = Ty->getNumElements();
465 StructLayout *L =
466 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
467
468 // Set SL before calling StructLayout's ctor. The ctor could cause other
469 // entries to be added to TheMap, invalidating our reference.
470 SL = L;
471
472 new (L) StructLayout(Ty, *this);
473
474 return L;
475}
476
Micah Villmowb4faa152012-10-04 23:01:22 +0000477std::string DataLayout::getStringRepresentation() const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000478 std::string Result;
479 raw_string_ostream OS(Result);
480
Micah Villmow89021e42012-10-09 16:06:12 +0000481 OS << (LittleEndian ? "e" : "E");
482 SmallVector<unsigned, 8> addrSpaces;
483 // Lets get all of the known address spaces and sort them
484 // into increasing order so that we can emit the string
485 // in a cleaner format.
486 for (DenseMap<unsigned, PointerAlignElem>::const_iterator
487 pib = Pointers.begin(), pie = Pointers.end();
488 pib != pie; ++pib) {
489 addrSpaces.push_back(pib->first);
490 }
491 std::sort(addrSpaces.begin(), addrSpaces.end());
Craig Topperaf0dea12013-07-04 01:31:24 +0000492 for (SmallVectorImpl<unsigned>::iterator asb = addrSpaces.begin(),
Micah Villmow89021e42012-10-09 16:06:12 +0000493 ase = addrSpaces.end(); asb != ase; ++asb) {
494 const PointerAlignElem &PI = Pointers.find(*asb)->second;
Rafael Espindola458a4852013-12-19 23:03:03 +0000495
496 // Skip default.
497 if (PI.AddressSpace == 0 && PI.ABIAlign == 8 && PI.PrefAlign == 8 &&
498 PI.TypeByteWidth == 8)
499 continue;
500
Micah Villmow89021e42012-10-09 16:06:12 +0000501 OS << "-p";
502 if (PI.AddressSpace) {
503 OS << PI.AddressSpace;
504 }
Rafael Espindola458a4852013-12-19 23:03:03 +0000505 OS << ":" << PI.TypeByteWidth*8 << ':' << PI.ABIAlign*8;
506 if (PI.PrefAlign != PI.ABIAlign)
507 OS << ':' << PI.PrefAlign*8;
Micah Villmow89021e42012-10-09 16:06:12 +0000508 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000509
Rafael Espindolae23b8772013-12-20 15:21:32 +0000510 const LayoutAlignElem *DefaultStart = DefaultAlignments;
511 const LayoutAlignElem *DefaultEnd =
Rafael Espindola458a4852013-12-19 23:03:03 +0000512 DefaultStart + array_lengthof(DefaultAlignments);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000513 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmowb4faa152012-10-04 23:01:22 +0000514 const LayoutAlignElem &AI = Alignments[i];
Rafael Espindola458a4852013-12-19 23:03:03 +0000515 if (std::find(DefaultStart, DefaultEnd, AI) != DefaultEnd)
516 continue;
517 OS << '-' << (char)AI.AlignType;
518 if (AI.TypeBitWidth)
519 OS << AI.TypeBitWidth;
520 OS << ':' << AI.ABIAlign*8;
521 if (AI.ABIAlign != AI.PrefAlign)
522 OS << ':' << AI.PrefAlign*8;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000523 }
524
525 if (!LegalIntWidths.empty()) {
526 OS << "-n" << (unsigned)LegalIntWidths[0];
527
528 for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
529 OS << ':' << (unsigned)LegalIntWidths[i];
530 }
Rafael Espindola458a4852013-12-19 23:03:03 +0000531
532 if (StackNaturalAlign)
533 OS << "-S" << StackNaturalAlign*8;
534
Micah Villmowac34b5c2012-10-04 22:08:14 +0000535 return OS.str();
536}
537
Matt Arsenault6f4be902013-07-26 17:37:20 +0000538unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
539 assert(Ty->isPtrOrPtrVectorTy() &&
540 "This should only be called with a pointer or pointer vector type");
541
542 if (Ty->isPointerTy())
543 return getTypeSizeInBits(Ty);
544
Matt Arsenault517cf482013-07-27 19:22:28 +0000545 return getTypeSizeInBits(Ty->getScalarType());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000546}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000547
Micah Villmowac34b5c2012-10-04 22:08:14 +0000548/*!
549 \param abi_or_pref Flag that determines which alignment is returned. true
550 returns the ABI alignment, false returns the preferred alignment.
551 \param Ty The underlying type for which alignment is determined.
552
553 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
554 == false) for the requested type \a Ty.
555 */
Micah Villmowb4faa152012-10-04 23:01:22 +0000556unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000557 int AlignType = -1;
558
559 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
560 switch (Ty->getTypeID()) {
561 // Early escape for the non-numeric types.
562 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000563 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000564 ? getPointerABIAlignment(0)
565 : getPointerPrefAlignment(0));
566 case Type::PointerTyID: {
567 unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace();
568 return (abi_or_pref
569 ? getPointerABIAlignment(AS)
570 : getPointerPrefAlignment(AS));
571 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000572 case Type::ArrayTyID:
573 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
574
575 case Type::StructTyID: {
576 // Packed structure types always have an ABI alignment of one.
577 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
578 return 1;
579
580 // Get the layout annotation... which is lazily created on demand.
581 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
582 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
583 return std::max(Align, Layout->getAlignment());
584 }
585 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000586 AlignType = INTEGER_ALIGN;
587 break;
588 case Type::HalfTyID:
589 case Type::FloatTyID:
590 case Type::DoubleTyID:
591 // PPC_FP128TyID and FP128TyID have different data contents, but the
592 // same size and alignment, so they look the same here.
593 case Type::PPC_FP128TyID:
594 case Type::FP128TyID:
595 case Type::X86_FP80TyID:
596 AlignType = FLOAT_ALIGN;
597 break;
598 case Type::X86_MMXTyID:
599 case Type::VectorTyID:
600 AlignType = VECTOR_ALIGN;
601 break;
602 default:
603 llvm_unreachable("Bad type for getAlignment!!!");
604 }
605
606 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
607 abi_or_pref, Ty);
608}
609
Micah Villmowb4faa152012-10-04 23:01:22 +0000610unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000611 return getAlignment(Ty, true);
612}
613
614/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
615/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000616unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000617 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0);
618}
619
Micah Villmowb4faa152012-10-04 23:01:22 +0000620unsigned DataLayout::getCallFrameTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000621 for (unsigned i = 0, e = Alignments.size(); i != e; ++i)
622 if (Alignments[i].AlignType == STACK_ALIGN)
623 return Alignments[i].ABIAlign;
624
625 return getABITypeAlignment(Ty);
626}
627
Micah Villmowb4faa152012-10-04 23:01:22 +0000628unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000629 return getAlignment(Ty, false);
630}
631
Micah Villmowb4faa152012-10-04 23:01:22 +0000632unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000633 unsigned Align = getPrefTypeAlignment(Ty);
634 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
635 return Log2_32(Align);
636}
637
Micah Villmow89021e42012-10-09 16:06:12 +0000638IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
639 unsigned AddressSpace) const {
640 return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000641}
642
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000643Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000644 assert(Ty->isPtrOrPtrVectorTy() &&
645 "Expected a pointer or pointer vector type.");
Chandler Carruth7ec50852012-11-01 08:07:29 +0000646 unsigned NumBits = getTypeSizeInBits(Ty->getScalarType());
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000647 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
648 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
649 return VectorType::get(IntTy, VecTy->getNumElements());
650 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000651}
652
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000653Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
654 for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i)
655 if (Width <= LegalIntWidths[i])
656 return Type::getIntNTy(C, LegalIntWidths[i]);
657 return 0;
658}
659
Matt Arsenault899f7d22013-09-16 22:43:16 +0000660unsigned DataLayout::getLargestLegalIntTypeSize() const {
661 unsigned MaxWidth = 0;
662 for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i)
663 MaxWidth = std::max<unsigned>(MaxWidth, LegalIntWidths[i]);
664 return MaxWidth;
665}
666
Micah Villmowb4faa152012-10-04 23:01:22 +0000667uint64_t DataLayout::getIndexedOffset(Type *ptrTy,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000668 ArrayRef<Value *> Indices) const {
669 Type *Ty = ptrTy;
670 assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
671 uint64_t Result = 0;
672
673 generic_gep_type_iterator<Value* const*>
674 TI = gep_type_begin(ptrTy, Indices);
675 for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
676 ++CurIDX, ++TI) {
677 if (StructType *STy = dyn_cast<StructType>(*TI)) {
678 assert(Indices[CurIDX]->getType() ==
679 Type::getInt32Ty(ptrTy->getContext()) &&
680 "Illegal struct idx");
681 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
682
683 // Get structure layout information...
684 const StructLayout *Layout = getStructLayout(STy);
685
686 // Add in the offset, as calculated by the structure layout info...
687 Result += Layout->getElementOffset(FieldNo);
688
689 // Update Ty to refer to current element
690 Ty = STy->getElementType(FieldNo);
691 } else {
692 // Update Ty to refer to current element
693 Ty = cast<SequentialType>(Ty)->getElementType();
694
695 // Get the array index and the size of each array element.
696 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
697 Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
698 }
699 }
700
701 return Result;
702}
703
704/// getPreferredAlignment - Return the preferred alignment of the specified
705/// global. This includes an explicitly requested alignment (if the global
706/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000707unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000708 Type *ElemType = GV->getType()->getElementType();
709 unsigned Alignment = getPrefTypeAlignment(ElemType);
710 unsigned GVAlignment = GV->getAlignment();
711 if (GVAlignment >= Alignment) {
712 Alignment = GVAlignment;
713 } else if (GVAlignment != 0) {
714 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
715 }
716
717 if (GV->hasInitializer() && GVAlignment == 0) {
718 if (Alignment < 16) {
719 // If the global is not external, see if it is large. If so, give it a
720 // larger alignment.
721 if (getTypeSizeInBits(ElemType) > 128)
722 Alignment = 16; // 16-byte alignment.
723 }
724 }
725 return Alignment;
726}
727
728/// getPreferredAlignmentLog - Return the preferred alignment of the
729/// specified global, returned in log form. This includes an explicitly
730/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000731unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000732 return Log2_32(getPreferredAlignment(GV));
733}