MATCH: Add `~MAX(~X, Y)` pattern: [PR96694]
Checks
Commit Message
This adds `~MAX(~X, Y)` and `~MIN(~X, Y)` patterns
that are like the `~(~a & b)` and `~(~a | b)` patterns
and allows to reduce the number of ~ by 1.
OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
PR tree-optimization/96694
gcc/ChangeLog:
* match.pd (`~MAX(~X, Y)`, `~MIN(~X, Y)`): New patterns.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/minmax-24.c: New test.
---
gcc/match.pd | 7 ++++-
gcc/testsuite/gcc.dg/tree-ssa/minmax-24.c | 31 +++++++++++++++++++++++
2 files changed, 37 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmax-24.c
Comments
On 9/3/23 18:21, Andrew Pinski via Gcc-patches wrote:
> This adds `~MAX(~X, Y)` and `~MIN(~X, Y)` patterns
> that are like the `~(~a & b)` and `~(~a | b)` patterns
> and allows to reduce the number of ~ by 1.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
> PR tree-optimization/96694
>
> gcc/ChangeLog:
>
> * match.pd (`~MAX(~X, Y)`, `~MIN(~X, Y)`): New patterns.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/tree-ssa/minmax-24.c: New test.
OK.
jeff
@@ -3786,7 +3786,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
maxmin (max min)
(simplify
(minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
- (bit_not (maxmin @0 @1))))
+ (bit_not (maxmin @0 @1)))
+/* ~MAX(~X, Y) --> MIN(X, ~Y) */
+/* ~MIN(~X, Y) --> MAX(X, ~Y) */
+ (simplify
+ (bit_not (minmax:cs (bit_not @0) @1))
+ (maxmin @0 (bit_not @1))))
/* MIN (X, Y) == X -> X <= Y */
(for minmax (min min max max)
new file mode 100644
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/96694 */
+
+static inline int min(int a, int b)
+{
+ return a < b ? a : b;
+}
+
+static inline int max(int a, int b)
+{
+ return a > b ? a : b;
+}
+
+int max_not(int x, int y)
+{
+ return ~max(~x, y); // min (x, ~y)
+}
+/* { dg-final { scan-tree-dump "~y_\[0-9\]+.D.;" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "~x_\[0-9\]+.D.;" "optimized" } } */
+/* { dg-final { scan-tree-dump "MIN_EXPR <x_\[0-9\]+.D., _\[0-9\]+>|MIN_EXPR <_\[0-9\]+, x_\[0-9\]+.D.>" "optimized" } } */
+
+int min_not(int c, int d)
+{
+ return ~min(~c, d); // max (c, ~d)
+}
+/* { dg-final { scan-tree-dump "~d_\[0-9\]+.D.;" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "~c_\[0-9\]+.D.;" "optimized" } } */
+/* { dg-final { scan-tree-dump "MAX_EXPR <c_\[0-9\]+.D., _\[0-9\]+>|MIN_EXPR <_\[0-9\]+, c_\[0-9\]+.D.>" "optimized" } } */
+
+/* { dg-final { scan-tree-dump-times "~" 2 "optimized" } } */