[llvm] [ConstantFolding] Fold deinterleave2 of any splat vector not just zeroinitializer (PR #144144)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 13 12:05:11 PDT 2025
https://github.com/topperc created https://github.com/llvm/llvm-project/pull/144144
While there remove an unnecessary dyn_cast from Constant to Constant. Reverse a branch condition into an early out to reduce nesting.
>From c6ae85cd495841fd8fde69aa915828423ac5c3a5 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Fri, 13 Jun 2025 12:02:53 -0700
Subject: [PATCH] [ConstantFolding] Fold deinterleave2 of any splat vector not
just zeroinitializer
While there remove an unnecessary dyn_cast from Constant to Constant.
Reverse a branch condition into an early out to reduce nesting.
---
llvm/lib/Analysis/ConstantFolding.cpp | 43 +++++++++----------
.../InstSimplify/ConstProp/vector-calls.ll | 16 +++++++
2 files changed, 37 insertions(+), 22 deletions(-)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 64a0f4641250c..2b7a438a9ef01 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3990,31 +3990,30 @@ ConstantFoldStructCall(StringRef Name, Intrinsic::ID IntrinsicID,
return ConstantStruct::get(StTy, SinResult, CosResult);
}
case Intrinsic::vector_deinterleave2: {
- auto *Vec = dyn_cast<Constant>(Operands[0]);
- if (!Vec)
- return nullptr;
-
+ auto *Vec = Operands[0];
auto *VecTy = cast<VectorType>(Vec->getType());
- unsigned NumElements = VecTy->getElementCount().getKnownMinValue() / 2;
- if (isa<ConstantAggregateZero>(Vec)) {
- auto *HalfVecTy = VectorType::getHalfElementsVectorType(VecTy);
- return ConstantStruct::get(StTy, ConstantAggregateZero::get(HalfVecTy),
- ConstantAggregateZero::get(HalfVecTy));
+
+ if (auto *EltC = Vec->getSplatValue()) {
+ ElementCount HalfEC = VecTy->getElementCount().divideCoefficientBy(2);
+ auto *HalfVec = ConstantVector::getSplat(HalfEC, EltC);
+ return ConstantStruct::get(StTy, HalfVec, HalfVec);
}
- if (isa<FixedVectorType>(Vec->getType())) {
- SmallVector<Constant *, 4> Res0(NumElements), Res1(NumElements);
- for (unsigned I = 0; I < NumElements; ++I) {
- Constant *Elt0 = Vec->getAggregateElement(2 * I);
- Constant *Elt1 = Vec->getAggregateElement(2 * I + 1);
- if (!Elt0 || !Elt1)
- return nullptr;
- Res0[I] = Elt0;
- Res1[I] = Elt1;
- }
- return ConstantStruct::get(StTy, ConstantVector::get(Res0),
- ConstantVector::get(Res1));
+
+ if (!isa<FixedVectorType>(Vec->getType()))
+ return nullptr;
+
+ unsigned NumElements = VecTy->getElementCount().getFixedValue() / 2;
+ SmallVector<Constant *, 4> Res0(NumElements), Res1(NumElements);
+ for (unsigned I = 0; I < NumElements; ++I) {
+ Constant *Elt0 = Vec->getAggregateElement(2 * I);
+ Constant *Elt1 = Vec->getAggregateElement(2 * I + 1);
+ if (!Elt0 || !Elt1)
+ return nullptr;
+ Res0[I] = Elt0;
+ Res1[I] = Elt1;
}
- return nullptr;
+ return ConstantStruct::get(StTy, ConstantVector::get(Res0),
+ ConstantVector::get(Res1));
}
default:
// TODO: Constant folding of vector intrinsics that fall through here does
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll b/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll
index 9dbe3d4e50ee1..14543f339db5d 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/vector-calls.ll
@@ -66,3 +66,19 @@ define {<vscale x 4 x i32>, <vscale x 4 x i32>} @fold_scalable_vector_deinterlea
%1 = call {<vscale x 4 x i32>, <vscale x 4 x i32>} @llvm.vector.deinterleave2.v4i32.v8i32(<vscale x 8 x i32> zeroinitializer)
ret {<vscale x 4 x i32>, <vscale x 4 x i32>} %1
}
+
+define {<vscale x 4 x i32>, <vscale x 4 x i32>} @fold_scalable_vector_deinterleave2_splat() {
+; CHECK-LABEL: define { <vscale x 4 x i32>, <vscale x 4 x i32> } @fold_scalable_vector_deinterleave2_splat() {
+; CHECK-NEXT: ret { <vscale x 4 x i32>, <vscale x 4 x i32> } { <vscale x 4 x i32> splat (i32 1), <vscale x 4 x i32> splat (i32 1) }
+;
+ %1 = call {<vscale x 4 x i32>, <vscale x 4 x i32>} @llvm.vector.deinterleave2.v4i32.v8i32(<vscale x 8 x i32> splat (i32 1))
+ ret {<vscale x 4 x i32>, <vscale x 4 x i32>} %1
+}
+
+define {<vscale x 4 x float>, <vscale x 4 x float>} @fold_scalable_vector_deinterleave2_splatfp() {
+; CHECK-LABEL: define { <vscale x 4 x float>, <vscale x 4 x float> } @fold_scalable_vector_deinterleave2_splatfp() {
+; CHECK-NEXT: ret { <vscale x 4 x float>, <vscale x 4 x float> } { <vscale x 4 x float> splat (float 1.000000e+00), <vscale x 4 x float> splat (float 1.000000e+00) }
+;
+ %1 = call {<vscale x 4 x float>, <vscale x 4 x float>} @llvm.vector.deinterleave2.v4f32.v8f32(<vscale x 8 x float> splat (float 1.0))
+ ret {<vscale x 4 x float>, <vscale x 4 x float>} %1
+}
More information about the llvm-commits
mailing list