Skip to content

[X86] Use the standard cmp+cmov for select (X != 0), -1, Y if we will be setting sbb to 0 anyway #149672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24917,6 +24917,30 @@ static SDValue LowerSELECTWithCmpZero(SDValue CmpVal, SDValue LHS, SDValue RHS,
if ((X86CC == X86::COND_E || X86CC == X86::COND_NE) &&
(isAllOnesConstant(LHS) || isAllOnesConstant(RHS))) {
SDValue Y = isAllOnesConstant(RHS) ? LHS : RHS;

// If CMOV is available, use it instead. Only prefer CMOV when SBB
// dependency breaking is not available or when CMOV is likely to be more
// efficient
if (Subtarget.canUseCMOV() &&
(VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) &&
!Subtarget.hasSBBDepBreaking()) {
// Create comparison against zero to set EFLAGS
SDValue Zero = DAG.getConstant(0, DL, CmpVT);
SDValue Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpVal, Zero);

// For CMOV: FalseVal is used when condition is false, TrueVal when
// condition is true We want: when X==0 return -1, when X!=0 return Y So
// condition should be (X == 0), TrueVal = -1, FalseVal = Y The SBB
// pattern implements: (CmpVal X86CC 0) ? LHS : RHS We need to implement
// exactly the same select operation with CMOV CMOV semantics: CMOV
// condition, TrueVal, FalseVal Returns TrueVal if condition is true,
// FalseVal if condition is false

return DAG.getNode(X86ISD::CMOV, DL, VT, RHS, LHS,
DAG.getTargetConstant(X86CC, DL, MVT::i8), Cmp);
}

// Fall back to SBB pattern for older processors or unsupported types
SDVTList CmpVTs = DAG.getVTList(CmpVT, MVT::i32);

// 'X - 1' sets the carry flag if X == 0.
Expand Down
161 changes: 57 additions & 104 deletions llvm/test/CodeGen/X86/bmi-select-distrib.ll
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,23 @@ define i32 @and_neg_select_pos_i32(i1 %a0, i32 inreg %a1) nounwind {
define i16 @and_select_neg_i16(i1 %a0, i16 %a1) nounwind {
; X86-LABEL: and_select_neg_i16:
; X86: # %bb.0:
; X86-NEXT: pushl %esi
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: andb $1, %cl
; X86-NEXT: movl {{[0-9]+}}(%esp), %edx
; X86-NEXT: movl %edx, %esi
; X86-NEXT: negl %esi
; X86-NEXT: xorl %eax, %eax
; X86-NEXT: cmpb $1, %cl
; X86-NEXT: sbbl %eax, %eax
; X86-NEXT: orl %esi, %eax
; X86-NEXT: andl %edx, %eax
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: movl %ecx, %edx
; X86-NEXT: negl %edx
; X86-NEXT: testb $1, {{[0-9]+}}(%esp)
; X86-NEXT: movw $-1, %ax
; X86-NEXT: cmovnew %dx, %ax
; X86-NEXT: andl %ecx, %eax
; X86-NEXT: # kill: def $ax killed $ax killed $eax
; X86-NEXT: popl %esi
; X86-NEXT: retl
;
; X64-LABEL: and_select_neg_i16:
; X64: # %bb.0:
; X64-NEXT: andb $1, %dil
; X64-NEXT: movl %esi, %ecx
; X64-NEXT: negl %ecx
; X64-NEXT: xorl %eax, %eax
; X64-NEXT: cmpb $1, %dil
; X64-NEXT: sbbl %eax, %eax
; X64-NEXT: orl %ecx, %eax
; X64-NEXT: testb $1, %dil
; X64-NEXT: movw $-1, %ax
; X64-NEXT: cmovnew %cx, %ax
; X64-NEXT: andl %esi, %eax
; X64-NEXT: # kill: def $ax killed $ax killed $eax
; X64-NEXT: retq
Expand Down Expand Up @@ -200,22 +193,17 @@ define <4 x i32> @and_select_neg_v4xi32(i1 %a0, <4 x i32> %a1) nounwind {
define i32 @and_select_no_neg(i1 %a0, i32 inreg %a1) nounwind {
; X86-LABEL: and_select_no_neg:
; X86: # %bb.0:
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: andb $1, %cl
; X86-NEXT: xorl %edx, %edx
; X86-NEXT: cmpb $1, %cl
; X86-NEXT: sbbl %edx, %edx
; X86-NEXT: orl %eax, %edx
; X86-NEXT: andl %edx, %eax
; X86-NEXT: testb $1, {{[0-9]+}}(%esp)
; X86-NEXT: movl $-1, %ecx
; X86-NEXT: cmovnel %eax, %ecx
; X86-NEXT: andl %ecx, %eax
; X86-NEXT: retl
;
; X64-LABEL: and_select_no_neg:
; X64: # %bb.0:
; X64-NEXT: andb $1, %dil
; X64-NEXT: xorl %eax, %eax
; X64-NEXT: cmpb $1, %dil
; X64-NEXT: sbbl %eax, %eax
; X64-NEXT: orl %esi, %eax
; X64-NEXT: testb $1, %dil
; X64-NEXT: movl $-1, %eax
; X64-NEXT: cmovnel %esi, %eax
; X64-NEXT: andl %esi, %eax
; X64-NEXT: retq
%sub = sub i32 %a1, 0
Expand Down Expand Up @@ -255,26 +243,19 @@ define i32 @and_select_neg_wrong_const(i1 %a0, i32 inreg %a1) nounwind {
define i32 @and_select_neg_different_op(i1 %a0, i32 inreg %a1, i32 inreg %a2) nounwind {
; X86-LABEL: and_select_neg_different_op:
; X86: # %bb.0:
; X86-NEXT: pushl %esi
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: andb $1, %cl
; X86-NEXT: negl %edx
; X86-NEXT: xorl %esi, %esi
; X86-NEXT: cmpb $1, %cl
; X86-NEXT: sbbl %esi, %esi
; X86-NEXT: orl %edx, %esi
; X86-NEXT: andl %esi, %eax
; X86-NEXT: popl %esi
; X86-NEXT: testb $1, {{[0-9]+}}(%esp)
; X86-NEXT: movl $-1, %ecx
; X86-NEXT: cmovnel %edx, %ecx
; X86-NEXT: andl %ecx, %eax
; X86-NEXT: retl
;
; X64-LABEL: and_select_neg_different_op:
; X64: # %bb.0:
; X64-NEXT: andb $1, %dil
; X64-NEXT: negl %edx
; X64-NEXT: xorl %eax, %eax
; X64-NEXT: cmpb $1, %dil
; X64-NEXT: sbbl %eax, %eax
; X64-NEXT: orl %edx, %eax
; X64-NEXT: testb $1, %dil
; X64-NEXT: movl $-1, %eax
; X64-NEXT: cmovnel %edx, %eax
; X64-NEXT: andl %esi, %eax
; X64-NEXT: retq
%sub = sub i32 0, %a2
Expand Down Expand Up @@ -427,29 +408,22 @@ define i64 @and_select_sub_1_to_blsr_i64(i1 %a0, i64 %a1) nounwind {
define i16 @and_select_sub_1_i16(i1 %a0, i16 %a1) nounwind {
; X86-LABEL: and_select_sub_1_i16:
; X86: # %bb.0:
; X86-NEXT: pushl %esi
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: andb $1, %cl
; X86-NEXT: movl {{[0-9]+}}(%esp), %edx
; X86-NEXT: leal -1(%edx), %esi
; X86-NEXT: xorl %eax, %eax
; X86-NEXT: cmpb $1, %cl
; X86-NEXT: sbbl %eax, %eax
; X86-NEXT: orl %esi, %eax
; X86-NEXT: andl %edx, %eax
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: leal -1(%ecx), %edx
; X86-NEXT: testb $1, {{[0-9]+}}(%esp)
; X86-NEXT: movw $-1, %ax
; X86-NEXT: cmovnew %dx, %ax
; X86-NEXT: andl %ecx, %eax
; X86-NEXT: # kill: def $ax killed $ax killed $eax
; X86-NEXT: popl %esi
; X86-NEXT: retl
;
; X64-LABEL: and_select_sub_1_i16:
; X64: # %bb.0:
; X64-NEXT: # kill: def $esi killed $esi def $rsi
; X64-NEXT: andb $1, %dil
; X64-NEXT: leal -1(%rsi), %ecx
; X64-NEXT: xorl %eax, %eax
; X64-NEXT: cmpb $1, %dil
; X64-NEXT: sbbl %eax, %eax
; X64-NEXT: orl %ecx, %eax
; X64-NEXT: testb $1, %dil
; X64-NEXT: movw $-1, %ax
; X64-NEXT: cmovnew %cx, %ax
; X64-NEXT: andl %esi, %eax
; X64-NEXT: # kill: def $ax killed $ax killed $eax
; X64-NEXT: retq
Expand Down Expand Up @@ -492,27 +466,20 @@ define <4 x i32> @and_select_sub_1_v4xi32(i1 %a0, <4 x i32> %a1) nounwind {
define i32 @and_select_no_sub_1(i1 %a0, i32 inreg %a1) nounwind {
; X86-LABEL: and_select_no_sub_1:
; X86: # %bb.0:
; X86-NEXT: pushl %esi
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: andb $1, %cl
; X86-NEXT: leal -2(%eax), %edx
; X86-NEXT: xorl %esi, %esi
; X86-NEXT: cmpb $1, %cl
; X86-NEXT: sbbl %esi, %esi
; X86-NEXT: orl %edx, %esi
; X86-NEXT: andl %esi, %eax
; X86-NEXT: popl %esi
; X86-NEXT: leal -2(%eax), %ecx
; X86-NEXT: testb $1, {{[0-9]+}}(%esp)
; X86-NEXT: movl $-1, %edx
; X86-NEXT: cmovnel %ecx, %edx
; X86-NEXT: andl %edx, %eax
; X86-NEXT: retl
;
; X64-LABEL: and_select_no_sub_1:
; X64: # %bb.0:
; X64-NEXT: # kill: def $esi killed $esi def $rsi
; X64-NEXT: andb $1, %dil
; X64-NEXT: leal -2(%rsi), %ecx
; X64-NEXT: xorl %eax, %eax
; X64-NEXT: cmpb $1, %dil
; X64-NEXT: sbbl %eax, %eax
; X64-NEXT: orl %ecx, %eax
; X64-NEXT: testb $1, %dil
; X64-NEXT: movl $-1, %eax
; X64-NEXT: cmovnel %ecx, %eax
; X64-NEXT: andl %esi, %eax
; X64-NEXT: retq
%sub = add i32 %a1, -2
Expand Down Expand Up @@ -551,27 +518,20 @@ define i32 @and_select_sub_1_wrong_const(i1 %a0, i32 inreg %a1) nounwind {
define i32 @and_select_sub_1_different_op(i1 %a0, i32 inreg %a1, i32 inreg %a2) nounwind {
; X86-LABEL: and_select_sub_1_different_op:
; X86: # %bb.0:
; X86-NEXT: pushl %esi
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: andb $1, %cl
; X86-NEXT: decl %edx
; X86-NEXT: xorl %esi, %esi
; X86-NEXT: cmpb $1, %cl
; X86-NEXT: sbbl %esi, %esi
; X86-NEXT: orl %edx, %esi
; X86-NEXT: andl %esi, %eax
; X86-NEXT: popl %esi
; X86-NEXT: leal -1(%edx), %ecx
; X86-NEXT: testb $1, {{[0-9]+}}(%esp)
; X86-NEXT: movl $-1, %edx
; X86-NEXT: cmovnel %ecx, %edx
; X86-NEXT: andl %edx, %eax
; X86-NEXT: retl
;
; X64-LABEL: and_select_sub_1_different_op:
; X64: # %bb.0:
; X64-NEXT: # kill: def $edx killed $edx def $rdx
; X64-NEXT: andb $1, %dil
; X64-NEXT: leal -1(%rdx), %ecx
; X64-NEXT: xorl %eax, %eax
; X64-NEXT: cmpb $1, %dil
; X64-NEXT: sbbl %eax, %eax
; X64-NEXT: orl %ecx, %eax
; X64-NEXT: testb $1, %dil
; X64-NEXT: movl $-1, %eax
; X64-NEXT: cmovnel %ecx, %eax
; X64-NEXT: andl %esi, %eax
; X64-NEXT: retq
%sub = add i32 %a2, -1
Expand Down Expand Up @@ -809,27 +769,20 @@ define i32 @xor_select_no_sub_1(i1 %a0, i32 inreg %a1) nounwind {
define i32 @xor_select_sub_1_wrong_const(i1 %a0, i32 inreg %a1) nounwind {
; X86-LABEL: xor_select_sub_1_wrong_const:
; X86: # %bb.0:
; X86-NEXT: pushl %esi
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: andb $1, %cl
; X86-NEXT: leal -1(%eax), %edx
; X86-NEXT: xorl %esi, %esi
; X86-NEXT: cmpb $1, %cl
; X86-NEXT: sbbl %esi, %esi
; X86-NEXT: orl %edx, %esi
; X86-NEXT: xorl %esi, %eax
; X86-NEXT: popl %esi
; X86-NEXT: leal -1(%eax), %ecx
; X86-NEXT: testb $1, {{[0-9]+}}(%esp)
; X86-NEXT: movl $-1, %edx
; X86-NEXT: cmovnel %ecx, %edx
; X86-NEXT: xorl %edx, %eax
; X86-NEXT: retl
;
; X64-LABEL: xor_select_sub_1_wrong_const:
; X64: # %bb.0:
; X64-NEXT: # kill: def $esi killed $esi def $rsi
; X64-NEXT: andb $1, %dil
; X64-NEXT: leal -1(%rsi), %ecx
; X64-NEXT: xorl %eax, %eax
; X64-NEXT: cmpb $1, %dil
; X64-NEXT: sbbl %eax, %eax
; X64-NEXT: orl %ecx, %eax
; X64-NEXT: testb $1, %dil
; X64-NEXT: movl $-1, %eax
; X64-NEXT: cmovnel %ecx, %eax
; X64-NEXT: xorl %esi, %eax
; X64-NEXT: retq
%sub = add i32 %a1, -1
Expand Down
7 changes: 4 additions & 3 deletions llvm/test/CodeGen/X86/pr35972.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ define void @test3(i32 %c, ptr %ptr) {
; 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
; CHECK-NEXT: cmpl $0, {{[0-9]+}}(%esp)
; CHECK-NEXT: movl $-1, %edx
; CHECK-NEXT: cmovnel %ecx, %edx
; CHECK-NEXT: kmovd %edx, %k0
; CHECK-NEXT: kunpckdq %k0, %k0, %k0
; CHECK-NEXT: kmovq %k0, (%eax)
; CHECK-NEXT: retl
Expand Down
34 changes: 15 additions & 19 deletions llvm/test/CodeGen/X86/sbb-false-dep.ll
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,32 @@ define i32 @mallocbench_gs(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 n
; CHECK-NEXT: pushq %r14
; CHECK-NEXT: pushq %r12
; CHECK-NEXT: pushq %rbx
; CHECK-NEXT: movl %r8d, %ebp
; CHECK-NEXT: movl %ecx, %r14d
; CHECK-NEXT: movl %edx, %r15d
; CHECK-NEXT: movq %rsi, %rbx
; CHECK-NEXT: movl %r8d, %ebx
; CHECK-NEXT: movl %ecx, %ebp
; CHECK-NEXT: movl %edx, %r14d
; CHECK-NEXT: movq %rsi, %r15
; CHECK-NEXT: movq %rdi, %r12
; CHECK-NEXT: movq (%rsi), %rdi
; CHECK-NEXT: movq 8(%rsi), %rsi
; CHECK-NEXT: movq %rbx, %rdx
; CHECK-NEXT: movq %r15, %rdx
; CHECK-NEXT: callq foo1@PLT
; CHECK-NEXT: movq 8(%rbx), %rax
; CHECK-NEXT: testl %ebx, %ebx
; CHECK-NEXT: movq 8(%r15), %rax
; CHECK-NEXT: movq (%rax), %rax
; CHECK-NEXT: xorl %r10d, %r10d
; CHECK-NEXT: movl %ebp, %ecx
; CHECK-NEXT: negl %ecx
; CHECK-NEXT: movl $0, %r11d
; CHECK-NEXT: sbbq %r11, %r11
; CHECK-NEXT: orq %rax, %r11
; CHECK-NEXT: cmpl $1, %ebp
; CHECK-NEXT: sbbq %r10, %r10
; CHECK-NEXT: orq %rax, %r10
; CHECK-NEXT: movq $-1, %rcx
; CHECK-NEXT: movq $-1, %r10
; CHECK-NEXT: cmoveq %rax, %r10
; CHECK-NEXT: cmoveq %rcx, %rax
; CHECK-NEXT: subq $8, %rsp
; CHECK-NEXT: movq %r12, %rdi
; CHECK-NEXT: movl %r15d, %esi
; CHECK-NEXT: movl %r14d, %edx
; CHECK-NEXT: movl %r14d, %esi
; CHECK-NEXT: movl %ebp, %edx
; CHECK-NEXT: xorl %ecx, %ecx
; CHECK-NEXT: xorl %r8d, %r8d
; CHECK-NEXT: xorl %r9d, %r9d
; CHECK-NEXT: pushq %rax
; CHECK-NEXT: pushq %r10
; CHECK-NEXT: pushq %r11
; CHECK-NEXT: pushq %rbx
; CHECK-NEXT: pushq %r15
; CHECK-NEXT: callq foo2@PLT
; CHECK-NEXT: addq $32, %rsp
; CHECK-NEXT: popq %rbx
Expand Down
Loading
Loading