This is an archive of the discontinued LLVM Phabricator instance.

InstCombine] fold mul with decremented "shl -1" factor (2nd try)
ClosedPublic

Authored by spatel on Nov 1 2022, 6:33 AM.

Details

Summary

This is a hopefully corrected version of:
bc886e9b587b

I thought the patch was obvious enough to not require pre-commit review the first time, but I made a copy-paste error that created an "add" instead of the intended "sub". The regression tests showed the bug, but I overlooked that too (embarrassing).

So this time, I'm posting for review in case someone (or bots) would like to apply the patch and confirm that the previous errors (there were several independent reports) are no longer there. As I said in a comment on issue #58717, the bug reports confirm that the pattern does occur in many real-world applications, so hopefully, eliminating the multiply results in better code.

I added one more regression test in this version of the patch, and here's an Alive2 proof to show that exact example:
https://alive2.llvm.org/ce/z/dge7VC

Original commit message:

This is a sibling to:
6064e92b0a84
...but we canonicalize the shl+add to shl+xor,
so the pattern is different than I expected:
https://alive2.llvm.org/ce/z/8CX16e

I have not found any patterns that are safe
to propagate no-wrap, so that is not included
here.

Diff Detail

Event Timeline

spatel created this revision.Nov 1 2022, 6:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 1 2022, 6:33 AM
spatel requested review of this revision.Nov 1 2022, 6:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 1 2022, 6:33 AM
spatel edited the summary of this revision. (Show Details)Nov 1 2022, 6:42 AM
spatel added a reviewer: nlopes.

Thanks! My testscases seem to run fine with this version of the patch.

(Note, the patch doesn't seem to apply entirely cleanly on current git main; the last hunk of the testcase seems to fail to apply here.)

spatel added a comment.Nov 1 2022, 6:51 AM

Thanks! My testscases seem to run fine with this version of the patch.

Great! - thank you for checking.

(Note, the patch doesn't seem to apply entirely cleanly on current git main; the last hunk of the testcase seems to fail to apply here.)

Ah - I had not pushed the new test up to main. Should be clean after:
087e721dd8a7

FWIW, I tested replacing CreateAdd with CreateSub on the original commit yesterday, and it fixed the issues I had.

chapuni accepted this revision.Nov 1 2022, 5:59 PM

I have reconfirmed this fine to build.
(who could approve this?)

This revision is now accepted and ready to land.Nov 1 2022, 5:59 PM
nikic accepted this revision.Nov 2 2022, 1:02 AM
spatel added a comment.Nov 2 2022, 4:54 AM

Thanks all for the extra testing! I'll re-land the corrected patch.

This revision was landed with ongoing or failed builds.Nov 2 2022, 6:30 AM
This revision was automatically updated to reflect the committed changes.