https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86427
Bug ID: 86427 Summary: strlen not folded after strcpy into a zeroed-out local array 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: --- GCC eliminates a call to strlen() on a local copy into an uninitialized array and from a character string of known length, but doesn't do the same when local array is first zeroed-out. $ cat c.c && gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout c.c const char a[] = "123"; int f (void) { char d[8]; __builtin_strcpy (d, a); // eliminated return __builtin_strlen (d); // folded } int g (void) { char d[8] = ""; __builtin_strcpy (d, a); // not eliminated return __builtin_strlen (d); // not folded } ;; Function f (f, funcdef_no=0, decl_uid=1899, cgraph_uid=1, symbol_order=1) f () { <bb 2> [local count: 1073741825]: return 3; } ;; Function g (g, funcdef_no=1, decl_uid=1903, cgraph_uid=2, symbol_order=2) g () { char d[8]; long unsigned int _1; int _5; <bb 2> [local count: 1073741825]: d = ""; MEM[(char * {ref-all})&d] = MEM[(char * {ref-all})&a]; _1 = __builtin_strlen (&d); _5 = (int) _1; d ={v} {CLOBBER}; return _5; }