[llvm] [InstCombine] Treat `uitofp nneg` as non-negative signed range for fcmp clamp folds (PR #208234)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 9 20:46:37 PDT 2026


https://github.com/imkiva updated https://github.com/llvm/llvm-project/pull/208234

>From e92637425e9a0817eaeea5a5397f742a0a8579e4 Mon Sep 17 00:00:00 2001
From: imkiva <zengtao at iscas.ac.cn>
Date: Mon, 13 Jul 2026 14:10:09 +0800
Subject: [PATCH 1/8] [InstCombine] Use samesign constraints in unsigned
 known-bits folds

---
 llvm/include/llvm/Analysis/ValueTracking.h    |  2 +
 llvm/lib/Analysis/ValueTracking.cpp           | 17 +++-
 .../InstCombine/InstCombineCompares.cpp       |  8 +-
 .../InstCombine/icmp-samesign-canonicalize.ll | 92 +++++++++++++++++++
 4 files changed, 114 insertions(+), 5 deletions(-)
 create mode 100644 llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll

diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index c87e39bca8215..f622fd2ad8491 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -864,6 +864,8 @@ LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root,
 /// form with the strictness flipped predicate. Return the new predicate and
 /// corresponding constant RHS if possible. Otherwise return std::nullopt.
 /// E.g., (icmp sgt X, 0) -> (icmp sle X, 1).
+/// For a samesign predicate, fail if adjusting the constant would change its
+/// sign bit, because that would change the comparison's poison domain.
 LLVM_ABI std::optional<std::pair<CmpPredicate, Constant *>>
 getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C);
 
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 44a1240f5635a..8e94512b1b405 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8964,8 +8964,18 @@ llvm::getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C) {
 
   // Check if the constant operand can be safely incremented/decremented
   // without overflowing/underflowing.
-  auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
-    return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
+  auto ConstantIsOk = [Pred, WillIncrement, IsSigned](ConstantInt *C) {
+    if (WillIncrement ? C->isMaxValue(IsSigned) : C->isMinValue(IsSigned))
+      return false;
+
+    if (!Pred.hasSameSign())
+      return true;
+
+    // Preserve samesign only if adjusting the constant does not change its
+    // sign bit, and therefore does not change the poison domain.
+    const APInt &Value = C->getValue();
+    APInt Adjusted = WillIncrement ? Value + 1 : Value - 1;
+    return Value.isNegative() == Adjusted.isNegative();
   };
 
   Constant *SafeReplacementConstant = nullptr;
@@ -9013,7 +9023,8 @@ llvm::getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C) {
     C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
   }
 
-  CmpInst::Predicate NewPred = CmpInst::getFlippedStrictnessPredicate(Pred);
+  CmpPredicate NewPred(CmpInst::getFlippedStrictnessPredicate(Pred),
+                       Pred.hasSameSign());
 
   // Increment or decrement the constant.
   Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index cc79f67e9f32f..8b85b68cec73d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7373,11 +7373,15 @@ static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) {
   if (!Op1C)
     return nullptr;
 
-  auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, Op1C);
+  auto FlippedStrictness =
+      getFlippedStrictnessPredicateAndConstant(I.getCmpPredicate(), Op1C);
   if (!FlippedStrictness)
     return nullptr;
 
-  return new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
+  auto *NewCmp =
+      new ICmpInst(FlippedStrictness->first, Op0, FlippedStrictness->second);
+  NewCmp->setSameSign(FlippedStrictness->first.hasSameSign());
+  return NewCmp;
 }
 
 /// If we have a comparison with a non-canonical predicate, if we can update
diff --git a/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll b/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll
new file mode 100644
index 0000000000000..fa66d471d4bfe
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll
@@ -0,0 +1,92 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i1 @uge_non_endpoint(i8 %x) {
+; CHECK-LABEL: define i1 @uge_non_endpoint(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign ugt i8 [[X]], 41
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp samesign uge i8 %x, 42
+  ret i1 %cmp
+}
+
+define <2 x i1> @ule_non_endpoint_vec(<2 x i8> %x) {
+; CHECK-LABEL: define <2 x i1> @ule_non_endpoint_vec(
+; CHECK-SAME: <2 x i8> [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign ult <2 x i8> [[X]], splat (i8 43)
+; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+;
+  %cmp = icmp samesign ule <2 x i8> %x, splat (i8 42)
+  ret <2 x i1> %cmp
+}
+
+define i1 @sge_zero_sign_crossing(i8 %x) {
+; CHECK-LABEL: define i1 @sge_zero_sign_crossing(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign sge i8 [[X]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp samesign sge i8 %x, 0
+  ret i1 %cmp
+}
+
+define i1 @sle_minus_one_sign_crossing(i8 %x) {
+; CHECK-LABEL: define i1 @sle_minus_one_sign_crossing(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign sle i8 [[X]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp samesign sle i8 %x, -1
+  ret i1 %cmp
+}
+
+define i1 @sge_non_endpoint(i8 %x) {
+; CHECK-LABEL: define i1 @sge_non_endpoint(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign sgt i8 [[X]], 41
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp samesign sge i8 %x, 42
+  ret i1 %cmp
+}
+
+define <2 x i1> @uge_mixed_sign_crossing_vec(<2 x i8> %x) {
+; CHECK-LABEL: define <2 x i1> @uge_mixed_sign_crossing_vec(
+; CHECK-SAME: <2 x i8> [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign uge <2 x i8> [[X]], <i8 42, i8 -128>
+; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+;
+  %cmp = icmp samesign uge <2 x i8> %x, <i8 42, i8 -128>
+  ret <2 x i1> %cmp
+}
+
+define <2 x i1> @ule_mixed_sign_crossing_vec(<2 x i8> %x) {
+; CHECK-LABEL: define <2 x i1> @ule_mixed_sign_crossing_vec(
+; CHECK-SAME: <2 x i8> [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign ule <2 x i8> [[X]], <i8 42, i8 127>
+; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+;
+  %cmp = icmp samesign ule <2 x i8> %x, <i8 42, i8 127>
+  ret <2 x i1> %cmp
+}
+
+define i1 @sge_zero_no_samesign(i8 %x) {
+; CHECK-LABEL: define i1 @sge_zero_no_samesign(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X]], -1
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp sge i8 %x, 0
+  ret i1 %cmp
+}
+
+define i1 @sle_minus_one_no_samesign(i8 %x) {
+; CHECK-LABEL: define i1 @sle_minus_one_no_samesign(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp sle i8 %x, -1
+  ret i1 %cmp
+}

>From ab2b02ca2bc2af13e4b7c133881daa43b2c055b2 Mon Sep 17 00:00:00 2001
From: imkiva <zengtao at iscas.ac.cn>
Date: Tue, 14 Jul 2026 12:34:26 +0800
Subject: [PATCH 2/8] Address review comments

---
 llvm/lib/Analysis/ValueTracking.cpp                     | 9 ++++-----
 llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 4 ++--
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 8e94512b1b405..28d47d8f7df27 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8971,11 +8971,10 @@ llvm::getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C) {
     if (!Pred.hasSameSign())
       return true;
 
-    // Preserve samesign only if adjusting the constant does not change its
-    // sign bit, and therefore does not change the poison domain.
-    const APInt &Value = C->getValue();
-    APInt Adjusted = WillIncrement ? Value + 1 : Value - 1;
-    return Value.isNegative() == Adjusted.isNegative();
+    // Crossing the corresponding boundary in the other ordering changes the
+    // sign bit, and therefore changes the poison domain.
+    return WillIncrement ? !C->isMaxValue(!IsSigned)
+                         : !C->isMinValue(!IsSigned);
   };
 
   Constant *SafeReplacementConstant = nullptr;
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 8b85b68cec73d..95b525e210efb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7362,7 +7362,7 @@ Instruction *InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
 /// it into the appropriate icmp lt or icmp gt instruction. This transform
 /// allows them to be folded in visitICmpInst.
 static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) {
-  ICmpInst::Predicate Pred = I.getPredicate();
+  CmpPredicate Pred = I.getCmpPredicate();
   if (ICmpInst::isEquality(Pred) || !ICmpInst::isIntPredicate(Pred) ||
       InstCombiner::isCanonicalPredicate(Pred))
     return nullptr;
@@ -7374,7 +7374,7 @@ static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) {
     return nullptr;
 
   auto FlippedStrictness =
-      getFlippedStrictnessPredicateAndConstant(I.getCmpPredicate(), Op1C);
+      getFlippedStrictnessPredicateAndConstant(Pred, Op1C);
   if (!FlippedStrictness)
     return nullptr;
 

>From 83b68015f78851bfd58a571996fa5b64507e0bca Mon Sep 17 00:00:00 2001
From: imkiva <zengtao at iscas.ac.cn>
Date: Tue, 14 Jul 2026 12:58:49 +0800
Subject: [PATCH 3/8] clang-format

---
 llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 95b525e210efb..741d8c89f6dbd 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7373,8 +7373,7 @@ static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) {
   if (!Op1C)
     return nullptr;
 
-  auto FlippedStrictness =
-      getFlippedStrictnessPredicateAndConstant(Pred, Op1C);
+  auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, Op1C);
   if (!FlippedStrictness)
     return nullptr;
 

>From ebbfda55752ccd78927c6e2f56313f090d17b1bb Mon Sep 17 00:00:00 2001
From: imkiva <zengtao at iscas.ac.cn>
Date: Wed, 15 Jul 2026 11:34:11 +0800
Subject: [PATCH 4/8] Add non-strict samesign endpoint tests

---
 .../InstCombine/icmp-samesign-canonicalize.ll | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll b/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll
index fa66d471d4bfe..869cd00ca3eb0 100644
--- a/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll
@@ -21,6 +21,34 @@ define <2 x i1> @ule_non_endpoint_vec(<2 x i8> %x) {
   ret <2 x i1> %cmp
 }
 
+define i1 @ule_smax(i8 %x) {
+; CHECK-LABEL: define i1 @ule_smax(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    ret i1 true
+;
+  %cmp = icmp samesign ule i8 %x, 127
+  ret i1 %cmp
+}
+
+define i1 @uge_smax(i8 %x) {
+; CHECK-LABEL: define i1 @uge_smax(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X]], 127
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp samesign uge i8 %x, 127
+  ret i1 %cmp
+}
+
+define i1 @uge_smin(i8 %x) {
+; CHECK-LABEL: define i1 @uge_smin(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    ret i1 true
+;
+  %cmp = icmp samesign uge i8 %x, -128
+  ret i1 %cmp
+}
+
 define i1 @sge_zero_sign_crossing(i8 %x) {
 ; CHECK-LABEL: define i1 @sge_zero_sign_crossing(
 ; CHECK-SAME: i8 [[X:%.*]]) {

>From d949909686eefa48d159449984ece5085d6b486a Mon Sep 17 00:00:00 2001
From: imkiva <zengtao at iscas.ac.cn>
Date: Thu, 16 Jul 2026 12:51:37 +0800
Subject: [PATCH 5/8] Adjust tests for independent samesign canonicalization

---
 .../InstCombine/icmp-samesign-canonicalize.ll | 28 ++++---------------
 1 file changed, 5 insertions(+), 23 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll b/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll
index 869cd00ca3eb0..e0d8073dcbe6d 100644
--- a/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll
@@ -24,7 +24,8 @@ define <2 x i1> @ule_non_endpoint_vec(<2 x i8> %x) {
 define i1 @ule_smax(i8 %x) {
 ; CHECK-LABEL: define i1 @ule_smax(
 ; CHECK-SAME: i8 [[X:%.*]]) {
-; CHECK-NEXT:    ret i1 true
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign ule i8 [[X]], 127
+; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %cmp = icmp samesign ule i8 %x, 127
   ret i1 %cmp
@@ -33,7 +34,7 @@ define i1 @ule_smax(i8 %x) {
 define i1 @uge_smax(i8 %x) {
 ; CHECK-LABEL: define i1 @uge_smax(
 ; CHECK-SAME: i8 [[X:%.*]]) {
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X]], 127
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign ugt i8 [[X]], 126
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %cmp = icmp samesign uge i8 %x, 127
@@ -43,7 +44,8 @@ define i1 @uge_smax(i8 %x) {
 define i1 @uge_smin(i8 %x) {
 ; CHECK-LABEL: define i1 @uge_smin(
 ; CHECK-SAME: i8 [[X:%.*]]) {
-; CHECK-NEXT:    ret i1 true
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign uge i8 [[X]], -128
+; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %cmp = icmp samesign uge i8 %x, -128
   ret i1 %cmp
@@ -98,23 +100,3 @@ define <2 x i1> @ule_mixed_sign_crossing_vec(<2 x i8> %x) {
   %cmp = icmp samesign ule <2 x i8> %x, <i8 42, i8 127>
   ret <2 x i1> %cmp
 }
-
-define i1 @sge_zero_no_samesign(i8 %x) {
-; CHECK-LABEL: define i1 @sge_zero_no_samesign(
-; CHECK-SAME: i8 [[X:%.*]]) {
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[X]], -1
-; CHECK-NEXT:    ret i1 [[CMP]]
-;
-  %cmp = icmp sge i8 %x, 0
-  ret i1 %cmp
-}
-
-define i1 @sle_minus_one_no_samesign(i8 %x) {
-; CHECK-LABEL: define i1 @sle_minus_one_no_samesign(
-; CHECK-SAME: i8 [[X:%.*]]) {
-; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X]], 0
-; CHECK-NEXT:    ret i1 [[CMP]]
-;
-  %cmp = icmp sle i8 %x, -1
-  ret i1 %cmp
-}

>From 7cf71c3c272fc19da883aa30f58e339b661cc2bd Mon Sep 17 00:00:00 2001
From: imkiva <zengtao at iscas.ac.cn>
Date: Wed, 15 Jul 2026 11:16:13 +0800
Subject: [PATCH 6/8] [InstCombine] Use samesign constraints in unsigned
 known-bits folds

---
 .../InstCombine/InstCombineCompares.cpp       | 16 ++++++
 .../InstCombine/icmp-samesign-known-bits.ll   | 51 +++++++++++++++++++
 2 files changed, 67 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/icmp-samesign-known-bits.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 741d8c89f6dbd..2ef4c287bb1cc 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7088,6 +7088,22 @@ Instruction *InstCombinerImpl::foldICmpUsingKnownBits(ICmpInst &I) {
       return &I;
   }
 
+  // If an unsigned samesign comparison is not poison, both operands have the
+  // same sign bit. Propagate a known sign bit between the temporary KnownBits
+  // values so the existing range folds can use that constraint.
+  if (I.hasSameSign() && I.isUnsigned()) {
+    auto PropagateSignBit = [](const KnownBits &From, KnownBits &To) {
+      if (To.isNegative() || To.isNonNegative())
+        return;
+      if (From.isNegative())
+        To.makeNegative();
+      else if (From.isNonNegative())
+        To.makeNonNegative();
+    };
+    PropagateSignBit(Op0Known, Op1Known);
+    PropagateSignBit(Op1Known, Op0Known);
+  }
+
   if (!isa<Constant>(Op0) && Op0Known.isConstant())
     return new ICmpInst(
         Pred, ConstantExpr::getIntegerValue(Ty, Op0Known.getConstant()), Op1);
diff --git a/llvm/test/Transforms/InstCombine/icmp-samesign-known-bits.ll b/llvm/test/Transforms/InstCombine/icmp-samesign-known-bits.ll
new file mode 100644
index 0000000000000..798e4066c4976
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-samesign-known-bits.ll
@@ -0,0 +1,51 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i1 @ugt_smax_minus_one(i8 %x) {
+; CHECK-LABEL: define i1 @ugt_smax_minus_one(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X]], 127
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp samesign ugt i8 %x, 126
+  ret i1 %cmp
+}
+
+define i1 @ult_smax(i8 %x) {
+; CHECK-LABEL: define i1 @ult_smax(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[X]], 127
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp samesign ult i8 %x, 127
+  ret i1 %cmp
+}
+
+define i1 @ugt_smax(i8 %x) {
+; CHECK-LABEL: define i1 @ugt_smax(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    ret i1 false
+;
+  %cmp = icmp samesign ugt i8 %x, 127
+  ret i1 %cmp
+}
+
+define i1 @ult_negative_endpoint(i8 %x) {
+; CHECK-LABEL: define i1 @ult_negative_endpoint(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X]], -128
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp samesign ult i8 %x, -127
+  ret i1 %cmp
+}
+
+define <2 x i1> @ugt_smax_minus_one_vec(<2 x i8> %x) {
+; CHECK-LABEL: define <2 x i1> @ugt_smax_minus_one_vec(
+; CHECK-SAME: <2 x i8> [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i8> [[X]], splat (i8 127)
+; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+;
+  %cmp = icmp samesign ugt <2 x i8> %x, splat (i8 126)
+  ret <2 x i1> %cmp
+}

>From 221b2c5efedea4a5665400eec7fe482787b187e3 Mon Sep 17 00:00:00 2001
From: imkiva <zengtao at iscas.ac.cn>
Date: Thu, 16 Jul 2026 12:57:11 +0800
Subject: [PATCH 7/8] Update samesign endpoint checks for known-bits fold

---
 .../Transforms/InstCombine/icmp-samesign-canonicalize.ll  | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll b/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll
index e0d8073dcbe6d..67b12a1943929 100644
--- a/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-samesign-canonicalize.ll
@@ -24,8 +24,7 @@ define <2 x i1> @ule_non_endpoint_vec(<2 x i8> %x) {
 define i1 @ule_smax(i8 %x) {
 ; CHECK-LABEL: define i1 @ule_smax(
 ; CHECK-SAME: i8 [[X:%.*]]) {
-; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign ule i8 [[X]], 127
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %cmp = icmp samesign ule i8 %x, 127
   ret i1 %cmp
@@ -34,7 +33,7 @@ define i1 @ule_smax(i8 %x) {
 define i1 @uge_smax(i8 %x) {
 ; CHECK-LABEL: define i1 @uge_smax(
 ; CHECK-SAME: i8 [[X:%.*]]) {
-; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign ugt i8 [[X]], 126
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X]], 127
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %cmp = icmp samesign uge i8 %x, 127
@@ -44,8 +43,7 @@ define i1 @uge_smax(i8 %x) {
 define i1 @uge_smin(i8 %x) {
 ; CHECK-LABEL: define i1 @uge_smin(
 ; CHECK-SAME: i8 [[X:%.*]]) {
-; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign uge i8 [[X]], -128
-; CHECK-NEXT:    ret i1 [[CMP]]
+; CHECK-NEXT:    ret i1 true
 ;
   %cmp = icmp samesign uge i8 %x, -128
   ret i1 %cmp

>From b9aff1952da9ae47cc1b5cf82fbf93001c622424 Mon Sep 17 00:00:00 2001
From: imkiva <zengtao at iscas.ac.cn>
Date: Mon, 13 Jul 2026 14:18:28 +0800
Subject: [PATCH 8/8] [InstCombine] Use the nneg range for uitofp fcmp folds

---
 .../InstCombine/InstCombineCompares.cpp       |  24 ++-
 .../InstCombine/uitofp-nneg-fcmp-select.ll    | 151 ++++++++++++++++++
 2 files changed, 169 insertions(+), 6 deletions(-)
 create mode 100644 llvm/test/Transforms/InstCombine/uitofp-nneg-fcmp-select.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 2ef4c287bb1cc..236e298f94c49 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -8523,10 +8523,16 @@ Instruction *InstCombinerImpl::foldFCmpIntToFPConst(FCmpInst &I,
     }
   }
 
+  Value *X = LHSI->getOperand(0);
+
   // Lower this FP comparison into an appropriate integer version of the
   // comparison.
-  return new ICmpInst(Pred, LHSI->getOperand(0),
-                      ConstantInt::get(LHSI->getOperand(0)->getType(), RHSInt));
+  auto *NewCmp =
+      new ICmpInst(Pred, X, ConstantInt::get(X->getType(), RHSInt));
+  if (LHSUnsigned && cast<PossiblyNonNegInst>(LHSI)->hasNonNeg() &&
+      RHSInt.isSignBitClear())
+    NewCmp->setSameSign();
+  return NewCmp;
 }
 
 /// Fold fcmp/icmp pred (select C1, TV1, FV1), (select C2, TV2, FV2)
@@ -9152,11 +9158,17 @@ static bool isMinMaxCmpSelectEliminable(SelectPatternFlavor Flavor, Value *A,
     return false;
 
   bool IsUnsigned = I->getOpcode() == Instruction::UIToFP;
+  bool IsNonNegUIToFP = IsUnsigned && cast<PossiblyNonNegInst>(I)->hasNonNeg();
   unsigned BitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
-  APSInt IntBoundary = (Flavor == SPF_FMAXNUM)
-                           ? APSInt::getMinValue(BitWidth, IsUnsigned)
-                           : APSInt::getMaxValue(BitWidth, IsUnsigned);
-  APSInt ConvertedInt(BitWidth, IsUnsigned);
+  APSInt LowerBoundary = APSInt::getMinValue(BitWidth, IsUnsigned);
+  // The defined upper bound of uitofp nneg is signed max because negative
+  // signed inputs produce poison.
+  APSInt UpperBoundary =
+      IsNonNegUIToFP
+          ? APSInt(APInt::getSignedMaxValue(BitWidth), /*isUnsigned=*/true)
+          : APSInt::getMaxValue(BitWidth, IsUnsigned);
+  APSInt IntBoundary = Flavor == SPF_FMAXNUM ? LowerBoundary : UpperBoundary;
+  APSInt ConvertedInt(BitWidth, IntBoundary.isUnsigned());
   bool IsExact;
   APFloat::opStatus Status =
       APF->convertToInteger(ConvertedInt, APFloat::rmTowardZero, &IsExact);
diff --git a/llvm/test/Transforms/InstCombine/uitofp-nneg-fcmp-select.ll b/llvm/test/Transforms/InstCombine/uitofp-nneg-fcmp-select.ll
new file mode 100644
index 0000000000000..f0478f28dc85a
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/uitofp-nneg-fcmp-select.ll
@@ -0,0 +1,151 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define float @uitofp_nneg_i8_fmin_smax(i8 %x) {
+; CHECK-LABEL: define float @uitofp_nneg_i8_fmin_smax(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[F:%.*]] = uitofp i8 [[X]] to float
+; CHECK-NEXT:    ret float [[F]]
+;
+  %f = uitofp nneg i8 %x to float
+  %cmp = fcmp oge float %f, 1.270000e+02
+  %sel = select i1 %cmp, float 1.270000e+02, float %f
+  ret float %sel
+}
+
+define float @uitofp_nneg_i8_fmin_smax_inverted(i8 %x) {
+; CHECK-LABEL: define float @uitofp_nneg_i8_fmin_smax_inverted(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[F:%.*]] = uitofp i8 [[X]] to float
+; CHECK-NEXT:    ret float [[F]]
+;
+  %f = uitofp nneg i8 %x to float
+  %cmp = fcmp olt float %f, 1.270000e+02
+  %sel = select i1 %cmp, float %f, float 1.270000e+02
+  ret float %sel
+}
+
+define float @uitofp_nneg_i8_fmax_zero(i8 %x) {
+; CHECK-LABEL: define float @uitofp_nneg_i8_fmax_zero(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[F:%.*]] = uitofp i8 [[X]] to float
+; CHECK-NEXT:    ret float [[F]]
+;
+  %f = uitofp nneg i8 %x to float
+  %cmp = fcmp ole float %f, 0.000000e+00
+  %sel = select i1 %cmp, float 0.000000e+00, float %f
+  ret float %sel
+}
+
+define float @uitofp_i8_no_nneg_fmin_127_not_identity(i8 %x) {
+; CHECK-LABEL: define float @uitofp_i8_no_nneg_fmin_127_not_identity(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[F:%.*]] = uitofp i8 [[X]] to float
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge float [[F]], 1.270000e+02
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], float 1.270000e+02, float [[F]]
+; CHECK-NEXT:    ret float [[SEL]]
+;
+  %f = uitofp i8 %x to float
+  %cmp = fcmp oge float %f, 1.270000e+02
+  %sel = select i1 %cmp, float 1.270000e+02, float %f
+  ret float %sel
+}
+
+define float @uitofp_nneg_i8_fmin_126_not_identity(i8 %x) {
+; CHECK-LABEL: define float @uitofp_nneg_i8_fmin_126_not_identity(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[F:%.*]] = uitofp nneg i8 [[X]] to float
+; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge float [[F]], 1.260000e+02
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], float 1.260000e+02, float [[F]]
+; CHECK-NEXT:    ret float [[SEL]]
+;
+  %f = uitofp nneg i8 %x to float
+  %cmp = fcmp oge float %f, 1.260000e+02
+  %sel = select i1 %cmp, float 1.260000e+02, float %f
+  ret float %sel
+}
+
+define i1 @uitofp_nneg_i8_cmp_oge_smax(i8 %x) {
+; CHECK-LABEL: define i1 @uitofp_nneg_i8_cmp_oge_smax(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[X]], 127
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %f = uitofp nneg i8 %x to float
+  %cmp = fcmp oge float %f, 1.270000e+02
+  ret i1 %cmp
+}
+
+define i1 @uitofp_nneg_i8_cmp_olt_smax(i8 %x) {
+; CHECK-LABEL: define i1 @uitofp_nneg_i8_cmp_olt_smax(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[X]], 127
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %f = uitofp nneg i8 %x to float
+  %cmp = fcmp olt float %f, 1.270000e+02
+  ret i1 %cmp
+}
+
+define i1 @uitofp_nneg_i8_cmp_ogt_smax(i8 %x) {
+; CHECK-LABEL: define i1 @uitofp_nneg_i8_cmp_ogt_smax(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    ret i1 false
+;
+  %f = uitofp nneg i8 %x to float
+  %cmp = fcmp ogt float %f, 1.270000e+02
+  ret i1 %cmp
+}
+
+define i1 @uitofp_nneg_i8_cmp_ole_smax(i8 %x) {
+; CHECK-LABEL: define i1 @uitofp_nneg_i8_cmp_ole_smax(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    ret i1 true
+;
+  %f = uitofp nneg i8 %x to float
+  %cmp = fcmp ole float %f, 1.270000e+02
+  ret i1 %cmp
+}
+
+define i1 @uitofp_nneg_i8_cmp_oge_non_endpoint(i8 %x) {
+; CHECK-LABEL: define i1 @uitofp_nneg_i8_cmp_oge_non_endpoint(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp samesign ugt i8 [[X]], 41
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %f = uitofp nneg i8 %x to float
+  %cmp = fcmp oge float %f, 4.200000e+01
+  ret i1 %cmp
+}
+
+define i1 @uitofp_nneg_i8_cmp_ogt_zero(i8 %x) {
+; CHECK-LABEL: define i1 @uitofp_nneg_i8_cmp_ogt_zero(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne i8 [[X]], 0
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %f = uitofp nneg i8 %x to float
+  %cmp = fcmp ogt float %f, 0.000000e+00
+  ret i1 %cmp
+}
+
+define i1 @uitofp_nneg_i8_cmp_olt_zero(i8 %x) {
+; CHECK-LABEL: define i1 @uitofp_nneg_i8_cmp_olt_zero(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    ret i1 false
+;
+  %f = uitofp nneg i8 %x to float
+  %cmp = fcmp olt float %f, 0.000000e+00
+  ret i1 %cmp
+}
+
+define <2 x i1> @uitofp_nneg_v2i8_cmp_ogt_zero(<2 x i8> %x) {
+; CHECK-LABEL: define <2 x i1> @uitofp_nneg_v2i8_cmp_ogt_zero(
+; CHECK-SAME: <2 x i8> [[X:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne <2 x i8> [[X]], zeroinitializer
+; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+;
+  %f = uitofp nneg <2 x i8> %x to <2 x float>
+  %cmp = fcmp ogt <2 x float> %f, zeroinitializer
+  ret <2 x i1> %cmp
+}



More information about the llvm-commits mailing list