Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:11

0001 //===------- VectorTypeUtils.h - Vector type utility functions -*- C++ -*-====//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_IR_VECTORTYPEUTILS_H
0010 #define LLVM_IR_VECTORTYPEUTILS_H
0011 
0012 #include "llvm/IR/DerivedTypes.h"
0013 
0014 namespace llvm {
0015 
0016 /// A helper function for converting Scalar types to vector types. If
0017 /// the incoming type is void, we return void. If the EC represents a
0018 /// scalar, we return the scalar type.
0019 inline Type *toVectorTy(Type *Scalar, ElementCount EC) {
0020   if (Scalar->isVoidTy() || Scalar->isMetadataTy() || EC.isScalar())
0021     return Scalar;
0022   return VectorType::get(Scalar, EC);
0023 }
0024 
0025 inline Type *toVectorTy(Type *Scalar, unsigned VF) {
0026   return toVectorTy(Scalar, ElementCount::getFixed(VF));
0027 }
0028 
0029 /// A helper for converting structs of scalar types to structs of vector types.
0030 /// Note:
0031 ///   - If \p EC is scalar, \p StructTy is returned unchanged
0032 ///   - Only unpacked literal struct types are supported
0033 Type *toVectorizedStructTy(StructType *StructTy, ElementCount EC);
0034 
0035 /// A helper for converting structs of vector types to structs of scalar types.
0036 /// Note: Only unpacked literal struct types are supported.
0037 Type *toScalarizedStructTy(StructType *StructTy);
0038 
0039 /// Returns true if `StructTy` is an unpacked literal struct where all elements
0040 /// are vectors of matching element count. This does not include empty structs.
0041 bool isVectorizedStructTy(StructType *StructTy);
0042 
0043 /// Returns true if `StructTy` is an unpacked literal struct where all elements
0044 /// are scalars that can be used as vector element types.
0045 bool canVectorizeStructTy(StructType *StructTy);
0046 
0047 /// A helper for converting to vectorized types. For scalar types, this is
0048 /// equivalent to calling `toVectorTy`. For struct types, this returns a new
0049 /// struct where each element type has been widened to a vector type.
0050 /// Note:
0051 ///   - If the incoming type is void, we return void
0052 ///   - If \p EC is scalar, \p Ty is returned unchanged
0053 ///   - Only unpacked literal struct types are supported
0054 inline Type *toVectorizedTy(Type *Ty, ElementCount EC) {
0055   if (StructType *StructTy = dyn_cast<StructType>(Ty))
0056     return toVectorizedStructTy(StructTy, EC);
0057   return toVectorTy(Ty, EC);
0058 }
0059 
0060 /// A helper for converting vectorized types to scalarized (non-vector) types.
0061 /// For vector types, this is equivalent to calling .getScalarType(). For struct
0062 /// types, this returns a new struct where each element type has been converted
0063 /// to a scalar type. Note: Only unpacked literal struct types are supported.
0064 inline Type *toScalarizedTy(Type *Ty) {
0065   if (StructType *StructTy = dyn_cast<StructType>(Ty))
0066     return toScalarizedStructTy(StructTy);
0067   return Ty->getScalarType();
0068 }
0069 
0070 /// Returns true if `Ty` is a vector type or a struct of vector types where all
0071 /// vector types share the same VF.
0072 inline bool isVectorizedTy(Type *Ty) {
0073   if (StructType *StructTy = dyn_cast<StructType>(Ty))
0074     return isVectorizedStructTy(StructTy);
0075   return Ty->isVectorTy();
0076 }
0077 
0078 /// Returns true if `Ty` is a valid vector element type, void, or an unpacked
0079 /// literal struct where all elements are valid vector element types.
0080 /// Note: Even if a type can be vectorized that does not mean it is valid to do
0081 /// so in all cases. For example, a vectorized struct (as returned by
0082 /// toVectorizedTy) does not perform (de)interleaving, so it can't be used for
0083 /// vectorizing loads/stores.
0084 inline bool canVectorizeTy(Type *Ty) {
0085   if (StructType *StructTy = dyn_cast<StructType>(Ty))
0086     return canVectorizeStructTy(StructTy);
0087   return Ty->isVoidTy() || VectorType::isValidElementType(Ty);
0088 }
0089 
0090 /// Returns the types contained in `Ty`. For struct types, it returns the
0091 /// elements, all other types are returned directly.
0092 inline ArrayRef<Type *> getContainedTypes(Type *const &Ty) {
0093   if (auto *StructTy = dyn_cast<StructType>(Ty))
0094     return StructTy->elements();
0095   return ArrayRef<Type *>(&Ty, 1);
0096 }
0097 
0098 /// Returns the number of vector elements for a vectorized type.
0099 inline ElementCount getVectorizedTypeVF(Type *Ty) {
0100   assert(isVectorizedTy(Ty) && "expected vectorized type");
0101   return cast<VectorType>(getContainedTypes(Ty).front())->getElementCount();
0102 }
0103 
0104 inline bool isUnpackedStructLiteral(StructType *StructTy) {
0105   return StructTy->isLiteral() && !StructTy->isPacked();
0106 }
0107 
0108 } // namespace llvm
0109 
0110 #endif