https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112687
Bug ID: 112687 Summary: missed-optimization: switch statement does not simplify to it's expression Product: gcc Version: 13.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: goon.pri.low at gmail dot com Target Milestone: --- This following code: int unopt(int v) { switch (v & 3) { case 0: return 0; case 1: return 1; case 2: return 2; case 3: return 3; default: __builtin_unreachable(); } } unopt: mov eax, edi and eax, 3 lea edx, [rax-1] cmp edx, 3 mov edx, 0 cmovnb eax, edx ret Ought to be optimized to: int opt(int v) { return v & 3; } opt: mov eax, edi and eax, 3 ret Note: I decided to separate this problem from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112645 since it was unrelated to the main bug, though Richard offers some initial insight into the problem.