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