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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |83819
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-08-13
           Severity|normal                      |enhancement
                 CC|                            |msebor at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
Sounds like a good idea.  In fact, there is code to "Try to fold strstr (s, t)
eq/ne s to strncmp (s, t, strlen (t)) eq/ne 0" in tree-ssa-streln.c but it
triggers only when the length of the second argument has already been computed:

$ cat pr96601.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout pr96601.c
int f (const char *a, const char *b)
{
  if (__builtin_strlen (b) > 1)
    return __builtin_strstr (a, b) == a;
  return -1;
}

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

Removing basic block 5
f (const char * a, const char * b)
{
  long unsigned int _1;
  char * _2;
  _Bool _3;
  int _4;
  int _8;
  int _10;

  <bb 2> [local count: 1073741824]:
  _1 = __builtin_strlen (b_6(D));
  if (_1 > 1)
    goto <bb 3>; [42.57%]
  else
    goto <bb 4>; [57.43%]

  <bb 3> [local count: 457091896]:
  _10 = __builtin_strncmp (a_7(D), b_6(D), _1);
  _3 = _10 == 0;
  _8 = (int) _3;

  <bb 4> [local count: 1073741824]:
  # _4 = PHI <_8(3), -1(2)>
  return _4;

}


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83819
[Bug 83819] [meta-bug] missing strlen optimizations

Reply via email to