]> git.ipfire.org Git - thirdparty/gcc.git/commit
Simplify switch bit test clustering algorithm
authorAndi Kleen <ak@gcc.gnu.org>
Fri, 25 Oct 2024 22:04:06 +0000 (15:04 -0700)
committerAndi Kleen <ak@gcc.gnu.org>
Tue, 29 Oct 2024 22:10:23 +0000 (15:10 -0700)
commit3d06e9c3e07e13eab715e19dafbcfc1a0b7e43d6
tree469ac09f0fbe0335c8fca635890413b907b49d54
parenta4e2b13888267f2581ac03f076aa0d32cd045adb
Simplify switch bit test clustering algorithm

The current switch bit test clustering enumerates all possible case
clusters combinations to find ones that fit the bit test constrains
best.  This causes performance problems with very large switches.

For bit test clustering which happens naturally in word sized chunks
I don't think such an expensive algorithm is really needed.

This patch implements a simple greedy algorithm that walks
the sorted list and examines word sized windows and tries
to cluster them.

Surprisingly the new algorithm gives consistly better clusters
for the examples I tried.

For example from the gcc bootstrap:

old: 0-15 16-31 96-175
new: 0-31 96-175

I'm not fully sure why that is, probably some bug in the old
algorithm? This shows even up in the test suite where if-to-switch-6
now can generate a switch, as well as a case in switch-1.c

I don't have a proof that the new algorithm is always as good or better,
but so far at least I don't see any counter examples.

It also fixes the excessive compile time in PR117091,
however this was already fixed by an earlier patch
that doesn't run clustering when no targets have multiple
values.

gcc/ChangeLog:

PR middle-end/117091
* tree-switch-conversion.cc (bit_test_cluster::find_bit_tests):
Change clustering algorithm to simple greedy.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/if-to-switch-6.c: Allow condition chain.
* gcc.dg/tree-ssa/switch-1.c: Allow more bit tests.
* gcc.dg/pr21643.c: Use -fno-bit-tests
* gcc.target/aarch64/pr99988.c: Use -fno-bit-tests
gcc/testsuite/gcc.dg/pr21643.c
gcc/testsuite/gcc.dg/tree-ssa/if-to-switch-6.c
gcc/testsuite/gcc.dg/tree-ssa/switch-1.c
gcc/testsuite/gcc.target/aarch64/pr99988.c
gcc/tree-switch-conversion.cc