LLVM 22.0.0git
Types.h
Go to the documentation of this file.
1//===- ABI/Types.h ----------------------------------------------*- 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/// \file
10/// This file defines the type system for the LLVMABI library, which mirrors
11/// ABI-relevant aspects of frontend types.
12///
13//===----------------------------------------------------------------------===//
14#ifndef LLVM_ABI_TYPES_H
15#define LLVM_ABI_TYPES_H
16
17#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/STLExtras.h"
25
26namespace llvm {
27namespace abi {
28
40
41/// Represents the ABI-specific view of a type in LLVM.
42///
43/// This abstracts platform and language-specific ABI details from the
44/// frontend, providing a consistent interface for the ABI Library.
45class Type {
46private:
47 TypeSize getTypeStoreSize() const {
48 TypeSize StoreSizeInBits = getTypeStoreSizeInBits();
49 return {StoreSizeInBits.getKnownMinValue() / 8,
50 StoreSizeInBits.isScalable()};
51 }
52 TypeSize getTypeStoreSizeInBits() const {
53 TypeSize BaseSize = getSizeInBits();
54 uint64_t AlignedSizeInBits =
55 alignToPowerOf2(BaseSize.getKnownMinValue(), 8);
56 return {AlignedSizeInBits, BaseSize.isScalable()};
57 }
58
59protected:
63
66
67public:
68 TypeKind getKind() const { return Kind; }
69 TypeSize getSizeInBits() const { return SizeInBits; }
70 Align getAlignment() const { return ABIAlignment; }
71
73 return alignTo(getTypeStoreSize(), getAlignment().value());
74 }
75
76 bool isVoid() const { return Kind == TypeKind::Void; }
77 bool isInteger() const { return Kind == TypeKind::Integer; }
78 bool isFloat() const { return Kind == TypeKind::Float; }
79 bool isPointer() const { return Kind == TypeKind::Pointer; }
80 bool isArray() const { return Kind == TypeKind::Array; }
81 bool isVector() const { return Kind == TypeKind::Vector; }
82 bool isRecord() const { return Kind == TypeKind::Record; }
83 bool isMemberPointer() const { return Kind == TypeKind::MemberPointer; }
84 bool isComplex() const { return Kind == TypeKind::Complex; }
85};
86
87class VoidType : public Type {
88public:
89 VoidType() : Type(TypeKind::Void, TypeSize::getFixed(0), Align(1)) {}
90
91 static bool classof(const Type *T) { return T->getKind() == TypeKind::Void; }
92};
93
94class ComplexType : public Type {
95public:
96 ComplexType(const Type *ElementType, uint64_t SizeInBits, Align Alignment)
97 : Type(TypeKind::Complex, TypeSize::getFixed(SizeInBits), Alignment),
98 ElementType(ElementType) {}
99
100 const Type *getElementType() const { return ElementType; }
101
102 static bool classof(const Type *T) {
103 return T->getKind() == TypeKind::Complex;
104 }
105
106private:
107 const Type *ElementType;
108};
109
110class IntegerType : public Type {
111private:
112 bool IsSigned;
113 bool IsBitInt;
114
115public:
116 IntegerType(uint64_t BitWidth, Align ABIAlign, bool IsSigned,
117 bool IsBitInt = false)
118 : Type(TypeKind::Integer, TypeSize::getFixed(BitWidth), ABIAlign),
119 IsSigned(IsSigned), IsBitInt(IsBitInt) {}
120
121 bool isSigned() const { return IsSigned; }
122 bool isBitInt() const { return IsBitInt; }
123 bool isBool() const {
124 return getSizeInBits().getFixedValue() == 1 && !IsBitInt;
125 }
126
127 static bool classof(const Type *T) {
128 return T->getKind() == TypeKind::Integer;
129 }
130};
131
132class FloatType : public Type {
133private:
134 const fltSemantics *Semantics;
135
136public:
137 FloatType(const fltSemantics &FloatSemantics, Align ABIAlign)
139 TypeSize::getFixed(APFloat::getSizeInBits(FloatSemantics)),
140 ABIAlign),
141 Semantics(&FloatSemantics) {}
142
143 const fltSemantics *getSemantics() const { return Semantics; }
144 static bool classof(const Type *T) { return T->getKind() == TypeKind::Float; }
145};
146
147class PointerLikeType : public Type {
148protected:
149 unsigned AddrSpace;
151 : Type(K, SizeInBits, ABIAlign), AddrSpace(AS) {}
152
153public:
154 unsigned getAddrSpace() const { return AddrSpace; }
155 bool isMemberPointer() const { return getKind() == TypeKind::MemberPointer; }
156
157 static bool classof(const Type *T) {
158 return T->getKind() == TypeKind::Pointer ||
159 T->getKind() == TypeKind::MemberPointer;
160 }
161};
162
164public:
165 PointerType(uint64_t Size, Align ABIAlign, unsigned AddressSpace = 0)
166 : PointerLikeType(TypeKind::Pointer, TypeSize::getFixed(Size), ABIAlign,
167 AddressSpace) {}
168
169 static bool classof(const Type *T) {
170 return T->getKind() == TypeKind::Pointer;
171 }
172};
173
175private:
176 bool IsFunctionPointer;
177
178public:
179 MemberPointerType(bool IsFunctionPointer, uint64_t SizeInBits, Align ABIAlign,
180 unsigned AddressSpace = 0)
182 ABIAlign, AddressSpace),
183 IsFunctionPointer(IsFunctionPointer) {}
184 bool isFunctionPointer() const { return IsFunctionPointer; }
185
186 static bool classof(const Type *T) {
187 return T->getKind() == TypeKind::MemberPointer;
188 }
189};
190
191class ArrayType : public Type {
192private:
193 const Type *ElementType;
194 uint64_t NumElements;
195 bool IsMatrix;
196
197public:
198 ArrayType(const Type *ElementType, uint64_t NumElements, uint64_t SizeInBits,
199 bool IsMatrixType = false)
200 : Type(TypeKind::Array, TypeSize::getFixed(SizeInBits),
201 ElementType->getAlignment()),
202 ElementType(ElementType), NumElements(NumElements),
203 IsMatrix(IsMatrixType) {}
204
205 const Type *getElementType() const { return ElementType; }
206 uint64_t getNumElements() const { return NumElements; }
207 bool isMatrixType() const { return IsMatrix; }
208
209 static bool classof(const Type *T) { return T->getKind() == TypeKind::Array; }
210};
211
212class VectorType : public Type {
213private:
214 const Type *ElementType;
215 ElementCount NumElements;
216
217public:
218 VectorType(const Type *ElementType, ElementCount NumElements, Align ABIAlign)
220 TypeSize(ElementType->getSizeInBits().getFixedValue() *
221 NumElements.getKnownMinValue(),
222 NumElements.isScalable()),
223 ABIAlign),
224 ElementType(ElementType), NumElements(NumElements) {}
225
226 const Type *getElementType() const { return ElementType; }
227 ElementCount getNumElements() const { return NumElements; }
228
229 static bool classof(const Type *T) {
230 return T->getKind() == TypeKind::Vector;
231 }
232};
233
248
250
251enum RecordFlags : unsigned {
252 None = 0,
254 IsUnion = 1 << 1,
256 IsCXXRecord = 1 << 3,
260};
261
262class RecordType : public Type {
263private:
264 ArrayRef<FieldInfo> Fields;
265 ArrayRef<FieldInfo> BaseClasses;
266 ArrayRef<FieldInfo> VirtualBaseClasses;
267 StructPacking Packing;
268 RecordFlags Flags;
269
270public:
275 : Type(TypeKind::Record, Size, Align), Fields(StructFields),
276 BaseClasses(Bases), VirtualBaseClasses(VBases), Packing(Pack),
277 Flags(RecFlags) {}
278 uint32_t getNumFields() const { return Fields.size(); }
279 StructPacking getPacking() const { return Packing; }
280
281 bool isUnion() const {
282 return static_cast<unsigned>(Flags & RecordFlags::IsUnion) != 0;
283 }
284 bool isCXXRecord() const {
285 return static_cast<unsigned>(Flags & RecordFlags::IsCXXRecord) != 0;
286 }
287 bool isPolymorphic() const {
288 return static_cast<unsigned>(Flags & RecordFlags::IsPolymorphic) != 0;
289 }
290 bool canPassInRegisters() const {
291 return static_cast<unsigned>(Flags & RecordFlags::CanPassInRegisters) != 0;
292 }
294 return static_cast<unsigned>(Flags & RecordFlags::HasFlexibleArrayMember) !=
295 0;
296 }
297 uint32_t getNumBaseClasses() const { return BaseClasses.size(); }
299 return VirtualBaseClasses.size();
300 }
301 bool isTransparentUnion() const {
302 return static_cast<unsigned>(Flags & RecordFlags::IsTransparent) != 0;
303 }
304 ArrayRef<FieldInfo> getFields() const { return Fields; }
305 ArrayRef<FieldInfo> getBaseClasses() const { return BaseClasses; }
307 return VirtualBaseClasses;
308 }
309};
310
311/// TypeBuilder manages the lifecycle of ABI types using bump pointer
312/// allocation. Types created by a TypeBuilder are valid for the lifetime of the
313/// allocator.
314///
315/// Example usage:
316/// \code
317/// BumpPtrAllocator Alloc;
318/// TypeBuilder Builder(Alloc);
319/// const auto *IntTy = Builder.getIntegerType(32, Align(4), true);
320/// \endcode
322private:
323 BumpPtrAllocator &Allocator;
324
325public:
326 explicit TypeBuilder(BumpPtrAllocator &Alloc) : Allocator(Alloc) {}
327
329 return new (Allocator.Allocate<VoidType>()) VoidType();
330 }
331
333 bool IsBitInt = false) {
334 return new (Allocator.Allocate<IntegerType>())
335 IntegerType(BitWidth, Align, Signed, IsBitInt);
336 }
337
338 const FloatType *getFloatType(const fltSemantics &Semantics, Align Align) {
339 return new (Allocator.Allocate<FloatType>()) FloatType(Semantics, Align);
340 }
341
343 unsigned Addrspace = 0) {
344 return new (Allocator.Allocate<PointerType>())
345 PointerType(Size, Align, Addrspace);
346 }
347
348 const ArrayType *getArrayType(const Type *ElementType, uint64_t NumElements,
349 uint64_t SizeInBits,
350 bool IsMatrixType = false) {
351 return new (Allocator.Allocate<ArrayType>())
352 ArrayType(ElementType, NumElements, SizeInBits, IsMatrixType);
353 }
354
355 const VectorType *getVectorType(const Type *ElementType,
356 ElementCount NumElements, Align Align) {
357 return new (Allocator.Allocate<VectorType>())
358 VectorType(ElementType, NumElements, Align);
359 }
360
362 Align Align,
364 ArrayRef<FieldInfo> BaseClasses = {},
365 ArrayRef<FieldInfo> VirtualBaseClasses = {},
366 RecordFlags RecFlags = RecordFlags::None) {
367 FieldInfo *FieldArray = Allocator.Allocate<FieldInfo>(Fields.size());
368 std::copy(Fields.begin(), Fields.end(), FieldArray);
369
370 FieldInfo *BaseArray = nullptr;
371 if (!BaseClasses.empty()) {
372 BaseArray = Allocator.Allocate<FieldInfo>(BaseClasses.size());
373 std::copy(BaseClasses.begin(), BaseClasses.end(), BaseArray);
374 }
375
376 FieldInfo *VBaseArray = nullptr;
377 if (!VirtualBaseClasses.empty()) {
378 VBaseArray = Allocator.Allocate<FieldInfo>(VirtualBaseClasses.size());
379 std::copy(VirtualBaseClasses.begin(), VirtualBaseClasses.end(),
380 VBaseArray);
381 }
382
383 ArrayRef<FieldInfo> FieldsRef(FieldArray, Fields.size());
384 ArrayRef<FieldInfo> BasesRef(BaseArray, BaseClasses.size());
385 ArrayRef<FieldInfo> VBasesRef(VBaseArray, VirtualBaseClasses.size());
386
387 return new (Allocator.Allocate<RecordType>())
388 RecordType(FieldsRef, BasesRef, VBasesRef, Size, Align, Pack, RecFlags);
389 }
390
392 Align Align,
394 RecordFlags RecFlags = RecordFlags::None) {
395 FieldInfo *FieldArray = Allocator.Allocate<FieldInfo>(Fields.size());
396
397 for (size_t I = 0, E = Fields.size(); I != E; ++I) {
398 const FieldInfo &Field = Fields[I];
399 new (&FieldArray[I])
400 FieldInfo(Field.FieldType, 0, Field.IsBitField, Field.BitFieldWidth,
401 Field.IsUnnamedBitfield);
402 }
403
404 ArrayRef<FieldInfo> FieldsRef(FieldArray, Fields.size());
405
406 return new (Allocator.Allocate<RecordType>())
408 Size, Align, Pack, RecFlags | RecordFlags::IsUnion);
409 }
410
411 const ComplexType *getComplexType(const Type *ElementType, Align Align) {
412 // Complex types have two elements (real and imaginary parts)
413 uint64_t ElementSizeInBits = ElementType->getSizeInBits().getFixedValue();
414 uint64_t ComplexSizeInBits = ElementSizeInBits * 2;
415
416 return new (Allocator.Allocate<ComplexType>())
417 ComplexType(ElementType, ComplexSizeInBits, Align);
418 }
419
420 const MemberPointerType *getMemberPointerType(bool IsFunctionPointer,
421 uint64_t SizeInBits,
422 Align Align) {
423 return new (Allocator.Allocate<MemberPointerType>())
424 MemberPointerType(IsFunctionPointer, SizeInBits, Align);
425 }
426};
427
428} // namespace abi
429} // namespace llvm
430
431#endif
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define I(x, y, z)
Definition MD5.cpp:57
#define T
OptimizedStructLayoutField Field
Basic Register Allocator
This file contains some templates that are useful if you are working with the STL at all.
FunctionLoweringInfo::StatepointRelocationRecord RecordType
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
const Type * getElementType() const
Definition Types.h:205
bool isMatrixType() const
Definition Types.h:207
static bool classof(const Type *T)
Definition Types.h:209
ArrayType(const Type *ElementType, uint64_t NumElements, uint64_t SizeInBits, bool IsMatrixType=false)
Definition Types.h:198
uint64_t getNumElements() const
Definition Types.h:206
ComplexType(const Type *ElementType, uint64_t SizeInBits, Align Alignment)
Definition Types.h:96
const Type * getElementType() const
Definition Types.h:100
static bool classof(const Type *T)
Definition Types.h:102
FloatType(const fltSemantics &FloatSemantics, Align ABIAlign)
Definition Types.h:137
const fltSemantics * getSemantics() const
Definition Types.h:143
static bool classof(const Type *T)
Definition Types.h:144
static bool classof(const Type *T)
Definition Types.h:127
bool isBitInt() const
Definition Types.h:122
IntegerType(uint64_t BitWidth, Align ABIAlign, bool IsSigned, bool IsBitInt=false)
Definition Types.h:116
bool isBool() const
Definition Types.h:123
bool isSigned() const
Definition Types.h:121
bool isFunctionPointer() const
Definition Types.h:184
static bool classof(const Type *T)
Definition Types.h:186
MemberPointerType(bool IsFunctionPointer, uint64_t SizeInBits, Align ABIAlign, unsigned AddressSpace=0)
Definition Types.h:179
PointerLikeType(TypeKind K, TypeSize SizeInBits, Align ABIAlign, unsigned AS)
Definition Types.h:150
static bool classof(const Type *T)
Definition Types.h:157
bool isMemberPointer() const
Definition Types.h:155
unsigned getAddrSpace() const
Definition Types.h:154
PointerType(uint64_t Size, Align ABIAlign, unsigned AddressSpace=0)
Definition Types.h:165
static bool classof(const Type *T)
Definition Types.h:169
bool isPolymorphic() const
Definition Types.h:287
bool isUnion() const
Definition Types.h:281
bool canPassInRegisters() const
Definition Types.h:290
ArrayRef< FieldInfo > getBaseClasses() const
Definition Types.h:305
uint32_t getNumBaseClasses() const
Definition Types.h:297
ArrayRef< FieldInfo > getFields() const
Definition Types.h:304
uint32_t getNumVirtualBaseClasses() const
Definition Types.h:298
bool hasFlexibleArrayMember() const
Definition Types.h:293
bool isCXXRecord() const
Definition Types.h:284
bool isTransparentUnion() const
Definition Types.h:301
RecordType(ArrayRef< FieldInfo > StructFields, ArrayRef< FieldInfo > Bases, ArrayRef< FieldInfo > VBases, TypeSize Size, Align Align, StructPacking Pack=StructPacking::Default, RecordFlags RecFlags=RecordFlags::None)
Definition Types.h:271
uint32_t getNumFields() const
Definition Types.h:278
ArrayRef< FieldInfo > getVirtualBaseClasses() const
Definition Types.h:306
StructPacking getPacking() const
Definition Types.h:279
const ComplexType * getComplexType(const Type *ElementType, Align Align)
Definition Types.h:411
const FloatType * getFloatType(const fltSemantics &Semantics, Align Align)
Definition Types.h:338
const IntegerType * getIntegerType(uint64_t BitWidth, Align Align, bool Signed, bool IsBitInt=false)
Definition Types.h:332
const MemberPointerType * getMemberPointerType(bool IsFunctionPointer, uint64_t SizeInBits, Align Align)
Definition Types.h:420
const RecordType * getRecordType(ArrayRef< FieldInfo > Fields, TypeSize Size, Align Align, StructPacking Pack=StructPacking::Default, ArrayRef< FieldInfo > BaseClasses={}, ArrayRef< FieldInfo > VirtualBaseClasses={}, RecordFlags RecFlags=RecordFlags::None)
Definition Types.h:361
TypeBuilder(BumpPtrAllocator &Alloc)
Definition Types.h:326
const ArrayType * getArrayType(const Type *ElementType, uint64_t NumElements, uint64_t SizeInBits, bool IsMatrixType=false)
Definition Types.h:348
const PointerType * getPointerType(uint64_t Size, Align Align, unsigned Addrspace=0)
Definition Types.h:342
const VoidType * getVoidType()
Definition Types.h:328
const RecordType * getUnionType(ArrayRef< FieldInfo > Fields, TypeSize Size, Align Align, StructPacking Pack=StructPacking::Default, RecordFlags RecFlags=RecordFlags::None)
Definition Types.h:391
const VectorType * getVectorType(const Type *ElementType, ElementCount NumElements, Align Align)
Definition Types.h:355
Represents the ABI-specific view of a type in LLVM.
Definition Types.h:45
TypeSize getTypeAllocSize() const
Definition Types.h:72
Align ABIAlignment
Definition Types.h:62
bool isMemberPointer() const
Definition Types.h:83
bool isVoid() const
Definition Types.h:76
TypeSize SizeInBits
Definition Types.h:61
TypeSize getSizeInBits() const
Definition Types.h:69
bool isInteger() const
Definition Types.h:77
Type(TypeKind K, TypeSize SizeInBits, Align ABIAlign)
Definition Types.h:64
TypeKind Kind
Definition Types.h:60
bool isRecord() const
Definition Types.h:82
bool isArray() const
Definition Types.h:80
bool isVector() const
Definition Types.h:81
bool isFloat() const
Definition Types.h:78
Align getAlignment() const
Definition Types.h:70
bool isComplex() const
Definition Types.h:84
TypeKind getKind() const
Definition Types.h:68
bool isPointer() const
Definition Types.h:79
ElementCount getNumElements() const
Definition Types.h:227
const Type * getElementType() const
Definition Types.h:226
static bool classof(const Type *T)
Definition Types.h:229
VectorType(const Type *ElementType, ElementCount NumElements, Align ABIAlign)
Definition Types.h:218
static bool classof(const Type *T)
Definition Types.h:91
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
RecordFlags
Definition Types.h:251
@ IsPolymorphic
Definition Types.h:257
@ IsCXXRecord
Definition Types.h:256
@ CanPassInRegisters
Definition Types.h:253
@ IsTransparent
Definition Types.h:255
@ LLVM_MARK_AS_BITMASK_ENUM
Definition Types.h:259
@ IsUnion
Definition Types.h:254
@ HasFlexibleArrayMember
Definition Types.h:258
StructPacking
Definition Types.h:249
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
Definition MathExtras.h:493
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned BitWidth
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
FieldInfo(const Type *FieldType, uint64_t OffsetInBits=0, bool IsBitField=false, uint64_t BitFieldWidth=0, bool IsUnnamedBitField=false)
Definition Types.h:241
const Type * FieldType
Definition Types.h:235
uint64_t BitFieldWidth
Definition Types.h:237
uint64_t OffsetInBits
Definition Types.h:236