Skip to content

Commit f561daf

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. Closes #85676
1 parent 1708788 commit f561daf

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
@@ -7128,34 +7128,30 @@ Instruction *InstCombinerImpl::foldICmpCommutative(ICmpInst::Predicate Pred,
71287128
return replaceInstUsesWith(CxtI, V);
71297129

71307130
// Folding (X / Y) pred X => X swap(pred) 0 for constant Y other than 0 or 1
7131+
auto CheckUGT1 = [](const APInt &Divisor) { return Divisor.ugt(1); };
71317132
{
7132-
const APInt *Divisor;
7133-
if (match(Op0, m_UDiv(m_Specific(Op1), m_APInt(Divisor))) &&
7134-
Divisor->ugt(1)) {
7133+
if (match(Op0, m_UDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
71357134
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71367135
Constant::getNullValue(Op1->getType()));
71377136
}
71387137

71397138
if (!ICmpInst::isUnsigned(Pred) &&
7140-
match(Op0, m_SDiv(m_Specific(Op1), m_APInt(Divisor))) &&
7141-
Divisor->ugt(1)) {
7139+
match(Op0, m_SDiv(m_Specific(Op1), m_CheckedInt(CheckUGT1)))) {
71427140
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71437141
Constant::getNullValue(Op1->getType()));
71447142
}
71457143
}
71467144

71477145
// Another case of this fold is (X >> Y) pred X => X swap(pred) 0 if Y != 0
7146+
auto CheckNE0 = [](const APInt &Shift) { return !Shift.isZero(); };
71487147
{
7149-
const APInt *Shift;
7150-
if (match(Op0, m_LShr(m_Specific(Op1), m_APInt(Shift))) &&
7151-
!Shift->isZero()) {
7148+
if (match(Op0, m_LShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
71527149
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71537150
Constant::getNullValue(Op1->getType()));
71547151
}
71557152

71567153
if ((Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SGE) &&
7157-
match(Op0, m_AShr(m_Specific(Op1), m_APInt(Shift))) &&
7158-
!Shift->isZero()) {
7154+
match(Op0, m_AShr(m_Specific(Op1), m_CheckedInt(CheckNE0)))) {
71597155
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1,
71607156
Constant::getNullValue(Op1->getType()));
71617157
}

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)