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