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