public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/45115] pure functions returning structs are not optimized.
Date: Thu, 02 Feb 2023 10:15:13 +0000	[thread overview]
Message-ID: <bug-45115-4-lie8K9EvT6@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-45115-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This affects C++20 three-way comparisons, which return trivial structs wrapping
an integer.

From PR 108635:

    #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

  parent reply	other threads:[~2023-02-02 10:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-45115-4@http.gcc.gnu.org/bugzilla/>
2023-02-02  9:43 ` pinskia at gcc dot gnu.org
2023-02-02 10:15 ` redi at gcc dot gnu.org [this message]
2024-04-09  7:06 ` pinskia at gcc dot gnu.org
2010-07-28 19:45 [Bug c/45115] New: attribute((pure)) does not work when returning structs marco at technoboredom dot net
2010-07-28 19:48 ` [Bug tree-optimization/45115] pure functions returning structs are not optimized pinskia at gcc dot gnu dot org
2010-07-28 20:05 ` rguenth 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-45115-4-lie8K9EvT6@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).