https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112723
Bug ID: 112723 Summary: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: 652023330028 at smail dot nju.edu.cn Target Milestone: --- Hello, we noticed that maybe there is a missed optimization for invariants 'c+c' when c+= -2147483648 and c is a global variable. Such missed optimization affects many related optimizations, such as LICM (where we found this issue), CSE, and others. To describe this issue more clearly, we show an example of CSE. And we attach a link to an example of LICM at the end. In the following, the value of c+c is invariant between the two expressions. In this example, c is the global variable. https://godbolt.org/z/Y3djqKWjh int a, b, c; void test() { a = c + c; c += -2147483648; b = c + c; } But GCC -O3 -fwrapv or GCC -O3: test(): movl c(%rip), %eax leal (%rax,%rax), %edx addl $-2147483648, %eax movl %eax, c(%rip) addl %eax, %eax #Redundant computation movl %edx, a(%rip) movl %eax, b(%rip) ret Expected code (Clang): test(): # @test() mov eax, dword ptr [rip + c] lea ecx, [rax + rax] mov dword ptr [rip + a], ecx add eax, -2147483648 mov dword ptr [rip + c], eax mov dword ptr [rip + b], ecx ret When c appears as a function argument, it works as expected: https://godbolt.org/z/Tf6hP6M9W int a, b; void test(int c) { a = c + c; c += -2147483648; b = c + c; } GCC -O3: test(int): leal (%rdi,%rdi), %eax movl %eax, a(%rip) movl %eax, b(%rip) ret An example of LICM affected by this: https://godbolt.org/z/5T89TWaG9 Thank you very much for your time and effort! We look forward to hearing from you.