public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Move remaining flag_unsafe_math_optimizations using simplify and match
@ 2015-08-25  3:48 Hurugalawadi, Naveen
  2015-08-25 13:12 ` Richard Biener
  2015-08-27 22:42 ` Andreas Schwab
  0 siblings, 2 replies; 5+ messages in thread
From: Hurugalawadi, Naveen @ 2015-08-25  3:48 UTC (permalink / raw)
  To: gcc-patches; +Cc: Richard Biener

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

Hi,

Please find attached the remaining part of patch.

Tested the patch on AArch64 and X86 without any regressions.

Please review the patch and let me know if any modifications are required.

Thanks,
Naveen

ChangeLog

2015-08-25  Naveen H.S  <Naveen.Hurugalawadi@caviumnetworks.com>

	* fold-const.c (fold_binary_loc) : Move Optimize 
	root(x)*root(y)	as root(x*y) to match.pd.
	Move Optimize expN(x)*expN(y) as expN(x+y) to match.pd.
	Move Optimize pow(x,y)*pow(x,z) as pow(x,y+z) to match.pd.
	Move Optimize a/root(b/c) into a*root(c/b) to match.pd.
	Move Optimize x/expN(y) into x*expN(-y) to match.pd.

	* match.pd (mult (root:s @0) (root:s @1)): New simplifier.
	(mult (POW:s @0 @1) (POW:s @0 @2)) : New simplifier.
	(mult (exps:s @0) (exps:s @1)) : New simplifier.
	(rdiv @0 (root:s (rdiv:s @1 @2))) : New simplifier.
	(rdiv @0 (exps:s @1)) : New simplifier.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: simplify-2.patch --]
[-- Type: text/x-patch; name="simplify-2.patch", Size: 4716 bytes --]

diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 1e01726..c826e67 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -9947,51 +9947,6 @@ fold_binary_loc (location_t loc,
 
 	  if (flag_unsafe_math_optimizations)
 	    {
-	      enum built_in_function fcode0 = builtin_mathfn_code (arg0);
-	      enum built_in_function fcode1 = builtin_mathfn_code (arg1);
-
-	      /* Optimizations of root(...)*root(...).  */
-	      if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
-		{
-		  tree rootfn, arg;
-		  tree arg00 = CALL_EXPR_ARG (arg0, 0);
-		  tree arg10 = CALL_EXPR_ARG (arg1, 0);
-
-	          /* Optimize root(x)*root(y) as root(x*y).  */
-		  rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
-		  arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
-		  return build_call_expr_loc (loc, rootfn, 1, arg);
-		}
-
-	      /* Optimize expN(x)*expN(y) as expN(x+y).  */
-	      if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
-		{
-		  tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
-		  tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
-					  CALL_EXPR_ARG (arg0, 0),
-					  CALL_EXPR_ARG (arg1, 0));
-		  return build_call_expr_loc (loc, expfn, 1, arg);
-		}
-
-	      /* Optimizations of pow(...)*pow(...).  */
-	      if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
-		  || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
-		  || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
-		{
-		  tree arg00 = CALL_EXPR_ARG (arg0, 0);
-		  tree arg01 = CALL_EXPR_ARG (arg0, 1);
-		  tree arg10 = CALL_EXPR_ARG (arg1, 0);
-		  tree arg11 = CALL_EXPR_ARG (arg1, 1);
-
-		  /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z).  */
-		  if (operand_equal_p (arg00, arg10, 0))
-		    {
-		      tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
-		      tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
-					      arg01, arg11);
-		      return build_call_expr_loc (loc, powfn, 2, arg00, arg);
-		    }
-		}
 
 	      /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x.  */
 	      if (!in_gimple_form
@@ -10403,40 +10358,6 @@ fold_binary_loc (location_t loc,
 				TREE_OPERAND (arg1, 0));
 	}
 
-      if (flag_unsafe_math_optimizations)
-	{
-	  enum built_in_function fcode1 = builtin_mathfn_code (arg1);
-
-	  /* Optimize a/root(b/c) into a*root(c/b).  */
-	  if (BUILTIN_CBRT_P (fcode1))
-	    {
-	      tree rootarg = CALL_EXPR_ARG (arg1, 0);
-
-	      if (TREE_CODE (rootarg) == RDIV_EXPR)
-		{
-		  tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
-		  tree b = TREE_OPERAND (rootarg, 0);
-		  tree c = TREE_OPERAND (rootarg, 1);
-
-		  tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
-
-		  tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
-		  return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
-		}
-	    }
-
-	  /* Optimize x/expN(y) into x*expN(-y).  */
-	  if (BUILTIN_EXPONENT_P (fcode1))
-	    {
-	      tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
-	      tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
-	      arg1 = build_call_expr_loc (loc,
-				      expfn, 1,
-				      fold_convert_loc (loc, type, arg));
-	      return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
-	    }
-
-	}
       return NULL_TREE;
 
     case TRUNC_DIV_EXPR:

diff --git a/gcc/match.pd b/gcc/match.pd
index eb0ba9d..289bc5c 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2014,11 +2014,28 @@ along with GCC; see the file COPYING3.  If not see
   (if (!HONOR_SNANS (type))
    @0))
 
+ /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
+ (for root (SQRT CBRT)
+  (simplify
+   (mult (root:s @0) (root:s @1))
+    (root (mult @0 @1))))
+
+ /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
+ (simplify
+  (mult (POW:s @0 @1) (POW:s @0 @2))
+   (POW @0 (plus @1 @2)))
+
  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
  (simplify
   (mult (POW:s @0 @1) (POW:s @2 @1))
    (POW (mult @0 @2) @1))
 
+ /* Simplify expN(x) * expN(y) -> expN(x+y). */
+ (for exps (EXP EXP2 EXP10 POW10)
+  (simplify
+   (mult (exps:s @0) (exps:s @1))
+    (exps (plus @0 @1))))
+
  /* Simplify tan(x) * cos(x) -> sin(x). */
  (simplify
   (mult:c (TAN:s @0) (COS:s @0))
@@ -2061,9 +2078,16 @@ along with GCC; see the file COPYING3.  If not see
    (POW @0 (minus @1 { build_one_cst (type); }))))
 
  /* Simplify a/root(b/c) into a*root(c/b).  */
- (simplify
-  (rdiv @0 (SQRT:s (rdiv:s @1 @2)))
-   (mult @0 (SQRT (rdiv @2 @1))))
+ (for root (SQRT CBRT)
+  (simplify
+   (rdiv @0 (root:s (rdiv:s @1 @2)))
+    (mult @0 (root (rdiv @2 @1)))))
+
+ /* Simplify x/expN(y) into x*expN(-y).  */
+ (for exps (EXP EXP2 EXP10 POW10)
+  (simplify
+   (rdiv @0 (exps:s @1))
+    (mult @0 (exps (negate @1)))))
 
  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
  (simplify

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

* Re: Move remaining flag_unsafe_math_optimizations using simplify and match
  2015-08-25  3:48 Move remaining flag_unsafe_math_optimizations using simplify and match Hurugalawadi, Naveen
@ 2015-08-25 13:12 ` Richard Biener
  2015-08-27 22:42 ` Andreas Schwab
  1 sibling, 0 replies; 5+ messages in thread
From: Richard Biener @ 2015-08-25 13:12 UTC (permalink / raw)
  To: Hurugalawadi, Naveen; +Cc: gcc-patches

On Tue, Aug 25, 2015 at 5:29 AM, Hurugalawadi, Naveen
<Naveen.Hurugalawadi@caviumnetworks.com> wrote:
> Hi,
>
> Please find attached the remaining part of patch.
>
> Tested the patch on AArch64 and X86 without any regressions.
>
> Please review the patch and let me know if any modifications are required.

Ok.

Thanks,
Richard.

> Thanks,
> Naveen
>
> ChangeLog
>
> 2015-08-25  Naveen H.S  <Naveen.Hurugalawadi@caviumnetworks.com>
>
>         * fold-const.c (fold_binary_loc) : Move Optimize
>         root(x)*root(y) as root(x*y) to match.pd.
>         Move Optimize expN(x)*expN(y) as expN(x+y) to match.pd.
>         Move Optimize pow(x,y)*pow(x,z) as pow(x,y+z) to match.pd.
>         Move Optimize a/root(b/c) into a*root(c/b) to match.pd.
>         Move Optimize x/expN(y) into x*expN(-y) to match.pd.
>
>         * match.pd (mult (root:s @0) (root:s @1)): New simplifier.
>         (mult (POW:s @0 @1) (POW:s @0 @2)) : New simplifier.
>         (mult (exps:s @0) (exps:s @1)) : New simplifier.
>         (rdiv @0 (root:s (rdiv:s @1 @2))) : New simplifier.
>         (rdiv @0 (exps:s @1)) : New simplifier.

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

* Re: Move remaining flag_unsafe_math_optimizations using simplify and match
  2015-08-25  3:48 Move remaining flag_unsafe_math_optimizations using simplify and match Hurugalawadi, Naveen
  2015-08-25 13:12 ` Richard Biener
@ 2015-08-27 22:42 ` Andreas Schwab
  2015-08-28  7:15   ` Marc Glisse
  1 sibling, 1 reply; 5+ messages in thread
From: Andreas Schwab @ 2015-08-27 22:42 UTC (permalink / raw)
  To: Hurugalawadi, Naveen; +Cc: gcc-patches, Richard Biener

"Hurugalawadi, Naveen" <Naveen.Hurugalawadi@caviumnetworks.com> writes:

> 	* fold-const.c (fold_binary_loc) : Move Optimize 
> 	root(x)*root(y)	as root(x*y) to match.pd.
> 	Move Optimize expN(x)*expN(y) as expN(x+y) to match.pd.
> 	Move Optimize pow(x,y)*pow(x,z) as pow(x,y+z) to match.pd.
> 	Move Optimize a/root(b/c) into a*root(c/b) to match.pd.
> 	Move Optimize x/expN(y) into x*expN(-y) to match.pd.
>
> 	* match.pd (mult (root:s @0) (root:s @1)): New simplifier.
> 	(mult (POW:s @0 @1) (POW:s @0 @2)) : New simplifier.
> 	(mult (exps:s @0) (exps:s @1)) : New simplifier.
> 	(rdiv @0 (root:s (rdiv:s @1 @2))) : New simplifier.
> 	(rdiv @0 (exps:s @1)) : New simplifier.

FAIL: gcc.dg/builtins-11.c (test for excess errors)
Excess errors:
builtins-11.c:(.text+0x52): undefined reference to `link_error'

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: Move remaining flag_unsafe_math_optimizations using simplify and match
  2015-08-27 22:42 ` Andreas Schwab
@ 2015-08-28  7:15   ` Marc Glisse
  2015-08-28 12:00     ` Richard Biener
  0 siblings, 1 reply; 5+ messages in thread
From: Marc Glisse @ 2015-08-28  7:15 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Hurugalawadi, Naveen, gcc-patches, Richard Biener

On Thu, 27 Aug 2015, Andreas Schwab wrote:

> "Hurugalawadi, Naveen" <Naveen.Hurugalawadi@caviumnetworks.com> writes:
>
>> 	* fold-const.c (fold_binary_loc) : Move Optimize
>> 	root(x)*root(y)	as root(x*y) to match.pd.
>> 	Move Optimize expN(x)*expN(y) as expN(x+y) to match.pd.
>> 	Move Optimize pow(x,y)*pow(x,z) as pow(x,y+z) to match.pd.
>> 	Move Optimize a/root(b/c) into a*root(c/b) to match.pd.
>> 	Move Optimize x/expN(y) into x*expN(-y) to match.pd.
>>
>> 	* match.pd (mult (root:s @0) (root:s @1)): New simplifier.
>> 	(mult (POW:s @0 @1) (POW:s @0 @2)) : New simplifier.
>> 	(mult (exps:s @0) (exps:s @1)) : New simplifier.
>> 	(rdiv @0 (root:s (rdiv:s @1 @2))) : New simplifier.
>> 	(rdiv @0 (exps:s @1)) : New simplifier.
>
> FAIL: gcc.dg/builtins-11.c (test for excess errors)
> Excess errors:
> builtins-11.c:(.text+0x52): undefined reference to `link_error'

Indeed, generic-match ends up testing for sqrt(x)*sqrt(y) before testing 
for sqrt(x)@1*@1, so it simplifies it to sqrt(x*x)->abs(x) instead of 
plain x. Changing the genmatch machinery to better respect the order of 
patterns in match.pd might not be trivial without sacrificing performance, 
a workaround might be to add a special pattern sqrt(x)*sqrt(x), or to 
disable some patterns for GENERIC so CSE has a chance to run first.

-- 
Marc Glisse

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

* Re: Move remaining flag_unsafe_math_optimizations using simplify and match
  2015-08-28  7:15   ` Marc Glisse
@ 2015-08-28 12:00     ` Richard Biener
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Biener @ 2015-08-28 12:00 UTC (permalink / raw)
  To: GCC Patches; +Cc: Andreas Schwab, Hurugalawadi, Naveen

On Fri, Aug 28, 2015 at 7:33 AM, Marc Glisse <marc.glisse@inria.fr> wrote:
> On Thu, 27 Aug 2015, Andreas Schwab wrote:
>
>> "Hurugalawadi, Naveen" <Naveen.Hurugalawadi@caviumnetworks.com> writes:
>>
>>>         * fold-const.c (fold_binary_loc) : Move Optimize
>>>         root(x)*root(y) as root(x*y) to match.pd.
>>>         Move Optimize expN(x)*expN(y) as expN(x+y) to match.pd.
>>>         Move Optimize pow(x,y)*pow(x,z) as pow(x,y+z) to match.pd.
>>>         Move Optimize a/root(b/c) into a*root(c/b) to match.pd.
>>>         Move Optimize x/expN(y) into x*expN(-y) to match.pd.
>>>
>>>         * match.pd (mult (root:s @0) (root:s @1)): New simplifier.
>>>         (mult (POW:s @0 @1) (POW:s @0 @2)) : New simplifier.
>>>         (mult (exps:s @0) (exps:s @1)) : New simplifier.
>>>         (rdiv @0 (root:s (rdiv:s @1 @2))) : New simplifier.
>>>         (rdiv @0 (exps:s @1)) : New simplifier.
>>
>>
>> FAIL: gcc.dg/builtins-11.c (test for excess errors)
>> Excess errors:
>> builtins-11.c:(.text+0x52): undefined reference to `link_error'
>
>
> Indeed, generic-match ends up testing for sqrt(x)*sqrt(y) before testing for
> sqrt(x)@1*@1, so it simplifies it to sqrt(x*x)->abs(x) instead of plain x.
> Changing the genmatch machinery to better respect the order of patterns in
> match.pd might not be trivial without sacrificing performance, a workaround
> might be to add a special pattern sqrt(x)*sqrt(x), or to disable some
> patterns for GENERIC so CSE has a chance to run first.

Hum, it _does_ respect ordering (well, it is supposed to).  But indeed I can
see it does not.  Bah.

Mind opening a bugreport for this?

Thanks,
Richard.

> --
> Marc Glisse

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

end of thread, other threads:[~2015-08-28 11:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-25  3:48 Move remaining flag_unsafe_math_optimizations using simplify and match Hurugalawadi, Naveen
2015-08-25 13:12 ` Richard Biener
2015-08-27 22:42 ` Andreas Schwab
2015-08-28  7:15   ` Marc Glisse
2015-08-28 12:00     ` 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).