Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:59

0001 //===- llvm/DerivedTypes.h - Classes for handling data types ----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains the declarations of classes that represent "derived
0010 // types".  These are things like "arrays of x" or "structure of x, y, z" or
0011 // "function returning x taking (y,z) as parameters", etc...
0012 //
0013 // The implementations of these classes live in the Type.cpp file.
0014 //
0015 //===----------------------------------------------------------------------===//
0016 
0017 #ifndef LLVM_IR_DERIVEDTYPES_H
0018 #define LLVM_IR_DERIVEDTYPES_H
0019 
0020 #include "llvm/ADT/ArrayRef.h"
0021 #include "llvm/ADT/STLExtras.h"
0022 #include "llvm/ADT/StringRef.h"
0023 #include "llvm/IR/Type.h"
0024 #include "llvm/Support/Casting.h"
0025 #include "llvm/Support/Compiler.h"
0026 #include "llvm/Support/TypeSize.h"
0027 #include <cassert>
0028 #include <cstdint>
0029 
0030 namespace llvm {
0031 
0032 class Value;
0033 class APInt;
0034 class LLVMContext;
0035 template <typename T> class Expected;
0036 class Error;
0037 
0038 /// Class to represent integer types. Note that this class is also used to
0039 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
0040 /// Int64Ty.
0041 /// Integer representation type
0042 class IntegerType : public Type {
0043   friend class LLVMContextImpl;
0044 
0045 protected:
0046   explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){
0047     setSubclassData(NumBits);
0048   }
0049 
0050 public:
0051   /// This enum is just used to hold constants we need for IntegerType.
0052   enum {
0053     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
0054     MAX_INT_BITS = (1<<23)   ///< Maximum number of bits that can be specified
0055       ///< Note that bit width is stored in the Type classes SubclassData field
0056       ///< which has 24 bits. SelectionDAG type legalization can require a
0057       ///< power of 2 IntegerType, so limit to the largest representable power
0058       ///< of 2, 8388608.
0059   };
0060 
0061   /// This static method is the primary way of constructing an IntegerType.
0062   /// If an IntegerType with the same NumBits value was previously instantiated,
0063   /// that instance will be returned. Otherwise a new one will be created. Only
0064   /// one instance with a given NumBits value is ever created.
0065   /// Get or create an IntegerType instance.
0066   static IntegerType *get(LLVMContext &C, unsigned NumBits);
0067 
0068   /// Returns type twice as wide the input type.
0069   IntegerType *getExtendedType() const {
0070     return Type::getIntNTy(getContext(), 2 * getScalarSizeInBits());
0071   }
0072 
0073   /// Get the number of bits in this IntegerType
0074   unsigned getBitWidth() const { return getSubclassData(); }
0075 
0076   /// Return a bitmask with ones set for all of the bits that can be set by an
0077   /// unsigned version of this type. This is 0xFF for i8, 0xFFFF for i16, etc.
0078   uint64_t getBitMask() const {
0079     return ~uint64_t(0UL) >> (64-getBitWidth());
0080   }
0081 
0082   /// Return a uint64_t with just the most significant bit set (the sign bit, if
0083   /// the value is treated as a signed number).
0084   uint64_t getSignBit() const {
0085     return 1ULL << (getBitWidth()-1);
0086   }
0087 
0088   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
0089   /// @returns a bit mask with ones set for all the bits of this type.
0090   /// Get a bit mask for this type.
0091   APInt getMask() const;
0092 
0093   /// Methods for support type inquiry through isa, cast, and dyn_cast.
0094   static bool classof(const Type *T) {
0095     return T->getTypeID() == IntegerTyID;
0096   }
0097 };
0098 
0099 unsigned Type::getIntegerBitWidth() const {
0100   return cast<IntegerType>(this)->getBitWidth();
0101 }
0102 
0103 /// Class to represent function types
0104 ///
0105 class FunctionType : public Type {
0106   FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
0107 
0108 public:
0109   FunctionType(const FunctionType &) = delete;
0110   FunctionType &operator=(const FunctionType &) = delete;
0111 
0112   /// This static method is the primary way of constructing a FunctionType.
0113   static FunctionType *get(Type *Result,
0114                            ArrayRef<Type*> Params, bool isVarArg);
0115 
0116   /// Create a FunctionType taking no parameters.
0117   static FunctionType *get(Type *Result, bool isVarArg);
0118 
0119   /// Return true if the specified type is valid as a return type.
0120   static bool isValidReturnType(Type *RetTy);
0121 
0122   /// Return true if the specified type is valid as an argument type.
0123   static bool isValidArgumentType(Type *ArgTy);
0124 
0125   bool isVarArg() const { return getSubclassData()!=0; }
0126   Type *getReturnType() const { return ContainedTys[0]; }
0127 
0128   using param_iterator = Type::subtype_iterator;
0129 
0130   param_iterator param_begin() const { return ContainedTys + 1; }
0131   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
0132   ArrayRef<Type *> params() const {
0133     return ArrayRef(param_begin(), param_end());
0134   }
0135 
0136   /// Parameter type accessors.
0137   Type *getParamType(unsigned i) const {
0138     assert(i < getNumParams() && "getParamType() out of range!");
0139     return ContainedTys[i + 1];
0140   }
0141 
0142   /// Return the number of fixed parameters this function type requires.
0143   /// This does not consider varargs.
0144   unsigned getNumParams() const { return NumContainedTys - 1; }
0145 
0146   /// Methods for support type inquiry through isa, cast, and dyn_cast.
0147   static bool classof(const Type *T) {
0148     return T->getTypeID() == FunctionTyID;
0149   }
0150 };
0151 static_assert(alignof(FunctionType) >= alignof(Type *),
0152               "Alignment sufficient for objects appended to FunctionType");
0153 
0154 bool Type::isFunctionVarArg() const {
0155   return cast<FunctionType>(this)->isVarArg();
0156 }
0157 
0158 Type *Type::getFunctionParamType(unsigned i) const {
0159   return cast<FunctionType>(this)->getParamType(i);
0160 }
0161 
0162 unsigned Type::getFunctionNumParams() const {
0163   return cast<FunctionType>(this)->getNumParams();
0164 }
0165 
0166 /// A handy container for a FunctionType+Callee-pointer pair, which can be
0167 /// passed around as a single entity. This assists in replacing the use of
0168 /// PointerType::getElementType() to access the function's type, since that's
0169 /// slated for removal as part of the [opaque pointer types] project.
0170 class FunctionCallee {
0171 public:
0172   // Allow implicit conversion from types which have a getFunctionType member
0173   // (e.g. Function and InlineAsm).
0174   template <typename T, typename U = decltype(&T::getFunctionType)>
0175   FunctionCallee(T *Fn)
0176       : FnTy(Fn ? Fn->getFunctionType() : nullptr), Callee(Fn) {}
0177 
0178   FunctionCallee(FunctionType *FnTy, Value *Callee)
0179       : FnTy(FnTy), Callee(Callee) {
0180     assert((FnTy == nullptr) == (Callee == nullptr));
0181   }
0182 
0183   FunctionCallee(std::nullptr_t) {}
0184 
0185   FunctionCallee() = default;
0186 
0187   FunctionType *getFunctionType() { return FnTy; }
0188 
0189   Value *getCallee() { return Callee; }
0190 
0191   explicit operator bool() { return Callee; }
0192 
0193 private:
0194   FunctionType *FnTy = nullptr;
0195   Value *Callee = nullptr;
0196 };
0197 
0198 /// Class to represent struct types. There are two different kinds of struct
0199 /// types: Literal structs and Identified structs.
0200 ///
0201 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
0202 /// always have a body when created.  You can get one of these by using one of
0203 /// the StructType::get() forms.
0204 ///
0205 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
0206 /// uniqued.  The names for identified structs are managed at the LLVMContext
0207 /// level, so there can only be a single identified struct with a given name in
0208 /// a particular LLVMContext.  Identified structs may also optionally be opaque
0209 /// (have no body specified).  You get one of these by using one of the
0210 /// StructType::create() forms.
0211 ///
0212 /// Independent of what kind of struct you have, the body of a struct type are
0213 /// laid out in memory consecutively with the elements directly one after the
0214 /// other (if the struct is packed) or (if not packed) with padding between the
0215 /// elements as defined by DataLayout (which is required to match what the code
0216 /// generator for a target expects).
0217 ///
0218 class StructType : public Type {
0219   StructType(LLVMContext &C) : Type(C, StructTyID) {}
0220 
0221   enum {
0222     /// This is the contents of the SubClassData field.
0223     SCDB_HasBody = 1,
0224     SCDB_Packed = 2,
0225     SCDB_IsLiteral = 4,
0226     SCDB_IsSized = 8,
0227     SCDB_ContainsScalableVector = 16,
0228     SCDB_NotContainsScalableVector = 32,
0229     SCDB_ContainsNonGlobalTargetExtType = 64,
0230     SCDB_NotContainsNonGlobalTargetExtType = 128,
0231     SCDB_ContainsNonLocalTargetExtType = 64,
0232     SCDB_NotContainsNonLocalTargetExtType = 128,
0233   };
0234 
0235   /// For a named struct that actually has a name, this is a pointer to the
0236   /// symbol table entry (maintained by LLVMContext) for the struct.
0237   /// This is null if the type is an literal struct or if it is a identified
0238   /// type that has an empty name.
0239   void *SymbolTableEntry = nullptr;
0240 
0241 public:
0242   StructType(const StructType &) = delete;
0243   StructType &operator=(const StructType &) = delete;
0244 
0245   /// This creates an identified struct.
0246   static StructType *create(LLVMContext &Context, StringRef Name);
0247   static StructType *create(LLVMContext &Context);
0248 
0249   static StructType *create(ArrayRef<Type *> Elements, StringRef Name,
0250                             bool isPacked = false);
0251   static StructType *create(ArrayRef<Type *> Elements);
0252   static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements,
0253                             StringRef Name, bool isPacked = false);
0254   static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements);
0255   template <class... Tys>
0256   static std::enable_if_t<are_base_of<Type, Tys...>::value, StructType *>
0257   create(StringRef Name, Type *elt1, Tys *... elts) {
0258     assert(elt1 && "Cannot create a struct type with no elements with this");
0259     return create(ArrayRef<Type *>({elt1, elts...}), Name);
0260   }
0261 
0262   /// This static method is the primary way to create a literal StructType.
0263   static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
0264                          bool isPacked = false);
0265 
0266   /// Create an empty structure type.
0267   static StructType *get(LLVMContext &Context, bool isPacked = false);
0268 
0269   /// This static method is a convenience method for creating structure types by
0270   /// specifying the elements as arguments. Note that this method always returns
0271   /// a non-packed struct, and requires at least one element type.
0272   template <class... Tys>
0273   static std::enable_if_t<are_base_of<Type, Tys...>::value, StructType *>
0274   get(Type *elt1, Tys *... elts) {
0275     assert(elt1 && "Cannot create a struct type with no elements with this");
0276     LLVMContext &Ctx = elt1->getContext();
0277     return StructType::get(Ctx, ArrayRef<Type *>({elt1, elts...}));
0278   }
0279 
0280   /// Return the type with the specified name, or null if there is none by that
0281   /// name.
0282   static StructType *getTypeByName(LLVMContext &C, StringRef Name);
0283 
0284   bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
0285 
0286   /// Return true if this type is uniqued by structural equivalence, false if it
0287   /// is a struct definition.
0288   bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
0289 
0290   /// Return true if this is a type with an identity that has no body specified
0291   /// yet. These prints as 'opaque' in .ll files.
0292   bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
0293 
0294   /// isSized - Return true if this is a sized type.
0295   bool isSized(SmallPtrSetImpl<Type *> *Visited = nullptr) const;
0296 
0297   /// Returns true if this struct contains a scalable vector.
0298   bool isScalableTy(SmallPtrSetImpl<const Type *> &Visited) const;
0299   using Type::isScalableTy;
0300 
0301   /// Return true if this type is or contains a target extension type that
0302   /// disallows being used as a global.
0303   bool
0304   containsNonGlobalTargetExtType(SmallPtrSetImpl<const Type *> &Visited) const;
0305   using Type::containsNonGlobalTargetExtType;
0306 
0307   /// Return true if this type is or contains a target extension type that
0308   /// disallows being used as a local.
0309   bool
0310   containsNonLocalTargetExtType(SmallPtrSetImpl<const Type *> &Visited) const;
0311   using Type::containsNonLocalTargetExtType;
0312 
0313   /// Returns true if this struct contains homogeneous scalable vector types.
0314   /// Note that the definition of homogeneous scalable vector type is not
0315   /// recursive here. That means the following structure will return false
0316   /// when calling this function.
0317   /// {{<vscale x 2 x i32>, <vscale x 4 x i64>},
0318   ///  {<vscale x 2 x i32>, <vscale x 4 x i64>}}
0319   bool containsHomogeneousScalableVectorTypes() const;
0320 
0321   /// Return true if this struct is non-empty and all element types are the
0322   /// same.
0323   bool containsHomogeneousTypes() const;
0324 
0325   /// Return true if this is a named struct that has a non-empty name.
0326   bool hasName() const { return SymbolTableEntry != nullptr; }
0327 
0328   /// Return the name for this struct type if it has an identity.
0329   /// This may return an empty string for an unnamed struct type.  Do not call
0330   /// this on an literal type.
0331   StringRef getName() const;
0332 
0333   /// Change the name of this type to the specified name, or to a name with a
0334   /// suffix if there is a collision. Do not call this on an literal type.
0335   void setName(StringRef Name);
0336 
0337   /// Specify a body for an opaque identified type, which must not make the type
0338   /// recursive.
0339   void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
0340 
0341   /// Specify a body for an opaque identified type or return an error if it
0342   /// would make the type recursive.
0343   Error setBodyOrError(ArrayRef<Type *> Elements, bool isPacked = false);
0344 
0345   /// Return an error if the body for an opaque identified type would make it
0346   /// recursive.
0347   Error checkBody(ArrayRef<Type *> Elements);
0348 
0349   /// Return true if the specified type is valid as a element type.
0350   static bool isValidElementType(Type *ElemTy);
0351 
0352   // Iterator access to the elements.
0353   using element_iterator = Type::subtype_iterator;
0354 
0355   element_iterator element_begin() const { return ContainedTys; }
0356   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
0357   ArrayRef<Type *> elements() const {
0358     return ArrayRef(element_begin(), element_end());
0359   }
0360 
0361   /// Return true if this is layout identical to the specified struct.
0362   bool isLayoutIdentical(StructType *Other) const;
0363 
0364   /// Random access to the elements
0365   unsigned getNumElements() const { return NumContainedTys; }
0366   Type *getElementType(unsigned N) const {
0367     assert(N < NumContainedTys && "Element number out of range!");
0368     return ContainedTys[N];
0369   }
0370   /// Given an index value into the type, return the type of the element.
0371   Type *getTypeAtIndex(const Value *V) const;
0372   Type *getTypeAtIndex(unsigned N) const { return getElementType(N); }
0373   bool indexValid(const Value *V) const;
0374   bool indexValid(unsigned Idx) const { return Idx < getNumElements(); }
0375 
0376   /// Methods for support type inquiry through isa, cast, and dyn_cast.
0377   static bool classof(const Type *T) {
0378     return T->getTypeID() == StructTyID;
0379   }
0380 };
0381 
0382 StringRef Type::getStructName() const {
0383   return cast<StructType>(this)->getName();
0384 }
0385 
0386 unsigned Type::getStructNumElements() const {
0387   return cast<StructType>(this)->getNumElements();
0388 }
0389 
0390 Type *Type::getStructElementType(unsigned N) const {
0391   return cast<StructType>(this)->getElementType(N);
0392 }
0393 
0394 /// Class to represent array types.
0395 class ArrayType : public Type {
0396   /// The element type of the array.
0397   Type *ContainedType;
0398   /// Number of elements in the array.
0399   uint64_t NumElements;
0400 
0401   ArrayType(Type *ElType, uint64_t NumEl);
0402 
0403 public:
0404   ArrayType(const ArrayType &) = delete;
0405   ArrayType &operator=(const ArrayType &) = delete;
0406 
0407   uint64_t getNumElements() const { return NumElements; }
0408   Type *getElementType() const { return ContainedType; }
0409 
0410   /// This static method is the primary way to construct an ArrayType
0411   static ArrayType *get(Type *ElementType, uint64_t NumElements);
0412 
0413   /// Return true if the specified type is valid as a element type.
0414   static bool isValidElementType(Type *ElemTy);
0415 
0416   /// Methods for support type inquiry through isa, cast, and dyn_cast.
0417   static bool classof(const Type *T) {
0418     return T->getTypeID() == ArrayTyID;
0419   }
0420 };
0421 
0422 uint64_t Type::getArrayNumElements() const {
0423   return cast<ArrayType>(this)->getNumElements();
0424 }
0425 
0426 /// Base class of all SIMD vector types
0427 class VectorType : public Type {
0428   /// A fully specified VectorType is of the form <vscale x n x Ty>. 'n' is the
0429   /// minimum number of elements of type Ty contained within the vector, and
0430   /// 'vscale x' indicates that the total element count is an integer multiple
0431   /// of 'n', where the multiple is either guaranteed to be one, or is
0432   /// statically unknown at compile time.
0433   ///
0434   /// If the multiple is known to be 1, then the extra term is discarded in
0435   /// textual IR:
0436   ///
0437   /// <4 x i32>          - a vector containing 4 i32s
0438   /// <vscale x 4 x i32> - a vector containing an unknown integer multiple
0439   ///                      of 4 i32s
0440 
0441   /// The element type of the vector.
0442   Type *ContainedType;
0443 
0444 protected:
0445   /// The element quantity of this vector. The meaning of this value depends
0446   /// on the type of vector:
0447   /// - For FixedVectorType = <ElementQuantity x ty>, there are
0448   ///   exactly ElementQuantity elements in this vector.
0449   /// - For ScalableVectorType = <vscale x ElementQuantity x ty>,
0450   ///   there are vscale * ElementQuantity elements in this vector, where
0451   ///   vscale is a runtime-constant integer greater than 0.
0452   const unsigned ElementQuantity;
0453 
0454   VectorType(Type *ElType, unsigned EQ, Type::TypeID TID);
0455 
0456 public:
0457   VectorType(const VectorType &) = delete;
0458   VectorType &operator=(const VectorType &) = delete;
0459 
0460   Type *getElementType() const { return ContainedType; }
0461 
0462   /// This static method is the primary way to construct an VectorType.
0463   static VectorType *get(Type *ElementType, ElementCount EC);
0464 
0465   static VectorType *get(Type *ElementType, unsigned NumElements,
0466                          bool Scalable) {
0467     return VectorType::get(ElementType,
0468                            ElementCount::get(NumElements, Scalable));
0469   }
0470 
0471   static VectorType *get(Type *ElementType, const VectorType *Other) {
0472     return VectorType::get(ElementType, Other->getElementCount());
0473   }
0474 
0475   /// This static method gets a VectorType with the same number of elements as
0476   /// the input type, and the element type is an integer type of the same width
0477   /// as the input element type.
0478   static VectorType *getInteger(VectorType *VTy) {
0479     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
0480     assert(EltBits && "Element size must be of a non-zero size");
0481     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
0482     return VectorType::get(EltTy, VTy->getElementCount());
0483   }
0484 
0485   /// This static method is like getInteger except that the element types are
0486   /// twice as wide as the elements in the input type.
0487   static VectorType *getExtendedElementVectorType(VectorType *VTy) {
0488     assert(VTy->isIntOrIntVectorTy() && "VTy expected to be a vector of ints.");
0489     auto *EltTy = cast<IntegerType>(VTy->getElementType());
0490     return VectorType::get(EltTy->getExtendedType(), VTy->getElementCount());
0491   }
0492 
0493   // This static method gets a VectorType with the same number of elements as
0494   // the input type, and the element type is an integer or float type which
0495   // is half as wide as the elements in the input type.
0496   static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
0497     Type *EltTy;
0498     if (VTy->getElementType()->isFloatingPointTy()) {
0499       switch(VTy->getElementType()->getTypeID()) {
0500       case DoubleTyID:
0501         EltTy = Type::getFloatTy(VTy->getContext());
0502         break;
0503       case FloatTyID:
0504         EltTy = Type::getHalfTy(VTy->getContext());
0505         break;
0506       default:
0507         llvm_unreachable("Cannot create narrower fp vector element type");
0508       }
0509     } else {
0510       unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
0511       assert((EltBits & 1) == 0 &&
0512              "Cannot truncate vector element with odd bit-width");
0513       EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
0514     }
0515     return VectorType::get(EltTy, VTy->getElementCount());
0516   }
0517 
0518   // This static method returns a VectorType with a larger number of elements
0519   // of a smaller type than the input element type. For example, a <4 x i64>
0520   // subdivided twice would return <16 x i16>
0521   static VectorType *getSubdividedVectorType(VectorType *VTy, int NumSubdivs) {
0522     for (int i = 0; i < NumSubdivs; ++i) {
0523       VTy = VectorType::getDoubleElementsVectorType(VTy);
0524       VTy = VectorType::getTruncatedElementVectorType(VTy);
0525     }
0526     return VTy;
0527   }
0528 
0529   /// This static method returns a VectorType with half as many elements as the
0530   /// input type and the same element type.
0531   static VectorType *getHalfElementsVectorType(VectorType *VTy) {
0532     auto EltCnt = VTy->getElementCount();
0533     assert(EltCnt.isKnownEven() &&
0534            "Cannot halve vector with odd number of elements.");
0535     return VectorType::get(VTy->getElementType(),
0536                            EltCnt.divideCoefficientBy(2));
0537   }
0538 
0539   /// This static method returns a VectorType with twice as many elements as the
0540   /// input type and the same element type.
0541   static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
0542     auto EltCnt = VTy->getElementCount();
0543     assert((EltCnt.getKnownMinValue() * 2ull) <= UINT_MAX &&
0544            "Too many elements in vector");
0545     return VectorType::get(VTy->getElementType(), EltCnt * 2);
0546   }
0547 
0548   /// Return true if the specified type is valid as a element type.
0549   static bool isValidElementType(Type *ElemTy);
0550 
0551   /// Return an ElementCount instance to represent the (possibly scalable)
0552   /// number of elements in the vector.
0553   inline ElementCount getElementCount() const;
0554 
0555   /// Methods for support type inquiry through isa, cast, and dyn_cast.
0556   static bool classof(const Type *T) {
0557     return T->getTypeID() == FixedVectorTyID ||
0558            T->getTypeID() == ScalableVectorTyID;
0559   }
0560 };
0561 
0562 /// Class to represent fixed width SIMD vectors
0563 class FixedVectorType : public VectorType {
0564 protected:
0565   FixedVectorType(Type *ElTy, unsigned NumElts)
0566       : VectorType(ElTy, NumElts, FixedVectorTyID) {}
0567 
0568 public:
0569   static FixedVectorType *get(Type *ElementType, unsigned NumElts);
0570 
0571   static FixedVectorType *get(Type *ElementType, const FixedVectorType *FVTy) {
0572     return get(ElementType, FVTy->getNumElements());
0573   }
0574 
0575   static FixedVectorType *getInteger(FixedVectorType *VTy) {
0576     return cast<FixedVectorType>(VectorType::getInteger(VTy));
0577   }
0578 
0579   static FixedVectorType *getExtendedElementVectorType(FixedVectorType *VTy) {
0580     return cast<FixedVectorType>(VectorType::getExtendedElementVectorType(VTy));
0581   }
0582 
0583   static FixedVectorType *getTruncatedElementVectorType(FixedVectorType *VTy) {
0584     return cast<FixedVectorType>(
0585         VectorType::getTruncatedElementVectorType(VTy));
0586   }
0587 
0588   static FixedVectorType *getSubdividedVectorType(FixedVectorType *VTy,
0589                                                   int NumSubdivs) {
0590     return cast<FixedVectorType>(
0591         VectorType::getSubdividedVectorType(VTy, NumSubdivs));
0592   }
0593 
0594   static FixedVectorType *getHalfElementsVectorType(FixedVectorType *VTy) {
0595     return cast<FixedVectorType>(VectorType::getHalfElementsVectorType(VTy));
0596   }
0597 
0598   static FixedVectorType *getDoubleElementsVectorType(FixedVectorType *VTy) {
0599     return cast<FixedVectorType>(VectorType::getDoubleElementsVectorType(VTy));
0600   }
0601 
0602   static bool classof(const Type *T) {
0603     return T->getTypeID() == FixedVectorTyID;
0604   }
0605 
0606   unsigned getNumElements() const { return ElementQuantity; }
0607 };
0608 
0609 /// Class to represent scalable SIMD vectors
0610 class ScalableVectorType : public VectorType {
0611 protected:
0612   ScalableVectorType(Type *ElTy, unsigned MinNumElts)
0613       : VectorType(ElTy, MinNumElts, ScalableVectorTyID) {}
0614 
0615 public:
0616   static ScalableVectorType *get(Type *ElementType, unsigned MinNumElts);
0617 
0618   static ScalableVectorType *get(Type *ElementType,
0619                                  const ScalableVectorType *SVTy) {
0620     return get(ElementType, SVTy->getMinNumElements());
0621   }
0622 
0623   static ScalableVectorType *getInteger(ScalableVectorType *VTy) {
0624     return cast<ScalableVectorType>(VectorType::getInteger(VTy));
0625   }
0626 
0627   static ScalableVectorType *
0628   getExtendedElementVectorType(ScalableVectorType *VTy) {
0629     return cast<ScalableVectorType>(
0630         VectorType::getExtendedElementVectorType(VTy));
0631   }
0632 
0633   static ScalableVectorType *
0634   getTruncatedElementVectorType(ScalableVectorType *VTy) {
0635     return cast<ScalableVectorType>(
0636         VectorType::getTruncatedElementVectorType(VTy));
0637   }
0638 
0639   static ScalableVectorType *getSubdividedVectorType(ScalableVectorType *VTy,
0640                                                      int NumSubdivs) {
0641     return cast<ScalableVectorType>(
0642         VectorType::getSubdividedVectorType(VTy, NumSubdivs));
0643   }
0644 
0645   static ScalableVectorType *
0646   getHalfElementsVectorType(ScalableVectorType *VTy) {
0647     return cast<ScalableVectorType>(VectorType::getHalfElementsVectorType(VTy));
0648   }
0649 
0650   static ScalableVectorType *
0651   getDoubleElementsVectorType(ScalableVectorType *VTy) {
0652     return cast<ScalableVectorType>(
0653         VectorType::getDoubleElementsVectorType(VTy));
0654   }
0655 
0656   /// Get the minimum number of elements in this vector. The actual number of
0657   /// elements in the vector is an integer multiple of this value.
0658   unsigned getMinNumElements() const { return ElementQuantity; }
0659 
0660   static bool classof(const Type *T) {
0661     return T->getTypeID() == ScalableVectorTyID;
0662   }
0663 };
0664 
0665 inline ElementCount VectorType::getElementCount() const {
0666   return ElementCount::get(ElementQuantity, isa<ScalableVectorType>(this));
0667 }
0668 
0669 /// Class to represent pointers.
0670 class PointerType : public Type {
0671   explicit PointerType(LLVMContext &C, unsigned AddrSpace);
0672 
0673 public:
0674   PointerType(const PointerType &) = delete;
0675   PointerType &operator=(const PointerType &) = delete;
0676 
0677   /// This constructs a pointer to an object of the specified type in a numbered
0678   /// address space.
0679   static PointerType *get(Type *ElementType, unsigned AddressSpace);
0680   /// This constructs an opaque pointer to an object in a numbered address
0681   /// space.
0682   static PointerType *get(LLVMContext &C, unsigned AddressSpace);
0683 
0684   /// This constructs a pointer to an object of the specified type in the
0685   /// default address space (address space zero).
0686   static PointerType *getUnqual(Type *ElementType) {
0687     return PointerType::get(ElementType, 0);
0688   }
0689 
0690   /// This constructs an opaque pointer to an object in the
0691   /// default address space (address space zero).
0692   static PointerType *getUnqual(LLVMContext &C) {
0693     return PointerType::get(C, 0);
0694   }
0695 
0696   /// Return true if the specified type is valid as a element type.
0697   static bool isValidElementType(Type *ElemTy);
0698 
0699   /// Return true if we can load or store from a pointer to this type.
0700   static bool isLoadableOrStorableType(Type *ElemTy);
0701 
0702   /// Return the address space of the Pointer type.
0703   inline unsigned getAddressSpace() const { return getSubclassData(); }
0704 
0705   /// Implement support type inquiry through isa, cast, and dyn_cast.
0706   static bool classof(const Type *T) {
0707     return T->getTypeID() == PointerTyID;
0708   }
0709 };
0710 
0711 Type *Type::getExtendedType() const {
0712   assert(
0713       isIntOrIntVectorTy() &&
0714       "Original type expected to be a vector of integers or a scalar integer.");
0715   if (auto *VTy = dyn_cast<VectorType>(this))
0716     return VectorType::getExtendedElementVectorType(
0717         const_cast<VectorType *>(VTy));
0718   return cast<IntegerType>(this)->getExtendedType();
0719 }
0720 
0721 Type *Type::getWithNewType(Type *EltTy) const {
0722   if (auto *VTy = dyn_cast<VectorType>(this))
0723     return VectorType::get(EltTy, VTy->getElementCount());
0724   return EltTy;
0725 }
0726 
0727 Type *Type::getWithNewBitWidth(unsigned NewBitWidth) const {
0728   assert(
0729       isIntOrIntVectorTy() &&
0730       "Original type expected to be a vector of integers or a scalar integer.");
0731   return getWithNewType(getIntNTy(getContext(), NewBitWidth));
0732 }
0733 
0734 unsigned Type::getPointerAddressSpace() const {
0735   return cast<PointerType>(getScalarType())->getAddressSpace();
0736 }
0737 
0738 /// Class to represent target extensions types, which are generally
0739 /// unintrospectable from target-independent optimizations.
0740 ///
0741 /// Target extension types have a string name, and optionally have type and/or
0742 /// integer parameters. The exact meaning of any parameters is dependent on the
0743 /// target.
0744 class TargetExtType : public Type {
0745   TargetExtType(LLVMContext &C, StringRef Name, ArrayRef<Type *> Types,
0746                 ArrayRef<unsigned> Ints);
0747 
0748   // These strings are ultimately owned by the context.
0749   StringRef Name;
0750   unsigned *IntParams;
0751 
0752 public:
0753   TargetExtType(const TargetExtType &) = delete;
0754   TargetExtType &operator=(const TargetExtType &) = delete;
0755 
0756   /// Return a target extension type having the specified name and optional
0757   /// type and integer parameters.
0758   static TargetExtType *get(LLVMContext &Context, StringRef Name,
0759                             ArrayRef<Type *> Types = {},
0760                             ArrayRef<unsigned> Ints = {});
0761 
0762   /// Return a target extension type having the specified name and optional
0763   /// type and integer parameters, or an appropriate Error if it fails the
0764   /// parameters check.
0765   static Expected<TargetExtType *> getOrError(LLVMContext &Context,
0766                                               StringRef Name,
0767                                               ArrayRef<Type *> Types = {},
0768                                               ArrayRef<unsigned> Ints = {});
0769 
0770   /// Check that a newly created target extension type has the expected number
0771   /// of type parameters and integer parameters, returning the type itself if OK
0772   /// or an appropriate Error if not.
0773   static Expected<TargetExtType *> checkParams(TargetExtType *TTy);
0774 
0775   /// Return the name for this target extension type. Two distinct target
0776   /// extension types may have the same name if their type or integer parameters
0777   /// differ.
0778   StringRef getName() const { return Name; }
0779 
0780   /// Return the type parameters for this particular target extension type. If
0781   /// there are no parameters, an empty array is returned.
0782   ArrayRef<Type *> type_params() const {
0783     return ArrayRef(type_param_begin(), type_param_end());
0784   }
0785 
0786   using type_param_iterator = Type::subtype_iterator;
0787   type_param_iterator type_param_begin() const { return ContainedTys; }
0788   type_param_iterator type_param_end() const {
0789     return &ContainedTys[NumContainedTys];
0790   }
0791 
0792   Type *getTypeParameter(unsigned i) const { return getContainedType(i); }
0793   unsigned getNumTypeParameters() const { return getNumContainedTypes(); }
0794 
0795   /// Return the integer parameters for this particular target extension type.
0796   /// If there are no parameters, an empty array is returned.
0797   ArrayRef<unsigned> int_params() const {
0798     return ArrayRef(IntParams, getNumIntParameters());
0799   }
0800 
0801   unsigned getIntParameter(unsigned i) const { return IntParams[i]; }
0802   unsigned getNumIntParameters() const { return getSubclassData(); }
0803 
0804   enum Property {
0805     /// zeroinitializer is valid for this target extension type.
0806     HasZeroInit = 1U << 0,
0807     /// This type may be used as the value type of a global variable.
0808     CanBeGlobal = 1U << 1,
0809     /// This type may be allocated on the stack, either as the allocated type
0810     /// of an alloca instruction or as a byval function parameter.
0811     CanBeLocal = 1U << 2,
0812   };
0813 
0814   /// Returns true if the target extension type contains the given property.
0815   bool hasProperty(Property Prop) const;
0816 
0817   /// Returns an underlying layout type for the target extension type. This
0818   /// type can be used to query size and alignment information, if it is
0819   /// appropriate (although note that the layout type may also be void). It is
0820   /// not legal to bitcast between this type and the layout type, however.
0821   Type *getLayoutType() const;
0822 
0823   /// Methods for support type inquiry through isa, cast, and dyn_cast.
0824   static bool classof(const Type *T) { return T->getTypeID() == TargetExtTyID; }
0825 };
0826 
0827 StringRef Type::getTargetExtName() const {
0828   return cast<TargetExtType>(this)->getName();
0829 }
0830 
0831 } // end namespace llvm
0832 
0833 #endif // LLVM_IR_DERIVEDTYPES_H