[Alignment][NFC] Support compile time constants
[llvm-core.git] / include / llvm / IR / DataLayout.h
blob022d2e944b5afd26453953f3f7adeb48afb5865e
1 //===- llvm/DataLayout.h - Data size & alignment info -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines layout properties related to datatype size/offset/alignment
10 // information. It uses lazy annotations to cache information about how
11 // structure types are laid out and used.
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.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_IR_DATALAYOUT_H
20 #define LLVM_IR_DATALAYOUT_H
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 "llvm/Support/Alignment.h"
33 #include "llvm/Support/TypeSize.h"
34 #include <cassert>
35 #include <cstdint>
36 #include <string>
38 // This needs to be outside of the namespace, to avoid conflict with llvm-c
39 // decl.
40 using LLVMTargetDataRef = struct LLVMOpaqueTargetData *;
42 namespace llvm {
44 class GlobalVariable;
45 class LLVMContext;
46 class Module;
47 class StructLayout;
48 class Triple;
49 class Value;
51 /// Enum used to categorize the alignment types stored by LayoutAlignElem
52 enum AlignTypeEnum {
53 INVALID_ALIGN = 0,
54 INTEGER_ALIGN = 'i',
55 VECTOR_ALIGN = 'v',
56 FLOAT_ALIGN = 'f',
57 AGGREGATE_ALIGN = 'a'
60 // FIXME: Currently the DataLayout string carries a "preferred alignment"
61 // for types. As the DataLayout is module/global, this should likely be
62 // sunk down to an FTTI element that is queried rather than a global
63 // preference.
65 /// Layout alignment element.
66 ///
67 /// Stores the alignment data associated with a given alignment type (integer,
68 /// vector, float) and type bit width.
69 ///
70 /// \note The unusual order of elements in the structure attempts to reduce
71 /// padding and make the structure slightly more cache friendly.
72 struct LayoutAlignElem {
73 /// Alignment type from \c AlignTypeEnum
74 unsigned AlignType : 8;
75 unsigned TypeBitWidth : 24;
76 Align ABIAlign;
77 Align PrefAlign;
79 static LayoutAlignElem get(AlignTypeEnum align_type, Align abi_align,
80 Align pref_align, uint32_t bit_width);
82 bool operator==(const LayoutAlignElem &rhs) const;
85 /// Layout pointer alignment element.
86 ///
87 /// Stores the alignment data associated with a given pointer and address space.
88 ///
89 /// \note The unusual order of elements in the structure attempts to reduce
90 /// padding and make the structure slightly more cache friendly.
91 struct PointerAlignElem {
92 Align ABIAlign;
93 Align PrefAlign;
94 uint32_t TypeByteWidth;
95 uint32_t AddressSpace;
96 uint32_t IndexWidth;
98 /// Initializer
99 static PointerAlignElem get(uint32_t AddressSpace, Align ABIAlign,
100 Align PrefAlign, uint32_t TypeByteWidth,
101 uint32_t IndexWidth);
103 bool operator==(const PointerAlignElem &rhs) const;
106 /// A parsed version of the target data layout string in and methods for
107 /// querying it.
109 /// The target data layout string is specified *by the target* - a frontend
110 /// generating LLVM IR is required to generate the right target data for the
111 /// target being codegen'd to.
112 class DataLayout {
113 public:
114 enum class FunctionPtrAlignType {
115 /// The function pointer alignment is independent of the function alignment.
116 Independent,
117 /// The function pointer alignment is a multiple of the function alignment.
118 MultipleOfFunctionAlign,
120 private:
121 /// Defaults to false.
122 bool BigEndian;
124 unsigned AllocaAddrSpace;
125 MaybeAlign StackNaturalAlign;
126 unsigned ProgramAddrSpace;
128 MaybeAlign FunctionPtrAlign;
129 FunctionPtrAlignType TheFunctionPtrAlignType;
131 enum ManglingModeT {
132 MM_None,
133 MM_ELF,
134 MM_MachO,
135 MM_WinCOFF,
136 MM_WinCOFFX86,
137 MM_Mips
139 ManglingModeT ManglingMode;
141 SmallVector<unsigned char, 8> LegalIntWidths;
143 /// Primitive type alignment data. This is sorted by type and bit
144 /// width during construction.
145 using AlignmentsTy = SmallVector<LayoutAlignElem, 16>;
146 AlignmentsTy Alignments;
148 AlignmentsTy::const_iterator
149 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth) const {
150 return const_cast<DataLayout *>(this)->findAlignmentLowerBound(AlignType,
151 BitWidth);
154 AlignmentsTy::iterator
155 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth);
157 /// The string representation used to create this DataLayout
158 std::string StringRepresentation;
160 using PointersTy = SmallVector<PointerAlignElem, 8>;
161 PointersTy Pointers;
163 PointersTy::const_iterator
164 findPointerLowerBound(uint32_t AddressSpace) const {
165 return const_cast<DataLayout *>(this)->findPointerLowerBound(AddressSpace);
168 PointersTy::iterator findPointerLowerBound(uint32_t AddressSpace);
170 // The StructType -> StructLayout map.
171 mutable void *LayoutMap = nullptr;
173 /// Pointers in these address spaces are non-integral, and don't have a
174 /// well-defined bitwise representation.
175 SmallVector<unsigned, 8> NonIntegralAddressSpaces;
177 void setAlignment(AlignTypeEnum align_type, Align abi_align, Align pref_align,
178 uint32_t bit_width);
179 Align getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
180 bool ABIAlign, Type *Ty) const;
181 void setPointerAlignment(uint32_t AddrSpace, Align ABIAlign, Align PrefAlign,
182 uint32_t TypeByteWidth, uint32_t IndexWidth);
184 /// Internal helper method that returns requested alignment for type.
185 Align getAlignment(Type *Ty, bool abi_or_pref) const;
187 /// Parses a target data specification string. Assert if the string is
188 /// malformed.
189 void parseSpecifier(StringRef LayoutDescription);
191 // Free all internal data structures.
192 void clear();
194 public:
195 /// Constructs a DataLayout from a specification string. See reset().
196 explicit DataLayout(StringRef LayoutDescription) {
197 reset(LayoutDescription);
200 /// Initialize target data from properties stored in the module.
201 explicit DataLayout(const Module *M);
203 DataLayout(const DataLayout &DL) { *this = DL; }
205 ~DataLayout(); // Not virtual, do not subclass this class
207 DataLayout &operator=(const DataLayout &DL) {
208 clear();
209 StringRepresentation = DL.StringRepresentation;
210 BigEndian = DL.isBigEndian();
211 AllocaAddrSpace = DL.AllocaAddrSpace;
212 StackNaturalAlign = DL.StackNaturalAlign;
213 FunctionPtrAlign = DL.FunctionPtrAlign;
214 TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType;
215 ProgramAddrSpace = DL.ProgramAddrSpace;
216 ManglingMode = DL.ManglingMode;
217 LegalIntWidths = DL.LegalIntWidths;
218 Alignments = DL.Alignments;
219 Pointers = DL.Pointers;
220 NonIntegralAddressSpaces = DL.NonIntegralAddressSpaces;
221 return *this;
224 bool operator==(const DataLayout &Other) const;
225 bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
227 void init(const Module *M);
229 /// Parse a data layout string (with fallback to default values).
230 void reset(StringRef LayoutDescription);
232 /// Layout endianness...
233 bool isLittleEndian() const { return !BigEndian; }
234 bool isBigEndian() const { return BigEndian; }
236 /// Returns the string representation of the DataLayout.
238 /// This representation is in the same format accepted by the string
239 /// constructor above. This should not be used to compare two DataLayout as
240 /// different string can represent the same layout.
241 const std::string &getStringRepresentation() const {
242 return StringRepresentation;
245 /// Test if the DataLayout was constructed from an empty string.
246 bool isDefault() const { return StringRepresentation.empty(); }
248 /// Returns true if the specified type is known to be a native integer
249 /// type supported by the CPU.
251 /// For example, i64 is not native on most 32-bit CPUs and i37 is not native
252 /// on any known one. This returns false if the integer width is not legal.
254 /// The width is specified in bits.
255 bool isLegalInteger(uint64_t Width) const {
256 for (unsigned LegalIntWidth : LegalIntWidths)
257 if (LegalIntWidth == Width)
258 return true;
259 return false;
262 bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); }
264 /// Returns true if the given alignment exceeds the natural stack alignment.
265 bool exceedsNaturalStackAlignment(Align Alignment) const {
266 return StackNaturalAlign && (Alignment > StackNaturalAlign);
269 Align getStackAlignment() const {
270 assert(StackNaturalAlign && "StackNaturalAlign must be defined");
271 return *StackNaturalAlign;
274 unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; }
276 /// Returns the alignment of function pointers, which may or may not be
277 /// related to the alignment of functions.
278 /// \see getFunctionPtrAlignType
279 MaybeAlign getFunctionPtrAlign() const { return FunctionPtrAlign; }
281 /// Return the type of function pointer alignment.
282 /// \see getFunctionPtrAlign
283 FunctionPtrAlignType getFunctionPtrAlignType() const {
284 return TheFunctionPtrAlignType;
287 unsigned getProgramAddressSpace() const { return ProgramAddrSpace; }
289 bool hasMicrosoftFastStdCallMangling() const {
290 return ManglingMode == MM_WinCOFFX86;
293 /// Returns true if symbols with leading question marks should not receive IR
294 /// mangling. True for Windows mangling modes.
295 bool doNotMangleLeadingQuestionMark() const {
296 return ManglingMode == MM_WinCOFF || ManglingMode == MM_WinCOFFX86;
299 bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; }
301 StringRef getLinkerPrivateGlobalPrefix() const {
302 if (ManglingMode == MM_MachO)
303 return "l";
304 return "";
307 char getGlobalPrefix() const {
308 switch (ManglingMode) {
309 case MM_None:
310 case MM_ELF:
311 case MM_Mips:
312 case MM_WinCOFF:
313 return '\0';
314 case MM_MachO:
315 case MM_WinCOFFX86:
316 return '_';
318 llvm_unreachable("invalid mangling mode");
321 StringRef getPrivateGlobalPrefix() const {
322 switch (ManglingMode) {
323 case MM_None:
324 return "";
325 case MM_ELF:
326 case MM_WinCOFF:
327 return ".L";
328 case MM_Mips:
329 return "$";
330 case MM_MachO:
331 case MM_WinCOFFX86:
332 return "L";
334 llvm_unreachable("invalid mangling mode");
337 static const char *getManglingComponent(const Triple &T);
339 /// Returns true if the specified type fits in a native integer type
340 /// supported by the CPU.
342 /// For example, if the CPU only supports i32 as a native integer type, then
343 /// i27 fits in a legal integer type but i45 does not.
344 bool fitsInLegalInteger(unsigned Width) const {
345 for (unsigned LegalIntWidth : LegalIntWidths)
346 if (Width <= LegalIntWidth)
347 return true;
348 return false;
351 /// Layout pointer alignment
352 Align getPointerABIAlignment(unsigned AS) const;
354 /// Return target's alignment for stack-based pointers
355 /// FIXME: The defaults need to be removed once all of
356 /// the backends/clients are updated.
357 Align getPointerPrefAlignment(unsigned AS = 0) const;
359 /// Layout pointer size
360 /// FIXME: The defaults need to be removed once all of
361 /// the backends/clients are updated.
362 unsigned getPointerSize(unsigned AS = 0) const;
364 /// Returns the maximum pointer size over all address spaces.
365 unsigned getMaxPointerSize() const;
367 // Index size used for address calculation.
368 unsigned getIndexSize(unsigned AS) const;
370 /// Return the address spaces containing non-integral pointers. Pointers in
371 /// this address space don't have a well-defined bitwise representation.
372 ArrayRef<unsigned> getNonIntegralAddressSpaces() const {
373 return NonIntegralAddressSpaces;
376 bool isNonIntegralAddressSpace(unsigned AddrSpace) const {
377 ArrayRef<unsigned> NonIntegralSpaces = getNonIntegralAddressSpaces();
378 return find(NonIntegralSpaces, AddrSpace) != NonIntegralSpaces.end();
381 bool isNonIntegralPointerType(PointerType *PT) const {
382 return isNonIntegralAddressSpace(PT->getAddressSpace());
385 bool isNonIntegralPointerType(Type *Ty) const {
386 auto *PTy = dyn_cast<PointerType>(Ty);
387 return PTy && isNonIntegralPointerType(PTy);
390 /// Layout pointer size, in bits
391 /// FIXME: The defaults need to be removed once all of
392 /// the backends/clients are updated.
393 unsigned getPointerSizeInBits(unsigned AS = 0) const {
394 return getPointerSize(AS) * 8;
397 /// Returns the maximum pointer size over all address spaces.
398 unsigned getMaxPointerSizeInBits() const {
399 return getMaxPointerSize() * 8;
402 /// Size in bits of index used for address calculation in getelementptr.
403 unsigned getIndexSizeInBits(unsigned AS) const {
404 return getIndexSize(AS) * 8;
407 /// Layout pointer size, in bits, based on the type. If this function is
408 /// called with a pointer type, then the type size of the pointer is returned.
409 /// If this function is called with a vector of pointers, then the type size
410 /// of the pointer is returned. This should only be called with a pointer or
411 /// vector of pointers.
412 unsigned getPointerTypeSizeInBits(Type *) const;
414 /// Layout size of the index used in GEP calculation.
415 /// The function should be called with pointer or vector of pointers type.
416 unsigned getIndexTypeSizeInBits(Type *Ty) const;
418 unsigned getPointerTypeSize(Type *Ty) const {
419 return getPointerTypeSizeInBits(Ty) / 8;
422 /// Size examples:
424 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*]
425 /// ---- ---------- --------------- ---------------
426 /// i1 1 8 8
427 /// i8 8 8 8
428 /// i19 19 24 32
429 /// i32 32 32 32
430 /// i100 100 104 128
431 /// i128 128 128 128
432 /// Float 32 32 32
433 /// Double 64 64 64
434 /// X86_FP80 80 80 96
436 /// [*] The alloc size depends on the alignment, and thus on the target.
437 /// These values are for x86-32 linux.
439 /// Returns the number of bits necessary to hold the specified type.
441 /// If Ty is a scalable vector type, the scalable property will be set and
442 /// the runtime size will be a positive integer multiple of the base size.
444 /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must
445 /// have a size (Type::isSized() must return true).
446 TypeSize getTypeSizeInBits(Type *Ty) const;
448 /// Returns the maximum number of bytes that may be overwritten by
449 /// storing the specified type.
451 /// If Ty is a scalable vector type, the scalable property will be set and
452 /// the runtime size will be a positive integer multiple of the base size.
454 /// For example, returns 5 for i36 and 10 for x86_fp80.
455 TypeSize getTypeStoreSize(Type *Ty) const {
456 auto BaseSize = getTypeSizeInBits(Ty);
457 return { (BaseSize.getKnownMinSize() + 7) / 8, BaseSize.isScalable() };
460 /// Returns the maximum number of bits that may be overwritten by
461 /// storing the specified type; always a multiple of 8.
463 /// If Ty is a scalable vector type, the scalable property will be set and
464 /// the runtime size will be a positive integer multiple of the base size.
466 /// For example, returns 40 for i36 and 80 for x86_fp80.
467 TypeSize getTypeStoreSizeInBits(Type *Ty) const {
468 return 8 * getTypeStoreSize(Ty);
471 /// Returns true if no extra padding bits are needed when storing the
472 /// specified type.
474 /// For example, returns false for i19 that has a 24-bit store size.
475 bool typeSizeEqualsStoreSize(Type *Ty) const {
476 return getTypeSizeInBits(Ty) == getTypeStoreSizeInBits(Ty);
479 /// Returns the offset in bytes between successive objects of the
480 /// specified type, including alignment padding.
482 /// If Ty is a scalable vector type, the scalable property will be set and
483 /// the runtime size will be a positive integer multiple of the base size.
485 /// This is the amount that alloca reserves for this type. For example,
486 /// returns 12 or 16 for x86_fp80, depending on alignment.
487 TypeSize getTypeAllocSize(Type *Ty) const {
488 // Round up to the next alignment boundary.
489 return alignTo(getTypeStoreSize(Ty), getABITypeAlignment(Ty));
492 /// Returns the offset in bits between successive objects of the
493 /// specified type, including alignment padding; always a multiple of 8.
495 /// If Ty is a scalable vector type, the scalable property will be set and
496 /// the runtime size will be a positive integer multiple of the base size.
498 /// This is the amount that alloca reserves for this type. For example,
499 /// returns 96 or 128 for x86_fp80, depending on alignment.
500 TypeSize getTypeAllocSizeInBits(Type *Ty) const {
501 return 8 * getTypeAllocSize(Ty);
504 /// Returns the minimum ABI-required alignment for the specified type.
505 unsigned getABITypeAlignment(Type *Ty) const;
507 /// Returns the minimum ABI-required alignment for an integer type of
508 /// the specified bitwidth.
509 Align getABIIntegerTypeAlignment(unsigned BitWidth) const;
511 /// Returns the preferred stack/global alignment for the specified
512 /// type.
514 /// This is always at least as good as the ABI alignment.
515 unsigned getPrefTypeAlignment(Type *Ty) const;
517 /// Returns an integer type with size at least as big as that of a
518 /// pointer in the given address space.
519 IntegerType *getIntPtrType(LLVMContext &C, unsigned AddressSpace = 0) const;
521 /// Returns an integer (vector of integer) type with size at least as
522 /// big as that of a pointer of the given pointer (vector of pointer) type.
523 Type *getIntPtrType(Type *) const;
525 /// Returns the smallest integer type with size at least as big as
526 /// Width bits.
527 Type *getSmallestLegalIntType(LLVMContext &C, unsigned Width = 0) const;
529 /// Returns the largest legal integer type, or null if none are set.
530 Type *getLargestLegalIntType(LLVMContext &C) const {
531 unsigned LargestSize = getLargestLegalIntTypeSizeInBits();
532 return (LargestSize == 0) ? nullptr : Type::getIntNTy(C, LargestSize);
535 /// Returns the size of largest legal integer type size, or 0 if none
536 /// are set.
537 unsigned getLargestLegalIntTypeSizeInBits() const;
539 /// Returns the type of a GEP index.
540 /// If it was not specified explicitly, it will be the integer type of the
541 /// pointer width - IntPtrType.
542 Type *getIndexType(Type *PtrTy) const;
544 /// Returns the offset from the beginning of the type for the specified
545 /// indices.
547 /// Note that this takes the element type, not the pointer type.
548 /// This is used to implement getelementptr.
549 int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef<Value *> Indices) const;
551 /// Returns a StructLayout object, indicating the alignment of the
552 /// struct, its size, and the offsets of its fields.
554 /// Note that this information is lazily cached.
555 const StructLayout *getStructLayout(StructType *Ty) const;
557 /// Returns the preferred alignment of the specified global.
559 /// This includes an explicitly requested alignment (if the global has one).
560 unsigned getPreferredAlignment(const GlobalVariable *GV) const;
562 /// Returns the preferred alignment of the specified global, returned
563 /// in log form.
565 /// This includes an explicitly requested alignment (if the global has one).
566 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
569 inline DataLayout *unwrap(LLVMTargetDataRef P) {
570 return reinterpret_cast<DataLayout *>(P);
573 inline LLVMTargetDataRef wrap(const DataLayout *P) {
574 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P));
577 /// Used to lazily calculate structure layout information for a target machine,
578 /// based on the DataLayout structure.
579 class StructLayout {
580 uint64_t StructSize;
581 Align StructAlignment;
582 unsigned IsPadded : 1;
583 unsigned NumElements : 31;
584 uint64_t MemberOffsets[1]; // variable sized array!
586 public:
587 uint64_t getSizeInBytes() const { return StructSize; }
589 uint64_t getSizeInBits() const { return 8 * StructSize; }
591 Align getAlignment() const { return StructAlignment; }
593 /// Returns whether the struct has padding or not between its fields.
594 /// NB: Padding in nested element is not taken into account.
595 bool hasPadding() const { return IsPadded; }
597 /// Given a valid byte offset into the structure, returns the structure
598 /// index that contains it.
599 unsigned getElementContainingOffset(uint64_t Offset) const;
601 uint64_t getElementOffset(unsigned Idx) const {
602 assert(Idx < NumElements && "Invalid element idx!");
603 return MemberOffsets[Idx];
606 uint64_t getElementOffsetInBits(unsigned Idx) const {
607 return getElementOffset(Idx) * 8;
610 private:
611 friend class DataLayout; // Only DataLayout can create this class
613 StructLayout(StructType *ST, const DataLayout &DL);
616 // The implementation of this method is provided inline as it is particularly
617 // well suited to constant folding when called on a specific Type subclass.
618 inline TypeSize DataLayout::getTypeSizeInBits(Type *Ty) const {
619 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
620 switch (Ty->getTypeID()) {
621 case Type::LabelTyID:
622 return TypeSize::Fixed(getPointerSizeInBits(0));
623 case Type::PointerTyID:
624 return TypeSize::Fixed(getPointerSizeInBits(Ty->getPointerAddressSpace()));
625 case Type::ArrayTyID: {
626 ArrayType *ATy = cast<ArrayType>(Ty);
627 return ATy->getNumElements() *
628 getTypeAllocSizeInBits(ATy->getElementType());
630 case Type::StructTyID:
631 // Get the layout annotation... which is lazily created on demand.
632 return TypeSize::Fixed(
633 getStructLayout(cast<StructType>(Ty))->getSizeInBits());
634 case Type::IntegerTyID:
635 return TypeSize::Fixed(Ty->getIntegerBitWidth());
636 case Type::HalfTyID:
637 return TypeSize::Fixed(16);
638 case Type::FloatTyID:
639 return TypeSize::Fixed(32);
640 case Type::DoubleTyID:
641 case Type::X86_MMXTyID:
642 return TypeSize::Fixed(64);
643 case Type::PPC_FP128TyID:
644 case Type::FP128TyID:
645 return TypeSize::Fixed(128);
646 // In memory objects this is always aligned to a higher boundary, but
647 // only 80 bits contain information.
648 case Type::X86_FP80TyID:
649 return TypeSize::Fixed(80);
650 case Type::VectorTyID: {
651 VectorType *VTy = cast<VectorType>(Ty);
652 auto EltCnt = VTy->getElementCount();
653 uint64_t MinBits = EltCnt.Min *
654 getTypeSizeInBits(VTy->getElementType()).getFixedSize();
655 return TypeSize(MinBits, EltCnt.Scalable);
657 default:
658 llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
662 } // end namespace llvm
664 #endif // LLVM_IR_DATALAYOUT_H