From: Jiufu Guo <guojiufu@linux.ibm.com>
To: gcc-patches@gcc.gnu.org
Cc: rguenther@suse.de, jeffreyalaw@gmail.com,
segher@kernel.crashing.org, dje.gcc@gmail.com, linkw@gcc.gnu.org,
guojiufu@linux.ibm.com
Subject: [PATCH] Optimized "(X - N * M) / N + M" to "X / N" if valid
Date: Wed, 17 May 2023 14:10:50 +0800 [thread overview]
Message-ID: <20230517061050.3778864-1-guojiufu@linux.ibm.com> (raw)
Hi,
This patch tries to optimize "(X - N * M) / N + M" to "X / N".
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)) >> 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 <limits.h>
+#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 <limits.h>
+#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;
+}
--
2.31.1
next reply other threads:[~2023-05-17 6:10 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-17 6:10 Jiufu Guo [this message]
2023-05-30 9:48 ` Richard Biener
2023-05-31 9:18 ` Jiufu Guo
2023-06-01 5:33 ` Jiufu Guo
2023-06-02 8:58 ` Richard Biener
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230517061050.3778864-1-guojiufu@linux.ibm.com \
--to=guojiufu@linux.ibm.com \
--cc=dje.gcc@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jeffreyalaw@gmail.com \
--cc=linkw@gcc.gnu.org \
--cc=rguenther@suse.de \
--cc=segher@kernel.crashing.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).