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

            Bug ID: 114277
           Summary: Missed optimization: x*(x||b) => x
           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
(x*(x||b) => x), but gcc -O3 missed it.

https://godbolt.org/z/W8xYrGdej

int a,b;
void func(int x){
    a=x*(x||b);
}

GCC -O3:
func(int):
        mov     eax, edi
        or      eax, DWORD PTR b[rip]
        mov     eax, 0
        cmove   edi, eax
        mov     DWORD PTR a[rip], edi
        ret

Expected code:
func(int):
        mov     DWORD PTR a[rip], edi
        ret

Surprisingly, GCC optimizes as expected for the following slightly more complex
code:
int c;
void func2(int x){
    a=x*(x||(b&&c));
}
func2(int):
        mov     DWORD PTR a[rip], edi
        ret

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

Reply via email to