Skip to content

Commit

Permalink
[X86] combineAddOrSubToADCOrSBB - commute + neg subtraction patterns
Browse files Browse the repository at this point in the history
Handle SUB(AND(SRL(Y,Z),1),X) -> NEG(SBB(X,0,BT(Y,Z))) folds

I'll address the X86 lost folded-load regressions in a follow-up patch
  • Loading branch information
RKSimon committed Mar 21, 2022
1 parent 4a6fab7 commit ed51e26
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
10 changes: 7 additions & 3 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Expand Up @@ -52477,9 +52477,13 @@ static SDValue combineAddOrSubToADCOrSBB(SDNode *N, SelectionDAG &DAG) {
if (SDValue ADCOrSBB = combineAddOrSubToADCOrSBB(IsSub, DL, VT, X, Y, DAG))
return ADCOrSBB;

// If this is an add, commute and try again.
if (!IsSub)
return combineAddOrSubToADCOrSBB(IsSub, DL, VT, Y, X, DAG);
// Commute and try again (negate the result for subtracts).
if (SDValue ADCOrSBB = combineAddOrSubToADCOrSBB(IsSub, DL, VT, Y, X, DAG)) {
if (IsSub)
ADCOrSBB =
DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), ADCOrSBB);
return ADCOrSBB;
}

return SDValue();
}
Expand Down
55 changes: 23 additions & 32 deletions llvm/test/CodeGen/X86/add-sub-bool.ll
Expand Up @@ -215,28 +215,19 @@ define i32 @test_i32_sub_sub_idx(i32 %x, i32 %y, i32 %z) nounwind {
; X86-LABEL: test_i32_sub_sub_idx:
; X86: # %bb.0:
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
; X86-NEXT: shrl $16, %eax
; X86-NEXT: andl $1, %eax
; X86-NEXT: subl {{[0-9]+}}(%esp), %eax
; X86-NEXT: addl {{[0-9]+}}(%esp), %eax
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: btl $16, {{[0-9]+}}(%esp)
; X86-NEXT: sbbl $0, %ecx
; X86-NEXT: subl %ecx, %eax
; X86-NEXT: retl
;
; NOTBM-LABEL: test_i32_sub_sub_idx:
; NOTBM: # %bb.0:
; NOTBM-NEXT: # kill: def $edx killed $edx def $rdx
; NOTBM-NEXT: # kill: def $edi killed $edi def $rdi
; NOTBM-NEXT: shrl $16, %edx
; NOTBM-NEXT: andl $1, %edx
; NOTBM-NEXT: subl %esi, %edx
; NOTBM-NEXT: leal (%rdx,%rdi), %eax
; NOTBM-NEXT: retq
;
; TBM-LABEL: test_i32_sub_sub_idx:
; TBM: # %bb.0:
; TBM-NEXT: bextrl $272, %edx, %eax # imm = 0x110
; TBM-NEXT: subl %esi, %eax
; TBM-NEXT: addl %edi, %eax
; TBM-NEXT: retq
; X64-LABEL: test_i32_sub_sub_idx:
; X64: # %bb.0:
; X64-NEXT: movl %edi, %eax
; X64-NEXT: btl $16, %edx
; X64-NEXT: sbbl $0, %esi
; X64-NEXT: subl %esi, %eax
; X64-NEXT: retq
%shift = lshr i32 %z, 16
%mask = and i32 %shift, 1
%sub0 = sub i32 %y, %mask
Expand Down Expand Up @@ -436,23 +427,23 @@ define i32 @test_i32_sub_add_var(i32 %x, i32 %y, i32 %z, i32 %w) nounwind {
define i32 @test_i32_sub_sub_var(i32 %x, i32 %y, i32 %z, i32 %w) nounwind {
; X86-LABEL: test_i32_sub_sub_var:
; X86: # %bb.0:
; X86-NEXT: movb {{[0-9]+}}(%esp), %cl
; X86-NEXT: pushl %esi
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
; X86-NEXT: shrl %cl, %eax
; X86-NEXT: andl $1, %eax
; X86-NEXT: subl {{[0-9]+}}(%esp), %eax
; X86-NEXT: addl {{[0-9]+}}(%esp), %eax
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: movl {{[0-9]+}}(%esp), %edx
; X86-NEXT: movl {{[0-9]+}}(%esp), %esi
; X86-NEXT: btl %edx, %esi
; X86-NEXT: sbbl $0, %ecx
; X86-NEXT: subl %ecx, %eax
; X86-NEXT: popl %esi
; X86-NEXT: retl
;
; X64-LABEL: test_i32_sub_sub_var:
; X64: # %bb.0:
; X64-NEXT: # kill: def $edx killed $edx def $rdx
; X64-NEXT: # kill: def $edi killed $edi def $rdi
; X64-NEXT: # kill: def $cl killed $cl killed $ecx
; X64-NEXT: shrl %cl, %edx
; X64-NEXT: andl $1, %edx
; X64-NEXT: subl %esi, %edx
; X64-NEXT: leal (%rdx,%rdi), %eax
; X64-NEXT: movl %edi, %eax
; X64-NEXT: btl %ecx, %edx
; X64-NEXT: sbbl $0, %esi
; X64-NEXT: subl %esi, %eax
; X64-NEXT: retq
%shift = lshr i32 %z, %w
%mask = and i32 %shift, 1
Expand Down

0 comments on commit ed51e26

Please sign in to comment.