From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 2C5413858D3C; Fri, 2 Jun 2023 08:58:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2C5413858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 62E4F1FDEE; Fri, 2 Jun 2023 08:58:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1685696291; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ah8B4PvOhyYEp4tPe+kEI9JPf5UqPB4Gbqh6kHXurXE=; b=KLFI7BoMU5+ZZyTDBS/TZd2zYrW64slbHZUr6zqdq1zmce/PyuCTUCvzAa4POLQz5QDQaX AwFp9cB+G+siLsu8kFTHJPxdtK+o3+2NOEOH9apy1PTm66kFCkw7rWkFJR0q7gaaYCc42y xlPR+IUNJwcNcVDEzKwYjL9Hb/UFPmo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1685696291; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ah8B4PvOhyYEp4tPe+kEI9JPf5UqPB4Gbqh6kHXurXE=; b=SEPNTHzjY9/K/SpMrHeA/P/QIPgFvnN6VlatC9dwZ4uIAA/vVeMppQwtrT+QlWUDmdKp2f yN/AJeLSYAVzVRBA== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 275802C141; Fri, 2 Jun 2023 08:58:11 +0000 (UTC) Date: Fri, 2 Jun 2023 08:58:11 +0000 (UTC) From: Richard Biener To: Jiufu Guo cc: Jiufu Guo via Gcc-patches , jeffreyalaw@gmail.com, segher@kernel.crashing.org, dje.gcc@gmail.com, linkw@gcc.gnu.org Subject: Re: [PATCH] Optimized "(X - N * M) / N + M" to "X / N" if valid In-Reply-To: <7njzwnkaqy.fsf@ltcden2-lp1.aus.stglabs.ibm.com> Message-ID: References: <20230517061050.3778864-1-guojiufu@linux.ibm.com> <7nsfbcx3ja.fsf@ltcden2-lp1.aus.stglabs.ibm.com> <7njzwnkaqy.fsf@ltcden2-lp1.aus.stglabs.ibm.com> User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Thu, 1 Jun 2023, Jiufu Guo wrote: > Hi, > > Jiufu Guo via Gcc-patches writes: > > > Hi, > > > > Richard Biener writes: > > > >> On Wed, 17 May 2023, Jiufu Guo wrote: > >> > >>> Hi, > >>> > >>> This patch tries to optimize "(X - N * M) / N + M" to "X / N". > >> > >> But if that's valid why not make the transform simpler and transform > >> (X - N * M) / N to X / N - M instead? > > > > Great catch! > > If "N * M" is not constant, "X / N - M" would be better than > > "(X - N * M) / N". If "N, M" are constants, "(X - N * M) / N" and > > "X / N - M" may be similar; while for this case, "X / N - M" should > > also be fine! I would try to update accordingly. > > > >> > >> You use the same optimize_x_minus_NM_div_N_plus_M validator for > >> the division and shift variants but the overflow rules are different, > >> so I'm not sure that's warranted. I'd also prefer to not split out > >> the validator to a different file - iff then the appropriate file > >> is fold-const.cc, not gimple-match-head.cc (I see we're a bit > >> inconsistent here, for pure gimple matches gimple-fold.cc would > >> be another place). > > > > Thanks for pointing out this! > > For shift, I guess you may concern that: 1. if the right operand is > > negative or is greater than or equal to the type width. 2. if it is > > a signed negative value. They may UB or 'sign bit shift'? This patch > > assumes it is ok to do the transform. I may have more check to see > > if this is really ok, and hope some one can point out if this is > > invalid. "(X - N * M) >> log2(N)" ==> " X >> log2(N) - M". > > > > I split out the validator just because: it is shared for division and > > shift :). And it seems gimple-match-head.cc and generic-match-head.cc, > > may be introduced for match.pd. So, I put it into gimple-match-head.cc. > > > >> > >> Since you use range information why is the transform restricted > >> to constant M? > > > > If M is a variable, the range for "X" is varying_p. I did not find > > the method to get the bounds for "X" (or for "X - N * M") to check no > > wraps. Any suggestions? > > Oh, I may misunderstand here. > You may say: M could be with a range too, then we can check if > "X - N * M" has a valid range or possible wrap/overflow. Yes. Richard. > BR, > Jeff (Jiufu Guo) > > > > > > > Again, thanks for your great help! > > > > BR, > > Jeff (Jiufu Guo) > > > >> > >> Richard. > >> > >>> As per the discussions in PR108757, we know this transformation is valid > >>> only under some conditions. > >>> For C code, "/" towards zero (trunc_div), and "X - N * M" > >>> maybe wrap/overflow/underflow. So, it is valid that "X - N * M" does > >>> not cross zero and does not wrap/overflow/underflow. > >>> > >>> This patch also handles the case when "N" is the power of 2, where > >>> "(X - N * M) / N" is "(X - N * M) >> log2(N)". > >>> > >>> Bootstrap & regtest pass on ppc64{,le} and x86_64. > >>> Is this ok for trunk? > >>> > >>> BR, > >>> Jeff (Jiufu) > >>> > >>> PR tree-optimization/108757 > >>> > >>> gcc/ChangeLog: > >>> > >>> * gimple-match-head.cc (optimize_x_minus_NM_div_N_plus_M): New function. > >>> * match.pd ((X - N * M) / N + M): New pattern. > >>> > >>> gcc/testsuite/ChangeLog: > >>> > >>> * gcc.dg/pr108757-1.c: New test. > >>> * gcc.dg/pr108757-2.c: New test. > >>> * gcc.dg/pr108757.h: New test. > >>> > >>> --- > >>> gcc/gimple-match-head.cc | 54 ++++++++++ > >>> gcc/match.pd | 22 ++++ > >>> gcc/testsuite/gcc.dg/pr108757-1.c | 17 ++++ > >>> gcc/testsuite/gcc.dg/pr108757-2.c | 18 ++++ > >>> gcc/testsuite/gcc.dg/pr108757.h | 160 ++++++++++++++++++++++++++++++ > >>> 5 files changed, 271 insertions(+) > >>> create mode 100644 gcc/testsuite/gcc.dg/pr108757-1.c > >>> create mode 100644 gcc/testsuite/gcc.dg/pr108757-2.c > >>> create mode 100644 gcc/testsuite/gcc.dg/pr108757.h > >>> > >>> diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc > >>> index b08cd891a13..680a4cb2fc6 100644 > >>> --- a/gcc/gimple-match-head.cc > >>> +++ b/gcc/gimple-match-head.cc > >>> @@ -224,3 +224,57 @@ optimize_successive_divisions_p (tree divisor, tree inner_div) > >>> } > >>> return true; > >>> } > >>> + > >>> +/* Return true if "(X - N * M) / N + M" can be optimized into "X / N". > >>> + Otherwise return false. > >>> + > >>> + For unsigned, > >>> + If sign bit of M is 0 (clz is 0), valid range is [N*M, MAX]. > >>> + If sign bit of M is 1, valid range is [0, MAX - N*(-M)]. > >>> + > >>> + For signed, > >>> + If N*M > 0, valid range: [MIN+N*M, 0] + [N*M, MAX] > >>> + If N*M < 0, valid range: [MIN, -(-N*M)] + [0, MAX - (-N*M)]. */ > >>> + > >>> +static bool > >>> +optimize_x_minus_NM_div_N_plus_M (tree x, wide_int n, wide_int m, tree type) > >>> +{ > >>> + wide_int max = wi::max_value (type); > >>> + signop sgn = TYPE_SIGN (type); > >>> + wide_int nm; > >>> + wi::overflow_type ovf; > >>> + if (TYPE_UNSIGNED (type) && wi::clz (m) == 0) > >>> + nm = wi::mul (n, -m, sgn, &ovf); > >>> + else > >>> + nm = wi::mul (n, m, sgn, &ovf); > >>> + > >>> + if (ovf != wi::OVF_NONE) > >>> + return false; > >>> + > >>> + value_range vr0; > >>> + if (!get_range_query (cfun)->range_of_expr (vr0, x) || vr0.varying_p () > >>> + || vr0.undefined_p ()) > >>> + return false; > >>> + > >>> + wide_int wmin0 = vr0.lower_bound (); > >>> + wide_int wmax0 = vr0.upper_bound (); > >>> + wide_int min = wi::min_value (type); > >>> + > >>> + /* unsigned */ > >>> + if ((TYPE_UNSIGNED (type))) > >>> + /* M > 0 (clz != 0): [N*M, MAX], M < 0 : [0, MAX-N*(-M)] */ > >>> + return wi::clz (m) != 0 ? wi::ge_p (wmin0, nm, sgn) > >>> + : wi::le_p (wmax0, max - nm, sgn); > >>> + > >>> + /* signed, N*M > 0 */ > >>> + else if (wi::gt_p (nm, 0, sgn)) > >>> + /* [N*M, MAX] or [MIN+N*M, 0] */ > >>> + return wi::ge_p (wmin0, nm, sgn) > >>> + || (wi::ge_p (wmin0, min + nm, sgn) && wi::le_p (wmax0, 0, sgn)); > >>> + > >>> + /* signed, N*M < 0 */ > >>> + /* [MIN, N*M] or [0, MAX + N*M]*/ > >>> + else > >>> + return wi::le_p (wmax0, nm, sgn) > >>> + || (wi::ge_p (wmin0, 0, sgn) && wi::le_p (wmax0, max - (-nm), sgn)); > >>> +} > >>> diff --git a/gcc/match.pd b/gcc/match.pd > >>> index ceae1c34abc..1aaa5530577 100644 > >>> --- a/gcc/match.pd > >>> +++ b/gcc/match.pd > >>> @@ -881,6 +881,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > >>> #endif > >>> )))) > >>> > >>> +#if GIMPLE > >>> +/* Simplify ((t + -N*M) / N + M) -> t / N. */ > >>> +(for div (trunc_div exact_div) > >>> + (simplify > >>> + (plus (div (plus @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3) > >>> + (with {wide_int n = wi::to_wide (@2); wide_int m = wi::to_wide (@3);} > >>> + (if (INTEGRAL_TYPE_P (type) > >>> + && n * m == -wi::to_wide (@1) > >>> + && optimize_x_minus_NM_div_N_plus_M (@0, n, m, type)) > >>> + (div @0 @2))))) > >>> + > >>> +/* Simplify ((t + -(M<> N + M) -> t >> N. */ > >>> +(simplify > >>> + (plus (rshift (plus @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3) > >>> + (with {wide_int n = wi::to_wide (@2); wide_int m = wi::to_wide (@3);} > >>> + (if (INTEGRAL_TYPE_P (type) > >>> + && (m << n) == -wi::to_wide (@1) > >>> + && optimize_x_minus_NM_div_N_plus_M (@0, > >>> + wi::one (TYPE_PRECISION (type)) << n, m, type)) > >>> + (rshift @0 @2)))) > >>> +#endif > >>> + > >>> (for op (negate abs) > >>> /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */ > >>> (for coss (COS COSH) > >>> diff --git a/gcc/testsuite/gcc.dg/pr108757-1.c b/gcc/testsuite/gcc.dg/pr108757-1.c > >>> new file mode 100644 > >>> index 00000000000..349318a7c82 > >>> --- /dev/null > >>> +++ b/gcc/testsuite/gcc.dg/pr108757-1.c > >>> @@ -0,0 +1,17 @@ > >>> +/* PR tree-optimization/108757 */ > >>> +/* { dg-do compile } */ > >>> +/* { dg-options "-O2 -fdump-tree-optimized" } */ > >>> + > >>> +#include > >>> +#define N 4 > >>> +#define M 3 > >>> +#define GAP 0 > >>> +typedef unsigned int UINT; > >>> +typedef int INT; > >>> +#define UMAX UINT_MAX > >>> +#define IMAX INT_MAX > >>> +#define IMIN INT_MIN > >>> +#include "pr108757.h" > >>> + > >>> +/* { dg-final { scan-tree-dump-not " = x_\[0-9\]\\(D\\) \\+ " "optimized" } } */ > >>> +/* { dg-final { scan-tree-dump-not " = b_\[0-9\] \\+ " "optimized" } } */ > >>> diff --git a/gcc/testsuite/gcc.dg/pr108757-2.c b/gcc/testsuite/gcc.dg/pr108757-2.c > >>> new file mode 100644 > >>> index 00000000000..7302e9c606c > >>> --- /dev/null > >>> +++ b/gcc/testsuite/gcc.dg/pr108757-2.c > >>> @@ -0,0 +1,18 @@ > >>> +/* PR tree-optimization/108757 */ > >>> +/* { dg-do compile } */ > >>> +/* { dg-options "-O2 -fdump-tree-optimized" } */ > >>> + > >>> +#include > >>> +#define N 4 > >>> +#define M 3 > >>> +#define GAP 2 > >>> +typedef unsigned int UINT; > >>> +typedef int INT; > >>> +#define UMAX UINT_MAX > >>> +#define IMAX INT_MAX > >>> +#define IMIN INT_MIN > >>> +#include "pr108757.h" > >>> + > >>> +/* { dg-final { scan-tree-dump-times " = x_\[0-9\]\\(D\\) \\+ " 16 "optimized" } } */ > >>> +/* { dg-final { scan-tree-dump-times " = b_\[0-9\] \\+ " 16 "optimized" } } */ > >>> + > >>> diff --git a/gcc/testsuite/gcc.dg/pr108757.h b/gcc/testsuite/gcc.dg/pr108757.h > >>> new file mode 100644 > >>> index 00000000000..9e12d106aad > >>> --- /dev/null > >>> +++ b/gcc/testsuite/gcc.dg/pr108757.h > >>> @@ -0,0 +1,160 @@ > >>> +#define NOINLINE __attribute__ ((noinline)) > >>> +UINT NOINLINE > >>> +opt_u1 (UINT x) > >>> +{ > >>> + if (x < (M * N) - GAP) > >>> + return 0; > >>> + UINT a = x - (M * N); > >>> + UINT b = a / N; > >>> + return b + M; > >>> +} > >>> + > >>> +UINT NOINLINE > >>> +opt_u2 (UINT x) > >>> +{ > >>> + if (x > (UMAX - (M * N) + GAP)) > >>> + return 0; > >>> + UINT a = x + (M * N); > >>> + UINT b = a / N; > >>> + return b - M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s1 (INT x) > >>> +{ > >>> + if (x < (M * N) - GAP) > >>> + return 0; > >>> + INT a = x - (M * N); > >>> + INT b = a / N; > >>> + return b + M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s2 (INT x) > >>> +{ > >>> + if (x < IMIN + (M * N) - GAP || x > 0) > >>> + return 0; > >>> + INT a = x - (M * N); > >>> + INT b = a / N; > >>> + return b + M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s3 (INT x) > >>> +{ > >>> + if (x < (M * N) - GAP) > >>> + return 0; > >>> + INT a = x - (M * N); > >>> + INT b = a / -N; > >>> + return b + -M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s4 (INT x) > >>> +{ > >>> + if (x < IMIN + (M * N) - GAP || x > 0) > >>> + return 0; > >>> + INT a = x - (M * N); > >>> + INT b = a / -N; > >>> + return b + -M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s5 (INT x) > >>> +{ > >>> + if (x > (-M * N) + GAP) > >>> + return 0; > >>> + INT a = x - (-M * N); > >>> + INT b = a / N; > >>> + return b + -M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s6 (INT x) > >>> +{ > >>> + if (x > IMAX - (M * N) + GAP || x < 0) > >>> + return 0; > >>> + INT a = x - (-M * N); > >>> + INT b = a / N; > >>> + return b + -M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s7 (INT x) > >>> +{ > >>> + if (x > (M * -N) + GAP) > >>> + return 0; > >>> + INT a = x - (M * -N); > >>> + INT b = a / -N; > >>> + return b + M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s8 (INT x) > >>> +{ > >>> + if (x > IMAX - (M * N) + GAP || x < 0) > >>> + return 0; > >>> + INT a = x - (M * -N); > >>> + INT b = a / -N; > >>> + return b + M; > >>> +} > >>> + > >>> +UINT NOINLINE > >>> +opt_u3 (UINT x) > >>> +{ > >>> + if (x < (M << N) - GAP) > >>> + return 0; > >>> + UINT a = x - (M << N); > >>> + UINT b = a >> N; > >>> + return b + M; > >>> +} > >>> + > >>> +UINT NOINLINE > >>> +opt_u4 (UINT x) > >>> +{ > >>> + if (x > (UMAX - (M << N)) + GAP) > >>> + return 0; > >>> + UINT a = x + (M << N); > >>> + UINT b = a >> N; > >>> + return b - M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s9 (INT x) > >>> +{ > >>> + if (x < (M << N) - GAP) > >>> + return 0; > >>> + INT a = x - (M << N); > >>> + INT b = a >> N; > >>> + return b + M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s10 (INT x) > >>> +{ > >>> + if (x < IMIN + (M << N) - GAP || x > 0) > >>> + return 0; > >>> + INT a = x - (M << N); > >>> + INT b = a >> N; > >>> + return b + M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s11 (INT x) > >>> +{ > >>> + if (x > (-M << N) + GAP) > >>> + return 0; > >>> + INT a = x - (-M << N); > >>> + INT b = a >> N; > >>> + return b + -M; > >>> +} > >>> + > >>> +INT NOINLINE > >>> +opt_s12 (INT x) > >>> +{ > >>> + if (x > IMAX - (M << N) + GAP || x < 0) > >>> + return 0; > >>> + INT a = x - (-M << N); > >>> + INT b = a >> N; > >>> + return b + -M; > >>> +} > >>> > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; HRB 36809 (AG Nuernberg)