This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Teach RISCVDAGToDAGISel::selectShiftMask to replace sub from constant with neg.
ClosedPublic

Authored by craig.topper on Feb 6 2022, 11:46 AM.

Details

Summary

If the shift amount is (sub C, X) where C is 0 modulo the size of
the shift, we can replace it with neg or negw.

Similar is is done for AArch64 and X86.

Diff Detail

Event Timeline

craig.topper created this revision.Feb 6 2022, 11:46 AM
craig.topper requested review of this revision.Feb 6 2022, 11:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 6 2022, 11:46 AM

There are a few small regressions in this as noted. Not sure exactly how to fix them yet.

llvm/test/CodeGen/RISCV/rotl-rotr.ll
259

This sub could be a t0, a2, -32 if we changed the bltz that uses t0 to check bgtz.

271

There appears to be a missed optimization here. We have (31 - (64 - a2)). Using two subs. This could be simplified to (a2 + (-33)) using an addi.

llvm/test/CodeGen/RISCV/shifts.ll
158

After we form the neg, this sub is only used by two branches. The sub could be reversed to an addi if the branch conditions were inverted.

This is also i128 shift expansion on rv32. Clang doesn't support __int128 on rv32 so this codegen doesn't matter a lot.

khchen accepted this revision.Feb 9 2022, 9:24 AM

Cool, LGTM!
It seems it worth to have sub immediate + branch optimization.

This revision is now accepted and ready to land.Feb 9 2022, 9:24 AM
This revision was landed with ongoing or failed builds.Feb 9 2022, 12:37 PM
This revision was automatically updated to reflect the committed changes.