Skip to content

Commit e4b9b1e

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 c0c1049 commit e4b9b1e

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7123,34 +7123,30 @@ Instruction *InstCombinerImpl::foldICmpCommutative(ICmpInst::Predicate Pred,
71237123
return replaceInstUsesWith(CxtI, V);
71247124

71257125
// Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7126+
auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };
71267127
{
7127-
const APInt *Divisor;
7128-
if (match(Op0, m_UDiv(m_Specific(Op1), m_APInt(Divisor))) &&
7129-
Divisor->ugt(1)) {
7128+
if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
71307129
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71317130
Constant::getNullValue(Op1->getType()));
71327131
}
71337132

71347133
if (!ICmpInst::isUnsigned(Pred) &&
7135-
match(Op0, m_SDiv(m_Specific(Op1), m_APInt(Divisor))) &&
7136-
Divisor->ugt(1)) {
7134+
match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
71377135
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71387136
Constant::getNullValue(Op1->getType()));
71397137
}
71407138
}
71417139

71427140
// Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7141+
auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
71437142
{
7144-
const APInt *Shift;
7145-
if (match(Op0, m_LShr(m_Specific(Op1), m_APInt(Shift))) &&
7146-
!Shift->isZero()) {
7143+
if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
71477144
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71487145
Constant::getNullValue(Op1->getType()));
71497146
}
71507147

71517148
if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7152-
match(Op0, m_AShr(m_Specific(Op1), m_APInt(Shift))) &&
7153-
!Shift->isZero()) {
7149+
match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
71547150
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71557151
Constant::getNullValue(Op1->getType()));
71567152
}

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)