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

            Bug ID: 86936
           Summary: strlen() of a constant not folded due to laddress
                    transformation
           Product: gcc
           Version: 9.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: ---

Despite the argument being a constant string, as a result of the laddress
transformation the call to strnlen() in the function below is not folded into a
constant because it loses the knowledge about which element of the array the
strnlen argument refers to.

$ cat f.c && gcc -O2 -S -Wall -Wextra -Wnull-dereference
-fdump-tree-bswap=/dev/stdout -fdump-tree-laddress=/dev/stdout f.c
const char a[][4] = { "123", "1234" };

int f (int i)
{
  return __builtin_strnlen (&a[1][i], 4);
}

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

f (int i)
{
  const char * _1;
  long unsigned int _2;
  int _5;

  <bb 2> [local count: 1073741825]:
  _1 = &a[1][i_3(D)];
  _2 = __builtin_strnlen (_1, 4);
  _5 = (int) _2;
  return _5;

}



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

f (int i)
{
  const char * _1;
  long unsigned int _2;
  int _5;
  sizetype _6;
  sizetype _7;

  <bb 2> [local count: 1073741825]:
  _6 = (sizetype) i_3(D);
  _7 = _6 + 4;
  _1 = &a + _7;
  _2 = __builtin_strnlen (_1, 4);
  _5 = (int) _2;
  return _5;

}

Reply via email to