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

            Bug ID: 83190
           Summary: missing strlen optimization of the empty string
           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 tree-ssa-strlen pass keeps track of the lengths of strings as they are
created, including by initialization of local arrays.  As a result, it is able
to determine that the result of the strlen() call in function f() below is 0
and perform this substitution.  But the pass misses that the same is true in
function g(), and so it unnecessarily emits a call to strlen() there.  It
should be possible to enhance the pass to handle this case as well and perform
the same transformation in both cases.

$ cat a.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout a.c
int f (void)
{
  char a[] = "012\0";

  return __builtin_strlen (a + 3);   // folded into 0
}

int g (void)
{
  char a[] = "012\0";

  return __builtin_strlen (a + 4);   // not folded
}

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

f ()
{
  <bb 2> [local count: 1073741825]:
  return 0;

}



;; Function g (g, funcdef_no=1, decl_uid=1896, cgraph_uid=1, symbol_order=1)

g ()
{
  char a[5];
  long unsigned int _1;
  int _4;

  <bb 2> [local count: 1073741825]:
  a = "012";
  _1 = __builtin_strlen (&MEM[(void *)&a + 4B]);
  _4 = (int) _1;
  a ={v} {CLOBBER};
  return _4;

}

Reply via email to