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

            Bug ID: 92112
           Summary: fold strlen after strcmp(a, b) == 0
           Product: gcc
           Version: 9.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: ---

After a strcmp call evaluates to zero the lengths of the two arguments can be
assumed to be equal.  GCC could make use of that to fold strlen expressions
such as those in the test case below but doesn't (yet).

$ cat a.c && gcc -S -O2 -Wall -Wextra -fdump-tree-optimized=/dev/stdout a.c
extern char a[], b[];

void f (void)
{
  if (__builtin_strcmp (a, b) != 0)
    return;

  if (__builtin_strlen (a) != __builtin_strlen (b))   // can be folded to false
    __builtin_abort ();
}


void g (void)
{
  if (__builtin_strlen (a) < 7)
    return;
  if (__builtin_strcmp (a, b) != 0)
    return;

  if (__builtin_strlen (b) < 7)   // can be folded to false
    __builtin_abort ();
}

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

Removing basic block 6
Removing basic block 7
f ()
{
  int _1;
  long unsigned int _2;
  long unsigned int _3;

  <bb 2> [local count: 1073741824]:
  _1 = __builtin_strcmp (&a, &b);
  if (_1 != 0)
    goto <bb 5>; [34.00%]
  else
    goto <bb 3>; [66.00%]

  <bb 3> [local count: 708669605]:
  _2 = __builtin_strlen (&a);
  _3 = __builtin_strlen (&b);
  if (_2 != _3)
    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=1935, cgraph_uid=2, symbol_order=1)

Removing basic block 7
Removing basic block 8
Removing basic block 9
g ()
{
  long unsigned int _1;
  int _2;
  long unsigned int _3;

  <bb 2> [local count: 1073741823]:
  _1 = __builtin_strlen (&a);
  if (_1 <= 6)
    goto <bb 6>; [34.00%]
  else
    goto <bb 3>; [66.00%]

  <bb 3> [local count: 708669604]:
  _2 = __builtin_strcmp (&a, &b);
  if (_2 != 0)
    goto <bb 6>; [34.00%]
  else
    goto <bb 4>; [66.00%]

  <bb 4> [local count: 467721938]:
  _3 = __builtin_strlen (&b);
  if (_3 <= 6)
    goto <bb 5>; [0.00%]
  else
    goto <bb 6>; [100.00%]

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

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

}

Reply via email to