This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] Add transform ctpop(X) -> 1 iff X is non-zero power of 2
ClosedPublic

Authored by goldstein.w.n on Jan 17 2023, 10:47 PM.

Details

Summary

Definitionally a non-zero power of 2 will only have 1 bit set so this
is a freebee.

Diff Detail

Event Timeline

goldstein.w.n created this revision.Jan 17 2023, 10:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 17 2023, 10:47 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
goldstein.w.n requested review of this revision.Jan 17 2023, 10:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 17 2023, 10:47 PM

Can this be in InstSimplify?

Can this be in InstSimplify?

Where in InstSimplify should it go? Looking around there didn't seem any fitting place.

Can this be in InstSimplify?

Where in InstSimplify should it go? Looking around there didn't seem any fitting place.

I think you can add it to the ctpop handling in simplifyUnaryIntrinsic in llvm/lib/Analysis/InstructionSimplify.cpp

majnemer added inline comments.Jan 18 2023, 9:27 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
623–627 ↗(On Diff #490198)

Should this be in simplifyIntrinsic?

Can this be in InstSimplify?

Where in InstSimplify should it go? Looking around there didn't seem any fitting place.

I think you can add it to the ctpop handling in simplifyUnaryIntrinsic in llvm/lib/Analysis/InstructionSimplify.cpp

Ah, got it (I was looking in lib/Transform)

Move to instsimplify

goldstein.w.n retitled this revision from [InstCombine] Add transform ctpop(X) -> 1 iff X is non-zero power of 2 to [InstSimplify] Add transform ctpop(X) -> 1 iff X is non-zero power of 2.Jan 18 2023, 10:38 AM

Can this be in InstSimplify?

Where in InstSimplify should it go? Looking around there didn't seem any fitting place.

I think you can add it to the ctpop handling in simplifyUnaryIntrinsic in llvm/lib/Analysis/InstructionSimplify.cpp

Done, thank you for the helps!

goldstein.w.n marked an inline comment as done.Jan 18 2023, 10:40 AM
goldstein.w.n added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
623–627 ↗(On Diff #490198)

Should this be in simplifyIntrinsic?

Moved.

spatel added inline comments.Jan 18 2023, 10:50 AM
llvm/lib/Analysis/InstructionSimplify.cpp
5893

Why change the parameter? The arg type must match the call return type for ctpop, so it shouldn't make a difference for this patch. If there's some other transform where it makes a difference, we could adjust it later?

5931–5932

Remove the "Maybe better..." statement because we can't do that in InstSimplify (can't create new instructions here). But that should happen in InstCombine if it's not already done there.

nikic added inline comments.Jan 18 2023, 10:50 AM
llvm/lib/Analysis/InstructionSimplify.cpp
5895

Any particular reason for this change? It doesn't look like you make use of it (note that Call->getType() == Op0->getType() below).

goldstein.w.n marked 3 inline comments as done.

Don't change func decl

goldstein.w.n marked an inline comment as done.Jan 18 2023, 11:18 AM
goldstein.w.n added inline comments.
llvm/lib/Analysis/InstructionSimplify.cpp
5895

Any particular reason for this change? It doesn't look like you make use of it (note that Call->getType() == Op0->getType() below).

Didn't know that was guranteed, fixed.

5931–5932

Remove the "Maybe better..." statement because we can't do that in InstSimplify (can't create new instructions here). But that should happen in InstCombine if it's not already done there.

Is this change needed at all then? Would just knownpow2 -> icmp ne 0 in InstCombine be better?

But can do regardless. One thing, assuming we keep this AND add InstCombine change. Should I duplicate the test file or just add instcombine as a second pass in the current tests?

goldstein.w.n marked an inline comment as done and an inline comment as not done.Jan 18 2023, 11:18 AM
spatel accepted this revision.Jan 18 2023, 11:45 AM

LGTM

llvm/lib/Analysis/InstructionSimplify.cpp
5931–5932

It's a trade-off, and I'm not sure which way is better in this case. It's nice to make InstSimplify stronger because other passes (like EarlyCSE and GVN) use this as an analysis. But you're correct that it's also good to consolidate related folds. I don't think there's a real compile-time concern either way since ctpop should be too rare to make any difference to overall compile-time.

If you want to add the fold for known-pow2-or-zero to instcombine, add those tests to InstCombine rather than here. There shouldn't be a need to duplicate the ones that are already handled here. The regression tests are meant to be minimal, so we don't want to run multiple/different passes here.

This revision is now accepted and ready to land.Jan 18 2023, 11:45 AM
goldstein.w.n marked an inline comment as done.Jan 18 2023, 12:15 PM
goldstein.w.n added inline comments.
llvm/lib/Analysis/InstructionSimplify.cpp
5931–5932

It's a trade-off, and I'm not sure which way is better in this case. It's nice to make InstSimplify stronger because other passes (like EarlyCSE and GVN) use this as an analysis. But you're correct that it's also good to consolidate related folds. I don't think there's a real compile-time concern either way since ctpop should be too rare to make any difference to overall compile-time.

If you want to add the fold for known-pow2-or-zero to instcombine, add those tests to InstCombine rather than here. There shouldn't be a need to duplicate the ones that are already handled here. The regression tests are meant to be minimal, so we don't want to run multiple/different passes here.

Got it & Thanks!

Quick question about protocol when a code change is accepted but the test patch isn't. Is it assumed that NFC test changes are okay to just push in that case or should I wait on separate approval there?

spatel added inline comments.Jan 18 2023, 12:49 PM
llvm/lib/Analysis/InstructionSimplify.cpp
5931–5932

D141989 is accepted too. Generally, you can add/adjust tests as an NFC patch without pre-commit approval if you already have commit access.

goldstein.w.n marked 2 inline comments as done.Jan 18 2023, 12:57 PM
This revision was landed with ongoing or failed builds.Jan 19 2023, 11:29 AM
This revision was automatically updated to reflect the committed changes.