public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/97690] New: (cond ? 2 : 0) is not optimized to int(cond) << 1
@ 2020-11-03 10:28 redi at gcc dot gnu.org
  2020-11-03 10:30 ` [Bug tree-optimization/97690] " redi at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2020-11-03 10:28 UTC (permalink / raw)
  To: gcc-bugs

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

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

end of thread, other threads:[~2021-06-01 20:50 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-03 10:28 [Bug tree-optimization/97690] New: (cond ? 2 : 0) is not optimized to int(cond) << 1 redi at gcc dot gnu.org
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

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).