public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/113801] New: Missed optimization of loop invariant elimination
@ 2024-02-07 10:22 652023330028 at smail dot nju.edu.cn
  2024-02-07 10:55 ` [Bug tree-optimization/113801] " rguenth at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: 652023330028 at smail dot nju.edu.cn @ 2024-02-07 10:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113801

            Bug ID: 113801
           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 invariant
elimination.

Loop invariant: b+d.

https://godbolt.org/z/8fn48TGKv

int a, c;
void func(int b, int d) {
  for (int i=0; i < 400; i += 1) {
    a += a;
    b += c;
    d += -c;
    a += b + d;
  }
}

But GCC -O3
func(int, int):
        mov     ecx, DWORD PTR a[rip]
        mov     r8d, DWORD PTR c[rip]
        mov     edx, 400
.L2:
        mov     eax, edi
        mov     r9d, esi
        add     edi, r8d
        sub     esi, r8d
        add     eax, r9d
        lea     ecx, [rax+rcx*2]
        sub     edx, 1
        jne     .L2
        mov     DWORD PTR a[rip], ecx
        ret

Expected code:
Like optimizing the equivalent func2:
void func2(int b, int d) {
  for (int i=0; i < 400; i += 1) {
    a += a;
    //b += c;
    //d += -c;
    a += b + d;
  }
}
Result:
func2(int, int):
        mov     edx, DWORD PTR a[rip]
        lea     ecx, [rdi+rsi]
        mov     eax, 400
.L6:
        lea     edx, [rcx+rdx*2]
        sub     eax, 1
        jne     .L6
        mov     DWORD PTR a[rip], edx
        ret

Or like Clang:
func(int, int):                              # @func(int, int)
        mov     edx, dword ptr [rip + a]
        add     edi, esi
        lea     eax, [rdi + rdi]
        mov     ecx, 400
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        lea     edx, [rax + 4*rdx]
        add     edx, edi
        lea     edx, [rax + 4*rdx]
        add     edx, edi
        lea     edx, [rax + 4*rdx]
        add     edx, edi
        lea     edx, [rax + 4*rdx]
        add     edx, edi
        add     ecx, -8
        jne     .LBB0_1
        mov     dword ptr [rip + a], edx
        ret

Thank you very much for your time and effort! We look forward to hearing from
you.

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

* [Bug tree-optimization/113801] Missed optimization of loop invariant elimination
  2024-02-07 10:22 [Bug tree-optimization/113801] New: Missed optimization of loop invariant elimination 652023330028 at smail dot nju.edu.cn
@ 2024-02-07 10:55 ` rguenth at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-02-07 10:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113801

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           Keywords|                            |missed-optimization
   Last reconfirmed|                            |2024-02-07

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think there's a duplicate bug having the same loop-carried "zero" where
final value replacement gets the overall update to 'a', just a bit more
complicated here.

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

end of thread, other threads:[~2024-02-07 10:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-07 10:22 [Bug tree-optimization/113801] New: Missed optimization of loop invariant elimination 652023330028 at smail dot nju.edu.cn
2024-02-07 10:55 ` [Bug tree-optimization/113801] " rguenth 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).