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