public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/112844] New: Branches under -Os (unlike -O{1,2,3}) do not respect __builtin_expect hints
@ 2023-12-04 12:39 pskocik at gmail dot com
  2023-12-04 12:52 ` [Bug c/112844] " jakub at gcc dot gnu.org
  2024-03-30 14:10 ` [Bug middle-end/112844] " pskocik at gmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: pskocik at gmail dot com @ 2023-12-04 12:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112844

            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 before
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 

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 branch
layout of the output.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/112844] Branches under -Os (unlike -O{1,2,3}) do not respect __builtin_expect hints
  2023-12-04 12:39 [Bug c/112844] New: Branches under -Os (unlike -O{1,2,3}) do not respect __builtin_expect hints pskocik at gmail dot com
@ 2023-12-04 12:52 ` jakub at gcc dot gnu.org
  2024-03-30 14:10 ` [Bug middle-end/112844] " pskocik at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-12-04 12:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112844

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
With -Os you ask the code to be small.  So, while internally the hint is still
present in edge probabilities, -Os is considered more important and certain
code changes based on the probabilities aren't done if they are known or
expected to result in larger code.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug middle-end/112844] Branches under -Os (unlike -O{1,2,3}) do not respect __builtin_expect hints
  2023-12-04 12:39 [Bug c/112844] New: Branches under -Os (unlike -O{1,2,3}) do not respect __builtin_expect hints pskocik at gmail dot com
  2023-12-04 12:52 ` [Bug c/112844] " jakub at gcc dot gnu.org
@ 2024-03-30 14:10 ` pskocik at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: pskocik at gmail dot com @ 2024-03-30 14:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112844

--- Comment #2 from Petr Skocik <pskocik at gmail dot com> ---
(In reply to Jakub Jelinek from comment #1)
> With -Os you ask the code to be small.  So, while internally the hint is
> still present in edge probabilities, -Os is considered more important and
> certain code changes based on the probabilities aren't done if they are
> known or expected to result in larger code.

Thanks. I very much like the codegen I get with gcc -Os, often better than what
I get with clang. But the sometimes counter-obvious branch layout at -Os is
annoying to me, especially considering I've measured it a couple of times as
being the source of a slowdown.
Sure you can save a (most-often-than not 2-byte) jump by conditionally jumping
over an unlikely branch instead of conditionally jumping to an unlikely branch
placed after ret and having it jump back in the function body (the latter is
what all the other compilers do at -Os), but I'd rather have the code spend the
extra two bytes and have my happy paths be fall-through as they should be.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-03-30 14:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-04 12:39 [Bug c/112844] New: Branches under -Os (unlike -O{1,2,3}) do not respect __builtin_expect hints pskocik at gmail dot com
2023-12-04 12:52 ` [Bug c/112844] " jakub at gcc dot gnu.org
2024-03-30 14:10 ` [Bug middle-end/112844] " pskocik at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).