Skip to content

Commit 634bbc3

Browse files
committed
[InstCombine] Fold ((X / C) cmp X) and ((X >> C) cmp X) into X ~cmp 0
1 parent fcc57bb commit 634bbc3

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7103,6 +7103,46 @@ Instruction *InstCombinerImpl::foldICmpCommutative(ICmpInst::Predicate Pred,
71037103
if (Value *V = foldICmpWithLowBitMaskedVal(Pred, Op0, Op1, Q, *this))
71047104
return replaceInstUsesWith(CxtI, V);
71057105

7106+
// Folding (X / Y) cmp X => X ~cmp 0 for some constant Y other than 0 or 1
7107+
{
7108+
Value *Dividend;
7109+
if (match(Op0,
7110+
m_UDiv(m_Value(Dividend),
7111+
m_SpecificInt_ICMP(
7112+
CmpInst::ICMP_UGT,
7113+
APInt::getOneBitSet(
7114+
Op0->getType()->getScalarSizeInBits(), 0)))) &&
7115+
Op1 == Dividend) {
7116+
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Dividend,
7117+
Constant::getNullValue(Dividend->getType()));
7118+
}
7119+
7120+
if (match(Op0,
7121+
m_SDiv(m_Value(Dividend),
7122+
m_SpecificInt_ICMP(
7123+
CmpInst::ICMP_UGT,
7124+
APInt::getOneBitSet(
7125+
Op0->getType()->getScalarSizeInBits(), 0)))) &&
7126+
Op1 == Dividend && !ICmpInst::isUnsigned(Pred)) {
7127+
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Dividend,
7128+
Constant::getNullValue(Dividend->getType()));
7129+
}
7130+
}
7131+
7132+
// Another case of this fold is (X >> Y) cmp X => X ~cmp 0 if Y != 0
7133+
{
7134+
Value *V;
7135+
if (match(Op0, m_LShr(m_Value(V),
7136+
m_SpecificInt_ICMP(
7137+
CmpInst::ICMP_NE,
7138+
APInt::getZero(
7139+
Op0->getType()->getScalarSizeInBits())))) &&
7140+
Op1 == V) {
7141+
return new ICmpInst(ICmpInst::getInversePredicate(Pred), V,
7142+
Constant::getNullValue(V->getType()));
7143+
}
7144+
}
7145+
71067146
return nullptr;
71077147
}
71087148

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,7 @@ define i1 @sdiv_eq_smin_use(i32 %x, i32 %y) {
381381

382382
define i1 @sdiv_x_by_const_cmp_x(i32 %x) {
383383
; CHECK-LABEL: @sdiv_x_by_const_cmp_x(
384-
; CHECK-NEXT: [[V:%.*]] = udiv i32 [[X:%.*]], 13
385-
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[V]], [[X]]
384+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], 0
386385
; CHECK-NEXT: ret i1 [[TMP1]]
387386
;
388387
%v = udiv i32 %x, 13
@@ -392,8 +391,7 @@ define i1 @sdiv_x_by_const_cmp_x(i32 %x) {
392391

393392
define i1 @udiv_x_by_const_cmp_x(i32 %x) {
394393
; CHECK-LABEL: @udiv_x_by_const_cmp_x(
395-
; CHECK-NEXT: [[TMP2:%.*]] = udiv i32 [[X:%.*]], 123
396-
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[TMP2]], [[X]]
394+
; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 0
397395
; CHECK-NEXT: ret i1 [[TMP1]]
398396
;
399397
%1 = udiv i32 %x, 123
@@ -405,8 +403,7 @@ define i1 @udiv_x_by_const_cmp_x(i32 %x) {
405403

406404
define i1 @lshr_x_by_const_cmp_x(i32 %x) {
407405
; CHECK-LABEL: @lshr_x_by_const_cmp_x(
408-
; CHECK-NEXT: [[V:%.*]] = lshr i32 [[X:%.*]], 1
409-
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[V]], [[X]]
406+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X:%.*]], 0
410407
; CHECK-NEXT: ret i1 [[TMP1]]
411408
;
412409
%v = lshr i32 %x, 1
@@ -416,8 +413,7 @@ define i1 @lshr_x_by_const_cmp_x(i32 %x) {
416413

417414
define <4 x i1> @lshr_by_const_cmp_sle_value(<4 x i32> %x) {
418415
; CHECK-LABEL: @lshr_by_const_cmp_sle_value(
419-
; CHECK-NEXT: [[V:%.*]] = lshr <4 x i32> [[X:%.*]], <i32 2, i32 3, i32 2, i32 4>
420-
; CHECK-NEXT: [[R:%.*]] = icmp sle <4 x i32> [[V]], [[X]]
416+
; CHECK-NEXT: [[R:%.*]] = icmp sgt <4 x i32> [[X:%.*]], zeroinitializer
421417
; CHECK-NEXT: ret <4 x i1> [[R]]
422418
;
423419
%v = lshr <4 x i32> %x, <i32 2, i32 3, i32 2, i32 4>
@@ -427,8 +423,7 @@ define <4 x i1> @lshr_by_const_cmp_sle_value(<4 x i32> %x) {
427423

428424
define i1 @lshr_by_const_cmp_uge_value(i32 %x) {
429425
; CHECK-LABEL: @lshr_by_const_cmp_uge_value(
430-
; CHECK-NEXT: [[V:%.*]] = lshr i32 [[X:%.*]], 3
431-
; CHECK-NEXT: [[R:%.*]] = icmp sle i32 [[V]], [[X]]
426+
; CHECK-NEXT: [[R:%.*]] = icmp sgt i32 [[X:%.*]], 0
432427
; CHECK-NEXT: ret i1 [[R]]
433428
;
434429
%v = lshr i32 %x, 3

0 commit comments

Comments
 (0)