From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 02199385AC12; Tue, 16 Jan 2024 14:42:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 02199385AC12 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705416132; bh=4/nUDHbri6QpNQ3b7krbxJNrCtYEo2Tv85lrTOYL0+8=; h=From:To:Subject:Date:From; b=I40v53MnMqKkl4WQhZQJ271Qe2/IwsWrenzhCXG87gTwvRlGndEQK4w1jxJTRx0WH OblTkTcUDb3bmMusOa0bWE4CxIFpYCaI/cF2YVLMSJDEHRLiuFyf5x1kmAkhct49+2 z3Y/oehsBWHtr4cxWYD4CDinREYQlscepU1U8Fhc= From: "652023330028 at smail dot nju.edu.cn" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/113426] New: Missed optimization of loop invariant elimination Date: Tue, 16 Jan 2024 14:42:11 +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=3D113426 Bug ID: 113426 Summary: Missed optimization of loop invariant elimination 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 loop invari= ant elimination. Loop invariant: a+b. https://godbolt.org/z/Ebzd3dMPW int a, b, c; int n; int w; void func(){ for(int i=3D0;i<100;i++){ c=3Dn; a+=3Dw; b-=3Dw; n=3Da+b; } } GCC -O3: func(): mov r9d, DWORD PTR a[rip] mov r10d, DWORD PTR b[rip] mov eax, 100 mov edi, DWORD PTR n[rip] mov esi, DWORD PTR w[rip] mov ecx, r10d mov edx, r9d .L2: mov r8d, edi lea edi, [rcx+rdx]=20=20 sub ecx, esi add edx, esi sub eax, 1 jne .L2 imul edx, esi, -99 lea eax, [rsi+r9] add r9d, r10d mov DWORD PTR c[rip], r8d mov DWORD PTR n[rip], r9d sub eax, edx mov DWORD PTR a[rip], eax mov eax, r10d sub eax, esi add eax, edx mov DWORD PTR b[rip], eax ret Expected code (Clang): func(): # @func() mov eax, dword ptr [rip + a] mov ecx, dword ptr [rip + b] lea edx, [rcx + rax] imul esi, dword ptr [rip + w], 100 add eax, esi sub ecx, esi mov dword ptr [rip + n], edx mov dword ptr [rip + c], edx mov dword ptr [rip + a], eax mov dword ptr [rip + b], ecx ret We analyze and find that in the optimization results of GCC, the calculatio= n of n is actually outside the loop (add r9d, r10d; mov DWORD PTR n[rip], r9d).= =20 However, because of the effect of c=3Dn, a+b is calculated once each time t= hrough the loop. Hope this analysis will help with this issue. Thank you very much for your time and effort! We look forward to hearing fr= om you.=