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

            Bug ID: 114782
           Summary: [Regression] Missed optimization of Induction Variable
           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 for the following code, GCC may have missed an
optimization of inductive variables ('x'). It may be a regression from
gcc-4.8.1.

https://godbolt.org/z/je9sjzPj6

int a,b;
void func(int x){
    for(int i=0;i<b;i++){
        x=i*5;
        a+=x;
        b--;
    }
}

GCC-trunk -O3:
func(int):
        mov     edx, DWORD PTR b[rip]
        test    edx, edx
        jle     .L1
        mov     ecx, DWORD PTR a[rip]
        xor     eax, eax
.L3:
        lea     esi, [rax+rax*4]
        sub     edx, 1
        add     eax, 1
        add     ecx, esi
        cmp     edx, eax
        jg      .L3
        mov     DWORD PTR a[rip], ecx
        mov     DWORD PTR b[rip], edx
.L1:
        ret

Expected code:
GCC-4.7.4 -O3:
func(int):
        mov     esi, DWORD PTR b[rip]
        test    esi, esi
        jle     .L1
        mov     ecx, DWORD PTR a[rip]
        lea     edi, [rsi-1]
        xor     edx, edx
        xor     eax, eax
.L3:
        mov     esi, edi
        add     ecx, edx
        add     edx, 5
        sub     esi, eax
        add     eax, 1
        cmp     eax, esi
        jl      .L3
        mov     DWORD PTR a[rip], ecx
        mov     DWORD PTR b[rip], esi
.L1:
        rep
        ret

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

Reply via email to