This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] reduce icmp_eq0-of-and-of-select-of-constants
ClosedPublic

Authored by spatel on Jan 29 2023, 12:32 PM.

Details

Summary

This is the most basic patch to handle fixing issue #57666.

D133919 proposes to handle much more than this in a single patch, but I've used 10 regression tests just to make sure this part is doing what I expected and nothing more, and it already shows even more potential TODO items.

The more general proofs from D133919 are correct, but we should enable this in smaller steps to reduce risk:
https://alive2.llvm.org/ce/z/RrVEyX

Diff Detail

Event Timeline

spatel created this revision.Jan 29 2023, 12:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 29 2023, 12:32 PM
spatel requested review of this revision.Jan 29 2023, 12:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 29 2023, 12:32 PM
goldstein.w.n added inline comments.Jan 29 2023, 1:34 PM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
1901

Can this just be match(X, m_Select(m_Value(A), m_APInt(TC), m_APInt(FC)) and the m_SpecificInt in the match on Y?

spatel marked an inline comment as done.Jan 29 2023, 3:36 PM
spatel added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
1901

Sure - I was actually backtracking from the more general patch in this version, but I can adjust that to be the more direct matcher.

spatel updated this revision to Diff 493275.Jan 30 2023, 4:50 AM
spatel marked an inline comment as done.

Patch updated: use the more direct m_SpecificInt matchers (expected to generalize that in follow-up patches).

goldstein.w.n added inline comments.Jan 30 2023, 9:21 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
1898

Is the plan to handle the ICMP_NE case in another patch?

1898

Is the plan to handle the ICMP_NE case in another patch?

Ignore, see TODO.

goldstein.w.n accepted this revision.Jan 30 2023, 9:38 AM
goldstein.w.n added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
1896

You could also match other predicates i.e ((x?1:4)&(y?1:4))<2

Also it seems we already have some logic for match some other predicates in this case (although I can't find it):
https://godbolt.org/z/hjzMGKqKz

although it seems to have an off by-1 issue (see ugt and ugt3)

This revision is now accepted and ready to land.Jan 30 2023, 9:38 AM

LGTM (although I'm not a maintainer so may want to find a bit at least for someone more familiar to checkoff).

spatel marked 2 inline comments as done.Jan 30 2023, 11:52 AM

LGTM (although I'm not a maintainer so may want to find a bit at least for someone more familiar to checkoff).

Thanks! This has been under development in D133919 for a while, so I feel good about it. Stamping out all of the tests for the planned enhancements is the larger effort at this point.

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

From what I see in the debug spew, some of those examples reduce via demanded bits - we shrink a select constant because some bit(s) is/are irrelevant given the compare constants. That leads to further folding via FoldOpIntoSelect or some other transform.

But there's no general transform along these lines that I see. Ie, if we choose some other arbitrary set of constants in the tests, it may or may not fold.

goldstein.w.n added inline comments.Jan 30 2023, 11:56 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
1896

From what I see in the debug spew, some of those examples reduce via demanded bits - we shrink a select constant because some bit(s) is/are irrelevant given the compare constants. That leads to further folding via FoldOpIntoSelect or some other transform.

But there's no general transform along these lines that I see. Ie, if we choose some other arbitrary set of constants in the tests, it may or may not fold.

I see, then I would definetly add as a TODO to handle "other predicates" instead of just "ne".

This revision was landed with ongoing or failed builds.Jan 30 2023, 1:19 PM
This revision was automatically updated to reflect the committed changes.
spatel marked an inline comment as done.