This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Optimize (srl (and X, 0xffff), C) -> (srli (slli X, 16), 16 + C).
ClosedPublic

Authored by craig.topper on Jan 31 2021, 11:08 PM.

Details

Summary

Rather than materializing the 0xffff immediate for the AND, use
a shift left to remove the upper bits and then shift in zeros
from the right.

This pattern occurs when type legalizing an i16 right shift.

I've implemented this with custom selection code for a number of
reasons. I've limited this to the AND having a single use. We need
to compensate for SimplifyDemandedBits altering the AND mask. I'm
using *W opcodes on RV64. We may want to generlize this in the
future. For all these reason it seemed easiest to do it this way.

Diff Detail

Event Timeline

craig.topper created this revision.Jan 31 2021, 11:08 PM
craig.topper requested review of this revision.Jan 31 2021, 11:08 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 31 2021, 11:08 PM
Herald added a subscriber: MaskRay. · View Herald Transcript
luismarques accepted this revision.Feb 1 2021, 3:03 AM

LGTM. Nice set of optimization patches!

llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
556–557

Nit: maybe "set to zero" instead of "removed", otherwise it seems like the mask was shifted?

This revision is now accepted and ready to land.Feb 1 2021, 3:03 AM
luismarques added inline comments.Feb 1 2021, 3:06 AM
llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
556–557

Or, better yet, "unset".

Looks good, but I'm wondering if this is already a target-independent transform (depending on the cost to materialize the constant)? If not, why not?

Looks good, but I'm wondering if this is already a target-independent transform (depending on the cost to materialize the constant)? If not, why not?

There is a transform to swap the srl+and to and+srl in DAGCombiner::visitShiftByConstant but its pretty restrictive about when it fires. I didn't find one turn srl+and into a pair of shifts. We do have the opposite transform to turn a pair of shifts into a shift+and which can be disabled with shouldFoldConstantShiftPairToMask. ARM has CombineANDShift which handles and+shl/shr by turning it into a shift pair. The shouldFoldConstantShiftPairToMask hook was added along with it.

This revision was landed with ongoing or failed builds.Feb 1 2021, 9:38 AM
This revision was automatically updated to reflect the committed changes.