From: Andrew Pinski <apinski@marvell.com>
To: <gcc-patches@gcc.gnu.org>
Cc: Andrew Pinski <apinski@marvell.com>
Subject: [PATCH 1/2] MATCH: [PR111364] Add some more minmax cmp operand simplifications
Date: Tue, 12 Sep 2023 08:25:41 -0700 [thread overview]
Message-ID: <20230912152542.1440800-1-apinski@marvell.com> (raw)
This adds a few more minmax cmp operand simplifications which were missed before.
`MIN(a,b) < a` -> `a > b`
`MIN(a,b) >= a` -> `a <= b`
`MAX(a,b) > a` -> `a < b`
`MAX(a,b) <= a` -> `a >= b`
OK? Bootstrapped and tested on x86_64-linux-gnu.
Note gcc.dg/pr96708-negative.c needed to updated to remove the
check for MIN/MAX as they have been optimized (correctly) away.
PR tree-optimization/111364
gcc/ChangeLog:
* match.pd (`MIN (X, Y) == X`): Extend
to min/lt, min/ge, max/gt, max/le.
gcc/testsuite/ChangeLog:
* gcc.c-torture/execute/minmaxcmp-1.c: New test.
* gcc.dg/tree-ssa/minmaxcmp-2.c: New test.
* gcc.dg/pr96708-negative.c: Update testcase.
* gcc.dg/pr96708-positive.c: Add comment about `return 0`.
---
gcc/match.pd | 8 +--
.../gcc.c-torture/execute/minmaxcmp-1.c | 51 +++++++++++++++++++
gcc/testsuite/gcc.dg/pr96708-negative.c | 4 +-
gcc/testsuite/gcc.dg/pr96708-positive.c | 1 +
gcc/testsuite/gcc.dg/tree-ssa/minmaxcmp-2.c | 30 +++++++++++
5 files changed, 89 insertions(+), 5 deletions(-)
create mode 100644 gcc/testsuite/gcc.c-torture/execute/minmaxcmp-1.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmaxcmp-2.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 51985c1bad4..36e3da4841b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3902,9 +3902,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(maxmin @0 (bit_not @1))))
/* MIN (X, Y) == X -> X <= Y */
-(for minmax (min min max max)
- cmp (eq ne eq ne )
- out (le gt ge lt )
+/* MIN (X, Y) < X -> X > Y */
+/* MIN (X, Y) >= X -> X <= Y */
+(for minmax (min min min min max max max max)
+ cmp (eq ne lt ge eq ne gt le )
+ out (le gt gt le ge lt lt ge )
(simplify
(cmp:c (minmax:c @0 @1) @0)
(if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
diff --git a/gcc/testsuite/gcc.c-torture/execute/minmaxcmp-1.c b/gcc/testsuite/gcc.c-torture/execute/minmaxcmp-1.c
new file mode 100644
index 00000000000..6705a053768
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/minmaxcmp-1.c
@@ -0,0 +1,51 @@
+#define func(vol, op1, op2) \
+_Bool op1##_##op2##_##vol (int a, int b) \
+{ \
+ vol int x = op_##op1(a, b); \
+ return op_##op2(x, a); \
+}
+
+#define op_lt(a, b) ((a) < (b))
+#define op_le(a, b) ((a) <= (b))
+#define op_eq(a, b) ((a) == (b))
+#define op_ne(a, b) ((a) != (b))
+#define op_gt(a, b) ((a) > (b))
+#define op_ge(a, b) ((a) >= (b))
+#define op_min(a, b) ((a) < (b) ? (a) : (b))
+#define op_max(a, b) ((a) > (b) ? (a) : (b))
+
+
+#define funcs(a) \
+ a(min,lt) \
+ a(max,lt) \
+ a(min,gt) \
+ a(max,gt) \
+ a(min,le) \
+ a(max,le) \
+ a(min,ge) \
+ a(max,ge) \
+ a(min,ne) \
+ a(max,ne) \
+ a(min,eq) \
+ a(max,eq)
+
+#define funcs1(a,b) \
+func(,a,b) \
+func(volatile,a,b)
+
+funcs(funcs1)
+
+#define test(op1,op2) \
+do { \
+ if (op1##_##op2##_(x,y) != op1##_##op2##_volatile(x,y)) \
+ __builtin_abort(); \
+} while(0);
+
+int main()
+{
+ for(int x = -10; x < 10; x++)
+ for(int y = -10; y < 10; y++)
+ {
+ funcs(test)
+ }
+}
diff --git a/gcc/testsuite/gcc.dg/pr96708-negative.c b/gcc/testsuite/gcc.dg/pr96708-negative.c
index 91964d3b971..c9c1aa85558 100644
--- a/gcc/testsuite/gcc.dg/pr96708-negative.c
+++ b/gcc/testsuite/gcc.dg/pr96708-negative.c
@@ -42,7 +42,7 @@ int main()
return 0;
}
-/* { dg-final { scan-tree-dump-times "MAX_EXPR" 2 "optimized" } } */
-/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "optimized" } } */
+/* Even though test[1-4] originally has MIN/MAX, those can be optimized away
+ into just comparing a and b arguments. */
/* { dg-final { scan-tree-dump-times "return 0;" 1 "optimized" } } */
/* { dg-final { scan-tree-dump-not { "return 1;" } "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/pr96708-positive.c b/gcc/testsuite/gcc.dg/pr96708-positive.c
index 65af85344b6..12c5fedfd30 100644
--- a/gcc/testsuite/gcc.dg/pr96708-positive.c
+++ b/gcc/testsuite/gcc.dg/pr96708-positive.c
@@ -42,6 +42,7 @@ int main()
return 0;
}
+/* Note main has one `return 0`. */
/* { dg-final { scan-tree-dump-times "return 0;" 3 "optimized" } } */
/* { dg-final { scan-tree-dump-times "return 1;" 2 "optimized" } } */
/* { dg-final { scan-tree-dump-not { "MAX_EXPR" } "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmaxcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/minmaxcmp-2.c
new file mode 100644
index 00000000000..f64a9253cfb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmaxcmp-2.c
@@ -0,0 +1,30 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+/* PR tree-optimization/111364 */
+
+#define min1(a, b) ((a) < (b) ? (a) : (b))
+#define max1(a, b) ((a) > (b) ? (a) : (b))
+
+int minlt(int a, int b)
+{
+ return min1(a, b) < a; // b < a or a > b
+}
+/* { dg-final { scan-tree-dump "return a > b;|return b < a;" "original" } } */
+
+int minge(int c, int d)
+{
+ return min1(c, d) >= c; // d >= c or c <= d
+}
+/* { dg-final { scan-tree-dump "return c <= d;|return d <= c;" "original" } } */
+
+int maxgt(int e, int f)
+{
+ return max1(e, f) > e; // e > f or f < e
+}
+/* { dg-final { scan-tree-dump "return e < f;|return f > e;" "original" } } */
+
+int maxle(int x, int y)
+{
+ return max1(x, y) <= x; // y <= x or x >= y
+}
+/* { dg-final { scan-tree-dump "return x >= y;|return y <= x;" "original" } } */
--
2.31.1
next reply other threads:[~2023-09-12 15:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-12 15:25 Andrew Pinski [this message]
2023-09-12 15:25 ` [PATCH 2/2] MATCH: Move `X <= MAX(X, Y)` before `MIN (X, C1) < C2` pattern Andrew Pinski
2023-09-13 6:43 ` Richard Biener
2023-09-13 6:42 ` [PATCH 1/2] MATCH: [PR111364] Add some more minmax cmp operand simplifications Richard Biener
2023-09-13 12:17 ` Andrew Pinski
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=20230912152542.1440800-1-apinski@marvell.com \
--to=apinski@marvell.com \
--cc=gcc-patches@gcc.gnu.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).