public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: __builtin_expect for indirect function calls
@ 2008-01-06 20:42 Ross Ridge
  0 siblings, 0 replies; 13+ messages in thread
From: Ross Ridge @ 2008-01-06 20:42 UTC (permalink / raw)
  To: gcc

Mark Mitchell writes:
> What do people think?  Do we have the leeway to change this?

If it were just cases where using __builtin_expect is pointless that
would break, like function overloading and sizeof then I don't think
it would be a problem.  However, it would change behaviour when C++
conversion operators are used and I can see these being legitimately
used with __builtin_expect.  Something like:

	struct C {
		operator long();
	};

	int foo() {
		if (__builtin_expect(C(), 0))
			return 1;
		return 2;
	}

If cases like these are rare enough it's probably an acceptable change
if they give an error because the argument types don't match.

					Ross Ridge

^ permalink raw reply	[flat|nested] 13+ messages in thread
* __builtin_expect for indirect function calls
@ 2007-12-18  0:52 trevor_smigiel
  2007-12-18  2:27 ` Jonathan Adamczewski
  2007-12-22  3:42 ` Hans-Peter Nilsson
  0 siblings, 2 replies; 13+ messages in thread
From: trevor_smigiel @ 2007-12-18  0:52 UTC (permalink / raw)
  To: gcc; +Cc: Ulrich Weigand, Russell_Olsen

Hi,

I'm looking for comments on a possible GCC extensions described below.

For the target I'm interested in, Cell SPU, taken branches are only
predicted correctly by explicitly inserting a specific instructions (a
hint instruction) that says "the branch at address A is branching to
address B".  This allows the processor to prefetch the instructions at
B, potentially with no penalty.

For indirect function calls, the ideal case is we know the target soon
enough at run-time that the hint instruction simply specifies the real
target.  Soon enough means about 18 cycles before the execution of the
branch.  I don't have any numbers as to how often this happens, but
there are enough cases where it doesn't.

When we can't hint the real target, we want to hint the most common
target.   There are potentially clever ways for the compiler to do this
automatically, but I'm most interested in giving the user some way to do
it explicitly.  One possiblity is to have something similar to
__builtin_expect, but for functions.  For example, I propose:

  __builtin_expect_call (FP, PFP)

which returns the value of FP with the same type as FP, and tells the
compiler that PFP is the expected target of FP.  Trival examples:

  typedef void (*fptr_t)(void);

  extern void foo(void);

  void
  call_fp (fptr_t fp)
  {
    /* Call the function pointed to by fp, but predict it as if it is
       calling foo() */
    __builtin_expect_call (fp, foo)();
  }

  void
  call_fp_predicted (fptr_t fp, fptr_t predicted)
  {
    /* same as above but the function we are calling doesn't have to be
       known at compile time */
    __builtin_expect_call (fp, predicted)();
  }

I believe I can add this just for the SPU target without effecting
anything else, but it could be useful for other targets.

Are there any comments about the name, semantics, or usefulness of this
extension?

Thanks,
Trevor


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

end of thread, other threads:[~2008-01-08 15:51 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-06 20:42 __builtin_expect for indirect function calls Ross Ridge
  -- strict thread matches above, loose matches on Subject: below --
2007-12-18  0:52 trevor_smigiel
2007-12-18  2:27 ` Jonathan Adamczewski
2007-12-22  3:42 ` Hans-Peter Nilsson
2007-12-26 19:10   ` Mark Mitchell
2008-01-03 23:36     ` trevor_smigiel
2008-01-05  5:44       ` Mark Mitchell
2008-01-05 10:40         ` Richard Guenther
2008-01-06 19:44           ` Mark Mitchell
2008-01-07 21:15             ` Mark Mendell
2008-01-08 15:36               ` Dave Korn
2008-01-08 15:51                 ` Dave Korn
2008-01-03 23:46   ` trevor_smigiel

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).