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