https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80519

            Bug ID: 80519
           Summary: if(p)free(p) with -Os
           Product: gcc
           Version: 8.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
  Target Milestone: ---

It is quite common to end up with a call to free protected by a check that the
pointer is not 0. This can be done because of old implementations of free that
did not handle 0 properly (I certainly hope those have been gone for a while),
for optimization if the 0 case is common, because of abstraction (C++
(de)allocators are not generally specified to accept 0, so we protect the call
even when the allocator is the default one), etc.

I propose to optimize out the check in some cases, and particularly when
optimizing for size. It might also make sense to remove the check when the
frequency of the shortcut is too low.

void f(void*p){
  if(p)
    __builtin_free(p);
}

Reply via email to