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

            Bug ID: 113716
           Summary: Missed optimization: (2/a)*b*(!b) => 0
           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
((2/a)*b*(!b) => 0), but gcc -O3 -fwrapv missed it.

https://godbolt.org/z/34Ejoxeqo

int m;
void func(int a, int b){
    a=(2/a)*b;
    m=a*(!b);
}

GCC -O3 -fwrapv:
func(int, int):
        xor     edx, edx
        mov     eax, 2
        idiv    edi
        xor     edx, edx
        test    esi, esi
        cmovne  eax, edx
        imul    eax, esi
        mov     DWORD PTR m[rip], eax
        ret

Expected code (Clang):
func(int, int):                              # @func(int, int)
        mov     dword ptr [rip + m], 0
        ret

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

Reply via email to