public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* attribute nonnull/expect_nonnull for return value?
@ 2012-03-24  8:40 Daniel Frey
  2012-03-26  3:17 ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Frey @ 2012-03-24  8:40 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1327 bytes --]

Given a function declaration

  void* f( void* a, void* b ) __attribute__(( nonnull( 1, 2 ) ));

GCC now knows that the arguments are nonnull, but how to I tell the compiler that the returned pointer is nonnull? Or if null is allowed but only the exception to the rule, I would like to tell the compiler that is should *expect* the pointer to be nonnull, but not disallow null. Actually, the second case is more important to me, but I think both should be possible.

Rationale:

Writing a library, the function's definition goes into a separate compile unit and I would like to allow the user's code to be optimized automatically. Currently, in cases where the optimization matters, the user is required to do the work manually:

  T* p = (T*)f( a, b );
  if( __builtin_expect( !p, false ) ) {
    // ...don't optimize for this case...
  }
  return p;

This is not a good solution, the library/API should take care of it. In fact, I think that this applies to an awful lot of functions (think system calls) and could really help to optimize user programs. If this is currently not possible, would it be possible that it will be added in future versions of GCC? Seeing __attribute__(( malloc )) exists, the infrastructure seems to be there, only that malloc does/guarantees way too much...

Regards, Daniel


[-- Attachment #2: Message signed with OpenPGP using GPGMail --]
[-- Type: application/pgp-signature, Size: 203 bytes --]

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

* Re: attribute nonnull/expect_nonnull for return value?
  2012-03-24  8:40 attribute nonnull/expect_nonnull for return value? Daniel Frey
@ 2012-03-26  3:17 ` Ian Lance Taylor
  2012-03-26  7:30   ` Marc Glisse
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Lance Taylor @ 2012-03-26  3:17 UTC (permalink / raw)
  To: Daniel Frey; +Cc: gcc-help

Daniel Frey <d.frey@gmx.de> writes:

> GCC now knows that the arguments are nonnull, but how to I tell the
> compiler that the returned pointer is nonnull? Or if null is allowed
> but only the exception to the rule, I would like to tell the compiler
> that is should *expect* the pointer to be nonnull, but not disallow
> null. Actually, the second case is more important to me, but I think
> both should be possible.

At present, as far as I know, there is no way to do either.

Ian

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

* Re: attribute nonnull/expect_nonnull for return value?
  2012-03-26  3:17 ` Ian Lance Taylor
@ 2012-03-26  7:30   ` Marc Glisse
  0 siblings, 0 replies; 3+ messages in thread
From: Marc Glisse @ 2012-03-26  7:30 UTC (permalink / raw)
  To: gcc-help; +Cc: Daniel Frey

On Sun, 25 Mar 2012, Ian Lance Taylor wrote:

> Daniel Frey <d.frey@gmx.de> writes:
>
>> GCC now knows that the arguments are nonnull, but how to I tell the
>> compiler that the returned pointer is nonnull? Or if null is allowed
>> but only the exception to the rule, I would like to tell the compiler
>> that is should *expect* the pointer to be nonnull, but not disallow
>> null. Actually, the second case is more important to me, but I think
>> both should be possible.
>
> At present, as far as I know, there is no way to do either.

To completely disallow null return values, you could write an inline 
wrapper that forwards its arguments to the real function and dereferences 
the return pointer before returning it. IIRC, gcc gets the message that a 
pointer that is dereferenced earlier can be assumed to be nonnull.

For a simple expectation, I can't think of a good way, although I believe 
that in the absence of contrary information, the compiler considers 
nonnull pointers more likely than null, for branch prediction and stuff 
(but maybe not as strongly as if you had a __builtin_expect).

-- 
Marc Glisse

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

end of thread, other threads:[~2012-03-26  7:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-24  8:40 attribute nonnull/expect_nonnull for return value? Daniel Frey
2012-03-26  3:17 ` Ian Lance Taylor
2012-03-26  7:30   ` Marc Glisse

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