aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorAndrew Pinski <apinski@marvell.com>2023-08-30 21:21:01 -0700
committerAndrew Pinski <apinski@marvell.com>2023-08-31 23:57:24 -0700
commit3d86e7f4a8aef1b864a51660825597eafe9059b1 (patch)
tree4d16d3d84d91b1ff6d7e1e77bf124c1be5337a76 /gcc
parent1967f21d000e09d3d3190317af7923b578ce02b1 (diff)
downloadgcc-3d86e7f4a8aef1b864a51660825597eafe9059b1.zip
gcc-3d86e7f4a8aef1b864a51660825597eafe9059b1.tar.gz
gcc-3d86e7f4a8aef1b864a51660825597eafe9059b1.tar.bz2
MATCH [PR19832]: Optimize some `(a != b) ? a OP b : c`
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.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/match.pd31
-rw-r--r--gcc/testsuite/g++.dg/opt/vectcond-1.C57
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/phi-opt-same-1.c60
3 files changed, 148 insertions, 0 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index c01362e..487a7e3 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 0000000..3877ad1
--- /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 0000000..24e757b
--- /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" } } */