Skip to content

Commit 0019711

Browse files
committed
[InstCombine] Extend bitmask->select combine to match and->mul
Change-Id: I1cc2acd3804dde50636518f3ef2c9581848ae9f6
1 parent 0a68a9d commit 0019711

File tree

2 files changed

+163
-54
lines changed

2 files changed

+163
-54
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3593,6 +3593,72 @@ static Value *foldOrOfInversions(BinaryOperator &I,
35933593
return nullptr;
35943594
}
35953595

3596+
struct DecomposedBitMaskMul {
3597+
Value *X;
3598+
APInt Factor;
3599+
APInt Mask;
3600+
};
3601+
3602+
static std::optional<DecomposedBitMaskMul> matchBitmaskMul(Value *V) {
3603+
Instruction *Op = dyn_cast<Instruction>(V);
3604+
if (!Op)
3605+
return std::nullopt;
3606+
3607+
Value *MulOp = nullptr;
3608+
const APInt *MulConst = nullptr;
3609+
if (match(Op, m_Mul(m_Value(MulOp), m_APInt(MulConst)))) {
3610+
Value *Original = nullptr;
3611+
const APInt *Mask = nullptr;
3612+
if (!MulConst->isStrictlyPositive())
3613+
return std::nullopt;
3614+
3615+
if (match(MulOp, m_And(m_Value(Original), m_APInt(Mask)))) {
3616+
if (!Mask->isStrictlyPositive())
3617+
return std::nullopt;
3618+
DecomposedBitMaskMul Ret;
3619+
Ret.X = Original;
3620+
Ret.Mask = *Mask;
3621+
Ret.Factor = *MulConst;
3622+
return Ret;
3623+
}
3624+
return std::nullopt;
3625+
}
3626+
3627+
Value *Cond = nullptr;
3628+
const APInt *EqZero = nullptr, *NeZero = nullptr;
3629+
3630+
// (!(A & N) ? 0 : N * C) + (!(A & M) ? 0 : M * C) -> A & (N + M) * C
3631+
if (match(Op, m_Select(m_Value(Cond), m_APInt(EqZero), m_APInt(NeZero)))) {
3632+
auto ICmpDecompose =
3633+
decomposeBitTest(Cond, /*LookThruTrunc=*/true,
3634+
/*AllowNonZeroC=*/false, /*DecomposeBitMask=*/true);
3635+
if (!ICmpDecompose.has_value())
3636+
return std::nullopt;
3637+
3638+
if (ICmpDecompose->Pred == ICmpInst::ICMP_NE)
3639+
std::swap(EqZero, NeZero);
3640+
3641+
if (!EqZero->isZero() || !NeZero->isStrictlyPositive())
3642+
return std::nullopt;
3643+
3644+
if (!ICmpInst::isEquality(ICmpDecompose->Pred) ||
3645+
!ICmpDecompose->C.isZero() || !ICmpDecompose->Mask.isPowerOf2() ||
3646+
ICmpDecompose->Mask.isNegative())
3647+
return std::nullopt;
3648+
3649+
if (!NeZero->urem(ICmpDecompose->Mask).isZero())
3650+
return std::nullopt;
3651+
3652+
DecomposedBitMaskMul Ret;
3653+
Ret.X = ICmpDecompose->X;
3654+
Ret.Mask = ICmpDecompose->Mask;
3655+
Ret.Factor = NeZero->udiv(ICmpDecompose->Mask);
3656+
return Ret;
3657+
}
3658+
3659+
return std::nullopt;
3660+
}
3661+
35963662
// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
35973663
// here. We should standardize that construct where it is needed or choose some
35983664
// other way to ensure that commutated variants of patterns are not missed.
@@ -3675,49 +3741,19 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
36753741
/*NSW=*/true, /*NUW=*/true))
36763742
return R;
36773743

3678-
Value *Cond0 = nullptr, *Cond1 = nullptr;
3679-
const APInt *Op0Eq = nullptr, *Op0Ne = nullptr;
3680-
const APInt *Op1Eq = nullptr, *Op1Ne = nullptr;
3681-
3682-
// (!(A & N) ? 0 : N * C) + (!(A & M) ? 0 : M * C) -> A & (N + M) * C
3683-
if (match(I.getOperand(0),
3684-
m_Select(m_Value(Cond0), m_APInt(Op0Eq), m_APInt(Op0Ne))) &&
3685-
match(I.getOperand(1),
3686-
m_Select(m_Value(Cond1), m_APInt(Op1Eq), m_APInt(Op1Ne)))) {
3687-
3688-
auto LHSDecompose =
3689-
decomposeBitTest(Cond0, /*LookThruTrunc=*/true,
3690-
/*AllowNonZeroC=*/false, /*DecomposeAnd=*/true);
3691-
auto RHSDecompose =
3692-
decomposeBitTest(Cond1, /*LookThruTrunc=*/true,
3693-
/*AllowNonZeroC=*/false, /*DecomposeAnd=*/true);
3694-
3695-
if (LHSDecompose && RHSDecompose && LHSDecompose->X == RHSDecompose->X &&
3696-
RHSDecompose->Mask.isPowerOf2() && LHSDecompose->Mask.isPowerOf2() &&
3697-
LHSDecompose->Mask != RHSDecompose->Mask &&
3698-
LHSDecompose->Mask.getBitWidth() == Op0Ne->getBitWidth() &&
3699-
RHSDecompose->Mask.getBitWidth() == Op1Ne->getBitWidth()) {
3700-
assert(Op0Ne->getBitWidth() == Op1Ne->getBitWidth());
3701-
assert(ICmpInst::isEquality(LHSDecompose->Pred));
3702-
if (LHSDecompose->Pred == ICmpInst::ICMP_NE)
3703-
std::swap(Op0Eq, Op0Ne);
3704-
if (RHSDecompose->Pred == ICmpInst::ICMP_NE)
3705-
std::swap(Op1Eq, Op1Ne);
3706-
3707-
if (!Op0Ne->isZero() && !Op1Ne->isZero() && Op0Eq->isZero() &&
3708-
Op1Eq->isZero() && Op0Ne->urem(LHSDecompose->Mask).isZero() &&
3709-
Op1Ne->urem(RHSDecompose->Mask).isZero() &&
3710-
Op0Ne->udiv(LHSDecompose->Mask) ==
3711-
Op1Ne->udiv(RHSDecompose->Mask)) {
3712-
auto NewAnd = Builder.CreateAnd(
3713-
LHSDecompose->X,
3714-
ConstantInt::get(LHSDecompose->X->getType(),
3715-
(LHSDecompose->Mask + RHSDecompose->Mask)));
3716-
3717-
return BinaryOperator::CreateMul(
3718-
NewAnd, ConstantInt::get(NewAnd->getType(),
3719-
Op0Ne->udiv(LHSDecompose->Mask)));
3720-
}
3744+
auto Decomp0 = matchBitmaskMul(I.getOperand(0));
3745+
auto Decomp1 = matchBitmaskMul(I.getOperand(1));
3746+
3747+
if (Decomp0 && Decomp1) {
3748+
if (Decomp0->X == Decomp1->X &&
3749+
(Decomp0->Mask & Decomp1->Mask).isZero() &&
3750+
Decomp0->Factor == Decomp1->Factor) {
3751+
auto NewAnd = Builder.CreateAnd(
3752+
Decomp0->X, ConstantInt::get(Decomp0->X->getType(),
3753+
(Decomp0->Mask + Decomp1->Mask)));
3754+
3755+
return BinaryOperator::CreateMul(
3756+
NewAnd, ConstantInt::get(NewAnd->getType(), Decomp1->Factor));
37213757
}
37223758
}
37233759
}

llvm/test/Transforms/InstCombine/or-bitmask.ll

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@ define i32 @add_select_cmp_and2(i32 %in) {
3636

3737
define i32 @add_select_cmp_and3(i32 %in) {
3838
; CHECK-LABEL: @add_select_cmp_and3(
39-
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[IN:%.*]], 3
39+
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[IN:%.*]], 7
4040
; CHECK-NEXT: [[TEMP:%.*]] = mul nuw nsw i32 [[TMP1]], 72
41-
; CHECK-NEXT: [[BITOP2:%.*]] = and i32 [[IN]], 4
42-
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[BITOP2]], 0
43-
; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[CMP2]], i32 0, i32 288
44-
; CHECK-NEXT: [[OUT:%.*]] = or disjoint i32 [[TEMP]], [[SEL2]]
45-
; CHECK-NEXT: ret i32 [[OUT]]
41+
; CHECK-NEXT: ret i32 [[TEMP]]
4642
;
4743
%bitop0 = and i32 %in, 1
4844
%cmp0 = icmp eq i32 %bitop0, 0
@@ -60,12 +56,9 @@ define i32 @add_select_cmp_and3(i32 %in) {
6056

6157
define i32 @add_select_cmp_and4(i32 %in) {
6258
; CHECK-LABEL: @add_select_cmp_and4(
63-
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[IN:%.*]], 3
64-
; CHECK-NEXT: [[OUT:%.*]] = mul nuw nsw i32 [[TMP1]], 72
65-
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[IN]], 12
59+
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[IN:%.*]], 15
6660
; CHECK-NEXT: [[TEMP3:%.*]] = mul nuw nsw i32 [[TMP2]], 72
67-
; CHECK-NEXT: [[OUT1:%.*]] = or disjoint i32 [[OUT]], [[TEMP3]]
68-
; CHECK-NEXT: ret i32 [[OUT1]]
61+
; CHECK-NEXT: ret i32 [[TEMP3]]
6962
;
7063
%bitop0 = and i32 %in, 1
7164
%cmp0 = icmp eq i32 %bitop0, 0
@@ -361,6 +354,86 @@ define i64 @mask_select_types_1(i64 %in) {
361354
ret i64 %out
362355
}
363356

357+
define i32 @add_select_cmp_mixed1(i32 %in) {
358+
; CHECK-LABEL: @add_select_cmp_mixed1(
359+
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[IN:%.*]], 3
360+
; CHECK-NEXT: [[OUT:%.*]] = mul nuw nsw i32 [[TMP1]], 72
361+
; CHECK-NEXT: ret i32 [[OUT]]
362+
;
363+
%mask = and i32 %in, 1
364+
%sel0 = mul i32 %mask, 72
365+
%bitop1 = and i32 %in, 2
366+
%cmp1 = icmp eq i32 %bitop1, 0
367+
%sel1 = select i1 %cmp1, i32 0, i32 144
368+
%out = or disjoint i32 %sel0, %sel1
369+
ret i32 %out
370+
}
371+
372+
define i32 @add_select_cmp_mixed2(i32 %in) {
373+
; CHECK-LABEL: @add_select_cmp_mixed2(
374+
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[IN:%.*]], 3
375+
; CHECK-NEXT: [[OUT:%.*]] = mul nuw nsw i32 [[TMP1]], 72
376+
; CHECK-NEXT: ret i32 [[OUT]]
377+
;
378+
%bitop0 = and i32 %in, 1
379+
%cmp0 = icmp eq i32 %bitop0, 0
380+
%mask = and i32 %in, 2
381+
%sel0 = select i1 %cmp0, i32 0, i32 72
382+
%sel1 = mul i32 %mask, 72
383+
%out = or disjoint i32 %sel0, %sel1
384+
ret i32 %out
385+
}
386+
387+
define i32 @add_select_cmp_and_mul(i32 %in) {
388+
; CHECK-LABEL: @add_select_cmp_and_mul(
389+
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[IN:%.*]], 3
390+
; CHECK-NEXT: [[OUT:%.*]] = mul nuw nsw i32 [[TMP1]], 72
391+
; CHECK-NEXT: ret i32 [[OUT]]
392+
;
393+
%mask0 = and i32 %in, 1
394+
%sel0 = mul i32 %mask0, 72
395+
%mask1 = and i32 %in, 2
396+
%sel1 = mul i32 %mask1, 72
397+
%out = or disjoint i32 %sel0, %sel1
398+
ret i32 %out
399+
}
400+
401+
define i32 @add_select_cmp_mixed2_mismatch(i32 %in) {
402+
; CHECK-LABEL: @add_select_cmp_mixed2_mismatch(
403+
; CHECK-NEXT: [[BITOP0:%.*]] = and i32 [[IN:%.*]], 1
404+
; CHECK-NEXT: [[CMP0:%.*]] = icmp eq i32 [[BITOP0]], 0
405+
; CHECK-NEXT: [[MASK:%.*]] = and i32 [[IN]], 2
406+
; CHECK-NEXT: [[SEL0:%.*]] = select i1 [[CMP0]], i32 0, i32 73
407+
; CHECK-NEXT: [[SEL1:%.*]] = mul nuw nsw i32 [[MASK]], 72
408+
; CHECK-NEXT: [[OUT:%.*]] = or disjoint i32 [[SEL0]], [[SEL1]]
409+
; CHECK-NEXT: ret i32 [[OUT]]
410+
;
411+
%bitop0 = and i32 %in, 1
412+
%cmp0 = icmp eq i32 %bitop0, 0
413+
%mask = and i32 %in, 2
414+
%sel0 = select i1 %cmp0, i32 0, i32 73
415+
%sel1 = mul i32 %mask, 72
416+
%out = or disjoint i32 %sel0, %sel1
417+
ret i32 %out
418+
}
419+
420+
define i32 @add_select_cmp_and_mul_mismatch(i32 %in) {
421+
; CHECK-LABEL: @add_select_cmp_and_mul_mismatch(
422+
; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[IN:%.*]] to i1
423+
; CHECK-NEXT: [[SEL0:%.*]] = select i1 [[TMP1]], i32 73, i32 0
424+
; CHECK-NEXT: [[MASK1:%.*]] = and i32 [[IN]], 2
425+
; CHECK-NEXT: [[SEL1:%.*]] = mul nuw nsw i32 [[MASK1]], 72
426+
; CHECK-NEXT: [[OUT:%.*]] = or disjoint i32 [[SEL0]], [[SEL1]]
427+
; CHECK-NEXT: ret i32 [[OUT]]
428+
;
429+
%mask0 = and i32 %in, 1
430+
%sel0 = mul i32 %mask0, 73
431+
%mask1 = and i32 %in, 2
432+
%sel1 = mul i32 %mask1, 72
433+
%out = or disjoint i32 %sel0, %sel1
434+
ret i32 %out
435+
}
436+
364437
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
365438
; CONSTSPLAT: {{.*}}
366439
; CONSTVEC: {{.*}}

0 commit comments

Comments
 (0)