Skip to content

Commit 774fba6

Browse files
committed
[X86] Create special case for (a-b) - (a< b) -> sbb a, b
1 parent 25e179d commit 774fba6

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52388,16 +52388,41 @@ static SDValue combineAddOrSubToADCOrSBB(bool IsSub, const SDLoc &DL, EVT VT,
5238852388
// Do not flip "e > c", where "c" is a constant, because Cmp instruction
5238952389
// cannot take an immediate as its first operand.
5239052390
//
52391-
if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.getNode()->hasOneUse() &&
52392-
EFLAGS.getValueType().isInteger() &&
52393-
!isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
52394-
SDValue NewSub =
52395-
DAG.getNode(X86ISD::SUB, SDLoc(EFLAGS), EFLAGS.getNode()->getVTList(),
52396-
EFLAGS.getOperand(1), EFLAGS.getOperand(0));
52397-
SDValue NewEFLAGS = NewSub.getValue(EFLAGS.getResNo());
52391+
// If EFLAGS is from a CMP that compares the same operands as the earlier
52392+
// SUB producing X (i.e. CMP X, Y), we can directly use the carry flag with
52393+
// SBB/ADC without creating a flipped SUB.
52394+
if (EFLAGS.getOpcode() == X86ISD::CMP &&
52395+
EFLAGS.getValueType().isInteger() && X == EFLAGS.getOperand(0)) {
5239852396
return DAG.getNode(IsSub ? X86ISD::SBB : X86ISD::ADC, DL,
5239952397
DAG.getVTList(VT, MVT::i32), X,
52400-
DAG.getConstant(0, DL, VT), NewEFLAGS);
52398+
DAG.getConstant(0, DL, VT), EFLAGS);
52399+
}
52400+
52401+
if (EFLAGS.getOpcode() == X86ISD::SUB &&
52402+
EFLAGS.getValueType().isInteger() &&
52403+
!isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
52404+
// Only create NewSub if we know one of the folds will succeed to avoid
52405+
// introducing a temporary node that may persist and affect one-use checks
52406+
// below.
52407+
if (EFLAGS.getNode()->hasOneUse()) {
52408+
SDValue NewSub = DAG.getNode(
52409+
X86ISD::SUB, SDLoc(EFLAGS), EFLAGS.getNode()->getVTList(),
52410+
EFLAGS.getOperand(1), EFLAGS.getOperand(0));
52411+
SDValue NewEFLAGS = NewSub.getValue(EFLAGS.getResNo());
52412+
return DAG.getNode(IsSub ? X86ISD::SBB : X86ISD::ADC, DL,
52413+
DAG.getVTList(VT, MVT::i32), X,
52414+
DAG.getConstant(0, DL, VT), NewEFLAGS);
52415+
}
52416+
52417+
if (IsSub && X == EFLAGS.getValue(0)) {
52418+
SDValue NewSub = DAG.getNode(
52419+
X86ISD::SUB, SDLoc(EFLAGS), EFLAGS.getNode()->getVTList(),
52420+
EFLAGS.getOperand(1), EFLAGS.getOperand(0));
52421+
SDValue NewEFLAGS = NewSub.getValue(EFLAGS.getResNo());
52422+
return DAG.getNode(X86ISD::SBB, DL, DAG.getVTList(VT, MVT::i32),
52423+
EFLAGS.getOperand(0), EFLAGS.getOperand(1),
52424+
NewEFLAGS);
52425+
}
5240152426
}
5240252427
}
5240352428

llvm/test/CodeGen/X86/sbb.ll

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,8 @@ define i32 @sub_sub_ugt(i32 %a, i32 %b) {
370370
; CHECK-LABEL: sub_sub_ugt:
371371
; CHECK: # %bb.0:
372372
; CHECK-NEXT: movl %edi, %eax
373-
; CHECK-NEXT: xorl %ecx, %ecx
374-
; CHECK-NEXT: subl %esi, %eax
375-
; CHECK-NEXT: seta %cl
376-
; CHECK-NEXT: subl %ecx, %eax
373+
; CHECK-NEXT: cmpl %edi, %esi
374+
; CHECK-NEXT: sbbl %esi, %eax
377375
; CHECK-NEXT: retq
378376
%cmp = icmp ugt i32 %a, %b
379377
%conv = zext i1 %cmp to i32
@@ -386,10 +384,8 @@ define i32 @sub_sub_ult(i32 %a, i32 %b) {
386384
; CHECK-LABEL: sub_sub_ult:
387385
; CHECK: # %bb.0:
388386
; CHECK-NEXT: movl %edi, %eax
389-
; CHECK-NEXT: xorl %ecx, %ecx
390-
; CHECK-NEXT: subl %esi, %eax
391-
; CHECK-NEXT: seta %cl
392-
; CHECK-NEXT: subl %ecx, %eax
387+
; CHECK-NEXT: cmpl %edi, %esi
388+
; CHECK-NEXT: sbbl %esi, %eax
393389
; CHECK-NEXT: retq
394390
%cmp = icmp ult i32 %b, %a
395391
%conv = zext i1 %cmp to i32

0 commit comments

Comments
 (0)