blob: 0b52f1e95f30b198ca057afd4a9f5d1e1e4fa452 [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) {
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000228 case 's':
229 // Ignored for backward compatibility.
230 // FIXME: remove this on LLVM 4.0.
231 break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000232 case 'E':
Patrik Hägglund01860a62012-11-14 09:04:56 +0000233 LittleEndian = false;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000234 break;
235 case 'e':
Patrik Hägglund01860a62012-11-14 09:04:56 +0000236 LittleEndian = true;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000237 break;
238 case 'p': {
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000239 // Address space.
240 unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok);
241 assert(AddrSpace < 1 << 24 &&
242 "Invalid address space, must be a 24bit integer");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000243
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000244 // Size.
245 Split = split(Rest, ':');
246 unsigned PointerMemSize = inBytes(getInt(Tok));
247
248 // ABI alignment.
249 Split = split(Rest, ':');
250 unsigned PointerABIAlign = inBytes(getInt(Tok));
251
252 // Preferred alignment.
253 unsigned PointerPrefAlign = PointerABIAlign;
254 if (!Rest.empty()) {
255 Split = split(Rest, ':');
256 PointerPrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000257 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000258
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000259 setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign,
260 PointerMemSize);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000261 break;
262 }
263 case 'i':
264 case 'v':
265 case 'f':
Rafael Espindola6994fdf2014-01-01 22:29:43 +0000266 case 'a': {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000267 AlignTypeEnum AlignType;
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000268 switch (Specifier) {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000269 default:
270 case 'i': AlignType = INTEGER_ALIGN; break;
271 case 'v': AlignType = VECTOR_ALIGN; break;
272 case 'f': AlignType = FLOAT_ALIGN; break;
273 case 'a': AlignType = AGGREGATE_ALIGN; break;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000274 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000275
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000276 // Bit size.
277 unsigned Size = Tok.empty() ? 0 : getInt(Tok);
278
279 // ABI alignment.
280 Split = split(Rest, ':');
281 unsigned ABIAlign = inBytes(getInt(Tok));
282
283 // Preferred alignment.
284 unsigned PrefAlign = ABIAlign;
285 if (!Rest.empty()) {
286 Split = split(Rest, ':');
287 PrefAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000288 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000289
Patrik Hägglund01860a62012-11-14 09:04:56 +0000290 setAlignment(AlignType, ABIAlign, PrefAlign, Size);
Micah Villmowb4faa152012-10-04 23:01:22 +0000291
Micah Villmowac34b5c2012-10-04 22:08:14 +0000292 break;
293 }
294 case 'n': // Native integer types.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000295 for (;;) {
296 unsigned Width = getInt(Tok);
297 assert(Width != 0 && "width must be non-zero");
Patrik Hägglund3eb16c52012-11-28 12:13:12 +0000298 LegalIntWidths.push_back(Width);
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000299 if (Rest.empty())
300 break;
301 Split = split(Rest, ':');
302 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000303 break;
304 case 'S': { // Stack natural alignment.
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000305 StackNaturalAlign = inBytes(getInt(Tok));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000306 break;
307 }
308 default:
Patrik Hagglund086ee1e2012-11-30 10:06:59 +0000309 llvm_unreachable("Unknown specifier in datalayout string");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000310 break;
311 }
312 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000313}
314
315/// Default ctor.
316///
317/// @note This has to exist, because this is a pass, but it should never be
318/// used.
Micah Villmowb4faa152012-10-04 23:01:22 +0000319DataLayout::DataLayout() : ImmutablePass(ID) {
320 report_fatal_error("Bad DataLayout ctor used. "
Eli Benderskyfaf5e3e2013-01-30 19:24:23 +0000321 "Tool did not specify a DataLayout to use?");
Micah Villmowac34b5c2012-10-04 22:08:14 +0000322}
323
Micah Villmowb4faa152012-10-04 23:01:22 +0000324DataLayout::DataLayout(const Module *M)
Micah Villmowac34b5c2012-10-04 22:08:14 +0000325 : ImmutablePass(ID) {
Patrik Hägglund01860a62012-11-14 09:04:56 +0000326 init(M->getDataLayout());
Micah Villmowac34b5c2012-10-04 22:08:14 +0000327}
328
329void
Micah Villmowb4faa152012-10-04 23:01:22 +0000330DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000331 unsigned pref_align, uint32_t bit_width) {
332 assert(abi_align <= pref_align && "Preferred alignment worse than ABI!");
333 assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield");
334 assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield");
335 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmow6d05e692012-10-05 17:02:14 +0000336 if (Alignments[i].AlignType == (unsigned)align_type &&
Micah Villmowac34b5c2012-10-04 22:08:14 +0000337 Alignments[i].TypeBitWidth == bit_width) {
338 // Update the abi, preferred alignments.
339 Alignments[i].ABIAlign = abi_align;
340 Alignments[i].PrefAlign = pref_align;
341 return;
342 }
343 }
344
Micah Villmowb4faa152012-10-04 23:01:22 +0000345 Alignments.push_back(LayoutAlignElem::get(align_type, abi_align,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000346 pref_align, bit_width));
347}
348
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000349void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
350 unsigned PrefAlign,
351 uint32_t TypeByteWidth) {
352 assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!");
353 DenseMap<unsigned,PointerAlignElem>::iterator val = Pointers.find(AddrSpace);
Micah Villmow89021e42012-10-09 16:06:12 +0000354 if (val == Pointers.end()) {
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000355 Pointers[AddrSpace] =
356 PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign, TypeByteWidth);
Micah Villmow89021e42012-10-09 16:06:12 +0000357 } else {
Rafael Espindolaf39136c2013-12-13 23:15:20 +0000358 val->second.ABIAlign = ABIAlign;
359 val->second.PrefAlign = PrefAlign;
360 val->second.TypeByteWidth = TypeByteWidth;
Micah Villmow89021e42012-10-09 16:06:12 +0000361 }
362}
363
Micah Villmowac34b5c2012-10-04 22:08:14 +0000364/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or
Micah Villmowb4faa152012-10-04 23:01:22 +0000365/// preferred if ABIInfo = false) the layout wants for the specified datatype.
366unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000367 uint32_t BitWidth, bool ABIInfo,
368 Type *Ty) const {
369 // Check to see if we have an exact match and remember the best match we see.
370 int BestMatchIdx = -1;
371 int LargestInt = -1;
372 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmow6d05e692012-10-05 17:02:14 +0000373 if (Alignments[i].AlignType == (unsigned)AlignType &&
Micah Villmowac34b5c2012-10-04 22:08:14 +0000374 Alignments[i].TypeBitWidth == BitWidth)
375 return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign;
376
377 // The best match so far depends on what we're looking for.
378 if (AlignType == INTEGER_ALIGN &&
379 Alignments[i].AlignType == INTEGER_ALIGN) {
380 // The "best match" for integers is the smallest size that is larger than
381 // the BitWidth requested.
382 if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 ||
Eli Benderskyfaf5e3e2013-01-30 19:24:23 +0000383 Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth))
Micah Villmowac34b5c2012-10-04 22:08:14 +0000384 BestMatchIdx = i;
385 // However, if there isn't one that's larger, then we must use the
386 // largest one we have (see below)
387 if (LargestInt == -1 ||
388 Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth)
389 LargestInt = i;
390 }
391 }
392
393 // Okay, we didn't find an exact solution. Fall back here depending on what
394 // is being looked for.
395 if (BestMatchIdx == -1) {
396 // If we didn't find an integer alignment, fall back on most conservative.
397 if (AlignType == INTEGER_ALIGN) {
398 BestMatchIdx = LargestInt;
399 } else {
400 assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!");
401
402 // By default, use natural alignment for vector types. This is consistent
403 // with what clang and llvm-gcc do.
404 unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType());
405 Align *= cast<VectorType>(Ty)->getNumElements();
406 // If the alignment is not a power of 2, round up to the next power of 2.
407 // This happens for non-power-of-2 length vectors.
408 if (Align & (Align-1))
409 Align = NextPowerOf2(Align);
410 return Align;
411 }
412 }
413
414 // Since we got a "best match" index, just return it.
415 return ABIInfo ? Alignments[BestMatchIdx].ABIAlign
416 : Alignments[BestMatchIdx].PrefAlign;
417}
418
419namespace {
420
421class StructLayoutMap {
422 typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy;
423 LayoutInfoTy LayoutInfo;
424
425public:
426 virtual ~StructLayoutMap() {
427 // Remove any layouts.
428 for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end();
429 I != E; ++I) {
430 StructLayout *Value = I->second;
431 Value->~StructLayout();
432 free(Value);
433 }
434 }
435
436 StructLayout *&operator[](StructType *STy) {
437 return LayoutInfo[STy];
438 }
439
440 // for debugging...
441 virtual void dump() const {}
442};
443
444} // end anonymous namespace
445
Micah Villmowb4faa152012-10-04 23:01:22 +0000446DataLayout::~DataLayout() {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000447 delete static_cast<StructLayoutMap*>(LayoutMap);
448}
449
Pete Cooper6308a822013-03-12 17:37:31 +0000450bool DataLayout::doFinalization(Module &M) {
451 delete static_cast<StructLayoutMap*>(LayoutMap);
452 LayoutMap = 0;
453 return false;
454}
455
Micah Villmowb4faa152012-10-04 23:01:22 +0000456const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000457 if (!LayoutMap)
458 LayoutMap = new StructLayoutMap();
459
460 StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap);
461 StructLayout *&SL = (*STM)[Ty];
462 if (SL) return SL;
463
464 // Otherwise, create the struct layout. Because it is variable length, we
465 // malloc it, then use placement new.
466 int NumElts = Ty->getNumElements();
467 StructLayout *L =
468 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t));
469
470 // Set SL before calling StructLayout's ctor. The ctor could cause other
471 // entries to be added to TheMap, invalidating our reference.
472 SL = L;
473
474 new (L) StructLayout(Ty, *this);
475
476 return L;
477}
478
Micah Villmowb4faa152012-10-04 23:01:22 +0000479std::string DataLayout::getStringRepresentation() const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000480 std::string Result;
481 raw_string_ostream OS(Result);
482
Micah Villmow89021e42012-10-09 16:06:12 +0000483 OS << (LittleEndian ? "e" : "E");
484 SmallVector<unsigned, 8> addrSpaces;
485 // Lets get all of the known address spaces and sort them
486 // into increasing order so that we can emit the string
487 // in a cleaner format.
488 for (DenseMap<unsigned, PointerAlignElem>::const_iterator
489 pib = Pointers.begin(), pie = Pointers.end();
490 pib != pie; ++pib) {
491 addrSpaces.push_back(pib->first);
492 }
493 std::sort(addrSpaces.begin(), addrSpaces.end());
Craig Topperaf0dea12013-07-04 01:31:24 +0000494 for (SmallVectorImpl<unsigned>::iterator asb = addrSpaces.begin(),
Micah Villmow89021e42012-10-09 16:06:12 +0000495 ase = addrSpaces.end(); asb != ase; ++asb) {
496 const PointerAlignElem &PI = Pointers.find(*asb)->second;
Rafael Espindola458a4852013-12-19 23:03:03 +0000497
498 // Skip default.
499 if (PI.AddressSpace == 0 && PI.ABIAlign == 8 && PI.PrefAlign == 8 &&
500 PI.TypeByteWidth == 8)
501 continue;
502
Micah Villmow89021e42012-10-09 16:06:12 +0000503 OS << "-p";
504 if (PI.AddressSpace) {
505 OS << PI.AddressSpace;
506 }
Rafael Espindola458a4852013-12-19 23:03:03 +0000507 OS << ":" << PI.TypeByteWidth*8 << ':' << PI.ABIAlign*8;
508 if (PI.PrefAlign != PI.ABIAlign)
509 OS << ':' << PI.PrefAlign*8;
Micah Villmow89021e42012-10-09 16:06:12 +0000510 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000511
Rafael Espindolae23b8772013-12-20 15:21:32 +0000512 const LayoutAlignElem *DefaultStart = DefaultAlignments;
513 const LayoutAlignElem *DefaultEnd =
Rafael Espindola458a4852013-12-19 23:03:03 +0000514 DefaultStart + array_lengthof(DefaultAlignments);
Micah Villmowac34b5c2012-10-04 22:08:14 +0000515 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) {
Micah Villmowb4faa152012-10-04 23:01:22 +0000516 const LayoutAlignElem &AI = Alignments[i];
Rafael Espindola458a4852013-12-19 23:03:03 +0000517 if (std::find(DefaultStart, DefaultEnd, AI) != DefaultEnd)
518 continue;
519 OS << '-' << (char)AI.AlignType;
520 if (AI.TypeBitWidth)
521 OS << AI.TypeBitWidth;
522 OS << ':' << AI.ABIAlign*8;
523 if (AI.ABIAlign != AI.PrefAlign)
524 OS << ':' << AI.PrefAlign*8;
Micah Villmowac34b5c2012-10-04 22:08:14 +0000525 }
526
527 if (!LegalIntWidths.empty()) {
528 OS << "-n" << (unsigned)LegalIntWidths[0];
529
530 for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i)
531 OS << ':' << (unsigned)LegalIntWidths[i];
532 }
Rafael Espindola458a4852013-12-19 23:03:03 +0000533
534 if (StackNaturalAlign)
535 OS << "-S" << StackNaturalAlign*8;
536
Micah Villmowac34b5c2012-10-04 22:08:14 +0000537 return OS.str();
538}
539
Matt Arsenault6f4be902013-07-26 17:37:20 +0000540unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {
541 assert(Ty->isPtrOrPtrVectorTy() &&
542 "This should only be called with a pointer or pointer vector type");
543
544 if (Ty->isPointerTy())
545 return getTypeSizeInBits(Ty);
546
Matt Arsenault517cf482013-07-27 19:22:28 +0000547 return getTypeSizeInBits(Ty->getScalarType());
Matt Arsenault6f4be902013-07-26 17:37:20 +0000548}
Micah Villmowac34b5c2012-10-04 22:08:14 +0000549
Micah Villmowac34b5c2012-10-04 22:08:14 +0000550/*!
551 \param abi_or_pref Flag that determines which alignment is returned. true
552 returns the ABI alignment, false returns the preferred alignment.
553 \param Ty The underlying type for which alignment is determined.
554
555 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref
556 == false) for the requested type \a Ty.
557 */
Micah Villmowb4faa152012-10-04 23:01:22 +0000558unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000559 int AlignType = -1;
560
561 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
562 switch (Ty->getTypeID()) {
563 // Early escape for the non-numeric types.
564 case Type::LabelTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000565 return (abi_or_pref
Micah Villmow89021e42012-10-09 16:06:12 +0000566 ? getPointerABIAlignment(0)
567 : getPointerPrefAlignment(0));
568 case Type::PointerTyID: {
569 unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace();
570 return (abi_or_pref
571 ? getPointerABIAlignment(AS)
572 : getPointerPrefAlignment(AS));
573 }
Micah Villmowac34b5c2012-10-04 22:08:14 +0000574 case Type::ArrayTyID:
575 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref);
576
577 case Type::StructTyID: {
578 // Packed structure types always have an ABI alignment of one.
579 if (cast<StructType>(Ty)->isPacked() && abi_or_pref)
580 return 1;
581
582 // Get the layout annotation... which is lazily created on demand.
583 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty));
584 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty);
585 return std::max(Align, Layout->getAlignment());
586 }
587 case Type::IntegerTyID:
Micah Villmowac34b5c2012-10-04 22:08:14 +0000588 AlignType = INTEGER_ALIGN;
589 break;
590 case Type::HalfTyID:
591 case Type::FloatTyID:
592 case Type::DoubleTyID:
593 // PPC_FP128TyID and FP128TyID have different data contents, but the
594 // same size and alignment, so they look the same here.
595 case Type::PPC_FP128TyID:
596 case Type::FP128TyID:
597 case Type::X86_FP80TyID:
598 AlignType = FLOAT_ALIGN;
599 break;
600 case Type::X86_MMXTyID:
601 case Type::VectorTyID:
602 AlignType = VECTOR_ALIGN;
603 break;
604 default:
605 llvm_unreachable("Bad type for getAlignment!!!");
606 }
607
608 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty),
609 abi_or_pref, Ty);
610}
611
Micah Villmowb4faa152012-10-04 23:01:22 +0000612unsigned DataLayout::getABITypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000613 return getAlignment(Ty, true);
614}
615
616/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for
617/// an integer type of the specified bitwidth.
Micah Villmowb4faa152012-10-04 23:01:22 +0000618unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000619 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0);
620}
621
Micah Villmowb4faa152012-10-04 23:01:22 +0000622unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000623 return getAlignment(Ty, false);
624}
625
Micah Villmowb4faa152012-10-04 23:01:22 +0000626unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000627 unsigned Align = getPrefTypeAlignment(Ty);
628 assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
629 return Log2_32(Align);
630}
631
Micah Villmow89021e42012-10-09 16:06:12 +0000632IntegerType *DataLayout::getIntPtrType(LLVMContext &C,
633 unsigned AddressSpace) const {
634 return IntegerType::get(C, getPointerSizeInBits(AddressSpace));
Micah Villmowac34b5c2012-10-04 22:08:14 +0000635}
636
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000637Type *DataLayout::getIntPtrType(Type *Ty) const {
Chandler Carruth7ec50852012-11-01 08:07:29 +0000638 assert(Ty->isPtrOrPtrVectorTy() &&
639 "Expected a pointer or pointer vector type.");
Chandler Carruth7ec50852012-11-01 08:07:29 +0000640 unsigned NumBits = getTypeSizeInBits(Ty->getScalarType());
Duncan Sands5bdd9dd2012-10-29 17:31:46 +0000641 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits);
642 if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
643 return VectorType::get(IntTy, VecTy->getNumElements());
644 return IntTy;
Micah Villmow12d91272012-10-24 15:52:52 +0000645}
646
Arnaud A. de Grandmaisonf364bc62013-03-22 08:25:01 +0000647Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const {
648 for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i)
649 if (Width <= LegalIntWidths[i])
650 return Type::getIntNTy(C, LegalIntWidths[i]);
651 return 0;
652}
653
Matt Arsenault899f7d22013-09-16 22:43:16 +0000654unsigned DataLayout::getLargestLegalIntTypeSize() const {
655 unsigned MaxWidth = 0;
656 for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i)
657 MaxWidth = std::max<unsigned>(MaxWidth, LegalIntWidths[i]);
658 return MaxWidth;
659}
660
Micah Villmowb4faa152012-10-04 23:01:22 +0000661uint64_t DataLayout::getIndexedOffset(Type *ptrTy,
Micah Villmowac34b5c2012-10-04 22:08:14 +0000662 ArrayRef<Value *> Indices) const {
663 Type *Ty = ptrTy;
664 assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()");
665 uint64_t Result = 0;
666
667 generic_gep_type_iterator<Value* const*>
668 TI = gep_type_begin(ptrTy, Indices);
669 for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX;
670 ++CurIDX, ++TI) {
671 if (StructType *STy = dyn_cast<StructType>(*TI)) {
672 assert(Indices[CurIDX]->getType() ==
673 Type::getInt32Ty(ptrTy->getContext()) &&
674 "Illegal struct idx");
675 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue();
676
677 // Get structure layout information...
678 const StructLayout *Layout = getStructLayout(STy);
679
680 // Add in the offset, as calculated by the structure layout info...
681 Result += Layout->getElementOffset(FieldNo);
682
683 // Update Ty to refer to current element
684 Ty = STy->getElementType(FieldNo);
685 } else {
686 // Update Ty to refer to current element
687 Ty = cast<SequentialType>(Ty)->getElementType();
688
689 // Get the array index and the size of each array element.
690 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue())
691 Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty);
692 }
693 }
694
695 return Result;
696}
697
698/// getPreferredAlignment - Return the preferred alignment of the specified
699/// global. This includes an explicitly requested alignment (if the global
700/// has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000701unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000702 Type *ElemType = GV->getType()->getElementType();
703 unsigned Alignment = getPrefTypeAlignment(ElemType);
704 unsigned GVAlignment = GV->getAlignment();
705 if (GVAlignment >= Alignment) {
706 Alignment = GVAlignment;
707 } else if (GVAlignment != 0) {
708 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
709 }
710
711 if (GV->hasInitializer() && GVAlignment == 0) {
712 if (Alignment < 16) {
713 // If the global is not external, see if it is large. If so, give it a
714 // larger alignment.
715 if (getTypeSizeInBits(ElemType) > 128)
716 Alignment = 16; // 16-byte alignment.
717 }
718 }
719 return Alignment;
720}
721
722/// getPreferredAlignmentLog - Return the preferred alignment of the
723/// specified global, returned in log form. This includes an explicitly
724/// requested alignment (if the global has one).
Micah Villmowb4faa152012-10-04 23:01:22 +0000725unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Micah Villmowac34b5c2012-10-04 22:08:14 +0000726 return Log2_32(getPreferredAlignment(GV));
727}