This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] ctpop(X) + ctpop(Y) => ctpop(X | Y) if X and Y have no common bits (PR48999)
ClosedPublic

Authored by xbolva00 on Apr 23 2021, 4:47 PM.

Details

Summary

For example:

int src(unsigned int a, unsigned int b)
{
    return __builtin_popcount(a << 16) + __builtin_popcount(b >> 16);
}

int tgt(unsigned int a, unsigned int b)
{
    return __builtin_popcount((a << 16)  | (b >> 16));
}

Diff Detail

Event Timeline

xbolva00 created this revision.Apr 23 2021, 4:47 PM
xbolva00 requested review of this revision.Apr 23 2021, 4:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 23 2021, 4:47 PM
xbolva00 updated this revision to Diff 340204.Apr 23 2021, 4:55 PM
xbolva00 edited the summary of this revision. (Show Details)Apr 23 2021, 5:00 PM
nikic added inline comments.Apr 24 2021, 7:52 AM
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
1453

As the pattern on both sides is the same, you don't need to use m_c_BinOp, you can just directly match LHS and RHS against ctpop.

xbolva00 added inline comments.Apr 24 2021, 8:10 AM
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
1453

I am bit confused now. Not same?
I can match lhs and rhs but then I need 2x mode code to handle ctpop(B) + ctpop(A) too.

xbolva00 added inline comments.Apr 24 2021, 8:14 AM
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
1453

Ignore me, got it :)

xbolva00 updated this revision to Diff 340281.Apr 24 2021, 8:21 AM

Code simplified. Thanks nikic

xbolva00 marked an inline comment as done.Apr 24 2021, 8:21 AM
nikic accepted this revision.Apr 24 2021, 8:26 AM

LGTM

llvm/test/Transforms/InstCombine/ctpop.ll
208

We could optimize ctpop(rot(X)) -> ctpop(X).

This revision is now accepted and ready to land.Apr 24 2021, 8:26 AM
nikic added inline comments.Apr 24 2021, 8:27 AM
llvm/test/Transforms/InstCombine/ctpop.ll
208

Though I'm not sure that was actually the case you wanted to test. As you still have the %a argument, maybe this was just a typo :)

This revision was landed with ongoing or failed builds.Apr 24 2021, 8:52 AM
This revision was automatically updated to reflect the committed changes.
xbolva00 added inline comments.Apr 24 2021, 9:00 AM
llvm/test/Transforms/InstCombine/ctpop.ll
208

Yeah, typo, but ctpop(rot(X)) -> ctpop(X) is good idea.