[llvm] [InstCombine] Fold vector.reduce.OP(F(X)) == 0 -> OP(X) == 0 (PR #173069)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 19 10:56:13 PST 2025
================
@@ -3790,6 +3793,139 @@ static Instruction *foldCtpopPow2Test(ICmpInst &I, IntrinsicInst *CtpopLhs,
return nullptr;
}
+static bool isNullOrPoison(Constant *C) {
+ const auto Pred = [](Constant *X) {
+ return X->isNullValue() || isa<UndefValue>(X) || isa<PoisonValue>(X);
+ };
+
+ if (auto *VecC = dyn_cast<ConstantVector>(C)) {
+ for (unsigned I = 0; I < VecC->getNumOperands(); ++I) {
+ if (Pred(VecC->getOperand(I)))
+ return true;
+ }
+ return false;
+ }
+
+ if (auto *DataVec = dyn_cast<ConstantDataVector>(C)) {
+ for (unsigned I = 0; I < DataVec->getNumElements(); ++I) {
+ if (Pred(DataVec->getElementAsConstant(I)))
+ return true;
+ }
+ return false;
+ }
+
+ return Pred(C);
+}
+
+Instruction *
+InstCombinerImpl::foldICmpEqZeroVectorReduceIntrinsic(ICmpInst &Cmp,
+ IntrinsicInst *II) {
+ // vector.reduce.OP f(X_i) == 0 -> vector.reduce.OP X_i == 0
+ //
+ // We can prove it for cases when:
+ //
+ // 1. OP X_i == 0 <=> \forall i \in [1, N] X_i == 0
+ // 1'. OP X_i == 0 <=> \exists j \in [1, N] X_j == 0
+ // 2. f(x) == 0 <=> x == 0
+ //
+ // From 1 and 2 (or 1' and 2), we can infer that
+ //
+ // OP f(X_i) == 0 <=> OP X_i == 0.
----------------
dtcxzyw wrote:
Can we extend and reuse the `stripNullTest` helper in ValueTracking?
https://github.com/llvm/llvm-project/pull/173069
More information about the llvm-commits
mailing list