-
Notifications
You must be signed in to change notification settings - Fork 12.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible missed optimization when calling memcpy or memmove in a loop #117332
Comments
The behavior is not the same. Suppose for example |
The memcpy's do get combined if you restrict-qualify |
Ah ah! Thanks both, that makes sense. I can see that this gets optimized if I However, if I switch to |
For memcpy overlapping ranges are UB, but memmove has to handle overlap:
I could not find if it is stated in the C++ standard. The above citation is from https://en.cppreference.com/w/cpp/string/byte/memmove In C99 however, the difference is pretty clear:
|
@b1ackviking I don't think that was unknown or up for discussion for anyone in this thread? Or I do not see how this is relevant to the remaining question? |
@keinflue I thought my comment could be helpful as a possible explanation to why the compiler does not optimize memmove even in the second case, that's not obvious to me. Sorry, if it was inappropriate, didn't want to break in just to say something. |
I noticed that the following code did not optimize to a single memcpy, unlike I would expect:
I would expect this to be equivalent to roughly:
Is this a problem with e.g. the lack of knowledge that the
[first, last)
range is all valid? Note that both GCC and Clang fail to perform this optimization.Godbolt: https://godbolt.org/z/zzdhcKPh4
The text was updated successfully, but these errors were encountered: