]> gcc.gnu.org Git - gcc.git/commitdiff
match.pd: Optimize a * !a to 0 [PR114009]
authorJakub Jelinek <jakub@redhat.com>
Thu, 7 Mar 2024 07:43:16 +0000 (08:43 +0100)
committerJakub Jelinek <jakub@redhat.com>
Thu, 7 Mar 2024 07:43:16 +0000 (08:43 +0100)
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.

gcc/genmatch.cc
gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/pr114009.c [new file with mode: 0644]

index 61c4c8c02949b307fac8178941c2191cfc2eeb33..c982c95b70f48ffc68f3d3d15099936bbde91cdc 100644 (file)
@@ -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)
        {
index 4edba7c84fb9ac5ea5b02777e2ebbd3c0d201cd7..9ce313323a30082abdb5eea827a8d37d2e66ae7f 100644 (file)
@@ -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 (file)
index 0000000..3b0486e
--- /dev/null
@@ -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);
+}
This page took 0.09567 seconds and 5 git commands to generate.