Skip to content

Commit 47cc91b

Browse files
committed
[InstCombine] Fold (X / C) cmp X and (X >> C) cmp X into X cmp 0
1 parent 6393ea9 commit 47cc91b

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7103,6 +7103,34 @@ 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) < X => X > 0 for some constant Y other than 0 or 1
7107+
{
7108+
const APInt *Divisor;
7109+
Value *Dividend;
7110+
if (match(Op0, m_UDiv(m_Value(Dividend), m_APInt(Divisor))) &&
7111+
Op1 == Dividend && Divisor->ugt(1)) {
7112+
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Dividend,
7113+
Constant::getNullValue(Dividend->getType()));
7114+
}
7115+
7116+
if (match(Op0, m_SDiv(m_Value(Dividend), m_APInt(Divisor))) &&
7117+
Op1 == Dividend && Divisor->ugt(1) && !ICmpInst::isUnsigned(Pred)) {
7118+
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Dividend,
7119+
Constant::getNullValue(Dividend->getType()));
7120+
}
7121+
}
7122+
7123+
// Another case of this fold is (X >> Y) < X => X > 0 if Y != 0
7124+
{
7125+
const APInt *Shift;
7126+
Value *V;
7127+
if (match(Op0, m_LShr(m_Value(V), m_APInt(Shift))) && Op1 == V &&
7128+
Shift->ugt(0) && !ICmpInst::isUnsigned(Pred)) {
7129+
return new ICmpInst(ICmpInst::getInversePredicate(Pred), V,
7130+
Constant::getNullValue(V->getType()));
7131+
}
7132+
}
7133+
71067134
return nullptr;
71077135
}
71087136

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

Lines changed: 3 additions & 6 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

0 commit comments

Comments
 (0)