https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107443
Bug ID: 107443 Summary: Switch conversion removing code Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: amacleod at redhat dot com Target Milestone: --- Compile the following with -O2 -fdump-tree-all -fdisable-tree-evrp void dead (unsigned n); void foo (unsigned n); void func (unsigned n) { if (n == 0) __builtin_unreachable(); if (n == 1) __builtin_unreachable(); if (n == 2) __builtin_unreachable(); if (n == 3) __builtin_unreachable(); if (n == 4) __builtin_unreachable(); if (n == 5) __builtin_unreachable(); if (n == 6) __builtin_unreachable(); if (n == 7) __builtin_unreachable(); foo (n); if (n < 8) dead (n); } iftoswitch converts the series of if's into a switch: <bb 2> : switch (n_2(D)) <default: <L26> [INV], case 0: <L18> [INV], case 1: <L19> [INV], case 2: <L20> [INV], case 3: <L21> [INV], case 4: <L22> [INV], case 5: <L23> [INV], case 6: <L24> [INV], case 7: <L25> [INV]> <bb 3> : <L18>: __builtin_unreachable (); <bb 4> : <L19>: __builtin_unreachable (); <bb 5> : <L20>: __builtin_unreachable (); <bb 6> : <L21>: __builtin_unreachable (); <bb 7> : <L22>: __builtin_unreachable (); <bb 8> : <L23>: __builtin_unreachable (); <bb 9> : <L24>: __builtin_unreachable (); <bb 10> : <L25>: __builtin_unreachable (); <bb 11> : <L26>: foo (n_2(D)); if (n_2(D) <= 7) goto <bb 12>; [INV] else goto <bb 13>; [INV] <bb 12> : dead (n_2(D)); switch conversion then runs, eliminates all the blocks, then bails on the conversion, leaving the IL without any of the __builtin_unreachable calls: Beginning to process the following SWITCH statement ((null):0) : ------- switch (n_2(D)) <default: <L26> [INV], case 0: <L18> [INV], case 1: <L19> [INV], case 2: <L20> [INV], case 3: <L21> [INV], case 4: <L22> [INV], case 5: <L23> [INV], case 6: <L24> [INV], case 7: <L25> [INV]> Removing basic block 3 Removing basic block 4 Removing basic block 5 Removing basic block 6 Removing basic block 7 Removing basic block 8 Removing basic block 9 Removing basic block 10 Bailing out - switch is a degenerate case -------------------------------- Merging blocks 2 and 11 void func (unsigned int n) { <bb 2> : foo (n_2(D)); if (n_2(D) <= 7) goto <bb 3>; [INV] else goto <bb 4>; [INV] <bb 3> : dead (n_2(D)); <bb 4> : return; } And now the optimizers cannot remove the call to dead() because we lose all the range information inferred from the __builtin_unreachable calls.