public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "652023330028 at smail dot nju.edu.cn" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/112723] New: Missed optimization for invariants 'c+c' when c += -2147483648 and c is a global variable
Date: Mon, 27 Nov 2023 09:42:49 +0000	[thread overview]
Message-ID: <bug-112723-4@http.gcc.gnu.org/bugzilla/> (raw)

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.

             reply	other threads:[~2023-11-27  9:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-27  9:42 652023330028 at smail dot nju.edu.cn [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-112723-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).