Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 1 | //===-- DataLayout.cpp - Data size & alignment routines --------------------==// |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 2 | // |
| 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 Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 10 | // This file defines layout properties related to datatype size/offset/alignment |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 11 | // 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 Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Rafael Espindola | 458a485 | 2013-12-19 23:03:03 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" |
| 24 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 25 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Module.h" |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ManagedStatic.h" |
| 29 | #include "llvm/Support/MathExtras.h" |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Mutex.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | #include <cstdlib> |
| 34 | using namespace llvm; |
| 35 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
| 37 | // Support for StructLayout |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 40 | StructLayout::StructLayout(StructType *ST, const DataLayout &DL) { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 41 | assert(!ST->isOpaque() && "Cannot get layout of opaque structs"); |
| 42 | StructAlignment = 0; |
| 43 | StructSize = 0; |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 44 | IsPadded = false; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 45 | NumElements = ST->getNumElements(); |
| 46 | |
| 47 | // Loop over each of the elements, placing them in memory. |
| 48 | for (unsigned i = 0, e = NumElements; i != e; ++i) { |
| 49 | Type *Ty = ST->getElementType(i); |
Eli Bendersky | 41913c7 | 2013-04-16 15:41:18 +0000 | [diff] [blame] | 50 | unsigned TyAlign = ST->isPacked() ? 1 : DL.getABITypeAlignment(Ty); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 51 | |
| 52 | // Add padding if necessary to align the data element properly. |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 53 | if ((StructSize & (TyAlign-1)) != 0) { |
| 54 | IsPadded = true; |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 55 | StructSize = alignTo(StructSize, TyAlign); |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 56 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 57 | |
| 58 | // Keep track of maximum alignment constraint. |
| 59 | StructAlignment = std::max(TyAlign, StructAlignment); |
| 60 | |
| 61 | MemberOffsets[i] = StructSize; |
Eli Bendersky | 41913c7 | 2013-04-16 15:41:18 +0000 | [diff] [blame] | 62 | StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Empty structures have alignment of 1 byte. |
| 66 | if (StructAlignment == 0) StructAlignment = 1; |
| 67 | |
| 68 | // Add padding to the end of the struct so that it could be put in an array |
| 69 | // and all array elements would be aligned correctly. |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 70 | if ((StructSize & (StructAlignment-1)) != 0) { |
| 71 | IsPadded = true; |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 72 | StructSize = alignTo(StructSize, StructAlignment); |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 73 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
| 77 | /// getElementContainingOffset - Given a valid offset into the structure, |
| 78 | /// return the structure index that contains it. |
| 79 | unsigned 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 Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 98 | // LayoutAlignElem, LayoutAlign support |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 99 | //===----------------------------------------------------------------------===// |
| 100 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 101 | LayoutAlignElem |
| 102 | LayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align, |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 103 | unsigned pref_align, uint32_t bit_width) { |
| 104 | assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 105 | LayoutAlignElem retval; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 106 | retval.AlignType = align_type; |
| 107 | retval.ABIAlign = abi_align; |
| 108 | retval.PrefAlign = pref_align; |
| 109 | retval.TypeBitWidth = bit_width; |
| 110 | return retval; |
| 111 | } |
| 112 | |
| 113 | bool |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 114 | LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 115 | return (AlignType == rhs.AlignType |
| 116 | && ABIAlign == rhs.ABIAlign |
| 117 | && PrefAlign == rhs.PrefAlign |
| 118 | && TypeBitWidth == rhs.TypeBitWidth); |
| 119 | } |
| 120 | |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 121 | //===----------------------------------------------------------------------===// |
| 122 | // PointerAlignElem, PointerAlign support |
| 123 | //===----------------------------------------------------------------------===// |
| 124 | |
| 125 | PointerAlignElem |
Rafael Espindola | f39136c | 2013-12-13 23:15:20 +0000 | [diff] [blame] | 126 | PointerAlignElem::get(uint32_t AddressSpace, unsigned ABIAlign, |
| 127 | unsigned PrefAlign, uint32_t TypeByteWidth) { |
| 128 | assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!"); |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 129 | PointerAlignElem retval; |
Rafael Espindola | f39136c | 2013-12-13 23:15:20 +0000 | [diff] [blame] | 130 | retval.AddressSpace = AddressSpace; |
| 131 | retval.ABIAlign = ABIAlign; |
| 132 | retval.PrefAlign = PrefAlign; |
| 133 | retval.TypeByteWidth = TypeByteWidth; |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 134 | return retval; |
| 135 | } |
| 136 | |
| 137 | bool |
| 138 | PointerAlignElem::operator==(const PointerAlignElem &rhs) const { |
| 139 | return (ABIAlign == rhs.ABIAlign |
| 140 | && AddressSpace == rhs.AddressSpace |
| 141 | && PrefAlign == rhs.PrefAlign |
Rafael Espindola | f39136c | 2013-12-13 23:15:20 +0000 | [diff] [blame] | 142 | && TypeByteWidth == rhs.TypeByteWidth); |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 145 | //===----------------------------------------------------------------------===// |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 146 | // DataLayout Class Implementation |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 147 | //===----------------------------------------------------------------------===// |
| 148 | |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 149 | const char *DataLayout::getManglingComponent(const Triple &T) { |
| 150 | if (T.isOSBinFormatMachO()) |
| 151 | return "-m:o"; |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 152 | if (T.isOSWindows() && T.isOSBinFormatCOFF()) |
| 153 | return T.getArch() == Triple::x86 ? "-m:x" : "-m:w"; |
Saleem Abdulrasool | cd13082 | 2014-04-02 20:32:05 +0000 | [diff] [blame] | 154 | return "-m:e"; |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Rafael Espindola | e23b877 | 2013-12-20 15:21:32 +0000 | [diff] [blame] | 157 | static const LayoutAlignElem DefaultAlignments[] = { |
Rafael Espindola | 458a485 | 2013-12-19 23:03:03 +0000 | [diff] [blame] | 158 | { INTEGER_ALIGN, 1, 1, 1 }, // i1 |
| 159 | { INTEGER_ALIGN, 8, 1, 1 }, // i8 |
| 160 | { INTEGER_ALIGN, 16, 2, 2 }, // i16 |
| 161 | { INTEGER_ALIGN, 32, 4, 4 }, // i32 |
| 162 | { INTEGER_ALIGN, 64, 4, 8 }, // i64 |
| 163 | { FLOAT_ALIGN, 16, 2, 2 }, // half |
| 164 | { FLOAT_ALIGN, 32, 4, 4 }, // float |
| 165 | { FLOAT_ALIGN, 64, 8, 8 }, // double |
| 166 | { FLOAT_ALIGN, 128, 16, 16 }, // ppcf128, quad, ... |
| 167 | { VECTOR_ALIGN, 64, 8, 8 }, // v2i32, v1i64, ... |
| 168 | { VECTOR_ALIGN, 128, 16, 16 }, // v16i8, v8i16, v4i32, ... |
| 169 | { AGGREGATE_ALIGN, 0, 0, 8 } // struct |
| 170 | }; |
| 171 | |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 172 | void DataLayout::reset(StringRef Desc) { |
| 173 | clear(); |
| 174 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 175 | LayoutMap = nullptr; |
Chandler Carruth | f67321c | 2014-10-20 10:41:29 +0000 | [diff] [blame] | 176 | BigEndian = false; |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 177 | AllocaAddrSpace = 0; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 178 | StackNaturalAlign = 0; |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 179 | ManglingMode = MM_None; |
Sanjoy Das | c6af5ea | 2016-07-28 23:43:38 +0000 | [diff] [blame] | 180 | NonIntegralAddressSpaces.clear(); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 181 | |
| 182 | // Default alignments |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 183 | for (const LayoutAlignElem &E : DefaultAlignments) { |
Rafael Espindola | 458a485 | 2013-12-19 23:03:03 +0000 | [diff] [blame] | 184 | setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign, |
| 185 | E.TypeBitWidth); |
| 186 | } |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 187 | setPointerAlignment(0, 8, 8, 8); |
Patrik Hägglund | 01860a6 | 2012-11-14 09:04:56 +0000 | [diff] [blame] | 188 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 189 | parseSpecifier(Desc); |
| 190 | } |
| 191 | |
| 192 | /// Checked version of split, to ensure mandatory subparts. |
| 193 | static std::pair<StringRef, StringRef> split(StringRef Str, char Separator) { |
| 194 | assert(!Str.empty() && "parse error, string can't be empty here"); |
| 195 | std::pair<StringRef, StringRef> Split = Str.split(Separator); |
David Majnemer | 2dc1b0f | 2014-12-10 01:38:28 +0000 | [diff] [blame] | 196 | if (Split.second.empty() && Split.first != Str) |
| 197 | report_fatal_error("Trailing separator in datalayout string"); |
David Majnemer | 612f312 | 2014-12-10 02:36:41 +0000 | [diff] [blame] | 198 | if (!Split.second.empty() && Split.first.empty()) |
| 199 | report_fatal_error("Expected token before separator in datalayout string"); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 200 | return Split; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Cameron McInally | 8af9eac | 2014-01-07 19:51:38 +0000 | [diff] [blame] | 203 | /// Get an unsigned integer, including error checks. |
Patrik Hägglund | 3eb16c5 | 2012-11-28 12:13:12 +0000 | [diff] [blame] | 204 | static unsigned getInt(StringRef R) { |
Cameron McInally | f0379fa | 2014-01-13 22:04:55 +0000 | [diff] [blame] | 205 | unsigned Result; |
Patrik Hägglund | 504f478 | 2012-11-28 14:32:52 +0000 | [diff] [blame] | 206 | bool error = R.getAsInteger(10, Result); (void)error; |
Cameron McInally | f0379fa | 2014-01-13 22:04:55 +0000 | [diff] [blame] | 207 | if (error) |
| 208 | report_fatal_error("not a number, or does not fit in an unsigned int"); |
Patrik Hägglund | 3eb16c5 | 2012-11-28 12:13:12 +0000 | [diff] [blame] | 209 | return Result; |
| 210 | } |
| 211 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 212 | /// Convert bits into bytes. Assert if not a byte width multiple. |
| 213 | static unsigned inBytes(unsigned Bits) { |
David Majnemer | 2dc1b0f | 2014-12-10 01:38:28 +0000 | [diff] [blame] | 214 | if (Bits % 8) |
| 215 | report_fatal_error("number of bits must be a byte width multiple"); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 216 | return Bits / 8; |
| 217 | } |
| 218 | |
| 219 | void DataLayout::parseSpecifier(StringRef Desc) { |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 220 | StringRepresentation = Desc; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 221 | while (!Desc.empty()) { |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 222 | // Split at '-'. |
| 223 | std::pair<StringRef, StringRef> Split = split(Desc, '-'); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 224 | Desc = Split.second; |
| 225 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 226 | // Split at ':'. |
| 227 | Split = split(Split.first, ':'); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 228 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 229 | // Aliases used below. |
| 230 | StringRef &Tok = Split.first; // Current token. |
| 231 | StringRef &Rest = Split.second; // The rest of the string. |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 232 | |
Sanjoy Das | c6af5ea | 2016-07-28 23:43:38 +0000 | [diff] [blame] | 233 | if (Tok == "ni") { |
| 234 | do { |
| 235 | Split = split(Rest, ':'); |
| 236 | Rest = Split.second; |
| 237 | unsigned AS = getInt(Split.first); |
| 238 | if (AS == 0) |
| 239 | report_fatal_error("Address space 0 can never be non-integral"); |
| 240 | NonIntegralAddressSpaces.push_back(AS); |
| 241 | } while (!Rest.empty()); |
| 242 | |
| 243 | continue; |
| 244 | } |
| 245 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 246 | char Specifier = Tok.front(); |
| 247 | Tok = Tok.substr(1); |
| 248 | |
| 249 | switch (Specifier) { |
Rafael Espindola | 6994fdf | 2014-01-01 22:29:43 +0000 | [diff] [blame] | 250 | case 's': |
| 251 | // Ignored for backward compatibility. |
| 252 | // FIXME: remove this on LLVM 4.0. |
| 253 | break; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 254 | case 'E': |
Chandler Carruth | f67321c | 2014-10-20 10:41:29 +0000 | [diff] [blame] | 255 | BigEndian = true; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 256 | break; |
| 257 | case 'e': |
Chandler Carruth | f67321c | 2014-10-20 10:41:29 +0000 | [diff] [blame] | 258 | BigEndian = false; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 259 | break; |
| 260 | case 'p': { |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 261 | // Address space. |
| 262 | unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok); |
David Majnemer | 5330c69 | 2014-12-10 01:17:08 +0000 | [diff] [blame] | 263 | if (!isUInt<24>(AddrSpace)) |
| 264 | report_fatal_error("Invalid address space, must be a 24bit integer"); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 265 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 266 | // Size. |
David Majnemer | 2dc1b0f | 2014-12-10 01:38:28 +0000 | [diff] [blame] | 267 | if (Rest.empty()) |
| 268 | report_fatal_error( |
| 269 | "Missing size specification for pointer in datalayout string"); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 270 | Split = split(Rest, ':'); |
| 271 | unsigned PointerMemSize = inBytes(getInt(Tok)); |
Owen Anderson | 5bc2bbe | 2015-03-02 06:00:02 +0000 | [diff] [blame] | 272 | if (!PointerMemSize) |
| 273 | report_fatal_error("Invalid pointer size of 0 bytes"); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 274 | |
| 275 | // ABI alignment. |
David Majnemer | 2dc1b0f | 2014-12-10 01:38:28 +0000 | [diff] [blame] | 276 | if (Rest.empty()) |
| 277 | report_fatal_error( |
| 278 | "Missing alignment specification for pointer in datalayout string"); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 279 | Split = split(Rest, ':'); |
| 280 | unsigned PointerABIAlign = inBytes(getInt(Tok)); |
Owen Anderson | 040f2f8 | 2015-03-02 06:33:51 +0000 | [diff] [blame] | 281 | if (!isPowerOf2_64(PointerABIAlign)) |
| 282 | report_fatal_error( |
| 283 | "Pointer ABI alignment must be a power of 2"); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 284 | |
| 285 | // Preferred alignment. |
| 286 | unsigned PointerPrefAlign = PointerABIAlign; |
| 287 | if (!Rest.empty()) { |
| 288 | Split = split(Rest, ':'); |
| 289 | PointerPrefAlign = inBytes(getInt(Tok)); |
Owen Anderson | 040f2f8 | 2015-03-02 06:33:51 +0000 | [diff] [blame] | 290 | if (!isPowerOf2_64(PointerPrefAlign)) |
| 291 | report_fatal_error( |
| 292 | "Pointer preferred alignment must be a power of 2"); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 293 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 294 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 295 | setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign, |
| 296 | PointerMemSize); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 297 | break; |
| 298 | } |
| 299 | case 'i': |
| 300 | case 'v': |
| 301 | case 'f': |
Rafael Espindola | 6994fdf | 2014-01-01 22:29:43 +0000 | [diff] [blame] | 302 | case 'a': { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 303 | AlignTypeEnum AlignType; |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 304 | switch (Specifier) { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 305 | default: |
| 306 | case 'i': AlignType = INTEGER_ALIGN; break; |
| 307 | case 'v': AlignType = VECTOR_ALIGN; break; |
| 308 | case 'f': AlignType = FLOAT_ALIGN; break; |
| 309 | case 'a': AlignType = AGGREGATE_ALIGN; break; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 310 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 311 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 312 | // Bit size. |
| 313 | unsigned Size = Tok.empty() ? 0 : getInt(Tok); |
| 314 | |
David Majnemer | 5330c69 | 2014-12-10 01:17:08 +0000 | [diff] [blame] | 315 | if (AlignType == AGGREGATE_ALIGN && Size != 0) |
| 316 | report_fatal_error( |
| 317 | "Sized aggregate specification in datalayout string"); |
Rafael Espindola | abdd726 | 2014-01-06 21:40:24 +0000 | [diff] [blame] | 318 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 319 | // ABI alignment. |
David Majnemer | 612f312 | 2014-12-10 02:36:41 +0000 | [diff] [blame] | 320 | if (Rest.empty()) |
| 321 | report_fatal_error( |
| 322 | "Missing alignment specification in datalayout string"); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 323 | Split = split(Rest, ':'); |
| 324 | unsigned ABIAlign = inBytes(getInt(Tok)); |
Owen Anderson | ab1c7a7 | 2015-03-02 09:34:59 +0000 | [diff] [blame] | 325 | if (AlignType != AGGREGATE_ALIGN && !ABIAlign) |
| 326 | report_fatal_error( |
| 327 | "ABI alignment specification must be >0 for non-aggregate types"); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 328 | |
| 329 | // Preferred alignment. |
| 330 | unsigned PrefAlign = ABIAlign; |
| 331 | if (!Rest.empty()) { |
| 332 | Split = split(Rest, ':'); |
| 333 | PrefAlign = inBytes(getInt(Tok)); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 334 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 335 | |
Patrik Hägglund | 01860a6 | 2012-11-14 09:04:56 +0000 | [diff] [blame] | 336 | setAlignment(AlignType, ABIAlign, PrefAlign, Size); |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 337 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 338 | break; |
| 339 | } |
| 340 | case 'n': // Native integer types. |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 341 | for (;;) { |
| 342 | unsigned Width = getInt(Tok); |
David Majnemer | 5330c69 | 2014-12-10 01:17:08 +0000 | [diff] [blame] | 343 | if (Width == 0) |
| 344 | report_fatal_error( |
| 345 | "Zero width native integer type in datalayout string"); |
Patrik Hägglund | 3eb16c5 | 2012-11-28 12:13:12 +0000 | [diff] [blame] | 346 | LegalIntWidths.push_back(Width); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 347 | if (Rest.empty()) |
| 348 | break; |
| 349 | Split = split(Rest, ':'); |
| 350 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 351 | break; |
| 352 | case 'S': { // Stack natural alignment. |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 353 | StackNaturalAlign = inBytes(getInt(Tok)); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 354 | break; |
| 355 | } |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 356 | case 'A': { // Default stack/alloca address space. |
| 357 | AllocaAddrSpace = getInt(Tok); |
| 358 | if (!isUInt<24>(AllocaAddrSpace)) |
| 359 | report_fatal_error("Invalid address space, must be a 24bit integer"); |
| 360 | break; |
| 361 | } |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 362 | case 'm': |
David Majnemer | 612f312 | 2014-12-10 02:36:41 +0000 | [diff] [blame] | 363 | if (!Tok.empty()) |
| 364 | report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string"); |
| 365 | if (Rest.empty()) |
| 366 | report_fatal_error("Expected mangling specifier in datalayout string"); |
| 367 | if (Rest.size() > 1) |
| 368 | report_fatal_error("Unknown mangling specifier in datalayout string"); |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 369 | switch(Rest[0]) { |
| 370 | default: |
David Majnemer | 5330c69 | 2014-12-10 01:17:08 +0000 | [diff] [blame] | 371 | report_fatal_error("Unknown mangling in datalayout string"); |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 372 | case 'e': |
| 373 | ManglingMode = MM_ELF; |
| 374 | break; |
| 375 | case 'o': |
| 376 | ManglingMode = MM_MachO; |
| 377 | break; |
| 378 | case 'm': |
| 379 | ManglingMode = MM_Mips; |
| 380 | break; |
Rafael Espindola | af77e12 | 2014-01-10 13:42:12 +0000 | [diff] [blame] | 381 | case 'w': |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 382 | ManglingMode = MM_WinCOFF; |
| 383 | break; |
| 384 | case 'x': |
| 385 | ManglingMode = MM_WinCOFFX86; |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 386 | break; |
| 387 | } |
| 388 | break; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 389 | default: |
David Majnemer | 5330c69 | 2014-12-10 01:17:08 +0000 | [diff] [blame] | 390 | report_fatal_error("Unknown specifier in datalayout string"); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 396 | DataLayout::DataLayout(const Module *M) : LayoutMap(nullptr) { |
Rafael Espindola | c435adc | 2014-09-10 21:27:43 +0000 | [diff] [blame] | 397 | init(M); |
| 398 | } |
| 399 | |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 400 | void DataLayout::init(const Module *M) { *this = M->getDataLayout(); } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 401 | |
Rafael Espindola | ae593f1 | 2014-02-26 17:02:08 +0000 | [diff] [blame] | 402 | bool DataLayout::operator==(const DataLayout &Other) const { |
Chandler Carruth | f67321c | 2014-10-20 10:41:29 +0000 | [diff] [blame] | 403 | bool Ret = BigEndian == Other.BigEndian && |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 404 | AllocaAddrSpace == Other.AllocaAddrSpace && |
Rafael Espindola | ae593f1 | 2014-02-26 17:02:08 +0000 | [diff] [blame] | 405 | StackNaturalAlign == Other.StackNaturalAlign && |
| 406 | ManglingMode == Other.ManglingMode && |
| 407 | LegalIntWidths == Other.LegalIntWidths && |
Rafael Espindola | 89992b0 | 2014-04-22 17:47:03 +0000 | [diff] [blame] | 408 | Alignments == Other.Alignments && Pointers == Other.Pointers; |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 409 | // Note: getStringRepresentation() might differs, it is not canonicalized |
Rafael Espindola | ae593f1 | 2014-02-26 17:02:08 +0000 | [diff] [blame] | 410 | return Ret; |
| 411 | } |
| 412 | |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 413 | DataLayout::AlignmentsTy::iterator |
| 414 | DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType, |
| 415 | uint32_t BitWidth) { |
| 416 | auto Pair = std::make_pair((unsigned)AlignType, BitWidth); |
| 417 | return std::lower_bound(Alignments.begin(), Alignments.end(), Pair, |
| 418 | [](const LayoutAlignElem &LHS, |
| 419 | const std::pair<unsigned, uint32_t> &RHS) { |
| 420 | return std::tie(LHS.AlignType, LHS.TypeBitWidth) < |
| 421 | std::tie(RHS.first, RHS.second); |
| 422 | }); |
| 423 | } |
| 424 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 425 | void |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 426 | DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align, |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 427 | unsigned pref_align, uint32_t bit_width) { |
David Majnemer | 1b9fc3a | 2015-02-16 05:41:53 +0000 | [diff] [blame] | 428 | if (!isUInt<24>(bit_width)) |
| 429 | report_fatal_error("Invalid bit width, must be a 24bit integer"); |
| 430 | if (!isUInt<16>(abi_align)) |
| 431 | report_fatal_error("Invalid ABI alignment, must be a 16bit integer"); |
| 432 | if (!isUInt<16>(pref_align)) |
| 433 | report_fatal_error("Invalid preferred alignment, must be a 16bit integer"); |
Owen Anderson | 5af4b21 | 2015-03-02 09:35:03 +0000 | [diff] [blame] | 434 | if (abi_align != 0 && !isPowerOf2_64(abi_align)) |
| 435 | report_fatal_error("Invalid ABI alignment, must be a power of 2"); |
| 436 | if (pref_align != 0 && !isPowerOf2_64(pref_align)) |
| 437 | report_fatal_error("Invalid preferred alignment, must be a power of 2"); |
David Majnemer | 1b9fc3a | 2015-02-16 05:41:53 +0000 | [diff] [blame] | 438 | |
| 439 | if (pref_align < abi_align) |
| 440 | report_fatal_error( |
| 441 | "Preferred alignment cannot be less than the ABI alignment"); |
| 442 | |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 443 | AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width); |
| 444 | if (I != Alignments.end() && |
| 445 | I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) { |
| 446 | // Update the abi, preferred alignments. |
| 447 | I->ABIAlign = abi_align; |
| 448 | I->PrefAlign = pref_align; |
| 449 | } else { |
| 450 | // Insert before I to keep the vector sorted. |
| 451 | Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align, |
| 452 | pref_align, bit_width)); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 453 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 456 | DataLayout::PointersTy::iterator |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 457 | DataLayout::findPointerLowerBound(uint32_t AddressSpace) { |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 458 | return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace, |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 459 | [](const PointerAlignElem &A, uint32_t AddressSpace) { |
| 460 | return A.AddressSpace < AddressSpace; |
| 461 | }); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Rafael Espindola | f39136c | 2013-12-13 23:15:20 +0000 | [diff] [blame] | 464 | void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign, |
| 465 | unsigned PrefAlign, |
| 466 | uint32_t TypeByteWidth) { |
David Majnemer | 4b04292 | 2015-02-16 05:41:55 +0000 | [diff] [blame] | 467 | if (PrefAlign < ABIAlign) |
| 468 | report_fatal_error( |
| 469 | "Preferred alignment cannot be less than the ABI alignment"); |
| 470 | |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 471 | PointersTy::iterator I = findPointerLowerBound(AddrSpace); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 472 | if (I == Pointers.end() || I->AddressSpace != AddrSpace) { |
| 473 | Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign, |
| 474 | TypeByteWidth)); |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 475 | } else { |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 476 | I->ABIAlign = ABIAlign; |
| 477 | I->PrefAlign = PrefAlign; |
| 478 | I->TypeByteWidth = TypeByteWidth; |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 482 | /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 483 | /// preferred if ABIInfo = false) the layout wants for the specified datatype. |
| 484 | unsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType, |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 485 | uint32_t BitWidth, bool ABIInfo, |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 486 | Type *Ty) const { |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 487 | AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth); |
| 488 | // See if we found an exact match. Of if we are looking for an integer type, |
| 489 | // but don't have an exact match take the next largest integer. This is where |
| 490 | // the lower_bound will point to when it fails an exact match. |
| 491 | if (I != Alignments.end() && I->AlignType == (unsigned)AlignType && |
| 492 | (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN)) |
| 493 | return ABIInfo ? I->ABIAlign : I->PrefAlign; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 494 | |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 495 | if (AlignType == INTEGER_ALIGN) { |
| 496 | // If we didn't have a larger value try the largest value we have. |
| 497 | if (I != Alignments.begin()) { |
| 498 | --I; // Go to the previous entry and see if its an integer. |
| 499 | if (I->AlignType == INTEGER_ALIGN) |
| 500 | return ABIInfo ? I->ABIAlign : I->PrefAlign; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 501 | } |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 502 | } else if (AlignType == VECTOR_ALIGN) { |
| 503 | // By default, use natural alignment for vector types. This is consistent |
| 504 | // with what clang and llvm-gcc do. |
| 505 | unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType()); |
| 506 | Align *= cast<VectorType>(Ty)->getNumElements(); |
| 507 | Align = PowerOf2Ceil(Align); |
| 508 | return Align; |
| 509 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 510 | |
Owen Anderson | 7e621e9 | 2015-03-08 21:53:59 +0000 | [diff] [blame] | 511 | // If we still couldn't find a reasonable default alignment, fall back |
| 512 | // to a simple heuristic that the alignment is the first power of two |
| 513 | // greater-or-equal to the store size of the type. This is a reasonable |
| 514 | // approximation of reality, and if the user wanted something less |
| 515 | // less conservative, they should have specified it explicitly in the data |
| 516 | // layout. |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 517 | unsigned Align = getTypeStoreSize(Ty); |
| 518 | Align = PowerOf2Ceil(Align); |
| 519 | return Align; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | namespace { |
| 523 | |
| 524 | class StructLayoutMap { |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 525 | typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 526 | LayoutInfoTy LayoutInfo; |
| 527 | |
| 528 | public: |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 529 | ~StructLayoutMap() { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 530 | // Remove any layouts. |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 531 | for (const auto &I : LayoutInfo) { |
| 532 | StructLayout *Value = I.second; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 533 | Value->~StructLayout(); |
| 534 | free(Value); |
| 535 | } |
| 536 | } |
| 537 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 538 | StructLayout *&operator[](StructType *STy) { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 539 | return LayoutInfo[STy]; |
| 540 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 541 | }; |
| 542 | |
| 543 | } // end anonymous namespace |
| 544 | |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 545 | void DataLayout::clear() { |
| 546 | LegalIntWidths.clear(); |
| 547 | Alignments.clear(); |
| 548 | Pointers.clear(); |
| 549 | delete static_cast<StructLayoutMap *>(LayoutMap); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 550 | LayoutMap = nullptr; |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 553 | DataLayout::~DataLayout() { |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 554 | clear(); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 557 | const StructLayout *DataLayout::getStructLayout(StructType *Ty) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 558 | if (!LayoutMap) |
| 559 | LayoutMap = new StructLayoutMap(); |
| 560 | |
| 561 | StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap); |
| 562 | StructLayout *&SL = (*STM)[Ty]; |
| 563 | if (SL) return SL; |
| 564 | |
| 565 | // Otherwise, create the struct layout. Because it is variable length, we |
| 566 | // malloc it, then use placement new. |
| 567 | int NumElts = Ty->getNumElements(); |
| 568 | StructLayout *L = |
| 569 | (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t)); |
| 570 | |
| 571 | // Set SL before calling StructLayout's ctor. The ctor could cause other |
| 572 | // entries to be added to TheMap, invalidating our reference. |
| 573 | SL = L; |
| 574 | |
| 575 | new (L) StructLayout(Ty, *this); |
| 576 | |
| 577 | return L; |
| 578 | } |
| 579 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 580 | |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 581 | unsigned DataLayout::getPointerABIAlignment(unsigned AS) const { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 582 | PointersTy::const_iterator I = findPointerLowerBound(AS); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 583 | if (I == Pointers.end() || I->AddressSpace != AS) { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 584 | I = findPointerLowerBound(0); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 585 | assert(I->AddressSpace == 0); |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 586 | } |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 587 | return I->ABIAlign; |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 591 | PointersTy::const_iterator I = findPointerLowerBound(AS); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 592 | if (I == Pointers.end() || I->AddressSpace != AS) { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 593 | I = findPointerLowerBound(0); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 594 | assert(I->AddressSpace == 0); |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 595 | } |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 596 | return I->PrefAlign; |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | unsigned DataLayout::getPointerSize(unsigned AS) const { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 600 | PointersTy::const_iterator I = findPointerLowerBound(AS); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 601 | if (I == Pointers.end() || I->AddressSpace != AS) { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 602 | I = findPointerLowerBound(0); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 603 | assert(I->AddressSpace == 0); |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 604 | } |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 605 | return I->TypeByteWidth; |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 608 | unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const { |
Matt Arsenault | 6f4be90 | 2013-07-26 17:37:20 +0000 | [diff] [blame] | 609 | assert(Ty->isPtrOrPtrVectorTy() && |
| 610 | "This should only be called with a pointer or pointer vector type"); |
| 611 | |
| 612 | if (Ty->isPointerTy()) |
| 613 | return getTypeSizeInBits(Ty); |
| 614 | |
Matt Arsenault | 517cf48 | 2013-07-27 19:22:28 +0000 | [diff] [blame] | 615 | return getTypeSizeInBits(Ty->getScalarType()); |
Matt Arsenault | 6f4be90 | 2013-07-26 17:37:20 +0000 | [diff] [blame] | 616 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 617 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 618 | /*! |
| 619 | \param abi_or_pref Flag that determines which alignment is returned. true |
| 620 | returns the ABI alignment, false returns the preferred alignment. |
| 621 | \param Ty The underlying type for which alignment is determined. |
| 622 | |
| 623 | Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref |
| 624 | == false) for the requested type \a Ty. |
| 625 | */ |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 626 | unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 627 | int AlignType = -1; |
| 628 | |
| 629 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
| 630 | switch (Ty->getTypeID()) { |
| 631 | // Early escape for the non-numeric types. |
| 632 | case Type::LabelTyID: |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 633 | return (abi_or_pref |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 634 | ? getPointerABIAlignment(0) |
| 635 | : getPointerPrefAlignment(0)); |
| 636 | case Type::PointerTyID: { |
Matt Arsenault | c172897 | 2014-09-18 22:28:56 +0000 | [diff] [blame] | 637 | unsigned AS = cast<PointerType>(Ty)->getAddressSpace(); |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 638 | return (abi_or_pref |
| 639 | ? getPointerABIAlignment(AS) |
| 640 | : getPointerPrefAlignment(AS)); |
| 641 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 642 | case Type::ArrayTyID: |
| 643 | return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref); |
| 644 | |
| 645 | case Type::StructTyID: { |
| 646 | // Packed structure types always have an ABI alignment of one. |
| 647 | if (cast<StructType>(Ty)->isPacked() && abi_or_pref) |
| 648 | return 1; |
| 649 | |
| 650 | // Get the layout annotation... which is lazily created on demand. |
| 651 | const StructLayout *Layout = getStructLayout(cast<StructType>(Ty)); |
| 652 | unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty); |
| 653 | return std::max(Align, Layout->getAlignment()); |
| 654 | } |
| 655 | case Type::IntegerTyID: |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 656 | AlignType = INTEGER_ALIGN; |
| 657 | break; |
| 658 | case Type::HalfTyID: |
| 659 | case Type::FloatTyID: |
| 660 | case Type::DoubleTyID: |
| 661 | // PPC_FP128TyID and FP128TyID have different data contents, but the |
| 662 | // same size and alignment, so they look the same here. |
| 663 | case Type::PPC_FP128TyID: |
| 664 | case Type::FP128TyID: |
| 665 | case Type::X86_FP80TyID: |
| 666 | AlignType = FLOAT_ALIGN; |
| 667 | break; |
| 668 | case Type::X86_MMXTyID: |
| 669 | case Type::VectorTyID: |
| 670 | AlignType = VECTOR_ALIGN; |
| 671 | break; |
| 672 | default: |
| 673 | llvm_unreachable("Bad type for getAlignment!!!"); |
| 674 | } |
| 675 | |
| 676 | return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty), |
| 677 | abi_or_pref, Ty); |
| 678 | } |
| 679 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 680 | unsigned DataLayout::getABITypeAlignment(Type *Ty) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 681 | return getAlignment(Ty, true); |
| 682 | } |
| 683 | |
| 684 | /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for |
| 685 | /// an integer type of the specified bitwidth. |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 686 | unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 687 | return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 690 | unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 691 | return getAlignment(Ty, false); |
| 692 | } |
| 693 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 694 | unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 695 | unsigned Align = getPrefTypeAlignment(Ty); |
| 696 | assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); |
| 697 | return Log2_32(Align); |
| 698 | } |
| 699 | |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 700 | IntegerType *DataLayout::getIntPtrType(LLVMContext &C, |
| 701 | unsigned AddressSpace) const { |
| 702 | return IntegerType::get(C, getPointerSizeInBits(AddressSpace)); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 705 | Type *DataLayout::getIntPtrType(Type *Ty) const { |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 706 | assert(Ty->isPtrOrPtrVectorTy() && |
| 707 | "Expected a pointer or pointer vector type."); |
Matt Arsenault | 4dbd489 | 2014-04-23 21:10:15 +0000 | [diff] [blame] | 708 | unsigned NumBits = getPointerTypeSizeInBits(Ty); |
Duncan Sands | 5bdd9dd | 2012-10-29 17:31:46 +0000 | [diff] [blame] | 709 | IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits); |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 710 | if (VectorType *VecTy = dyn_cast<VectorType>(Ty)) |
Duncan Sands | 5bdd9dd | 2012-10-29 17:31:46 +0000 | [diff] [blame] | 711 | return VectorType::get(IntTy, VecTy->getNumElements()); |
| 712 | return IntTy; |
Micah Villmow | 12d9127 | 2012-10-24 15:52:52 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 715 | Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const { |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 716 | for (unsigned LegalIntWidth : LegalIntWidths) |
| 717 | if (Width <= LegalIntWidth) |
| 718 | return Type::getIntNTy(C, LegalIntWidth); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 719 | return nullptr; |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Jun Bum Lim | be11bdc | 2016-05-13 18:38:35 +0000 | [diff] [blame] | 722 | unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const { |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 723 | auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end()); |
| 724 | return Max != LegalIntWidths.end() ? *Max : 0; |
Matt Arsenault | 899f7d2 | 2013-09-16 22:43:16 +0000 | [diff] [blame] | 725 | } |
| 726 | |
David Majnemer | 17bdf44 | 2016-07-13 03:42:38 +0000 | [diff] [blame] | 727 | int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy, |
| 728 | ArrayRef<Value *> Indices) const { |
| 729 | int64_t Result = 0; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 730 | |
| 731 | generic_gep_type_iterator<Value* const*> |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 732 | GTI = gep_type_begin(ElemTy, Indices), |
| 733 | GTE = gep_type_end(ElemTy, Indices); |
Eduard Burtescu | 68e7f49 | 2016-01-22 03:08:27 +0000 | [diff] [blame] | 734 | for (; GTI != GTE; ++GTI) { |
| 735 | Value *Idx = GTI.getOperand(); |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 736 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
Manuel Jacob | cc13c2c | 2016-01-22 03:30:27 +0000 | [diff] [blame] | 737 | assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx"); |
Eduard Burtescu | 68e7f49 | 2016-01-22 03:08:27 +0000 | [diff] [blame] | 738 | unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue(); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 739 | |
| 740 | // Get structure layout information... |
| 741 | const StructLayout *Layout = getStructLayout(STy); |
| 742 | |
| 743 | // Add in the offset, as calculated by the structure layout info... |
| 744 | Result += Layout->getElementOffset(FieldNo); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 745 | } else { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 746 | // Get the array index and the size of each array element. |
Eduard Burtescu | 68e7f49 | 2016-01-22 03:08:27 +0000 | [diff] [blame] | 747 | if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue()) |
David Majnemer | 17bdf44 | 2016-07-13 03:42:38 +0000 | [diff] [blame] | 748 | Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType()); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
| 752 | return Result; |
| 753 | } |
| 754 | |
| 755 | /// getPreferredAlignment - Return the preferred alignment of the specified |
| 756 | /// global. This includes an explicitly requested alignment (if the global |
| 757 | /// has one). |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 758 | unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const { |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 759 | Type *ElemType = GV->getValueType(); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 760 | unsigned Alignment = getPrefTypeAlignment(ElemType); |
| 761 | unsigned GVAlignment = GV->getAlignment(); |
| 762 | if (GVAlignment >= Alignment) { |
| 763 | Alignment = GVAlignment; |
| 764 | } else if (GVAlignment != 0) { |
| 765 | Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType)); |
| 766 | } |
| 767 | |
| 768 | if (GV->hasInitializer() && GVAlignment == 0) { |
| 769 | if (Alignment < 16) { |
| 770 | // If the global is not external, see if it is large. If so, give it a |
| 771 | // larger alignment. |
| 772 | if (getTypeSizeInBits(ElemType) > 128) |
| 773 | Alignment = 16; // 16-byte alignment. |
| 774 | } |
| 775 | } |
| 776 | return Alignment; |
| 777 | } |
| 778 | |
| 779 | /// getPreferredAlignmentLog - Return the preferred alignment of the |
| 780 | /// specified global, returned in log form. This includes an explicitly |
| 781 | /// requested alignment (if the global has one). |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 782 | unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 783 | return Log2_32(getPreferredAlignment(GV)); |
| 784 | } |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 785 | |