From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E55913858D3C; Thu, 1 Sep 2022 17:11:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E55913858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662052318; bh=vxWEoT+K3hEIY2KvPw5Q+J7hMlWNW5ps+e9MUUIgdJY=; h=From:To:Subject:Date:From; b=w7fx/2B1XOLugxc7qCe1OfWU4i50btbgacwLM8omj0rYrcNjEUugWbMshiCXtsWGb xzua1Y15tc9GGj6SOizQPV2zDjhkBeanGGUm+aafOiDqSkKbFqOtbNy6Kx6zL5qRTk D2UPImvy4+rJCb4XwWR649ACrsV6e+0F1FmFpJwQ= From: "anthony.mikh at yandex dot ru" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: anthony.mikh at yandex dot ru X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106804 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 =3D (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.=