public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rguenth at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/32667] block copy with exact overlap is expanded as memcpy
Date: Fri, 24 Nov 2023 09:29:55 +0000	[thread overview]
Message-ID: <bug-32667-4-9qP6kaEyN4@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-32667-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=32667

--- Comment #46 from Richard Biener <rguenth at gcc dot gnu.org> ---
I'll note that restrict on the parameters can help optimizing the copying loop
because you can then unroll and hoist loads before stores.  I'll also note that
almost all (important) targets implement memcpy with special assembler in libc.

But yes, a compiler might, seeing

void foo (char * __restrict d, char * s)
{
  *d = *s;
  if (d != s)
    abort ();
}

optimize the compare to always true because of the wording of the C standard
with regard to restrict.  GCC doesn't (yet) do this.  A compiler might also
transform

 foo (p, p);

to trap (as part of eliminating paths with undefined behavior) if it
can analyze foo inter-procedurally seeing that foo will always invoke
undefined behavior if d == s.

For memcpy the same would hold true but the compiler would need to know
that the size of the copy is not zero.

So yes, the most clean solution would be to have __forgiving_memcpy
possibly also allowing NULL pointers when n == 0 besides allowing
the exact overlap.  Its prototype wouldn't have restrict then or
nonnull.

This all doesn't change the fact that GCC up to now always assumed
memcpy with exact overlap is fine to do, so it's practically impossible
to change implementations without recompiling the world with a "fixed"
compiler.

  parent reply	other threads:[~2023-11-24  9:29 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-32667-4@http.gcc.gnu.org/bugzilla/>
2011-12-05 17:00 ` [Bug middle-end/32667] builtin operator= generates memcpy with overlapping memory regions lu_zero at gentoo dot org
2011-12-05 20:38 ` mans at mansr dot com
2011-12-06  8:46 ` [Bug middle-end/32667] block copy with exact overlap is expanded as memcpy rguenth at gcc dot gnu.org
2015-02-12  5:38 ` pinskia at gcc dot gnu.org
2015-02-12  9:00 ` rguenth at gcc dot gnu.org
2015-02-12  9:42 ` jakub at gcc dot gnu.org
2015-09-28 18:18 ` hugh at mimosa dot com
2021-06-09 18:50 ` public at timruffing dot de
2021-06-10  6:36 ` rguenther at suse dot de
2023-01-05  8:53 ` pinskia at gcc dot gnu.org
2023-01-05  9:19 ` nyh at math dot technion.ac.il
2023-01-06 18:48 ` lopresti at gmail dot com
2023-10-28 10:04 ` post+gcc at ralfj dot de
2023-11-21 15:57 ` bugdal at aerifal dot cx
2023-11-22  7:33 ` rguenther at suse dot de
2023-11-22 14:57 ` bugdal at aerifal dot cx
2023-11-22 15:06 ` jakub at gcc dot gnu.org
2023-11-22 19:07 ` bugdal at aerifal dot cx
2023-11-23  0:00 ` lopresti at gmail dot com
2023-11-23  7:44 ` post+gcc at ralfj dot de
2023-11-23  7:58 ` rguenth at gcc dot gnu.org
2023-11-23  8:03 ` fw at gcc dot gnu.org
2023-11-23  8:08 ` rguenther at suse dot de
2023-11-23  8:32 ` cvs-commit at gcc dot gnu.org
2023-11-23  8:39 ` jakub at gcc dot gnu.org
2023-11-23 13:54 ` bugdal at aerifal dot cx
2023-11-23 13:57 ` bugdal at aerifal dot cx
2023-11-23 14:41 ` public at timruffing dot de
2023-11-23 15:00 ` public at timruffing dot de
2023-11-23 15:01 ` sjames at gcc dot gnu.org
2023-11-23 15:29 ` post+gcc at ralfj dot de
2023-11-23 18:55 ` bugdal at aerifal dot cx
2023-11-23 19:04 ` post+gcc at ralfj dot de
2023-11-23 20:27 ` bugdal at aerifal dot cx
2023-11-24  8:19 ` public at timruffing dot de
2023-11-24  9:29 ` rguenth at gcc dot gnu.org [this message]
2023-11-24 10:09 ` jakub at gcc dot gnu.org
2023-11-24 19:08 ` post+gcc at ralfj dot de
2023-11-27 14:12 ` post+gcc at ralfj dot de
2023-11-27 23:32 ` joseph at codesourcery dot com
2023-11-28  7:17 ` post+gcc at ralfj dot de
2023-11-28  7:20 ` post+gcc at ralfj dot de
2023-11-28 10:58 ` rguenther at suse dot de
2023-11-28 11:03 ` rguenth at gcc dot gnu.org
2023-11-28 11:07 ` fw at gcc dot gnu.org
2024-01-04 12:52 ` rearnsha at gcc dot gnu.org
2024-01-04 13:22 ` bugdal at aerifal dot cx
2024-01-04 13:37 ` jakub at gcc dot gnu.org
2024-01-04 14:28 ` rearnsha at gcc dot gnu.org
2024-01-04 14:29 ` bugdal at aerifal dot cx
2024-01-04 14:41 ` rearnsha at gcc dot gnu.org
2024-01-04 14:44 ` bugdal at aerifal dot cx

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-32667-4-9qP6kaEyN4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).