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

            Bug ID: 114009
           Summary: Missed optimization: (!a) * a => 0 when a=(a/2)*2
           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 code below can be optimized as stated in the title
((!a) * a => 0), but gcc -O3 missed it.

Different from PR 113716, this example can be optimized under the option of gcc
-O3 -fwrapv. At the same time, their patterns are different.

https://godbolt.org/z/GrPY1d6o3

int w;
void func(int a) {
    a = (a/2)*2;
    w = (!a) * a;
}

GCC -O3 :
func(int):
        mov     eax, edi
        xor     edx, edx
        shr     eax, 31
        add     eax, edi
        add     edi, 1
        sar     eax
        cmp     edi, 2
        cmova   eax, edx
        add     eax, eax
        mov     DWORD PTR w[rip], eax
        ret

Expected code (GCC -O3 -fwrapv):
func(int):
        mov     DWORD PTR w[rip], 0
        ret

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

Reply via email to