public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/114086] New: Boolean switches could have a lot better codegen, possibly utilizing bit-vectors
@ 2024-02-24  9:58 janschultke at googlemail dot com
  2024-02-24 10:14 ` [Bug middle-end/114086] " jakub at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: janschultke at googlemail dot com @ 2024-02-24  9:58 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114086
           Summary: Boolean switches could have a lot better codegen,
                    possibly utilizing bit-vectors
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: janschultke at googlemail dot com
  Target Milestone: ---

https://godbolt.org/z/3acqbbn3E

enum struct E { a, b, c, d, e, f, g, h };

bool test_switch(E e) {
    switch (e) {
    case E::a:
    case E::c:
    case E::e:
    case E::g: return true;
    default: return false;
    }
}


Expected output
===============

test_switch(E):
  mov eax, edi
  and eax, 1
  ret



Actual output (-O3)
===================

test_switch(E):
  xor eax, eax
  cmp edi, 6
  ja .L1
  mov eax, 85
  bt rax, rdi
  setc al
.L1:
  ret


Explanation
===========

Boolean switches in general can be optimized a lot better than what GCC
currently does. Clang does find the optimization to a bitwise AND, although
this may be a big ask.

Generally, contiguous boolean switches (that is, switch statements where all
cases yield a boolean value and the labels are contiguous) can be optimized to
accessing a bit vector.

That switch could have been transformed into:

> return 0b01010101 >> int(e);

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

end of thread, other threads:[~2024-02-26 14:58 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-24  9:58 [Bug middle-end/114086] New: Boolean switches could have a lot better codegen, possibly utilizing bit-vectors janschultke at googlemail dot com
2024-02-24 10:14 ` [Bug middle-end/114086] " jakub at gcc dot gnu.org
2024-02-24 10:49 ` janschultke at googlemail dot com
2024-02-24 11:02 ` jakub at gcc dot gnu.org
2024-02-24 11:22 ` jakub at gcc dot gnu.org
2024-02-24 11:27 ` janschultke at googlemail dot com
2024-02-24 11:52 ` [Bug tree-optimization/114086] " jakub at gcc dot gnu.org
2024-02-24 12:13 ` jakub at gcc dot gnu.org
2024-02-26  8:38 ` rguenth at gcc dot gnu.org
2024-02-26 12:04 ` jakub at gcc dot gnu.org
2024-02-26 14:58 ` amacleod at redhat dot com

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