[llvm] [InstCombine] Missed optimization: Fold (sext(a) & c1) == c2 to (a & c3) == trunc(c2) (PR #112646)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 3 13:02:34 PST 2024


================
@@ -0,0 +1,174 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+declare void @use(i8)
+
+define i1 @fold_sext_to_and(i8 %x) {
+; CHECK-LABEL: define i1 @fold_sext_to_and(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[X]], -127
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i8 [[TMP1]], 1
+; CHECK-NEXT:    ret i1 [[TMP3]]
+;
+  %1 = sext i8 %x to i32
+  %2 = and i32 %1, -2147483647
+  %3 = icmp eq i32 %2, 1
+  ret i1 %3
+}
+
+define i1 @fold_sext_to_and1(i8 %x) {
+; CHECK-LABEL: define i1 @fold_sext_to_and1(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[X]], -127
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp ne i8 [[TMP1]], 1
+; CHECK-NEXT:    ret i1 [[TMP3]]
+;
+  %1 = sext i8 %x to i32
+  %2 = and i32 %1, -2147483647
+  %3 = icmp ne i32 %2, 1
+  ret i1 %3
+}
+
+define i1 @fold_sext_to_and_multi_use(i8 %x) {
+; CHECK-LABEL: define i1 @fold_sext_to_and_multi_use(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sext i8 [[X]] to i32
+; CHECK-NEXT:    call void @use(i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP2:%.*]] = and i8 [[X]], -127
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i8 [[TMP2]], 1
+; CHECK-NEXT:    ret i1 [[TMP3]]
+;
+  %1 = sext i8 %x to i32
+  call void @use(i32 %1)
+  %2 = and i32 %1, -2147483647
+  %3 = icmp eq i32 %2, 1
+  ret i1 %3
+}
+
+define i1 @fold_sext_to_and_multi_use1(i8 %x) {
+; CHECK-LABEL: define i1 @fold_sext_to_and_multi_use1(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT:    [[TMP1:%.*]] = sext i8 [[X]] to i32
+; CHECK-NEXT:    call void @use(i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP2:%.*]] = and i8 [[X]], -127
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp ne i8 [[TMP2]], 1
+; CHECK-NEXT:    ret i1 [[TMP3]]
+;
+  %1 = sext i8 %x to i32
+  call void @use(i32 %1)
+  %2 = and i32 %1, -2147483647
+  %3 = icmp ne i32 %2, 1
+  ret i1 %3
+}
----------------
nikic wrote:

Would be nice to have a test that uses constants other than -2147483647 and 1. For example u0x40000002 and 2.

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


More information about the llvm-commits mailing list