On Tue, Oct 27, 2026 at 11:23 AM Richard Biener <[email protected]> 
wrote:
> I wonder if a more appropriate fix is to move lowering switches to
> after the first FSM threading opportunity.

I tried that: I moved pass_lower_switch to after the late
pass_thread_jumps_full (threadfull2), the FSM threading instance that
actually threads this loop (the switch already survives to the early
threadfull1, but that instance does not thread the path).  It fixes the
testcase -- the switch is still a GIMPLE_SWITCH when the backward threader
runs, the thread starts at the switch block, and the loop body is replicated
so the '=' test is left 11 times.  The benchmark goes from 4.10 cycles/byte
on unpatched trunk to 2.11 cycles/byte at -O3 (2.11 at -O2), back to the
GCC 11 level, with no relaxation of the duplication limits.  So the
mechanism is confirmed.

However, the reorder is not zero-regression.  The full testsuite shows one
new failure:

  gcc.dg/uninit-pr44547.c (test for excess errors)
  uninit-pr44547.c:24:16: warning: 'n' may be used uninitialized 
[-Wmaybe-uninitialized]

That test guards against a false positive in nested switch statements.  With
the switch lowered only after threadfull2, at -O1 there is not enough
follow-on optimization left to fold away the unreachable default branch of
the inner switch, so a spurious "argc == 0" edge carrying the uninitialized
'n' survives into the return PHI and -Wmaybe-uninitialized flags it.  The
-O2 function in the same file is unaffected -- only -O1 runs out of runway.

So the reorder fixes the performance issue but trades it for a
diagnostic-quality regression on nested switches, exactly the case PR 44547
guards, and does not look like a clean fix on its own.

Which direction would you prefer: keep the duplication-limit change but
narrow it to "the same branching sequence" as you describe, teach the
backward threader to recognize an already-lowered switch (a chain of
conditionals implementing the state dispatch) as a multi-way branch so no
pass reordering is needed, or fix the uninit analysis to cope with the later
lowering?  I'm happy to pursue whichever you think is best.

Reply via email to