public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "luoxhu at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/22326] promotions (from float to double) are not removed when they should be able to
Date: Mon, 23 Nov 2020 08:39:25 +0000	[thread overview]
Message-ID: <bug-22326-4-Fp7zzzIjkL@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-22326-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=22326

--- Comment #9 from luoxhu at gcc dot gnu.org ---
(In reply to Andrew Pinski from comment #6)
> (In reply to luoxhu from comment #4)
> > float foo(float f, float x, float y) {
> > return (fabs(f)*x+y);
> > }
> > 
> > the input of fabs is float type, so use fabsf is enough here, drafted a
> > patch to avoid double promotion when generating gimple if fabs could be
> > replaced by fabsf as argument[0] is float type.
> 
> what about adding something to match.pd for:
> ABS<(float_convert)f> into (float_convert)ABS<f>
> This is only valid prompting and not reducing the precision.

Thanks, this is already implemented in fold-const.c, though not using match.pd
and fabsf really.  fabs will always convert arguments to double type first in
front-end.  And there are 3 kind of cases for this issue:

1) "return fabs(x);"
tree
fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
{
...
    case ABS_EXPR:
      /* Convert fabs((double)float) into (double)fabsf(float).  */
      if (TREE_CODE (arg0) == NOP_EXPR
          && TREE_CODE (type) == REAL_TYPE)
        {
          tree targ0 = strip_float_extensions (arg0);
          if (targ0 != arg0)
            return fold_convert_loc (loc, type,
                                     fold_build1_loc (loc, ABS_EXPR,
                                                  TREE_TYPE (targ0),
                                                  targ0));
        }
      return NULL_TREE;
...
}

This piece of code could convert the code from "(float)fabs((double)x)" to
"(float)(double)(float)fabs(x)", then match.pd could remove the useless
convert.

2) "return fabs(x)*y;"

Frontend will generate "(float) (fabs((double) x) * (double) y)" expression
first, 
then fold-const.c:fold_unary_loc will Convert fabs((double)float) into
(double)fabsf(float) and get "(float)((double)fabs(x) * (double)y)", finally,
match.pd will convert (outertype)((innertype0)a+(innertype1)b) into
((newtype)a+(newtype)b) to remove the double conversion.

3)"return fabs(x)*y + z;"

Frontend produces: (float) ((fabs((double) float) * (double) y) + (double z))

So what we need here is to match the MUL&ADD in match.pd as followed, any
comments?

+(simplify (convert (plus (mult (convert@3 (abs @0)) (convert@4 @1)) (convert@5
@2)))
+ (if (( flag_unsafe_math_optimizations
+       && types_match (type, float_type_node)
+       && types_match (TREE_TYPE(@0), float_type_node)
+       && types_match (TREE_TYPE(@1), float_type_node)
+       && types_match (TREE_TYPE(@2), float_type_node)
+       && element_precision (TREE_TYPE(@3)) > element_precision (TREE_TYPE
(@0))
+       && element_precision (TREE_TYPE(@4)) > element_precision (TREE_TYPE
(@1))
+       && element_precision (TREE_TYPE(@5)) > element_precision (TREE_TYPE
(@2))
+   && ! HONOR_NANS (type)
+         && ! HONOR_INFINITIES (type)))
+  (plus (mult (abs @0) @1) @2) ))
+

1) and 2) won't generate double conversion, only 3) has frsp in fast-math mode,
and it could be removed by above pattern.

PS: convert_to_real_1 seems to me not quite related here? It converts
(float)sqrt((double)x) where x is float into sqrtf(x), but with recursive call
to convert_to_real_1 and build_call_expr with new mathfn_built_in, I suppose it
a bit complicated to move them to match.pd?

The optimization should be under fast-math mode, is
flag_unsafe_math_optimizations enough to guard them?

  parent reply	other threads:[~2020-11-23  8:39 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-22326-4@http.gcc.gnu.org/bugzilla/>
2020-11-17  7:07 ` luoxhu at gcc dot gnu.org
2020-11-17  7:10 ` luoxhu at gcc dot gnu.org
2020-11-17 10:21 ` pinskia at gcc dot gnu.org
2020-11-17 21:56 ` joseph at codesourcery dot com
2020-11-18 15:22 ` segher at gcc dot gnu.org
2020-11-23  8:39 ` luoxhu at gcc dot gnu.org [this message]
2020-11-23  8:44 ` luoxhu at gcc dot gnu.org
2020-11-23 12:09 ` rguenther at suse dot de
2020-11-24 10:00 ` rsandifo at gcc dot gnu.org
2020-11-27  6:20 ` luoxhu at gcc dot gnu.org
2020-11-27  6:27 ` luoxhu at gcc dot gnu.org
2020-11-27  7:12 ` rguenther at suse dot de
2020-11-27  9:24 ` rsandifo at gcc dot gnu.org
2020-11-30  8:31 ` luoxhu at gcc dot gnu.org
2020-12-11 13:49 ` segher at gcc dot gnu.org
2020-12-11 14:24 ` rguenth at gcc dot gnu.org
2020-12-11 17:51 ` segher at gcc dot gnu.org
2020-12-11 19:24 ` rguenther at suse dot de
2020-12-14  7:21 ` luoxhu at gcc dot gnu.org
     [not found] <bug-22326-6528@http.gcc.gnu.org/bugzilla/>
2006-08-21  6:08 ` pinskia at gcc dot gnu dot org
2007-07-09  6:08 ` pinskia at gcc dot gnu dot org
2005-07-06 15:40 [Bug tree-optimization/22326] New: " pinskia at gcc dot gnu dot org
2005-07-12 20:37 ` [Bug tree-optimization/22326] " pinskia at gcc dot gnu dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-22326-4-Fp7zzzIjkL@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).