Skip to content

Conversation

AZero13
Copy link
Contributor

@AZero13 AZero13 commented Sep 30, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Sep 30, 2025

@llvm/pr-subscribers-backend-x86

Author: AZero13 (AZero13)

Changes

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

2 Files Affected:

  • (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+10)
  • (modified) llvm/test/CodeGen/X86/sbb.ll (+14)
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index cd04ff5bc7ef4..23861913d36a1 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -52380,6 +52380,16 @@ static SDValue combineAddOrSubToADCOrSBB(bool IsSub, const SDLoc &DL, EVT VT,
                          DAG.getVTList(VT, MVT::i32), X,
                          DAG.getConstant(0, DL, VT), NewEFLAGS);
     }
+
+    if (IsSub && X.getOpcode() == X86ISD::SUB && X.getValueType().isInteger() &&
+        EFLAGS == X.getValue(1) && !isa<ConstantSDNode>(X.getOperand(1))) {
+      SDValue NewSub =
+          DAG.getNode(X86ISD::SUB, SDLoc(X), X.getNode()->getVTList(),
+                      X.getOperand(1), X.getOperand(0));
+      SDValue NewEFLAGS = NewSub.getValue(1);
+      return DAG.getNode(X86ISD::SBB, DL, DAG.getVTList(VT, MVT::i32),
+                         X.getOperand(0), X.getOperand(1), NewEFLAGS);
+    }
   }
 
   if (CC == X86::COND_AE) {
diff --git a/llvm/test/CodeGen/X86/sbb.ll b/llvm/test/CodeGen/X86/sbb.ll
index 78d609d3a17e6..579608b628582 100644
--- a/llvm/test/CodeGen/X86/sbb.ll
+++ b/llvm/test/CodeGen/X86/sbb.ll
@@ -365,3 +365,17 @@ define i32 @uge_sext_add(i32 %0, i32 %1, i32 %2) {
   %6 = add nsw i32 %5, %0
   ret i32 %6
 }
+
+define i32 @sub_sub_ugt(i32 %a, i32 %b) {
+; CHECK-LABEL: sub_sub_ugt:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    movl %edi, %eax
+; CHECK-NEXT:    cmpl %edi, %esi
+; CHECK-NEXT:    sbbl %esi, %eax
+; CHECK-NEXT:    retq
+  %cmp = icmp ugt i32 %a, %b
+  %conv = zext i1 %cmp to i32
+  %sub = sub i32 %a, %b
+  %res = sub i32 %sub, %conv
+  ret i32 %res
+}

Copy link

github-actions bot commented Sep 30, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@AZero13
Copy link
Contributor Author

AZero13 commented Sep 30, 2025

@topperc @phoebewang Thoughts?

@RKSimon RKSimon self-requested a review October 2, 2025 10:50
@AZero13 AZero13 force-pushed the ucmp2 branch 2 times, most recently from 21d3016 to 87c80aa Compare October 2, 2025 14:01
@AZero13 AZero13 requested review from RKSimon and phoebewang October 2, 2025 14:04
Copy link
Contributor

@phoebewang phoebewang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@AZero13
Copy link
Contributor Author

AZero13 commented Oct 2, 2025

@phoebewang Thank you. Can you please merge?

@phoebewang phoebewang merged commit 197e77b into llvm:main Oct 2, 2025
9 checks passed
@AZero13 AZero13 deleted the ucmp2 branch October 2, 2025 15:15
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants