Bug 20318 - RFE: add attribute to specify that a function never returns NULL
Summary: RFE: add attribute to specify that a function never returns NULL
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.0.0
: P2 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, missed-optimization
Depends on:
Blocks: 19476 21856 24242 24825
  Show dependency treegraph
 
Reported: 2005-03-04 15:32 UTC by Joe Orton
Modified: 2018-05-05 12:20 UTC (History)
8 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-04-08 01:23:55


Attachments
Pre-processed SVN code. (52.39 KB, text/plain)
2005-03-04 17:58 UTC, Diego Novillo
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Joe Orton 2005-03-04 15:32:43 UTC
It would be useful to have a function attribute which specifies that the
function never returns NULL.

Currently the GCC 4 snapshots with -O2 -Wall generate ~15 spurious warnings in
the Subversion source code which could be eliminated if a couple of functions
could be marked as never returning NULL.
Comment 1 Andrew Pinski 2005-03-04 15:38:22 UTC
Confirmed, PR 19476 is case which depends on this.
Comment 2 Giovanni Bajo 2005-03-04 17:53:43 UTC
I don't object that this feature is indeed needed, but I would still like to 
see a reduced testcase from Subversion which shows a bogus warning that could 
be fixed with this attribute.

BTW, Diego, once ASSERT_EXPR becomes a generic node, can't just the C++ FE uses 
it to assert that the return value for a CALL_EXPR to an allocation function is 
non-zero?
Comment 3 Diego Novillo 2005-03-04 17:58:27 UTC
Created attachment 8331 [details]
Pre-processed SVN code.
Comment 4 Diego Novillo 2005-03-04 17:59:50 UTC
Subject: Re:  RFE: add attribute to specify that a function never
 returns NULL

giovannibajo at libero dot it wrote:
> ------- Additional Comments From giovannibajo at libero dot it  2005-03-04 17:53 -------
> I don't object that this feature is indeed needed, but I would still like to 
> see a reduced testcase from Subversion which shows a bogus warning that could 
> be fixed with this attribute.
> 
I have attached the pre-processed test case from Joe.

> BTW, Diego, once ASSERT_EXPR becomes a generic node, can't just the C++ FE uses 
> it to assert that the return value for a CALL_EXPR to an allocation function is 
> non-zero?
> 
The attribute would be enough.  The FE need only set an ECF_ flag to the 
CALL_EXPR and the optimizers will pick it up from there.


Diego.
Comment 5 Diego Novillo 2005-03-04 18:01:01 UTC
Adding some of the early analysis that went on in private mail.


> So is there an __attribute__ that we can use to mark that function to
> always return non-NULL, so the compiler knows that path is not possible?
>
No.  There is __attribute__ ((noreturn)), but the compiler has no way of knowing
that svn_fs_fs__id_parse is guaranteed to return non-NULL.

This is what the compiler sees in the IL:

  root_id_41 = svn_fs_fs__id_parse (node_id_str_39, D.10885_40, pool_5);
  if (root_id_41 == 0B) goto <L13>; else goto <L17>;

<L13>:;
  svn_error__locate (...);
  D.10887_45 = dgettext (...);
  svn_err__temp_46 = svn_error_create (160004, 0B, D.10887_45);

  # root_id_257 = PHI <root_id_28(10), root_id_41(8)>;
<L17>:;
  [ ... ]
<L19>:;
  *root_id_p_30 = root_id_257;


At L19, it sees two possible values for root_id (root_id_28 and root_id_41). 
root_id_28 is the uninitialized value.

We don't have a "can't return NULL" attribute.  Though it would be useful.  In
this case, you're better off initializing *root_id_p at the start of the function.


Diego.
Comment 6 Andrew Pinski 2005-06-03 14:20:19 UTC
Here is a reduced testcase for the SVN code:
void *g();

static void *count_and_verify_instructions(int *ninst,const unsigned char *p)
{
   if (p == ((void *)0))
    return g();
 *ninst = 0;
 return 0;
}
void f(int);
void *decode_window(const unsigned char *data)
{
  int ninst;
  void *svn_err__temp = count_and_verify_instructions(&ninst, data);
  if (svn_err__temp)
    return svn_err__temp;

  f (ninst);
  return 0;
}
----- cut ---------
g returns only non null values.

This reduced testcase comes from PR 21320 which comes from OLH.
Comment 7 Anthony Green 2005-11-11 23:29:48 UTC
This feature would also be useful for java, as we can eliminate certain inlined null pointer checks when we know that a method can't return null.
See http://gcc.gnu.org/ml/java/2005-11/msg00124.html
Comment 8 Andrew Pinski 2005-11-12 19:26:18 UTC
I have a patch already.
Comment 9 Andrew Pinski 2006-01-09 18:34:04 UTC
No longer working on this, I am too busy working on the gfortran front-end.
Comment 10 Marc Glisse 2013-10-09 13:03:14 UTC
Author: glisse
Date: Wed Oct  9 13:03:13 2013
New Revision: 203316

URL: http://gcc.gnu.org/viewcvs?rev=203316&root=gcc&view=rev
Log:
2013-10-09  Marc Glisse  <marc.glisse@inria.fr>

	PR tree-optimization/20318
gcc/c-family/
	* c-common.c (handle_returns_nonnull_attribute): New function.
	(c_common_attribute_table): Add returns_nonnull.

gcc/
	* doc/extend.texi (returns_nonnull): New function attribute.
	* fold-const.c (tree_expr_nonzero_warnv_p): Look for returns_nonnull
	attribute.
	* tree-vrp.c (gimple_stmt_nonzero_warnv_p): Likewise.
	(stmt_interesting_for_vrp): Accept all GIMPLE_CALL.

gcc/testsuite/
	* c-c++-common/pr20318.c: New file.
	* gcc.dg/tree-ssa/pr20318.c: New file.

Added:
    trunk/gcc/testsuite/c-c++-common/pr20318.c   (with props)
    trunk/gcc/testsuite/gcc.dg/tree-ssa/pr20318.c   (with props)
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-common.c
    trunk/gcc/doc/extend.texi
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-vrp.c

Propchange: trunk/gcc/testsuite/c-c++-common/pr20318.c
            ('svn:eol-style' added)

Propchange: trunk/gcc/testsuite/c-c++-common/pr20318.c
            ('svn:keywords' added)

Propchange: trunk/gcc/testsuite/gcc.dg/tree-ssa/pr20318.c
            ('svn:eol-style' added)

Propchange: trunk/gcc/testsuite/gcc.dg/tree-ssa/pr20318.c
            ('svn:keywords' added)
Comment 11 Marc Glisse 2013-10-11 11:48:06 UTC
Marking svn_fs_fs__id_parse and svn_error_create with the new __attribute__((__returns_nonnull__)) makes the warning about root_id disappear at -O3, so I am going to mark this bug as fixed, please reopen if necessary.