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

            Bug ID: 98709
           Summary: gcc optimizes bitwise operations, but doesn't optimize
                    logical ones
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vanyacpp at gmail dot com
  Target Milestone: ---

GCC 10.2 produces very good code for this function noticing that both sides of
conjuntion are the same:

unsigned foo_bitwise(unsigned a, unsigned b)
{
    return (~a ^ b) & ~(a ^ b);
}

foo_bitwise(unsigned int, unsigned int):
        xor     edi, esi
        mov     eax, edi
        not     eax
        ret

But when I write a similar function with logical operations it doesn't notice
that:

bool foo_logical(bool a, bool b)
{
    return (!a ^ b) & !(a ^ b);
}

foo_logical(bool, bool):
        mov     eax, esi
        xor     eax, edi
        xor     eax, 1
        cmp     dil, sil
        sete    dl
        and     eax, edx
        ret

I believe that in a similar manner it can be optimized to something like this:

foo_logical(bool, bool):
        xor     edi, esi
        mov     eax, edi
        xor     eax, 1
        ret

Reply via email to