From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id BBDE33857348; Fri, 2 Jun 2023 07:46:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BBDE33857348 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685691972; bh=8OdVOKY2n9Ok0/FfNDOqjYqUoqZ3J6v7MD3wwrhYkLo=; h=From:To:Subject:Date:From; b=DOttFtkLgQicEKec0IDJc/l21qZVW3JLCKlgwCDJHfeHgk4u6wZK3BQWc36Z0eA5E THYSQPTtv2Afm1oGGkn/h3Hrzg+XOSTlIIG9/IxY+NPz17cFKiCS2uqExRy8MgVL+7 +d3q7/Qx4hxCPFzvn6p7hCbMt3vpLPrD5lH8lu5g= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-10841] match.pd: Ensure (op CONSTANT_CLASS_P CONSTANT_CLASS_P) is simplified [PR109505] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: bfa476528ceeac96865a48c49f3f1a15d566d209 X-Git-Newrev: ca4a4cc0060cb8ae1a326d6dbfcd9459452e1574 Message-Id: <20230602074612.BBDE33857348@sourceware.org> Date: Fri, 2 Jun 2023 07:46:12 +0000 (GMT) List-Id: https://gcc.gnu.org/g:ca4a4cc0060cb8ae1a326d6dbfcd9459452e1574 commit r11-10841-gca4a4cc0060cb8ae1a326d6dbfcd9459452e1574 Author: Jakub Jelinek Date: Sun May 21 13:36:56 2023 +0200 match.pd: Ensure (op CONSTANT_CLASS_P CONSTANT_CLASS_P) is simplified [PR109505] On the following testcase we hang, because POLY_INT_CST is CONSTANT_CLASS_P, but BIT_AND_EXPR with it and INTEGER_CST doesn't simplify and the (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) simplification actually relies on the (CST1 & CST2) simplification, otherwise it is a deoptimization, trading 2 ops for 3 and furthermore running into /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both operands are another bit-wise operation with a common input. If so, distribute the bit operations to save an operation and possibly two if constants are involved. For example, convert (A | B) & (A | C) into A | (B & C) Further simplification will occur if B and C are constants. */ simplification which simplifies that (x & CST2) | (CST1 & CST2) back to CST2 & (x | CST1). I went through all other places I could find where we have a simplification with 2 CONSTANT_CLASS_P operands and perform some operation on those two, while the other spots aren't that severe (just trade 2 operations for another 2 if the two constants don't simplify, rather than as in the above case trading 2 ops for 3), I still think all those spots really intend to optimize only if the 2 constants simplify. So, the following patch adds to those a ! modifier to ensure that, even at GENERIC that modifier means !EXPR_P which is exactly what we want IMHO. 2023-05-21 Jakub Jelinek PR tree-optimization/109505 * match.pd ((x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2), Combine successive equal operations with constants, (A +- CST1) +- CST2 -> A + CST3, (CST1 - A) +- CST2 -> CST3 - A, CST1 - (CST2 - A) -> CST3 + A): Use ! on ops with 2 CONSTANT_CLASS_P operands. * gcc.target/aarch64/sve/pr109505.c: New test. (cherry picked from commit f211757f6fa9515e3fd1a4f66f1a8b48e500c9de) Diff: --- gcc/match.pd | 20 ++++++++++---------- gcc/testsuite/gcc.target/aarch64/sve/pr109505.c | 12 ++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index 91a3762e320..4f83c9c2d36 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1543,7 +1543,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */ (simplify (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) - (bit_ior (bit_and @0 @2) (bit_and @1 @2))) + (bit_ior (bit_and @0 @2) (bit_and! @1 @2))) /* Combine successive equal operations with constants. */ (for bitop (bit_and bit_ior bit_xor) @@ -1552,7 +1552,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (!CONSTANT_CLASS_P (@0)) /* This is the canonical form regardless of whether (bitop @1 @2) can be folded to a constant. */ - (bitop @0 (bitop @1 @2)) + (bitop @0 (bitop! @1 @2)) /* In this case we have three constants and (bitop @0 @1) doesn't fold to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if the values involved are such that the operation can't be decided at @@ -2432,13 +2432,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) forever if something doesn't simplify into a constant. */ (if (!CONSTANT_CLASS_P (@0)) (if (outer_op == PLUS_EXPR) - (plus (view_convert @0) (inner_op @2 (view_convert @1))) - (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))) + (plus (view_convert @0) (inner_op! @2 (view_convert @1))) + (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1))))) (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) (if (outer_op == PLUS_EXPR) - (view_convert (plus @0 (inner_op (view_convert @2) @1))) - (view_convert (minus @0 (neg_inner_op (view_convert @2) @1)))) + (view_convert (plus @0 (inner_op! (view_convert @2) @1))) + (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1)))) /* If the constant operation overflows we cannot do the transform directly as we would introduce undefined overflow, for example with (a - 1) + INT_MIN. */ @@ -2469,10 +2469,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse forever if something doesn't simplify into a constant. */ (if (!CONSTANT_CLASS_P (@0)) - (minus (outer_op (view_convert @1) @2) (view_convert @0))) + (minus (outer_op! (view_convert @1) @2) (view_convert @0))) (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) - (view_convert (minus (outer_op @1 (view_convert @2)) @0)) + (view_convert (minus (outer_op! @1 (view_convert @2)) @0)) (if (types_match (type, @0)) (with { tree cst = const_binop (outer_op, type, @1, @2); } (if (cst && !TREE_OVERFLOW (cst)) @@ -2488,10 +2488,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse forever if something doesn't simplify into a constant. */ (if (!CONSTANT_CLASS_P (@0)) - (plus (view_convert @0) (minus @1 (view_convert @2)))) + (plus (view_convert @0) (minus! @1 (view_convert @2)))) (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) - (view_convert (plus @0 (minus (view_convert @1) @2))) + (view_convert (plus @0 (minus! (view_convert @1) @2))) (if (types_match (type, @0)) (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); } (if (cst && !TREE_OVERFLOW (cst)) diff --git a/gcc/testsuite/gcc.target/aarch64/sve/pr109505.c b/gcc/testsuite/gcc.target/aarch64/sve/pr109505.c new file mode 100644 index 00000000000..b975ae75ae6 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/sve/pr109505.c @@ -0,0 +1,12 @@ +/* PR tree-optimization/109505 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=armv8.2-a+sve" } */ + +#pragma GCC aarch64 "arm_sve.h" + +unsigned long +foo (unsigned long x) +{ + unsigned long y = svcntb (); + return (x | 15) & y; +}