diff mbox

[PR71078] x / abs(x) -> copysign (1.0, x)

Message ID CAAgBjMmB5XPJQRJyH5CF9dvKnS3WiKSQ5KvVTnp10GEL0CprNQ@mail.gmail.com
State New
Headers show

Commit Message

Prathamesh Kulkarni July 27, 2016, 10:47 a.m. UTC
On 26 July 2016 at 17:41, Richard Biener <rguenther@suse.de> wrote:
> On Mon, 25 Jul 2016, Prathamesh Kulkarni wrote:

>

>> Hi,

>> The attached patch tries to fix PR71078.

>> I am not sure if I have got the converts right.

>> I put (convert? @0) and (convert1? (abs @1))

>> to match for cases when operands's types may

>> be different from outermost type like in pr71078-3.c

>

> Types of RDIV_EXPR have to be the same so as you have a

> match on @0 the converts need to be either both present

> or not present.

>

> +  (if (FLOAT_TYPE_P (type)

>

> as you special-case several types below please use SCALAR_FLOAT_TYPE_P

> here.

>

> +       && ! HONOR_NANS (type)

> +       && ! HONOR_INFINITIES (type))

> +   (switch

> +    (if (type == float_type_node)

> +     (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))

>

> please use if (types_match (type, float_type_node)) instead of

> pointer equality.  I _think_ you can do better here by using

> IFN_COPYSIGN but possibly only so if the target supports it.

> Richard - this seems to be the first pattern in need of

> generating a builtin where no other was there to match the type

> to - any idea how we can safely use the internal function here?

> I see those do not have an expander that would fall back to

> expanding the regular builtin, correct?

>

> Please place the pattern next to

>

> /* Optimize -A / A to -1.0 if we don't care about

>    NaNs or Infinities.  */

> (simplify

>  (rdiv:C @0 (negate @0))

>  (if (FLOAT_TYPE_P (type)

>       && ! HONOR_NANS (type)

>       && ! HONOR_INFINITIES (type))

>   { build_minus_one_cst (type); }))

>

> where it logically belongs.

Hi,
Is this  version OK ?
Bootstrap + test in progress on x86_64-unknown-linux-gnu.

Thanks,
Prathamesh
>

> Thanks,

> Richard.

>

>> test-case (included in patch).

>> Bootstrap+test in progress on x86_64-unknown-linux-gnu.

>>

>> Thanks,

>> Prathamesh

>>

>

> --

> Richard Biener <rguenther@suse.de>

> SUSE LINUX GmbH, GF: Felix Imendoerffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nuernberg)
2016-07-27  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

	PR middle-end/71078
	* match.pd (x / abs(x) -> copysign(1.0, x)): New pattern.

testsuite/
	* gcc.dg/tree-ssa/pr71078-1.c: New test-case.
	* gcc.dg/tree-ssa/pr71078-2.c: Likewise.
	* gcc.dg/tree-ssa/pr71078-3.c: Likewise.
diff mbox

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index 21bf617..2fd898a 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -195,6 +195,20 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       && ! HONOR_INFINITIES (type))
   { build_minus_one_cst (type); }))
 
+/* PR71078: x / abs(x) -> copysign (1.0, x) */
+(simplify
+ (rdiv:C (convert? @0) (convert? (abs @0)))
+  (if (SCALAR_FLOAT_TYPE_P (type)
+       && ! HONOR_NANS (type)
+       && ! HONOR_INFINITIES (type))
+   (switch
+    (if (types_match (type, float_type_node))
+     (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
+    (if (types_match (type, double_type_node))
+     (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
+    (if (types_match (type, long_double_type_node))
+     (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
+
 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
 (simplify
  (rdiv @0 real_onep)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71078-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71078-1.c
new file mode 100644
index 0000000..6204c14
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71078-1.c
@@ -0,0 +1,29 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -ffast-math -fdump-tree-forwprop-details" } */
+
+#include <math.h>
+
+float f1(float x)
+{
+  float t1 = fabsf (x);
+  float t2 = x / t1;
+  return t2;
+}
+ 
+double f2(double x)
+{
+  double t1 = fabs (x);
+  double t2 = x / t1;
+  return t2;
+}
+
+long double f3 (long double x)
+{
+  long double t1 = fabsl (x);
+  long double t2 = x / t1;
+  return t2;
+}
+
+/* { dg-final { scan-tree-dump "__builtin_copysignf" "forwprop1" } } */
+/* { dg-final { scan-tree-dump "__builtin_copysign" "forwprop1" } } */
+/* { dg-final { scan-tree-dump "__builtin_copysignl" "forwprop1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71078-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71078-2.c
new file mode 100644
index 0000000..96485af
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71078-2.c
@@ -0,0 +1,29 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -ffast-math -fdump-tree-forwprop-details" } */
+
+#include <math.h>
+
+float f1(float x)
+{
+  float t1 = fabsf (x);
+  float t2 = t1 / x; 
+  return t2;
+}
+ 
+double f2(double x)
+{
+  double t1 = fabs (x);
+  double t2 = t1 / x; 
+  return t2;
+}
+
+long double f3 (long double x)
+{
+  long double t1 = fabsl (x);
+  long double t2 = t1 / x; 
+  return t2;
+}
+
+/* { dg-final { scan-tree-dump "__builtin_copysignf" "forwprop1" } } */
+/* { dg-final { scan-tree-dump "__builtin_copysign" "forwprop1" } } */
+/* { dg-final { scan-tree-dump "__builtin_copysignl" "forwprop1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71078-3.c b/gcc/testsuite/gcc.dg/tree-ssa/pr71078-3.c
new file mode 100644
index 0000000..8780b6a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71078-3.c
@@ -0,0 +1,12 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -ffast-math -fdump-tree-forwprop-details" } */
+
+#include <math.h>
+double f(float f)
+{
+  double t1 = fabs(f); 
+  double t2 = f / t1;
+  return t2;
+}
+
+/* { dg-final { scan-tree-dump "__builtin_copysign" "forwprop1" } } */