public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "anthony.mikh at yandex dot ru" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/106804] New: Poor codegen for selecting and incrementing value behind a reference
Date: Thu, 01 Sep 2022 17:11:58 +0000	[thread overview]
Message-ID: <bug-106804-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 106804
           Summary: Poor codegen for selecting and incrementing value
                    behind a reference
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: anthony.mikh at yandex dot ru
  Target Milestone: ---

Godbolt: https://godbolt.org/z/e9ePs7Ece

For the following source code:

    void increment_largest(int& a, int& b) {
        ++(a > b ? a : b);
    }

gcc 12 with -O2 produces the following asm:

increment_largest(int&, int&):
        mov     edx, DWORD PTR [rdi]
        mov     eax, DWORD PTR [rsi]
        cmp     edx, eax
        jle     .L2
        add     edx, 1
        mov     DWORD PTR [rdi], edx
        ret
.L2:
        add     eax, 1
        mov     DWORD PTR [rsi], eax
        ret

For equivalent code using pointers:

    void increment_largest(int* a, int* b) {
        ++*(*a > *b ? a : b);
    }

gcc with -O2 gives something slightly different:

increment_largest(int*, int*):
        mov     edx, DWORD PTR [rdi]
        mov     eax, DWORD PTR [rsi]
        cmp     edx, eax
        jle     .L2
        mov     eax, edx
        mov     rsi, rdi
.L2:
        add     eax, 1
        mov     DWORD PTR [rsi], eax
        ret

If one rewrites code with references to assign the selected reference to a
variable:

    void increment_largest(int& a, int& b) {
        auto& tgt = (a > b ? a : b);
        ++tgt;
    }

it gives exactly the same asm as the version with pointers. Anyway it is
seemingly worse than what clang-14 -O2 produces for all three sources:

increment_largest(int&, int&):
        mov     eax, dword ptr [rdi]
        cmp     eax, dword ptr [rsi]
        cmovg   rsi, rdi
        add     dword ptr [rsi], 1
        ret

Likely to be related to PR94006.

             reply	other threads:[~2022-09-01 17:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-01 17:11 anthony.mikh at yandex dot ru [this message]
2022-09-01 17:17 ` [Bug middle-end/106804] " pinskia at gcc dot gnu.org
2022-09-01 17:17 ` pinskia at gcc dot gnu.org
2022-09-02  6:32 ` rguenth at gcc dot gnu.org
2022-09-02  7:18 ` amonakov at gcc dot gnu.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-106804-4@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).