[Bug tree-optimization/83026] New: missing strlen optimization for strcmp of unequal strings

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Nov 16 23:29:00 GMT 2017


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

            Bug ID: 83026
           Summary: missing strlen optimization for strcmp of unequal
                    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: ---

Here's another strcmp optimization opportunity.  When strlen() determines that
two strings are of unequal length it's safe to assume they do not compare
equal.  Therefore, in the function below, the conditional with the the strcmp()
call can be folded into false.

$ cat c.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout c.c
void g (const char *a, const char *b)
{
  if (__builtin_strlen (a) == __builtin_strlen (b))
    return;

  if (!__builtin_strcmp (a, b))
    __builtin_abort ();
}

;; Function g (g, funcdef_no=0, decl_uid=1892, cgraph_uid=0, symbol_order=0)

g (const char * a, const char * b)
{
  long unsigned int _1;
  long unsigned int _2;
  int _3;

  <bb 2> [local count: 10000]:
  _1 = __builtin_strlen (a_5(D));
  _2 = __builtin_strlen (b_6(D));
  if (_1 == _2)
    goto <bb 5>; [20.97%]
  else
    goto <bb 3>; [79.03%]

  <bb 3> [local count: 7903]:
  _3 = __builtin_strcmp (a_5(D), b_6(D));
  if (_3 == 0)
    goto <bb 4>; [0.04%]
  else
    goto <bb 5>; [99.96%]

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

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

}


More information about the Gcc-bugs mailing list