https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82950
Bug ID: 82950 Summary: possible strlen optimization for memcmp/strcmp of arrays 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: --- While testing some unrelated work I noticed that GCC doesn't eliminate the call to strcmp in the program below even though it could based on the knowledge that the two arrays a and b contain a copy of the same string. A nsimilar optimization is possible for memcpy (regardless of the size specified by the third argument). It seems that a good place to add this optimization might be the tree-ssa-strlen.c pass. $ cat c.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout c.c char a[4]; char b[4]; int f (void) { __builtin_strcpy (a, "abc"); __builtin_strcpy (b, "abc"); return __builtin_strcmp (a, b); // not folded but could be } int g (const char *s) { __builtin_strcpy (a, s); __builtin_strcpy (b, s); return __builtin_strcmp (a, b); // not folded but could be } ;; Function f (f, funcdef_no=0, decl_uid=1893, cgraph_uid=0, symbol_order=2) f () { int _4; <bb 2> [local count: 10000]: MEM[(char * {ref-all})&a] = "abc"; MEM[(char * {ref-all})&b] = "abc"; _4 = __builtin_strcmp (&a, &b); [tail call] return _4; } ;; Function g (g, funcdef_no=1, decl_uid=1896, cgraph_uid=1, symbol_order=3) g (const char * s) { int _5; <bb 2> [local count: 10000]: __builtin_strcpy (&a, s_2(D)); __builtin_strcpy (&b, s_2(D)); _5 = __builtin_strcmp (&a, &b); [tail call] return _5; }