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

            Bug ID: 78257
           Summary: missing memcmp optimization with constant arrays
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

GCC folds some calls to memcmp involving constants but not others even though
it's able to fold equivalent expressions not involving the function.  The test
case below shows two such examples.  Using memcmp to compare constant string
arrays is folded unless the comparison includes the terminating nul.  Using
memcmp to compare arrays of integers is not folded even though comparing the
array elements (even recursively) is.

$ cat t.c && gcc -O2 -S -Wall -Wextra -Wpedantic
-fdump-tree-optimized=/dev/stdout t.c
const char a[] = "1234";
const char b[] = "1234";

const int i[] = { 1234 };
const int j[] = { 1234 };

int f0 (void)
{
  return __builtin_memcmp (a, b, sizeof a);
}

int f1 (void)
{
  return __builtin_memcmp (a, b, sizeof a - 1);
}

int f2 (void)
{
  return __builtin_memcmp (i, j, sizeof i);
}

int f3 (void)
{
  return *i == *j;
}

int f4 (void)
{
  return __builtin_strcmp (a, b);
}

;; Function f0 (f0, funcdef_no=0, decl_uid=1799, cgraph_uid=0, symbol_order=4)

f0 ()
{
  int _2;

  <bb 2>:
  _2 = __builtin_memcmp (&a, &b, 5); [tail call]
  return _2;

}



;; Function f1 (f1, funcdef_no=6, decl_uid=1802, cgraph_uid=1, symbol_order=5)

f1 ()
{
  <bb 2>:
  return 0;

}



;; Function f2 (f2, funcdef_no=2, decl_uid=1805, cgraph_uid=2, symbol_order=6)

f2 ()
{
  int _2;

  <bb 2>:
  _2 = __builtin_memcmp (&i, &j, 4); [tail call]
  return _2;

}



;; Function f3 (f3, funcdef_no=3, decl_uid=1808, cgraph_uid=3, symbol_order=7)

f3 ()
{
  <bb 2>:
  return 1;

}



;; Function f4 (f4, funcdef_no=4, decl_uid=1811, cgraph_uid=4, symbol_order=8)

f4 ()
{
  <bb 2>:
  return 0;

}

Reply via email to