[llvm] [InstCombine] Fold `A == MIN_INT ? B != MIN_INT : A < B` to `A < B` (PR #120177)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 18 21:46:06 PST 2024


================
@@ -0,0 +1,380 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i1 @compare_unsigned_min(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_unsigned_min(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[TMP2]]
+;
+start:
+  %2 = icmp eq i8 %0, 0
+  %3 = icmp ne i8 %1, 0
+  %4 = icmp ult i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @compare_signed_min(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_signed_min(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp slt i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[TMP4]]
+;
+start:
+  %2 = icmp eq i8 %0, -128
+  %3 = icmp ne i8 %1, -128
+  %4 = icmp slt i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @compare_unsigned_max(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_unsigned_max(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp ugt i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[TMP4]]
+;
+start:
+  %2 = icmp eq i8 %0, 255
+  %3 = icmp ne i8 %1, 255
+  %4 = icmp ugt i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @compare_signed_max(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_signed_max(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp sgt i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[TMP4]]
+;
+start:
+  %2 = icmp eq i8 %0, 127
+  %3 = icmp ne i8 %1, 127
+  %4 = icmp sgt i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @relational_cmp_unsigned_min(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @relational_cmp_unsigned_min(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp ult i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[TMP4]]
+;
+start:
+  %2 = icmp ule i8 %0, 0
+  %3 = icmp ugt i8 %1, 0
+  %4 = icmp ult i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @relational_cmp_signed_min(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @relational_cmp_signed_min(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp slt i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[TMP4]]
+;
+start:
+  %2 = icmp sle i8 %0, -128
+  %3 = icmp sgt i8 %1, -128
+  %4 = icmp slt i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @relational_cmp_unsigned_max(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @relational_cmp_unsigned_max(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp ugt i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[TMP4]]
+;
+start:
+  %2 = icmp uge i8 %0, 255
+  %3 = icmp ult i8 %1, 255
+  %4 = icmp ugt i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @relational_cmp_signed_max(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @relational_cmp_signed_max(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp sgt i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[TMP4]]
+;
+start:
+  %2 = icmp sge i8 %0, 127
+  %3 = icmp slt i8 %1, 127
+  %4 = icmp sgt i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+declare void @use(i1)
+
+define i1 @compare_signed_max_multiuse(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_signed_max_multiuse(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp sgt i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    call void @use(i1 [[TMP4]])
+; CHECK-NEXT:    [[RESULT:%.*]] = icmp sgt i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+start:
+  %2 = icmp eq i8 %0, 127
+  %3 = icmp ne i8 %1, 127
+  %4 = icmp sgt i8 %0, %1
+  call void @use(i1 %4)
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @compare_signed_min_samesign(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_signed_min_samesign(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[RESULT:%.*]] = icmp slt i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+start:
+  %2 = icmp eq i8 %0, -128
+  %3 = icmp ne i8 %1, -128
+  %4 = icmp samesign slt i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @compare_flipped(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_flipped(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[RESULT:%.*]] = icmp ult i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+start:
+  %2 = icmp eq i8 %0, 0
+  %3 = icmp ne i8 %1, 0
+  %4 = icmp ugt i8 %1, %0
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @compare_swapped(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_swapped(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[RESULT:%.*]] = icmp ult i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+start:
+  %2 = icmp ne i8 %0, 0
+  %3 = icmp ne i8 %1, 0
+  %4 = icmp ult i8 %0, %1
+  %result = select i1 %2, i1 %4, i1 %3
+  ret i1 %result
+}
+
+define i1 @compare_swapped_flipped_unsigned_max(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_swapped_flipped_unsigned_max(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[RESULT:%.*]] = icmp ugt i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+start:
+  %2 = icmp ne i8 %0, 255
+  %3 = icmp ne i8 %1, 255
+  %4 = icmp ult i8 %1, %0
+  %result = select i1 %2, i1 %4, i1 %3
+  ret i1 %result
+}
+
+define i1 @compare_unsigned_min_illegal_type(i9 %0, i9 %1) {
+; CHECK-LABEL: define i1 @compare_unsigned_min_illegal_type(
+; CHECK-SAME: i9 [[TMP0:%.*]], i9 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i9 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    ret i1 [[TMP2]]
+;
+start:
+  %2 = icmp eq i9 %0, 0
+  %3 = icmp ne i9 %1, 0
+  %4 = icmp ult i9 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define <2 x i1> @compare_vector(<2 x i8> %x, <2 x i8> %y) {
+; CHECK-LABEL: define <2 x i1> @compare_vector(
+; CHECK-SAME: <2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult <2 x i8> [[X]], [[Y]]
+; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
+;
+  %2 = icmp eq <2 x i8> %x, <i8 0, i8 0>
+  %3 = icmp ne <2 x i8> %y, <i8 0, i8 0>
+  %4 = icmp ult <2 x i8> %x, %y
+  %result = select <2 x i1> %2, <2 x i1> %3, <2 x i1> %4
+  ret <2 x i1> %result
+}
+
+define i1 @compare_pointer_negative(ptr %0, ptr %1) {
+; CHECK-LABEL: define i1 @compare_pointer_negative(
+; CHECK-SAME: ptr [[TMP0:%.*]], ptr [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq ptr [[TMP0]], inttoptr (i8 127 to ptr)
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp ne ptr [[TMP1]], inttoptr (i8 127 to ptr)
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp sgt ptr [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    [[RESULT:%.*]] = select i1 [[TMP2]], i1 [[TMP3]], i1 [[TMP4]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+start:
+  %X = inttoptr i8 127 to ptr
+  %2 = icmp eq ptr %0, %X
+  %3 = icmp ne ptr %1, %X
+  %4 = icmp sgt ptr %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @compare_float_negative(half %0, half %1) {
+; CHECK-LABEL: define i1 @compare_float_negative(
+; CHECK-SAME: half [[TMP0:%.*]], half [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP2:%.*]] = fcmp oeq half [[TMP0]], 0xH0000
+; CHECK-NEXT:    [[TMP3:%.*]] = fcmp one half [[TMP1]], 0xH0000
+; CHECK-NEXT:    [[TMP4:%.*]] = fcmp ult half [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    [[RESULT:%.*]] = select i1 [[TMP2]], i1 [[TMP3]], i1 [[TMP4]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+start:
+  %2 = fcmp oeq half %0, 0.0
+  %3 = fcmp one half %1, 0.0
+  %4 = fcmp ult half %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @compare_unsigned_max_swapped_lhs_rhs_negative(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_unsigned_max_swapped_lhs_rhs_negative(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 [[TMP0]], -1
+; CHECK-NEXT:    [[DOTNOT:%.*]] = icmp ne i8 [[TMP1]], -1
+; CHECK-NEXT:    [[RESULT:%.*]] = select i1 [[DOTNOT]], i1 [[TMP2]], i1 false
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+start:
+  %2 = icmp eq i8 %0, 255
+  %3 = icmp ne i8 %1, 255
+  %4 = icmp ugt i8 %0, %1
+  %result = select i1 %3, i1 %2, i1 %4
+  ret i1 %result
+}
+
+define i1 @compare_signed_min_negative(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_signed_min_negative(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 [[TMP0]], -127
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp ne i8 [[TMP1]], -127
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp slt i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    [[RESULT:%.*]] = select i1 [[TMP2]], i1 [[TMP3]], i1 [[TMP4]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+start:
+  %2 = icmp eq i8 %0, -127
+  %3 = icmp ne i8 %1, -127
+  %4 = icmp slt i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @compare_unsigned_max_negative(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @compare_unsigned_max_negative(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 [[TMP0]], -1
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp ne i8 [[TMP1]], -1
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp ult i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    [[RESULT:%.*]] = select i1 [[TMP2]], i1 [[TMP3]], i1 [[TMP4]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+start:
+  %2 = icmp eq i8 %0, 255
+  %3 = icmp ne i8 %1, 255
+  %4 = icmp ult i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @non_strict_op_negative(i8 %0, i8 %1) {
+; CHECK-LABEL: define i1 @non_strict_op_negative(
+; CHECK-SAME: i8 [[TMP0:%.*]], i8 [[TMP1:%.*]]) {
+; CHECK-NEXT:  [[START:.*:]]
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i8 [[TMP0]], 0
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp ne i8 [[TMP1]], 0
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp ule i8 [[TMP0]], [[TMP1]]
+; CHECK-NEXT:    [[RESULT:%.*]] = select i1 [[TMP2]], i1 [[TMP3]], i1 [[TMP4]]
+; CHECK-NEXT:    ret i1 [[RESULT]]
+;
+start:
+  %2 = icmp eq i8 %0, 0
+  %3 = icmp ne i8 %1, 0
+  %4 = icmp ule i8 %0, %1
+  %result = select i1 %2, i1 %3, i1 %4
+  ret i1 %result
+}
+
+define i1 @compare_poison_negative(i8 %0, i8 %1) {
----------------
veera-sivarajan wrote:

Sure, removed them.

https://github.com/llvm/llvm-project/pull/120177


More information about the llvm-commits mailing list