This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Add transform `(icmp eq/ne bitreverse(x), C)` -> `(icmp eq/ne x, bitreverse(C))`
ClosedPublic

Authored by goldstein.w.n on Mar 5 2023, 5:20 PM.

Diff Detail

Event Timeline

goldstein.w.n created this revision.Mar 5 2023, 5:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 5 2023, 5:20 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
goldstein.w.n requested review of this revision.Mar 5 2023, 5:20 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 5 2023, 5:20 PM
nikic accepted this revision.Mar 6 2023, 1:39 AM

LGTM

This revision is now accepted and ready to land.Mar 6 2023, 1:39 AM
This revision was landed with ongoing or failed builds.Mar 6 2023, 6:31 PM
This revision was automatically updated to reflect the committed changes.
reames added a subscriber: reames.Mar 10 2023, 7:48 AM

Just to note, the basic idea in this patch is generalizable. For any function F for which we can compute the inverse function F' such that F'(F(x)) == x, replacing F(x) == C with x == F'(C) is legal. Bitreverse (and the existing bswap) just happen to be cases where F == F'.

Not saying you have to implement the generalization, but might be worth some thought.

You applied this to the case where RHS is a constant, but we could also do it when F'(RHS) folds (e.g. InstSimpliy) or RHS is loop invariant when LHS isn't.

See also getInvertibleOperands from ValueTracking.cpp. It implements basically the same idea, just with a different application.