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
isPacked()276 bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
277
278 /// Return true if this type is uniqued by structural equivalence, false if it
279 /// is a struct definition.
isLiteral()280 bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
281
282 /// Return true if this is a type with an identity that has no body specified
283 /// yet. These prints as 'opaque' in .ll files.
isOpaque()284 bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
285
286 /// isSized - Return true if this is a sized type.
287 bool isSized(SmallPtrSetImpl<Type *> *Visited = nullptr) const;
288
289 /// Return true if this is a named struct that has a non-empty name.
hasName()290 bool hasName() const { return SymbolTableEntry != nullptr; }
291
292 /// Return the name for this struct type if it has an identity.
293 /// This may return an empty string for an unnamed struct type. Do not call
294 /// this on an literal type.
295 StringRef getName() const;
296
297 /// Change the name of this type to the specified name, or to a name with a
298 /// suffix if there is a collision. Do not call this on an literal type.
299 void setName(StringRef Name);
300
301 /// Specify a body for an opaque identified type.
302 void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
303
304 template <typename... Tys>
305 std::enable_if_t<are_base_of<Type, Tys...>::value, void>
setBody(Type * elt1,Tys * ...elts)306 setBody(Type *elt1, Tys *... elts) {
307 assert(elt1 && "Cannot create a struct type with no elements with this");
308 SmallVector<llvm::Type *, 8> StructFields({elt1, elts...});
309 setBody(StructFields);
310 }
311
312 /// Return true if the specified type is valid as a element type.
313 static bool isValidElementType(Type *ElemTy);
314
315 // Iterator access to the elements.
316 using element_iterator = Type::subtype_iterator;
317
element_begin()318 element_iterator element_begin() const { return ContainedTys; }
element_end()319 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
elements()320 ArrayRef<Type *> const elements() const {
321 return makeArrayRef(element_begin(), element_end());
322 }
323
324 /// Return true if this is layout identical to the specified struct.
325 bool isLayoutIdentical(StructType *Other) const;
326
327 /// Random access to the elements
getNumElements()328 unsigned getNumElements() const { return NumContainedTys; }
getElementType(unsigned N)329 Type *getElementType(unsigned N) const {
330 assert(N < NumContainedTys && "Element number out of range!");
331 return ContainedTys[N];
332 }
333 /// Given an index value into the type, return the type of the element.
334 Type *getTypeAtIndex(const Value *V) const;
getTypeAtIndex(unsigned N)335 Type *getTypeAtIndex(unsigned N) const { return getElementType(N); }
336 bool indexValid(const Value *V) const;
indexValid(unsigned Idx)337 bool indexValid(unsigned Idx) const { return Idx < getNumElements(); }
338
339 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)340 static bool classof(const Type *T) {
341 return T->getTypeID() == StructTyID;
342 }
343 };
344
getStructName()345 StringRef Type::getStructName() const {
346 return cast<StructType>(this)->getName();
347 }
348
getStructNumElements()349 unsigned Type::getStructNumElements() const {
350 return cast<StructType>(this)->getNumElements();
351 }
352
getStructElementType(unsigned N)353 Type *Type::getStructElementType(unsigned N) const {
354 return cast<StructType>(this)->getElementType(N);
355 }
356
357 /// Class to represent array types.
358 class ArrayType : public Type {
359 /// The element type of the array.
360 Type *ContainedType;
361 /// Number of elements in the array.
362 uint64_t NumElements;
363
364 ArrayType(Type *ElType, uint64_t NumEl);
365
366 public:
367 ArrayType(const ArrayType &) = delete;
368 ArrayType &operator=(const ArrayType &) = delete;
369
getNumElements()370 uint64_t getNumElements() const { return NumElements; }
getElementType()371 Type *getElementType() const { return ContainedType; }
372
373 /// This static method is the primary way to construct an ArrayType
374 static ArrayType *get(Type *ElementType, uint64_t NumElements);
375
376 /// Return true if the specified type is valid as a element type.
377 static bool isValidElementType(Type *ElemTy);
378
379 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)380 static bool classof(const Type *T) {
381 return T->getTypeID() == ArrayTyID;
382 }
383 };
384
getArrayNumElements()385 uint64_t Type::getArrayNumElements() const {
386 return cast<ArrayType>(this)->getNumElements();
387 }
388
389 /// Base class of all SIMD vector types
390 class VectorType : public Type {
391 /// A fully specified VectorType is of the form <vscale x n x Ty>. 'n' is the
392 /// minimum number of elements of type Ty contained within the vector, and
393 /// 'vscale x' indicates that the total element count is an integer multiple
394 /// of 'n', where the multiple is either guaranteed to be one, or is
395 /// statically unknown at compile time.
396 ///
397 /// If the multiple is known to be 1, then the extra term is discarded in
398 /// textual IR:
399 ///
400 /// <4 x i32> - a vector containing 4 i32s
401 /// <vscale x 4 x i32> - a vector containing an unknown integer multiple
402 /// of 4 i32s
403
404 /// The element type of the vector.
405 Type *ContainedType;
406
407 protected:
408 /// The element quantity of this vector. The meaning of this value depends
409 /// on the type of vector:
410 /// - For FixedVectorType = <ElementQuantity x ty>, there are
411 /// exactly ElementQuantity elements in this vector.
412 /// - For ScalableVectorType = <vscale x ElementQuantity x ty>,
413 /// there are vscale * ElementQuantity elements in this vector, where
414 /// vscale is a runtime-constant integer greater than 0.
415 const unsigned ElementQuantity;
416
417 VectorType(Type *ElType, unsigned EQ, Type::TypeID TID);
418
419 public:
420 VectorType(const VectorType &) = delete;
421 VectorType &operator=(const VectorType &) = delete;
422
423 /// Get the number of elements in this vector. It does not make sense to call
424 /// this function on a scalable vector, and this will be moved into
425 /// FixedVectorType in a future commit
getNumElements()426 unsigned getNumElements() const {
427 ElementCount EC = getElementCount();
428 #ifdef STRICT_FIXED_SIZE_VECTORS
429 assert(!EC.Scalable &&
430 "Request for fixed number of elements from scalable vector");
431 return EC.Min;
432 #else
433 if (EC.Scalable)
434 WithColor::warning()
435 << "The code that requested the fixed number of elements has made "
436 "the assumption that this vector is not scalable. This assumption "
437 "was not correct, and this may lead to broken code\n";
438 return EC.Min;
439 #endif
440 }
441
getElementType()442 Type *getElementType() const { return ContainedType; }
443
444 /// This static method is the primary way to construct an VectorType.
445 static VectorType *get(Type *ElementType, ElementCount EC);
446
447 /// Base class getter that specifically constructs a FixedVectorType. This
448 /// function is deprecated, and will be removed after LLVM 11 ships. Since
449 /// this always returns a FixedVectorType via a base VectorType pointer,
450 /// FixedVectorType::get(Type *, unsigned) is strictly better since no cast is
451 /// required to call getNumElements() on the result.
452 LLVM_ATTRIBUTE_DEPRECATED(
453 inline static VectorType *get(Type *ElementType, unsigned NumElements),
454 "The base class version of get with the scalable argument defaulted to "
455 "false is deprecated. Either call VectorType::get(Type *, unsigned, "
456 "bool) and pass false, or call FixedVectorType::get(Type *, unsigned).");
457
get(Type * ElementType,unsigned NumElements,bool Scalable)458 static VectorType *get(Type *ElementType, unsigned NumElements,
459 bool Scalable) {
460 return VectorType::get(ElementType, {NumElements, Scalable});
461 }
462
get(Type * ElementType,const VectorType * Other)463 static VectorType *get(Type *ElementType, const VectorType *Other) {
464 return VectorType::get(ElementType, Other->getElementCount());
465 }
466
467 /// This static method gets a VectorType with the same number of elements as
468 /// the input type, and the element type is an integer type of the same width
469 /// as the input element type.
getInteger(VectorType * VTy)470 static VectorType *getInteger(VectorType *VTy) {
471 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
472 assert(EltBits && "Element size must be of a non-zero size");
473 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
474 return VectorType::get(EltTy, VTy->getElementCount());
475 }
476
477 /// This static method is like getInteger except that the element types are
478 /// twice as wide as the elements in the input type.
getExtendedElementVectorType(VectorType * VTy)479 static VectorType *getExtendedElementVectorType(VectorType *VTy) {
480 assert(VTy->isIntOrIntVectorTy() && "VTy expected to be a vector of ints.");
481 auto *EltTy = cast<IntegerType>(VTy->getElementType());
482 return VectorType::get(EltTy->getExtendedType(), VTy->getElementCount());
483 }
484
485 // This static method gets a VectorType with the same number of elements as
486 // the input type, and the element type is an integer or float type which
487 // is half as wide as the elements in the input type.
getTruncatedElementVectorType(VectorType * VTy)488 static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
489 Type *EltTy;
490 if (VTy->getElementType()->isFloatingPointTy()) {
491 switch(VTy->getElementType()->getTypeID()) {
492 case DoubleTyID:
493 EltTy = Type::getFloatTy(VTy->getContext());
494 break;
495 case FloatTyID:
496 EltTy = Type::getHalfTy(VTy->getContext());
497 break;
498 default:
499 llvm_unreachable("Cannot create narrower fp vector element type");
500 }
501 } else {
502 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
503 assert((EltBits & 1) == 0 &&
504 "Cannot truncate vector element with odd bit-width");
505 EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
506 }
507 return VectorType::get(EltTy, VTy->getElementCount());
508 }
509
510 // This static method returns a VectorType with a smaller number of elements
511 // of a larger type than the input element type. For example, a <16 x i8>
512 // subdivided twice would return <4 x i32>
getSubdividedVectorType(VectorType * VTy,int NumSubdivs)513 static VectorType *getSubdividedVectorType(VectorType *VTy, int NumSubdivs) {
514 for (int i = 0; i < NumSubdivs; ++i) {
515 VTy = VectorType::getDoubleElementsVectorType(VTy);
516 VTy = VectorType::getTruncatedElementVectorType(VTy);
517 }
518 return VTy;
519 }
520
521 /// This static method returns a VectorType with half as many elements as the
522 /// input type and the same element type.
getHalfElementsVectorType(VectorType * VTy)523 static VectorType *getHalfElementsVectorType(VectorType *VTy) {
524 auto EltCnt = VTy->getElementCount();
525 assert ((EltCnt.Min & 1) == 0 &&
526 "Cannot halve vector with odd number of elements.");
527 return VectorType::get(VTy->getElementType(), EltCnt/2);
528 }
529
530 /// This static method returns a VectorType with twice as many elements as the
531 /// input type and the same element type.
getDoubleElementsVectorType(VectorType * VTy)532 static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
533 auto EltCnt = VTy->getElementCount();
534 assert((EltCnt.Min * 2ull) <= UINT_MAX && "Too many elements in vector");
535 return VectorType::get(VTy->getElementType(), EltCnt * 2);
536 }
537
538 /// Return true if the specified type is valid as a element type.
539 static bool isValidElementType(Type *ElemTy);
540
541 /// Return an ElementCount instance to represent the (possibly scalable)
542 /// number of elements in the vector.
543 inline ElementCount getElementCount() const;
544
545 /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)546 static bool classof(const Type *T) {
547 return T->getTypeID() == FixedVectorTyID ||
548 T->getTypeID() == ScalableVectorTyID;
549 }
550 };
551
get(Type * ElementType,unsigned NumElements)552 inline VectorType *VectorType::get(Type *ElementType, unsigned NumElements) {
553 return VectorType::get(ElementType, NumElements, false);
554 }
555
556 /// Class to represent fixed width SIMD vectors
557 class FixedVectorType : public VectorType {
558 protected:
FixedVectorType(Type * ElTy,unsigned NumElts)559 FixedVectorType(Type *ElTy, unsigned NumElts)
560 : VectorType(ElTy, NumElts, FixedVectorTyID) {}
561
562 public:
563 static FixedVectorType *get(Type *ElementType, unsigned NumElts);
564
get(Type * ElementType,const FixedVectorType * FVTy)565 static FixedVectorType *get(Type *ElementType, const FixedVectorType *FVTy) {
566 return get(ElementType, FVTy->getNumElements());
567 }
568
getInteger(FixedVectorType * VTy)569 static FixedVectorType *getInteger(FixedVectorType *VTy) {
570 return cast<FixedVectorType>(VectorType::getInteger(VTy));
571 }
572
getExtendedElementVectorType(FixedVectorType * VTy)573 static FixedVectorType *getExtendedElementVectorType(FixedVectorType *VTy) {
574 return cast<FixedVectorType>(VectorType::getExtendedElementVectorType(VTy));
575 }
576
getTruncatedElementVectorType(FixedVectorType * VTy)577 static FixedVectorType *getTruncatedElementVectorType(FixedVectorType *VTy) {
578 return cast<FixedVectorType>(
579 VectorType::getTruncatedElementVectorType(VTy));
580 }
581
getSubdividedVectorType(FixedVectorType * VTy,int NumSubdivs)582 static FixedVectorType *getSubdividedVectorType(FixedVectorType *VTy,
583 int NumSubdivs) {
584 return cast<FixedVectorType>(
585 VectorType::getSubdividedVectorType(VTy, NumSubdivs));
586 }
587
getHalfElementsVectorType(FixedVectorType * VTy)588 static FixedVectorType *getHalfElementsVectorType(FixedVectorType *VTy) {
589 return cast<FixedVectorType>(VectorType::getHalfElementsVectorType(VTy));
590 }
591
getDoubleElementsVectorType(FixedVectorType * VTy)592 static FixedVectorType *getDoubleElementsVectorType(FixedVectorType *VTy) {
593 return cast<FixedVectorType>(VectorType::getDoubleElementsVectorType(VTy));
594 }
595
classof(const Type * T)596 static bool classof(const Type *T) {
597 return T->getTypeID() == FixedVectorTyID;
598 }
599 };
600
601 /// Class to represent scalable SIMD vectors
602 class ScalableVectorType : public VectorType {
603 protected:
ScalableVectorType(Type * ElTy,unsigned MinNumElts)604 ScalableVectorType(Type *ElTy, unsigned MinNumElts)
605 : VectorType(ElTy, MinNumElts, ScalableVectorTyID) {}
606
607 public:
608 static ScalableVectorType *get(Type *ElementType, unsigned MinNumElts);
609
get(Type * ElementType,const ScalableVectorType * SVTy)610 static ScalableVectorType *get(Type *ElementType,
611 const ScalableVectorType *SVTy) {
612 return get(ElementType, SVTy->getMinNumElements());
613 }
614
getInteger(ScalableVectorType * VTy)615 static ScalableVectorType *getInteger(ScalableVectorType *VTy) {
616 return cast<ScalableVectorType>(VectorType::getInteger(VTy));
617 }
618
619 static ScalableVectorType *
getExtendedElementVectorType(ScalableVectorType * VTy)620 getExtendedElementVectorType(ScalableVectorType *VTy) {
621 return cast<ScalableVectorType>(
622 VectorType::getExtendedElementVectorType(VTy));
623 }
624
625 static ScalableVectorType *
getTruncatedElementVectorType(ScalableVectorType * VTy)626 getTruncatedElementVectorType(ScalableVectorType *VTy) {
627 return cast<ScalableVectorType>(
628 VectorType::getTruncatedElementVectorType(VTy));
629 }
630
getSubdividedVectorType(ScalableVectorType * VTy,int NumSubdivs)631 static ScalableVectorType *getSubdividedVectorType(ScalableVectorType *VTy,
632 int NumSubdivs) {
633 return cast<ScalableVectorType>(
634 VectorType::getSubdividedVectorType(VTy, NumSubdivs));
635 }
636
637 static ScalableVectorType *
getHalfElementsVectorType(ScalableVectorType * VTy)638 getHalfElementsVectorType(ScalableVectorType *VTy) {
639 return cast<ScalableVectorType>(VectorType::getHalfElementsVectorType(VTy));
640 }
641
642 static ScalableVectorType *
getDoubleElementsVectorType(ScalableVectorType * VTy)643 getDoubleElementsVectorType(ScalableVectorType *VTy) {
644 return cast<ScalableVectorType>(
645 VectorType::getDoubleElementsVectorType(VTy));
646 }
647
648 /// Get the minimum number of elements in this vector. The actual number of
649 /// elements in the vector is an integer multiple of this value.
getMinNumElements()650 uint64_t getMinNumElements() const { return ElementQuantity; }
651
classof(const Type * T)652 static bool classof(const Type *T) {
653 return T->getTypeID() == ScalableVectorTyID;
654 }
655 };
656
getElementCount()657 inline ElementCount VectorType::getElementCount() const {
658 return ElementCount(ElementQuantity, isa<ScalableVectorType>(this));
659 }
660
661 /// Class to represent pointers.
662 class PointerType : public Type {
663 explicit PointerType(Type *ElType, unsigned AddrSpace);
664
665 Type *PointeeTy;
666
667 public:
668 PointerType(const PointerType &) = delete;
669 PointerType &operator=(const PointerType &) = delete;
670
671 /// This constructs a pointer to an object of the specified type in a numbered
672 /// address space.
673 static PointerType *get(Type *ElementType, unsigned AddressSpace);
674
675 /// This constructs a pointer to an object of the specified type in the
676 /// generic address space (address space zero).
getUnqual(Type * ElementType)677 static PointerType *getUnqual(Type *ElementType) {
678 return PointerType::get(ElementType, 0);
679 }
680
getElementType()681 Type *getElementType() const { return PointeeTy; }
682
683 /// Return true if the specified type is valid as a element type.
684 static bool isValidElementType(Type *ElemTy);
685
686 /// Return true if we can load or store from a pointer to this type.
687 static bool isLoadableOrStorableType(Type *ElemTy);
688
689 /// Return the address space of the Pointer type.
getAddressSpace()690 inline unsigned getAddressSpace() const { return getSubclassData(); }
691
692 /// Implement support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)693 static bool classof(const Type *T) {
694 return T->getTypeID() == PointerTyID;
695 }
696 };
697
getExtendedType()698 Type *Type::getExtendedType() const {
699 assert(
700 isIntOrIntVectorTy() &&
701 "Original type expected to be a vector of integers or a scalar integer.");
702 if (auto *VTy = dyn_cast<VectorType>(this))
703 return VectorType::getExtendedElementVectorType(
704 const_cast<VectorType *>(VTy));
705 return cast<IntegerType>(this)->getExtendedType();
706 }
707
getWithNewBitWidth(unsigned NewBitWidth)708 Type *Type::getWithNewBitWidth(unsigned NewBitWidth) const {
709 assert(
710 isIntOrIntVectorTy() &&
711 "Original type expected to be a vector of integers or a scalar integer.");
712 Type *NewType = getIntNTy(getContext(), NewBitWidth);
713 if (auto *VTy = dyn_cast<VectorType>(this))
714 NewType = VectorType::get(NewType, VTy->getElementCount());
715 return NewType;
716 }
717
getPointerAddressSpace()718 unsigned Type::getPointerAddressSpace() const {
719 return cast<PointerType>(getScalarType())->getAddressSpace();
720 }
721
722 } // end namespace llvm
723
724 #endif // LLVM_IR_DERIVEDTYPES_H
725