https://gcc.gnu.org/g:d9254f775ce7915885f4ff9e7ae3b75f4b2ceade

commit r17-5809-gd9254f775ce7915885f4ff9e7ae3b75f4b2ceade
Author: Kyrylo Tkachov <[email protected]>
Date:   Thu Oct 15 20:15:49 2026 +0200

    common: Enable -fduplicate-switch-dispatch at -O2 [PR middle-end/126236]
    
    The switch-dispatch transformation trades code size for dispatch speed.  At
    -O2 on an AArch64 Olympus system it improves mawk by 5.7% and QuickJS-ng by
    25.2%.  Their text changes are 3.73% and 0.90%, respectively.  At -Ofast it
    improves 714.cpython_r by 7.7% with a 0.20% text change.
    
    Enable the option at -O2 for speed builds, where the existing computed-goto
    unfactoring also runs.  Keep it disabled for -Os, -Oz, and size-optimized
    functions.  It applies to every target that does not refuse to share jump
    tables through TARGET_CAN_SHARE_JUMP_TABLE_P.
    
    gcc/ChangeLog:
    
            PR middle-end/126236
            * opts.cc (default_options_table): Enable
            -fduplicate-switch-dispatch at -O2 for speed builds.
            * doc/invoke.texi: Document it.  List it among the options that
            -Os does not enable.
    
    gcc/testsuite/ChangeLog:
    
            PR middle-end/126236
            * gcc.dg/switch-dispatch-5.c: New test.
            * gcc.target/aarch64/switch-dispatch-1.c: Test default enablement at
            -O2.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/doc/invoke.texi                                  |  5 ++++-
 gcc/opts.cc                                          |  1 +
 gcc/testsuite/gcc.dg/switch-dispatch-5.c             | 10 ++++++++++
 gcc/testsuite/gcc.target/aarch64/switch-dispatch-1.c |  2 +-
 4 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index e1b452e81e3c..6139462669b2 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -13663,6 +13663,7 @@ also turns on the following optimization flags:
 -fcse-follow-jumps  -fcse-skip-blocks
 -fdelete-null-pointer-checks -fdep-fusion
 -fdevirtualize  -fdevirtualize-speculatively
+-fduplicate-switch-dispatch
 -fexpensive-optimizations
 -ffinite-loops
 -fgcse  -fgcse-lm
@@ -13736,7 +13737,7 @@ Optimize for size.  @option{-Os} enables all 
@option{-O2} optimizations
 except those that often increase code size:
 
 @gccoptlist{-falign-functions  -falign-jumps
--falign-labels  -falign-loops
+-falign-labels  -falign-loops  -fduplicate-switch-dispatch
 -fprefetch-loop-arrays  -freorder-blocks-algorithm=stc}
 
 It also enables @option{-finline-functions}, causes the compiler to tune for
@@ -14322,6 +14323,8 @@ It trades size for speed, so the amount copied is 
bounded by
 with @option{-fexpensive-optimizations} and only in functions that are
 optimized for speed.
 
+Enabled at levels @option{-O2}, @option{-O3}.
+
 @opindex fif-conversion
 @opindex fno-if-conversion
 @item -fif-conversion
diff --git a/gcc/opts.cc b/gcc/opts.cc
index 21a773f8da75..ec98edc9a515 100644
--- a/gcc/opts.cc
+++ b/gcc/opts.cc
@@ -685,6 +685,7 @@ static const struct default_options default_options_table[] 
=
     { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_jumps, NULL, 1 },
     { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_labels, NULL, 1 },
     { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_loops, NULL, 1 },
+    { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_fduplicate_switch_dispatch, NULL, 1 },
     { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_foptimize_strlen, NULL, 1 },
     { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_freorder_blocks_algorithm_, NULL,
       REORDER_BLOCKS_ALGORITHM_STC },
diff --git a/gcc/testsuite/gcc.dg/switch-dispatch-5.c 
b/gcc/testsuite/gcc.dg/switch-dispatch-5.c
new file mode 100644
index 000000000000..380b222dc578
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/switch-dispatch-5.c
@@ -0,0 +1,10 @@
+/* PR middle-end/126236 */
+/* { dg-do run } */
+/* { dg-options "-O2 --param=max-goto-duplication-insns=128 
--param=case-values-threshold=1 -fdump-rtl-compgotos-details" } */
+
+/* -O2 enables -fduplicate-switch-dispatch, so both tables of
+   switch-dispatch-run-5.c are shared.  */
+
+#include "torture/switch-dispatch-run-5.c"
+
+/* { dg-final { scan-rtl-dump-times {addr_(?:diff_)?vec/u} 2 "compgotos" { 
target shared_jump_tables } } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/switch-dispatch-1.c 
b/gcc/testsuite/gcc.target/aarch64/switch-dispatch-1.c
index 5d60bed79d93..72b6e613fb46 100644
--- a/gcc/testsuite/gcc.target/aarch64/switch-dispatch-1.c
+++ b/gcc/testsuite/gcc.target/aarch64/switch-dispatch-1.c
@@ -1,6 +1,6 @@
 /* PR middle-end/126236 */
 /* { dg-do assemble } */
-/* { dg-options "-O2 -fduplicate-switch-dispatch -save-temps" } */
+/* { dg-options "-O2 -save-temps" } */
 
 /* Each of the 11 continuing handlers, the default handler and the loop entry
    get a dispatch of their own, so 13 register indirect branches.  All of them

Reply via email to