This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] fold one-bit set/clear icmp with mul operand
AbandonedPublic

Authored by spatel on Jan 24 2022, 11:58 AM.

Details

Summary
((X1 * C1) & C2) ==/!= 0 --> (X1 & 1) ==/!= 0

... iff C2 is a power-of-2 and C1 is a multiple of C2, and C2 does not have known zero low-bits set

This is a reduced form of one proposed transform in D114272.

If I translated the pre-conditions correctly, then this is just a test of the LSB of the input operand:
https://alive2.llvm.org/ce/z/YB_4pn

Diff Detail

Event Timeline

spatel created this revision.Jan 24 2022, 11:58 AM
spatel requested review of this revision.Jan 24 2022, 11:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 24 2022, 11:58 AM
spatel edited the summary of this revision. (Show Details)Jan 24 2022, 11:58 AM
lebedev.ri edited the summary of this revision. (Show Details)Jan 24 2022, 11:59 AM
lebedev.ri accepted this revision.Jan 24 2022, 12:15 PM

LGTM, thanks.
The proof looks correct to me, but a fear that we are missing things due to extra artificial uses due to the preconditions has struck me before.

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
1863

This fold should probably be in foldICmpAndConstConst()

This revision is now accepted and ready to land.Jan 24 2022, 12:15 PM
spatel updated this revision to Diff 402625.Jan 24 2022, 12:32 PM
spatel marked an inline comment as done.

Patch updated:
Moved the code into foldICmpAndConstConst, so we have more shared matches/captures with existing code.

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
1863

Yes, will move.

I had regrets about this patch shortly after my previous comment. It's not an icmp fold that we're looking for. It's either "one-demanded-bit-of-multiply" or an "and" fold:
https://alive2.llvm.org/ce/z/D_Rma8

spatel planned changes to this revision.Jan 25 2022, 4:01 AM
spatel abandoned this revision.Feb 5 2022, 7:08 AM

Abandoned in favor of a more general demanded-bits fold:
0236c571810d