This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] If there is a known-bit transform is_pow2 check to just check for any other bits
ClosedPublic

Authored by goldstein.w.n on Jun 11 2023, 11:31 PM.

Details

Summary

in ctpop(X) eq/ne 1 or ctpop(X) ugt/ule 1, if there is any
known-bit in X, instead of going through ctpop, we can just test
if there are any other known bits in X. If there are, X is not a
power of 2. If there aren't, X is a power of 2.

https://alive2.llvm.org/ce/z/eLMJgU

Diff Detail

Event Timeline

goldstein.w.n created this revision.Jun 11 2023, 11:31 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 11 2023, 11:31 PM
goldstein.w.n requested review of this revision.Jun 11 2023, 11:31 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 11 2023, 11:31 PM
nikic added inline comments.Jun 12 2023, 2:37 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
6973

Shouldn't this code be in foldICmpIntrinsicWithConstant?

6975

Do you think it would make sense to canonicalize ctpop(X) < 2 to ctpop(X) == 1 if isKnownNonZero(X)? Or does this run into the issue that ctpop(X) < 2 is actually the cheaper check once expanded, and we might not recover the non-zero fact at that point anymore?

6992

Move to helper used in intrinsic folding function

goldstein.w.n marked an inline comment as done.Jun 12 2023, 10:27 AM
goldstein.w.n added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
6975

ctpop(X) < 2 is cheaper for the backend and indeed isknownnonzero is often 'lost' by the backend.

nikic added inline comments.Jul 17 2023, 9:20 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
3777–3796

If you rebase, there should be a switch here where you can handle both equality and non-equality comparisons in one place.

goldstein.w.n added inline comments.Jul 17 2023, 9:38 AM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
3777–3796

Hmm? Don't see any change to the function signiture on master.

nikic added inline comments.Jul 17 2023, 12:34 PM
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
3777–3796

I'm referring to the switch directly above.

goldstein.w.n marked 2 inline comments as done.Jul 17 2023, 1:29 PM
nikic accepted this revision.Jul 18 2023, 8:06 AM

LGTM, but the attached proof is for the wrong transform.

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

Unnecessary llvm prefix.

3433
This revision is now accepted and ready to land.Jul 18 2023, 8:06 AM
goldstein.w.n edited the summary of this revision. (Show Details)Jul 18 2023, 8:35 AM

LGTM, but the attached proof is for the wrong transform.

Bah, sorry must have copied wrong link. Updated summary with: https://alive2.llvm.org/ce/z/eLMJgU

goldstein.w.n marked an inline comment as done.Jul 18 2023, 8:52 AM

Remove unnecesary llvm:: ns

This revision was landed with ongoing or failed builds.Jul 21 2023, 12:15 PM
This revision was automatically updated to reflect the committed changes.