https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84069
Bug ID: 84069 Summary: missing strlen optimization after constant memcpy with offset 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: --- With pr83501 fixed by r256475, GCC 8 is able to fold the call to strlen() in function f0() below into a constant, and replace the whole body of the function with just a return statement. Unfortunately (as pointed out in https://gcc.gnu.org/ml/gcc-patches/2018-01/msg01879.html), it is not able to perform the same transformation in the equivalent function f1() in the test case. $ cat t.c && /ssd/build/gcc-svn/gcc/xgcc -B /ssd/build/gcc-svn/gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout t.c int f0 (void) // folded into 'return 5' { const char s[] = "1234"; char d[sizeof s + 1]; *d = '0'; __builtin_strcpy (d + 1, s); return __builtin_strlen (d); // folded into 5 } int f1 (void) // not folded { const char s[] = "1234"; char d[sizeof s + 1]; *d = '0'; __builtin_memcpy (d + 1, s, sizeof s); return __builtin_strlen (d); // not folded } ;; Function f0 (f0, funcdef_no=0, decl_uid=1950, cgraph_uid=0, symbol_order=0) f0 () { <bb 2> [local count: 1073741825]: return 5; } ;; Function f1 (f1, funcdef_no=1, decl_uid=1955, cgraph_uid=1, symbol_order=1) f1 () { char d[6]; const char s[5]; long unsigned int _1; int _6; <bb 2> [local count: 1073741825]: s = "1234"; d[0] = 48; MEM[(char * {ref-all})&d + 1B] = MEM[(char * {ref-all})&s]; _1 = __builtin_strlen (&d); _6 = (int) _1; s ={v} {CLOBBER}; d ={v} {CLOBBER}; return _6; }