public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/97690] New: (cond ? 2 : 0) is not optimized to int(cond) << 1
Date: Tue, 03 Nov 2020 10:28:49 +0000	[thread overview]
Message-ID: <bug-97690-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 97690
           Summary: (cond ? 2 : 0) is not optimized to int(cond) << 1
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

enum E { init=0, active=1, done=2 };

int f(bool d)
{
    return d ? done : init;
}

int g(bool d)
{
    return int(d) << 1;
}

The first function is more readable and less fragile (in case the order of bits
is changed) but GCC produces larger code for it.

For x86_64 with -O3:

f(bool):
        xor     eax, eax
        test    dil, dil
        setne   al
        add     eax, eax
        ret
g(bool):
        movzx   eax, dil
        add     eax, eax
        ret

And for x86_64 -Os:

f(bool):
        neg     dil
        sbb     eax, eax
        and     eax, 2
        ret
g(bool):
        movzx   eax, dil
        add     eax, eax
        ret


Clang produces the same code for both functions at all optimization levels:

f(bool):                                  # @f(bool)
        lea     eax, [rdi + rdi]
        ret
g(bool):                                  # @g(bool)
        lea     eax, [rdi + rdi]
        ret

             reply	other threads:[~2020-11-03 10:28 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-03 10:28 redi at gcc dot gnu.org [this message]
2020-11-03 10:30 ` [Bug tree-optimization/97690] " redi at gcc dot gnu.org
2020-11-03 10:37 ` redi at gcc dot gnu.org
2020-11-03 12:55 ` rguenth at gcc dot gnu.org
2020-11-03 17:32 ` jakub at gcc dot gnu.org
2020-11-04 11:15 ` cvs-commit at gcc dot gnu.org
2021-05-24  7:12 ` pinskia at gcc dot gnu.org
2021-06-01 20:50 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-97690-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).