public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/96601] New: Failure to optimize equality comparison involving strstr to strlen+strncmp
@ 2020-08-13  9:45 gabravier at gmail dot com
  2020-08-13 19:05 ` [Bug tree-optimization/96601] " msebor at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: gabravier at gmail dot com @ 2020-08-13  9:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96601
           Summary: Failure to optimize equality comparison involving
                    strstr to strlen+strncmp
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

bool f(const char *a, const char *b)
{
    return strstr(a, b) == a;
}

This can be optimized to `strncmp(a, b, strlen(b))`. This transformation is
done by LLVM, but not by GCC.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug tree-optimization/96601] Failure to optimize equality comparison involving strstr to strlen+strncmp
  2020-08-13  9:45 [Bug tree-optimization/96601] New: Failure to optimize equality comparison involving strstr to strlen+strncmp gabravier at gmail dot com
@ 2020-08-13 19:05 ` msebor at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-08-13 19:05 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-08-13 19:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-13  9:45 [Bug tree-optimization/96601] New: Failure to optimize equality comparison involving strstr to strlen+strncmp gabravier at gmail dot com
2020-08-13 19:05 ` [Bug tree-optimization/96601] " msebor at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).