| 1 | //===- DataLayout.cpp - Data size & alignment routines ---------------------==// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines layout properties related to datatype size/offset/alignment |
| 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 | |
| 18 | #include "llvm/IR/DataLayout.h" |
| 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/IR/Constants.h" |
| 23 | #include "llvm/IR/DerivedTypes.h" |
| 24 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
| 25 | #include "llvm/IR/GlobalVariable.h" |
| 26 | #include "llvm/IR/Type.h" |
| 27 | #include "llvm/IR/Value.h" |
| 28 | #include "llvm/Support/Casting.h" |
| 29 | #include "llvm/Support/Error.h" |
| 30 | #include "llvm/Support/ErrorHandling.h" |
| 31 | #include "llvm/Support/MathExtras.h" |
| 32 | #include "llvm/Support/MemAlloc.h" |
| 33 | #include "llvm/Support/TypeSize.h" |
| 34 | #include "llvm/TargetParser/Triple.h" |
| 35 | #include <algorithm> |
| 36 | #include <cassert> |
| 37 | #include <cstdint> |
| 38 | #include <cstdlib> |
| 39 | #include <new> |
| 40 | #include <utility> |
| 41 | |
| 42 | using namespace llvm; |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | // Support for StructLayout |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
| 48 | StructLayout::StructLayout(StructType *ST, const DataLayout &DL) |
| 49 | : StructSize(TypeSize::getFixed(ExactSize: 0)) { |
| 50 | assert(!ST->isOpaque() && "Cannot get layout of opaque structs" ); |
| 51 | IsPadded = false; |
| 52 | NumElements = ST->getNumElements(); |
| 53 | |
| 54 | // Loop over each of the elements, placing them in memory. |
| 55 | for (unsigned i = 0, e = NumElements; i != e; ++i) { |
| 56 | Type *Ty = ST->getElementType(N: i); |
| 57 | if (i == 0 && Ty->isScalableTy()) |
| 58 | StructSize = TypeSize::getScalable(MinimumSize: 0); |
| 59 | |
| 60 | const Align TyAlign = ST->isPacked() ? Align(1) : DL.getABITypeAlign(Ty); |
| 61 | |
| 62 | // Add padding if necessary to align the data element properly. |
| 63 | // Currently the only structure with scalable size will be the homogeneous |
| 64 | // scalable vector types. Homogeneous scalable vector types have members of |
| 65 | // the same data type so no alignment issue will happen. The condition here |
| 66 | // assumes so and needs to be adjusted if this assumption changes (e.g. we |
| 67 | // support structures with arbitrary scalable data type, or structure that |
| 68 | // contains both fixed size and scalable size data type members). |
| 69 | if (!StructSize.isScalable() && !isAligned(Lhs: TyAlign, SizeInBytes: StructSize)) { |
| 70 | IsPadded = true; |
| 71 | StructSize = TypeSize::getFixed(ExactSize: alignTo(Size: StructSize, A: TyAlign)); |
| 72 | } |
| 73 | |
| 74 | // Keep track of maximum alignment constraint. |
| 75 | StructAlignment = std::max(a: TyAlign, b: StructAlignment); |
| 76 | |
| 77 | getMemberOffsets()[i] = StructSize; |
| 78 | // Consume space for this data item |
| 79 | StructSize += DL.getTypeAllocSize(Ty); |
| 80 | } |
| 81 | |
| 82 | // Add padding to the end of the struct so that it could be put in an array |
| 83 | // and all array elements would be aligned correctly. |
| 84 | if (!StructSize.isScalable() && !isAligned(Lhs: StructAlignment, SizeInBytes: StructSize)) { |
| 85 | IsPadded = true; |
| 86 | StructSize = TypeSize::getFixed(ExactSize: alignTo(Size: StructSize, A: StructAlignment)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// getElementContainingOffset - Given a valid offset into the structure, |
| 91 | /// return the structure index that contains it. |
| 92 | unsigned StructLayout::getElementContainingOffset(uint64_t FixedOffset) const { |
| 93 | assert(!StructSize.isScalable() && |
| 94 | "Cannot get element at offset for structure containing scalable " |
| 95 | "vector types" ); |
| 96 | TypeSize Offset = TypeSize::getFixed(ExactSize: FixedOffset); |
| 97 | ArrayRef<TypeSize> MemberOffsets = getMemberOffsets(); |
| 98 | |
| 99 | const auto *SI = llvm::upper_bound(Range&: MemberOffsets, Value&: Offset, |
| 100 | C: [](TypeSize LHS, TypeSize RHS) -> bool { |
| 101 | return TypeSize::isKnownLT(LHS, RHS); |
| 102 | }); |
| 103 | assert(SI != MemberOffsets.begin() && "Offset not in structure type!" ); |
| 104 | --SI; |
| 105 | assert(TypeSize::isKnownLE(*SI, Offset) && "upper_bound didn't work" ); |
| 106 | assert( |
| 107 | (SI == MemberOffsets.begin() || TypeSize::isKnownLE(*(SI - 1), Offset)) && |
| 108 | (SI + 1 == MemberOffsets.end() || |
| 109 | TypeSize::isKnownGT(*(SI + 1), Offset)) && |
| 110 | "Upper bound didn't work!" ); |
| 111 | |
| 112 | // Multiple fields can have the same offset if any of them are zero sized. |
| 113 | // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop |
| 114 | // at the i32 element, because it is the last element at that offset. This is |
| 115 | // the right one to return, because anything after it will have a higher |
| 116 | // offset, implying that this element is non-empty. |
| 117 | return SI - MemberOffsets.begin(); |
| 118 | } |
| 119 | |
| 120 | namespace { |
| 121 | |
| 122 | class StructLayoutMap { |
| 123 | using LayoutInfoTy = DenseMap<StructType *, StructLayout *>; |
| 124 | LayoutInfoTy LayoutInfo; |
| 125 | |
| 126 | public: |
| 127 | ~StructLayoutMap() { |
| 128 | // Remove any layouts. |
| 129 | for (const auto &I : LayoutInfo) { |
| 130 | StructLayout *Value = I.second; |
| 131 | Value->~StructLayout(); |
| 132 | free(ptr: Value); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | StructLayout *&operator[](StructType *STy) { return LayoutInfo[STy]; } |
| 137 | }; |
| 138 | |
| 139 | } // end anonymous namespace |
| 140 | |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | // DataLayout Class Implementation |
| 143 | //===----------------------------------------------------------------------===// |
| 144 | |
| 145 | bool DataLayout::PrimitiveSpec::operator==(const PrimitiveSpec &Other) const { |
| 146 | return BitWidth == Other.BitWidth && ABIAlign == Other.ABIAlign && |
| 147 | PrefAlign == Other.PrefAlign; |
| 148 | } |
| 149 | |
| 150 | bool DataLayout::PointerSpec::operator==(const PointerSpec &Other) const { |
| 151 | return AddrSpace == Other.AddrSpace && BitWidth == Other.BitWidth && |
| 152 | ABIAlign == Other.ABIAlign && PrefAlign == Other.PrefAlign && |
| 153 | IndexBitWidth == Other.IndexBitWidth && |
| 154 | HasUnstableRepresentation == Other.HasUnstableRepresentation && |
| 155 | HasExternalState == Other.HasExternalState && |
| 156 | AddrSpaceName == Other.AddrSpaceName; |
| 157 | } |
| 158 | |
| 159 | namespace { |
| 160 | /// Predicate to sort primitive specs by bit width. |
| 161 | struct LessPrimitiveBitWidth { |
| 162 | bool operator()(const DataLayout::PrimitiveSpec &LHS, |
| 163 | unsigned RHSBitWidth) const { |
| 164 | return LHS.BitWidth < RHSBitWidth; |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | /// Predicate to sort pointer specs by address space number. |
| 169 | struct LessPointerAddrSpace { |
| 170 | bool operator()(const DataLayout::PointerSpec &LHS, |
| 171 | unsigned RHSAddrSpace) const { |
| 172 | return LHS.AddrSpace < RHSAddrSpace; |
| 173 | } |
| 174 | }; |
| 175 | } // namespace |
| 176 | |
| 177 | // Default primitive type specifications. |
| 178 | // NOTE: These arrays must be sorted by type bit width. |
| 179 | constexpr DataLayout::PrimitiveSpec DefaultIntSpecs[] = { |
| 180 | {.BitWidth: 8, .ABIAlign: Align::Constant<1>(), .PrefAlign: Align::Constant<1>()}, // i8:8:8 |
| 181 | {.BitWidth: 16, .ABIAlign: Align::Constant<2>(), .PrefAlign: Align::Constant<2>()}, // i16:16:16 |
| 182 | {.BitWidth: 32, .ABIAlign: Align::Constant<4>(), .PrefAlign: Align::Constant<4>()}, // i32:32:32 |
| 183 | {.BitWidth: 64, .ABIAlign: Align::Constant<4>(), .PrefAlign: Align::Constant<8>()}, // i64:32:64 |
| 184 | }; |
| 185 | constexpr DataLayout::PrimitiveSpec DefaultFloatSpecs[] = { |
| 186 | {.BitWidth: 16, .ABIAlign: Align::Constant<2>(), .PrefAlign: Align::Constant<2>()}, // f16:16:16 |
| 187 | {.BitWidth: 32, .ABIAlign: Align::Constant<4>(), .PrefAlign: Align::Constant<4>()}, // f32:32:32 |
| 188 | {.BitWidth: 64, .ABIAlign: Align::Constant<8>(), .PrefAlign: Align::Constant<8>()}, // f64:64:64 |
| 189 | {.BitWidth: 128, .ABIAlign: Align::Constant<16>(), .PrefAlign: Align::Constant<16>()}, // f128:128:128 |
| 190 | }; |
| 191 | |
| 192 | DataLayout::DataLayout() |
| 193 | : IntSpecs(ArrayRef(DefaultIntSpecs)), |
| 194 | FloatSpecs(ArrayRef(DefaultFloatSpecs)) { |
| 195 | // Default pointer type specifications. |
| 196 | setPointerSpec(AddrSpace: 0, BitWidth: 64, ABIAlign: Align::Constant<8>(), PrefAlign: Align::Constant<8>(), IndexBitWidth: 64, HasUnstableRepr: false, |
| 197 | HasExternalState: false, AddrSpaceName: "" ); |
| 198 | } |
| 199 | |
| 200 | DataLayout::DataLayout(StringRef LayoutString) : DataLayout() { |
| 201 | if (Error Err = parseLayoutString(LayoutString)) |
| 202 | report_fatal_error(Err: std::move(Err)); |
| 203 | } |
| 204 | |
| 205 | DataLayout &DataLayout::operator=(const DataLayout &Other) { |
| 206 | delete static_cast<StructLayoutMap *>(LayoutMap); |
| 207 | LayoutMap = nullptr; |
| 208 | StringRepresentation = Other.StringRepresentation; |
| 209 | BigEndian = Other.BigEndian; |
| 210 | VectorsAreElementAligned = Other.VectorsAreElementAligned; |
| 211 | AllocaAddrSpace = Other.AllocaAddrSpace; |
| 212 | ProgramAddrSpace = Other.ProgramAddrSpace; |
| 213 | DefaultGlobalsAddrSpace = Other.DefaultGlobalsAddrSpace; |
| 214 | StackNaturalAlign = Other.StackNaturalAlign; |
| 215 | FunctionPtrAlign = Other.FunctionPtrAlign; |
| 216 | TheFunctionPtrAlignType = Other.TheFunctionPtrAlignType; |
| 217 | ManglingMode = Other.ManglingMode; |
| 218 | LegalIntWidths = Other.LegalIntWidths; |
| 219 | IntSpecs = Other.IntSpecs; |
| 220 | FloatSpecs = Other.FloatSpecs; |
| 221 | VectorSpecs = Other.VectorSpecs; |
| 222 | PointerSpecs = Other.PointerSpecs; |
| 223 | StructABIAlignment = Other.StructABIAlignment; |
| 224 | StructPrefAlignment = Other.StructPrefAlignment; |
| 225 | return *this; |
| 226 | } |
| 227 | |
| 228 | bool DataLayout::operator==(const DataLayout &Other) const { |
| 229 | // NOTE: StringRepresentation might differ, it is not canonicalized. |
| 230 | return BigEndian == Other.BigEndian && |
| 231 | VectorsAreElementAligned == Other.VectorsAreElementAligned && |
| 232 | AllocaAddrSpace == Other.AllocaAddrSpace && |
| 233 | ProgramAddrSpace == Other.ProgramAddrSpace && |
| 234 | DefaultGlobalsAddrSpace == Other.DefaultGlobalsAddrSpace && |
| 235 | StackNaturalAlign == Other.StackNaturalAlign && |
| 236 | FunctionPtrAlign == Other.FunctionPtrAlign && |
| 237 | TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType && |
| 238 | ManglingMode == Other.ManglingMode && |
| 239 | LegalIntWidths == Other.LegalIntWidths && IntSpecs == Other.IntSpecs && |
| 240 | FloatSpecs == Other.FloatSpecs && VectorSpecs == Other.VectorSpecs && |
| 241 | PointerSpecs == Other.PointerSpecs && |
| 242 | StructABIAlignment == Other.StructABIAlignment && |
| 243 | StructPrefAlignment == Other.StructPrefAlignment; |
| 244 | } |
| 245 | |
| 246 | Expected<DataLayout> DataLayout::parse(StringRef LayoutString) { |
| 247 | DataLayout Layout; |
| 248 | if (Error Err = Layout.parseLayoutString(LayoutString)) |
| 249 | return std::move(Err); |
| 250 | return Layout; |
| 251 | } |
| 252 | |
| 253 | static Error createSpecFormatError(Twine Format) { |
| 254 | return createStringError(S: "malformed specification, must be of the form \"" + |
| 255 | Format + "\"" ); |
| 256 | } |
| 257 | |
| 258 | /// Attempts to parse an address space component of a specification. |
| 259 | static Error parseAddrSpace(StringRef Str, unsigned &AddrSpace) { |
| 260 | if (Str.empty()) |
| 261 | return createStringError(Fmt: "address space component cannot be empty" ); |
| 262 | |
| 263 | if (!to_integer(S: Str, Num&: AddrSpace, Base: 10) || !isUInt<24>(x: AddrSpace)) |
| 264 | return createStringError(Fmt: "address space must be a 24-bit integer" ); |
| 265 | |
| 266 | return Error::success(); |
| 267 | } |
| 268 | |
| 269 | /// Attempts to parse an address space component of a specification allowing |
| 270 | /// name to be specified as well. The input is expected to be of the form |
| 271 | /// <number> '(' name ' )', with the name otional and the number is optional as |
| 272 | /// well. |
| 273 | static Error parseAddrSpaceAndName(StringRef Str, unsigned &AddrSpace, |
| 274 | StringRef &AddrSpaceName) { |
| 275 | if (Str.empty()) |
| 276 | return createStringError(Fmt: "address space component cannot be empty" ); |
| 277 | |
| 278 | if (isDigit(C: Str.front())) { |
| 279 | if (Str.consumeInteger(Radix: 10, Result&: AddrSpace) || !isUInt<24>(x: AddrSpace)) |
| 280 | return createStringError(Fmt: "address space must be a 24-bit integer" ); |
| 281 | } |
| 282 | |
| 283 | if (Str.empty()) |
| 284 | return Error::success(); |
| 285 | |
| 286 | if (Str.front() != '(') |
| 287 | return createStringError(Fmt: "address space must be a 24-bit integer" ); |
| 288 | |
| 289 | // Expect atleast one character in between the ( and ). |
| 290 | if (Str.back() != ')' || Str.size() == 2) |
| 291 | return createStringError(Fmt: "Expected `( address space name )`" ); |
| 292 | |
| 293 | AddrSpaceName = Str.drop_front().drop_back(); |
| 294 | // TODO: Do we need any additional verification for address space name? Like |
| 295 | // should be a valid identifier of some sort? Its not strictly needed. |
| 296 | |
| 297 | // LLVM's assembly parser used names "P", "G" and "A" to represent the |
| 298 | // program, default global, and alloca address space. This mapping is not 1:1 |
| 299 | // in the sense that all of them can map to the same numberic address space. |
| 300 | // Diallow using these predefined symbolic address space names as address |
| 301 | // space names specified in the data layout. |
| 302 | if (AddrSpaceName.size() == 1) { |
| 303 | char C = AddrSpaceName.front(); |
| 304 | if (C == 'P' || C == 'G' || C == 'A') |
| 305 | return createStringError( |
| 306 | Fmt: "Cannot use predefined address space names P/G/A in data layout" ); |
| 307 | } |
| 308 | return Error::success(); |
| 309 | } |
| 310 | |
| 311 | /// Attempts to parse a size component of a specification. |
| 312 | static Error parseSize(StringRef Str, unsigned &BitWidth, |
| 313 | StringRef Name = "size" ) { |
| 314 | if (Str.empty()) |
| 315 | return createStringError(S: Name + " component cannot be empty" ); |
| 316 | |
| 317 | if (!to_integer(S: Str, Num&: BitWidth, Base: 10) || BitWidth == 0 || !isUInt<24>(x: BitWidth)) |
| 318 | return createStringError(S: Name + " must be a non-zero 24-bit integer" ); |
| 319 | |
| 320 | return Error::success(); |
| 321 | } |
| 322 | |
| 323 | /// Attempts to parse an alignment component of a specification. |
| 324 | /// |
| 325 | /// On success, returns the value converted to byte amount in \p Alignment. |
| 326 | /// If the value is zero and \p AllowZero is true, \p Alignment is set to one. |
| 327 | /// |
| 328 | /// Return an error in a number of cases: |
| 329 | /// - \p Str is empty or contains characters other than decimal digits; |
| 330 | /// - the value is zero and \p AllowZero is false; |
| 331 | /// - the value is too large; |
| 332 | /// - the value is not a multiple of the byte width; |
| 333 | /// - the value converted to byte amount is not not a power of two. |
| 334 | static Error parseAlignment(StringRef Str, Align &Alignment, StringRef Name, |
| 335 | bool AllowZero = false) { |
| 336 | if (Str.empty()) |
| 337 | return createStringError(S: Name + " alignment component cannot be empty" ); |
| 338 | |
| 339 | unsigned Value; |
| 340 | if (!to_integer(S: Str, Num&: Value, Base: 10) || !isUInt<16>(x: Value)) |
| 341 | return createStringError(S: Name + " alignment must be a 16-bit integer" ); |
| 342 | |
| 343 | if (Value == 0) { |
| 344 | if (!AllowZero) |
| 345 | return createStringError(S: Name + " alignment must be non-zero" ); |
| 346 | Alignment = Align(1); |
| 347 | return Error::success(); |
| 348 | } |
| 349 | |
| 350 | constexpr unsigned ByteWidth = 8; |
| 351 | if (Value % ByteWidth || !isPowerOf2_32(Value: Value / ByteWidth)) |
| 352 | return createStringError( |
| 353 | S: Name + " alignment must be a power of two times the byte width" ); |
| 354 | |
| 355 | Alignment = Align(Value / ByteWidth); |
| 356 | return Error::success(); |
| 357 | } |
| 358 | |
| 359 | Error DataLayout::parsePrimitiveSpec(StringRef Spec) { |
| 360 | // [ifv]<size>:<abi>[:<pref>] |
| 361 | SmallVector<StringRef, 3> Components; |
| 362 | char Specifier = Spec.front(); |
| 363 | assert(Specifier == 'i' || Specifier == 'f' || Specifier == 'v'); |
| 364 | Spec.drop_front().split(A&: Components, Separator: ':'); |
| 365 | |
| 366 | if (Components.size() < 2 || Components.size() > 3) |
| 367 | return createSpecFormatError(Format: Twine(Specifier) + "<size>:<abi>[:<pref>]" ); |
| 368 | |
| 369 | // Size. Required, cannot be zero. |
| 370 | unsigned BitWidth; |
| 371 | if (Error Err = parseSize(Str: Components[0], BitWidth)) |
| 372 | return Err; |
| 373 | |
| 374 | // ABI alignment. |
| 375 | Align ABIAlign; |
| 376 | if (Error Err = parseAlignment(Str: Components[1], Alignment&: ABIAlign, Name: "ABI" )) |
| 377 | return Err; |
| 378 | |
| 379 | if (Specifier == 'i' && BitWidth == 8 && ABIAlign != 1) |
| 380 | return createStringError(Fmt: "i8 must be 8-bit aligned" ); |
| 381 | |
| 382 | // Preferred alignment. Optional, defaults to the ABI alignment. |
| 383 | Align PrefAlign = ABIAlign; |
| 384 | if (Components.size() > 2) |
| 385 | if (Error Err = parseAlignment(Str: Components[2], Alignment&: PrefAlign, Name: "preferred" )) |
| 386 | return Err; |
| 387 | |
| 388 | if (PrefAlign < ABIAlign) |
| 389 | return createStringError( |
| 390 | Fmt: "preferred alignment cannot be less than the ABI alignment" ); |
| 391 | |
| 392 | setPrimitiveSpec(Specifier, BitWidth, ABIAlign, PrefAlign); |
| 393 | return Error::success(); |
| 394 | } |
| 395 | |
| 396 | Error DataLayout::parseAggregateSpec(StringRef Spec) { |
| 397 | // a<size>:<abi>[:<pref>] |
| 398 | SmallVector<StringRef, 3> Components; |
| 399 | assert(Spec.front() == 'a'); |
| 400 | Spec.drop_front().split(A&: Components, Separator: ':'); |
| 401 | |
| 402 | if (Components.size() < 2 || Components.size() > 3) |
| 403 | return createSpecFormatError(Format: "a:<abi>[:<pref>]" ); |
| 404 | |
| 405 | // According to LangRef, <size> component must be absent altogether. |
| 406 | // For backward compatibility, allow it to be specified, but require |
| 407 | // it to be zero. |
| 408 | if (!Components[0].empty()) { |
| 409 | unsigned BitWidth; |
| 410 | if (!to_integer(S: Components[0], Num&: BitWidth, Base: 10) || BitWidth != 0) |
| 411 | return createStringError(Fmt: "size must be zero" ); |
| 412 | } |
| 413 | |
| 414 | // ABI alignment. Required. Can be zero, meaning use one byte alignment. |
| 415 | Align ABIAlign; |
| 416 | if (Error Err = |
| 417 | parseAlignment(Str: Components[1], Alignment&: ABIAlign, Name: "ABI" , /*AllowZero=*/true)) |
| 418 | return Err; |
| 419 | |
| 420 | // Preferred alignment. Optional, defaults to the ABI alignment. |
| 421 | Align PrefAlign = ABIAlign; |
| 422 | if (Components.size() > 2) |
| 423 | if (Error Err = parseAlignment(Str: Components[2], Alignment&: PrefAlign, Name: "preferred" )) |
| 424 | return Err; |
| 425 | |
| 426 | if (PrefAlign < ABIAlign) |
| 427 | return createStringError( |
| 428 | Fmt: "preferred alignment cannot be less than the ABI alignment" ); |
| 429 | |
| 430 | StructABIAlignment = ABIAlign; |
| 431 | StructPrefAlignment = PrefAlign; |
| 432 | return Error::success(); |
| 433 | } |
| 434 | |
| 435 | Error DataLayout::parsePointerSpec( |
| 436 | StringRef Spec, SmallDenseSet<StringRef, 8> &AddrSpaceNames) { |
| 437 | // p[<n>]:<size>:<abi>[:<pref>[:<idx>]] |
| 438 | SmallVector<StringRef, 5> Components; |
| 439 | assert(Spec.front() == 'p'); |
| 440 | Spec.drop_front().split(A&: Components, Separator: ':'); |
| 441 | |
| 442 | if (Components.size() < 3 || Components.size() > 5) |
| 443 | return createSpecFormatError(Format: "p[<n>]:<size>:<abi>[:<pref>[:<idx>]]" ); |
| 444 | |
| 445 | // Address space. Optional, defaults to 0. |
| 446 | unsigned AddrSpace = 0; |
| 447 | bool ExternalState = false; |
| 448 | bool UnstableRepr = false; |
| 449 | StringRef AddrSpaceName; |
| 450 | StringRef AddrSpaceStr = Components[0]; |
| 451 | while (!AddrSpaceStr.empty()) { |
| 452 | char C = AddrSpaceStr.front(); |
| 453 | if (C == 'e') { |
| 454 | ExternalState = true; |
| 455 | } else if (C == 'u') { |
| 456 | UnstableRepr = true; |
| 457 | } else if (isAlpha(C)) { |
| 458 | return createStringError(Fmt: "'%c' is not a valid pointer specification flag" , |
| 459 | Vals: C); |
| 460 | } else { |
| 461 | break; // not a valid flag, remaining must be the address space number. |
| 462 | } |
| 463 | AddrSpaceStr = AddrSpaceStr.drop_front(N: 1); |
| 464 | } |
| 465 | if (!AddrSpaceStr.empty()) |
| 466 | if (Error Err = |
| 467 | parseAddrSpaceAndName(Str: AddrSpaceStr, AddrSpace, AddrSpaceName)) |
| 468 | return Err; // Failed to parse the remaining characters as a number |
| 469 | if (AddrSpace == 0 && (ExternalState || UnstableRepr)) |
| 470 | return createStringError( |
| 471 | Fmt: "address space 0 cannot be unstable or have external state" ); |
| 472 | |
| 473 | // Check for duplicate address space names. |
| 474 | if (!AddrSpaceName.empty() && !AddrSpaceNames.insert(V: AddrSpaceName).second) |
| 475 | return createStringError(S: "address space name `" + AddrSpaceName + |
| 476 | "` already used" ); |
| 477 | |
| 478 | // Size. Required, cannot be zero. |
| 479 | unsigned BitWidth; |
| 480 | if (Error Err = parseSize(Str: Components[1], BitWidth, Name: "pointer size" )) |
| 481 | return Err; |
| 482 | |
| 483 | // ABI alignment. Required, cannot be zero. |
| 484 | Align ABIAlign; |
| 485 | if (Error Err = parseAlignment(Str: Components[2], Alignment&: ABIAlign, Name: "ABI" )) |
| 486 | return Err; |
| 487 | |
| 488 | // Preferred alignment. Optional, defaults to the ABI alignment. |
| 489 | // Cannot be zero. |
| 490 | Align PrefAlign = ABIAlign; |
| 491 | if (Components.size() > 3) |
| 492 | if (Error Err = parseAlignment(Str: Components[3], Alignment&: PrefAlign, Name: "preferred" )) |
| 493 | return Err; |
| 494 | |
| 495 | if (PrefAlign < ABIAlign) |
| 496 | return createStringError( |
| 497 | Fmt: "preferred alignment cannot be less than the ABI alignment" ); |
| 498 | |
| 499 | // Index size. Optional, defaults to pointer size. Cannot be zero. |
| 500 | unsigned IndexBitWidth = BitWidth; |
| 501 | if (Components.size() > 4) |
| 502 | if (Error Err = parseSize(Str: Components[4], BitWidth&: IndexBitWidth, Name: "index size" )) |
| 503 | return Err; |
| 504 | |
| 505 | if (IndexBitWidth > BitWidth) |
| 506 | return createStringError( |
| 507 | Fmt: "index size cannot be larger than the pointer size" ); |
| 508 | |
| 509 | setPointerSpec(AddrSpace, BitWidth, ABIAlign, PrefAlign, IndexBitWidth, |
| 510 | HasUnstableRepr: UnstableRepr, HasExternalState: ExternalState, AddrSpaceName); |
| 511 | return Error::success(); |
| 512 | } |
| 513 | |
| 514 | Error DataLayout::parseSpecification( |
| 515 | StringRef Spec, SmallVectorImpl<unsigned> &NonIntegralAddressSpaces, |
| 516 | SmallDenseSet<StringRef, 8> &AddrSpaceNames) { |
| 517 | // The "ni" specifier is the only two-character specifier. Handle it first. |
| 518 | if (Spec.starts_with(Prefix: "ni" )) { |
| 519 | // ni:<address space>[:<address space>]... |
| 520 | StringRef Rest = Spec.drop_front(N: 2); |
| 521 | |
| 522 | // Drop the first ':', then split the rest of the string the usual way. |
| 523 | if (!Rest.consume_front(Prefix: ":" )) |
| 524 | return createSpecFormatError(Format: "ni:<address space>[:<address space>]..." ); |
| 525 | |
| 526 | for (StringRef Str : split(Str: Rest, Separator: ':')) { |
| 527 | unsigned AddrSpace; |
| 528 | if (Error Err = parseAddrSpace(Str, AddrSpace)) |
| 529 | return Err; |
| 530 | if (AddrSpace == 0) |
| 531 | return createStringError(Fmt: "address space 0 cannot be non-integral" ); |
| 532 | NonIntegralAddressSpaces.push_back(Elt: AddrSpace); |
| 533 | } |
| 534 | return Error::success(); |
| 535 | } |
| 536 | |
| 537 | if (Spec == "ve" ) { |
| 538 | VectorsAreElementAligned = true; |
| 539 | return Error::success(); |
| 540 | } |
| 541 | |
| 542 | // The rest of the specifiers are single-character. |
| 543 | assert(!Spec.empty() && "Empty specification is handled by the caller" ); |
| 544 | char Specifier = Spec.front(); |
| 545 | |
| 546 | if (Specifier == 'i' || Specifier == 'f' || Specifier == 'v') |
| 547 | return parsePrimitiveSpec(Spec); |
| 548 | |
| 549 | if (Specifier == 'a') |
| 550 | return parseAggregateSpec(Spec); |
| 551 | |
| 552 | if (Specifier == 'p') |
| 553 | return parsePointerSpec(Spec, AddrSpaceNames); |
| 554 | |
| 555 | StringRef Rest = Spec.drop_front(); |
| 556 | switch (Specifier) { |
| 557 | case 's': |
| 558 | // Deprecated, but ignoring here to preserve loading older textual llvm |
| 559 | // ASM file |
| 560 | break; |
| 561 | case 'e': |
| 562 | case 'E': |
| 563 | if (!Rest.empty()) |
| 564 | return createStringError( |
| 565 | Fmt: "malformed specification, must be just 'e' or 'E'" ); |
| 566 | BigEndian = Specifier == 'E'; |
| 567 | break; |
| 568 | case 'n': // Native integer types. |
| 569 | // n<size>[:<size>]... |
| 570 | for (StringRef Str : split(Str: Rest, Separator: ':')) { |
| 571 | unsigned BitWidth; |
| 572 | if (Error Err = parseSize(Str, BitWidth)) |
| 573 | return Err; |
| 574 | LegalIntWidths.push_back(Elt: BitWidth); |
| 575 | } |
| 576 | break; |
| 577 | case 'S': { // Stack natural alignment. |
| 578 | // S<size> |
| 579 | if (Rest.empty()) |
| 580 | return createSpecFormatError(Format: "S<size>" ); |
| 581 | Align Alignment; |
| 582 | if (Error Err = parseAlignment(Str: Rest, Alignment, Name: "stack natural" )) |
| 583 | return Err; |
| 584 | StackNaturalAlign = Alignment; |
| 585 | break; |
| 586 | } |
| 587 | case 'F': { |
| 588 | // F<type><abi> |
| 589 | if (Rest.empty()) |
| 590 | return createSpecFormatError(Format: "F<type><abi>" ); |
| 591 | char Type = Rest.front(); |
| 592 | Rest = Rest.drop_front(); |
| 593 | switch (Type) { |
| 594 | case 'i': |
| 595 | TheFunctionPtrAlignType = FunctionPtrAlignType::Independent; |
| 596 | break; |
| 597 | case 'n': |
| 598 | TheFunctionPtrAlignType = FunctionPtrAlignType::MultipleOfFunctionAlign; |
| 599 | break; |
| 600 | default: |
| 601 | return createStringError(S: "unknown function pointer alignment type '" + |
| 602 | Twine(Type) + "'" ); |
| 603 | } |
| 604 | Align Alignment; |
| 605 | if (Error Err = parseAlignment(Str: Rest, Alignment, Name: "ABI" )) |
| 606 | return Err; |
| 607 | FunctionPtrAlign = Alignment; |
| 608 | break; |
| 609 | } |
| 610 | case 'P': { // Function address space. |
| 611 | if (Rest.empty()) |
| 612 | return createSpecFormatError(Format: "P<address space>" ); |
| 613 | if (Error Err = parseAddrSpace(Str: Rest, AddrSpace&: ProgramAddrSpace)) |
| 614 | return Err; |
| 615 | break; |
| 616 | } |
| 617 | case 'A': { // Default stack/alloca address space. |
| 618 | if (Rest.empty()) |
| 619 | return createSpecFormatError(Format: "A<address space>" ); |
| 620 | if (Error Err = parseAddrSpace(Str: Rest, AddrSpace&: AllocaAddrSpace)) |
| 621 | return Err; |
| 622 | break; |
| 623 | } |
| 624 | case 'G': { // Default address space for global variables. |
| 625 | if (Rest.empty()) |
| 626 | return createSpecFormatError(Format: "G<address space>" ); |
| 627 | if (Error Err = parseAddrSpace(Str: Rest, AddrSpace&: DefaultGlobalsAddrSpace)) |
| 628 | return Err; |
| 629 | break; |
| 630 | } |
| 631 | case 'm': |
| 632 | if (!Rest.consume_front(Prefix: ":" ) || Rest.empty()) |
| 633 | return createSpecFormatError(Format: "m:<mangling>" ); |
| 634 | if (Rest.size() > 1) |
| 635 | return createStringError(Fmt: "unknown mangling mode" ); |
| 636 | switch (Rest[0]) { |
| 637 | default: |
| 638 | return createStringError(Fmt: "unknown mangling mode" ); |
| 639 | case 'e': |
| 640 | ManglingMode = MM_ELF; |
| 641 | break; |
| 642 | case 'l': |
| 643 | ManglingMode = MM_GOFF; |
| 644 | break; |
| 645 | case 'o': |
| 646 | ManglingMode = MM_MachO; |
| 647 | break; |
| 648 | case 'm': |
| 649 | ManglingMode = MM_Mips; |
| 650 | break; |
| 651 | case 'w': |
| 652 | ManglingMode = MM_WinCOFF; |
| 653 | break; |
| 654 | case 'x': |
| 655 | ManglingMode = MM_WinCOFFX86; |
| 656 | break; |
| 657 | case 'a': |
| 658 | ManglingMode = MM_XCOFF; |
| 659 | break; |
| 660 | } |
| 661 | break; |
| 662 | default: |
| 663 | return createStringError(S: "unknown specifier '" + Twine(Specifier) + "'" ); |
| 664 | } |
| 665 | |
| 666 | return Error::success(); |
| 667 | } |
| 668 | |
| 669 | Error DataLayout::parseLayoutString(StringRef LayoutString) { |
| 670 | StringRepresentation = LayoutString.str(); |
| 671 | |
| 672 | if (LayoutString.empty()) |
| 673 | return Error::success(); |
| 674 | |
| 675 | // Split the data layout string into specifications separated by '-' and |
| 676 | // parse each specification individually, updating internal data structures. |
| 677 | SmallVector<unsigned, 8> NonIntegralAddressSpaces; |
| 678 | SmallDenseSet<StringRef, 8> AddessSpaceNames; |
| 679 | for (StringRef Spec : split(Str: StringRepresentation, Separator: '-')) { |
| 680 | if (Spec.empty()) |
| 681 | return createStringError(Fmt: "empty specification is not allowed" ); |
| 682 | if (Error Err = parseSpecification(Spec, NonIntegralAddressSpaces, |
| 683 | AddrSpaceNames&: AddessSpaceNames)) |
| 684 | return Err; |
| 685 | } |
| 686 | // Mark all address spaces that were qualified as non-integral now. This has |
| 687 | // to be done later since the non-integral property is not part of the data |
| 688 | // layout pointer specification. |
| 689 | for (unsigned AS : NonIntegralAddressSpaces) { |
| 690 | // If there is no special spec for a given AS, getPointerSpec(AS) returns |
| 691 | // the spec for AS0, and we then update that to mark it non-integral. |
| 692 | const PointerSpec &PS = getPointerSpec(AddrSpace: AS); |
| 693 | setPointerSpec(AddrSpace: AS, BitWidth: PS.BitWidth, ABIAlign: PS.ABIAlign, PrefAlign: PS.PrefAlign, IndexBitWidth: PS.IndexBitWidth, |
| 694 | /*HasUnstableRepr=*/true, /*HasExternalState=*/false, |
| 695 | AddrSpaceName: getAddressSpaceName(AS)); |
| 696 | } |
| 697 | |
| 698 | return Error::success(); |
| 699 | } |
| 700 | |
| 701 | void DataLayout::setPrimitiveSpec(char Specifier, uint32_t BitWidth, |
| 702 | Align ABIAlign, Align PrefAlign) { |
| 703 | SmallVectorImpl<PrimitiveSpec> *Specs; |
| 704 | switch (Specifier) { |
| 705 | default: |
| 706 | llvm_unreachable("Unexpected specifier" ); |
| 707 | case 'i': |
| 708 | Specs = &IntSpecs; |
| 709 | break; |
| 710 | case 'f': |
| 711 | Specs = &FloatSpecs; |
| 712 | break; |
| 713 | case 'v': |
| 714 | Specs = &VectorSpecs; |
| 715 | break; |
| 716 | } |
| 717 | |
| 718 | auto I = lower_bound(Range&: *Specs, Value&: BitWidth, C: LessPrimitiveBitWidth()); |
| 719 | if (I != Specs->end() && I->BitWidth == BitWidth) { |
| 720 | // Update the abi, preferred alignments. |
| 721 | I->ABIAlign = ABIAlign; |
| 722 | I->PrefAlign = PrefAlign; |
| 723 | } else { |
| 724 | // Insert before I to keep the vector sorted. |
| 725 | Specs->insert(I, Elt: PrimitiveSpec{.BitWidth: BitWidth, .ABIAlign: ABIAlign, .PrefAlign: PrefAlign}); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | const DataLayout::PointerSpec & |
| 730 | DataLayout::getPointerSpec(uint32_t AddrSpace) const { |
| 731 | if (AddrSpace != 0) { |
| 732 | auto I = lower_bound(Range: PointerSpecs, Value&: AddrSpace, C: LessPointerAddrSpace()); |
| 733 | if (I != PointerSpecs.end() && I->AddrSpace == AddrSpace) |
| 734 | return *I; |
| 735 | } |
| 736 | |
| 737 | assert(PointerSpecs[0].AddrSpace == 0); |
| 738 | return PointerSpecs[0]; |
| 739 | } |
| 740 | |
| 741 | void DataLayout::setPointerSpec(uint32_t AddrSpace, uint32_t BitWidth, |
| 742 | Align ABIAlign, Align PrefAlign, |
| 743 | uint32_t IndexBitWidth, bool HasUnstableRepr, |
| 744 | bool HasExternalState, |
| 745 | StringRef AddrSpaceName) { |
| 746 | auto I = lower_bound(Range&: PointerSpecs, Value&: AddrSpace, C: LessPointerAddrSpace()); |
| 747 | if (I == PointerSpecs.end() || I->AddrSpace != AddrSpace) { |
| 748 | PointerSpecs.insert(I, Elt: PointerSpec{.AddrSpace: AddrSpace, .BitWidth: BitWidth, .ABIAlign: ABIAlign, .PrefAlign: PrefAlign, |
| 749 | .IndexBitWidth: IndexBitWidth, .HasUnstableRepresentation: HasUnstableRepr, |
| 750 | .HasExternalState: HasExternalState, .AddrSpaceName: AddrSpaceName.str()}); |
| 751 | } else { |
| 752 | I->BitWidth = BitWidth; |
| 753 | I->ABIAlign = ABIAlign; |
| 754 | I->PrefAlign = PrefAlign; |
| 755 | I->IndexBitWidth = IndexBitWidth; |
| 756 | I->HasUnstableRepresentation = HasUnstableRepr; |
| 757 | I->HasExternalState = HasExternalState; |
| 758 | I->AddrSpaceName = AddrSpaceName.str(); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | Align DataLayout::getIntegerAlignment(uint32_t BitWidth, |
| 763 | bool abi_or_pref) const { |
| 764 | auto I = IntSpecs.begin(); |
| 765 | for (; I != IntSpecs.end(); ++I) { |
| 766 | if (I->BitWidth >= BitWidth) |
| 767 | break; |
| 768 | } |
| 769 | |
| 770 | // If we don't have an exact match, use alignment of next larger integer |
| 771 | // type. If there is none, use alignment of largest integer type by going |
| 772 | // back one element. |
| 773 | if (I == IntSpecs.end()) |
| 774 | --I; |
| 775 | return abi_or_pref ? I->ABIAlign : I->PrefAlign; |
| 776 | } |
| 777 | |
| 778 | DataLayout::~DataLayout() { delete static_cast<StructLayoutMap *>(LayoutMap); } |
| 779 | |
| 780 | const StructLayout *DataLayout::getStructLayout(StructType *Ty) const { |
| 781 | if (!LayoutMap) |
| 782 | LayoutMap = new StructLayoutMap(); |
| 783 | |
| 784 | StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap); |
| 785 | StructLayout *&SL = (*STM)[Ty]; |
| 786 | if (SL) return SL; |
| 787 | |
| 788 | // Otherwise, create the struct layout. Because it is variable length, we |
| 789 | // malloc it, then use placement new. |
| 790 | StructLayout *L = (StructLayout *)safe_malloc( |
| 791 | Sz: StructLayout::totalSizeToAlloc<TypeSize>(Counts: Ty->getNumElements())); |
| 792 | |
| 793 | // Set SL before calling StructLayout's ctor. The ctor could cause other |
| 794 | // entries to be added to TheMap, invalidating our reference. |
| 795 | SL = L; |
| 796 | |
| 797 | new (L) StructLayout(Ty, *this); |
| 798 | |
| 799 | return L; |
| 800 | } |
| 801 | |
| 802 | Align DataLayout::getPointerABIAlignment(unsigned AS) const { |
| 803 | return getPointerSpec(AddrSpace: AS).ABIAlign; |
| 804 | } |
| 805 | |
| 806 | StringRef DataLayout::getAddressSpaceName(unsigned AS) const { |
| 807 | return getPointerSpec(AddrSpace: AS).AddrSpaceName; |
| 808 | } |
| 809 | |
| 810 | std::optional<unsigned> DataLayout::getNamedAddressSpace(StringRef Name) const { |
| 811 | auto II = llvm::find_if(Range: PointerSpecs, P: [Name](const PointerSpec &PS) { |
| 812 | return PS.AddrSpaceName == Name; |
| 813 | }); |
| 814 | if (II != PointerSpecs.end()) |
| 815 | return II->AddrSpace; |
| 816 | return std::nullopt; |
| 817 | } |
| 818 | |
| 819 | Align DataLayout::getPointerPrefAlignment(unsigned AS) const { |
| 820 | return getPointerSpec(AddrSpace: AS).PrefAlign; |
| 821 | } |
| 822 | |
| 823 | unsigned DataLayout::getPointerSize(unsigned AS) const { |
| 824 | return divideCeil(Numerator: getPointerSpec(AddrSpace: AS).BitWidth, Denominator: 8); |
| 825 | } |
| 826 | |
| 827 | unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const { |
| 828 | assert(Ty->isPtrOrPtrVectorTy() && |
| 829 | "This should only be called with a pointer or pointer vector type" ); |
| 830 | Ty = Ty->getScalarType(); |
| 831 | return getPointerSizeInBits(AS: cast<PointerType>(Val: Ty)->getAddressSpace()); |
| 832 | } |
| 833 | |
| 834 | unsigned DataLayout::getIndexSize(unsigned AS) const { |
| 835 | return divideCeil(Numerator: getPointerSpec(AddrSpace: AS).IndexBitWidth, Denominator: 8); |
| 836 | } |
| 837 | |
| 838 | unsigned DataLayout::getIndexTypeSizeInBits(Type *Ty) const { |
| 839 | assert(Ty->isPtrOrPtrVectorTy() && |
| 840 | "This should only be called with a pointer or pointer vector type" ); |
| 841 | Ty = Ty->getScalarType(); |
| 842 | return getIndexSizeInBits(AS: cast<PointerType>(Val: Ty)->getAddressSpace()); |
| 843 | } |
| 844 | |
| 845 | /*! |
| 846 | \param abi_or_pref Flag that determines which alignment is returned. true |
| 847 | returns the ABI alignment, false returns the preferred alignment. |
| 848 | \param Ty The underlying type for which alignment is determined. |
| 849 | |
| 850 | Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref |
| 851 | == false) for the requested type \a Ty. |
| 852 | */ |
| 853 | Align DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const { |
| 854 | assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!" ); |
| 855 | switch (Ty->getTypeID()) { |
| 856 | // Early escape for the non-numeric types. |
| 857 | case Type::LabelTyID: |
| 858 | return abi_or_pref ? getPointerABIAlignment(AS: 0) : getPointerPrefAlignment(AS: 0); |
| 859 | case Type::PointerTyID: { |
| 860 | unsigned AS = cast<PointerType>(Val: Ty)->getAddressSpace(); |
| 861 | return abi_or_pref ? getPointerABIAlignment(AS) |
| 862 | : getPointerPrefAlignment(AS); |
| 863 | } |
| 864 | case Type::ArrayTyID: |
| 865 | return getAlignment(Ty: cast<ArrayType>(Val: Ty)->getElementType(), abi_or_pref); |
| 866 | |
| 867 | case Type::StructTyID: { |
| 868 | // Packed structure types always have an ABI alignment of one. |
| 869 | if (cast<StructType>(Val: Ty)->isPacked() && abi_or_pref) |
| 870 | return Align(1); |
| 871 | |
| 872 | // Get the layout annotation... which is lazily created on demand. |
| 873 | const StructLayout *Layout = getStructLayout(Ty: cast<StructType>(Val: Ty)); |
| 874 | const Align Align = abi_or_pref ? StructABIAlignment : StructPrefAlignment; |
| 875 | return std::max(a: Align, b: Layout->getAlignment()); |
| 876 | } |
| 877 | case Type::ByteTyID: |
| 878 | // The byte type has the same alignment as the equally sized integer type. |
| 879 | return getIntegerAlignment(BitWidth: Ty->getByteBitWidth(), abi_or_pref); |
| 880 | case Type::IntegerTyID: |
| 881 | return getIntegerAlignment(BitWidth: Ty->getIntegerBitWidth(), abi_or_pref); |
| 882 | case Type::HalfTyID: |
| 883 | case Type::BFloatTyID: |
| 884 | case Type::FloatTyID: |
| 885 | case Type::DoubleTyID: |
| 886 | // PPC_FP128TyID and FP128TyID have different data contents, but the |
| 887 | // same size and alignment, so they look the same here. |
| 888 | case Type::PPC_FP128TyID: |
| 889 | case Type::FP128TyID: |
| 890 | case Type::X86_FP80TyID: { |
| 891 | unsigned BitWidth = getTypeSizeInBits(Ty).getFixedValue(); |
| 892 | auto I = lower_bound(Range: FloatSpecs, Value&: BitWidth, C: LessPrimitiveBitWidth()); |
| 893 | if (I != FloatSpecs.end() && I->BitWidth == BitWidth) |
| 894 | return abi_or_pref ? I->ABIAlign : I->PrefAlign; |
| 895 | |
| 896 | // If we still couldn't find a reasonable default alignment, fall back |
| 897 | // to a simple heuristic that the alignment is the first power of two |
| 898 | // greater-or-equal to the store size of the type. This is a reasonable |
| 899 | // approximation of reality, and if the user wanted something less |
| 900 | // less conservative, they should have specified it explicitly in the data |
| 901 | // layout. |
| 902 | return Align(PowerOf2Ceil(A: BitWidth / 8)); |
| 903 | } |
| 904 | case Type::FixedVectorTyID: |
| 905 | case Type::ScalableVectorTyID: { |
| 906 | unsigned BitWidth = getTypeSizeInBits(Ty).getKnownMinValue(); |
| 907 | auto I = lower_bound(Range: VectorSpecs, Value&: BitWidth, C: LessPrimitiveBitWidth()); |
| 908 | if (I != VectorSpecs.end() && I->BitWidth == BitWidth) |
| 909 | return abi_or_pref ? I->ABIAlign : I->PrefAlign; |
| 910 | |
| 911 | if (vectorsAreElementAligned()) |
| 912 | return getAlignment(Ty: cast<VectorType>(Val: Ty)->getElementType(), abi_or_pref); |
| 913 | |
| 914 | // By default, use natural alignment for vector types. This is consistent |
| 915 | // with what clang and llvm-gcc do. |
| 916 | // |
| 917 | // We're only calculating a natural alignment, so it doesn't have to be |
| 918 | // based on the full size for scalable vectors. Using the minimum element |
| 919 | // count should be enough here. |
| 920 | return Align(PowerOf2Ceil(A: getTypeStoreSize(Ty).getKnownMinValue())); |
| 921 | } |
| 922 | case Type::X86_AMXTyID: |
| 923 | return Align(64); |
| 924 | case Type::TargetExtTyID: { |
| 925 | Type *LayoutTy = cast<TargetExtType>(Val: Ty)->getLayoutType(); |
| 926 | return getAlignment(Ty: LayoutTy, abi_or_pref); |
| 927 | } |
| 928 | default: |
| 929 | llvm_unreachable("Bad type for getAlignment!!!" ); |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | TypeSize DataLayout::getTypeAllocSize(Type *Ty) const { |
| 934 | switch (Ty->getTypeID()) { |
| 935 | case Type::ArrayTyID: { |
| 936 | // The alignment of the array is the alignment of the element, so there |
| 937 | // is no need for further adjustment. |
| 938 | auto *ATy = cast<ArrayType>(Val: Ty); |
| 939 | return ATy->getNumElements() * getTypeAllocSize(Ty: ATy->getElementType()); |
| 940 | } |
| 941 | case Type::StructTyID: { |
| 942 | const StructLayout *Layout = getStructLayout(Ty: cast<StructType>(Val: Ty)); |
| 943 | TypeSize Size = Layout->getSizeInBytes(); |
| 944 | |
| 945 | if (cast<StructType>(Val: Ty)->isPacked()) |
| 946 | return Size; |
| 947 | |
| 948 | Align A = std::max(a: StructABIAlignment, b: Layout->getAlignment()); |
| 949 | return alignTo(Size, Align: A.value()); |
| 950 | } |
| 951 | case Type::IntegerTyID: { |
| 952 | unsigned BitWidth = Ty->getIntegerBitWidth(); |
| 953 | TypeSize Size = TypeSize::getFixed(ExactSize: divideCeil(Numerator: BitWidth, Denominator: 8)); |
| 954 | Align A = getIntegerAlignment(BitWidth, /*ABI=*/abi_or_pref: true); |
| 955 | return alignTo(Size, Align: A.value()); |
| 956 | } |
| 957 | case Type::PointerTyID: { |
| 958 | unsigned AS = Ty->getPointerAddressSpace(); |
| 959 | TypeSize Size = TypeSize::getFixed(ExactSize: getPointerSize(AS)); |
| 960 | return alignTo(Size, Align: getPointerABIAlignment(AS).value()); |
| 961 | } |
| 962 | case Type::TargetExtTyID: { |
| 963 | Type *LayoutTy = cast<TargetExtType>(Val: Ty)->getLayoutType(); |
| 964 | return getTypeAllocSize(Ty: LayoutTy); |
| 965 | } |
| 966 | default: |
| 967 | return alignTo(Size: getTypeStoreSize(Ty), Align: getABITypeAlign(Ty).value()); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | Align DataLayout::getABITypeAlign(Type *Ty) const { |
| 972 | return getAlignment(Ty, abi_or_pref: true); |
| 973 | } |
| 974 | |
| 975 | Align DataLayout::getPrefTypeAlign(Type *Ty) const { |
| 976 | return getAlignment(Ty, abi_or_pref: false); |
| 977 | } |
| 978 | |
| 979 | IntegerType *DataLayout::getIntPtrType(LLVMContext &C, |
| 980 | unsigned AddressSpace) const { |
| 981 | return IntegerType::get(C, NumBits: getPointerSizeInBits(AS: AddressSpace)); |
| 982 | } |
| 983 | |
| 984 | Type *DataLayout::getIntPtrType(Type *Ty) const { |
| 985 | assert(Ty->isPtrOrPtrVectorTy() && |
| 986 | "Expected a pointer or pointer vector type." ); |
| 987 | unsigned NumBits = getPointerTypeSizeInBits(Ty); |
| 988 | IntegerType *IntTy = IntegerType::get(C&: Ty->getContext(), NumBits); |
| 989 | if (VectorType *VecTy = dyn_cast<VectorType>(Val: Ty)) |
| 990 | return VectorType::get(ElementType: IntTy, Other: VecTy); |
| 991 | return IntTy; |
| 992 | } |
| 993 | |
| 994 | ByteType *DataLayout::getBytePtrType(LLVMContext &C, |
| 995 | unsigned AddressSpace) const { |
| 996 | return ByteType::get(C, NumBits: getPointerSizeInBits(AS: AddressSpace)); |
| 997 | } |
| 998 | |
| 999 | Type *DataLayout::getBytePtrType(Type *Ty) const { |
| 1000 | assert(Ty->isPtrOrPtrVectorTy() && |
| 1001 | "Expected a pointer or pointer vector type." ); |
| 1002 | unsigned NumBits = getPointerTypeSizeInBits(Ty); |
| 1003 | ByteType *ByteTy = ByteType::get(C&: Ty->getContext(), NumBits); |
| 1004 | if (VectorType *VecTy = dyn_cast<VectorType>(Val: Ty)) |
| 1005 | return VectorType::get(ElementType: ByteTy, Other: VecTy); |
| 1006 | return ByteTy; |
| 1007 | } |
| 1008 | |
| 1009 | Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const { |
| 1010 | for (unsigned LegalIntWidth : LegalIntWidths) |
| 1011 | if (Width <= LegalIntWidth) |
| 1012 | return Type::getIntNTy(C, N: LegalIntWidth); |
| 1013 | return nullptr; |
| 1014 | } |
| 1015 | |
| 1016 | unsigned DataLayout::getLargestLegalIntTypeSizeInBits() const { |
| 1017 | auto Max = llvm::max_element(Range: LegalIntWidths); |
| 1018 | return Max != LegalIntWidths.end() ? *Max : 0; |
| 1019 | } |
| 1020 | |
| 1021 | IntegerType *DataLayout::getIndexType(LLVMContext &C, |
| 1022 | unsigned AddressSpace) const { |
| 1023 | return IntegerType::get(C, NumBits: getIndexSizeInBits(AS: AddressSpace)); |
| 1024 | } |
| 1025 | |
| 1026 | Type *DataLayout::getIndexType(Type *Ty) const { |
| 1027 | assert(Ty->isPtrOrPtrVectorTy() && |
| 1028 | "Expected a pointer or pointer vector type." ); |
| 1029 | unsigned NumBits = getIndexTypeSizeInBits(Ty); |
| 1030 | IntegerType *IntTy = IntegerType::get(C&: Ty->getContext(), NumBits); |
| 1031 | if (VectorType *VecTy = dyn_cast<VectorType>(Val: Ty)) |
| 1032 | return VectorType::get(ElementType: IntTy, Other: VecTy); |
| 1033 | return IntTy; |
| 1034 | } |
| 1035 | |
| 1036 | int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy, |
| 1037 | ArrayRef<Value *> Indices) const { |
| 1038 | int64_t Result = 0; |
| 1039 | |
| 1040 | generic_gep_type_iterator<Value* const*> |
| 1041 | GTI = gep_type_begin(Op0: ElemTy, A: Indices), |
| 1042 | GTE = gep_type_end(ElemTy, A: Indices); |
| 1043 | for (; GTI != GTE; ++GTI) { |
| 1044 | Value *Idx = GTI.getOperand(); |
| 1045 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
| 1046 | assert(Idx->getType()->isIntegerTy(32) && "Illegal struct idx" ); |
| 1047 | unsigned FieldNo = cast<ConstantInt>(Val: Idx)->getZExtValue(); |
| 1048 | |
| 1049 | // Get structure layout information... |
| 1050 | const StructLayout *Layout = getStructLayout(Ty: STy); |
| 1051 | |
| 1052 | // Add in the offset, as calculated by the structure layout info... |
| 1053 | Result += Layout->getElementOffset(Idx: FieldNo); |
| 1054 | } else { |
| 1055 | if (int64_t ArrayIdx = cast<ConstantInt>(Val: Idx)->getSExtValue()) |
| 1056 | Result += ArrayIdx * GTI.getSequentialElementStride(DL: *this); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | return Result; |
| 1061 | } |
| 1062 | |
| 1063 | static APInt getElementIndex(TypeSize ElemSize, APInt &Offset) { |
| 1064 | // Skip over scalable or zero size elements. Also skip element sizes larger |
| 1065 | // than the positive index space, because the arithmetic below may not be |
| 1066 | // correct in that case. |
| 1067 | unsigned BitWidth = Offset.getBitWidth(); |
| 1068 | if (ElemSize.isScalable() || ElemSize == 0 || |
| 1069 | !isUIntN(N: BitWidth - 1, x: ElemSize)) { |
| 1070 | return APInt::getZero(numBits: BitWidth); |
| 1071 | } |
| 1072 | |
| 1073 | uint64_t FixedElemSize = ElemSize.getFixedValue(); |
| 1074 | APInt Index = Offset.sdiv(RHS: FixedElemSize); |
| 1075 | Offset -= Index * FixedElemSize; |
| 1076 | if (Offset.isNegative()) { |
| 1077 | // Prefer a positive remaining offset to allow struct indexing. |
| 1078 | --Index; |
| 1079 | Offset += FixedElemSize; |
| 1080 | assert(Offset.isNonNegative() && "Remaining offset shouldn't be negative" ); |
| 1081 | } |
| 1082 | return Index; |
| 1083 | } |
| 1084 | |
| 1085 | std::optional<APInt> DataLayout::getGEPIndexForOffset(Type *&ElemTy, |
| 1086 | APInt &Offset) const { |
| 1087 | if (auto *ArrTy = dyn_cast<ArrayType>(Val: ElemTy)) { |
| 1088 | ElemTy = ArrTy->getElementType(); |
| 1089 | return getElementIndex(ElemSize: getTypeAllocSize(Ty: ElemTy), Offset); |
| 1090 | } |
| 1091 | |
| 1092 | if (isa<VectorType>(Val: ElemTy)) { |
| 1093 | // Vector GEPs are partially broken (e.g. for overaligned element types), |
| 1094 | // and may be forbidden in the future, so avoid generating GEPs into |
| 1095 | // vectors. See https://discourse.llvm.org/t/67497 |
| 1096 | return std::nullopt; |
| 1097 | } |
| 1098 | |
| 1099 | if (auto *STy = dyn_cast<StructType>(Val: ElemTy)) { |
| 1100 | const StructLayout *SL = getStructLayout(Ty: STy); |
| 1101 | uint64_t IntOffset = Offset.getZExtValue(); |
| 1102 | if (IntOffset >= SL->getSizeInBytes()) |
| 1103 | return std::nullopt; |
| 1104 | |
| 1105 | unsigned Index = SL->getElementContainingOffset(FixedOffset: IntOffset); |
| 1106 | Offset -= SL->getElementOffset(Idx: Index); |
| 1107 | ElemTy = STy->getElementType(N: Index); |
| 1108 | return APInt(32, Index); |
| 1109 | } |
| 1110 | |
| 1111 | // Non-aggregate type. |
| 1112 | return std::nullopt; |
| 1113 | } |
| 1114 | |
| 1115 | SmallVector<APInt> DataLayout::getGEPIndicesForOffset(Type *&ElemTy, |
| 1116 | APInt &Offset) const { |
| 1117 | assert(ElemTy->isSized() && "Element type must be sized" ); |
| 1118 | SmallVector<APInt> Indices; |
| 1119 | Indices.push_back(Elt: getElementIndex(ElemSize: getTypeAllocSize(Ty: ElemTy), Offset)); |
| 1120 | while (Offset != 0) { |
| 1121 | std::optional<APInt> Index = getGEPIndexForOffset(ElemTy, Offset); |
| 1122 | if (!Index) |
| 1123 | break; |
| 1124 | Indices.push_back(Elt: *Index); |
| 1125 | } |
| 1126 | |
| 1127 | return Indices; |
| 1128 | } |
| 1129 | |
| 1130 | /// getPreferredAlign - Return the preferred alignment of the specified global. |
| 1131 | /// This includes an explicitly requested alignment (if the global has one). |
| 1132 | Align DataLayout::getPreferredAlign(const GlobalVariable *GV) const { |
| 1133 | MaybeAlign GVAlignment = GV->getAlign(); |
| 1134 | // If a section is specified, always precisely honor explicit alignment, |
| 1135 | // so we don't insert padding into a section we don't control. |
| 1136 | if (GVAlignment && GV->hasSection()) |
| 1137 | return *GVAlignment; |
| 1138 | |
| 1139 | // If no explicit alignment is specified, compute the alignment based on |
| 1140 | // the IR type. If an alignment is specified, increase it to match the ABI |
| 1141 | // alignment of the IR type. |
| 1142 | // |
| 1143 | // FIXME: Not sure it makes sense to use the alignment of the type if |
| 1144 | // there's already an explicit alignment specification. |
| 1145 | Type *ElemType = GV->getValueType(); |
| 1146 | Align Alignment = getPrefTypeAlign(Ty: ElemType); |
| 1147 | if (GVAlignment) { |
| 1148 | if (*GVAlignment >= Alignment) |
| 1149 | Alignment = *GVAlignment; |
| 1150 | else |
| 1151 | Alignment = std::max(a: *GVAlignment, b: getABITypeAlign(Ty: ElemType)); |
| 1152 | } |
| 1153 | |
| 1154 | // If no explicit alignment is specified, and the global is large, increase |
| 1155 | // the alignment to 16. |
| 1156 | // FIXME: Why 16, specifically? |
| 1157 | if (GV->hasInitializer() && !GVAlignment) { |
| 1158 | if (Alignment < Align(16)) { |
| 1159 | // If the global is not external, see if it is large. If so, give it a |
| 1160 | // larger alignment. |
| 1161 | if (getTypeSizeInBits(Ty: ElemType) > 128) |
| 1162 | Alignment = Align(16); // 16-byte alignment. |
| 1163 | } |
| 1164 | } |
| 1165 | return Alignment; |
| 1166 | } |
| 1167 | |