blob: 540f4de2f617a3a30b2bf0de970a1738d6490de5 [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
Micah Villmowb4faa152012-10-04 23:01:22 +000019#include "llvm/DataLayout.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000020#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Module.h"
23#include "llvm/Support/GetElementPtrTypeIterator.h"
24#include "llvm/Support/MathExtras.h"
25#include "llvm/Support/ManagedStatic.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/Support/Mutex.h"
29#include "llvm/ADT/DenseMap.h"
30#include <algorithm>
31#include <cstdlib>
32using namespace llvm;
33
Micah Villmowb4faa152012-10-04 23:01:22 +000034// Handle the Pass registration stuff necessary to use DataLayout's.
Micah Villmowac34b5c2012-10-04 22:08:14 +000035
36// Register the default SparcV9 implementation...
Micah Villmowb4faa152012-10-04 23:01:22 +000037INITIALIZE_PASS(DataLayout, "datalayout", "Data Layout", false, true)
38char DataLayout::ID = 0;
Micah Villmowac34b5c2012-10-04 22:08:14 +000039
40//===----------------------------------------------------------------------===//
41// Support for StructLayout
42//===----------------------------------------------------------------------===//
43
Micah Villmowb4faa152012-10-04 23:01:22 +000044StructLayout::StructLayout(StructType *ST, const DataLayout &TD) {
Micah Villmowac34b5c2012-10-04 22:08:14 +000045 assert(!ST->isOpaque() && "Cannot get layout of opaque structs");
46 StructAlignment = 0;
47 StructSize = 0;
48 NumElements = ST->getNumElements();
49
50 // Loop over each of the elements, placing them in memory.
51 for (unsigned i = 0, e = NumElements; i != e; ++i) {
52 Type *Ty = ST->getElementType(i);
53 unsigned TyAlign = ST->isPacked() ? 1 : TD.getABITypeAlignment(Ty);
54
55 // Add padding if necessary to align the data element properly.
56 if ((StructSize & (TyAlign-1)) != 0)
Micah Villmowb4faa152012-10-04 23:01:22 +000057 StructSize = DataLayout::RoundUpAlignment(StructSize, TyAlign);
Micah Villmowac34b5c2012-10-04 22:08:14 +000058
59 // Keep track of maximum alignment constraint.
60 StructAlignment = std::max(TyAlign, StructAlignment);
61
62 MemberOffsets[i] = StructSize;
63 StructSize += TD.getTypeAllocSize(Ty); // Consume space for this data item
64 }
65
66 // Empty structures have alignment of 1 byte.
67 if (StructAlignment == 0) StructAlignment = 1;
68
69 // Add padding to the end of the struct so that it could be put in an array
70 // and all array elements would be aligned correctly.
71 if ((StructSize & (StructAlignment-1)) != 0)
Micah Villmowb4faa152012-10-04 23:01:22 +000072 StructSize = DataLayout::RoundUpAlignment(StructSize, StructAlignment);
Micah Villmowac34b5c2012-10-04 22:08:14 +000073}
74
75
76/// getElementContainingOffset - Given a valid offset into the structure,
77/// return the structure index that contains it.
78unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const {
79 const uint64_t *SI =
80 std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset);
81 assert(SI != &MemberOffsets[0] && "Offset not in structure type!");
82 --SI;
83 assert(*SI <= Offset && "upper_bound didn't work");
84 assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) &&
85 (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) &&
86 "Upper bound didn't work!");
87
88 // Multiple fields can have the same offset if any of them are zero sized.
89 // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop
90 // at the i32 element, because it is the last element at that offset. This is
91 // the right one to return, because anything after it will have a higher
92 // offset, implying that this element is non-empty.
93 return SI-&MemberOffsets[0];
94}
95
96//===----------------------------------------------------------------------===//
Micah Villmowb4faa152012-10-04 23:01:22 +000097// LayoutAlignElem, LayoutAlign support
Micah Villmowac34b5c2012-10-04 22:08:14 +000098//===----------------------------------------------------------------------===//
99
Micah Villmowb4faa152012-10-04 23:01:22 +0000100LayoutAlignElem
101LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000102 unsigned pref_align, uint32_t bit_width) {
103 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
Micah Villmowb4faa152012-10-04 23:01:22 +0000104 LayoutAlignElem retval;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000105 retval.AlignType = align_type;
106 retval.ABIAlign = abi_align;
107 retval.PrefAlign = pref_align;
108 retval.TypeBitWidth = bit_width;
109 return retval;
110}
111
112bool
Micah Villmowb4faa152012-10-04 23:01:22 +0000113LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000114 return (AlignType == rhs.AlignType
115 && ABIAlign == rhs.ABIAlign
116 && PrefAlign == rhs.PrefAlign
117 && TypeBitWidth == rhs.TypeBitWidth);
118}
119
Micah Villmowb4faa152012-10-04 23:01:22 +0000120const LayoutAlignElem
Micah Villmow89021e42012-10-09 16:06:12 +0000121DataLayout::InvalidAlignmentElem =
122 LayoutAlignElem::get((AlignTypeEnum) -1, 0, 0, 0);
123
124//===----------------------------------------------------------------------===//
125// PointerAlignElem, PointerAlign support
126//===----------------------------------------------------------------------===//
127
128PointerAlignElem
129PointerAlignElem::get(uint32_t addr_space, unsigned abi_align,
130 unsigned pref_align, uint32_t bit_width) {
131 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
132 PointerAlignElem retval;
133 retval.AddressSpace = addr_space;
134 retval.ABIAlign = abi_align;
135 retval.PrefAlign = pref_align;
136 retval.TypeBitWidth = bit_width;
137 return retval;
138}
139
140bool
141PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
142 return (ABIAlign == rhs.ABIAlign
143 && AddressSpace == rhs.AddressSpace
144 && PrefAlign == rhs.PrefAlign
145 && TypeBitWidth == rhs.TypeBitWidth);
146}
147
148const PointerAlignElem
149DataLayout::InvalidPointerElem = PointerAlignElem::get(~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
Patrik Hägglund01860a62012-11-14 09:04:56 +0000155void DataLayout::init(StringRef Desc) {
Micah Villmowb4faa152012-10-04 23:01:22 +0000156 initializeDataLayoutPass(*PassRegistry::getPassRegistry());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000157
158 LayoutMap = 0;
159 LittleEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000160 StackNaturalAlign = 0;
161
162 // Default alignments
163 setAlignment(INTEGER_ALIGN, 1, 1, 1); // i1
164 setAlignment(INTEGER_ALIGN, 1, 1, 8); // i8
165 setAlignment(INTEGER_ALIGN, 2, 2, 16); // i16
166 setAlignment(INTEGER_ALIGN, 4, 4, 32); // i32
167 setAlignment(INTEGER_ALIGN, 4, 8, 64); // i64
168 setAlignment(FLOAT_ALIGN, 2, 2, 16); // half
169 setAlignment(FLOAT_ALIGN, 4, 4, 32); // float
170 setAlignment(FLOAT_ALIGN, 8, 8, 64); // double
171 setAlignment(FLOAT_ALIGN, 16, 16, 128); // ppcf128, quad, ...
172 setAlignment(VECTOR_ALIGN, 8, 8, 64); // v2i32, v1i64, ...
173 setAlignment(VECTOR_ALIGN, 16, 16, 128); // v16i8, v8i16, v4i32, ...
174 setAlignment(AGGREGATE_ALIGN, 0, 8, 0); // struct
Micah Villmow89021e42012-10-09 16:06:12 +0000175 setPointerAlignment(0, 8, 8, 8);
Patrik Hägglund01860a62012-11-14 09:04:56 +0000176
177 std::string errMsg = parseSpecifier(Desc);
178 assert(errMsg == "" && "Invalid target data layout string.");
179 (void)errMsg;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000180}
181
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000182/// Get an unsinged integer, including error checks.
183static unsigned getInt(StringRef R) {
184 if (R.empty())
185 return 0;
186 unsigned Result;
187 bool error = R.getAsInteger(10, Result);
188 assert(!error && "not a number, or does not fit in an unsigned int");
189 return Result;
190}
191
Patrik Hägglund01860a62012-11-14 09:04:56 +0000192std::string DataLayout::parseSpecifier(StringRef Desc) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000193
194 while (!Desc.empty()) {
195 std::pair<StringRef, StringRef> Split = Desc.split('-');
196 StringRef Token = Split.first;
197 Desc = Split.second;
198
Micah Villmowac34b5c2012-10-04 22:08:14 +0000199 Split = Token.split(':');
200 StringRef Specifier = Split.first;
201 Token = Split.second;
202
203 assert(!Specifier.empty() && "Can't be empty here");
204
205 switch (Specifier[0]) {
206 case 'E':
Patrik Hägglund01860a62012-11-14 09:04:56 +0000207 LittleEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000208 break;
209 case 'e':
Patrik Hägglund01860a62012-11-14 09:04:56 +0000210 LittleEndian = true;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000211 break;
212 case 'p': {
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000213 unsigned AddrSpace = 0;
Micah Villmow89021e42012-10-09 16:06:12 +0000214 if (Specifier.size() > 1) {
215 AddrSpace = getInt(Specifier.substr(1));
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000216 if (AddrSpace > (1 << 24))
217 return "Invalid address space, must be a 24bit integer";
Micah Villmow89021e42012-10-09 16:06:12 +0000218 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000219 Split = Token.split(':');
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000220 unsigned PointerMemSizeBits = getInt(Split.first);
221 if (PointerMemSizeBits % 8 != 0)
222 return "invalid pointer size, must be an 8-bit multiple";
Micah Villmowac34b5c2012-10-04 22:08:14 +0000223
224 // Pointer ABI alignment.
225 Split = Split.second.split(':');
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000226 unsigned PointerABIAlignBits = getInt(Split.first);
227 if (PointerABIAlignBits % 8 != 0) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000228 return "invalid pointer ABI alignment, "
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000229 "must be an 8-bit multiple";
Micah Villmowac34b5c2012-10-04 22:08:14 +0000230 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000231
232 // Pointer preferred alignment.
233 Split = Split.second.split(':');
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000234 unsigned PointerPrefAlignBits = getInt(Split.first);
235 if (PointerPrefAlignBits % 8 != 0) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000236 return "invalid pointer preferred alignment, "
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000237 "must be an 8-bit multiple";
Micah Villmowac34b5c2012-10-04 22:08:14 +0000238 }
Micah Villmow89021e42012-10-09 16:06:12 +0000239
240 if (PointerPrefAlignBits == 0)
241 PointerPrefAlignBits = PointerABIAlignBits;
Patrik Hägglund01860a62012-11-14 09:04:56 +0000242 setPointerAlignment(AddrSpace, PointerABIAlignBits/8,
243 PointerPrefAlignBits/8, PointerMemSizeBits/8);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000244 break;
245 }
246 case 'i':
247 case 'v':
248 case 'f':
249 case 'a':
250 case 's': {
251 AlignTypeEnum AlignType;
252 char field = Specifier[0];
253 switch (field) {
254 default:
255 case 'i': AlignType = INTEGER_ALIGN; break;
256 case 'v': AlignType = VECTOR_ALIGN; break;
257 case 'f': AlignType = FLOAT_ALIGN; break;
258 case 'a': AlignType = AGGREGATE_ALIGN; break;
259 case 's': AlignType = STACK_ALIGN; break;
260 }
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000261 unsigned Size = getInt(Specifier.substr(1));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000262
263 Split = Token.split(':');
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000264 unsigned ABIAlignBits = getInt(Split.first);
265 if (ABIAlignBits % 8 != 0) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000266 return std::string("invalid ") + field +"-abi-alignment field, "
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000267 "must be an 8-bit multiple";
Micah Villmowac34b5c2012-10-04 22:08:14 +0000268 }
269 unsigned ABIAlign = ABIAlignBits / 8;
270
271 Split = Split.second.split(':');
272
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000273 unsigned PrefAlignBits = getInt(Split.first);
274 if (PrefAlignBits % 8 != 0) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000275 return std::string("invalid ") + field +"-preferred-alignment field, "
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000276 "must be an 8-bit multiple";
Micah Villmowac34b5c2012-10-04 22:08:14 +0000277 }
278 unsigned PrefAlign = PrefAlignBits / 8;
279 if (PrefAlign == 0)
280 PrefAlign = ABIAlign;
Patrik Hägglund01860a62012-11-14 09:04:56 +0000281 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmowb4faa152012-10-04 23:01:22 +0000282
Micah Villmowac34b5c2012-10-04 22:08:14 +0000283 break;
284 }
285 case 'n': // Native integer types.
286 Specifier = Specifier.substr(1);
287 do {
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000288 unsigned Width = getInt(Specifier);
289 if (Width == 0) {
Micah Villmowb4faa152012-10-04 23:01:22 +0000290 return std::string("invalid native integer size \'") +
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000291 Specifier.str() + "\', must be a non-zero integer.";
Micah Villmowac34b5c2012-10-04 22:08:14 +0000292 }
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000293 LegalIntWidths.push_back(Width);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000294 Split = Token.split(':');
295 Specifier = Split.first;
296 Token = Split.second;
297 } while (!Specifier.empty() || !Token.empty());
298 break;
299 case 'S': { // Stack natural alignment.
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000300 unsigned StackNaturalAlignBits = getInt(Specifier.substr(1));
301 if (StackNaturalAlignBits % 8 != 0) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000302 return "invalid natural stack alignment (S-field), "
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000303 "must be an 8-bit multiple";
Micah Villmowac34b5c2012-10-04 22:08:14 +0000304 }
Patrik Hägglund01860a62012-11-14 09:04:56 +0000305 StackNaturalAlign = StackNaturalAlignBits / 8;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000306 break;
307 }
308 default:
309 break;
310 }
311 }
312
313 return "";
314}
315
316/// Default ctor.
317///
318/// @note This has to exist, because this is a pass, but it should never be
319/// used.
Micah Villmowb4faa152012-10-04 23:01:22 +0000320DataLayout::DataLayout() : ImmutablePass(ID) {
321 report_fatal_error("Bad DataLayout ctor used. "
322 "Tool did not specify a DataLayout to use?");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000323}
324
Micah Villmowb4faa152012-10-04 23:01:22 +0000325DataLayout::DataLayout(const Module *M)
Micah Villmowac34b5c2012-10-04 22:08:14 +0000326 : ImmutablePass(ID) {
Patrik Hägglund01860a62012-11-14 09:04:56 +0000327 init(M->getDataLayout());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000328}
329
330void
Micah Villmowb4faa152012-10-04 23:01:22 +0000331DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000332 unsigned pref_align, uint32_t bit_width) {
333 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
334 assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield");
335 assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield");
336 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmow6d05e692012-10-05 17:02:14 +0000337 if (Alignments[i].AlignType == (unsigned)align_type &&
Micah Villmowac34b5c2012-10-04 22:08:14 +0000338 Alignments[i].TypeBitWidth == bit_width) {
339 // Update the abi, preferred alignments.
340 Alignments[i].ABIAlign = abi_align;
341 Alignments[i].PrefAlign = pref_align;
342 return;
343 }
344 }
345
Micah Villmowb4faa152012-10-04 23:01:22 +0000346 Alignments.push_back(LayoutAlignElem::get(align_type, abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000347 pref_align, bit_width));
348}
349
Micah Villmow89021e42012-10-09 16:06:12 +0000350void
351DataLayout::setPointerAlignment(uint32_t addr_space, unsigned abi_align,
352 unsigned pref_align, uint32_t bit_width) {
353 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
354 DenseMap<unsigned,PointerAlignElem>::iterator val = Pointers.find(addr_space);
355 if (val == Pointers.end()) {
356 Pointers[addr_space] = PointerAlignElem::get(addr_space,
357 abi_align, pref_align, bit_width);
358 } else {
359 val->second.ABIAlign = abi_align;
360 val->second.PrefAlign = pref_align;
361 val->second.TypeBitWidth = bit_width;
362 }
363}
364
Micah Villmowac34b5c2012-10-04 22:08:14 +0000365/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000366/// preferred if ABIInfo = false) the layout wants for the specified datatype.
367unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000368 uint32_t BitWidth, bool ABIInfo,
369 Type *Ty) const {
370 // Check to see if we have an exact match and remember the best match we see.
371 int BestMatchIdx = -1;
372 int LargestInt = -1;
373 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmow6d05e692012-10-05 17:02:14 +0000374 if (Alignments[i].AlignType == (unsigned)AlignType &&
Micah Villmowac34b5c2012-10-04 22:08:14 +0000375 Alignments[i].TypeBitWidth == BitWidth)
376 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
377
378 // The best match so far depends on what we're looking for.
379 if (AlignType == INTEGER_ALIGN &&
380 Alignments[i].AlignType == INTEGER_ALIGN) {
381 // The "best match" for integers is the smallest size that is larger than
382 // the BitWidth requested.
383 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
384 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
385 BestMatchIdx = i;
386 // However, if there isn't one that's larger, then we must use the
387 // largest one we have (see below)
388 if (LargestInt == -1 ||
389 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
390 LargestInt = i;
391 }
392 }
393
394 // Okay, we didn't find an exact solution. Fall back here depending on what
395 // is being looked for.
396 if (BestMatchIdx == -1) {
397 // If we didn't find an integer alignment, fall back on most conservative.
398 if (AlignType == INTEGER_ALIGN) {
399 BestMatchIdx = LargestInt;
400 } else {
401 assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
402
403 // By default, use natural alignment for vector types. This is consistent
404 // with what clang and llvm-gcc do.
405 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
406 Align *= cast<VectorType>(Ty)->getNumElements();
407 // If the alignment is not a power of 2, round up to the next power of 2.
408 // This happens for non-power-of-2 length vectors.
409 if (Align & (Align-1))
410 Align = NextPowerOf2(Align);
411 return Align;
412 }
413 }
414
415 // Since we got a "best match" index, just return it.
416 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
417 : Alignments[BestMatchIdx].PrefAlign;
418}
419
420namespace {
421
422class StructLayoutMap {
423 typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
424 LayoutInfoTy LayoutInfo;
425
426public:
427 virtual ~StructLayoutMap() {
428 // Remove any layouts.
429 for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end();
430 I != E; ++I) {
431 StructLayout *Value = I->second;
432 Value->~StructLayout();
433 free(Value);
434 }
435 }
436
437 StructLayout *&operator[](StructType *STy) {
438 return LayoutInfo[STy];
439 }
440
441 // for debugging...
442 virtual void dump() const {}
443};
444
445} // end anonymous namespace
446
Micah Villmowb4faa152012-10-04 23:01:22 +0000447DataLayout::~DataLayout() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000448 delete static_cast<StructLayoutMap*>(LayoutMap);
449}
450
Micah Villmowb4faa152012-10-04 23:01:22 +0000451const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000452 if (!LayoutMap)
453 LayoutMap = new StructLayoutMap();
454
455 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
456 StructLayout *&SL = (*STM)[Ty];
457 if (SL) return SL;
458
459 // Otherwise, create the struct layout. Because it is variable length, we
460 // malloc it, then use placement new.
461 int NumElts = Ty->getNumElements();
462 StructLayout *L =
463 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
464
465 // Set SL before calling StructLayout's ctor. The ctor could cause other
466 // entries to be added to TheMap, invalidating our reference.
467 SL = L;
468
469 new (L) StructLayout(Ty, *this);
470
471 return L;
472}
473
Micah Villmowb4faa152012-10-04 23:01:22 +0000474std::string DataLayout::getStringRepresentation() const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000475 std::string Result;
476 raw_string_ostream OS(Result);
477
Micah Villmow89021e42012-10-09 16:06:12 +0000478 OS << (LittleEndian ? "e" : "E");
479 SmallVector<unsigned, 8> addrSpaces;
480 // Lets get all of the known address spaces and sort them
481 // into increasing order so that we can emit the string
482 // in a cleaner format.
483 for (DenseMap<unsigned, PointerAlignElem>::const_iterator
484 pib = Pointers.begin(), pie = Pointers.end();
485 pib != pie; ++pib) {
486 addrSpaces.push_back(pib->first);
487 }
488 std::sort(addrSpaces.begin(), addrSpaces.end());
489 for (SmallVector<unsigned, 8>::iterator asb = addrSpaces.begin(),
490 ase = addrSpaces.end(); asb != ase; ++asb) {
491 const PointerAlignElem &PI = Pointers.find(*asb)->second;
492 OS << "-p";
493 if (PI.AddressSpace) {
494 OS << PI.AddressSpace;
495 }
496 OS << ":" << PI.TypeBitWidth*8 << ':' << PI.ABIAlign*8
497 << ':' << PI.PrefAlign*8;
498 }
499 OS << "-S" << StackNaturalAlign*8;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000500
501 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmowb4faa152012-10-04 23:01:22 +0000502 const LayoutAlignElem &AI = Alignments[i];
Micah Villmowac34b5c2012-10-04 22:08:14 +0000503 OS << '-' << (char)AI.AlignType << AI.TypeBitWidth << ':'
504 << AI.ABIAlign*8 << ':' << AI.PrefAlign*8;
505 }
506
507 if (!LegalIntWidths.empty()) {
508 OS << "-n" << (unsigned)LegalIntWidths[0];
509
510 for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
511 OS << ':' << (unsigned)LegalIntWidths[i];
512 }
513 return OS.str();
514}
515
516
Micah Villmowb4faa152012-10-04 23:01:22 +0000517uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000518 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
519 switch (Ty->getTypeID()) {
520 case Type::LabelTyID:
Micah Villmow89021e42012-10-09 16:06:12 +0000521 return getPointerSizeInBits(0);
522 case Type::PointerTyID: {
523 unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace();
524 return getPointerSizeInBits(AS);
525 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000526 case Type::ArrayTyID: {
527 ArrayType *ATy = cast<ArrayType>(Ty);
528 return getTypeAllocSizeInBits(ATy->getElementType())*ATy->getNumElements();
529 }
530 case Type::StructTyID:
531 // Get the layout annotation... which is lazily created on demand.
532 return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
533 case Type::IntegerTyID:
534 return cast<IntegerType>(Ty)->getBitWidth();
Micah Villmowac34b5c2012-10-04 22:08:14 +0000535 case Type::HalfTyID:
536 return 16;
537 case Type::FloatTyID:
538 return 32;
539 case Type::DoubleTyID:
540 case Type::X86_MMXTyID:
541 return 64;
542 case Type::PPC_FP128TyID:
543 case Type::FP128TyID:
544 return 128;
545 // In memory objects this is always aligned to a higher boundary, but
546 // only 80 bits contain information.
547 case Type::X86_FP80TyID:
548 return 80;
Hal Finkel8884dc32012-10-21 20:38:03 +0000549 case Type::VectorTyID: {
550 VectorType *VTy = cast<VectorType>(Ty);
551 return VTy->getNumElements()*getTypeSizeInBits(VTy->getElementType());
552 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000553 default:
Micah Villmowb4faa152012-10-04 23:01:22 +0000554 llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000555 }
556}
557
558/*!
559 \param abi_or_pref Flag that determines which alignment is returned. true
560 returns the ABI alignment, false returns the preferred alignment.
561 \param Ty The underlying type for which alignment is determined.
562
563 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
564 == false) for the requested type \a Ty.
565 */
Micah Villmowb4faa152012-10-04 23:01:22 +0000566unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000567 int AlignType = -1;
568
569 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
570 switch (Ty->getTypeID()) {
571 // Early escape for the non-numeric types.
572 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000573 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000574 ? getPointerABIAlignment(0)
575 : getPointerPrefAlignment(0));
576 case Type::PointerTyID: {
577 unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace();
578 return (abi_or_pref
579 ? getPointerABIAlignment(AS)
580 : getPointerPrefAlignment(AS));
581 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000582 case Type::ArrayTyID:
583 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
584
585 case Type::StructTyID: {
586 // Packed structure types always have an ABI alignment of one.
587 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
588 return 1;
589
590 // Get the layout annotation... which is lazily created on demand.
591 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
592 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
593 return std::max(Align, Layout->getAlignment());
594 }
595 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000596 AlignType = INTEGER_ALIGN;
597 break;
598 case Type::HalfTyID:
599 case Type::FloatTyID:
600 case Type::DoubleTyID:
601 // PPC_FP128TyID and FP128TyID have different data contents, but the
602 // same size and alignment, so they look the same here.
603 case Type::PPC_FP128TyID:
604 case Type::FP128TyID:
605 case Type::X86_FP80TyID:
606 AlignType = FLOAT_ALIGN;
607 break;
608 case Type::X86_MMXTyID:
609 case Type::VectorTyID:
610 AlignType = VECTOR_ALIGN;
611 break;
612 default:
613 llvm_unreachable("Bad type for getAlignment!!!");
614 }
615
616 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
617 abi_or_pref, Ty);
618}
619
Micah Villmowb4faa152012-10-04 23:01:22 +0000620unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000621 return getAlignment(Ty, true);
622}
623
624/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
625/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000626unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000627 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0);
628}
629
630
Micah Villmowb4faa152012-10-04 23:01:22 +0000631unsigned DataLayout::getCallFrameTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000632 for (unsigned i = 0, e = Alignments.size(); i != e; ++i)
633 if (Alignments[i].AlignType == STACK_ALIGN)
634 return Alignments[i].ABIAlign;
635
636 return getABITypeAlignment(Ty);
637}
638
Micah Villmowb4faa152012-10-04 23:01:22 +0000639unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000640 return getAlignment(Ty, false);
641}
642
Micah Villmowb4faa152012-10-04 23:01:22 +0000643unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000644 unsigned Align = getPrefTypeAlignment(Ty);
645 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
646 return Log2_32(Align);
647}
648
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000649/// getIntPtrType - Return an integer type with size at least as big as that
650/// of a pointer in the given address space.
Micah Villmow89021e42012-10-09 16:06:12 +0000651IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
652 unsigned AddressSpace) const {
653 return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000654}
655
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000656/// getIntPtrType - Return an integer (vector of integer) type with size at
657/// least as big as that of a pointer of the given pointer (vector of pointer)
658/// type.
659Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000660 assert(Ty->isPtrOrPtrVectorTy() &&
661 "Expected a pointer or pointer vector type.");
Chandler Carruth7ec50852012-11-01 08:07:29 +0000662 unsigned NumBits = getTypeSizeInBits(Ty->getScalarType());
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000663 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
664 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
665 return VectorType::get(IntTy, VecTy->getNumElements());
666 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000667}
668
Micah Villmowb4faa152012-10-04 23:01:22 +0000669uint64_t DataLayout::getIndexedOffset(Type *ptrTy,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000670 ArrayRef<Value *> Indices) const {
671 Type *Ty = ptrTy;
672 assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
673 uint64_t Result = 0;
674
675 generic_gep_type_iterator<Value* const*>
676 TI = gep_type_begin(ptrTy, Indices);
677 for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
678 ++CurIDX, ++TI) {
679 if (StructType *STy = dyn_cast<StructType>(*TI)) {
680 assert(Indices[CurIDX]->getType() ==
681 Type::getInt32Ty(ptrTy->getContext()) &&
682 "Illegal struct idx");
683 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
684
685 // Get structure layout information...
686 const StructLayout *Layout = getStructLayout(STy);
687
688 // Add in the offset, as calculated by the structure layout info...
689 Result += Layout->getElementOffset(FieldNo);
690
691 // Update Ty to refer to current element
692 Ty = STy->getElementType(FieldNo);
693 } else {
694 // Update Ty to refer to current element
695 Ty = cast<SequentialType>(Ty)->getElementType();
696
697 // Get the array index and the size of each array element.
698 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
699 Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
700 }
701 }
702
703 return Result;
704}
705
706/// getPreferredAlignment - Return the preferred alignment of the specified
707/// global. This includes an explicitly requested alignment (if the global
708/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000709unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000710 Type *ElemType = GV->getType()->getElementType();
711 unsigned Alignment = getPrefTypeAlignment(ElemType);
712 unsigned GVAlignment = GV->getAlignment();
713 if (GVAlignment >= Alignment) {
714 Alignment = GVAlignment;
715 } else if (GVAlignment != 0) {
716 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
717 }
718
719 if (GV->hasInitializer() && GVAlignment == 0) {
720 if (Alignment < 16) {
721 // If the global is not external, see if it is large. If so, give it a
722 // larger alignment.
723 if (getTypeSizeInBits(ElemType) > 128)
724 Alignment = 16; // 16-byte alignment.
725 }
726 }
727 return Alignment;
728}
729
730/// getPreferredAlignmentLog - Return the preferred alignment of the
731/// specified global, returned in log form. This includes an explicitly
732/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000733unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000734 return Log2_32(getPreferredAlignment(GV));
735}