src/hotspot/share/opto/loopnode.cpp
changeset 48145 f913f6dba2d3
parent 47216 71c04702a3d5
child 48526 d52bb1d8ae7b
equal deleted inserted replaced
48144:364207a23251 48145:f913f6dba2d3
   259 
   259 
   260   // Fixup self
   260   // Fixup self
   261   set_early_ctrl( n );
   261   set_early_ctrl( n );
   262 }
   262 }
   263 
   263 
       
   264 // Create a skeleton strip mined outer loop: a Loop head before the
       
   265 // inner strip mined loop, a safepoint and an exit condition guarded
       
   266 // by an opaque node after the inner strip mined loop with a backedge
       
   267 // to the loop head. The inner strip mined loop is left as it is. Only
       
   268 // once loop optimizations are over, do we adjust the inner loop exit
       
   269 // condition to limit its number of iterations, set the outer loop
       
   270 // exit condition and add Phis to the outer loop head. Some loop
       
   271 // optimizations that operate on the inner strip mined loop need to be
       
   272 // aware of the outer strip mined loop: loop unswitching needs to
       
   273 // clone the outer loop as well as the inner, unrolling needs to only
       
   274 // clone the inner loop etc. No optimizations need to change the outer
       
   275 // strip mined loop as it is only a skeleton.
       
   276 IdealLoopTree* PhaseIdealLoop::create_outer_strip_mined_loop(BoolNode *test, Node *cmp, Node *init_control,
       
   277                                                              IdealLoopTree* loop, float cl_prob, float le_fcnt,
       
   278                                                              Node*& entry_control, Node*& iffalse) {
       
   279   Node* outer_test = _igvn.intcon(0);
       
   280   set_ctrl(outer_test, C->root());
       
   281   Node *orig = iffalse;
       
   282   iffalse = iffalse->clone();
       
   283   _igvn.register_new_node_with_optimizer(iffalse);
       
   284   set_idom(iffalse, idom(orig), dom_depth(orig));
       
   285 
       
   286   IfNode *outer_le = new OuterStripMinedLoopEndNode(iffalse, outer_test, cl_prob, le_fcnt);
       
   287   Node *outer_ift = new IfTrueNode (outer_le);
       
   288   Node* outer_iff = orig;
       
   289   _igvn.replace_input_of(outer_iff, 0, outer_le);
       
   290 
       
   291   LoopNode *outer_l = new OuterStripMinedLoopNode(C, init_control, outer_ift);
       
   292   entry_control = outer_l;
       
   293 
       
   294   IdealLoopTree* outer_ilt = new IdealLoopTree(this, outer_l, outer_ift);
       
   295   IdealLoopTree* parent = loop->_parent;
       
   296   IdealLoopTree* sibling = parent->_child;
       
   297   if (sibling == loop) {
       
   298     parent->_child = outer_ilt;
       
   299   } else {
       
   300     while (sibling->_next != loop) {
       
   301       sibling = sibling->_next;
       
   302     }
       
   303     sibling->_next = outer_ilt;
       
   304   }
       
   305   outer_ilt->_next = loop->_next;
       
   306   outer_ilt->_parent = parent;
       
   307   outer_ilt->_child = loop;
       
   308   outer_ilt->_nest = loop->_nest;
       
   309   loop->_parent = outer_ilt;
       
   310   loop->_next = NULL;
       
   311   loop->_nest++;
       
   312 
       
   313   set_loop(iffalse, outer_ilt);
       
   314   register_control(outer_le, outer_ilt, iffalse);
       
   315   register_control(outer_ift, outer_ilt, outer_le);
       
   316   set_idom(outer_iff, outer_le, dom_depth(outer_le));
       
   317   _igvn.register_new_node_with_optimizer(outer_l);
       
   318   set_loop(outer_l, outer_ilt);
       
   319   set_idom(outer_l, init_control, dom_depth(init_control)+1);
       
   320 
       
   321   return outer_ilt;
       
   322 }
       
   323 
   264 //------------------------------is_counted_loop--------------------------------
   324 //------------------------------is_counted_loop--------------------------------
   265 bool PhaseIdealLoop::is_counted_loop( Node *x, IdealLoopTree *loop ) {
   325 bool PhaseIdealLoop::is_counted_loop(Node* x, IdealLoopTree*& loop) {
   266   PhaseGVN *gvn = &_igvn;
   326   PhaseGVN *gvn = &_igvn;
   267 
   327 
   268   // Counted loop head must be a good RegionNode with only 3 not NULL
   328   // Counted loop head must be a good RegionNode with only 3 not NULL
   269   // control input edges: Self, Entry, LoopBack.
   329   // control input edges: Self, Entry, LoopBack.
   270   if (x->in(LoopNode::Self) == NULL || x->req() != 3 || loop->_irreducible) {
   330   if (x->in(LoopNode::Self) == NULL || x->req() != 3 || loop->_irreducible) {
   278   if (init_control->is_top() || back_control->is_top())
   338   if (init_control->is_top() || back_control->is_top())
   279     return false;
   339     return false;
   280 
   340 
   281   // Allow funny placement of Safepoint
   341   // Allow funny placement of Safepoint
   282   if (back_control->Opcode() == Op_SafePoint) {
   342   if (back_control->Opcode() == Op_SafePoint) {
   283     if (UseCountedLoopSafepoints) {
   343     if (LoopStripMiningIter != 0) {
   284       // Leaving the safepoint on the backedge and creating a
   344       // Leaving the safepoint on the backedge and creating a
   285       // CountedLoop will confuse optimizations. We can't move the
   345       // CountedLoop will confuse optimizations. We can't move the
   286       // safepoint around because its jvm state wouldn't match a new
   346       // safepoint around because its jvm state wouldn't match a new
   287       // location. Give up on that loop.
   347       // location. Give up on that loop.
   288       return false;
   348       return false;
   598     else
   658     else
   599       ShouldNotReachHere();
   659       ShouldNotReachHere();
   600   }
   660   }
   601   set_subtree_ctrl( limit );
   661   set_subtree_ctrl( limit );
   602 
   662 
   603   if (!UseCountedLoopSafepoints) {
   663   if (LoopStripMiningIter == 0) {
   604     // Check for SafePoint on backedge and remove
   664     // Check for SafePoint on backedge and remove
   605     Node *sfpt = x->in(LoopNode::LoopBackControl);
   665     Node *sfpt = x->in(LoopNode::LoopBackControl);
   606     if (sfpt->Opcode() == Op_SafePoint && is_deleteable_safept(sfpt)) {
   666     if (sfpt->Opcode() == Op_SafePoint && is_deleteable_safept(sfpt)) {
   607       lazy_replace( sfpt, iftrue );
   667       lazy_replace( sfpt, iftrue );
   608       if (loop->_safepts != NULL) {
   668       if (loop->_safepts != NULL) {
   681   set_idom(iftrue,  le, dd+1);
   741   set_idom(iftrue,  le, dd+1);
   682   set_idom(iffalse, le, dd+1);
   742   set_idom(iffalse, le, dd+1);
   683   assert(iff->outcnt() == 0, "should be dead now");
   743   assert(iff->outcnt() == 0, "should be dead now");
   684   lazy_replace( iff, le ); // fix 'get_ctrl'
   744   lazy_replace( iff, le ); // fix 'get_ctrl'
   685 
   745 
       
   746   Node *sfpt2 = le->in(0);
       
   747 
       
   748   Node* entry_control = init_control;
       
   749   bool strip_mine_loop = LoopStripMiningIter > 1 && loop->_child == NULL &&
       
   750     sfpt2->Opcode() == Op_SafePoint && !loop->_has_call;
       
   751   IdealLoopTree* outer_ilt = NULL;
       
   752   if (strip_mine_loop) {
       
   753     outer_ilt = create_outer_strip_mined_loop(test, cmp, init_control, loop,
       
   754                                               cl_prob, le->_fcnt, entry_control,
       
   755                                               iffalse);
       
   756   }
       
   757 
   686   // Now setup a new CountedLoopNode to replace the existing LoopNode
   758   // Now setup a new CountedLoopNode to replace the existing LoopNode
   687   CountedLoopNode *l = new CountedLoopNode(init_control, back_control);
   759   CountedLoopNode *l = new CountedLoopNode(entry_control, back_control);
   688   l->set_unswitch_count(x->as_Loop()->unswitch_count()); // Preserve
   760   l->set_unswitch_count(x->as_Loop()->unswitch_count()); // Preserve
   689   // The following assert is approximately true, and defines the intention
   761   // The following assert is approximately true, and defines the intention
   690   // of can_be_counted_loop.  It fails, however, because phase->type
   762   // of can_be_counted_loop.  It fails, however, because phase->type
   691   // is not yet initialized for this loop and its parts.
   763   // is not yet initialized for this loop and its parts.
   692   //assert(l->can_be_counted_loop(this), "sanity");
   764   //assert(l->can_be_counted_loop(this), "sanity");
   694   set_loop(l, loop);
   766   set_loop(l, loop);
   695   loop->_head = l;
   767   loop->_head = l;
   696   // Fix all data nodes placed at the old loop head.
   768   // Fix all data nodes placed at the old loop head.
   697   // Uses the lazy-update mechanism of 'get_ctrl'.
   769   // Uses the lazy-update mechanism of 'get_ctrl'.
   698   lazy_replace( x, l );
   770   lazy_replace( x, l );
   699   set_idom(l, init_control, dom_depth(x));
   771   set_idom(l, entry_control, dom_depth(entry_control) + 1);
   700 
   772 
   701   if (!UseCountedLoopSafepoints) {
   773   if (LoopStripMiningIter == 0 || strip_mine_loop) {
   702     // Check for immediately preceding SafePoint and remove
   774     // Check for immediately preceding SafePoint and remove
   703     Node *sfpt2 = le->in(0);
   775     if (sfpt2->Opcode() == Op_SafePoint && (LoopStripMiningIter != 0 || is_deleteable_safept(sfpt2))) {
   704     if (sfpt2->Opcode() == Op_SafePoint && is_deleteable_safept(sfpt2)) {
   776       if (strip_mine_loop) {
       
   777         Node* outer_le = outer_ilt->_tail->in(0);
       
   778         Node* sfpt = sfpt2->clone();
       
   779         sfpt->set_req(0, iffalse);
       
   780         outer_le->set_req(0, sfpt);
       
   781         register_control(sfpt, outer_ilt, iffalse);
       
   782         set_idom(outer_le, sfpt, dom_depth(sfpt));
       
   783       }
   705       lazy_replace( sfpt2, sfpt2->in(TypeFunc::Control));
   784       lazy_replace( sfpt2, sfpt2->in(TypeFunc::Control));
   706       if (loop->_safepts != NULL) {
   785       if (loop->_safepts != NULL) {
   707         loop->_safepts->yank(sfpt2);
   786         loop->_safepts->yank(sfpt2);
   708       }
   787       }
   709     }
   788     }
   727 
   806 
   728   // Capture bounds of the loop in the induction variable Phi before
   807   // Capture bounds of the loop in the induction variable Phi before
   729   // subsequent transformation (iteration splitting) obscures the
   808   // subsequent transformation (iteration splitting) obscures the
   730   // bounds
   809   // bounds
   731   l->phi()->as_Phi()->set_type(l->phi()->Value(&_igvn));
   810   l->phi()->as_Phi()->set_type(l->phi()->Value(&_igvn));
       
   811 
       
   812   if (strip_mine_loop) {
       
   813     l->mark_strip_mined();
       
   814     l->verify_strip_mined(1);
       
   815     outer_ilt->_head->as_Loop()->verify_strip_mined(1);
       
   816     loop = outer_ilt;
       
   817   }
   732 
   818 
   733   return true;
   819   return true;
   734 }
   820 }
   735 
   821 
   736 //----------------------exact_limit-------------------------------------------
   822 //----------------------exact_limit-------------------------------------------
   774 
   860 
   775 //------------------------------Ideal------------------------------------------
   861 //------------------------------Ideal------------------------------------------
   776 // Return a node which is more "ideal" than the current node.
   862 // Return a node which is more "ideal" than the current node.
   777 // Attempt to convert into a counted-loop.
   863 // Attempt to convert into a counted-loop.
   778 Node *LoopNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   864 Node *LoopNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   779   if (!can_be_counted_loop(phase)) {
   865   if (!can_be_counted_loop(phase) && !is_OuterStripMinedLoop()) {
   780     phase->C->set_major_progress();
   866     phase->C->set_major_progress();
   781   }
   867   }
   782   return RegionNode::Ideal(phase, can_reshape);
   868   return RegionNode::Ideal(phase, can_reshape);
   783 }
   869 }
   784 
   870 
       
   871 void LoopNode::verify_strip_mined(int expect_skeleton) const {
       
   872 #ifdef ASSERT
       
   873   const OuterStripMinedLoopNode* outer = NULL;
       
   874   const CountedLoopNode* inner = NULL;
       
   875   if (is_strip_mined()) {
       
   876     assert(is_CountedLoop(), "no Loop should be marked strip mined");
       
   877     inner = as_CountedLoop();
       
   878     outer = inner->in(LoopNode::EntryControl)->as_OuterStripMinedLoop();
       
   879   } else if (is_OuterStripMinedLoop()) {
       
   880     outer = this->as_OuterStripMinedLoop();
       
   881     inner = outer->unique_ctrl_out()->as_CountedLoop();
       
   882     assert(!is_strip_mined(), "outer loop shouldn't be marked strip mined");
       
   883   }
       
   884   if (inner != NULL || outer != NULL) {
       
   885     assert(inner != NULL && outer != NULL, "missing loop in strip mined nest");
       
   886     Node* outer_tail = outer->in(LoopNode::LoopBackControl);
       
   887     Node* outer_le = outer_tail->in(0);
       
   888     assert(outer_le->Opcode() == Op_OuterStripMinedLoopEnd, "tail of outer loop should be an If");
       
   889     Node* sfpt = outer_le->in(0);
       
   890     assert(sfpt->Opcode() == Op_SafePoint, "where's the safepoint?");
       
   891     Node* inner_out = sfpt->in(0);
       
   892     if (inner_out->outcnt() != 1) {
       
   893       ResourceMark rm;
       
   894       Unique_Node_List wq;
       
   895 
       
   896       for (DUIterator_Fast imax, i = inner_out->fast_outs(imax); i < imax; i++) {
       
   897         Node* u = inner_out->fast_out(i);
       
   898         if (u == sfpt) {
       
   899           continue;
       
   900         }
       
   901         wq.clear();
       
   902         wq.push(u);
       
   903         bool found_sfpt = false;
       
   904         for (uint next = 0; next < wq.size() && !found_sfpt; next++) {
       
   905           Node *n = wq.at(next);
       
   906           for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax && !found_sfpt; i++) {
       
   907             Node* u = n->fast_out(i);
       
   908             if (u == sfpt) {
       
   909               found_sfpt = true;
       
   910             }
       
   911             if (!u->is_CFG()) {
       
   912               wq.push(u);
       
   913             }
       
   914           }
       
   915         }
       
   916         assert(found_sfpt, "no node in loop that's not input to safepoint");
       
   917       }
       
   918     }
       
   919     CountedLoopEndNode* cle = inner_out->in(0)->as_CountedLoopEnd();
       
   920     assert(cle == inner->loopexit(), "mismatch");
       
   921     bool has_skeleton = outer_le->in(1)->bottom_type()->singleton() && outer_le->in(1)->bottom_type()->is_int()->get_con() == 0;
       
   922     if (has_skeleton) {
       
   923       assert(expect_skeleton == 1 || expect_skeleton == -1, "unexpected skeleton node");
       
   924       assert(outer->outcnt() == 2, "only phis");
       
   925     } else {
       
   926       assert(expect_skeleton == 0 || expect_skeleton == -1, "no skeleton node?");
       
   927       uint phis = 0;
       
   928       for (DUIterator_Fast imax, i = inner->fast_outs(imax); i < imax; i++) {
       
   929         Node* u = inner->fast_out(i);
       
   930         if (u->is_Phi()) {
       
   931           phis++;
       
   932         }
       
   933       }
       
   934       for (DUIterator_Fast imax, i = outer->fast_outs(imax); i < imax; i++) {
       
   935         Node* u = outer->fast_out(i);
       
   936         assert(u == outer || u == inner || u->is_Phi(), "nothing between inner and outer loop");
       
   937       }
       
   938       uint stores = 0;
       
   939       for (DUIterator_Fast imax, i = inner_out->fast_outs(imax); i < imax; i++) {
       
   940         Node* u = inner_out->fast_out(i);
       
   941         if (u->is_Store()) {
       
   942           stores++;
       
   943         }
       
   944       }
       
   945       assert(outer->outcnt() >= phis + 2 && outer->outcnt() <= phis + 2 + stores + 1, "only phis");
       
   946     }
       
   947     assert(sfpt->outcnt() == 1, "no data node");
       
   948     assert(outer_tail->outcnt() == 1 || !has_skeleton, "no data node");
       
   949   }
       
   950 #endif
       
   951 }
   785 
   952 
   786 //=============================================================================
   953 //=============================================================================
   787 //------------------------------Ideal------------------------------------------
   954 //------------------------------Ideal------------------------------------------
   788 // Return a node which is more "ideal" than the current node.
   955 // Return a node which is more "ideal" than the current node.
   789 // Attempt to convert into a counted-loop.
   956 // Attempt to convert into a counted-loop.
   800     st->print("stride: %d ",stride_con());
   967     st->print("stride: %d ",stride_con());
   801   }
   968   }
   802   if (is_pre_loop ()) st->print("pre of N%d" , _main_idx);
   969   if (is_pre_loop ()) st->print("pre of N%d" , _main_idx);
   803   if (is_main_loop()) st->print("main of N%d", _idx);
   970   if (is_main_loop()) st->print("main of N%d", _idx);
   804   if (is_post_loop()) st->print("post of N%d", _main_idx);
   971   if (is_post_loop()) st->print("post of N%d", _main_idx);
       
   972   if (is_strip_mined()) st->print(" strip mined");
   805 }
   973 }
   806 #endif
   974 #endif
   807 
   975 
   808 //=============================================================================
   976 //=============================================================================
   809 int CountedLoopEndNode::stride_con() const {
   977 int CountedLoopEndNode::stride_con() const {
   988 
  1156 
   989   // failed
  1157   // failed
   990   return NULL;
  1158   return NULL;
   991 }
  1159 }
   992 
  1160 
       
  1161 LoopNode* CountedLoopNode::skip_strip_mined(int expect_opaq) {
       
  1162   if (is_strip_mined()) {
       
  1163     verify_strip_mined(expect_opaq);
       
  1164     return in(EntryControl)->as_Loop();
       
  1165   }
       
  1166   return this;
       
  1167 }
       
  1168 
       
  1169 OuterStripMinedLoopNode* CountedLoopNode::outer_loop() const {
       
  1170   assert(is_strip_mined(), "not a strip mined loop");
       
  1171   Node* c = in(EntryControl);
       
  1172   if (c == NULL || c->is_top() || !c->is_OuterStripMinedLoop()) {
       
  1173     return NULL;
       
  1174   }
       
  1175   return c->as_OuterStripMinedLoop();
       
  1176 }
       
  1177 
       
  1178 IfTrueNode* OuterStripMinedLoopNode::outer_loop_tail() const {
       
  1179   Node* c = in(LoopBackControl);
       
  1180   if (c == NULL || c->is_top()) {
       
  1181     return NULL;
       
  1182   }
       
  1183   return c->as_IfTrue();
       
  1184 }
       
  1185 
       
  1186 IfTrueNode* CountedLoopNode::outer_loop_tail() const {
       
  1187   LoopNode* l = outer_loop();
       
  1188   if (l == NULL) {
       
  1189     return NULL;
       
  1190   }
       
  1191   return l->outer_loop_tail();
       
  1192 }
       
  1193 
       
  1194 OuterStripMinedLoopEndNode* OuterStripMinedLoopNode::outer_loop_end() const {
       
  1195   IfTrueNode* proj = outer_loop_tail();
       
  1196   if (proj == NULL) {
       
  1197     return NULL;
       
  1198   }
       
  1199   Node* c = proj->in(0);
       
  1200   if (c == NULL || c->is_top() || c->outcnt() != 2) {
       
  1201     return NULL;
       
  1202   }
       
  1203   return c->as_OuterStripMinedLoopEnd();
       
  1204 }
       
  1205 
       
  1206 OuterStripMinedLoopEndNode* CountedLoopNode::outer_loop_end() const {
       
  1207   LoopNode* l = outer_loop();
       
  1208   if (l == NULL) {
       
  1209     return NULL;
       
  1210   }
       
  1211   return l->outer_loop_end();
       
  1212 }
       
  1213 
       
  1214 IfFalseNode* OuterStripMinedLoopNode::outer_loop_exit() const {
       
  1215   IfNode* le = outer_loop_end();
       
  1216   if (le == NULL) {
       
  1217     return NULL;
       
  1218   }
       
  1219   Node* c = le->proj_out(false);
       
  1220   if (c == NULL) {
       
  1221     return NULL;
       
  1222   }
       
  1223   return c->as_IfFalse();
       
  1224 }
       
  1225 
       
  1226 IfFalseNode* CountedLoopNode::outer_loop_exit() const {
       
  1227   LoopNode* l = outer_loop();
       
  1228   if (l == NULL) {
       
  1229     return NULL;
       
  1230   }
       
  1231   return l->outer_loop_exit();
       
  1232 }
       
  1233 
       
  1234 SafePointNode* OuterStripMinedLoopNode::outer_safepoint() const {
       
  1235   IfNode* le = outer_loop_end();
       
  1236   if (le == NULL) {
       
  1237     return NULL;
       
  1238   }
       
  1239   Node* c = le->in(0);
       
  1240   if (c == NULL || c->is_top()) {
       
  1241     return NULL;
       
  1242   }
       
  1243   assert(c->Opcode() == Op_SafePoint, "broken outer loop");
       
  1244   return c->as_SafePoint();
       
  1245 }
       
  1246 
       
  1247 SafePointNode* CountedLoopNode::outer_safepoint() const {
       
  1248   LoopNode* l = outer_loop();
       
  1249   if (l == NULL) {
       
  1250     return NULL;
       
  1251   }
       
  1252   return l->outer_safepoint();
       
  1253 }
       
  1254 
       
  1255 void OuterStripMinedLoopNode::adjust_strip_mined_loop(PhaseIterGVN* igvn) {
       
  1256   // Look for the outer & inner strip mined loop, reduce number of
       
  1257   // iterations of the inner loop, set exit condition of outer loop,
       
  1258   // construct required phi nodes for outer loop.
       
  1259   CountedLoopNode* inner_cl = unique_ctrl_out()->as_CountedLoop();
       
  1260   assert(inner_cl->is_strip_mined(), "inner loop should be strip mined");
       
  1261   Node* inner_iv_phi = inner_cl->phi();
       
  1262   if (inner_iv_phi == NULL) {
       
  1263     return;
       
  1264   }
       
  1265   CountedLoopEndNode* inner_cle = inner_cl->loopexit();
       
  1266 
       
  1267   int stride = inner_cl->stride_con();
       
  1268   jlong scaled_iters_long = ((jlong)LoopStripMiningIter) * ABS(stride);
       
  1269   int scaled_iters = (int)scaled_iters_long;
       
  1270   int short_scaled_iters = LoopStripMiningIterShortLoop* ABS(stride);
       
  1271   const TypeInt* inner_iv_t = igvn->type(inner_iv_phi)->is_int();
       
  1272   jlong iter_estimate = (jlong)inner_iv_t->_hi - (jlong)inner_iv_t->_lo;
       
  1273   assert(iter_estimate > 0, "broken");
       
  1274   if ((jlong)scaled_iters != scaled_iters_long || iter_estimate <= short_scaled_iters) {
       
  1275     // Remove outer loop and safepoint (too few iterations)
       
  1276     Node* outer_sfpt = outer_safepoint();
       
  1277     Node* outer_out = outer_loop_exit();
       
  1278     igvn->replace_node(outer_out, outer_sfpt->in(0));
       
  1279     igvn->replace_input_of(outer_sfpt, 0, igvn->C->top());
       
  1280     inner_cl->clear_strip_mined();
       
  1281     return;
       
  1282   }
       
  1283   if (iter_estimate <= scaled_iters_long) {
       
  1284     // We would only go through one iteration of
       
  1285     // the outer loop: drop the outer loop but
       
  1286     // keep the safepoint so we don't run for
       
  1287     // too long without a safepoint
       
  1288     IfNode* outer_le = outer_loop_end();
       
  1289     Node* iff = igvn->transform(new IfNode(outer_le->in(0), outer_le->in(1), outer_le->_prob, outer_le->_fcnt));
       
  1290     igvn->replace_node(outer_le, iff);
       
  1291     inner_cl->clear_strip_mined();
       
  1292     return;
       
  1293   }
       
  1294 
       
  1295   Node* cle_tail = inner_cle->proj_out(true);
       
  1296   ResourceMark rm;
       
  1297   Node_List old_new;
       
  1298   if (cle_tail->outcnt() > 1) {
       
  1299     // Look for nodes on backedge of inner loop and clone them
       
  1300     Unique_Node_List backedge_nodes;
       
  1301     for (DUIterator_Fast imax, i = cle_tail->fast_outs(imax); i < imax; i++) {
       
  1302       Node* u = cle_tail->fast_out(i);
       
  1303       if (u != inner_cl) {
       
  1304         assert(!u->is_CFG(), "control flow on the backedge?");
       
  1305         backedge_nodes.push(u);
       
  1306       }
       
  1307     }
       
  1308     uint last = igvn->C->unique();
       
  1309     for (uint next = 0; next < backedge_nodes.size(); next++) {
       
  1310       Node* n = backedge_nodes.at(next);
       
  1311       old_new.map(n->_idx, n->clone());
       
  1312       for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
       
  1313         Node* u = n->fast_out(i);
       
  1314         assert(!u->is_CFG(), "broken");
       
  1315         if (u->_idx >= last) {
       
  1316           continue;
       
  1317         }
       
  1318         if (!u->is_Phi()) {
       
  1319           backedge_nodes.push(u);
       
  1320         } else {
       
  1321           assert(u->in(0) == inner_cl, "strange phi on the backedge");
       
  1322         }
       
  1323       }
       
  1324     }
       
  1325     // Put the clones on the outer loop backedge
       
  1326     Node* le_tail = outer_loop_tail();
       
  1327     for (uint next = 0; next < backedge_nodes.size(); next++) {
       
  1328       Node *n = old_new[backedge_nodes.at(next)->_idx];
       
  1329       for (uint i = 1; i < n->req(); i++) {
       
  1330         if (n->in(i) != NULL && old_new[n->in(i)->_idx] != NULL) {
       
  1331           n->set_req(i, old_new[n->in(i)->_idx]);
       
  1332         }
       
  1333       }
       
  1334       if (n->in(0) != NULL) {
       
  1335         assert(n->in(0) == cle_tail, "node not on backedge?");
       
  1336         n->set_req(0, le_tail);
       
  1337       }
       
  1338       igvn->register_new_node_with_optimizer(n);
       
  1339     }
       
  1340   }
       
  1341 
       
  1342   Node* iv_phi = NULL;
       
  1343   // Make a clone of each phi in the inner loop
       
  1344   // for the outer loop
       
  1345   for (uint i = 0; i < inner_cl->outcnt(); i++) {
       
  1346     Node* u = inner_cl->raw_out(i);
       
  1347     if (u->is_Phi()) {
       
  1348       assert(u->in(0) == inner_cl, "inconsistent");
       
  1349       Node* phi = u->clone();
       
  1350       phi->set_req(0, this);
       
  1351       Node* be = old_new[phi->in(LoopNode::LoopBackControl)->_idx];
       
  1352       if (be != NULL) {
       
  1353         phi->set_req(LoopNode::LoopBackControl, be);
       
  1354       }
       
  1355       phi = igvn->transform(phi);
       
  1356       igvn->replace_input_of(u, LoopNode::EntryControl, phi);
       
  1357       if (u == inner_iv_phi) {
       
  1358         iv_phi = phi;
       
  1359       }
       
  1360     }
       
  1361   }
       
  1362   Node* cle_out = inner_cle->proj_out(false);
       
  1363   if (cle_out->outcnt() > 1) {
       
  1364     // Look for chains of stores that were sunk
       
  1365     // out of the inner loop and are in the outer loop
       
  1366     for (DUIterator_Fast imax, i = cle_out->fast_outs(imax); i < imax; i++) {
       
  1367       Node* u = cle_out->fast_out(i);
       
  1368       if (u->is_Store()) {
       
  1369         Node* first = u;
       
  1370         for(;;) {
       
  1371           Node* next = first->in(MemNode::Memory);
       
  1372           if (!next->is_Store() || next->in(0) != cle_out) {
       
  1373             break;
       
  1374           }
       
  1375           first = next;
       
  1376         }
       
  1377         Node* last = u;
       
  1378         for(;;) {
       
  1379           Node* next = NULL;
       
  1380           for (DUIterator_Fast jmax, j = last->fast_outs(jmax); j < jmax; j++) {
       
  1381             Node* uu = last->fast_out(j);
       
  1382             if (uu->is_Store() && uu->in(0) == cle_out) {
       
  1383               assert(next == NULL, "only one in the outer loop");
       
  1384               next = uu;
       
  1385             }
       
  1386           }
       
  1387           if (next == NULL) {
       
  1388             break;
       
  1389           }
       
  1390           last = next;
       
  1391         }
       
  1392         Node* phi = NULL;
       
  1393         for (DUIterator_Fast jmax, j = fast_outs(jmax); j < jmax; j++) {
       
  1394           Node* uu = fast_out(j);
       
  1395           if (uu->is_Phi()) {
       
  1396             Node* be = uu->in(LoopNode::LoopBackControl);
       
  1397             while (be->is_Store() && old_new[be->_idx] != NULL) {
       
  1398               ShouldNotReachHere();
       
  1399               be = be->in(MemNode::Memory);
       
  1400             }
       
  1401             if (be == last || be == first->in(MemNode::Memory)) {
       
  1402               assert(phi == NULL, "only one phi");
       
  1403               phi = uu;
       
  1404             }
       
  1405           }
       
  1406         }
       
  1407 #ifdef ASSERT
       
  1408         for (DUIterator_Fast jmax, j = fast_outs(jmax); j < jmax; j++) {
       
  1409           Node* uu = fast_out(j);
       
  1410           if (uu->is_Phi() && uu->bottom_type() == Type::MEMORY) {
       
  1411             if (uu->adr_type() == igvn->C->get_adr_type(igvn->C->get_alias_index(u->adr_type()))) {
       
  1412               assert(phi == uu, "what's that phi?");
       
  1413             } else if (uu->adr_type() == TypePtr::BOTTOM) {
       
  1414               Node* n = uu->in(LoopNode::LoopBackControl);
       
  1415               uint limit = igvn->C->live_nodes();
       
  1416               uint i = 0;
       
  1417               while (n != uu) {
       
  1418                 i++;
       
  1419                 assert(i < limit, "infinite loop");
       
  1420                 if (n->is_Proj()) {
       
  1421                   n = n->in(0);
       
  1422                 } else if (n->is_SafePoint() || n->is_MemBar()) {
       
  1423                   n = n->in(TypeFunc::Memory);
       
  1424                 } else if (n->is_Phi()) {
       
  1425                   n = n->in(1);
       
  1426                 } else if (n->is_MergeMem()) {
       
  1427                   n = n->as_MergeMem()->memory_at(igvn->C->get_alias_index(u->adr_type()));
       
  1428                 } else if (n->is_Store() || n->is_LoadStore() || n->is_ClearArray()) {
       
  1429                   n = n->in(MemNode::Memory);
       
  1430                 } else {
       
  1431                   n->dump();
       
  1432                   ShouldNotReachHere();
       
  1433                 }
       
  1434               }
       
  1435             }
       
  1436           }
       
  1437         }
       
  1438 #endif
       
  1439         if (phi == NULL) {
       
  1440           // If the an entire chains was sunk, the
       
  1441           // inner loop has no phi for that memory
       
  1442           // slice, create one for the outer loop
       
  1443           phi = PhiNode::make(this, first->in(MemNode::Memory), Type::MEMORY,
       
  1444                               igvn->C->get_adr_type(igvn->C->get_alias_index(u->adr_type())));
       
  1445           phi->set_req(LoopNode::LoopBackControl, last);
       
  1446           phi = igvn->transform(phi);
       
  1447           igvn->replace_input_of(first, MemNode::Memory, phi);
       
  1448         } else {
       
  1449           // Or fix the outer loop fix to include
       
  1450           // that chain of stores.
       
  1451           Node* be = phi->in(LoopNode::LoopBackControl);
       
  1452           while (be->is_Store() && old_new[be->_idx] != NULL) {
       
  1453             ShouldNotReachHere();
       
  1454             be = be->in(MemNode::Memory);
       
  1455           }
       
  1456           if (be == first->in(MemNode::Memory)) {
       
  1457             if (be == phi->in(LoopNode::LoopBackControl)) {
       
  1458               igvn->replace_input_of(phi, LoopNode::LoopBackControl, last);
       
  1459             } else {
       
  1460               igvn->replace_input_of(be, MemNode::Memory, last);
       
  1461             }
       
  1462           } else {
       
  1463 #ifdef ASSERT
       
  1464             if (be == phi->in(LoopNode::LoopBackControl)) {
       
  1465               assert(phi->in(LoopNode::LoopBackControl) == last, "");
       
  1466             } else {
       
  1467               assert(be->in(MemNode::Memory) == last, "");
       
  1468             }
       
  1469 #endif
       
  1470           }
       
  1471         }
       
  1472       }
       
  1473     }
       
  1474   }
       
  1475 
       
  1476   if (iv_phi != NULL) {
       
  1477     // Now adjust the inner loop's exit condition
       
  1478     Node* limit = inner_cl->limit();
       
  1479     Node* sub = NULL;
       
  1480     if (stride > 0) {
       
  1481       sub = igvn->transform(new SubINode(limit, iv_phi));
       
  1482     } else {
       
  1483       sub = igvn->transform(new SubINode(iv_phi, limit));
       
  1484     }
       
  1485     Node* min = igvn->transform(new MinINode(sub, igvn->intcon(scaled_iters)));
       
  1486     Node* new_limit = NULL;
       
  1487     if (stride > 0) {
       
  1488       new_limit = igvn->transform(new AddINode(min, iv_phi));
       
  1489     } else {
       
  1490       new_limit = igvn->transform(new SubINode(iv_phi, min));
       
  1491     }
       
  1492     igvn->replace_input_of(inner_cle->cmp_node(), 2, new_limit);
       
  1493     Node* cmp = inner_cle->cmp_node()->clone();
       
  1494     Node* bol = inner_cle->in(CountedLoopEndNode::TestValue)->clone();
       
  1495     cmp->set_req(2, limit);
       
  1496     bol->set_req(1, igvn->transform(cmp));
       
  1497     igvn->replace_input_of(outer_loop_end(), 1, igvn->transform(bol));
       
  1498   } else {
       
  1499     assert(false, "should be able to adjust outer loop");
       
  1500     IfNode* outer_le = outer_loop_end();
       
  1501     Node* iff = igvn->transform(new IfNode(outer_le->in(0), outer_le->in(1), outer_le->_prob, outer_le->_fcnt));
       
  1502     igvn->replace_node(outer_le, iff);
       
  1503     inner_cl->clear_strip_mined();
       
  1504   }
       
  1505 }
       
  1506 
       
  1507 const Type* OuterStripMinedLoopEndNode::Value(PhaseGVN* phase) const {
       
  1508   if (!in(0)) return Type::TOP;
       
  1509   if (phase->type(in(0)) == Type::TOP)
       
  1510     return Type::TOP;
       
  1511 
       
  1512   return TypeTuple::IFBOTH;
       
  1513 }
       
  1514 
       
  1515 Node *OuterStripMinedLoopEndNode::Ideal(PhaseGVN *phase, bool can_reshape) {
       
  1516   if (remove_dead_region(phase, can_reshape))  return this;
       
  1517 
       
  1518   return NULL;
       
  1519 }
   993 
  1520 
   994 //------------------------------filtered_type--------------------------------
  1521 //------------------------------filtered_type--------------------------------
   995 // Return a type based on condition control flow
  1522 // Return a type based on condition control flow
   996 // A successful return will be a type that is restricted due
  1523 // A successful return will be a type that is restricted due
   997 // to a series of dominating if-tests, such as:
  1524 // to a series of dominating if-tests, such as:
  1776   // For grins, set the inner-loop flag here
  2303   // For grins, set the inner-loop flag here
  1777   if (!_child) {
  2304   if (!_child) {
  1778     if (_head->is_Loop()) _head->as_Loop()->set_inner_loop();
  2305     if (_head->is_Loop()) _head->as_Loop()->set_inner_loop();
  1779   }
  2306   }
  1780 
  2307 
       
  2308   IdealLoopTree* loop = this;
  1781   if (_head->is_CountedLoop() ||
  2309   if (_head->is_CountedLoop() ||
  1782       phase->is_counted_loop(_head, this)) {
  2310       phase->is_counted_loop(_head, loop)) {
  1783 
  2311 
  1784     if (!UseCountedLoopSafepoints) {
  2312     if (LoopStripMiningIter == 0 || (LoopStripMiningIter > 1 && _child == NULL)) {
  1785       // Indicate we do not need a safepoint here
  2313       // Indicate we do not need a safepoint here
  1786       _has_sfpt = 1;
  2314       _has_sfpt = 1;
  1787     }
  2315     }
  1788 
  2316 
  1789     // Remove safepoints
  2317     // Remove safepoints
  1798     bool keep_one_sfpt = true;
  2326     bool keep_one_sfpt = true;
  1799     remove_safepoints(phase, keep_one_sfpt);
  2327     remove_safepoints(phase, keep_one_sfpt);
  1800   }
  2328   }
  1801 
  2329 
  1802   // Recursively
  2330   // Recursively
  1803   if (_child) _child->counted_loop( phase );
  2331   assert(loop->_child != this || (loop->_head->as_Loop()->is_OuterStripMinedLoop() && _head->as_CountedLoop()->is_strip_mined()), "what kind of loop was added?");
  1804   if (_next)  _next ->counted_loop( phase );
  2332   assert(loop->_child != this || (loop->_child->_child == NULL && loop->_child->_next == NULL), "would miss some loops");
       
  2333   if (loop->_child && loop->_child != this) loop->_child->counted_loop(phase);
       
  2334   if (loop->_next)  loop->_next ->counted_loop(phase);
  1805 }
  2335 }
  1806 
  2336 
  1807 #ifndef PRODUCT
  2337 #ifndef PRODUCT
  1808 //------------------------------dump_head--------------------------------------
  2338 //------------------------------dump_head--------------------------------------
  1809 // Dump 1 liner for loop header info
  2339 // Dump 1 liner for loop header info
  1810 void IdealLoopTree::dump_head( ) const {
  2340 void IdealLoopTree::dump_head( ) const {
  1811   for (uint i=0; i<_nest; i++)
  2341   for (uint i=0; i<_nest; i++)
  1812     tty->print("  ");
  2342     tty->print("  ");
  1813   tty->print("Loop: N%d/N%d ",_head->_idx,_tail->_idx);
  2343   tty->print("Loop: N%d/N%d ",_head->_idx,_tail->_idx);
  1814   if (_irreducible) tty->print(" IRREDUCIBLE");
  2344   if (_irreducible) tty->print(" IRREDUCIBLE");
  1815   Node* entry = _head->in(LoopNode::EntryControl);
  2345   Node* entry = _head->as_Loop()->skip_strip_mined(-1)->in(LoopNode::EntryControl);
  1816   Node* predicate = PhaseIdealLoop::find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
  2346   Node* predicate = PhaseIdealLoop::find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
  1817   if (predicate != NULL ) {
  2347   if (predicate != NULL ) {
  1818     tty->print(" limit_check");
  2348     tty->print(" limit_check");
  1819     entry = entry->in(0)->in(0);
  2349     entry = entry->in(0)->in(0);
  1820   }
  2350   }
  1860   if (_required_safept != NULL && _required_safept->size() > 0) {
  2390   if (_required_safept != NULL && _required_safept->size() > 0) {
  1861     tty->print(" req={"); _required_safept->dump_simple(); tty->print(" }");
  2391     tty->print(" req={"); _required_safept->dump_simple(); tty->print(" }");
  1862   }
  2392   }
  1863   if (Verbose) {
  2393   if (Verbose) {
  1864     tty->print(" body={"); _body.dump_simple(); tty->print(" }");
  2394     tty->print(" body={"); _body.dump_simple(); tty->print(" }");
       
  2395   }
       
  2396   if (_head->as_Loop()->is_strip_mined()) {
       
  2397     tty->print(" strip_mined");
  1865   }
  2398   }
  1866   tty->cr();
  2399   tty->cr();
  1867 }
  2400 }
  1868 
  2401 
  1869 //------------------------------dump-------------------------------------------
  2402 //------------------------------dump-------------------------------------------
  3230 // be guaranteed anymore.
  3763 // be guaranteed anymore.
  3231 bool PhaseIdealLoop::is_canonical_loop_entry(CountedLoopNode* cl) {
  3764 bool PhaseIdealLoop::is_canonical_loop_entry(CountedLoopNode* cl) {
  3232   if (!cl->is_main_loop() && !cl->is_post_loop()) {
  3765   if (!cl->is_main_loop() && !cl->is_post_loop()) {
  3233     return false;
  3766     return false;
  3234   }
  3767   }
  3235   Node* ctrl = cl->in(LoopNode::EntryControl);
  3768   Node* ctrl = cl->skip_strip_mined()->in(LoopNode::EntryControl);
  3236   if (ctrl == NULL || (!ctrl->is_IfTrue() && !ctrl->is_IfFalse())) {
  3769   if (ctrl == NULL || (!ctrl->is_IfTrue() && !ctrl->is_IfFalse())) {
  3237     return false;
  3770     return false;
  3238   }
  3771   }
  3239   Node* iffm = ctrl->in(0);
  3772   Node* iffm = ctrl->in(0);
  3240   if (iffm == NULL || !iffm->is_If()) {
  3773   if (iffm == NULL || !iffm->is_If()) {
  3290       Node* s = mem->fast_out(i);
  3823       Node* s = mem->fast_out(i);
  3291       worklist.push(s);
  3824       worklist.push(s);
  3292     }
  3825     }
  3293     while(worklist.size() != 0 && LCA != early) {
  3826     while(worklist.size() != 0 && LCA != early) {
  3294       Node* s = worklist.pop();
  3827       Node* s = worklist.pop();
  3295       if (s->is_Load()) {
  3828       if (s->is_Load() || s->Opcode() == Op_SafePoint) {
  3296         continue;
  3829         continue;
  3297       } else if (s->is_MergeMem()) {
  3830       } else if (s->is_MergeMem()) {
  3298         for (DUIterator_Fast imax, i = s->fast_outs(imax); i < imax; i++) {
  3831         for (DUIterator_Fast imax, i = s->fast_outs(imax); i < imax; i++) {
  3299           Node* s1 = s->fast_out(i);
  3832           Node* s1 = s->fast_out(i);
  3300           worklist.push(s1);
  3833           worklist.push(s1);
  3469       }
  4002       }
  3470     }
  4003     }
  3471   }
  4004   }
  3472 }
  4005 }
  3473 
  4006 
       
  4007 // Verify that no data node is schedules in the outer loop of a strip
       
  4008 // mined loop.
       
  4009 void PhaseIdealLoop::verify_strip_mined_scheduling(Node *n, Node* least) {
       
  4010 #ifdef ASSERT
       
  4011   if (get_loop(least)->_nest == 0) {
       
  4012     return;
       
  4013   }
       
  4014   IdealLoopTree* loop = get_loop(least);
       
  4015   Node* head = loop->_head;
       
  4016   if (head->is_OuterStripMinedLoop()) {
       
  4017     Node* sfpt = head->as_Loop()->outer_safepoint();
       
  4018     ResourceMark rm;
       
  4019     Unique_Node_List wq;
       
  4020     wq.push(sfpt);
       
  4021     for (uint i = 0; i < wq.size(); i++) {
       
  4022       Node *m = wq.at(i);
       
  4023       for (uint i = 1; i < m->req(); i++) {
       
  4024         Node* nn = m->in(i);
       
  4025         if (nn == n) {
       
  4026           return;
       
  4027         }
       
  4028         if (nn != NULL && has_ctrl(nn) && get_loop(get_ctrl(nn)) == loop) {
       
  4029           wq.push(nn);
       
  4030         }
       
  4031       }
       
  4032     }
       
  4033     ShouldNotReachHere();
       
  4034   }
       
  4035 #endif
       
  4036 }
       
  4037 
       
  4038 
  3474 //------------------------------build_loop_late_post---------------------------
  4039 //------------------------------build_loop_late_post---------------------------
  3475 // Put Data nodes into some loop nest, by setting the _nodes[]->loop mapping.
  4040 // Put Data nodes into some loop nest, by setting the _nodes[]->loop mapping.
  3476 // Second pass finds latest legal placement, and ideal loop placement.
  4041 // Second pass finds latest legal placement, and ideal loop placement.
  3477 void PhaseIdealLoop::build_loop_late_post( Node *n ) {
  4042 void PhaseIdealLoop::build_loop_late_post( Node *n ) {
  3478 
  4043 
  3578 
  4143 
  3579   // Try not to place code on a loop entry projection
  4144   // Try not to place code on a loop entry projection
  3580   // which can inhibit range check elimination.
  4145   // which can inhibit range check elimination.
  3581   if (least != early) {
  4146   if (least != early) {
  3582     Node* ctrl_out = least->unique_ctrl_out();
  4147     Node* ctrl_out = least->unique_ctrl_out();
  3583     if (ctrl_out && ctrl_out->is_CountedLoop() &&
  4148     if (ctrl_out && ctrl_out->is_Loop() &&
  3584         least == ctrl_out->in(LoopNode::EntryControl)) {
  4149         least == ctrl_out->in(LoopNode::EntryControl) &&
       
  4150         (ctrl_out->is_CountedLoop() || ctrl_out->is_OuterStripMinedLoop())) {
  3585       Node* least_dom = idom(least);
  4151       Node* least_dom = idom(least);
  3586       if (get_loop(least_dom)->is_member(get_loop(least))) {
  4152       if (get_loop(least_dom)->is_member(get_loop(least))) {
  3587         least = least_dom;
  4153         least = least_dom;
  3588       }
  4154       }
  3589     }
  4155     }
  3604   }
  4170   }
  3605 #endif
  4171 #endif
  3606 
  4172 
  3607   // Assign discovered "here or above" point
  4173   // Assign discovered "here or above" point
  3608   least = find_non_split_ctrl(least);
  4174   least = find_non_split_ctrl(least);
       
  4175   verify_strip_mined_scheduling(n, least);
  3609   set_ctrl(n, least);
  4176   set_ctrl(n, least);
  3610 
  4177 
  3611   // Collect inner loop bodies
  4178   // Collect inner loop bodies
  3612   IdealLoopTree *chosen_loop = get_loop(least);
  4179   IdealLoopTree *chosen_loop = get_loop(least);
  3613   if( !chosen_loop->_child )   // Inner loop?
  4180   if( !chosen_loop->_child )   // Inner loop?