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

            Bug ID: 115483
           Summary: Missed optimization for loop invariant code
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zhiwuyazhe154 at gmail dot com
  Target Milestone: ---

Godbolt example: https://godbolt.org/z/s75d4GfTT

code example:
unsigned m,n;
unsigned func(unsigned a, unsigned b, 
            unsigned c, unsigned d)
{
    for(unsigned i=0; i<100; i++)
    {
        m += 1024;
        b += c/n + (c + d) + m;
        m += m;
    }
    return b;
} 

In this case, c/n + (c + d) is loop invariant code.
It seems that reassociate breaks the optimization of licm.
There are a few other issues that mention the shortcomings of reassociate, but
there doesn't seem to be an issue that shows its negative impact.

GCC -O3:
func(unsigned int, unsigned int, unsigned int, unsigned int):
        mov     eax, edx
        mov     r9d, edx
        xor     edx, edx
        mov     r8d, esi
        div     DWORD PTR n[rip]
        mov     esi, DWORD PTR m[rip]
        mov     edi, 100
.L2:
        add     esi, 1024
        lea     edx, [rsi+rcx]
        add     esi, esi
        add     edx, r9d
        add     edx, eax
        add     r8d, edx
        sub     edi, 1
        jne     .L2
        mov     DWORD PTR m[rip], esi
        mov     eax, r8d
        ret

Reply via email to