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