1 //===------- VectorTypeUtils.cpp - Vector type utility functions ----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/VectorTypeUtils.h"
10 #include "llvm/ADT/SmallVectorExtras.h"
14 /// A helper for converting structs of scalar types to structs of vector types.
15 /// Note: Only unpacked literal struct types are supported.
16 Type
*llvm::toVectorizedStructTy(StructType
*StructTy
, ElementCount EC
) {
19 assert(isUnpackedStructLiteral(StructTy
) &&
20 "expected unpacked struct literal");
21 assert(all_of(StructTy
->elements(), VectorType::isValidElementType
) &&
22 "expected all element types to be valid vector element types");
23 return StructType::get(
24 StructTy
->getContext(),
25 map_to_vector(StructTy
->elements(), [&](Type
*ElTy
) -> Type
* {
26 return VectorType::get(ElTy
, EC
);
30 /// A helper for converting structs of vector types to structs of scalar types.
31 /// Note: Only unpacked literal struct types are supported.
32 Type
*llvm::toScalarizedStructTy(StructType
*StructTy
) {
33 assert(isUnpackedStructLiteral(StructTy
) &&
34 "expected unpacked struct literal");
35 return StructType::get(
36 StructTy
->getContext(),
37 map_to_vector(StructTy
->elements(), [](Type
*ElTy
) -> Type
* {
38 return ElTy
->getScalarType();
42 /// Returns true if `StructTy` is an unpacked literal struct where all elements
43 /// are vectors of matching element count. This does not include empty structs.
44 bool llvm::isVectorizedStructTy(StructType
*StructTy
) {
45 if (!isUnpackedStructLiteral(StructTy
))
47 auto ElemTys
= StructTy
->elements();
48 if (ElemTys
.empty() || !ElemTys
.front()->isVectorTy())
50 ElementCount VF
= cast
<VectorType
>(ElemTys
.front())->getElementCount();
51 return all_of(ElemTys
, [&](Type
*Ty
) {
52 return Ty
->isVectorTy() && cast
<VectorType
>(Ty
)->getElementCount() == VF
;
56 /// Returns true if `StructTy` is an unpacked literal struct where all elements
57 /// are scalars that can be used as vector element types.
58 bool llvm::canVectorizeStructTy(StructType
*StructTy
) {
59 auto ElemTys
= StructTy
->elements();
60 return !ElemTys
.empty() && isUnpackedStructLiteral(StructTy
) &&
61 all_of(ElemTys
, VectorType::isValidElementType
);