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

            Bug ID: 58480
           Summary: Use attribute((nonnull)) to optimize callers
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: glisse at gcc dot gnu.org

Hello,

in this first function, gcc knows that p is not zero and removes the test (I
have to make sure q doesn't disappear too early, or the compiler forgets that p
is dereferenced, maybe when vrp information becomes persistent...):

int f(int*p){
  int q=*p;
  if(!p) __builtin_abort();
  return q;
}

However, in the following:

void g(int*p,int*q){
  __builtin_memcpy(q,p,4);
  if(!p) __builtin_abort();
}

the test isn't removed, although calling memcpy with a zero argument is just as
bad a dereferencing 0.

Replacing __builtin_memcpy with this doesn't help either:

extern void * my_memcpy (void *dest, const void *src, unsigned long len)
__attribute__((nonnull));

However, this is optimized:

__attribute__((nonnull))void g(int*p){
  if(!p) __builtin_abort();
}

So I think gcc should make use of the nonnull attribute for optimization
purposes not just inside the function, but in the callers as well (and make
sure whatever we do for the nonnull attribute applies to __builtin_memcpy as
well).

Reply via email to