1 //===- llvm/DataLayout.h - Data size & alignment info -----------*- C++ -*-===// 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. It uses lazy annotations to cache information about how 11 // structure types are laid out and used. 12 // 13 // This structure should be created once, filled in if the defaults are not 14 // correct and then passed around by const&. None of the members functions 15 // require modification to the object. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #ifndef LLVM_IR_DATALAYOUT_H 20 #define LLVM_IR_DATALAYOUT_H 21 22 #include "llvm/ADT/APInt.h" 23 #include "llvm/ADT/ArrayRef.h" 24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/ADT/SmallVector.h" 26 #include "llvm/ADT/StringRef.h" 27 #include "llvm/IR/DerivedTypes.h" 28 #include "llvm/IR/Type.h" 29 #include "llvm/Support/Alignment.h" 30 #include "llvm/Support/Casting.h" 31 #include "llvm/Support/Compiler.h" 32 #include "llvm/Support/ErrorHandling.h" 33 #include "llvm/Support/MathExtras.h" 34 #include "llvm/Support/TrailingObjects.h" 35 #include "llvm/Support/TypeSize.h" 36 #include <cassert> 37 #include <cstdint> 38 #include <string> 39 40 // This needs to be outside of the namespace, to avoid conflict with llvm-c 41 // decl. 42 using LLVMTargetDataRef = struct LLVMOpaqueTargetData *; 43 44 namespace llvm { 45 46 class GlobalVariable; 47 class LLVMContext; 48 class Module; 49 class StructLayout; 50 class Triple; 51 class Value; 52 53 /// Enum used to categorize the alignment types stored by LayoutAlignElem 54 enum AlignTypeEnum { 55 INTEGER_ALIGN = 'i', 56 VECTOR_ALIGN = 'v', 57 FLOAT_ALIGN = 'f', 58 AGGREGATE_ALIGN = 'a' 59 }; 60 61 // FIXME: Currently the DataLayout string carries a "preferred alignment" 62 // for types. As the DataLayout is module/global, this should likely be 63 // sunk down to an FTTI element that is queried rather than a global 64 // preference. 65 66 /// Layout alignment element. 67 /// 68 /// Stores the alignment data associated with a given type bit width. 69 /// 70 /// \note The unusual order of elements in the structure attempts to reduce 71 /// padding and make the structure slightly more cache friendly. 72 struct LayoutAlignElem { 73 uint32_t TypeBitWidth; 74 Align ABIAlign; 75 Align PrefAlign; 76 77 static LayoutAlignElem get(Align ABIAlign, Align PrefAlign, 78 uint32_t BitWidth); 79 80 bool operator==(const LayoutAlignElem &rhs) const; 81 }; 82 83 /// Layout pointer alignment element. 84 /// 85 /// Stores the alignment data associated with a given pointer and address space. 86 /// 87 /// \note The unusual order of elements in the structure attempts to reduce 88 /// padding and make the structure slightly more cache friendly. 89 struct PointerAlignElem { 90 Align ABIAlign; 91 Align PrefAlign; 92 uint32_t TypeBitWidth; 93 uint32_t AddressSpace; 94 uint32_t IndexBitWidth; 95 96 /// Initializer 97 static PointerAlignElem getInBits(uint32_t AddressSpace, Align ABIAlign, 98 Align PrefAlign, uint32_t TypeBitWidth, 99 uint32_t IndexBitWidth); 100 101 bool operator==(const PointerAlignElem &rhs) const; 102 }; 103 104 /// A parsed version of the target data layout string in and methods for 105 /// querying it. 106 /// 107 /// The target data layout string is specified *by the target* - a frontend 108 /// generating LLVM IR is required to generate the right target data for the 109 /// target being codegen'd to. 110 class DataLayout { 111 public: 112 enum class FunctionPtrAlignType { 113 /// The function pointer alignment is independent of the function alignment. 114 Independent, 115 /// The function pointer alignment is a multiple of the function alignment. 116 MultipleOfFunctionAlign, 117 }; 118 private: 119 /// Defaults to false. 120 bool BigEndian; 121 122 unsigned AllocaAddrSpace; 123 MaybeAlign StackNaturalAlign; 124 unsigned ProgramAddrSpace; 125 unsigned DefaultGlobalsAddrSpace; 126 127 MaybeAlign FunctionPtrAlign; 128 FunctionPtrAlignType TheFunctionPtrAlignType; 129 130 enum ManglingModeT { 131 MM_None, 132 MM_ELF, 133 MM_MachO, 134 MM_WinCOFF, 135 MM_WinCOFFX86, 136 MM_GOFF, 137 MM_Mips, 138 MM_XCOFF 139 }; 140 ManglingModeT ManglingMode; 141 142 SmallVector<unsigned char, 8> LegalIntWidths; 143 144 /// Primitive type alignment data. This is sorted by type and bit 145 /// width during construction. 146 using AlignmentsTy = SmallVector<LayoutAlignElem, 4>; 147 AlignmentsTy IntAlignments; 148 AlignmentsTy FloatAlignments; 149 AlignmentsTy VectorAlignments; 150 LayoutAlignElem StructAlignment; 151 152 /// The string representation used to create this DataLayout 153 std::string StringRepresentation; 154 155 using PointersTy = SmallVector<PointerAlignElem, 8>; 156 PointersTy Pointers; 157 158 const PointerAlignElem &getPointerAlignElem(uint32_t AddressSpace) const; 159 160 // The StructType -> StructLayout map. 161 mutable void *LayoutMap = nullptr; 162 163 /// Pointers in these address spaces are non-integral, and don't have a 164 /// well-defined bitwise representation. 165 SmallVector<unsigned, 8> NonIntegralAddressSpaces; 166 167 /// Attempts to set the alignment of the given type. Returns an error 168 /// description on failure. 169 Error setAlignment(AlignTypeEnum AlignType, Align ABIAlign, Align PrefAlign, 170 uint32_t BitWidth); 171 172 /// Attempts to set the alignment of a pointer in the given address space. 173 /// Returns an error description on failure. 174 Error setPointerAlignmentInBits(uint32_t AddrSpace, Align ABIAlign, 175 Align PrefAlign, uint32_t TypeBitWidth, 176 uint32_t IndexBitWidth); 177 178 /// Internal helper to get alignment for integer of given bitwidth. 179 Align getIntegerAlignment(uint32_t BitWidth, bool abi_or_pref) const; 180 181 /// Internal helper method that returns requested alignment for type. 182 Align getAlignment(Type *Ty, bool abi_or_pref) const; 183 184 /// Attempts to parse a target data specification string and reports an error 185 /// if the string is malformed. 186 Error parseSpecifier(StringRef Desc); 187 188 // Free all internal data structures. 189 void clear(); 190 191 public: 192 /// Constructs a DataLayout from a specification string. See reset(). 193 explicit DataLayout(StringRef LayoutDescription) { 194 reset(LayoutDescription); 195 } 196 197 /// Initialize target data from properties stored in the module. 198 explicit DataLayout(const Module *M); 199 200 DataLayout(const DataLayout &DL) { *this = DL; } 201 202 ~DataLayout(); // Not virtual, do not subclass this class 203 204 DataLayout &operator=(const DataLayout &DL) { 205 clear(); 206 StringRepresentation = DL.StringRepresentation; 207 BigEndian = DL.isBigEndian(); 208 AllocaAddrSpace = DL.AllocaAddrSpace; 209 StackNaturalAlign = DL.StackNaturalAlign; 210 FunctionPtrAlign = DL.FunctionPtrAlign; 211 TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType; 212 ProgramAddrSpace = DL.ProgramAddrSpace; 213 DefaultGlobalsAddrSpace = DL.DefaultGlobalsAddrSpace; 214 ManglingMode = DL.ManglingMode; 215 LegalIntWidths = DL.LegalIntWidths; 216 IntAlignments = DL.IntAlignments; 217 FloatAlignments = DL.FloatAlignments; 218 VectorAlignments = DL.VectorAlignments; 219 StructAlignment = DL.StructAlignment; 220 Pointers = DL.Pointers; 221 NonIntegralAddressSpaces = DL.NonIntegralAddressSpaces; 222 return *this; 223 } 224 225 bool operator==(const DataLayout &Other) const; 226 bool operator!=(const DataLayout &Other) const { return !(*this == Other); } 227 228 void init(const Module *M); 229 230 /// Parse a data layout string (with fallback to default values). 231 void reset(StringRef LayoutDescription); 232 233 /// Parse a data layout string and return the layout. Return an error 234 /// description on failure. 235 static Expected<DataLayout> parse(StringRef LayoutDescription); 236 237 /// Layout endianness... 238 bool isLittleEndian() const { return !BigEndian; } 239 bool isBigEndian() const { return BigEndian; } 240 241 /// Returns the string representation of the DataLayout. 242 /// 243 /// This representation is in the same format accepted by the string 244 /// constructor above. This should not be used to compare two DataLayout as 245 /// different string can represent the same layout. 246 const std::string &getStringRepresentation() const { 247 return StringRepresentation; 248 } 249 250 /// Test if the DataLayout was constructed from an empty string. 251 bool isDefault() const { return StringRepresentation.empty(); } 252 253 /// Returns true if the specified type is known to be a native integer 254 /// type supported by the CPU. 255 /// 256 /// For example, i64 is not native on most 32-bit CPUs and i37 is not native 257 /// on any known one. This returns false if the integer width is not legal. 258 /// 259 /// The width is specified in bits. 260 bool isLegalInteger(uint64_t Width) const { 261 return llvm::is_contained(LegalIntWidths, Width); 262 } 263 264 bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); } 265 266 /// Returns true if the given alignment exceeds the natural stack alignment. 267 bool exceedsNaturalStackAlignment(Align Alignment) const { 268 return StackNaturalAlign && (Alignment > *StackNaturalAlign); 269 } 270 271 Align getStackAlignment() const { 272 assert(StackNaturalAlign && "StackNaturalAlign must be defined"); 273 return *StackNaturalAlign; 274 } 275 276 unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; } 277 278 /// Returns the alignment of function pointers, which may or may not be 279 /// related to the alignment of functions. 280 /// \see getFunctionPtrAlignType 281 MaybeAlign getFunctionPtrAlign() const { return FunctionPtrAlign; } 282 283 /// Return the type of function pointer alignment. 284 /// \see getFunctionPtrAlign 285 FunctionPtrAlignType getFunctionPtrAlignType() const { 286 return TheFunctionPtrAlignType; 287 } 288 289 unsigned getProgramAddressSpace() const { return ProgramAddrSpace; } 290 unsigned getDefaultGlobalsAddressSpace() const { 291 return DefaultGlobalsAddrSpace; 292 } 293 294 bool hasMicrosoftFastStdCallMangling() const { 295 return ManglingMode == MM_WinCOFFX86; 296 } 297 298 /// Returns true if symbols with leading question marks should not receive IR 299 /// mangling. True for Windows mangling modes. 300 bool doNotMangleLeadingQuestionMark() const { 301 return ManglingMode == MM_WinCOFF || ManglingMode == MM_WinCOFFX86; 302 } 303 304 bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; } 305 306 StringRef getLinkerPrivateGlobalPrefix() const { 307 if (ManglingMode == MM_MachO) 308 return "l"; 309 return ""; 310 } 311 312 char getGlobalPrefix() const { 313 switch (ManglingMode) { 314 case MM_None: 315 case MM_ELF: 316 case MM_GOFF: 317 case MM_Mips: 318 case MM_WinCOFF: 319 case MM_XCOFF: 320 return '\0'; 321 case MM_MachO: 322 case MM_WinCOFFX86: 323 return '_'; 324 } 325 llvm_unreachable("invalid mangling mode"); 326 } 327 328 StringRef getPrivateGlobalPrefix() const { 329 switch (ManglingMode) { 330 case MM_None: 331 return ""; 332 case MM_ELF: 333 case MM_WinCOFF: 334 return ".L"; 335 case MM_GOFF: 336 return "@"; 337 case MM_Mips: 338 return "$"; 339 case MM_MachO: 340 case MM_WinCOFFX86: 341 return "L"; 342 case MM_XCOFF: 343 return "L.."; 344 } 345 llvm_unreachable("invalid mangling mode"); 346 } 347 348 static const char *getManglingComponent(const Triple &T); 349 350 /// Returns true if the specified type fits in a native integer type 351 /// supported by the CPU. 352 /// 353 /// For example, if the CPU only supports i32 as a native integer type, then 354 /// i27 fits in a legal integer type but i45 does not. 355 bool fitsInLegalInteger(unsigned Width) const { 356 for (unsigned LegalIntWidth : LegalIntWidths) 357 if (Width <= LegalIntWidth) 358 return true; 359 return false; 360 } 361 362 /// Layout pointer alignment 363 Align getPointerABIAlignment(unsigned AS) const; 364 365 /// Return target's alignment for stack-based pointers 366 /// FIXME: The defaults need to be removed once all of 367 /// the backends/clients are updated. 368 Align getPointerPrefAlignment(unsigned AS = 0) const; 369 370 /// Layout pointer size in bytes, rounded up to a whole 371 /// number of bytes. 372 /// FIXME: The defaults need to be removed once all of 373 /// the backends/clients are updated. 374 unsigned getPointerSize(unsigned AS = 0) const; 375 376 /// Returns the maximum index size over all address spaces. 377 unsigned getMaxIndexSize() const; 378 379 // Index size in bytes used for address calculation, 380 /// rounded up to a whole number of bytes. 381 unsigned getIndexSize(unsigned AS) const; 382 383 /// Return the address spaces containing non-integral pointers. Pointers in 384 /// this address space don't have a well-defined bitwise representation. 385 ArrayRef<unsigned> getNonIntegralAddressSpaces() const { 386 return NonIntegralAddressSpaces; 387 } 388 389 bool isNonIntegralAddressSpace(unsigned AddrSpace) const { 390 ArrayRef<unsigned> NonIntegralSpaces = getNonIntegralAddressSpaces(); 391 return is_contained(NonIntegralSpaces, AddrSpace); 392 } 393 394 bool isNonIntegralPointerType(PointerType *PT) const { 395 return isNonIntegralAddressSpace(PT->getAddressSpace()); 396 } 397 398 bool isNonIntegralPointerType(Type *Ty) const { 399 auto *PTy = dyn_cast<PointerType>(Ty); 400 return PTy && isNonIntegralPointerType(PTy); 401 } 402 403 /// Layout pointer size, in bits 404 /// FIXME: The defaults need to be removed once all of 405 /// the backends/clients are updated. 406 unsigned getPointerSizeInBits(unsigned AS = 0) const { 407 return getPointerAlignElem(AS).TypeBitWidth; 408 } 409 410 /// Returns the maximum index size over all address spaces. 411 unsigned getMaxIndexSizeInBits() const { 412 return getMaxIndexSize() * 8; 413 } 414 415 /// Size in bits of index used for address calculation in getelementptr. 416 unsigned getIndexSizeInBits(unsigned AS) const { 417 return getPointerAlignElem(AS).IndexBitWidth; 418 } 419 420 /// Layout pointer size, in bits, based on the type. If this function is 421 /// called with a pointer type, then the type size of the pointer is returned. 422 /// If this function is called with a vector of pointers, then the type size 423 /// of the pointer is returned. This should only be called with a pointer or 424 /// vector of pointers. 425 unsigned getPointerTypeSizeInBits(Type *) const; 426 427 /// Layout size of the index used in GEP calculation. 428 /// The function should be called with pointer or vector of pointers type. 429 unsigned getIndexTypeSizeInBits(Type *Ty) const; 430 431 unsigned getPointerTypeSize(Type *Ty) const { 432 return getPointerTypeSizeInBits(Ty) / 8; 433 } 434 435 /// Size examples: 436 /// 437 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*] 438 /// ---- ---------- --------------- --------------- 439 /// i1 1 8 8 440 /// i8 8 8 8 441 /// i19 19 24 32 442 /// i32 32 32 32 443 /// i100 100 104 128 444 /// i128 128 128 128 445 /// Float 32 32 32 446 /// Double 64 64 64 447 /// X86_FP80 80 80 96 448 /// 449 /// [*] The alloc size depends on the alignment, and thus on the target. 450 /// These values are for x86-32 linux. 451 452 /// Returns the number of bits necessary to hold the specified type. 453 /// 454 /// If Ty is a scalable vector type, the scalable property will be set and 455 /// the runtime size will be a positive integer multiple of the base size. 456 /// 457 /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must 458 /// have a size (Type::isSized() must return true). 459 TypeSize getTypeSizeInBits(Type *Ty) const; 460 461 /// Returns the maximum number of bytes that may be overwritten by 462 /// storing the specified type. 463 /// 464 /// If Ty is a scalable vector type, the scalable property will be set and 465 /// the runtime size will be a positive integer multiple of the base size. 466 /// 467 /// For example, returns 5 for i36 and 10 for x86_fp80. 468 TypeSize getTypeStoreSize(Type *Ty) const { 469 TypeSize BaseSize = getTypeSizeInBits(Ty); 470 return {divideCeil(BaseSize.getKnownMinValue(), 8), BaseSize.isScalable()}; 471 } 472 473 /// Returns the maximum number of bits that may be overwritten by 474 /// storing the specified type; always a multiple of 8. 475 /// 476 /// If Ty is a scalable vector type, the scalable property will be set and 477 /// the runtime size will be a positive integer multiple of the base size. 478 /// 479 /// For example, returns 40 for i36 and 80 for x86_fp80. 480 TypeSize getTypeStoreSizeInBits(Type *Ty) const { 481 return 8 * getTypeStoreSize(Ty); 482 } 483 484 /// Returns true if no extra padding bits are needed when storing the 485 /// specified type. 486 /// 487 /// For example, returns false for i19 that has a 24-bit store size. 488 bool typeSizeEqualsStoreSize(Type *Ty) const { 489 return getTypeSizeInBits(Ty) == getTypeStoreSizeInBits(Ty); 490 } 491 492 /// Returns the offset in bytes between successive objects of the 493 /// specified type, including alignment padding. 494 /// 495 /// If Ty is a scalable vector type, the scalable property will be set and 496 /// the runtime size will be a positive integer multiple of the base size. 497 /// 498 /// This is the amount that alloca reserves for this type. For example, 499 /// returns 12 or 16 for x86_fp80, depending on alignment. 500 TypeSize getTypeAllocSize(Type *Ty) const { 501 // Round up to the next alignment boundary. 502 return alignTo(getTypeStoreSize(Ty), getABITypeAlign(Ty).value()); 503 } 504 505 /// Returns the offset in bits between successive objects of the 506 /// specified type, including alignment padding; always a multiple of 8. 507 /// 508 /// If Ty is a scalable vector type, the scalable property will be set and 509 /// the runtime size will be a positive integer multiple of the base size. 510 /// 511 /// This is the amount that alloca reserves for this type. For example, 512 /// returns 96 or 128 for x86_fp80, depending on alignment. 513 TypeSize getTypeAllocSizeInBits(Type *Ty) const { 514 return 8 * getTypeAllocSize(Ty); 515 } 516 517 /// Returns the minimum ABI-required alignment for the specified type. 518 Align getABITypeAlign(Type *Ty) const; 519 520 /// Helper function to return `Alignment` if it's set or the result of 521 /// `getABITypeAlign(Ty)`, in any case the result is a valid alignment. 522 inline Align getValueOrABITypeAlignment(MaybeAlign Alignment, 523 Type *Ty) const { 524 return Alignment ? *Alignment : getABITypeAlign(Ty); 525 } 526 527 /// Returns the minimum ABI-required alignment for an integer type of 528 /// the specified bitwidth. 529 Align getABIIntegerTypeAlignment(unsigned BitWidth) const { 530 return getIntegerAlignment(BitWidth, /* abi_or_pref */ true); 531 } 532 533 /// Returns the preferred stack/global alignment for the specified 534 /// type. 535 /// 536 /// This is always at least as good as the ABI alignment. 537 /// FIXME: Deprecate this function once migration to Align is over. 538 LLVM_DEPRECATED("use getPrefTypeAlign instead", "getPrefTypeAlign") 539 uint64_t getPrefTypeAlignment(Type *Ty) const; 540 541 /// Returns the preferred stack/global alignment for the specified 542 /// type. 543 /// 544 /// This is always at least as good as the ABI alignment. 545 Align getPrefTypeAlign(Type *Ty) const; 546 547 /// Returns an integer type with size at least as big as that of a 548 /// pointer in the given address space. 549 IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const; 550 551 /// Returns an integer (vector of integer) type with size at least as 552 /// big as that of a pointer of the given pointer (vector of pointer) type. 553 Type *getIntPtrType(Type *) const; 554 555 /// Returns the smallest integer type with size at least as big as 556 /// Width bits. 557 Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const; 558 559 /// Returns the largest legal integer type, or null if none are set. 560 Type *getLargestLegalIntType(LLVMContext &C) const { 561 unsigned LargestSize = getLargestLegalIntTypeSizeInBits(); 562 return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize); 563 } 564 565 /// Returns the size of largest legal integer type size, or 0 if none 566 /// are set. 567 unsigned getLargestLegalIntTypeSizeInBits() const; 568 569 /// Returns the type of a GEP index in AddressSpace. 570 /// If it was not specified explicitly, it will be the integer type of the 571 /// pointer width - IntPtrType. 572 IntegerType *getIndexType(LLVMContext &C, unsigned AddressSpace) const; 573 574 /// Returns the type of a GEP index. 575 /// If it was not specified explicitly, it will be the integer type of the 576 /// pointer width - IntPtrType. 577 Type *getIndexType(Type *PtrTy) const; 578 579 /// Returns the offset from the beginning of the type for the specified 580 /// indices. 581 /// 582 /// Note that this takes the element type, not the pointer type. 583 /// This is used to implement getelementptr. 584 int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef<Value *> Indices) const; 585 586 /// Get GEP indices to access Offset inside ElemTy. ElemTy is updated to be 587 /// the result element type and Offset to be the residual offset. 588 SmallVector<APInt> getGEPIndicesForOffset(Type *&ElemTy, APInt &Offset) const; 589 590 /// Get single GEP index to access Offset inside ElemTy. Returns std::nullopt 591 /// if index cannot be computed, e.g. because the type is not an aggregate. 592 /// ElemTy is updated to be the result element type and Offset to be the 593 /// residual offset. 594 std::optional<APInt> getGEPIndexForOffset(Type *&ElemTy, APInt &Offset) const; 595 596 /// Returns a StructLayout object, indicating the alignment of the 597 /// struct, its size, and the offsets of its fields. 598 /// 599 /// Note that this information is lazily cached. 600 const StructLayout *getStructLayout(StructType *Ty) const; 601 602 /// Returns the preferred alignment of the specified global. 603 /// 604 /// This includes an explicitly requested alignment (if the global has one). 605 Align getPreferredAlign(const GlobalVariable *GV) const; 606 }; 607 608 inline DataLayout *unwrap(LLVMTargetDataRef P) { 609 return reinterpret_cast<DataLayout *>(P); 610 } 611 612 inline LLVMTargetDataRef wrap(const DataLayout *P) { 613 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P)); 614 } 615 616 /// Used to lazily calculate structure layout information for a target machine, 617 /// based on the DataLayout structure. 618 class StructLayout final : public TrailingObjects<StructLayout, TypeSize> { 619 TypeSize StructSize; 620 Align StructAlignment; 621 unsigned IsPadded : 1; 622 unsigned NumElements : 31; 623 624 public: 625 TypeSize getSizeInBytes() const { return StructSize; } 626 627 TypeSize getSizeInBits() const { return 8 * StructSize; } 628 629 Align getAlignment() const { return StructAlignment; } 630 631 /// Returns whether the struct has padding or not between its fields. 632 /// NB: Padding in nested element is not taken into account. 633 bool hasPadding() const { return IsPadded; } 634 635 /// Given a valid byte offset into the structure, returns the structure 636 /// index that contains it. 637 unsigned getElementContainingOffset(uint64_t FixedOffset) const; 638 639 MutableArrayRef<TypeSize> getMemberOffsets() { 640 return llvm::MutableArrayRef(getTrailingObjects<TypeSize>(), NumElements); 641 } 642 643 ArrayRef<TypeSize> getMemberOffsets() const { 644 return llvm::ArrayRef(getTrailingObjects<TypeSize>(), NumElements); 645 } 646 647 TypeSize getElementOffset(unsigned Idx) const { 648 assert(Idx < NumElements && "Invalid element idx!"); 649 return getMemberOffsets()[Idx]; 650 } 651 652 TypeSize getElementOffsetInBits(unsigned Idx) const { 653 return getElementOffset(Idx) * 8; 654 } 655 656 private: 657 friend class DataLayout; // Only DataLayout can create this class 658 659 StructLayout(StructType *ST, const DataLayout &DL); 660 661 size_t numTrailingObjects(OverloadToken<TypeSize>) const { 662 return NumElements; 663 } 664 }; 665 666 // The implementation of this method is provided inline as it is particularly 667 // well suited to constant folding when called on a specific Type subclass. 668 inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const { 669 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); 670 switch (Ty->getTypeID()) { 671 case Type::LabelTyID: 672 return TypeSize::Fixed(getPointerSizeInBits(0)); 673 case Type::PointerTyID: 674 return TypeSize::Fixed(getPointerSizeInBits(Ty->getPointerAddressSpace())); 675 case Type::ArrayTyID: { 676 ArrayType *ATy = cast<ArrayType>(Ty); 677 return ATy->getNumElements() * 678 getTypeAllocSizeInBits(ATy->getElementType()); 679 } 680 case Type::StructTyID: 681 // Get the layout annotation... which is lazily created on demand. 682 return getStructLayout(cast<StructType>(Ty))->getSizeInBits(); 683 case Type::IntegerTyID: 684 return TypeSize::Fixed(Ty->getIntegerBitWidth()); 685 case Type::HalfTyID: 686 case Type::BFloatTyID: 687 return TypeSize::Fixed(16); 688 case Type::FloatTyID: 689 return TypeSize::Fixed(32); 690 case Type::DoubleTyID: 691 case Type::X86_MMXTyID: 692 return TypeSize::Fixed(64); 693 case Type::PPC_FP128TyID: 694 case Type::FP128TyID: 695 return TypeSize::Fixed(128); 696 case Type::X86_AMXTyID: 697 return TypeSize::Fixed(8192); 698 // In memory objects this is always aligned to a higher boundary, but 699 // only 80 bits contain information. 700 case Type::X86_FP80TyID: 701 return TypeSize::Fixed(80); 702 case Type::FixedVectorTyID: 703 case Type::ScalableVectorTyID: { 704 VectorType *VTy = cast<VectorType>(Ty); 705 auto EltCnt = VTy->getElementCount(); 706 uint64_t MinBits = EltCnt.getKnownMinValue() * 707 getTypeSizeInBits(VTy->getElementType()).getFixedValue(); 708 return TypeSize(MinBits, EltCnt.isScalable()); 709 } 710 case Type::TargetExtTyID: { 711 Type *LayoutTy = cast<TargetExtType>(Ty)->getLayoutType(); 712 return getTypeSizeInBits(LayoutTy); 713 } 714 default: 715 llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type"); 716 } 717 } 718 719 } // end namespace llvm 720 721 #endif // LLVM_IR_DATALAYOUT_H 722