From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C601E38D4700; Wed, 5 Jun 2024 00:53:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C601E38D4700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1717548808; bh=EQYALM/MfRczDgRV/cZwui6tB1KcZRH9zktOfqUB6qo=; h=From:To:Subject:Date:From; b=UJToWGO5+sUD9PcuA2mRvIe0T0qJRPHKCCSHDyXD7KnUc4emhA/0dutxqf9tQcdq3 mhogaGsnvD3GlP9eMoeOserZGXdy6Y/d6qVEZODrRmaaNehRb2LrTJZPx2D1l7QVeS qxgHGOb/x4KF4VWl3IJugXSqbMdiGPcJG4+Qt8eM= From: "zhiwuyazhe154 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/115350] New: Missing optimzation: fold `n = std::min(f ? 0 : 3, -a)` to `n = -a` Date: Wed, 05 Jun 2024 00:53:28 +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: 15.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: zhiwuyazhe154 at gmail dot com 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 keywords 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=3D115350 Bug ID: 115350 Summary: Missing optimzation: fold `n =3D std::min(f ? 0 : 3, -a)` to `n =3D -a` Product: gcc Version: 15.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: zhiwuyazhe154 at gmail dot com Target Milestone: --- Godbolt Example: https://godbolt.org/z/j9rj5q6W5 Code Example: unsigned short n; #include void fn1( unsigned short f, unsigned char a) { n =3D std::min(f ? 0 : 3, -a); // equals to "n =3D -a" } During the operation, -a will be converted to int type, and its value range= is [-255, 0], which must be less than 0 or 3, so =E2=80=9Cn =3D std::min(f ? 0= : 3, -a)=E2=80=9D is equivalent to "n =3D -a". GCC -O3: fn1(unsigned short, unsigned char): test di, di je .L5 xor eax, eax test sil, sil jne .L5 mov WORD PTR n[rip], ax ret .L5: movzx eax, sil neg eax mov WORD PTR n[rip], ax ret n: .zero 2 Expected code (CLANG -O3): fn1(unsigned short, unsigned char):=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20 neg esi mov word ptr [rip + n], si ret n: .short 0=