DataLayout.h revision 353358
12061Sjkh//===- llvm/DataLayout.h - Data size & alignment info -----------*- C++ -*-===// 250479Speter// 32061Sjkh// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 438666Sjb// See https://llvm.org/LICENSE.txt for license information. 532427Sjb// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6111131Sru// 7111131Sru//===----------------------------------------------------------------------===// 8217733Sbz// 9217733Sbz// This file defines layout properties related to datatype size/offset/alignment 1038666Sjb// information. It uses lazy annotations to cache information about how 1138666Sjb// structure types are laid out and used. 1238666Sjb// 13159363Strhodes// This structure should be created once, filled in if the defaults are not 1464049Salex// correct and then passed around by const&. None of the members functions 1564049Salex// require modification to the object. 16116679Ssimokawa// 1766071Smarkm//===----------------------------------------------------------------------===// 18116679Ssimokawa 1973504Sobrien#ifndef LLVM_IR_DATALAYOUT_H 20204661Simp#define LLVM_IR_DATALAYOUT_H 21232907Sjmallett 22158962Snetchild#include "llvm/ADT/ArrayRef.h" 23223148Sru#include "llvm/ADT/STLExtras.h" 24169597Sdes#include "llvm/ADT/SmallVector.h" 25169597Sdes#include "llvm/ADT/StringRef.h" 26169597Sdes#include "llvm/IR/DerivedTypes.h" 27169597Sdes#include "llvm/IR/Type.h" 28231821Spluknet#include "llvm/Pass.h" 29169597Sdes#include "llvm/Support/Casting.h" 30169597Sdes#include "llvm/Support/ErrorHandling.h" 31169597Sdes#include "llvm/Support/MathExtras.h" 32217815Sbz#include <cassert> 33217815Sbz#include <cstdint> 34218524Sjhb#include <string> 3532427Sjb 3638666Sjb// This needs to be outside of the namespace, to avoid conflict with llvm-c 37108451Sschweikh// decl. 3838666Sjbusing LLVMTargetDataRef = struct LLVMOpaqueTargetData *; 3938666Sjb 4038666Sjbnamespace llvm { 4138666Sjb 4217308Speterclass GlobalVariable; 43217273Simpclass LLVMContext; 44217294Simpclass Module; 4519175Sbdeclass StructLayout; 4696205Sjwdclass Triple; 47217297Simpclass Value; 48217297Simp 4938042Sbde/// Enum used to categorize the alignment types stored by LayoutAlignElem 5096205Sjwdenum AlignTypeEnum { 5196205Sjwd INVALID_ALIGN = 0, 5238042Sbde INTEGER_ALIGN = 'i', 5396205Sjwd VECTOR_ALIGN = 'v', 54159363Strhodes FLOAT_ALIGN = 'f', 55159363Strhodes AGGREGATE_ALIGN = 'a' 5617308Speter}; 5796205Sjwd 5896205Sjwd// FIXME: Currently the DataLayout string carries a "preferred alignment" 5917308Speter// for types. As the DataLayout is module/global, this should likely be 60148330Snetchild// sunk down to an FTTI element that is queried rather than a global 61148330Snetchild// preference. 62148330Snetchild 63148330Snetchild/// Layout alignment element. 64159831Sobrien/// 65148330Snetchild/// Stores the alignment data associated with a given alignment type (integer, 66148330Snetchild/// vector, float) and type bit width. 67148330Snetchild/// 68148330Snetchild/// \note The unusual order of elements in the structure attempts to reduce 69220512Sdougb/// padding and make the structure slightly more cache friendly. 70148330Snetchildstruct LayoutAlignElem { 71148330Snetchild /// Alignment type from \c AlignTypeEnum 7296205Sjwd unsigned AlignType : 8; 7396205Sjwd unsigned TypeBitWidth : 24; 7496205Sjwd unsigned ABIAlign : 16; 75162147Sru unsigned PrefAlign : 16; 76162147Sru 7798723Sdillon static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align, 7898723Sdillon unsigned pref_align, uint32_t bit_width); 7998723Sdillon 8038666Sjb bool operator==(const LayoutAlignElem &rhs) const; 8138666Sjb}; 8217308Speter 83123311Speter/// Layout pointer alignment element. 84123311Speter/// 85123311Speter/// Stores the alignment data associated with a given pointer and address space. 86123311Speter/// 87175833Sjhb/// \note The unusual order of elements in the structure attempts to reduce 88175833Sjhb/// padding and make the structure slightly more cache friendly. 89169597Sdesstruct PointerAlignElem { 90169597Sdes unsigned ABIAlign; 91169597Sdes unsigned PrefAlign; 92169597Sdes uint32_t TypeByteWidth; 93219177Snwhitehorn uint32_t AddressSpace; 94219177Snwhitehorn uint32_t IndexWidth; 95238051Sobrien 96219177Snwhitehorn /// Initializer 97219177Snwhitehorn static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign, 98158962Snetchild unsigned PrefAlign, uint32_t TypeByteWidth, 99156840Sru uint32_t IndexWidth); 100123311Speter 101137288Speter bool operator==(const PointerAlignElem &rhs) const; 102209128Sraj}; 103209128Sraj 104156740Sru/// A parsed version of the target data layout string in and methods for 1052061Sjkh/// querying it. 10697769Sru/// 10797252Sru/// The target data layout string is specified *by the target* - a frontend 108119579Sru/// generating LLVM IR is required to generate the right target data for the 10997252Sru/// target being codegen'd to. 11095730Sruclass DataLayout { 11195793Srupublic: 112111617Sru enum class FunctionPtrAlignType { 11395730Sru /// The function pointer alignment is independent of the function alignment. 114116679Ssimokawa Independent, 11595730Sru /// The function pointer alignment is a multiple of the function alignment. 116116679Ssimokawa MultipleOfFunctionAlign, 11795730Sru }; 118110035Sruprivate: 119107516Sru /// Defaults to false. 120138921Sru bool BigEndian; 121156145Syar 122138921Sru unsigned AllocaAddrSpace; 123133942Sru unsigned StackNaturalAlign; 124133942Sru unsigned ProgramAddrSpace; 125156145Syar 126133942Sru unsigned FunctionPtrAlign; 127110035Sru FunctionPtrAlignType TheFunctionPtrAlignType; 128117234Sru 129110035Sru enum ManglingModeT { 130117229Sru MM_None, 131218206Simp MM_ELF, 13254324Smarcel MM_MachO, 133218130Simp MM_WinCOFF, 134218130Simp MM_WinCOFFX86, 135233644Sjmallett MM_Mips 136218130Simp }; 137218130Simp ManglingModeT ManglingMode; 138233644Sjmallett 139218130Simp SmallVector<unsigned char, 8> LegalIntWidths; 140233644Sjmallett 141218130Simp /// Primitive type alignment data. This is sorted by type and bit 142233644Sjmallett /// width during construction. 143233644Sjmallett using AlignmentsTy = SmallVector<LayoutAlignElem, 16>; 144233644Sjmallett AlignmentsTy Alignments; 145218130Simp 146233644Sjmallett AlignmentsTy::const_iterator 147233644Sjmallett findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth) const { 148218130Simp return const_cast<DataLayout *>(this)->findAlignmentLowerBound(AlignType, 149218130Simp BitWidth); 150218130Simp } 151218130Simp 152218130Simp AlignmentsTy::iterator 153218130Simp findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth); 154218130Simp 155218130Simp /// The string representation used to create this DataLayout 156218130Simp std::string StringRepresentation; 157218130Simp 158218130Simp using PointersTy = SmallVector<PointerAlignElem, 8>; 159218130Simp PointersTy Pointers; 160218130Simp 161218130Simp PointersTy::const_iterator 162218130Simp findPointerLowerBound(uint32_t AddressSpace) const { 163218130Simp return const_cast<DataLayout *>(this)->findPointerLowerBound(AddressSpace); 16417308Speter } 165119519Smarcel 166119519Smarcel PointersTy::iterator findPointerLowerBound(uint32_t AddressSpace); 167119519Smarcel 168119519Smarcel // The StructType -> StructLayout map. 169119519Smarcel mutable void *LayoutMap = nullptr; 170119519Smarcel 171119579Sru /// Pointers in these address spaces are non-integral, and don't have a 172119519Smarcel /// well-defined bitwise representation. 173119519Smarcel SmallVector<unsigned, 8> NonIntegralAddressSpaces; 174119519Smarcel 175119519Smarcel void setAlignment(AlignTypeEnum align_type, unsigned abi_align, 176119519Smarcel unsigned pref_align, uint32_t bit_width); 177126031Sgad unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width, 178126024Sgad bool ABIAlign, Type *Ty) const; 179126024Sgad void setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign, 180126024Sgad unsigned PrefAlign, uint32_t TypeByteWidth, 181126024Sgad uint32_t IndexWidth); 182126024Sgad 183126024Sgad /// Internal helper method that returns requested alignment for type. 184126024Sgad unsigned getAlignment(Type *Ty, bool abi_or_pref) const; 185227771Sgjb 186126024Sgad /// Parses a target data specification string. Assert if the string is 187126024Sgad /// malformed. 188227769Sgjb void parseSpecifier(StringRef LayoutDescription); 189227771Sgjb 190227771Sgjb // Free all internal data structures. 191126024Sgad void clear(); 192126024Sgad 193126031Sgadpublic: 194126024Sgad /// Constructs a DataLayout from a specification string. See reset(). 195126024Sgad explicit DataLayout(StringRef LayoutDescription) { 196126024Sgad reset(LayoutDescription); 197172744Sdelphij } 198126024Sgad 199126024Sgad /// Initialize target data from properties stored in the module. 200126024Sgad explicit DataLayout(const Module *M); 201133376Sharti 202126024Sgad DataLayout(const DataLayout &DL) { *this = DL; } 203126024Sgad 204172744Sdelphij ~DataLayout(); // Not virtual, do not subclass this class 205126024Sgad 206126024Sgad DataLayout &operator=(const DataLayout &DL) { 207125885Sgad clear(); 208125885Sgad StringRepresentation = DL.StringRepresentation; 20938666Sjb BigEndian = DL.isBigEndian(); 21017308Speter AllocaAddrSpace = DL.AllocaAddrSpace; 211119519Smarcel StackNaturalAlign = DL.StackNaturalAlign; 212119579Sru FunctionPtrAlign = DL.FunctionPtrAlign; 213218206Simp TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType; 2142302Spaul ProgramAddrSpace = DL.ProgramAddrSpace; 21539206Sjkh ManglingMode = DL.ManglingMode; 21639206Sjkh LegalIntWidths = DL.LegalIntWidths; 21739206Sjkh Alignments = DL.Alignments; 218133945Sru Pointers = DL.Pointers; 219220556Sbz NonIntegralAddressSpaces = DL.NonIntegralAddressSpaces; 220177609Sru return *this; 221177609Sru } 222177609Sru 223133945Sru bool operator==(const DataLayout &Other) const; 224132358Smarkm bool operator!=(const DataLayout &Other) const { return !(*this == Other); } 22517308Speter 22654324Smarcel void init(const Module *M); 22754324Smarcel 228132234Smarcel /// Parse a data layout string (with fallback to default values). 229132234Smarcel void reset(StringRef LayoutDescription); 230132234Smarcel 231132234Smarcel /// Layout endianness... 23254324Smarcel bool isLittleEndian() const { return !BigEndian; } 23354324Smarcel bool isBigEndian() const { return BigEndian; } 23454324Smarcel 235118531Sru /// Returns the string representation of the DataLayout. 23654324Smarcel /// 23754324Smarcel /// This representation is in the same format accepted by the string 23854324Smarcel /// constructor above. This should not be used to compare two DataLayout as 23954324Smarcel /// different string can represent the same layout. 24054324Smarcel const std::string &getStringRepresentation() const { 24154324Smarcel return StringRepresentation; 242133376Sharti } 24354324Smarcel 244133376Sharti /// Test if the DataLayout was constructed from an empty string. 245133376Sharti bool isDefault() const { return StringRepresentation.empty(); } 24654324Smarcel 24754324Smarcel /// Returns true if the specified type is known to be a native integer 24854324Smarcel /// type supported by the CPU. 24954324Smarcel /// 25054324Smarcel /// For example, i64 is not native on most 32-bit CPUs and i37 is not native 251133376Sharti /// on any known one. This returns false if the integer width is not legal. 25254324Smarcel /// 25354324Smarcel /// The width is specified in bits. 25454324Smarcel bool isLegalInteger(uint64_t Width) const { 255118531Sru for (unsigned LegalIntWidth : LegalIntWidths) 256118531Sru if (LegalIntWidth == Width) 25754324Smarcel return true; 258132234Smarcel return false; 259132234Smarcel } 260132234Smarcel 261132234Smarcel bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); } 262132234Smarcel 263132588Skensmith /// Returns true if the given alignment exceeds the natural stack alignment. 264132358Smarkm bool exceedsNaturalStackAlignment(unsigned Align) const { 265132234Smarcel return (StackNaturalAlign != 0) && (Align > StackNaturalAlign); 266132358Smarkm } 267132234Smarcel 268132234Smarcel unsigned getStackAlignment() const { return StackNaturalAlign; } 269132234Smarcel unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; } 27054324Smarcel 27154324Smarcel /// Returns the alignment of function pointers, which may or may not be 27295730Sru /// related to the alignment of functions. 27395730Sru /// \see getFunctionPtrAlignType 27495730Sru unsigned getFunctionPtrAlign() const { return FunctionPtrAlign; } 27595730Sru 27695730Sru /// Return the type of function pointer alignment. 27795730Sru /// \see getFunctionPtrAlign 27895730Sru FunctionPtrAlignType getFunctionPtrAlignType() const { 27938666Sjb return TheFunctionPtrAlignType; 280107374Sru } 28117308Speter 28255678Smarcel unsigned getProgramAddressSpace() const { return ProgramAddrSpace; } 283143032Sharti 284138515Sharti bool hasMicrosoftFastStdCallMangling() const { 285117793Sru return ManglingMode == MM_WinCOFFX86; 286110035Sru } 287174564Simp 288110035Sru /// Returns true if symbols with leading question marks should not receive IR 2892061Sjkh /// mangling. True for Windows mangling modes. 29017308Speter bool doNotMangleLeadingQuestionMark() const { 291107516Sru return ManglingMode == MM_WinCOFF || ManglingMode == MM_WinCOFFX86; 292174539Simp } 293174539Simp 29455678Smarcel bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; } 295107516Sru 296107516Sru StringRef getLinkerPrivateGlobalPrefix() const { 297107516Sru if (ManglingMode == MM_MachO) 298174564Simp return "l"; 299107516Sru return ""; 300139112Sru } 301164470Sjb 302107516Sru char getGlobalPrefix() const { 303122204Skris switch (ManglingMode) { 30455678Smarcel case MM_None: 30555678Smarcel case MM_ELF: 306116696Sru case MM_Mips: 30755678Smarcel case MM_WinCOFF: 308133376Sharti return '\0'; 309107516Sru case MM_MachO: 310107516Sru case MM_WinCOFFX86: 311107516Sru return '_'; 312107516Sru } 31355678Smarcel llvm_unreachable("invalid mangling mode"); 314185499Salfred } 315218524Sjhb 316185499Salfred StringRef getPrivateGlobalPrefix() const { 317218524Sjhb switch (ManglingMode) { 318218524Sjhb case MM_None: 319218524Sjhb return ""; 320232907Sjmallett case MM_ELF: 321232907Sjmallett case MM_WinCOFF: 322232907Sjmallett return ".L"; 32355678Smarcel case MM_Mips: 324111131Sru return "$"; 325111131Sru case MM_MachO: 326111131Sru case MM_WinCOFFX86: 327133945Sru return "L"; 328111131Sru } 329111131Sru llvm_unreachable("invalid mangling mode"); 330217125Simp } 331221869Sattilio 332216520Snwhitehorn static const char *getManglingComponent(const Triple &T); 333233644Sjmallett 334216520Snwhitehorn /// Returns true if the specified type fits in a native integer type 335216520Snwhitehorn /// supported by the CPU. 336216520Snwhitehorn /// 337216520Snwhitehorn /// For example, if the CPU only supports i32 as a native integer type, then 338216520Snwhitehorn /// i27 fits in a legal integer type but i45 does not. 339168280Smarcel bool fitsInLegalInteger(unsigned Width) const { 340218524Sjhb for (unsigned LegalIntWidth : LegalIntWidths) 341218524Sjhb if (Width <= LegalIntWidth) 342218524Sjhb return true; 343218524Sjhb return false; 344218524Sjhb } 345219137Sjhb 346218524Sjhb /// Layout pointer alignment 347217125Simp unsigned getPointerABIAlignment(unsigned AS) const; 348217815Sbz 349217125Simp /// Return target's alignment for stack-based pointers 350217125Simp /// FIXME: The defaults need to be removed once all of 351217125Simp /// the backends/clients are updated. 352217125Simp unsigned getPointerPrefAlignment(unsigned AS = 0) const; 353217125Simp 354217125Simp /// Layout pointer size 355185499Salfred /// FIXME: The defaults need to be removed once all of 356217735Sbz /// the backends/clients are updated. 357185499Salfred unsigned getPointerSize(unsigned AS = 0) const; 358185499Salfred 359185499Salfred /// Returns the maximum pointer size over all address spaces. 360185499Salfred unsigned getMaxPointerSize() const; 361185499Salfred 362133945Sru // Index size used for address calculation. 363133945Sru unsigned getIndexSize(unsigned AS) const; 364103985Sphk 365103985Sphk /// Return the address spaces containing non-integral pointers. Pointers in 366103985Sphk /// this address space don't have a well-defined bitwise representation. 367185499Salfred ArrayRef<unsigned> getNonIntegralAddressSpaces() const { 368217754Sbz return NonIntegralAddressSpaces; 369185499Salfred } 370168280Smarcel 371162147Sru bool isNonIntegralAddressSpace(unsigned AddrSpace) const { 372162147Sru ArrayRef<unsigned> NonIntegralSpaces = getNonIntegralAddressSpaces(); 373216520Snwhitehorn return find(NonIntegralSpaces, AddrSpace) != NonIntegralSpaces.end(); 374216520Snwhitehorn } 375216520Snwhitehorn 376179232Sjb bool isNonIntegralPointerType(PointerType *PT) const { 377216520Snwhitehorn return isNonIntegralAddressSpace(PT->getAddressSpace()); 378216520Snwhitehorn } 379216520Snwhitehorn 380218524Sjhb bool isNonIntegralPointerType(Type *Ty) const { 381185250Sdes auto *PTy = dyn_cast<PointerType>(Ty); 382218524Sjhb return PTy && isNonIntegralPointerType(PTy); 383162147Sru } 384216520Snwhitehorn 385218524Sjhb /// Layout pointer size, in bits 386218524Sjhb /// FIXME: The defaults need to be removed once all of 387218524Sjhb /// the backends/clients are updated. 388216520Snwhitehorn unsigned getPointerSizeInBits(unsigned AS = 0) const { 389218524Sjhb return getPointerSize(AS) * 8; 390216520Snwhitehorn } 391179232Sjb 392205290Sdougb /// Returns the maximum pointer size over all address spaces. 393219137Sjhb unsigned getMaxPointerSizeInBits() const { 394219137Sjhb return getMaxPointerSize() * 8; 395185250Sdes } 396185499Salfred 397185499Salfred /// Size in bits of index used for address calculation in getelementptr. 398103985Sphk unsigned getIndexSizeInBits(unsigned AS) const { 399201815Sbz return getIndexSize(AS) * 8; 400201815Sbz } 401205290Sdougb 402201815Sbz /// Layout pointer size, in bits, based on the type. If this function is 403201815Sbz /// called with a pointer type, then the type size of the pointer is returned. 404201815Sbz /// If this function is called with a vector of pointers, then the type size 405202095Sbz /// of the pointer is returned. This should only be called with a pointer or 406202095Sbz /// vector of pointers. 407202095Sbz unsigned getPointerTypeSizeInBits(Type *) const; 408219137Sjhb 409201815Sbz /// Layout size of the index used in GEP calculation. 410201815Sbz /// The function should be called with pointer or vector of pointers type. 411201815Sbz unsigned getIndexTypeSizeInBits(Type *Ty) const; 412148154Sru 413219137Sjhb unsigned getPointerTypeSize(Type *Ty) const { 414219137Sjhb return getPointerTypeSizeInBits(Ty) / 8; 415216934Simp } 416216934Simp 417216934Simp /// Size examples: 418216934Simp /// 419216520Snwhitehorn /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*] 420216520Snwhitehorn /// ---- ---------- --------------- --------------- 421185250Sdes /// i1 1 8 8 422185250Sdes /// i8 8 8 8 423201815Sbz /// i19 19 24 32 424216520Snwhitehorn /// i32 32 32 32 425148154Sru /// i100 100 104 128 426201815Sbz /// i128 128 128 128 427201815Sbz /// Float 32 32 32 428201815Sbz /// Double 64 64 64 429148154Sru /// X86_FP80 80 80 96 430133945Sru /// 431133945Sru /// [*] The alloc size depends on the alignment, and thus on the target. 432103985Sphk /// These values are for x86-32 linux. 433118531Sru 434118531Sru /// Returns the number of bits necessary to hold the specified type. 435103985Sphk /// 436185499Salfred /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must 437185499Salfred /// have a size (Type::isSized() must return true). 438185499Salfred uint64_t getTypeSizeInBits(Type *Ty) const; 439185499Salfred 440185499Salfred /// Returns the maximum number of bytes that may be overwritten by 441185499Salfred /// storing the specified type. 442133945Sru /// 443185499Salfred /// For example, returns 5 for i36 and 10 for x86_fp80. 444 uint64_t getTypeStoreSize(Type *Ty) const { 445 return (getTypeSizeInBits(Ty) + 7) / 8; 446 } 447 448 /// Returns the maximum number of bits that may be overwritten by 449 /// storing the specified type; always a multiple of 8. 450 /// 451 /// For example, returns 40 for i36 and 80 for x86_fp80. 452 uint64_t getTypeStoreSizeInBits(Type *Ty) const { 453 return 8 * getTypeStoreSize(Ty); 454 } 455 456 /// Returns true if no extra padding bits are needed when storing the 457 /// specified type. 458 /// 459 /// For example, returns false for i19 that has a 24-bit store size. 460 bool typeSizeEqualsStoreSize(Type *Ty) const { 461 return getTypeSizeInBits(Ty) == getTypeStoreSizeInBits(Ty); 462 } 463 464 /// Returns the offset in bytes between successive objects of the 465 /// specified type, including alignment padding. 466 /// 467 /// This is the amount that alloca reserves for this type. For example, 468 /// returns 12 or 16 for x86_fp80, depending on alignment. 469 uint64_t getTypeAllocSize(Type *Ty) const { 470 // Round up to the next alignment boundary. 471 return alignTo(getTypeStoreSize(Ty), getABITypeAlignment(Ty)); 472 } 473 474 /// Returns the offset in bits between successive objects of the 475 /// specified type, including alignment padding; always a multiple of 8. 476 /// 477 /// This is the amount that alloca reserves for this type. For example, 478 /// returns 96 or 128 for x86_fp80, depending on alignment. 479 uint64_t getTypeAllocSizeInBits(Type *Ty) const { 480 return 8 * getTypeAllocSize(Ty); 481 } 482 483 /// Returns the minimum ABI-required alignment for the specified type. 484 unsigned getABITypeAlignment(Type *Ty) const; 485 486 /// Returns the minimum ABI-required alignment for an integer type of 487 /// the specified bitwidth. 488 unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const; 489 490 /// Returns the preferred stack/global alignment for the specified 491 /// type. 492 /// 493 /// This is always at least as good as the ABI alignment. 494 unsigned getPrefTypeAlignment(Type *Ty) const; 495 496 /// Returns the preferred alignment for the specified type, returned as 497 /// log2 of the value (a shift amount). 498 unsigned getPreferredTypeAlignmentShift(Type *Ty) const; 499 500 /// Returns an integer type with size at least as big as that of a 501 /// pointer in the given address space. 502 IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const; 503 504 /// Returns an integer (vector of integer) type with size at least as 505 /// big as that of a pointer of the given pointer (vector of pointer) type. 506 Type *getIntPtrType(Type *) const; 507 508 /// Returns the smallest integer type with size at least as big as 509 /// Width bits. 510 Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const; 511 512 /// Returns the largest legal integer type, or null if none are set. 513 Type *getLargestLegalIntType(LLVMContext &C) const { 514 unsigned LargestSize = getLargestLegalIntTypeSizeInBits(); 515 return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize); 516 } 517 518 /// Returns the size of largest legal integer type size, or 0 if none 519 /// are set. 520 unsigned getLargestLegalIntTypeSizeInBits() const; 521 522 /// Returns the type of a GEP index. 523 /// If it was not specified explicitly, it will be the integer type of the 524 /// pointer width - IntPtrType. 525 Type *getIndexType(Type *PtrTy) const; 526 527 /// Returns the offset from the beginning of the type for the specified 528 /// indices. 529 /// 530 /// Note that this takes the element type, not the pointer type. 531 /// This is used to implement getelementptr. 532 int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef<Value *> Indices) const; 533 534 /// Returns a StructLayout object, indicating the alignment of the 535 /// struct, its size, and the offsets of its fields. 536 /// 537 /// Note that this information is lazily cached. 538 const StructLayout *getStructLayout(StructType *Ty) const; 539 540 /// Returns the preferred alignment of the specified global. 541 /// 542 /// This includes an explicitly requested alignment (if the global has one). 543 unsigned getPreferredAlignment(const GlobalVariable *GV) const; 544 545 /// Returns the preferred alignment of the specified global, returned 546 /// in log form. 547 /// 548 /// This includes an explicitly requested alignment (if the global has one). 549 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const; 550}; 551 552inline DataLayout *unwrap(LLVMTargetDataRef P) { 553 return reinterpret_cast<DataLayout *>(P); 554} 555 556inline LLVMTargetDataRef wrap(const DataLayout *P) { 557 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P)); 558} 559 560/// Used to lazily calculate structure layout information for a target machine, 561/// based on the DataLayout structure. 562class StructLayout { 563 uint64_t StructSize; 564 unsigned StructAlignment; 565 unsigned IsPadded : 1; 566 unsigned NumElements : 31; 567 uint64_t MemberOffsets[1]; // variable sized array! 568 569public: 570 uint64_t getSizeInBytes() const { return StructSize; } 571 572 uint64_t getSizeInBits() const { return 8 * StructSize; } 573 574 unsigned getAlignment() const { return StructAlignment; } 575 576 /// Returns whether the struct has padding or not between its fields. 577 /// NB: Padding in nested element is not taken into account. 578 bool hasPadding() const { return IsPadded; } 579 580 /// Given a valid byte offset into the structure, returns the structure 581 /// index that contains it. 582 unsigned getElementContainingOffset(uint64_t Offset) const; 583 584 uint64_t getElementOffset(unsigned Idx) const { 585 assert(Idx < NumElements && "Invalid element idx!"); 586 return MemberOffsets[Idx]; 587 } 588 589 uint64_t getElementOffsetInBits(unsigned Idx) const { 590 return getElementOffset(Idx) * 8; 591 } 592 593private: 594 friend class DataLayout; // Only DataLayout can create this class 595 596 StructLayout(StructType *ST, const DataLayout &DL); 597}; 598 599// The implementation of this method is provided inline as it is particularly 600// well suited to constant folding when called on a specific Type subclass. 601inline uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const { 602 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); 603 switch (Ty->getTypeID()) { 604 case Type::LabelTyID: 605 return getPointerSizeInBits(0); 606 case Type::PointerTyID: 607 return getPointerSizeInBits(Ty->getPointerAddressSpace()); 608 case Type::ArrayTyID: { 609 ArrayType *ATy = cast<ArrayType>(Ty); 610 return ATy->getNumElements() * 611 getTypeAllocSizeInBits(ATy->getElementType()); 612 } 613 case Type::StructTyID: 614 // Get the layout annotation... which is lazily created on demand. 615 return getStructLayout(cast<StructType>(Ty))->getSizeInBits(); 616 case Type::IntegerTyID: 617 return Ty->getIntegerBitWidth(); 618 case Type::HalfTyID: 619 return 16; 620 case Type::FloatTyID: 621 return 32; 622 case Type::DoubleTyID: 623 case Type::X86_MMXTyID: 624 return 64; 625 case Type::PPC_FP128TyID: 626 case Type::FP128TyID: 627 return 128; 628 // In memory objects this is always aligned to a higher boundary, but 629 // only 80 bits contain information. 630 case Type::X86_FP80TyID: 631 return 80; 632 case Type::VectorTyID: { 633 VectorType *VTy = cast<VectorType>(Ty); 634 return VTy->getNumElements() * getTypeSizeInBits(VTy->getElementType()); 635 } 636 default: 637 llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type"); 638 } 639} 640 641} // end namespace llvm 642 643#endif // LLVM_IR_DATALAYOUT_H 644