[ARM] MVE integer min and max
[llvm-complete.git] / include / llvm / IR / DataLayout.h
blobac9770a15120d929be94d72462acd0d00aef5441
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 <cassert>
33 #include <cstdint>
34 #include <string>
36 // This needs to be outside of the namespace, to avoid conflict with llvm-c
37 // decl.
38 using LLVMTargetDataRef = struct LLVMOpaqueTargetData *;
40 namespace llvm {
42 class GlobalVariable;
43 class LLVMContext;
44 class Module;
45 class StructLayout;
46 class Triple;
47 class Value;
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'
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.
63 /// Layout alignment element.
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 {
71 /// Alignment type from \c AlignTypeEnum
72 unsigned AlignType : 8;
73 unsigned TypeBitWidth : 24;
74 unsigned ABIAlign : 16;
75 unsigned PrefAlign : 16;
77 static LayoutAlignElem get(AlignTypeEnum align_type, unsigned abi_align,
78 unsigned pref_align, uint32_t bit_width);
80 bool operator==(const LayoutAlignElem &rhs) const;
83 /// Layout pointer alignment element.
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;
96 /// Initializer
97 static PointerAlignElem get(uint32_t AddressSpace, unsigned ABIAlign,
98 unsigned PrefAlign, uint32_t TypeByteWidth,
99 uint32_t IndexWidth);
101 bool operator==(const PointerAlignElem &rhs) const;
104 /// A parsed version of the target data layout string in and methods for
105 /// querying it.
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 public:
112 enum class FunctionPtrAlignType {
113 /// The function pointer alignment is independent of the function alignment.
114 Independent,
115 /// The function pointer alignment is a multiple of the function alignment.
116 MultipleOfFunctionAlign,
118 private:
119 /// Defaults to false.
120 bool BigEndian;
122 unsigned AllocaAddrSpace;
123 unsigned StackNaturalAlign;
124 unsigned ProgramAddrSpace;
126 unsigned FunctionPtrAlign;
127 FunctionPtrAlignType TheFunctionPtrAlignType;
129 enum ManglingModeT {
130 MM_None,
131 MM_ELF,
132 MM_MachO,
133 MM_WinCOFF,
134 MM_WinCOFFX86,
135 MM_Mips
137 ManglingModeT ManglingMode;
139 SmallVector<unsigned char, 8> LegalIntWidths;
141 /// Primitive type alignment data. This is sorted by type and bit
142 /// width during construction.
143 using AlignmentsTy = SmallVector<LayoutAlignElem, 16>;
144 AlignmentsTy Alignments;
146 AlignmentsTy::const_iterator
147 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth) const {
148 return const_cast<DataLayout *>(this)->findAlignmentLowerBound(AlignType,
149 BitWidth);
152 AlignmentsTy::iterator
153 findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth);
155 /// The string representation used to create this DataLayout
156 std::string StringRepresentation;
158 using PointersTy = SmallVector<PointerAlignElem, 8>;
159 PointersTy Pointers;
161 PointersTy::const_iterator
162 findPointerLowerBound(uint32_t AddressSpace) const {
163 return const_cast<DataLayout *>(this)->findPointerLowerBound(AddressSpace);
166 PointersTy::iterator findPointerLowerBound(uint32_t AddressSpace);
168 // The StructType -> StructLayout map.
169 mutable void *LayoutMap = nullptr;
171 /// Pointers in these address spaces are non-integral, and don't have a
172 /// well-defined bitwise representation.
173 SmallVector<unsigned, 8> NonIntegralAddressSpaces;
175 void setAlignment(AlignTypeEnum align_type, unsigned abi_align,
176 unsigned pref_align, uint32_t bit_width);
177 unsigned getAlignmentInfo(AlignTypeEnum align_type, uint32_t bit_width,
178 bool ABIAlign, Type *Ty) const;
179 void setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign,
180 unsigned PrefAlign, uint32_t TypeByteWidth,
181 uint32_t IndexWidth);
183 /// Internal helper method that returns requested alignment for type.
184 unsigned getAlignment(Type *Ty, bool abi_or_pref) const;
186 /// Parses a target data specification string. Assert if the string is
187 /// malformed.
188 void parseSpecifier(StringRef LayoutDescription);
190 // Free all internal data structures.
191 void clear();
193 public:
194 /// Constructs a DataLayout from a specification string. See reset().
195 explicit DataLayout(StringRef LayoutDescription) {
196 reset(LayoutDescription);
199 /// Initialize target data from properties stored in the module.
200 explicit DataLayout(const Module *M);
202 DataLayout(const DataLayout &DL) { *this = DL; }
204 ~DataLayout(); // Not virtual, do not subclass this class
206 DataLayout &operator=(const DataLayout &DL) {
207 clear();
208 StringRepresentation = DL.StringRepresentation;
209 BigEndian = DL.isBigEndian();
210 AllocaAddrSpace = DL.AllocaAddrSpace;
211 StackNaturalAlign = DL.StackNaturalAlign;
212 FunctionPtrAlign = DL.FunctionPtrAlign;
213 TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType;
214 ProgramAddrSpace = DL.ProgramAddrSpace;
215 ManglingMode = DL.ManglingMode;
216 LegalIntWidths = DL.LegalIntWidths;
217 Alignments = DL.Alignments;
218 Pointers = DL.Pointers;
219 NonIntegralAddressSpaces = DL.NonIntegralAddressSpaces;
220 return *this;
223 bool operator==(const DataLayout &Other) const;
224 bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
226 void init(const Module *M);
228 /// Parse a data layout string (with fallback to default values).
229 void reset(StringRef LayoutDescription);
231 /// Layout endianness...
232 bool isLittleEndian() const { return !BigEndian; }
233 bool isBigEndian() const { return BigEndian; }
235 /// Returns the string representation of the DataLayout.
237 /// This representation is in the same format accepted by the string
238 /// constructor above. This should not be used to compare two DataLayout as
239 /// different string can represent the same layout.
240 const std::string &getStringRepresentation() const {
241 return StringRepresentation;
244 /// Test if the DataLayout was constructed from an empty string.
245 bool isDefault() const { return StringRepresentation.empty(); }
247 /// Returns true if the specified type is known to be a native integer
248 /// type supported by the CPU.
250 /// For example, i64 is not native on most 32-bit CPUs and i37 is not native
251 /// on any known one. This returns false if the integer width is not legal.
253 /// The width is specified in bits.
254 bool isLegalInteger(uint64_t Width) const {
255 for (unsigned LegalIntWidth : LegalIntWidths)
256 if (LegalIntWidth == Width)
257 return true;
258 return false;
261 bool isIllegalInteger(uint64_t Width) const { return !isLegalInteger(Width); }
263 /// Returns true if the given alignment exceeds the natural stack alignment.
264 bool exceedsNaturalStackAlignment(unsigned Align) const {
265 return (StackNaturalAlign != 0) && (Align > StackNaturalAlign);
268 unsigned getStackAlignment() const { return StackNaturalAlign; }
269 unsigned getAllocaAddrSpace() const { return AllocaAddrSpace; }
271 /// Returns the alignment of function pointers, which may or may not be
272 /// related to the alignment of functions.
273 /// \see getFunctionPtrAlignType
274 unsigned getFunctionPtrAlign() const { return FunctionPtrAlign; }
276 /// Return the type of function pointer alignment.
277 /// \see getFunctionPtrAlign
278 FunctionPtrAlignType getFunctionPtrAlignType() const {
279 return TheFunctionPtrAlignType;
282 unsigned getProgramAddressSpace() const { return ProgramAddrSpace; }
284 bool hasMicrosoftFastStdCallMangling() const {
285 return ManglingMode == MM_WinCOFFX86;
288 /// Returns true if symbols with leading question marks should not receive IR
289 /// mangling. True for Windows mangling modes.
290 bool doNotMangleLeadingQuestionMark() const {
291 return ManglingMode == MM_WinCOFF || ManglingMode == MM_WinCOFFX86;
294 bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; }
296 StringRef getLinkerPrivateGlobalPrefix() const {
297 if (ManglingMode == MM_MachO)
298 return "l";
299 return "";
302 char getGlobalPrefix() const {
303 switch (ManglingMode) {
304 case MM_None:
305 case MM_ELF:
306 case MM_Mips:
307 case MM_WinCOFF:
308 return '\0';
309 case MM_MachO:
310 case MM_WinCOFFX86:
311 return '_';
313 llvm_unreachable("invalid mangling mode");
316 StringRef getPrivateGlobalPrefix() const {
317 switch (ManglingMode) {
318 case MM_None:
319 return "";
320 case MM_ELF:
321 case MM_WinCOFF:
322 return ".L";
323 case MM_Mips:
324 return "$";
325 case MM_MachO:
326 case MM_WinCOFFX86:
327 return "L";
329 llvm_unreachable("invalid mangling mode");
332 static const char *getManglingComponent(const Triple &T);
334 /// Returns true if the specified type fits in a native integer type
335 /// supported by the CPU.
337 /// For example, if the CPU only supports i32 as a native integer type, then
338 /// i27 fits in a legal integer type but i45 does not.
339 bool fitsInLegalInteger(unsigned Width) const {
340 for (unsigned LegalIntWidth : LegalIntWidths)
341 if (Width <= LegalIntWidth)
342 return true;
343 return false;
346 /// Layout pointer alignment
347 unsigned getPointerABIAlignment(unsigned AS) const;
349 /// Return target's alignment for stack-based pointers
350 /// FIXME: The defaults need to be removed once all of
351 /// the backends/clients are updated.
352 unsigned getPointerPrefAlignment(unsigned AS = 0) const;
354 /// Layout pointer size
355 /// FIXME: The defaults need to be removed once all of
356 /// the backends/clients are updated.
357 unsigned getPointerSize(unsigned AS = 0) const;
359 /// Returns the maximum pointer size over all address spaces.
360 unsigned getMaxPointerSize() const;
362 // Index size used for address calculation.
363 unsigned getIndexSize(unsigned AS) const;
365 /// Return the address spaces containing non-integral pointers. Pointers in
366 /// this address space don't have a well-defined bitwise representation.
367 ArrayRef<unsigned> getNonIntegralAddressSpaces() const {
368 return NonIntegralAddressSpaces;
371 bool isNonIntegralAddressSpace(unsigned AddrSpace) const {
372 ArrayRef<unsigned> NonIntegralSpaces = getNonIntegralAddressSpaces();
373 return find(NonIntegralSpaces, AddrSpace) != NonIntegralSpaces.end();
376 bool isNonIntegralPointerType(PointerType *PT) const {
377 return isNonIntegralAddressSpace(PT->getAddressSpace());
380 bool isNonIntegralPointerType(Type *Ty) const {
381 auto *PTy = dyn_cast<PointerType>(Ty);
382 return PTy && isNonIntegralPointerType(PTy);
385 /// Layout pointer size, in bits
386 /// FIXME: The defaults need to be removed once all of
387 /// the backends/clients are updated.
388 unsigned getPointerSizeInBits(unsigned AS = 0) const {
389 return getPointerSize(AS) * 8;
392 /// Returns the maximum pointer size over all address spaces.
393 unsigned getMaxPointerSizeInBits() const {
394 return getMaxPointerSize() * 8;
397 /// Size in bits of index used for address calculation in getelementptr.
398 unsigned getIndexSizeInBits(unsigned AS) const {
399 return getIndexSize(AS) * 8;
402 /// Layout pointer size, in bits, based on the type. If this function is
403 /// called with a pointer type, then the type size of the pointer is returned.
404 /// If this function is called with a vector of pointers, then the type size
405 /// of the pointer is returned. This should only be called with a pointer or
406 /// vector of pointers.
407 unsigned getPointerTypeSizeInBits(Type *) const;
409 /// Layout size of the index used in GEP calculation.
410 /// The function should be called with pointer or vector of pointers type.
411 unsigned getIndexTypeSizeInBits(Type *Ty) const;
413 unsigned getPointerTypeSize(Type *Ty) const {
414 return getPointerTypeSizeInBits(Ty) / 8;
417 /// Size examples:
419 /// Type SizeInBits StoreSizeInBits AllocSizeInBits[*]
420 /// ---- ---------- --------------- ---------------
421 /// i1 1 8 8
422 /// i8 8 8 8
423 /// i19 19 24 32
424 /// i32 32 32 32
425 /// i100 100 104 128
426 /// i128 128 128 128
427 /// Float 32 32 32
428 /// Double 64 64 64
429 /// X86_FP80 80 80 96
431 /// [*] The alloc size depends on the alignment, and thus on the target.
432 /// These values are for x86-32 linux.
434 /// Returns the number of bits necessary to hold the specified type.
436 /// For example, returns 36 for i36 and 80 for x86_fp80. The type passed must
437 /// have a size (Type::isSized() must return true).
438 uint64_t getTypeSizeInBits(Type *Ty) const;
440 /// Returns the maximum number of bytes that may be overwritten by
441 /// storing the specified type.
443 /// For example, returns 5 for i36 and 10 for x86_fp80.
444 uint64_t getTypeStoreSize(Type *Ty) const {
445 return (getTypeSizeInBits(Ty) + 7) / 8;
448 /// Returns the maximum number of bits that may be overwritten by
449 /// storing the specified type; always a multiple of 8.
451 /// For example, returns 40 for i36 and 80 for x86_fp80.
452 uint64_t getTypeStoreSizeInBits(Type *Ty) const {
453 return 8 * getTypeStoreSize(Ty);
456 /// Returns true if no extra padding bits are needed when storing the
457 /// specified type.
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);
464 /// Returns the offset in bytes between successive objects of the
465 /// specified type, including alignment padding.
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));
474 /// Returns the offset in bits between successive objects of the
475 /// specified type, including alignment padding; always a multiple of 8.
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);
483 /// Returns the minimum ABI-required alignment for the specified type.
484 unsigned getABITypeAlignment(Type *Ty) const;
486 /// Returns the minimum ABI-required alignment for an integer type of
487 /// the specified bitwidth.
488 unsigned getABIIntegerTypeAlignment(unsigned BitWidth) const;
490 /// Returns the preferred stack/global alignment for the specified
491 /// type.
493 /// This is always at least as good as the ABI alignment.
494 unsigned getPrefTypeAlignment(Type *Ty) const;
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;
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;
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;
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;
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);
518 /// Returns the size of largest legal integer type size, or 0 if none
519 /// are set.
520 unsigned getLargestLegalIntTypeSizeInBits() const;
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;
527 /// Returns the offset from the beginning of the type for the specified
528 /// indices.
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;
534 /// Returns a StructLayout object, indicating the alignment of the
535 /// struct, its size, and the offsets of its fields.
537 /// Note that this information is lazily cached.
538 const StructLayout *getStructLayout(StructType *Ty) const;
540 /// Returns the preferred alignment of the specified global.
542 /// This includes an explicitly requested alignment (if the global has one).
543 unsigned getPreferredAlignment(const GlobalVariable *GV) const;
545 /// Returns the preferred alignment of the specified global, returned
546 /// in log form.
548 /// This includes an explicitly requested alignment (if the global has one).
549 unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
552 inline DataLayout *unwrap(LLVMTargetDataRef P) {
553 return reinterpret_cast<DataLayout *>(P);
556 inline LLVMTargetDataRef wrap(const DataLayout *P) {
557 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout *>(P));
560 /// Used to lazily calculate structure layout information for a target machine,
561 /// based on the DataLayout structure.
562 class StructLayout {
563 uint64_t StructSize;
564 unsigned StructAlignment;
565 unsigned IsPadded : 1;
566 unsigned NumElements : 31;
567 uint64_t MemberOffsets[1]; // variable sized array!
569 public:
570 uint64_t getSizeInBytes() const { return StructSize; }
572 uint64_t getSizeInBits() const { return 8 * StructSize; }
574 unsigned getAlignment() const { return StructAlignment; }
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; }
580 /// Given a valid byte offset into the structure, returns the structure
581 /// index that contains it.
582 unsigned getElementContainingOffset(uint64_t Offset) const;
584 uint64_t getElementOffset(unsigned Idx) const {
585 assert(Idx < NumElements && "Invalid element idx!");
586 return MemberOffsets[Idx];
589 uint64_t getElementOffsetInBits(unsigned Idx) const {
590 return getElementOffset(Idx) * 8;
593 private:
594 friend class DataLayout; // Only DataLayout can create this class
596 StructLayout(StructType *ST, const DataLayout &DL);
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.
601 inline 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());
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());
636 default:
637 llvm_unreachable("DataLayout::getTypeSizeInBits(): Unsupported type");
641 } // end namespace llvm
643 #endif // LLVM_IR_DATALAYOUT_H