* [PATCH] MATCH [PR19832]: Optimize some `(a != b) ? a OP b : c`
@ 2023-08-31 17:24 Andrew Pinski
2023-09-01 6:36 ` Richard Biener
2023-09-01 12:27 ` [committed] testsuite: Fix vectcond-1.C FAIL on i686-linux [PR19832] Jakub Jelinek
0 siblings, 2 replies; 3+ messages in thread
From: Andrew Pinski @ 2023-08-31 17:24 UTC (permalink / raw)
To: gcc-patches; +Cc: Andrew Pinski
This patch adds the following match patterns to optimize these:
/* (a != b) ? (a - b) : 0 -> (a - b) */
/* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
/* (a != b) ? (a & b) : a -> (a & b) */
/* (a != b) ? (a | b) : a -> (a | b) */
/* (a != b) ? min(a,b) : a -> min(a,b) */
/* (a != b) ? max(a,b) : a -> max(a,b) */
/* (a != b) ? (a * b) : (a * a) -> (a * b) */
/* (a != b) ? (a + b) : (a + a) -> (a + b) */
/* (a != b) ? (a + b) : (2 * a) -> (a + b) */
Note currently only integer types (include vector types)
are handled. Floating point types can be added later on.
OK? Bootstrapped and tested on x86_64-linux-gnu.
The first pattern had still shows up in GCC in cse.c's preferable
function which was the original motivation for this patch.
PR tree-optimization/19832
gcc/ChangeLog:
* match.pd: Add pattern to optimize
`(a != b) ? a OP b : c`.
gcc/testsuite/ChangeLog:
* g++.dg/opt/vectcond-1.C: New test.
* gcc.dg/tree-ssa/phi-opt-same-1.c: New test.
---
gcc/match.pd | 31 ++++++++++
gcc/testsuite/g++.dg/opt/vectcond-1.C | 57 ++++++++++++++++++
.../gcc.dg/tree-ssa/phi-opt-same-1.c | 60 +++++++++++++++++++
3 files changed, 148 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/opt/vectcond-1.C
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c
diff --git a/gcc/match.pd b/gcc/match.pd
index c01362ee359..487a7e38719 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5261,6 +5261,37 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(convert @c0))))))))
#endif
+(for cnd (cond vec_cond)
+ /* (a != b) ? (a - b) : 0 -> (a - b) */
+ (simplify
+ (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
+ @2)
+ /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
+ (simplify
+ (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
+ @2)
+ /* (a != b) ? (a & b) : a -> (a & b) */
+ /* (a != b) ? (a | b) : a -> (a | b) */
+ /* (a != b) ? min(a,b) : a -> min(a,b) */
+ /* (a != b) ? max(a,b) : a -> max(a,b) */
+ (for op (bit_and bit_ior min max)
+ (simplify
+ (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
+ @2))
+ /* (a != b) ? (a * b) : (a * a) -> (a * b) */
+ /* (a != b) ? (a + b) : (a + a) -> (a + b) */
+ (for op (mult plus)
+ (simplify
+ (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
+ (if (ANY_INTEGRAL_TYPE_P (type))
+ @2)))
+ /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
+ (simplify
+ (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
+ (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
+ @2))
+)
+
/* These was part of minmax phiopt. */
/* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
to minmax<min/max<a, b>, c> */
diff --git a/gcc/testsuite/g++.dg/opt/vectcond-1.C b/gcc/testsuite/g++.dg/opt/vectcond-1.C
new file mode 100644
index 00000000000..3877ad11414
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/vectcond-1.C
@@ -0,0 +1,57 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-ccp1 -fdump-tree-optimized" } */
+/* This is the vector version of these optimizations. */
+/* PR tree-optimization/19832 */
+
+#define vector __attribute__((vector_size(sizeof(unsigned)*2)))
+
+static inline vector int max_(vector int a, vector int b)
+{
+ return (a > b)? a : b;
+}
+static inline vector int min_(vector int a, vector int b)
+{
+ return (a < b) ? a : b;
+}
+
+vector int f_minus(vector int a, vector int b)
+{
+ return (a != b) ? a - b : (a - a);
+}
+vector int f_xor(vector int a, vector int b)
+{
+ return (a != b) ? a ^ b : (a ^ a);
+}
+
+vector int f_ior(vector int a, vector int b)
+{
+ return (a != b) ? a | b : (a | a);
+}
+vector int f_and(vector int a, vector int b)
+{
+ return (a != b) ? a & b : (a & a);
+}
+vector int f_max(vector int a, vector int b)
+{
+ return (a != b) ? max_(a, b) : max_(a, a);
+}
+vector int f_min(vector int a, vector int b)
+{
+ return (a != b) ? min_(a, b) : min_(a, a);
+}
+vector int f_mult(vector int a, vector int b)
+{
+ return (a != b) ? a * b : (a * a);
+}
+vector int f_plus(vector int a, vector int b)
+{
+ return (a != b) ? a + b : (a + a);
+}
+vector int f_plus_alt(vector int a, vector int b)
+{
+ return (a != b) ? a + b : (a * 2);
+}
+
+/* All of the above function's VEC_COND_EXPR should have been optimized away. */
+/* { dg-final { scan-tree-dump-not "VEC_COND_EXPR " "ccp1" } } */
+/* { dg-final { scan-tree-dump-not "VEC_COND_EXPR " "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c
new file mode 100644
index 00000000000..24e757b9b9f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c
@@ -0,0 +1,60 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-phiopt1 -fdump-tree-optimized" } */
+/* PR tree-optimization/19832 */
+
+static inline int max_(int a, int b)
+{
+ if (a > b) return a;
+ return b;
+}
+static inline int min_(int a, int b)
+{
+ if (a < b) return a;
+ return b;
+}
+
+int f_minus(int a, int b)
+{
+ if (a != b) return a - b;
+ return a - a;
+}
+int f_xor(int a, int b)
+{
+ if (a != b) return a ^ b;
+ return a ^ a;
+}
+
+int f_ior(int a, int b)
+{
+ if (a != b) return a | b;
+ return a | a;
+}
+int f_and(int a, int b)
+{
+ if (a != b) return a & b;
+ return a & a;
+}
+int f_max(int a, int b)
+{
+ if (a != b) return max_(a,b);
+ return max_(a,a);
+}
+int f_min(int a, int b)
+{
+ if (a != b) return min_(a,b);
+ return min_(a,a);
+}
+int f_mult(int a, int b)
+{
+ if (a != b) return a * b;
+ return a * a;
+}
+int f_plus(int a, int b)
+{
+ if (a != b) return a + b;
+ return a + a;
+}
+
+/* All of the above function's if should have been optimized away even in phiopt1. */
+/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not "if " "optimized" } } */
--
2.31.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] MATCH [PR19832]: Optimize some `(a != b) ? a OP b : c`
2023-08-31 17:24 [PATCH] MATCH [PR19832]: Optimize some `(a != b) ? a OP b : c` Andrew Pinski
@ 2023-09-01 6:36 ` Richard Biener
2023-09-01 12:27 ` [committed] testsuite: Fix vectcond-1.C FAIL on i686-linux [PR19832] Jakub Jelinek
1 sibling, 0 replies; 3+ messages in thread
From: Richard Biener @ 2023-09-01 6:36 UTC (permalink / raw)
To: Andrew Pinski; +Cc: gcc-patches
On Thu, Aug 31, 2023 at 7:25 PM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> This patch adds the following match patterns to optimize these:
> /* (a != b) ? (a - b) : 0 -> (a - b) */
> /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
> /* (a != b) ? (a & b) : a -> (a & b) */
> /* (a != b) ? (a | b) : a -> (a | b) */
> /* (a != b) ? min(a,b) : a -> min(a,b) */
> /* (a != b) ? max(a,b) : a -> max(a,b) */
> /* (a != b) ? (a * b) : (a * a) -> (a * b) */
> /* (a != b) ? (a + b) : (a + a) -> (a + b) */
> /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
> Note currently only integer types (include vector types)
> are handled. Floating point types can be added later on.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu.
OK.
> The first pattern had still shows up in GCC in cse.c's preferable
> function which was the original motivation for this patch.
>
> PR tree-optimization/19832
>
> gcc/ChangeLog:
>
> * match.pd: Add pattern to optimize
> `(a != b) ? a OP b : c`.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/opt/vectcond-1.C: New test.
> * gcc.dg/tree-ssa/phi-opt-same-1.c: New test.
> ---
> gcc/match.pd | 31 ++++++++++
> gcc/testsuite/g++.dg/opt/vectcond-1.C | 57 ++++++++++++++++++
> .../gcc.dg/tree-ssa/phi-opt-same-1.c | 60 +++++++++++++++++++
> 3 files changed, 148 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/opt/vectcond-1.C
> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index c01362ee359..487a7e38719 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -5261,6 +5261,37 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> (convert @c0))))))))
> #endif
>
> +(for cnd (cond vec_cond)
> + /* (a != b) ? (a - b) : 0 -> (a - b) */
> + (simplify
> + (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
> + @2)
> + /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
> + (simplify
> + (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
> + @2)
> + /* (a != b) ? (a & b) : a -> (a & b) */
> + /* (a != b) ? (a | b) : a -> (a | b) */
> + /* (a != b) ? min(a,b) : a -> min(a,b) */
> + /* (a != b) ? max(a,b) : a -> max(a,b) */
> + (for op (bit_and bit_ior min max)
> + (simplify
> + (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
> + @2))
> + /* (a != b) ? (a * b) : (a * a) -> (a * b) */
> + /* (a != b) ? (a + b) : (a + a) -> (a + b) */
> + (for op (mult plus)
> + (simplify
> + (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
> + (if (ANY_INTEGRAL_TYPE_P (type))
> + @2)))
> + /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
> + (simplify
> + (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
> + (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
> + @2))
> +)
> +
> /* These was part of minmax phiopt. */
> /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
> to minmax<min/max<a, b>, c> */
> diff --git a/gcc/testsuite/g++.dg/opt/vectcond-1.C b/gcc/testsuite/g++.dg/opt/vectcond-1.C
> new file mode 100644
> index 00000000000..3877ad11414
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/opt/vectcond-1.C
> @@ -0,0 +1,57 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-ccp1 -fdump-tree-optimized" } */
> +/* This is the vector version of these optimizations. */
> +/* PR tree-optimization/19832 */
> +
> +#define vector __attribute__((vector_size(sizeof(unsigned)*2)))
> +
> +static inline vector int max_(vector int a, vector int b)
> +{
> + return (a > b)? a : b;
> +}
> +static inline vector int min_(vector int a, vector int b)
> +{
> + return (a < b) ? a : b;
> +}
> +
> +vector int f_minus(vector int a, vector int b)
> +{
> + return (a != b) ? a - b : (a - a);
> +}
> +vector int f_xor(vector int a, vector int b)
> +{
> + return (a != b) ? a ^ b : (a ^ a);
> +}
> +
> +vector int f_ior(vector int a, vector int b)
> +{
> + return (a != b) ? a | b : (a | a);
> +}
> +vector int f_and(vector int a, vector int b)
> +{
> + return (a != b) ? a & b : (a & a);
> +}
> +vector int f_max(vector int a, vector int b)
> +{
> + return (a != b) ? max_(a, b) : max_(a, a);
> +}
> +vector int f_min(vector int a, vector int b)
> +{
> + return (a != b) ? min_(a, b) : min_(a, a);
> +}
> +vector int f_mult(vector int a, vector int b)
> +{
> + return (a != b) ? a * b : (a * a);
> +}
> +vector int f_plus(vector int a, vector int b)
> +{
> + return (a != b) ? a + b : (a + a);
> +}
> +vector int f_plus_alt(vector int a, vector int b)
> +{
> + return (a != b) ? a + b : (a * 2);
> +}
> +
> +/* All of the above function's VEC_COND_EXPR should have been optimized away. */
> +/* { dg-final { scan-tree-dump-not "VEC_COND_EXPR " "ccp1" } } */
> +/* { dg-final { scan-tree-dump-not "VEC_COND_EXPR " "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c
> new file mode 100644
> index 00000000000..24e757b9b9f
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c
> @@ -0,0 +1,60 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-phiopt1 -fdump-tree-optimized" } */
> +/* PR tree-optimization/19832 */
> +
> +static inline int max_(int a, int b)
> +{
> + if (a > b) return a;
> + return b;
> +}
> +static inline int min_(int a, int b)
> +{
> + if (a < b) return a;
> + return b;
> +}
> +
> +int f_minus(int a, int b)
> +{
> + if (a != b) return a - b;
> + return a - a;
> +}
> +int f_xor(int a, int b)
> +{
> + if (a != b) return a ^ b;
> + return a ^ a;
> +}
> +
> +int f_ior(int a, int b)
> +{
> + if (a != b) return a | b;
> + return a | a;
> +}
> +int f_and(int a, int b)
> +{
> + if (a != b) return a & b;
> + return a & a;
> +}
> +int f_max(int a, int b)
> +{
> + if (a != b) return max_(a,b);
> + return max_(a,a);
> +}
> +int f_min(int a, int b)
> +{
> + if (a != b) return min_(a,b);
> + return min_(a,a);
> +}
> +int f_mult(int a, int b)
> +{
> + if (a != b) return a * b;
> + return a * a;
> +}
> +int f_plus(int a, int b)
> +{
> + if (a != b) return a + b;
> + return a + a;
> +}
> +
> +/* All of the above function's if should have been optimized away even in phiopt1. */
> +/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-not "if " "optimized" } } */
> --
> 2.31.1
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* [committed] testsuite: Fix vectcond-1.C FAIL on i686-linux [PR19832]
2023-08-31 17:24 [PATCH] MATCH [PR19832]: Optimize some `(a != b) ? a OP b : c` Andrew Pinski
2023-09-01 6:36 ` Richard Biener
@ 2023-09-01 12:27 ` Jakub Jelinek
1 sibling, 0 replies; 3+ messages in thread
From: Jakub Jelinek @ 2023-09-01 12:27 UTC (permalink / raw)
To: Andrew Pinski; +Cc: gcc-patches
On Thu, Aug 31, 2023 at 10:24:10AM -0700, Andrew Pinski via Gcc-patches wrote:
> This patch adds the following match patterns to optimize these:
> /* (a != b) ? (a - b) : 0 -> (a - b) */
These tests FAIL on i686-linux, with
.../gcc/testsuite/gcc.dg/pr110915-1.c:8:1: warning: MMX vector return without MMX enabled changes the ABI [-Wpsabi]
.../gcc/testsuite/gcc.dg/pr110915-1.c:7:15: warning: MMX vector argument without MMX enabled changes the ABI [-Wpsabi]
excess warnings. I've added -Wno-psabi to quiet that up, plus I think
it is undesirable to define macros like vector before including C library
headers in case the header would use that identifier in non-obfuscated
form somewhere.
Tested with
make check-g++ RUNTESTFLAGS='--target_board=unix\{-m32,-m32/-march=i686,-m64\} dg.exp=vectcond-1.C'
on x86_64-linux which previously FAILed, committed to trunk as obvious.
2023-09-01 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/19832
* g++.dg/opt/vectcond-1.C: Add -Wno-psabi to dg-options.
--- gcc/testsuite/g++.dg/opt/vectcond-1.C.jj 2023-09-01 12:15:36.072430927 +0200
+++ gcc/testsuite/g++.dg/opt/vectcond-1.C 2023-09-01 14:20:22.688739458 +0200
@@ -1,5 +1,5 @@
/* { dg-do compile } */
-/* { dg-options "-O2 -fdump-tree-ccp1 -fdump-tree-optimized" } */
+/* { dg-options "-O2 -fdump-tree-ccp1 -fdump-tree-optimized -Wno-psabi" } */
/* This is the vector version of these optimizations. */
/* PR tree-optimization/19832 */
Jakub
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-09-01 12:27 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-31 17:24 [PATCH] MATCH [PR19832]: Optimize some `(a != b) ? a OP b : c` Andrew Pinski
2023-09-01 6:36 ` Richard Biener
2023-09-01 12:27 ` [committed] testsuite: Fix vectcond-1.C FAIL on i686-linux [PR19832] Jakub Jelinek
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).