Small switches can pay for a bounds check, a table load and an
indirect jump. Raise the default speed threshold to 11 so that
CPython 3.11.15 uses conditional branches for its eight-way character
set dispatch. Keep the generic threshold when optimizing for size.
Use a threshold of 5 in pr126236.c so its eight-way switch continues
to exercise vsetvl on shared jump-table edges.
PyBench relative gains are positive when elapsed time decreases.
Case K3 C9501
-------------------------- ------- -------
Geomean (58 cases) +0.89% +1.35%
Cases that ran more than 5% faster or slower on C9501
Case K3 C9501
-------------------------- ------- -------
CompareUnicode +1.57% +14.02%
UnicodeSlicing -1.53% +12.21%
CompareFloats +17.54% +10.84%
CreateStringsWithConcat -2.68% +10.38%
StringSlicing -1.85% +10.32%
CreateUnicodeWithConcat -3.17% +9.97%
TryExcept -13.45% +9.76%
SmallLists +1.33% +5.34%
CompareIntegers +26.47% +5.30%
ComplexPythonFunctionCalls +0.67% -5.26%
SPEC CPU2017 Integer testing on C9501 showed only a very small gain,
mainly in perlbench_r, with no clear overall regression.
Assisted-by: qwenwork
gcc/ChangeLog:
* config/riscv/riscv.cc (riscv_case_values_threshold): New function.
(TARGET_CASE_VALUES_THRESHOLD): Define.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/case-values-threshold-1.c: New test.
* gcc.target/riscv/case-values-threshold-2.c: New test.
* gcc.target/riscv/case-values-threshold-3.c: New test.
* gcc.target/riscv/case-values-threshold-4.c: New test.
* gcc.target/riscv/case-values-threshold-5.c: New test.
* gcc.target/riscv/jump-table-large-code-model.c: Keep the jump table.
* gcc.target/riscv/switch-qi.c: Likewise.
* gcc.target/riscv/switch-si.c: Likewise.
* gcc.target/riscv/rvv/vsetvl/pr126236.c: Keep the shared jump table.
* gcc.target/riscv/rvv/vsetvl/vlmax_back_prop-4.c: Keep the jump
table and disable loop unrolling.
---
gcc/config/riscv/riscv.cc | 13 ++++++
.../riscv/case-values-threshold-1.c | 40 +++++++++++++++++++
.../riscv/case-values-threshold-2.c | 6 +++
.../riscv/case-values-threshold-3.c | 6 +++
.../riscv/case-values-threshold-4.c | 8 ++++
.../riscv/case-values-threshold-5.c | 8 ++++
.../riscv/jump-table-large-code-model.c | 1 +
.../gcc.target/riscv/rvv/vsetvl/pr126236.c | 4 ++
.../riscv/rvv/vsetvl/vlmax_back_prop-4.c | 4 ++
gcc/testsuite/gcc.target/riscv/switch-qi.c | 2 +-
gcc/testsuite/gcc.target/riscv/switch-si.c | 2 +-
11 files changed, 92 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gcc.target/riscv/case-values-threshold-1.c
create mode 100644 gcc/testsuite/gcc.target/riscv/case-values-threshold-2.c
create mode 100644 gcc/testsuite/gcc.target/riscv/case-values-threshold-3.c
create mode 100644 gcc/testsuite/gcc.target/riscv/case-values-threshold-4.c
create mode 100644 gcc/testsuite/gcc.target/riscv/case-values-threshold-5.c
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 3e0aa58d3f3..935402fdd67 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -2464,6 +2464,16 @@ riscv_cannot_force_const_mem (machine_mode mode
ATTRIBUTE_UNUSED, rtx x)
return false;
}
+/* Implement TARGET_CASE_VALUES_THRESHOLD. A jump table needs a bounds
+ check, a table lookup and an indirect jump. Prefer branches for small
+ switches when optimizing for speed. */
+
+static unsigned int
+riscv_case_values_threshold (void)
+{
+ return optimize_size ? default_case_values_threshold () : 11;
+}
+
/* Return true if register REGNO is a valid base register for mode MODE.
STRICT_P is true if REG_OK_STRICT is in effect. */
@@ -17350,6 +17360,9 @@ riscv_memtag_tag_bitsize ()
#undef TARGET_CANNOT_FORCE_CONST_MEM
#define TARGET_CANNOT_FORCE_CONST_MEM riscv_cannot_force_const_mem
+#undef TARGET_CASE_VALUES_THRESHOLD
+#define TARGET_CASE_VALUES_THRESHOLD riscv_case_values_threshold
+
#undef TARGET_LEGITIMATE_CONSTANT_P
#define TARGET_LEGITIMATE_CONSTANT_P riscv_legitimate_constant_p
diff --git a/gcc/testsuite/gcc.target/riscv/case-values-threshold-1.c
b/gcc/testsuite/gcc.target/riscv/case-values-threshold-1.c
new file mode 100644
index 00000000000..ff8b7d10c05
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/case-values-threshold-1.c
@@ -0,0 +1,40 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -ffat-lto-objects -fdump-tree-switchlower1" } */
+
+#ifndef NUM_CASES
+#define NUM_CASES 8
+#endif
+
+/* Give each case a distinct destination so that switch conversion
+ cannot replace the dispatch with a table of return values. */
+extern int f0 (void), f1 (void), f2 (void), f3 (void);
+extern int f4 (void), f5 (void), f6 (void), f7 (void);
+extern int f8 (void), f9 (void), f10 (void);
+
+int
+dispatch (int x)
+{
+ switch (x)
+ {
+ case 0: return f0 ();
+ case 1: return f1 ();
+ case 2: return f2 ();
+ case 3: return f3 ();
+ case 4: return f4 ();
+ case 5: return f5 ();
+ case 6: return f6 ();
+ case 7: return f7 ();
+#if NUM_CASES >= 9
+ case 8: return f8 ();
+#endif
+#if NUM_CASES >= 10
+ case 9: return f9 ();
+#endif
+#if NUM_CASES >= 11
+ case 10: return f10 ();
+#endif
+ default: return -1;
+ }
+}
+
+/* { dg-final { scan-tree-dump "case clusters: 0 1 2 3 4 5 6 7" "switchlower1"
} } */
diff --git a/gcc/testsuite/gcc.target/riscv/case-values-threshold-2.c
b/gcc/testsuite/gcc.target/riscv/case-values-threshold-2.c
new file mode 100644
index 00000000000..c1189508027
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/case-values-threshold-2.c
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -ffat-lto-objects -fdump-tree-switchlower1
--param=case-values-threshold=5" } */
+
+#include "case-values-threshold-1.c"
+
+/* { dg-final { scan-tree-dump "case clusters: JT:0-7" "switchlower1" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/case-values-threshold-3.c
b/gcc/testsuite/gcc.target/riscv/case-values-threshold-3.c
new file mode 100644
index 00000000000..656d7dd4ccb
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/case-values-threshold-3.c
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+/* { dg-options "-Os -ffat-lto-objects -fdump-tree-switchlower1" } */
+
+#include "case-values-threshold-1.c"
+
+/* { dg-final { scan-tree-dump "case clusters: JT:0-7" "switchlower1" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/case-values-threshold-4.c
b/gcc/testsuite/gcc.target/riscv/case-values-threshold-4.c
new file mode 100644
index 00000000000..856e32ed431
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/case-values-threshold-4.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -ffat-lto-objects -fdump-tree-switchlower1" } */
+
+/* Ten cases are just below the speed threshold. */
+#define NUM_CASES 10
+#include "case-values-threshold-1.c"
+
+/* { dg-final { scan-tree-dump "case clusters: 0 1 2 3 4 5 6 7 8 9"
"switchlower1" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/case-values-threshold-5.c
b/gcc/testsuite/gcc.target/riscv/case-values-threshold-5.c
new file mode 100644
index 00000000000..3dc1359567e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/case-values-threshold-5.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -ffat-lto-objects -fdump-tree-switchlower1" } */
+
+/* Eleven cases meet the speed threshold. */
+#define NUM_CASES 11
+#include "case-values-threshold-1.c"
+
+/* { dg-final { scan-tree-dump "case clusters: JT:0-10" "switchlower1" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/jump-table-large-code-model.c
b/gcc/testsuite/gcc.target/riscv/jump-table-large-code-model.c
index ab97b0fc0bb..75b8645254d 100644
--- a/gcc/testsuite/gcc.target/riscv/jump-table-large-code-model.c
+++ b/gcc/testsuite/gcc.target/riscv/jump-table-large-code-model.c
@@ -1,5 +1,6 @@
/* { dg-do compile } */
/* { dg-options "-march=rv64gc -mabi=lp64 -mcmodel=large -fno-pie" } */
+/* { dg-additional-options "--param=case-values-threshold=5" } */
int foo(int x, int y)
{
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/pr126236.c
b/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/pr126236.c
index 9e84d5837af..f6c2458d73a 100644
--- a/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/pr126236.c
+++ b/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/pr126236.c
@@ -2,6 +2,10 @@
/* { dg-do run { target { riscv_v } } } */
/* { dg-additional-options "-std=c99 -O2 -fduplicate-switch-dispatch
-mno-explicit-relocs -fdump-rtl-compgotos" } */
+/* Keep the eight-way switch eligible for a jump table so vsetvl sees
+ incoming edges from dispatches that share the table. */
+/* { dg-additional-options "--param=case-values-threshold=5" } */
+
/* Execute each ordered pair of handlers with different vector configurations.
The reference uses the same unsigned operations without vectorization. */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/vlmax_back_prop-4.c
b/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/vlmax_back_prop-4.c
index 49f31ed92b6..c77ec1cd3b7 100644
--- a/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/vlmax_back_prop-4.c
+++ b/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/vlmax_back_prop-4.c
@@ -1,6 +1,10 @@
/* { dg-do compile } */
/* { dg-options "-mrvv-vector-bits=scalable -march=rv32gcv -mabi=ilp32
-fno-tree-vectorize" } */
+/* Keep the jump table and a single copy of the final loop so the scans
+ below count the VTYPE change that preserves VL. */
+/* { dg-additional-options "-fno-unroll-loops --param=case-values-threshold=5"
} */
+
#include "riscv_vector.h"
void f (int32_t * restrict in, int32_t * restrict out, int n, int cond)
diff --git a/gcc/testsuite/gcc.target/riscv/switch-qi.c
b/gcc/testsuite/gcc.target/riscv/switch-qi.c
index 9126e0a10b2..5108e408855 100644
--- a/gcc/testsuite/gcc.target/riscv/switch-qi.c
+++ b/gcc/testsuite/gcc.target/riscv/switch-qi.c
@@ -1,5 +1,5 @@
/* { dg-do compile { target { riscv64*-*-* } } } */
-/* { dg-options "-march=rv64gc -mabi=lp64" } */
+/* { dg-options "-march=rv64gc -mabi=lp64 --param=case-values-threshold=5" } */
/* Test for riscv_extend_comparands patch. */
extern void asdf(int);
diff --git a/gcc/testsuite/gcc.target/riscv/switch-si.c
b/gcc/testsuite/gcc.target/riscv/switch-si.c
index e76c2aa088d..bc88322ebed 100644
--- a/gcc/testsuite/gcc.target/riscv/switch-si.c
+++ b/gcc/testsuite/gcc.target/riscv/switch-si.c
@@ -1,5 +1,5 @@
/* { dg-do compile } */
-/* { dg-options "" } */
+/* { dg-options "--param=case-values-threshold=5" } */
/* Test for do_tablejump patch. */
extern void asdf(int);
--
2.52.0