[Bug tree-optimization/83907] New: missing strlen optimization for non-zero memset followed by a nul byte store

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Jan 16 20:28:00 GMT 2018


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

            Bug ID: 83907
           Summary: missing strlen optimization for non-zero memset
                    followed by a nul byte store
           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: ---

The memset call in the function below together with the store of the
terminating NUL byte create a string of known length that the strlen pass could
determine but doesn't.  This is a potential (albeit minor) optimization
opportunity that could be exploited.

$ cat z.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout z.c
extern char a[];

void f (void)
{
  __SIZE_TYPE__ n = 12;

  __builtin_memset (a, 'x', n);
  a[n] = '\0';

  if (__builtin_strlen (a) != n)   // can be folded to false
    __builtin_abort ();
}

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

f ()
{
  long unsigned int _1;

  <bb 2> [100.00%]:
  __builtin_memset (&a, 120, 12);
  a[12] = 0;
  _1 = __builtin_strlen (&a);
  if (_1 != 12)
    goto <bb 3>; [0.04%]
  else
    goto <bb 4>; [99.96%]

  <bb 3> [0.04%]:
  __builtin_abort ();

  <bb 4> [99.96%]:
  return;

}


More information about the Gcc-bugs mailing list