1 //===- llvm/DerivedTypes.h - Classes for handling data types ----*- 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 contains the declarations of classes that represent "derived
10 // types". These are things like "arrays of x" or "structure of x, y, z" or
11 // "function returning x taking (y,z) as parameters", etc...
12 //
13 // The implementations of these classes live in the Type.cpp file.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_IR_DERIVEDTYPES_H
18 #define LLVM_IR_DERIVEDTYPES_H
19
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/TypeSize.h"
27 #include <cassert>
28 #include <cstdint>
29
30 namespace llvm {
31
32 class Value;
33 class APInt;
34 class LLVMContext;
35
36 /// Class to represent integer types. Note that this class is also used to
37 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
38 /// Int64Ty.
39 /// Integer representation type
40 class IntegerType : public Type {
41 friend class LLVMContextImpl;
42
43 protected:
IntegerType(LLVMContext & C,unsigned NumBits)44 explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){
45 setSubclassData(NumBits);
46 }
47
48 public:
49 /// This enum is just used to hold constants we need for IntegerType.
50 enum {
51 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
52 MAX_INT_BITS = (1<<23) ///< Maximum number of bits that can be specified
53 ///< Note that bit width is stored in the Type classes SubclassData field
54 ///< which has 24 bits. SelectionDAG type legalization can require a
55 ///< power of 2 IntegerType, so limit to the largest representable power
56 ///< of 2, 8388608.
57 };
58
59 /// This static method is the primary way of constructing an IntegerType.
60 /// If an IntegerType with the same NumBits value was previously instantiated,
61 /// that instance will be returned. Otherwise a new one will be created. Only
62 /// one instance with a given NumBits value is ever created.
63 /// Get or create an IntegerType instance.
64 static IntegerType *get(LLVMContext &C, unsigned NumBits);
65
66 /// Returns type twice as wide the input type.
getExtendedType()67 IntegerType *getExtendedType() const {
68 return Type::getIntNTy(getContext(), 2 * getScalarSizeInBits());
69 }
70
71 /// Get the number of bits in this IntegerType
getBitWidth()72 unsigned getBitWidth() const { return getSubclassData(); }
73
74 /// Return a bitmask with ones set for all of the bits that can be set by an
75 /// unsigned version of this type. This is 0xFF for i8, 0xFFFF for i16, etc.
getBitMask()76 uint64_t getBitMask() const {
77 return ~uint64_t(0UL) >> (64-getBitWidth());
78 }
79
80 /// Return a uint64_t with just the most significant bit set (the sign bit, if
81 /// the value is treated as a signed number).
getSignBit()82 uint64_t getSignBit() const {
83 return 1ULL << (getBitWidth()-1);
84 }
85
86 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
87 /// @returns a bit mask with ones set for all the bits of this type.
88 /// Get a bit mask for this type.
89 APInt getMask() const;
90
91 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)92 static bool classof(const Type *T) {
93 return T->getTypeID() == IntegerTyID;
94 }
95 };
96
getIntegerBitWidth()97 unsigned Type::getIntegerBitWidth() const {
98 return cast<IntegerType>(this)->getBitWidth();
99 }
100
101 /// Class to represent function types
102 ///
103 class FunctionType : public Type {
104 FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
105
106 public:
107 FunctionType(const FunctionType &) = delete;
108 FunctionType &operator=(const FunctionType &) = delete;
109
110 /// This static method is the primary way of constructing a FunctionType.
111 static FunctionType *get(Type *Result,
112 ArrayRef<Type*> Params, bool isVarArg);
113
114 /// Create a FunctionType taking no parameters.
115 static FunctionType *get(Type *Result, bool isVarArg);
116
117 /// Return true if the specified type is valid as a return type.
118 static bool isValidReturnType(Type *RetTy);
119
120 /// Return true if the specified type is valid as an argument type.
121 static bool isValidArgumentType(Type *ArgTy);
122
isVarArg()123 bool isVarArg() const { return getSubclassData()!=0; }
getReturnType()124 Type *getReturnType() const { return ContainedTys[0]; }
125
126 using param_iterator = Type::subtype_iterator;
127
param_begin()128 param_iterator param_begin() const { return ContainedTys + 1; }
param_end()129 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
params()130 ArrayRef<Type *> params() const {
131 return ArrayRef(param_begin(), param_end());
132 }
133
134 /// Parameter type accessors.
getParamType(unsigned i)135 Type *getParamType(unsigned i) const {
136 assert(i < getNumParams() && "getParamType() out of range!");
137 return ContainedTys[i + 1];
138 }
139
140 /// Return the number of fixed parameters this function type requires.
141 /// This does not consider varargs.
getNumParams()142 unsigned getNumParams() const { return NumContainedTys - 1; }
143
144 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)145 static bool classof(const Type *T) {
146 return T->getTypeID() == FunctionTyID;
147 }
148 };
149 static_assert(alignof(FunctionType) >= alignof(Type *),
150 "Alignment sufficient for objects appended to FunctionType");
151
isFunctionVarArg()152 bool Type::isFunctionVarArg() const {
153 return cast<FunctionType>(this)->isVarArg();
154 }
155
getFunctionParamType(unsigned i)156 Type *Type::getFunctionParamType(unsigned i) const {
157 return cast<FunctionType>(this)->getParamType(i);
158 }
159
getFunctionNumParams()160 unsigned Type::getFunctionNumParams() const {
161 return cast<FunctionType>(this)->getNumParams();
162 }
163
164 /// A handy container for a FunctionType+Callee-pointer pair, which can be
165 /// passed around as a single entity. This assists in replacing the use of
166 /// PointerType::getElementType() to access the function's type, since that's
167 /// slated for removal as part of the [opaque pointer types] project.
168 class FunctionCallee {
169 public:
170 // Allow implicit conversion from types which have a getFunctionType member
171 // (e.g. Function and InlineAsm).
172 template <typename T, typename U = decltype(&T::getFunctionType)>
FunctionCallee(T * Fn)173 FunctionCallee(T *Fn)
174 : FnTy(Fn ? Fn->getFunctionType() : nullptr), Callee(Fn) {}
175
FunctionCallee(FunctionType * FnTy,Value * Callee)176 FunctionCallee(FunctionType *FnTy, Value *Callee)
177 : FnTy(FnTy), Callee(Callee) {
178 assert((FnTy == nullptr) == (Callee == nullptr));
179 }
180
FunctionCallee(std::nullptr_t)181 FunctionCallee(std::nullptr_t) {}
182
183 FunctionCallee() = default;
184
getFunctionType()185 FunctionType *getFunctionType() { return FnTy; }
186
getCallee()187 Value *getCallee() { return Callee; }
188
189 explicit operator bool() { return Callee; }
190
191 private:
192 FunctionType *FnTy = nullptr;
193 Value *Callee = nullptr;
194 };
195
196 /// Class to represent struct types. There are two different kinds of struct
197 /// types: Literal structs and Identified structs.
198 ///
199 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
200 /// always have a body when created. You can get one of these by using one of
201 /// the StructType::get() forms.
202 ///
203 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
204 /// uniqued. The names for identified structs are managed at the LLVMContext
205 /// level, so there can only be a single identified struct with a given name in
206 /// a particular LLVMContext. Identified structs may also optionally be opaque
207 /// (have no body specified). You get one of these by using one of the
208 /// StructType::create() forms.
209 ///
210 /// Independent of what kind of struct you have, the body of a struct type are
211 /// laid out in memory consecutively with the elements directly one after the
212 /// other (if the struct is packed) or (if not packed) with padding between the
213 /// elements as defined by DataLayout (which is required to match what the code
214 /// generator for a target expects).
215 ///
216 class StructType : public Type {
StructType(LLVMContext & C)217 StructType(LLVMContext &C) : Type(C, StructTyID) {}
218
219 enum {
220 /// This is the contents of the SubClassData field.
221 SCDB_HasBody = 1,
222 SCDB_Packed = 2,
223 SCDB_IsLiteral = 4,
224 SCDB_IsSized = 8,
225 SCDB_ContainsScalableVector = 16,
226 SCDB_NotContainsScalableVector = 32
227 };
228
229 /// For a named struct that actually has a name, this is a pointer to the
230 /// symbol table entry (maintained by LLVMContext) for the struct.
231 /// This is null if the type is an literal struct or if it is a identified
232 /// type that has an empty name.
233 void *SymbolTableEntry = nullptr;
234
235 public:
236 StructType(const StructType &) = delete;
237 StructType &operator=(const StructType &) = delete;
238
239 /// This creates an identified struct.
240 static StructType *create(LLVMContext &Context, StringRef Name);
241 static StructType *create(LLVMContext &Context);
242
243 static StructType *create(ArrayRef<Type *> Elements, StringRef Name,
244 bool isPacked = false);
245 static StructType *create(ArrayRef<Type *> Elements);
246 static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements,
247 StringRef Name, bool isPacked = false);
248 static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements);
249 template <class... Tys>
250 static std::enable_if_t<are_base_of<Type, Tys...>::value, StructType *>
create(StringRef Name,Type * elt1,Tys * ...elts)251 create(StringRef Name, Type *elt1, Tys *... elts) {
252 assert(elt1 && "Cannot create a struct type with no elements with this");
253 return create(ArrayRef<Type *>({elt1, elts...}), Name);
254 }
255
256 /// This static method is the primary way to create a literal StructType.
257 static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
258 bool isPacked = false);
259
260 /// Create an empty structure type.
261 static StructType *get(LLVMContext &Context, bool isPacked = false);
262
263 /// This static method is a convenience method for creating structure types by
264 /// specifying the elements as arguments. Note that this method always returns
265 /// a non-packed struct, and requires at least one element type.
266 template <class... Tys>
267 static std::enable_if_t<are_base_of<Type, Tys...>::value, StructType *>
get(Type * elt1,Tys * ...elts)268 get(Type *elt1, Tys *... elts) {
269 assert(elt1 && "Cannot create a struct type with no elements with this");
270 LLVMContext &Ctx = elt1->getContext();
271 return StructType::get(Ctx, ArrayRef<Type *>({elt1, elts...}));
272 }
273
274 /// Return the type with the specified name, or null if there is none by that
275 /// name.
276 static StructType *getTypeByName(LLVMContext &C, StringRef Name);
277
isPacked()278 bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
279
280 /// Return true if this type is uniqued by structural equivalence, false if it
281 /// is a struct definition.
isLiteral()282 bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
283
284 /// Return true if this is a type with an identity that has no body specified
285 /// yet. These prints as 'opaque' in .ll files.
isOpaque()286 bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
287
288 /// isSized - Return true if this is a sized type.
289 bool isSized(SmallPtrSetImpl<Type *> *Visited = nullptr) const;
290
291 /// Returns true if this struct contains a scalable vector.
292 bool
293 containsScalableVectorType(SmallPtrSetImpl<Type *> *Visited = nullptr) const;
294
295 /// Returns true if this struct contains homogeneous scalable vector types.
296 /// Note that the definition of homogeneous scalable vector type is not
297 /// recursive here. That means the following structure will return false
298 /// when calling this function.
299 /// {{<vscale x 2 x i32>, <vscale x 4 x i64>},
300 /// {<vscale x 2 x i32>, <vscale x 4 x i64>}}
301 bool containsHomogeneousScalableVectorTypes() const;
302
303 /// Return true if this is a named struct that has a non-empty name.
hasName()304 bool hasName() const { return SymbolTableEntry != nullptr; }
305
306 /// Return the name for this struct type if it has an identity.
307 /// This may return an empty string for an unnamed struct type. Do not call
308 /// this on an literal type.
309 StringRef getName() const;
310
311 /// Change the name of this type to the specified name, or to a name with a
312 /// suffix if there is a collision. Do not call this on an literal type.
313 void setName(StringRef Name);
314
315 /// Specify a body for an opaque identified type.
316 void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
317
318 template <typename... Tys>
319 std::enable_if_t<are_base_of<Type, Tys...>::value, void>
setBody(Type * elt1,Tys * ...elts)320 setBody(Type *elt1, Tys *... elts) {
321 assert(elt1 && "Cannot create a struct type with no elements with this");
322 setBody(ArrayRef<Type *>({elt1, elts...}));
323 }
324
325 /// Return true if the specified type is valid as a element type.
326 static bool isValidElementType(Type *ElemTy);
327
328 // Iterator access to the elements.
329 using element_iterator = Type::subtype_iterator;
330
element_begin()331 element_iterator element_begin() const { return ContainedTys; }
element_end()332 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
elements()333 ArrayRef<Type *> elements() const {
334 return ArrayRef(element_begin(), element_end());
335 }
336
337 /// Return true if this is layout identical to the specified struct.
338 bool isLayoutIdentical(StructType *Other) const;
339
340 /// Random access to the elements
getNumElements()341 unsigned getNumElements() const { return NumContainedTys; }
getElementType(unsigned N)342 Type *getElementType(unsigned N) const {
343 assert(N < NumContainedTys && "Element number out of range!");
344 return ContainedTys[N];
345 }
346 /// Given an index value into the type, return the type of the element.
347 Type *getTypeAtIndex(const Value *V) const;
getTypeAtIndex(unsigned N)348 Type *getTypeAtIndex(unsigned N) const { return getElementType(N); }
349 bool indexValid(const Value *V) const;
indexValid(unsigned Idx)350 bool indexValid(unsigned Idx) const { return Idx < getNumElements(); }
351
352 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)353 static bool classof(const Type *T) {
354 return T->getTypeID() == StructTyID;
355 }
356 };
357
getStructName()358 StringRef Type::getStructName() const {
359 return cast<StructType>(this)->getName();
360 }
361
getStructNumElements()362 unsigned Type::getStructNumElements() const {
363 return cast<StructType>(this)->getNumElements();
364 }
365
getStructElementType(unsigned N)366 Type *Type::getStructElementType(unsigned N) const {
367 return cast<StructType>(this)->getElementType(N);
368 }
369
370 /// Class to represent array types.
371 class ArrayType : public Type {
372 /// The element type of the array.
373 Type *ContainedType;
374 /// Number of elements in the array.
375 uint64_t NumElements;
376
377 ArrayType(Type *ElType, uint64_t NumEl);
378
379 public:
380 ArrayType(const ArrayType &) = delete;
381 ArrayType &operator=(const ArrayType &) = delete;
382
getNumElements()383 uint64_t getNumElements() const { return NumElements; }
getElementType()384 Type *getElementType() const { return ContainedType; }
385
386 /// This static method is the primary way to construct an ArrayType
387 static ArrayType *get(Type *ElementType, uint64_t NumElements);
388
389 /// Return true if the specified type is valid as a element type.
390 static bool isValidElementType(Type *ElemTy);
391
392 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)393 static bool classof(const Type *T) {
394 return T->getTypeID() == ArrayTyID;
395 }
396 };
397
getArrayNumElements()398 uint64_t Type::getArrayNumElements() const {
399 return cast<ArrayType>(this)->getNumElements();
400 }
401
402 /// Base class of all SIMD vector types
403 class VectorType : public Type {
404 /// A fully specified VectorType is of the form <vscale x n x Ty>. 'n' is the
405 /// minimum number of elements of type Ty contained within the vector, and
406 /// 'vscale x' indicates that the total element count is an integer multiple
407 /// of 'n', where the multiple is either guaranteed to be one, or is
408 /// statically unknown at compile time.
409 ///
410 /// If the multiple is known to be 1, then the extra term is discarded in
411 /// textual IR:
412 ///
413 /// <4 x i32> - a vector containing 4 i32s
414 /// <vscale x 4 x i32> - a vector containing an unknown integer multiple
415 /// of 4 i32s
416
417 /// The element type of the vector.
418 Type *ContainedType;
419
420 protected:
421 /// The element quantity of this vector. The meaning of this value depends
422 /// on the type of vector:
423 /// - For FixedVectorType = <ElementQuantity x ty>, there are
424 /// exactly ElementQuantity elements in this vector.
425 /// - For ScalableVectorType = <vscale x ElementQuantity x ty>,
426 /// there are vscale * ElementQuantity elements in this vector, where
427 /// vscale is a runtime-constant integer greater than 0.
428 const unsigned ElementQuantity;
429
430 VectorType(Type *ElType, unsigned EQ, Type::TypeID TID);
431
432 public:
433 VectorType(const VectorType &) = delete;
434 VectorType &operator=(const VectorType &) = delete;
435
getElementType()436 Type *getElementType() const { return ContainedType; }
437
438 /// This static method is the primary way to construct an VectorType.
439 static VectorType *get(Type *ElementType, ElementCount EC);
440
get(Type * ElementType,unsigned NumElements,bool Scalable)441 static VectorType *get(Type *ElementType, unsigned NumElements,
442 bool Scalable) {
443 return VectorType::get(ElementType,
444 ElementCount::get(NumElements, Scalable));
445 }
446
get(Type * ElementType,const VectorType * Other)447 static VectorType *get(Type *ElementType, const VectorType *Other) {
448 return VectorType::get(ElementType, Other->getElementCount());
449 }
450
451 /// This static method gets a VectorType with the same number of elements as
452 /// the input type, and the element type is an integer type of the same width
453 /// as the input element type.
getInteger(VectorType * VTy)454 static VectorType *getInteger(VectorType *VTy) {
455 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
456 assert(EltBits && "Element size must be of a non-zero size");
457 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
458 return VectorType::get(EltTy, VTy->getElementCount());
459 }
460
461 /// This static method is like getInteger except that the element types are
462 /// twice as wide as the elements in the input type.
getExtendedElementVectorType(VectorType * VTy)463 static VectorType *getExtendedElementVectorType(VectorType *VTy) {
464 assert(VTy->isIntOrIntVectorTy() && "VTy expected to be a vector of ints.");
465 auto *EltTy = cast<IntegerType>(VTy->getElementType());
466 return VectorType::get(EltTy->getExtendedType(), VTy->getElementCount());
467 }
468
469 // This static method gets a VectorType with the same number of elements as
470 // the input type, and the element type is an integer or float type which
471 // is half as wide as the elements in the input type.
getTruncatedElementVectorType(VectorType * VTy)472 static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
473 Type *EltTy;
474 if (VTy->getElementType()->isFloatingPointTy()) {
475 switch(VTy->getElementType()->getTypeID()) {
476 case DoubleTyID:
477 EltTy = Type::getFloatTy(VTy->getContext());
478 break;
479 case FloatTyID:
480 EltTy = Type::getHalfTy(VTy->getContext());
481 break;
482 default:
483 llvm_unreachable("Cannot create narrower fp vector element type");
484 }
485 } else {
486 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
487 assert((EltBits & 1) == 0 &&
488 "Cannot truncate vector element with odd bit-width");
489 EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
490 }
491 return VectorType::get(EltTy, VTy->getElementCount());
492 }
493
494 // This static method returns a VectorType with a smaller number of elements
495 // of a larger type than the input element type. For example, a <16 x i8>
496 // subdivided twice would return <4 x i32>
getSubdividedVectorType(VectorType * VTy,int NumSubdivs)497 static VectorType *getSubdividedVectorType(VectorType *VTy, int NumSubdivs) {
498 for (int i = 0; i < NumSubdivs; ++i) {
499 VTy = VectorType::getDoubleElementsVectorType(VTy);
500 VTy = VectorType::getTruncatedElementVectorType(VTy);
501 }
502 return VTy;
503 }
504
505 /// This static method returns a VectorType with half as many elements as the
506 /// input type and the same element type.
getHalfElementsVectorType(VectorType * VTy)507 static VectorType *getHalfElementsVectorType(VectorType *VTy) {
508 auto EltCnt = VTy->getElementCount();
509 assert(EltCnt.isKnownEven() &&
510 "Cannot halve vector with odd number of elements.");
511 return VectorType::get(VTy->getElementType(),
512 EltCnt.divideCoefficientBy(2));
513 }
514
515 /// This static method returns a VectorType with twice as many elements as the
516 /// input type and the same element type.
getDoubleElementsVectorType(VectorType * VTy)517 static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
518 auto EltCnt = VTy->getElementCount();
519 assert((EltCnt.getKnownMinValue() * 2ull) <= UINT_MAX &&
520 "Too many elements in vector");
521 return VectorType::get(VTy->getElementType(), EltCnt * 2);
522 }
523
524 /// Return true if the specified type is valid as a element type.
525 static bool isValidElementType(Type *ElemTy);
526
527 /// Return an ElementCount instance to represent the (possibly scalable)
528 /// number of elements in the vector.
529 inline ElementCount getElementCount() const;
530
531 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)532 static bool classof(const Type *T) {
533 return T->getTypeID() == FixedVectorTyID ||
534 T->getTypeID() == ScalableVectorTyID;
535 }
536 };
537
538 /// Class to represent fixed width SIMD vectors
539 class FixedVectorType : public VectorType {
540 protected:
FixedVectorType(Type * ElTy,unsigned NumElts)541 FixedVectorType(Type *ElTy, unsigned NumElts)
542 : VectorType(ElTy, NumElts, FixedVectorTyID) {}
543
544 public:
545 static FixedVectorType *get(Type *ElementType, unsigned NumElts);
546
get(Type * ElementType,const FixedVectorType * FVTy)547 static FixedVectorType *get(Type *ElementType, const FixedVectorType *FVTy) {
548 return get(ElementType, FVTy->getNumElements());
549 }
550
getInteger(FixedVectorType * VTy)551 static FixedVectorType *getInteger(FixedVectorType *VTy) {
552 return cast<FixedVectorType>(VectorType::getInteger(VTy));
553 }
554
getExtendedElementVectorType(FixedVectorType * VTy)555 static FixedVectorType *getExtendedElementVectorType(FixedVectorType *VTy) {
556 return cast<FixedVectorType>(VectorType::getExtendedElementVectorType(VTy));
557 }
558
getTruncatedElementVectorType(FixedVectorType * VTy)559 static FixedVectorType *getTruncatedElementVectorType(FixedVectorType *VTy) {
560 return cast<FixedVectorType>(
561 VectorType::getTruncatedElementVectorType(VTy));
562 }
563
getSubdividedVectorType(FixedVectorType * VTy,int NumSubdivs)564 static FixedVectorType *getSubdividedVectorType(FixedVectorType *VTy,
565 int NumSubdivs) {
566 return cast<FixedVectorType>(
567 VectorType::getSubdividedVectorType(VTy, NumSubdivs));
568 }
569
getHalfElementsVectorType(FixedVectorType * VTy)570 static FixedVectorType *getHalfElementsVectorType(FixedVectorType *VTy) {
571 return cast<FixedVectorType>(VectorType::getHalfElementsVectorType(VTy));
572 }
573
getDoubleElementsVectorType(FixedVectorType * VTy)574 static FixedVectorType *getDoubleElementsVectorType(FixedVectorType *VTy) {
575 return cast<FixedVectorType>(VectorType::getDoubleElementsVectorType(VTy));
576 }
577
classof(const Type * T)578 static bool classof(const Type *T) {
579 return T->getTypeID() == FixedVectorTyID;
580 }
581
getNumElements()582 unsigned getNumElements() const { return ElementQuantity; }
583 };
584
585 /// Class to represent scalable SIMD vectors
586 class ScalableVectorType : public VectorType {
587 protected:
ScalableVectorType(Type * ElTy,unsigned MinNumElts)588 ScalableVectorType(Type *ElTy, unsigned MinNumElts)
589 : VectorType(ElTy, MinNumElts, ScalableVectorTyID) {}
590
591 public:
592 static ScalableVectorType *get(Type *ElementType, unsigned MinNumElts);
593
get(Type * ElementType,const ScalableVectorType * SVTy)594 static ScalableVectorType *get(Type *ElementType,
595 const ScalableVectorType *SVTy) {
596 return get(ElementType, SVTy->getMinNumElements());
597 }
598
getInteger(ScalableVectorType * VTy)599 static ScalableVectorType *getInteger(ScalableVectorType *VTy) {
600 return cast<ScalableVectorType>(VectorType::getInteger(VTy));
601 }
602
603 static ScalableVectorType *
getExtendedElementVectorType(ScalableVectorType * VTy)604 getExtendedElementVectorType(ScalableVectorType *VTy) {
605 return cast<ScalableVectorType>(
606 VectorType::getExtendedElementVectorType(VTy));
607 }
608
609 static ScalableVectorType *
getTruncatedElementVectorType(ScalableVectorType * VTy)610 getTruncatedElementVectorType(ScalableVectorType *VTy) {
611 return cast<ScalableVectorType>(
612 VectorType::getTruncatedElementVectorType(VTy));
613 }
614
getSubdividedVectorType(ScalableVectorType * VTy,int NumSubdivs)615 static ScalableVectorType *getSubdividedVectorType(ScalableVectorType *VTy,
616 int NumSubdivs) {
617 return cast<ScalableVectorType>(
618 VectorType::getSubdividedVectorType(VTy, NumSubdivs));
619 }
620
621 static ScalableVectorType *
getHalfElementsVectorType(ScalableVectorType * VTy)622 getHalfElementsVectorType(ScalableVectorType *VTy) {
623 return cast<ScalableVectorType>(VectorType::getHalfElementsVectorType(VTy));
624 }
625
626 static ScalableVectorType *
getDoubleElementsVectorType(ScalableVectorType * VTy)627 getDoubleElementsVectorType(ScalableVectorType *VTy) {
628 return cast<ScalableVectorType>(
629 VectorType::getDoubleElementsVectorType(VTy));
630 }
631
632 /// Get the minimum number of elements in this vector. The actual number of
633 /// elements in the vector is an integer multiple of this value.
getMinNumElements()634 unsigned getMinNumElements() const { return ElementQuantity; }
635
classof(const Type * T)636 static bool classof(const Type *T) {
637 return T->getTypeID() == ScalableVectorTyID;
638 }
639 };
640
getElementCount()641 inline ElementCount VectorType::getElementCount() const {
642 return ElementCount::get(ElementQuantity, isa<ScalableVectorType>(this));
643 }
644
645 /// Class to represent pointers.
646 class PointerType : public Type {
647 explicit PointerType(LLVMContext &C, unsigned AddrSpace);
648
649 public:
650 PointerType(const PointerType &) = delete;
651 PointerType &operator=(const PointerType &) = delete;
652
653 /// This constructs a pointer to an object of the specified type in a numbered
654 /// address space.
655 static PointerType *get(Type *ElementType, unsigned AddressSpace);
656 /// This constructs an opaque pointer to an object in a numbered address
657 /// space.
658 static PointerType *get(LLVMContext &C, unsigned AddressSpace);
659
660 /// This constructs a pointer to an object of the specified type in the
661 /// default address space (address space zero).
getUnqual(Type * ElementType)662 static PointerType *getUnqual(Type *ElementType) {
663 return PointerType::get(ElementType, 0);
664 }
665
666 /// This constructs an opaque pointer to an object in the
667 /// default address space (address space zero).
getUnqual(LLVMContext & C)668 static PointerType *getUnqual(LLVMContext &C) {
669 return PointerType::get(C, 0);
670 }
671
672 /// Return true if the specified type is valid as a element type.
673 static bool isValidElementType(Type *ElemTy);
674
675 /// Return true if we can load or store from a pointer to this type.
676 static bool isLoadableOrStorableType(Type *ElemTy);
677
678 /// Return the address space of the Pointer type.
getAddressSpace()679 inline unsigned getAddressSpace() const { return getSubclassData(); }
680
681 /// Implement support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)682 static bool classof(const Type *T) {
683 return T->getTypeID() == PointerTyID;
684 }
685 };
686
getExtendedType()687 Type *Type::getExtendedType() const {
688 assert(
689 isIntOrIntVectorTy() &&
690 "Original type expected to be a vector of integers or a scalar integer.");
691 if (auto *VTy = dyn_cast<VectorType>(this))
692 return VectorType::getExtendedElementVectorType(
693 const_cast<VectorType *>(VTy));
694 return cast<IntegerType>(this)->getExtendedType();
695 }
696
getWithNewType(Type * EltTy)697 Type *Type::getWithNewType(Type *EltTy) const {
698 if (auto *VTy = dyn_cast<VectorType>(this))
699 return VectorType::get(EltTy, VTy->getElementCount());
700 return EltTy;
701 }
702
getWithNewBitWidth(unsigned NewBitWidth)703 Type *Type::getWithNewBitWidth(unsigned NewBitWidth) const {
704 assert(
705 isIntOrIntVectorTy() &&
706 "Original type expected to be a vector of integers or a scalar integer.");
707 return getWithNewType(getIntNTy(getContext(), NewBitWidth));
708 }
709
getPointerAddressSpace()710 unsigned Type::getPointerAddressSpace() const {
711 return cast<PointerType>(getScalarType())->getAddressSpace();
712 }
713
714 /// Class to represent target extensions types, which are generally
715 /// unintrospectable from target-independent optimizations.
716 ///
717 /// Target extension types have a string name, and optionally have type and/or
718 /// integer parameters. The exact meaning of any parameters is dependent on the
719 /// target.
720 class TargetExtType : public Type {
721 TargetExtType(LLVMContext &C, StringRef Name, ArrayRef<Type *> Types,
722 ArrayRef<unsigned> Ints);
723
724 // These strings are ultimately owned by the context.
725 StringRef Name;
726 unsigned *IntParams;
727
728 public:
729 TargetExtType(const TargetExtType &) = delete;
730 TargetExtType &operator=(const TargetExtType &) = delete;
731
732 /// Return a target extension type having the specified name and optional
733 /// type and integer parameters.
734 static TargetExtType *get(LLVMContext &Context, StringRef Name,
735 ArrayRef<Type *> Types = std::nullopt,
736 ArrayRef<unsigned> Ints = std::nullopt);
737
738 /// Return the name for this target extension type. Two distinct target
739 /// extension types may have the same name if their type or integer parameters
740 /// differ.
getName()741 StringRef getName() const { return Name; }
742
743 /// Return the type parameters for this particular target extension type. If
744 /// there are no parameters, an empty array is returned.
type_params()745 ArrayRef<Type *> type_params() const {
746 return ArrayRef(type_param_begin(), type_param_end());
747 }
748
749 using type_param_iterator = Type::subtype_iterator;
type_param_begin()750 type_param_iterator type_param_begin() const { return ContainedTys; }
type_param_end()751 type_param_iterator type_param_end() const {
752 return &ContainedTys[NumContainedTys];
753 }
754
getTypeParameter(unsigned i)755 Type *getTypeParameter(unsigned i) const { return getContainedType(i); }
getNumTypeParameters()756 unsigned getNumTypeParameters() const { return getNumContainedTypes(); }
757
758 /// Return the integer parameters for this particular target extension type.
759 /// If there are no parameters, an empty array is returned.
int_params()760 ArrayRef<unsigned> int_params() const {
761 return ArrayRef(IntParams, getNumIntParameters());
762 }
763
getIntParameter(unsigned i)764 unsigned getIntParameter(unsigned i) const { return IntParams[i]; }
getNumIntParameters()765 unsigned getNumIntParameters() const { return getSubclassData(); }
766
767 enum Property {
768 /// zeroinitializer is valid for this target extension type.
769 HasZeroInit = 1U << 0,
770 /// This type may be used as the value type of a global variable.
771 CanBeGlobal = 1U << 1,
772 };
773
774 /// Returns true if the target extension type contains the given property.
775 bool hasProperty(Property Prop) const;
776
777 /// Returns an underlying layout type for the target extension type. This
778 /// type can be used to query size and alignment information, if it is
779 /// appropriate (although note that the layout type may also be void). It is
780 /// not legal to bitcast between this type and the layout type, however.
781 Type *getLayoutType() const;
782
783 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)784 static bool classof(const Type *T) { return T->getTypeID() == TargetExtTyID; }
785 };
786
getTargetExtName()787 StringRef Type::getTargetExtName() const {
788 return cast<TargetExtType>(this)->getName();
789 }
790
791 } // end namespace llvm
792
793 #endif // LLVM_IR_DERIVEDTYPES_H
794