[PATCH] Optimize x*x*x*x*x*x using 3 multiplications.

Richard Henderson rth@redhat.com
Thu Jul 31 03:09:00 GMT 2003


On Tue, Jul 29, 2003 at 11:13:44AM -0600, Roger Sayle wrote:
> The following patch allows gcc to optimize floating point expressions
> such as x*x*x*x*x*x when compiled with -ffast-math to use only three
> multiplications rather than five (as currently generated by mainline).

I'd really wish we handled this properly via reassociation.

> + 	      /* Optimize x*pow(x,c) as pow(x,c+1).  */
> + 	      if (fcode1 == BUILT_IN_POW
> + 		  || fcode1 == BUILT_IN_POWF
> + 		  || fcode1 == BUILT_IN_POWL)
> + 		{
> + 		  tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
> + 		  tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
> + 								     1)));
> + 		  if (TREE_CODE (arg11) == REAL_CST
> + 		      && ! TREE_CONSTANT_OVERFLOW (arg11)
> + 		      && operand_equal_p (arg0, arg10, 0))

I don't see how you'll prevent "x*x*x" -> "pow(x,3)" ...

> ! 	  /* If the exponent is -1, 0, 1 or 2, then expand_powi is exact.
> ! 	     Otherwise, check the number of multiplications required.
> ! 	     Note that pow never sets errno for an integer exponent.  */
> ! 	  if ((n >= -1 && n <= 2)
> ! 	      || (flag_unsafe_math_optimizations
> ! 		  && ! optimize_size
> ! 		  && powi_cost (n) <= POWI_MAX_MULTS))

from *not* calling the libm function if !flag_unsafe_math_optimizations.


r~



More information about the Gcc-patches mailing list