public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
@ 2020-05-02 10:53 gabravier at gmail dot com
  2020-05-04  6:37 ` [Bug tree-optimization/94920] " rguenth at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: gabravier at gmail dot com @ 2020-05-02 10:53 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94920
           Summary: Failure to optimize abs pattern from arithmetic with
                    selected operands based on comparisons with 0
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

unsigned f(int x)
{
    return (x >= 0 ? x : 0) + (x <= 0 ? -x : 0);
}

This can be optimized to `return abs(x)`. LLVM does this transformation, but
GCC does not.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/94920] Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
  2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
@ 2020-05-04  6:37 ` rguenth at gcc dot gnu.org
  2021-08-14 23:58 ` pinskia at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-05-04  6:37 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
           Keywords|                            |missed-optimization
   Last reconfirmed|                            |2020-05-04
             Status|UNCONFIRMED                 |NEW

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/94920] Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
  2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
  2020-05-04  6:37 ` [Bug tree-optimization/94920] " rguenth at gcc dot gnu.org
@ 2021-08-14 23:58 ` pinskia at gcc dot gnu.org
  2022-07-21 21:27 ` cvs-commit at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-14 23:58 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2020-05-04 00:00:00         |2021-8-14
           Severity|normal                      |enhancement

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
  if (x_4(D) <= 0)
    goto <bb 3>; [INV]
  else
    goto <bb 4>; [INV]

  <bb 3> :
  iftmp.0_6 = -x_4(D);

  <bb 4> :
  # iftmp.0_3 = PHI <iftmp.0_6(3), 0(2)>

x <= 0 ? -x : 0 isn't this MAX<-x, 0>?

So there are two issues here, first is the above one, the second one is
MAX<a, 0> + MAX<-a,0> is not optimized to just MAX<-a,a> or just ABS<a>.

E.g.:
unsigned f1(int x)
{
   int t = (x >= 0 ? x : 0) ;
   int tt = -x;
   int t1 = (tt >= 0 ? tt : 0) ;
    return t + t1;
}

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/94920] Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
  2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
  2020-05-04  6:37 ` [Bug tree-optimization/94920] " rguenth at gcc dot gnu.org
  2021-08-14 23:58 ` pinskia at gcc dot gnu.org
@ 2022-07-21 21:27 ` cvs-commit at gcc dot gnu.org
  2022-07-27 10:04 ` cvs-commit at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-07-21 21:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Sam Feifer <sfeifer@gcc.gnu.org>:

https://gcc.gnu.org/g:633e9920589ddfaf2d6da1c24ce99b18a2638db4

commit r13-1785-g633e9920589ddfaf2d6da1c24ce99b18a2638db4
Author: Sam Feifer <sfeifer@redhat.com>
Date:   Thu Jul 21 16:31:41 2022 -0400

    match.pd: Add new abs pattern [PR94920]

    This patch is intended to fix a missed optimization in match.pd. It
optimizes (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) to just abs(x). Additionally,
the pattern (x <= 0 ? -x : 0) now gets optimized to max(-x, 0), which helps
with the other simplification rule.

    Tests are also included to be added to the testsuite.

    Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

            PR tree-optimization/94920

    gcc/ChangeLog:

            * match.pd (x >= 0 ? x : 0) + (x <= 0 ? -x : 0): New
simplification.
                       (x <= 0 ? -x : 0): New simplification.

    gcc/testsuite/ChangeLog:

            * g++.dg/pr94920-1.C: New test.
            * g++.dg/pr94920.C: New test.
            * gcc.dg/pr94920-2.c: New test.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/94920] Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
  2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2022-07-21 21:27 ` cvs-commit at gcc dot gnu.org
@ 2022-07-27 10:04 ` cvs-commit at gcc dot gnu.org
  2022-07-27 10:27 ` gabravier at gmail dot com
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-07-27 10:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:0bc1566dec0cab9410723c96d2ef3280fdab8e8e

commit r13-1854-g0bc1566dec0cab9410723c96d2ef3280fdab8e8e
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Jul 27 12:02:12 2022 +0200

    testsuite: Add -Wno-psabi to pr94920 tests [PR94920]

    These tests fail on ia32, because we get -Wpsabi warnings.
    Fixed by adding -Wno-psabi.  The pr94920.C test still fails the
    ABS_EXPR scan-tree-dump though, I think we'll need to add vect
    options and use vect_int effective target or something similar.

    2022-07-27  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/94920
            * g++.dg/pr94920.C: Add -Wno-psabi to dg-options.
            * g++.dg/pr94920-1.C: Add dg-additional-options -Wno-psabi.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/94920] Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
  2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2022-07-27 10:04 ` cvs-commit at gcc dot gnu.org
@ 2022-07-27 10:27 ` gabravier at gmail dot com
  2022-08-06  8:14 ` paul.hua.gm at gmail dot com
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: gabravier at gmail dot com @ 2022-07-27 10:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Gabriel Ravier <gabravier at gmail dot com> ---
So, is this fully fixed, or did I miss something ?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/94920] Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
  2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
                   ` (4 preceding siblings ...)
  2022-07-27 10:27 ` gabravier at gmail dot com
@ 2022-08-06  8:14 ` paul.hua.gm at gmail dot com
  2022-08-25 22:20 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: paul.hua.gm at gmail dot com @ 2022-08-06  8:14 UTC (permalink / raw)
  To: gcc-bugs

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

Paul Hua <paul.hua.gm at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |paul.hua.gm at gmail dot com

--- Comment #5 from Paul Hua <paul.hua.gm at gmail dot com> ---
(In reply to CVS Commits from comment #3)
> The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:
> 
> https://gcc.gnu.org/g:0bc1566dec0cab9410723c96d2ef3280fdab8e8e
> 
> commit r13-1854-g0bc1566dec0cab9410723c96d2ef3280fdab8e8e
> Author: Jakub Jelinek <jakub@redhat.com>
> Date:   Wed Jul 27 12:02:12 2022 +0200
> 
>     testsuite: Add -Wno-psabi to pr94920 tests [PR94920]
>     
>     These tests fail on ia32, because we get -Wpsabi warnings.
>     Fixed by adding -Wno-psabi.  The pr94920.C test still fails the
>     ABS_EXPR scan-tree-dump though, I think we'll need to add vect
>     options and use vect_int effective target or something similar.

Yes, we should do. This also fails the ABS_EXPR scan-tree-dump on LoongArch.

>     
>     2022-07-27  Jakub Jelinek  <jakub@redhat.com>
>     
>             PR tree-optimization/94920
>             * g++.dg/pr94920.C: Add -Wno-psabi to dg-options.
>             * g++.dg/pr94920-1.C: Add dg-additional-options -Wno-psabi.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/94920] Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
  2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
                   ` (5 preceding siblings ...)
  2022-08-06  8:14 ` paul.hua.gm at gmail dot com
@ 2022-08-25 22:20 ` pinskia at gcc dot gnu.org
  2022-08-25 22:23 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-08-25 22:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #6)
> (In reply to Paul Hua from comment #5)
> > 
> > Yes, we should do. This also fails the ABS_EXPR scan-tree-dump on LoongArch.
> 
> And on riscv32. I will look into that failure later this week.

I am suspecting it is because for both LoongArch and RISCV,
logical-op-non-short-circuit defualts to 0. I am going to debug it right now.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/94920] Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
  2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
                   ` (6 preceding siblings ...)
  2022-08-25 22:20 ` pinskia at gcc dot gnu.org
@ 2022-08-25 22:23 ` pinskia at gcc dot gnu.org
  2022-10-14 17:56 ` danglin at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-08-25 22:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #7)
> (In reply to Andrew Pinski from comment #6)
> > (In reply to Paul Hua from comment #5)
> > > 
> > > Yes, we should do. This also fails the ABS_EXPR scan-tree-dump on LoongArch.
> > 
> > And on riscv32. I will look into that failure later this week.
> 
> I am suspecting it is because for both LoongArch and RISCV,
> logical-op-non-short-circuit defualts to 0. I am going to debug it right now.

Oh I see the issue. it is not related at all to logical-op-non-short-circuit
but rather than the testcase should be split into two, one for the scalar and
one for the vector case. And then the vector case should match not at optimized
but right before vector lowering happens, maybe in forwprop1 or 2.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/94920] Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
  2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
                   ` (7 preceding siblings ...)
  2022-08-25 22:23 ` pinskia at gcc dot gnu.org
@ 2022-10-14 17:56 ` danglin at gcc dot gnu.org
  2023-03-04 22:35 ` sandra at gcc dot gnu.org
  2023-09-17 16:44 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: danglin at gcc dot gnu.org @ 2022-10-14 17:56 UTC (permalink / raw)
  To: gcc-bugs

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

John David Anglin <danglin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |danglin at gcc dot gnu.org

--- Comment #9 from John David Anglin <danglin at gcc dot gnu.org> ---
Test also occurs on hppa-unknown-linux-gnu.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/94920] Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
  2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
                   ` (8 preceding siblings ...)
  2022-10-14 17:56 ` danglin at gcc dot gnu.org
@ 2023-03-04 22:35 ` sandra at gcc dot gnu.org
  2023-09-17 16:44 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: sandra at gcc dot gnu.org @ 2023-03-04 22:35 UTC (permalink / raw)
  To: gcc-bugs

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

sandra at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sandra at gcc dot gnu.org

--- Comment #10 from sandra at gcc dot gnu.org ---
The ABS_EXPR test is also failing on nios2.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug tree-optimization/94920] Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0
  2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
                   ` (9 preceding siblings ...)
  2023-03-04 22:35 ` sandra at gcc dot gnu.org
@ 2023-09-17 16:44 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-17 16:44 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED
   Target Milestone|---                         |13.0

--- Comment #11 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #8)
> Oh I see the issue. it is not related at all to logical-op-non-short-circuit
> but rather than the testcase should be split into two, one for the scalar
> and one for the vector case. And then the vector case should match not at
> optimized but right before vector lowering happens, maybe in forwprop1 or 2.

That was fixed in r13-6927-gad32fcb8e91ba85cb4676c62 So all fixed in GCC 13.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-09-17 16:44 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-02 10:53 [Bug tree-optimization/94920] New: Failure to optimize abs pattern from arithmetic with selected operands based on comparisons with 0 gabravier at gmail dot com
2020-05-04  6:37 ` [Bug tree-optimization/94920] " rguenth at gcc dot gnu.org
2021-08-14 23:58 ` pinskia at gcc dot gnu.org
2022-07-21 21:27 ` cvs-commit at gcc dot gnu.org
2022-07-27 10:04 ` cvs-commit at gcc dot gnu.org
2022-07-27 10:27 ` gabravier at gmail dot com
2022-08-06  8:14 ` paul.hua.gm at gmail dot com
2022-08-25 22:20 ` pinskia at gcc dot gnu.org
2022-08-25 22:23 ` pinskia at gcc dot gnu.org
2022-10-14 17:56 ` danglin at gcc dot gnu.org
2023-03-04 22:35 ` sandra at gcc dot gnu.org
2023-09-17 16:44 ` pinskia at gcc dot gnu.org

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).