The backward jump threader fails to thread the state-machine loop in
binascii_a2b_base64 because the switch has already been lowered to a
decision tree by the time the late FSM threading runs, and the
duplication limits are tight for the resulting branch sequence.
Run the non-FSM jump threading first to clean up the CFG, then the FSM
threader while the GIMPLE_SWITCH is still intact, and only lower
switches after that. pass_dominator and pass_strlen move after
pass_lower_switch so the lowered decision tree is folded away by DOM.
This restores the GCC 11 performance (PR128102) without introducing a
-Wmaybe-uninitialized false positive.
The reordering also removes a pre-existing false positive in
uninit-pred-7_a.c. pr71437.c now has the loop-rotation threading
attempted earlier by the FSM threader, which rejects it as "would copy
too many statements", so the DOM3 "Threaded jump" count drops to zero
with no change in the final code.
gcc/
* passes.def (pass_lower_switch): Move after the second
pass_thread_jumps_full and before pass_dominator.
(pass_thread_jumps_full): Move before pass_lower_switch, keeping
it after the non-FSM pass_thread_jumps.
gcc/testsuite/
* gcc.dg/tree-ssa/pr128102.c: New test.
* gcc.dg/tree-ssa/pr71437.c: Update the DOM3 "Threaded jump"
count for the new pass order.
* gcc.dg/uninit-pred-7_a.c: Remove the xfail on the bogus warning
at -O1.
---
Changes from v2:
This v3 supersedes v2; please test this version instead.
- restore the gcc.dg/tree-ssa/pr128102.c regression test, which was
accidentally dropped from v2;
- keep the non-FSM pass_thread_jumps before pass_thread_jumps_full
(instead of moving it after pass_strlen). The backward threader
still sees the GIMPLE_SWITCH, and this recovers another ~9% on the
PR128102 microbenchmark (2.11 vs 2.32 cycles/byte at -O2) with the
same regression-test results;
- update gcc.dg/tree-ssa/pr71437.c: with the switch lowered after the
late FSM threading, the backward threader now runs before DOM3 and
rejects these loop-rotation paths as "would copy too many
statements", so the DOM3 "Threaded jump" count drops from 2 to 0.
The final code is unchanged (verified by comparing the optimized
dumps).
Bootstrap on x86_64-linux succeeded (stage comparison clean). The
regression tests (make check-gcc) are still running; I'll follow up
with the full results.
---
gcc/passes.def | 8 ++-
gcc/testsuite/gcc.dg/tree-ssa/pr128102.c | 69 ++++++++++++++++++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr71437.c | 7 ++-
gcc/testsuite/gcc.dg/uninit-pred-7_a.c | 2 +-
4 files changed, 81 insertions(+), 5 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr128102.c
diff --git a/gcc/passes.def b/gcc/passes.def
index 9095c134f49..c3b40777111 100644
--- a/gcc/passes.def
+++ b/gcc/passes.def
@@ -346,7 +346,6 @@ along with GCC; see the file COPYING3. If not see
POP_INSERT_PASSES ()
NEXT_PASS (pass_simduid_cleanup);
NEXT_PASS (pass_lower_vector_ssa);
- NEXT_PASS (pass_lower_switch);
NEXT_PASS (pass_cse_sincos);
NEXT_PASS (pass_cse_reciprocals);
NEXT_PASS (pass_reassoc, false /* early_p */);
@@ -355,10 +354,15 @@ along with GCC; see the file COPYING3. If not see
NEXT_PASS (pass_fre, false /* may_iterate */);
/* After late FRE we rewrite no longer addressed locals into SSA
form if possible. */
+ /* Run the non-FSM jump threading first, then thread the loop with
+ the backward threader before lowering switches so it still sees the
+ GIMPLE_SWITCH. Lower switches before DOM so the decision tree is
+ folded away by DOM. */
NEXT_PASS (pass_thread_jumps, /*first=*/false);
+ NEXT_PASS (pass_thread_jumps_full, /*first=*/false);
+ NEXT_PASS (pass_lower_switch);
NEXT_PASS (pass_dominator, false /* may_peel_loop_headers_p */);
NEXT_PASS (pass_strlen);
- NEXT_PASS (pass_thread_jumps_full, /*first=*/false);
NEXT_PASS (pass_vrp, true /* final_p */);
/* Run CCP to compute alignment and nonzero bits. */
NEXT_PASS (pass_ccp, true /* nonzero_p */);
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr128102.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr128102.c
new file mode 100644
index 00000000000..29374fc3a73
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr128102.c
@@ -0,0 +1,69 @@
+/* PR tree-optimization/128102 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+typedef __SIZE_TYPE__ size_t;
+typedef unsigned char u8;
+
+static u8 table_a2b_base64[256];
+
+__attribute__ ((noinline, noclone)) size_t
+decode_b64 (const u8 *restrict src, size_t len, u8 *restrict dst)
+{
+ u8 *out = dst;
+ int quad_pos = 0;
+ u8 leftchar = 0;
+
+ for (size_t i = 0; i < len; i++)
+ {
+ u8 this_ch = src[i];
+
+ if (this_ch == '=')
+ continue;
+
+ this_ch = table_a2b_base64[this_ch];
+ if (this_ch >= 64)
+ continue;
+
+ switch (quad_pos)
+ {
+ case 0:
+ quad_pos = 1;
+ leftchar = this_ch;
+ break;
+ case 1:
+ quad_pos = 2;
+ *out++ = (u8) ((leftchar << 2) | (this_ch >> 4));
+ leftchar = this_ch & 0x0f;
+ break;
+ case 2:
+ quad_pos = 3;
+ *out++ = (u8) ((leftchar << 4) | (this_ch >> 2));
+ leftchar = this_ch & 0x03;
+ break;
+ case 3:
+ quad_pos = 0;
+ *out++ = (u8) ((leftchar << 6) | this_ch);
+ leftchar = 0;
+ break;
+ }
+ }
+ return (size_t) (out - dst);
+}
+
+int
+main (void)
+{
+ static u8 src[256], dst[256];
+
+ for (int i = 0; i < 256; i++)
+ table_a2b_base64[i] = (u8) (i & 63);
+
+ return (int) decode_b64 (src, sizeof src, dst);
+}
+
+/* Threading the loop back edge replicates the loop body, and quad_pos
+ becomes a constant on every copy, so the '=' test of the source loop
+ is left 11 times. Without the fix the loop stays rolled and the test
+ appears only once. */
+/* { dg-final { scan-tree-dump-times "== 61" 11 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr71437.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr71437.c
index eab3a25928e..ea086be5ce7 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/pr71437.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr71437.c
@@ -42,5 +42,8 @@ void foo (int K)
/* We used to get 1 vrp-thread1 candidates here, but they now get
deferred until after loop opts are done, because they were rotating
- loops. */
-/* { dg-final { scan-tree-dump-times "Threaded jump " 2 "dom3" } } */
+ loops. With the switch lowering moved after the late FSM threading
+ (PR128102) the backward threader runs before DOM3 and rejects these
+ paths as "would copy too many statements", so DOM3 no longer threads
+ them. */
+/* { dg-final { scan-tree-dump-times "Threaded jump " 0 "dom3" } } */
diff --git a/gcc/testsuite/gcc.dg/uninit-pred-7_a.c
b/gcc/testsuite/gcc.dg/uninit-pred-7_a.c
index 5dd92176c8f..f1fcb364a0d 100644
--- a/gcc/testsuite/gcc.dg/uninit-pred-7_a.c
+++ b/gcc/testsuite/gcc.dg/uninit-pred-7_a.c
@@ -19,7 +19,7 @@ int foo (int n, int l, int m, int r)
blah(v); /* { dg-bogus "uninitialized" "bogus warning" } */
if ( n )
- blah(v); /* { dg-bogus "uninitialized" "bogus warning" { xfail *-*-* } }
*/
+ blah(v); /* { dg-bogus "uninitialized" "bogus warning" } */
if ( l )
blah(v); /* { dg-bogus "uninitialized" "bogus warning" } */
--
2.34.1