Skip to content

Commit 197e77b

Browse files
authored
[X86] Create special case for (a-b) - (a<b) -> sbb a, b (#161388)
1 parent c242aff commit 197e77b

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,32 @@ define i32 @uge_sext_add(i32 %0, i32 %1, i32 %2) {
365365
%6 = add nsw i32 %5, %0
366366
ret i32 %6
367367
}
368+
369+
define i32 @sub_sub_ugt(i32 %a, i32 %b) {
370+
; CHECK-LABEL: sub_sub_ugt:
371+
; CHECK: # %bb.0:
372+
; CHECK-NEXT: movl %edi, %eax
373+
; CHECK-NEXT: cmpl %edi, %esi
374+
; CHECK-NEXT: sbbl %esi, %eax
375+
; CHECK-NEXT: retq
376+
%cmp = icmp ugt i32 %a, %b
377+
%conv = zext i1 %cmp to i32
378+
%sub = sub i32 %a, %b
379+
%res = sub i32 %sub, %conv
380+
ret i32 %res
381+
}
382+
383+
define i32 @sub_sub_ult(i32 %a, i32 %b) {
384+
; CHECK-LABEL: sub_sub_ult:
385+
; CHECK: # %bb.0:
386+
; CHECK-NEXT: movl %edi, %eax
387+
; CHECK-NEXT: cmpl %edi, %esi
388+
; CHECK-NEXT: sbbl %esi, %eax
389+
; CHECK-NEXT: retq
390+
%cmp = icmp ult i32 %b, %a
391+
%conv = zext i1 %cmp to i32
392+
%sub = sub i32 %a, %b
393+
%res = sub i32 %sub, %conv
394+
ret i32 %res
395+
}
396+

0 commit comments

Comments
 (0)