[Bug middle-end/112844] Branches under -Os (unlike -O{1, 2, 3}) do not respect __builtin_expect hints

"pinskia at gcc dot gnu.org via Gcc-bugs" <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112844

--- Comment #5 from Drea Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Petr Skocik from comment #4)
> (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.
> 
> I think this approach is abit problematic because
> (a) it fails to deliver the promised smaller code
> e.g., the following
> 
> void fn(void);
> void maybefn(int X){ if(__builtin_expect(X,0)) fn(); }
> 
> under gcc -Os codegens
> 
> maybefn:
>         testl   %edi, %edi
>         je      .L1
>         jmp     fn
> .L1:
>         ret
> 
> which is 1-jmp-larger than what you'd get if the compiler followed the hint
> (clang's codegen):
> 
> maybefn:
>         test    edi, edi
>         jne     fn@PLT
>         ret
> 

That is recorded as PR 47253 and a patch for review was submitted just
recently.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.