https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84816
Bug ID: 84816 Summary: [7.2.0/8.0.1 x86_64] Incorrect code generation if signed overflow Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: dlesnikov at hotmail dot com Target Milestone: --- Hello! # g++-7 --version g++-7 (Ubuntu 7.2.0-8ubuntu3.1~16.04.york0) 7.2.0 # g++-8 --version g++-8 (Ubuntu 8-20180218-1ubuntu1~16.04.york0) 8.0.1 20180218 (experimental) [trunk revision 257787] c++ code <sigover.cpp>: #include <stdio.h> class C1 {}; int main(void) { C1 c1; int a = 0x7FFFFFFA; for(int i=0; i<10; i++) printf("%d\n", ++a); return 0; } g++-7[8] -O2 -S sigover.cpp the loop assembler code with error: .L2: addl $1, %ebx movl $.LC0, %esi movl $1, %edi movl %ebx, %edx xorl %eax, %eax call __printf_chk jmp .L2 <-- infinite loop .cfi_endproc .LFE30: g++-7[8] -O1 -S sigover.cpp the loop assembler code without error: .L2: addl $1, %ebx movl %ebx, %edx movl $.LC0, %esi movl $1, %edi movl $0, %eax call __printf_chk cmpl $-2147483644, %ebx <-- check the end of loop jne .L2 movl $0, %eax popq %rbx .cfi_def_cfa_offset 8 ret .cfi_endproc .LFE30: if comment 1st line in main: // C1 c1; then g++-7 -O2 correctly generates the code but g++-8 -O2 still makes an infinite loop