llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Redundant memset followed by memcpy not eliminated in ctors if this->field aliases a parameter

Open jvoung opened this issue 2 years ago • 1 comments

Example https://godbolt.org/z/1exh1cjaj C1::C1 has a memset from Options() ctor, and a memcpy from options_ = options but in the end the code is left with both (if you accidentally don't use the initializer list to init options_ like in C2).

IIUC the concern is that options param and this->options_ may alias. Does the restrictions against aliasing this from https://cplusplus.github.io/CWG/issues/2271.html apply to let us assume no alias?

jvoung avatar Dec 12 '23 19:12 jvoung

Unfortunately, I'm not sure of a simple solution (say, putting the attribute noalias on the parameter this for constructors).

Re-reading the spec, say section 11.10.4 of https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4849.pdf (or the earlier link), it seems more like we know that the other parameters can't alias in terms of reading ("the value of the object or subobject thus obtained..."), but not fully "no alias" (e.g., considering writes as well).

jvoung avatar Jan 02 '24 20:01 jvoung