GCC lowers calls to strpcpy whose result isn't used to strcpy, presumably because the latter tends to be more efficient. With pr90263 resolved, GCC also expands some calls to mempcpy to memcpy on most targets, again because the latter is almost always more efficient. But when the result of the mempcpy call is not used, the call could be transformed to memcpy unconditionally, on all targets, including those like x86 where targetm.libc_has_fast_function (BUILT_IN_MEMPCPY) returns true. $ cat a.c && gcc -S -Wall -fdump-tree-lower=/dev/stdout a.c void f0 (char *d, const char *s) { __builtin_stpcpy (d, s); // lowered to strcpy } void f1 (void *d, const void *s, __SIZE_TYPE__ n) { __builtin_mempcpy (d, s, n); // should be lowered to memcpy } ;; Function f0 (f0, funcdef_no=0, decl_uid=1931, cgraph_uid=1, symbol_order=0) f0 (char * d, const char * s) { __builtin_strcpy (d, s); return; } ;; Function f1 (f1, funcdef_no=1, decl_uid=1936, cgraph_uid=2, symbol_order=1) f1 (void * d, const void * s, long unsigned int n) { __builtin_mempcpy (d, s, n); return; }
Confirmed.
This should be easy to add in forwprop or gimple fold.
(In reply to Andrew Pinski from comment #2) > This should be easy to add in forwprop or gimple fold. Hey! I found this issue via "GNU Tools Weekly" Mastodon post, which mentioned the possibility of mentorship. I’d be happy to take a look at the issue, but would appreciate initial guidance since I am new to GCC project and so far only have some experience with C++ frontend. Thanks!
(In reply to Valentyn Yukhymenko from comment #3) > (In reply to Andrew Pinski from comment #2) > > This should be easy to add in forwprop or gimple fold. > > Hey! > > I found this issue via "GNU Tools Weekly" Mastodon post, which mentioned the > possibility of mentorship. > > I’d be happy to take a look at the issue, but would appreciate initial > guidance since I am new to GCC project and so far only have some experience > with C++ frontend. See gimple_fold_builtin and gimple_fold_builtin_memory_op in gimple-fold.cc. A simple way of implementing this would be if gimple_fold_builtin_memory_op returns false in gimple_fold_builtin and the fcode is BUILT_IN_MEMPCPY, then call a new function which do the replacement if gimple_call_lhs is NULL. See gimple_fold_builtin_stpcpy for an example of replacement that is already done.
One more thing BUILT_IN_MEMPCPY gets changed into BUILT_IN_MEMCPY like BUILT_IN_STPCPY is being changed into BUILT_IN_STRCPY in gimple_fold_builtin_stpcpy.
The master branch has been updated by Jeff Law <law@gcc.gnu.org>: https://gcc.gnu.org/g:4765a2e3980749c9eebd313b53d5be5aecf855be commit r17-102-g4765a2e3980749c9eebd313b53d5be5aecf855be Author: Netanel Komm <netanelkomm@gmail.com> Date: Mon Apr 27 07:14:46 2026 -0600 [PATCH v3] tree-optimization: lower mempcpy to memcpy when result is unused [PR93556] This patch allows the GIMPLE folder to transform __builtin_mempcpy into __builtin_memcpy in cases where the return value is ignored. This is beneficial because most targets have an efficient implementation for memcpy. Existing tests that relied on the unfolded mempcpy have been duplicated - one version now takes the folded mempcpy into account, and the other intentionally prevents the folding from happening. Bootstrapped and regression tested on x86_64-linux-gnu. PR tree-optimization/93556 gcc/ChangeLog: * gimple-fold.cc (gimple_fold_builtin_mempcpy): New function. (gimple_fold_builtin): Handle BUILT_IN_MEMPCPY. gcc/testsuite/ChangeLog: * gcc.dg/pr79223.c: Rename to gcc.dg/pr79223-1.c and update scans. * gcc.dg/tree-prof/val-prof-7.c: Rename to gcc.dg/tree-prof/val-prof-7-1.c and update scans. * gcc.dg/tree-ssa/builtins-folding-gimple-3.c: Update scans. * gcc.dg/builtin-mempcpy-1.c: New test. * gcc.dg/builtin-mempcpy-2.c: New test. * gcc.dg/pr79223-2.c: New test. * gcc.dg/tree-prof/val-prof-7-2.c: New test. * gcc.dg/tree-ssa/builtins-folding-gimple-4.c: New test. Signed-off-by: Netanel Komm <netanelkomm@gmail.com>
Fixed.