https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86043
Bug ID: 86043 Summary: strlen after memcpy partially overwriting a string not optimized 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: --- The tree-ssa-strlen optimization has code to detect non-nul character stores into the initial elements of a string of a known length and avoid invalidating its length information, but it's missing the same optimization for corresponding stores by memcpy (or similarly, strcpy). $ cat d.c && gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout d.c char a[5]; int f (void) { __builtin_strcpy (a, "123"); a[0] = '1'; return __builtin_strlen (a); // folded, good } int g (void) { __builtin_strcpy (a, "123"); __builtin_memcpy (a + 1, "2", 1); return __builtin_strlen (a); // not folded } ;; Function f (f, funcdef_no=0, decl_uid=1957, cgraph_uid=0, symbol_order=1) f () { <bb 2> [local count: 1073741825]: __builtin_memcpy (&a, "123", 4); return 3; } ;; Function g (g, funcdef_no=1, decl_uid=1960, cgraph_uid=1, symbol_order=2) g () { long unsigned int _1; int _5; <bb 2> [local count: 1073741825]: __builtin_memcpy (&a, "123", 4); __builtin_memcpy (&MEM[(void *)&a + 1B], "2", 1); _1 = __builtin_strlen (&a); _5 = (int) _1; return _5; }