https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77975
--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> --- > Proved that loop 1 iterates 2 times using brute force. so the question is why that doesn't work for the new form (and this is what we should fix). Because static gphi * get_base_for (struct loop *loop, tree x) { ... init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop)); next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop)); if (TREE_CODE (next) != SSA_NAME) return NULL; Index: gcc/tree-ssa-loop-niter.c =================================================================== --- gcc/tree-ssa-loop-niter.c (revision 241148) +++ gcc/tree-ssa-loop-niter.c (working copy) @@ -2545,13 +2551,11 @@ get_base_for (struct loop *loop, tree x) init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop)); next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop)); - if (TREE_CODE (next) != SSA_NAME) - return NULL; - if (!is_gimple_min_invariant (init)) return NULL; - if (chain_of_csts_start (loop, next) != phi) + if (TREE_CODE (next) == SSA_NAME + && chain_of_csts_start (loop, next) != phi) return NULL; return phi; @@ -2574,6 +2578,8 @@ get_val_for (tree x, tree base) if (!x) return base; + else if (is_gimple_min_invariant (x)) + return x; stmt = SSA_NAME_DEF_STMT (x); if (gimple_code (stmt) == GIMPLE_PHI) fixes it. But I think it's not correct as it misses the intermediate stmt evaluations. Some more refactoring (passing in the PHI def to get_val_for) is necessary I think.