]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
MATCH: Fix PR 109834, ICE with popcount combined with bswap
authorAndrew Pinski <apinski@marvell.com>
Fri, 12 May 2023 23:33:44 +0000 (16:33 -0700)
committerAndrew Pinski <apinski@marvell.com>
Sat, 13 May 2023 03:10:19 +0000 (20:10 -0700)
After r14-673-gc0dd80e4c4c3, there was a check in the match
patterns which was checking the type is unsigned but
instead of using the type, the patch used the expression.
This adds the needed TREE_TYPE so get the correct answer and don't ICE.

Committed as obvious after a bootstrap/test on x86_64-linux-gnu.

PR tree-optimization/109834

gcc/ChangeLog:

* match.pd (popcount(bswap(x))->popcount(x)): Fix up unsigned type checking.
(popcount(rotate(x,y))->popcount(x)): Likewise.

gcc/testsuite/ChangeLog:

* gcc.c-torture/compile/pr109834-1.c: New test.
* gcc.dg/tree-ssa/pr109834-1.c: New test.

gcc/match.pd
gcc/testsuite/gcc.c-torture/compile/pr109834-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr109834-1.c [new file with mode: 0644]

index 2e46e074e9354e6be0f184785cda5dfa12a1434f..26395880047b6da3af7f5c0f603792954f7bac52 100644 (file)
@@ -7849,7 +7849,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
           && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
        (with { unsigned int prec0 = TYPE_PRECISION (TREE_TYPE (@0));
                unsigned int prec1 = TYPE_PRECISION (TREE_TYPE (@1)); }
-         (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (@1)))
+         (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (TREE_TYPE (@1))))
            (popcount @2)))))))
 
 /* popcount(rotate(X Y)) is popcount(X).  */
@@ -7862,7 +7862,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
           && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
        (with { unsigned int prec0 = TYPE_PRECISION (TREE_TYPE (@0));
                unsigned int prec1 = TYPE_PRECISION (TREE_TYPE (@1)); }
-         (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (@1)))
+         (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (TREE_TYPE (@1))))
            (popcount @2)))))))
 
 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr109834-1.c b/gcc/testsuite/gcc.c-torture/compile/pr109834-1.c
new file mode 100644 (file)
index 0000000..e6c0152
--- /dev/null
@@ -0,0 +1,4 @@
+int f (char v)
+{
+  return __builtin_popcount((int)__builtin_bswap16(v));
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr109834-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr109834-1.c
new file mode 100644 (file)
index 0000000..f2af18b
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int f (char v)
+{
+  return __builtin_popcount((int)__builtin_bswap16(v));
+}
+
+/* We should be able to remove the bswap here as it does not matter
+   for the popcount.  */
+/* { dg-final { scan-tree-dump-not "bswap" "optimized"} } */