846 for( uint i = 1; i < n->req(); i++ ) { 847 Node *g = clone_up_backedge_goo( back_ctrl, preheader_ctrl, n->in(i), visited, clones ); 848 if( g != n->in(i) ) { 849 if( !x ) { 850 assert(clones.find(n->_idx) == NULL, "dead loop"); 851 x = n->clone(); 852 clones.push(x, n->_idx); 853 } 854 x->set_req(i, g); 855 } 856 } 857 if( x ) { // x can legally float to pre-header location 858 register_new_node( x, preheader_ctrl ); 859 return x; 860 } else { // raise n to cover LCA of uses 861 set_ctrl( n, find_non_split_ctrl(back_ctrl->in(0)) ); 862 } 863 return n; 864 } 865 866 //------------------------------insert_pre_post_loops-------------------------- 867 // Insert pre and post loops. If peel_only is set, the pre-loop can not have 868 // more iterations added. It acts as a 'peel' only, no lower-bound RCE, no 869 // alignment. Useful to unroll loops that do no array accesses. 870 void PhaseIdealLoop::insert_pre_post_loops( IdealLoopTree *loop, Node_List &old_new, bool peel_only ) { 871 872 #ifndef PRODUCT 873 if (TraceLoopOpts) { 874 if (peel_only) 875 tty->print("PeelMainPost "); 876 else 877 tty->print("PreMainPost "); 878 loop->dump_head(); 879 } 880 #endif 881 C->set_major_progress(); 882 883 // Find common pieces of the loop being guarded with pre & post loops 884 CountedLoopNode *main_head = loop->_head->as_CountedLoop(); 885 assert( main_head->is_normal_loop(), "" ); 1043 main_head->set_req(LoopNode::EntryControl, min_taken); 1044 set_idom(main_head, min_taken, dd_main_head); 1045 1046 visited.Clear(); 1047 clones.clear(); 1048 // Step B3: Make the fall-in values to the main-loop come from the 1049 // fall-out values of the pre-loop. 1050 for (DUIterator_Fast i2max, i2 = main_head->fast_outs(i2max); i2 < i2max; i2++) { 1051 Node* main_phi = main_head->fast_out(i2); 1052 if( main_phi->is_Phi() && main_phi->in(0) == main_head && main_phi->outcnt() > 0 ) { 1053 Node *pre_phi = old_new[main_phi->_idx]; 1054 Node *fallpre = clone_up_backedge_goo(pre_head->back_control(), 1055 main_head->init_control(), 1056 pre_phi->in(LoopNode::LoopBackControl), 1057 visited, clones); 1058 _igvn.hash_delete(main_phi); 1059 main_phi->set_req( LoopNode::EntryControl, fallpre ); 1060 } 1061 } 1062 1063 // Step B4: Shorten the pre-loop to run only 1 iteration (for now). 1064 // RCE and alignment may change this later. 1065 Node *cmp_end = pre_end->cmp_node(); 1066 assert( cmp_end->in(2) == limit, "" ); 1067 Node *pre_limit = new (C) AddINode( init, stride ); 1068 1069 // Save the original loop limit in this Opaque1 node for 1070 // use by range check elimination. 1071 Node *pre_opaq = new (C) Opaque1Node(C, pre_limit, limit); 1072 1073 register_new_node( pre_limit, pre_head->in(0) ); 1074 register_new_node( pre_opaq , pre_head->in(0) ); 1075 1076 // Since no other users of pre-loop compare, I can hack limit directly 1077 assert( cmp_end->outcnt() == 1, "no other users" ); 1078 _igvn.hash_delete(cmp_end); 1079 cmp_end->set_req(2, peel_only ? pre_limit : pre_opaq); 1080 1081 // Special case for not-equal loop bounds: 1082 // Change pre loop test, main loop test, and the | 846 for( uint i = 1; i < n->req(); i++ ) { 847 Node *g = clone_up_backedge_goo( back_ctrl, preheader_ctrl, n->in(i), visited, clones ); 848 if( g != n->in(i) ) { 849 if( !x ) { 850 assert(clones.find(n->_idx) == NULL, "dead loop"); 851 x = n->clone(); 852 clones.push(x, n->_idx); 853 } 854 x->set_req(i, g); 855 } 856 } 857 if( x ) { // x can legally float to pre-header location 858 register_new_node( x, preheader_ctrl ); 859 return x; 860 } else { // raise n to cover LCA of uses 861 set_ctrl( n, find_non_split_ctrl(back_ctrl->in(0)) ); 862 } 863 return n; 864 } 865 866 bool PhaseIdealLoop::cast_incr_before_loop(Node* incr, Node* ctrl, Node* loop) { 867 Node* castii = new (C) CastIINode(incr, TypeInt::INT, true); 868 castii->set_req(0, ctrl); 869 register_new_node(castii, ctrl); 870 for (DUIterator_Fast imax, i = incr->fast_outs(imax); i < imax; i++) { 871 Node* n = incr->fast_out(i); 872 if (n->is_Phi() && n->in(0) == loop) { 873 int nrep = n->replace_edge(incr, castii); 874 return true; 875 } 876 } 877 return false; 878 } 879 880 //------------------------------insert_pre_post_loops-------------------------- 881 // Insert pre and post loops. If peel_only is set, the pre-loop can not have 882 // more iterations added. It acts as a 'peel' only, no lower-bound RCE, no 883 // alignment. Useful to unroll loops that do no array accesses. 884 void PhaseIdealLoop::insert_pre_post_loops( IdealLoopTree *loop, Node_List &old_new, bool peel_only ) { 885 886 #ifndef PRODUCT 887 if (TraceLoopOpts) { 888 if (peel_only) 889 tty->print("PeelMainPost "); 890 else 891 tty->print("PreMainPost "); 892 loop->dump_head(); 893 } 894 #endif 895 C->set_major_progress(); 896 897 // Find common pieces of the loop being guarded with pre & post loops 898 CountedLoopNode *main_head = loop->_head->as_CountedLoop(); 899 assert( main_head->is_normal_loop(), "" ); 1057 main_head->set_req(LoopNode::EntryControl, min_taken); 1058 set_idom(main_head, min_taken, dd_main_head); 1059 1060 visited.Clear(); 1061 clones.clear(); 1062 // Step B3: Make the fall-in values to the main-loop come from the 1063 // fall-out values of the pre-loop. 1064 for (DUIterator_Fast i2max, i2 = main_head->fast_outs(i2max); i2 < i2max; i2++) { 1065 Node* main_phi = main_head->fast_out(i2); 1066 if( main_phi->is_Phi() && main_phi->in(0) == main_head && main_phi->outcnt() > 0 ) { 1067 Node *pre_phi = old_new[main_phi->_idx]; 1068 Node *fallpre = clone_up_backedge_goo(pre_head->back_control(), 1069 main_head->init_control(), 1070 pre_phi->in(LoopNode::LoopBackControl), 1071 visited, clones); 1072 _igvn.hash_delete(main_phi); 1073 main_phi->set_req( LoopNode::EntryControl, fallpre ); 1074 } 1075 } 1076 1077 // Nodes inside the loop may be control dependent on a predicate 1078 // that was moved before the preloop. If the back branch of the main 1079 // or post loops becomes dead, those nodes won't be dependent on the 1080 // test that guards that loop nest anymore which could lead to an 1081 // incorrect array access because it executes independently of the 1082 // test that was guarding the loop nest. We add a special CastII on 1083 // the if branch that enters the loop, between the input induction 1084 // variable value and the induction variable Phi to preserve correct 1085 // dependencies. 1086 1087 // CastII for the post loop: 1088 bool inserted = cast_incr_before_loop(zer_opaq->in(1), zer_taken, post_head); 1089 assert(inserted, "no castII inserted"); 1090 1091 // CastII for the main loop: 1092 inserted = cast_incr_before_loop(pre_incr, min_taken, main_head); 1093 assert(inserted, "no castII inserted"); 1094 1095 // Step B4: Shorten the pre-loop to run only 1 iteration (for now). 1096 // RCE and alignment may change this later. 1097 Node *cmp_end = pre_end->cmp_node(); 1098 assert( cmp_end->in(2) == limit, "" ); 1099 Node *pre_limit = new (C) AddINode( init, stride ); 1100 1101 // Save the original loop limit in this Opaque1 node for 1102 // use by range check elimination. 1103 Node *pre_opaq = new (C) Opaque1Node(C, pre_limit, limit); 1104 1105 register_new_node( pre_limit, pre_head->in(0) ); 1106 register_new_node( pre_opaq , pre_head->in(0) ); 1107 1108 // Since no other users of pre-loop compare, I can hack limit directly 1109 assert( cmp_end->outcnt() == 1, "no other users" ); 1110 _igvn.hash_delete(cmp_end); 1111 cmp_end->set_req(2, peel_only ? pre_limit : pre_opaq); 1112 1113 // Special case for not-equal loop bounds: 1114 // Change pre loop test, main loop test, and the |