https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92425
Bug ID: 92425 Summary: Incorrect logical AND on 64bit variable using 32bit register Product: gcc Version: 9.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: akobets at mail dot ru Target Milestone: --- Created attachment 47200 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47200&action=edit Assembly output === prog.cpp === unsigned long long a, b; int start() { a = b & 0xFFF; return 1; } ================ Compiling gcc -c -fno-PIC -Wall -Wextra -save-temps -fverbose-asm -masm=intel -march=core2 -mtune=generic -O2 -fno-rtti -fno-default-inline --no-exceptions -O2 prog.cpp No errors No warnings Produces wrong assembly code === cut === _Z5startv: .LFB0: .cfi_startproc endbr64 # prog.cpp:5: a = b & 0xFFF; mov rax, QWORD PTR b[rip] # tmp87, b and eax, 4095 # tmp87, # prog.cpp:5: a = b & 0xFFF; mov QWORD PTR a[rip], rax # a, tmp87 # prog.cpp:7: } mov eax, 1 #, ret .cfi_endproc === cut === There is 'and' on 32bit register eax, while 'b' is 64bit variable. Bits 32...63 of 'b' stays unchanged, but must be zeroed.