public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/109424] New: ~
@ 2023-04-05 19:47 pinskia at gcc dot gnu.org
  2023-04-05 19:47 ` [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-05 19:47 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109424
           Summary: ~
           Product: gcc
           Version: 13.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 x, int y)
{
    int t = ((x > y) ? x : y);
    return ~t;
}
int f1(int x, int y)
{
    return ~((x > y) ? x : y);
}
```
You would assume GCC produce the same code for both, but nope, the first f is
worse.

The reason why is GCC decides to move the ~ into the ?: operator making the
code worse.

fold does this "optimization" but nothing undones it, phi-opt undones it for
casts in some but not all cases.

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

* [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions
  2023-04-05 19:47 [Bug tree-optimization/109424] New: ~ pinskia at gcc dot gnu.org
@ 2023-04-05 19:47 ` pinskia at gcc dot gnu.org
  2023-04-05 19:48 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-05 19:47 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|~                           |~((x > y) ? x : y) produces
                   |                            |two not instructions

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
> You would assume GCC produce the same code for both, but nope, the first f is worse.

Sorry f1 is worse.

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

* [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions
  2023-04-05 19:47 [Bug tree-optimization/109424] New: ~ pinskia at gcc dot gnu.org
  2023-04-05 19:47 ` [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions pinskia at gcc dot gnu.org
@ 2023-04-05 19:48 ` pinskia at gcc dot gnu.org
  2023-04-07 16:29 ` roger at nextmovesoftware dot com
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-05 19:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I noticed this while looking into PR 96694 .

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

* [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions
  2023-04-05 19:47 [Bug tree-optimization/109424] New: ~ pinskia at gcc dot gnu.org
  2023-04-05 19:47 ` [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions pinskia at gcc dot gnu.org
  2023-04-05 19:48 ` pinskia at gcc dot gnu.org
@ 2023-04-07 16:29 ` roger at nextmovesoftware dot com
  2023-04-16 18:32 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: roger at nextmovesoftware dot com @ 2023-04-07 16:29 UTC (permalink / raw)
  To: gcc-bugs

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

Roger Sayle <roger at nextmovesoftware dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |roger at nextmovesoftware dot com

--- Comment #3 from Roger Sayle <roger at nextmovesoftware dot com> ---
Interestingly, this is a C++ front-end issue, as the C front-end doesn't
exhibit this behaviour.  However, in C++, it affects both unary operators,
NOT_EXPR and NEGATE_EXPR.

It would be great if the middle-end could hoist unary operators, such as
int f2(int x, int y)
{
    return (x > y) ? ~x : ~y;
}
[like LLVM].

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

* [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions
  2023-04-05 19:47 [Bug tree-optimization/109424] New: ~ pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-04-07 16:29 ` roger at nextmovesoftware dot com
@ 2023-04-16 18:32 ` pinskia at gcc dot gnu.org
  2023-05-08  7:38 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-16 18:32 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Depends on|                            |59424

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The patch which fixes PR 59424 also fixes this one.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59424
[Bug 59424] Optimization issue on min/max

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

* [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions
  2023-04-05 19:47 [Bug tree-optimization/109424] New: ~ pinskia at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-04-16 18:32 ` pinskia at gcc dot gnu.org
@ 2023-05-08  7:38 ` cvs-commit at gcc dot gnu.org
  2023-05-08  7:39 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-05-08  7:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS 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:6d6c17e45f62cfe0b7de502af299348fca548b01

commit r14-575-g6d6c17e45f62cfe0b7de502af299348fca548b01
Author: Andrew Pinski <apinski@marvell.com>
Date:   Thu Apr 27 12:21:54 2023 -0700

    PHIOPT: factor out unary operations instead of just conversions

    After using factor_out_conditional_conversion with diamond bb,
    we should be able do use it also for all normal unary gimple and not
    just conversions. This allows to optimize PR 59424 for an example.
    This is also a start to optimize PR 64700 and a few others.

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

    An example of this is:
    ```
    static inline unsigned long long g(int t)
    {
      unsigned t1 = t;
      return t1;
    }
    static int abs1(int a)
    {
      if (a < 0)
        a = -a;
      return a;
    }
    unsigned long long f(int c, int d, int e)
    {
      unsigned long long t;
      if (d > e)
        t = g(abs1(d));
      else
        t = g(abs1(e));
      return t;
    }
    ```

    Which should be optimized to:
      _9 = MAX_EXPR <d_5(D), e_6(D)>;
      _4 = ABS_EXPR <_9>;
      t_3 = (long long unsigned intD.16) _4;

    gcc/ChangeLog:

            * tree-ssa-phiopt.cc (factor_out_conditional_conversion): Rename to
...
            (factor_out_conditional_operation): This and add support for all
unary
            operations.
            (pass_phiopt::execute): Update call to
factor_out_conditional_conversion
            to call factor_out_conditional_operation instead.

            PR tree-optimization/109424
            PR tree-optimization/59424

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/abs-2.c: Update tree scan for
            details change in wording.
            * gcc.dg/tree-ssa/minmax-17.c: Likewise.
            * gcc.dg/tree-ssa/pr103771.c: Likewise.
            * gcc.dg/tree-ssa/minmax-18.c: New test.
            * gcc.dg/tree-ssa/minmax-19.c: New test.

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

* [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions
  2023-04-05 19:47 [Bug tree-optimization/109424] New: ~ pinskia at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-05-08  7:38 ` cvs-commit at gcc dot gnu.org
@ 2023-05-08  7:39 ` pinskia at gcc dot gnu.org
  2023-05-08  7:41 ` pinskia at gcc dot gnu.org
  2023-05-16  3:50 ` cvs-commit at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-08  7:39 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

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

* [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions
  2023-04-05 19:47 [Bug tree-optimization/109424] New: ~ pinskia at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-05-08  7:39 ` pinskia at gcc dot gnu.org
@ 2023-05-08  7:41 ` pinskia at gcc dot gnu.org
  2023-05-16  3:50 ` cvs-commit at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-08  7:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109424
Bug 109424 depends on bug 59424, which changed state.

Bug 59424 Summary: Optimization issue on min/max
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59424

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

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

* [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions
  2023-04-05 19:47 [Bug tree-optimization/109424] New: ~ pinskia at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2023-05-08  7:41 ` pinskia at gcc dot gnu.org
@ 2023-05-16  3:50 ` cvs-commit at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-05-16  3:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS 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:b06cfb62229f17eca59fa4aabf853d7e17e2327b

commit r14-868-gb06cfb62229f17eca59fa4aabf853d7e17e2327b
Author: Andrew Pinski <apinski@marvell.com>
Date:   Mon May 15 21:44:27 2023 +0000

    MATCH: [PR109424] Simplify min/max of boolean arguments

    This is version 2 of
https://gcc.gnu.org/pipermail/gcc-patches/2021-August/577394.html
    which does not depend on adding gimple_truth_valued_p at this point.
    Instead will use zero_one_valued_p which is already used for mult
simplifications
    to make sure that we only have [0,1] rather having the mistake of maybe
having [-1,0]
    as the range for signed bools.

    This shows up in a few places in GCC itself but only at -O1, we miss the
min/max conversion
    because of PR 107888 (which I will be testing seperately).

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

    Thanks,
    Andrew Pinski

            PR tree-optimization/109424

    gcc/ChangeLog:

            * match.pd: Add patterns for min/max of zero_one_valued
            values to `&`/`|`.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/bool-12.c: New test.
            * gcc.dg/tree-ssa/bool-13.c: New test.
            * gcc.dg/tree-ssa/minmax-20.c: New test.
            * gcc.dg/tree-ssa/minmax-21.c: New test.

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

end of thread, other threads:[~2023-05-16  3:50 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-05 19:47 [Bug tree-optimization/109424] New: ~ pinskia at gcc dot gnu.org
2023-04-05 19:47 ` [Bug tree-optimization/109424] ~((x > y) ? x : y) produces two not instructions pinskia at gcc dot gnu.org
2023-04-05 19:48 ` pinskia at gcc dot gnu.org
2023-04-07 16:29 ` roger at nextmovesoftware dot com
2023-04-16 18:32 ` pinskia at gcc dot gnu.org
2023-05-08  7:38 ` cvs-commit at gcc dot gnu.org
2023-05-08  7:39 ` pinskia at gcc dot gnu.org
2023-05-08  7:41 ` pinskia at gcc dot gnu.org
2023-05-16  3:50 ` cvs-commit 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).