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

            Bug ID: 81892
           Summary: suboptimal code for a if (p) free(p) else free(p)
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

As mentioned in bug 80528, comment 2, GCC generates suboptimal code for the
following test case.  It would be more optimal to emit an unconditional jump to
free as Clang and ICC do.

Separately it might also be worth considering eliminating the 'if (p)' test
altogether, even in cases with no else clause.  I.e., using
-fdelete-null-pointer-checks to transform 'if (p) free (p)' to a call to an
unconditional free (p).  Since (in the absence of any information to the
contrary) a pointer passed to free() is far less likely to be null than not,
keeping the test only makes the code bigger and slower.

$ cat b.c && gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout  b.c
void free (void*);

void g (void *p)
{
  if (p)
    free (p);
  else
    free (p);
}


;; Function g (g, funcdef_no=0, decl_uid=1817, cgraph_uid=0, symbol_order=0)

Removing basic block 5
g (void * p)
{
  <bb 2> [100.00%] [count: INV]:
  if (p_2(D) != 0B)
    goto <bb 3>; [53.47%] [count: INV]
  else
    goto <bb 4>; [46.53%] [count: INV]

  <bb 3> [53.47%] [count: INV]:
  free (p_2(D)); [tail call]

  <bb 4> [100.00%] [count: INV]:
  return;

}

Reply via email to