This is the mail archive of the libc-hacker@sourceware.cygnus.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

__builtin_expect


You might have seen that I've added in the last week several more
__builtin_expect "calls" to the sources.  Most of you will probably
see that this does not change anything since __builtin_expect is
defined for you as a macro which is appsing down the first argument.

But that need not be so.  If you use the current mainline gcc you'll
get the nifty code rth implemented last weekend which adds
__builtin_expect to the official gcc. It was before an ugly hack in
Cygnus's internal compiler which could not possibly be ported over.

__builtin_expect has the prototype

	long int __builtin_expect (long int exp, long int val)

and it means that almost certainly the value of `exp' is `val'.  This
probability value will then be used in condition expressions.

The use of __builtin_expect has now two uses:

- on platforms where conditional jump instructions have prediction bits this
  information can be used to set them

- on all architectures it should now be possible to use
	-freorder-blocks
  to use the information to put the basic blocks in an order which guarantees
  an optimal straight pass through the code for the normal case.


In glibc this means __builtin_expect should be used everywhere the
results of comparisons etc is statically predictable.  At the very
least it should be used for error handling.  E.g.;

	ptr = malloc (size);
	if (ptr == NULL)
	  {
	    ... some work ...
	    return -1;
	  }

should be written as

	ptr = malloc (size);
	if (__builtin_expect (ptr == NULL, 0))
	  {
	    ... some work ...
	    return -1;
	  }

This will (normally) ensure that the basic block for the `then' block
is put out of line and does not disturb the normal execution (and
keeps the icache free of garbage).

What you can see as well in the example is how to handle expression
which are not of `int' or `long int' value.  Simply put the whole
comparison in the first argument and you get an expression of type `int'.

-- 
---------------.      drepper at gnu.org  ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]