public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/98709] New: gcc optimizes bitwise operations, but doesn't optimize logical ones
@ 2021-01-17  1:31 vanyacpp at gmail dot com
  2021-01-17 17:34 ` [Bug middle-end/98709] " glisse at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: vanyacpp at gmail dot com @ 2021-01-17  1:31 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-10-22  2:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-17  1:31 [Bug middle-end/98709] New: gcc optimizes bitwise operations, but doesn't optimize logical ones vanyacpp at gmail dot com
2021-01-17 17:34 ` [Bug middle-end/98709] " glisse at gcc dot gnu.org
2021-08-01 18:17 ` [Bug tree-optimization/98709] " pinskia at gcc dot gnu.org
2022-08-10 11:46 ` vanyacpp at gmail dot com
2022-10-22  2:13 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).