https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128962
--- Comment #5 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jeff Law <[email protected]>: https://gcc.gnu.org/g:369b3893ad7218dbdd62dc3b7a57e4c47036ff93 commit r17-6778-g369b3893ad7218dbdd62dc3b7a57e4c47036ff93 Author: Sendao Yan <[email protected]> Date: Sat Nov 28 09:56:08 2026 -0700 [PATCH v3] tree-optimization/128962 - only defer switch lowering for data dependent switches The late threaders were made to see GIMPLE_SWITCH by moving pass_lower_switch after them (r17-6332-gc9609467230d5d), which helps state-machine switches such as the base64 decoder in CPython. For a switch whose controlling value is loop invariant, however, the compare chain produced by lowering is evaluated with the same outcome on every iteration, so the threaders gain nothing from seeing it: they only duplicate the whole case chain once per case value, which grows the function and can cost more in i-cache misses than the removed branches save. On 548.exchange2_r this makes digits_2 20% larger and costs 4-6% on Zen5 and Xeon 8480 (PR tree-optimization/128962). Add a pass_lower_switch_early instance, run before cse_sincos, that lowers the switches whose controlling value is invariant in their innermost loop; switches outside any natural loop (straight line code and irreducible regions) are left to the threaders. The instance after the late threaders lowers the remaining switches, including all the ones outside any loop, so data dependent state machines are still visible to the threaders and the straight line code is lowered exactly as before. Note that a switch that is expanded into a jump table re-emits a GIMPLE_SWITCH, so such switches stay visible to the threaders; handling them is left for a follow-up. This restores the exchange2_r performance without giving up the switch visibility that r17-6332 added. PR tree-optimization/128962 gcc/ChangeLog: * passes.def: Run pass_lower_switch_early before pass_cse_sincos. * tree-pass.h (make_pass_lower_switch_early): Declare. * tree-switch-conversion.cc (pass_data_lower_switch_early): New. (pass_lower_switch): Store the pass_data instead of using a static member, and add only_invariant_p. (pass_lower_switch::execute): Only lower the switches that are invariant in their innermost loop for the early instance, decided in the first walk over the blocks so that the skipped switches are not passed to group_case_labels_stmt; mark a preserved loop tree for fixup when the lowering changed the CFG. (make_pass_lower_switch_early): New. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/switchlower-early-1.c: New test. * gcc.dg/tree-ssa/switchlower-early-2.c: New test. Signed-off-by: Sendao Yan <[email protected]>
