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/// A helper for converting to vectorized types. For scalar types, this is
44/// equivalent to calling `ToVectorTy`. For struct types, this returns a new
45/// struct where each element type has been widened to a vector type.
46/// Note:
47/// - If the incoming type is void, we return void
48/// - If \p EC is scalar, \p Ty is returned unchanged
49/// - Only unpacked literal struct types are supported
51 if (StructType *StructTy = dyn_cast<StructType>(Ty))
52 return toVectorizedStructTy(StructTy, EC);
53 return ToVectorTy(Ty, EC);
54}
55
56/// A helper for converting vectorized types to scalarized (non-vector) types.
57/// For vector types, this is equivalent to calling .getScalarType(). For struct
58/// types, this returns a new struct where each element type has been converted
59/// to a scalar type. Note: Only unpacked literal struct types are supported.
60inline Type *toScalarizedTy(Type *Ty) {
61 if (StructType *StructTy = dyn_cast<StructType>(Ty))
62 return toScalarizedStructTy(StructTy);
63 return Ty->getScalarType();
64}
65
66/// Returns true if `Ty` is a vector type or a struct of vector types where all
67/// vector types share the same VF.
68inline bool isVectorizedTy(Type *Ty) {
69 if (StructType *StructTy = dyn_cast<StructType>(Ty))
70 return isVectorizedStructTy(StructTy);
71 return Ty->isVectorTy();
72}
73
74/// Returns the types contained in `Ty`. For struct types, it returns the
75/// elements, all other types are returned directly.
77 if (auto *StructTy = dyn_cast<StructType>(Ty))
78 return StructTy->elements();
79 return ArrayRef<Type *>(&Ty, 1);
80}
81
82/// Returns the number of vector elements for a vectorized type.
84 assert(isVectorizedTy(Ty) && "expected vectorized type");
85 return cast<VectorType>(getContainedTypes(Ty).front())->getElementCount();
86}
87
88inline bool isUnpackedStructLiteral(StructType *StructTy) {
89 return StructTy->isLiteral() && !StructTy->isPacked();
90}
91
92} // namespace llvm
93
94#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
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.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
Type * ToVectorTy(Type *Scalar, ElementCount EC)
A helper function for converting Scalar types to vector types.
ArrayRef< Type * > getContainedTypes(Type *const &Ty)
Returns the types contained in Ty.
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...