[llvm] [InstCombine] Add ctpop(A | B) + ctpop(A & B) -> ctpop(A) + ctpop(B) (PR #79089)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 22 19:59:57 PST 2024
================
@@ -1718,6 +1718,25 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
Builder.CreateIntrinsic(Intrinsic::umax, {I.getType()}, {A, B}));
}
+ // ctpop(A | B) + ctpop(A & B) -> ctpop(A) + ctpop(B)
+ if ((match(LHS, m_OneUse(m_Intrinsic<Intrinsic::ctpop>(
+ m_And(m_Value(A), m_Value(B))))) &&
+ (match(RHS, m_OneUse(m_Intrinsic<Intrinsic::ctpop>(
+ m_Or(m_Specific(A), m_Specific(B))))) ||
+ match(RHS, m_OneUse(m_Intrinsic<Intrinsic::ctpop>(
+ m_Or(m_Specific(B), m_Specific(A))))))) ||
+ (match(LHS, m_OneUse(m_Intrinsic<Intrinsic::ctpop>(
+ m_Or(m_Value(A), m_Value(B))))) &&
+ (match(RHS, m_OneUse(m_Intrinsic<Intrinsic::ctpop>(
+ m_And(m_Specific(A), m_Specific(B))))) ||
+ match(RHS, m_OneUse(m_Intrinsic<Intrinsic::ctpop>(
+ m_And(m_Specific(B), m_Specific(A)))))))) {
+ return replaceInstUsesWith(
+ I, Builder.CreateAdd(
+ Builder.CreateIntrinsic(Intrinsic::ctpop, {A->getType()}, {A}),
+ Builder.CreateIntrinsic(Intrinsic::ctpop, {B->getType()}, {B})));
----------------
dtcxzyw wrote:
```suggestion
return BinaryOperator::CreateAdd(
Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, A),
Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, B));
```
https://github.com/llvm/llvm-project/pull/79089
More information about the llvm-commits
mailing list