RE: [PATCHv2][GCC] Optimise the fpclassify builtin to perform integer operations when possible

2016-10-21 Thread Tamar Christina
Hi Richard, Jeff,

Fair enough, I understand the reservations both of you have.

I'll spend some time experimenting with what kind of code I'd
Get out of it from lowering early and come up with an updated
Patch.

Thanks!
Tamar

> -Original Message-
> From: Richard Biener [mailto:rguent...@suse.de]
> Sent: 21 October 2016 09:05
> To: Jeff Law
> Cc: Tamar Christina; GCC Patches; nd; Richard Earnshaw; Wilco Dijkstra;
> ja...@redhat.com; Joseph Myers; Michael Meissner; Moritz Klammler;
> Andrew Pinski
> Subject: Re: [PATCHv2][GCC] Optimise the fpclassify builtin to perform
> integer operations when possible
> 
> On Thu, 20 Oct 2016, Jeff Law wrote:
> 
> > On 09/30/2016 07:22 AM, Tamar Christina wrote:
> > > Hi All,
> > >
> > > This is v2 of the patch which adds an optimized route to the
> > > fpclassify builtin for floating point numbers which are similar to
> > > IEEE-754 in format.
> > >
> > > I have addressed most comments from everyone except for two things:
> > >
> > > 1) Providing a back-end hook to override the functionality. While 
> > > certainly
> > >possible the current fpclassify doesn't provide this either. So
> > > I'd like to
> > >treat it as an enhancement rather than an issue.
> > I think the concern here is PPC, particularly the newer ones which
> > have significant hardware support for these kind of characterizations.
> >
> > Based on the discussions though, I suspect we're going to need
> > something nontrivial due to the way the API for __builtin_fpclassify
> > works.  In the end I can easily see some target way to override the default
> code synthesis.
> >
> > I think these issues should be left for the PPC folks to propose a
> > solution when they're ready to exploit their new hardware.  I don't
> > think this should block the patch.
> >
> >
> >
> > >
> > > 2) Doing it in a lowering phase. If the general consensus is that
> > > this is the
> > >path the patch must take then I'd be happy to reconsider. However at
> this
> > >this patch does not seem to produce worse code than what there
> > > was before.
> > I think that was a desire from Richi.   I'm a bit torn here.
> >
> > The code looks more like lowering rather than folding.  But it's also
> > generating non-gimple trees and relies on gimple_fold_builtin to
> > re-gimplify the result AFAICT.
> >
> > Richi -- thoughts?
> 
> I'm not entirely happy with the patch but also not with the current state of
> handling of fpclassify.  I do see the need to lower(!) fpclassify early 
> because
> we want to optimize it both depending on the return value usage and the
> input value.
> 
> The lowering we currently apply open-codes isnormal (we have a builtin for
> this) and isfinite (likewise).  I'd prefer if we can apply the lowering in the
> gimplifier and somehow avoid the early decision on whether to use FP or
> integer code to perform the operation.  Sth like
> 
>  fpclassify(x) -> isnan(x) ? FP_NAN : isnormal(x) ? FP_NORMAL
> : !isfinite(x) ? FP_INFINITE : x == 0 ? FP_ZERO : FP_SUBNORMAL
> 
> (leaves the comparison against zero in explicit FP math).  We do have later
> foldings that expand isnormal and isfinite and isinf to use compares -- those
> are the ones that we might want to change to integer reps.
> 
> We are also missing optabs for most of the sub-classification tasks which
> would make it possible to re-combine the whole thing back to a single
> fpclassify asm op.
> 
> That said, the folding to integer ops obfuscates the real operation and thus
> makes the job of a (not yet existing) pass optimizing these kind of
> classifications via range analysis or the like hard.
> Thus I'd rather apply those at or near to RTL expansion time.
> 
> Richard.
> 
> > --
> >
> > I think its nontrivial to judge worse vs better since it's really a
> > function of the target's micro-architecture and the context in which
> > fpclassify is called -- particularly where the input value lives and
> > whether or not its used in other ways nearby.
> >
> > In the case where the input value is in memory or not used in floating
> > point arithmetic nearby, your change should be a clear win (with the
> > exception of the latest ppc hardware perhaps).
> >
> > If the input value is not in memory and used nearby in FP ops, then it
> > gets a lot trickier.  We run the risk of making the object addressable
> > which means it won't be an SSA_NAME and thus not exposed to the high
> level optimizers.
> >
> > Richi has in

Re: [PATCHv2][GCC] Optimise the fpclassify builtin to perform integer operations when possible

2016-10-21 Thread Richard Biener
On Thu, 20 Oct 2016, Jeff Law wrote:

> On 09/30/2016 07:22 AM, Tamar Christina wrote:
> > Hi All,
> > 
> > This is v2 of the patch which adds an optimized route to the fpclassify
> > builtin
> > for floating point numbers which are similar to IEEE-754 in format.
> > 
> > I have addressed most comments from everyone except for two things:
> > 
> > 1) Providing a back-end hook to override the functionality. While certainly
> >possible the current fpclassify doesn't provide this either. So I'd like
> > to
> >treat it as an enhancement rather than an issue.
> I think the concern here is PPC, particularly the newer ones which have
> significant hardware support for these kind of characterizations.
> 
> Based on the discussions though, I suspect we're going to need something
> nontrivial due to the way the API for __builtin_fpclassify works.  In the end
> I can easily see some target way to override the default code synthesis.
> 
> I think these issues should be left for the PPC folks to propose a solution
> when they're ready to exploit their new hardware.  I don't think this should
> block the patch.
> 
> 
> 
> > 
> > 2) Doing it in a lowering phase. If the general consensus is that this is
> > the
> >path the patch must take then I'd be happy to reconsider. However at this
> >this patch does not seem to produce worse code than what there was
> > before.
> I think that was a desire from Richi.   I'm a bit torn here.
> 
> The code looks more like lowering rather than folding.  But it's also
> generating non-gimple trees and relies on gimple_fold_builtin to re-gimplify
> the result AFAICT.
>
> Richi -- thoughts?

I'm not entirely happy with the patch but also not with the current
state of handling of fpclassify.  I do see the need to lower(!)
fpclassify early because we want to optimize it both depending on
the return value usage and the input value.

The lowering we currently apply open-codes isnormal (we have a
builtin for this) and isfinite (likewise).  I'd prefer if we can
apply the lowering in the gimplifier and somehow avoid the
early decision on whether to use FP or integer code to perform
the operation.  Sth like

 fpclassify(x) -> isnan(x) ? FP_NAN : isnormal(x) ? FP_NORMAL
: !isfinite(x) ? FP_INFINITE : x == 0 ? FP_ZERO : FP_SUBNORMAL

(leaves the comparison against zero in explicit FP math).  We do
have later foldings that expand isnormal and isfinite and
isinf to use compares -- those are the ones that we might want to
change to integer reps.

We are also missing optabs for most of the sub-classification
tasks which would make it possible to re-combine the whole
thing back to a single fpclassify asm op.

That said, the folding to integer ops obfuscates the real operation
and thus makes the job of a (not yet existing) pass optimizing
these kind of classifications via range analysis or the like hard.
Thus I'd rather apply those at or near to RTL expansion time.

Richard.
 
> --
> 
> I think its nontrivial to judge worse vs better since it's really a function
> of the target's micro-architecture and the context in which fpclassify is
> called -- particularly where the input value lives and whether or not its used
> in other ways nearby.
> 
> In the case where the input value is in memory or not used in floating point
> arithmetic nearby, your change should be a clear win (with the exception of
> the latest ppc hardware perhaps).
> 
> If the input value is not in memory and used nearby in FP ops, then it gets a
> lot trickier.  We run the risk of making the object addressable which means it
> won't be an SSA_NAME and thus not exposed to the high level optimizers.
> 
> Richi has indicated that in gimple an object need not be addressable just
> because we access random pieces of it, including the ability to avoid marking
> something as addressable even though we have MEM () style expressions.
> I'm not sure how all that works, but trust Richi implicitly.  Additionally
> you're using VIEW_CONVERT_EXPR now rather than ADDR_EXPR, so that may mitigate
> things as well.
> 
> Finally there's the issue of having to transfer the object between the FP and
> GP register files which can be highly expensive on some architectures.  Short
> of looking at the defs & immediate uses of the input argument and trying to
> guess at the cost of moving the object between the register files I don't see
> a good way to tackle this issue.
> 
> I'm inclined to not object on the performance questions.  But I would like to
> hear from Richi WRT lowering vs folding and whether or not he believes this
> belongs elsewhere.  If he does, I'd be inclined to suggest earlier rather than
> later since we do want to expose the generated code to the gimple optimizers.
> 
> Additional implementation comments follow inline.
> 
> 
> > 
> > gcc/
> > 2016-08-25  Tamar Christina  
> > Wilco Dijkstra  
> > 
> > * gcc/builtins.c (fold_builtin_fpclassify): Added optimized 

Re: [PATCHv2][GCC] Optimise the fpclassify builtin to perform integer operations when possible

2016-10-20 Thread Jeff Law

On 09/30/2016 07:22 AM, Tamar Christina wrote:

Hi All,

This is v2 of the patch which adds an optimized route to the fpclassify builtin
for floating point numbers which are similar to IEEE-754 in format.

I have addressed most comments from everyone except for two things:

1) Providing a back-end hook to override the functionality. While certainly
   possible the current fpclassify doesn't provide this either. So I'd like to
   treat it as an enhancement rather than an issue.
I think the concern here is PPC, particularly the newer ones which have 
significant hardware support for these kind of characterizations.


Based on the discussions though, I suspect we're going to need something 
nontrivial due to the way the API for __builtin_fpclassify works.  In 
the end I can easily see some target way to override the default code 
synthesis.


I think these issues should be left for the PPC folks to propose a 
solution when they're ready to exploit their new hardware.  I don't 
think this should block the patch.






2) Doing it in a lowering phase. If the general consensus is that this is the
   path the patch must take then I'd be happy to reconsider. However at this
   this patch does not seem to produce worse code than what there was before.

I think that was a desire from Richi.   I'm a bit torn here.

The code looks more like lowering rather than folding.  But it's also 
generating non-gimple trees and relies on gimple_fold_builtin to 
re-gimplify the result AFAICT.


Richi -- thoughts?

--

I think its nontrivial to judge worse vs better since it's really a 
function of the target's micro-architecture and the context in which 
fpclassify is called -- particularly where the input value lives and 
whether or not its used in other ways nearby.


In the case where the input value is in memory or not used in floating 
point arithmetic nearby, your change should be a clear win (with the 
exception of the latest ppc hardware perhaps).


If the input value is not in memory and used nearby in FP ops, then it 
gets a lot trickier.  We run the risk of making the object addressable 
which means it won't be an SSA_NAME and thus not exposed to the high 
level optimizers.


Richi has indicated that in gimple an object need not be addressable 
just because we access random pieces of it, including the ability to 
avoid marking something as addressable even though we have MEM () 
style expressions.  I'm not sure how all that works, but trust Richi 
implicitly.  Additionally you're using VIEW_CONVERT_EXPR now rather than 
ADDR_EXPR, so that may mitigate things as well.


Finally there's the issue of having to transfer the object between the 
FP and GP register files which can be highly expensive on some 
architectures.  Short of looking at the defs & immediate uses of the 
input argument and trying to guess at the cost of moving the object 
between the register files I don't see a good way to tackle this issue.


I'm inclined to not object on the performance questions.  But I would 
like to hear from Richi WRT lowering vs folding and whether or not he 
believes this belongs elsewhere.  If he does, I'd be inclined to suggest 
earlier rather than later since we do want to expose the generated code 
to the gimple optimizers.


Additional implementation comments follow inline.




gcc/
2016-08-25  Tamar Christina  
Wilco Dijkstra  

* gcc/builtins.c (fold_builtin_fpclassify): Added optimized version.
* gcc/real.h (real_format): Added is_ieee_compatible field.
* gcc/real.c (ieee_single_format): Set is_ieee_compatible flag.
(mips_single_format): Likewise.
(motorola_single_format): Likewise.
(spu_single_format): Likewise.
(ieee_double_format): Likewise.
(mips_double_format): Likewise.
(motorola_double_format): Likewise.
(ieee_extended_motorola_format): Likewise.
(ieee_extended_intel_128_format): Likewise.
(ieee_extended_intel_96_round_53_format): Likewise.
(ibm_extended_format): Likewise.
(mips_extended_format): Likewise.
(ieee_quad_format): Likewise.
(mips_quad_format): Likewise.
(vax_f_format): Likewise.
(vax_d_format): Likewise.
(vax_g_format): Likewise.
(decimal_single_format): Likewise.
(decimal_quad_format): Likewise.
(iee_half_format): Likewise.
(mips_single_format): Likewise.
(arm_half_format): Likewise.
(real_internal_format): Likewise.


gcc/testsuite/
2016-09-27  Tamar Christina  

* gcc.target/aarch64/builtin-fpclassify.c: New codegen test.


gcc-v2-fpclassify.patch


diff --git a/gcc/builtins.c b/gcc/builtins.c
index 
9a19a75cc8ed6edb5f543cd7bd26bcc0693e6ebb..1b4878c5ba098dcc0a4a506dbc7959d150cc9028
 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -7943,10 +7943,8 @@ static tree
 fold_builtin_fpclassify (location_t loc, 

Re: [PATCHv2][GCC] Optimise the fpclassify builtin to perform integer operations when possible

2016-10-17 Thread Tamar Christina
Ping


From: gcc-patches-ow...@gcc.gnu.org <gcc-patches-ow...@gcc.gnu.org> on behalf 
of Tamar Christina <tamar.christ...@arm.com>
Sent: Friday, September 30, 2016 2:22:35 PM
To: GCC Patches
Cc: nd; Richard Earnshaw; Wilco Dijkstra; ja...@redhat.com; Joseph Myers; 
Michael Meissner; rguent...@suse.de; Moritz Klammler; Andrew Pinski; 
l...@redhat.com
Subject: [PATCHv2][GCC] Optimise the fpclassify builtin to perform integer 
operations when possible

Hi All,

This is v2 of the patch which adds an optimized route to the fpclassify builtin
for floating point numbers which are similar to IEEE-754 in format.

I have addressed most comments from everyone except for two things:

1) Providing a back-end hook to override the functionality. While certainly
   possible the current fpclassify doesn't provide this either. So I'd like to
   treat it as an enhancement rather than an issue.

2) Doing it in a lowering phase. If the general consensus is that this is the
   path the patch must take then I'd be happy to reconsider. However at this
   this patch does not seem to produce worse code than what there was before.

The goal is to make it faster by:
1. Trying to determine the most common case first
   (e.g. the float is a Normal number) and then the
   rest. The amount of code generated at -O2 are
   about the same +/- 1 instruction, but the code
   is much better.
2. Using integer operation in the optimized path.

At a high level, the optimized path uses integer operations
to perform the following:

  if (exponent bits aren't all set or unset)
 return Normal;
  else if (no bits are set on the number after masking out
   sign bits then)
 return Zero;
  else if (exponent has no bits set)
 return Subnormal;
  else if (mantissa has no bits set)
 return Infinite;
  else
 return NaN;

In case the optimization can't be applied the old
implementation is used as a fall-back.

A limitation with this new approach is that the exponent
of the floating point has to fit in 31 bits and the floating
point has to have an IEEE like format and values for NaN and INF
(e.g. for NaN and INF all bits of the exp must be set).

To determine this IEEE likeness a new boolean was added to real_format.

As an example, Aarch64 now generates for classification of doubles:

f:
fmovx1, d0
mov w0, 7
sbfxx2, x1, 52, 11
add w3, w2, 1
tst w3, 0x07FE
bne .L1
mov w0, 13
tst x1, 0x7fff
beq .L1
mov w0, 11
tbz x2, 0, .L1
tst x1, 0xf
mov w0, 3
mov w1, 5
cselw0, w0, w1, ne

.L1:
ret

No new tests as there are existing tests to test functionality.
glibc benchmarks ran against the builtin and this shows a 42.5%
performance gain on Aarch64.

Regression tests ran on aarch64-none-linux and arm-none-linux-gnueabi
and no regression. x86 also has no regressions and modest gains (3%).

Ok for trunk?

Thanks,
Tamar

gcc/
2016-08-25  Tamar Christina  <tamar.christ...@arm.com>
Wilco Dijkstra  <wilco.dijks...@arm.com>

* gcc/builtins.c (fold_builtin_fpclassify): Added optimized version.
* gcc/real.h (real_format): Added is_ieee_compatible field.
* gcc/real.c (ieee_single_format): Set is_ieee_compatible flag.
(mips_single_format): Likewise.
(motorola_single_format): Likewise.
(spu_single_format): Likewise.
(ieee_double_format): Likewise.
(mips_double_format): Likewise.
(motorola_double_format): Likewise.
(ieee_extended_motorola_format): Likewise.
(ieee_extended_intel_128_format): Likewise.
(ieee_extended_intel_96_round_53_format): Likewise.
(ibm_extended_format): Likewise.
(mips_extended_format): Likewise.
(ieee_quad_format): Likewise.
(mips_quad_format): Likewise.
(vax_f_format): Likewise.
(vax_d_format): Likewise.
(vax_g_format): Likewise.
(decimal_single_format): Likewise.
(decimal_quad_format): Likewise.
(iee_half_format): Likewise.
(mips_single_format): Likewise.
(arm_half_format): Likewise.
(real_internal_format): Likewise.


gcc/testsuite/
2016-09-27  Tamar Christina  <tamar.christ...@arm.com>

* gcc.target/aarch64/builtin-fpclassify.c: New codegen test.



[PATCHv2][GCC] Optimise the fpclassify builtin to perform integer operations when possible

2016-09-30 Thread Tamar Christina
Hi All,

This is v2 of the patch which adds an optimized route to the fpclassify builtin
for floating point numbers which are similar to IEEE-754 in format.

I have addressed most comments from everyone except for two things:

1) Providing a back-end hook to override the functionality. While certainly
   possible the current fpclassify doesn't provide this either. So I'd like to
   treat it as an enhancement rather than an issue.

2) Doing it in a lowering phase. If the general consensus is that this is the
   path the patch must take then I'd be happy to reconsider. However at this
   this patch does not seem to produce worse code than what there was before.

The goal is to make it faster by:
1. Trying to determine the most common case first
   (e.g. the float is a Normal number) and then the
   rest. The amount of code generated at -O2 are
   about the same +/- 1 instruction, but the code
   is much better.
2. Using integer operation in the optimized path.

At a high level, the optimized path uses integer operations
to perform the following:

  if (exponent bits aren't all set or unset)
 return Normal;
  else if (no bits are set on the number after masking out
   sign bits then)
 return Zero;
  else if (exponent has no bits set)
 return Subnormal;
  else if (mantissa has no bits set)
 return Infinite;
  else
 return NaN;

In case the optimization can't be applied the old
implementation is used as a fall-back.

A limitation with this new approach is that the exponent
of the floating point has to fit in 31 bits and the floating
point has to have an IEEE like format and values for NaN and INF
(e.g. for NaN and INF all bits of the exp must be set).

To determine this IEEE likeness a new boolean was added to real_format.

As an example, Aarch64 now generates for classification of doubles:

f:
fmovx1, d0
mov w0, 7
sbfxx2, x1, 52, 11
add w3, w2, 1
tst w3, 0x07FE
bne .L1
mov w0, 13
tst x1, 0x7fff
beq .L1
mov w0, 11
tbz x2, 0, .L1
tst x1, 0xf
mov w0, 3
mov w1, 5
cselw0, w0, w1, ne

.L1:
ret

No new tests as there are existing tests to test functionality.
glibc benchmarks ran against the builtin and this shows a 42.5%
performance gain on Aarch64.

Regression tests ran on aarch64-none-linux and arm-none-linux-gnueabi
and no regression. x86 also has no regressions and modest gains (3%).

Ok for trunk?

Thanks,
Tamar

gcc/
2016-08-25  Tamar Christina  
Wilco Dijkstra  

* gcc/builtins.c (fold_builtin_fpclassify): Added optimized version. 
* gcc/real.h (real_format): Added is_ieee_compatible field.
* gcc/real.c (ieee_single_format): Set is_ieee_compatible flag.
(mips_single_format): Likewise.
(motorola_single_format): Likewise.
(spu_single_format): Likewise.
(ieee_double_format): Likewise.
(mips_double_format): Likewise.
(motorola_double_format): Likewise.
(ieee_extended_motorola_format): Likewise.
(ieee_extended_intel_128_format): Likewise.
(ieee_extended_intel_96_round_53_format): Likewise.
(ibm_extended_format): Likewise.
(mips_extended_format): Likewise.
(ieee_quad_format): Likewise.
(mips_quad_format): Likewise.
(vax_f_format): Likewise.
(vax_d_format): Likewise.
(vax_g_format): Likewise.
(decimal_single_format): Likewise.
(decimal_quad_format): Likewise.
(iee_half_format): Likewise.
(mips_single_format): Likewise.
(arm_half_format): Likewise.
(real_internal_format): Likewise.


gcc/testsuite/
2016-09-27  Tamar Christina  

* gcc.target/aarch64/builtin-fpclassify.c: New codegen test.diff --git a/gcc/builtins.c b/gcc/builtins.c
index 9a19a75cc8ed6edb5f543cd7bd26bcc0693e6ebb..1b4878c5ba098dcc0a4a506dbc7959d150cc9028 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -7943,10 +7943,8 @@ static tree
 fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
 {
   tree fp_nan, fp_infinite, fp_normal, fp_subnormal, fp_zero,
-arg, type, res, tmp;
+arg, type, res;
   machine_mode mode;
-  REAL_VALUE_TYPE r;
-  char buf[128];
 
   /* Verify the required arguments in the original call.  */
   if (nargs != 6
@@ -7966,14 +7964,164 @@ fold_builtin_fpclassify (location_t loc, tree *args, int nargs)
   arg = args[5];
   type = TREE_TYPE (arg);
   mode = TYPE_MODE (type);
-  arg = builtin_save_expr (fold_build1_loc (loc, ABS_EXPR, type, arg));
+  const real_format *format = REAL_MODE_FORMAT (mode);
+  const HOST_WIDE_INT type_width = TYPE_PRECISION (type);
+
+  /*
+  For IEEE 754 types:
+
+  fpclassify (x) ->
+   !((exp + 1) & (exp_mask & ~1)) // exponent bits not all set or