From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 446B1385B835; Thu, 16 Apr 2020 11:28:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 446B1385B835 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1587036504; bh=j4DPXmr00+lLdlg2klpZeEt3NCP23AwpA0KAWOmciyg=; h=From:To:Subject:Date:From; b=n1RJWvVyqRz6f6AQNWLKtEDOcsjHkyw97z432iQBBHqKhBY956UrcU06nC3Z4QsAJ qkwz/t47V+/eKCv+ifdPW3kF4UrCcnU1LX1/HVB3USl6PLuaNLHdzlYvXUe/ICcbi+ ch3IMsXR4TNs/+q6NbOR3matmflI5B49E6TrHRZg= From: "soap at gentoo dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/94617] New: Simple if condition not optimized Date: Thu, 16 Apr 2020 11:28:24 +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: 10.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: soap at gentoo dot 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 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: Thu, 16 Apr 2020 11:28:24 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D94617 Bug ID: 94617 Summary: Simple if condition not optimized Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: soap at gentoo dot org Target Milestone: --- Given the following C++ snippet const char* vanilla_bandpass(int a, int b, int x, const char* low, const char* high) { const bool within_interval { (a <=3D x) && (x < b) }; return (within_interval ? high : low); } GCC trunk yields with -O3 -march=3Dznver2 the following assembly vanilla_bandpass(int, int, int, char const*, char const*): mov rax, r8 cmp edi, edx jg .L4 cmp edx, esi jge .L4 ret .L4: mov rax, rcx ret which is terrible. On the other hand, Clang emits vanilla_bandpass(int, int, int, char const*, char const*): cmp edx, esi cmovge r8, rcx cmp edi, edx cmovg r8, rcx mov rax, r8 ret which is a lot better. There exists an unbranched version for which I'm not 100% certain whether it's free of UB: #include const char* funky_bandpass(int a, int b, int x, const char* low, const ch= ar* high) { const bool within_interval { (a <=3D x) && (x < b) }; const auto low_ptr =3D reinterpret_cast(low) * (!within_interval); const auto high_ptr =3D reinterpret_cast(high) * within_interval; const auto ptr_sum =3D low_ptr + high_ptr; const auto* result =3D reinterpret_cast(ptr_sum); return result; } which yields funky_bandpass(int, int, int, char const*, char const*): cmp edi, edx setle al cmp edx, esi setl dl and eax, edx mov edx, eax xor edx, 1 movzx edx, dl movzx eax, al imul rcx, rdx imul rax, r8 add rax, rcx ret which is jump-free and in practice executes at the same observable rate as Clang's assembly, but still looks needlessly complex. Clang manages to comp= ile this code to the same assembly as vanilla_bandpass. Any chance of getting the optimizer ironed out for this?=