[RISCV] Optimize (brcond (seteq (and X, 1 << C), 0))
authorCraig Topper <craig.topper@sifive.com>
Thu, 21 Jul 2022 01:11:19 +0000 (18:11 -0700)
committerCraig Topper <craig.topper@sifive.com>
Thu, 21 Jul 2022 01:40:49 +0000 (18:40 -0700)
commit8983db15a3d8c026706d464f8cdb92072a4dc5ce
treed15089e37c4912fe6655aa75cca337af159a7ce6
parent7abbd6224b0b6089e4483a9c939be5d9a16b682b
[RISCV] Optimize (brcond (seteq (and X, 1 << C), 0))

If C > 10, this will require a constant to be materialized for the
And. To avoid this, we can shift X left by XLen-1-C bits to put the
tested bit in the MSB, then we can do a signed compare with 0 to
determine if the MSB is 0 or 1. Thanks to @reames for the suggestion.

I've implemented this inside of translateSetCCForBranch which is
called when setcc+brcond or setcc+select is converted to br_cc or
select_cc during lowering. It doesn't make sense to do this for
general setcc since we lack a sgez instruction.

I've tested bit 10, 11, 31, 32, 63 and a couple bits betwen 11 and 31
and between 32 and 63 for both i32 and i64 where applicable. Select
has some deficiencies where we receive (and (srl X, C), 1) instead.
This doesn't happen for br_cc due to the call to rebuildSetCC in the
generic DAGCombiner for brcond. I'll explore improving select in a
future patch.

Reviewed By: reames

Differential Revision: https://reviews.llvm.org/D130203
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
llvm/test/CodeGen/RISCV/bittest.ll
llvm/test/CodeGen/RISCV/rvv/fixed-vectors-masked-gather.ll
llvm/test/CodeGen/RISCV/rvv/fixed-vectors-masked-scatter.ll