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