public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/108635] New: Redundant calls to C++ spaceship operator<=> with attribute pure or const
@ 2023-02-02  8:00 jzwinck at gmail dot com
  2023-02-02  9:36 ` [Bug tree-optimization/108635] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: jzwinck at gmail dot com @ 2023-02-02  8:00 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108635
           Summary: Redundant calls to C++ spaceship operator<=> with
                    attribute pure or const
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jzwinck at gmail dot com
  Target Milestone: ---

This code seems to have a missed optimization in GCC 10/11/12:

    #include <compare>

    struct S
    {
        std::weak_ordering operator<=>(const S&) const __attribute__((const));
    };

    int compare3way(S& a, S& b)
    {
        return (a < b) ? -1 : (a > b) ? 1 : 0;
    }

I expect operator<=> to be called once, but it is called twice.  This can be a
major missed optimization if operator<=> is expensive.  It happens regardless
of:

 1. Using attribute((const)) or attribute((pure)).
 2. Making operator<=> a free function or a member.
 3. Comparing (a > b) or (a < b) in the second ternary expression.  This is
especially strange, because it's really calling the same pure function twice,
and that's optimized correctly when the function being called is operator<
instead of operator<=>.

Clang optimizes it as expected.

Demo: https://godbolt.org/z/jP51E6xaz

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

* [Bug tree-optimization/108635] Redundant calls to C++ spaceship operator<=> with attribute pure or const
  2023-02-02  8:00 [Bug tree-optimization/108635] New: Redundant calls to C++ spaceship operator<=> with attribute pure or const jzwinck at gmail dot com
@ 2023-02-02  9:36 ` pinskia at gcc dot gnu.org
  2023-02-02  9:38 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-02-02  9:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I suspect you need noexcept too. Otherwise you could in theory have an
exception.

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

* [Bug tree-optimization/108635] Redundant calls to C++ spaceship operator<=> with attribute pure or const
  2023-02-02  8:00 [Bug tree-optimization/108635] New: Redundant calls to C++ spaceship operator<=> with attribute pure or const jzwinck at gmail dot com
  2023-02-02  9:36 ` [Bug tree-optimization/108635] " pinskia at gcc dot gnu.org
@ 2023-02-02  9:38 ` redi at gcc dot gnu.org
  2023-02-02  9:42 ` pinskia at gcc dot gnu.org
  2023-02-02  9:43 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2023-02-02  9:38 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-02-02
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
GCC still makes two calls even if it's noexcept.

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

* [Bug tree-optimization/108635] Redundant calls to C++ spaceship operator<=> with attribute pure or const
  2023-02-02  8:00 [Bug tree-optimization/108635] New: Redundant calls to C++ spaceship operator<=> with attribute pure or const jzwinck at gmail dot com
  2023-02-02  9:36 ` [Bug tree-optimization/108635] " pinskia at gcc dot gnu.org
  2023-02-02  9:38 ` redi at gcc dot gnu.org
@ 2023-02-02  9:42 ` pinskia at gcc dot gnu.org
  2023-02-02  9:43 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-02-02  9:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=45115

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Then it might be a dup of bug 45115

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

* [Bug tree-optimization/108635] Redundant calls to C++ spaceship operator<=> with attribute pure or const
  2023-02-02  8:00 [Bug tree-optimization/108635] New: Redundant calls to C++ spaceship operator<=> with attribute pure or const jzwinck at gmail dot com
                   ` (2 preceding siblings ...)
  2023-02-02  9:42 ` pinskia at gcc dot gnu.org
@ 2023-02-02  9:43 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-02-02  9:43 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Yes it is a dup.

*** This bug has been marked as a duplicate of bug 45115 ***

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

end of thread, other threads:[~2023-02-02  9:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-02  8:00 [Bug tree-optimization/108635] New: Redundant calls to C++ spaceship operator<=> with attribute pure or const jzwinck at gmail dot com
2023-02-02  9:36 ` [Bug tree-optimization/108635] " pinskia at gcc dot gnu.org
2023-02-02  9:38 ` redi at gcc dot gnu.org
2023-02-02  9:42 ` pinskia at gcc dot gnu.org
2023-02-02  9:43 ` 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).