https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89043
Bug ID: 89043 Summary: strcat (strcpy (d, a), b) not folded to stpcpy (strcpy (d, a), b) 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 folds a small subset of calls to C strcpy to the POSIX stpcpy when doing so is profitable, such as in function f below. But it doesn't do the same folding in the common case when strcat is being called with the result of strcpy as the first argument, as in function g below. $ cat u.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout u.c extern char* stpcpy (char*, const char*); extern char* strcat (char*, const char*); extern char* strcpy (char*, const char*); extern char* strchr (const char*, int); extern char *a, *b, *d; char* f (char *d, const char *s) { strcpy (d, s); // folded to 'p = stpcpy (d, s);' return strchr (d, 0); // folded to 'return p;' } void g (void) { // could be folded to 'strcpy (stpcpy (d, a));' strcat (strcpy (d, a), b); } ;; Function f (f, funcdef_no=0, decl_uid=1922, cgraph_uid=1, symbol_order=0) f (char * d, const char * s) { char * _7; <bb 2> [local count: 1073741824]: _7 = __builtin_stpcpy (d_2(D), s_3(D)); return _7; } ;; Function g (g, funcdef_no=1, decl_uid=1925, cgraph_uid=2, symbol_order=1) g () { char * b.0_1; char * a.1_2; char * d.2_3; char * _4; <bb 2> [local count: 1073741824]: b.0_1 = b; a.1_2 = a; d.2_3 = d; _4 = strcpy (d.2_3, a.1_2); strcat (_4, b.0_1); [tail call] return; }