branch prediction hints (was Re: patch for 2.1.55 pre-1 minix/sysv/affs)

Richard Henderson (
Fri, 12 Sep 1997 20:32:52 -0700 (PDT)

> But supporting hints about which way is the likely branch would be good.
> I'd prefer something like
> __builtin_unlikely_if (x) {
> }
> which would work fine inside #defines and inline functions etc.

Interestingly, I've discovered here at Cygnus a set of patches
(that needs lots of cleaning up) to do something very like this.
It takes the form:

x = __builtin_expect(some_int_expr, const_int_expr);
if (x) {

that is, __builtin_expect infects the rtl associated with the
value it got from some_int_expr, which may then be inspected
by a later conditional. The value of X is predicted to be
the same as const_int_expr.

It does need some amount of backend support to be effective,
and is currently only implemented for i960 and rs6000.

> Also, a "__builtin_unlikely_if()" can be used to move the unlikely code
> away from the likely case, so that it doesn't pollute the icache at all
> (into another ELF segment etc).

That is, of course, the ultimate goal, though this code only deals
with setting branch prediction bits. Due to other, extremely nasty
code generation habits of e.g. exception handling, I may look into
general motion of basic blocks, but probably not soon.