[llvm] [InstCombine] Add more cases for simplifying `(icmp (and/or x, Mask), y)` (PR #85138)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 13 14:50:35 PDT 2024
================
@@ -657,3 +675,238 @@ define i1 @src_is_mask_const_sge(i8 %x_in) {
%r = icmp sge i8 %and, %x
ret i1 %r
}
+
+define i1 @src_x_and_mask_slt(i8 %x, i8 %y, i1 %cond) {
+; CHECK-LABEL: @src_x_and_mask_slt(
+; CHECK-NEXT: [[MASK0:%.*]] = lshr i8 -1, [[Y:%.*]]
+; CHECK-NEXT: [[MASK:%.*]] = select i1 [[COND:%.*]], i8 [[MASK0]], i8 0
+; CHECK-NEXT: [[MASK_POS:%.*]] = icmp sgt i8 [[MASK]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[MASK_POS]])
+; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[MASK]], [[X:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %mask0 = lshr i8 -1, %y
+ %mask = select i1 %cond, i8 %mask0, i8 0
+ %mask_pos = icmp sge i8 %mask, 0
+ call void @llvm.assume(i1 %mask_pos)
+ %and = and i8 %x, %mask
+ %r = icmp slt i8 %and, %x
+ ret i1 %r
+}
+
+define i1 @src_x_and_mask_sge(i8 %x, i8 %y, i1 %cond) {
+; CHECK-LABEL: @src_x_and_mask_sge(
+; CHECK-NEXT: [[MASK0:%.*]] = lshr i8 -1, [[Y:%.*]]
+; CHECK-NEXT: [[MASK:%.*]] = select i1 [[COND:%.*]], i8 [[MASK0]], i8 0
+; CHECK-NEXT: [[MASK_POS:%.*]] = icmp sgt i8 [[MASK]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[MASK_POS]])
+; CHECK-NEXT: [[R:%.*]] = icmp sge i8 [[MASK]], [[X:%.*]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %mask0 = lshr i8 -1, %y
+ %mask = select i1 %cond, i8 %mask0, i8 0
+ %mask_pos = icmp sge i8 %mask, 0
+ call void @llvm.assume(i1 %mask_pos)
+ %and = and i8 %x, %mask
+ %r = icmp sge i8 %and, %x
+ ret i1 %r
+}
+
+define i1 @src_x_and_mask_slt_fail_maybe_neg(i8 %x, i8 %y, i1 %cond) {
+; CHECK-LABEL: @src_x_and_mask_slt_fail_maybe_neg(
+; CHECK-NEXT: [[MASK0:%.*]] = lshr i8 -1, [[Y:%.*]]
+; CHECK-NEXT: [[MASK:%.*]] = select i1 [[COND:%.*]], i8 [[MASK0]], i8 0
+; CHECK-NEXT: [[AND:%.*]] = and i8 [[MASK]], [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[AND]], [[X]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %mask0 = lshr i8 -1, %y
+ %mask = select i1 %cond, i8 %mask0, i8 0
+ %and = and i8 %x, %mask
+ %r = icmp slt i8 %and, %x
+ ret i1 %r
+}
+
+define i1 @src_x_and_mask_sge_fail_maybe_neg(i8 %x, i8 %y, i1 %cond) {
+; CHECK-LABEL: @src_x_and_mask_sge_fail_maybe_neg(
+; CHECK-NEXT: [[MASK0:%.*]] = lshr i8 -1, [[Y:%.*]]
+; CHECK-NEXT: [[MASK:%.*]] = select i1 [[COND:%.*]], i8 [[MASK0]], i8 0
+; CHECK-NEXT: [[AND:%.*]] = and i8 [[MASK]], [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp sge i8 [[AND]], [[X]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %mask0 = lshr i8 -1, %y
+ %mask = select i1 %cond, i8 %mask0, i8 0
+ %and = and i8 %x, %mask
+ %r = icmp sge i8 %and, %x
+ ret i1 %r
+}
+
+define i1 @src_x_and_nmask_eq(i8 %x, i8 %y, i1 %cond) {
+; CHECK-LABEL: @src_x_and_nmask_eq(
+; CHECK-NEXT: [[NOT_MASK0:%.*]] = shl nsw i8 -1, [[Y:%.*]]
+; CHECK-NEXT: [[R1:%.*]] = icmp ule i8 [[NOT_MASK0]], [[X:%.*]]
+; CHECK-NEXT: [[NOT_COND:%.*]] = xor i1 [[COND:%.*]], true
+; CHECK-NEXT: [[R:%.*]] = select i1 [[NOT_COND]], i1 true, i1 [[R1]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %not_mask0 = shl i8 -1, %y
+ %not_mask = select i1 %cond, i8 %not_mask0, i8 0
+ %and = and i8 %x, %not_mask
+ %r = icmp eq i8 %not_mask, %and
+ ret i1 %r
+}
+
+define i1 @src_x_and_nmask_ne(i8 %x, i8 %y, i1 %cond) {
+; CHECK-LABEL: @src_x_and_nmask_ne(
+; CHECK-NEXT: [[NOT_MASK0:%.*]] = shl nsw i8 -1, [[Y:%.*]]
+; CHECK-NEXT: [[R1:%.*]] = icmp ugt i8 [[NOT_MASK0]], [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[COND:%.*]], i1 [[R1]], i1 false
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %not_mask0 = shl i8 -1, %y
+ %not_mask = select i1 %cond, i8 %not_mask0, i8 0
+ %and = and i8 %x, %not_mask
+ %r = icmp ne i8 %and, %not_mask
+ ret i1 %r
+}
+
+define i1 @src_x_and_nmask_ult(i8 %x, i8 %y, i1 %cond) {
+; CHECK-LABEL: @src_x_and_nmask_ult(
+; CHECK-NEXT: [[NOT_MASK0:%.*]] = shl nsw i8 -1, [[Y:%.*]]
+; CHECK-NEXT: [[R1:%.*]] = icmp ugt i8 [[NOT_MASK0]], [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[COND:%.*]], i1 [[R1]], i1 false
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %not_mask0 = shl i8 -1, %y
+ %not_mask = select i1 %cond, i8 %not_mask0, i8 0
+ %and = and i8 %x, %not_mask
+ %r = icmp ult i8 %and, %not_mask
+ ret i1 %r
+}
+
+define i1 @src_x_and_nmask_uge(i8 %x, i8 %y, i1 %cond) {
+; CHECK-LABEL: @src_x_and_nmask_uge(
+; CHECK-NEXT: [[NOT_MASK0:%.*]] = shl nsw i8 -1, [[Y:%.*]]
+; CHECK-NEXT: [[R1:%.*]] = icmp ule i8 [[NOT_MASK0]], [[X:%.*]]
+; CHECK-NEXT: [[NOT_COND:%.*]] = xor i1 [[COND:%.*]], true
+; CHECK-NEXT: [[R:%.*]] = select i1 [[NOT_COND]], i1 true, i1 [[R1]]
----------------
goldsteinn wrote:
NB: We do kind of save an instruction here.
The reason its the same before/after is we run into the case where we add instruction to canonicalize `select` to logical_or
https://github.com/llvm/llvm-project/pull/85138
More information about the llvm-commits
mailing list