Skip to content

Commit 9705962

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 625121b commit 9705962

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
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: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,8 @@ define i1 @udiv_x_by_const_cmp_x(i32 %x) {
401401

402402
define <2 x i1> @udiv_x_by_const_cmp_x_non_splat(<2 x i32> %x) {
403403
; CHECK-LABEL: @udiv_x_by_const_cmp_x_non_splat(
404-
; CHECK-NEXT: [[TMP1:%.*]] = udiv <2 x i32> [[X:%.*]], <i32 123, i32 -123>
405-
; CHECK-NEXT: [[TMP2:%.*]] = icmp slt <2 x i32> [[TMP1]], [[X]]
406-
; CHECK-NEXT: ret <2 x i1> [[TMP2]]
404+
; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i32> [[X:%.*]], zeroinitializer
405+
; CHECK-NEXT: ret <2 x i1> [[TMP1]]
407406
;
408407
%1 = udiv <2 x i32> %x, <i32 123, i32 -123>
409408
%2 = icmp slt <2 x i32> %1, %x
@@ -413,9 +412,8 @@ define <2 x i1> @udiv_x_by_const_cmp_x_non_splat(<2 x i32> %x) {
413412

414413
define <2 x i1> @sdiv_x_by_const_cmp_x_non_splat(<2 x i32> %x) {
415414
; CHECK-LABEL: @sdiv_x_by_const_cmp_x_non_splat(
416-
; CHECK-NEXT: [[TMP1:%.*]] = sdiv <2 x i32> [[X:%.*]], <i32 2, i32 3>
417-
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i32> [[TMP1]], [[X]]
418-
; CHECK-NEXT: ret <2 x i1> [[TMP2]]
415+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i32> [[X:%.*]], zeroinitializer
416+
; CHECK-NEXT: ret <2 x i1> [[TMP1]]
419417
;
420418
%1 = sdiv <2 x i32> %x, <i32 2, i32 3>
421419
%2 = icmp eq <2 x i32> %1, %x
@@ -446,8 +444,7 @@ define <4 x i1> @lshr_by_const_cmp_sle_value(<4 x i32> %x) {
446444

447445
define <4 x i1> @lshr_by_const_cmp_sle_value_non_splat(<4 x i32> %x) {
448446
; CHECK-LABEL: @lshr_by_const_cmp_sle_value_non_splat(
449-
; CHECK-NEXT: [[V:%.*]] = lshr <4 x i32> [[X:%.*]], <i32 3, i32 3, i32 3, i32 5>
450-
; CHECK-NEXT: [[R:%.*]] = icmp sle <4 x i32> [[V]], [[X]]
447+
; CHECK-NEXT: [[R:%.*]] = icmp sgt <4 x i32> [[X:%.*]], <i32 -1, i32 -1, i32 -1, i32 -1>
451448
; CHECK-NEXT: ret <4 x i1> [[R]]
452449
;
453450
%v = lshr <4 x i32> %x, <i32 3, i32 3, i32 3, i32 5>
@@ -458,8 +455,7 @@ define <4 x i1> @lshr_by_const_cmp_sle_value_non_splat(<4 x i32> %x) {
458455

459456
define <4 x i1> @ashr_by_const_cmp_sge_value_non_splat(<4 x i32> %x) {
460457
; CHECK-LABEL: @ashr_by_const_cmp_sge_value_non_splat(
461-
; CHECK-NEXT: [[V:%.*]] = ashr <4 x i32> [[X:%.*]], <i32 1, i32 2, i32 3, i32 4>
462-
; CHECK-NEXT: [[R:%.*]] = icmp sge <4 x i32> [[V]], [[X]]
458+
; CHECK-NEXT: [[R:%.*]] = icmp slt <4 x i32> [[X:%.*]], <i32 1, i32 1, i32 1, i32 1>
463459
; CHECK-NEXT: ret <4 x i1> [[R]]
464460
;
465461
%v = ashr <4 x i32> %x, <i32 1, i32 2, i32 3, i32 4>

0 commit comments

Comments
 (0)