[PATCH] D123109: [x86] improve select lowering for smin(x, 0) & smax(x, 0)
Wei Xiao via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 6 07:33:56 PDT 2022
wxiao3 updated this revision to Diff 420843.
wxiao3 added a comment.
Address Phoebe's comments.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D123109/new/
https://reviews.llvm.org/D123109
Files:
llvm/lib/Target/X86/X86ISelLowering.cpp
llvm/test/CodeGen/X86/select-smin-smax.ll
Index: llvm/test/CodeGen/X86/select-smin-smax.ll
===================================================================
--- llvm/test/CodeGen/X86/select-smin-smax.ll
+++ llvm/test/CodeGen/X86/select-smin-smax.ll
@@ -9,9 +9,9 @@
define i32 @test_i32_smax(i32 %a) nounwind {
; CHECK-LABEL: test_i32_smax:
; CHECK: # %bb.0:
-; CHECK-NEXT: xorl %eax, %eax
-; CHECK-NEXT: testl %edi, %edi
-; CHECK-NEXT: cmovgl %edi, %eax
+; CHECK-NEXT: movl %edi, %eax
+; CHECK-NEXT: sarl $31, %eax
+; CHECK-NEXT: andnl %edi, %eax, %eax
; CHECK-NEXT: retq
%r = call i32 @llvm.smax.i32(i32 %a, i32 0)
ret i32 %r
@@ -20,9 +20,9 @@
define i32 @test_i32_smin(i32 %a) nounwind {
; CHECK-LABEL: test_i32_smin:
; CHECK: # %bb.0:
-; CHECK-NEXT: xorl %eax, %eax
-; CHECK-NEXT: testl %edi, %edi
-; CHECK-NEXT: cmovsl %edi, %eax
+; CHECK-NEXT: movl %edi, %eax
+; CHECK-NEXT: sarl $31, %eax
+; CHECK-NEXT: andl %edi, %eax
; CHECK-NEXT: retq
%r = call i32 @llvm.smin.i32(i32 %a, i32 0)
ret i32 %r
@@ -31,9 +31,9 @@
define i64 @test_i64_smax(i64 %a) nounwind {
; CHECK-LABEL: test_i64_smax:
; CHECK: # %bb.0:
-; CHECK-NEXT: xorl %eax, %eax
-; CHECK-NEXT: testq %rdi, %rdi
-; CHECK-NEXT: cmovgq %rdi, %rax
+; CHECK-NEXT: movq %rdi, %rax
+; CHECK-NEXT: sarq $63, %rax
+; CHECK-NEXT: andnq %rdi, %rax, %rax
; CHECK-NEXT: retq
%r = call i64 @llvm.smax.i64(i64 %a, i64 0)
ret i64 %r
@@ -42,9 +42,9 @@
define i64 @test_i64_smin(i64 %a) nounwind {
; CHECK-LABEL: test_i64_smin:
; CHECK: # %bb.0:
-; CHECK-NEXT: xorl %eax, %eax
-; CHECK-NEXT: testq %rdi, %rdi
-; CHECK-NEXT: cmovsq %rdi, %rax
+; CHECK-NEXT: movq %rdi, %rax
+; CHECK-NEXT: sarq $63, %rax
+; CHECK-NEXT: andq %rdi, %rax
; CHECK-NEXT: retq
%r = call i64 @llvm.smin.i64(i64 %a, i64 0)
ret i64 %r
Index: llvm/lib/Target/X86/X86ISelLowering.cpp
===================================================================
--- llvm/lib/Target/X86/X86ISelLowering.cpp
+++ llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -24698,6 +24698,8 @@
// (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
// (select (and (x , 0x1) == 0), y, (z ^ y) ) -> (-(and (x , 0x1)) & z ) ^ y
// (select (and (x , 0x1) == 0), y, (z | y) ) -> (-(and (x , 0x1)) & z ) | y
+ // (select (x > 0), x, 0) -> (~(x >> (size_in_bits(x)-1))) & x
+ // (select (x < 0), x, 0) -> ((x >> (size_in_bits(x)-1))) & x
if (Cond.getOpcode() == X86ISD::SETCC &&
Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
isNullConstant(Cond.getOperand(1).getOperand(1))) {
@@ -24778,6 +24780,22 @@
SDValue And = DAG.getNode(ISD::AND, DL, VT, Mask, Src1); // Mask & z
return DAG.getNode(Op2.getOpcode(), DL, VT, And, Src2); // And Op y
}
+ } else if ((VT == MVT::i32 || VT == MVT::i64) && isNullConstant(Op2) &&
+ Cmp.getNode()->hasOneUse() && (CmpOp0 == Op1) &&
+ ((CondCode == X86::COND_S) || // smin(x, 0)
+ (CondCode == X86::COND_G && hasAndNot(Op1)))) { // smax(x, 0)
+ // (select (x < 0), x, 0) -> ((x >> (size_in_bits(x)-1))) & x
+ //
+ // If the comparison is testing for a positive value, we have to invert
+ // the sign bit mask, so only do that transform if the target has a
+ // bitwise 'and not' instruction (the invert is free).
+ // (select (x > 0), x, 0) -> (~(x >> (size_in_bits(x)-1))) & x
+ unsigned ShCt = VT.getSizeInBits() - 1;
+ SDValue ShiftAmt = DAG.getConstant(ShCt, DL, VT);
+ SDValue Shift = DAG.getNode(ISD::SRA, DL, VT, Op1, ShiftAmt);
+ if (CondCode == X86::COND_G)
+ Shift = DAG.getNOT(DL, Shift, VT);
+ return DAG.getNode(ISD::AND, DL, VT, Shift, Op1);
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123109.420843.patch
Type: text/x-patch
Size: 3775 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220406/71643a89/attachment.bin>
More information about the llvm-commits
mailing list