Thu, 24 Mar 2011 23:04:36 -0700
Merge
src/share/vm/opto/library_call.cpp | file | annotate | diff | comparison | revisions | |
src/share/vm/opto/parse.hpp | file | annotate | diff | comparison | revisions | |
src/share/vm/opto/stringopts.cpp | file | annotate | diff | comparison | revisions | |
test/compiler/6987555/Test6987555.java | file | annotate | diff | comparison | revisions | |
test/compiler/6991596/Test6991596.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/cpu/sparc/vm/methodHandles_sparc.cpp Thu Mar 24 23:00:27 2011 -0700 1.2 +++ b/src/cpu/sparc/vm/methodHandles_sparc.cpp Thu Mar 24 23:04:36 2011 -0700 1.3 @@ -775,9 +775,13 @@ 1.4 switch (ek) { 1.5 case _adapter_opt_i2l: 1.6 { 1.7 - __ ldsw(arg_lsw, O2_scratch); // Load LSW 1.8 - NOT_LP64(__ srlx(O2_scratch, BitsPerInt, O3_scratch)); // Move high bits to lower bits for std 1.9 - __ st_long(O2_scratch, arg_msw); // Uses O2/O3 on !_LP64 1.10 +#ifdef _LP64 1.11 + __ ldsw(arg_lsw, O2_scratch); // Load LSW sign-extended 1.12 +#else 1.13 + __ ldsw(arg_lsw, O3_scratch); // Load LSW sign-extended 1.14 + __ srlx(O3_scratch, BitsPerInt, O2_scratch); // Move MSW value to lower 32-bits for std 1.15 +#endif 1.16 + __ st_long(O2_scratch, arg_msw); // Uses O2/O3 on !_LP64 1.17 } 1.18 break; 1.19 case _adapter_opt_unboxl:
2.1 --- a/src/share/vm/opto/c2_globals.hpp Thu Mar 24 23:00:27 2011 -0700 2.2 +++ b/src/share/vm/opto/c2_globals.hpp Thu Mar 24 23:04:36 2011 -0700 2.3 @@ -180,6 +180,9 @@ 2.4 develop(bool, TraceLoopPredicate, false, \ 2.5 "Trace generation of loop predicates") \ 2.6 \ 2.7 + develop(bool, TraceLoopOpts, false, \ 2.8 + "Trace executed loop optimizations") \ 2.9 + \ 2.10 product(bool, OptimizeFill, false, \ 2.11 "convert fill/copy loops into intrinsic") \ 2.12 \
3.1 --- a/src/share/vm/opto/graphKit.cpp Thu Mar 24 23:00:27 2011 -0700 3.2 +++ b/src/share/vm/opto/graphKit.cpp Thu Mar 24 23:04:36 2011 -0700 3.3 @@ -3338,6 +3338,49 @@ 3.4 return NULL; 3.5 } 3.6 3.7 +//----------------------------- loop predicates --------------------------- 3.8 + 3.9 +//------------------------------add_predicate_impl---------------------------- 3.10 +void GraphKit::add_predicate_impl(Deoptimization::DeoptReason reason, int nargs) { 3.11 + // Too many traps seen? 3.12 + if (too_many_traps(reason)) { 3.13 +#ifdef ASSERT 3.14 + if (TraceLoopPredicate) { 3.15 + int tc = C->trap_count(reason); 3.16 + tty->print("too many traps=%s tcount=%d in ", 3.17 + Deoptimization::trap_reason_name(reason), tc); 3.18 + method()->print(); // which method has too many predicate traps 3.19 + tty->cr(); 3.20 + } 3.21 +#endif 3.22 + // We cannot afford to take more traps here, 3.23 + // do not generate predicate. 3.24 + return; 3.25 + } 3.26 + 3.27 + Node *cont = _gvn.intcon(1); 3.28 + Node* opq = _gvn.transform(new (C, 2) Opaque1Node(C, cont)); 3.29 + Node *bol = _gvn.transform(new (C, 2) Conv2BNode(opq)); 3.30 + IfNode* iff = create_and_map_if(control(), bol, PROB_MAX, COUNT_UNKNOWN); 3.31 + Node* iffalse = _gvn.transform(new (C, 1) IfFalseNode(iff)); 3.32 + C->add_predicate_opaq(opq); 3.33 + { 3.34 + PreserveJVMState pjvms(this); 3.35 + set_control(iffalse); 3.36 + _sp += nargs; 3.37 + uncommon_trap(reason, Deoptimization::Action_maybe_recompile); 3.38 + } 3.39 + Node* iftrue = _gvn.transform(new (C, 1) IfTrueNode(iff)); 3.40 + set_control(iftrue); 3.41 +} 3.42 + 3.43 +//------------------------------add_predicate--------------------------------- 3.44 +void GraphKit::add_predicate(int nargs) { 3.45 + if (UseLoopPredicate) { 3.46 + add_predicate_impl(Deoptimization::Reason_predicate, nargs); 3.47 + } 3.48 +} 3.49 + 3.50 //----------------------------- store barriers ---------------------------- 3.51 #define __ ideal. 3.52
4.1 --- a/src/share/vm/opto/graphKit.hpp Thu Mar 24 23:00:27 2011 -0700 4.2 +++ b/src/share/vm/opto/graphKit.hpp Thu Mar 24 23:04:36 2011 -0700 4.3 @@ -793,6 +793,10 @@ 4.4 if (!tst->is_Con()) record_for_igvn(iff); // Range-check and Null-check removal is later 4.5 return iff; 4.6 } 4.7 + 4.8 + // Insert a loop predicate into the graph 4.9 + void add_predicate(int nargs = 0); 4.10 + void add_predicate_impl(Deoptimization::DeoptReason reason, int nargs); 4.11 }; 4.12 4.13 // Helper class to support building of control flow branches. Upon
5.1 --- a/src/share/vm/opto/idealKit.cpp Thu Mar 24 23:00:27 2011 -0700 5.2 +++ b/src/share/vm/opto/idealKit.cpp Thu Mar 24 23:04:36 2011 -0700 5.3 @@ -154,8 +154,18 @@ 5.4 // 5.5 // Pushes the loop top cvstate first, then the else (loop exit) cvstate 5.6 // onto the stack. 5.7 -void IdealKit::loop(IdealVariable& iv, Node* init, BoolTest::mask relop, Node* limit, float prob, float cnt) { 5.8 +void IdealKit::loop(GraphKit* gkit, int nargs, IdealVariable& iv, Node* init, BoolTest::mask relop, Node* limit, float prob, float cnt) { 5.9 assert((state() & (BlockS|LoopS|IfThenS|ElseS)), "bad state for new loop"); 5.10 + 5.11 + // Sync IdealKit and graphKit. 5.12 + gkit->set_all_memory(this->merged_memory()); 5.13 + gkit->set_control(this->ctrl()); 5.14 + // Add loop predicate. 5.15 + gkit->add_predicate(nargs); 5.16 + // Update IdealKit memory. 5.17 + this->set_all_memory(gkit->merged_memory()); 5.18 + this->set_ctrl(gkit->control()); 5.19 + 5.20 set(iv, init); 5.21 Node* head = make_label(1); 5.22 bind(head);
6.1 --- a/src/share/vm/opto/idealKit.hpp Thu Mar 24 23:00:27 2011 -0700 6.2 +++ b/src/share/vm/opto/idealKit.hpp Thu Mar 24 23:04:36 2011 -0700 6.3 @@ -29,6 +29,7 @@ 6.4 #include "opto/cfgnode.hpp" 6.5 #include "opto/connode.hpp" 6.6 #include "opto/divnode.hpp" 6.7 +#include "opto/graphKit.hpp" 6.8 #include "opto/mulnode.hpp" 6.9 #include "opto/phaseX.hpp" 6.10 #include "opto/subnode.hpp" 6.11 @@ -160,7 +161,7 @@ 6.12 bool push_new_state = true); 6.13 void else_(); 6.14 void end_if(); 6.15 - void loop(IdealVariable& iv, Node* init, BoolTest::mask cmp, Node* limit, 6.16 + void loop(GraphKit* gkit, int nargs, IdealVariable& iv, Node* init, BoolTest::mask cmp, Node* limit, 6.17 float prob = PROB_LIKELY(0.9), float cnt = COUNT_UNKNOWN); 6.18 void end_loop(); 6.19 Node* make_label(int goto_ct);
7.1 --- a/src/share/vm/opto/library_call.cpp Thu Mar 24 23:00:27 2011 -0700 7.2 +++ b/src/share/vm/opto/library_call.cpp Thu Mar 24 23:04:36 2011 -0700 7.3 @@ -1101,6 +1101,8 @@ 7.4 float likely = PROB_LIKELY(0.9); 7.5 float unlikely = PROB_UNLIKELY(0.9); 7.6 7.7 + const int nargs = 2; // number of arguments to push back for uncommon trap in predicate 7.8 + 7.9 const int value_offset = java_lang_String::value_offset_in_bytes(); 7.10 const int count_offset = java_lang_String::count_offset_in_bytes(); 7.11 const int offset_offset = java_lang_String::offset_offset_in_bytes(); 7.12 @@ -1138,12 +1140,12 @@ 7.13 Node* return_ = __ make_label(1); 7.14 7.15 __ set(rtn,__ ConI(-1)); 7.16 - __ loop(i, sourceOffset, BoolTest::lt, sourceEnd); { 7.17 + __ loop(this, nargs, i, sourceOffset, BoolTest::lt, sourceEnd); { 7.18 Node* i2 = __ AddI(__ value(i), targetCountLess1); 7.19 // pin to prohibit loading of "next iteration" value which may SEGV (rare) 7.20 Node* src = load_array_element(__ ctrl(), source, i2, TypeAryPtr::CHARS); 7.21 __ if_then(src, BoolTest::eq, lastChar, unlikely); { 7.22 - __ loop(j, zero, BoolTest::lt, targetCountLess1); { 7.23 + __ loop(this, nargs, j, zero, BoolTest::lt, targetCountLess1); { 7.24 Node* tpj = __ AddI(targetOffset, __ value(j)); 7.25 Node* targ = load_array_element(no_ctrl, target, tpj, target_type); 7.26 Node* ipj = __ AddI(__ value(i), __ value(j));
8.1 --- a/src/share/vm/opto/loopTransform.cpp Thu Mar 24 23:00:27 2011 -0700 8.2 +++ b/src/share/vm/opto/loopTransform.cpp Thu Mar 24 23:04:36 2011 -0700 8.3 @@ -205,6 +205,8 @@ 8.4 } 8.5 phase->register_new_node(addx, phase->get_ctrl(x)); 8.6 phase->_igvn.replace_node(n1, addx); 8.7 + assert(phase->get_loop(phase->get_ctrl(n1)) == this, ""); 8.8 + _body.yank(n1); 8.9 return addx; 8.10 } 8.11 8.12 @@ -307,15 +309,21 @@ 8.13 // iterations adjusted. Therefore, we need to declare this loop as 8.14 // no longer a 'main' loop; it will need new pre and post loops before 8.15 // we can do further RCE. 8.16 +#ifndef PRODUCT 8.17 + if (TraceLoopOpts) { 8.18 + tty->print("Peel "); 8.19 + loop->dump_head(); 8.20 + } 8.21 +#endif 8.22 Node *h = loop->_head; 8.23 - if( h->is_CountedLoop() ) { 8.24 + if (h->is_CountedLoop()) { 8.25 CountedLoopNode *cl = h->as_CountedLoop(); 8.26 assert(cl->trip_count() > 0, "peeling a fully unrolled loop"); 8.27 cl->set_trip_count(cl->trip_count() - 1); 8.28 - if( cl->is_main_loop() ) { 8.29 + if (cl->is_main_loop()) { 8.30 cl->set_normal_loop(); 8.31 #ifndef PRODUCT 8.32 - if( PrintOpto && VerifyLoopOptimizations ) { 8.33 + if (PrintOpto && VerifyLoopOptimizations) { 8.34 tty->print("Peeling a 'main' loop; resetting to 'normal' "); 8.35 loop->dump_head(); 8.36 } 8.37 @@ -645,6 +653,15 @@ 8.38 // alignment. Useful to unroll loops that do no array accesses. 8.39 void PhaseIdealLoop::insert_pre_post_loops( IdealLoopTree *loop, Node_List &old_new, bool peel_only ) { 8.40 8.41 +#ifndef PRODUCT 8.42 + if (TraceLoopOpts) { 8.43 + if (peel_only) 8.44 + tty->print("PeelMainPost "); 8.45 + else 8.46 + tty->print("PreMainPost "); 8.47 + loop->dump_head(); 8.48 + } 8.49 +#endif 8.50 C->set_major_progress(); 8.51 8.52 // Find common pieces of the loop being guarded with pre & post loops 8.53 @@ -897,16 +914,19 @@ 8.54 //------------------------------do_unroll-------------------------------------- 8.55 // Unroll the loop body one step - make each trip do 2 iterations. 8.56 void PhaseIdealLoop::do_unroll( IdealLoopTree *loop, Node_List &old_new, bool adjust_min_trip ) { 8.57 - assert( LoopUnrollLimit, "" ); 8.58 + assert(LoopUnrollLimit, ""); 8.59 + CountedLoopNode *loop_head = loop->_head->as_CountedLoop(); 8.60 + CountedLoopEndNode *loop_end = loop_head->loopexit(); 8.61 + assert(loop_end, ""); 8.62 #ifndef PRODUCT 8.63 - if( PrintOpto && VerifyLoopOptimizations ) { 8.64 + if (PrintOpto && VerifyLoopOptimizations) { 8.65 tty->print("Unrolling "); 8.66 loop->dump_head(); 8.67 + } else if (TraceLoopOpts) { 8.68 + tty->print("Unroll %d ", loop_head->unrolled_count()*2); 8.69 + loop->dump_head(); 8.70 } 8.71 #endif 8.72 - CountedLoopNode *loop_head = loop->_head->as_CountedLoop(); 8.73 - CountedLoopEndNode *loop_end = loop_head->loopexit(); 8.74 - assert( loop_end, "" ); 8.75 8.76 // Remember loop node count before unrolling to detect 8.77 // if rounds of unroll,optimize are making progress 8.78 @@ -915,7 +935,7 @@ 8.79 Node *ctrl = loop_head->in(LoopNode::EntryControl); 8.80 Node *limit = loop_head->limit(); 8.81 Node *init = loop_head->init_trip(); 8.82 - Node *strid = loop_head->stride(); 8.83 + Node *stride = loop_head->stride(); 8.84 8.85 Node *opaq = NULL; 8.86 if( adjust_min_trip ) { // If not maximally unrolling, need adjustment 8.87 @@ -955,13 +975,13 @@ 8.88 // odd iteration: (trip_cnt & ~1). Then back compute a new limit. 8.89 Node *span = new (C, 3) SubINode( limit, init ); 8.90 register_new_node( span, ctrl ); 8.91 - Node *trip = new (C, 3) DivINode( 0, span, strid ); 8.92 + Node *trip = new (C, 3) DivINode( 0, span, stride ); 8.93 register_new_node( trip, ctrl ); 8.94 Node *mtwo = _igvn.intcon(-2); 8.95 set_ctrl(mtwo, C->root()); 8.96 Node *rond = new (C, 3) AndINode( trip, mtwo ); 8.97 register_new_node( rond, ctrl ); 8.98 - Node *spn2 = new (C, 3) MulINode( rond, strid ); 8.99 + Node *spn2 = new (C, 3) MulINode( rond, stride ); 8.100 register_new_node( spn2, ctrl ); 8.101 Node *lim2 = new (C, 3) AddINode( spn2, init ); 8.102 register_new_node( lim2, ctrl ); 8.103 @@ -1040,17 +1060,23 @@ 8.104 8.105 void PhaseIdealLoop::do_maximally_unroll( IdealLoopTree *loop, Node_List &old_new ) { 8.106 CountedLoopNode *cl = loop->_head->as_CountedLoop(); 8.107 - assert( cl->trip_count() > 0, ""); 8.108 + assert(cl->trip_count() > 0, ""); 8.109 +#ifndef PRODUCT 8.110 + if (TraceLoopOpts) { 8.111 + tty->print("MaxUnroll %d ", cl->trip_count()); 8.112 + loop->dump_head(); 8.113 + } 8.114 +#endif 8.115 8.116 // If loop is tripping an odd number of times, peel odd iteration 8.117 - if( (cl->trip_count() & 1) == 1 ) { 8.118 - do_peeling( loop, old_new ); 8.119 + if ((cl->trip_count() & 1) == 1) { 8.120 + do_peeling(loop, old_new); 8.121 } 8.122 8.123 // Now its tripping an even number of times remaining. Double loop body. 8.124 // Do not adjust pre-guards; they are not needed and do not exist. 8.125 - if( cl->trip_count() > 0 ) { 8.126 - do_unroll( loop, old_new, false ); 8.127 + if (cl->trip_count() > 0) { 8.128 + do_unroll(loop, old_new, false); 8.129 } 8.130 } 8.131 8.132 @@ -1227,35 +1253,55 @@ 8.133 // Eliminate range-checks and other trip-counter vs loop-invariant tests. 8.134 void PhaseIdealLoop::do_range_check( IdealLoopTree *loop, Node_List &old_new ) { 8.135 #ifndef PRODUCT 8.136 - if( PrintOpto && VerifyLoopOptimizations ) { 8.137 + if (PrintOpto && VerifyLoopOptimizations) { 8.138 tty->print("Range Check Elimination "); 8.139 loop->dump_head(); 8.140 + } else if (TraceLoopOpts) { 8.141 + tty->print("RangeCheck "); 8.142 + loop->dump_head(); 8.143 } 8.144 #endif 8.145 - assert( RangeCheckElimination, "" ); 8.146 + assert(RangeCheckElimination, ""); 8.147 CountedLoopNode *cl = loop->_head->as_CountedLoop(); 8.148 - assert( cl->is_main_loop(), "" ); 8.149 + assert(cl->is_main_loop(), ""); 8.150 + 8.151 + // protect against stride not being a constant 8.152 + if (!cl->stride_is_con()) 8.153 + return; 8.154 8.155 // Find the trip counter; we are iteration splitting based on it 8.156 Node *trip_counter = cl->phi(); 8.157 // Find the main loop limit; we will trim it's iterations 8.158 // to not ever trip end tests 8.159 Node *main_limit = cl->limit(); 8.160 + 8.161 + // Need to find the main-loop zero-trip guard 8.162 + Node *ctrl = cl->in(LoopNode::EntryControl); 8.163 + assert(ctrl->Opcode() == Op_IfTrue || ctrl->Opcode() == Op_IfFalse, ""); 8.164 + Node *iffm = ctrl->in(0); 8.165 + assert(iffm->Opcode() == Op_If, ""); 8.166 + Node *bolzm = iffm->in(1); 8.167 + assert(bolzm->Opcode() == Op_Bool, ""); 8.168 + Node *cmpzm = bolzm->in(1); 8.169 + assert(cmpzm->is_Cmp(), ""); 8.170 + Node *opqzm = cmpzm->in(2); 8.171 + // Can not optimize a loop if pre-loop Opaque1 node is optimized 8.172 + // away and then another round of loop opts attempted. 8.173 + if (opqzm->Opcode() != Op_Opaque1) 8.174 + return; 8.175 + assert(opqzm->in(1) == main_limit, "do not understand situation"); 8.176 + 8.177 // Find the pre-loop limit; we will expand it's iterations to 8.178 // not ever trip low tests. 8.179 - Node *ctrl = cl->in(LoopNode::EntryControl); 8.180 - assert( ctrl->Opcode() == Op_IfTrue || ctrl->Opcode() == Op_IfFalse, "" ); 8.181 - Node *iffm = ctrl->in(0); 8.182 - assert( iffm->Opcode() == Op_If, "" ); 8.183 Node *p_f = iffm->in(0); 8.184 - assert( p_f->Opcode() == Op_IfFalse, "" ); 8.185 + assert(p_f->Opcode() == Op_IfFalse, ""); 8.186 CountedLoopEndNode *pre_end = p_f->in(0)->as_CountedLoopEnd(); 8.187 - assert( pre_end->loopnode()->is_pre_loop(), "" ); 8.188 + assert(pre_end->loopnode()->is_pre_loop(), ""); 8.189 Node *pre_opaq1 = pre_end->limit(); 8.190 // Occasionally it's possible for a pre-loop Opaque1 node to be 8.191 // optimized away and then another round of loop opts attempted. 8.192 // We can not optimize this particular loop in that case. 8.193 - if( pre_opaq1->Opcode() != Op_Opaque1 ) 8.194 + if (pre_opaq1->Opcode() != Op_Opaque1) 8.195 return; 8.196 Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1; 8.197 Node *pre_limit = pre_opaq->in(1); 8.198 @@ -1266,25 +1312,11 @@ 8.199 // Ensure the original loop limit is available from the 8.200 // pre-loop Opaque1 node. 8.201 Node *orig_limit = pre_opaq->original_loop_limit(); 8.202 - if( orig_limit == NULL || _igvn.type(orig_limit) == Type::TOP ) 8.203 + if (orig_limit == NULL || _igvn.type(orig_limit) == Type::TOP) 8.204 return; 8.205 8.206 - // Need to find the main-loop zero-trip guard 8.207 - Node *bolzm = iffm->in(1); 8.208 - assert( bolzm->Opcode() == Op_Bool, "" ); 8.209 - Node *cmpzm = bolzm->in(1); 8.210 - assert( cmpzm->is_Cmp(), "" ); 8.211 - Node *opqzm = cmpzm->in(2); 8.212 - if( opqzm->Opcode() != Op_Opaque1 ) 8.213 - return; 8.214 - assert( opqzm->in(1) == main_limit, "do not understand situation" ); 8.215 - 8.216 // Must know if its a count-up or count-down loop 8.217 8.218 - // protect against stride not being a constant 8.219 - if ( !cl->stride_is_con() ) { 8.220 - return; 8.221 - } 8.222 int stride_con = cl->stride_con(); 8.223 Node *zero = _igvn.intcon(0); 8.224 Node *one = _igvn.intcon(1); 8.225 @@ -1566,16 +1598,24 @@ 8.226 // have on the last iteration. This will break the loop. 8.227 bool IdealLoopTree::policy_do_remove_empty_loop( PhaseIdealLoop *phase ) { 8.228 // Minimum size must be empty loop 8.229 - if( _body.size() > 7/*number of nodes in an empty loop*/ ) return false; 8.230 + if (_body.size() > 7/*number of nodes in an empty loop*/) 8.231 + return false; 8.232 8.233 - if( !_head->is_CountedLoop() ) return false; // Dead loop 8.234 + if (!_head->is_CountedLoop()) 8.235 + return false; // Dead loop 8.236 CountedLoopNode *cl = _head->as_CountedLoop(); 8.237 - if( !cl->loopexit() ) return false; // Malformed loop 8.238 - if( !phase->is_member(this,phase->get_ctrl(cl->loopexit()->in(CountedLoopEndNode::TestValue)) ) ) 8.239 + if (!cl->loopexit()) 8.240 + return false; // Malformed loop 8.241 + if (!phase->is_member(this, phase->get_ctrl(cl->loopexit()->in(CountedLoopEndNode::TestValue)))) 8.242 return false; // Infinite loop 8.243 #ifndef PRODUCT 8.244 - if( PrintOpto ) 8.245 - tty->print_cr("Removing empty loop"); 8.246 + if (PrintOpto) { 8.247 + tty->print("Removing empty loop"); 8.248 + this->dump_head(); 8.249 + } else if (TraceLoopOpts) { 8.250 + tty->print("Empty "); 8.251 + this->dump_head(); 8.252 + } 8.253 #endif 8.254 #ifdef ASSERT 8.255 // Ensure only one phi which is the iv. 8.256 @@ -1720,7 +1760,7 @@ 8.257 //------------------------------iteration_split-------------------------------- 8.258 bool IdealLoopTree::iteration_split( PhaseIdealLoop *phase, Node_List &old_new ) { 8.259 // Recursively iteration split nested loops 8.260 - if( _child && !_child->iteration_split( phase, old_new )) 8.261 + if (_child && !_child->iteration_split(phase, old_new)) 8.262 return false; 8.263 8.264 // Clean out prior deadwood 8.265 @@ -1729,21 +1769,20 @@ 8.266 8.267 // Look for loop-exit tests with my 50/50 guesses from the Parsing stage. 8.268 // Replace with a 1-in-10 exit guess. 8.269 - if( _parent /*not the root loop*/ && 8.270 + if (_parent /*not the root loop*/ && 8.271 !_irreducible && 8.272 // Also ignore the occasional dead backedge 8.273 - !tail()->is_top() ) { 8.274 + !tail()->is_top()) { 8.275 adjust_loop_exit_prob(phase); 8.276 } 8.277 8.278 - 8.279 // Gate unrolling, RCE and peeling efforts. 8.280 - if( !_child && // If not an inner loop, do not split 8.281 + if (!_child && // If not an inner loop, do not split 8.282 !_irreducible && 8.283 _allow_optimizations && 8.284 - !tail()->is_top() ) { // Also ignore the occasional dead backedge 8.285 + !tail()->is_top()) { // Also ignore the occasional dead backedge 8.286 if (!_has_call) { 8.287 - if (!iteration_split_impl( phase, old_new )) { 8.288 + if (!iteration_split_impl(phase, old_new)) { 8.289 return false; 8.290 } 8.291 } else if (policy_unswitching(phase)) { 8.292 @@ -1752,16 +1791,17 @@ 8.293 } 8.294 8.295 // Minor offset re-organization to remove loop-fallout uses of 8.296 - // trip counter. 8.297 - if( _head->is_CountedLoop() ) phase->reorg_offsets( this ); 8.298 - if( _next && !_next->iteration_split( phase, old_new )) 8.299 + // trip counter when there was no major reshaping. 8.300 + phase->reorg_offsets(this); 8.301 + 8.302 + if (_next && !_next->iteration_split(phase, old_new)) 8.303 return false; 8.304 return true; 8.305 } 8.306 8.307 //-------------------------------is_uncommon_trap_proj---------------------------- 8.308 // Return true if proj is the form of "proj->[region->..]call_uct" 8.309 -bool PhaseIdealLoop::is_uncommon_trap_proj(ProjNode* proj, bool must_reason_predicate) { 8.310 +bool PhaseIdealLoop::is_uncommon_trap_proj(ProjNode* proj, Deoptimization::DeoptReason reason) { 8.311 int path_limit = 10; 8.312 assert(proj, "invalid argument"); 8.313 Node* out = proj; 8.314 @@ -1772,8 +1812,8 @@ 8.315 if (out->is_CallStaticJava()) { 8.316 int req = out->as_CallStaticJava()->uncommon_trap_request(); 8.317 if (req != 0) { 8.318 - Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(req); 8.319 - if (!must_reason_predicate || reason == Deoptimization::Reason_predicate){ 8.320 + Deoptimization::DeoptReason trap_reason = Deoptimization::trap_request_reason(req); 8.321 + if (trap_reason == reason || reason == Deoptimization::Reason_none) { 8.322 return true; 8.323 } 8.324 } 8.325 @@ -1790,15 +1830,15 @@ 8.326 // other_proj->[region->..]call_uct" 8.327 // 8.328 // "must_reason_predicate" means the uct reason must be Reason_predicate 8.329 -bool PhaseIdealLoop::is_uncommon_trap_if_pattern(ProjNode *proj, bool must_reason_predicate) { 8.330 +bool PhaseIdealLoop::is_uncommon_trap_if_pattern(ProjNode *proj, Deoptimization::DeoptReason reason) { 8.331 Node *in0 = proj->in(0); 8.332 if (!in0->is_If()) return false; 8.333 // Variation of a dead If node. 8.334 if (in0->outcnt() < 2) return false; 8.335 IfNode* iff = in0->as_If(); 8.336 8.337 - // we need "If(Conv2B(Opaque1(...)))" pattern for must_reason_predicate 8.338 - if (must_reason_predicate) { 8.339 + // we need "If(Conv2B(Opaque1(...)))" pattern for reason_predicate 8.340 + if (reason != Deoptimization::Reason_none) { 8.341 if (iff->in(1)->Opcode() != Op_Conv2B || 8.342 iff->in(1)->in(1)->Opcode() != Op_Opaque1) { 8.343 return false; 8.344 @@ -1806,7 +1846,19 @@ 8.345 } 8.346 8.347 ProjNode* other_proj = iff->proj_out(1-proj->_con)->as_Proj(); 8.348 - return is_uncommon_trap_proj(other_proj, must_reason_predicate); 8.349 + return is_uncommon_trap_proj(other_proj, reason); 8.350 +} 8.351 + 8.352 +//-------------------------------register_control------------------------- 8.353 +void PhaseIdealLoop::register_control(Node* n, IdealLoopTree *loop, Node* pred) { 8.354 + assert(n->is_CFG(), "must be control node"); 8.355 + _igvn.register_new_node_with_optimizer(n); 8.356 + loop->_body.push(n); 8.357 + set_loop(n, loop); 8.358 + // When called from beautify_loops() idom is not constructed yet. 8.359 + if (_idom != NULL) { 8.360 + set_idom(n, pred, dom_depth(pred)); 8.361 + } 8.362 } 8.363 8.364 //------------------------------create_new_if_for_predicate------------------------ 8.365 @@ -1843,8 +1895,10 @@ 8.366 // 8.367 // We will create a region to guard the uct call if there is no one there. 8.368 // The true projecttion (if_cont) of the new_iff is returned. 8.369 -ProjNode* PhaseIdealLoop::create_new_if_for_predicate(ProjNode* cont_proj) { 8.370 - assert(is_uncommon_trap_if_pattern(cont_proj, true), "must be a uct if pattern!"); 8.371 +// This code is also used to clone predicates to clonned loops. 8.372 +ProjNode* PhaseIdealLoop::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry, 8.373 + Deoptimization::DeoptReason reason) { 8.374 + assert(is_uncommon_trap_if_pattern(cont_proj, reason), "must be a uct if pattern!"); 8.375 IfNode* iff = cont_proj->in(0)->as_If(); 8.376 8.377 ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con); 8.378 @@ -1854,57 +1908,84 @@ 8.379 if (!rgn->is_Region()) { // create a region to guard the call 8.380 assert(rgn->is_Call(), "must be call uct"); 8.381 CallNode* call = rgn->as_Call(); 8.382 + IdealLoopTree* loop = get_loop(call); 8.383 rgn = new (C, 1) RegionNode(1); 8.384 - _igvn.set_type(rgn, rgn->bottom_type()); 8.385 rgn->add_req(uncommon_proj); 8.386 - set_idom(rgn, idom(uncommon_proj), dom_depth(uncommon_proj)+1); 8.387 + register_control(rgn, loop, uncommon_proj); 8.388 _igvn.hash_delete(call); 8.389 call->set_req(0, rgn); 8.390 + // When called from beautify_loops() idom is not constructed yet. 8.391 + if (_idom != NULL) { 8.392 + set_idom(call, rgn, dom_depth(rgn)); 8.393 + } 8.394 } 8.395 8.396 + Node* entry = iff->in(0); 8.397 + if (new_entry != NULL) { 8.398 + // Clonning the predicate to new location. 8.399 + entry = new_entry; 8.400 + } 8.401 // Create new_iff 8.402 - uint iffdd = dom_depth(iff); 8.403 - IdealLoopTree* lp = get_loop(iff); 8.404 - IfNode *new_iff = new (C, 2) IfNode(iff->in(0), NULL, iff->_prob, iff->_fcnt); 8.405 - register_node(new_iff, lp, idom(iff), iffdd); 8.406 + IdealLoopTree* lp = get_loop(entry); 8.407 + IfNode *new_iff = new (C, 2) IfNode(entry, NULL, iff->_prob, iff->_fcnt); 8.408 + register_control(new_iff, lp, entry); 8.409 Node *if_cont = new (C, 1) IfTrueNode(new_iff); 8.410 Node *if_uct = new (C, 1) IfFalseNode(new_iff); 8.411 if (cont_proj->is_IfFalse()) { 8.412 // Swap 8.413 Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp; 8.414 } 8.415 - register_node(if_cont, lp, new_iff, iffdd); 8.416 - register_node(if_uct, get_loop(rgn), new_iff, iffdd); 8.417 - 8.418 - // if_cont to iff 8.419 - _igvn.hash_delete(iff); 8.420 - iff->set_req(0, if_cont); 8.421 - set_idom(iff, if_cont, dom_depth(iff)); 8.422 + register_control(if_cont, lp, new_iff); 8.423 + register_control(if_uct, get_loop(rgn), new_iff); 8.424 8.425 // if_uct to rgn 8.426 _igvn.hash_delete(rgn); 8.427 rgn->add_req(if_uct); 8.428 - Node* ridom = idom(rgn); 8.429 - Node* nrdom = dom_lca(ridom, new_iff); 8.430 - set_idom(rgn, nrdom, dom_depth(rgn)); 8.431 - 8.432 + // When called from beautify_loops() idom is not constructed yet. 8.433 + if (_idom != NULL) { 8.434 + Node* ridom = idom(rgn); 8.435 + Node* nrdom = dom_lca(ridom, new_iff); 8.436 + set_idom(rgn, nrdom, dom_depth(rgn)); 8.437 + } 8.438 // rgn must have no phis 8.439 assert(!rgn->as_Region()->has_phi(), "region must have no phis"); 8.440 8.441 + if (new_entry == NULL) { 8.442 + // Attach if_cont to iff 8.443 + _igvn.hash_delete(iff); 8.444 + iff->set_req(0, if_cont); 8.445 + if (_idom != NULL) { 8.446 + set_idom(iff, if_cont, dom_depth(iff)); 8.447 + } 8.448 + } 8.449 return if_cont->as_Proj(); 8.450 } 8.451 8.452 -//------------------------------find_predicate_insertion_point-------------------------- 8.453 +//--------------------------find_predicate_insertion_point------------------- 8.454 // Find a good location to insert a predicate 8.455 -ProjNode* PhaseIdealLoop::find_predicate_insertion_point(Node* start_c) { 8.456 - if (start_c == C->root() || !start_c->is_Proj()) 8.457 +ProjNode* PhaseIdealLoop::find_predicate_insertion_point(Node* start_c, Deoptimization::DeoptReason reason) { 8.458 + if (start_c == NULL || !start_c->is_Proj()) 8.459 return NULL; 8.460 - if (is_uncommon_trap_if_pattern(start_c->as_Proj(), true/*Reason_Predicate*/)) { 8.461 + if (is_uncommon_trap_if_pattern(start_c->as_Proj(), reason)) { 8.462 return start_c->as_Proj(); 8.463 } 8.464 return NULL; 8.465 } 8.466 8.467 +//--------------------------find_predicate------------------------------------ 8.468 +// Find a predicate 8.469 +Node* PhaseIdealLoop::find_predicate(Node* entry) { 8.470 + Node* predicate = NULL; 8.471 + if (UseLoopPredicate) { 8.472 + predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); 8.473 + if (predicate != NULL) { // right pattern that can be used by loop predication 8.474 + assert(entry->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be"); 8.475 + return entry; 8.476 + } 8.477 + } 8.478 + return NULL; 8.479 +} 8.480 + 8.481 //------------------------------Invariance----------------------------------- 8.482 // Helper class for loop_predication_impl to compute invariance on the fly and 8.483 // clone invariants. 8.484 @@ -2151,6 +2232,11 @@ 8.485 return false; 8.486 } 8.487 8.488 + if (loop->_head->unique_ctrl_out()->Opcode() == Op_NeverBranch) { 8.489 + // do nothing for infinite loops 8.490 + return false; 8.491 + } 8.492 + 8.493 CountedLoopNode *cl = NULL; 8.494 if (loop->_head->is_CountedLoop()) { 8.495 cl = loop->_head->as_CountedLoop(); 8.496 @@ -2158,40 +2244,22 @@ 8.497 if (!cl->is_normal_loop()) return false; 8.498 } 8.499 8.500 - // Too many traps seen? 8.501 - bool tmt = C->too_many_traps(C->method(), 0, Deoptimization::Reason_predicate); 8.502 - int tc = C->trap_count(Deoptimization::Reason_predicate); 8.503 - if (tmt || tc > 0) { 8.504 - if (TraceLoopPredicate) { 8.505 - tty->print_cr("too many predicate traps: %d", tc); 8.506 - C->method()->print(); // which method has too many predicate traps 8.507 - tty->print_cr(""); 8.508 - } 8.509 - return false; 8.510 - } 8.511 - 8.512 LoopNode *lpn = loop->_head->as_Loop(); 8.513 Node* entry = lpn->in(LoopNode::EntryControl); 8.514 8.515 - ProjNode *predicate_proj = find_predicate_insertion_point(entry); 8.516 - if (!predicate_proj){ 8.517 + ProjNode *predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); 8.518 + if (!predicate_proj) { 8.519 #ifndef PRODUCT 8.520 if (TraceLoopPredicate) { 8.521 tty->print("missing predicate:"); 8.522 loop->dump_head(); 8.523 + lpn->dump(1); 8.524 } 8.525 #endif 8.526 return false; 8.527 } 8.528 - 8.529 ConNode* zero = _igvn.intcon(0); 8.530 set_ctrl(zero, C->root()); 8.531 - Node *cond_false = new (C, 2) Conv2BNode(zero); 8.532 - register_new_node(cond_false, C->root()); 8.533 - ConNode* one = _igvn.intcon(1); 8.534 - set_ctrl(one, C->root()); 8.535 - Node *cond_true = new (C, 2) Conv2BNode(one); 8.536 - register_new_node(cond_true, C->root()); 8.537 8.538 ResourceArea *area = Thread::current()->resource_area(); 8.539 Invariance invar(area, loop); 8.540 @@ -2218,7 +2286,7 @@ 8.541 ProjNode* proj = if_proj_list.pop()->as_Proj(); 8.542 IfNode* iff = proj->in(0)->as_If(); 8.543 8.544 - if (!is_uncommon_trap_if_pattern(proj)) { 8.545 + if (!is_uncommon_trap_if_pattern(proj, Deoptimization::Reason_none)) { 8.546 if (loop->is_loop_exit(iff)) { 8.547 // stop processing the remaining projs in the list because the execution of them 8.548 // depends on the condition of "iff" (iff->in(1)). 8.549 @@ -2242,7 +2310,8 @@ 8.550 BoolNode* bol = test->as_Bool(); 8.551 if (invar.is_invariant(bol)) { 8.552 // Invariant test 8.553 - new_predicate_proj = create_new_if_for_predicate(predicate_proj); 8.554 + new_predicate_proj = create_new_if_for_predicate(predicate_proj, NULL, 8.555 + Deoptimization::Reason_predicate); 8.556 Node* ctrl = new_predicate_proj->in(0)->as_If()->in(0); 8.557 BoolNode* new_predicate_bol = invar.clone(bol, ctrl)->as_Bool(); 8.558 8.559 @@ -2256,8 +2325,15 @@ 8.560 IfNode* new_predicate_iff = new_predicate_proj->in(0)->as_If(); 8.561 _igvn.hash_delete(new_predicate_iff); 8.562 new_predicate_iff->set_req(1, new_predicate_bol); 8.563 - if (TraceLoopPredicate) tty->print_cr("invariant if%s: %d", negated ? " negated" : "", new_predicate_iff->_idx); 8.564 - 8.565 +#ifndef PRODUCT 8.566 + if (TraceLoopPredicate) { 8.567 + tty->print("Predicate invariant if%s: %d ", negated ? " negated" : "", new_predicate_iff->_idx); 8.568 + loop->dump_head(); 8.569 + } else if (TraceLoopOpts) { 8.570 + tty->print("Predicate IC "); 8.571 + loop->dump_head(); 8.572 + } 8.573 +#endif 8.574 } else if (cl != NULL && loop->is_range_check_if(iff, this, invar)) { 8.575 assert(proj->_con == predicate_proj->_con, "must match"); 8.576 8.577 @@ -2281,8 +2357,8 @@ 8.578 // lower_bound test will dominate the upper bound test and all 8.579 // cloned or created nodes will use the lower bound test as 8.580 // their declared control. 8.581 - ProjNode* lower_bound_proj = create_new_if_for_predicate(predicate_proj); 8.582 - ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj); 8.583 + ProjNode* lower_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate); 8.584 + ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate); 8.585 assert(upper_bound_proj->in(0)->as_If()->in(0) == lower_bound_proj, "should dominate"); 8.586 Node *ctrl = lower_bound_proj->in(0)->as_If()->in(0); 8.587 8.588 @@ -2311,41 +2387,24 @@ 8.589 // Fall through into rest of the clean up code which will move 8.590 // any dependent nodes onto the upper bound test. 8.591 new_predicate_proj = upper_bound_proj; 8.592 + 8.593 +#ifndef PRODUCT 8.594 + if (TraceLoopOpts && !TraceLoopPredicate) { 8.595 + tty->print("Predicate RC "); 8.596 + loop->dump_head(); 8.597 + } 8.598 +#endif 8.599 } else { 8.600 - // The other proj of the "iff" is a uncommon trap projection, and we can assume 8.601 - // the other proj will not be executed ("executed" means uct raised). 8.602 + // Loop variant check (for example, range check in non-counted loop) 8.603 + // with uncommon trap. 8.604 continue; 8.605 } 8.606 - 8.607 + assert(new_predicate_proj != NULL, "sanity"); 8.608 // Success - attach condition (new_predicate_bol) to predicate if 8.609 invar.map_ctrl(proj, new_predicate_proj); // so that invariance test can be appropriate 8.610 8.611 - // Eliminate the old if in the loop body 8.612 - _igvn.hash_delete(iff); 8.613 - iff->set_req(1, proj->is_IfFalse() ? cond_false : cond_true); 8.614 - 8.615 - Node* ctrl = new_predicate_proj; // new control 8.616 - ProjNode* dp = proj; // old control 8.617 - assert(get_loop(dp) == loop, "guaranteed at the time of collecting proj"); 8.618 - // Find nodes (depends only on the test) off the surviving projection; 8.619 - // move them outside the loop with the control of proj_clone 8.620 - for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) { 8.621 - Node* cd = dp->fast_out(i); // Control-dependent node 8.622 - if (cd->depends_only_on_test()) { 8.623 - assert(cd->in(0) == dp, ""); 8.624 - _igvn.hash_delete(cd); 8.625 - cd->set_req(0, ctrl); // ctrl, not NULL 8.626 - set_early_ctrl(cd); 8.627 - _igvn._worklist.push(cd); 8.628 - IdealLoopTree *new_loop = get_loop(get_ctrl(cd)); 8.629 - if (new_loop != loop) { 8.630 - if (!loop->_child) loop->_body.yank(cd); 8.631 - if (!new_loop->_child ) new_loop->_body.push(cd); 8.632 - } 8.633 - --i; 8.634 - --imax; 8.635 - } 8.636 - } 8.637 + // Eliminate the old If in the loop body 8.638 + dominated_by( new_predicate_proj, iff, proj->_con != new_predicate_proj->_con ); 8.639 8.640 hoisted = true; 8.641 C->set_major_progress();
9.1 --- a/src/share/vm/opto/loopUnswitch.cpp Thu Mar 24 23:00:27 2011 -0700 9.2 +++ b/src/share/vm/opto/loopUnswitch.cpp Thu Mar 24 23:04:36 2011 -0700 9.3 @@ -110,6 +110,13 @@ 9.4 IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop); 9.5 assert(unswitch_iff != NULL, "should be at least one"); 9.6 9.7 +#ifndef PRODUCT 9.8 + if (TraceLoopOpts) { 9.9 + tty->print("Unswitch %d ", head->unswitch_count()+1); 9.10 + loop->dump_head(); 9.11 + } 9.12 +#endif 9.13 + 9.14 // Need to revert back to normal loop 9.15 if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) { 9.16 head->as_CountedLoop()->set_normal_loop();
10.1 --- a/src/share/vm/opto/loopnode.cpp Thu Mar 24 23:00:27 2011 -0700 10.2 +++ b/src/share/vm/opto/loopnode.cpp Thu Mar 24 23:04:36 2011 -0700 10.3 @@ -56,12 +56,32 @@ 10.4 // Dump special per-node info 10.5 #ifndef PRODUCT 10.6 void LoopNode::dump_spec(outputStream *st) const { 10.7 - if( is_inner_loop () ) st->print( "inner " ); 10.8 - if( is_partial_peel_loop () ) st->print( "partial_peel " ); 10.9 - if( partial_peel_has_failed () ) st->print( "partial_peel_failed " ); 10.10 + if (is_inner_loop()) st->print( "inner " ); 10.11 + if (is_partial_peel_loop()) st->print( "partial_peel " ); 10.12 + if (partial_peel_has_failed()) st->print( "partial_peel_failed " ); 10.13 } 10.14 #endif 10.15 10.16 +//------------------------------is_valid_counted_loop------------------------- 10.17 +bool LoopNode::is_valid_counted_loop() const { 10.18 + if (is_CountedLoop()) { 10.19 + CountedLoopNode* l = as_CountedLoop(); 10.20 + CountedLoopEndNode* le = l->loopexit(); 10.21 + if (le != NULL && 10.22 + le->proj_out(1 /* true */) == l->in(LoopNode::LoopBackControl)) { 10.23 + Node* phi = l->phi(); 10.24 + Node* exit = le->proj_out(0 /* false */); 10.25 + if (exit != NULL && exit->Opcode() == Op_IfFalse && 10.26 + phi != NULL && phi->is_Phi() && 10.27 + phi->in(LoopNode::LoopBackControl) == l->incr() && 10.28 + le->loopnode() == l && le->stride_is_con()) { 10.29 + return true; 10.30 + } 10.31 + } 10.32 + } 10.33 + return false; 10.34 +} 10.35 + 10.36 //------------------------------get_early_ctrl--------------------------------- 10.37 // Compute earliest legal control 10.38 Node *PhaseIdealLoop::get_early_ctrl( Node *n ) { 10.39 @@ -142,43 +162,44 @@ 10.40 } 10.41 10.42 //------------------------------is_counted_loop-------------------------------- 10.43 -Node *PhaseIdealLoop::is_counted_loop( Node *x, IdealLoopTree *loop ) { 10.44 +bool PhaseIdealLoop::is_counted_loop( Node *x, IdealLoopTree *loop ) { 10.45 PhaseGVN *gvn = &_igvn; 10.46 10.47 // Counted loop head must be a good RegionNode with only 3 not NULL 10.48 // control input edges: Self, Entry, LoopBack. 10.49 - if ( x->in(LoopNode::Self) == NULL || x->req() != 3 ) 10.50 - return NULL; 10.51 + if (x->in(LoopNode::Self) == NULL || x->req() != 3) 10.52 + return false; 10.53 10.54 Node *init_control = x->in(LoopNode::EntryControl); 10.55 Node *back_control = x->in(LoopNode::LoopBackControl); 10.56 - if( init_control == NULL || back_control == NULL ) // Partially dead 10.57 - return NULL; 10.58 + if (init_control == NULL || back_control == NULL) // Partially dead 10.59 + return false; 10.60 // Must also check for TOP when looking for a dead loop 10.61 - if( init_control->is_top() || back_control->is_top() ) 10.62 - return NULL; 10.63 + if (init_control->is_top() || back_control->is_top()) 10.64 + return false; 10.65 10.66 // Allow funny placement of Safepoint 10.67 - if( back_control->Opcode() == Op_SafePoint ) 10.68 + if (back_control->Opcode() == Op_SafePoint) 10.69 back_control = back_control->in(TypeFunc::Control); 10.70 10.71 // Controlling test for loop 10.72 Node *iftrue = back_control; 10.73 uint iftrue_op = iftrue->Opcode(); 10.74 - if( iftrue_op != Op_IfTrue && 10.75 - iftrue_op != Op_IfFalse ) 10.76 + if (iftrue_op != Op_IfTrue && 10.77 + iftrue_op != Op_IfFalse) 10.78 // I have a weird back-control. Probably the loop-exit test is in 10.79 // the middle of the loop and I am looking at some trailing control-flow 10.80 // merge point. To fix this I would have to partially peel the loop. 10.81 - return NULL; // Obscure back-control 10.82 + return false; // Obscure back-control 10.83 10.84 // Get boolean guarding loop-back test 10.85 Node *iff = iftrue->in(0); 10.86 - if( get_loop(iff) != loop || !iff->in(1)->is_Bool() ) return NULL; 10.87 + if (get_loop(iff) != loop || !iff->in(1)->is_Bool()) 10.88 + return false; 10.89 BoolNode *test = iff->in(1)->as_Bool(); 10.90 BoolTest::mask bt = test->_test._test; 10.91 float cl_prob = iff->as_If()->_prob; 10.92 - if( iftrue_op == Op_IfFalse ) { 10.93 + if (iftrue_op == Op_IfFalse) { 10.94 bt = BoolTest(bt).negate(); 10.95 cl_prob = 1.0 - cl_prob; 10.96 } 10.97 @@ -186,7 +207,7 @@ 10.98 Node *cmp = test->in(1); 10.99 int cmp_op = cmp->Opcode(); 10.100 if( cmp_op != Op_CmpI ) 10.101 - return NULL; // Avoid pointer & float compares 10.102 + return false; // Avoid pointer & float compares 10.103 10.104 // Find the trip-counter increment & limit. Limit must be loop invariant. 10.105 Node *incr = cmp->in(1); 10.106 @@ -196,55 +217,64 @@ 10.107 // need 'loop()' test to tell if limit is loop invariant 10.108 // --------- 10.109 10.110 - if( !is_member( loop, get_ctrl(incr) ) ) { // Swapped trip counter and limit? 10.111 - Node *tmp = incr; // Then reverse order into the CmpI 10.112 + if (!is_member(loop, get_ctrl(incr))) { // Swapped trip counter and limit? 10.113 + Node *tmp = incr; // Then reverse order into the CmpI 10.114 incr = limit; 10.115 limit = tmp; 10.116 bt = BoolTest(bt).commute(); // And commute the exit test 10.117 } 10.118 - if( is_member( loop, get_ctrl(limit) ) ) // Limit must loop-invariant 10.119 - return NULL; 10.120 + if (is_member(loop, get_ctrl(limit))) // Limit must be loop-invariant 10.121 + return false; 10.122 + if (!is_member(loop, get_ctrl(incr))) // Trip counter must be loop-variant 10.123 + return false; 10.124 10.125 + Node* phi_incr = NULL; 10.126 // Trip-counter increment must be commutative & associative. 10.127 - uint incr_op = incr->Opcode(); 10.128 - if( incr_op == Op_Phi && incr->req() == 3 ) { 10.129 - incr = incr->in(2); // Assume incr is on backedge of Phi 10.130 - incr_op = incr->Opcode(); 10.131 + if (incr->is_Phi()) { 10.132 + if (incr->as_Phi()->region() != x || incr->req() != 3) 10.133 + return false; // Not simple trip counter expression 10.134 + phi_incr = incr; 10.135 + incr = phi_incr->in(LoopNode::LoopBackControl); // Assume incr is on backedge of Phi 10.136 + if (!is_member(loop, get_ctrl(incr))) // Trip counter must be loop-variant 10.137 + return false; 10.138 } 10.139 + 10.140 Node* trunc1 = NULL; 10.141 Node* trunc2 = NULL; 10.142 const TypeInt* iv_trunc_t = NULL; 10.143 if (!(incr = CountedLoopNode::match_incr_with_optional_truncation(incr, &trunc1, &trunc2, &iv_trunc_t))) { 10.144 - return NULL; // Funny increment opcode 10.145 + return false; // Funny increment opcode 10.146 } 10.147 + assert(incr->Opcode() == Op_AddI, "wrong increment code"); 10.148 10.149 // Get merge point 10.150 Node *xphi = incr->in(1); 10.151 Node *stride = incr->in(2); 10.152 - if( !stride->is_Con() ) { // Oops, swap these 10.153 - if( !xphi->is_Con() ) // Is the other guy a constant? 10.154 - return NULL; // Nope, unknown stride, bail out 10.155 + if (!stride->is_Con()) { // Oops, swap these 10.156 + if (!xphi->is_Con()) // Is the other guy a constant? 10.157 + return false; // Nope, unknown stride, bail out 10.158 Node *tmp = xphi; // 'incr' is commutative, so ok to swap 10.159 xphi = stride; 10.160 stride = tmp; 10.161 } 10.162 - //if( loop(xphi) != l) return NULL;// Merge point is in inner loop?? 10.163 - if( !xphi->is_Phi() ) return NULL; // Too much math on the trip counter 10.164 + // Stride must be constant 10.165 + int stride_con = stride->get_int(); 10.166 + assert(stride_con != 0, "missed some peephole opt"); 10.167 + 10.168 + if (!xphi->is_Phi()) 10.169 + return false; // Too much math on the trip counter 10.170 + if (phi_incr != NULL && phi_incr != xphi) 10.171 + return false; 10.172 PhiNode *phi = xphi->as_Phi(); 10.173 10.174 - // Stride must be constant 10.175 - const Type *stride_t = stride->bottom_type(); 10.176 - int stride_con = stride_t->is_int()->get_con(); 10.177 - assert( stride_con, "missed some peephole opt" ); 10.178 - 10.179 // Phi must be of loop header; backedge must wrap to increment 10.180 - if( phi->region() != x ) return NULL; 10.181 - if( trunc1 == NULL && phi->in(LoopNode::LoopBackControl) != incr || 10.182 - trunc1 != NULL && phi->in(LoopNode::LoopBackControl) != trunc1 ) { 10.183 - return NULL; 10.184 + if (phi->region() != x) 10.185 + return false; 10.186 + if (trunc1 == NULL && phi->in(LoopNode::LoopBackControl) != incr || 10.187 + trunc1 != NULL && phi->in(LoopNode::LoopBackControl) != trunc1) { 10.188 + return false; 10.189 } 10.190 Node *init_trip = phi->in(LoopNode::EntryControl); 10.191 - //if (!init_trip->is_Con()) return NULL; // avoid rolling over MAXINT/MININT 10.192 10.193 // If iv trunc type is smaller than int, check for possible wrap. 10.194 if (!TypeInt::INT->higher_equal(iv_trunc_t)) { 10.195 @@ -267,12 +297,12 @@ 10.196 if (stride_con > 0) { 10.197 if (iv_trunc_t->_hi - phi_ft->_hi < stride_con || 10.198 iv_trunc_t->_lo > phi_ft->_lo) { 10.199 - return NULL; // truncation may occur 10.200 + return false; // truncation may occur 10.201 } 10.202 } else if (stride_con < 0) { 10.203 if (iv_trunc_t->_lo - phi_ft->_lo > stride_con || 10.204 iv_trunc_t->_hi < phi_ft->_hi) { 10.205 - return NULL; // truncation may occur 10.206 + return false; // truncation may occur 10.207 } 10.208 } 10.209 // No possibility of wrap so truncation can be discarded 10.210 @@ -281,35 +311,45 @@ 10.211 assert(trunc1 == NULL && trunc2 == NULL, "no truncation for int"); 10.212 } 10.213 10.214 + // If the condition is inverted and we will be rolling 10.215 + // through MININT to MAXINT, then bail out. 10.216 + if (bt == BoolTest::eq || // Bail out, but this loop trips at most twice! 10.217 + // Odd stride 10.218 + bt == BoolTest::ne && stride_con != 1 && stride_con != -1 || 10.219 + // Count down loop rolls through MAXINT 10.220 + (bt == BoolTest::le || bt == BoolTest::lt) && stride_con < 0 || 10.221 + // Count up loop rolls through MININT 10.222 + (bt == BoolTest::ge || bt == BoolTest::gt) && stride_con > 0 ) { 10.223 + return false; // Bail out 10.224 + } 10.225 + 10.226 + const TypeInt* init_t = gvn->type(init_trip)->is_int(); 10.227 + const TypeInt* limit_t = gvn->type(limit)->is_int(); 10.228 + 10.229 + if (stride_con > 0) { 10.230 + long init_p = (long)init_t->_lo + stride_con; 10.231 + if (init_p > (long)max_jint || init_p > (long)limit_t->_hi) 10.232 + return false; // cyclic loop or this loop trips only once 10.233 + } else { 10.234 + long init_p = (long)init_t->_hi + stride_con; 10.235 + if (init_p < (long)min_jint || init_p < (long)limit_t->_lo) 10.236 + return false; // cyclic loop or this loop trips only once 10.237 + } 10.238 + 10.239 // ================================================= 10.240 // ---- SUCCESS! Found A Trip-Counted Loop! ----- 10.241 // 10.242 - // Canonicalize the condition on the test. If we can exactly determine 10.243 - // the trip-counter exit value, then set limit to that value and use 10.244 - // a '!=' test. Otherwise use condition '<' for count-up loops and 10.245 - // '>' for count-down loops. If the condition is inverted and we will 10.246 - // be rolling through MININT to MAXINT, then bail out. 10.247 - 10.248 + assert(x->Opcode() == Op_Loop, "regular loops only"); 10.249 C->print_method("Before CountedLoop", 3); 10.250 10.251 - // Check for SafePoint on backedge and remove 10.252 - Node *sfpt = x->in(LoopNode::LoopBackControl); 10.253 - if( sfpt->Opcode() == Op_SafePoint && is_deleteable_safept(sfpt)) { 10.254 - lazy_replace( sfpt, iftrue ); 10.255 - loop->_tail = iftrue; 10.256 - } 10.257 - 10.258 - 10.259 // If compare points to incr, we are ok. Otherwise the compare 10.260 // can directly point to the phi; in this case adjust the compare so that 10.261 // it points to the incr by adjusting the limit. 10.262 - if( cmp->in(1) == phi || cmp->in(2) == phi ) 10.263 + if (cmp->in(1) == phi || cmp->in(2) == phi) 10.264 limit = gvn->transform(new (C, 3) AddINode(limit,stride)); 10.265 10.266 // trip-count for +-tive stride should be: (limit - init_trip + stride - 1)/stride. 10.267 // Final value for iterator should be: trip_count * stride + init_trip. 10.268 - const Type *limit_t = limit->bottom_type(); 10.269 - const Type *init_t = init_trip->bottom_type(); 10.270 Node *one_p = gvn->intcon( 1); 10.271 Node *one_m = gvn->intcon(-1); 10.272 10.273 @@ -317,15 +357,15 @@ 10.274 Node *hook = new (C, 6) Node(6); 10.275 switch( bt ) { 10.276 case BoolTest::eq: 10.277 - return NULL; // Bail out, but this loop trips at most twice! 10.278 + ShouldNotReachHere(); 10.279 case BoolTest::ne: // Ahh, the case we desire 10.280 - if( stride_con == 1 ) 10.281 + if (stride_con == 1) 10.282 trip_count = gvn->transform(new (C, 3) SubINode(limit,init_trip)); 10.283 - else if( stride_con == -1 ) 10.284 + else if (stride_con == -1) 10.285 trip_count = gvn->transform(new (C, 3) SubINode(init_trip,limit)); 10.286 else 10.287 - return NULL; // Odd stride; must prove we hit limit exactly 10.288 - set_subtree_ctrl( trip_count ); 10.289 + ShouldNotReachHere(); 10.290 + set_subtree_ctrl(trip_count); 10.291 //_loop.map(trip_count->_idx,loop(limit)); 10.292 break; 10.293 case BoolTest::le: // Maybe convert to '<' case 10.294 @@ -338,7 +378,8 @@ 10.295 //_loop.map(limit->_idx,limit_loop); 10.296 // Fall into next case 10.297 case BoolTest::lt: { // Maybe convert to '!=' case 10.298 - if( stride_con < 0 ) return NULL; // Count down loop rolls through MAXINT 10.299 + if (stride_con < 0) // Count down loop rolls through MAXINT 10.300 + ShouldNotReachHere(); 10.301 Node *range = gvn->transform(new (C, 3) SubINode(limit,init_trip)); 10.302 set_subtree_ctrl( range ); 10.303 hook->init_req(0, range); 10.304 @@ -367,7 +408,8 @@ 10.305 //_loop.map(limit->_idx,limit_loop); 10.306 // Fall into next case 10.307 case BoolTest::gt: { // Maybe convert to '!=' case 10.308 - if( stride_con > 0 ) return NULL; // count up loop rolls through MININT 10.309 + if (stride_con > 0) // count up loop rolls through MININT 10.310 + ShouldNotReachHere(); 10.311 Node *range = gvn->transform(new (C, 3) SubINode(limit,init_trip)); 10.312 set_subtree_ctrl( range ); 10.313 hook->init_req(0, range); 10.314 @@ -385,7 +427,7 @@ 10.315 hook->init_req(3, trip_count); 10.316 break; 10.317 } 10.318 - } 10.319 + } // switch( bt ) 10.320 10.321 Node *span = gvn->transform(new (C, 3) MulINode(trip_count,stride)); 10.322 set_subtree_ctrl( span ); 10.323 @@ -394,83 +436,82 @@ 10.324 limit = gvn->transform(new (C, 3) AddINode(span,init_trip)); 10.325 set_subtree_ctrl( limit ); 10.326 10.327 + // Check for SafePoint on backedge and remove 10.328 + Node *sfpt = x->in(LoopNode::LoopBackControl); 10.329 + if (sfpt->Opcode() == Op_SafePoint && is_deleteable_safept(sfpt)) { 10.330 + lazy_replace( sfpt, iftrue ); 10.331 + loop->_tail = iftrue; 10.332 + } 10.333 + 10.334 // Build a canonical trip test. 10.335 // Clone code, as old values may be in use. 10.336 + Node* nphi = PhiNode::make(x, init_trip, TypeInt::INT); 10.337 + nphi = _igvn.register_new_node_with_optimizer(nphi); 10.338 + set_ctrl(nphi, get_ctrl(phi)); 10.339 + 10.340 incr = incr->clone(); 10.341 - incr->set_req(1,phi); 10.342 + incr->set_req(1,nphi); 10.343 incr->set_req(2,stride); 10.344 incr = _igvn.register_new_node_with_optimizer(incr); 10.345 set_early_ctrl( incr ); 10.346 - _igvn.hash_delete(phi); 10.347 - phi->set_req_X( LoopNode::LoopBackControl, incr, &_igvn ); 10.348 10.349 - // If phi type is more restrictive than Int, raise to 10.350 - // Int to prevent (almost) infinite recursion in igvn 10.351 - // which can only handle integer types for constants or minint..maxint. 10.352 - if (!TypeInt::INT->higher_equal(phi->bottom_type())) { 10.353 - Node* nphi = PhiNode::make(phi->in(0), phi->in(LoopNode::EntryControl), TypeInt::INT); 10.354 - nphi->set_req(LoopNode::LoopBackControl, phi->in(LoopNode::LoopBackControl)); 10.355 - nphi = _igvn.register_new_node_with_optimizer(nphi); 10.356 - set_ctrl(nphi, get_ctrl(phi)); 10.357 - _igvn.replace_node(phi, nphi); 10.358 - phi = nphi->as_Phi(); 10.359 - } 10.360 + nphi->set_req(LoopNode::LoopBackControl, incr); 10.361 + _igvn.replace_node(phi, nphi); 10.362 + phi = nphi->as_Phi(); 10.363 + 10.364 cmp = cmp->clone(); 10.365 cmp->set_req(1,incr); 10.366 cmp->set_req(2,limit); 10.367 cmp = _igvn.register_new_node_with_optimizer(cmp); 10.368 set_ctrl(cmp, iff->in(0)); 10.369 10.370 - Node *tmp = test->clone(); 10.371 - assert( tmp->is_Bool(), "" ); 10.372 - test = (BoolNode*)tmp; 10.373 - (*(BoolTest*)&test->_test)._test = bt; //BoolTest::ne; 10.374 + test = test->clone()->as_Bool(); 10.375 + (*(BoolTest*)&test->_test)._test = bt; 10.376 test->set_req(1,cmp); 10.377 _igvn.register_new_node_with_optimizer(test); 10.378 set_ctrl(test, iff->in(0)); 10.379 - // If the exit test is dead, STOP! 10.380 - if( test == NULL ) return NULL; 10.381 - _igvn.hash_delete(iff); 10.382 - iff->set_req_X( 1, test, &_igvn ); 10.383 10.384 // Replace the old IfNode with a new LoopEndNode 10.385 - Node *lex = _igvn.register_new_node_with_optimizer(new (C, 2) CountedLoopEndNode( iff->in(0), iff->in(1), cl_prob, iff->as_If()->_fcnt )); 10.386 + Node *lex = _igvn.register_new_node_with_optimizer(new (C, 2) CountedLoopEndNode( iff->in(0), test, cl_prob, iff->as_If()->_fcnt )); 10.387 IfNode *le = lex->as_If(); 10.388 uint dd = dom_depth(iff); 10.389 set_idom(le, le->in(0), dd); // Update dominance for loop exit 10.390 set_loop(le, loop); 10.391 10.392 // Get the loop-exit control 10.393 - Node *if_f = iff->as_If()->proj_out(!(iftrue_op == Op_IfTrue)); 10.394 + Node *iffalse = iff->as_If()->proj_out(!(iftrue_op == Op_IfTrue)); 10.395 10.396 // Need to swap loop-exit and loop-back control? 10.397 - if( iftrue_op == Op_IfFalse ) { 10.398 + if (iftrue_op == Op_IfFalse) { 10.399 Node *ift2=_igvn.register_new_node_with_optimizer(new (C, 1) IfTrueNode (le)); 10.400 Node *iff2=_igvn.register_new_node_with_optimizer(new (C, 1) IfFalseNode(le)); 10.401 10.402 loop->_tail = back_control = ift2; 10.403 set_loop(ift2, loop); 10.404 - set_loop(iff2, get_loop(if_f)); 10.405 + set_loop(iff2, get_loop(iffalse)); 10.406 10.407 // Lazy update of 'get_ctrl' mechanism. 10.408 - lazy_replace_proj( if_f , iff2 ); 10.409 - lazy_replace_proj( iftrue, ift2 ); 10.410 + lazy_replace_proj( iffalse, iff2 ); 10.411 + lazy_replace_proj( iftrue, ift2 ); 10.412 10.413 // Swap names 10.414 - if_f = iff2; 10.415 - iftrue = ift2; 10.416 + iffalse = iff2; 10.417 + iftrue = ift2; 10.418 } else { 10.419 - _igvn.hash_delete(if_f ); 10.420 + _igvn.hash_delete(iffalse); 10.421 _igvn.hash_delete(iftrue); 10.422 - if_f ->set_req_X( 0, le, &_igvn ); 10.423 - iftrue->set_req_X( 0, le, &_igvn ); 10.424 + iffalse->set_req_X( 0, le, &_igvn ); 10.425 + iftrue ->set_req_X( 0, le, &_igvn ); 10.426 } 10.427 10.428 - set_idom(iftrue, le, dd+1); 10.429 - set_idom(if_f, le, dd+1); 10.430 + set_idom(iftrue, le, dd+1); 10.431 + set_idom(iffalse, le, dd+1); 10.432 + assert(iff->outcnt() == 0, "should be dead now"); 10.433 + lazy_replace( iff, le ); // fix 'get_ctrl' 10.434 10.435 // Now setup a new CountedLoopNode to replace the existing LoopNode 10.436 CountedLoopNode *l = new (C, 3) CountedLoopNode(init_control, back_control); 10.437 + l->set_unswitch_count(x->as_Loop()->unswitch_count()); // Preserve 10.438 // The following assert is approximately true, and defines the intention 10.439 // of can_be_counted_loop. It fails, however, because phase->type 10.440 // is not yet initialized for this loop and its parts. 10.441 @@ -491,10 +532,14 @@ 10.442 // Free up intermediate goo 10.443 _igvn.remove_dead_node(hook); 10.444 10.445 +#ifdef ASSERT 10.446 + assert(l->is_valid_counted_loop(), "counted loop shape is messed up"); 10.447 + assert(l == loop->_head && l->phi() == phi && l->loopexit() == lex, "" ); 10.448 +#endif 10.449 + 10.450 C->print_method("After CountedLoop", 3); 10.451 10.452 - // Return trip counter 10.453 - return trip_count; 10.454 + return true; 10.455 } 10.456 10.457 10.458 @@ -1256,17 +1301,98 @@ 10.459 return true; 10.460 } 10.461 10.462 +//---------------------------replace_parallel_iv------------------------------- 10.463 +// Replace parallel induction variable (parallel to trip counter) 10.464 +void PhaseIdealLoop::replace_parallel_iv(IdealLoopTree *loop) { 10.465 + assert(loop->_head->is_CountedLoop(), ""); 10.466 + CountedLoopNode *cl = loop->_head->as_CountedLoop(); 10.467 + Node *incr = cl->incr(); 10.468 + if (incr == NULL) 10.469 + return; // Dead loop? 10.470 + Node *init = cl->init_trip(); 10.471 + Node *phi = cl->phi(); 10.472 + // protect against stride not being a constant 10.473 + if (!cl->stride_is_con()) 10.474 + return; 10.475 + int stride_con = cl->stride_con(); 10.476 + 10.477 + PhaseGVN *gvn = &_igvn; 10.478 + 10.479 + // Visit all children, looking for Phis 10.480 + for (DUIterator i = cl->outs(); cl->has_out(i); i++) { 10.481 + Node *out = cl->out(i); 10.482 + // Look for other phis (secondary IVs). Skip dead ones 10.483 + if (!out->is_Phi() || out == phi || !has_node(out)) 10.484 + continue; 10.485 + PhiNode* phi2 = out->as_Phi(); 10.486 + Node *incr2 = phi2->in( LoopNode::LoopBackControl ); 10.487 + // Look for induction variables of the form: X += constant 10.488 + if (phi2->region() != loop->_head || 10.489 + incr2->req() != 3 || 10.490 + incr2->in(1) != phi2 || 10.491 + incr2 == incr || 10.492 + incr2->Opcode() != Op_AddI || 10.493 + !incr2->in(2)->is_Con()) 10.494 + continue; 10.495 + 10.496 + // Check for parallel induction variable (parallel to trip counter) 10.497 + // via an affine function. In particular, count-down loops with 10.498 + // count-up array indices are common. We only RCE references off 10.499 + // the trip-counter, so we need to convert all these to trip-counter 10.500 + // expressions. 10.501 + Node *init2 = phi2->in( LoopNode::EntryControl ); 10.502 + int stride_con2 = incr2->in(2)->get_int(); 10.503 + 10.504 + // The general case here gets a little tricky. We want to find the 10.505 + // GCD of all possible parallel IV's and make a new IV using this 10.506 + // GCD for the loop. Then all possible IVs are simple multiples of 10.507 + // the GCD. In practice, this will cover very few extra loops. 10.508 + // Instead we require 'stride_con2' to be a multiple of 'stride_con', 10.509 + // where +/-1 is the common case, but other integer multiples are 10.510 + // also easy to handle. 10.511 + int ratio_con = stride_con2/stride_con; 10.512 + 10.513 + if ((ratio_con * stride_con) == stride_con2) { // Check for exact 10.514 + // Convert to using the trip counter. The parallel induction 10.515 + // variable differs from the trip counter by a loop-invariant 10.516 + // amount, the difference between their respective initial values. 10.517 + // It is scaled by the 'ratio_con'. 10.518 + // Perform local Ideal transformation since in most cases ratio == 1. 10.519 + Node* ratio = _igvn.intcon(ratio_con); 10.520 + set_ctrl(ratio, C->root()); 10.521 + Node* hook = new (C, 3) Node(3); 10.522 + Node* ratio_init = gvn->transform(new (C, 3) MulINode(init, ratio)); 10.523 + hook->init_req(0, ratio_init); 10.524 + Node* diff = gvn->transform(new (C, 3) SubINode(init2, ratio_init)); 10.525 + hook->init_req(1, diff); 10.526 + Node* ratio_idx = gvn->transform(new (C, 3) MulINode(phi, ratio)); 10.527 + hook->init_req(2, ratio_idx); 10.528 + Node* add = gvn->transform(new (C, 3) AddINode(ratio_idx, diff)); 10.529 + set_subtree_ctrl(add); 10.530 + _igvn.replace_node( phi2, add ); 10.531 + // Free up intermediate goo 10.532 + _igvn.remove_dead_node(hook); 10.533 + // Sometimes an induction variable is unused 10.534 + if (add->outcnt() == 0) { 10.535 + _igvn.remove_dead_node(add); 10.536 + } 10.537 + --i; // deleted this phi; rescan starting with next position 10.538 + continue; 10.539 + } 10.540 + } 10.541 +} 10.542 + 10.543 //------------------------------counted_loop----------------------------------- 10.544 // Convert to counted loops where possible 10.545 void IdealLoopTree::counted_loop( PhaseIdealLoop *phase ) { 10.546 10.547 // For grins, set the inner-loop flag here 10.548 - if( !_child ) { 10.549 - if( _head->is_Loop() ) _head->as_Loop()->set_inner_loop(); 10.550 + if (!_child) { 10.551 + if (_head->is_Loop()) _head->as_Loop()->set_inner_loop(); 10.552 } 10.553 10.554 - if( _head->is_CountedLoop() || 10.555 - phase->is_counted_loop( _head, this ) ) { 10.556 + if (_head->is_CountedLoop() || 10.557 + phase->is_counted_loop(_head, this)) { 10.558 _has_sfpt = 1; // Indicate we do not need a safepoint here 10.559 10.560 // Look for a safepoint to remove 10.561 @@ -1275,79 +1401,9 @@ 10.562 phase->is_deleteable_safept(n)) 10.563 phase->lazy_replace(n,n->in(TypeFunc::Control)); 10.564 10.565 - CountedLoopNode *cl = _head->as_CountedLoop(); 10.566 - Node *incr = cl->incr(); 10.567 - if( !incr ) return; // Dead loop? 10.568 - Node *init = cl->init_trip(); 10.569 - Node *phi = cl->phi(); 10.570 - // protect against stride not being a constant 10.571 - if( !cl->stride_is_con() ) return; 10.572 - int stride_con = cl->stride_con(); 10.573 + // Look for induction variables 10.574 + phase->replace_parallel_iv(this); 10.575 10.576 - // Look for induction variables 10.577 - 10.578 - // Visit all children, looking for Phis 10.579 - for (DUIterator i = cl->outs(); cl->has_out(i); i++) { 10.580 - Node *out = cl->out(i); 10.581 - // Look for other phis (secondary IVs). Skip dead ones 10.582 - if (!out->is_Phi() || out == phi || !phase->has_node(out)) continue; 10.583 - PhiNode* phi2 = out->as_Phi(); 10.584 - Node *incr2 = phi2->in( LoopNode::LoopBackControl ); 10.585 - // Look for induction variables of the form: X += constant 10.586 - if( phi2->region() != _head || 10.587 - incr2->req() != 3 || 10.588 - incr2->in(1) != phi2 || 10.589 - incr2 == incr || 10.590 - incr2->Opcode() != Op_AddI || 10.591 - !incr2->in(2)->is_Con() ) 10.592 - continue; 10.593 - 10.594 - // Check for parallel induction variable (parallel to trip counter) 10.595 - // via an affine function. In particular, count-down loops with 10.596 - // count-up array indices are common. We only RCE references off 10.597 - // the trip-counter, so we need to convert all these to trip-counter 10.598 - // expressions. 10.599 - Node *init2 = phi2->in( LoopNode::EntryControl ); 10.600 - int stride_con2 = incr2->in(2)->get_int(); 10.601 - 10.602 - // The general case here gets a little tricky. We want to find the 10.603 - // GCD of all possible parallel IV's and make a new IV using this 10.604 - // GCD for the loop. Then all possible IVs are simple multiples of 10.605 - // the GCD. In practice, this will cover very few extra loops. 10.606 - // Instead we require 'stride_con2' to be a multiple of 'stride_con', 10.607 - // where +/-1 is the common case, but other integer multiples are 10.608 - // also easy to handle. 10.609 - int ratio_con = stride_con2/stride_con; 10.610 - 10.611 - if( ratio_con * stride_con == stride_con2 ) { // Check for exact 10.612 - // Convert to using the trip counter. The parallel induction 10.613 - // variable differs from the trip counter by a loop-invariant 10.614 - // amount, the difference between their respective initial values. 10.615 - // It is scaled by the 'ratio_con'. 10.616 - Compile* C = phase->C; 10.617 - Node* ratio = phase->_igvn.intcon(ratio_con); 10.618 - phase->set_ctrl(ratio, C->root()); 10.619 - Node* ratio_init = new (C, 3) MulINode(init, ratio); 10.620 - phase->_igvn.register_new_node_with_optimizer(ratio_init, init); 10.621 - phase->set_early_ctrl(ratio_init); 10.622 - Node* diff = new (C, 3) SubINode(init2, ratio_init); 10.623 - phase->_igvn.register_new_node_with_optimizer(diff, init2); 10.624 - phase->set_early_ctrl(diff); 10.625 - Node* ratio_idx = new (C, 3) MulINode(phi, ratio); 10.626 - phase->_igvn.register_new_node_with_optimizer(ratio_idx, phi); 10.627 - phase->set_ctrl(ratio_idx, cl); 10.628 - Node* add = new (C, 3) AddINode(ratio_idx, diff); 10.629 - phase->_igvn.register_new_node_with_optimizer(add); 10.630 - phase->set_ctrl(add, cl); 10.631 - phase->_igvn.replace_node( phi2, add ); 10.632 - // Sometimes an induction variable is unused 10.633 - if (add->outcnt() == 0) { 10.634 - phase->_igvn.remove_dead_node(add); 10.635 - } 10.636 - --i; // deleted this phi; rescan starting with next position 10.637 - continue; 10.638 - } 10.639 - } 10.640 } else if (_parent != NULL && !_irreducible) { 10.641 // Not a counted loop. 10.642 // Look for a safepoint on the idom-path to remove, preserving the first one 10.643 @@ -1366,24 +1422,31 @@ 10.644 } 10.645 10.646 // Recursively 10.647 - if( _child ) _child->counted_loop( phase ); 10.648 - if( _next ) _next ->counted_loop( phase ); 10.649 + if (_child) _child->counted_loop( phase ); 10.650 + if (_next) _next ->counted_loop( phase ); 10.651 } 10.652 10.653 #ifndef PRODUCT 10.654 //------------------------------dump_head-------------------------------------- 10.655 // Dump 1 liner for loop header info 10.656 void IdealLoopTree::dump_head( ) const { 10.657 - for( uint i=0; i<_nest; i++ ) 10.658 + for (uint i=0; i<_nest; i++) 10.659 tty->print(" "); 10.660 tty->print("Loop: N%d/N%d ",_head->_idx,_tail->_idx); 10.661 - if( _irreducible ) tty->print(" IRREDUCIBLE"); 10.662 - if( _head->is_CountedLoop() ) { 10.663 + if (_irreducible) tty->print(" IRREDUCIBLE"); 10.664 + if (UseLoopPredicate) { 10.665 + Node* entry = _head->in(LoopNode::EntryControl); 10.666 + if (entry != NULL && entry->is_Proj() && 10.667 + PhaseIdealLoop::is_uncommon_trap_if_pattern(entry->as_Proj(), Deoptimization::Reason_predicate)) { 10.668 + tty->print(" predicated"); 10.669 + } 10.670 + } 10.671 + if (_head->is_CountedLoop()) { 10.672 CountedLoopNode *cl = _head->as_CountedLoop(); 10.673 tty->print(" counted"); 10.674 - if( cl->is_pre_loop () ) tty->print(" pre" ); 10.675 - if( cl->is_main_loop() ) tty->print(" main"); 10.676 - if( cl->is_post_loop() ) tty->print(" post"); 10.677 + if (cl->is_pre_loop ()) tty->print(" pre" ); 10.678 + if (cl->is_main_loop()) tty->print(" main"); 10.679 + if (cl->is_post_loop()) tty->print(" post"); 10.680 } 10.681 tty->cr(); 10.682 } 10.683 @@ -1392,8 +1455,8 @@ 10.684 // Dump loops by loop tree 10.685 void IdealLoopTree::dump( ) const { 10.686 dump_head(); 10.687 - if( _child ) _child->dump(); 10.688 - if( _next ) _next ->dump(); 10.689 + if (_child) _child->dump(); 10.690 + if (_next) _next ->dump(); 10.691 } 10.692 10.693 #endif 10.694 @@ -1439,19 +1502,19 @@ 10.695 } 10.696 10.697 // self (only loops that we can apply loop predication may use their predicates) 10.698 - if (loop->_head->is_Loop() && 10.699 - !loop->_irreducible && 10.700 + if (loop->_head->is_Loop() && 10.701 + !loop->_irreducible && 10.702 !loop->tail()->is_top()) { 10.703 - LoopNode *lpn = loop->_head->as_Loop(); 10.704 + LoopNode* lpn = loop->_head->as_Loop(); 10.705 Node* entry = lpn->in(LoopNode::EntryControl); 10.706 - ProjNode *predicate_proj = find_predicate_insertion_point(entry); 10.707 + Node* predicate_proj = find_predicate(entry); 10.708 if (predicate_proj != NULL ) { // right pattern that can be used by loop predication 10.709 - assert(entry->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be"); 10.710 + assert(entry->in(0)->in(1)->in(1)->Opcode() == Op_Opaque1, "must be"); 10.711 useful_predicates.push(entry->in(0)->in(1)->in(1)); // good one 10.712 } 10.713 } 10.714 10.715 - if ( loop->_next ) { // sibling 10.716 + if (loop->_next) { // sibling 10.717 collect_potentially_useful_predicates(loop->_next, useful_predicates); 10.718 } 10.719 } 10.720 @@ -1459,7 +1522,8 @@ 10.721 //------------------------eliminate_useless_predicates----------------------------- 10.722 // Eliminate all inserted predicates if they could not be used by loop predication. 10.723 void PhaseIdealLoop::eliminate_useless_predicates() { 10.724 - if (C->predicate_count() == 0) return; // no predicate left 10.725 + if (C->predicate_count() == 0) 10.726 + return; // no predicate left 10.727 10.728 Unique_Node_List useful_predicates; // to store useful predicates 10.729 if (C->has_loops()) { 10.730 @@ -1647,12 +1711,15 @@ 10.731 10.732 #ifndef PRODUCT 10.733 C->verify_graph_edges(); 10.734 - if( _verify_me ) { // Nested verify pass? 10.735 + if (_verify_me) { // Nested verify pass? 10.736 // Check to see if the verify mode is broken 10.737 assert(C->unique() == unique, "non-optimize mode made Nodes? ? ?"); 10.738 return; 10.739 } 10.740 - if( VerifyLoopOptimizations ) verify(); 10.741 + if(VerifyLoopOptimizations) verify(); 10.742 + if(TraceLoopOpts && C->has_loops()) { 10.743 + _ltree_root->dump(); 10.744 + } 10.745 #endif 10.746 10.747 if (ReassociateInvariants) {
11.1 --- a/src/share/vm/opto/loopnode.hpp Thu Mar 24 23:00:27 2011 -0700 11.2 +++ b/src/share/vm/opto/loopnode.hpp Thu Mar 24 23:04:36 2011 -0700 11.3 @@ -93,6 +93,7 @@ 11.4 in(1) != NULL && phase->type(in(1)) != Type::TOP && 11.5 in(2) != NULL && phase->type(in(2)) != Type::TOP; 11.6 } 11.7 + bool is_valid_counted_loop() const; 11.8 #ifndef PRODUCT 11.9 virtual void dump_spec(outputStream *st) const; 11.10 #endif 11.11 @@ -101,9 +102,8 @@ 11.12 //------------------------------Counted Loops---------------------------------- 11.13 // Counted loops are all trip-counted loops, with exactly 1 trip-counter exit 11.14 // path (and maybe some other exit paths). The trip-counter exit is always 11.15 -// last in the loop. The trip-counter does not have to stride by a constant, 11.16 -// but it does have to stride by a loop-invariant amount; the exit value is 11.17 -// also loop invariant. 11.18 +// last in the loop. The trip-counter have to stride by a constant; 11.19 +// the exit value is also loop invariant. 11.20 11.21 // CountedLoopNodes and CountedLoopEndNodes come in matched pairs. The 11.22 // CountedLoopNode has the incoming loop control and the loop-back-control 11.23 @@ -112,7 +112,7 @@ 11.24 // CountedLoopNode if there is control flow in the loop), the post-increment 11.25 // trip-counter value, and the limit. The trip-counter value is always of 11.26 // the form (Op old-trip-counter stride). The old-trip-counter is produced 11.27 -// by a Phi connected to the CountedLoopNode. The stride is loop invariant. 11.28 +// by a Phi connected to the CountedLoopNode. The stride is constant. 11.29 // The Op is any commutable opcode, including Add, Mul, Xor. The 11.30 // CountedLoopEndNode also takes in the loop-invariant limit value. 11.31 11.32 @@ -696,6 +696,9 @@ 11.33 // Is safept not required by an outer loop? 11.34 bool is_deleteable_safept(Node* sfpt); 11.35 11.36 + // Replace parallel induction variable (parallel to trip counter) 11.37 + void replace_parallel_iv(IdealLoopTree *loop); 11.38 + 11.39 // Perform verification that the graph is valid. 11.40 PhaseIdealLoop( PhaseIterGVN &igvn) : 11.41 PhaseTransform(Ideal_Loop), 11.42 @@ -751,7 +754,7 @@ 11.43 // Per-Node transform 11.44 virtual Node *transform( Node *a_node ) { return 0; } 11.45 11.46 - Node *is_counted_loop( Node *x, IdealLoopTree *loop ); 11.47 + bool is_counted_loop( Node *x, IdealLoopTree *loop ); 11.48 11.49 // Return a post-walked LoopNode 11.50 IdealLoopTree *get_loop( Node *n ) const { 11.51 @@ -815,16 +818,22 @@ 11.52 bool is_scaled_iv_plus_offset(Node* exp, Node* iv, int* p_scale, Node** p_offset, int depth = 0); 11.53 11.54 // Return true if proj is for "proj->[region->..]call_uct" 11.55 - bool is_uncommon_trap_proj(ProjNode* proj, bool must_reason_predicate = false); 11.56 + // Return true if proj is for "proj->[region->..]call_uct" 11.57 + static bool is_uncommon_trap_proj(ProjNode* proj, Deoptimization::DeoptReason reason); 11.58 // Return true for "if(test)-> proj -> ... 11.59 // | 11.60 // V 11.61 // other_proj->[region->..]call_uct" 11.62 - bool is_uncommon_trap_if_pattern(ProjNode* proj, bool must_reason_predicate = false); 11.63 + static bool is_uncommon_trap_if_pattern(ProjNode* proj, Deoptimization::DeoptReason reason); 11.64 // Create a new if above the uncommon_trap_if_pattern for the predicate to be promoted 11.65 - ProjNode* create_new_if_for_predicate(ProjNode* cont_proj); 11.66 - // Find a good location to insert a predicate 11.67 - ProjNode* find_predicate_insertion_point(Node* start_c); 11.68 + ProjNode* create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry, 11.69 + Deoptimization::DeoptReason reason); 11.70 + void register_control(Node* n, IdealLoopTree *loop, Node* pred); 11.71 + 11.72 + // Find a good location to insert a predicate 11.73 + static ProjNode* find_predicate_insertion_point(Node* start_c, Deoptimization::DeoptReason reason); 11.74 + // Find a predicate 11.75 + static Node* find_predicate(Node* entry); 11.76 // Construct a range check for a predicate if 11.77 BoolNode* rc_predicate(Node* ctrl, 11.78 int scale, Node* offset, 11.79 @@ -936,7 +945,7 @@ 11.80 Node *has_local_phi_input( Node *n ); 11.81 // Mark an IfNode as being dominated by a prior test, 11.82 // without actually altering the CFG (and hence IDOM info). 11.83 - void dominated_by( Node *prevdom, Node *iff ); 11.84 + void dominated_by( Node *prevdom, Node *iff, bool flip = false ); 11.85 11.86 // Split Node 'n' through merge point 11.87 Node *split_thru_region( Node *n, Node *region );
12.1 --- a/src/share/vm/opto/loopopts.cpp Thu Mar 24 23:00:27 2011 -0700 12.2 +++ b/src/share/vm/opto/loopopts.cpp Thu Mar 24 23:04:36 2011 -0700 12.3 @@ -42,13 +42,13 @@ 12.4 return NULL; 12.5 } 12.6 int wins = 0; 12.7 - assert( !n->is_CFG(), "" ); 12.8 - assert( region->is_Region(), "" ); 12.9 + assert(!n->is_CFG(), ""); 12.10 + assert(region->is_Region(), ""); 12.11 12.12 const Type* type = n->bottom_type(); 12.13 const TypeOopPtr *t_oop = _igvn.type(n)->isa_oopptr(); 12.14 Node *phi; 12.15 - if( t_oop != NULL && t_oop->is_known_instance_field() ) { 12.16 + if (t_oop != NULL && t_oop->is_known_instance_field()) { 12.17 int iid = t_oop->instance_id(); 12.18 int index = C->get_alias_index(t_oop); 12.19 int offset = t_oop->offset(); 12.20 @@ -57,20 +57,20 @@ 12.21 phi = PhiNode::make_blank(region, n); 12.22 } 12.23 uint old_unique = C->unique(); 12.24 - for( uint i = 1; i < region->req(); i++ ) { 12.25 + for (uint i = 1; i < region->req(); i++) { 12.26 Node *x; 12.27 Node* the_clone = NULL; 12.28 - if( region->in(i) == C->top() ) { 12.29 + if (region->in(i) == C->top()) { 12.30 x = C->top(); // Dead path? Use a dead data op 12.31 } else { 12.32 x = n->clone(); // Else clone up the data op 12.33 the_clone = x; // Remember for possible deletion. 12.34 // Alter data node to use pre-phi inputs 12.35 - if( n->in(0) == region ) 12.36 + if (n->in(0) == region) 12.37 x->set_req( 0, region->in(i) ); 12.38 - for( uint j = 1; j < n->req(); j++ ) { 12.39 + for (uint j = 1; j < n->req(); j++) { 12.40 Node *in = n->in(j); 12.41 - if( in->is_Phi() && in->in(0) == region ) 12.42 + if (in->is_Phi() && in->in(0) == region) 12.43 x->set_req( j, in->in(i) ); // Use pre-Phi input for the clone 12.44 } 12.45 } 12.46 @@ -85,7 +85,7 @@ 12.47 // happen if the singleton occurs on loop entry, as the elimination of 12.48 // the PhiNode may cause the resulting node to migrate back to a previous 12.49 // loop iteration. 12.50 - if( singleton && t == Type::TOP ) { 12.51 + if (singleton && t == Type::TOP) { 12.52 // Is_Loop() == false does not confirm the absence of a loop (e.g., an 12.53 // irreducible loop may not be indicated by an affirmative is_Loop()); 12.54 // therefore, the only top we can split thru a phi is on a backedge of 12.55 @@ -93,7 +93,7 @@ 12.56 singleton &= region->is_Loop() && (i != LoopNode::EntryControl); 12.57 } 12.58 12.59 - if( singleton ) { 12.60 + if (singleton) { 12.61 wins++; 12.62 x = ((PhaseGVN&)_igvn).makecon(t); 12.63 } else { 12.64 @@ -108,12 +108,12 @@ 12.65 // igvn->type(x) is set to x->Value() already. 12.66 x->raise_bottom_type(t); 12.67 Node *y = x->Identity(&_igvn); 12.68 - if( y != x ) { 12.69 + if (y != x) { 12.70 wins++; 12.71 x = y; 12.72 } else { 12.73 y = _igvn.hash_find(x); 12.74 - if( y ) { 12.75 + if (y) { 12.76 wins++; 12.77 x = y; 12.78 } else { 12.79 @@ -129,7 +129,7 @@ 12.80 phi->set_req( i, x ); 12.81 } 12.82 // Too few wins? 12.83 - if( wins <= policy ) { 12.84 + if (wins <= policy) { 12.85 _igvn.remove_dead_node(phi); 12.86 return NULL; 12.87 } 12.88 @@ -137,7 +137,7 @@ 12.89 // Record Phi 12.90 register_new_node( phi, region ); 12.91 12.92 - for( uint i2 = 1; i2 < phi->req(); i2++ ) { 12.93 + for (uint i2 = 1; i2 < phi->req(); i2++) { 12.94 Node *x = phi->in(i2); 12.95 // If we commoned up the cloned 'x' with another existing Node, 12.96 // the existing Node picks up a new use. We need to make the 12.97 @@ -145,24 +145,44 @@ 12.98 Node *old_ctrl; 12.99 IdealLoopTree *old_loop; 12.100 12.101 + if (x->is_Con()) { 12.102 + // Constant's control is always root. 12.103 + set_ctrl(x, C->root()); 12.104 + continue; 12.105 + } 12.106 // The occasional new node 12.107 - if( x->_idx >= old_unique ) { // Found a new, unplaced node? 12.108 - old_ctrl = x->is_Con() ? C->root() : NULL; 12.109 - old_loop = NULL; // Not in any prior loop 12.110 + if (x->_idx >= old_unique) { // Found a new, unplaced node? 12.111 + old_ctrl = NULL; 12.112 + old_loop = NULL; // Not in any prior loop 12.113 } else { 12.114 - old_ctrl = x->is_Con() ? C->root() : get_ctrl(x); 12.115 + old_ctrl = get_ctrl(x); 12.116 old_loop = get_loop(old_ctrl); // Get prior loop 12.117 } 12.118 // New late point must dominate new use 12.119 - Node *new_ctrl = dom_lca( old_ctrl, region->in(i2) ); 12.120 + Node *new_ctrl = dom_lca(old_ctrl, region->in(i2)); 12.121 + if (new_ctrl == old_ctrl) // Nothing is changed 12.122 + continue; 12.123 + 12.124 + IdealLoopTree *new_loop = get_loop(new_ctrl); 12.125 + 12.126 + // Don't move x into a loop if its uses are 12.127 + // outside of loop. Otherwise x will be cloned 12.128 + // for each use outside of this loop. 12.129 + IdealLoopTree *use_loop = get_loop(region); 12.130 + if (!new_loop->is_member(use_loop) && 12.131 + (old_loop == NULL || !new_loop->is_member(old_loop))) { 12.132 + // Take early control, later control will be recalculated 12.133 + // during next iteration of loop optimizations. 12.134 + new_ctrl = get_early_ctrl(x); 12.135 + new_loop = get_loop(new_ctrl); 12.136 + } 12.137 // Set new location 12.138 set_ctrl(x, new_ctrl); 12.139 - IdealLoopTree *new_loop = get_loop( new_ctrl ); 12.140 // If changing loop bodies, see if we need to collect into new body 12.141 - if( old_loop != new_loop ) { 12.142 - if( old_loop && !old_loop->_child ) 12.143 + if (old_loop != new_loop) { 12.144 + if (old_loop && !old_loop->_child) 12.145 old_loop->_body.yank(x); 12.146 - if( !new_loop->_child ) 12.147 + if (!new_loop->_child) 12.148 new_loop->_body.push(x); // Collect body info 12.149 } 12.150 } 12.151 @@ -174,9 +194,9 @@ 12.152 // Replace the dominated test with an obvious true or false. Place it on the 12.153 // IGVN worklist for later cleanup. Move control-dependent data Nodes on the 12.154 // live path up to the dominating control. 12.155 -void PhaseIdealLoop::dominated_by( Node *prevdom, Node *iff ) { 12.156 +void PhaseIdealLoop::dominated_by( Node *prevdom, Node *iff, bool flip ) { 12.157 #ifndef PRODUCT 12.158 - if( VerifyLoopOptimizations && PrintOpto ) tty->print_cr("dominating test"); 12.159 + if (VerifyLoopOptimizations && PrintOpto) tty->print_cr("dominating test"); 12.160 #endif 12.161 12.162 12.163 @@ -185,6 +205,12 @@ 12.164 assert( iff->Opcode() == Op_If || iff->Opcode() == Op_CountedLoopEnd, "Check this code when new subtype is added"); 12.165 int pop = prevdom->Opcode(); 12.166 assert( pop == Op_IfFalse || pop == Op_IfTrue, "" ); 12.167 + if (flip) { 12.168 + if (pop == Op_IfTrue) 12.169 + pop = Op_IfFalse; 12.170 + else 12.171 + pop = Op_IfTrue; 12.172 + } 12.173 // 'con' is set to true or false to kill the dominated test. 12.174 Node *con = _igvn.makecon(pop == Op_IfTrue ? TypeInt::ONE : TypeInt::ZERO); 12.175 set_ctrl(con, C->root()); // Constant gets a new use 12.176 @@ -197,7 +223,7 @@ 12.177 // I can assume this path reaches an infinite loop. In this case it's not 12.178 // important to optimize the data Nodes - either the whole compilation will 12.179 // be tossed or this path (and all data Nodes) will go dead. 12.180 - if( iff->outcnt() != 2 ) return; 12.181 + if (iff->outcnt() != 2) return; 12.182 12.183 // Make control-dependent data Nodes on the live path (path that will remain 12.184 // once the dominated IF is removed) become control-dependent on the 12.185 @@ -207,16 +233,16 @@ 12.186 12.187 for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) { 12.188 Node* cd = dp->fast_out(i); // Control-dependent node 12.189 - if( cd->depends_only_on_test() ) { 12.190 - assert( cd->in(0) == dp, "" ); 12.191 - _igvn.hash_delete( cd ); 12.192 + if (cd->depends_only_on_test()) { 12.193 + assert(cd->in(0) == dp, ""); 12.194 + _igvn.hash_delete(cd); 12.195 cd->set_req(0, prevdom); 12.196 - set_early_ctrl( cd ); 12.197 + set_early_ctrl(cd); 12.198 _igvn._worklist.push(cd); 12.199 IdealLoopTree *new_loop = get_loop(get_ctrl(cd)); 12.200 - if( old_loop != new_loop ) { 12.201 - if( !old_loop->_child ) old_loop->_body.yank(cd); 12.202 - if( !new_loop->_child ) new_loop->_body.push(cd); 12.203 + if (old_loop != new_loop) { 12.204 + if (!old_loop->_child) old_loop->_body.yank(cd); 12.205 + if (!new_loop->_child) new_loop->_body.push(cd); 12.206 } 12.207 --i; 12.208 --imax; 12.209 @@ -2338,6 +2364,11 @@ 12.210 } 12.211 12.212 #if !defined(PRODUCT) 12.213 + if (TraceLoopOpts) { 12.214 + tty->print("PartialPeel "); 12.215 + loop->dump_head(); 12.216 + } 12.217 + 12.218 if (TracePartialPeeling) { 12.219 tty->print_cr("before partial peel one iteration"); 12.220 Node_List wl; 12.221 @@ -2481,6 +2512,7 @@ 12.222 // Create new loop head for new phis and to hang 12.223 // the nodes being moved (sinked) from the peel region. 12.224 LoopNode* new_head = new (C, 3) LoopNode(last_peel, last_peel); 12.225 + new_head->set_unswitch_count(head->unswitch_count()); // Preserve 12.226 _igvn.register_new_node_with_optimizer(new_head); 12.227 assert(first_not_peeled->in(0) == last_peel, "last_peel <- first_not_peeled"); 12.228 first_not_peeled->set_req(0, new_head); 12.229 @@ -2651,24 +2683,23 @@ 12.230 // prevent loop-fallout uses of the pre-incremented trip counter (which are 12.231 // then alive with the post-incremented trip counter forcing an extra 12.232 // register move) 12.233 -void PhaseIdealLoop::reorg_offsets( IdealLoopTree *loop ) { 12.234 +void PhaseIdealLoop::reorg_offsets(IdealLoopTree *loop) { 12.235 + // Perform it only for canonical counted loops. 12.236 + // Loop's shape could be messed up by iteration_split_impl. 12.237 + if (!loop->_head->is_CountedLoop()) 12.238 + return; 12.239 + if (!loop->_head->as_Loop()->is_valid_counted_loop()) 12.240 + return; 12.241 12.242 CountedLoopNode *cl = loop->_head->as_CountedLoop(); 12.243 CountedLoopEndNode *cle = cl->loopexit(); 12.244 - if( !cle ) return; // The occasional dead loop 12.245 - // Find loop exit control 12.246 Node *exit = cle->proj_out(false); 12.247 - assert( exit->Opcode() == Op_IfFalse, "" ); 12.248 + Node *phi = cl->phi(); 12.249 12.250 // Check for the special case of folks using the pre-incremented 12.251 // trip-counter on the fall-out path (forces the pre-incremented 12.252 // and post-incremented trip counter to be live at the same time). 12.253 // Fix this by adjusting to use the post-increment trip counter. 12.254 - Node *phi = cl->phi(); 12.255 - if( !phi ) return; // Dead infinite loop 12.256 - 12.257 - // Shape messed up, probably by iteration_split_impl 12.258 - if (phi->in(LoopNode::LoopBackControl) != cl->incr()) return; 12.259 12.260 bool progress = true; 12.261 while (progress) { 12.262 @@ -2677,21 +2708,19 @@ 12.263 Node* use = phi->fast_out(i); // User of trip-counter 12.264 if (!has_ctrl(use)) continue; 12.265 Node *u_ctrl = get_ctrl(use); 12.266 - if( use->is_Phi() ) { 12.267 + if (use->is_Phi()) { 12.268 u_ctrl = NULL; 12.269 - for( uint j = 1; j < use->req(); j++ ) 12.270 - if( use->in(j) == phi ) 12.271 - u_ctrl = dom_lca( u_ctrl, use->in(0)->in(j) ); 12.272 + for (uint j = 1; j < use->req(); j++) 12.273 + if (use->in(j) == phi) 12.274 + u_ctrl = dom_lca(u_ctrl, use->in(0)->in(j)); 12.275 } 12.276 IdealLoopTree *u_loop = get_loop(u_ctrl); 12.277 // Look for loop-invariant use 12.278 - if( u_loop == loop ) continue; 12.279 - if( loop->is_member( u_loop ) ) continue; 12.280 + if (u_loop == loop) continue; 12.281 + if (loop->is_member(u_loop)) continue; 12.282 // Check that use is live out the bottom. Assuming the trip-counter 12.283 // update is right at the bottom, uses of of the loop middle are ok. 12.284 - if( dom_lca( exit, u_ctrl ) != exit ) continue; 12.285 - // protect against stride not being a constant 12.286 - if( !cle->stride_is_con() ) continue; 12.287 + if (dom_lca(exit, u_ctrl) != exit) continue; 12.288 // Hit! Refactor use to use the post-incremented tripcounter. 12.289 // Compute a post-increment tripcounter. 12.290 Node *opaq = new (C, 2) Opaque2Node( C, cle->incr() ); 12.291 @@ -2702,9 +2731,10 @@ 12.292 register_new_node( post, u_ctrl ); 12.293 _igvn.hash_delete(use); 12.294 _igvn._worklist.push(use); 12.295 - for( uint j = 1; j < use->req(); j++ ) 12.296 - if( use->in(j) == phi ) 12.297 + for (uint j = 1; j < use->req(); j++) { 12.298 + if (use->in(j) == phi) 12.299 use->set_req(j, post); 12.300 + } 12.301 // Since DU info changed, rerun loop 12.302 progress = true; 12.303 break;
13.1 --- a/src/share/vm/opto/parse.hpp Thu Mar 24 23:00:27 2011 -0700 13.2 +++ b/src/share/vm/opto/parse.hpp Thu Mar 24 23:04:36 2011 -0700 13.3 @@ -136,6 +136,7 @@ 13.4 uint _count; // how many times executed? Currently only set by _goto's 13.5 bool _is_parsed; // has this block been parsed yet? 13.6 bool _is_handler; // is this block an exception handler? 13.7 + bool _has_merged_backedge; // does this block have merged backedge? 13.8 SafePointNode* _start_map; // all values flowing into this block 13.9 MethodLivenessResult _live_locals; // lazily initialized liveness bitmap 13.10 13.11 @@ -168,6 +169,18 @@ 13.12 // True after any predecessor flows control into this block 13.13 bool is_merged() const { return _start_map != NULL; } 13.14 13.15 +#ifdef ASSERT 13.16 + // True after backedge predecessor flows control into this block 13.17 + bool has_merged_backedge() const { return _has_merged_backedge; } 13.18 + void mark_merged_backedge(Block* pred) { 13.19 + assert(is_SEL_head(), "should be loop head"); 13.20 + if (pred != NULL && is_SEL_backedge(pred)) { 13.21 + assert(is_parsed(), "block should be parsed before merging backedges"); 13.22 + _has_merged_backedge = true; 13.23 + } 13.24 + } 13.25 +#endif 13.26 + 13.27 // True when all non-exception predecessors have been parsed. 13.28 bool is_ready() const { return preds_parsed() == pred_count(); } 13.29 13.30 @@ -441,11 +454,6 @@ 13.31 } 13.32 } 13.33 13.34 - // Return true if the parser should add a loop predicate 13.35 - bool should_add_predicate(int target_bci); 13.36 - // Insert a loop predicate into the graph 13.37 - void add_predicate(); 13.38 - 13.39 // Note: Intrinsic generation routines may be found in library_call.cpp. 13.40 13.41 // Helper function to setup Ideal Call nodes
14.1 --- a/src/share/vm/opto/parse1.cpp Thu Mar 24 23:00:27 2011 -0700 14.2 +++ b/src/share/vm/opto/parse1.cpp Thu Mar 24 23:04:36 2011 -0700 14.3 @@ -637,6 +637,25 @@ 14.4 // (Note that dead locals do not get phis built, ever.) 14.5 ensure_phis_everywhere(); 14.6 14.7 + if (block->is_SEL_head() && 14.8 + UseLoopPredicate) { 14.9 + // Add predicate to single entry (not irreducible) loop head. 14.10 + assert(!block->has_merged_backedge(), "only entry paths should be merged for now"); 14.11 + // Need correct bci for predicate. 14.12 + // It is fine to set it here since do_one_block() will set it anyway. 14.13 + set_parse_bci(block->start()); 14.14 + add_predicate(); 14.15 + // Add new region for back branches. 14.16 + int edges = block->pred_count() - block->preds_parsed() + 1; // +1 for original region 14.17 + RegionNode *r = new (C, edges+1) RegionNode(edges+1); 14.18 + _gvn.set_type(r, Type::CONTROL); 14.19 + record_for_igvn(r); 14.20 + r->init_req(edges, control()); 14.21 + set_control(r); 14.22 + // Add new phis. 14.23 + ensure_phis_everywhere(); 14.24 + } 14.25 + 14.26 // Leave behind an undisturbed copy of the map, for future merges. 14.27 set_map(clone_map()); 14.28 } 14.29 @@ -1113,7 +1132,7 @@ 14.30 _preds_parsed = 0; 14.31 _count = 0; 14.32 assert(pred_count() == 0 && preds_parsed() == 0, "sanity"); 14.33 - assert(!(is_merged() || is_parsed() || is_handler()), "sanity"); 14.34 + assert(!(is_merged() || is_parsed() || is_handler() || has_merged_backedge()), "sanity"); 14.35 assert(_live_locals.size() == 0, "sanity"); 14.36 14.37 // entry point has additional predecessor 14.38 @@ -1350,10 +1369,6 @@ 14.39 set_parse_bci(iter().cur_bci()); 14.40 14.41 if (bci() == block()->limit()) { 14.42 - // insert a predicate if it falls through to a loop head block 14.43 - if (should_add_predicate(bci())){ 14.44 - add_predicate(); 14.45 - } 14.46 // Do not walk into the next block until directed by do_all_blocks. 14.47 merge(bci()); 14.48 break; 14.49 @@ -1498,17 +1513,29 @@ 14.50 || target->is_handler() // These have unpredictable inputs. 14.51 || target->is_loop_head() // Known multiple inputs 14.52 || control()->is_Region()) { // We must hide this guy. 14.53 + 14.54 + int current_bci = bci(); 14.55 + set_parse_bci(target->start()); // Set target bci 14.56 + if (target->is_SEL_head()) { 14.57 + DEBUG_ONLY( target->mark_merged_backedge(block()); ) 14.58 + if (target->start() == 0) { 14.59 + // Add loop predicate for the special case when 14.60 + // there are backbranches to the method entry. 14.61 + add_predicate(); 14.62 + } 14.63 + } 14.64 // Add a Region to start the new basic block. Phis will be added 14.65 // later lazily. 14.66 int edges = target->pred_count(); 14.67 if (edges < pnum) edges = pnum; // might be a new path! 14.68 - Node *r = new (C, edges+1) RegionNode(edges+1); 14.69 + RegionNode *r = new (C, edges+1) RegionNode(edges+1); 14.70 gvn().set_type(r, Type::CONTROL); 14.71 record_for_igvn(r); 14.72 // zap all inputs to NULL for debugging (done in Node(uint) constructor) 14.73 // for (int j = 1; j < edges+1; j++) { r->init_req(j, NULL); } 14.74 r->init_req(pnum, control()); 14.75 set_control(r); 14.76 + set_parse_bci(current_bci); // Restore bci 14.77 } 14.78 14.79 // Convert the existing Parser mapping into a mapping at this bci. 14.80 @@ -1517,7 +1544,11 @@ 14.81 14.82 } else { // Prior mapping at this bci 14.83 if (TraceOptoParse) { tty->print(" with previous state"); } 14.84 - 14.85 +#ifdef ASSERT 14.86 + if (target->is_SEL_head()) { 14.87 + target->mark_merged_backedge(block()); 14.88 + } 14.89 +#endif 14.90 // We must not manufacture more phis if the target is already parsed. 14.91 bool nophi = target->is_parsed(); 14.92 14.93 @@ -2054,37 +2085,6 @@ 14.94 } 14.95 } 14.96 14.97 -//------------------------------should_add_predicate-------------------------- 14.98 -bool Parse::should_add_predicate(int target_bci) { 14.99 - if (!UseLoopPredicate) return false; 14.100 - Block* target = successor_for_bci(target_bci); 14.101 - if (target != NULL && 14.102 - target->is_loop_head() && 14.103 - block()->rpo() < target->rpo()) { 14.104 - return true; 14.105 - } 14.106 - return false; 14.107 -} 14.108 - 14.109 -//------------------------------add_predicate--------------------------------- 14.110 -void Parse::add_predicate() { 14.111 - assert(UseLoopPredicate,"use only for loop predicate"); 14.112 - Node *cont = _gvn.intcon(1); 14.113 - Node* opq = _gvn.transform(new (C, 2) Opaque1Node(C, cont)); 14.114 - Node *bol = _gvn.transform(new (C, 2) Conv2BNode(opq)); 14.115 - IfNode* iff = create_and_map_if(control(), bol, PROB_MAX, COUNT_UNKNOWN); 14.116 - Node* iffalse = _gvn.transform(new (C, 1) IfFalseNode(iff)); 14.117 - C->add_predicate_opaq(opq); 14.118 - { 14.119 - PreserveJVMState pjvms(this); 14.120 - set_control(iffalse); 14.121 - uncommon_trap(Deoptimization::Reason_predicate, 14.122 - Deoptimization::Action_maybe_recompile); 14.123 - } 14.124 - Node* iftrue = _gvn.transform(new (C, 1) IfTrueNode(iff)); 14.125 - set_control(iftrue); 14.126 -} 14.127 - 14.128 #ifndef PRODUCT 14.129 //------------------------show_parse_info-------------------------------------- 14.130 void Parse::show_parse_info() {
15.1 --- a/src/share/vm/opto/parse2.cpp Thu Mar 24 23:00:27 2011 -0700 15.2 +++ b/src/share/vm/opto/parse2.cpp Thu Mar 24 23:04:36 2011 -0700 15.3 @@ -293,11 +293,6 @@ 15.4 if (len < 1) { 15.5 // If this is a backward branch, add safepoint 15.6 maybe_add_safepoint(default_dest); 15.7 - if (should_add_predicate(default_dest)){ 15.8 - _sp += 1; // set original stack for use by uncommon_trap 15.9 - add_predicate(); 15.10 - _sp -= 1; 15.11 - } 15.12 merge(default_dest); 15.13 return; 15.14 } 15.15 @@ -344,11 +339,6 @@ 15.16 15.17 if (len < 1) { // If this is a backward branch, add safepoint 15.18 maybe_add_safepoint(default_dest); 15.19 - if (should_add_predicate(default_dest)){ 15.20 - _sp += 1; // set original stack for use by uncommon_trap 15.21 - add_predicate(); 15.22 - _sp -= 1; 15.23 - } 15.24 merge(default_dest); 15.25 return; 15.26 } 15.27 @@ -756,9 +746,6 @@ 15.28 push(_gvn.makecon(ret_addr)); 15.29 15.30 // Flow to the jsr. 15.31 - if (should_add_predicate(jsr_bci)){ 15.32 - add_predicate(); 15.33 - } 15.34 merge(jsr_bci); 15.35 } 15.36 15.37 @@ -1040,11 +1027,6 @@ 15.38 profile_taken_branch(target_bci); 15.39 adjust_map_after_if(btest, c, prob, branch_block, next_block); 15.40 if (!stopped()) { 15.41 - if (should_add_predicate(target_bci)){ // add a predicate if it branches to a loop 15.42 - int nargs = repush_if_args(); // set original stack for uncommon_trap 15.43 - add_predicate(); 15.44 - _sp -= nargs; 15.45 - } 15.46 merge(target_bci); 15.47 } 15.48 } 15.49 @@ -1168,11 +1150,6 @@ 15.50 profile_taken_branch(target_bci); 15.51 adjust_map_after_if(taken_btest, c, prob, branch_block, next_block); 15.52 if (!stopped()) { 15.53 - if (should_add_predicate(target_bci)){ // add a predicate if it branches to a loop 15.54 - int nargs = repush_if_args(); // set original stack for the uncommon_trap 15.55 - add_predicate(); 15.56 - _sp -= nargs; 15.57 - } 15.58 merge(target_bci); 15.59 } 15.60 } 15.61 @@ -2166,10 +2143,6 @@ 15.62 // Update method data 15.63 profile_taken_branch(target_bci); 15.64 15.65 - // Add loop predicate if it goes to a loop 15.66 - if (should_add_predicate(target_bci)){ 15.67 - add_predicate(); 15.68 - } 15.69 // Merge the current control into the target basic block 15.70 merge(target_bci); 15.71
16.1 --- a/src/share/vm/opto/stringopts.cpp Thu Mar 24 23:00:27 2011 -0700 16.2 +++ b/src/share/vm/opto/stringopts.cpp Thu Mar 24 23:04:36 2011 -0700 16.3 @@ -969,6 +969,10 @@ 16.4 // for (int i=0; ; i++) 16.5 // if (x <= sizeTable[i]) 16.6 // return i+1; 16.7 + 16.8 + // Add loop predicate first. 16.9 + kit.add_predicate(); 16.10 + 16.11 RegionNode *loop = new (C, 3) RegionNode(3); 16.12 loop->init_req(1, kit.control()); 16.13 kit.gvn().set_type(loop, Type::CONTROL); 16.14 @@ -1086,6 +1090,9 @@ 16.15 // } 16.16 16.17 { 16.18 + // Add loop predicate first. 16.19 + kit.add_predicate(); 16.20 + 16.21 RegionNode *head = new (C, 3) RegionNode(3); 16.22 head->init_req(1, kit.control()); 16.23 kit.gvn().set_type(head, Type::CONTROL);
17.1 --- a/test/compiler/6987555/Test6987555.java Thu Mar 24 23:00:27 2011 -0700 17.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 17.3 @@ -1,177 +0,0 @@ 17.4 -/* 17.5 - * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. 17.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 17.7 - * 17.8 - * This code is free software; you can redistribute it and/or modify it 17.9 - * under the terms of the GNU General Public License version 2 only, as 17.10 - * published by the Free Software Foundation. 17.11 - * 17.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 17.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 17.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 17.15 - * version 2 for more details (a copy is included in the LICENSE file that 17.16 - * accompanied this code). 17.17 - * 17.18 - * You should have received a copy of the GNU General Public License version 17.19 - * 2 along with this work; if not, write to the Free Software Foundation, 17.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17.21 - * 17.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 17.23 - * or visit www.oracle.com if you need additional information or have any 17.24 - * questions. 17.25 - * 17.26 - */ 17.27 - 17.28 -/** 17.29 - * @test 17.30 - * @bug 6987555 17.31 - * @summary JSR 292 unboxing to a boolean value fails on big-endian SPARC 17.32 - * 17.33 - * @run main/othervm -Xint -ea -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles -XX:+EnableInvokeDynamic -XX:+UnlockDiagnosticVMOptions -XX:+VerifyMethodHandles Test6987555 17.34 - */ 17.35 - 17.36 -import java.dyn.*; 17.37 - 17.38 -public class Test6987555 { 17.39 - private static final Class CLASS = Test6987555.class; 17.40 - private static final String NAME = "foo"; 17.41 - private static final boolean DEBUG = false; 17.42 - 17.43 - public static void main(String[] args) throws Throwable { 17.44 - testboolean(); 17.45 - testbyte(); 17.46 - testchar(); 17.47 - testshort(); 17.48 - testint(); 17.49 - } 17.50 - 17.51 - // boolean 17.52 - static void testboolean() throws Throwable { 17.53 - doboolean(false); 17.54 - doboolean(true); 17.55 - } 17.56 - static void doboolean(boolean x) throws Throwable { 17.57 - if (DEBUG) System.out.println("boolean=" + x); 17.58 - MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(boolean.class, boolean.class)); 17.59 - MethodHandle mh2 = mh1.asType(MethodType.methodType(boolean.class, Boolean.class)); 17.60 - boolean a = (boolean) mh1.invokeExact(x); 17.61 - boolean b = (boolean) mh2.invokeExact(Boolean.valueOf(x)); 17.62 - assert a == b : a + " != " + b; 17.63 - } 17.64 - 17.65 - // byte 17.66 - static void testbyte() throws Throwable { 17.67 - byte[] a = new byte[] { 17.68 - Byte.MIN_VALUE, 17.69 - Byte.MIN_VALUE + 1, 17.70 - -0x0F, 17.71 - -1, 17.72 - 0, 17.73 - 1, 17.74 - 0x0F, 17.75 - Byte.MAX_VALUE - 1, 17.76 - Byte.MAX_VALUE 17.77 - }; 17.78 - for (int i = 0; i < a.length; i++) { 17.79 - dobyte(a[i]); 17.80 - } 17.81 - } 17.82 - static void dobyte(byte x) throws Throwable { 17.83 - if (DEBUG) System.out.println("byte=" + x); 17.84 - MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(byte.class, byte.class)); 17.85 - MethodHandle mh2 = mh1.asType(MethodType.methodType(byte.class, Byte.class)); 17.86 - byte a = (byte) mh1.invokeExact(x); 17.87 - byte b = (byte) mh2.invokeExact(Byte.valueOf(x)); 17.88 - assert a == b : a + " != " + b; 17.89 - } 17.90 - 17.91 - // char 17.92 - static void testchar() throws Throwable { 17.93 - char[] a = new char[] { 17.94 - Character.MIN_VALUE, 17.95 - Character.MIN_VALUE + 1, 17.96 - 0x000F, 17.97 - 0x00FF, 17.98 - 0x0FFF, 17.99 - Character.MAX_VALUE - 1, 17.100 - Character.MAX_VALUE 17.101 - }; 17.102 - for (int i = 0; i < a.length; i++) { 17.103 - dochar(a[i]); 17.104 - } 17.105 - } 17.106 - static void dochar(char x) throws Throwable { 17.107 - if (DEBUG) System.out.println("char=" + x); 17.108 - MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(char.class, char.class)); 17.109 - MethodHandle mh2 = mh1.asType(MethodType.methodType(char.class, Character.class)); 17.110 - char a = (char) mh1.invokeExact(x); 17.111 - char b = (char) mh2.invokeExact(Character.valueOf(x)); 17.112 - assert a == b : a + " != " + b; 17.113 - } 17.114 - 17.115 - // short 17.116 - static void testshort() throws Throwable { 17.117 - short[] a = new short[] { 17.118 - Short.MIN_VALUE, 17.119 - Short.MIN_VALUE + 1, 17.120 - -0x0FFF, 17.121 - -0x00FF, 17.122 - -0x000F, 17.123 - -1, 17.124 - 0, 17.125 - 1, 17.126 - 0x000F, 17.127 - 0x00FF, 17.128 - 0x0FFF, 17.129 - Short.MAX_VALUE - 1, 17.130 - Short.MAX_VALUE 17.131 - }; 17.132 - for (int i = 0; i < a.length; i++) { 17.133 - doshort(a[i]); 17.134 - } 17.135 - } 17.136 - static void doshort(short x) throws Throwable { 17.137 - if (DEBUG) System.out.println("short=" + x); 17.138 - MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(short.class, short.class)); 17.139 - MethodHandle mh2 = mh1.asType(MethodType.methodType(short.class, Short.class)); 17.140 - short a = (short) mh1.invokeExact(x); 17.141 - short b = (short) mh2.invokeExact(Short.valueOf(x)); 17.142 - assert a == b : a + " != " + b; 17.143 - } 17.144 - 17.145 - // int 17.146 - static void testint() throws Throwable { 17.147 - int[] a = new int[] { 17.148 - Integer.MIN_VALUE, 17.149 - Integer.MIN_VALUE + 1, 17.150 - -0x00000FFF, 17.151 - -0x000000FF, 17.152 - -0x0000000F, 17.153 - -1, 17.154 - 0, 17.155 - 1, 17.156 - 0x0000000F, 17.157 - 0x000000FF, 17.158 - 0x00000FFF, 17.159 - Integer.MAX_VALUE - 1, 17.160 - Integer.MAX_VALUE 17.161 - }; 17.162 - for (int i = 0; i < a.length; i++) { 17.163 - doint(a[i]); 17.164 - } 17.165 - } 17.166 - static void doint(int x) throws Throwable { 17.167 - if (DEBUG) System.out.println("int=" + x); 17.168 - MethodHandle mh1 = MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(int.class, int.class)); 17.169 - MethodHandle mh2 = mh1.asType(MethodType.methodType(int.class, Integer.class)); 17.170 - int a = (int) mh1.invokeExact(x); 17.171 - int b = (int) mh2.invokeExact(Integer.valueOf(x)); 17.172 - assert a == b : a + " != " + b; 17.173 - } 17.174 - 17.175 - public static boolean foo(boolean i) { return i; } 17.176 - public static byte foo(byte i) { return i; } 17.177 - public static char foo(char i) { return i; } 17.178 - public static short foo(short i) { return i; } 17.179 - public static int foo(int i) { return i; } 17.180 -}
18.1 --- a/test/compiler/6991596/Test6991596.java Thu Mar 24 23:00:27 2011 -0700 18.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 18.3 @@ -1,465 +0,0 @@ 18.4 -/* 18.5 - * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 18.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 18.7 - * 18.8 - * This code is free software; you can redistribute it and/or modify it 18.9 - * under the terms of the GNU General Public License version 2 only, as 18.10 - * published by the Free Software Foundation. 18.11 - * 18.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 18.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 18.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 18.15 - * version 2 for more details (a copy is included in the LICENSE file that 18.16 - * accompanied this code). 18.17 - * 18.18 - * You should have received a copy of the GNU General Public License version 18.19 - * 2 along with this work; if not, write to the Free Software Foundation, 18.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18.21 - * 18.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 18.23 - * or visit www.oracle.com if you need additional information or have any 18.24 - * questions. 18.25 - * 18.26 - */ 18.27 - 18.28 -/** 18.29 - * @test 18.30 - * @bug 6991596 18.31 - * @summary JSR 292 unimplemented adapter_opt_i2i and adapter_opt_l2i on SPARC 18.32 - * 18.33 - * @run main/othervm -ea -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles -XX:+EnableInvokeDynamic -XX:+UnlockDiagnosticVMOptions -XX:+VerifyMethodHandles Test6991596 18.34 - */ 18.35 - 18.36 -import java.dyn.*; 18.37 - 18.38 -public class Test6991596 { 18.39 - private static final Class CLASS = Test6991596.class; 18.40 - private static final String NAME = "foo"; 18.41 - private static final boolean DEBUG = System.getProperty("DEBUG", "false").equals("true"); 18.42 - 18.43 - public static void main(String[] args) throws Throwable { 18.44 - testboolean(); 18.45 - testbyte(); 18.46 - testchar(); 18.47 - testshort(); 18.48 - testint(); 18.49 - testlong(); 18.50 - } 18.51 - 18.52 - // Helpers to get various methods. 18.53 - static MethodHandle getmh1(Class ret, Class arg) throws NoAccessException { 18.54 - return MethodHandles.lookup().findStatic(CLASS, NAME, MethodType.methodType(ret, arg)); 18.55 - } 18.56 - static MethodHandle getmh2(MethodHandle mh1, Class ret, Class arg) { 18.57 - return MethodHandles.convertArguments(mh1, MethodType.methodType(ret, arg)); 18.58 - } 18.59 - static MethodHandle getmh3(MethodHandle mh1, Class ret, Class arg) { 18.60 - return MethodHandles.convertArguments(mh1, MethodType.methodType(ret, arg)); 18.61 - } 18.62 - 18.63 - // test adapter_opt_i2i 18.64 - static void testboolean() throws Throwable { 18.65 - boolean[] a = new boolean[] { 18.66 - true, 18.67 - false 18.68 - }; 18.69 - for (int i = 0; i < a.length; i++) { 18.70 - doboolean(a[i]); 18.71 - } 18.72 - } 18.73 - static void doboolean(boolean x) throws Throwable { 18.74 - if (DEBUG) System.out.println("boolean=" + x); 18.75 - 18.76 - // boolean 18.77 - { 18.78 - MethodHandle mh1 = getmh1( boolean.class, boolean.class); 18.79 - MethodHandle mh2 = getmh2(mh1, boolean.class, boolean.class); 18.80 - // TODO add this for all cases when the bugs are fixed. 18.81 - //MethodHandle mh3 = getmh3(mh1, boolean.class, boolean.class); 18.82 - boolean a = (boolean) mh1.invokeExact((boolean) x); 18.83 - boolean b = (boolean) mh2.invokeExact(x); 18.84 - //boolean c = mh3.<boolean>invokeExact((boolean) x); 18.85 - check(x, a, b); 18.86 - //check(x, c, x); 18.87 - } 18.88 - 18.89 - // byte 18.90 - { 18.91 - MethodHandle mh1 = getmh1( byte.class, byte.class ); 18.92 - MethodHandle mh2 = getmh2(mh1, byte.class, boolean.class); 18.93 - byte a = (byte) mh1.invokeExact((byte) (x ? 1 : 0)); 18.94 - byte b = (byte) mh2.invokeExact(x); 18.95 - check(x, a, b); 18.96 - } 18.97 - 18.98 - // char 18.99 - { 18.100 - MethodHandle mh1 = getmh1( char.class, char.class); 18.101 - MethodHandle mh2 = getmh2(mh1, char.class, boolean.class); 18.102 - char a = (char) mh1.invokeExact((char) (x ? 1 : 0)); 18.103 - char b = (char) mh2.invokeExact(x); 18.104 - check(x, a, b); 18.105 - } 18.106 - 18.107 - // short 18.108 - { 18.109 - MethodHandle mh1 = getmh1( short.class, short.class); 18.110 - MethodHandle mh2 = getmh2(mh1, short.class, boolean.class); 18.111 - short a = (short) mh1.invokeExact((short) (x ? 1 : 0)); 18.112 - short b = (short) mh2.invokeExact(x); 18.113 - check(x, a, b); 18.114 - } 18.115 - } 18.116 - 18.117 - static void testbyte() throws Throwable { 18.118 - byte[] a = new byte[] { 18.119 - Byte.MIN_VALUE, 18.120 - Byte.MIN_VALUE + 1, 18.121 - -0x0F, 18.122 - -1, 18.123 - 0, 18.124 - 1, 18.125 - 0x0F, 18.126 - Byte.MAX_VALUE - 1, 18.127 - Byte.MAX_VALUE 18.128 - }; 18.129 - for (int i = 0; i < a.length; i++) { 18.130 - dobyte(a[i]); 18.131 - } 18.132 - } 18.133 - static void dobyte(byte x) throws Throwable { 18.134 - if (DEBUG) System.out.println("byte=" + x); 18.135 - 18.136 - // boolean 18.137 - { 18.138 - MethodHandle mh1 = getmh1( boolean.class, boolean.class); 18.139 - MethodHandle mh2 = getmh2(mh1, boolean.class, byte.class); 18.140 - boolean a = (boolean) mh1.invokeExact((x & 1) == 1); 18.141 - boolean b = (boolean) mh2.invokeExact(x); 18.142 - check(x, a, b); 18.143 - } 18.144 - 18.145 - // byte 18.146 - { 18.147 - MethodHandle mh1 = getmh1( byte.class, byte.class); 18.148 - MethodHandle mh2 = getmh2(mh1, byte.class, byte.class); 18.149 - byte a = (byte) mh1.invokeExact((byte) x); 18.150 - byte b = (byte) mh2.invokeExact(x); 18.151 - check(x, a, b); 18.152 - } 18.153 - 18.154 - // char 18.155 - { 18.156 - MethodHandle mh1 = getmh1( char.class, char.class); 18.157 - MethodHandle mh2 = getmh2(mh1, char.class, byte.class); 18.158 - char a = (char) mh1.invokeExact((char) x); 18.159 - char b = (char) mh2.invokeExact(x); 18.160 - check(x, a, b); 18.161 - } 18.162 - 18.163 - // short 18.164 - { 18.165 - MethodHandle mh1 = getmh1( short.class, short.class); 18.166 - MethodHandle mh2 = getmh2(mh1, short.class, byte.class); 18.167 - short a = (short) mh1.invokeExact((short) x); 18.168 - short b = (short) mh2.invokeExact(x); 18.169 - check(x, a, b); 18.170 - } 18.171 - } 18.172 - 18.173 - static void testchar() throws Throwable { 18.174 - char[] a = new char[] { 18.175 - Character.MIN_VALUE, 18.176 - Character.MIN_VALUE + 1, 18.177 - 0x000F, 18.178 - 0x00FF, 18.179 - 0x0FFF, 18.180 - Character.MAX_VALUE - 1, 18.181 - Character.MAX_VALUE 18.182 - }; 18.183 - for (int i = 0; i < a.length; i++) { 18.184 - dochar(a[i]); 18.185 - } 18.186 - } 18.187 - static void dochar(char x) throws Throwable { 18.188 - if (DEBUG) System.out.println("char=" + x); 18.189 - 18.190 - // boolean 18.191 - { 18.192 - MethodHandle mh1 = getmh1( boolean.class, boolean.class); 18.193 - MethodHandle mh2 = getmh2(mh1, boolean.class, char.class); 18.194 - boolean a = (boolean) mh1.invokeExact((x & 1) == 1); 18.195 - boolean b = (boolean) mh2.invokeExact(x); 18.196 - check(x, a, b); 18.197 - } 18.198 - 18.199 - // byte 18.200 - { 18.201 - MethodHandle mh1 = getmh1( byte.class, byte.class); 18.202 - MethodHandle mh2 = getmh2(mh1, byte.class, char.class); 18.203 - byte a = (byte) mh1.invokeExact((byte) x); 18.204 - byte b = (byte) mh2.invokeExact(x); 18.205 - check(x, a, b); 18.206 - } 18.207 - 18.208 - // char 18.209 - { 18.210 - MethodHandle mh1 = getmh1( char.class, char.class); 18.211 - MethodHandle mh2 = getmh2(mh1, char.class, char.class); 18.212 - char a = (char) mh1.invokeExact((char) x); 18.213 - char b = (char) mh2.invokeExact(x); 18.214 - check(x, a, b); 18.215 - } 18.216 - 18.217 - // short 18.218 - { 18.219 - MethodHandle mh1 = getmh1( short.class, short.class); 18.220 - MethodHandle mh2 = getmh2(mh1, short.class, char.class); 18.221 - short a = (short) mh1.invokeExact((short) x); 18.222 - short b = (short) mh2.invokeExact(x); 18.223 - check(x, a, b); 18.224 - } 18.225 - } 18.226 - 18.227 - static void testshort() throws Throwable { 18.228 - short[] a = new short[] { 18.229 - Short.MIN_VALUE, 18.230 - Short.MIN_VALUE + 1, 18.231 - -0x0FFF, 18.232 - -0x00FF, 18.233 - -0x000F, 18.234 - -1, 18.235 - 0, 18.236 - 1, 18.237 - 0x000F, 18.238 - 0x00FF, 18.239 - 0x0FFF, 18.240 - Short.MAX_VALUE - 1, 18.241 - Short.MAX_VALUE 18.242 - }; 18.243 - for (int i = 0; i < a.length; i++) { 18.244 - doshort(a[i]); 18.245 - } 18.246 - } 18.247 - static void doshort(short x) throws Throwable { 18.248 - if (DEBUG) System.out.println("short=" + x); 18.249 - 18.250 - // boolean 18.251 - { 18.252 - MethodHandle mh1 = getmh1( boolean.class, boolean.class); 18.253 - MethodHandle mh2 = getmh2(mh1, boolean.class, short.class); 18.254 - boolean a = (boolean) mh1.invokeExact((x & 1) == 1); 18.255 - boolean b = (boolean) mh2.invokeExact(x); 18.256 - check(x, a, b); 18.257 - } 18.258 - 18.259 - // byte 18.260 - { 18.261 - MethodHandle mh1 = getmh1( byte.class, byte.class); 18.262 - MethodHandle mh2 = getmh2(mh1, byte.class, short.class); 18.263 - byte a = (byte) mh1.invokeExact((byte) x); 18.264 - byte b = (byte) mh2.invokeExact(x); 18.265 - check(x, a, b); 18.266 - } 18.267 - 18.268 - // char 18.269 - { 18.270 - MethodHandle mh1 = getmh1( char.class, char.class); 18.271 - MethodHandle mh2 = getmh2(mh1, char.class, short.class); 18.272 - char a = (char) mh1.invokeExact((char) x); 18.273 - char b = (char) mh2.invokeExact(x); 18.274 - check(x, a, b); 18.275 - } 18.276 - 18.277 - // short 18.278 - { 18.279 - MethodHandle mh1 = getmh1( short.class, short.class); 18.280 - MethodHandle mh2 = getmh2(mh1, short.class, short.class); 18.281 - short a = (short) mh1.invokeExact((short) x); 18.282 - short b = (short) mh2.invokeExact(x); 18.283 - check(x, a, b); 18.284 - } 18.285 - } 18.286 - 18.287 - static void testint() throws Throwable { 18.288 - int[] a = new int[] { 18.289 - Integer.MIN_VALUE, 18.290 - Integer.MIN_VALUE + 1, 18.291 - -0x0FFFFFFF, 18.292 - -0x00FFFFFF, 18.293 - -0x000FFFFF, 18.294 - -0x0000FFFF, 18.295 - -0x00000FFF, 18.296 - -0x000000FF, 18.297 - -0x0000000F, 18.298 - -1, 18.299 - 0, 18.300 - 1, 18.301 - 0x0000000F, 18.302 - 0x000000FF, 18.303 - 0x00000FFF, 18.304 - 0x0000FFFF, 18.305 - 0x000FFFFF, 18.306 - 0x00FFFFFF, 18.307 - 0x0FFFFFFF, 18.308 - Integer.MAX_VALUE - 1, 18.309 - Integer.MAX_VALUE 18.310 - }; 18.311 - for (int i = 0; i < a.length; i++) { 18.312 - doint(a[i]); 18.313 - } 18.314 - } 18.315 - static void doint(int x) throws Throwable { 18.316 - if (DEBUG) System.out.println("int=" + x); 18.317 - 18.318 - // boolean 18.319 - { 18.320 - MethodHandle mh1 = getmh1( boolean.class, boolean.class); 18.321 - MethodHandle mh2 = getmh2(mh1, boolean.class, int.class); 18.322 - boolean a = (boolean) mh1.invokeExact((x & 1) == 1); 18.323 - boolean b = (boolean) mh2.invokeExact(x); 18.324 - check(x, a, b); 18.325 - } 18.326 - 18.327 - // byte 18.328 - { 18.329 - MethodHandle mh1 = getmh1( byte.class, byte.class); 18.330 - MethodHandle mh2 = getmh2(mh1, byte.class, int.class); 18.331 - byte a = (byte) mh1.invokeExact((byte) x); 18.332 - byte b = (byte) mh2.invokeExact(x); 18.333 - check(x, a, b); 18.334 - } 18.335 - 18.336 - // char 18.337 - { 18.338 - MethodHandle mh1 = getmh1( char.class, char.class); 18.339 - MethodHandle mh2 = getmh2(mh1, char.class, int.class); 18.340 - char a = (char) mh1.invokeExact((char) x); 18.341 - char b = (char) mh2.invokeExact(x); 18.342 - check(x, a, b); 18.343 - } 18.344 - 18.345 - // short 18.346 - { 18.347 - MethodHandle mh1 = getmh1( short.class, short.class); 18.348 - MethodHandle mh2 = getmh2(mh1, short.class, int.class); 18.349 - short a = (short) mh1.invokeExact((short) x); 18.350 - short b = (short) mh2.invokeExact(x); 18.351 - assert a == b : a + " != " + b; 18.352 - check(x, a, b); 18.353 - } 18.354 - 18.355 - // int 18.356 - { 18.357 - MethodHandle mh1 = getmh1( int.class, int.class); 18.358 - MethodHandle mh2 = getmh2(mh1, int.class, int.class); 18.359 - int a = (int) mh1.invokeExact((int) x); 18.360 - int b = (int) mh2.invokeExact(x); 18.361 - check(x, a, b); 18.362 - } 18.363 - } 18.364 - 18.365 - // test adapter_opt_l2i 18.366 - static void testlong() throws Throwable { 18.367 - long[] a = new long[] { 18.368 - Long.MIN_VALUE, 18.369 - Long.MIN_VALUE + 1, 18.370 - -0x000000000FFFFFFFL, 18.371 - -0x0000000000FFFFFFL, 18.372 - -0x00000000000FFFFFL, 18.373 - -0x000000000000FFFFL, 18.374 - -0x0000000000000FFFL, 18.375 - -0x00000000000000FFL, 18.376 - -0x000000000000000FL, 18.377 - -1L, 18.378 - 0L, 18.379 - 1L, 18.380 - 0x000000000000000FL, 18.381 - 0x00000000000000FFL, 18.382 - 0x0000000000000FFFL, 18.383 - 0x0000000000000FFFL, 18.384 - 0x000000000000FFFFL, 18.385 - 0x00000000000FFFFFL, 18.386 - 0x0000000000FFFFFFL, 18.387 - 0x000000000FFFFFFFL, 18.388 - Long.MAX_VALUE - 1, 18.389 - Long.MAX_VALUE 18.390 - }; 18.391 - for (int i = 0; i < a.length; i++) { 18.392 - dolong(a[i]); 18.393 - } 18.394 - } 18.395 - static void dolong(long x) throws Throwable { 18.396 - if (DEBUG) System.out.println("long=" + x); 18.397 - 18.398 - // boolean 18.399 - { 18.400 - MethodHandle mh1 = getmh1( boolean.class, boolean.class); 18.401 - MethodHandle mh2 = getmh2(mh1, boolean.class, long.class); 18.402 - boolean a = (boolean) mh1.invokeExact((x & 1L) == 1L); 18.403 - boolean b = (boolean) mh2.invokeExact(x); 18.404 - check(x, a, b); 18.405 - } 18.406 - 18.407 - // byte 18.408 - { 18.409 - MethodHandle mh1 = getmh1( byte.class, byte.class); 18.410 - MethodHandle mh2 = getmh2(mh1, byte.class, long.class); 18.411 - byte a = (byte) mh1.invokeExact((byte) x); 18.412 - byte b = (byte) mh2.invokeExact(x); 18.413 - check(x, a, b); 18.414 - } 18.415 - 18.416 - // char 18.417 - { 18.418 - MethodHandle mh1 = getmh1( char.class, char.class); 18.419 - MethodHandle mh2 = getmh2(mh1, char.class, long.class); 18.420 - char a = (char) mh1.invokeExact((char) x); 18.421 - char b = (char) mh2.invokeExact(x); 18.422 - check(x, a, b); 18.423 - } 18.424 - 18.425 - // short 18.426 - { 18.427 - MethodHandle mh1 = getmh1( short.class, short.class); 18.428 - MethodHandle mh2 = getmh2(mh1, short.class, long.class); 18.429 - short a = (short) mh1.invokeExact((short) x); 18.430 - short b = (short) mh2.invokeExact(x); 18.431 - check(x, a, b); 18.432 - } 18.433 - 18.434 - // int 18.435 - { 18.436 - MethodHandle mh1 = getmh1( int.class, int.class); 18.437 - MethodHandle mh2 = getmh2(mh1, int.class, long.class); 18.438 - int a = (int) mh1.invokeExact((int) x); 18.439 - int b = (int) mh2.invokeExact(x); 18.440 - check(x, a, b); 18.441 - } 18.442 - } 18.443 - 18.444 - static void check(boolean x, boolean e, boolean a) { p(z2h(x), z2h(e), z2h(a)); assert e == a : z2h(x) + ": " + z2h(e) + " != " + z2h(a); } 18.445 - static void check(boolean x, byte e, byte a) { p(z2h(x), i2h(e), i2h(a)); assert e == a : z2h(x) + ": " + i2h(e) + " != " + i2h(a); } 18.446 - static void check(boolean x, int e, int a) { p(z2h(x), i2h(e), i2h(a)); assert e == a : z2h(x) + ": " + i2h(e) + " != " + i2h(a); } 18.447 - 18.448 - static void check(int x, boolean e, boolean a) { p(i2h(x), z2h(e), z2h(a)); assert e == a : i2h(x) + ": " + z2h(e) + " != " + z2h(a); } 18.449 - static void check(int x, byte e, byte a) { p(i2h(x), i2h(e), i2h(a)); assert e == a : i2h(x) + ": " + i2h(e) + " != " + i2h(a); } 18.450 - static void check(int x, int e, int a) { p(i2h(x), i2h(e), i2h(a)); assert e == a : i2h(x) + ": " + i2h(e) + " != " + i2h(a); } 18.451 - 18.452 - static void check(long x, boolean e, boolean a) { p(l2h(x), z2h(e), z2h(a)); assert e == a : l2h(x) + ": " + z2h(e) + " != " + z2h(a); } 18.453 - static void check(long x, byte e, byte a) { p(l2h(x), i2h(e), i2h(a)); assert e == a : l2h(x) + ": " + i2h(e) + " != " + i2h(a); } 18.454 - static void check(long x, int e, int a) { p(l2h(x), i2h(e), i2h(a)); assert e == a : l2h(x) + ": " + i2h(e) + " != " + i2h(a); } 18.455 - 18.456 - static void p(String x, String e, String a) { if (DEBUG) System.out.println(x + ": expected: " + e + ", actual: " + a); } 18.457 - 18.458 - static String z2h(boolean x) { return x ? "1" : "0"; } 18.459 - static String i2h(int x) { return Integer.toHexString(x); } 18.460 - static String l2h(long x) { return Long.toHexString(x); } 18.461 - 18.462 - // to int 18.463 - public static boolean foo(boolean i) { return i; } 18.464 - public static byte foo(byte i) { return i; } 18.465 - public static char foo(char i) { return i; } 18.466 - public static short foo(short i) { return i; } 18.467 - public static int foo(int i) { return i; } 18.468 -}