https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128102

--- Comment #2 from 颜森道 <yansendao at 126 dot com> ---
Bisection result for the needs-bisection keyword.

The regression is introduced by Aldy Hernandez's jump threading rework for
GCC 12 — the series that disabled threading through loop latches until after
loop optimizations — first commit:

  01b503871805  "Disable threading through latches until after loop
                 optimizations."  (Aldy Hernandez, 2021-09-09)

followed by the same empty-latch / loop-crossing guards being added to
jt_path_registry::cancel_invalid_paths:
  4a960d548b7d  (2021-09-23)  empty-latch check
  d8edfadfc7a9  (2021-10-04)  "Path crosses loop header" check
  ec0124e0acb5  (2021-10-05)  "Path crosses loops" check

All of these land after GCC 11.4 (2021-04), so GCC 12 is the first release
affected.

On GCC 11 the early threader threads the state machine (thread1):
  Registering FSM jump thread: (6,10) incoming; (10,11)(11,3)(3,4)(4,5)(5,7)
  nocopy; ...
The latch is non-empty (5 insns) and quad_pos becomes constant on the 4
threaded switch paths.

On trunk the early threader rejects the same thread, in order:
  FAIL: Thread through latch before loop opts would create non-empty latch
  Path crosses loop header but does not exit it
  Path rotates loop
and the late threader (after loop opts, where these guards are off) rejects:
  FAIL: Did not thread around loop and would copy too many statements
  (net_insns * fsm-scale-path-stmts >= max-jump-thread-duplication-stmts,
   i.e. 23 * 2 >= 15).

Suggested fix (cost-model only, no loop-form change): the late threader can
thread the loop if the x2 fsm-scale-path-stmts penalty is removed or the
duplication budget raised.  Both reproduce GCC 11's codegen and restore the
performance on this machine:

  --param=fsm-scale-path-stmts=1               -> 0x30b / 11 cmp / 2.10 cyc/B
  --param=max-jump-thread-duplication-stmts=20 -> 0x2f1 / 10 cmp / 2.38 cyc/B
  default                                      -> 0xe3  /  1 cmp / 4.12 cyc/B
  GCC 11.4.0                                   -> 0x2c3 / 11 cmp / 2.23 cyc/B

So the likely fix is to exempt multiway-branch (switch) threads from the
fsm-scale-path-stmts penalty in the late pass, or raise the duplication
budget for such threads.

Reply via email to