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.
  • [Bug tree-optimization/1... 652023330028 at smail dot nju.edu.cn via Gcc-bugs

Reply via email to