LLVM 17.0.0git
DerivedTypes.h
Go to the documentation of this file.
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"
27#include <cassert>
28#include <cstdint>
29
30namespace llvm {
31
32class Value;
33class APInt;
34class 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
40class IntegerType : public Type {
41 friend class LLVMContextImpl;
42
43protected:
44 explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){
45 setSubclassData(NumBits);
46 }
47
48public:
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.
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.
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).
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
97unsigned Type::getIntegerBitWidth() const {
98 return cast<IntegerType>(this)->getBitWidth();
99}
100
101/// Class to represent function types
102///
103class FunctionType : public Type {
104 FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
105
106public:
107 FunctionType(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
127
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};
146static_assert(alignof(FunctionType) >= alignof(Type *),
147 "Alignment sufficient for objects appended to FunctionType");
148
149bool Type::isFunctionVarArg() const {
150 return cast<FunctionType>(this)->isVarArg();
151}
152
153Type *Type::getFunctionParamType(unsigned i) const {
154 return cast<FunctionType>(this)->getParamType(i);
155}
156
157unsigned Type::getFunctionNumParams() const {
158 return cast<FunctionType>(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.
166public:
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)>
171 : FnTy(Fn ? Fn->getFunctionType() : nullptr), Callee(Fn) {}
172
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
188private:
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///
213class StructType : public Type {
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
232public:
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
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(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(Ctx, ArrayRef<Type *>({elt1, elts...}));
269 }
270
271 /// Return the type with the specified name, or null if there is none by that
272 /// 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>}}
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(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.
327
332 }
333
334 /// Return true if this is layout identical to the specified struct.
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
355StringRef Type::getStructName() const {
356 return cast<StructType>(this)->getName();
357}
358
359unsigned Type::getStructNumElements() const {
360 return cast<StructType>(this)->getNumElements();
361}
362
363Type *Type::getStructElementType(unsigned N) const {
364 return cast<StructType>(this)->getElementType(N);
365}
366
367/// Class to represent array types.
368class 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
376public:
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
396 return cast<ArrayType>(this)->getNumElements();
397}
398
399/// Base class of all SIMD vector types
400class 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
417protected:
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
429public:
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 ElementCount::get(NumElements, Scalable));
442 }
443
444 static VectorType *get(Type *ElementType, const VectorType *Other) {
445 return VectorType::get(ElementType, 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.
452 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
453 assert(EltBits && "Element size must be of a non-zero size");
454 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
455 return VectorType::get(EltTy, 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.
461 assert(VTy->isIntOrIntVectorTy() && "VTy expected to be a vector of ints.");
462 auto *EltTy = cast<IntegerType>(VTy->getElementType());
463 return VectorType::get(EltTy->getExtendedType(), 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.
470 Type *EltTy;
471 if (VTy->getElementType()->isFloatingPointTy()) {
472 switch(VTy->getElementType()->getTypeID()) {
473 case DoubleTyID:
474 EltTy = Type::getFloatTy(VTy->getContext());
475 break;
476 case FloatTyID:
477 EltTy = Type::getHalfTy(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(VTy->getContext(), EltBits / 2);
487 }
488 return VectorType::get(EltTy, 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) {
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.
505 auto EltCnt = VTy->getElementCount();
506 assert(EltCnt.isKnownEven() &&
507 "Cannot halve vector with odd number of elements.");
508 return VectorType::get(VTy->getElementType(),
509 EltCnt.divideCoefficientBy(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.
515 auto EltCnt = VTy->getElementCount();
516 assert((EltCnt.getKnownMinValue() * 2ull) <= UINT_MAX &&
517 "Too many elements in vector");
518 return VectorType::get(VTy->getElementType(), 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
537protected:
538 FixedVectorType(Type *ElTy, unsigned NumElts)
539 : VectorType(ElTy, NumElts, FixedVectorTyID) {}
540
541public:
542 static FixedVectorType *get(Type *ElementType, unsigned NumElts);
543
544 static FixedVectorType *get(Type *ElementType, const FixedVectorType *FVTy) {
545 return get(ElementType, FVTy->getNumElements());
546 }
547
549 return cast<FixedVectorType>(VectorType::getInteger(VTy));
550 }
551
553 return cast<FixedVectorType>(VectorType::getExtendedElementVectorType(VTy));
554 }
555
557 return cast<FixedVectorType>(
559 }
560
562 int NumSubdivs) {
563 return cast<FixedVectorType>(
564 VectorType::getSubdividedVectorType(VTy, NumSubdivs));
565 }
566
568 return cast<FixedVectorType>(VectorType::getHalfElementsVectorType(VTy));
569 }
570
572 return cast<FixedVectorType>(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
584protected:
585 ScalableVectorType(Type *ElTy, unsigned MinNumElts)
586 : VectorType(ElTy, MinNumElts, ScalableVectorTyID) {}
587
588public:
589 static ScalableVectorType *get(Type *ElementType, unsigned MinNumElts);
590
591 static ScalableVectorType *get(Type *ElementType,
592 const ScalableVectorType *SVTy) {
593 return get(ElementType, SVTy->getMinNumElements());
594 }
595
597 return cast<ScalableVectorType>(VectorType::getInteger(VTy));
598 }
599
600 static ScalableVectorType *
602 return cast<ScalableVectorType>(
604 }
605
606 static ScalableVectorType *
608 return cast<ScalableVectorType>(
610 }
611
613 int NumSubdivs) {
614 return cast<ScalableVectorType>(
615 VectorType::getSubdividedVectorType(VTy, NumSubdivs));
616 }
617
618 static ScalableVectorType *
620 return cast<ScalableVectorType>(VectorType::getHalfElementsVectorType(VTy));
621 }
622
623 static ScalableVectorType *
625 return cast<ScalableVectorType>(
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.
632
633 static bool classof(const Type *T) {
634 return T->getTypeID() == ScalableVectorTyID;
635 }
636};
637
639 return ElementCount::get(ElementQuantity, isa<ScalableVectorType>(this));
640}
641
642/// Class to represent pointers.
643class PointerType : public Type {
644 explicit PointerType(Type *ElType, unsigned AddrSpace);
645 explicit PointerType(LLVMContext &C, unsigned AddrSpace);
646
647 Type *PointeeTy;
648
649public:
650 PointerType(const PointerType &) = delete;
652
653 /// This constructs a pointer to an object of the specified type in a numbered
654 /// address space.
655 static PointerType *get(Type *ElementType, unsigned AddressSpace);
656 /// This constructs an opaque pointer to an object in a numbered address
657 /// space.
658 static PointerType *get(LLVMContext &C, unsigned AddressSpace);
659
660 /// This constructs a pointer to an object of the specified type in the
661 /// default address space (address space zero).
662 static PointerType *getUnqual(Type *ElementType) {
663 return PointerType::get(ElementType, 0);
664 }
665
666 /// This constructs an opaque pointer to an object in the
667 /// default address space (address space zero).
669 return PointerType::get(C, 0);
670 }
671
672 /// This constructs a pointer type with the same pointee type as input
673 /// PointerType (or opaque pointer if the input PointerType is opaque) and the
674 /// given address space. This is only useful during the opaque pointer
675 /// transition.
676 /// TODO: remove after opaque pointer transition is complete.
678 unsigned AddressSpace) {
679 if (PT->isOpaque())
680 return get(PT->getContext(), AddressSpace);
681 return get(PT->PointeeTy, AddressSpace);
682 }
683
684 bool isOpaque() const { return !PointeeTy; }
685
686 /// Return true if the specified type is valid as a element type.
687 static bool isValidElementType(Type *ElemTy);
688
689 /// Return true if we can load or store from a pointer to this type.
690 static bool isLoadableOrStorableType(Type *ElemTy);
691
692 /// Return the address space of the Pointer type.
693 inline unsigned getAddressSpace() const { return getSubclassData(); }
694
695 /// Return true if either this is an opaque pointer type or if this pointee
696 /// type matches Ty. Primarily used for checking if an instruction's pointer
697 /// operands are valid types. Will be useless after non-opaque pointers are
698 /// removed.
700 return isOpaque() || PointeeTy == Ty;
701 }
702
703 /// Return true if both pointer types have the same element type. Two opaque
704 /// pointers are considered to have the same element type, while an opaque
705 /// and a non-opaque pointer have different element types.
706 /// TODO: Remove after opaque pointer transition is complete.
708 return PointeeTy == Other->PointeeTy;
709 }
710
711 /// Implement support type inquiry through isa, cast, and dyn_cast.
712 static bool classof(const Type *T) {
713 return T->getTypeID() == PointerTyID;
714 }
715};
716
717Type *Type::getExtendedType() const {
718 assert(
720 "Original type expected to be a vector of integers or a scalar integer.");
721 if (auto *VTy = dyn_cast<VectorType>(this))
723 const_cast<VectorType *>(VTy));
724 return cast<IntegerType>(this)->getExtendedType();
725}
726
727Type *Type::getWithNewType(Type *EltTy) const {
728 if (auto *VTy = dyn_cast<VectorType>(this))
729 return VectorType::get(EltTy, VTy->getElementCount());
730 return EltTy;
731}
732
733Type *Type::getWithNewBitWidth(unsigned NewBitWidth) const {
734 assert(
736 "Original type expected to be a vector of integers or a scalar integer.");
737 return getWithNewType(getIntNTy(getContext(), NewBitWidth));
738}
739
740unsigned Type::getPointerAddressSpace() const {
741 return cast<PointerType>(getScalarType())->getAddressSpace();
742}
743
744/// Class to represent target extensions types, which are generally
745/// unintrospectable from target-independent optimizations.
746///
747/// Target extension types have a string name, and optionally have type and/or
748/// integer parameters. The exact meaning of any parameters is dependent on the
749/// target.
750class TargetExtType : public Type {
752 ArrayRef<unsigned> Ints);
753
754 // These strings are ultimately owned by the context.
755 StringRef Name;
756 unsigned *IntParams;
757
758public:
759 TargetExtType(const TargetExtType &) = delete;
761
762 /// Return a target extension type having the specified name and optional
763 /// type and integer parameters.
764 static TargetExtType *get(LLVMContext &Context, StringRef Name,
765 ArrayRef<Type *> Types = std::nullopt,
766 ArrayRef<unsigned> Ints = std::nullopt);
767
768 /// Return the name for this target extension type. Two distinct target
769 /// extension types may have the same name if their type or integer parameters
770 /// differ.
771 StringRef getName() const { return Name; }
772
773 /// Return the type parameters for this particular target extension type. If
774 /// there are no parameters, an empty array is returned.
777 }
778
783 }
784
785 Type *getTypeParameter(unsigned i) const { return getContainedType(i); }
786 unsigned getNumTypeParameters() const { return getNumContainedTypes(); }
787
788 /// Return the integer parameters for this particular target extension type.
789 /// If there are no parameters, an empty array is returned.
791 return ArrayRef(IntParams, getNumIntParameters());
792 }
793
794 unsigned getIntParameter(unsigned i) const { return IntParams[i]; }
795 unsigned getNumIntParameters() const { return getSubclassData(); }
796
797 enum Property {
798 /// zeroinitializer is valid for this target extension type.
799 HasZeroInit = 1U << 0,
800 /// This type may be used as the value type of a global variable.
801 CanBeGlobal = 1U << 1,
802 };
803
804 /// Returns true if the target extension type contains the given property.
805 bool hasProperty(Property Prop) const;
806
807 /// Returns an underlying layout type for the target extension type. This
808 /// type can be used to query size and alignment information, if it is
809 /// appropriate (although note that the layout type may also be void). It is
810 /// not legal to bitcast between this type and the layout type, however.
811 Type *getLayoutType() const;
812
813 /// Methods for support type inquiry through isa, cast, and dyn_cast.
814 static bool classof(const Type *T) { return T->getTypeID() == TargetExtTyID; }
815};
816
817StringRef Type::getTargetExtName() const {
818 return cast<TargetExtType>(this)->getName();
819}
820
821} // end namespace llvm
822
823#endif // LLVM_IR_DERIVEDTYPES_H
amdgpu Simplify well known AMD library false FunctionCallee Callee
return RetTy
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Given that RA is a live value
std::string Name
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
Class for arbitrary precision integers.
Definition: APInt.h:75
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Class to represent array types.
Definition: DerivedTypes.h:368
uint64_t getNumElements() const
Definition: DerivedTypes.h:380
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition: Type.cpp:720
ArrayType & operator=(const ArrayType &)=delete
ArrayType(const ArrayType &)=delete
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:390
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:708
Type * getElementType() const
Definition: DerivedTypes.h:381
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition: TypeSize.h:297
Class to represent fixed width SIMD vectors.
Definition: DerivedTypes.h:536
unsigned getNumElements() const
Definition: DerivedTypes.h:579
static FixedVectorType * getDoubleElementsVectorType(FixedVectorType *VTy)
Definition: DerivedTypes.h:571
static FixedVectorType * getInteger(FixedVectorType *VTy)
Definition: DerivedTypes.h:548
static FixedVectorType * getSubdividedVectorType(FixedVectorType *VTy, int NumSubdivs)
Definition: DerivedTypes.h:561
static FixedVectorType * getExtendedElementVectorType(FixedVectorType *VTy)
Definition: DerivedTypes.h:552
FixedVectorType(Type *ElTy, unsigned NumElts)
Definition: DerivedTypes.h:538
static FixedVectorType * get(Type *ElementType, const FixedVectorType *FVTy)
Definition: DerivedTypes.h:544
static FixedVectorType * getTruncatedElementVectorType(FixedVectorType *VTy)
Definition: DerivedTypes.h:556
static bool classof(const Type *T)
Definition: DerivedTypes.h:575
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition: Type.cpp:754
static FixedVectorType * getHalfElementsVectorType(FixedVectorType *VTy)
Definition: DerivedTypes.h:567
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:165
FunctionCallee(std::nullptr_t)
Definition: DerivedTypes.h:178
FunctionType * getFunctionType()
Definition: DerivedTypes.h:182
FunctionCallee()=default
FunctionCallee(FunctionType *FnTy, Value *Callee)
Definition: DerivedTypes.h:173
Class to represent function types.
Definition: DerivedTypes.h:103
param_iterator param_begin() const
Definition: DerivedTypes.h:128
static bool isValidArgumentType(Type *ArgTy)
Return true if the specified type is valid as an argument type.
Definition: Type.cpp:424
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
Type::subtype_iterator param_iterator
Definition: DerivedTypes.h:126
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:135
static bool isValidReturnType(Type *RetTy)
Return true if the specified type is valid as a return type.
Definition: Type.cpp:419
FunctionType(const FunctionType &)=delete
ArrayRef< Type * > params() const
Definition: DerivedTypes.h:130
FunctionType & operator=(const FunctionType &)=delete
bool isVarArg() const
Definition: DerivedTypes.h:123
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:142
Type * getReturnType() const
Definition: DerivedTypes.h:124
param_iterator param_end() const
Definition: DerivedTypes.h:129
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Class to represent integer types.
Definition: DerivedTypes.h:40
@ MIN_INT_BITS
Minimum number of bits that can be specified.
Definition: DerivedTypes.h:51
@ MAX_INT_BITS
Maximum number of bits that can be specified.
Definition: DerivedTypes.h:52
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:339
uint64_t getSignBit() const
Return a uint64_t with just the most significant bit set (the sign bit, if the value is treated as a ...
Definition: DerivedTypes.h:82
APInt getMask() const
For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
Definition: Type.cpp:363
IntegerType * getExtendedType() const
Returns type twice as wide the input type.
Definition: DerivedTypes.h:67
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:72
uint64_t getBitMask() const
Return a bitmask with ones set for all of the bits that can be set by an unsigned version of this typ...
Definition: DerivedTypes.h:76
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:92
IntegerType(LLVMContext &C, unsigned NumBits)
Definition: DerivedTypes.h:44
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Class to represent pointers.
Definition: DerivedTypes.h:643
static bool isLoadableOrStorableType(Type *ElemTy)
Return true if we can load or store from a pointer to this type.
Definition: Type.cpp:851
bool isOpaqueOrPointeeTypeMatches(Type *Ty)
Return true if either this is an opaque pointer type or if this pointee type matches Ty.
Definition: DerivedTypes.h:699
PointerType(const PointerType &)=delete
static PointerType * getUnqual(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
Definition: DerivedTypes.h:668
bool hasSameElementTypeAs(PointerType *Other)
Return true if both pointer types have the same element type.
Definition: DerivedTypes.h:707
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static bool classof(const Type *T)
Implement support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:712
bool isOpaque() const
Definition: DerivedTypes.h:684
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition: Type.cpp:845
PointerType & operator=(const PointerType &)=delete
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:662
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:693
static PointerType * getWithSamePointeeType(PointerType *PT, unsigned AddressSpace)
This constructs a pointer type with the same pointee type as input PointerType (or opaque pointer if ...
Definition: DerivedTypes.h:677
Class to represent scalable SIMD vectors.
Definition: DerivedTypes.h:583
static ScalableVectorType * get(Type *ElementType, const ScalableVectorType *SVTy)
Definition: DerivedTypes.h:591
static ScalableVectorType * getInteger(ScalableVectorType *VTy)
Definition: DerivedTypes.h:596
static ScalableVectorType * get(Type *ElementType, unsigned MinNumElts)
Definition: Type.cpp:775
static bool classof(const Type *T)
Definition: DerivedTypes.h:633
static ScalableVectorType * getExtendedElementVectorType(ScalableVectorType *VTy)
Definition: DerivedTypes.h:601
static ScalableVectorType * getHalfElementsVectorType(ScalableVectorType *VTy)
Definition: DerivedTypes.h:619
uint64_t getMinNumElements() const
Get the minimum number of elements in this vector.
Definition: DerivedTypes.h:631
static ScalableVectorType * getSubdividedVectorType(ScalableVectorType *VTy, int NumSubdivs)
Definition: DerivedTypes.h:612
static ScalableVectorType * getDoubleElementsVectorType(ScalableVectorType *VTy)
Definition: DerivedTypes.h:624
ScalableVectorType(Type *ElTy, unsigned MinNumElts)
Definition: DerivedTypes.h:585
static ScalableVectorType * getTruncatedElementVectorType(ScalableVectorType *VTy)
Definition: DerivedTypes.h:607
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:344
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Class to represent struct types.
Definition: DerivedTypes.h:213
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:350
static std::enable_if_t< are_base_of< Type, Tys... >::value, StructType * > create(StringRef Name, Type *elt1, Tys *... elts)
Definition: DerivedTypes.h:248
bool indexValid(const Value *V) const
Definition: Type.cpp:679
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:434
element_iterator element_end() const
Definition: DerivedTypes.h:329
StructType(const StructType &)=delete
ArrayRef< Type * > elements() const
Definition: DerivedTypes.h:330
void setBody(ArrayRef< Type * > Elements, bool isPacked=false)
Specify a body for an opaque identified type.
Definition: Type.cpp:506
bool containsHomogeneousScalableVectorTypes() const
Returns true if this struct contains homogeneous scalable vector types.
Definition: Type.cpp:496
element_iterator element_begin() const
Definition: DerivedTypes.h:328
static StructType * getTypeByName(LLVMContext &C, StringRef Name)
Return the type with the specified name, or null if there is none by that name.
Definition: Type.cpp:693
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:574
bool isPacked() const
Definition: DerivedTypes.h:275
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition: Type.cpp:658
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:338
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
isSized - Return true if this is a sized type.
Definition: Type.cpp:613
bool containsScalableVectorType(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Returns true if this struct contains a scalable vector.
Definition: Type.cpp:461
Type * getTypeAtIndex(unsigned N) const
Definition: DerivedTypes.h:345
StructType & operator=(const StructType &)=delete
void setName(StringRef Name)
Change the name of this type to the specified name, or to a name with a suffix if there is a collisio...
Definition: Type.cpp:523
bool isLayoutIdentical(StructType *Other) const
Return true if this is layout identical to the specified struct.
Definition: Type.cpp:664
Type * getTypeAtIndex(const Value *V) const
Given an index value into the type, return the type of the element.
Definition: Type.cpp:673
bool hasName() const
Return true if this is a named struct that has a non-empty name.
Definition: DerivedTypes.h:301
bool isLiteral() const
Return true if this type is uniqued by structural equivalence, false if it is a struct definition.
Definition: DerivedTypes.h:279
bool indexValid(unsigned Idx) const
Definition: DerivedTypes.h:347
bool isOpaque() const
Return true if this is a type with an identity that has no body specified yet.
Definition: DerivedTypes.h:283
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:339
Type::subtype_iterator element_iterator
Definition: DerivedTypes.h:326
static std::enable_if_t< are_base_of< Type, Tys... >::value, StructType * > get(Type *elt1, Tys *... elts)
This static method is a convenience method for creating structure types by specifying the elements as...
Definition: DerivedTypes.h:265
std::enable_if_t< are_base_of< Type, Tys... >::value, void > setBody(Type *elt1, Tys *... elts)
Definition: DerivedTypes.h:317
StringRef getName() const
Return the name for this struct type if it has an identity.
Definition: Type.cpp:651
Symbol info for RuntimeDyld.
Class to represent target extensions types, which are generally unintrospectable from target-independ...
Definition: DerivedTypes.h:750
ArrayRef< Type * > type_params() const
Return the type parameters for this particular target extension type.
Definition: DerivedTypes.h:775
unsigned getNumIntParameters() const
Definition: DerivedTypes.h:795
static TargetExtType * get(LLVMContext &Context, StringRef Name, ArrayRef< Type * > Types=std::nullopt, ArrayRef< unsigned > Ints=std::nullopt)
Return a target extension type having the specified name and optional type and integer parameters.
Definition: Type.cpp:877
type_param_iterator type_param_end() const
Definition: DerivedTypes.h:781
Type::subtype_iterator type_param_iterator
Definition: DerivedTypes.h:779
Type * getTypeParameter(unsigned i) const
Definition: DerivedTypes.h:785
unsigned getNumTypeParameters() const
Definition: DerivedTypes.h:786
ArrayRef< unsigned > int_params() const
Return the integer parameters for this particular target extension type.
Definition: DerivedTypes.h:790
type_param_iterator type_param_begin() const
Definition: DerivedTypes.h:780
unsigned getIntParameter(unsigned i) const
Definition: DerivedTypes.h:794
TargetExtType(const TargetExtType &)=delete
bool hasProperty(Property Prop) const
Returns true if the target extension type contains the given property.
Definition: Type.cpp:933
TargetExtType & operator=(const TargetExtType &)=delete
@ HasZeroInit
zeroinitializer is valid for this target extension type.
Definition: DerivedTypes.h:799
@ CanBeGlobal
This type may be used as the value type of a global variable.
Definition: DerivedTypes.h:801
StringRef getName() const
Return the name for this target extension type.
Definition: DerivedTypes.h:771
Type * getLayoutType() const
Returns an underlying layout type for the target extension type.
Definition: Type.cpp:929
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:814
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Type * getHalfTy(LLVMContext &C)
unsigned getIntegerBitWidth() const
Type * getStructElementType(unsigned N) const
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:235
StringRef getStructName() const
Type *const * subtype_iterator
Definition: Type.h:357
unsigned getStructNumElements() const
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
uint64_t getArrayNumElements() const
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
@ FunctionTyID
Functions.
Definition: Type.h:72
@ ArrayTyID
Arrays.
Definition: Type.h:75
@ TargetExtTyID
Target extension type.
Definition: Type.h:79
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition: Type.h:77
@ FloatTyID
32-bit floating point type
Definition: Type.h:58
@ StructTyID
Structures.
Definition: Type.h:74
@ IntegerTyID
Arbitrary bit width integers.
Definition: Type.h:71
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition: Type.h:76
@ DoubleTyID
64-bit floating point type
Definition: Type.h:59
@ PointerTyID
Pointers.
Definition: Type.h:73
unsigned getNumContainedTypes() const
Return the number of types in the derived type.
Definition: Type.h:383
unsigned NumContainedTys
Keeps track of how many Type*'s there are in the ContainedTys list.
Definition: Type.h:107
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
StringRef getTargetExtName() const
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Type * getWithNewBitWidth(unsigned NewBitWidth) const
Given an integer or vector type, change the lane bitwidth to NewBitwidth, whilst keeping the old numb...
Type * getWithNewType(Type *EltTy) const
Given vector type, change the element type, whilst keeping the old number of elements.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:129
Type *const * ContainedTys
A pointer to the array of Types contained by this Type.
Definition: Type.h:114
unsigned getSubclassData() const
Definition: Type.h:98
bool isFunctionVarArg() const
void setSubclassData(unsigned val)
Definition: Type.h:100
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition: Type.h:185
Type * getExtendedType() const
Given scalar/vector integer type, returns a type with elements twice as wide as in the original type.
static Type * getFloatTy(LLVMContext &C)
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:137
Type * getFunctionParamType(unsigned i) const
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Type * getContainedType(unsigned i) const
This method is used to implement the type iterator (defined at the end of the file).
Definition: Type.h:377
unsigned getFunctionNumParams() const
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:348
LLVM Value Representation.
Definition: Value.h:74
Base class of all SIMD vector types.
Definition: DerivedTypes.h:400
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition: Type.cpp:745
static VectorType * getHalfElementsVectorType(VectorType *VTy)
This static method returns a VectorType with half as many elements as the input type and the same ele...
Definition: DerivedTypes.h:504
static VectorType * getExtendedElementVectorType(VectorType *VTy)
This static method is like getInteger except that the element types are twice as wide as the elements...
Definition: DerivedTypes.h:460
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:529
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Definition: DerivedTypes.h:638
static VectorType * getSubdividedVectorType(VectorType *VTy, int NumSubdivs)
Definition: DerivedTypes.h:494
static VectorType * getInteger(VectorType *VTy)
This static method gets a VectorType with the same number of elements as the input type,...
Definition: DerivedTypes.h:451
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:738
const unsigned ElementQuantity
The element quantity of this vector.
Definition: DerivedTypes.h:425
static VectorType * get(Type *ElementType, const VectorType *Other)
Definition: DerivedTypes.h:444
static VectorType * getTruncatedElementVectorType(VectorType *VTy)
Definition: DerivedTypes.h:469
VectorType & operator=(const VectorType &)=delete
static VectorType * getDoubleElementsVectorType(VectorType *VTy)
This static method returns a VectorType with twice as many elements as the input type and the same el...
Definition: DerivedTypes.h:514
static VectorType * get(Type *ElementType, unsigned NumElements, bool Scalable)
Definition: DerivedTypes.h:438
VectorType(const VectorType &)=delete
Type * getElementType() const
Definition: DerivedTypes.h:433
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:48
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::conjunction< std::is_base_of< T, Ts >... > are_base_of
traits class for checking whether type T is a base class for all the given types in the variadic list...
Definition: STLExtras.h:218
AddressSpace
Definition: NVPTXBaseInfo.h:21
#define N
#define EQ(a, b)
Definition: regexec.c:112