public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable
@ 2023-11-27  9:42 652023330028 at smail dot nju.edu.cn
  2023-11-27  9:51 ` [Bug tree-optimization/112723] " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: 652023330028 at smail dot nju.edu.cn @ 2023-11-27  9:42 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2024-04-13 14:10 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-27  9:42 [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable 652023330028 at smail dot nju.edu.cn
2023-11-27  9:51 ` [Bug tree-optimization/112723] " pinskia at gcc dot gnu.org
2023-11-27  9:55 ` rguenth at gcc dot gnu.org
2023-11-27  9:56 ` rguenth at gcc dot gnu.org
2024-03-28 11:13 ` [Bug tree-optimization/112723] Missed optimization for invariants 'c+c' when c += -2147483647-1 " 652023330028 at smail dot nju.edu.cn
2024-03-28 17:06 ` [Bug tree-optimization/112723] [11/12/13/14 Regression] " pinskia at gcc dot gnu.org
2024-03-28 17:18 ` pinskia at gcc dot gnu.org
2024-03-28 17:47 ` pinskia at gcc dot gnu.org
2024-04-05 15:07 ` jakub at gcc dot gnu.org
2024-04-05 15:13 ` jakub at gcc dot gnu.org
2024-04-13 14:10 ` law at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).