[llvm] [InstCombine] Restrict foldICmpOfVectorReduce to one-use (PR #182833)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 23 03:30:45 PST 2026
https://github.com/artagnon created https://github.com/llvm/llvm-project/pull/182833
Follow up on 279b3dbe ([InstCombine] Fold icmp (vreduce_(or|and) %x), (0|-1), #182684) to fix reported regressions by restricting the fold to one-use.
Regression: https://godbolt.org/z/f38b169MM
>From c8c9da3ce025470ecb8da28215090107daa38179 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 23 Feb 2026 11:23:52 +0000
Subject: [PATCH 1/2] [InstCombine] Pre-commit one-use
icmp-vector-bitwise-reductions test
---
.../icmp-vector-bitwise-reductions.ll | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/icmp-vector-bitwise-reductions.ll b/llvm/test/Transforms/InstCombine/icmp-vector-bitwise-reductions.ll
index 09da979b7e873..088758971f3cd 100644
--- a/llvm/test/Transforms/InstCombine/icmp-vector-bitwise-reductions.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-vector-bitwise-reductions.ll
@@ -99,3 +99,20 @@ define i1 @scalable(<vscale x 4 x i8> %v) {
%cmp = icmp eq i8 %red, -1
ret i1 %cmp
}
+
+define i1 @multiuse(<4 x i16> %v) {
+; CHECK-LABEL: define i1 @multiuse(
+; CHECK-SAME: <4 x i16> [[V:%.*]]) {
+; CHECK-NEXT: [[RED:%.*]] = call i16 @llvm.vector.reduce.or.v4i16(<4 x i16> [[V]])
+; CHECK-NEXT: [[TMP1:%.*]] = bitcast <4 x i16> [[V]] to i64
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i64 [[TMP1]], 0
+; CHECK-NEXT: [[USE:%.*]] = trunc i16 [[RED]] to i1
+; CHECK-NEXT: [[RET:%.*]] = xor i1 [[CMP]], [[USE]]
+; CHECK-NEXT: ret i1 [[RET]]
+;
+ %red = call i16 @llvm.vector.reduce.or.v4i16(<4 x i16> %v)
+ %cmp = icmp ne i16 %red, 0
+ %use = trunc i16 %red to i1
+ %ret = xor i1 %use, %cmp
+ ret i1 %ret
+}
>From 36b204a6a64df72e18faef346d2f484afbe38b4a Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Mon, 23 Feb 2026 11:25:32 +0000
Subject: [PATCH 2/2] [InstCombine] Restrict foldICmpOfVectorReduce to one-use
Follow up on 279b3dbe ([InstCombine] Fold icmp (vreduce_(or|and) %x),
(0|-1), #182684) to fix reported regressions by restricting the fold to
one-use.
Regression: https://godbolt.org/z/f38b169MM
---
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 6 ++++--
.../InstCombine/icmp-vector-bitwise-reductions.ll | 3 +--
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 546b3e11a075f..fbc728bbb9ee3 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7540,9 +7540,11 @@ static Instruction *foldICmpOfVectorReduce(ICmpInst &I, const DataLayout &DL,
// with a bitcast.
Value *Vec;
if ((match(Const, m_ZeroInt()) &&
- match(Op, m_Intrinsic<Intrinsic::vector_reduce_or>(m_Value(Vec)))) ||
+ match(Op, m_OneUse(m_Intrinsic<Intrinsic::vector_reduce_or>(
+ m_Value(Vec))))) ||
(match(Const, m_AllOnes()) &&
- match(Op, m_Intrinsic<Intrinsic::vector_reduce_and>(m_Value(Vec))))) {
+ match(Op, m_OneUse(m_Intrinsic<Intrinsic::vector_reduce_and>(
+ m_Value(Vec)))))) {
auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType());
if (!VecTy)
return nullptr;
diff --git a/llvm/test/Transforms/InstCombine/icmp-vector-bitwise-reductions.ll b/llvm/test/Transforms/InstCombine/icmp-vector-bitwise-reductions.ll
index 088758971f3cd..457ad902ac438 100644
--- a/llvm/test/Transforms/InstCombine/icmp-vector-bitwise-reductions.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-vector-bitwise-reductions.ll
@@ -104,8 +104,7 @@ define i1 @multiuse(<4 x i16> %v) {
; CHECK-LABEL: define i1 @multiuse(
; CHECK-SAME: <4 x i16> [[V:%.*]]) {
; CHECK-NEXT: [[RED:%.*]] = call i16 @llvm.vector.reduce.or.v4i16(<4 x i16> [[V]])
-; CHECK-NEXT: [[TMP1:%.*]] = bitcast <4 x i16> [[V]] to i64
-; CHECK-NEXT: [[CMP:%.*]] = icmp ne i64 [[TMP1]], 0
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne i16 [[RED]], 0
; CHECK-NEXT: [[USE:%.*]] = trunc i16 [[RED]] to i1
; CHECK-NEXT: [[RET:%.*]] = xor i1 [[CMP]], [[USE]]
; CHECK-NEXT: ret i1 [[RET]]
More information about the llvm-commits
mailing list