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

            Bug ID: 112123
           Summary: Missed optimization of loop deletion because loop
                    invariants are not identified
           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 in the following loop, a+b is a loop invariant, so the
value of m can be computed directly. The values of a and b can also be computed
without loop iteration.

https://godbolt.org/z/6xTE7zrPj

int m;

int a, b;
void test() {
  for (int c = 0; c < 4087153; c += 1) {
    m = a + b;
    a += b;
    b += -b;
  }
}

GCC:
test():
        mov     edx, DWORD PTR a[rip]
        mov     ecx, DWORD PTR b[rip]
        mov     eax, 4087153
.L2:
        add     edx, ecx
        xor     ecx, ecx
        sub     eax, 1
        jne     .L2
        mov     DWORD PTR m[rip], edx
        mov     DWORD PTR a[rip], edx
        mov     DWORD PTR b[rip], 0
        ret

Expected code (Clang):
test():                               # @test()
        mov     eax, dword ptr [rip + b]
        add     eax, dword ptr [rip + a]
        mov     dword ptr [rip + a], eax
        mov     dword ptr [rip + b], 0
        mov     dword ptr [rip + m], eax
        ret

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

Reply via email to