From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 0992C3836643; Thu, 16 Jun 2022 12:36:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0992C3836643 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 r13-1133] match.pd: Fix up __builtin_mul_overflow_p signed type optimization [PR105984] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 6a27c430468cb85454b19cef881a1422580657ff X-Git-Newrev: 74e6a40335765077e235269f19d2d9905d0d9e44 Message-Id: <20220616123641.0992C3836643@sourceware.org> Date: Thu, 16 Jun 2022 12:36:41 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Jun 2022 12:36:41 -0000 https://gcc.gnu.org/g:74e6a40335765077e235269f19d2d9905d0d9e44 commit r13-1133-g74e6a40335765077e235269f19d2d9905d0d9e44 Author: Jakub Jelinek Date: Thu Jun 16 14:36:04 2022 +0200 match.pd: Fix up __builtin_mul_overflow_p signed type optimization [PR105984] Earlier in the simplification pattern, we require that @0 has compatible type to the type of IMAGPART_EXPR, but for @1 which is a non-zero constant all we require is that it the constant fits into that type. Later the code checks if the constant is negative, because when min / max values are divided by negative divisor, lo will be higher than hi. In the following testcase, @1 has unsigned char type, while @0 has int type, so @1 which is 254 is wi::neg_p and we were swapping lo and hi, even when @1 cast to int isn't negative. We could use tree_int_cst_sgn (@1) < 0 as the check instead and it would work both for narrower types of @1 and even same or wider ones, but I've noticed we probably don't want to call fold_convert (TREE_TYPE (@0), @1) twice and when we save that result in a temporary, we can just use wi::neg_p on that temporary. 2022-06-16 Jakub Jelinek PR tree-optimization/105984 * match.pd (__builtin_mul_overflow_p (x, cst, (stype) 0) -> x > stype_max / cst || x < stype_min / cst): fold_convert @1 to TREE_TYPE (@0) just once and test for negative divisor also on that folded constant instead of on @1. * gcc.c-torture/execute/pr105984.c: New test. Diff: --- gcc/match.pd | 9 ++++----- gcc/testsuite/gcc.c-torture/execute/pr105984.c | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index c0aa3a2342b..ae5dc820fa5 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -6076,16 +6076,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); })) (with { + tree div = fold_convert (TREE_TYPE (@0), @1); tree lo = int_const_binop (TRUNC_DIV_EXPR, - TYPE_MIN_VALUE (TREE_TYPE (@0)), - fold_convert (TREE_TYPE (@0), @1)); + TYPE_MIN_VALUE (TREE_TYPE (@0)), div); tree hi = int_const_binop (TRUNC_DIV_EXPR, - TYPE_MAX_VALUE (TREE_TYPE (@0)), - fold_convert (TREE_TYPE (@0), @1)); + TYPE_MAX_VALUE (TREE_TYPE (@0)), div); tree etype = range_check_type (TREE_TYPE (@0)); if (etype) { - if (wi::neg_p (wi::to_wide (@1))) + if (wi::neg_p (wi::to_wide (div))) std::swap (lo, hi); lo = fold_convert (etype, lo); hi = fold_convert (etype, hi); diff --git a/gcc/testsuite/gcc.c-torture/execute/pr105984.c b/gcc/testsuite/gcc.c-torture/execute/pr105984.c new file mode 100644 index 00000000000..9143b3d3e7e --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr105984.c @@ -0,0 +1,19 @@ +/* PR tree-optimization/105984 */ + +unsigned long long g; + +static inline unsigned long long +foo (unsigned char c) +{ + g -= __builtin_mul_overflow_p (4, (unsigned char) ~c, 0); + return g; +} + +int +main () +{ + unsigned long long x = foo (1); + if (x != 0) + __builtin_abort (); + return 0; +}