[llvm] [InstCombine] Fold (X / C) < X and (X >> C) < X into X > 0 (PR #85555)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 16 15:17:53 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: None (Poseydon42)
<details>
<summary>Changes</summary>
Proofs:
https://alive2.llvm.org/ce/z/yG_wxp
https://alive2.llvm.org/ce/z/iNBZVy
https://alive2.llvm.org/ce/z/XA_HG5
This resolves #<!-- -->85313
---
Full diff: https://github.com/llvm/llvm-project/pull/85555.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp (+25)
- (modified) llvm/test/Transforms/InstCombine/icmp-div-constant.ll (+76)
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 0dce0077bf1588..415ea61362776d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7406,6 +7406,31 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
if (Instruction *Res = foldReductionIdiom(I, Builder, DL))
return Res;
+ // Folding (X / Y) < X => X > 0 for some constant Y other than 0 or 1
+ {
+ Constant *Divisor;
+ Value *Dividend;
+ if (match(Op0,
+ m_CombineOr(m_SDiv(m_Value(Dividend), m_ImmConstant(Divisor)),
+ m_UDiv(m_Value(Dividend), m_ImmConstant(Divisor)))) &&
+ match(Op1, m_Deferred(Dividend)) &&
+ !(Divisor->isZeroValue() || Divisor->isOneValue())) {
+ return new ICmpInst(ICmpInst::ICMP_SGT, Dividend,
+ Constant::getNullValue(Dividend->getType()));
+ }
+ }
+
+ // Another case of this fold is (X >> Y) < X => X > 0 if Y != 0
+ {
+ Constant *Shift;
+ Value *V;
+ if (match(Op0, m_LShr(m_Value(V), m_ImmConstant(Shift))) &&
+ match(Op1, m_Deferred(V)) && !Shift->isZeroValue()) {
+ return new ICmpInst(ICmpInst::ICMP_SGT, V,
+ Constant::getNullValue(V->getType()));
+ }
+ }
+
return Changed ? &I : nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/icmp-div-constant.ll b/llvm/test/Transforms/InstCombine/icmp-div-constant.ll
index 8dcb96284685ff..0bdfa5daa547a5 100644
--- a/llvm/test/Transforms/InstCombine/icmp-div-constant.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-div-constant.ll
@@ -375,3 +375,79 @@ define i1 @sdiv_eq_smin_use(i32 %x, i32 %y) {
%r = icmp eq i32 %d, -2147483648
ret i1 %r
}
+
+; Folding (X / C) < X => X > 0 for C != 1
+
+define i1 @sdiv_x_by_const_cmp_x(i32 %x) {
+; CHECK-LABEL: @sdiv_x_by_const_cmp_x(
+; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 0
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %1 = sdiv i32 %x, 123
+ %2 = icmp slt i32 %1, %x
+ ret i1 %2
+}
+
+define i1 @udiv_x_by_const_cmp_x(i32 %x) {
+; CHECK-LABEL: @udiv_x_by_const_cmp_x(
+; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 0
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %1 = udiv i32 %x, 123
+ %2 = icmp slt i32 %1, %x
+ ret i1 %2
+}
+
+; Same as above but with right shift instead of division (C != 0)
+
+define i1 @lshr_x_by_const_cmp_x(i32 %x) {
+; CHECK-LABEL: @lshr_x_by_const_cmp_x(
+; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 0
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %1 = lshr i32 %x, 1
+ %2 = icmp slt i32 %1, %x
+ ret i1 %2
+}
+
+; Negative tests for the folds above
+
+define i1 @sdiv_x_by_1_cmp_x_negative(i32 %x) {
+; CHECK-LABEL: @sdiv_x_by_1_cmp_x_negative(
+; CHECK-NEXT: ret i1 false
+;
+ %1 = sdiv i32 %x, 1
+ %2 = icmp slt i32 %1, %x
+ ret i1 %2
+}
+
+define i1 @sdiv_x_by_1_cmp_y(i32 %x, i32 %y) {
+; CHECK-LABEL: @sdiv_x_by_1_cmp_y(
+; CHECK-NEXT: [[TMP1:%.*]] = sdiv i32 [[X:%.*]], 123
+; CHECK-NEXT: [[TMP2:%.*]] = icmp slt i32 [[TMP1]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[TMP2]]
+;
+ %1 = sdiv i32 %x, 123
+ %2 = icmp slt i32 %1, %y
+ ret i1 %2
+}
+
+define i1 @lshr_x_by_0_cmp_x(i32 %x) {
+; CHECK-LABEL: @lshr_x_by_0_cmp_x(
+; CHECK-NEXT: ret i1 false
+;
+ %1 = lshr i32 %x, 0
+ %2 = icmp slt i32 %1, %x
+ ret i1 %2
+}
+
+define i1 @lshr_x_by_const_cmp_y(i32 %x, i32 %y) {
+; CHECK-LABEL: @lshr_x_by_const_cmp_y(
+; CHECK-NEXT: [[X:%.*]] = lshr i32 [[X1:%.*]], 1
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %1 = lshr i32 %x, 1
+ %2 = icmp slt i32 %1, %y
+ ret i1 %2
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/85555
More information about the llvm-commits
mailing list