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

            Bug ID: 82571
           Summary: missing strlen optimization for memchr
           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 result of a call to memchr(a, '\0', sizeof a) where a is a nul-terminated
string is the same as (a + strlen (a)).  GCC could use this equivalence to
optimize code like that in functions f0() and f1() the same way it optimizes
functions g0() and g1().

$ gcc -O2 -S -fdump-tree-optimized=/dev/stdout a.c

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

f0 ()
{
  char * p;
  char a[9];

  <bb 2> [100.00%] [count: INV]:
  __builtin_memcpy (&a, "012345", 7);
  p_3 = __builtin_memchr (&a, 0, 9);
  if (p_3 == 0B)
    goto <bb 3>; [0.04%] [count: 0]
  else
    goto <bb 4>; [99.96%] [count: INV]

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

  <bb 4> [99.96%] [count: INV]:
  a ={v} {CLOBBER};
  return;

}



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

f1 ()
{
  char * p;
  char a[9];
  long int p.0_1;
  long int a.1_2;
  long int _3;

  <bb 2> [100.00%] [count: INV]:
  __builtin_memcpy (&a, "012345", 7);
  p_8 = __builtin_memchr (&a, 0, 9);
  p.0_1 = (long int) p_8;
  a.1_2 = (long int) &a;
  _3 = p.0_1 - a.1_2;
  if (_3 != 6)
    goto <bb 3>; [0.04%] [count: 0]
  else
    goto <bb 4>; [99.96%] [count: INV]

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

  <bb 4> [99.96%] [count: INV]:
  a ={v} {CLOBBER};
  return;

}



;; Function g0 (g0, funcdef_no=2, decl_uid=1825, cgraph_uid=2, symbol_order=2)

g0 ()
{
  <bb 2> [100.00%] [count: INV]:
  return;

}



;; Function g1 (g1, funcdef_no=3, decl_uid=1830, cgraph_uid=3, symbol_order=3)

g1 ()
{
  <bb 2> [100.00%] [count: INV]:
  return;

}

Reply via email to