[llvm] [InstCombine] Fold and-of-srem-sign-contradictions to false (PR #230413)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 9 01:53:46 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Archmushroom (Misasasa)
<details>
<summary>Changes</summary>
Fixes #<!-- -->230409
If one icmp requires `srem(X, C)` (`C > 0`) to be strictly positive/negative and
the other icmp constrains X to the opposite sign, the AND is always false.
`(srem X, C) > 0 && (X <= 0) -> false`
`(srem X, C) < 0 && (X >= 0) -> false`
since srem(X, C) shares X's sign (or is zero).
Added regression test in
`llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll`,
and all InstCombine lit tests pass.
---
Full diff: https://github.com/llvm/llvm-project/pull/230413.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+58)
- (modified) llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll (+30)
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 62f0da83e8c124..814f70399ec626 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3676,6 +3676,64 @@ Value *InstCombinerImpl::foldAndOrOfICmps(Value *LHS, Value *RHS,
return Builder.createIsFPClass(X, IsAnd ? FPClassTest::fcNormal
: ~FPClassTest::fcNormal);
+ // srem(X, C) with C > 0 has the same sign as X (or is zero).
+ // So srem(X, C) > 0 requires X > 0, and srem(X, C) < 0 requires X < 0.
+ // If one icmp requires srem(X, C) to have a certain sign and the other
+ // icmp constrains X to the opposite sign, the AND is always false.
+ if (IsAnd) {
+ auto checkSRemSignContradiction =
+ [&](CmpPredicate PredRem, Value *RemOp, const APInt *RemC,
+ CmpPredicate PredX, Value *XOp, const APInt *XC) -> Value * {
+ Value *X;
+ const APInt *DivC;
+ if (!match(RemOp, m_SRem(m_Value(X), m_APInt(DivC))) ||
+ !DivC->isStrictlyPositive() || X != XOp || !RemC || !XC)
+ return nullptr;
+
+ // Determine if the rem icmp requires strictly positive/negative result.
+ // sgt srem, K -> srem > K -> positive if K >= 0
+ // sge srem, K -> srem >= K -> positive if K >= 1
+ // slt srem, K -> srem < K -> negative if K <= 0
+ // sle srem, K -> srem <= K -> negative if K <= -1
+ bool RemRequiresPos = false, RemRequiresNeg = false;
+ if (PredRem == ICmpInst::ICMP_SGT && RemC->isNonNegative())
+ RemRequiresPos = true;
+ else if (PredRem == ICmpInst::ICMP_SGE && RemC->isStrictlyPositive())
+ RemRequiresPos = true;
+ else if (PredRem == ICmpInst::ICMP_SLT && RemC->isNonPositive())
+ RemRequiresNeg = true;
+ else if (PredRem == ICmpInst::ICMP_SLE && RemC->isNegative())
+ RemRequiresNeg = true;
+ else
+ return nullptr;
+
+ // Check if the X icmp implies the opposite sign.
+ if (RemRequiresPos) {
+ // srem requires X > 0. Contradict if X icmp implies X <= 0.
+ // slt X, K: X < K, implies X <= 0 when K <= 1
+ // sle X, K: X <= K, implies X <= 0 when K <= 0
+ if ((PredX == ICmpInst::ICMP_SLT && XC->sle(1)) ||
+ (PredX == ICmpInst::ICMP_SLE && XC->isNonPositive()))
+ return ConstantInt::getFalse(LHS->getType());
+ } else {
+ // RemRequiresNeg: srem requires X < 0. Contradict if X >= 0.
+ // sgt X, K: X > K, implies X >= 0 when K >= -1
+ // sge X, K: X >= K, implies X >= 0 when K >= 0
+ if ((PredX == ICmpInst::ICMP_SGT && XC->sge(-1)) ||
+ (PredX == ICmpInst::ICMP_SGE && XC->isNonNegative()))
+ return ConstantInt::getFalse(LHS->getType());
+ }
+ return nullptr;
+ };
+
+ if (Value *V = checkSRemSignContradiction(PredR, RHS0, RHSC, PredL, LHS0,
+ LHSC))
+ return V;
+ if (Value *V = checkSRemSignContradiction(PredL, LHS0, LHSC, PredR, RHS0,
+ RHSC))
+ return V;
+ }
+
return foldAndOrOfICmpsUsingRanges(PredL, LHS0, LHS1, LHSOneUse, PredR, RHS0,
RHS1, RHSOneUse, IsAnd);
}
diff --git a/llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll b/llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll
index ee76e0eb105109..848d4332bbfbb4 100644
--- a/llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll
+++ b/llvm/test/Transforms/InstCombine/and-or-icmp-const-icmp.ll
@@ -287,3 +287,33 @@ define i1 @ne_commuted_equal_minus_1(i8 %x, i8 %py) {
}
declare void @use(i1)
+
+; ==============================================================================
+; srem sign-contradiction folds (foldAndOrOfICmps):
+; srem(X, C) has the same sign as X (or is zero) for C > 0.
+; So "srem > 0" requires X > 0, "srem < 0" requires X < 0.
+; AND-ing with an icmp that forces X to the opposite sign is always false.
+; ==============================================================================
+define i1 @and_srem_sgt_zero_x_slt_neg(i64 %x) {
+; CHECK-LABEL: define i1 @and_srem_sgt_zero_x_slt_neg(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT: ret i1 false
+;
+ %rem = srem i64 %x, 1000000
+ %c1 = icmp sgt i64 %rem, 0
+ %c2 = icmp slt i64 %x, 1
+ %r = and i1 %c1, %c2
+ ret i1 %r
+}
+
+define i1 @and_srem_slt_zero_x_sgt_pos(i64 %x) {
+; CHECK-LABEL: define i1 @and_srem_slt_zero_x_sgt_pos(
+; CHECK-SAME: i64 [[X:%.*]]) {
+; CHECK-NEXT: ret i1 false
+;
+ %rem = srem i64 %x, 1000000
+ %c1 = icmp slt i64 %rem, 0
+ %c2 = icmp sgt i64 %x, -1
+ %r = and i1 %c1, %c2
+ ret i1 %r
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/230413
More information about the llvm-commits
mailing list