SDValue Z = Cmp.getOperand(0);
SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
- // If X is -1 or 0, then we have an opportunity to avoid constants required by
- // the cmp transform below. 'neg' sets the carry flag when Z != 0, so create 0
- // or -1 using 'sbb' with fake operands:
- // 0 - (Z != 0) --> sbb %eax, %eax, (neg Z)
- // -1 + (Z == 0) --> sbb %eax, %eax, (neg Z)
+ // If X is -1 or 0, then we have an opportunity to avoid constants required in
+ // the general case below.
if (auto *ConstantX = dyn_cast<ConstantSDNode>(X)) {
+ // 'neg' sets the carry flag when Z != 0, so create 0 or -1 using 'sbb' with
+ // fake operands:
+ // 0 - (Z != 0) --> sbb %eax, %eax, (neg Z)
+ // -1 + (Z == 0) --> sbb %eax, %eax, (neg Z)
if ((IsSub && CC == X86::COND_NE && ConstantX->isNullValue()) ||
(!IsSub && CC == X86::COND_E && ConstantX->isAllOnesValue())) {
SDValue Zero = DAG.getConstant(0, DL, VT);
DAG.getConstant(X86::COND_B, DL, MVT::i8),
SDValue(Neg.getNode(), 1));
}
+ // cmp with 1 sets the carry flag when Z == 0, so create 0 or -1 using 'sbb'
+ // with fake operands:
+ // 0 - (Z == 0) --> sbb %eax, %eax, (cmp Z, 1)
+ // -1 + (Z != 0) --> sbb %eax, %eax, (cmp Z, 1)
+ if ((IsSub && CC == X86::COND_E && ConstantX->isNullValue()) ||
+ (!IsSub && CC == X86::COND_NE && ConstantX->isAllOnesValue())) {
+ SDValue One = DAG.getConstant(1, DL, Z.getValueType());
+ SDValue Cmp1 = DAG.getNode(X86ISD::CMP, DL, MVT::i32, Z, One);
+ return DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
+ DAG.getConstant(X86::COND_B, DL, MVT::i8), Cmp1);
+ }
}
// (cmp Z, 1) sets the carry flag if Z is 0.
define i32 @i32_select_neg1_or_0_as_math(i32 %x) {
; CHECK-LABEL: i32_select_neg1_or_0_as_math:
; CHECK: # BB#0:
-; CHECK-NEXT: xorl %eax, %eax
; CHECK-NEXT: cmpl $1, %edi
-; CHECK-NEXT: sbbl $0, %eax
+; CHECK-NEXT: sbbl %eax, %eax
; CHECK-NEXT: retq
%cmp = icmp eq i32 %x, 0
%ext = zext i1 %cmp to i32
; CHECK-LABEL: i16_select_neg1_or_0_commuted:
; CHECK: # BB#0:
; CHECK-NEXT: cmpw $1, %di
-; CHECK-NEXT: movw $-1, %ax
-; CHECK-NEXT: sbbw $-1, %ax
+; CHECK-NEXT: sbbw %ax, %ax
; CHECK-NEXT: retq
%cmp = icmp ne i16 %x, 0
%sel = select i1 %cmp, i16 0, i16 -1
; CHECK-LABEL: i8_select_neg1_or_0_commuted_as_math:
; CHECK: # BB#0:
; CHECK-NEXT: cmpb $1, %dil
-; CHECK-NEXT: movb $-1, %al
-; CHECK-NEXT: sbbb $-1, %al
+; CHECK-NEXT: sbbb %al, %al
; CHECK-NEXT: retq
%cmp = icmp ne i8 %x, 0
%ext = zext i1 %cmp to i8