@@ -1299,6 +1299,22 @@ Node *PhaseIdealLoop::clone_up_backedge_goo(Node *back_ctrl, Node *preheader_ctr
12991299 return n;
13001300}
13011301
1302+ // When a counted loop is created, the loop phi type may be narrowed down. As a consequence, the control input of some
1303+ // nodes may be cleared: in particular in the case of a division by the loop iv, the Div node would lose its control
1304+ // dependency if the loop phi is never zero. After pre/main/post loops are created (and possibly unrolling), the
1305+ // loop phi type is only correct if the loop is indeed reachable: there's an implicit dependency between the loop phi
1306+ // type and the zero trip guard for the main or post loop and as a consequence a dependency between the Div node and the
1307+ // zero trip guard. This makes the dependency explicit by adding a CastII for the loop entry input of the loop phi. If
1308+ // the backedge of the main or post loop is removed, a Div node won't be able to float above the zero trip guard of the
1309+ // loop and can't execute even if the loop is not reached.
1310+ void PhaseIdealLoop::cast_incr_before_loop (Node* incr, Node* ctrl, CountedLoopNode* loop) {
1311+ Node* castii = new CastIINode (ctrl, incr, TypeInt::INT, ConstraintCastNode::UnconditionalDependency);
1312+ register_new_node (castii, ctrl);
1313+ Node* phi = loop->phi ();
1314+ assert (phi->in (LoopNode::EntryControl) == incr, " replacing wrong input?" );
1315+ _igvn.replace_input_of (phi, LoopNode::EntryControl, castii);
1316+ }
1317+
13021318#ifdef ASSERT
13031319void PhaseIdealLoop::ensure_zero_trip_guard_proj (Node* node, bool is_main_loop) {
13041320 assert (node->is_IfProj (), " must be the zero trip guard If node" );
@@ -1453,6 +1469,8 @@ void PhaseIdealLoop::insert_pre_post_loops(IdealLoopTree *loop, Node_List &old_n
14531469 initialize_assertion_predicates_for_main_loop (pre_head, main_head, first_node_index_in_pre_loop_body,
14541470 last_node_index_in_pre_loop_body,
14551471 DEBUG_ONLY (last_node_index_from_backedge_goo COMMA) old_new);
1472+ // CastII for the main loop:
1473+ cast_incr_before_loop (pre_incr, min_taken, main_head);
14561474
14571475 // Step B4: Shorten the pre-loop to run only 1 iteration (for now).
14581476 // RCE and alignment may change this later.
@@ -1593,7 +1611,7 @@ void PhaseIdealLoop::insert_vector_post_loop(IdealLoopTree *loop, Node_List &old
15931611// Insert post loops. Add a post loop to the given loop passed.
15941612Node *PhaseIdealLoop::insert_post_loop (IdealLoopTree* loop, Node_List& old_new,
15951613 CountedLoopNode* main_head, CountedLoopEndNode* main_end,
1596- Node*& incr, Node* limit, CountedLoopNode*& post_head) {
1614+ Node* incr, Node* limit, CountedLoopNode*& post_head) {
15971615 IfNode* outer_main_end = main_end;
15981616 IdealLoopTree* outer_loop = loop;
15991617 if (main_head->is_strip_mined ()) {
@@ -1682,6 +1700,7 @@ Node *PhaseIdealLoop::insert_post_loop(IdealLoopTree* loop, Node_List& old_new,
16821700
16831701 DEBUG_ONLY (ensure_zero_trip_guard_proj (post_head->in (LoopNode::EntryControl), false );)
16841702 initialize_assertion_predicates_for_post_loop (main_head, post_head, first_node_index_in_cloned_loop_body);
1703+ cast_incr_before_loop (zer_opaq->in (1 ), zer_taken, post_head);
16851704 return new_main_exit;
16861705}
16871706
@@ -1777,8 +1796,8 @@ void PhaseIdealLoop::create_assertion_predicates_at_main_or_post_loop(CountedLoo
17771796// to do because these control dependent nodes on the old target loop entry created by clone_up_backedge_goo() were
17781797// pinned on the loop backedge before. The Assertion Predicates are not control dependent on these nodes in any way.
17791798void PhaseIdealLoop::rewire_old_target_loop_entry_dependency_to_new_entry (
1780- LoopNode * target_loop_head, const Node* old_target_loop_entry,
1781- const uint node_index_before_new_assertion_predicate_nodes) {
1799+ CountedLoopNode * target_loop_head, const Node* old_target_loop_entry,
1800+ const uint node_index_before_new_assertion_predicate_nodes) {
17821801 Node* new_main_loop_entry = target_loop_head->skip_strip_mined ()->in (LoopNode::EntryControl);
17831802 if (new_main_loop_entry == old_target_loop_entry) {
17841803 // No Assertion Predicates added.
@@ -1788,6 +1807,7 @@ void PhaseIdealLoop::rewire_old_target_loop_entry_dependency_to_new_entry(
17881807 for (DUIterator_Fast imax, i = old_target_loop_entry->fast_outs (imax); i < imax; i++) {
17891808 Node* out = old_target_loop_entry->fast_out (i);
17901809 if (!out->is_CFG () && out->_idx < node_index_before_new_assertion_predicate_nodes) {
1810+ assert (out != target_loop_head->init_trip (), " CastII on loop entry?" );
17911811 _igvn.replace_input_of (out, 0 , new_main_loop_entry);
17921812 set_ctrl (out, new_main_loop_entry);
17931813 --i;
@@ -2677,7 +2697,8 @@ void PhaseIdealLoop::do_range_check(IdealLoopTree* loop) {
26772697 if (b_test._test == BoolTest::lt) { // Range checks always use lt
26782698 // The underflow and overflow limits: 0 <= scale*I+offset < limit
26792699 add_constraint (stride_con, lscale_con, offset, zero, limit, next_limit_ctrl, &pre_limit, &main_limit);
2680- Node* init = cl->init_trip ();
2700+ Node* init = cl->uncasted_init_trip (true );
2701+
26812702 Node* opaque_init = new OpaqueLoopInitNode (C, init);
26822703 register_new_node (opaque_init, loop_entry);
26832704
0 commit comments