public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106804] New: Poor codegen for selecting and incrementing value behind a reference
@ 2022-09-01 17:11 anthony.mikh at yandex dot ru
  2022-09-01 17:17 ` [Bug middle-end/106804] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: anthony.mikh at yandex dot ru @ 2022-09-01 17:11 UTC (permalink / raw)
  To: gcc-bugs

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.

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

end of thread, other threads:[~2022-09-02  7:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-01 17:11 [Bug c++/106804] New: Poor codegen for selecting and incrementing value behind a reference anthony.mikh at yandex dot ru
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

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).