public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/26366]  New: __builtin_expect needs better documentation
@ 2006-02-19 20:19 acahalan at gmail dot com
  2006-02-19 20:25 ` [Bug c/26366] " pinskia at gcc dot gnu dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: acahalan at gmail dot com @ 2006-02-19 20:19 UTC (permalink / raw)
  To: gcc-bugs

I can't tell if it would be useful to do this:

x = get_x();
__builtin_expect(x&MASK,KNOWNBITS);
__builtin_expect(x<=KNOWNMAX,1);
__builtin_expect(x>=KNOWNMIN,1);
// code which uses x would follow this

Given two booleans that are 2/3 likely, the chance of both is 4/9 likely. So
maybe it is appropriate to do this:

if(__builtin_expect((__builtin_expect(x,1) && __builtin_expect(y,1)), 0))

Yes? No?

Suppose that I am quite certain of how likely something will be, and that I
care deeply about optimization. At what point should I mark my code with
__builtin_expect()? The obvious choice is whenever the liklihood is not exactly
50%, but I hear that this varies by architecture. ARM supposedly uses a very
strong branch prediction bit that forces a major stall when the expectation is
wrong, while x86 is not so punishing. No hint of this is in the documentation.


-- 
           Summary: __builtin_expect needs better documentation
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: acahalan at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26366


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/26366] __builtin_expect needs better documentation
  2006-02-19 20:19 [Bug c/26366] New: __builtin_expect needs better documentation acahalan at gmail dot com
@ 2006-02-19 20:25 ` pinskia at gcc dot gnu dot org
  2006-02-19 21:48 ` acahalan at gmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-02-19 20:25 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2006-02-19 20:25 -------
I thought the documention said only to use __builtin_expect when you know that
in almost 100% of the time it is going to be true (or false).


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26366


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/26366] __builtin_expect needs better documentation
  2006-02-19 20:19 [Bug c/26366] New: __builtin_expect needs better documentation acahalan at gmail dot com
  2006-02-19 20:25 ` [Bug c/26366] " pinskia at gcc dot gnu dot org
@ 2006-02-19 21:48 ` acahalan at gmail dot com
  2006-02-20  2:06 ` pinskia at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: acahalan at gmail dot com @ 2006-02-19 21:48 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from acahalan at gmail dot com  2006-02-19 21:48 -------
Nope, at least if the documentation  at
http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html is what you refer to.

It would be good to document how strong the expectation is for each
architecture. Apparently the differences are dramatic.

There are the other issues too. I can't tell from the documentation if it is
useful to do as in my first example. (giving known bits and min/max) Would the
compiler remember what I told it? (if not, it should, but that needs a
different bug number)


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26366


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/26366] __builtin_expect needs better documentation
  2006-02-19 20:19 [Bug c/26366] New: __builtin_expect needs better documentation acahalan at gmail dot com
  2006-02-19 20:25 ` [Bug c/26366] " pinskia at gcc dot gnu dot org
  2006-02-19 21:48 ` acahalan at gmail dot com
@ 2006-02-20  2:06 ` pinskia at gcc dot gnu dot org
  2006-02-21  9:57 ` rguenth at gcc dot gnu dot org
  2006-02-22  3:14 ` acahalan at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-02-20  2:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2006-02-20 02:06 -------
x86 is forgiving because there is no such bit that is used (well except for the
prescott and GCC disables it because it was a wash and used up space).


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26366


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/26366] __builtin_expect needs better documentation
  2006-02-19 20:19 [Bug c/26366] New: __builtin_expect needs better documentation acahalan at gmail dot com
                   ` (2 preceding siblings ...)
  2006-02-20  2:06 ` pinskia at gcc dot gnu dot org
@ 2006-02-21  9:57 ` rguenth at gcc dot gnu dot org
  2006-02-22  3:14 ` acahalan at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-02-21  9:57 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from rguenth at gcc dot gnu dot org  2006-02-21 09:57 -------
Another way instead of

if(__builtin_expect((__builtin_expect(x,1) && __builtin_expect(y,1)), 0))

would be

if(__builtin_expect(!(x && y), 1))

I'm sure this does _not_ result in same behavior as __builtin_expect is
evaluated
quite late during compilation.  Some clarification in the docs would be really
nice.

As to standalone __builtin_expect() calls, they should have no effect.  There
was talking about a __builtin_assert() or __builtin_assume() that would force
a known value.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2006-02-21 09:57:21
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26366


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c/26366] __builtin_expect needs better documentation
  2006-02-19 20:19 [Bug c/26366] New: __builtin_expect needs better documentation acahalan at gmail dot com
                   ` (3 preceding siblings ...)
  2006-02-21  9:57 ` rguenth at gcc dot gnu dot org
@ 2006-02-22  3:14 ` acahalan at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: acahalan at gmail dot com @ 2006-02-22  3:14 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from acahalan at gmail dot com  2006-02-22 03:14 -------
The code in comment #4 really isn't the same as the original. The original is a
decorated version of this:

if(x && y)

It is meant to express that:

x is likely
y is likely
x&&y is unlikely

Here, the probability of x times the probability of y is less than 0.5 even
though the probability of x and the probability of y are both greater than 0.5.

Nothing says that standalone __builtin_expect() calls should have no effect.
This can be much more readable than using the calls in a test.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26366


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-02-22  3:14 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-19 20:19 [Bug c/26366] New: __builtin_expect needs better documentation acahalan at gmail dot com
2006-02-19 20:25 ` [Bug c/26366] " pinskia at gcc dot gnu dot org
2006-02-19 21:48 ` acahalan at gmail dot com
2006-02-20  2:06 ` pinskia at gcc dot gnu dot org
2006-02-21  9:57 ` rguenth at gcc dot gnu dot org
2006-02-22  3:14 ` acahalan at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).