@@ -3234,29 +3234,21 @@ bool IdealLoopTree::do_remove_empty_loop(PhaseIdealLoop *phase) {
32343234 }
32353235
32363236 // Replace the phi at loop head with the final value of the last
3237- // iteration. Then the CountedLoopEnd will collapse (backedge never
3238- // taken) and all loop-invariant uses of the exit values will be correct.
3239- Node *phi = cl->phi ();
3240- Node *exact_limit = phase->exact_limit (this );
3241- if (exact_limit != cl->limit ()) {
3242- // We also need to replace the original limit to collapse loop exit.
3243- Node* cmp = cl->loopexit ()->cmp_node ();
3244- assert (cl->limit () == cmp->in (2 ), " sanity" );
3245- // Duplicate cmp node if it has other users
3246- if (cmp->outcnt () > 1 ) {
3247- cmp = cmp->clone ();
3248- cmp = phase->_igvn .register_new_node_with_optimizer (cmp);
3249- BoolNode *bol = cl->loopexit ()->in (CountedLoopEndNode::TestValue)->as_Bool ();
3250- phase->_igvn .replace_input_of (bol, 1 , cmp); // put bol on worklist
3251- }
3252- phase->_igvn ._worklist .push (cmp->in (2 )); // put limit on worklist
3253- phase->_igvn .replace_input_of (cmp, 2 , exact_limit); // put cmp on worklist
3254- }
3237+ // iteration (exact_limit - stride), to make sure the loop exit value
3238+ // is correct, for any users after the loop.
32553239 // Note: the final value after increment should not overflow since
32563240 // counted loop has limit check predicate.
3257- Node *final = new SubINode (exact_limit, cl->stride ());
3258- phase->register_new_node (final ,cl->in (LoopNode::EntryControl));
3259- phase->_igvn .replace_node (phi,final );
3241+ Node* phi = cl->phi ();
3242+ Node* exact_limit = phase->exact_limit (this );
3243+ Node* final_iv = new SubINode (exact_limit, cl->stride ());
3244+ phase->register_new_node (final_iv, cl->in (LoopNode::EntryControl));
3245+ phase->_igvn .replace_node (phi, final_iv);
3246+
3247+ // Set loop-exit condition to false. Then the CountedLoopEnd will collapse,
3248+ // because the back edge is never taken.
3249+ Node* zero = phase->_igvn .intcon (0 );
3250+ phase->_igvn .replace_input_of (cl->loopexit (), CountedLoopEndNode::TestValue, zero);
3251+
32603252 phase->C ->set_major_progress ();
32613253 return true ;
32623254}
0 commit comments