< prev index next >

src/share/vm/opto/loopPredicate.cpp

Print this page




  75 //     rgn       loop                          |         iff
  76 //      |                                      |        /     \
  77 //      |                                      |       /       \
  78 //      v                                      |      v         v
  79 // uncommon_trap                               | uncommon_proj cont_proj
  80 //                                           \  \    |           |
  81 //                                            \  \   |           |
  82 //                                             v  v  v           v
  83 //                                               rgn           loop
  84 //                                                |
  85 //                                                |
  86 //                                                v
  87 //                                           uncommon_trap
  88 //
  89 //
  90 // We will create a region to guard the uct call if there is no one there.
  91 // The true projecttion (if_cont) of the new_iff is returned.
  92 // This code is also used to clone predicates to cloned loops.
  93 ProjNode* PhaseIdealLoop::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry,
  94                                                       Deoptimization::DeoptReason reason,
  95                                                       int opcode) {
  96   assert(cont_proj->is_uncommon_trap_if_pattern(reason), "must be a uct if pattern!");
  97   IfNode* iff = cont_proj->in(0)->as_If();
  98 
  99   ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con);
 100   Node     *rgn   = uncommon_proj->unique_ctrl_out();
 101   assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
 102 
 103   uint proj_index = 1; // region's edge corresponding to uncommon_proj
 104   if (!rgn->is_Region()) { // create a region to guard the call
 105     assert(rgn->is_Call(), "must be call uct");
 106     CallNode* call = rgn->as_Call();
 107     IdealLoopTree* loop = get_loop(call);
 108     rgn = new RegionNode(1);
 109     rgn->add_req(uncommon_proj);
 110     register_control(rgn, loop, uncommon_proj);
 111     _igvn.replace_input_of(call, 0, rgn);
 112     // When called from beautify_loops() idom is not constructed yet.
 113     if (_idom != NULL) {
 114       set_idom(call, rgn, dom_depth(rgn));
 115     }


 118       if (n->is_Load() || n->is_Store()) {
 119         _igvn.replace_input_of(n, 0, rgn);
 120         --i; --imax;
 121       }
 122     }
 123   } else {
 124     // Find region's edge corresponding to uncommon_proj
 125     for (; proj_index < rgn->req(); proj_index++)
 126       if (rgn->in(proj_index) == uncommon_proj) break;
 127     assert(proj_index < rgn->req(), "sanity");
 128   }
 129 
 130   Node* entry = iff->in(0);
 131   if (new_entry != NULL) {
 132     // Clonning the predicate to new location.
 133     entry = new_entry;
 134   }
 135   // Create new_iff
 136   IdealLoopTree* lp = get_loop(entry);
 137   IfNode* new_iff = NULL;
 138   if (opcode == Op_If) {
 139     new_iff = new IfNode(entry, iff->in(1), iff->_prob, iff->_fcnt);
 140   } else {
 141     assert(opcode == Op_RangeCheck, "no other if variant here");
 142     new_iff = new RangeCheckNode(entry, iff->in(1), iff->_prob, iff->_fcnt);
 143   }
 144   register_control(new_iff, lp, entry);
 145   Node *if_cont = new IfTrueNode(new_iff);
 146   Node *if_uct  = new IfFalseNode(new_iff);
 147   if (cont_proj->is_IfFalse()) {
 148     // Swap
 149     Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp;
 150   }
 151   register_control(if_cont, lp, new_iff);
 152   register_control(if_uct, get_loop(rgn), new_iff);
 153 
 154   // if_uct to rgn
 155   _igvn.hash_delete(rgn);
 156   rgn->add_req(if_uct);
 157   // When called from beautify_loops() idom is not constructed yet.
 158   if (_idom != NULL) {
 159     Node* ridom = idom(rgn);
 160     Node* nrdom = dom_lca(ridom, new_iff);
 161     set_idom(rgn, nrdom, dom_depth(rgn));


 173       use->add_req(use->in(proj_index));
 174       has_phi = true;
 175     }
 176   }
 177   assert(!has_phi || rgn->req() > 3, "no phis when region is created");
 178 
 179   if (new_entry == NULL) {
 180     // Attach if_cont to iff
 181     _igvn.replace_input_of(iff, 0, if_cont);
 182     if (_idom != NULL) {
 183       set_idom(iff, if_cont, dom_depth(iff));
 184     }
 185   }
 186   return if_cont->as_Proj();
 187 }
 188 
 189 //------------------------------create_new_if_for_predicate------------------------
 190 // Create a new if below new_entry for the predicate to be cloned (IGVN optimization)
 191 ProjNode* PhaseIterGVN::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry,
 192                                                     Deoptimization::DeoptReason reason,
 193                                                     int opcode) {
 194   assert(new_entry != 0, "only used for clone predicate");
 195   assert(cont_proj->is_uncommon_trap_if_pattern(reason), "must be a uct if pattern!");
 196   IfNode* iff = cont_proj->in(0)->as_If();
 197 
 198   ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con);
 199   Node     *rgn   = uncommon_proj->unique_ctrl_out();
 200   assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
 201 
 202   uint proj_index = 1; // region's edge corresponding to uncommon_proj
 203   if (!rgn->is_Region()) { // create a region to guard the call
 204     assert(rgn->is_Call(), "must be call uct");
 205     CallNode* call = rgn->as_Call();
 206     rgn = new RegionNode(1);
 207     register_new_node_with_optimizer(rgn);
 208     rgn->add_req(uncommon_proj);
 209     replace_input_of(call, 0, rgn);
 210   } else {
 211     // Find region's edge corresponding to uncommon_proj
 212     for (; proj_index < rgn->req(); proj_index++)
 213       if (rgn->in(proj_index) == uncommon_proj) break;
 214     assert(proj_index < rgn->req(), "sanity");
 215   }
 216 
 217   // Create new_iff in new location.
 218   IfNode* new_iff = NULL;
 219   if (opcode == Op_If) {
 220     new_iff = new IfNode(new_entry, iff->in(1), iff->_prob, iff->_fcnt);
 221   } else {
 222     assert(opcode == Op_RangeCheck, "no other if variant here");
 223     new_iff = new RangeCheckNode(new_entry, iff->in(1), iff->_prob, iff->_fcnt);
 224   }
 225 
 226   register_new_node_with_optimizer(new_iff);
 227   Node *if_cont = new IfTrueNode(new_iff);
 228   Node *if_uct  = new IfFalseNode(new_iff);
 229   if (cont_proj->is_IfFalse()) {
 230     // Swap
 231     Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp;
 232   }
 233   register_new_node_with_optimizer(if_cont);
 234   register_new_node_with_optimizer(if_uct);
 235 
 236   // if_uct to rgn
 237   hash_delete(rgn);
 238   rgn->add_req(if_uct);
 239 
 240   // If rgn has phis add corresponding new edges which has the same
 241   // value as on original uncommon_proj pass.
 242   assert(rgn->in(rgn->req() -1) == if_uct, "new edge should be last");


 244   for (DUIterator_Fast imax, i = rgn->fast_outs(imax); i < imax; i++) {
 245     Node* use = rgn->fast_out(i);
 246     if (use->is_Phi() && use->outcnt() > 0) {
 247       rehash_node_delayed(use);
 248       use->add_req(use->in(proj_index));
 249       has_phi = true;
 250     }
 251   }
 252   assert(!has_phi || rgn->req() > 3, "no phis when region is created");
 253 
 254   return if_cont->as_Proj();
 255 }
 256 
 257 //--------------------------clone_predicate-----------------------
 258 ProjNode* PhaseIdealLoop::clone_predicate(ProjNode* predicate_proj, Node* new_entry,
 259                                           Deoptimization::DeoptReason reason,
 260                                           PhaseIdealLoop* loop_phase,
 261                                           PhaseIterGVN* igvn) {
 262   ProjNode* new_predicate_proj;
 263   if (loop_phase != NULL) {
 264     new_predicate_proj = loop_phase->create_new_if_for_predicate(predicate_proj, new_entry, reason, Op_If);
 265   } else {
 266     new_predicate_proj =       igvn->create_new_if_for_predicate(predicate_proj, new_entry, reason, Op_If);
 267   }
 268   IfNode* iff = new_predicate_proj->in(0)->as_If();
 269   Node* ctrl  = iff->in(0);
 270 
 271   // Match original condition since predicate's projections could be swapped.
 272   assert(predicate_proj->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be");
 273   Node* opq = new Opaque1Node(igvn->C, predicate_proj->in(0)->in(1)->in(1)->in(1));
 274   igvn->C->add_predicate_opaq(opq);
 275 
 276   Node* bol = new Conv2BNode(opq);
 277   if (loop_phase != NULL) {
 278     loop_phase->register_new_node(opq, ctrl);
 279     loop_phase->register_new_node(bol, ctrl);
 280   } else {
 281     igvn->register_new_node_with_optimizer(opq);
 282     igvn->register_new_node_with_optimizer(bol);
 283   }
 284   igvn->hash_delete(iff);
 285   iff->set_req(1, bol);
 286   return new_predicate_proj;
 287 }
 288 
 289 
 290 //--------------------------clone_loop_predicates-----------------------
 291 // Interface from IGVN
 292 Node* PhaseIterGVN::clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check) {


 566 
 567 //------------------------------is_range_check_if -----------------------------------
 568 // Returns true if the predicate of iff is in "scale*iv + offset u< load_range(ptr)" format
 569 // Note: this function is particularly designed for loop predication. We require load_range
 570 //       and offset to be loop invariant computed on the fly by "invar"
 571 bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const {
 572   if (!is_loop_exit(iff)) {
 573     return false;
 574   }
 575   if (!iff->in(1)->is_Bool()) {
 576     return false;
 577   }
 578   const BoolNode *bol = iff->in(1)->as_Bool();
 579   if (bol->_test._test != BoolTest::lt) {
 580     return false;
 581   }
 582   if (!bol->in(1)->is_Cmp()) {
 583     return false;
 584   }
 585   const CmpNode *cmp = bol->in(1)->as_Cmp();
 586   if (cmp->Opcode() != Op_CmpU) {
 587     return false;
 588   }
 589   Node* range = cmp->in(2);
 590   if (range->Opcode() != Op_LoadRange && !iff->is_RangeCheck()) {
 591     const TypeInt* tint = phase->_igvn.type(range)->isa_int();
 592     if (tint == NULL || tint->empty() || tint->_lo < 0) {
 593       // Allow predication on positive values that aren't LoadRanges.
 594       // This allows optimization of loops where the length of the
 595       // array is a known value and doesn't need to be loaded back
 596       // from the array.
 597       return false;
 598     }
 599   }
 600   if (!invar.is_invariant(range)) {
 601     return false;
 602   }
 603   Node *iv     = _head->as_CountedLoop()->phi();
 604   int   scale  = 0;
 605   Node *offset = NULL;
 606   if (!phase->is_scaled_iv_plus_offset(cmp->in(1), iv, &scale, &offset)) {
 607     return false;
 608   }
 609   if (offset && !invar.is_invariant(offset)) { // offset must be invariant
 610     return false;


 673   register_new_node(bol, ctrl);
 674 
 675   if (TraceLoopPredicate) {
 676     predString->print_cr("<u range");
 677     tty->print("%s", predString->as_string());
 678   }
 679   return bol;
 680 }
 681 
 682 //------------------------------ loop_predication_impl--------------------------
 683 // Insert loop predicates for null checks and range checks
 684 bool PhaseIdealLoop::loop_predication_impl(IdealLoopTree *loop) {
 685   if (!UseLoopPredicate) return false;
 686 
 687   if (!loop->_head->is_Loop()) {
 688     // Could be a simple region when irreducible loops are present.
 689     return false;
 690   }
 691   LoopNode* head = loop->_head->as_Loop();
 692 
 693   if (head->unique_ctrl_out()->Opcode() == Op_NeverBranch) {
 694     // do nothing for infinite loops
 695     return false;
 696   }
 697 
 698   CountedLoopNode *cl = NULL;
 699   if (head->is_valid_counted_loop()) {
 700     cl = head->as_CountedLoop();
 701     // do nothing for iteration-splitted loops
 702     if (!cl->is_normal_loop()) return false;
 703     // Avoid RCE if Counted loop's test is '!='.
 704     BoolTest::mask bt = cl->loopexit()->test_trip();
 705     if (bt != BoolTest::lt && bt != BoolTest::gt)
 706       cl = NULL;
 707   }
 708 
 709   Node* entry = head->in(LoopNode::EntryControl);
 710   ProjNode *predicate_proj = NULL;
 711   // Loop limit check predicate should be near the loop.
 712   predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
 713   if (predicate_proj != NULL)


 719       tty->print("missing predicate:");
 720       loop->dump_head();
 721       head->dump(1);
 722     }
 723 #endif
 724     return false;
 725   }
 726   ConNode* zero = _igvn.intcon(0);
 727   set_ctrl(zero, C->root());
 728 
 729   ResourceArea *area = Thread::current()->resource_area();
 730   Invariance invar(area, loop);
 731 
 732   // Create list of if-projs such that a newer proj dominates all older
 733   // projs in the list, and they all dominate loop->tail()
 734   Node_List if_proj_list(area);
 735   Node *current_proj = loop->tail(); //start from tail
 736   while (current_proj != head) {
 737     if (loop == get_loop(current_proj) && // still in the loop ?
 738         current_proj->is_Proj()        && // is a projection  ?
 739         (current_proj->in(0)->Opcode() == Op_If ||
 740          current_proj->in(0)->Opcode() == Op_RangeCheck)) { // is a if projection ?
 741       if_proj_list.push(current_proj);
 742     }
 743     current_proj = idom(current_proj);
 744   }
 745 
 746   bool hoisted = false; // true if at least one proj is promoted
 747   while (if_proj_list.size() > 0) {
 748     // Following are changed to nonnull when a predicate can be hoisted
 749     ProjNode* new_predicate_proj = NULL;
 750 
 751     ProjNode* proj = if_proj_list.pop()->as_Proj();
 752     IfNode*   iff  = proj->in(0)->as_If();
 753 
 754     if (!proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none)) {
 755       if (loop->is_loop_exit(iff)) {
 756         // stop processing the remaining projs in the list because the execution of them
 757         // depends on the condition of "iff" (iff->in(1)).
 758         break;
 759       } else {
 760         // Both arms are inside the loop. There are two cases:


 789         negated = true;
 790       }
 791       IfNode* new_predicate_iff = new_predicate_proj->in(0)->as_If();
 792       _igvn.hash_delete(new_predicate_iff);
 793       new_predicate_iff->set_req(1, new_predicate_bol);
 794 #ifndef PRODUCT
 795       if (TraceLoopPredicate) {
 796         tty->print("Predicate invariant if%s: %d ", negated ? " negated" : "", new_predicate_iff->_idx);
 797         loop->dump_head();
 798       } else if (TraceLoopOpts) {
 799         tty->print("Predicate IC ");
 800         loop->dump_head();
 801       }
 802 #endif
 803     } else if (cl != NULL && loop->is_range_check_if(iff, this, invar)) {
 804       // Range check for counted loops
 805       const Node*    cmp    = bol->in(1)->as_Cmp();
 806       Node*          idx    = cmp->in(1);
 807       assert(!invar.is_invariant(idx), "index is variant");
 808       Node* rng = cmp->in(2);
 809       assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int() >= 0, "must be");
 810       assert(invar.is_invariant(rng), "range must be invariant");
 811       int scale    = 1;
 812       Node* offset = zero;
 813       bool ok = is_scaled_iv_plus_offset(idx, cl->phi(), &scale, &offset);
 814       assert(ok, "must be index expression");
 815 
 816       Node* init    = cl->init_trip();
 817       Node* limit   = cl->limit();
 818       Node* stride  = cl->stride();
 819 
 820       // Build if's for the upper and lower bound tests.  The
 821       // lower_bound test will dominate the upper bound test and all
 822       // cloned or created nodes will use the lower bound test as
 823       // their declared control.
 824       ProjNode* lower_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate, iff->Opcode());
 825       ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate, iff->Opcode());
 826       assert(upper_bound_proj->in(0)->as_If()->in(0) == lower_bound_proj, "should dominate");
 827       Node *ctrl = lower_bound_proj->in(0)->as_If()->in(0);
 828 
 829       // Perform cloning to keep Invariance state correct since the




  75 //     rgn       loop                          |         iff
  76 //      |                                      |        /     \
  77 //      |                                      |       /       \
  78 //      v                                      |      v         v
  79 // uncommon_trap                               | uncommon_proj cont_proj
  80 //                                           \  \    |           |
  81 //                                            \  \   |           |
  82 //                                             v  v  v           v
  83 //                                               rgn           loop
  84 //                                                |
  85 //                                                |
  86 //                                                v
  87 //                                           uncommon_trap
  88 //
  89 //
  90 // We will create a region to guard the uct call if there is no one there.
  91 // The true projecttion (if_cont) of the new_iff is returned.
  92 // This code is also used to clone predicates to cloned loops.
  93 ProjNode* PhaseIdealLoop::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry,
  94                                                       Deoptimization::DeoptReason reason,
  95                                                       Opcodes opcode) {
  96   assert(cont_proj->is_uncommon_trap_if_pattern(reason), "must be a uct if pattern!");
  97   IfNode* iff = cont_proj->in(0)->as_If();
  98 
  99   ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con);
 100   Node     *rgn   = uncommon_proj->unique_ctrl_out();
 101   assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
 102 
 103   uint proj_index = 1; // region's edge corresponding to uncommon_proj
 104   if (!rgn->is_Region()) { // create a region to guard the call
 105     assert(rgn->is_Call(), "must be call uct");
 106     CallNode* call = rgn->as_Call();
 107     IdealLoopTree* loop = get_loop(call);
 108     rgn = new RegionNode(1);
 109     rgn->add_req(uncommon_proj);
 110     register_control(rgn, loop, uncommon_proj);
 111     _igvn.replace_input_of(call, 0, rgn);
 112     // When called from beautify_loops() idom is not constructed yet.
 113     if (_idom != NULL) {
 114       set_idom(call, rgn, dom_depth(rgn));
 115     }


 118       if (n->is_Load() || n->is_Store()) {
 119         _igvn.replace_input_of(n, 0, rgn);
 120         --i; --imax;
 121       }
 122     }
 123   } else {
 124     // Find region's edge corresponding to uncommon_proj
 125     for (; proj_index < rgn->req(); proj_index++)
 126       if (rgn->in(proj_index) == uncommon_proj) break;
 127     assert(proj_index < rgn->req(), "sanity");
 128   }
 129 
 130   Node* entry = iff->in(0);
 131   if (new_entry != NULL) {
 132     // Clonning the predicate to new location.
 133     entry = new_entry;
 134   }
 135   // Create new_iff
 136   IdealLoopTree* lp = get_loop(entry);
 137   IfNode* new_iff = NULL;
 138   if (opcode == Opcodes::Op_If) {
 139     new_iff = new IfNode(entry, iff->in(1), iff->_prob, iff->_fcnt);
 140   } else {
 141     assert(opcode == Opcodes::Op_RangeCheck, "no other if variant here");
 142     new_iff = new RangeCheckNode(entry, iff->in(1), iff->_prob, iff->_fcnt);
 143   }
 144   register_control(new_iff, lp, entry);
 145   Node *if_cont = new IfTrueNode(new_iff);
 146   Node *if_uct  = new IfFalseNode(new_iff);
 147   if (cont_proj->is_IfFalse()) {
 148     // Swap
 149     Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp;
 150   }
 151   register_control(if_cont, lp, new_iff);
 152   register_control(if_uct, get_loop(rgn), new_iff);
 153 
 154   // if_uct to rgn
 155   _igvn.hash_delete(rgn);
 156   rgn->add_req(if_uct);
 157   // When called from beautify_loops() idom is not constructed yet.
 158   if (_idom != NULL) {
 159     Node* ridom = idom(rgn);
 160     Node* nrdom = dom_lca(ridom, new_iff);
 161     set_idom(rgn, nrdom, dom_depth(rgn));


 173       use->add_req(use->in(proj_index));
 174       has_phi = true;
 175     }
 176   }
 177   assert(!has_phi || rgn->req() > 3, "no phis when region is created");
 178 
 179   if (new_entry == NULL) {
 180     // Attach if_cont to iff
 181     _igvn.replace_input_of(iff, 0, if_cont);
 182     if (_idom != NULL) {
 183       set_idom(iff, if_cont, dom_depth(iff));
 184     }
 185   }
 186   return if_cont->as_Proj();
 187 }
 188 
 189 //------------------------------create_new_if_for_predicate------------------------
 190 // Create a new if below new_entry for the predicate to be cloned (IGVN optimization)
 191 ProjNode* PhaseIterGVN::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry,
 192                                                     Deoptimization::DeoptReason reason,
 193                                                     Opcodes opcode) {
 194   assert(new_entry != 0, "only used for clone predicate");
 195   assert(cont_proj->is_uncommon_trap_if_pattern(reason), "must be a uct if pattern!");
 196   IfNode* iff = cont_proj->in(0)->as_If();
 197 
 198   ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con);
 199   Node     *rgn   = uncommon_proj->unique_ctrl_out();
 200   assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
 201 
 202   uint proj_index = 1; // region's edge corresponding to uncommon_proj
 203   if (!rgn->is_Region()) { // create a region to guard the call
 204     assert(rgn->is_Call(), "must be call uct");
 205     CallNode* call = rgn->as_Call();
 206     rgn = new RegionNode(1);
 207     register_new_node_with_optimizer(rgn);
 208     rgn->add_req(uncommon_proj);
 209     replace_input_of(call, 0, rgn);
 210   } else {
 211     // Find region's edge corresponding to uncommon_proj
 212     for (; proj_index < rgn->req(); proj_index++)
 213       if (rgn->in(proj_index) == uncommon_proj) break;
 214     assert(proj_index < rgn->req(), "sanity");
 215   }
 216 
 217   // Create new_iff in new location.
 218   IfNode* new_iff = NULL;
 219   if (opcode == Opcodes::Op_If) {
 220     new_iff = new IfNode(new_entry, iff->in(1), iff->_prob, iff->_fcnt);
 221   } else {
 222     assert(opcode == Opcodes::Op_RangeCheck, "no other if variant here");
 223     new_iff = new RangeCheckNode(new_entry, iff->in(1), iff->_prob, iff->_fcnt);
 224   }
 225 
 226   register_new_node_with_optimizer(new_iff);
 227   Node *if_cont = new IfTrueNode(new_iff);
 228   Node *if_uct  = new IfFalseNode(new_iff);
 229   if (cont_proj->is_IfFalse()) {
 230     // Swap
 231     Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp;
 232   }
 233   register_new_node_with_optimizer(if_cont);
 234   register_new_node_with_optimizer(if_uct);
 235 
 236   // if_uct to rgn
 237   hash_delete(rgn);
 238   rgn->add_req(if_uct);
 239 
 240   // If rgn has phis add corresponding new edges which has the same
 241   // value as on original uncommon_proj pass.
 242   assert(rgn->in(rgn->req() -1) == if_uct, "new edge should be last");


 244   for (DUIterator_Fast imax, i = rgn->fast_outs(imax); i < imax; i++) {
 245     Node* use = rgn->fast_out(i);
 246     if (use->is_Phi() && use->outcnt() > 0) {
 247       rehash_node_delayed(use);
 248       use->add_req(use->in(proj_index));
 249       has_phi = true;
 250     }
 251   }
 252   assert(!has_phi || rgn->req() > 3, "no phis when region is created");
 253 
 254   return if_cont->as_Proj();
 255 }
 256 
 257 //--------------------------clone_predicate-----------------------
 258 ProjNode* PhaseIdealLoop::clone_predicate(ProjNode* predicate_proj, Node* new_entry,
 259                                           Deoptimization::DeoptReason reason,
 260                                           PhaseIdealLoop* loop_phase,
 261                                           PhaseIterGVN* igvn) {
 262   ProjNode* new_predicate_proj;
 263   if (loop_phase != NULL) {
 264     new_predicate_proj = loop_phase->create_new_if_for_predicate(predicate_proj, new_entry, reason, Opcodes::Op_If);
 265   } else {
 266     new_predicate_proj =       igvn->create_new_if_for_predicate(predicate_proj, new_entry, reason, Opcodes::Op_If);
 267   }
 268   IfNode* iff = new_predicate_proj->in(0)->as_If();
 269   Node* ctrl  = iff->in(0);
 270 
 271   // Match original condition since predicate's projections could be swapped.
 272   assert(predicate_proj->in(0)->in(1)->in(1)->Opcode()==Opcodes::Op_Opaque1, "must be");
 273   Node* opq = new Opaque1Node(igvn->C, predicate_proj->in(0)->in(1)->in(1)->in(1));
 274   igvn->C->add_predicate_opaq(opq);
 275 
 276   Node* bol = new Conv2BNode(opq);
 277   if (loop_phase != NULL) {
 278     loop_phase->register_new_node(opq, ctrl);
 279     loop_phase->register_new_node(bol, ctrl);
 280   } else {
 281     igvn->register_new_node_with_optimizer(opq);
 282     igvn->register_new_node_with_optimizer(bol);
 283   }
 284   igvn->hash_delete(iff);
 285   iff->set_req(1, bol);
 286   return new_predicate_proj;
 287 }
 288 
 289 
 290 //--------------------------clone_loop_predicates-----------------------
 291 // Interface from IGVN
 292 Node* PhaseIterGVN::clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check) {


 566 
 567 //------------------------------is_range_check_if -----------------------------------
 568 // Returns true if the predicate of iff is in "scale*iv + offset u< load_range(ptr)" format
 569 // Note: this function is particularly designed for loop predication. We require load_range
 570 //       and offset to be loop invariant computed on the fly by "invar"
 571 bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const {
 572   if (!is_loop_exit(iff)) {
 573     return false;
 574   }
 575   if (!iff->in(1)->is_Bool()) {
 576     return false;
 577   }
 578   const BoolNode *bol = iff->in(1)->as_Bool();
 579   if (bol->_test._test != BoolTest::lt) {
 580     return false;
 581   }
 582   if (!bol->in(1)->is_Cmp()) {
 583     return false;
 584   }
 585   const CmpNode *cmp = bol->in(1)->as_Cmp();
 586   if (cmp->Opcode() != Opcodes::Op_CmpU) {
 587     return false;
 588   }
 589   Node* range = cmp->in(2);
 590   if (range->Opcode() != Opcodes::Op_LoadRange && !iff->is_RangeCheck()) {
 591     const TypeInt* tint = phase->_igvn.type(range)->isa_int();
 592     if (tint == NULL || tint->empty() || tint->_lo < 0) {
 593       // Allow predication on positive values that aren't LoadRanges.
 594       // This allows optimization of loops where the length of the
 595       // array is a known value and doesn't need to be loaded back
 596       // from the array.
 597       return false;
 598     }
 599   }
 600   if (!invar.is_invariant(range)) {
 601     return false;
 602   }
 603   Node *iv     = _head->as_CountedLoop()->phi();
 604   int   scale  = 0;
 605   Node *offset = NULL;
 606   if (!phase->is_scaled_iv_plus_offset(cmp->in(1), iv, &scale, &offset)) {
 607     return false;
 608   }
 609   if (offset && !invar.is_invariant(offset)) { // offset must be invariant
 610     return false;


 673   register_new_node(bol, ctrl);
 674 
 675   if (TraceLoopPredicate) {
 676     predString->print_cr("<u range");
 677     tty->print("%s", predString->as_string());
 678   }
 679   return bol;
 680 }
 681 
 682 //------------------------------ loop_predication_impl--------------------------
 683 // Insert loop predicates for null checks and range checks
 684 bool PhaseIdealLoop::loop_predication_impl(IdealLoopTree *loop) {
 685   if (!UseLoopPredicate) return false;
 686 
 687   if (!loop->_head->is_Loop()) {
 688     // Could be a simple region when irreducible loops are present.
 689     return false;
 690   }
 691   LoopNode* head = loop->_head->as_Loop();
 692 
 693   if (head->unique_ctrl_out()->Opcode() == Opcodes::Op_NeverBranch) {
 694     // do nothing for infinite loops
 695     return false;
 696   }
 697 
 698   CountedLoopNode *cl = NULL;
 699   if (head->is_valid_counted_loop()) {
 700     cl = head->as_CountedLoop();
 701     // do nothing for iteration-splitted loops
 702     if (!cl->is_normal_loop()) return false;
 703     // Avoid RCE if Counted loop's test is '!='.
 704     BoolTest::mask bt = cl->loopexit()->test_trip();
 705     if (bt != BoolTest::lt && bt != BoolTest::gt)
 706       cl = NULL;
 707   }
 708 
 709   Node* entry = head->in(LoopNode::EntryControl);
 710   ProjNode *predicate_proj = NULL;
 711   // Loop limit check predicate should be near the loop.
 712   predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
 713   if (predicate_proj != NULL)


 719       tty->print("missing predicate:");
 720       loop->dump_head();
 721       head->dump(1);
 722     }
 723 #endif
 724     return false;
 725   }
 726   ConNode* zero = _igvn.intcon(0);
 727   set_ctrl(zero, C->root());
 728 
 729   ResourceArea *area = Thread::current()->resource_area();
 730   Invariance invar(area, loop);
 731 
 732   // Create list of if-projs such that a newer proj dominates all older
 733   // projs in the list, and they all dominate loop->tail()
 734   Node_List if_proj_list(area);
 735   Node *current_proj = loop->tail(); //start from tail
 736   while (current_proj != head) {
 737     if (loop == get_loop(current_proj) && // still in the loop ?
 738         current_proj->is_Proj()        && // is a projection  ?
 739         (current_proj->in(0)->Opcode() == Opcodes::Op_If ||
 740          current_proj->in(0)->Opcode() == Opcodes::Op_RangeCheck)) { // is a if projection ?
 741       if_proj_list.push(current_proj);
 742     }
 743     current_proj = idom(current_proj);
 744   }
 745 
 746   bool hoisted = false; // true if at least one proj is promoted
 747   while (if_proj_list.size() > 0) {
 748     // Following are changed to nonnull when a predicate can be hoisted
 749     ProjNode* new_predicate_proj = NULL;
 750 
 751     ProjNode* proj = if_proj_list.pop()->as_Proj();
 752     IfNode*   iff  = proj->in(0)->as_If();
 753 
 754     if (!proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none)) {
 755       if (loop->is_loop_exit(iff)) {
 756         // stop processing the remaining projs in the list because the execution of them
 757         // depends on the condition of "iff" (iff->in(1)).
 758         break;
 759       } else {
 760         // Both arms are inside the loop. There are two cases:


 789         negated = true;
 790       }
 791       IfNode* new_predicate_iff = new_predicate_proj->in(0)->as_If();
 792       _igvn.hash_delete(new_predicate_iff);
 793       new_predicate_iff->set_req(1, new_predicate_bol);
 794 #ifndef PRODUCT
 795       if (TraceLoopPredicate) {
 796         tty->print("Predicate invariant if%s: %d ", negated ? " negated" : "", new_predicate_iff->_idx);
 797         loop->dump_head();
 798       } else if (TraceLoopOpts) {
 799         tty->print("Predicate IC ");
 800         loop->dump_head();
 801       }
 802 #endif
 803     } else if (cl != NULL && loop->is_range_check_if(iff, this, invar)) {
 804       // Range check for counted loops
 805       const Node*    cmp    = bol->in(1)->as_Cmp();
 806       Node*          idx    = cmp->in(1);
 807       assert(!invar.is_invariant(idx), "index is variant");
 808       Node* rng = cmp->in(2);
 809       assert(rng->Opcode() == Opcodes::Op_LoadRange || _igvn.type(rng)->is_int() >= 0, "must be");
 810       assert(invar.is_invariant(rng), "range must be invariant");
 811       int scale    = 1;
 812       Node* offset = zero;
 813       bool ok = is_scaled_iv_plus_offset(idx, cl->phi(), &scale, &offset);
 814       assert(ok, "must be index expression");
 815 
 816       Node* init    = cl->init_trip();
 817       Node* limit   = cl->limit();
 818       Node* stride  = cl->stride();
 819 
 820       // Build if's for the upper and lower bound tests.  The
 821       // lower_bound test will dominate the upper bound test and all
 822       // cloned or created nodes will use the lower bound test as
 823       // their declared control.
 824       ProjNode* lower_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate, iff->Opcode());
 825       ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate, iff->Opcode());
 826       assert(upper_bound_proj->in(0)->as_If()->in(0) == lower_bound_proj, "should dominate");
 827       Node *ctrl = lower_bound_proj->in(0)->as_If()->in(0);
 828 
 829       // Perform cloning to keep Invariance state correct since the


< prev index next >