Patchwork PR 42327: Add checks for sqrt optab being present before doing pow (x, 0.75) optimization

login
register
mail settings
Submitter Michael Meissner
Date Dec. 7, 2010, 5:38 p.m.
Message ID <20101207173857.GA14620@hungry-tiger.westford.ibm.com>
Download mbox | patch
Permalink /patch/74586/
State New
Headers show

Comments

Michael Meissner - Dec. 7, 2010, 5:38 p.m.
Jakub Jelinek and Richard Guenther asked that I put in checks for a port having
a sqrt instruction before optimizing pow (x, 0.75) into sqrt(sqrt(x)) *
sqrt(x).

This patch adds those checks.  I did a bootstrap and make check, and there were
no regressions.  Is this ok to apply to the tree?

[gcc]
2010-12-07  Michael Meissner  <meissner@linux.vnet.ibm.com>

	PR middle-end/42694
	* builtins.c (expand_builtin_pow_root): Don't optimize pow(x,y)
	where y is 0.25, 1./6., or 0.75 if the target does not have a sqrt
	instruction, but do optimize if y is 0.5 or 1./6. since that
	changes an expensive call into a cheaper one.

[gcc/testsuite]
2010-12-07  Michael Meissner  <meissner@linux.vnet.ibm.com>

	PR middle-end/42694
	* gcc.target/powerpc/ppc-pow.c: New file to make sure pow (x,
	0.75) is not optimized if the machine has no sqrt instruction.
Richard Guenther - Dec. 8, 2010, 9:45 a.m.
On Tue, Dec 7, 2010 at 6:38 PM, Michael Meissner
<meissner@linux.vnet.ibm.com> wrote:
> Jakub Jelinek and Richard Guenther asked that I put in checks for a port having
> a sqrt instruction before optimizing pow (x, 0.75) into sqrt(sqrt(x)) *
> sqrt(x).
>
> This patch adds those checks.  I did a bootstrap and make check, and there were
> no regressions.  Is this ok to apply to the tree?

Ok.

Thanks,
Richard.

> [gcc]
> 2010-12-07  Michael Meissner  <meissner@linux.vnet.ibm.com>
>
>        PR middle-end/42694
>        * builtins.c (expand_builtin_pow_root): Don't optimize pow(x,y)
>        where y is 0.25, 1./6., or 0.75 if the target does not have a sqrt
>        instruction, but do optimize if y is 0.5 or 1./6. since that
>        changes an expensive call into a cheaper one.
>
> [gcc/testsuite]
> 2010-12-07  Michael Meissner  <meissner@linux.vnet.ibm.com>
>
>        PR middle-end/42694
>        * gcc.target/powerpc/ppc-pow.c: New file to make sure pow (x,
>        0.75) is not optimized if the machine has no sqrt instruction.
>
> Index: gcc/builtins.c
> ===================================================================
> --- gcc/builtins.c      (revision 167551)
> +++ gcc/builtins.c      (working copy)
> @@ -3068,7 +3068,8 @@ expand_builtin_pow_root (location_t loc,
>          if (REAL_VALUES_EQUAL (c, dconsthalf))
>            op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
>
> -         else
> +         /* Don't do this optimization if we don't have a sqrt insn.  */
> +         else if (optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)
>            {
>              REAL_VALUE_TYPE dconst1_4 = dconst1;
>              REAL_VALUE_TYPE dconst3_4;
> @@ -3114,7 +3115,8 @@ expand_builtin_pow_root (location_t loc,
>            op = build_call_nofold_loc (loc, cbrtfn, 1, arg0);
>
>              /* Now try 1/6.  */
> -         else if (optimize_insn_for_speed_p ())
> +         else if (optimize_insn_for_speed_p ()
> +                  && optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)
>            {
>              REAL_VALUE_TYPE dconst1_6 = dconst1_3;
>              SET_REAL_EXP (&dconst1_6, REAL_EXP (&dconst1_6) - 1);
> Index: gcc/testsuite/gcc.target/powerpc/ppc-pow.c
> ===================================================================
> --- gcc/testsuite/gcc.target/powerpc/ppc-pow.c  (revision 0)
> +++ gcc/testsuite/gcc.target/powerpc/ppc-pow.c  (revision 0)
> @@ -0,0 +1,34 @@
> +/* { dg-do compile { target { { powerpc*-*-* } && { ! powerpc*-apple-darwin* } } } } */
> +/* { dg-options "-O2 -ffast-math -mcpu=power6" } */
> +/* { dg-final { scan-assembler-times "fsqrt" 3 } } */
> +/* { dg-final { scan-assembler-times "fmul" 1 } } */
> +/* { dg-final { scan-assembler-times "bl pow" 1 } } */
> +/* { dg-final { scan-assembler-times "bl sqrt" 1 } } */
> +
> +double
> +do_pow_0_75_default (double a)
> +{
> +  return __builtin_pow (a, 0.75);      /* should generate 2 fsqrts */
> +}
> +
> +double
> +do_pow_0_5_default (double a)
> +{
> +  return __builtin_pow (a, 0.5);       /* should generate fsqrt */
> +}
> +
> +#pragma GCC target "no-powerpc-gpopt,no-powerpc-gfxopt"
> +
> +double
> +do_pow_0_75_nosqrt (double a)
> +{
> +  return __builtin_pow (a, 0.75);      /* should call pow */
> +}
> +
> +double
> +do_pow_0_5_nosqrt (double a)
> +{
> +  return __builtin_pow (a, 0.5);       /* should call sqrt */
> +}
> +
> +#pragma GCC reset_options
>
> --
> Michael Meissner, IBM
> 5 Technology Place Drive, M/S 2757, Westford, MA 01886-3141, USA
> meissner@linux.vnet.ibm.com     fax +1 (978) 399-6899
>
Paolo Bonzini - Dec. 8, 2010, 10:39 a.m.
On 12/07/2010 06:38 PM, Michael Meissner wrote:
> 	* builtins.c (expand_builtin_pow_root): Don't optimize pow(x,y)
> 	where y is 0.25, 1./6., or 0.75 if the target does not have a sqrt
> 	instruction, but do optimize if y is 0.5 or 1./6. since that
                                                     ^^^^^

Did you mean 1./3. here?

> 	changes an expensive call into a cheaper one.

Paolo
Michael Meissner - Dec. 8, 2010, 11:57 a.m.
On Wed, Dec 08, 2010 at 11:39:32AM +0100, Paolo Bonzini wrote:
> On 12/07/2010 06:38 PM, Michael Meissner wrote:
> >	* builtins.c (expand_builtin_pow_root): Don't optimize pow(x,y)
> >	where y is 0.25, 1./6., or 0.75 if the target does not have a sqrt
> >	instruction, but do optimize if y is 0.5 or 1./6. since that
>                                                     ^^^^^
> 
> Did you mean 1./3. here?
> 
> >	changes an expensive call into a cheaper one.

Yes.

Patch

Index: gcc/builtins.c
===================================================================
--- gcc/builtins.c	(revision 167551)
+++ gcc/builtins.c	(working copy)
@@ -3068,7 +3068,8 @@  expand_builtin_pow_root (location_t loc,
 	  if (REAL_VALUES_EQUAL (c, dconsthalf))
 	    op = build_call_nofold_loc (loc, sqrtfn, 1, arg0);
 
-	  else
+	  /* Don't do this optimization if we don't have a sqrt insn.  */
+	  else if (optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)
 	    {
 	      REAL_VALUE_TYPE dconst1_4 = dconst1;
 	      REAL_VALUE_TYPE dconst3_4;
@@ -3114,7 +3115,8 @@  expand_builtin_pow_root (location_t loc,
 	    op = build_call_nofold_loc (loc, cbrtfn, 1, arg0);
 
 	      /* Now try 1/6.  */
-	  else if (optimize_insn_for_speed_p ())
+	  else if (optimize_insn_for_speed_p ()
+		   && optab_handler (sqrt_optab, mode) != CODE_FOR_nothing)
 	    {
 	      REAL_VALUE_TYPE dconst1_6 = dconst1_3;
 	      SET_REAL_EXP (&dconst1_6, REAL_EXP (&dconst1_6) - 1);
Index: gcc/testsuite/gcc.target/powerpc/ppc-pow.c
===================================================================
--- gcc/testsuite/gcc.target/powerpc/ppc-pow.c	(revision 0)
+++ gcc/testsuite/gcc.target/powerpc/ppc-pow.c	(revision 0)
@@ -0,0 +1,34 @@ 
+/* { dg-do compile { target { { powerpc*-*-* } && { ! powerpc*-apple-darwin* } } } } */
+/* { dg-options "-O2 -ffast-math -mcpu=power6" } */
+/* { dg-final { scan-assembler-times "fsqrt" 3 } } */
+/* { dg-final { scan-assembler-times "fmul" 1 } } */
+/* { dg-final { scan-assembler-times "bl pow" 1 } } */
+/* { dg-final { scan-assembler-times "bl sqrt" 1 } } */
+
+double
+do_pow_0_75_default (double a)
+{
+  return __builtin_pow (a, 0.75);	/* should generate 2 fsqrts */
+}
+
+double
+do_pow_0_5_default (double a)
+{
+  return __builtin_pow (a, 0.5);	/* should generate fsqrt */
+}
+
+#pragma GCC target "no-powerpc-gpopt,no-powerpc-gfxopt"
+
+double
+do_pow_0_75_nosqrt (double a)
+{
+  return __builtin_pow (a, 0.75);	/* should call pow */
+}
+
+double
+do_pow_0_5_nosqrt (double a)
+{
+  return __builtin_pow (a, 0.5);	/* should call sqrt */
+}
+
+#pragma GCC reset_options