Skip to content

[InstCombine] Combine or-disjoint (and->mul), (and->mul) to and->mul #136013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3560,6 +3560,72 @@ static Value *foldOrOfInversions(BinaryOperator &I,
return nullptr;
}

struct DecomposedBitMaskMul {
Value *X;
APInt Factor;
APInt Mask;
};

static std::optional<DecomposedBitMaskMul> matchBitmaskMul(Value *V) {
Instruction *Op = dyn_cast<Instruction>(V);
if (!Op)
return std::nullopt;

Value *MulOp = nullptr;
const APInt *MulConst = nullptr;
if (match(Op, m_Mul(m_Value(MulOp), m_APInt(MulConst)))) {
Value *Original = nullptr;
const APInt *Mask = nullptr;
if (!MulConst->isStrictlyPositive())
return std::nullopt;

if (match(MulOp, m_And(m_Value(Original), m_APInt(Mask)))) {
if (!Mask->isStrictlyPositive())
return std::nullopt;
DecomposedBitMaskMul Ret;
Ret.X = Original;
Ret.Mask = *Mask;
Ret.Factor = *MulConst;
return Ret;
}
return std::nullopt;
}

Value *Cond = nullptr;
const APInt *EqZero = nullptr, *NeZero = nullptr;

// (!(A & N) ? 0 : N * C) + (!(A & M) ? 0 : M * C) -> A & (N + M) * C
if (match(Op, m_Select(m_Value(Cond), m_APInt(EqZero), m_APInt(NeZero)))) {
auto ICmpDecompose =
decomposeBitTest(Cond, /*LookThruTrunc=*/true,
/*AllowNonZeroC=*/false, /*DecomposeBitMask=*/true);
if (!ICmpDecompose.has_value())
return std::nullopt;

if (ICmpDecompose->Pred == ICmpInst::ICMP_NE)
std::swap(EqZero, NeZero);

if (!EqZero->isZero() || !NeZero->isStrictlyPositive())
return std::nullopt;

if (!ICmpInst::isEquality(ICmpDecompose->Pred) ||
!ICmpDecompose->C.isZero() || !ICmpDecompose->Mask.isPowerOf2() ||
ICmpDecompose->Mask.isNegative())
return std::nullopt;

if (!NeZero->urem(ICmpDecompose->Mask).isZero())
return std::nullopt;

DecomposedBitMaskMul Ret;
Ret.X = ICmpDecompose->X;
Ret.Mask = ICmpDecompose->Mask;
Ret.Factor = NeZero->udiv(ICmpDecompose->Mask);
return Ret;
}

return std::nullopt;
}

// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
// here. We should standardize that construct where it is needed or choose some
// other way to ensure that commutated variants of patterns are not missed.
Expand Down Expand Up @@ -3641,6 +3707,22 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
foldAddLikeCommutative(I.getOperand(1), I.getOperand(0),
/*NSW=*/true, /*NUW=*/true))
return R;

auto Decomp0 = matchBitmaskMul(I.getOperand(0));
auto Decomp1 = matchBitmaskMul(I.getOperand(1));

if (Decomp0 && Decomp1) {
if (Decomp0->X == Decomp1->X &&
(Decomp0->Mask & Decomp1->Mask).isZero() &&
Decomp0->Factor == Decomp1->Factor) {
auto NewAnd = Builder.CreateAnd(
Decomp0->X, ConstantInt::get(Decomp0->X->getType(),
(Decomp0->Mask + Decomp1->Mask)));

return BinaryOperator::CreateMul(
NewAnd, ConstantInt::get(NewAnd->getType(), Decomp1->Factor));
}
}
}

Value *X, *Y;
Expand Down
Loading
Loading