Bug 93556 - lower mempcpy to memcpy when result is unused
Summary: lower mempcpy to memcpy when result is unused
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 10.0
: P3 enhancement
Target Milestone: 17.0
Assignee: Not yet assigned to anyone
URL:
Keywords: easyhack, missed-optimization
Depends on:
Blocks:
 
Reported: 2020-02-03 18:49 UTC by Martin Sebor
Modified: 2026-04-27 14:09 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-09-05 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2020-02-03 18:49:12 UTC
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;
}
Comment 1 Drea Pinski 2021-09-05 05:32:17 UTC
Confirmed.
Comment 2 Drea Pinski 2025-09-04 02:42:34 UTC
This should be easy to add in forwprop or gimple fold.
Comment 3 Valentyn Yukhymenko 2026-02-10 00:03:18 UTC
(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!
Comment 4 Drea Pinski 2026-02-10 01:55:42 UTC
(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.
Comment 5 Drea Pinski 2026-02-10 01:56:59 UTC
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.
Comment 6 GCC Commits 2026-04-27 13:15:56 UTC
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>
Comment 7 Drea Pinski 2026-04-27 14:09:53 UTC
Fixed.