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

            Bug ID: 85897
           Summary: missing strncmp optimization for bound in known range
           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: ---

GCC folds strncmp() calls with all constant/known arguments but fails to
perform a similar simplification for corresponding calls where the bound is
non-constant but in a known range that could be used to do the same folding.

$ cat a.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout a.c
void f (unsigned n)
{
  const char *a = "abcX";
  const char *b = "abcY";

  n = 3;

  if (__builtin_strncmp (a, b, n))   // folded
    __builtin_abort ();              // eliminated
}

void g (unsigned n)
{
  const char *a = "abcX";
  const char *b = "abcY";

  if (3 < n)
    n = 3;

  if (__builtin_strncmp (a, b, n))   // not folded
    __builtin_abort ();              // not eliminated
}


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

f (unsigned int n)
{
  <bb 2> [local count: 1073741825]:
  return;

}



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

g (unsigned int n)
{
  long unsigned int _1;
  int _2;

  <bb 2> [local count: 1073741825]:
  if (n_4(D) > 3)
    goto <bb 5>; [50.00%]
  else
    goto <bb 3>; [50.00%]

  <bb 3> [local count: 536870913]:
  _1 = (long unsigned int) n_4(D);
  _2 = __builtin_strncmp ("abcX", "abcY", _1);
  if (_2 != 0)
    goto <bb 4>; [0.00%]
  else
    goto <bb 5>; [99.92%]

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

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

}

Reply via email to