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

            Bug ID: 71650
           Summary: unnecessary call to __memcpy_chk emitted on a bounded
                    copy
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

Object Size Checking functions like __builtin___memcpy_chk apparently don't
take full advantage of the value range information available with the Value
Range Optimization and unnecessarily result in calls to the runtime checking
functions even in cases when the calls are provably safe.

For example, in the program below, the call to memcpy is bounded by the size of
the destination object yet GCC still emits a call to __memcpy_chk when it could
instead fold the call.

$ cat memcpy.c && /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -O2
-S -Wall -Wextra -fdump-tree-optimized=/dev/stdout memcpy.ctypedef
__SIZE_TYPE__ size_t;

char buf [13];

void f (void *p, size_t n)
{
  if (n <= sizeof buf)
    __builtin___memcpy_chk (buf, p, n, sizeof buf);
}

;; Function f (f, funcdef_no=0, decl_uid=1754, cgraph_uid=0, symbol_order=1)

Removing basic block 5
f (void * p, size_t n)
{
  <bb 2>:
  if (n_2(D) <= 13)
    goto <bb 3>;
  else
    goto <bb 4>;

  <bb 3>:
  __builtin___memcpy_chk (&buf, p_4(D), n_2(D), 13); [tail call]

  <bb 4>:
  return;

}

Reply via email to