https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94485
Bug ID: 94485 Summary: [10.0.1, c++2a] g++ optimizes away the code, accepts arbitrary inline asm Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: dimitri.gorokhovik at free dot fr Target Milestone: --- Created attachment 48194 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48194&action=edit Sample code illustrating the sighting. Version: gcc (GCC) 10.0.1 20200404 (experimental) command line: ~/gcc-trunk/dist/bin/g++ -std=c++2a -O3 -Wall -Wextra -W bug-202004041709.cpp -o bug-1 && ./bug-1 The attached file (apologies, still big) contains the following code (line 425): constexpr iterator (tesselation const& me) : cube_ { me.cube_ }, inner_ { me.as_parent ().begin () }, tess_ {}, outer_ { tess_ } { if (inner_) { tess_ = tesselation_of_two_cubes { cube_, *inner_ }; asm("before:"); #if 0 // breaks only with -O3 auto const tmp { tess_.begin () }; asm("middle:"); outer_ = tmp; ^^^^^^^^^^^^ #else // breaks with -O3, -O1, fixes with -fsanitize=undefined|null outer_ = { tess_.begin () }; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ asm("after:"); #endif inner_.next (); if (! outer_) throw 749; }; }; (Minor remark: 'asm' statements in this constexpr method raise no brows. I tried putting there insns that require evaluation, seems anything is silently accepted. The same asm statements are rejected in a more simple constexpr code.) It seems, some optimization levels completely remove the highlighted assignment so that 'throw 749' is executed (main() prints it then). Other passes keep the copying code and produce expected result. This over-optimization can be fixed by enabling certain sanitizers: undefined, null, vptr. The asm code produced: -- broken (compiled with -O3 or -O1): #APP # 425 "bug-1.cpp" 1 before: # 0 "" 2 #NO_APP movq %r12, 216(%rsp) #APP # 433 "bug-1.cpp" 1 after: # 0 "" 2 #NO_APP -- working (compiled with -O0 or -O1 or with -fsanitizer=null): #APP # 425 "bug-1.cpp" 1 before: # 0 "" 2 #NO_APP xorl %eax, %eax cmpb $0, 43(%rbx) movq %r14, 64(%rsp) movw %ax, 72(%rsp) movb $0, 74(%rsp) je .L73 cmpb $0, 63(%rbx) je .L73 cmpb $0, 83(%rbx) sete %al .L68: movb %al, 75(%rsp) movq 64(%rsp), %rax movdqa 64(%rsp), %xmm6 movq %rax, 88(%rbx) movl 72(%rsp), %eax movaps %xmm6, 96(%rsp) movl %eax, 96(%rbx) #APP # 433 "bug-1.cpp" 1 after: # 0 "" 2 #NO_APP