[llvm] [InstCombine] Fold icmp eq/ne (or X, Y), 0 with known-nonzero OR (PR #190775)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 7 04:17:56 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Abhinay (abhinay-anubola)

<details>
<summary>Changes</summary>

When isKnownNonZero holds for the `or`, icmp eq 0 is always false and icmp ne 0 is always true. This matches (X|Y)==0 iff X==0 and Y==0.

Adds regression test icmp-or-known-nonzero.ll

Fixes https://github.com/llvm/llvm-project/issues/189320

---
Full diff: https://github.com/llvm/llvm-project/pull/190775.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp (+10) 
- (added) llvm/test/Transforms/InstCombine/icmp-or-known-nonzero.ll (+64) 


``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index ff1868f3a5840..19c47e8725a46 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -2093,6 +2093,16 @@ Instruction *InstCombinerImpl::foldICmpOrConstant(ICmpInst &Cmp,
 
   Value *OrOp0 = Or->getOperand(0), *OrOp1 = Or->getOperand(1);
 
+  // icmp eq/ne (or X, Y), 0 -> false/true if the OR is known non-zero (e.g. one
+  // operand is known non-zero at this point).
+  if (Cmp.isEquality() && C.isZero()) {
+    const SimplifyQuery Q = SQ.getWithInstruction(&Cmp);
+    if (isKnownNonZero(Or, Q))
+      return replaceInstUsesWith(
+          Cmp, Pred == ICmpInst::ICMP_EQ ? ConstantInt::getFalse(Cmp.getType())
+                                         : ConstantInt::getTrue(Cmp.getType()));
+  }
+
   // (icmp eq/ne (or disjoint x, C0), C1)
   //    -> (icmp eq/ne x, C0^C1)
   if (Cmp.isEquality() && match(OrOp1, m_ImmConstant()) &&
diff --git a/llvm/test/Transforms/InstCombine/icmp-or-known-nonzero.ll b/llvm/test/Transforms/InstCombine/icmp-or-known-nonzero.ll
new file mode 100644
index 0000000000000..8f3f930e7ef7e
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-or-known-nonzero.ll
@@ -0,0 +1,64 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+; Fold icmp eq/ne (or X, Y), 0 when the OR is known non-zero on this path.
+; Regression for https://github.com/llvm/llvm-project/issues/189320
+
+define i1 @dom_nonzero_implies_or_not_eq_zero(i32 %a, i32 %b) {
+; CHECK-LABEL: @dom_nonzero_implies_or_not_eq_zero(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[A_ZERO:%.*]] = icmp eq i32 [[A:%.*]], 0
+; CHECK-NEXT:    br i1 [[A_ZERO]], label [[IF_A_ZERO:%.*]], label [[IF_A_NONZERO:%.*]]
+; CHECK:       if_a_nonzero:
+; CHECK-NEXT:    ret i1 false
+; CHECK:       if_a_zero:
+; CHECK-NEXT:    ret i1 false
+;
+entry:
+  %a_zero = icmp eq i32 %a, 0
+  br i1 %a_zero, label %if_a_zero, label %if_a_nonzero
+
+if_a_nonzero:
+  %o = or i32 %b, %a
+  %cmp = icmp eq i32 %o, 0
+  ret i1 %cmp
+
+if_a_zero:
+  ret i1 false
+}
+
+define i1 @dom_nonzero_implies_or_not_ne_zero(i32 %a, i32 %b) {
+; CHECK-LABEL: @dom_nonzero_implies_or_not_ne_zero(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[A_ZERO:%.*]] = icmp eq i32 [[A:%.*]], 0
+; CHECK-NEXT:    br i1 [[A_ZERO]], label [[IF_A_ZERO:%.*]], label [[IF_A_NONZERO:%.*]]
+; CHECK:       if_a_nonzero:
+; CHECK-NEXT:    ret i1 true
+; CHECK:       if_a_zero:
+; CHECK-NEXT:    ret i1 false
+;
+entry:
+  %a_zero = icmp eq i32 %a, 0
+  br i1 %a_zero, label %if_a_zero, label %if_a_nonzero
+
+if_a_nonzero:
+  %o = or i32 %a, %b
+  %cmp = icmp ne i32 %o, 0
+  ret i1 %cmp
+
+if_a_zero:
+  ret i1 false
+}
+
+define i1 @no_fold_without_dom_fact(i32 %a, i32 %b) {
+; CHECK-LABEL: @no_fold_without_dom_fact(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[O:%.*]] = or i32 [[B:%.*]], [[A:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[O]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+entry:
+  %o = or i32 %b, %a
+  %cmp = icmp eq i32 %o, 0
+  ret i1 %cmp
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/190775


More information about the llvm-commits mailing list