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