[llvm] [InstCombine] Fold (X / C) < X and (X >> C) < X into X > 0 (PR #85555)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 9 00:36:52 PDT 2024
================
@@ -375,3 +375,144 @@ define i1 @sdiv_eq_smin_use(i32 %x, i32 %y) {
%r = icmp eq i32 %d, -2147483648
ret i1 %r
}
+
+; Fold (X / C) cmp X into X ~cmp 0 (~cmp is the inverse predicate of cmp), for some C != 1
+; Alternative form of this fold is when division is replaced with logic right shift
+
+define i1 @sdiv_x_by_const_cmp_x(i32 %x) {
+; CHECK-LABEL: @sdiv_x_by_const_cmp_x(
+; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], 0
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %v = sdiv i32 %x, 13
+ %r = icmp eq i32 %v, %x
+ ret i1 %r
+}
+
+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 eq i32 [[X:%.*]], 0
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %v = lshr i32 %x, 1
+ %r = icmp eq i32 %v, %x
+ ret i1 %r
+}
+
+define <4 x i1> @lshr_by_const_cmp_sle_value(<4 x i32> %x) {
+; CHECK-LABEL: @lshr_by_const_cmp_sle_value(
+; CHECK-NEXT: [[R:%.*]] = icmp sgt <4 x i32> [[X:%.*]], <i32 -1, i32 -1, i32 -1, i32 -1>
+; CHECK-NEXT: ret <4 x i1> [[R]]
+;
+ %v = lshr <4 x i32> %x, <i32 3, i32 3, i32 3, i32 3>
+ %r = icmp sle <4 x i32> %v, %x
+ ret <4 x i1> %r
+}
+
+define i1 @lshr_by_const_cmp_uge_value(i32 %x) {
----------------
nikic wrote:
```suggestion
define i1 @lshr_by_const_cmp_sge_value(i32 %x) {
```
https://github.com/llvm/llvm-project/pull/85555
More information about the llvm-commits
mailing list