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 /// This method determines if the width of this IntegerType is a power-of-2
91 /// in terms of 8 bit bytes.
92 /// @returns true if this is a power-of-2 byte width.
93 /// Is this a power-of-2 byte-width IntegerType ?
94 bool isPowerOf2ByteWidth() const;
95
96 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)97 static bool classof(const Type *T) {
98 return T->getTypeID() == IntegerTyID;
99 }
100 };
101
getIntegerBitWidth()102 unsigned Type::getIntegerBitWidth() const {
103 return cast<IntegerType>(this)->getBitWidth();
104 }
105
106 /// Class to represent function types
107 ///
108 class FunctionType : public Type {
109 FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
110
111 public:
112 FunctionType(const FunctionType &) = delete;
113 FunctionType &operator=(const FunctionType &) = delete;
114
115 /// This static method is the primary way of constructing a FunctionType.
116 static FunctionType *get(Type *Result,
117 ArrayRef<Type*> Params, bool isVarArg);
118
119 /// Create a FunctionType taking no parameters.
120 static FunctionType *get(Type *Result, bool isVarArg);
121
122 /// Return true if the specified type is valid as a return type.
123 static bool isValidReturnType(Type *RetTy);
124
125 /// Return true if the specified type is valid as an argument type.
126 static bool isValidArgumentType(Type *ArgTy);
127
isVarArg()128 bool isVarArg() const { return getSubclassData()!=0; }
getReturnType()129 Type *getReturnType() const { return ContainedTys[0]; }
130
131 using param_iterator = Type::subtype_iterator;
132
param_begin()133 param_iterator param_begin() const { return ContainedTys + 1; }
param_end()134 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
params()135 ArrayRef<Type *> params() const {
136 return makeArrayRef(param_begin(), param_end());
137 }
138
139 /// Parameter type accessors.
getParamType(unsigned i)140 Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
141
142 /// Return the number of fixed parameters this function type requires.
143 /// This does not consider varargs.
getNumParams()144 unsigned getNumParams() const { return NumContainedTys - 1; }
145
146 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)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
isFunctionVarArg()154 bool Type::isFunctionVarArg() const {
155 return cast<FunctionType>(this)->isVarArg();
156 }
157
getFunctionParamType(unsigned i)158 Type *Type::getFunctionParamType(unsigned i) const {
159 return cast<FunctionType>(this)->getParamType(i);
160 }
161
getFunctionNumParams()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)>
FunctionCallee(T * Fn)175 FunctionCallee(T *Fn)
176 : FnTy(Fn ? Fn->getFunctionType() : nullptr), Callee(Fn) {}
177
FunctionCallee(FunctionType * FnTy,Value * Callee)178 FunctionCallee(FunctionType *FnTy, Value *Callee)
179 : FnTy(FnTy), Callee(Callee) {
180 assert((FnTy == nullptr) == (Callee == nullptr));
181 }
182
FunctionCallee(std::nullptr_t)183 FunctionCallee(std::nullptr_t) {}
184
185 FunctionCallee() = default;
186
getFunctionType()187 FunctionType *getFunctionType() { return FnTy; }
188
getCallee()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 {
StructType(LLVMContext & C)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 };
228
229 /// For a named struct that actually has a name, this is a pointer to the
230 /// symbol table entry (maintained by LLVMContext) for the struct.
231 /// This is null if the type is an literal struct or if it is a identified
232 /// type that has an empty name.
233 void *SymbolTableEntry = nullptr;
234
235 public:
236 StructType(const StructType &) = delete;
237 StructType &operator=(const StructType &) = delete;
238
239 /// This creates an identified struct.
240 static StructType *create(LLVMContext &Context, StringRef Name);
241 static StructType *create(LLVMContext &Context);
242
243 static StructType *create(ArrayRef<Type *> Elements, StringRef Name,
244 bool isPacked = false);
245 static StructType *create(ArrayRef<Type *> Elements);
246 static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements,
247 StringRef Name, bool isPacked = false);
248 static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements);
249 template <class... Tys>
250 static std::enable_if_t<are_base_of<Type, Tys...>::value, StructType *>
create(StringRef Name,Type * elt1,Tys * ...elts)251 create(StringRef Name, Type *elt1, Tys *... elts) {
252 assert(elt1 && "Cannot create a struct type with no elements with this");
253 SmallVector<llvm::Type *, 8> StructFields({elt1, elts...});
254 return create(StructFields, Name);
255 }
256
257 /// This static method is the primary way to create a literal StructType.
258 static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
259 bool isPacked = false);
260
261 /// Create an empty structure type.
262 static StructType *get(LLVMContext &Context, bool isPacked = false);
263
264 /// This static method is a convenience method for creating structure types by
265 /// specifying the elements as arguments. Note that this method always returns
266 /// a non-packed struct, and requires at least one element type.
267 template <class... Tys>
268 static std::enable_if_t<are_base_of<Type, Tys...>::value, StructType *>
get(Type * elt1,Tys * ...elts)269 get(Type *elt1, Tys *... elts) {
270 assert(elt1 && "Cannot create a struct type with no elements with this");
271 LLVMContext &Ctx = elt1->getContext();
272 SmallVector<llvm::Type *, 8> StructFields({elt1, elts...});
273 return llvm::StructType::get(Ctx, StructFields);
274 }
275
276 /// Return the type with the specified name, or null if there is none by that
277 /// name.
278 static StructType *getTypeByName(LLVMContext &C, StringRef Name);
279
isPacked()280 bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
281
282 /// Return true if this type is uniqued by structural equivalence, false if it
283 /// is a struct definition.
isLiteral()284 bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
285
286 /// Return true if this is a type with an identity that has no body specified
287 /// yet. These prints as 'opaque' in .ll files.
isOpaque()288 bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
289
290 /// isSized - Return true if this is a sized type.
291 bool isSized(SmallPtrSetImpl<Type *> *Visited = nullptr) const;
292
293 /// Return true if this is a named struct that has a non-empty name.
hasName()294 bool hasName() const { return SymbolTableEntry != nullptr; }
295
296 /// Return the name for this struct type if it has an identity.
297 /// This may return an empty string for an unnamed struct type. Do not call
298 /// this on an literal type.
299 StringRef getName() const;
300
301 /// Change the name of this type to the specified name, or to a name with a
302 /// suffix if there is a collision. Do not call this on an literal type.
303 void setName(StringRef Name);
304
305 /// Specify a body for an opaque identified type.
306 void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
307
308 template <typename... Tys>
309 std::enable_if_t<are_base_of<Type, Tys...>::value, void>
setBody(Type * elt1,Tys * ...elts)310 setBody(Type *elt1, Tys *... elts) {
311 assert(elt1 && "Cannot create a struct type with no elements with this");
312 SmallVector<llvm::Type *, 8> StructFields({elt1, elts...});
313 setBody(StructFields);
314 }
315
316 /// Return true if the specified type is valid as a element type.
317 static bool isValidElementType(Type *ElemTy);
318
319 // Iterator access to the elements.
320 using element_iterator = Type::subtype_iterator;
321
element_begin()322 element_iterator element_begin() const { return ContainedTys; }
element_end()323 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
elements()324 ArrayRef<Type *> const elements() const {
325 return makeArrayRef(element_begin(), element_end());
326 }
327
328 /// Return true if this is layout identical to the specified struct.
329 bool isLayoutIdentical(StructType *Other) const;
330
331 /// Random access to the elements
getNumElements()332 unsigned getNumElements() const { return NumContainedTys; }
getElementType(unsigned N)333 Type *getElementType(unsigned N) const {
334 assert(N < NumContainedTys && "Element number out of range!");
335 return ContainedTys[N];
336 }
337 /// Given an index value into the type, return the type of the element.
338 Type *getTypeAtIndex(const Value *V) const;
getTypeAtIndex(unsigned N)339 Type *getTypeAtIndex(unsigned N) const { return getElementType(N); }
340 bool indexValid(const Value *V) const;
indexValid(unsigned Idx)341 bool indexValid(unsigned Idx) const { return Idx < getNumElements(); }
342
343 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)344 static bool classof(const Type *T) {
345 return T->getTypeID() == StructTyID;
346 }
347 };
348
getStructName()349 StringRef Type::getStructName() const {
350 return cast<StructType>(this)->getName();
351 }
352
getStructNumElements()353 unsigned Type::getStructNumElements() const {
354 return cast<StructType>(this)->getNumElements();
355 }
356
getStructElementType(unsigned N)357 Type *Type::getStructElementType(unsigned N) const {
358 return cast<StructType>(this)->getElementType(N);
359 }
360
361 /// Class to represent array types.
362 class ArrayType : public Type {
363 /// The element type of the array.
364 Type *ContainedType;
365 /// Number of elements in the array.
366 uint64_t NumElements;
367
368 ArrayType(Type *ElType, uint64_t NumEl);
369
370 public:
371 ArrayType(const ArrayType &) = delete;
372 ArrayType &operator=(const ArrayType &) = delete;
373
getNumElements()374 uint64_t getNumElements() const { return NumElements; }
getElementType()375 Type *getElementType() const { return ContainedType; }
376
377 /// This static method is the primary way to construct an ArrayType
378 static ArrayType *get(Type *ElementType, uint64_t NumElements);
379
380 /// Return true if the specified type is valid as a element type.
381 static bool isValidElementType(Type *ElemTy);
382
383 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)384 static bool classof(const Type *T) {
385 return T->getTypeID() == ArrayTyID;
386 }
387 };
388
getArrayNumElements()389 uint64_t Type::getArrayNumElements() const {
390 return cast<ArrayType>(this)->getNumElements();
391 }
392
393 /// Base class of all SIMD vector types
394 class VectorType : public Type {
395 /// A fully specified VectorType is of the form <vscale x n x Ty>. 'n' is the
396 /// minimum number of elements of type Ty contained within the vector, and
397 /// 'vscale x' indicates that the total element count is an integer multiple
398 /// of 'n', where the multiple is either guaranteed to be one, or is
399 /// statically unknown at compile time.
400 ///
401 /// If the multiple is known to be 1, then the extra term is discarded in
402 /// textual IR:
403 ///
404 /// <4 x i32> - a vector containing 4 i32s
405 /// <vscale x 4 x i32> - a vector containing an unknown integer multiple
406 /// of 4 i32s
407
408 /// The element type of the vector.
409 Type *ContainedType;
410
411 protected:
412 /// The element quantity of this vector. The meaning of this value depends
413 /// on the type of vector:
414 /// - For FixedVectorType = <ElementQuantity x ty>, there are
415 /// exactly ElementQuantity elements in this vector.
416 /// - For ScalableVectorType = <vscale x ElementQuantity x ty>,
417 /// there are vscale * ElementQuantity elements in this vector, where
418 /// vscale is a runtime-constant integer greater than 0.
419 const unsigned ElementQuantity;
420
421 VectorType(Type *ElType, unsigned EQ, Type::TypeID TID);
422
423 public:
424 VectorType(const VectorType &) = delete;
425 VectorType &operator=(const VectorType &) = delete;
426
427 /// Get the number of elements in this vector. It does not make sense to call
428 /// this function on a scalable vector, and this will be moved into
429 /// FixedVectorType in a future commit
430 LLVM_ATTRIBUTE_DEPRECATED(
431 inline unsigned getNumElements() const,
432 "Calling this function via a base VectorType is deprecated. Either call "
433 "getElementCount() and handle the case where Scalable is true or cast to "
434 "FixedVectorType.");
435
getElementType()436 Type *getElementType() const { return ContainedType; }
437
438 /// This static method is the primary way to construct an VectorType.
439 static VectorType *get(Type *ElementType, ElementCount EC);
440
get(Type * ElementType,unsigned NumElements,bool Scalable)441 static VectorType *get(Type *ElementType, unsigned NumElements,
442 bool Scalable) {
443 return VectorType::get(ElementType,
444 ElementCount::get(NumElements, Scalable));
445 }
446
get(Type * ElementType,const VectorType * Other)447 static VectorType *get(Type *ElementType, const VectorType *Other) {
448 return VectorType::get(ElementType, Other->getElementCount());
449 }
450
451 /// This static method gets a VectorType with the same number of elements as
452 /// the input type, and the element type is an integer type of the same width
453 /// as the input element type.
getInteger(VectorType * VTy)454 static VectorType *getInteger(VectorType *VTy) {
455 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
456 assert(EltBits && "Element size must be of a non-zero size");
457 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
458 return VectorType::get(EltTy, VTy->getElementCount());
459 }
460
461 /// This static method is like getInteger except that the element types are
462 /// twice as wide as the elements in the input type.
getExtendedElementVectorType(VectorType * VTy)463 static VectorType *getExtendedElementVectorType(VectorType *VTy) {
464 assert(VTy->isIntOrIntVectorTy() && "VTy expected to be a vector of ints.");
465 auto *EltTy = cast<IntegerType>(VTy->getElementType());
466 return VectorType::get(EltTy->getExtendedType(), VTy->getElementCount());
467 }
468
469 // This static method gets a VectorType with the same number of elements as
470 // the input type, and the element type is an integer or float type which
471 // is half as wide as the elements in the input type.
getTruncatedElementVectorType(VectorType * VTy)472 static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
473 Type *EltTy;
474 if (VTy->getElementType()->isFloatingPointTy()) {
475 switch(VTy->getElementType()->getTypeID()) {
476 case DoubleTyID:
477 EltTy = Type::getFloatTy(VTy->getContext());
478 break;
479 case FloatTyID:
480 EltTy = Type::getHalfTy(VTy->getContext());
481 break;
482 default:
483 llvm_unreachable("Cannot create narrower fp vector element type");
484 }
485 } else {
486 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
487 assert((EltBits & 1) == 0 &&
488 "Cannot truncate vector element with odd bit-width");
489 EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
490 }
491 return VectorType::get(EltTy, VTy->getElementCount());
492 }
493
494 // This static method returns a VectorType with a smaller number of elements
495 // of a larger type than the input element type. For example, a <16 x i8>
496 // subdivided twice would return <4 x i32>
getSubdividedVectorType(VectorType * VTy,int NumSubdivs)497 static VectorType *getSubdividedVectorType(VectorType *VTy, int NumSubdivs) {
498 for (int i = 0; i < NumSubdivs; ++i) {
499 VTy = VectorType::getDoubleElementsVectorType(VTy);
500 VTy = VectorType::getTruncatedElementVectorType(VTy);
501 }
502 return VTy;
503 }
504
505 /// This static method returns a VectorType with half as many elements as the
506 /// input type and the same element type.
getHalfElementsVectorType(VectorType * VTy)507 static VectorType *getHalfElementsVectorType(VectorType *VTy) {
508 auto EltCnt = VTy->getElementCount();
509 assert(EltCnt.isKnownEven() &&
510 "Cannot halve vector with odd number of elements.");
511 return VectorType::get(VTy->getElementType(),
512 EltCnt.divideCoefficientBy(2));
513 }
514
515 /// This static method returns a VectorType with twice as many elements as the
516 /// input type and the same element type.
getDoubleElementsVectorType(VectorType * VTy)517 static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
518 auto EltCnt = VTy->getElementCount();
519 assert((EltCnt.getKnownMinValue() * 2ull) <= UINT_MAX &&
520 "Too many elements in vector");
521 return VectorType::get(VTy->getElementType(), EltCnt * 2);
522 }
523
524 /// Return true if the specified type is valid as a element type.
525 static bool isValidElementType(Type *ElemTy);
526
527 /// Return an ElementCount instance to represent the (possibly scalable)
528 /// number of elements in the vector.
529 inline ElementCount getElementCount() const;
530
531 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)532 static bool classof(const Type *T) {
533 return T->getTypeID() == FixedVectorTyID ||
534 T->getTypeID() == ScalableVectorTyID;
535 }
536 };
537
getNumElements()538 unsigned VectorType::getNumElements() const {
539 ElementCount EC = getElementCount();
540 #ifdef STRICT_FIXED_SIZE_VECTORS
541 assert(!EC.isScalable() &&
542 "Request for fixed number of elements from scalable vector");
543 #else
544 if (EC.isScalable())
545 WithColor::warning()
546 << "The code that requested the fixed number of elements has made the "
547 "assumption that this vector is not scalable. This assumption was "
548 "not correct, and this may lead to broken code\n";
549 #endif
550 return EC.getKnownMinValue();
551 }
552
553 /// Class to represent fixed width SIMD vectors
554 class FixedVectorType : public VectorType {
555 protected:
FixedVectorType(Type * ElTy,unsigned NumElts)556 FixedVectorType(Type *ElTy, unsigned NumElts)
557 : VectorType(ElTy, NumElts, FixedVectorTyID) {}
558
559 public:
560 static FixedVectorType *get(Type *ElementType, unsigned NumElts);
561
get(Type * ElementType,const FixedVectorType * FVTy)562 static FixedVectorType *get(Type *ElementType, const FixedVectorType *FVTy) {
563 return get(ElementType, FVTy->getNumElements());
564 }
565
getInteger(FixedVectorType * VTy)566 static FixedVectorType *getInteger(FixedVectorType *VTy) {
567 return cast<FixedVectorType>(VectorType::getInteger(VTy));
568 }
569
getExtendedElementVectorType(FixedVectorType * VTy)570 static FixedVectorType *getExtendedElementVectorType(FixedVectorType *VTy) {
571 return cast<FixedVectorType>(VectorType::getExtendedElementVectorType(VTy));
572 }
573
getTruncatedElementVectorType(FixedVectorType * VTy)574 static FixedVectorType *getTruncatedElementVectorType(FixedVectorType *VTy) {
575 return cast<FixedVectorType>(
576 VectorType::getTruncatedElementVectorType(VTy));
577 }
578
getSubdividedVectorType(FixedVectorType * VTy,int NumSubdivs)579 static FixedVectorType *getSubdividedVectorType(FixedVectorType *VTy,
580 int NumSubdivs) {
581 return cast<FixedVectorType>(
582 VectorType::getSubdividedVectorType(VTy, NumSubdivs));
583 }
584
getHalfElementsVectorType(FixedVectorType * VTy)585 static FixedVectorType *getHalfElementsVectorType(FixedVectorType *VTy) {
586 return cast<FixedVectorType>(VectorType::getHalfElementsVectorType(VTy));
587 }
588
getDoubleElementsVectorType(FixedVectorType * VTy)589 static FixedVectorType *getDoubleElementsVectorType(FixedVectorType *VTy) {
590 return cast<FixedVectorType>(VectorType::getDoubleElementsVectorType(VTy));
591 }
592
classof(const Type * T)593 static bool classof(const Type *T) {
594 return T->getTypeID() == FixedVectorTyID;
595 }
596
getNumElements()597 unsigned getNumElements() const { return ElementQuantity; }
598 };
599
600 /// Class to represent scalable SIMD vectors
601 class ScalableVectorType : public VectorType {
602 protected:
ScalableVectorType(Type * ElTy,unsigned MinNumElts)603 ScalableVectorType(Type *ElTy, unsigned MinNumElts)
604 : VectorType(ElTy, MinNumElts, ScalableVectorTyID) {}
605
606 public:
607 static ScalableVectorType *get(Type *ElementType, unsigned MinNumElts);
608
get(Type * ElementType,const ScalableVectorType * SVTy)609 static ScalableVectorType *get(Type *ElementType,
610 const ScalableVectorType *SVTy) {
611 return get(ElementType, SVTy->getMinNumElements());
612 }
613
getInteger(ScalableVectorType * VTy)614 static ScalableVectorType *getInteger(ScalableVectorType *VTy) {
615 return cast<ScalableVectorType>(VectorType::getInteger(VTy));
616 }
617
618 static ScalableVectorType *
getExtendedElementVectorType(ScalableVectorType * VTy)619 getExtendedElementVectorType(ScalableVectorType *VTy) {
620 return cast<ScalableVectorType>(
621 VectorType::getExtendedElementVectorType(VTy));
622 }
623
624 static ScalableVectorType *
getTruncatedElementVectorType(ScalableVectorType * VTy)625 getTruncatedElementVectorType(ScalableVectorType *VTy) {
626 return cast<ScalableVectorType>(
627 VectorType::getTruncatedElementVectorType(VTy));
628 }
629
getSubdividedVectorType(ScalableVectorType * VTy,int NumSubdivs)630 static ScalableVectorType *getSubdividedVectorType(ScalableVectorType *VTy,
631 int NumSubdivs) {
632 return cast<ScalableVectorType>(
633 VectorType::getSubdividedVectorType(VTy, NumSubdivs));
634 }
635
636 static ScalableVectorType *
getHalfElementsVectorType(ScalableVectorType * VTy)637 getHalfElementsVectorType(ScalableVectorType *VTy) {
638 return cast<ScalableVectorType>(VectorType::getHalfElementsVectorType(VTy));
639 }
640
641 static ScalableVectorType *
getDoubleElementsVectorType(ScalableVectorType * VTy)642 getDoubleElementsVectorType(ScalableVectorType *VTy) {
643 return cast<ScalableVectorType>(
644 VectorType::getDoubleElementsVectorType(VTy));
645 }
646
647 /// Get the minimum number of elements in this vector. The actual number of
648 /// elements in the vector is an integer multiple of this value.
getMinNumElements()649 uint64_t getMinNumElements() const { return ElementQuantity; }
650
classof(const Type * T)651 static bool classof(const Type *T) {
652 return T->getTypeID() == ScalableVectorTyID;
653 }
654 };
655
getElementCount()656 inline ElementCount VectorType::getElementCount() const {
657 return ElementCount::get(ElementQuantity, isa<ScalableVectorType>(this));
658 }
659
660 /// Class to represent pointers.
661 class PointerType : public Type {
662 explicit PointerType(Type *ElType, unsigned AddrSpace);
663
664 Type *PointeeTy;
665
666 public:
667 PointerType(const PointerType &) = delete;
668 PointerType &operator=(const PointerType &) = delete;
669
670 /// This constructs a pointer to an object of the specified type in a numbered
671 /// address space.
672 static PointerType *get(Type *ElementType, unsigned AddressSpace);
673
674 /// This constructs a pointer to an object of the specified type in the
675 /// generic address space (address space zero).
getUnqual(Type * ElementType)676 static PointerType *getUnqual(Type *ElementType) {
677 return PointerType::get(ElementType, 0);
678 }
679
getElementType()680 Type *getElementType() const { return PointeeTy; }
681
682 /// Return true if the specified type is valid as a element type.
683 static bool isValidElementType(Type *ElemTy);
684
685 /// Return true if we can load or store from a pointer to this type.
686 static bool isLoadableOrStorableType(Type *ElemTy);
687
688 /// Return the address space of the Pointer type.
getAddressSpace()689 inline unsigned getAddressSpace() const { return getSubclassData(); }
690
691 /// Implement support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)692 static bool classof(const Type *T) {
693 return T->getTypeID() == PointerTyID;
694 }
695 };
696
getExtendedType()697 Type *Type::getExtendedType() const {
698 assert(
699 isIntOrIntVectorTy() &&
700 "Original type expected to be a vector of integers or a scalar integer.");
701 if (auto *VTy = dyn_cast<VectorType>(this))
702 return VectorType::getExtendedElementVectorType(
703 const_cast<VectorType *>(VTy));
704 return cast<IntegerType>(this)->getExtendedType();
705 }
706
getWithNewBitWidth(unsigned NewBitWidth)707 Type *Type::getWithNewBitWidth(unsigned NewBitWidth) const {
708 assert(
709 isIntOrIntVectorTy() &&
710 "Original type expected to be a vector of integers or a scalar integer.");
711 Type *NewType = getIntNTy(getContext(), NewBitWidth);
712 if (auto *VTy = dyn_cast<VectorType>(this))
713 NewType = VectorType::get(NewType, VTy->getElementCount());
714 return NewType;
715 }
716
getPointerAddressSpace()717 unsigned Type::getPointerAddressSpace() const {
718 return cast<PointerType>(getScalarType())->getAddressSpace();
719 }
720
721 } // end namespace llvm
722
723 #endif // LLVM_IR_DERIVEDTYPES_H
724