From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id CFD43384F9A4; Mon, 4 Dec 2023 12:39:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CFD43384F9A4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1701693557; bh=en21JcnCUhjLjXC1yD2ODP2bd1HNB4Lf9tbuf94uMN4=; h=From:To:Subject:Date:From; b=cioqv0hQgCdLyI/GvZo9M67EJRRvaD5olQTSbQMnco1mYdDQZiuj5Cl776BnhEbgN RprDRkIIt7GKfxPEfWEhtkFyMdNLulPXmbXc2KW3aEbz0vFP+LHv7YZ6TObYtE6PMg I7GzEvMuGsNYx0bo7NfCLMLaadqi8ABWk0+j8x28= From: "pskocik at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/112844] New: Branches under -Os (unlike -O{1,2,3}) do not respect __builtin_expect hints Date: Mon, 04 Dec 2023 12:39:17 +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: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: pskocik 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 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=3D112844 Bug ID: 112844 Summary: Branches under -Os (unlike -O{1,2,3}) do not respect __builtin_expect hints Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: pskocik at gmail dot com Target Milestone: --- A simple example that demonstrates this is: int test(void); void yes(void); void expect_yes(void){ if (__builtin_expect(test(),1)) yes(); else {} } void expect_no(void){ if (__builtin_expect(test(),0)) yes(); else {} } For an optimized x86-64 output, one should expect: -a fall-through to a yes() tailcall for the expect_yes() case, preceded = by a conditional jump to code doing a plain return -a fall-through to a plain return for the expect_no() case, preceded by a conditional jump to a yes() tailcall (or even more preferably: a conditional-taicall to yes() with the needed stack adjustment done once bef= ore the test instead of being duplicated in each branch after the test) Indeed, that's how gcc lays it out for -O{1,2,3} (https://godbolt.org/z/rG3P3d6f7) as does clang at -O{1,2,3,s} (https://godbolt.org/z/EcKbrn1b7) and icc at -O{1,2,3,s} (https://godbolt.org/z/Err73eGsb). But gcc at -Os seems to have a very strong preference to falling through to call yes() even in=20 void expect_no(void){ if (__builtin_expect(test(),0)) yes(); else {} } and even in void expect_no2(void){ if (__builtin_expect(!test(),1)){} else yes(); } essentially completely disregarding any user attempts at controlling the br= anch layout of the output.=