Skip to content

Commit ea43726

Browse files
committed
[InstCombine] Add example usage for new Checked matcher API
There is no real motivation for this change other than to highlight a case where the new `Checked` matcher API can handle non-splat-vecs without increasing code complexity.
1 parent 8f9cabe commit ea43726

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7144,33 +7144,37 @@ Instruction *InstCombinerImpl::foldICmpCommutative(ICmpInst::Predicate Pred,
71447144

71457145
// Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
71467146
{
7147-
const APInt *Divisor;
7148-
if (match(Op0, m_UDiv(m_Specific(Op1), m_APInt(Divisor))) &&
7149-
Divisor->ugt(1)) {
7147+
if (match(Op0,
7148+
m_UDiv(m_Specific(Op1), m_CheckedInt([](const APInt &Divisor) {
7149+
return Divisor.ugt(1);
7150+
})))) {
71507151
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71517152
Constant::getNullValue(Op1->getType()));
71527153
}
71537154

71547155
if (!ICmpInst::isUnsigned(Pred) &&
7155-
match(Op0, m_SDiv(m_Specific(Op1), m_APInt(Divisor))) &&
7156-
Divisor->ugt(1)) {
7156+
match(Op0,
7157+
m_SDiv(m_Specific(Op1), m_CheckedInt([](const APInt &Divisor) {
7158+
return Divisor.ugt(1);
7159+
})))) {
71577160
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71587161
Constant::getNullValue(Op1->getType()));
71597162
}
71607163
}
71617164

71627165
// Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
71637166
{
7164-
const APInt *Shift;
7165-
if (match(Op0, m_LShr(m_Specific(Op1), m_APInt(Shift))) &&
7166-
!Shift->isZero()) {
7167+
if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt([](const APInt &Shift) {
7168+
return !Shift.isZero();
7169+
})))) {
71677170
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71687171
Constant::getNullValue(Op1->getType()));
71697172
}
71707173

71717174
if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7172-
match(Op0, m_AShr(m_Specific(Op1), m_APInt(Shift))) &&
7173-
!Shift->isZero()) {
7175+
match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt([](const APInt &Shift) {
7176+
return !Shift.isZero();
7177+
})))) {
71747178
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71757179
Constant::getNullValue(Op1->getType()));
71767180
}

llvm/test/Transforms/InstCombine/icmp-div-constant.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,7 @@ define <4 x i1> @lshr_by_const_cmp_sle_value(<4 x i32> %x) {
423423

424424
define <4 x i1> @lshr_by_const_cmp_sle_value_non_splat(<4 x i32> %x) {
425425
; CHECK-LABEL: @lshr_by_const_cmp_sle_value_non_splat(
426-
; CHECK-NEXT: [[V:%.*]] = lshr <4 x i32> [[X:%.*]], <i32 3, i32 3, i32 3, i32 5>
427-
; CHECK-NEXT: [[R:%.*]] = icmp sle <4 x i32> [[V]], [[X]]
426+
; CHECK-NEXT: [[R:%.*]] = icmp sgt <4 x i32> [[X:%.*]], <i32 -1, i32 -1, i32 -1, i32 -1>
428427
; CHECK-NEXT: ret <4 x i1> [[R]]
429428
;
430429
%v = lshr <4 x i32> %x, <i32 3, i32 3, i32 3, i32 5>

0 commit comments

Comments
 (0)