This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] try to canonicalize logical shift after bswap
ClosedPublic

Authored by spatel on Mar 18 2022, 8:23 AM.

Details

Summary

When shifting by a byte-multiple:
bswap (shl X, C) --> lshr (bswap X), C
bswap (lshr X, C) --> shl (bswap X), C

This is an IR implementation of a transform suggested in D120648. The "swaps cancel" test models the motivating optimization from that proposal.

Alive2 checks (as noted in the other review, we could use knownbits to handle shift-by-variable-amount, but that can be an enhancement patch):
https://alive2.llvm.org/ce/z/pXUaRf
https://alive2.llvm.org/ce/z/ZnaMLf

Diff Detail

Event Timeline

spatel created this revision.Mar 18 2022, 8:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 18 2022, 8:23 AM
spatel requested review of this revision.Mar 18 2022, 8:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 18 2022, 8:23 AM

I haven't had chance to look at https://github.com/llvm/llvm-project/issues/53867 yet but this has a lot in common with it (and feel free to grab it if you have the bandwidth) - please can you add test coverage for that as well?

I haven't had chance to look at https://github.com/llvm/llvm-project/issues/53867 yet but this has a lot in common with it (and feel free to grab it if you have the bandwidth) - please can you add test coverage for that as well?

Sure - if we canonicalize in this direction, then that problem won't be solved by this patch, but I think it becomes a simpler fold (we don't have to worry about the trailing trunc/demanded bits):
https://alive2.llvm.org/ce/z/zC5Gez

RKSimon accepted this revision.Mar 21 2022, 2:57 PM

LGTM with one (optional) minor

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1360

Worth using KnownBits to allow non-uniform shift amounts?

This revision is now accepted and ready to land.Mar 21 2022, 2:57 PM
spatel marked an inline comment as done.Mar 22 2022, 5:57 AM
spatel added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
1360

I'll put a TODO comment on it. I haven't seen a motivating case for variable or non-splat shifts yet.

This revision was automatically updated to reflect the committed changes.
spatel marked an inline comment as done.