public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch match.pd] Add a simplify rule for x * copysign (1.0, y);
@ 2015-10-01 13:57 James Greenhalgh
  2015-10-01 14:28 ` pinskia
  2015-10-01 18:36 ` Jakub Jelinek
  0 siblings, 2 replies; 20+ messages in thread
From: James Greenhalgh @ 2015-10-01 13:57 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1233 bytes --]


Hi,

If it is cheap enough to treat a floating-point value as an integer and
to do bitwise arithmetic on it (as it is for AArch64) we can rewrite:

  x * copysign (1.0, y)

as:

  x ^ (y & (1 << sign_bit_position))

This patch implements that rewriting rule in match.pd, and a testcase
expecting the transform.

This is worth about 6% in 481.wrf for AArch64. I don't don't know enough
about the x86 microarchitectures to know how productive this transformation
is there. In Spec2006FP I didn't see any interesting results in either
direction. Looking at code generation for the testcase I add, I think the
x86 code generation looks worse, but I can't understand why it doesn't use
a vector-side xor and load the mask vector-side. With that fixed up I think
the code generation would look better - though as I say, I'm not an expert
here...

Bootstrapped on both aarch64-none-linux-gnu and x86_64 with no issues.

OK for trunk?

Thanks,
James

---
gcc/

2015-10-01  James Greenhalgh  <james.greenhalgh@arm.com>

	* match.pd (mult (COPYSIGN:s real_onep @0) @1): New simplifier.

gcc/testsuite/

2015-10-01  James Greenhalgh  <james.greenhalgh@arm.com>

	* gcc.dg/tree-ssa/copysign.c: New.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Patch-match.pd-Add-a-simplify-rule-for-x-copysign-1..patch --]
[-- Type: text/x-patch;  name=0001-Patch-match.pd-Add-a-simplify-rule-for-x-copysign-1..patch, Size: 1956 bytes --]

diff --git a/gcc/match.pd b/gcc/match.pd
index bd5c267..d51ad2e 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -61,6 +61,7 @@ along with GCC; see the file COPYING3.  If not see
 (define_operator_list TAN BUILT_IN_TANF BUILT_IN_TAN BUILT_IN_TANL)
 (define_operator_list COSH BUILT_IN_COSHF BUILT_IN_COSH BUILT_IN_COSHL)
 (define_operator_list CEXPI BUILT_IN_CEXPIF BUILT_IN_CEXPI BUILT_IN_CEXPIL)
+(define_operator_list COPYSIGN BUILT_IN_COPYSIGNF BUILT_IN_COPYSIGN BUILT_IN_COPYSIGNL)
 
 /* Simplifications of operations with one constant operand and
    simplifications to constants or single values.  */
@@ -2079,6 +2080,21 @@ along with GCC; see the file COPYING3.  If not see
 
 /* Simplification of math builtins.  */
 
+/* Simplify x * copysign (1.0, y) -> x ^ (y & (1 << sign_bit_position)).  */
+(simplify
+  (mult:c (COPYSIGN:s real_onep @0) @1)
+  (with
+    {
+      wide_int m = wi::min_value (TYPE_PRECISION (type), SIGNED);
+      tree tt
+	= build_nonstandard_integer_type (TYPE_PRECISION (type),
+					  false);
+      tree mask = wide_int_to_tree (tt, m);
+    }
+    (view_convert (bit_xor (view_convert:tt @1)
+			   (bit_and (view_convert:tt @0)
+				     { mask; })))))
+
 /* fold_builtin_logarithm */
 (if (flag_unsafe_math_optimizations)
 
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/copysign.c b/gcc/testsuite/gcc.dg/tree-ssa/copysign.c
new file mode 100644
index 0000000..b67f3c1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/copysign.c
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-gimple" } */
+
+double
+foo_d (double x, double y)
+{
+  return x * __builtin_copysign (1.0, y);
+}
+
+float
+foo_f (float x, float y)
+{
+  return x * __builtin_copysignf (1.0f, y);
+}
+
+long double
+foo_l (long double x, long double y)
+{
+  return x * __builtin_copysignl (1.0, y);
+}
+
+/* { dg-final { scan-tree-dump-not "copysign" "gimple"} } */

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2015-10-02 10:32 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-01 13:57 [Patch match.pd] Add a simplify rule for x * copysign (1.0, y); James Greenhalgh
2015-10-01 14:28 ` pinskia
2015-10-01 14:51   ` James Greenhalgh
2015-10-01 15:03     ` pinskia
2015-10-01 15:43     ` Michael Matz
2015-10-01 15:49       ` Jakub Jelinek
2015-10-01 16:09         ` Michael Matz
2015-10-01 15:59       ` Joseph Myers
2015-10-01 16:15         ` Michael Matz
2015-10-01 18:36 ` Jakub Jelinek
2015-10-02  8:18   ` Richard Biener
2015-10-02  9:04     ` Jakub Jelinek
2015-10-02  9:11       ` Richard Biener
2015-10-02  9:27         ` Jakub Jelinek
2015-10-02  9:45           ` Richard Biener
2015-10-02  9:58             ` Jakub Jelinek
2015-10-02 10:07               ` Marek Polacek
2015-10-02 10:24                 ` James Greenhalgh
2015-10-02 10:29                   ` Marek Polacek
2015-10-02 10:32                   ` Richard Biener

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).