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

            Bug ID: 93261
           Summary: fold strstr(a, b) to zero when b is longer than a
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

Similarly to pr90879, pr90626, pr90876 and pr83026, the expression strstr(a, b)
can be folded to null when the string b is longer than the string a, or the
object in which the string a is stored.  The latter should avoid problems like
some of those discussed in pr92765.

$ cat z.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout z.c
void f (const char *s)
{
  if (__builtin_strlen (s) > 2)
    return;

  if (__builtin_strstr (s, "bar"))   // can be folded to false
    __builtin_abort ();
}

void g (void)
{
  extern char a[6];   // strlen (a) must be less than 6

  if (__builtin_strstr (a, "foobar"))   // can be folded to false
    __builtin_abort ();
}


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

Removing basic block 6
Removing basic block 7
f (const char * s)
{
  long unsigned int _1;
  char * _2;

  <bb 2> [local count: 1073741824]:
  _1 = __builtin_strlen (s_4(D));
  if (_1 > 2)
    goto <bb 5>; [34.00%]
  else
    goto <bb 3>; [66.00%]

  <bb 3> [local count: 708669605]:
  _2 = __builtin_strstr (s_4(D), "bar");
  if (_2 != 0B)
    goto <bb 4>; [0.00%]
  else
    goto <bb 5>; [100.00%]

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

  <bb 5> [local count: 1073741824]:
  return;

}



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

g ()
{
  char * _1;

  <bb 2> [local count: 1073741824]:
  _1 = __builtin_strstr (&a, "foobar");
  if (_1 != 0B)
    goto <bb 3>; [0.00%]
  else
    goto <bb 4>; [100.00%]

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

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

}

Reply via email to