https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104276
Bug ID: 104276
Summary: Fail to eliminate deadstore from vector constructor
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: Darrell.Wright at gmail dot com
Target Milestone: ---
clang is unable to remove the memset in code like
std::vector<int> foo() {
auto result = std::vector<int>(SZ);
int *ptr = result.data();
for (std::size_t n = 0; n < SZ; ++n) {
ptr[n] = static_cast<int>( n );
}
return result;
}
https://gcc.godbolt.org/z/5cbKejfqr
This is unaffected if the value is set during resize. That may result in
inlining of the memset but does not eliminate it.
However for code that uses blessed methods like memset subsequently
std::vector<int> foo() {
auto result = std::vector<int>(SZ);
std::memset(result.data(), 5, sizeof(int) * SZ);
return result;
}
https://gcc.godbolt.org/z/Kfs9x8Pe9
It is. This seems to be the usecase of resize_and_overwrite in future string.
Is there a way to formulate the code to do this. Or, and I think a better way,
is there a builtin or could there be that lets the compiler assume that the
memory will be subsequently written to? e.g. `__bultin_assume_set( void *
dest, size_t count )` ?