public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/112392] New: `a == nonnegative ? a : abs<a>` -> abs<a>
@ 2023-11-05  5:56 pinskia at gcc dot gnu.org
  2023-11-05  5:58 ` [Bug tree-optimization/112392] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-05  5:56 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112392
           Summary: `a == nonnegative ? a : abs<a>` -> abs<a>
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: pinskia at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
int f(int a, unsigned char b)
{
  int absb = b;
  if (a == absb)  return absb;
  return a > 0 ? a : -a;
}
```
This should just be turned into
`return ABS_EXPR<a>;`
as we know that if a was a nonnegative value, then taking the ABS of that is
the same as a.

So something like:
```
(for cmp (eq gt ge)
 (simplify
  (cond (cmp:c @0 tree_expr_nonnegative_p@1) @0 (abs@3 @0))
  (if (INTEGRAL_TYPE_P (type))
   @3))
```

`a > nonnegative ? a : abs<a>` is also just `abs<a>` too. 

There are some more improvements dealing with type changing between signed and
unsigned but this is the first step.

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

* [Bug tree-optimization/112392] `a == nonnegative ? a : abs<a>` -> abs<a>
  2023-11-05  5:56 [Bug tree-optimization/112392] New: `a == nonnegative ? a : abs<a>` -> abs<a> pinskia at gcc dot gnu.org
@ 2023-11-05  5:58 ` pinskia at gcc dot gnu.org
  2023-11-14  0:34 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-05  5:58 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=111957
   Last reconfirmed|                            |2023-11-05
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is related to PR 111957. For positive values this would catch it too.

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

* [Bug tree-optimization/112392] `a == nonnegative ? a : abs<a>` -> abs<a>
  2023-11-05  5:56 [Bug tree-optimization/112392] New: `a == nonnegative ? a : abs<a>` -> abs<a> pinskia at gcc dot gnu.org
  2023-11-05  5:58 ` [Bug tree-optimization/112392] " pinskia at gcc dot gnu.org
@ 2023-11-14  0:34 ` pinskia at gcc dot gnu.org
  2024-05-08  3:25 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-14  0:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 56579
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=56579&action=edit
More complete testcase for the ones which should be detected

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

* [Bug tree-optimization/112392] `a == nonnegative ? a : abs<a>` -> abs<a>
  2023-11-05  5:56 [Bug tree-optimization/112392] New: `a == nonnegative ? a : abs<a>` -> abs<a> pinskia at gcc dot gnu.org
  2023-11-05  5:58 ` [Bug tree-optimization/112392] " pinskia at gcc dot gnu.org
  2023-11-14  0:34 ` pinskia at gcc dot gnu.org
@ 2024-05-08  3:25 ` pinskia at gcc dot gnu.org
  2024-05-08 15:38 ` cvs-commit at gcc dot gnu.org
  2024-05-08 15:39 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-08  3:25 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                URL|                            |https://gcc.gnu.org/piperma
                   |                            |il/gcc-patches/2024-May/651
                   |                            |006.html

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Patch submitted:
https://gcc.gnu.org/pipermail/gcc-patches/2024-May/651006.html

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

* [Bug tree-optimization/112392] `a == nonnegative ? a : abs<a>` -> abs<a>
  2023-11-05  5:56 [Bug tree-optimization/112392] New: `a == nonnegative ? a : abs<a>` -> abs<a> pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2024-05-08  3:25 ` pinskia at gcc dot gnu.org
@ 2024-05-08 15:38 ` cvs-commit at gcc dot gnu.org
  2024-05-08 15:39 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-05-08 15:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:

https://gcc.gnu.org/g:5726de79e2154a16d8a045567d2cfad035f7ed19

commit r15-328-g5726de79e2154a16d8a045567d2cfad035f7ed19
Author: Andrew Pinski <quic_apinski@quicinc.com>
Date:   Mon May 6 23:53:41 2024 -0700

    match: `a CMP nonnegative ? a : ABS<a>` simplified to just `ABS<a>`
[PR112392]

    We can optimize `a == nonnegative ? a : ABS<a>`, `a > nonnegative ? a :
ABS<a>`
    and `a >= nonnegative ? a : ABS<a>` into `ABS<a>`. This allows removal of
    some extra comparison and extra conditional moves in some cases.
    I don't remember where I had found though but it is simple to add so
    let's add it.

    Bootstrapped and tested on x86_64-linux-gnu with no regressions.

    Note I have a secondary pattern for the equal case as either a or
nonnegative
    could be used.

            PR tree-optimization/112392

    gcc/ChangeLog:

            * match.pd (`x CMP nonnegative ? x : ABS<x>`): New pattern;
            where CMP is ==, > and >=.
            (`x CMP nonnegative@y ? y : ABS<x>`): New pattern.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/phi-opt-41.c: New test.

    Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>

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

* [Bug tree-optimization/112392] `a == nonnegative ? a : abs<a>` -> abs<a>
  2023-11-05  5:56 [Bug tree-optimization/112392] New: `a == nonnegative ? a : abs<a>` -> abs<a> pinskia at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2024-05-08 15:38 ` cvs-commit at gcc dot gnu.org
@ 2024-05-08 15:39 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-08 15:39 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
   Target Milestone|---                         |15.0
         Resolution|---                         |FIXED

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2024-05-08 15:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-05  5:56 [Bug tree-optimization/112392] New: `a == nonnegative ? a : abs<a>` -> abs<a> pinskia at gcc dot gnu.org
2023-11-05  5:58 ` [Bug tree-optimization/112392] " pinskia at gcc dot gnu.org
2023-11-14  0:34 ` pinskia at gcc dot gnu.org
2024-05-08  3:25 ` pinskia at gcc dot gnu.org
2024-05-08 15:38 ` cvs-commit at gcc dot gnu.org
2024-05-08 15:39 ` 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).