On Thu, 5 Sep 2019, Barnaby Wilks wrote:

> Hello,
> 
> This patch changes a match.pd expression so that binary math expressions 
> will not be done in the precision of it's widest type if
> -funsafe-math-optimizations is enabled.
> 
> This patch has been extracted from
> https://gcc.gnu.org/ml/gcc-patches/2019-09/msg00072.html
> based on upstream comments.
> 
> For example take the function:
> 
>       float f (float x, float y) {
>         double z = 1.0 / x;
>         return z * y;
>       }
> 
> Without this patch this would generate the following (with -Ofast):
> 
>       _1 = (double) x_4(D);
>       z_5 = 1.0e+0 / _1;
>       _2 = (double) y_6(D);
>       _3 = _2 * z_5;
>       _7 = (float) _3;
>       return _7;
> 
> This contains 3 unnecessary casts - since all outputs of the expression
> are in single precision, the calculation itself can be done entirely in
> single precision - instead of converting the operands to double,
> doing the calculation and then converting the result back to
> single precision again.
> 
> With this patch (and -Ofast) the following GIMPLE is generated:
> 
>       _5 = 1.0e+0 / x_1(D);
>       _3 = y_2(D) * _5;
>       return _3;
> 
> The benefits can then be seen in the generated code:
> 
> Without this patch
> 
>       f:
>         fcvt  d1, s1
>         fcvt  d0, s0
>         fdiv  d0, d1, d0
>         fcvt  s0, d0
>         ret
> 
> 
> With this patch
> 
>       f:
>         fdiv s0, s1, s0
>         ret
> 
> Added tests to verify that no unnecessary casts are kept.
> 
> Bootstrapped and regtested on aarch64 and x86_64 with no regressions.
> 
> I don't have write access, so if OK for trunk then can someone commit on 
> my behalf?

OK.  I'll commit it after a round of testing.

Thanks,
Richard.

> Regards
> Barney
> 
> gcc/ChangeLog:
> 
> 2019-09-05  Barnaby Wilks  <barnaby.wi...@arm.com>
> 
>       * match.pd: Add flag_unsafe_math_optimizations check
>       before deciding on the widest type in a binary math operation.
> 
> gcc/testsuite/ChangeLog:
> 
> 2019-09-05  Barnaby Wilks  <barnaby.wi...@arm.com>
> 
>       * gcc.dg/fold-binary-math-casts.c: New test.

Reply via email to