Skip to content

Commit 0d36e34

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

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7103,6 +7103,35 @@ 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+
const APInt *Divisor;
7110+
if (match(Op0, m_UDiv(m_Value(Dividend), m_APInt(Divisor))) &&
7111+
Op1 == Dividend && !Divisor->isZero() && !Divisor->isOne()) {
7112+
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Dividend,
7113+
Constant::getNullValue(Dividend->getType()));
7114+
}
7115+
7116+
if (match(Op0, m_UDiv(m_Value(Dividend), m_APInt(Divisor))) &&
7117+
Op1 == Dividend && !Divisor->isZero() && !Divisor->isOne() &&
7118+
!ICmpInst::isUnsigned(Pred)) {
7119+
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Dividend,
7120+
Constant::getNullValue(Dividend->getType()));
7121+
}
7122+
}
7123+
7124+
// Another case of this fold is (X >> Y) cmp X => X ~cmp 0 if Y != 0
7125+
{
7126+
Value *V;
7127+
const APInt *Shift;
7128+
if (match(Op0, m_LShr(m_Value(V), m_APInt(Shift))) && Op1 == V &&
7129+
!Shift->isZero()) {
7130+
return new ICmpInst(ICmpInst::getInversePredicate(Pred), V,
7131+
Constant::getNullValue(V->getType()));
7132+
}
7133+
}
7134+
71067135
return nullptr;
71077136
}
71087137

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 3, i32 3, i32 3, i32 3>
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 3, i32 3, i32 3, i32 3>
@@ -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)