From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 273213858C36; Mon, 27 Nov 2023 09:42:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 273213858C36 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1701078170; bh=A5JuP6sZiN2Na1KmDk17b8ncOUPXwDBbp9RL57XMVqs=; h=From:To:Subject:Date:From; b=Svx8gLCLxbVwOzJEWbvOIa9nM20Rstd7exAoc0QFEDuhzKsTwoWP/E3w4w+1WNXNm +bzTMZ/tUULjTKJuWQNEZF99WOHBroTLCHfAFdt9k+zkj59O5UEPgXXcRGk+bXgkEV Teutiuan5XsHZGbsSnrygko8KdOMclw47wmCElj8= From: "652023330028 at smail dot nju.edu.cn" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: 652023330028 at smail dot nju.edu.cn X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D112723 Bug ID: 112723 Summary: Missed optimization for invariants 'c+c' when c +=3D -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+=3D -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 atta= ch 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 =3D c + c; c +=3D -2147483648; b =3D 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 =3D c + c; c +=3D -2147483648; b =3D 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 fr= om you.=