https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81365
Bug ID: 81365 Summary: GCC miscompiles swap Product: gcc Version: 7.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: kaufmann at cs dot uni-potsdam.de Target Milestone: --- Created attachment 41700 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=41700&action=edit Minimal test case For the attached code, gcc miscompiles the call to swap in Foo::swapToHead() with the result that the value of 'a' is lost. gcc-7.1 miscompiles the code at -O2 and above (-O1 and above if std::swap is used) at least with x86_64-pc-linux-gnu. gcc-6.3 compiles the code correctly. ### Relevant sequence of instructions: mov edx,edx lea rcx,[rdi+0x8] ; tail[0] mov DWORD PTR [rdi+rdx*4+0x10],esi ; store to head_[1] - This does not seem to be right mov eax,DWORD PTR [rdi+rdx*4+0x10] mov DWORD PTR [rcx],eax ; now head_[1] and tail[0] are the same ### COMMAND: g++ -O3 bug-min.ii ./a.out ### EXPECTED OUTPUT: (rc=0) ### ACTUAL OUTPUT: x.swapToHead(Lit::make( 2), 1) && "Lit 2 went missing" Aborted ### g++ -v Using built-in specs. COLLECT_GCC=/home/wv/bin/linux/64/gcc-7.1/bin/g++ COLLECT_LTO_WRAPPER=/home/wv/bin/linux/64/gcc-7.1/libexec/gcc/x86_64-pc-linux-gnu/7.1.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../../sources/gcc-7.1.0/configure --prefix=/home/wv/bin/linux/64/gcc-7.1 --enable-languages=c,c++ Thread model: posix gcc version 7.1.0 (GCC)