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

            Bug ID: 114546
           Summary: Missed optimization: ~m || n || m+2 ==> 1
           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
(c = b || n || a ==> 1), but gcc -O3 (-fwrapv) missed it.

https://godbolt.org/z/6Kss3shKc

int a, b, c;
void test(int m, int n) {
    int t = m ^ 100;
    n = n / t;
    a = m + 2;
    b = ~m;
    c = b || n || a;
}

GCC -O3 -fwrapv:
test(int, int):
        mov     eax, esi
        lea     ecx, [rdi+2]
        mov     r8d, edi
        xor     edi, 100
        cdq
        not     r8d
        mov     DWORD PTR a[rip], ecx
        idiv    edi
        mov     DWORD PTR b[rip], r8d
        or      ecx, r8d
        mov     esi, eax
        xor     eax, eax
        or      esi, ecx
        setne   al
        mov     DWORD PTR c[rip], eax
        ret

Expected code (Clang):
test(int, int):                              # @test(int, int)
        lea     eax, [rdi + 2]
        mov     dword ptr [rip + a], eax
        not     edi
        mov     dword ptr [rip + b], edi
        mov     dword ptr [rip + c], 1
        ret

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

Reply via email to