]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
match.pd: Fix (FTYPE) N CMP (FTYPE) M optimization for GENERIC [PR118522]
authorJakub Jelinek <jakub@redhat.com>
Fri, 17 Jan 2025 10:30:07 +0000 (11:30 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 10 Feb 2025 14:37:43 +0000 (15:37 +0100)
The last case of this optimization assumes that if 2 integral types
have same precision and TYPE_UNSIGNED, then they are uselessly convertible.
While that is very likely the case for GIMPLE, it is not the case for
GENERIC, so the following patch adds there a convert so that the
optimization produces also valid GENERIC.  Without it we got
(int) p == b where b had _BitInt(32) type, so incompatible types.

2025-01-17  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/118522
* match.pd ((FTYPE) N CMP (FTYPE) M): Add convert, as in GENERIC
integral types with the same precision and sign might actually not
be compatible types.

* gcc.dg/bitint-120.c: New test.

(cherry picked from commit 3ab9eb6946f7b832834b3d808c5617935e0be727)

gcc/match.pd
gcc/testsuite/gcc.dg/bitint-120.c [new file with mode: 0644]

index f16fdd2d776061f9d964488a9deec6d0ef8d9ba6..8465af07df8dbf327bb8d04d5168c3fba265e8b4 100644 (file)
@@ -6548,7 +6548,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
          (icmp (convert:type2 @1) @2)
          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
               && type1_signed_p == type2_signed_p)
-         (icmp @1 @2))))))))))
+         (icmp @1 (convert @2)))))))))))
 
 /* Optimize various special cases of (FTYPE) N CMP CST.  */
 (for cmp  (lt le eq ne ge gt)
diff --git a/gcc/testsuite/gcc.dg/bitint-120.c b/gcc/testsuite/gcc.dg/bitint-120.c
new file mode 100644 (file)
index 0000000..5109888
--- /dev/null
@@ -0,0 +1,11 @@
+/* PR tree-optimization/118522 */
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-O2" } */
+
+_BitInt(32) b;
+
+int
+foo (unsigned short p)
+{
+  return p == (double) b;
+}