Since r17-5736, loop_optimizer_init in CCP merges the latch edges of a
loop with several latches into one forwarder block. ethread rejects
paths through that block ("Path crosses loops"); before r17-5736 the
first such merge came after ethread (in fre1 at -O2). On the 80186
emulator of PiTubeDirect (Arm, -mtune=arm1176jzf-s -Ofast -flto) this
makes the emulator execute 8.9% more instructions, counted under QEMU.
Once the early CCP is done, remove the latch forwarders that
loop_optimizer_init created; the later CCP passes run after ethread.
The emulator then executes the same number of instructions as without
r17-5736. On the PR testcase at -O1 -fno-checking (x86_64, cc1 built
with -O2), CCP takes 0.60 s, against 0.48 s on trunk and 7.8 s without
r17-5736. Total GC allocation grows from 174M to 272M, but peak memory
is unchanged.
Bootstrapped and regression tested on x86_64-pc-linux-gnu. Also tested
gcc.dg and gcc.target/aarch64 on aarch64-linux-gnu under QEMU.
Assisted-by: Claude Opus 5.5 (Anthropic)
PR tree-optimization/128277
gcc/ChangeLog:
* tree-cfgcleanup.cc (maybe_remove_forwarder_block): Export.
Add ALLOW_MULTIPLE_LATCHES parameter to allow removing a latch with
several predecessors.
* tree-cfgcleanup.h (maybe_remove_forwarder_block): Declare.
* tree-ssa-ccp.cc: Include tree-cfgcleanup.h.
(do_ssa_ccp): In the early CCP, remove the latch forwarders that
loop_optimizer_init created for loops with several latches, unless
the loops need fixing up, before freeing dominators.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/pr128277.c: New test.
* g++.dg/opt/pr128277.C: New test.
Signed-off-by: Dominic P <[email protected]>
---
Richard, this is meant as a suggestion. It costs some CCP time and
memory on the PR testcase (see above), and the same paths would also
be kept by letting the threader cross such a latch or by the visit
limit you mentioned in the PR; if you prefer either, or another fix,
please go ahead and I will drop this one.
OK for trunk?
gcc/testsuite/g++.dg/opt/pr128277.C | 23 +++++++++++++++++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr128277.c | 23 +++++++++++++++++++++++
gcc/tree-cfgcleanup.cc | 10 ++++++----
gcc/tree-cfgcleanup.h | 2 ++
gcc/tree-ssa-ccp.cc | 20 ++++++++++++++++++--
5 files changed, 72 insertions(+), 6 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/opt/pr128277.C
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr128277.c
diff --git a/gcc/testsuite/g++.dg/opt/pr128277.C
b/gcc/testsuite/g++.dg/opt/pr128277.C
new file mode 100644
index 00000000000..55361cfcf9b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/pr128277.C
@@ -0,0 +1,23 @@
+// PR tree-optimization/128277
+// { dg-do compile }
+// { dg-options "-O2 -fnon-call-exceptions" }
+
+int g (int);
+
+int
+f (int x, int n)
+{
+ int c = 1, r = 0;
+ try { r = x / c; }
+ catch (...)
+ {
+ int i = 0;
+ while (i < n)
+ {
+ if (g (i)) { i += 2; continue; }
+ i++;
+ }
+ r = i;
+ }
+ return r;
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr128277.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr128277.c
new file mode 100644
index 00000000000..4c750401905
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr128277.c
@@ -0,0 +1,23 @@
+/* PR tree-optimization/128277 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-ethread-stats" } */
+
+int seg;
+
+int
+f (unsigned char *p)
+{
+ int done = 0;
+ while (!done)
+ switch (*p++)
+ {
+ case 0x26: seg = 1; break;
+ case 0x2e: seg = 2; break;
+ case 0x36: seg = 3; break;
+ case 0x3e: seg = 4; break;
+ default: done = 1;
+ }
+ return *p;
+}
+
+/* { dg-final { scan-tree-dump "Jumps threaded" "ethread" } } */
diff --git a/gcc/tree-cfgcleanup.cc b/gcc/tree-cfgcleanup.cc
index 36a3d02f32f..47e053787e0 100644
--- a/gcc/tree-cfgcleanup.cc
+++ b/gcc/tree-cfgcleanup.cc
@@ -478,10 +478,12 @@ bb_phis_references_abnormal_uses (basic_block bb)
As a precondition, we require that BB be not equal to
the entry block.
If CAN_SPLIT is true, we can split the edge to have
- another bb with with the phi. */
+ another bb with the phi. If ALLOW_MULTIPLE_LATCHES is true, BB may be a
+ loop latch with several predecessors. */
-static bool
-maybe_remove_forwarder_block (basic_block bb, bool can_split = false)
+bool
+maybe_remove_forwarder_block (basic_block bb, bool can_split,
+ bool allow_multiple_latches)
{
gimple_stmt_iterator gsi;
location_t locus;
@@ -643,7 +645,7 @@ maybe_remove_forwarder_block (basic_block bb, bool
can_split = false)
/* If bb doesn't have a single predecessor we'd make this
loop have multiple latches. Don't do that if that
would in turn require disambiguating them over again. */
- if (!single_pred_p (bb))
+ if (!single_pred_p (bb) && !allow_multiple_latches)
return false;
}
/* cleanup_tree_cfg_noloop just created the loop preheader, don't
diff --git a/gcc/tree-cfgcleanup.h b/gcc/tree-cfgcleanup.h
index 602cfbd25a4..1ee04d75311 100644
--- a/gcc/tree-cfgcleanup.h
+++ b/gcc/tree-cfgcleanup.h
@@ -29,5 +29,7 @@ extern bool delete_unreachable_blocks_update_callgraph
(cgraph_node *dst_node,
extern unsigned clean_up_loop_closed_phi (function *);
extern bool phi_alternatives_equal (basic_block, edge, edge);
extern bool canonicalize_bool_cond (gcond *stmt, basic_block bb);
+extern bool maybe_remove_forwarder_block (basic_block, bool = false,
+ bool = false);
#endif /* GCC_TREE_CFGCLEANUP_H */
diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc
index f81a52afba8..a124e0d335c 100644
--- a/gcc/tree-ssa-ccp.cc
+++ b/gcc/tree-ssa-ccp.cc
@@ -138,6 +138,7 @@ along with GCC; see the file COPYING3. If not see
#include "dbgcnt.h"
#include "builtins.h"
#include "cfgloop.h"
+#include "tree-cfgcleanup.h"
#include "stor-layout.h"
#include "optabs-query.h"
#include "tree-ssa-ccp.h"
@@ -3033,6 +3034,13 @@ do_ssa_ccp (bool nonzero_p)
{
unsigned int todo = 0;
+ auto_vec<int> multi_latch;
+ if (!nonzero_p)
+ for (auto loop : loops_list (cfun, 0))
+ if (!loop->latch)
+ multi_latch.safe_push (loop->num);
+ int last_bb = last_basic_block_for_fn (cfun);
+
/* We want to minimize the number of latches to reduce iteration. */
loop_optimizer_init (LOOPS_HAVE_SIMPLE_LATCHES);
@@ -3049,10 +3057,18 @@ do_ssa_ccp (bool nonzero_p)
loops_state_clear (LOOP_CLOSED_SSA);
}
- free_dominance_info (CDI_DOMINATORS);
-
loop_optimizer_finalize ();
+ /* In the early CCP, remove the latch forwarders that
+ loop_optimizer_init created for loops with several latches. */
+ if (!loops_state_satisfies_p (LOOPS_NEED_FIXUP))
+ for (int num : multi_latch)
+ if (class loop *loop = get_loop (cfun, num))
+ if (loop->header->index >= last_bb)
+ maybe_remove_forwarder_block (loop->latch, false, true);
+
+ free_dominance_info (CDI_DOMINATORS);
+
return todo;
}
--
2.56.0