1 //===--------- llvm/DataLayout.h - Data size & alignment info ---*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines layout properties related to datatype size/offset/alignment 11 // information. It uses lazy annotations to cache information about how 12 // structure types are laid out and used. 13 // 14 // This structure should be created once, filled in if the defaults are not 15 // correct and then passed around by const&. None of the members functions 16 // require modification to the object. 17 // 18 //===----------------------------------------------------------------------===// 19 20 #ifndef LLVM_IR_DATALAYOUT_H 21 #define LLVM_IR_DATALAYOUT_H 22 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/IR/DerivedTypes.h" 26 #include "llvm/IR/Type.h" 27 #include "llvm/Pass.h" 28 #include "llvm/Support/DataTypes.h" 29 30 // This needs to be outside of the namespace, to avoid conflict with llvm-c 31 // decl. 32 typedef struct LLVMOpaqueTargetData *LLVMTargetDataRef; 33 34 namespace llvm { 35 36 class Value; 37 class StructType; 38 class StructLayout; 39 class Triple; 40 class GlobalVariable; 41 class LLVMContext; 42 template<typename T> 43 class ArrayRef; 44 45 /// Enum used to categorize the alignment types stored by LayoutAlignElem 46 enum AlignTypeEnum { 47 INVALID_ALIGN = 0, 48 INTEGER_ALIGN = 'i', 49 VECTOR_ALIGN = 'v', 50 FLOAT_ALIGN = 'f', 51 AGGREGATE_ALIGN = 'a' 52 }; 53 54 // FIXME: Currently the DataLayout string carries a "preferred alignment" 55 // for types. As the DataLayout is module/global, this should likely be 56 // sunk down to an FTTI element that is queried rather than a global 57 // preference. 58 59 /// \brief Layout alignment element. 60 /// 61 /// Stores the alignment data associated with a given alignment type (integer, 62 /// vector, float) and type bit width. 63 /// 64 /// \note The unusual order of elements in the structure attempts to reduce 65 /// padding and make the structure slightly more cache friendly. 66 struct LayoutAlignElem { 67 /// \brief Alignment type from \c AlignTypeEnum 68 unsigned AlignType : 8; 69 unsigned TypeBitWidth : 24; 70 unsigned ABIAlign : 16; 71 unsigned PrefAlign : 16; 72 73 static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align, 74 unsigned pref_align, uint32_t bit_width); 75 bool operator==(const LayoutAlignElem &rhs) const; 76 }; 77 78 /// \brief Layout pointer alignment element. 79 /// 80 /// Stores the alignment data associated with a given pointer and address space. 81 /// 82 /// \note The unusual order of elements in the structure attempts to reduce 83 /// padding and make the structure slightly more cache friendly. 84 struct PointerAlignElem { 85 unsigned ABIAlign; 86 unsigned PrefAlign; 87 uint32_t TypeByteWidth; 88 uint32_t AddressSpace; 89 90 /// Initializer 91 static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign, 92 unsigned PrefAlign, uint32_t TypeByteWidth); 93 bool operator==(const PointerAlignElem &rhs) const; 94 }; 95 96 /// \brief A parsed version of the target data layout string in and methods for 97 /// querying it. 98 /// 99 /// The target data layout string is specified *by the target* - a frontend 100 /// generating LLVM IR is required to generate the right target data for the 101 /// target being codegen'd to. 102 class DataLayout { 103 private: 104 /// Defaults to false. 105 bool BigEndian; 106 107 unsigned AllocaAddrSpace; 108 unsigned StackNaturalAlign; 109 110 enum ManglingModeT { 111 MM_None, 112 MM_ELF, 113 MM_MachO, 114 MM_WinCOFF, 115 MM_WinCOFFX86, 116 MM_Mips 117 }; 118 ManglingModeT ManglingMode; 119 120 SmallVector<unsigned char, 8> LegalIntWidths; 121 122 /// \brief Primitive type alignment data. This is sorted by type and bit 123 /// width during construction. 124 typedef SmallVector<LayoutAlignElem, 16> AlignmentsTy; 125 AlignmentsTy Alignments; 126 127 AlignmentsTy::const_iterator 128 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth) const { 129 return const_cast<DataLayout *>(this)->findAlignmentLowerBound(AlignType, 130 BitWidth); 131 } 132 133 AlignmentsTy::iterator 134 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth); 135 136 /// \brief The string representation used to create this DataLayout 137 std::string StringRepresentation; 138 139 typedef SmallVector<PointerAlignElem, 8> PointersTy; 140 PointersTy Pointers; 141 142 PointersTy::const_iterator 143 findPointerLowerBound(uint32_t AddressSpace) const { 144 return const_cast<DataLayout *>(this)->findPointerLowerBound(AddressSpace); 145 } 146 147 PointersTy::iterator findPointerLowerBound(uint32_t AddressSpace); 148 149 // The StructType -> StructLayout map. 150 mutable void *LayoutMap; 151 152 /// Pointers in these address spaces are non-integral, and don't have a 153 /// well-defined bitwise representation. 154 SmallVector<unsigned, 8> NonIntegralAddressSpaces; 155 156 void setAlignment(AlignTypeEnum align_type, unsigned abi_align, 157 unsigned pref_align, uint32_t bit_width); 158 unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width, 159 bool ABIAlign, Type *Ty) const; 160 void setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign, 161 unsigned PrefAlign, uint32_t TypeByteWidth); 162 163 /// Internal helper method that returns requested alignment for type. 164 unsigned getAlignment(Type *Ty, bool abi_or_pref) const; 165 166 /// Parses a target data specification string. Assert if the string is 167 /// malformed. 168 void parseSpecifier(StringRef LayoutDescription); 169 170 // Free all internal data structures. 171 void clear(); 172 173 public: 174 /// Constructs a DataLayout from a specification string. See reset(). 175 explicit DataLayout(StringRef LayoutDescription) : LayoutMap(nullptr) { 176 reset(LayoutDescription); 177 } 178 179 /// Initialize target data from properties stored in the module. 180 explicit DataLayout(const Module *M); 181 182 void init(const Module *M); 183 184 DataLayout(const DataLayout &DL) : LayoutMap(nullptr) { *this = DL; } 185 186 DataLayout &operator=(const DataLayout &DL) { 187 clear(); 188 StringRepresentation = DL.StringRepresentation; 189 BigEndian = DL.isBigEndian(); 190 AllocaAddrSpace = DL.AllocaAddrSpace; 191 StackNaturalAlign = DL.StackNaturalAlign; 192 ManglingMode = DL.ManglingMode; 193 LegalIntWidths = DL.LegalIntWidths; 194 Alignments = DL.Alignments; 195 Pointers = DL.Pointers; 196 NonIntegralAddressSpaces = DL.NonIntegralAddressSpaces; 197 return *this; 198 } 199 200 bool operator==(const DataLayout &Other) const; 201 bool operator!=(const DataLayout &Other) const { return !(*this == Other); } 202 203 ~DataLayout(); // Not virtual, do not subclass this class 204 205 /// Parse a data layout string (with fallback to default values). 206 void reset(StringRef LayoutDescription); 207 208 /// Layout endianness... 209 bool isLittleEndian() const { return !BigEndian; } 210 bool isBigEndian() const { return BigEndian; } 211 212 /// \brief Returns the string representation of the DataLayout. 213 /// 214 /// This representation is in the same format accepted by the string 215 /// constructor above. This should not be used to compare two DataLayout as 216 /// different string can represent the same layout. 217 const std::string &getStringRepresentation() const { 218 return StringRepresentation; 219 } 220 221 /// \brief Test if the DataLayout was constructed from an empty string. 222 bool isDefault() const { return StringRepresentation.empty(); } 223 224 /// \brief Returns true if the specified type is known to be a native integer 225 /// type supported by the CPU. 226 /// 227 /// For example, i64 is not native on most 32-bit CPUs and i37 is not native 228 /// on any known one. This returns false if the integer width is not legal. 229 /// 230 /// The width is specified in bits. 231 bool isLegalInteger(uint64_t Width) const { 232 for (unsigned LegalIntWidth : LegalIntWidths) 233 if (LegalIntWidth == Width) 234 return true; 235 return false; 236 } 237 238 bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); } 239 240 /// Returns true if the given alignment exceeds the natural stack alignment. 241 bool exceedsNaturalStackAlignment(unsigned Align) const { 242 return (StackNaturalAlign != 0) && (Align > StackNaturalAlign); 243 } 244 245 unsigned getStackAlignment() const { return StackNaturalAlign; } 246 unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; } 247 248 bool hasMicrosoftFastStdCallMangling() const { 249 return ManglingMode == MM_WinCOFFX86; 250 } 251 252 bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; } 253 254 StringRef getLinkerPrivateGlobalPrefix() const { 255 if (ManglingMode == MM_MachO) 256 return "l"; 257 return ""; 258 } 259 260 char getGlobalPrefix() const { 261 switch (ManglingMode) { 262 case MM_None: 263 case MM_ELF: 264 case MM_Mips: 265 case MM_WinCOFF: 266 return '\0'; 267 case MM_MachO: 268 case MM_WinCOFFX86: 269 return '_'; 270 } 271 llvm_unreachable("invalid mangling mode"); 272 } 273 274 StringRef getPrivateGlobalPrefix() const { 275 switch (ManglingMode) { 276 case MM_None: 277 return ""; 278 case MM_ELF: 279 case MM_WinCOFF: 280 return ".L"; 281 case MM_Mips: 282 return "$"; 283 case MM_MachO: 284 case MM_WinCOFFX86: 285 return "L"; 286 } 287 llvm_unreachable("invalid mangling mode"); 288 } 289 290 static const char *getManglingComponent(const Triple &T); 291 292 /// \brief Returns true if the specified type fits in a native integer type 293 /// supported by the CPU. 294 /// 295 /// For example, if the CPU only supports i32 as a native integer type, then 296 /// i27 fits in a legal integer type but i45 does not. 297 bool fitsInLegalInteger(unsigned Width) const { 298 for (unsigned LegalIntWidth : LegalIntWidths) 299 if (Width <= LegalIntWidth) 300 return true; 301 return false; 302 } 303 304 /// Layout pointer alignment 305 /// FIXME: The defaults need to be removed once all of 306 /// the backends/clients are updated. 307 unsigned getPointerABIAlignment(unsigned AS = 0) const; 308 309 /// Return target's alignment for stack-based pointers 310 /// FIXME: The defaults need to be removed once all of 311 /// the backends/clients are updated. 312 unsigned getPointerPrefAlignment(unsigned AS = 0) const; 313 314 /// Layout pointer size 315 /// FIXME: The defaults need to be removed once all of 316 /// the backends/clients are updated. 317 unsigned getPointerSize(unsigned AS = 0) const; 318 319 /// Return the address spaces containing non-integral pointers. Pointers in 320 /// this address space don't have a well-defined bitwise representation. 321 ArrayRef<unsigned> getNonIntegralAddressSpaces() const { 322 return NonIntegralAddressSpaces; 323 } 324 325 bool isNonIntegralPointerType(PointerType *PT) const { 326 ArrayRef<unsigned> NonIntegralSpaces = getNonIntegralAddressSpaces(); 327 return find(NonIntegralSpaces, PT->getAddressSpace()) != 328 NonIntegralSpaces.end(); 329 } 330 331 bool isNonIntegralPointerType(Type *Ty) const { 332 auto *PTy = dyn_cast<PointerType>(Ty); 333 return PTy && isNonIntegralPointerType(PTy); 334 } 335 336 /// Layout pointer size, in bits 337 /// FIXME: The defaults need to be removed once all of 338 /// the backends/clients are updated. 339 unsigned getPointerSizeInBits(unsigned AS = 0) const { 340 return getPointerSize(AS) * 8; 341 } 342 343 /// Layout pointer size, in bits, based on the type. If this function is 344 /// called with a pointer type, then the type size of the pointer is returned. 345 /// If this function is called with a vector of pointers, then the type size 346 /// of the pointer is returned. This should only be called with a pointer or 347 /// vector of pointers. 348 unsigned getPointerTypeSizeInBits(Type *) const; 349 350 unsigned getPointerTypeSize(Type *Ty) const { 351 return getPointerTypeSizeInBits(Ty) / 8; 352 } 353 354 /// Size examples: 355 /// 356 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*] 357 /// ---- ---------- --------------- --------------- 358 /// i1 1 8 8 359 /// i8 8 8 8 360 /// i19 19 24 32 361 /// i32 32 32 32 362 /// i100 100 104 128 363 /// i128 128 128 128 364 /// Float 32 32 32 365 /// Double 64 64 64 366 /// X86_FP80 80 80 96 367 /// 368 /// [*] The alloc size depends on the alignment, and thus on the target. 369 /// These values are for x86-32 linux. 370 371 /// \brief Returns the number of bits necessary to hold the specified type. 372 /// 373 /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must 374 /// have a size (Type::isSized() must return true). 375 uint64_t getTypeSizeInBits(Type *Ty) const; 376 377 /// \brief Returns the maximum number of bytes that may be overwritten by 378 /// storing the specified type. 379 /// 380 /// For example, returns 5 for i36 and 10 for x86_fp80. 381 uint64_t getTypeStoreSize(Type *Ty) const { 382 return (getTypeSizeInBits(Ty) + 7) / 8; 383 } 384 385 /// \brief Returns the maximum number of bits that may be overwritten by 386 /// storing the specified type; always a multiple of 8. 387 /// 388 /// For example, returns 40 for i36 and 80 for x86_fp80. 389 uint64_t getTypeStoreSizeInBits(Type *Ty) const { 390 return 8 * getTypeStoreSize(Ty); 391 } 392 393 /// \brief Returns the offset in bytes between successive objects of the 394 /// specified type, including alignment padding. 395 /// 396 /// This is the amount that alloca reserves for this type. For example, 397 /// returns 12 or 16 for x86_fp80, depending on alignment. 398 uint64_t getTypeAllocSize(Type *Ty) const { 399 // Round up to the next alignment boundary. 400 return alignTo(getTypeStoreSize(Ty), getABITypeAlignment(Ty)); 401 } 402 403 /// \brief Returns the offset in bits between successive objects of the 404 /// specified type, including alignment padding; always a multiple of 8. 405 /// 406 /// This is the amount that alloca reserves for this type. For example, 407 /// returns 96 or 128 for x86_fp80, depending on alignment. 408 uint64_t getTypeAllocSizeInBits(Type *Ty) const { 409 return 8 * getTypeAllocSize(Ty); 410 } 411 412 /// \brief Returns the minimum ABI-required alignment for the specified type. 413 unsigned getABITypeAlignment(Type *Ty) const; 414 415 /// \brief Returns the minimum ABI-required alignment for an integer type of 416 /// the specified bitwidth. 417 unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const; 418 419 /// \brief Returns the preferred stack/global alignment for the specified 420 /// type. 421 /// 422 /// This is always at least as good as the ABI alignment. 423 unsigned getPrefTypeAlignment(Type *Ty) const; 424 425 /// \brief Returns the preferred alignment for the specified type, returned as 426 /// log2 of the value (a shift amount). 427 unsigned getPreferredTypeAlignmentShift(Type *Ty) const; 428 429 /// \brief Returns an integer type with size at least as big as that of a 430 /// pointer in the given address space. 431 IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const; 432 433 /// \brief Returns an integer (vector of integer) type with size at least as 434 /// big as that of a pointer of the given pointer (vector of pointer) type. 435 Type *getIntPtrType(Type *) const; 436 437 /// \brief Returns the smallest integer type with size at least as big as 438 /// Width bits. 439 Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const; 440 441 /// \brief Returns the largest legal integer type, or null if none are set. 442 Type *getLargestLegalIntType(LLVMContext &C) const { 443 unsigned LargestSize = getLargestLegalIntTypeSizeInBits(); 444 return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize); 445 } 446 447 /// \brief Returns the size of largest legal integer type size, or 0 if none 448 /// are set. 449 unsigned getLargestLegalIntTypeSizeInBits() const; 450 451 /// \brief Returns the offset from the beginning of the type for the specified 452 /// indices. 453 /// 454 /// Note that this takes the element type, not the pointer type. 455 /// This is used to implement getelementptr. 456 int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef<Value *> Indices) const; 457 458 /// \brief Returns a StructLayout object, indicating the alignment of the 459 /// struct, its size, and the offsets of its fields. 460 /// 461 /// Note that this information is lazily cached. 462 const StructLayout *getStructLayout(StructType *Ty) const; 463 464 /// \brief Returns the preferred alignment of the specified global. 465 /// 466 /// This includes an explicitly requested alignment (if the global has one). 467 unsigned getPreferredAlignment(const GlobalVariable *GV) const; 468 469 /// \brief Returns the preferred alignment of the specified global, returned 470 /// in log form. 471 /// 472 /// This includes an explicitly requested alignment (if the global has one). 473 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const; 474 }; 475 476 inline DataLayout *unwrap(LLVMTargetDataRef P) { 477 return reinterpret_cast<DataLayout *>(P); 478 } 479 480 inline LLVMTargetDataRef wrap(const DataLayout *P) { 481 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P)); 482 } 483 484 /// Used to lazily calculate structure layout information for a target machine, 485 /// based on the DataLayout structure. 486 class StructLayout { 487 uint64_t StructSize; 488 unsigned StructAlignment; 489 unsigned IsPadded : 1; 490 unsigned NumElements : 31; 491 uint64_t MemberOffsets[1]; // variable sized array! 492 public: 493 uint64_t getSizeInBytes() const { return StructSize; } 494 495 uint64_t getSizeInBits() const { return 8 * StructSize; } 496 497 unsigned getAlignment() const { return StructAlignment; } 498 499 /// Returns whether the struct has padding or not between its fields. 500 /// NB: Padding in nested element is not taken into account. 501 bool hasPadding() const { return IsPadded; } 502 503 /// \brief Given a valid byte offset into the structure, returns the structure 504 /// index that contains it. 505 unsigned getElementContainingOffset(uint64_t Offset) const; 506 507 uint64_t getElementOffset(unsigned Idx) const { 508 assert(Idx < NumElements && "Invalid element idx!"); 509 return MemberOffsets[Idx]; 510 } 511 512 uint64_t getElementOffsetInBits(unsigned Idx) const { 513 return getElementOffset(Idx) * 8; 514 } 515 516 private: 517 friend class DataLayout; // Only DataLayout can create this class 518 StructLayout(StructType *ST, const DataLayout &DL); 519 }; 520 521 // The implementation of this method is provided inline as it is particularly 522 // well suited to constant folding when called on a specific Type subclass. 523 inline uint64_t DataLayout::getTypeSizeInBits(Type *Ty) const { 524 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); 525 switch (Ty->getTypeID()) { 526 case Type::LabelTyID: 527 return getPointerSizeInBits(0); 528 case Type::PointerTyID: 529 return getPointerSizeInBits(Ty->getPointerAddressSpace()); 530 case Type::ArrayTyID: { 531 ArrayType *ATy = cast<ArrayType>(Ty); 532 return ATy->getNumElements() * 533 getTypeAllocSizeInBits(ATy->getElementType()); 534 } 535 case Type::StructTyID: 536 // Get the layout annotation... which is lazily created on demand. 537 return getStructLayout(cast<StructType>(Ty))->getSizeInBits(); 538 case Type::IntegerTyID: 539 return Ty->getIntegerBitWidth(); 540 case Type::HalfTyID: 541 return 16; 542 case Type::FloatTyID: 543 return 32; 544 case Type::DoubleTyID: 545 case Type::X86_MMXTyID: 546 return 64; 547 case Type::PPC_FP128TyID: 548 case Type::FP128TyID: 549 return 128; 550 // In memory objects this is always aligned to a higher boundary, but 551 // only 80 bits contain information. 552 case Type::X86_FP80TyID: 553 return 80; 554 case Type::VectorTyID: { 555 VectorType *VTy = cast<VectorType>(Ty); 556 return VTy->getNumElements() * getTypeSizeInBits(VTy->getElementType()); 557 } 558 default: 559 llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type"); 560 } 561 } 562 563 } // End llvm namespace 564 565 #endif 566