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

            Bug ID: 112277
           Summary: Missed optimization of loop deletion because of missed
                    loopUnswitch and useless instruction 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 the loop in the code below is not necessary. But GCC
misses this loop deletion optimization, probably because of the missing in loop
Unswitch and useless instruction elimination annotated below.

https://godbolt.org/z/vMPGnhc9n
int b, c;
int n;
void test() {
  for (int i = 0; i < 1000; i += b) {
    if(c < 0){
        c = n;
    }
  }
}

GCC -O3:
test():
        mov     ecx, DWORD PTR c[rip]
        mov     edi, DWORD PTR n[rip]
        xor     esi, esi
        xor     eax, eax
        mov     edx, DWORD PTR b[rip]
        test    ecx, ecx
        jns     .L3
.L2:                          #Loop
        add     eax, edx
        mov     esi, 1
        mov     ecx, edi      #Loop invariants: edi
        cmp     eax, 999
        jg      .L13
        test    ecx, ecx      #Loop invariants: ecx
        js      .L2           #Missed LoopUnswitch
.L3:                          #Useless instructions
        add     eax, edx
        cmp     eax, 999
        jle     .L3           #Useless instructions
        test    sil, sil
        je      .L1
.L13:
        mov     DWORD PTR c[rip], ecx
.L1:
        ret

Expected code:
test():
        mov     eax, DWORD PTR c[rip]
        test    eax, eax
        js      .L4
        ret
.L4:
        mov     eax, DWORD PTR n[rip]
        mov     DWORD PTR c[rip], eax
        ret

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

Reply via email to