https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82017
Bug ID: 82017 Summary: missing strlen optimization for chained mempcpy calls 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: --- GCC is able to track the length of the string created by the two calls to memcpy in f() below but it's fails to do the same for the chained calls to mempcpy in g(). Since mempcpy is specifically designed for such chained copying it's even more likely to benefit from the strlen optimization than memcpy. $ cat t.c && gcc -O2 -S -Wall -Wextra -Wpedantic -fdump-tree-optimized=/dev/stdout t.c unsigned f (char *d) { __builtin_memcpy (d, "abc", 3); __builtin_memcpy (d + 3, "def", 4); return __builtin_strlen (d); // strlen call folded into a constant } unsigned g (char *d) { __builtin_mempcpy (__builtin_mempcpy (d, "abc", 3), "def", 4); return __builtin_strlen (d); // strlen call not folded } ;; Function f (f, funcdef_no=0, decl_uid=1815, cgraph_uid=0, symbol_order=0) f (char * d) { char * _1; <bb 2> [100.00%] [count: INV]: __builtin_memcpy (d_4(D), "abc", 3); _1 = d_4(D) + 3; __builtin_memcpy (_1, "def", 4); return 6; } ;; Function g (g, funcdef_no=1, decl_uid=1818, cgraph_uid=1, symbol_order=1) g (char * d) { void * _1; long unsigned int _2; unsigned int _7; <bb 2> [100.00%] [count: INV]: _1 = __builtin_mempcpy (d_4(D), "abc", 3); __builtin_mempcpy (_1, "def", 4); _2 = __builtin_strlen (d_4(D)); _7 = (unsigned int) _2; return _7; }