]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
MATCH: [PR111364] Add some more minmax cmp operand simplifications
authorAndrew Pinski <apinski@marvell.com>
Tue, 12 Sep 2023 05:16:06 +0000 (05:16 +0000)
committerAndrew Pinski <apinski@marvell.com>
Wed, 13 Sep 2023 12:06:48 +0000 (12:06 +0000)
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
gcc/testsuite/gcc.c-torture/execute/minmaxcmp-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr96708-negative.c
gcc/testsuite/gcc.dg/pr96708-positive.c
gcc/testsuite/gcc.dg/tree-ssa/minmaxcmp-2.c [new file with mode: 0644]

index 0d865d8295f1ebc77a40ef3815c08a2c46d79a2a..ea4c7c5cf536f9e4dc894bf6f9588969b005208f 100644 (file)
@@ -3918,9 +3918,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 (file)
index 0000000..6705a05
--- /dev/null
@@ -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)
+    }
+}
index 91964d3b971dc52055db43f6a7fc6fdd257ac8e7..c9c1aa855583613a158e59b0a371e5c449e114d1 100644 (file)
@@ -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" } } */
index 65af85344b62303294ca8b332d3556b092a01889..12c5fedfd30bfc10fc4321159438f35d3ef49962 100644 (file)
@@ -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 (file)
index 0000000..f64a925
--- /dev/null
@@ -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" } } */