https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99101
--- Comment #9 from Richard Biener <rguenth at gcc dot gnu.org> --- Another fix for the finite-loop issue would be to avoid considering loops not post-dominated by the exit block as finite. Like with the imperfect diff --git a/gcc/tree-ssa-loop-niter.c b/gcc/tree-ssa-loop-niter.c index 3817ec423e7..388c03ee6fc 100644 --- a/gcc/tree-ssa-loop-niter.c +++ b/gcc/tree-ssa-loop-niter.c @@ -2865,6 +2865,16 @@ finite_loop_p (class loop *loop) FOR_EACH_VEC_ELT (exits, i, ex) if (!(ex->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_FAKE))) { + /* But do not count noreturn regions as exit. + ??? This is a to simple check, we'd have to either + use post-dominance by the exit block (which isn't + computed) or a DFS walk. See PR99101 where the + the fact that control dependence (and post-dominance) + is not well-defined for not backward reachable regions + causes issues otherwise. */ + if (ex->dest->index != EXIT_BLOCK + && EDGE_COUNT (ex->dest->succs) == 0) + continue; if (dump_file) fprintf (dump_file, "Assume loop %i to be finite: it has an exit " "and -ffinite-loops is on.\n", loop->num); but that still leaves post-dominance and thus control dependence not well-defined in not backwards reachable regions and thus possibly "bogus" control-dependent DCE decisions in those. Note even with the above CD-DCE will then use control-dependence to make the loop control necessary: FOR_EACH_LOOP (loop, 0) if (!finite_loop_p (loop)) { if (dump_file) fprintf (dump_file, "cannot prove finiteness of loop %i\n", loop->num); mark_control_dependent_edges_necessary (loop->latch, false); } Which means to be absolutely sure we could use non-CD DCE whenever there are not backwards reachable blocks in the function.