Skip to content

Commit

Permalink
[x86] use SETCC_CARRY instead of SBB node for select lowering
Browse files Browse the repository at this point in the history
This is a suggested follow-up to D116765.
This removes a clear of the register operand, so it is better
for code size, but it does potentially create a false register
dependency on surrounding code. If that is a problem, it should
be solvable using dependency-breaking code that is used for
other instructions.

Differential Revision: https://reviews.llvm.org/D116804
  • Loading branch information
rotateright committed Jan 9, 2022
1 parent 80e2c58 commit aab1f55
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 102 deletions.
16 changes: 7 additions & 9 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Expand Up @@ -24552,8 +24552,6 @@ SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
} else if ((isAllOnesConstant(Op1) || isAllOnesConstant(Op2)) &&
(CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
SDValue Y = isAllOnesConstant(Op2) ? Op1 : Op2;

SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
SDVTList CmpVTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);

// 'X - 1' sets the carry flag if X == 0.
Expand All @@ -24563,18 +24561,18 @@ SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
// select (X == 0), Y, -1 --> 0 - X; or (sbb), Y
// select (X != 0), Y, -1 --> X - 1; or (sbb), Y
// select (X == 0), -1, Y --> X - 1; or (sbb), Y
SDValue Sub;
if (isAllOnesConstant(Op1) == (CondCode == X86::COND_NE)) {
SDValue Zero = DAG.getConstant(0, DL, CmpOp0.getValueType());
Cmp = DAG.getNode(X86ISD::SUB, DL, CmpVTs, Zero, CmpOp0);
Sub = DAG.getNode(X86ISD::SUB, DL, CmpVTs, Zero, CmpOp0);
} else {
SDValue One = DAG.getConstant(1, DL, CmpOp0.getValueType());
Cmp = DAG.getNode(X86ISD::SUB, DL, CmpVTs, CmpOp0, One);
Sub = DAG.getNode(X86ISD::SUB, DL, CmpVTs, CmpOp0, One);
}
// TODO: We don't need "0 - 0" here. This should use X86ISD::SETCC_CARRY.
SDValue Zero = DAG.getConstant(0, DL, Op.getValueType());
SDValue Res = // Res = 0 or -1.
DAG.getNode(X86ISD::SBB, DL, VTs, Zero, Zero, Cmp.getValue(1));
return DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
SDValue SBB = DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
DAG.getTargetConstant(X86::COND_B, DL, MVT::i8),
Sub.getValue(1));
return DAG.getNode(ISD::OR, DL, VT, SBB, Y);
} else if (!Subtarget.hasCMov() && CondCode == X86::COND_E &&
Cmp.getOperand(0).getOpcode() == ISD::AND &&
isOneConstant(Cmp.getOperand(0).getOperand(1))) {
Expand Down
1 change: 0 additions & 1 deletion llvm/test/CodeGen/X86/pr35972.ll
Expand Up @@ -5,7 +5,6 @@ define void @test3(i32 %c, <64 x i1>* %ptr) {
; CHECK-LABEL: test3:
; CHECK: # %bb.0:
; CHECK-NEXT: movl {{[0-9]+}}(%esp), %eax
; CHECK-NEXT: xorl %ecx, %ecx
; CHECK-NEXT: cmpl $1, {{[0-9]+}}(%esp)
; CHECK-NEXT: sbbl %ecx, %ecx
; CHECK-NEXT: kmovd %ecx, %k0
Expand Down
4 changes: 0 additions & 4 deletions llvm/test/CodeGen/X86/sdiv_fix_sat.ll
Expand Up @@ -1219,7 +1219,6 @@ define <4 x i32> @vec(<4 x i32> %x, <4 x i32> %y) nounwind {
; X86-NEXT: movl {{[-0-9]+}}(%e{{[sb]}}p), %eax # 4-byte Reload
; X86-NEXT: andl %eax, %ebx
; X86-NEXT: negl %eax
; X86-NEXT: movl $0, %ecx
; X86-NEXT: sbbl %ecx, %ecx
; X86-NEXT: orl {{[-0-9]+}}(%e{{[sb]}}p), %ecx # 4-byte Folded Reload
; X86-NEXT: orl {{[-0-9]+}}(%e{{[sb]}}p), %edx # 4-byte Folded Reload
Expand All @@ -1243,7 +1242,6 @@ define <4 x i32> @vec(<4 x i32> %x, <4 x i32> %y) nounwind {
; X86-NEXT: movl {{[-0-9]+}}(%e{{[sb]}}p), %eax # 4-byte Reload
; X86-NEXT: andl %eax, %edi
; X86-NEXT: negl %eax
; X86-NEXT: movl $0, %eax
; X86-NEXT: sbbl %eax, %eax
; X86-NEXT: orl {{[-0-9]+}}(%e{{[sb]}}p), %eax # 4-byte Folded Reload
; X86-NEXT: movl {{[-0-9]+}}(%e{{[sb]}}p), %ecx # 4-byte Reload
Expand All @@ -1270,7 +1268,6 @@ define <4 x i32> @vec(<4 x i32> %x, <4 x i32> %y) nounwind {
; X86-NEXT: movl {{[-0-9]+}}(%e{{[sb]}}p), %edx # 4-byte Reload
; X86-NEXT: andl %eax, %edx
; X86-NEXT: negl %eax
; X86-NEXT: movl $0, %eax
; X86-NEXT: sbbl %eax, %eax
; X86-NEXT: orl {{[-0-9]+}}(%e{{[sb]}}p), %eax # 4-byte Folded Reload
; X86-NEXT: movl {{[-0-9]+}}(%e{{[sb]}}p), %ecx # 4-byte Reload
Expand All @@ -1294,7 +1291,6 @@ define <4 x i32> @vec(<4 x i32> %x, <4 x i32> %y) nounwind {
; X86-NEXT: movl {{[-0-9]+}}(%e{{[sb]}}p), %edi # 4-byte Reload
; X86-NEXT: andl %eax, %edi
; X86-NEXT: negl %eax
; X86-NEXT: movl $0, %eax
; X86-NEXT: sbbl %eax, %eax
; X86-NEXT: orl {{[-0-9]+}}(%e{{[sb]}}p), %eax # 4-byte Folded Reload
; X86-NEXT: movl {{[-0-9]+}}(%e{{[sb]}}p), %ecx # 4-byte Reload
Expand Down
199 changes: 129 additions & 70 deletions llvm/test/CodeGen/X86/select.ll
Expand Up @@ -629,13 +629,21 @@ define void @test8(i1 %c, <6 x i32>* %dst.addr, <6 x i32> %src1,<6 x i32> %src2)
;; Test integer select between values and constants.

define i64 @test9(i64 %x, i64 %y) nounwind readnone ssp noredzone {
; CHECK-LABEL: test9:
; CHECK: ## %bb.0:
; CHECK-NEXT: xorl %eax, %eax
; CHECK-NEXT: cmpq $1, %rdi
; CHECK-NEXT: sbbq %rax, %rax
; CHECK-NEXT: orq %rsi, %rax
; CHECK-NEXT: retq
; GENERIC-LABEL: test9:
; GENERIC: ## %bb.0:
; GENERIC-NEXT: cmpq $1, %rdi
; GENERIC-NEXT: sbbq %rax, %rax
; GENERIC-NEXT: orq %rsi, %rax
; GENERIC-NEXT: retq
;
; ATOM-LABEL: test9:
; ATOM: ## %bb.0:
; ATOM-NEXT: cmpq $1, %rdi
; ATOM-NEXT: sbbq %rax, %rax
; ATOM-NEXT: orq %rsi, %rax
; ATOM-NEXT: nop
; ATOM-NEXT: nop
; ATOM-NEXT: retq
;
; ATHLON-LABEL: test9:
; ATHLON: ## %bb.0:
Expand Down Expand Up @@ -669,13 +677,21 @@ define i64 @test9(i64 %x, i64 %y) nounwind readnone ssp noredzone {

;; Same as test9
define i64 @test9a(i64 %x, i64 %y) nounwind readnone ssp noredzone {
; CHECK-LABEL: test9a:
; CHECK: ## %bb.0:
; CHECK-NEXT: xorl %eax, %eax
; CHECK-NEXT: cmpq $1, %rdi
; CHECK-NEXT: sbbq %rax, %rax
; CHECK-NEXT: orq %rsi, %rax
; CHECK-NEXT: retq
; GENERIC-LABEL: test9a:
; GENERIC: ## %bb.0:
; GENERIC-NEXT: cmpq $1, %rdi
; GENERIC-NEXT: sbbq %rax, %rax
; GENERIC-NEXT: orq %rsi, %rax
; GENERIC-NEXT: retq
;
; ATOM-LABEL: test9a:
; ATOM: ## %bb.0:
; ATOM-NEXT: cmpq $1, %rdi
; ATOM-NEXT: sbbq %rax, %rax
; ATOM-NEXT: orq %rsi, %rax
; ATOM-NEXT: nop
; ATOM-NEXT: nop
; ATOM-NEXT: retq
;
; ATHLON-LABEL: test9a:
; ATHLON: ## %bb.0:
Expand Down Expand Up @@ -790,13 +806,21 @@ define i64 @test10(i64 %x, i64 %y) nounwind readnone ssp noredzone {
}

define i64 @test11(i64 %x, i64 %y) nounwind readnone ssp noredzone {
; CHECK-LABEL: test11:
; CHECK: ## %bb.0:
; CHECK-NEXT: xorl %eax, %eax
; CHECK-NEXT: negq %rdi
; CHECK-NEXT: sbbq %rax, %rax
; CHECK-NEXT: orq %rsi, %rax
; CHECK-NEXT: retq
; GENERIC-LABEL: test11:
; GENERIC: ## %bb.0:
; GENERIC-NEXT: negq %rdi
; GENERIC-NEXT: sbbq %rax, %rax
; GENERIC-NEXT: orq %rsi, %rax
; GENERIC-NEXT: retq
;
; ATOM-LABEL: test11:
; ATOM: ## %bb.0:
; ATOM-NEXT: negq %rdi
; ATOM-NEXT: sbbq %rax, %rax
; ATOM-NEXT: orq %rsi, %rax
; ATOM-NEXT: nop
; ATOM-NEXT: nop
; ATOM-NEXT: retq
;
; ATHLON-LABEL: test11:
; ATHLON: ## %bb.0:
Expand Down Expand Up @@ -829,13 +853,21 @@ define i64 @test11(i64 %x, i64 %y) nounwind readnone ssp noredzone {
}

define i64 @test11a(i64 %x, i64 %y) nounwind readnone ssp noredzone {
; CHECK-LABEL: test11a:
; CHECK: ## %bb.0:
; CHECK-NEXT: xorl %eax, %eax
; CHECK-NEXT: negq %rdi
; CHECK-NEXT: sbbq %rax, %rax
; CHECK-NEXT: orq %rsi, %rax
; CHECK-NEXT: retq
; GENERIC-LABEL: test11a:
; GENERIC: ## %bb.0:
; GENERIC-NEXT: negq %rdi
; GENERIC-NEXT: sbbq %rax, %rax
; GENERIC-NEXT: orq %rsi, %rax
; GENERIC-NEXT: retq
;
; ATOM-LABEL: test11a:
; ATOM: ## %bb.0:
; ATOM-NEXT: negq %rdi
; ATOM-NEXT: sbbq %rax, %rax
; ATOM-NEXT: orq %rsi, %rax
; ATOM-NEXT: nop
; ATOM-NEXT: nop
; ATOM-NEXT: retq
;
; ATHLON-LABEL: test11a:
; ATHLON: ## %bb.0:
Expand Down Expand Up @@ -867,13 +899,21 @@ define i64 @test11a(i64 %x, i64 %y) nounwind readnone ssp noredzone {
}

define i32 @eqzero_const_or_all_ones(i32 %x) {
; CHECK-LABEL: eqzero_const_or_all_ones:
; CHECK: ## %bb.0:
; CHECK-NEXT: xorl %eax, %eax
; CHECK-NEXT: negl %edi
; CHECK-NEXT: sbbl %eax, %eax
; CHECK-NEXT: orl $42, %eax
; CHECK-NEXT: retq
; GENERIC-LABEL: eqzero_const_or_all_ones:
; GENERIC: ## %bb.0:
; GENERIC-NEXT: negl %edi
; GENERIC-NEXT: sbbl %eax, %eax
; GENERIC-NEXT: orl $42, %eax
; GENERIC-NEXT: retq
;
; ATOM-LABEL: eqzero_const_or_all_ones:
; ATOM: ## %bb.0:
; ATOM-NEXT: negl %edi
; ATOM-NEXT: sbbl %eax, %eax
; ATOM-NEXT: orl $42, %eax
; ATOM-NEXT: nop
; ATOM-NEXT: nop
; ATOM-NEXT: retq
;
; ATHLON-LABEL: eqzero_const_or_all_ones:
; ATHLON: ## %bb.0:
Expand All @@ -885,55 +925,66 @@ define i32 @eqzero_const_or_all_ones(i32 %x) {
;
; MCU-LABEL: eqzero_const_or_all_ones:
; MCU: # %bb.0:
; MCU-NEXT: xorl %ecx, %ecx
; MCU-NEXT: negl %eax
; MCU-NEXT: sbbl %ecx, %ecx
; MCU-NEXT: orl $42, %ecx
; MCU-NEXT: movl %ecx, %eax
; MCU-NEXT: sbbl %eax, %eax
; MCU-NEXT: orl $42, %eax
; MCU-NEXT: retl
%z = icmp eq i32 %x, 0
%r = select i1 %z, i32 42, i32 -1
ret i32 %r
}

define i32 @nezero_const_or_all_ones(i32 %x) {
; CHECK-LABEL: nezero_const_or_all_ones:
; CHECK: ## %bb.0:
; CHECK-NEXT: xorl %eax, %eax
; CHECK-NEXT: cmpl $1, %edi
; CHECK-NEXT: sbbl %eax, %eax
; CHECK-NEXT: orl $42, %eax
; CHECK-NEXT: retq
; GENERIC-LABEL: nezero_const_or_all_ones:
; GENERIC: ## %bb.0:
; GENERIC-NEXT: cmpl $1, %edi
; GENERIC-NEXT: sbbl %eax, %eax
; GENERIC-NEXT: orl $42, %eax
; GENERIC-NEXT: retq
;
; ATOM-LABEL: nezero_const_or_all_ones:
; ATOM: ## %bb.0:
; ATOM-NEXT: cmpl $1, %edi
; ATOM-NEXT: sbbl %eax, %eax
; ATOM-NEXT: orl $42, %eax
; ATOM-NEXT: nop
; ATOM-NEXT: nop
; ATOM-NEXT: retq
;
; ATHLON-LABEL: nezero_const_or_all_ones:
; ATHLON: ## %bb.0:
; ATHLON-NEXT: xorl %eax, %eax
; ATHLON-NEXT: cmpl $1, {{[0-9]+}}(%esp)
; ATHLON-NEXT: sbbl %eax, %eax
; ATHLON-NEXT: orl $42, %eax
; ATHLON-NEXT: retl
;
; MCU-LABEL: nezero_const_or_all_ones:
; MCU: # %bb.0:
; MCU-NEXT: xorl %ecx, %ecx
; MCU-NEXT: cmpl $1, %eax
; MCU-NEXT: sbbl %ecx, %ecx
; MCU-NEXT: orl $42, %ecx
; MCU-NEXT: movl %ecx, %eax
; MCU-NEXT: sbbl %eax, %eax
; MCU-NEXT: orl $42, %eax
; MCU-NEXT: retl
%z = icmp ne i32 %x, 0
%r = select i1 %z, i32 42, i32 -1
ret i32 %r
}

define i64 @eqzero_all_ones_or_const(i64 %x) {
; CHECK-LABEL: eqzero_all_ones_or_const:
; CHECK: ## %bb.0:
; CHECK-NEXT: xorl %eax, %eax
; CHECK-NEXT: cmpq $1, %rdi
; CHECK-NEXT: sbbq %rax, %rax
; CHECK-NEXT: orq $42, %rax
; CHECK-NEXT: retq
; GENERIC-LABEL: eqzero_all_ones_or_const:
; GENERIC: ## %bb.0:
; GENERIC-NEXT: cmpq $1, %rdi
; GENERIC-NEXT: sbbq %rax, %rax
; GENERIC-NEXT: orq $42, %rax
; GENERIC-NEXT: retq
;
; ATOM-LABEL: eqzero_all_ones_or_const:
; ATOM: ## %bb.0:
; ATOM-NEXT: cmpq $1, %rdi
; ATOM-NEXT: sbbq %rax, %rax
; ATOM-NEXT: orq $42, %rax
; ATOM-NEXT: nop
; ATOM-NEXT: nop
; ATOM-NEXT: retq
;
; ATHLON-LABEL: eqzero_all_ones_or_const:
; ATHLON: ## %bb.0:
Expand Down Expand Up @@ -963,14 +1014,23 @@ define i64 @eqzero_all_ones_or_const(i64 %x) {
}

define i8 @nezero_all_ones_or_const(i8 %x) {
; CHECK-LABEL: nezero_all_ones_or_const:
; CHECK: ## %bb.0:
; CHECK-NEXT: xorl %eax, %eax
; CHECK-NEXT: negb %dil
; CHECK-NEXT: sbbl %eax, %eax
; CHECK-NEXT: orb $42, %al
; CHECK-NEXT: ## kill: def $al killed $al killed $eax
; CHECK-NEXT: retq
; GENERIC-LABEL: nezero_all_ones_or_const:
; GENERIC: ## %bb.0:
; GENERIC-NEXT: negb %dil
; GENERIC-NEXT: sbbl %eax, %eax
; GENERIC-NEXT: orb $42, %al
; GENERIC-NEXT: ## kill: def $al killed $al killed $eax
; GENERIC-NEXT: retq
;
; ATOM-LABEL: nezero_all_ones_or_const:
; ATOM: ## %bb.0:
; ATOM-NEXT: negb %dil
; ATOM-NEXT: sbbl %eax, %eax
; ATOM-NEXT: orb $42, %al
; ATOM-NEXT: ## kill: def $al killed $al killed $eax
; ATOM-NEXT: nop
; ATOM-NEXT: nop
; ATOM-NEXT: retq
;
; ATHLON-LABEL: nezero_all_ones_or_const:
; ATHLON: ## %bb.0:
Expand All @@ -983,11 +1043,10 @@ define i8 @nezero_all_ones_or_const(i8 %x) {
;
; MCU-LABEL: nezero_all_ones_or_const:
; MCU: # %bb.0:
; MCU-NEXT: xorl %ecx, %ecx
; MCU-NEXT: negb %al
; MCU-NEXT: sbbl %ecx, %ecx
; MCU-NEXT: orb $42, %cl
; MCU-NEXT: movl %ecx, %eax
; MCU-NEXT: sbbl %eax, %eax
; MCU-NEXT: orb $42, %al
; MCU-NEXT: # kill: def $al killed $al killed $eax
; MCU-NEXT: retl
%z = icmp ne i8 %x, 0
%r = select i1 %z, i8 -1, i8 42
Expand Down
1 change: 0 additions & 1 deletion llvm/test/CodeGen/X86/shl-crash-on-legalize.ll
Expand Up @@ -14,7 +14,6 @@ define i32 @PR29058(i8 %x, i32 %y) {
; CHECK-NEXT: testb %dil, %dil
; CHECK-NEXT: movl $2147483646, %eax # imm = 0x7FFFFFFE
; CHECK-NEXT: cmovnel %esi, %eax
; CHECK-NEXT: xorl %ecx, %ecx
; CHECK-NEXT: cmpb $1, %dil
; CHECK-NEXT: sbbl %ecx, %ecx
; CHECK-NEXT: orb %sil, %cl
Expand Down

0 comments on commit aab1f55

Please sign in to comment.