From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id A8302398303C; Tue, 3 Nov 2020 10:28:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A8302398303C From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/97690] New: (cond ? 2 : 0) is not optimized to int(cond) << 1 Date: Tue, 03 Nov 2020 10:28:49 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 Nov 2020 10:28:49 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D97690 Bug ID: 97690 Summary: (cond ? 2 : 0) is not optimized to int(cond) << 1 Product: gcc Version: 11.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: redi at gcc dot gnu.org Target Milestone: --- enum E { init=3D0, active=3D1, done=3D2 }; int f(bool d) { return d ? done : init; } int g(bool d) { return int(d) << 1; } The first function is more readable and less fragile (in case the order of = bits is changed) but GCC produces larger code for it. For x86_64 with -O3: f(bool): xor eax, eax test dil, dil setne al add eax, eax ret g(bool): movzx eax, dil add eax, eax ret And for x86_64 -Os: f(bool): neg dil sbb eax, eax and eax, 2 ret g(bool): movzx eax, dil add eax, eax ret Clang produces the same code for both functions at all optimization levels: f(bool): # @f(bool) lea eax, [rdi + rdi] ret g(bool): # @g(bool) lea eax, [rdi + rdi] ret=