This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Add special case for (select cc, 1.0, 0.0) to lowerSELECT
ClosedPublic

Authored by liaolucy on May 30 2023, 8:29 AM.

Details

Summary

Use sint_to_fp instead of select.
Reduce the number of branch instructions and
avoid generating TargetConstantPool for double.

(select cc, 1.0, 0.0) -> (sint_to_fp (zext cc))
https://alive2.llvm.org/ce/z/aoEcd9
https://godbolt.org/z/n543Y9v3e

(select cc, 0.0, 1.0) -> (sint_to_fp (zext (xor cc, 1)))
https://alive2.llvm.org/ce/z/zngvSB

Diff Detail

Event Timeline

liaolucy created this revision.May 30 2023, 8:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 30 2023, 8:29 AM
liaolucy requested review of this revision.May 30 2023, 8:29 AM
liaolucy retitled this revision from Add special case for (select_cc 0, x, setlt, 0.0, 1.0) to lowerSELECT to [RISCV] Add special case for (select_cc 0, x, setlt, 0.0, 1.0) to lowerSELECT.May 30 2023, 8:30 AM
craig.topper added inline comments.May 30 2023, 2:18 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5596

Why does the condition code matter? Isn't this equally valid for any condition code?

craig.topper added inline comments.May 30 2023, 2:22 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5596

Or rather, why do the operands to the setcc matter? If you the select true/false are 1.0 and 0.0 you can replace the select with a sint_to_fp regardless of what the condition is.

liaolucy updated this revision to Diff 526876.May 30 2023, 8:17 PM
liaolucy marked 2 inline comments as done.
liaolucy retitled this revision from [RISCV] Add special case for (select_cc 0, x, setlt, 0.0, 1.0) to lowerSELECT to [RISCV] Add special case for (select cc, 1.0, 0.0) to lowerSELECT.
liaolucy edited the summary of this revision. (Show Details)

If the select true/false are 1.0 and 0.0 we can replace the select with a sint_to_fp.
(select cc, 1.0, 0.0) -> (sint_to_fp (zext cc))
https://alive2.llvm.org/ce/z/aoEcd9
thanks

craig.topper added inline comments.May 30 2023, 8:21 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5519

This ZERO_EXTEND shoudn't be needed. The condition must be a legal integer type so it could only be XLenVT.

liaolucy updated this revision to Diff 526880.May 30 2023, 8:36 PM

Deleted two ZERO_EXTEND.
The second ZERO_EXTEND should not be needed either. Setcc and zext are all XLenVT.

craig.topper added inline comments.May 30 2023, 8:45 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5518

Drop curly braces

5597

Why not handle this like the earlier, but use an XOR with 1 to flip the condition? Wouldn't that always better than the branch?

liaolucy added inline comments.May 30 2023, 9:36 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5597

The select true/false are 0.0 and 1.0 (not 1.0 and 0.0) we can not replace the select with a sint_to_fp.
https://alive2.llvm.org/ce/z/L32zaJ
I didn't find an easier replacement.

liaolucy added inline comments.May 30 2023, 9:42 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5597

Maybe I can change it to perform select and reuse the above code, I'll try it, thanks.

craig.topper added inline comments.May 30 2023, 10:02 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5597
liaolucy updated this revision to Diff 526903.May 30 2023, 10:28 PM
liaolucy edited the summary of this revision. (Show Details)

Using xor to replace setcc.
A DAGCombine patch may be needed here. I can send a new patch.

+++ b/llvm/test/CodeGen/RISCV/half-select-icmp.ll
@@ -510,26 +510,30 @@ define half @select_icmp_slt_one(i32 signext %a) {
 define half @select_icmp_sgt_zero(i32 signext %a) {
 ; CHECK-LABEL: select_icmp_sgt_zero:
 ; CHECK:       # %bb.0:
-; CHECK-NEXT:    slti a0, a0, 1
+; CHECK-NEXT:    sgtz a0, a0
+; CHECK-NEXT:    xori a0, a0, 1
 ; CHECK-NEXT:    fcvt.h.w fa0, a0
 ; CHECK-NEXT:    ret
craig.topper added inline comments.May 30 2023, 10:32 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
5522

No need for else after return.

llvm/test/CodeGen/RISCV/double-select-icmp.ll
510

CHECKZFINX isn't a valid prefix for this file.

llvm/test/CodeGen/RISCV/half-select-icmp.ll
540

CHECKZFINX isn't a valid prefix for this file

liaolucy updated this revision to Diff 526908.May 30 2023, 10:47 PM

Address comments. Thanks.

This revision is now accepted and ready to land.May 30 2023, 11:15 PM