LLVM 24.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
12#include "llvm/IR/DataLayout.h"
15
16namespace llvm {
17
18/// A helper function for converting Scalar types to vector types. If
19/// the incoming type is void, we return void. If the EC represents a
20/// scalar, we return the scalar type.
21inline Type *toVectorTy(Type *Scalar, ElementCount EC) {
22 if (Scalar->isVoidTy() || Scalar->isMetadataTy() || EC.isScalar())
23 return Scalar;
24 return VectorType::get(Scalar, EC);
25}
26
27inline Type *toVectorTy(Type *Scalar, unsigned VF) {
28 return toVectorTy(Scalar, ElementCount::getFixed(VF));
29}
30
31/// A helper for converting structs of scalar types to structs of vector types.
32/// Note:
33/// - If \p EC is scalar, \p StructTy is returned unchanged
34/// - Only unpacked literal struct types are supported
35LLVM_ABI Type *toVectorizedStructTy(StructType *StructTy, ElementCount EC);
36
37/// A helper for converting structs of vector types to structs of scalar types.
38/// Note: Only unpacked literal struct types are supported.
39LLVM_ABI Type *toScalarizedStructTy(StructType *StructTy);
40
41/// Returns true if `StructTy` is an unpacked literal struct where all elements
42/// are vectors of matching element count. This does not include empty structs.
43LLVM_ABI bool isVectorizedStructTy(StructType *StructTy);
44
45/// Returns true if `StructTy` is an unpacked literal struct where all elements
46/// are scalars that can be used as vector element types.
47LLVM_ABI bool canVectorizeStructTy(StructType *StructTy);
48
49/// A helper for converting to vectorized types. For scalar types, this is
50/// equivalent to calling `toVectorTy`. For struct types, this returns a new
51/// struct where each element type has been widened to a vector type.
52/// Note:
53/// - If the incoming type is void, we return void
54/// - If \p EC is scalar, \p Ty is returned unchanged
55/// - Only unpacked literal struct types are supported
57 if (StructType *StructTy = dyn_cast<StructType>(Ty))
58 return toVectorizedStructTy(StructTy, EC);
59 return toVectorTy(Ty, EC);
60}
61
62/// A helper for converting vectorized types to scalarized (non-vector) types.
63/// For vector types, this is equivalent to calling .getScalarType(). For struct
64/// types, this returns a new struct where each element type has been converted
65/// to a scalar type. Note: Only unpacked literal struct types are supported.
66inline Type *toScalarizedTy(Type *Ty) {
67 if (StructType *StructTy = dyn_cast<StructType>(Ty))
68 return toScalarizedStructTy(StructTy);
69 return Ty->getScalarType();
70}
71
72/// Returns true if `Ty` is a vector type or a struct of vector types where all
73/// vector types share the same VF.
74inline bool isVectorizedTy(Type *Ty) {
75 if (StructType *StructTy = dyn_cast<StructType>(Ty))
76 return isVectorizedStructTy(StructTy);
77 return Ty->isVectorTy();
78}
79
80/// Returns true if `Ty` is a valid vector element type, void, or an unpacked
81/// literal struct where all elements are valid vector element types.
82/// Note: Even if a type can be vectorized that does not mean it is valid to do
83/// so in all cases. For example, a vectorized struct (as returned by
84/// toVectorizedTy) does not perform (de)interleaving, so it can't be used for
85/// vectorizing loads/stores.
86inline bool canVectorizeTy(Type *Ty) {
87 if (StructType *StructTy = dyn_cast<StructType>(Ty))
88 return canVectorizeStructTy(StructTy);
89 return Ty->isVoidTy() || VectorType::isValidElementType(Ty);
90}
91
92/// Returns the types contained in `Ty`. For struct types, it returns the
93/// elements, all other types are returned directly.
95 if (auto *StructTy = dyn_cast<StructType>(Ty))
96 return StructTy->elements();
97 return ArrayRef<Type *>(&Ty, 1);
98}
99
100/// Returns the number of vector elements for a vectorized type.
102 assert(isVectorizedTy(Ty) && "expected vectorized type");
103 return cast<VectorType>(getContainedTypes(Ty).front())->getElementCount();
104}
105
106inline bool isUnpackedStructLiteral(StructType *StructTy) {
107 return StructTy->isLiteral() && !StructTy->isPacked();
108}
109
110/// A helper function that returns true if the given type is irregular. The
111/// type is irregular if its allocated size doesn't equal the store size of an
112/// element of the corresponding vector type.
113inline bool hasIrregularType(Type *Ty, const DataLayout &DL) {
114 // Determine if an array of N elements of type Ty is "bitcast compatible"
115 // with a <N x Ty> vector.
116 // This is only true if there is no padding between the array elements.
117 return DL.getTypeAllocSizeInBits(Ty) != DL.getTypeSizeInBits(Ty);
118}
119
120} // namespace llvm
121
122#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_ABI
Definition Compiler.h:211
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:306
Class to represent struct types.
bool isPacked() const
bool isLiteral() const
Return true if this type is uniqued by structural equivalence, false if it is a struct definition.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
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.
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.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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.
bool hasIrregularType(Type *Ty, const DataLayout &DL)
A helper function that returns true if the given type is irregular.
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(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
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...