LLVM 22.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
14
15namespace llvm {
16
17/// A helper function for converting Scalar types to vector types. If
18/// the incoming type is void, we return void. If the EC represents a
19/// scalar, we return the scalar type.
20inline Type *toVectorTy(Type *Scalar, ElementCount EC) {
21 if (Scalar->isVoidTy() || Scalar->isMetadataTy() || EC.isScalar())
22 return Scalar;
23 return VectorType::get(Scalar, EC);
24}
25
26inline Type *toVectorTy(Type *Scalar, unsigned VF) {
27 return toVectorTy(Scalar, ElementCount::getFixed(VF));
28}
29
30/// A helper for converting structs of scalar types to structs of vector types.
31/// Note:
32/// - If \p EC is scalar, \p StructTy is returned unchanged
33/// - Only unpacked literal struct types are supported
34LLVM_ABI Type *toVectorizedStructTy(StructType *StructTy, ElementCount EC);
35
36/// A helper for converting structs of vector types to structs of scalar types.
37/// Note: Only unpacked literal struct types are supported.
38LLVM_ABI Type *toScalarizedStructTy(StructType *StructTy);
39
40/// Returns true if `StructTy` is an unpacked literal struct where all elements
41/// are vectors of matching element count. This does not include empty structs.
42LLVM_ABI bool isVectorizedStructTy(StructType *StructTy);
43
44/// Returns true if `StructTy` is an unpacked literal struct where all elements
45/// are scalars that can be used as vector element types.
46LLVM_ABI bool canVectorizeStructTy(StructType *StructTy);
47
48/// A helper for converting to vectorized types. For scalar types, this is
49/// equivalent to calling `toVectorTy`. For struct types, this returns a new
50/// struct where each element type has been widened to a vector type.
51/// Note:
52/// - If the incoming type is void, we return void
53/// - If \p EC is scalar, \p Ty is returned unchanged
54/// - Only unpacked literal struct types are supported
56 if (StructType *StructTy = dyn_cast<StructType>(Ty))
57 return toVectorizedStructTy(StructTy, EC);
58 return toVectorTy(Ty, EC);
59}
60
61/// A helper for converting vectorized types to scalarized (non-vector) types.
62/// For vector types, this is equivalent to calling .getScalarType(). For struct
63/// types, this returns a new struct where each element type has been converted
64/// to a scalar type. Note: Only unpacked literal struct types are supported.
65inline Type *toScalarizedTy(Type *Ty) {
66 if (StructType *StructTy = dyn_cast<StructType>(Ty))
67 return toScalarizedStructTy(StructTy);
68 return Ty->getScalarType();
69}
70
71/// Returns true if `Ty` is a vector type or a struct of vector types where all
72/// vector types share the same VF.
73inline bool isVectorizedTy(Type *Ty) {
74 if (StructType *StructTy = dyn_cast<StructType>(Ty))
75 return isVectorizedStructTy(StructTy);
76 return Ty->isVectorTy();
77}
78
79/// Returns true if `Ty` is a valid vector element type, void, or an unpacked
80/// literal struct where all elements are valid vector element types.
81/// Note: Even if a type can be vectorized that does not mean it is valid to do
82/// so in all cases. For example, a vectorized struct (as returned by
83/// toVectorizedTy) does not perform (de)interleaving, so it can't be used for
84/// vectorizing loads/stores.
85inline bool canVectorizeTy(Type *Ty) {
86 if (StructType *StructTy = dyn_cast<StructType>(Ty))
87 return canVectorizeStructTy(StructTy);
88 return Ty->isVoidTy() || VectorType::isValidElementType(Ty);
89}
90
91/// Returns the types contained in `Ty`. For struct types, it returns the
92/// elements, all other types are returned directly.
94 if (auto *StructTy = dyn_cast<StructType>(Ty))
95 return StructTy->elements();
96 return ArrayRef<Type *>(&Ty, 1);
97}
98
99/// Returns the number of vector elements for a vectorized type.
101 assert(isVectorizedTy(Ty) && "expected vectorized type");
102 return cast<VectorType>(getContainedTypes(Ty).front())->getElementCount();
103}
104
105inline bool isUnpackedStructLiteral(StructType *StructTy) {
106 return StructTy->isLiteral() && !StructTy->isPacked();
107}
108
109} // namespace llvm
110
111#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
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:312
Class to represent struct types.
Definition: DerivedTypes.h:218
bool isPacked() const
Definition: DerivedTypes.h:286
bool isLiteral() const
Return true if this type is uniqued by structural equivalence, false if it is a struct definition.
Definition: DerivedTypes.h:290
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:273
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:352
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
static LLVM_ABI 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
LLVM_ABI 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.
LLVM_ABI 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.
LLVM_ABI Type * toScalarizedStructTy(StructType *StructTy)
A helper for converting structs of vector types to structs of scalar types.
LLVM_ABI bool isVectorizedStructTy(StructType *StructTy)
Returns true if StructTy is an unpacked literal struct where all elements are vectors of matching ele...