[llvm] 0f3a9ef - [InstCombine] Add tests for transforming `(icmp eq/ne trunc(x), truncOrZext(y))`; NFC
Noah Goldstein via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 19 10:15:29 PST 2023
Author: Noah Goldstein
Date: 2023-11-19T12:15:04-06:00
New Revision: 0f3a9efcbdc90b9e29dce405ee6c2ac5703fd63f
URL: https://github.com/llvm/llvm-project/commit/0f3a9efcbdc90b9e29dce405ee6c2ac5703fd63f
DIFF: https://github.com/llvm/llvm-project/commit/0f3a9efcbdc90b9e29dce405ee6c2ac5703fd63f.diff
LOG: [InstCombine] Add tests for transforming `(icmp eq/ne trunc(x), truncOrZext(y))`; NFC
Added:
llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll
Modified:
Removed:
################################################################################
diff --git a/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll b/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll
new file mode 100644
index 000000000000000..7cce1320e7b4b97
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/icmp-of-trunc-ext.ll
@@ -0,0 +1,280 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+declare void @llvm.assume(i1)
+declare void @use(i16)
+define i1 @icmp_trunc_x_trunc_y(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_trunc_x_trunc_y(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp ult i32 [[X:%.*]], 65536
+; CHECK-NEXT: [[Y_LB_ONLY:%.*]] = icmp ult i32 [[Y:%.*]], 65536
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: call void @llvm.assume(i1 [[Y_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = trunc i32 [[X]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = trunc i32 [[Y]] to i16
+; CHECK-NEXT: [[R:%.*]] = icmp eq i16 [[X16]], [[Y16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp ult i32 %x, 65536
+ %y_lb_only = icmp ult i32 %y, 65536
+ call void @llvm.assume(i1 %x_lb_only)
+ call void @llvm.assume(i1 %y_lb_only)
+ %x16 = trunc i32 %x to i16
+ %y16 = trunc i32 %y to i16
+ %r = icmp eq i16 %x16, %y16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_trunc_y_fail_from_illegal1(i256 %x, i256 %y) {
+; CHECK-LABEL: @icmp_trunc_x_trunc_y_fail_from_illegal1(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp ult i256 [[X:%.*]], 65536
+; CHECK-NEXT: [[Y_LB_ONLY:%.*]] = icmp ult i256 [[Y:%.*]], 65536
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: call void @llvm.assume(i1 [[Y_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = trunc i256 [[X]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = trunc i256 [[Y]] to i16
+; CHECK-NEXT: [[R:%.*]] = icmp eq i16 [[X16]], [[Y16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp ult i256 %x, 65536
+ %y_lb_only = icmp ult i256 %y, 65536
+ call void @llvm.assume(i1 %x_lb_only)
+ call void @llvm.assume(i1 %y_lb_only)
+ %x16 = trunc i256 %x to i16
+ %y16 = trunc i256 %y to i16
+ %r = icmp eq i16 %x16, %y16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_trunc_y_illegal_trunc_to_legal_anyways(i123 %x, i32 %y) {
+; CHECK-LABEL: @icmp_trunc_x_trunc_y_illegal_trunc_to_legal_anyways(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp ult i123 [[X:%.*]], 65536
+; CHECK-NEXT: [[Y_LB_ONLY:%.*]] = icmp ult i32 [[Y:%.*]], 65536
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: call void @llvm.assume(i1 [[Y_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = trunc i123 [[X]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = trunc i32 [[Y]] to i16
+; CHECK-NEXT: [[R:%.*]] = icmp eq i16 [[X16]], [[Y16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp ult i123 %x, 65536
+ %y_lb_only = icmp ult i32 %y, 65536
+ call void @llvm.assume(i1 %x_lb_only)
+ call void @llvm.assume(i1 %y_lb_only)
+ %x16 = trunc i123 %x to i16
+ %y16 = trunc i32 %y to i16
+ %r = icmp eq i16 %x16, %y16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_trunc_y_2_illegal_anyways(i33 %x, i63 %y) {
+; CHECK-LABEL: @icmp_trunc_x_trunc_y_2_illegal_anyways(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp ult i33 [[X:%.*]], 512
+; CHECK-NEXT: [[Y_LB_ONLY:%.*]] = icmp ult i63 [[Y:%.*]], 512
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: call void @llvm.assume(i1 [[Y_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = trunc i33 [[X]] to i9
+; CHECK-NEXT: [[Y16:%.*]] = trunc i63 [[Y]] to i9
+; CHECK-NEXT: [[R:%.*]] = icmp ult i9 [[Y16]], [[X16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp ult i33 %x, 512
+ %y_lb_only = icmp ult i63 %y, 512
+ call void @llvm.assume(i1 %x_lb_only)
+ call void @llvm.assume(i1 %y_lb_only)
+ %x16 = trunc i33 %x to i9
+ %y16 = trunc i63 %y to i9
+ %r = icmp ult i9 %y16, %x16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_trunc_y_3(i64 %x, i32 %y) {
+; CHECK-LABEL: @icmp_trunc_x_trunc_y_3(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp ult i64 [[X:%.*]], 123
+; CHECK-NEXT: [[Y_LB_ONLY:%.*]] = icmp ult i32 [[Y:%.*]], 256
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: call void @llvm.assume(i1 [[Y_LB_ONLY]])
+; CHECK-NEXT: [[XI8:%.*]] = trunc i64 [[X]] to i8
+; CHECK-NEXT: [[YI8:%.*]] = trunc i32 [[Y]] to i8
+; CHECK-NEXT: [[R:%.*]] = icmp ule i8 [[YI8]], [[XI8]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp ult i64 %x, 123
+ %y_lb_only = icmp ult i32 %y, 256
+ call void @llvm.assume(i1 %x_lb_only)
+ call void @llvm.assume(i1 %y_lb_only)
+ %xi8 = trunc i64 %x to i8
+ %yi8 = trunc i32 %y to i8
+ %r = icmp ule i8 %yi8, %xi8
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_trunc_y_fail_maybe_dirty_upper(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_trunc_x_trunc_y_fail_maybe_dirty_upper(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp ult i32 [[X:%.*]], 65536
+; CHECK-NEXT: [[Y_LB_ONLY:%.*]] = icmp ult i32 [[Y:%.*]], 65537
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: call void @llvm.assume(i1 [[Y_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = trunc i32 [[X]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = trunc i32 [[Y]] to i16
+; CHECK-NEXT: [[R:%.*]] = icmp ne i16 [[X16]], [[Y16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp ult i32 %x, 65536
+ %y_lb_only = icmp ult i32 %y, 65537
+ call void @llvm.assume(i1 %x_lb_only)
+ call void @llvm.assume(i1 %y_lb_only)
+ %x16 = trunc i32 %x to i16
+ %y16 = trunc i32 %y to i16
+ %r = icmp ne i16 %x16, %y16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_trunc_y_fail_maybe_dirty_upper_2(i32 %x, i32 %y) {
+; CHECK-LABEL: @icmp_trunc_x_trunc_y_fail_maybe_dirty_upper_2(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp slt i32 [[X:%.*]], 65536
+; CHECK-NEXT: [[Y_LB_ONLY:%.*]] = icmp ult i32 [[Y:%.*]], 65536
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: call void @llvm.assume(i1 [[Y_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = trunc i32 [[X]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = trunc i32 [[Y]] to i16
+; CHECK-NEXT: [[R:%.*]] = icmp ne i16 [[X16]], [[Y16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp slt i32 %x, 65536
+ %y_lb_only = icmp ult i32 %y, 65536
+ call void @llvm.assume(i1 %x_lb_only)
+ call void @llvm.assume(i1 %y_lb_only)
+ %x16 = trunc i32 %x to i16
+ %y16 = trunc i32 %y to i16
+ %r = icmp ne i16 %x16, %y16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_trunc_y_swap0(i33 %x, i32 %y) {
+; CHECK-LABEL: @icmp_trunc_x_trunc_y_swap0(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp ult i33 [[X:%.*]], 65536
+; CHECK-NEXT: [[Y_LB_ONLY:%.*]] = icmp ult i32 [[Y:%.*]], 65536
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: call void @llvm.assume(i1 [[Y_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = trunc i33 [[X]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = trunc i32 [[Y]] to i16
+; CHECK-NEXT: [[R:%.*]] = icmp ule i16 [[X16]], [[Y16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp ult i33 %x, 65536
+ %y_lb_only = icmp ult i32 %y, 65536
+ call void @llvm.assume(i1 %x_lb_only)
+ call void @llvm.assume(i1 %y_lb_only)
+ %x16 = trunc i33 %x to i16
+ %y16 = trunc i32 %y to i16
+ %r = icmp ule i16 %x16, %y16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_trunc_y_swap1(i33 %x, i32 %y) {
+; CHECK-LABEL: @icmp_trunc_x_trunc_y_swap1(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp ult i33 [[X:%.*]], 65536
+; CHECK-NEXT: [[Y_LB_ONLY:%.*]] = icmp ult i32 [[Y:%.*]], 65536
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: call void @llvm.assume(i1 [[Y_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = trunc i33 [[X]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = trunc i32 [[Y]] to i16
+; CHECK-NEXT: [[R:%.*]] = icmp ule i16 [[Y16]], [[X16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp ult i33 %x, 65536
+ %y_lb_only = icmp ult i32 %y, 65536
+ call void @llvm.assume(i1 %x_lb_only)
+ call void @llvm.assume(i1 %y_lb_only)
+ %x16 = trunc i33 %x to i16
+ %y16 = trunc i32 %y to i16
+ %r = icmp ule i16 %y16, %x16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_zext_y(i32 %x, i8 %y) {
+; CHECK-LABEL: @icmp_trunc_x_zext_y(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp ult i32 [[X:%.*]], 65536
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = trunc i32 [[X]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = zext i8 [[Y:%.*]] to i16
+; CHECK-NEXT: [[R:%.*]] = icmp ugt i16 [[X16]], [[Y16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp ult i32 %x, 65536
+ call void @llvm.assume(i1 %x_lb_only)
+ %x16 = trunc i32 %x to i16
+ %y16 = zext i8 %y to i16
+ %r = icmp ugt i16 %x16, %y16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_zext_y_2(i32 %x, i8 %y) {
+; CHECK-LABEL: @icmp_trunc_x_zext_y_2(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp ult i32 [[X:%.*]], 65536
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = trunc i32 [[X]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = zext i8 [[Y:%.*]] to i16
+; CHECK-NEXT: [[R:%.*]] = icmp uge i16 [[Y16]], [[X16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp ult i32 %x, 65536
+ call void @llvm.assume(i1 %x_lb_only)
+ %x16 = trunc i32 %x to i16
+ %y16 = zext i8 %y to i16
+ %r = icmp uge i16 %y16, %x16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_zext_y_3(i6 %x, i32 %y) {
+; CHECK-LABEL: @icmp_trunc_x_zext_y_3(
+; CHECK-NEXT: [[Y_LB_ONLY:%.*]] = icmp ult i32 [[Y:%.*]], 65536
+; CHECK-NEXT: call void @llvm.assume(i1 [[Y_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = zext i6 [[X:%.*]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = trunc i32 [[Y]] to i16
+; CHECK-NEXT: [[R:%.*]] = icmp ne i16 [[Y16]], [[X16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %y_lb_only = icmp ult i32 %y, 65536
+ call void @llvm.assume(i1 %y_lb_only)
+ %x16 = zext i6 %x to i16
+ %y16 = trunc i32 %y to i16
+ %r = icmp ne i16 %y16, %x16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_zext_y_3_fail_illegal(i6 %x, i45 %y) {
+; CHECK-LABEL: @icmp_trunc_x_zext_y_3_fail_illegal(
+; CHECK-NEXT: [[Y_LB_ONLY:%.*]] = icmp ult i45 [[Y:%.*]], 65536
+; CHECK-NEXT: call void @llvm.assume(i1 [[Y_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = zext i6 [[X:%.*]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = trunc i45 [[Y]] to i16
+; CHECK-NEXT: [[R:%.*]] = icmp ne i16 [[Y16]], [[X16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %y_lb_only = icmp ult i45 %y, 65536
+ call void @llvm.assume(i1 %y_lb_only)
+ %x16 = zext i6 %x to i16
+ %y16 = trunc i45 %y to i16
+ %r = icmp ne i16 %y16, %x16
+ ret i1 %r
+}
+
+define i1 @icmp_trunc_x_zext_y_fail_multiuse(i32 %x, i8 %y) {
+; CHECK-LABEL: @icmp_trunc_x_zext_y_fail_multiuse(
+; CHECK-NEXT: [[X_LB_ONLY:%.*]] = icmp ult i32 [[X:%.*]], 65536
+; CHECK-NEXT: call void @llvm.assume(i1 [[X_LB_ONLY]])
+; CHECK-NEXT: [[X16:%.*]] = trunc i32 [[X]] to i16
+; CHECK-NEXT: [[Y16:%.*]] = zext i8 [[Y:%.*]] to i16
+; CHECK-NEXT: call void @use(i16 [[Y16]])
+; CHECK-NEXT: [[R:%.*]] = icmp ule i16 [[X16]], [[Y16]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %x_lb_only = icmp ult i32 %x, 65536
+ call void @llvm.assume(i1 %x_lb_only)
+ %x16 = trunc i32 %x to i16
+ %y16 = zext i8 %y to i16
+ call void @use(i16 %y16)
+ %r = icmp ule i16 %x16, %y16
+ ret i1 %r
+}
More information about the llvm-commits
mailing list