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

            Bug ID: 83673
           Summary: missing strlen optimization on multiple strcpy calls
                    with same length strings
           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: ---

GCC is able to track the length of the string copied using the single strcpy
call in function f() below but it can't do the same for the equivalent code in
function g().  It would be a useful enhancement if it could.

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

void f (int i)
{
  __builtin_strcpy (d, i < 0 ? "123456" : "654321");

  if (__builtin_strlen (d) != 6)
    __builtin_abort ();
}

void g (int i)
{
  if (i < 0)
    __builtin_strcpy (d, "123456");
  else
    __builtin_strcpy (d, "654321");

  if (__builtin_strlen (d) != 6)
    __builtin_abort ();
}


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

f (int i)
{
  char[7] * iftmp.0_7;

  <bb 2> [local count: 1073741825]:
  if (i_3(D) < 0)
    goto <bb 4>; [36.00%]
  else
    goto <bb 3>; [64.00%]

  <bb 3> [local count: 687194769]:

  <bb 4> [local count: 1073312329]:
  # iftmp.0_7 = PHI <"123456"(2), "654321"(3)>
  __builtin_memcpy (&d, iftmp.0_7, 7); [tail call]
  return;

}



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

g (int i)
{
  long unsigned int _1;

  <bb 2> [local count: 1073741825]:
  if (i_3(D) < 0)
    goto <bb 3>; [36.00%]
  else
    goto <bb 4>; [64.00%]

  <bb 3> [local count: 386547057]:
  __builtin_memcpy (&d, "123456", 7);
  goto <bb 5>; [100.00%]

  <bb 4> [local count: 687194768]:
  __builtin_memcpy (&d, "654321", 7);

  <bb 5> [local count: 1073741825]:
  _1 = __builtin_strlen (&d);
  if (_1 != 6)
    goto <bb 6>; [0.00%]
  else
    goto <bb 7>; [99.96%]

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

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

}

Reply via email to