Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 1 | //===- DataLayout.cpp - Data size & alignment routines ---------------------==// |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 9 | // This file defines layout properties related to datatype size/offset/alignment |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 10 | // information. |
| 11 | // |
| 12 | // This structure should be created once, filled in if the defaults are not |
| 13 | // correct and then passed around by const&. None of the members functions |
| 14 | // require modification to the object. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constants.h" |
| 23 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 24 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 25 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Module.h" |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Type.h" |
| 28 | #include "llvm/IR/Value.h" |
| 29 | #include "llvm/Support/Casting.h" |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 30 | #include "llvm/Support/Error.h" |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MathExtras.h" |
Graham Hunter | b302561 | 2019-10-08 12:53:54 +0000 | [diff] [blame] | 33 | #include "llvm/Support/TypeSize.h" |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 35 | #include <cassert> |
| 36 | #include <cstdint> |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 37 | #include <cstdlib> |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 38 | #include <tuple> |
| 39 | #include <utility> |
| 40 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 43 | //===----------------------------------------------------------------------===// |
| 44 | // Support for StructLayout |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 47 | StructLayout::StructLayout(StructType *ST, const DataLayout &DL) { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 48 | assert(!ST->isOpaque() && "Cannot get layout of opaque structs"); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 49 | StructSize = 0; |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 50 | IsPadded = false; |
Micah Villmow | ac34b5c | 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); |
Guillaume Chatelet | d3085c2 | 2020-07-01 14:31:56 +0000 | [diff] [blame] | 56 | const Align TyAlign = ST->isPacked() ? Align(1) : DL.getABITypeAlign(Ty); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 57 | |
| 58 | // Add padding if necessary to align the data element properly. |
Guillaume Chatelet | 6c127cd | 2019-09-20 13:40:31 +0000 | [diff] [blame] | 59 | if (!isAligned(TyAlign, StructSize)) { |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 60 | IsPadded = true; |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 61 | StructSize = alignTo(StructSize, TyAlign); |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 62 | } |
Micah Villmow | ac34b5c | 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 | 41913c7 | 2013-04-16 15:41:18 +0000 | [diff] [blame] | 68 | StructSize += DL.getTypeAllocSize(Ty); // Consume space for this data item |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 71 | // Add padding to the end of the struct so that it could be put in an array |
| 72 | // and all array elements would be aligned correctly. |
Guillaume Chatelet | 6c127cd | 2019-09-20 13:40:31 +0000 | [diff] [blame] | 73 | if (!isAligned(StructAlignment, StructSize)) { |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 74 | IsPadded = true; |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 75 | StructSize = alignTo(StructSize, StructAlignment); |
Mehdi Amini | 1c131b3 | 2015-12-15 01:44:07 +0000 | [diff] [blame] | 76 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 79 | /// getElementContainingOffset - Given a valid offset into the structure, |
| 80 | /// return the structure index that contains it. |
| 81 | unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const { |
| 82 | const uint64_t *SI = |
| 83 | std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset); |
| 84 | assert(SI != &MemberOffsets[0] && "Offset not in structure type!"); |
| 85 | --SI; |
| 86 | assert(*SI <= Offset && "upper_bound didn't work"); |
| 87 | assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) && |
| 88 | (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) && |
| 89 | "Upper bound didn't work!"); |
| 90 | |
| 91 | // Multiple fields can have the same offset if any of them are zero sized. |
| 92 | // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop |
| 93 | // at the i32 element, because it is the last element at that offset. This is |
| 94 | // the right one to return, because anything after it will have a higher |
| 95 | // offset, implying that this element is non-empty. |
| 96 | return SI-&MemberOffsets[0]; |
| 97 | } |
| 98 | |
| 99 | //===----------------------------------------------------------------------===// |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 100 | // LayoutAlignElem, LayoutAlign support |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 101 | //===----------------------------------------------------------------------===// |
| 102 | |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 103 | LayoutAlignElem LayoutAlignElem::get(AlignTypeEnum align_type, Align abi_align, |
| 104 | Align pref_align, uint32_t bit_width) { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 105 | assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 106 | LayoutAlignElem retval; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 107 | retval.AlignType = align_type; |
| 108 | retval.ABIAlign = abi_align; |
| 109 | retval.PrefAlign = pref_align; |
| 110 | retval.TypeBitWidth = bit_width; |
| 111 | return retval; |
| 112 | } |
| 113 | |
| 114 | bool |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 115 | LayoutAlignElem::operator==(const LayoutAlignElem &rhs) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 116 | return (AlignType == rhs.AlignType |
| 117 | && ABIAlign == rhs.ABIAlign |
| 118 | && PrefAlign == rhs.PrefAlign |
| 119 | && TypeBitWidth == rhs.TypeBitWidth); |
| 120 | } |
| 121 | |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 122 | //===----------------------------------------------------------------------===// |
| 123 | // PointerAlignElem, PointerAlign support |
| 124 | //===----------------------------------------------------------------------===// |
| 125 | |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 126 | PointerAlignElem PointerAlignElem::get(uint32_t AddressSpace, Align ABIAlign, |
| 127 | Align PrefAlign, uint32_t TypeByteWidth, |
Guillaume Chatelet | 6c127cd | 2019-09-20 13:40:31 +0000 | [diff] [blame] | 128 | uint32_t IndexWidth) { |
Rafael Espindola | f39136c | 2013-12-13 23:15:20 +0000 | [diff] [blame] | 129 | assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!"); |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 130 | PointerAlignElem retval; |
Rafael Espindola | f39136c | 2013-12-13 23:15:20 +0000 | [diff] [blame] | 131 | retval.AddressSpace = AddressSpace; |
| 132 | retval.ABIAlign = ABIAlign; |
| 133 | retval.PrefAlign = PrefAlign; |
| 134 | retval.TypeByteWidth = TypeByteWidth; |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 135 | retval.IndexWidth = IndexWidth; |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 136 | return retval; |
| 137 | } |
| 138 | |
| 139 | bool |
| 140 | PointerAlignElem::operator==(const PointerAlignElem &rhs) const { |
| 141 | return (ABIAlign == rhs.ABIAlign |
| 142 | && AddressSpace == rhs.AddressSpace |
| 143 | && PrefAlign == rhs.PrefAlign |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 144 | && TypeByteWidth == rhs.TypeByteWidth |
| 145 | && IndexWidth == rhs.IndexWidth); |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 148 | //===----------------------------------------------------------------------===// |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 149 | // DataLayout Class Implementation |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 150 | //===----------------------------------------------------------------------===// |
| 151 | |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 152 | const char *DataLayout::getManglingComponent(const Triple &T) { |
| 153 | if (T.isOSBinFormatMachO()) |
| 154 | return "-m:o"; |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 155 | if (T.isOSWindows() && T.isOSBinFormatCOFF()) |
| 156 | return T.getArch() == Triple::x86 ? "-m:x" : "-m:w"; |
jasonliu | 572dde5 | 2020-07-02 22:45:59 +0000 | [diff] [blame] | 157 | if (T.isOSBinFormatXCOFF()) |
| 158 | return "-m:a"; |
Saleem Abdulrasool | cd13082 | 2014-04-02 20:32:05 +0000 | [diff] [blame] | 159 | return "-m:e"; |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Rafael Espindola | e23b877 | 2013-12-20 15:21:32 +0000 | [diff] [blame] | 162 | static const LayoutAlignElem DefaultAlignments[] = { |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 163 | {INTEGER_ALIGN, 1, Align(1), Align(1)}, // i1 |
| 164 | {INTEGER_ALIGN, 8, Align(1), Align(1)}, // i8 |
| 165 | {INTEGER_ALIGN, 16, Align(2), Align(2)}, // i16 |
| 166 | {INTEGER_ALIGN, 32, Align(4), Align(4)}, // i32 |
| 167 | {INTEGER_ALIGN, 64, Align(4), Align(8)}, // i64 |
Ties Stuij | 8c24f33 | 2020-03-31 23:49:38 +0100 | [diff] [blame] | 168 | {FLOAT_ALIGN, 16, Align(2), Align(2)}, // half, bfloat |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 169 | {FLOAT_ALIGN, 32, Align(4), Align(4)}, // float |
| 170 | {FLOAT_ALIGN, 64, Align(8), Align(8)}, // double |
| 171 | {FLOAT_ALIGN, 128, Align(16), Align(16)}, // ppcf128, quad, ... |
| 172 | {VECTOR_ALIGN, 64, Align(8), Align(8)}, // v2i32, v1i64, ... |
| 173 | {VECTOR_ALIGN, 128, Align(16), Align(16)}, // v16i8, v8i16, v4i32, ... |
| 174 | {AGGREGATE_ALIGN, 0, Align(1), Align(8)} // struct |
Rafael Espindola | 458a485 | 2013-12-19 23:03:03 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 177 | void DataLayout::reset(StringRef Desc) { |
| 178 | clear(); |
| 179 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 180 | LayoutMap = nullptr; |
Chandler Carruth | f67321c | 2014-10-20 10:41:29 +0000 | [diff] [blame] | 181 | BigEndian = false; |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 182 | AllocaAddrSpace = 0; |
Guillaume Chatelet | 65e4b47 | 2019-08-05 09:00:43 +0000 | [diff] [blame] | 183 | StackNaturalAlign.reset(); |
Dylan McKay | ced2fe6 | 2018-02-19 09:56:22 +0000 | [diff] [blame] | 184 | ProgramAddrSpace = 0; |
Guillaume Chatelet | 65e4b47 | 2019-08-05 09:00:43 +0000 | [diff] [blame] | 185 | FunctionPtrAlign.reset(); |
Michael Platings | 308e82e | 2019-03-08 10:44:06 +0000 | [diff] [blame] | 186 | TheFunctionPtrAlignType = FunctionPtrAlignType::Independent; |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 187 | ManglingMode = MM_None; |
Sanjoy Das | c6af5ea | 2016-07-28 23:43:38 +0000 | [diff] [blame] | 188 | NonIntegralAddressSpaces.clear(); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 189 | |
| 190 | // Default alignments |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 191 | for (const LayoutAlignElem &E : DefaultAlignments) { |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 192 | if (Error Err = setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, |
| 193 | E.PrefAlign, E.TypeBitWidth)) |
| 194 | return report_fatal_error(std::move(Err)); |
Rafael Espindola | 458a485 | 2013-12-19 23:03:03 +0000 | [diff] [blame] | 195 | } |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 196 | if (Error Err = setPointerAlignment(0, Align(8), Align(8), 8, 8)) |
| 197 | return report_fatal_error(std::move(Err)); |
Patrik Hägglund | 01860a6 | 2012-11-14 09:04:56 +0000 | [diff] [blame] | 198 | |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 199 | if (Error Err = parseSpecifier(Desc)) |
| 200 | return report_fatal_error(std::move(Err)); |
| 201 | } |
| 202 | |
| 203 | Expected<DataLayout> DataLayout::parse(StringRef LayoutDescription) { |
| 204 | DataLayout Layout(""); |
| 205 | if (Error Err = Layout.parseSpecifier(LayoutDescription)) |
| 206 | return std::move(Err); |
| 207 | return Layout; |
| 208 | } |
| 209 | |
| 210 | static Error reportError(const Twine &Message) { |
| 211 | return createStringError(inconvertibleErrorCode(), Message); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /// Checked version of split, to ensure mandatory subparts. |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 215 | static Error split(StringRef Str, char Separator, |
| 216 | std::pair<StringRef, StringRef> &Split) { |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 217 | assert(!Str.empty() && "parse error, string can't be empty here"); |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 218 | Split = Str.split(Separator); |
David Majnemer | 2dc1b0f | 2014-12-10 01:38:28 +0000 | [diff] [blame] | 219 | if (Split.second.empty() && Split.first != Str) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 220 | return reportError("Trailing separator in datalayout string"); |
David Majnemer | 612f312 | 2014-12-10 02:36:41 +0000 | [diff] [blame] | 221 | if (!Split.second.empty() && Split.first.empty()) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 222 | return reportError("Expected token before separator in datalayout string"); |
| 223 | return Error::success(); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Cameron McInally | 8af9eac | 2014-01-07 19:51:38 +0000 | [diff] [blame] | 226 | /// Get an unsigned integer, including error checks. |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 227 | template <typename IntTy> static Error getInt(StringRef R, IntTy &Result) { |
Patrik Hägglund | 504f478 | 2012-11-28 14:32:52 +0000 | [diff] [blame] | 228 | bool error = R.getAsInteger(10, Result); (void)error; |
Cameron McInally | f0379fa | 2014-01-13 22:04:55 +0000 | [diff] [blame] | 229 | if (error) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 230 | return reportError("not a number, or does not fit in an unsigned int"); |
| 231 | return Error::success(); |
Patrik Hägglund | 3eb16c5 | 2012-11-28 12:13:12 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 234 | /// Get an unsigned integer representing the number of bits and convert it into |
| 235 | /// bytes. Error out of not a byte width multiple. |
| 236 | template <typename IntTy> |
| 237 | static Error getIntInBytes(StringRef R, IntTy &Result) { |
| 238 | if (Error Err = getInt<IntTy>(R, Result)) |
| 239 | return Err; |
| 240 | if (Result % 8) |
| 241 | return reportError("number of bits must be a byte width multiple"); |
| 242 | Result /= 8; |
| 243 | return Error::success(); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 246 | static Error getAddrSpace(StringRef R, unsigned &AddrSpace) { |
| 247 | if (Error Err = getInt(R, AddrSpace)) |
| 248 | return Err; |
Dylan McKay | ced2fe6 | 2018-02-19 09:56:22 +0000 | [diff] [blame] | 249 | if (!isUInt<24>(AddrSpace)) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 250 | return reportError("Invalid address space, must be a 24-bit integer"); |
| 251 | return Error::success(); |
Dylan McKay | ced2fe6 | 2018-02-19 09:56:22 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 254 | Error DataLayout::parseSpecifier(StringRef Desc) { |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 255 | StringRepresentation = std::string(Desc); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 256 | while (!Desc.empty()) { |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 257 | // Split at '-'. |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 258 | std::pair<StringRef, StringRef> Split; |
| 259 | if (Error Err = split(Desc, '-', Split)) |
| 260 | return Err; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 261 | Desc = Split.second; |
| 262 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 263 | // Split at ':'. |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 264 | if (Error Err = split(Split.first, ':', Split)) |
| 265 | return Err; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 266 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 267 | // Aliases used below. |
| 268 | StringRef &Tok = Split.first; // Current token. |
| 269 | StringRef &Rest = Split.second; // The rest of the string. |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 270 | |
Sanjoy Das | c6af5ea | 2016-07-28 23:43:38 +0000 | [diff] [blame] | 271 | if (Tok == "ni") { |
| 272 | do { |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 273 | if (Error Err = split(Rest, ':', Split)) |
| 274 | return Err; |
Sanjoy Das | c6af5ea | 2016-07-28 23:43:38 +0000 | [diff] [blame] | 275 | Rest = Split.second; |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 276 | unsigned AS; |
| 277 | if (Error Err = getInt(Split.first, AS)) |
| 278 | return Err; |
Sanjoy Das | c6af5ea | 2016-07-28 23:43:38 +0000 | [diff] [blame] | 279 | if (AS == 0) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 280 | return reportError("Address space 0 can never be non-integral"); |
Sanjoy Das | c6af5ea | 2016-07-28 23:43:38 +0000 | [diff] [blame] | 281 | NonIntegralAddressSpaces.push_back(AS); |
| 282 | } while (!Rest.empty()); |
| 283 | |
| 284 | continue; |
| 285 | } |
| 286 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 287 | char Specifier = Tok.front(); |
| 288 | Tok = Tok.substr(1); |
| 289 | |
| 290 | switch (Specifier) { |
Rafael Espindola | 6994fdf | 2014-01-01 22:29:43 +0000 | [diff] [blame] | 291 | case 's': |
Mehdi Amini | 4abf024 | 2020-06-25 23:49:07 +0000 | [diff] [blame] | 292 | // Deprecated, but ignoring here to preserve loading older textual llvm |
| 293 | // ASM file |
Rafael Espindola | 6994fdf | 2014-01-01 22:29:43 +0000 | [diff] [blame] | 294 | break; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 295 | case 'E': |
Chandler Carruth | f67321c | 2014-10-20 10:41:29 +0000 | [diff] [blame] | 296 | BigEndian = true; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 297 | break; |
| 298 | case 'e': |
Chandler Carruth | f67321c | 2014-10-20 10:41:29 +0000 | [diff] [blame] | 299 | BigEndian = false; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 300 | break; |
| 301 | case 'p': { |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 302 | // Address space. |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 303 | unsigned AddrSpace = 0; |
| 304 | if (!Tok.empty()) |
| 305 | if (Error Err = getInt(Tok, AddrSpace)) |
| 306 | return Err; |
David Majnemer | 5330c69 | 2014-12-10 01:17:08 +0000 | [diff] [blame] | 307 | if (!isUInt<24>(AddrSpace)) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 308 | return reportError("Invalid address space, must be a 24bit integer"); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 309 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 310 | // Size. |
David Majnemer | 2dc1b0f | 2014-12-10 01:38:28 +0000 | [diff] [blame] | 311 | if (Rest.empty()) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 312 | return reportError( |
David Majnemer | 2dc1b0f | 2014-12-10 01:38:28 +0000 | [diff] [blame] | 313 | "Missing size specification for pointer in datalayout string"); |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 314 | if (Error Err = split(Rest, ':', Split)) |
| 315 | return Err; |
| 316 | unsigned PointerMemSize; |
| 317 | if (Error Err = getIntInBytes(Tok, PointerMemSize)) |
| 318 | return Err; |
Owen Anderson | 5bc2bbe | 2015-03-02 06:00:02 +0000 | [diff] [blame] | 319 | if (!PointerMemSize) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 320 | return reportError("Invalid pointer size of 0 bytes"); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 321 | |
| 322 | // ABI alignment. |
David Majnemer | 2dc1b0f | 2014-12-10 01:38:28 +0000 | [diff] [blame] | 323 | if (Rest.empty()) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 324 | return reportError( |
David Majnemer | 2dc1b0f | 2014-12-10 01:38:28 +0000 | [diff] [blame] | 325 | "Missing alignment specification for pointer in datalayout string"); |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 326 | if (Error Err = split(Rest, ':', Split)) |
| 327 | return Err; |
| 328 | unsigned PointerABIAlign; |
| 329 | if (Error Err = getIntInBytes(Tok, PointerABIAlign)) |
| 330 | return Err; |
Owen Anderson | 040f2f8 | 2015-03-02 06:33:51 +0000 | [diff] [blame] | 331 | if (!isPowerOf2_64(PointerABIAlign)) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 332 | return reportError("Pointer ABI alignment must be a power of 2"); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 333 | |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 334 | // Size of index used in GEP for address calculation. |
| 335 | // The parameter is optional. By default it is equal to size of pointer. |
| 336 | unsigned IndexSize = PointerMemSize; |
| 337 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 338 | // Preferred alignment. |
| 339 | unsigned PointerPrefAlign = PointerABIAlign; |
| 340 | if (!Rest.empty()) { |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 341 | if (Error Err = split(Rest, ':', Split)) |
| 342 | return Err; |
| 343 | if (Error Err = getIntInBytes(Tok, PointerPrefAlign)) |
| 344 | return Err; |
Owen Anderson | 040f2f8 | 2015-03-02 06:33:51 +0000 | [diff] [blame] | 345 | if (!isPowerOf2_64(PointerPrefAlign)) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 346 | return reportError( |
| 347 | "Pointer preferred alignment must be a power of 2"); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 348 | |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 349 | // Now read the index. It is the second optional parameter here. |
| 350 | if (!Rest.empty()) { |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 351 | if (Error Err = split(Rest, ':', Split)) |
| 352 | return Err; |
| 353 | if (Error Err = getIntInBytes(Tok, IndexSize)) |
| 354 | return Err; |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 355 | if (!IndexSize) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 356 | return reportError("Invalid index size of 0 bytes"); |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 359 | if (Error Err = setPointerAlignment( |
| 360 | AddrSpace, assumeAligned(PointerABIAlign), |
| 361 | assumeAligned(PointerPrefAlign), PointerMemSize, IndexSize)) |
| 362 | return Err; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 363 | break; |
| 364 | } |
| 365 | case 'i': |
| 366 | case 'v': |
| 367 | case 'f': |
Rafael Espindola | 6994fdf | 2014-01-01 22:29:43 +0000 | [diff] [blame] | 368 | case 'a': { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 369 | AlignTypeEnum AlignType; |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 370 | switch (Specifier) { |
Craig Topper | 64a65ec | 2017-05-22 19:28:36 +0000 | [diff] [blame] | 371 | default: llvm_unreachable("Unexpected specifier!"); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 372 | case 'i': AlignType = INTEGER_ALIGN; break; |
| 373 | case 'v': AlignType = VECTOR_ALIGN; break; |
| 374 | case 'f': AlignType = FLOAT_ALIGN; break; |
| 375 | case 'a': AlignType = AGGREGATE_ALIGN; break; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 376 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 377 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 378 | // Bit size. |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 379 | unsigned Size = 0; |
| 380 | if (!Tok.empty()) |
| 381 | if (Error Err = getInt(Tok, Size)) |
| 382 | return Err; |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 383 | |
David Majnemer | 5330c69 | 2014-12-10 01:17:08 +0000 | [diff] [blame] | 384 | if (AlignType == AGGREGATE_ALIGN && Size != 0) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 385 | return reportError( |
David Majnemer | 5330c69 | 2014-12-10 01:17:08 +0000 | [diff] [blame] | 386 | "Sized aggregate specification in datalayout string"); |
Rafael Espindola | abdd726 | 2014-01-06 21:40:24 +0000 | [diff] [blame] | 387 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 388 | // ABI alignment. |
David Majnemer | 612f312 | 2014-12-10 02:36:41 +0000 | [diff] [blame] | 389 | if (Rest.empty()) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 390 | return reportError( |
David Majnemer | 612f312 | 2014-12-10 02:36:41 +0000 | [diff] [blame] | 391 | "Missing alignment specification in datalayout string"); |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 392 | if (Error Err = split(Rest, ':', Split)) |
| 393 | return Err; |
| 394 | unsigned ABIAlign; |
| 395 | if (Error Err = getIntInBytes(Tok, ABIAlign)) |
| 396 | return Err; |
Owen Anderson | ab1c7a7 | 2015-03-02 09:34:59 +0000 | [diff] [blame] | 397 | if (AlignType != AGGREGATE_ALIGN && !ABIAlign) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 398 | return reportError( |
Owen Anderson | ab1c7a7 | 2015-03-02 09:34:59 +0000 | [diff] [blame] | 399 | "ABI alignment specification must be >0 for non-aggregate types"); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 400 | |
Guillaume Chatelet | 6c127cd | 2019-09-20 13:40:31 +0000 | [diff] [blame] | 401 | if (!isUInt<16>(ABIAlign)) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 402 | return reportError("Invalid ABI alignment, must be a 16bit integer"); |
Guillaume Chatelet | 6c127cd | 2019-09-20 13:40:31 +0000 | [diff] [blame] | 403 | if (ABIAlign != 0 && !isPowerOf2_64(ABIAlign)) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 404 | return reportError("Invalid ABI alignment, must be a power of 2"); |
Guillaume Chatelet | 6c127cd | 2019-09-20 13:40:31 +0000 | [diff] [blame] | 405 | |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 406 | // Preferred alignment. |
| 407 | unsigned PrefAlign = ABIAlign; |
| 408 | if (!Rest.empty()) { |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 409 | if (Error Err = split(Rest, ':', Split)) |
| 410 | return Err; |
| 411 | if (Error Err = getIntInBytes(Tok, PrefAlign)) |
| 412 | return Err; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 413 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 414 | |
Guillaume Chatelet | 6c127cd | 2019-09-20 13:40:31 +0000 | [diff] [blame] | 415 | if (!isUInt<16>(PrefAlign)) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 416 | return reportError( |
Guillaume Chatelet | 6c127cd | 2019-09-20 13:40:31 +0000 | [diff] [blame] | 417 | "Invalid preferred alignment, must be a 16bit integer"); |
| 418 | if (PrefAlign != 0 && !isPowerOf2_64(PrefAlign)) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 419 | return reportError("Invalid preferred alignment, must be a power of 2"); |
Guillaume Chatelet | 6c127cd | 2019-09-20 13:40:31 +0000 | [diff] [blame] | 420 | |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 421 | if (Error Err = setAlignment(AlignType, assumeAligned(ABIAlign), |
| 422 | assumeAligned(PrefAlign), Size)) |
| 423 | return Err; |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 424 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 425 | break; |
| 426 | } |
| 427 | case 'n': // Native integer types. |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 428 | while (true) { |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 429 | unsigned Width; |
| 430 | if (Error Err = getInt(Tok, Width)) |
| 431 | return Err; |
David Majnemer | 5330c69 | 2014-12-10 01:17:08 +0000 | [diff] [blame] | 432 | if (Width == 0) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 433 | return reportError( |
David Majnemer | 5330c69 | 2014-12-10 01:17:08 +0000 | [diff] [blame] | 434 | "Zero width native integer type in datalayout string"); |
Patrik Hägglund | 3eb16c5 | 2012-11-28 12:13:12 +0000 | [diff] [blame] | 435 | LegalIntWidths.push_back(Width); |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 436 | if (Rest.empty()) |
| 437 | break; |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 438 | if (Error Err = split(Rest, ':', Split)) |
| 439 | return Err; |
Patrik Hagglund | 086ee1e | 2012-11-30 10:06:59 +0000 | [diff] [blame] | 440 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 441 | break; |
| 442 | case 'S': { // Stack natural alignment. |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 443 | uint64_t Alignment; |
| 444 | if (Error Err = getIntInBytes(Tok, Alignment)) |
| 445 | return Err; |
Florian Hahn | d8c3c17 | 2019-08-07 17:20:55 +0000 | [diff] [blame] | 446 | if (Alignment != 0 && !llvm::isPowerOf2_64(Alignment)) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 447 | return reportError("Alignment is neither 0 nor a power of 2"); |
Florian Hahn | d8c3c17 | 2019-08-07 17:20:55 +0000 | [diff] [blame] | 448 | StackNaturalAlign = MaybeAlign(Alignment); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 449 | break; |
| 450 | } |
Michael Platings | 308e82e | 2019-03-08 10:44:06 +0000 | [diff] [blame] | 451 | case 'F': { |
| 452 | switch (Tok.front()) { |
| 453 | case 'i': |
| 454 | TheFunctionPtrAlignType = FunctionPtrAlignType::Independent; |
| 455 | break; |
| 456 | case 'n': |
| 457 | TheFunctionPtrAlignType = FunctionPtrAlignType::MultipleOfFunctionAlign; |
| 458 | break; |
| 459 | default: |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 460 | return reportError("Unknown function pointer alignment type in " |
Michael Platings | 308e82e | 2019-03-08 10:44:06 +0000 | [diff] [blame] | 461 | "datalayout string"); |
| 462 | } |
| 463 | Tok = Tok.substr(1); |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 464 | uint64_t Alignment; |
| 465 | if (Error Err = getIntInBytes(Tok, Alignment)) |
| 466 | return Err; |
Florian Hahn | d8c3c17 | 2019-08-07 17:20:55 +0000 | [diff] [blame] | 467 | if (Alignment != 0 && !llvm::isPowerOf2_64(Alignment)) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 468 | return reportError("Alignment is neither 0 nor a power of 2"); |
Florian Hahn | d8c3c17 | 2019-08-07 17:20:55 +0000 | [diff] [blame] | 469 | FunctionPtrAlign = MaybeAlign(Alignment); |
Michael Platings | 308e82e | 2019-03-08 10:44:06 +0000 | [diff] [blame] | 470 | break; |
| 471 | } |
Dylan McKay | ced2fe6 | 2018-02-19 09:56:22 +0000 | [diff] [blame] | 472 | case 'P': { // Function address space. |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 473 | if (Error Err = getAddrSpace(Tok, ProgramAddrSpace)) |
| 474 | return Err; |
Dylan McKay | ced2fe6 | 2018-02-19 09:56:22 +0000 | [diff] [blame] | 475 | break; |
| 476 | } |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 477 | case 'A': { // Default stack/alloca address space. |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 478 | if (Error Err = getAddrSpace(Tok, AllocaAddrSpace)) |
| 479 | return Err; |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 480 | break; |
| 481 | } |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 482 | case 'm': |
David Majnemer | 612f312 | 2014-12-10 02:36:41 +0000 | [diff] [blame] | 483 | if (!Tok.empty()) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 484 | return reportError("Unexpected trailing characters after mangling " |
| 485 | "specifier in datalayout string"); |
David Majnemer | 612f312 | 2014-12-10 02:36:41 +0000 | [diff] [blame] | 486 | if (Rest.empty()) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 487 | return reportError("Expected mangling specifier in datalayout string"); |
David Majnemer | 612f312 | 2014-12-10 02:36:41 +0000 | [diff] [blame] | 488 | if (Rest.size() > 1) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 489 | return reportError("Unknown mangling specifier in datalayout string"); |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 490 | switch(Rest[0]) { |
| 491 | default: |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 492 | return reportError("Unknown mangling in datalayout string"); |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 493 | case 'e': |
| 494 | ManglingMode = MM_ELF; |
| 495 | break; |
| 496 | case 'o': |
| 497 | ManglingMode = MM_MachO; |
| 498 | break; |
| 499 | case 'm': |
| 500 | ManglingMode = MM_Mips; |
| 501 | break; |
Rafael Espindola | af77e12 | 2014-01-10 13:42:12 +0000 | [diff] [blame] | 502 | case 'w': |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 503 | ManglingMode = MM_WinCOFF; |
| 504 | break; |
| 505 | case 'x': |
| 506 | ManglingMode = MM_WinCOFFX86; |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 507 | break; |
jasonliu | 572dde5 | 2020-07-02 22:45:59 +0000 | [diff] [blame] | 508 | case 'a': |
| 509 | ManglingMode = MM_XCOFF; |
| 510 | break; |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 511 | } |
| 512 | break; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 513 | default: |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 514 | return reportError("Unknown specifier in datalayout string"); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 515 | break; |
| 516 | } |
| 517 | } |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 518 | |
| 519 | return Error::success(); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 522 | DataLayout::DataLayout(const Module *M) { |
Rafael Espindola | c435adc | 2014-09-10 21:27:43 +0000 | [diff] [blame] | 523 | init(M); |
| 524 | } |
| 525 | |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 526 | void DataLayout::init(const Module *M) { *this = M->getDataLayout(); } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 527 | |
Rafael Espindola | ae593f1 | 2014-02-26 17:02:08 +0000 | [diff] [blame] | 528 | bool DataLayout::operator==(const DataLayout &Other) const { |
Chandler Carruth | f67321c | 2014-10-20 10:41:29 +0000 | [diff] [blame] | 529 | bool Ret = BigEndian == Other.BigEndian && |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 530 | AllocaAddrSpace == Other.AllocaAddrSpace && |
Rafael Espindola | ae593f1 | 2014-02-26 17:02:08 +0000 | [diff] [blame] | 531 | StackNaturalAlign == Other.StackNaturalAlign && |
Dylan McKay | ced2fe6 | 2018-02-19 09:56:22 +0000 | [diff] [blame] | 532 | ProgramAddrSpace == Other.ProgramAddrSpace && |
Michael Platings | 308e82e | 2019-03-08 10:44:06 +0000 | [diff] [blame] | 533 | FunctionPtrAlign == Other.FunctionPtrAlign && |
| 534 | TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType && |
Rafael Espindola | ae593f1 | 2014-02-26 17:02:08 +0000 | [diff] [blame] | 535 | ManglingMode == Other.ManglingMode && |
| 536 | LegalIntWidths == Other.LegalIntWidths && |
Rafael Espindola | 89992b0 | 2014-04-22 17:47:03 +0000 | [diff] [blame] | 537 | Alignments == Other.Alignments && Pointers == Other.Pointers; |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 538 | // Note: getStringRepresentation() might differs, it is not canonicalized |
Rafael Espindola | ae593f1 | 2014-02-26 17:02:08 +0000 | [diff] [blame] | 539 | return Ret; |
| 540 | } |
| 541 | |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 542 | DataLayout::AlignmentsTy::iterator |
| 543 | DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType, |
| 544 | uint32_t BitWidth) { |
| 545 | auto Pair = std::make_pair((unsigned)AlignType, BitWidth); |
Fangrui Song | 78ee2fb | 2019-06-30 11:19:56 +0000 | [diff] [blame] | 546 | return partition_point(Alignments, [=](const LayoutAlignElem &E) { |
| 547 | return std::make_pair(E.AlignType, E.TypeBitWidth) < Pair; |
Fangrui Song | dc8de60 | 2019-06-21 05:40:31 +0000 | [diff] [blame] | 548 | }); |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 551 | Error DataLayout::setAlignment(AlignTypeEnum align_type, Align abi_align, |
| 552 | Align pref_align, uint32_t bit_width) { |
Guillaume Chatelet | 6c127cd | 2019-09-20 13:40:31 +0000 | [diff] [blame] | 553 | // AlignmentsTy::ABIAlign and AlignmentsTy::PrefAlign were once stored as |
| 554 | // uint16_t, it is unclear if there are requirements for alignment to be less |
| 555 | // than 2^16 other than storage. In the meantime we leave the restriction as |
| 556 | // an assert. See D67400 for context. |
| 557 | assert(Log2(abi_align) < 16 && Log2(pref_align) < 16 && "Alignment too big"); |
David Majnemer | 1b9fc3a | 2015-02-16 05:41:53 +0000 | [diff] [blame] | 558 | if (!isUInt<24>(bit_width)) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 559 | return reportError("Invalid bit width, must be a 24bit integer"); |
David Majnemer | 1b9fc3a | 2015-02-16 05:41:53 +0000 | [diff] [blame] | 560 | if (pref_align < abi_align) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 561 | return reportError( |
David Majnemer | 1b9fc3a | 2015-02-16 05:41:53 +0000 | [diff] [blame] | 562 | "Preferred alignment cannot be less than the ABI alignment"); |
| 563 | |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 564 | AlignmentsTy::iterator I = findAlignmentLowerBound(align_type, bit_width); |
| 565 | if (I != Alignments.end() && |
| 566 | I->AlignType == (unsigned)align_type && I->TypeBitWidth == bit_width) { |
| 567 | // Update the abi, preferred alignments. |
| 568 | I->ABIAlign = abi_align; |
| 569 | I->PrefAlign = pref_align; |
| 570 | } else { |
| 571 | // Insert before I to keep the vector sorted. |
| 572 | Alignments.insert(I, LayoutAlignElem::get(align_type, abi_align, |
| 573 | pref_align, bit_width)); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 574 | } |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 575 | return Error::success(); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 578 | DataLayout::PointersTy::iterator |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 579 | DataLayout::findPointerLowerBound(uint32_t AddressSpace) { |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 580 | return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace, |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 581 | [](const PointerAlignElem &A, uint32_t AddressSpace) { |
| 582 | return A.AddressSpace < AddressSpace; |
| 583 | }); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 586 | Error DataLayout::setPointerAlignment(uint32_t AddrSpace, Align ABIAlign, |
| 587 | Align PrefAlign, uint32_t TypeByteWidth, |
| 588 | uint32_t IndexWidth) { |
David Majnemer | 4b04292 | 2015-02-16 05:41:55 +0000 | [diff] [blame] | 589 | if (PrefAlign < ABIAlign) |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 590 | return reportError( |
David Majnemer | 4b04292 | 2015-02-16 05:41:55 +0000 | [diff] [blame] | 591 | "Preferred alignment cannot be less than the ABI alignment"); |
| 592 | |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 593 | PointersTy::iterator I = findPointerLowerBound(AddrSpace); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 594 | if (I == Pointers.end() || I->AddressSpace != AddrSpace) { |
| 595 | Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign, |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 596 | TypeByteWidth, IndexWidth)); |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 597 | } else { |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 598 | I->ABIAlign = ABIAlign; |
| 599 | I->PrefAlign = PrefAlign; |
| 600 | I->TypeByteWidth = TypeByteWidth; |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 601 | I->IndexWidth = IndexWidth; |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 602 | } |
Alex Zinenko | 874aef8 | 2020-08-17 13:34:07 +0200 | [diff] [blame] | 603 | return Error::success(); |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 606 | /// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 607 | /// preferred if ABIInfo = false) the layout wants for the specified datatype. |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 608 | Align DataLayout::getAlignmentInfo(AlignTypeEnum AlignType, uint32_t BitWidth, |
| 609 | bool ABIInfo, Type *Ty) const { |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 610 | AlignmentsTy::const_iterator I = findAlignmentLowerBound(AlignType, BitWidth); |
| 611 | // See if we found an exact match. Of if we are looking for an integer type, |
| 612 | // but don't have an exact match take the next largest integer. This is where |
| 613 | // the lower_bound will point to when it fails an exact match. |
| 614 | if (I != Alignments.end() && I->AlignType == (unsigned)AlignType && |
| 615 | (I->TypeBitWidth == BitWidth || AlignType == INTEGER_ALIGN)) |
Guillaume Chatelet | 046a16b | 2019-09-23 08:38:36 +0000 | [diff] [blame] | 616 | return ABIInfo ? I->ABIAlign : I->PrefAlign; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 617 | |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 618 | if (AlignType == INTEGER_ALIGN) { |
| 619 | // If we didn't have a larger value try the largest value we have. |
| 620 | if (I != Alignments.begin()) { |
| 621 | --I; // Go to the previous entry and see if its an integer. |
| 622 | if (I->AlignType == INTEGER_ALIGN) |
Guillaume Chatelet | 046a16b | 2019-09-23 08:38:36 +0000 | [diff] [blame] | 623 | return ABIInfo ? I->ABIAlign : I->PrefAlign; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 624 | } |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 625 | } else if (AlignType == VECTOR_ALIGN) { |
| 626 | // By default, use natural alignment for vector types. This is consistent |
| 627 | // with what clang and llvm-gcc do. |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 628 | unsigned Alignment = |
| 629 | getTypeAllocSize(cast<VectorType>(Ty)->getElementType()); |
David Sherwood | a400aa5 | 2020-05-05 16:47:31 +0100 | [diff] [blame] | 630 | // We're only calculating a natural alignment, so it doesn't have to be |
| 631 | // based on the full size for scalable vectors. Using the minimum element |
| 632 | // count should be enough here. |
David Sherwood | f4257c5 | 2020-08-14 12:15:59 +0100 | [diff] [blame] | 633 | Alignment *= cast<VectorType>(Ty)->getElementCount().getKnownMinValue(); |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 634 | Alignment = PowerOf2Ceil(Alignment); |
| 635 | return Align(Alignment); |
Craig Topper | 490889c | 2017-03-23 06:15:56 +0000 | [diff] [blame] | 636 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 637 | |
Owen Anderson | 7e621e9 | 2015-03-08 21:53:59 +0000 | [diff] [blame] | 638 | // If we still couldn't find a reasonable default alignment, fall back |
| 639 | // to a simple heuristic that the alignment is the first power of two |
| 640 | // greater-or-equal to the store size of the type. This is a reasonable |
| 641 | // approximation of reality, and if the user wanted something less |
| 642 | // less conservative, they should have specified it explicitly in the data |
| 643 | // layout. |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 644 | unsigned Alignment = getTypeStoreSize(Ty); |
| 645 | Alignment = PowerOf2Ceil(Alignment); |
| 646 | return Align(Alignment); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | namespace { |
| 650 | |
| 651 | class StructLayoutMap { |
Eugene Zelenko | f53a7b4 | 2017-05-05 22:30:37 +0000 | [diff] [blame] | 652 | using LayoutInfoTy = DenseMap<StructType*, StructLayout*>; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 653 | LayoutInfoTy LayoutInfo; |
| 654 | |
| 655 | public: |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 656 | ~StructLayoutMap() { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 657 | // Remove any layouts. |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 658 | for (const auto &I : LayoutInfo) { |
| 659 | StructLayout *Value = I.second; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 660 | Value->~StructLayout(); |
| 661 | free(Value); |
| 662 | } |
| 663 | } |
| 664 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 665 | StructLayout *&operator[](StructType *STy) { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 666 | return LayoutInfo[STy]; |
| 667 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 668 | }; |
| 669 | |
| 670 | } // end anonymous namespace |
| 671 | |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 672 | void DataLayout::clear() { |
| 673 | LegalIntWidths.clear(); |
| 674 | Alignments.clear(); |
| 675 | Pointers.clear(); |
| 676 | delete static_cast<StructLayoutMap *>(LayoutMap); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 677 | LayoutMap = nullptr; |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Micah Villmow | b4faa15 | 2012-10-04 23:01:22 +0000 | [diff] [blame] | 680 | DataLayout::~DataLayout() { |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 681 | clear(); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 684 | const StructLayout *DataLayout::getStructLayout(StructType *Ty) const { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 685 | if (!LayoutMap) |
| 686 | LayoutMap = new StructLayoutMap(); |
| 687 | |
| 688 | StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap); |
| 689 | StructLayout *&SL = (*STM)[Ty]; |
| 690 | if (SL) return SL; |
| 691 | |
| 692 | // Otherwise, create the struct layout. Because it is variable length, we |
| 693 | // malloc it, then use placement new. |
| 694 | int NumElts = Ty->getNumElements(); |
Serge Pavlov | 15681ad | 2018-06-09 05:19:45 +0000 | [diff] [blame] | 695 | StructLayout *L = (StructLayout *) |
| 696 | safe_malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t)); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 697 | |
| 698 | // Set SL before calling StructLayout's ctor. The ctor could cause other |
| 699 | // entries to be added to TheMap, invalidating our reference. |
| 700 | SL = L; |
| 701 | |
| 702 | new (L) StructLayout(Ty, *this); |
| 703 | |
| 704 | return L; |
| 705 | } |
| 706 | |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 707 | Align DataLayout::getPointerABIAlignment(unsigned AS) const { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 708 | PointersTy::const_iterator I = findPointerLowerBound(AS); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 709 | if (I == Pointers.end() || I->AddressSpace != AS) { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 710 | I = findPointerLowerBound(0); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 711 | assert(I->AddressSpace == 0); |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 712 | } |
Guillaume Chatelet | 1ae7905 | 2019-09-23 12:41:36 +0000 | [diff] [blame] | 713 | return I->ABIAlign; |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 716 | Align DataLayout::getPointerPrefAlignment(unsigned AS) const { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 717 | PointersTy::const_iterator I = findPointerLowerBound(AS); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 718 | if (I == Pointers.end() || I->AddressSpace != AS) { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 719 | I = findPointerLowerBound(0); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 720 | assert(I->AddressSpace == 0); |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 721 | } |
Guillaume Chatelet | 1ae7905 | 2019-09-23 12:41:36 +0000 | [diff] [blame] | 722 | return I->PrefAlign; |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | unsigned DataLayout::getPointerSize(unsigned AS) const { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 726 | PointersTy::const_iterator I = findPointerLowerBound(AS); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 727 | if (I == Pointers.end() || I->AddressSpace != AS) { |
Rafael Espindola | e8ae0db | 2014-02-26 17:05:38 +0000 | [diff] [blame] | 728 | I = findPointerLowerBound(0); |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 729 | assert(I->AddressSpace == 0); |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 730 | } |
Rafael Espindola | 667fcb8 | 2014-02-26 16:58:35 +0000 | [diff] [blame] | 731 | return I->TypeByteWidth; |
Rafael Espindola | 5109fcc | 2014-02-26 16:49:40 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Hal Finkel | 4f23814 | 2019-01-02 16:28:09 +0000 | [diff] [blame] | 734 | unsigned DataLayout::getMaxPointerSize() const { |
| 735 | unsigned MaxPointerSize = 0; |
| 736 | for (auto &P : Pointers) |
| 737 | MaxPointerSize = std::max(MaxPointerSize, P.TypeByteWidth); |
| 738 | |
| 739 | return MaxPointerSize; |
| 740 | } |
| 741 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 742 | unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const { |
Matt Arsenault | 6f4be90 | 2013-07-26 17:37:20 +0000 | [diff] [blame] | 743 | assert(Ty->isPtrOrPtrVectorTy() && |
| 744 | "This should only be called with a pointer or pointer vector type"); |
Craig Topper | 5b4f5b0 | 2017-04-17 18:22:36 +0000 | [diff] [blame] | 745 | Ty = Ty->getScalarType(); |
| 746 | return getPointerSizeInBits(cast<PointerType>(Ty)->getAddressSpace()); |
Matt Arsenault | 6f4be90 | 2013-07-26 17:37:20 +0000 | [diff] [blame] | 747 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 748 | |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 749 | unsigned DataLayout::getIndexSize(unsigned AS) const { |
| 750 | PointersTy::const_iterator I = findPointerLowerBound(AS); |
| 751 | if (I == Pointers.end() || I->AddressSpace != AS) { |
| 752 | I = findPointerLowerBound(0); |
| 753 | assert(I->AddressSpace == 0); |
| 754 | } |
| 755 | return I->IndexWidth; |
| 756 | } |
| 757 | |
| 758 | unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const { |
| 759 | assert(Ty->isPtrOrPtrVectorTy() && |
| 760 | "This should only be called with a pointer or pointer vector type"); |
| 761 | Ty = Ty->getScalarType(); |
| 762 | return getIndexSizeInBits(cast<PointerType>(Ty)->getAddressSpace()); |
| 763 | } |
| 764 | |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 765 | /*! |
| 766 | \param abi_or_pref Flag that determines which alignment is returned. true |
| 767 | returns the ABI alignment, false returns the preferred alignment. |
| 768 | \param Ty The underlying type for which alignment is determined. |
| 769 | |
| 770 | Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref |
| 771 | == false) for the requested type \a Ty. |
| 772 | */ |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 773 | Align DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const { |
Craig Topper | ff6922a | 2017-04-19 00:31:38 +0000 | [diff] [blame] | 774 | AlignTypeEnum AlignType; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 775 | |
| 776 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); |
| 777 | switch (Ty->getTypeID()) { |
| 778 | // Early escape for the non-numeric types. |
| 779 | case Type::LabelTyID: |
Guillaume Chatelet | 1ae7905 | 2019-09-23 12:41:36 +0000 | [diff] [blame] | 780 | return abi_or_pref ? getPointerABIAlignment(0) : getPointerPrefAlignment(0); |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 781 | case Type::PointerTyID: { |
Matt Arsenault | c172897 | 2014-09-18 22:28:56 +0000 | [diff] [blame] | 782 | unsigned AS = cast<PointerType>(Ty)->getAddressSpace(); |
Guillaume Chatelet | 1ae7905 | 2019-09-23 12:41:36 +0000 | [diff] [blame] | 783 | return abi_or_pref ? getPointerABIAlignment(AS) |
| 784 | : getPointerPrefAlignment(AS); |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 785 | } |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 786 | case Type::ArrayTyID: |
| 787 | return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref); |
| 788 | |
| 789 | case Type::StructTyID: { |
| 790 | // Packed structure types always have an ABI alignment of one. |
| 791 | if (cast<StructType>(Ty)->isPacked() && abi_or_pref) |
Guillaume Chatelet | 805c157 | 2020-01-21 15:00:04 +0100 | [diff] [blame] | 792 | return Align(1); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 793 | |
| 794 | // Get the layout annotation... which is lazily created on demand. |
| 795 | const StructLayout *Layout = getStructLayout(cast<StructType>(Ty)); |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 796 | const Align Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 797 | return std::max(Align, Layout->getAlignment()); |
| 798 | } |
| 799 | case Type::IntegerTyID: |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 800 | AlignType = INTEGER_ALIGN; |
| 801 | break; |
| 802 | case Type::HalfTyID: |
Ties Stuij | 8c24f33 | 2020-03-31 23:49:38 +0100 | [diff] [blame] | 803 | case Type::BFloatTyID: |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 804 | case Type::FloatTyID: |
| 805 | case Type::DoubleTyID: |
| 806 | // PPC_FP128TyID and FP128TyID have different data contents, but the |
| 807 | // same size and alignment, so they look the same here. |
| 808 | case Type::PPC_FP128TyID: |
| 809 | case Type::FP128TyID: |
| 810 | case Type::X86_FP80TyID: |
| 811 | AlignType = FLOAT_ALIGN; |
| 812 | break; |
| 813 | case Type::X86_MMXTyID: |
Christopher Tetreault | 2dea3f1 | 2020-04-22 08:02:02 -0700 | [diff] [blame] | 814 | case Type::FixedVectorTyID: |
| 815 | case Type::ScalableVectorTyID: |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 816 | AlignType = VECTOR_ALIGN; |
| 817 | break; |
| 818 | default: |
| 819 | llvm_unreachable("Bad type for getAlignment!!!"); |
| 820 | } |
| 821 | |
Graham Hunter | b302561 | 2019-10-08 12:53:54 +0000 | [diff] [blame] | 822 | // If we're dealing with a scalable vector, we just need the known minimum |
| 823 | // size for determining alignment. If not, we'll get the exact size. |
| 824 | return getAlignmentInfo(AlignType, getTypeSizeInBits(Ty).getKnownMinSize(), |
| 825 | abi_or_pref, Ty); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Guillaume Chatelet | 59f9522 | 2020-01-23 16:18:34 +0100 | [diff] [blame] | 828 | /// TODO: Remove this function once the transition to Align is over. |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 829 | unsigned DataLayout::getABITypeAlignment(Type *Ty) const { |
Guillaume Chatelet | 59f9522 | 2020-01-23 16:18:34 +0100 | [diff] [blame] | 830 | return getABITypeAlign(Ty).value(); |
| 831 | } |
| 832 | |
| 833 | Align DataLayout::getABITypeAlign(Type *Ty) const { |
| 834 | return getAlignment(Ty, true); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for |
| 838 | /// an integer type of the specified bitwidth. |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 839 | Align DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const { |
Guillaume Chatelet | 1ae7905 | 2019-09-23 12:41:36 +0000 | [diff] [blame] | 840 | return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Guillaume Chatelet | 59f9522 | 2020-01-23 16:18:34 +0100 | [diff] [blame] | 843 | /// TODO: Remove this function once the transition to Align is over. |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 844 | unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const { |
Guillaume Chatelet | 59f9522 | 2020-01-23 16:18:34 +0100 | [diff] [blame] | 845 | return getPrefTypeAlign(Ty).value(); |
| 846 | } |
| 847 | |
| 848 | Align DataLayout::getPrefTypeAlign(Type *Ty) const { |
| 849 | return getAlignment(Ty, false); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Micah Villmow | 89021e4 | 2012-10-09 16:06:12 +0000 | [diff] [blame] | 852 | IntegerType *DataLayout::getIntPtrType(LLVMContext &C, |
| 853 | unsigned AddressSpace) const { |
Nicola Zaghen | 9757277 | 2019-12-13 09:55:45 +0000 | [diff] [blame] | 854 | return IntegerType::get(C, getPointerSizeInBits(AddressSpace)); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 857 | Type *DataLayout::getIntPtrType(Type *Ty) const { |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 858 | assert(Ty->isPtrOrPtrVectorTy() && |
| 859 | "Expected a pointer or pointer vector type."); |
Nicola Zaghen | 9757277 | 2019-12-13 09:55:45 +0000 | [diff] [blame] | 860 | unsigned NumBits = getPointerTypeSizeInBits(Ty); |
Duncan Sands | 5bdd9dd | 2012-10-29 17:31:46 +0000 | [diff] [blame] | 861 | IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits); |
Pete Cooper | 0ae7393 | 2015-07-27 17:15:28 +0000 | [diff] [blame] | 862 | if (VectorType *VecTy = dyn_cast<VectorType>(Ty)) |
David Sherwood | 7a834a0 | 2020-06-22 14:09:34 +0100 | [diff] [blame] | 863 | return VectorType::get(IntTy, VecTy); |
Duncan Sands | 5bdd9dd | 2012-10-29 17:31:46 +0000 | [diff] [blame] | 864 | return IntTy; |
Micah Villmow | 12d9127 | 2012-10-24 15:52:52 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 867 | Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const { |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 868 | for (unsigned LegalIntWidth : LegalIntWidths) |
| 869 | if (Width <= LegalIntWidth) |
| 870 | return Type::getIntNTy(C, LegalIntWidth); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 871 | return nullptr; |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Jun Bum Lim | be11bdc | 2016-05-13 18:38:35 +0000 | [diff] [blame] | 874 | unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const { |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 875 | auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end()); |
| 876 | return Max != LegalIntWidths.end() ? *Max : 0; |
Matt Arsenault | 899f7d2 | 2013-09-16 22:43:16 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 879 | Type *DataLayout::getIndexType(Type *Ty) const { |
| 880 | assert(Ty->isPtrOrPtrVectorTy() && |
| 881 | "Expected a pointer or pointer vector type."); |
| 882 | unsigned NumBits = getIndexTypeSizeInBits(Ty); |
| 883 | IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits); |
| 884 | if (VectorType *VecTy = dyn_cast<VectorType>(Ty)) |
Eli Friedman | 90ad786 | 2020-06-17 16:35:35 -0700 | [diff] [blame] | 885 | return VectorType::get(IntTy, VecTy); |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 886 | return IntTy; |
| 887 | } |
| 888 | |
David Majnemer | 17bdf44 | 2016-07-13 03:42:38 +0000 | [diff] [blame] | 889 | int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy, |
| 890 | ArrayRef<Value *> Indices) const { |
| 891 | int64_t Result = 0; |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 892 | |
| 893 | generic_gep_type_iterator<Value* const*> |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 894 | GTI = gep_type_begin(ElemTy, Indices), |
| 895 | GTE = gep_type_end(ElemTy, Indices); |
Eduard Burtescu | 68e7f49 | 2016-01-22 03:08:27 +0000 | [diff] [blame] | 896 | for (; GTI != GTE; ++GTI) { |
| 897 | Value *Idx = GTI.getOperand(); |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 898 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
Manuel Jacob | cc13c2c | 2016-01-22 03:30:27 +0000 | [diff] [blame] | 899 | assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx"); |
Eduard Burtescu | 68e7f49 | 2016-01-22 03:08:27 +0000 | [diff] [blame] | 900 | unsigned FieldNo = cast<ConstantInt>(Idx)->getZExtValue(); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 901 | |
| 902 | // Get structure layout information... |
| 903 | const StructLayout *Layout = getStructLayout(STy); |
| 904 | |
| 905 | // Add in the offset, as calculated by the structure layout info... |
| 906 | Result += Layout->getElementOffset(FieldNo); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 907 | } else { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 908 | // Get the array index and the size of each array element. |
Eduard Burtescu | 68e7f49 | 2016-01-22 03:08:27 +0000 | [diff] [blame] | 909 | if (int64_t arrayIdx = cast<ConstantInt>(Idx)->getSExtValue()) |
David Majnemer | 17bdf44 | 2016-07-13 03:42:38 +0000 | [diff] [blame] | 910 | Result += arrayIdx * getTypeAllocSize(GTI.getIndexedType()); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
| 914 | return Result; |
| 915 | } |
| 916 | |
Guillaume Chatelet | 368a5e3 | 2020-06-29 11:24:36 +0000 | [diff] [blame] | 917 | /// getPreferredAlign - Return the preferred alignment of the specified global. |
| 918 | /// This includes an explicitly requested alignment (if the global has one). |
| 919 | Align DataLayout::getPreferredAlign(const GlobalVariable *GV) const { |
| 920 | MaybeAlign GVAlignment = GV->getAlign(); |
Eli Friedman | 3769639 | 2018-08-29 23:46:26 +0000 | [diff] [blame] | 921 | // If a section is specified, always precisely honor explicit alignment, |
| 922 | // so we don't insert padding into a section we don't control. |
| 923 | if (GVAlignment && GV->hasSection()) |
Guillaume Chatelet | 368a5e3 | 2020-06-29 11:24:36 +0000 | [diff] [blame] | 924 | return *GVAlignment; |
Eli Friedman | 3769639 | 2018-08-29 23:46:26 +0000 | [diff] [blame] | 925 | |
| 926 | // If no explicit alignment is specified, compute the alignment based on |
| 927 | // the IR type. If an alignment is specified, increase it to match the ABI |
| 928 | // alignment of the IR type. |
| 929 | // |
| 930 | // FIXME: Not sure it makes sense to use the alignment of the type if |
| 931 | // there's already an explicit alignment specification. |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 932 | Type *ElemType = GV->getValueType(); |
Guillaume Chatelet | 368a5e3 | 2020-06-29 11:24:36 +0000 | [diff] [blame] | 933 | Align Alignment = getPrefTypeAlign(ElemType); |
| 934 | if (GVAlignment) { |
| 935 | if (*GVAlignment >= Alignment) |
| 936 | Alignment = *GVAlignment; |
| 937 | else |
| 938 | Alignment = std::max(*GVAlignment, getABITypeAlign(ElemType)); |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Eli Friedman | 3769639 | 2018-08-29 23:46:26 +0000 | [diff] [blame] | 941 | // If no explicit alignment is specified, and the global is large, increase |
| 942 | // the alignment to 16. |
| 943 | // FIXME: Why 16, specifically? |
Guillaume Chatelet | 368a5e3 | 2020-06-29 11:24:36 +0000 | [diff] [blame] | 944 | if (GV->hasInitializer() && !GVAlignment) { |
| 945 | if (Alignment < Align(16)) { |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 946 | // If the global is not external, see if it is large. If so, give it a |
| 947 | // larger alignment. |
| 948 | if (getTypeSizeInBits(ElemType) > 128) |
Guillaume Chatelet | 368a5e3 | 2020-06-29 11:24:36 +0000 | [diff] [blame] | 949 | Alignment = Align(16); // 16-byte alignment. |
Micah Villmow | ac34b5c | 2012-10-04 22:08:14 +0000 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | return Alignment; |
| 953 | } |