https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94779
Bug ID: 94779 Summary: Bad optimization of simple switch Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: gabravier at gmail dot com Target Milestone: --- int f1(unsigned x) { switch (x) { case 0: return 1; case 1: return 2; } } gcc fails to optimize this to `return x + 1`, instead opting for some rather weird code generation (involving `sbb` on x86). However, adding this : if (x >= 2) __builtin_unreachable(); at the beginning of the function makes it be optimized properly. Maybe this is a sign of the `x >= 2` condition being always false (due to it leading to UB) being found too late ?