This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Optimize (brcond (seteq (and X, 1 << C), 0))
ClosedPublic

Authored by craig.topper on Jul 20 2022, 2:23 PM.

Details

Summary

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.

Diff Detail

Event Timeline

craig.topper created this revision.Jul 20 2022, 2:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 20 2022, 2:23 PM
craig.topper requested review of this revision.Jul 20 2022, 2:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 20 2022, 2:23 PM
craig.topper edited the summary of this revision. (Show Details)Jul 20 2022, 2:38 PM

Rebase after fixing a failure to use bexti in some cases. slli would
still be better in those cases since it is compressible.

reames accepted this revision.Jul 20 2022, 4:19 PM

LGTM

This revision is now accepted and ready to land.Jul 20 2022, 4:19 PM
jrtc27 added inline comments.Jul 20 2022, 4:24 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
1375

I assume you don't actually want bit-wise AND here?

This revision was landed with ongoing or failed builds.Jul 20 2022, 6:44 PM
This revision was automatically updated to reflect the committed changes.
craig.topper added inline comments.Jul 20 2022, 6:48 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
1375

Good catch. Fixed when I commited. Thanks!