[llvm] Missed optimization when `b - a` is known nonnegative inside `select` instruction (PR #187898)

via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 21 19:28:24 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Abu (user1342234)

<details>
<summary>Changes</summary>

This is part of #<!-- -->146131 and #<!-- -->182597

`func3` and `func4` are [equivalent](https://alive2.llvm.org/ce/z/NNMTDa) but `func3` produces a `sext` instead of `zext` when `b - a` is known non-negative.

```c++
#include <stdint.h>

uint64_t func3(int32_t a, int32_t b) {
    return (b < a ? 0 : (int32_t)(b - a));
}

uint64_t func4(int32_t a, int32_t b) {
    return (b < a ? 0 : (uint32_t)(b - a));
}
```
In x86-64, it would make shorter code by zero-extending instead of sign-extending b - a.

My PR fixes this by handling all patterns where `C` is non-negative.
`(X < Y) ? C : (X - Y)`
`(X > Y) ? C : (Y - X)`
`(X < Y) ? (Y - X) : C`
`(X > Y) ? (X - Y) : C`

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


2 Files Affected:

- (modified) llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp (+38-2) 
- (modified) llvm/test/Transforms/InstCombine/sext-nonneg-sub.ll (+116) 


``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 1de03254e8182..3d11820afa24d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -664,10 +664,46 @@ static Value *foldSelectICmpMinMax(const ICmpInst *Cmp, Value *TVal,
                                    Value *FVal,
                                    InstCombiner::BuilderTy &Builder,
                                    const SimplifyQuery &SQ) {
-  const Value *CmpLHS = Cmp->getOperand(0);
-  const Value *CmpRHS = Cmp->getOperand(1);
+  Value *CmpLHS = Cmp->getOperand(0);
+  Value *CmpRHS = Cmp->getOperand(1);
   ICmpInst::Predicate Pred = Cmp->getPredicate();
 
+  if (isKnownNonNegative(TVal, SQ)) {
+    // (X < Y) ? C : (X - Y)     (C non-negative)
+    if (Pred == CmpInst::ICMP_SLT &&
+        match(FVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) {
+      Value *SMin =
+          Builder.CreateBinaryIntrinsic(Intrinsic::smin, CmpRHS, CmpLHS);
+      return Builder.CreateNSWSub(CmpRHS, SMin);
+    }
+
+    // (X > Y) ? C : (Y - X)    (C non-negative)
+    if (Pred == CmpInst::ICMP_SGT && isKnownNonNegative(TVal, SQ) &&
+        match(FVal, m_NSWSub(m_Specific(CmpRHS), m_Specific(CmpLHS)))) {
+      Value *SMin =
+          Builder.CreateBinaryIntrinsic(Intrinsic::smin, CmpRHS, CmpLHS);
+      return Builder.CreateNSWSub(CmpRHS, SMin);
+    }
+  }
+
+  if (isKnownNonNegative(FVal, SQ)) {
+    // (X < Y) ? (Y - X) : C    (C non-negative)
+    if (Pred == CmpInst::ICMP_SLT &&
+        match(TVal, m_NSWSub(m_Specific(CmpRHS), m_Specific(CmpLHS)))) {
+      Value *SMin =
+          Builder.CreateBinaryIntrinsic(Intrinsic::smin, CmpRHS, CmpLHS);
+      return Builder.CreateNSWSub(CmpRHS, SMin);
+    }
+
+    // (X > Y) ? (X - Y) : C    (C non-negative)
+    if (Pred == CmpInst::ICMP_SGT && isKnownNonNegative(FVal, SQ) &&
+        match(TVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) {
+      Value *SMin =
+          Builder.CreateBinaryIntrinsic(Intrinsic::smin, CmpLHS, CmpRHS);
+      return Builder.CreateNSWSub(CmpLHS, SMin);
+    }
+  }
+
   // (X > Y) ? X : (Y - 1) ==> MIN(X, Y - 1)
   // (X < Y) ? X : (Y + 1) ==> MAX(X, Y + 1)
   // This transformation is valid when overflow corresponding to the sign of
diff --git a/llvm/test/Transforms/InstCombine/sext-nonneg-sub.ll b/llvm/test/Transforms/InstCombine/sext-nonneg-sub.ll
index b1121637c83b4..78e223d8960b8 100644
--- a/llvm/test/Transforms/InstCombine/sext-nonneg-sub.ll
+++ b/llvm/test/Transforms/InstCombine/sext-nonneg-sub.ll
@@ -61,3 +61,119 @@ define i64 @neg_unguarded_sub(i32 %a, i32 %b) {
   %ext = sext i32 %sub to i64
   ret i64 %ext
 }
+
+; Test that select i1 (X < Y) ? 0 : X - Y is recognized as non-negative, converting sext to zext
+define i64 @select_nonnegative_slt(i32 %x, i32 %y) {
+; CHECK-LABEL: define i64 @select_nonnegative_slt(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[Y]], i32 [[X]])
+; CHECK-NEXT:    [[COND:%.*]] = sub nsw i32 [[Y]], [[TMP1]]
+; CHECK-NEXT:    [[CONV:%.*]] = zext nneg i32 [[COND]] to i64
+; CHECK-NEXT:    ret i64 [[CONV]]
+;
+
+  %cmp = icmp slt i32 %x, %y ; b < a
+  %sub = sub nsw i32 %x, %y ; b - a
+  %cond = select i1 %cmp, i32 0, i32 %sub ; (X < Y) ? 0 : X - Y
+  %conv = sext i32 %cond to i64
+  ret i64 %conv
+}
+
+define i64 @slt_commuted(i32 %x, i32 %y) {
+; CHECK-LABEL: define i64 @slt_commuted(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[SMIN:%.*]] = call i32 @llvm.smin.i32(i32 [[Y]], i32 [[X]])
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i32 [[Y]], [[SMIN]]
+; CHECK-NEXT:    [[EXT:%.*]] = zext nneg i32 [[SUB]] to i64
+; CHECK-NEXT:    ret i64 [[EXT]]
+;
+  %cmp = icmp sgt i32 %y, %x
+  %sub = sub nsw i32 %y, %x
+  %sel = select i1 %cmp, i32 %sub, i32 0 ; (X < Y) ? Y - X : 0
+  %ext = sext i32 %sel to i64
+  ret i64 %ext
+}
+
+define i64 @sgt_commuted(i32 %x, i32 %y) {
+; CHECK-LABEL: define i64 @sgt_commuted(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[SEL:%.*]] = sub nsw i32 [[X]], [[TMP1]]
+; CHECK-NEXT:    [[EXT:%.*]] = zext nneg i32 [[SEL]] to i64
+; CHECK-NEXT:    ret i64 [[EXT]]
+;
+  %cmp = icmp slt i32 %y, %x ;
+  %sub = sub nsw i32 %x, %y ;
+  %sel = select i1 %cmp, i32 %sub, i32 0 ; (X > Y) ? X - Y : 0
+  %ext = sext i32 %sel to i64
+  ret i64 %ext
+}
+
+; TVal strictly greater than 0
+define i64 @slt_positive_tval(i32 %x, i32 %y) {
+;
+; CHECK-LABEL: define i64 @slt_positive_tval(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.smin.i32(i32 [[Y]], i32 [[X]])
+; CHECK-NEXT:    [[SEL:%.*]] = sub nsw i32 [[Y]], [[TMP1]]
+; CHECK-NEXT:    [[EXT:%.*]] = zext nneg i32 [[SEL]] to i64
+; CHECK-NEXT:    ret i64 [[EXT]]
+;
+  %cmp = icmp slt i32 %x, %y
+  %sub = sub nsw i32 %x, %y
+  %sel = select i1 %cmp, i32 1, i32 %sub ; (X < Y) ? 1 : X - Y
+  %ext = sext i32 %sel to i64
+  ret i64 %ext
+}
+
+; NEGATIVE TEST: FVal is negative
+define i64 @sgt_negative_fval(i32 %x, i32 %y) {
+; CHECK-LABEL: define i64 @sgt_negative_fval(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[Y]], [[X]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i32 [[SUB]], i32 -1
+; CHECK-NEXT:    [[EXT:%.*]] = sext i32 [[SEL]] to i64
+; CHECK-NEXT:    ret i64 [[EXT]]
+;
+  %cmp = icmp slt i32 %y, %x ;
+  %sub = sub nsw i32 %x, %y ;
+  %sel = select i1 %cmp, i32 %sub, i32 -1 ; (X > Y) ? X - Y : -1
+  %ext = sext i32 %sel to i64
+  ret i64 %ext
+}
+
+; NEGATIVE TEST: Wrong operand in select arm
+define i64 @slt_wrong_sub_order(i32 %x, i32 %y) {
+; CHECK-LABEL: define i64 @slt_wrong_sub_order(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i32 [[SUB]], i32 0
+; CHECK-NEXT:    [[EXT:%.*]] = sext i32 [[SEL]] to i64
+; CHECK-NEXT:    ret i64 [[EXT]]
+;
+  %cmp = icmp slt i32 %x, %y
+  %sub = sub nsw i32 %x, %y
+  %sel = select i1 %cmp, i32 %sub, i32 0 ; (X < Y) ? X - Y : 0
+  %ext = sext i32 %sel to i64
+  ret i64 %ext
+}
+
+; NEGATIVE TEST: TVal is negative
+define i64 @slt_negative_tval(i32 %x, i32 %y) {
+;
+; CHECK-LABEL: define i64 @slt_negative_tval(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i32 [[Y]], [[X]]
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i32 -1, i32 [[SUB]]
+; CHECK-NEXT:    [[EXT:%.*]] = sext i32 [[SEL]] to i64
+; CHECK-NEXT:    ret i64 [[EXT]]
+;
+  %cmp = icmp slt i32 %x, %y
+  %sub = sub nsw i32 %y, %x
+  %sel = select i1 %cmp, i32 -1, i32 %sub ; (X < Y) ? -1 : Y - X
+  %ext = sext i32 %sel to i64
+  ret i64 %ext
+}

``````````

</details>


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


More information about the llvm-commits mailing list