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

            Bug ID: 86955
           Summary: strlen of a known string in member array plus offset
                    not folded
           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: ---

GCC is able to fold three out of the four strlen calls in the test case below,
even though folding all four should be possible.

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

struct A { char i, a[8]; };

void f0 (void)
{
  __builtin_strcpy (a, "123");

  if (__builtin_strlen (a) != 3)   // folded
    __builtin_abort ();
}

void f1 (void)
{
  __builtin_strcpy (a, "123");

  if (__builtin_strlen (a + 1) != 2)   // folded
    __builtin_abort ();
}

void f2 (struct A* p)
{
  __builtin_strcpy (p->a, "123");

  if (__builtin_strlen (p->a) != 3)   // folded
    __builtin_abort ();
}

void f3 (struct A* p)
{
  __builtin_strcpy (p->a, "123");

  if (__builtin_strlen (p->a + 1) != 2)   // not folded
    __builtin_abort ();
}


;; Function f0 (f0, funcdef_no=0, decl_uid=1910, cgraph_uid=1, symbol_order=1)

f0 ()
{
  <bb 2> [local count: 1073741825]:
  __builtin_memcpy (&a, "123", 4); [tail call]
  return;

}



;; Function f1 (f1, funcdef_no=1, decl_uid=1913, cgraph_uid=2, symbol_order=2)

f1 ()
{
  <bb 2> [local count: 1073741825]:
  __builtin_memcpy (&a, "123", 4); [tail call]
  return;

}



;; Function f2 (f2, funcdef_no=2, decl_uid=1916, cgraph_uid=3, symbol_order=3)

f2 (struct A * p)
{
  char[8] * _1;

  <bb 2> [local count: 1073741825]:
  _1 = &p_3(D)->a;
  __builtin_memcpy (_1, "123", 4); [tail call]
  return;

}



;; Function f3 (f3, funcdef_no=3, decl_uid=1919, cgraph_uid=4, symbol_order=4)

f3 (struct A * p)
{
  char[8] * _1;
  const char * _2;
  long unsigned int _3;

  <bb 2> [local count: 1073741825]:
  _1 = &p_4(D)->a;
  __builtin_memcpy (_1, "123", 4);
  _2 = &MEM[(void *)p_4(D) + 2B];
  _3 = __builtin_strlen (_2);
  if (_3 != 2)
    goto <bb 3>; [0.00%]
  else
    goto <bb 4>; [99.96%]

  <bb 3> [count: 0]:
  __builtin_abort ();

  <bb 4> [local count: 1073312327]:
  return;

}

Reply via email to