From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 3CC343858C54; Tue, 28 Nov 2023 16:10:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3CC343858C54 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1701187810; bh=aPuN7gRyzTBwWLv1QIXMuawVPlSqM4dOJeW7n3CYMYI=; h=From:To:Subject:Date:From; b=BTHDalU1qS8JhdZ7GMPkiICX3+qEC3lq626cK01T61E5AYJP44kwTdOS31mkxCmnd X4dcz+HGRh75159OBQYdM+6Rv1BNpWNvEVzXA9CftOQbktgNcjKiM8gbi9Tv5AJQ4N F9DteM8Jj00tfvtMwLeobnkXHqR1YSSACRlKbVTo= From: "652023330028 at smail dot nju.edu.cn" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/112746] New: Missed optimization for redundancy computation elimination (fre1(tree) for global variable) Date: Tue, 28 Nov 2023 16:10:09 +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=3D112746 Bug ID: 112746 Summary: Missed optimization for redundancy computation elimination (fre1(tree) for 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 redundancy computation elimination. When b is a local variable, the computation of m optimizes to 3 as expected. When b appears as a global variable, it misses this optimization. https://godbolt.org/z/j3cKsrKb6 int n,m; int b; void test(int b){ b=3Dn+n; m=3D(n+b)/(n); } void test2(){ b=3Dn+n; m=3D(n+b)/(n); } But GCC -O3 -fwrapv or GCC -O3: test(int): mov DWORD PTR m[rip], 3 ret test2(): mov ecx, DWORD PTR n[rip] lea eax, [rcx+rcx] mov DWORD PTR b[rip], eax add eax, ecx cdq idiv ecx mov DWORD PTR m[rip], eax ret Expected code (Clang): test2(): # @test2() mov eax, dword ptr [rip + n] add eax, eax mov dword ptr [rip + b], eax mov dword ptr [rip + m], 3 ret We see a difference in fre1(tree) for these two functions, which might help with this issue: void test (int b) { int n.0_1; int _3; int _5; : # DEBUG BEGIN_STMT n.0_1 =3D n; b_7 =3D n.0_1 * 2; # DEBUG b =3D> b_7 # DEBUG BEGIN_STMT _3 =3D n.0_1 * 3; _5 =3D 3; m =3D 3; return; } void test2 () { int n.3_1; int _2; int _5; int _7; : # DEBUG BEGIN_STMT n.3_1 =3D n; _2 =3D n.3_1 * 2; b =3D _2; # DEBUG BEGIN_STMT _5 =3D n.3_1 + _2; _7 =3D _5 / n.3_1; m =3D _7; return; } Thank you very much for your time and effort! We look forward to hearing fr= om you.=