[llvm] [InstCombine] Fold x sgt/sle -x without nsw (PR #223466)

pratap singh via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 20 22:38:29 PDT 2026


https://github.com/pratap9572singh updated https://github.com/llvm/llvm-project/pull/223466

>From b931cfc5090bd43da607b86ba8f73cd5c84c9b9a Mon Sep 17 00:00:00 2001
From: Pratap Singh <pratap9572singh at gmail.com>
Date: Mon, 14 Sep 2026 15:28:53 +0000
Subject: [PATCH] [InstCombine] Fold x sgt/sle -x without nsw

foldICmpXNegX() required nsw on the negation, but the signed
greater-than and less-or-equal folds hold for wrapping negation too:
0 and INT_MIN are equal to their own negation, so the strict
comparison is false there, and every other value has the opposite
sign from its negation.

slt and sge are not valid without nsw - they differ at INT_MIN - so
they are excluded.

Alive2: https://alive2.llvm.org/ce/z/zuDKGb

Fixes #223348
---
 .../InstCombine/InstCombineCompares.cpp       | 10 +++
 .../Transforms/InstCombine/cmp-x-vs-neg-x.ll  | 62 ++++++++++++++++++-
 .../Transforms/InstCombine/icmp-of-xor-x.ll   |  6 +-
 3 files changed, 73 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index d0c9c159a335d..606055e80da35 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5124,6 +5124,16 @@ static Instruction *foldICmpXNegX(ICmpInst &I,
     return ICmpInst::Create(Instruction::ICmp, Pred, X,
                             Constant::getNullValue(X->getType()), I.getName());
   }
+  // Without nsw, x sgt -x --> x sgt 0 and x sle -x --> x sle 0 still hold:
+  // 0 and INT_MIN equal their wrapping negation (so the strict comparison is
+  // false there), and every other value has the opposite sign from its
+  // negation. slt/sge are not valid without nsw: they differ at INT_MIN.
+  if (match(&I, m_c_ICmp(Pred, m_Neg(m_Value(X)), m_Deferred(X))) &&
+      (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE)) {
+    Pred = ICmpInst::getSwappedPredicate(Pred);
+    return ICmpInst::Create(Instruction::ICmp, Pred, X,
+                            Constant::getNullValue(X->getType()), I.getName());
+  }
 
   // A value is not equal to its negation unless that value is 0 or
   // MinSignedValue, ie: a != -a --> (a & MaxSignedVal) != 0
diff --git a/llvm/test/Transforms/InstCombine/cmp-x-vs-neg-x.ll b/llvm/test/Transforms/InstCombine/cmp-x-vs-neg-x.ll
index 96b03e6cd054c..f2000d1438b24 100644
--- a/llvm/test/Transforms/InstCombine/cmp-x-vs-neg-x.ll
+++ b/llvm/test/Transforms/InstCombine/cmp-x-vs-neg-x.ll
@@ -135,7 +135,7 @@ define i1 @n10(i8 %x) {
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X]], [[NEG_X]]
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
-  %neg_x = sub i8 0, %x ; not nsw
+  %neg_x = sub i8 0, %x ; not nsw, but slt/sge are invalid without it (differ at INT_MIN)
   %cmp = icmp sgt i8 %neg_x, %x
   ret i1 %cmp
 }
@@ -161,3 +161,63 @@ define i1 @n12(i8 %x1, i8 %x2) {
   %cmp = icmp sgt i8 %neg_x, %x2 ; not %x1
   ret i1 %cmp
 }
+
+; The signed greater-than / less-or-equal folds do not require nsw:
+; 0 and INT_MIN are equal to their wrapping negation, so the strict
+; comparison is false there, and every other value has the opposite
+; sign from its negation.
+
+define i1 @t13_no_nsw(i8 %x) {
+; CHECK-LABEL: @t13_no_nsw(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %neg_x = sub i8 0, %x ; not nsw, folds anyway
+  %cmp = icmp slt i8 %neg_x, %x
+  ret i1 %cmp
+}
+
+define i1 @t14_no_nsw(i8 %x) {
+; CHECK-LABEL: @t14_no_nsw(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %neg_x = sub i8 0, %x ; not nsw, folds anyway
+  %cmp = icmp sge i8 %neg_x, %x
+  ret i1 %cmp
+}
+
+define i1 @t15_no_nsw_commutative(i8 %x) {
+; CHECK-LABEL: @t15_no_nsw_commutative(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %neg_x = sub i8 0, %x ; not nsw, folds anyway
+  %cmp = icmp sgt i8 %x, %neg_x
+  ret i1 %cmp
+}
+
+; Negative tests: these differ from the folded form at INT_MIN,
+; where -x wraps back to x.
+
+define i1 @n16_no_nsw_slt(i8 %x) {
+; CHECK-LABEL: @n16_no_nsw_slt(
+; CHECK-NEXT:    [[NEG_X:%.*]] = sub i8 0, [[X:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X]], [[NEG_X]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %neg_x = sub i8 0, %x ; not nsw
+  %cmp = icmp slt i8 %x, %neg_x
+  ret i1 %cmp
+}
+
+define i1 @n17_no_nsw_sge(i8 %x) {
+; CHECK-LABEL: @n17_no_nsw_sge(
+; CHECK-NEXT:    [[NEG_X:%.*]] = sub i8 0, [[X:%.*]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sge i8 [[X]], [[NEG_X]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %neg_x = sub i8 0, %x ; not nsw
+  %cmp = icmp sge i8 %x, %neg_x
+  ret i1 %cmp
+}
diff --git a/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll b/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll
index 2260f77ea5f44..bdfb5a12c33d2 100644
--- a/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll
@@ -314,8 +314,7 @@ define i1 @src_sle_incx_nx(i8 %x) {
 ; (X-1) comp X^Neg_C
 define i1 @src_sle_decx_nx(i8 %x) {
 ; CHECK-LABEL: @src_sle_decx_nx(
-; CHECK-NEXT:    [[TMP1:%.*]] = sub i8 0, [[X:%.*]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[X]], [[TMP1]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 1
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %nx = xor i8 %x, -1
@@ -338,8 +337,7 @@ define i1 @src_sle_x_nincx(i8 %x) {
 ; X comp (X-1)^Neg_C
 define i1 @src_sle_x_ndecx(i8 %x) {
 ; CHECK-LABEL: @src_sle_x_ndecx(
-; CHECK-NEXT:    [[NOT_DEC_X:%.*]] = sub i8 0, [[X:%.*]]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sle i8 [[X]], [[NOT_DEC_X]]
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 1
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %dec.x = add i8 %x, -1



More information about the llvm-commits mailing list