Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:58

0001 //===- llvm/DataLayout.h - Data size & alignment info -----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines layout properties related to datatype size/offset/alignment
0010 // information.  It uses lazy annotations to cache information about how
0011 // structure types are laid out and used.
0012 //
0013 // This structure should be created once, filled in if the defaults are not
0014 // correct and then passed around by const&.  None of the members functions
0015 // require modification to the object.
0016 //
0017 //===----------------------------------------------------------------------===//
0018 
0019 #ifndef LLVM_IR_DATALAYOUT_H
0020 #define LLVM_IR_DATALAYOUT_H
0021 
0022 #include "llvm/ADT/APInt.h"
0023 #include "llvm/ADT/ArrayRef.h"
0024 #include "llvm/ADT/STLExtras.h"
0025 #include "llvm/ADT/SmallVector.h"
0026 #include "llvm/ADT/StringRef.h"
0027 #include "llvm/IR/DerivedTypes.h"
0028 #include "llvm/IR/Type.h"
0029 #include "llvm/Support/Alignment.h"
0030 #include "llvm/Support/Casting.h"
0031 #include "llvm/Support/Compiler.h"
0032 #include "llvm/Support/ErrorHandling.h"
0033 #include "llvm/Support/MathExtras.h"
0034 #include "llvm/Support/TrailingObjects.h"
0035 #include "llvm/Support/TypeSize.h"
0036 #include <cassert>
0037 #include <cstdint>
0038 #include <string>
0039 
0040 // This needs to be outside of the namespace, to avoid conflict with llvm-c
0041 // decl.
0042 using LLVMTargetDataRef = struct LLVMOpaqueTargetData *;
0043 
0044 namespace llvm {
0045 
0046 class GlobalVariable;
0047 class LLVMContext;
0048 class StructLayout;
0049 class Triple;
0050 class Value;
0051 
0052 // FIXME: Currently the DataLayout string carries a "preferred alignment"
0053 // for types. As the DataLayout is module/global, this should likely be
0054 // sunk down to an FTTI element that is queried rather than a global
0055 // preference.
0056 
0057 /// A parsed version of the target data layout string in and methods for
0058 /// querying it.
0059 ///
0060 /// The target data layout string is specified *by the target* - a frontend
0061 /// generating LLVM IR is required to generate the right target data for the
0062 /// target being codegen'd to.
0063 class DataLayout {
0064 public:
0065   /// Primitive type specification.
0066   struct PrimitiveSpec {
0067     uint32_t BitWidth;
0068     Align ABIAlign;
0069     Align PrefAlign;
0070 
0071     bool operator==(const PrimitiveSpec &Other) const;
0072   };
0073 
0074   /// Pointer type specification.
0075   struct PointerSpec {
0076     uint32_t AddrSpace;
0077     uint32_t BitWidth;
0078     Align ABIAlign;
0079     Align PrefAlign;
0080     uint32_t IndexBitWidth;
0081     /// Pointers in this address space don't have a well-defined bitwise
0082     /// representation (e.g. may be relocated by a copying garbage collector).
0083     /// Additionally, they may also be non-integral (i.e. containing additional
0084     /// metadata such as bounds information/permissions).
0085     bool IsNonIntegral;
0086     bool operator==(const PointerSpec &Other) const;
0087   };
0088 
0089   enum class FunctionPtrAlignType {
0090     /// The function pointer alignment is independent of the function alignment.
0091     Independent,
0092     /// The function pointer alignment is a multiple of the function alignment.
0093     MultipleOfFunctionAlign,
0094   };
0095 private:
0096   bool BigEndian = false;
0097 
0098   unsigned AllocaAddrSpace = 0;
0099   unsigned ProgramAddrSpace = 0;
0100   unsigned DefaultGlobalsAddrSpace = 0;
0101 
0102   MaybeAlign StackNaturalAlign;
0103   MaybeAlign FunctionPtrAlign;
0104   FunctionPtrAlignType TheFunctionPtrAlignType =
0105       FunctionPtrAlignType::Independent;
0106 
0107   enum ManglingModeT {
0108     MM_None,
0109     MM_ELF,
0110     MM_MachO,
0111     MM_WinCOFF,
0112     MM_WinCOFFX86,
0113     MM_GOFF,
0114     MM_Mips,
0115     MM_XCOFF
0116   };
0117   ManglingModeT ManglingMode = MM_None;
0118 
0119   // FIXME: `unsigned char` truncates the value parsed by `parseSpecifier`.
0120   SmallVector<unsigned char, 8> LegalIntWidths;
0121 
0122   /// Primitive type specifications. Sorted and uniqued by type bit width.
0123   SmallVector<PrimitiveSpec, 6> IntSpecs;
0124   SmallVector<PrimitiveSpec, 4> FloatSpecs;
0125   SmallVector<PrimitiveSpec, 10> VectorSpecs;
0126 
0127   /// Pointer type specifications. Sorted and uniqued by address space number.
0128   SmallVector<PointerSpec, 8> PointerSpecs;
0129 
0130   /// The string representation used to create this DataLayout
0131   std::string StringRepresentation;
0132 
0133   /// Struct type ABI and preferred alignments. The default spec is "a:8:64".
0134   Align StructABIAlignment = Align::Constant<1>();
0135   Align StructPrefAlignment = Align::Constant<8>();
0136 
0137   // The StructType -> StructLayout map.
0138   mutable void *LayoutMap = nullptr;
0139 
0140   /// Sets or updates the specification for the given primitive type.
0141   void setPrimitiveSpec(char Specifier, uint32_t BitWidth, Align ABIAlign,
0142                         Align PrefAlign);
0143 
0144   /// Searches for a pointer specification that matches the given address space.
0145   /// Returns the default address space specification if not found.
0146   const PointerSpec &getPointerSpec(uint32_t AddrSpace) const;
0147 
0148   /// Sets or updates the specification for pointer in the given address space.
0149   void setPointerSpec(uint32_t AddrSpace, uint32_t BitWidth, Align ABIAlign,
0150                       Align PrefAlign, uint32_t IndexBitWidth,
0151                       bool IsNonIntegral);
0152 
0153   /// Internal helper to get alignment for integer of given bitwidth.
0154   Align getIntegerAlignment(uint32_t BitWidth, bool abi_or_pref) const;
0155 
0156   /// Internal helper method that returns requested alignment for type.
0157   Align getAlignment(Type *Ty, bool abi_or_pref) const;
0158 
0159   /// Attempts to parse primitive specification ('i', 'f', or 'v').
0160   Error parsePrimitiveSpec(StringRef Spec);
0161 
0162   /// Attempts to parse aggregate specification ('a').
0163   Error parseAggregateSpec(StringRef Spec);
0164 
0165   /// Attempts to parse pointer specification ('p').
0166   Error parsePointerSpec(StringRef Spec);
0167 
0168   /// Attempts to parse a single specification.
0169   Error parseSpecification(StringRef Spec,
0170                            SmallVectorImpl<unsigned> &NonIntegralAddressSpaces);
0171 
0172   /// Attempts to parse a data layout string.
0173   Error parseLayoutString(StringRef LayoutString);
0174 
0175 public:
0176   /// Constructs a DataLayout with default values.
0177   DataLayout();
0178 
0179   /// Constructs a DataLayout from a specification string.
0180   /// WARNING: Aborts execution if the string is malformed. Use parse() instead.
0181   explicit DataLayout(StringRef LayoutString);
0182 
0183   DataLayout(const DataLayout &DL) { *this = DL; }
0184 
0185   ~DataLayout(); // Not virtual, do not subclass this class
0186 
0187   DataLayout &operator=(const DataLayout &Other);
0188 
0189   bool operator==(const DataLayout &Other) const;
0190   bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
0191 
0192   /// Parse a data layout string and return the layout. Return an error
0193   /// description on failure.
0194   static Expected<DataLayout> parse(StringRef LayoutString);
0195 
0196   /// Layout endianness...
0197   bool isLittleEndian() const { return !BigEndian; }
0198   bool isBigEndian() const { return BigEndian; }
0199 
0200   /// Returns the string representation of the DataLayout.
0201   ///
0202   /// This representation is in the same format accepted by the string
0203   /// constructor above. This should not be used to compare two DataLayout as
0204   /// different string can represent the same layout.
0205   const std::string &getStringRepresentation() const {
0206     return StringRepresentation;
0207   }
0208 
0209   /// Test if the DataLayout was constructed from an empty string.
0210   bool isDefault() const { return StringRepresentation.empty(); }
0211 
0212   /// Returns true if the specified type is known to be a native integer
0213   /// type supported by the CPU.
0214   ///
0215   /// For example, i64 is not native on most 32-bit CPUs and i37 is not native
0216   /// on any known one. This returns false if the integer width is not legal.
0217   ///
0218   /// The width is specified in bits.
0219   bool isLegalInteger(uint64_t Width) const {
0220     return llvm::is_contained(LegalIntWidths, Width);
0221   }
0222 
0223   bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); }
0224 
0225   /// Returns the natural stack alignment, or MaybeAlign() if one wasn't
0226   /// specified.
0227   MaybeAlign getStackAlignment() const { return StackNaturalAlign; }
0228 
0229   unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; }
0230 
0231   PointerType *getAllocaPtrType(LLVMContext &Ctx) const {
0232     return PointerType::get(Ctx, AllocaAddrSpace);
0233   }
0234 
0235   /// Returns the alignment of function pointers, which may or may not be
0236   /// related to the alignment of functions.
0237   /// \see getFunctionPtrAlignType
0238   MaybeAlign getFunctionPtrAlign() const { return FunctionPtrAlign; }
0239 
0240   /// Return the type of function pointer alignment.
0241   /// \see getFunctionPtrAlign
0242   FunctionPtrAlignType getFunctionPtrAlignType() const {
0243     return TheFunctionPtrAlignType;
0244   }
0245 
0246   unsigned getProgramAddressSpace() const { return ProgramAddrSpace; }
0247   unsigned getDefaultGlobalsAddressSpace() const {
0248     return DefaultGlobalsAddrSpace;
0249   }
0250 
0251   bool hasMicrosoftFastStdCallMangling() const {
0252     return ManglingMode == MM_WinCOFFX86;
0253   }
0254 
0255   /// Returns true if symbols with leading question marks should not receive IR
0256   /// mangling. True for Windows mangling modes.
0257   bool doNotMangleLeadingQuestionMark() const {
0258     return ManglingMode == MM_WinCOFF || ManglingMode == MM_WinCOFFX86;
0259   }
0260 
0261   bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; }
0262 
0263   StringRef getLinkerPrivateGlobalPrefix() const {
0264     if (ManglingMode == MM_MachO)
0265       return "l";
0266     return "";
0267   }
0268 
0269   char getGlobalPrefix() const {
0270     switch (ManglingMode) {
0271     case MM_None:
0272     case MM_ELF:
0273     case MM_GOFF:
0274     case MM_Mips:
0275     case MM_WinCOFF:
0276     case MM_XCOFF:
0277       return '\0';
0278     case MM_MachO:
0279     case MM_WinCOFFX86:
0280       return '_';
0281     }
0282     llvm_unreachable("invalid mangling mode");
0283   }
0284 
0285   StringRef getPrivateGlobalPrefix() const {
0286     switch (ManglingMode) {
0287     case MM_None:
0288       return "";
0289     case MM_ELF:
0290     case MM_WinCOFF:
0291       return ".L";
0292     case MM_GOFF:
0293       return "L#";
0294     case MM_Mips:
0295       return "$";
0296     case MM_MachO:
0297     case MM_WinCOFFX86:
0298       return "L";
0299     case MM_XCOFF:
0300       return "L..";
0301     }
0302     llvm_unreachable("invalid mangling mode");
0303   }
0304 
0305   static const char *getManglingComponent(const Triple &T);
0306 
0307   /// Returns true if the specified type fits in a native integer type
0308   /// supported by the CPU.
0309   ///
0310   /// For example, if the CPU only supports i32 as a native integer type, then
0311   /// i27 fits in a legal integer type but i45 does not.
0312   bool fitsInLegalInteger(unsigned Width) const {
0313     for (unsigned LegalIntWidth : LegalIntWidths)
0314       if (Width <= LegalIntWidth)
0315         return true;
0316     return false;
0317   }
0318 
0319   /// Layout pointer alignment
0320   Align getPointerABIAlignment(unsigned AS) const;
0321 
0322   /// Return target's alignment for stack-based pointers
0323   /// FIXME: The defaults need to be removed once all of
0324   /// the backends/clients are updated.
0325   Align getPointerPrefAlignment(unsigned AS = 0) const;
0326 
0327   /// Layout pointer size in bytes, rounded up to a whole
0328   /// number of bytes.
0329   /// FIXME: The defaults need to be removed once all of
0330   /// the backends/clients are updated.
0331   unsigned getPointerSize(unsigned AS = 0) const;
0332 
0333   // Index size in bytes used for address calculation,
0334   /// rounded up to a whole number of bytes.
0335   unsigned getIndexSize(unsigned AS) const;
0336 
0337   /// Return the address spaces containing non-integral pointers.  Pointers in
0338   /// this address space don't have a well-defined bitwise representation.
0339   SmallVector<unsigned, 8> getNonIntegralAddressSpaces() const {
0340     SmallVector<unsigned, 8> AddrSpaces;
0341     for (const PointerSpec &PS : PointerSpecs) {
0342       if (PS.IsNonIntegral)
0343         AddrSpaces.push_back(PS.AddrSpace);
0344     }
0345     return AddrSpaces;
0346   }
0347 
0348   bool isNonIntegralAddressSpace(unsigned AddrSpace) const {
0349     return getPointerSpec(AddrSpace).IsNonIntegral;
0350   }
0351 
0352   bool isNonIntegralPointerType(PointerType *PT) const {
0353     return isNonIntegralAddressSpace(PT->getAddressSpace());
0354   }
0355 
0356   bool isNonIntegralPointerType(Type *Ty) const {
0357     auto *PTy = dyn_cast<PointerType>(Ty);
0358     return PTy && isNonIntegralPointerType(PTy);
0359   }
0360 
0361   /// Layout pointer size, in bits
0362   /// FIXME: The defaults need to be removed once all of
0363   /// the backends/clients are updated.
0364   unsigned getPointerSizeInBits(unsigned AS = 0) const {
0365     return getPointerSpec(AS).BitWidth;
0366   }
0367 
0368   /// Size in bits of index used for address calculation in getelementptr.
0369   unsigned getIndexSizeInBits(unsigned AS) const {
0370     return getPointerSpec(AS).IndexBitWidth;
0371   }
0372 
0373   /// Layout pointer size, in bits, based on the type.  If this function is
0374   /// called with a pointer type, then the type size of the pointer is returned.
0375   /// If this function is called with a vector of pointers, then the type size
0376   /// of the pointer is returned.  This should only be called with a pointer or
0377   /// vector of pointers.
0378   unsigned getPointerTypeSizeInBits(Type *) const;
0379 
0380   /// Layout size of the index used in GEP calculation.
0381   /// The function should be called with pointer or vector of pointers type.
0382   unsigned getIndexTypeSizeInBits(Type *Ty) const;
0383 
0384   unsigned getPointerTypeSize(Type *Ty) const {
0385     return getPointerTypeSizeInBits(Ty) / 8;
0386   }
0387 
0388   /// Size examples:
0389   ///
0390   /// Type        SizeInBits  StoreSizeInBits  AllocSizeInBits[*]
0391   /// ----        ----------  ---------------  ---------------
0392   ///  i1            1           8                8
0393   ///  i8            8           8                8
0394   ///  i19          19          24               32
0395   ///  i32          32          32               32
0396   ///  i100        100         104              128
0397   ///  i128        128         128              128
0398   ///  Float        32          32               32
0399   ///  Double       64          64               64
0400   ///  X86_FP80     80          80               96
0401   ///
0402   /// [*] The alloc size depends on the alignment, and thus on the target.
0403   ///     These values are for x86-32 linux.
0404 
0405   /// Returns the number of bits necessary to hold the specified type.
0406   ///
0407   /// If Ty is a scalable vector type, the scalable property will be set and
0408   /// the runtime size will be a positive integer multiple of the base size.
0409   ///
0410   /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must
0411   /// have a size (Type::isSized() must return true).
0412   TypeSize getTypeSizeInBits(Type *Ty) const;
0413 
0414   /// Returns the maximum number of bytes that may be overwritten by
0415   /// storing the specified type.
0416   ///
0417   /// If Ty is a scalable vector type, the scalable property will be set and
0418   /// the runtime size will be a positive integer multiple of the base size.
0419   ///
0420   /// For example, returns 5 for i36 and 10 for x86_fp80.
0421   TypeSize getTypeStoreSize(Type *Ty) const {
0422     TypeSize StoreSizeInBits = getTypeStoreSizeInBits(Ty);
0423     return {StoreSizeInBits.getKnownMinValue() / 8,
0424             StoreSizeInBits.isScalable()};
0425   }
0426 
0427   /// Returns the maximum number of bits that may be overwritten by
0428   /// storing the specified type; always a multiple of 8.
0429   ///
0430   /// If Ty is a scalable vector type, the scalable property will be set and
0431   /// the runtime size will be a positive integer multiple of the base size.
0432   ///
0433   /// For example, returns 40 for i36 and 80 for x86_fp80.
0434   TypeSize getTypeStoreSizeInBits(Type *Ty) const {
0435     TypeSize BaseSize = getTypeSizeInBits(Ty);
0436     uint64_t AlignedSizeInBits =
0437         alignToPowerOf2(BaseSize.getKnownMinValue(), 8);
0438     return {AlignedSizeInBits, BaseSize.isScalable()};
0439   }
0440 
0441   /// Returns true if no extra padding bits are needed when storing the
0442   /// specified type.
0443   ///
0444   /// For example, returns false for i19 that has a 24-bit store size.
0445   bool typeSizeEqualsStoreSize(Type *Ty) const {
0446     return getTypeSizeInBits(Ty) == getTypeStoreSizeInBits(Ty);
0447   }
0448 
0449   /// Returns the offset in bytes between successive objects of the
0450   /// specified type, including alignment padding.
0451   ///
0452   /// If Ty is a scalable vector type, the scalable property will be set and
0453   /// the runtime size will be a positive integer multiple of the base size.
0454   ///
0455   /// This is the amount that alloca reserves for this type. For example,
0456   /// returns 12 or 16 for x86_fp80, depending on alignment.
0457   TypeSize getTypeAllocSize(Type *Ty) const {
0458     // Round up to the next alignment boundary.
0459     return alignTo(getTypeStoreSize(Ty), getABITypeAlign(Ty).value());
0460   }
0461 
0462   /// Returns the offset in bits between successive objects of the
0463   /// specified type, including alignment padding; always a multiple of 8.
0464   ///
0465   /// If Ty is a scalable vector type, the scalable property will be set and
0466   /// the runtime size will be a positive integer multiple of the base size.
0467   ///
0468   /// This is the amount that alloca reserves for this type. For example,
0469   /// returns 96 or 128 for x86_fp80, depending on alignment.
0470   TypeSize getTypeAllocSizeInBits(Type *Ty) const {
0471     return 8 * getTypeAllocSize(Ty);
0472   }
0473 
0474   /// Returns the minimum ABI-required alignment for the specified type.
0475   Align getABITypeAlign(Type *Ty) const;
0476 
0477   /// Helper function to return `Alignment` if it's set or the result of
0478   /// `getABITypeAlign(Ty)`, in any case the result is a valid alignment.
0479   inline Align getValueOrABITypeAlignment(MaybeAlign Alignment,
0480                                           Type *Ty) const {
0481     return Alignment ? *Alignment : getABITypeAlign(Ty);
0482   }
0483 
0484   /// Returns the minimum ABI-required alignment for an integer type of
0485   /// the specified bitwidth.
0486   Align getABIIntegerTypeAlignment(unsigned BitWidth) const {
0487     return getIntegerAlignment(BitWidth, /* abi_or_pref */ true);
0488   }
0489 
0490   /// Returns the preferred stack/global alignment for the specified
0491   /// type.
0492   ///
0493   /// This is always at least as good as the ABI alignment.
0494   Align getPrefTypeAlign(Type *Ty) const;
0495 
0496   /// Returns an integer type with size at least as big as that of a
0497   /// pointer in the given address space.
0498   IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
0499 
0500   /// Returns an integer (vector of integer) type with size at least as
0501   /// big as that of a pointer of the given pointer (vector of pointer) type.
0502   Type *getIntPtrType(Type *) const;
0503 
0504   /// Returns the smallest integer type with size at least as big as
0505   /// Width bits.
0506   Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const;
0507 
0508   /// Returns the largest legal integer type, or null if none are set.
0509   Type *getLargestLegalIntType(LLVMContext &C) const {
0510     unsigned LargestSize = getLargestLegalIntTypeSizeInBits();
0511     return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize);
0512   }
0513 
0514   /// Returns the size of largest legal integer type size, or 0 if none
0515   /// are set.
0516   unsigned getLargestLegalIntTypeSizeInBits() const;
0517 
0518   /// Returns the type of a GEP index in AddressSpace.
0519   /// If it was not specified explicitly, it will be the integer type of the
0520   /// pointer width - IntPtrType.
0521   IntegerType *getIndexType(LLVMContext &C, unsigned AddressSpace) const;
0522 
0523   /// Returns the type of a GEP index.
0524   /// If it was not specified explicitly, it will be the integer type of the
0525   /// pointer width - IntPtrType.
0526   Type *getIndexType(Type *PtrTy) const;
0527 
0528   /// Returns the offset from the beginning of the type for the specified
0529   /// indices.
0530   ///
0531   /// Note that this takes the element type, not the pointer type.
0532   /// This is used to implement getelementptr.
0533   int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef<Value *> Indices) const;
0534 
0535   /// Get GEP indices to access Offset inside ElemTy. ElemTy is updated to be
0536   /// the result element type and Offset to be the residual offset.
0537   SmallVector<APInt> getGEPIndicesForOffset(Type *&ElemTy, APInt &Offset) const;
0538 
0539   /// Get single GEP index to access Offset inside ElemTy. Returns std::nullopt
0540   /// if index cannot be computed, e.g. because the type is not an aggregate.
0541   /// ElemTy is updated to be the result element type and Offset to be the
0542   /// residual offset.
0543   std::optional<APInt> getGEPIndexForOffset(Type *&ElemTy, APInt &Offset) const;
0544 
0545   /// Returns a StructLayout object, indicating the alignment of the
0546   /// struct, its size, and the offsets of its fields.
0547   ///
0548   /// Note that this information is lazily cached.
0549   const StructLayout *getStructLayout(StructType *Ty) const;
0550 
0551   /// Returns the preferred alignment of the specified global.
0552   ///
0553   /// This includes an explicitly requested alignment (if the global has one).
0554   Align getPreferredAlign(const GlobalVariable *GV) const;
0555 };
0556 
0557 inline DataLayout *unwrap(LLVMTargetDataRef P) {
0558   return reinterpret_cast<DataLayout *>(P);
0559 }
0560 
0561 inline LLVMTargetDataRef wrap(const DataLayout *P) {
0562   return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P));
0563 }
0564 
0565 /// Used to lazily calculate structure layout information for a target machine,
0566 /// based on the DataLayout structure.
0567 class StructLayout final : public TrailingObjects<StructLayout, TypeSize> {
0568   TypeSize StructSize;
0569   Align StructAlignment;
0570   unsigned IsPadded : 1;
0571   unsigned NumElements : 31;
0572 
0573 public:
0574   TypeSize getSizeInBytes() const { return StructSize; }
0575 
0576   TypeSize getSizeInBits() const { return 8 * StructSize; }
0577 
0578   Align getAlignment() const { return StructAlignment; }
0579 
0580   /// Returns whether the struct has padding or not between its fields.
0581   /// NB: Padding in nested element is not taken into account.
0582   bool hasPadding() const { return IsPadded; }
0583 
0584   /// Given a valid byte offset into the structure, returns the structure
0585   /// index that contains it.
0586   unsigned getElementContainingOffset(uint64_t FixedOffset) const;
0587 
0588   MutableArrayRef<TypeSize> getMemberOffsets() {
0589     return llvm::MutableArrayRef(getTrailingObjects<TypeSize>(), NumElements);
0590   }
0591 
0592   ArrayRef<TypeSize> getMemberOffsets() const {
0593     return llvm::ArrayRef(getTrailingObjects<TypeSize>(), NumElements);
0594   }
0595 
0596   TypeSize getElementOffset(unsigned Idx) const {
0597     assert(Idx < NumElements && "Invalid element idx!");
0598     return getMemberOffsets()[Idx];
0599   }
0600 
0601   TypeSize getElementOffsetInBits(unsigned Idx) const {
0602     return getElementOffset(Idx) * 8;
0603   }
0604 
0605 private:
0606   friend class DataLayout; // Only DataLayout can create this class
0607 
0608   StructLayout(StructType *ST, const DataLayout &DL);
0609 
0610   size_t numTrailingObjects(OverloadToken<TypeSize>) const {
0611     return NumElements;
0612   }
0613 };
0614 
0615 // The implementation of this method is provided inline as it is particularly
0616 // well suited to constant folding when called on a specific Type subclass.
0617 inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
0618   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
0619   switch (Ty->getTypeID()) {
0620   case Type::LabelTyID:
0621     return TypeSize::getFixed(getPointerSizeInBits(0));
0622   case Type::PointerTyID:
0623     return TypeSize::getFixed(
0624         getPointerSizeInBits(Ty->getPointerAddressSpace()));
0625   case Type::ArrayTyID: {
0626     ArrayType *ATy = cast<ArrayType>(Ty);
0627     return ATy->getNumElements() *
0628            getTypeAllocSizeInBits(ATy->getElementType());
0629   }
0630   case Type::StructTyID:
0631     // Get the layout annotation... which is lazily created on demand.
0632     return getStructLayout(cast<StructType>(Ty))->getSizeInBits();
0633   case Type::IntegerTyID:
0634     return TypeSize::getFixed(Ty->getIntegerBitWidth());
0635   case Type::HalfTyID:
0636   case Type::BFloatTyID:
0637     return TypeSize::getFixed(16);
0638   case Type::FloatTyID:
0639     return TypeSize::getFixed(32);
0640   case Type::DoubleTyID:
0641     return TypeSize::getFixed(64);
0642   case Type::PPC_FP128TyID:
0643   case Type::FP128TyID:
0644     return TypeSize::getFixed(128);
0645   case Type::X86_AMXTyID:
0646     return TypeSize::getFixed(8192);
0647   // In memory objects this is always aligned to a higher boundary, but
0648   // only 80 bits contain information.
0649   case Type::X86_FP80TyID:
0650     return TypeSize::getFixed(80);
0651   case Type::FixedVectorTyID:
0652   case Type::ScalableVectorTyID: {
0653     VectorType *VTy = cast<VectorType>(Ty);
0654     auto EltCnt = VTy->getElementCount();
0655     uint64_t MinBits = EltCnt.getKnownMinValue() *
0656                        getTypeSizeInBits(VTy->getElementType()).getFixedValue();
0657     return TypeSize(MinBits, EltCnt.isScalable());
0658   }
0659   case Type::TargetExtTyID: {
0660     Type *LayoutTy = cast<TargetExtType>(Ty)->getLayoutType();
0661     return getTypeSizeInBits(LayoutTy);
0662   }
0663   default:
0664     llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
0665   }
0666 }
0667 
0668 } // end namespace llvm
0669 
0670 #endif // LLVM_IR_DATALAYOUT_H