LLVM 20.0.0git
VectorTypeUtils.h
Go to the documentation of this file.
1//===------- VectorTypeUtils.h - Vector type utility functions -*- 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#ifndef LLVM_IR_VECTORTYPEUTILS_H
10#define LLVM_IR_VECTORTYPEUTILS_H
11
13
14namespace llvm {
15
16/// A helper function for converting Scalar types to vector types. If
17/// the incoming type is void, we return void. If the EC represents a
18/// scalar, we return the scalar type.
19inline Type *toVectorTy(Type *Scalar, ElementCount EC) {
20 if (Scalar->isVoidTy() || Scalar->isMetadataTy() || EC.isScalar())
21 return Scalar;
22 return VectorType::get(Scalar, EC);
23}
24
25inline Type *toVectorTy(Type *Scalar, unsigned VF) {
26 return toVectorTy(Scalar, ElementCount::getFixed(VF));
27}
28
29/// A helper for converting structs of scalar types to structs of vector types.
30/// Note:
31/// - If \p EC is scalar, \p StructTy is returned unchanged
32/// - Only unpacked literal struct types are supported
33Type *toVectorizedStructTy(StructType *StructTy, ElementCount EC);
34
35/// A helper for converting structs of vector types to structs of scalar types.
36/// Note: Only unpacked literal struct types are supported.
37Type *toScalarizedStructTy(StructType *StructTy);
38
39/// Returns true if `StructTy` is an unpacked literal struct where all elements
40/// are vectors of matching element count. This does not include empty structs.
41bool isVectorizedStructTy(StructType *StructTy);
42
43/// Returns true if `StructTy` is an unpacked literal struct where all elements
44/// are scalars that can be used as vector element types.
45bool canVectorizeStructTy(StructType *StructTy);
46
47/// A helper for converting to vectorized types. For scalar types, this is
48/// equivalent to calling `toVectorTy`. For struct types, this returns a new
49/// struct where each element type has been widened to a vector type.
50/// Note:
51/// - If the incoming type is void, we return void
52/// - If \p EC is scalar, \p Ty is returned unchanged
53/// - Only unpacked literal struct types are supported
55 if (StructType *StructTy = dyn_cast<StructType>(Ty))
56 return toVectorizedStructTy(StructTy, EC);
57 return toVectorTy(Ty, EC);
58}
59
60/// A helper for converting vectorized types to scalarized (non-vector) types.
61/// For vector types, this is equivalent to calling .getScalarType(). For struct
62/// types, this returns a new struct where each element type has been converted
63/// to a scalar type. Note: Only unpacked literal struct types are supported.
64inline Type *toScalarizedTy(Type *Ty) {
65 if (StructType *StructTy = dyn_cast<StructType>(Ty))
66 return toScalarizedStructTy(StructTy);
67 return Ty->getScalarType();
68}
69
70/// Returns true if `Ty` is a vector type or a struct of vector types where all
71/// vector types share the same VF.
72inline bool isVectorizedTy(Type *Ty) {
73 if (StructType *StructTy = dyn_cast<StructType>(Ty))
74 return isVectorizedStructTy(StructTy);
75 return Ty->isVectorTy();
76}
77
78/// Returns true if `Ty` is a valid vector element type, void, or an unpacked
79/// literal struct where all elements are valid vector element types.
80/// Note: Even if a type can be vectorized that does not mean it is valid to do
81/// so in all cases. For example, a vectorized struct (as returned by
82/// toVectorizedTy) does not perform (de)interleaving, so it can't be used for
83/// vectorizing loads/stores.
84inline bool canVectorizeTy(Type *Ty) {
85 if (StructType *StructTy = dyn_cast<StructType>(Ty))
86 return canVectorizeStructTy(StructTy);
87 return Ty->isVoidTy() || VectorType::isValidElementType(Ty);
88}
89
90/// Returns the types contained in `Ty`. For struct types, it returns the
91/// elements, all other types are returned directly.
93 if (auto *StructTy = dyn_cast<StructType>(Ty))
94 return StructTy->elements();
95 return ArrayRef<Type *>(&Ty, 1);
96}
97
98/// Returns the number of vector elements for a vectorized type.
100 assert(isVectorizedTy(Ty) && "expected vectorized type");
101 return cast<VectorType>(getContainedTypes(Ty).front())->getElementCount();
102}
103
104inline bool isUnpackedStructLiteral(StructType *StructTy) {
105 return StructTy->isLiteral() && !StructTy->isPacked();
106}
107
108} // namespace llvm
109
110#endif
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition: TypeSize.h:311
Class to represent struct types.
Definition: DerivedTypes.h:218
bool isPacked() const
Definition: DerivedTypes.h:284
bool isLiteral() const
Return true if this type is uniqued by structural equivalence, false if it is a struct definition.
Definition: DerivedTypes.h:288
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:270
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:139
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:355
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool canVectorizeStructTy(StructType *StructTy)
Returns true if StructTy is an unpacked literal struct where all elements are scalars that can be use...
Type * toScalarizedTy(Type *Ty)
A helper for converting vectorized types to scalarized (non-vector) types.
bool isUnpackedStructLiteral(StructType *StructTy)
bool isVectorizedTy(Type *Ty)
Returns true if Ty is a vector type or a struct of vector types where all vector types share the same...
ElementCount getVectorizedTypeVF(Type *Ty)
Returns the number of vector elements for a vectorized type.
Type * toVectorizedStructTy(StructType *StructTy, ElementCount EC)
A helper for converting structs of scalar types to structs of vector types.
Type * toVectorizedTy(Type *Ty, ElementCount EC)
A helper for converting to vectorized types.
bool canVectorizeTy(Type *Ty)
Returns true if Ty is a valid vector element type, void, or an unpacked literal struct where all elem...
ArrayRef< Type * > getContainedTypes(Type *const &Ty)
Returns the types contained in Ty.
Type * toVectorTy(Type *Scalar, ElementCount EC)
A helper function for converting Scalar types to vector types.
Type * toScalarizedStructTy(StructType *StructTy)
A helper for converting structs of vector types to structs of scalar types.
bool isVectorizedStructTy(StructType *StructTy)
Returns true if StructTy is an unpacked literal struct where all elements are vectors of matching ele...