This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Override TargetLowering::shouldProduceAndByConstByHoistingConstFromShiftsLHSOfAnd.
ClosedPublic

Authored by craig.topper on Apr 28 2022, 2:32 PM.

Details

Summary

This hook determines if SimplifySetcc transforms (X & (C l>>/<< Y))

/!= 0 into ((X <</l>> Y) & C) ==/!= 0. Where C is a constant and

X might be a constant.

The default implementation favors doing the transform if X is not
a constant. Otherwise the code is left alone. There is a provision
that if the target supports a bit test instruction then the transform
will favor ((1 << Y) & X) ==/!= 0. RISCV does not say it has a variable
bit test operation.

RISCV with Zbs does have a BEXT instruction that performs (X >> Y) & 1.
Without Zbs, (X >> Y) & 1 still looks preferable to ((1 << Y) & X) since
we can fold use ANDI instead of putting a 1 in a register for SLL.

This patch overrides this hook to favor bit extract patterns and
otherwise falls back to the "do the transform if X is not a constant"
heuristic.

I've added tests where both C and X are constants with both the shl form
and lshr form. I've also added a test for a switch statement that lowers
to a bit test. That was my original motivation for looking at this.

Diff Detail

Event Timeline

craig.topper created this revision.Apr 28 2022, 2:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2022, 2:32 PM
craig.topper requested review of this revision.Apr 28 2022, 2:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2022, 2:32 PM
asb accepted this revision.May 11 2022, 6:28 AM

LGTM, thanks.

This revision is now accepted and ready to land.May 11 2022, 6:28 AM
This revision was landed with ongoing or failed builds.May 11 2022, 11:13 AM
This revision was automatically updated to reflect the committed changes.