Re: likely and unlikely was: Re: [PATCH] split xfs_ioc_xattr

David Chinner
2008-04-21T21:41:54Z

On Mon, Apr 21, 2008 at 09:55:58AM +0200, Andi Kleen wrote:
> On Mon, Apr 21, 2008 at 10:33:43AM +1000, David Chinner wrote:
> > On Fri, Apr 18, 2008 at 09:05:25AM -0500, Eric Sandeen wrote:
> > > ISTR that the dir2 code on Irix had tons of compiler pragmas for likely
> > > and unlikely paths, and that it actually was well-profiled and tested.
> > > Did that ever get translated into Linux hints?
> >
> > The Irix code (#pragma mips_frequency_hint [FREQUENT|NEVER|INIT])
> > only controllered physical placement of code, it never issued
> > branch hints.
>
> likely/unlikely control placement too and I think it is actually
> more important for kernel code than branch hints (which x86 doesn't
> have explictely unlike ia64, they only way to do a branch hint is to reorder
> the code) due to icache effects.
>
> i.e. a "never" block gets moved out of line,
> > while a "frequent" block is left inline. Realistically, the way
> > this is used in the Irix dir2 code is completely useless - code like
> > this:
>
> Indeed because gcc has a heuristic that early returns are considered
> unlikely:
>
> /* Branch causing function to terminate is probably not taken. */
> DEF_PREDICTOR (PRED_TREE_EARLY_RETURN, "early return (on trees)", HITRATE (61),
> 0)
>
> It's only 61%, but that should be enough.
>
> I'm somewhat surprised that MipsPro didn't have such a default heuristic
> though ...

Different heuristics. IIRC the "true" branch of the if was typically
put inline so you could implicitly code your logic to get the
compiler to do the right thing....

And with the branch delay slot, things like:

if (error)
return error;

translate into three instructions:

cmp error, r0
jne return_prelude
mov v0, error <<< BDS

Seeing as the BDS is only executed when the jump occurs and v0 holds
the function return value, there's no out of line code to bother
hinting about....

Cheers,

Dave.
--
Dave Chinner
Principal Engineer
SGI Australian Software Group