From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 7F6A43858D35; Thu, 7 Mar 2024 07:47:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7F6A43858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709797622; bh=I+kUOAg6KPAmL9goVqwrtkbRDCMmVIj7qk4xHqmmxLA=; h=From:To:Subject:Date:From; b=ytbX5su1obrsT4mpHf8A3Dr283X3C2yn51w5Y/ALgJscyikqJ/KfRP00BHdZjX68D pZeXVBo/wCgWlzfGHg9+dRQjd0kBCAvYXsnF1rwvNy8ENVzivAeOAXoUu1HUj6M1px 6fFvYteMuiqyvooCfnfC01X+6WuEKc6gaJ43x53A= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-9350] match.pd: Optimize a * !a to 0 [PR114009] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 1cd8254ebad7b73993d2acee80a7caf37c21878a X-Git-Newrev: 95b6ee96348041eaee9133f082b57f3e57ef0b11 Message-Id: <20240307074702.7F6A43858D35@sourceware.org> Date: Thu, 7 Mar 2024 07:47:02 +0000 (GMT) List-Id: https://gcc.gnu.org/g:95b6ee96348041eaee9133f082b57f3e57ef0b11 commit r14-9350-g95b6ee96348041eaee9133f082b57f3e57ef0b11 Author: Jakub Jelinek Date: Thu Mar 7 08:43:16 2024 +0100 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 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. Diff: --- gcc/genmatch.cc | 2 +- gcc/match.pd | 11 +++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr114009.c | 33 ++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc index 61c4c8c0294..c982c95b70f 100644 --- a/gcc/genmatch.cc +++ b/gcc/genmatch.cc @@ -4071,7 +4071,7 @@ decision_tree::gen (vec &files, bool gimple) for (unsigned i = 0; i < as_a (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 4edba7c84fb..9ce313323a3 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 00000000000..3b0486e16ad --- /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| =) { 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); +}