This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold icmp of truncated left shift
ClosedPublic

Authored by spatel on Dec 9 2021, 4:16 PM.

Details

Summary

Refer to issue #51889. Folds:

  • (icmp eq (trunc (shl 1, Y) to i[N]), 0) --> (icmp ugt Y, N-1)
  • (icmp ne (trunc (shl 1, Y) to i[N]), 0) --> (icmp ult Y, N)

Diff Detail

Event Timeline

hasyimibhar created this revision.Dec 9 2021, 4:16 PM
hasyimibhar requested review of this revision.Dec 9 2021, 4:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 9 2021, 4:16 PM
hasyimibhar edited the summary of this revision. (Show Details)
hasyimibhar added inline comments.Dec 9 2021, 4:32 PM
llvm/test/Transforms/InstCombine/icmp-trunc-shl.ll
215 ↗(On Diff #393327)

Just noticed the output is different for ne with truncate to i1. According to alive, this is correct: https://alive2.llvm.org/ce/z/pWkv74.

Run git-clang-format

RKSimon added a subscriber: RKSimon.

Please can you add some vector tests? And also some negative (multi-use) tests?

There are a lot of tests here using different bit-widths, but they don't add much value. As suggested, we'd get more impact by varying types/uses (hint: if you're only creating 1 instruction, then extra uses of the intermediate values are almost always ok).

Also, as noted in the bug report, the compare to 0 is possibly a special-case of a more general pattern. Did you consider handling something like this:
https://alive2.llvm.org/ce/z/6SCoJu ?

Finally, if you don't have commit access yet, you probably have enough commits now to request it:
https://llvm.org/docs/DeveloperPolicy.html#obtaining-commit-access

spatel edited the summary of this revision. (Show Details)Aug 25 2022, 5:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2022, 5:05 AM

Reverse ping @hasyimibhar - do you plan to continue work on this patch, or should someone else commandeer it?

spatel commandeered this revision.Sep 5 2022, 8:54 AM
spatel edited reviewers, added: hasyimibhar; removed: spatel.
spatel updated this revision to Diff 458032.Sep 5 2022, 9:40 AM

Patch updated:

  1. Added/removed/pre-committed tests.
  2. Adjusted position of fold.

The diffs show that we can already fold some of these patterns via other transforms, but it is limited based on data type. That seems like an unnecessary restriction for this pattern since we can reduce to a single instruction (no one-use limits either). There are many TODO enhancements that can be done in follow-up patches.

RKSimon accepted this revision.Sep 8 2022, 2:33 AM

LGTM - cheers

This revision is now accepted and ready to land.Sep 8 2022, 2:33 AM
This revision was landed with ongoing or failed builds.Sep 8 2022, 7:49 AM
This revision was automatically updated to reflect the committed changes.