blob: ec04a17735ac51d2af36f888d4f257da0fb1ac0a [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DerivedTypes.h"
23#include "llvm/IR/Module.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000024#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/GetElementPtrTypeIterator.h"
26#include "llvm/Support/ManagedStatic.h"
27#include "llvm/Support/MathExtras.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000028#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Support/raw_ostream.h"
Micah Villmowac34b5c2012-10-04 22:08:14 +000030#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
Eli Bendersky41913c72013-04-16 15:41:18 +000044StructLayout::StructLayout(StructType *ST, const DataLayout &DL) {
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);
Eli Bendersky41913c72013-04-16 15:41:18 +000053 unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty);
Micah Villmowac34b5c2012-10-04 22:08:14 +000054
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;
Eli Bendersky41913c72013-04-16 15:41:18 +000063 StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item
Micah Villmowac34b5c2012-10-04 22:08:14 +000064 }
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
Benjamin Kramer058f5b32013-11-19 20:28:04 +0000121DataLayout::InvalidAlignmentElem = { INVALID_ALIGN, 0, 0, 0 };
Micah Villmow89021e42012-10-09 16:06:12 +0000122
123//===----------------------------------------------------------------------===//
124// PointerAlignElem, PointerAlign support
125//===----------------------------------------------------------------------===//
126
127PointerAlignElem
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000128PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign,
129 unsigned PrefAlign, uint32_t TypeByteWidth) {
130 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
Micah Villmow89021e42012-10-09 16:06:12 +0000131 PointerAlignElem retval;
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000132 retval.AddressSpace = AddressSpace;
133 retval.ABIAlign = ABIAlign;
134 retval.PrefAlign = PrefAlign;
135 retval.TypeByteWidth = TypeByteWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000136 return retval;
137}
138
139bool
140PointerAlignElem::operator==(const PointerAlignElem &rhs) const {
141 return (ABIAlign == rhs.ABIAlign
142 && AddressSpace == rhs.AddressSpace
143 && PrefAlign == rhs.PrefAlign
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000144 && TypeByteWidth == rhs.TypeByteWidth);
Micah Villmow89021e42012-10-09 16:06:12 +0000145}
146
147const PointerAlignElem
Benjamin Kramer058f5b32013-11-19 20:28:04 +0000148DataLayout::InvalidPointerElem = { 0U, 0U, 0U, ~0U };
Micah Villmowac34b5c2012-10-04 22:08:14 +0000149
150//===----------------------------------------------------------------------===//
Micah Villmowb4faa152012-10-04 23:01:22 +0000151// DataLayout Class Implementation
Micah Villmowac34b5c2012-10-04 22:08:14 +0000152//===----------------------------------------------------------------------===//
153
Patrik Hägglund01860a62012-11-14 09:04:56 +0000154void DataLayout::init(StringRef Desc) {
Micah Villmowb4faa152012-10-04 23:01:22 +0000155 initializeDataLayoutPass(*PassRegistry::getPassRegistry());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000156
157 LayoutMap = 0;
158 LittleEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000159 StackNaturalAlign = 0;
160
161 // Default alignments
162 setAlignment(INTEGER_ALIGN, 1, 1, 1); // i1
163 setAlignment(INTEGER_ALIGN, 1, 1, 8); // i8
164 setAlignment(INTEGER_ALIGN, 2, 2, 16); // i16
165 setAlignment(INTEGER_ALIGN, 4, 4, 32); // i32
166 setAlignment(INTEGER_ALIGN, 4, 8, 64); // i64
167 setAlignment(FLOAT_ALIGN, 2, 2, 16); // half
168 setAlignment(FLOAT_ALIGN, 4, 4, 32); // float
169 setAlignment(FLOAT_ALIGN, 8, 8, 64); // double
170 setAlignment(FLOAT_ALIGN, 16, 16, 128); // ppcf128, quad, ...
171 setAlignment(VECTOR_ALIGN, 8, 8, 64); // v2i32, v1i64, ...
172 setAlignment(VECTOR_ALIGN, 16, 16, 128); // v16i8, v8i16, v4i32, ...
173 setAlignment(AGGREGATE_ALIGN, 0, 8, 0); // struct
Micah Villmow89021e42012-10-09 16:06:12 +0000174 setPointerAlignment(0, 8, 8, 8);
Patrik Hägglund01860a62012-11-14 09:04:56 +0000175
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000176 parseSpecifier(Desc);
177}
178
179/// Checked version of split, to ensure mandatory subparts.
180static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) {
181 assert(!Str.empty() && "parse error, string can't be empty here");
182 std::pair<StringRef, StringRef> Split = Str.split(Separator);
183 assert((!Split.second.empty() || Split.first == Str) &&
184 "a trailing separator is not allowed");
185 return Split;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000186}
187
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000188/// Get an unsinged integer, including error checks.
189static unsigned getInt(StringRef R) {
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000190 unsigned Result;
Patrik Hägglund504f4782012-11-28 14:32:52 +0000191 bool error = R.getAsInteger(10, Result); (void)error;
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000192 assert(!error && "not a number, or does not fit in an unsigned int");
193 return Result;
194}
195
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000196/// Convert bits into bytes. Assert if not a byte width multiple.
197static unsigned inBytes(unsigned Bits) {
198 assert(Bits % 8 == 0 && "number of bits must be a byte width multiple");
199 return Bits / 8;
200}
201
202void DataLayout::parseSpecifier(StringRef Desc) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000203 while (!Desc.empty()) {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000204 // Split at '-'.
205 std::pair<StringRef, StringRef> Split = split(Desc, '-');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000206 Desc = Split.second;
207
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000208 // Split at ':'.
209 Split = split(Split.first, ':');
Micah Villmowac34b5c2012-10-04 22:08:14 +0000210
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000211 // Aliases used below.
212 StringRef &Tok = Split.first; // Current token.
213 StringRef &Rest = Split.second; // The rest of the string.
Micah Villmowac34b5c2012-10-04 22:08:14 +0000214
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000215 char Specifier = Tok.front();
216 Tok = Tok.substr(1);
217
218 switch (Specifier) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000219 case 'E':
Patrik Hägglund01860a62012-11-14 09:04:56 +0000220 LittleEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000221 break;
222 case 'e':
Patrik Hägglund01860a62012-11-14 09:04:56 +0000223 LittleEndian = true;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000224 break;
225 case 'p': {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000226 // Address space.
227 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
228 assert(AddrSpace < 1 << 24 &&
229 "Invalid address space, must be a 24bit integer");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000230
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000231 // Size.
232 Split = split(Rest, ':');
233 unsigned PointerMemSize = inBytes(getInt(Tok));
234
235 // ABI alignment.
236 Split = split(Rest, ':');
237 unsigned PointerABIAlign = inBytes(getInt(Tok));
238
239 // Preferred alignment.
240 unsigned PointerPrefAlign = PointerABIAlign;
241 if (!Rest.empty()) {
242 Split = split(Rest, ':');
243 PointerPrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000244 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000245
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000246 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
247 PointerMemSize);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000248 break;
249 }
250 case 'i':
251 case 'v':
252 case 'f':
253 case 'a':
254 case 's': {
255 AlignTypeEnum AlignType;
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000256 switch (Specifier) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000257 default:
258 case 'i': AlignType = INTEGER_ALIGN; break;
259 case 'v': AlignType = VECTOR_ALIGN; break;
260 case 'f': AlignType = FLOAT_ALIGN; break;
261 case 'a': AlignType = AGGREGATE_ALIGN; break;
262 case 's': AlignType = STACK_ALIGN; break;
263 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000264
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000265 // Bit size.
266 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
267
268 // ABI alignment.
269 Split = split(Rest, ':');
270 unsigned ABIAlign = inBytes(getInt(Tok));
271
272 // Preferred alignment.
273 unsigned PrefAlign = ABIAlign;
274 if (!Rest.empty()) {
275 Split = split(Rest, ':');
276 PrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000277 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000278
Patrik Hägglund01860a62012-11-14 09:04:56 +0000279 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmowb4faa152012-10-04 23:01:22 +0000280
Micah Villmowac34b5c2012-10-04 22:08:14 +0000281 break;
282 }
283 case 'n': // Native integer types.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000284 for (;;) {
285 unsigned Width = getInt(Tok);
286 assert(Width != 0 && "width must be non-zero");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000287 LegalIntWidths.push_back(Width);
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000288 if (Rest.empty())
289 break;
290 Split = split(Rest, ':');
291 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000292 break;
293 case 'S': { // Stack natural alignment.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000294 StackNaturalAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000295 break;
296 }
297 default:
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000298 llvm_unreachable("Unknown specifier in datalayout string");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000299 break;
300 }
301 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000302}
303
304/// Default ctor.
305///
306/// @note This has to exist, because this is a pass, but it should never be
307/// used.
Micah Villmowb4faa152012-10-04 23:01:22 +0000308DataLayout::DataLayout() : ImmutablePass(ID) {
309 report_fatal_error("Bad DataLayout ctor used. "
Eli Benderskyfaf5e3e2013-01-30 19:24:23 +0000310 "Tool did not specify a DataLayout to use?");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000311}
312
Micah Villmowb4faa152012-10-04 23:01:22 +0000313DataLayout::DataLayout(const Module *M)
Micah Villmowac34b5c2012-10-04 22:08:14 +0000314 : ImmutablePass(ID) {
Patrik Hägglund01860a62012-11-14 09:04:56 +0000315 init(M->getDataLayout());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000316}
317
318void
Micah Villmowb4faa152012-10-04 23:01:22 +0000319DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000320 unsigned pref_align, uint32_t bit_width) {
321 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
322 assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield");
323 assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield");
324 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmow6d05e692012-10-05 17:02:14 +0000325 if (Alignments[i].AlignType == (unsigned)align_type &&
Micah Villmowac34b5c2012-10-04 22:08:14 +0000326 Alignments[i].TypeBitWidth == bit_width) {
327 // Update the abi, preferred alignments.
328 Alignments[i].ABIAlign = abi_align;
329 Alignments[i].PrefAlign = pref_align;
330 return;
331 }
332 }
333
Micah Villmowb4faa152012-10-04 23:01:22 +0000334 Alignments.push_back(LayoutAlignElem::get(align_type, abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000335 pref_align, bit_width));
336}
337
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000338void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
339 unsigned PrefAlign,
340 uint32_t TypeByteWidth) {
341 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
342 DenseMap<unsigned,PointerAlignElem>::iterator val = Pointers.find(AddrSpace);
Micah Villmow89021e42012-10-09 16:06:12 +0000343 if (val == Pointers.end()) {
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000344 Pointers[AddrSpace] =
345 PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign, TypeByteWidth);
Micah Villmow89021e42012-10-09 16:06:12 +0000346 } else {
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000347 val->second.ABIAlign = ABIAlign;
348 val->second.PrefAlign = PrefAlign;
349 val->second.TypeByteWidth = TypeByteWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000350 }
351}
352
Micah Villmowac34b5c2012-10-04 22:08:14 +0000353/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000354/// preferred if ABIInfo = false) the layout wants for the specified datatype.
355unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000356 uint32_t BitWidth, bool ABIInfo,
357 Type *Ty) const {
358 // Check to see if we have an exact match and remember the best match we see.
359 int BestMatchIdx = -1;
360 int LargestInt = -1;
361 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmow6d05e692012-10-05 17:02:14 +0000362 if (Alignments[i].AlignType == (unsigned)AlignType &&
Micah Villmowac34b5c2012-10-04 22:08:14 +0000363 Alignments[i].TypeBitWidth == BitWidth)
364 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
365
366 // The best match so far depends on what we're looking for.
367 if (AlignType == INTEGER_ALIGN &&
368 Alignments[i].AlignType == INTEGER_ALIGN) {
369 // The "best match" for integers is the smallest size that is larger than
370 // the BitWidth requested.
371 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
Eli Benderskyfaf5e3e2013-01-30 19:24:23 +0000372 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
Micah Villmowac34b5c2012-10-04 22:08:14 +0000373 BestMatchIdx = i;
374 // However, if there isn't one that's larger, then we must use the
375 // largest one we have (see below)
376 if (LargestInt == -1 ||
377 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
378 LargestInt = i;
379 }
380 }
381
382 // Okay, we didn't find an exact solution. Fall back here depending on what
383 // is being looked for.
384 if (BestMatchIdx == -1) {
385 // If we didn't find an integer alignment, fall back on most conservative.
386 if (AlignType == INTEGER_ALIGN) {
387 BestMatchIdx = LargestInt;
388 } else {
389 assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
390
391 // By default, use natural alignment for vector types. This is consistent
392 // with what clang and llvm-gcc do.
393 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
394 Align *= cast<VectorType>(Ty)->getNumElements();
395 // If the alignment is not a power of 2, round up to the next power of 2.
396 // This happens for non-power-of-2 length vectors.
397 if (Align & (Align-1))
398 Align = NextPowerOf2(Align);
399 return Align;
400 }
401 }
402
403 // Since we got a "best match" index, just return it.
404 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
405 : Alignments[BestMatchIdx].PrefAlign;
406}
407
408namespace {
409
410class StructLayoutMap {
411 typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
412 LayoutInfoTy LayoutInfo;
413
414public:
415 virtual ~StructLayoutMap() {
416 // Remove any layouts.
417 for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end();
418 I != E; ++I) {
419 StructLayout *Value = I->second;
420 Value->~StructLayout();
421 free(Value);
422 }
423 }
424
425 StructLayout *&operator[](StructType *STy) {
426 return LayoutInfo[STy];
427 }
428
429 // for debugging...
430 virtual void dump() const {}
431};
432
433} // end anonymous namespace
434
Micah Villmowb4faa152012-10-04 23:01:22 +0000435DataLayout::~DataLayout() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000436 delete static_cast<StructLayoutMap*>(LayoutMap);
437}
438
Pete Cooper6308a822013-03-12 17:37:31 +0000439bool DataLayout::doFinalization(Module &M) {
440 delete static_cast<StructLayoutMap*>(LayoutMap);
441 LayoutMap = 0;
442 return false;
443}
444
Micah Villmowb4faa152012-10-04 23:01:22 +0000445const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000446 if (!LayoutMap)
447 LayoutMap = new StructLayoutMap();
448
449 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
450 StructLayout *&SL = (*STM)[Ty];
451 if (SL) return SL;
452
453 // Otherwise, create the struct layout. Because it is variable length, we
454 // malloc it, then use placement new.
455 int NumElts = Ty->getNumElements();
456 StructLayout *L =
457 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
458
459 // Set SL before calling StructLayout's ctor. The ctor could cause other
460 // entries to be added to TheMap, invalidating our reference.
461 SL = L;
462
463 new (L) StructLayout(Ty, *this);
464
465 return L;
466}
467
Micah Villmowb4faa152012-10-04 23:01:22 +0000468std::string DataLayout::getStringRepresentation() const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000469 std::string Result;
470 raw_string_ostream OS(Result);
471
Micah Villmow89021e42012-10-09 16:06:12 +0000472 OS << (LittleEndian ? "e" : "E");
473 SmallVector<unsigned, 8> addrSpaces;
474 // Lets get all of the known address spaces and sort them
475 // into increasing order so that we can emit the string
476 // in a cleaner format.
477 for (DenseMap<unsigned, PointerAlignElem>::const_iterator
478 pib = Pointers.begin(), pie = Pointers.end();
479 pib != pie; ++pib) {
480 addrSpaces.push_back(pib->first);
481 }
482 std::sort(addrSpaces.begin(), addrSpaces.end());
Craig Topperaf0dea12013-07-04 01:31:24 +0000483 for (SmallVectorImpl<unsigned>::iterator asb = addrSpaces.begin(),
Micah Villmow89021e42012-10-09 16:06:12 +0000484 ase = addrSpaces.end(); asb != ase; ++asb) {
485 const PointerAlignElem &PI = Pointers.find(*asb)->second;
486 OS << "-p";
487 if (PI.AddressSpace) {
488 OS << PI.AddressSpace;
489 }
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000490 OS << ":" << PI.TypeByteWidth*8 << ':' << PI.ABIAlign*8
Micah Villmow89021e42012-10-09 16:06:12 +0000491 << ':' << PI.PrefAlign*8;
492 }
493 OS << "-S" << StackNaturalAlign*8;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000494
495 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmowb4faa152012-10-04 23:01:22 +0000496 const LayoutAlignElem &AI = Alignments[i];
Micah Villmowac34b5c2012-10-04 22:08:14 +0000497 OS << '-' << (char)AI.AlignType << AI.TypeBitWidth << ':'
498 << AI.ABIAlign*8 << ':' << AI.PrefAlign*8;
499 }
500
501 if (!LegalIntWidths.empty()) {
502 OS << "-n" << (unsigned)LegalIntWidths[0];
503
504 for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
505 OS << ':' << (unsigned)LegalIntWidths[i];
506 }
507 return OS.str();
508}
509
Matt Arsenault6f4be902013-07-26 17:37:20 +0000510unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
511 assert(Ty->isPtrOrPtrVectorTy() &&
512 "This should only be called with a pointer or pointer vector type");
513
514 if (Ty->isPointerTy())
515 return getTypeSizeInBits(Ty);
516
Matt Arsenault517cf482013-07-27 19:22:28 +0000517 return getTypeSizeInBits(Ty->getScalarType());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000518}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000519
Micah Villmowac34b5c2012-10-04 22:08:14 +0000520/*!
521 \param abi_or_pref Flag that determines which alignment is returned. true
522 returns the ABI alignment, false returns the preferred alignment.
523 \param Ty The underlying type for which alignment is determined.
524
525 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
526 == false) for the requested type \a Ty.
527 */
Micah Villmowb4faa152012-10-04 23:01:22 +0000528unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000529 int AlignType = -1;
530
531 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
532 switch (Ty->getTypeID()) {
533 // Early escape for the non-numeric types.
534 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000535 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000536 ? getPointerABIAlignment(0)
537 : getPointerPrefAlignment(0));
538 case Type::PointerTyID: {
539 unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace();
540 return (abi_or_pref
541 ? getPointerABIAlignment(AS)
542 : getPointerPrefAlignment(AS));
543 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000544 case Type::ArrayTyID:
545 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
546
547 case Type::StructTyID: {
548 // Packed structure types always have an ABI alignment of one.
549 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
550 return 1;
551
552 // Get the layout annotation... which is lazily created on demand.
553 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
554 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
555 return std::max(Align, Layout->getAlignment());
556 }
557 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000558 AlignType = INTEGER_ALIGN;
559 break;
560 case Type::HalfTyID:
561 case Type::FloatTyID:
562 case Type::DoubleTyID:
563 // PPC_FP128TyID and FP128TyID have different data contents, but the
564 // same size and alignment, so they look the same here.
565 case Type::PPC_FP128TyID:
566 case Type::FP128TyID:
567 case Type::X86_FP80TyID:
568 AlignType = FLOAT_ALIGN;
569 break;
570 case Type::X86_MMXTyID:
571 case Type::VectorTyID:
572 AlignType = VECTOR_ALIGN;
573 break;
574 default:
575 llvm_unreachable("Bad type for getAlignment!!!");
576 }
577
578 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
579 abi_or_pref, Ty);
580}
581
Micah Villmowb4faa152012-10-04 23:01:22 +0000582unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000583 return getAlignment(Ty, true);
584}
585
586/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
587/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000588unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000589 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0);
590}
591
Micah Villmowb4faa152012-10-04 23:01:22 +0000592unsigned DataLayout::getCallFrameTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000593 for (unsigned i = 0, e = Alignments.size(); i != e; ++i)
594 if (Alignments[i].AlignType == STACK_ALIGN)
595 return Alignments[i].ABIAlign;
596
597 return getABITypeAlignment(Ty);
598}
599
Micah Villmowb4faa152012-10-04 23:01:22 +0000600unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000601 return getAlignment(Ty, false);
602}
603
Micah Villmowb4faa152012-10-04 23:01:22 +0000604unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000605 unsigned Align = getPrefTypeAlignment(Ty);
606 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
607 return Log2_32(Align);
608}
609
Micah Villmow89021e42012-10-09 16:06:12 +0000610IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
611 unsigned AddressSpace) const {
612 return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000613}
614
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000615Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000616 assert(Ty->isPtrOrPtrVectorTy() &&
617 "Expected a pointer or pointer vector type.");
Chandler Carruth7ec50852012-11-01 08:07:29 +0000618 unsigned NumBits = getTypeSizeInBits(Ty->getScalarType());
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000619 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
620 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
621 return VectorType::get(IntTy, VecTy->getNumElements());
622 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000623}
624
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000625Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
626 for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i)
627 if (Width <= LegalIntWidths[i])
628 return Type::getIntNTy(C, LegalIntWidths[i]);
629 return 0;
630}
631
Matt Arsenault899f7d22013-09-16 22:43:16 +0000632unsigned DataLayout::getLargestLegalIntTypeSize() const {
633 unsigned MaxWidth = 0;
634 for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i)
635 MaxWidth = std::max<unsigned>(MaxWidth, LegalIntWidths[i]);
636 return MaxWidth;
637}
638
Micah Villmowb4faa152012-10-04 23:01:22 +0000639uint64_t DataLayout::getIndexedOffset(Type *ptrTy,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000640 ArrayRef<Value *> Indices) const {
641 Type *Ty = ptrTy;
642 assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
643 uint64_t Result = 0;
644
645 generic_gep_type_iterator<Value* const*>
646 TI = gep_type_begin(ptrTy, Indices);
647 for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
648 ++CurIDX, ++TI) {
649 if (StructType *STy = dyn_cast<StructType>(*TI)) {
650 assert(Indices[CurIDX]->getType() ==
651 Type::getInt32Ty(ptrTy->getContext()) &&
652 "Illegal struct idx");
653 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
654
655 // Get structure layout information...
656 const StructLayout *Layout = getStructLayout(STy);
657
658 // Add in the offset, as calculated by the structure layout info...
659 Result += Layout->getElementOffset(FieldNo);
660
661 // Update Ty to refer to current element
662 Ty = STy->getElementType(FieldNo);
663 } else {
664 // Update Ty to refer to current element
665 Ty = cast<SequentialType>(Ty)->getElementType();
666
667 // Get the array index and the size of each array element.
668 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
669 Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
670 }
671 }
672
673 return Result;
674}
675
676/// getPreferredAlignment - Return the preferred alignment of the specified
677/// global. This includes an explicitly requested alignment (if the global
678/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000679unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000680 Type *ElemType = GV->getType()->getElementType();
681 unsigned Alignment = getPrefTypeAlignment(ElemType);
682 unsigned GVAlignment = GV->getAlignment();
683 if (GVAlignment >= Alignment) {
684 Alignment = GVAlignment;
685 } else if (GVAlignment != 0) {
686 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
687 }
688
689 if (GV->hasInitializer() && GVAlignment == 0) {
690 if (Alignment < 16) {
691 // If the global is not external, see if it is large. If so, give it a
692 // larger alignment.
693 if (getTypeSizeInBits(ElemType) > 128)
694 Alignment = 16; // 16-byte alignment.
695 }
696 }
697 return Alignment;
698}
699
700/// getPreferredAlignmentLog - Return the preferred alignment of the
701/// specified global, returned in log form. This includes an explicitly
702/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000703unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000704 return Log2_32(getPreferredAlignment(GV));
705}