This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug tree-optimization/66573] Unexpected change in static, branch-prediction cost from O1 to O2 in if-then-else.
- From: "segher at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 21 Nov 2015 19:27:44 +0000
- Subject: [Bug tree-optimization/66573] Unexpected change in static, branch-prediction cost from O1 to O2 in if-then-else.
- Auto-submitted: auto-generated
- References: <bug-66573-4 at http dot gcc dot gnu dot org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66573
--- Comment #8 from Segher Boessenkool <segher at gcc dot gnu.org> ---
(In reply to Joshua Green from comment #7)
> and I see no reason why expecting the "else" block should a priori be
> preferable in either case.
GCC does some fairly involved prediction (in predict.c). It isn't
"a priori".
> (It's also not clear HOW this could be "faster
> on essentially all processors"
Fall-through is faster than branching in most cases. Most CPUs have
some kind of pipelining on instruction fetch.
> Of course, the compiler is
> free to make whatever guess it wants, but it would be nice if the programmer
> had some portable way of expressing his/her own expectations,
There is __builtin_expect, if that is portable enough for you. You
can wrap it with some macros if you need it to be more portable.
> and it seems
> that other compilers provide that by "agreeing" to expect the "if" block
> (as, indeed, various online articles recommend).
I much doubt such trivial heuristics work better. Feel free to
prove me wrong :-)