aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2024-03-07 08:43:16 +0100
committerJakub Jelinek <jakub@redhat.com>2024-03-07 08:43:16 +0100
commit95b6ee96348041eaee9133f082b57f3e57ef0b11 (patch)
tree48ce45b9b597f606edaa0bd74508c21501f1b8b0 /gcc
parent1cd8254ebad7b73993d2acee80a7caf37c21878a (diff)
downloadgcc-95b6ee96348041eaee9133f082b57f3e57ef0b11.zip
gcc-95b6ee96348041eaee9133f082b57f3e57ef0b11.tar.gz
gcc-95b6ee96348041eaee9133f082b57f3e57ef0b11.tar.bz2
match.pd: Optimize a * !a to 0 [PR114009]
The following patch attempts to fix an optimization regression through adding a simple simplification. We already have the /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */ (if (!canonicalize_math_p ()) (for cmp (tcc_comparison) (simplify (mult:c (convert (cmp@0 @1 @2)) @3) (if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (@0))) (cond @0 @3 { build_zero_cst (type); }))) optimization which otherwise triggers during the a * !a multiplication, but that is done only late and we aren't able through range assumptions optimize it yet anyway. The patch adds a specific simplification for it. If a is zero, then a * !a will be 0 * 1 (or for signed 1-bit 0 * -1) and so 0. If a is non-zero, then a * !a will be a * 0 and so again 0. THe pattern is valid for scalar integers, complex integers and vector types, but I think will actually trigger only for the scalar integers. For vector types I've added other two with VEC_COND_EXPR in it, for complex there are different GENERIC trees to match and it is something that likely would be never matched in GIMPLE, so I didn't handle that. 2024-03-07 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/114009 * genmatch.cc (decision_tree::gen): Emit ARG_UNUSED for captures argument even for GENERIC, not just for GIMPLE. * match.pd (a * !a -> 0): New simplifications. * gcc.dg/tree-ssa/pr114009.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/genmatch.cc2
-rw-r--r--gcc/match.pd11
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/pr114009.c33
3 files changed, 45 insertions, 1 deletions
diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc
index 61c4c8c..c982c95 100644
--- a/gcc/genmatch.cc
+++ b/gcc/genmatch.cc
@@ -4071,7 +4071,7 @@ decision_tree::gen (vec <FILE *> &files, bool gimple)
for (unsigned i = 0;
i < as_a <expr *>(s->s->s->match)->ops.length (); ++i)
fp_decl (f, " tree ARG_UNUSED (_p%d),", i);
- fp_decl (f, " tree *captures");
+ fp_decl (f, " tree *ARG_UNUSED (captures)");
}
for (unsigned i = 0; i < s->s->s->for_subst_vec.length (); ++i)
{
diff --git a/gcc/match.pd b/gcc/match.pd
index 4edba7c..9ce3133 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1219,6 +1219,17 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
&& tree_nop_conversion_p (type, TREE_TYPE (@1)))
(lshift @0 @2)))
+/* Fold a * !a into 0. */
+(simplify
+ (mult:c @0 (convert? (eq @0 integer_zerop)))
+ { build_zero_cst (type); })
+(simplify
+ (mult:c @0 (vec_cond (eq @0 integer_zerop) @1 integer_zerop))
+ { build_zero_cst (type); })
+(simplify
+ (mult:c @0 (vec_cond (ne @0 integer_zerop) integer_zerop @1))
+ { build_zero_cst (type); })
+
/* Shifts by precision or greater result in zero. */
(for shift (lshift rshift)
(simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr114009.c b/gcc/testsuite/gcc.dg/tree-ssa/pr114009.c
new file mode 100644
index 0000000..3b0486e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr114009.c
@@ -0,0 +1,33 @@
+/* PR tree-optimization/114009 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wno-psabi -fdump-tree-forwprop1" } */
+/* { dg-final { scan-tree-dump-times " return 0;" 3 "forwprop1" } } */
+/* { dg-final { scan-tree-dump-times " (?:return|<retval> =) { 0, 0, 0, 0 };" 1 "forwprop1" } } */
+
+int
+foo (int x)
+{
+ x = (x / 2) * 2;
+ return (!x) * x;
+}
+
+int
+bar (int x, int y)
+{
+ (void) x;
+ return y * !y;
+}
+
+unsigned long long
+baz (unsigned long long x)
+{
+ return (!x) * x;
+}
+
+typedef int V __attribute__((vector_size (4 * sizeof (int))));
+
+V
+qux (V x)
+{
+ return x * (x == 0);
+}