match.pd: Simplify copysign (x, -x) to -x [PR96715]
authorJakub Jelinek <jakub@redhat.com>
Tue, 25 Aug 2020 05:21:26 +0000 (07:21 +0200)
committerJakub Jelinek <jakub@redhat.com>
Tue, 25 Aug 2020 05:21:26 +0000 (07:21 +0200)
The following patch implements an optimization suggested in the PR,
copysign(x,-x) can be optimized into -x (even without -ffast-math,
should work fine even for signed zeros and infinities or nans).

2020-08-25  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/96715
* match.pd (copysign(x,-x) -> -x): New simplification.

* gcc.dg/tree-ssa/copy-sign-3.c: New test.

gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/copy-sign-3.c [new file with mode: 0644]

index 2cffcae3d8b85b6973a499452205724632474b62..6e45836e32be5be25c510c64f26e761a12664e64 100644 (file)
@@ -5293,6 +5293,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (COPYSIGN_ALL @0 @0)
  @0)
 
+(simplify
+ /* copysign(x,-x) -> -x.  */
+ (COPYSIGN_ALL @0 (negate@1 @0))
+ @1)
+
 (simplify
  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/copy-sign-3.c b/gcc/testsuite/gcc.dg/tree-ssa/copy-sign-3.c
new file mode 100644 (file)
index 0000000..f52e04d
--- /dev/null
@@ -0,0 +1,23 @@
+/* PR tree-optimization/96715 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-not "= __builtin_copysign" "optimized" } } */
+/* { dg-final { scan-tree-dump-times " = -x_\[0-9]*\\(D\\)" 3 "optimized" } } */
+
+float
+foo (float x)
+{
+  return __builtin_copysignf (x, -x);
+}
+
+double
+bar (double x)
+{
+  return __builtin_copysign (x, -x);
+}
+
+long double
+baz (long double x)
+{
+  return __builtin_copysignl (x, -x);
+}