1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/opto/loopTransform.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,1729 @@ 1.4 +/* 1.5 + * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "incls/_precompiled.incl" 1.29 +#include "incls/_loopTransform.cpp.incl" 1.30 + 1.31 +//------------------------------is_loop_exit----------------------------------- 1.32 +// Given an IfNode, return the loop-exiting projection or NULL if both 1.33 +// arms remain in the loop. 1.34 +Node *IdealLoopTree::is_loop_exit(Node *iff) const { 1.35 + if( iff->outcnt() != 2 ) return NULL; // Ignore partially dead tests 1.36 + PhaseIdealLoop *phase = _phase; 1.37 + // Test is an IfNode, has 2 projections. If BOTH are in the loop 1.38 + // we need loop unswitching instead of peeling. 1.39 + if( !is_member(phase->get_loop( iff->raw_out(0) )) ) 1.40 + return iff->raw_out(0); 1.41 + if( !is_member(phase->get_loop( iff->raw_out(1) )) ) 1.42 + return iff->raw_out(1); 1.43 + return NULL; 1.44 +} 1.45 + 1.46 + 1.47 +//============================================================================= 1.48 + 1.49 + 1.50 +//------------------------------record_for_igvn---------------------------- 1.51 +// Put loop body on igvn work list 1.52 +void IdealLoopTree::record_for_igvn() { 1.53 + for( uint i = 0; i < _body.size(); i++ ) { 1.54 + Node *n = _body.at(i); 1.55 + _phase->_igvn._worklist.push(n); 1.56 + } 1.57 +} 1.58 + 1.59 +//------------------------------compute_profile_trip_cnt---------------------------- 1.60 +// Compute loop trip count from profile data as 1.61 +// (backedge_count + loop_exit_count) / loop_exit_count 1.62 +void IdealLoopTree::compute_profile_trip_cnt( PhaseIdealLoop *phase ) { 1.63 + if (!_head->is_CountedLoop()) { 1.64 + return; 1.65 + } 1.66 + CountedLoopNode* head = _head->as_CountedLoop(); 1.67 + if (head->profile_trip_cnt() != COUNT_UNKNOWN) { 1.68 + return; // Already computed 1.69 + } 1.70 + float trip_cnt = (float)max_jint; // default is big 1.71 + 1.72 + Node* back = head->in(LoopNode::LoopBackControl); 1.73 + while (back != head) { 1.74 + if ((back->Opcode() == Op_IfTrue || back->Opcode() == Op_IfFalse) && 1.75 + back->in(0) && 1.76 + back->in(0)->is_If() && 1.77 + back->in(0)->as_If()->_fcnt != COUNT_UNKNOWN && 1.78 + back->in(0)->as_If()->_prob != PROB_UNKNOWN) { 1.79 + break; 1.80 + } 1.81 + back = phase->idom(back); 1.82 + } 1.83 + if (back != head) { 1.84 + assert((back->Opcode() == Op_IfTrue || back->Opcode() == Op_IfFalse) && 1.85 + back->in(0), "if-projection exists"); 1.86 + IfNode* back_if = back->in(0)->as_If(); 1.87 + float loop_back_cnt = back_if->_fcnt * back_if->_prob; 1.88 + 1.89 + // Now compute a loop exit count 1.90 + float loop_exit_cnt = 0.0f; 1.91 + for( uint i = 0; i < _body.size(); i++ ) { 1.92 + Node *n = _body[i]; 1.93 + if( n->is_If() ) { 1.94 + IfNode *iff = n->as_If(); 1.95 + if( iff->_fcnt != COUNT_UNKNOWN && iff->_prob != PROB_UNKNOWN ) { 1.96 + Node *exit = is_loop_exit(iff); 1.97 + if( exit ) { 1.98 + float exit_prob = iff->_prob; 1.99 + if (exit->Opcode() == Op_IfFalse) exit_prob = 1.0 - exit_prob; 1.100 + if (exit_prob > PROB_MIN) { 1.101 + float exit_cnt = iff->_fcnt * exit_prob; 1.102 + loop_exit_cnt += exit_cnt; 1.103 + } 1.104 + } 1.105 + } 1.106 + } 1.107 + } 1.108 + if (loop_exit_cnt > 0.0f) { 1.109 + trip_cnt = (loop_back_cnt + loop_exit_cnt) / loop_exit_cnt; 1.110 + } else { 1.111 + // No exit count so use 1.112 + trip_cnt = loop_back_cnt; 1.113 + } 1.114 + } 1.115 +#ifndef PRODUCT 1.116 + if (TraceProfileTripCount) { 1.117 + tty->print_cr("compute_profile_trip_cnt lp: %d cnt: %f\n", head->_idx, trip_cnt); 1.118 + } 1.119 +#endif 1.120 + head->set_profile_trip_cnt(trip_cnt); 1.121 +} 1.122 + 1.123 +//---------------------is_invariant_addition----------------------------- 1.124 +// Return nonzero index of invariant operand for an Add or Sub 1.125 +// of (nonconstant) invariant and variant values. Helper for reassoicate_invariants. 1.126 +int IdealLoopTree::is_invariant_addition(Node* n, PhaseIdealLoop *phase) { 1.127 + int op = n->Opcode(); 1.128 + if (op == Op_AddI || op == Op_SubI) { 1.129 + bool in1_invar = this->is_invariant(n->in(1)); 1.130 + bool in2_invar = this->is_invariant(n->in(2)); 1.131 + if (in1_invar && !in2_invar) return 1; 1.132 + if (!in1_invar && in2_invar) return 2; 1.133 + } 1.134 + return 0; 1.135 +} 1.136 + 1.137 +//---------------------reassociate_add_sub----------------------------- 1.138 +// Reassociate invariant add and subtract expressions: 1.139 +// 1.140 +// inv1 + (x + inv2) => ( inv1 + inv2) + x 1.141 +// (x + inv2) + inv1 => ( inv1 + inv2) + x 1.142 +// inv1 + (x - inv2) => ( inv1 - inv2) + x 1.143 +// inv1 - (inv2 - x) => ( inv1 - inv2) + x 1.144 +// (x + inv2) - inv1 => (-inv1 + inv2) + x 1.145 +// (x - inv2) + inv1 => ( inv1 - inv2) + x 1.146 +// (x - inv2) - inv1 => (-inv1 - inv2) + x 1.147 +// inv1 + (inv2 - x) => ( inv1 + inv2) - x 1.148 +// inv1 - (x - inv2) => ( inv1 + inv2) - x 1.149 +// (inv2 - x) + inv1 => ( inv1 + inv2) - x 1.150 +// (inv2 - x) - inv1 => (-inv1 + inv2) - x 1.151 +// inv1 - (x + inv2) => ( inv1 - inv2) - x 1.152 +// 1.153 +Node* IdealLoopTree::reassociate_add_sub(Node* n1, PhaseIdealLoop *phase) { 1.154 + if (!n1->is_Add() && !n1->is_Sub() || n1->outcnt() == 0) return NULL; 1.155 + if (is_invariant(n1)) return NULL; 1.156 + int inv1_idx = is_invariant_addition(n1, phase); 1.157 + if (!inv1_idx) return NULL; 1.158 + // Don't mess with add of constant (igvn moves them to expression tree root.) 1.159 + if (n1->is_Add() && n1->in(2)->is_Con()) return NULL; 1.160 + Node* inv1 = n1->in(inv1_idx); 1.161 + Node* n2 = n1->in(3 - inv1_idx); 1.162 + int inv2_idx = is_invariant_addition(n2, phase); 1.163 + if (!inv2_idx) return NULL; 1.164 + Node* x = n2->in(3 - inv2_idx); 1.165 + Node* inv2 = n2->in(inv2_idx); 1.166 + 1.167 + bool neg_x = n2->is_Sub() && inv2_idx == 1; 1.168 + bool neg_inv2 = n2->is_Sub() && inv2_idx == 2; 1.169 + bool neg_inv1 = n1->is_Sub() && inv1_idx == 2; 1.170 + if (n1->is_Sub() && inv1_idx == 1) { 1.171 + neg_x = !neg_x; 1.172 + neg_inv2 = !neg_inv2; 1.173 + } 1.174 + Node* inv1_c = phase->get_ctrl(inv1); 1.175 + Node* inv2_c = phase->get_ctrl(inv2); 1.176 + Node* n_inv1; 1.177 + if (neg_inv1) { 1.178 + Node *zero = phase->_igvn.intcon(0); 1.179 + phase->set_ctrl(zero, phase->C->root()); 1.180 + n_inv1 = new (phase->C, 3) SubINode(zero, inv1); 1.181 + phase->register_new_node(n_inv1, inv1_c); 1.182 + } else { 1.183 + n_inv1 = inv1; 1.184 + } 1.185 + Node* inv; 1.186 + if (neg_inv2) { 1.187 + inv = new (phase->C, 3) SubINode(n_inv1, inv2); 1.188 + } else { 1.189 + inv = new (phase->C, 3) AddINode(n_inv1, inv2); 1.190 + } 1.191 + phase->register_new_node(inv, phase->get_early_ctrl(inv)); 1.192 + 1.193 + Node* addx; 1.194 + if (neg_x) { 1.195 + addx = new (phase->C, 3) SubINode(inv, x); 1.196 + } else { 1.197 + addx = new (phase->C, 3) AddINode(x, inv); 1.198 + } 1.199 + phase->register_new_node(addx, phase->get_ctrl(x)); 1.200 + phase->_igvn.hash_delete(n1); 1.201 + phase->_igvn.subsume_node(n1, addx); 1.202 + return addx; 1.203 +} 1.204 + 1.205 +//---------------------reassociate_invariants----------------------------- 1.206 +// Reassociate invariant expressions: 1.207 +void IdealLoopTree::reassociate_invariants(PhaseIdealLoop *phase) { 1.208 + for (int i = _body.size() - 1; i >= 0; i--) { 1.209 + Node *n = _body.at(i); 1.210 + for (int j = 0; j < 5; j++) { 1.211 + Node* nn = reassociate_add_sub(n, phase); 1.212 + if (nn == NULL) break; 1.213 + n = nn; // again 1.214 + }; 1.215 + } 1.216 +} 1.217 + 1.218 +//------------------------------policy_peeling--------------------------------- 1.219 +// Return TRUE or FALSE if the loop should be peeled or not. Peel if we can 1.220 +// make some loop-invariant test (usually a null-check) happen before the loop. 1.221 +bool IdealLoopTree::policy_peeling( PhaseIdealLoop *phase ) const { 1.222 + Node *test = ((IdealLoopTree*)this)->tail(); 1.223 + int body_size = ((IdealLoopTree*)this)->_body.size(); 1.224 + int uniq = phase->C->unique(); 1.225 + // Peeling does loop cloning which can result in O(N^2) node construction 1.226 + if( body_size > 255 /* Prevent overflow for large body_size */ 1.227 + || (body_size * body_size + uniq > MaxNodeLimit) ) { 1.228 + return false; // too large to safely clone 1.229 + } 1.230 + while( test != _head ) { // Scan till run off top of loop 1.231 + if( test->is_If() ) { // Test? 1.232 + Node *ctrl = phase->get_ctrl(test->in(1)); 1.233 + if (ctrl->is_top()) 1.234 + return false; // Found dead test on live IF? No peeling! 1.235 + // Standard IF only has one input value to check for loop invariance 1.236 + assert( test->Opcode() == Op_If || test->Opcode() == Op_CountedLoopEnd, "Check this code when new subtype is added"); 1.237 + // Condition is not a member of this loop? 1.238 + if( !is_member(phase->get_loop(ctrl)) && 1.239 + is_loop_exit(test) ) 1.240 + return true; // Found reason to peel! 1.241 + } 1.242 + // Walk up dominators to loop _head looking for test which is 1.243 + // executed on every path thru loop. 1.244 + test = phase->idom(test); 1.245 + } 1.246 + return false; 1.247 +} 1.248 + 1.249 +//------------------------------peeled_dom_test_elim--------------------------- 1.250 +// If we got the effect of peeling, either by actually peeling or by making 1.251 +// a pre-loop which must execute at least once, we can remove all 1.252 +// loop-invariant dominated tests in the main body. 1.253 +void PhaseIdealLoop::peeled_dom_test_elim( IdealLoopTree *loop, Node_List &old_new ) { 1.254 + bool progress = true; 1.255 + while( progress ) { 1.256 + progress = false; // Reset for next iteration 1.257 + Node *prev = loop->_head->in(LoopNode::LoopBackControl);//loop->tail(); 1.258 + Node *test = prev->in(0); 1.259 + while( test != loop->_head ) { // Scan till run off top of loop 1.260 + 1.261 + int p_op = prev->Opcode(); 1.262 + if( (p_op == Op_IfFalse || p_op == Op_IfTrue) && 1.263 + test->is_If() && // Test? 1.264 + !test->in(1)->is_Con() && // And not already obvious? 1.265 + // Condition is not a member of this loop? 1.266 + !loop->is_member(get_loop(get_ctrl(test->in(1))))){ 1.267 + // Walk loop body looking for instances of this test 1.268 + for( uint i = 0; i < loop->_body.size(); i++ ) { 1.269 + Node *n = loop->_body.at(i); 1.270 + if( n->is_If() && n->in(1) == test->in(1) /*&& n != loop->tail()->in(0)*/ ) { 1.271 + // IfNode was dominated by version in peeled loop body 1.272 + progress = true; 1.273 + dominated_by( old_new[prev->_idx], n ); 1.274 + } 1.275 + } 1.276 + } 1.277 + prev = test; 1.278 + test = idom(test); 1.279 + } // End of scan tests in loop 1.280 + 1.281 + } // End of while( progress ) 1.282 +} 1.283 + 1.284 +//------------------------------do_peeling------------------------------------- 1.285 +// Peel the first iteration of the given loop. 1.286 +// Step 1: Clone the loop body. The clone becomes the peeled iteration. 1.287 +// The pre-loop illegally has 2 control users (old & new loops). 1.288 +// Step 2: Make the old-loop fall-in edges point to the peeled iteration. 1.289 +// Do this by making the old-loop fall-in edges act as if they came 1.290 +// around the loopback from the prior iteration (follow the old-loop 1.291 +// backedges) and then map to the new peeled iteration. This leaves 1.292 +// the pre-loop with only 1 user (the new peeled iteration), but the 1.293 +// peeled-loop backedge has 2 users. 1.294 +// Step 3: Cut the backedge on the clone (so its not a loop) and remove the 1.295 +// extra backedge user. 1.296 +void PhaseIdealLoop::do_peeling( IdealLoopTree *loop, Node_List &old_new ) { 1.297 + 1.298 + C->set_major_progress(); 1.299 + // Peeling a 'main' loop in a pre/main/post situation obfuscates the 1.300 + // 'pre' loop from the main and the 'pre' can no longer have it's 1.301 + // iterations adjusted. Therefore, we need to declare this loop as 1.302 + // no longer a 'main' loop; it will need new pre and post loops before 1.303 + // we can do further RCE. 1.304 + Node *h = loop->_head; 1.305 + if( h->is_CountedLoop() ) { 1.306 + CountedLoopNode *cl = h->as_CountedLoop(); 1.307 + assert(cl->trip_count() > 0, "peeling a fully unrolled loop"); 1.308 + cl->set_trip_count(cl->trip_count() - 1); 1.309 + if( cl->is_main_loop() ) { 1.310 + cl->set_normal_loop(); 1.311 +#ifndef PRODUCT 1.312 + if( PrintOpto && VerifyLoopOptimizations ) { 1.313 + tty->print("Peeling a 'main' loop; resetting to 'normal' "); 1.314 + loop->dump_head(); 1.315 + } 1.316 +#endif 1.317 + } 1.318 + } 1.319 + 1.320 + // Step 1: Clone the loop body. The clone becomes the peeled iteration. 1.321 + // The pre-loop illegally has 2 control users (old & new loops). 1.322 + clone_loop( loop, old_new, dom_depth(loop->_head) ); 1.323 + 1.324 + 1.325 + // Step 2: Make the old-loop fall-in edges point to the peeled iteration. 1.326 + // Do this by making the old-loop fall-in edges act as if they came 1.327 + // around the loopback from the prior iteration (follow the old-loop 1.328 + // backedges) and then map to the new peeled iteration. This leaves 1.329 + // the pre-loop with only 1 user (the new peeled iteration), but the 1.330 + // peeled-loop backedge has 2 users. 1.331 + for (DUIterator_Fast jmax, j = loop->_head->fast_outs(jmax); j < jmax; j++) { 1.332 + Node* old = loop->_head->fast_out(j); 1.333 + if( old->in(0) == loop->_head && old->req() == 3 && 1.334 + (old->is_Loop() || old->is_Phi()) ) { 1.335 + Node *new_exit_value = old_new[old->in(LoopNode::LoopBackControl)->_idx]; 1.336 + if( !new_exit_value ) // Backedge value is ALSO loop invariant? 1.337 + // Then loop body backedge value remains the same. 1.338 + new_exit_value = old->in(LoopNode::LoopBackControl); 1.339 + _igvn.hash_delete(old); 1.340 + old->set_req(LoopNode::EntryControl, new_exit_value); 1.341 + } 1.342 + } 1.343 + 1.344 + 1.345 + // Step 3: Cut the backedge on the clone (so its not a loop) and remove the 1.346 + // extra backedge user. 1.347 + Node *nnn = old_new[loop->_head->_idx]; 1.348 + _igvn.hash_delete(nnn); 1.349 + nnn->set_req(LoopNode::LoopBackControl, C->top()); 1.350 + for (DUIterator_Fast j2max, j2 = nnn->fast_outs(j2max); j2 < j2max; j2++) { 1.351 + Node* use = nnn->fast_out(j2); 1.352 + if( use->in(0) == nnn && use->req() == 3 && use->is_Phi() ) { 1.353 + _igvn.hash_delete(use); 1.354 + use->set_req(LoopNode::LoopBackControl, C->top()); 1.355 + } 1.356 + } 1.357 + 1.358 + 1.359 + // Step 4: Correct dom-depth info. Set to loop-head depth. 1.360 + int dd = dom_depth(loop->_head); 1.361 + set_idom(loop->_head, loop->_head->in(1), dd); 1.362 + for (uint j3 = 0; j3 < loop->_body.size(); j3++) { 1.363 + Node *old = loop->_body.at(j3); 1.364 + Node *nnn = old_new[old->_idx]; 1.365 + if (!has_ctrl(nnn)) 1.366 + set_idom(nnn, idom(nnn), dd-1); 1.367 + // While we're at it, remove any SafePoints from the peeled code 1.368 + if( old->Opcode() == Op_SafePoint ) { 1.369 + Node *nnn = old_new[old->_idx]; 1.370 + lazy_replace(nnn,nnn->in(TypeFunc::Control)); 1.371 + } 1.372 + } 1.373 + 1.374 + // Now force out all loop-invariant dominating tests. The optimizer 1.375 + // finds some, but we _know_ they are all useless. 1.376 + peeled_dom_test_elim(loop,old_new); 1.377 + 1.378 + loop->record_for_igvn(); 1.379 +} 1.380 + 1.381 +//------------------------------policy_maximally_unroll------------------------ 1.382 +// Return exact loop trip count, or 0 if not maximally unrolling 1.383 +bool IdealLoopTree::policy_maximally_unroll( PhaseIdealLoop *phase ) const { 1.384 + CountedLoopNode *cl = _head->as_CountedLoop(); 1.385 + assert( cl->is_normal_loop(), "" ); 1.386 + 1.387 + Node *init_n = cl->init_trip(); 1.388 + Node *limit_n = cl->limit(); 1.389 + 1.390 + // Non-constant bounds 1.391 + if( init_n == NULL || !init_n->is_Con() || 1.392 + limit_n == NULL || !limit_n->is_Con() || 1.393 + // protect against stride not being a constant 1.394 + !cl->stride_is_con() ) { 1.395 + return false; 1.396 + } 1.397 + int init = init_n->get_int(); 1.398 + int limit = limit_n->get_int(); 1.399 + int span = limit - init; 1.400 + int stride = cl->stride_con(); 1.401 + 1.402 + if (init >= limit || stride > span) { 1.403 + // return a false (no maximally unroll) and the regular unroll/peel 1.404 + // route will make a small mess which CCP will fold away. 1.405 + return false; 1.406 + } 1.407 + uint trip_count = span/stride; // trip_count can be greater than 2 Gig. 1.408 + assert( (int)trip_count*stride == span, "must divide evenly" ); 1.409 + 1.410 + // Real policy: if we maximally unroll, does it get too big? 1.411 + // Allow the unrolled mess to get larger than standard loop 1.412 + // size. After all, it will no longer be a loop. 1.413 + uint body_size = _body.size(); 1.414 + uint unroll_limit = (uint)LoopUnrollLimit * 4; 1.415 + assert( (intx)unroll_limit == LoopUnrollLimit * 4, "LoopUnrollLimit must fit in 32bits"); 1.416 + cl->set_trip_count(trip_count); 1.417 + if( trip_count <= unroll_limit && body_size <= unroll_limit ) { 1.418 + uint new_body_size = body_size * trip_count; 1.419 + if (new_body_size <= unroll_limit && 1.420 + body_size == new_body_size / trip_count && 1.421 + // Unrolling can result in a large amount of node construction 1.422 + new_body_size < MaxNodeLimit - phase->C->unique()) { 1.423 + return true; // maximally unroll 1.424 + } 1.425 + } 1.426 + 1.427 + return false; // Do not maximally unroll 1.428 +} 1.429 + 1.430 + 1.431 +//------------------------------policy_unroll---------------------------------- 1.432 +// Return TRUE or FALSE if the loop should be unrolled or not. Unroll if 1.433 +// the loop is a CountedLoop and the body is small enough. 1.434 +bool IdealLoopTree::policy_unroll( PhaseIdealLoop *phase ) const { 1.435 + 1.436 + CountedLoopNode *cl = _head->as_CountedLoop(); 1.437 + assert( cl->is_normal_loop() || cl->is_main_loop(), "" ); 1.438 + 1.439 + // protect against stride not being a constant 1.440 + if( !cl->stride_is_con() ) return false; 1.441 + 1.442 + // protect against over-unrolling 1.443 + if( cl->trip_count() <= 1 ) return false; 1.444 + 1.445 + int future_unroll_ct = cl->unrolled_count() * 2; 1.446 + 1.447 + // Don't unroll if the next round of unrolling would push us 1.448 + // over the expected trip count of the loop. One is subtracted 1.449 + // from the expected trip count because the pre-loop normally 1.450 + // executes 1 iteration. 1.451 + if (UnrollLimitForProfileCheck > 0 && 1.452 + cl->profile_trip_cnt() != COUNT_UNKNOWN && 1.453 + future_unroll_ct > UnrollLimitForProfileCheck && 1.454 + (float)future_unroll_ct > cl->profile_trip_cnt() - 1.0) { 1.455 + return false; 1.456 + } 1.457 + 1.458 + // When unroll count is greater than LoopUnrollMin, don't unroll if: 1.459 + // the residual iterations are more than 10% of the trip count 1.460 + // and rounds of "unroll,optimize" are not making significant progress 1.461 + // Progress defined as current size less than 20% larger than previous size. 1.462 + if (UseSuperWord && cl->node_count_before_unroll() > 0 && 1.463 + future_unroll_ct > LoopUnrollMin && 1.464 + (future_unroll_ct - 1) * 10.0 > cl->profile_trip_cnt() && 1.465 + 1.2 * cl->node_count_before_unroll() < (double)_body.size()) { 1.466 + return false; 1.467 + } 1.468 + 1.469 + Node *init_n = cl->init_trip(); 1.470 + Node *limit_n = cl->limit(); 1.471 + // Non-constant bounds. 1.472 + // Protect against over-unrolling when init or/and limit are not constant 1.473 + // (so that trip_count's init value is maxint) but iv range is known. 1.474 + if( init_n == NULL || !init_n->is_Con() || 1.475 + limit_n == NULL || !limit_n->is_Con() ) { 1.476 + Node* phi = cl->phi(); 1.477 + if( phi != NULL ) { 1.478 + assert(phi->is_Phi() && phi->in(0) == _head, "Counted loop should have iv phi."); 1.479 + const TypeInt* iv_type = phase->_igvn.type(phi)->is_int(); 1.480 + int next_stride = cl->stride_con() * 2; // stride after this unroll 1.481 + if( next_stride > 0 ) { 1.482 + if( iv_type->_lo + next_stride <= iv_type->_lo || // overflow 1.483 + iv_type->_lo + next_stride > iv_type->_hi ) { 1.484 + return false; // over-unrolling 1.485 + } 1.486 + } else if( next_stride < 0 ) { 1.487 + if( iv_type->_hi + next_stride >= iv_type->_hi || // overflow 1.488 + iv_type->_hi + next_stride < iv_type->_lo ) { 1.489 + return false; // over-unrolling 1.490 + } 1.491 + } 1.492 + } 1.493 + } 1.494 + 1.495 + // Adjust body_size to determine if we unroll or not 1.496 + uint body_size = _body.size(); 1.497 + // Key test to unroll CaffeineMark's Logic test 1.498 + int xors_in_loop = 0; 1.499 + // Also count ModL, DivL and MulL which expand mightly 1.500 + for( uint k = 0; k < _body.size(); k++ ) { 1.501 + switch( _body.at(k)->Opcode() ) { 1.502 + case Op_XorI: xors_in_loop++; break; // CaffeineMark's Logic test 1.503 + case Op_ModL: body_size += 30; break; 1.504 + case Op_DivL: body_size += 30; break; 1.505 + case Op_MulL: body_size += 10; break; 1.506 + } 1.507 + } 1.508 + 1.509 + // Check for being too big 1.510 + if( body_size > (uint)LoopUnrollLimit ) { 1.511 + if( xors_in_loop >= 4 && body_size < (uint)LoopUnrollLimit*4) return true; 1.512 + // Normal case: loop too big 1.513 + return false; 1.514 + } 1.515 + 1.516 + // Check for stride being a small enough constant 1.517 + if( abs(cl->stride_con()) > (1<<3) ) return false; 1.518 + 1.519 + // Unroll once! (Each trip will soon do double iterations) 1.520 + return true; 1.521 +} 1.522 + 1.523 +//------------------------------policy_align----------------------------------- 1.524 +// Return TRUE or FALSE if the loop should be cache-line aligned. Gather the 1.525 +// expression that does the alignment. Note that only one array base can be 1.526 +// aligned in a loop (unless the VM guarentees mutual alignment). Note that 1.527 +// if we vectorize short memory ops into longer memory ops, we may want to 1.528 +// increase alignment. 1.529 +bool IdealLoopTree::policy_align( PhaseIdealLoop *phase ) const { 1.530 + return false; 1.531 +} 1.532 + 1.533 +//------------------------------policy_range_check----------------------------- 1.534 +// Return TRUE or FALSE if the loop should be range-check-eliminated. 1.535 +// Actually we do iteration-splitting, a more powerful form of RCE. 1.536 +bool IdealLoopTree::policy_range_check( PhaseIdealLoop *phase ) const { 1.537 + if( !RangeCheckElimination ) return false; 1.538 + 1.539 + CountedLoopNode *cl = _head->as_CountedLoop(); 1.540 + // If we unrolled with no intention of doing RCE and we later 1.541 + // changed our minds, we got no pre-loop. Either we need to 1.542 + // make a new pre-loop, or we gotta disallow RCE. 1.543 + if( cl->is_main_no_pre_loop() ) return false; // Disallowed for now. 1.544 + Node *trip_counter = cl->phi(); 1.545 + 1.546 + // Check loop body for tests of trip-counter plus loop-invariant vs 1.547 + // loop-invariant. 1.548 + for( uint i = 0; i < _body.size(); i++ ) { 1.549 + Node *iff = _body[i]; 1.550 + if( iff->Opcode() == Op_If ) { // Test? 1.551 + 1.552 + // Comparing trip+off vs limit 1.553 + Node *bol = iff->in(1); 1.554 + if( bol->req() != 2 ) continue; // dead constant test 1.555 + Node *cmp = bol->in(1); 1.556 + 1.557 + Node *rc_exp = cmp->in(1); 1.558 + Node *limit = cmp->in(2); 1.559 + 1.560 + Node *limit_c = phase->get_ctrl(limit); 1.561 + if( limit_c == phase->C->top() ) 1.562 + return false; // Found dead test on live IF? No RCE! 1.563 + if( is_member(phase->get_loop(limit_c) ) ) { 1.564 + // Compare might have operands swapped; commute them 1.565 + rc_exp = cmp->in(2); 1.566 + limit = cmp->in(1); 1.567 + limit_c = phase->get_ctrl(limit); 1.568 + if( is_member(phase->get_loop(limit_c) ) ) 1.569 + continue; // Both inputs are loop varying; cannot RCE 1.570 + } 1.571 + 1.572 + if (!phase->is_scaled_iv_plus_offset(rc_exp, trip_counter, NULL, NULL)) { 1.573 + continue; 1.574 + } 1.575 + // Yeah! Found a test like 'trip+off vs limit' 1.576 + // Test is an IfNode, has 2 projections. If BOTH are in the loop 1.577 + // we need loop unswitching instead of iteration splitting. 1.578 + if( is_loop_exit(iff) ) 1.579 + return true; // Found reason to split iterations 1.580 + } // End of is IF 1.581 + } 1.582 + 1.583 + return false; 1.584 +} 1.585 + 1.586 +//------------------------------policy_peel_only------------------------------- 1.587 +// Return TRUE or FALSE if the loop should NEVER be RCE'd or aligned. Useful 1.588 +// for unrolling loops with NO array accesses. 1.589 +bool IdealLoopTree::policy_peel_only( PhaseIdealLoop *phase ) const { 1.590 + 1.591 + for( uint i = 0; i < _body.size(); i++ ) 1.592 + if( _body[i]->is_Mem() ) 1.593 + return false; 1.594 + 1.595 + // No memory accesses at all! 1.596 + return true; 1.597 +} 1.598 + 1.599 +//------------------------------clone_up_backedge_goo-------------------------- 1.600 +// If Node n lives in the back_ctrl block and cannot float, we clone a private 1.601 +// version of n in preheader_ctrl block and return that, otherwise return n. 1.602 +Node *PhaseIdealLoop::clone_up_backedge_goo( Node *back_ctrl, Node *preheader_ctrl, Node *n ) { 1.603 + if( get_ctrl(n) != back_ctrl ) return n; 1.604 + 1.605 + Node *x = NULL; // If required, a clone of 'n' 1.606 + // Check for 'n' being pinned in the backedge. 1.607 + if( n->in(0) && n->in(0) == back_ctrl ) { 1.608 + x = n->clone(); // Clone a copy of 'n' to preheader 1.609 + x->set_req( 0, preheader_ctrl ); // Fix x's control input to preheader 1.610 + } 1.611 + 1.612 + // Recursive fixup any other input edges into x. 1.613 + // If there are no changes we can just return 'n', otherwise 1.614 + // we need to clone a private copy and change it. 1.615 + for( uint i = 1; i < n->req(); i++ ) { 1.616 + Node *g = clone_up_backedge_goo( back_ctrl, preheader_ctrl, n->in(i) ); 1.617 + if( g != n->in(i) ) { 1.618 + if( !x ) 1.619 + x = n->clone(); 1.620 + x->set_req(i, g); 1.621 + } 1.622 + } 1.623 + if( x ) { // x can legally float to pre-header location 1.624 + register_new_node( x, preheader_ctrl ); 1.625 + return x; 1.626 + } else { // raise n to cover LCA of uses 1.627 + set_ctrl( n, find_non_split_ctrl(back_ctrl->in(0)) ); 1.628 + } 1.629 + return n; 1.630 +} 1.631 + 1.632 +//------------------------------insert_pre_post_loops-------------------------- 1.633 +// Insert pre and post loops. If peel_only is set, the pre-loop can not have 1.634 +// more iterations added. It acts as a 'peel' only, no lower-bound RCE, no 1.635 +// alignment. Useful to unroll loops that do no array accesses. 1.636 +void PhaseIdealLoop::insert_pre_post_loops( IdealLoopTree *loop, Node_List &old_new, bool peel_only ) { 1.637 + 1.638 + C->set_major_progress(); 1.639 + 1.640 + // Find common pieces of the loop being guarded with pre & post loops 1.641 + CountedLoopNode *main_head = loop->_head->as_CountedLoop(); 1.642 + assert( main_head->is_normal_loop(), "" ); 1.643 + CountedLoopEndNode *main_end = main_head->loopexit(); 1.644 + assert( main_end->outcnt() == 2, "1 true, 1 false path only" ); 1.645 + uint dd_main_head = dom_depth(main_head); 1.646 + uint max = main_head->outcnt(); 1.647 + 1.648 + Node *pre_header= main_head->in(LoopNode::EntryControl); 1.649 + Node *init = main_head->init_trip(); 1.650 + Node *incr = main_end ->incr(); 1.651 + Node *limit = main_end ->limit(); 1.652 + Node *stride = main_end ->stride(); 1.653 + Node *cmp = main_end ->cmp_node(); 1.654 + BoolTest::mask b_test = main_end->test_trip(); 1.655 + 1.656 + // Need only 1 user of 'bol' because I will be hacking the loop bounds. 1.657 + Node *bol = main_end->in(CountedLoopEndNode::TestValue); 1.658 + if( bol->outcnt() != 1 ) { 1.659 + bol = bol->clone(); 1.660 + register_new_node(bol,main_end->in(CountedLoopEndNode::TestControl)); 1.661 + _igvn.hash_delete(main_end); 1.662 + main_end->set_req(CountedLoopEndNode::TestValue, bol); 1.663 + } 1.664 + // Need only 1 user of 'cmp' because I will be hacking the loop bounds. 1.665 + if( cmp->outcnt() != 1 ) { 1.666 + cmp = cmp->clone(); 1.667 + register_new_node(cmp,main_end->in(CountedLoopEndNode::TestControl)); 1.668 + _igvn.hash_delete(bol); 1.669 + bol->set_req(1, cmp); 1.670 + } 1.671 + 1.672 + //------------------------------ 1.673 + // Step A: Create Post-Loop. 1.674 + Node* main_exit = main_end->proj_out(false); 1.675 + assert( main_exit->Opcode() == Op_IfFalse, "" ); 1.676 + int dd_main_exit = dom_depth(main_exit); 1.677 + 1.678 + // Step A1: Clone the loop body. The clone becomes the post-loop. The main 1.679 + // loop pre-header illegally has 2 control users (old & new loops). 1.680 + clone_loop( loop, old_new, dd_main_exit ); 1.681 + assert( old_new[main_end ->_idx]->Opcode() == Op_CountedLoopEnd, "" ); 1.682 + CountedLoopNode *post_head = old_new[main_head->_idx]->as_CountedLoop(); 1.683 + post_head->set_post_loop(main_head); 1.684 + 1.685 + // Build the main-loop normal exit. 1.686 + IfFalseNode *new_main_exit = new (C, 1) IfFalseNode(main_end); 1.687 + _igvn.register_new_node_with_optimizer( new_main_exit ); 1.688 + set_idom(new_main_exit, main_end, dd_main_exit ); 1.689 + set_loop(new_main_exit, loop->_parent); 1.690 + 1.691 + // Step A2: Build a zero-trip guard for the post-loop. After leaving the 1.692 + // main-loop, the post-loop may not execute at all. We 'opaque' the incr 1.693 + // (the main-loop trip-counter exit value) because we will be changing 1.694 + // the exit value (via unrolling) so we cannot constant-fold away the zero 1.695 + // trip guard until all unrolling is done. 1.696 + Node *zer_opaq = new (C, 2) Opaque1Node(incr); 1.697 + Node *zer_cmp = new (C, 3) CmpINode( zer_opaq, limit ); 1.698 + Node *zer_bol = new (C, 2) BoolNode( zer_cmp, b_test ); 1.699 + register_new_node( zer_opaq, new_main_exit ); 1.700 + register_new_node( zer_cmp , new_main_exit ); 1.701 + register_new_node( zer_bol , new_main_exit ); 1.702 + 1.703 + // Build the IfNode 1.704 + IfNode *zer_iff = new (C, 2) IfNode( new_main_exit, zer_bol, PROB_FAIR, COUNT_UNKNOWN ); 1.705 + _igvn.register_new_node_with_optimizer( zer_iff ); 1.706 + set_idom(zer_iff, new_main_exit, dd_main_exit); 1.707 + set_loop(zer_iff, loop->_parent); 1.708 + 1.709 + // Plug in the false-path, taken if we need to skip post-loop 1.710 + _igvn.hash_delete( main_exit ); 1.711 + main_exit->set_req(0, zer_iff); 1.712 + _igvn._worklist.push(main_exit); 1.713 + set_idom(main_exit, zer_iff, dd_main_exit); 1.714 + set_idom(main_exit->unique_out(), zer_iff, dd_main_exit); 1.715 + // Make the true-path, must enter the post loop 1.716 + Node *zer_taken = new (C, 1) IfTrueNode( zer_iff ); 1.717 + _igvn.register_new_node_with_optimizer( zer_taken ); 1.718 + set_idom(zer_taken, zer_iff, dd_main_exit); 1.719 + set_loop(zer_taken, loop->_parent); 1.720 + // Plug in the true path 1.721 + _igvn.hash_delete( post_head ); 1.722 + post_head->set_req(LoopNode::EntryControl, zer_taken); 1.723 + set_idom(post_head, zer_taken, dd_main_exit); 1.724 + 1.725 + // Step A3: Make the fall-in values to the post-loop come from the 1.726 + // fall-out values of the main-loop. 1.727 + for (DUIterator_Fast imax, i = main_head->fast_outs(imax); i < imax; i++) { 1.728 + Node* main_phi = main_head->fast_out(i); 1.729 + if( main_phi->is_Phi() && main_phi->in(0) == main_head && main_phi->outcnt() >0 ) { 1.730 + Node *post_phi = old_new[main_phi->_idx]; 1.731 + Node *fallmain = clone_up_backedge_goo(main_head->back_control(), 1.732 + post_head->init_control(), 1.733 + main_phi->in(LoopNode::LoopBackControl)); 1.734 + _igvn.hash_delete(post_phi); 1.735 + post_phi->set_req( LoopNode::EntryControl, fallmain ); 1.736 + } 1.737 + } 1.738 + 1.739 + // Update local caches for next stanza 1.740 + main_exit = new_main_exit; 1.741 + 1.742 + 1.743 + //------------------------------ 1.744 + // Step B: Create Pre-Loop. 1.745 + 1.746 + // Step B1: Clone the loop body. The clone becomes the pre-loop. The main 1.747 + // loop pre-header illegally has 2 control users (old & new loops). 1.748 + clone_loop( loop, old_new, dd_main_head ); 1.749 + CountedLoopNode* pre_head = old_new[main_head->_idx]->as_CountedLoop(); 1.750 + CountedLoopEndNode* pre_end = old_new[main_end ->_idx]->as_CountedLoopEnd(); 1.751 + pre_head->set_pre_loop(main_head); 1.752 + Node *pre_incr = old_new[incr->_idx]; 1.753 + 1.754 + // Find the pre-loop normal exit. 1.755 + Node* pre_exit = pre_end->proj_out(false); 1.756 + assert( pre_exit->Opcode() == Op_IfFalse, "" ); 1.757 + IfFalseNode *new_pre_exit = new (C, 1) IfFalseNode(pre_end); 1.758 + _igvn.register_new_node_with_optimizer( new_pre_exit ); 1.759 + set_idom(new_pre_exit, pre_end, dd_main_head); 1.760 + set_loop(new_pre_exit, loop->_parent); 1.761 + 1.762 + // Step B2: Build a zero-trip guard for the main-loop. After leaving the 1.763 + // pre-loop, the main-loop may not execute at all. Later in life this 1.764 + // zero-trip guard will become the minimum-trip guard when we unroll 1.765 + // the main-loop. 1.766 + Node *min_opaq = new (C, 2) Opaque1Node(limit); 1.767 + Node *min_cmp = new (C, 3) CmpINode( pre_incr, min_opaq ); 1.768 + Node *min_bol = new (C, 2) BoolNode( min_cmp, b_test ); 1.769 + register_new_node( min_opaq, new_pre_exit ); 1.770 + register_new_node( min_cmp , new_pre_exit ); 1.771 + register_new_node( min_bol , new_pre_exit ); 1.772 + 1.773 + // Build the IfNode 1.774 + IfNode *min_iff = new (C, 2) IfNode( new_pre_exit, min_bol, PROB_FAIR, COUNT_UNKNOWN ); 1.775 + _igvn.register_new_node_with_optimizer( min_iff ); 1.776 + set_idom(min_iff, new_pre_exit, dd_main_head); 1.777 + set_loop(min_iff, loop->_parent); 1.778 + 1.779 + // Plug in the false-path, taken if we need to skip main-loop 1.780 + _igvn.hash_delete( pre_exit ); 1.781 + pre_exit->set_req(0, min_iff); 1.782 + set_idom(pre_exit, min_iff, dd_main_head); 1.783 + set_idom(pre_exit->unique_out(), min_iff, dd_main_head); 1.784 + // Make the true-path, must enter the main loop 1.785 + Node *min_taken = new (C, 1) IfTrueNode( min_iff ); 1.786 + _igvn.register_new_node_with_optimizer( min_taken ); 1.787 + set_idom(min_taken, min_iff, dd_main_head); 1.788 + set_loop(min_taken, loop->_parent); 1.789 + // Plug in the true path 1.790 + _igvn.hash_delete( main_head ); 1.791 + main_head->set_req(LoopNode::EntryControl, min_taken); 1.792 + set_idom(main_head, min_taken, dd_main_head); 1.793 + 1.794 + // Step B3: Make the fall-in values to the main-loop come from the 1.795 + // fall-out values of the pre-loop. 1.796 + for (DUIterator_Fast i2max, i2 = main_head->fast_outs(i2max); i2 < i2max; i2++) { 1.797 + Node* main_phi = main_head->fast_out(i2); 1.798 + if( main_phi->is_Phi() && main_phi->in(0) == main_head && main_phi->outcnt() > 0 ) { 1.799 + Node *pre_phi = old_new[main_phi->_idx]; 1.800 + Node *fallpre = clone_up_backedge_goo(pre_head->back_control(), 1.801 + main_head->init_control(), 1.802 + pre_phi->in(LoopNode::LoopBackControl)); 1.803 + _igvn.hash_delete(main_phi); 1.804 + main_phi->set_req( LoopNode::EntryControl, fallpre ); 1.805 + } 1.806 + } 1.807 + 1.808 + // Step B4: Shorten the pre-loop to run only 1 iteration (for now). 1.809 + // RCE and alignment may change this later. 1.810 + Node *cmp_end = pre_end->cmp_node(); 1.811 + assert( cmp_end->in(2) == limit, "" ); 1.812 + Node *pre_limit = new (C, 3) AddINode( init, stride ); 1.813 + 1.814 + // Save the original loop limit in this Opaque1 node for 1.815 + // use by range check elimination. 1.816 + Node *pre_opaq = new (C, 3) Opaque1Node(pre_limit, limit); 1.817 + 1.818 + register_new_node( pre_limit, pre_head->in(0) ); 1.819 + register_new_node( pre_opaq , pre_head->in(0) ); 1.820 + 1.821 + // Since no other users of pre-loop compare, I can hack limit directly 1.822 + assert( cmp_end->outcnt() == 1, "no other users" ); 1.823 + _igvn.hash_delete(cmp_end); 1.824 + cmp_end->set_req(2, peel_only ? pre_limit : pre_opaq); 1.825 + 1.826 + // Special case for not-equal loop bounds: 1.827 + // Change pre loop test, main loop test, and the 1.828 + // main loop guard test to use lt or gt depending on stride 1.829 + // direction: 1.830 + // positive stride use < 1.831 + // negative stride use > 1.832 + 1.833 + if (pre_end->in(CountedLoopEndNode::TestValue)->as_Bool()->_test._test == BoolTest::ne) { 1.834 + 1.835 + BoolTest::mask new_test = (main_end->stride_con() > 0) ? BoolTest::lt : BoolTest::gt; 1.836 + // Modify pre loop end condition 1.837 + Node* pre_bol = pre_end->in(CountedLoopEndNode::TestValue)->as_Bool(); 1.838 + BoolNode* new_bol0 = new (C, 2) BoolNode(pre_bol->in(1), new_test); 1.839 + register_new_node( new_bol0, pre_head->in(0) ); 1.840 + _igvn.hash_delete(pre_end); 1.841 + pre_end->set_req(CountedLoopEndNode::TestValue, new_bol0); 1.842 + // Modify main loop guard condition 1.843 + assert(min_iff->in(CountedLoopEndNode::TestValue) == min_bol, "guard okay"); 1.844 + BoolNode* new_bol1 = new (C, 2) BoolNode(min_bol->in(1), new_test); 1.845 + register_new_node( new_bol1, new_pre_exit ); 1.846 + _igvn.hash_delete(min_iff); 1.847 + min_iff->set_req(CountedLoopEndNode::TestValue, new_bol1); 1.848 + // Modify main loop end condition 1.849 + BoolNode* main_bol = main_end->in(CountedLoopEndNode::TestValue)->as_Bool(); 1.850 + BoolNode* new_bol2 = new (C, 2) BoolNode(main_bol->in(1), new_test); 1.851 + register_new_node( new_bol2, main_end->in(CountedLoopEndNode::TestControl) ); 1.852 + _igvn.hash_delete(main_end); 1.853 + main_end->set_req(CountedLoopEndNode::TestValue, new_bol2); 1.854 + } 1.855 + 1.856 + // Flag main loop 1.857 + main_head->set_main_loop(); 1.858 + if( peel_only ) main_head->set_main_no_pre_loop(); 1.859 + 1.860 + // It's difficult to be precise about the trip-counts 1.861 + // for the pre/post loops. They are usually very short, 1.862 + // so guess that 4 trips is a reasonable value. 1.863 + post_head->set_profile_trip_cnt(4.0); 1.864 + pre_head->set_profile_trip_cnt(4.0); 1.865 + 1.866 + // Now force out all loop-invariant dominating tests. The optimizer 1.867 + // finds some, but we _know_ they are all useless. 1.868 + peeled_dom_test_elim(loop,old_new); 1.869 +} 1.870 + 1.871 +//------------------------------is_invariant----------------------------- 1.872 +// Return true if n is invariant 1.873 +bool IdealLoopTree::is_invariant(Node* n) const { 1.874 + Node *n_c = _phase->get_ctrl(n); 1.875 + if (n_c->is_top()) return false; 1.876 + return !is_member(_phase->get_loop(n_c)); 1.877 +} 1.878 + 1.879 + 1.880 +//------------------------------do_unroll-------------------------------------- 1.881 +// Unroll the loop body one step - make each trip do 2 iterations. 1.882 +void PhaseIdealLoop::do_unroll( IdealLoopTree *loop, Node_List &old_new, bool adjust_min_trip ) { 1.883 + assert( LoopUnrollLimit, "" ); 1.884 +#ifndef PRODUCT 1.885 + if( PrintOpto && VerifyLoopOptimizations ) { 1.886 + tty->print("Unrolling "); 1.887 + loop->dump_head(); 1.888 + } 1.889 +#endif 1.890 + CountedLoopNode *loop_head = loop->_head->as_CountedLoop(); 1.891 + CountedLoopEndNode *loop_end = loop_head->loopexit(); 1.892 + assert( loop_end, "" ); 1.893 + 1.894 + // Remember loop node count before unrolling to detect 1.895 + // if rounds of unroll,optimize are making progress 1.896 + loop_head->set_node_count_before_unroll(loop->_body.size()); 1.897 + 1.898 + Node *ctrl = loop_head->in(LoopNode::EntryControl); 1.899 + Node *limit = loop_head->limit(); 1.900 + Node *init = loop_head->init_trip(); 1.901 + Node *strid = loop_head->stride(); 1.902 + 1.903 + Node *opaq = NULL; 1.904 + if( adjust_min_trip ) { // If not maximally unrolling, need adjustment 1.905 + assert( loop_head->is_main_loop(), "" ); 1.906 + assert( ctrl->Opcode() == Op_IfTrue || ctrl->Opcode() == Op_IfFalse, "" ); 1.907 + Node *iff = ctrl->in(0); 1.908 + assert( iff->Opcode() == Op_If, "" ); 1.909 + Node *bol = iff->in(1); 1.910 + assert( bol->Opcode() == Op_Bool, "" ); 1.911 + Node *cmp = bol->in(1); 1.912 + assert( cmp->Opcode() == Op_CmpI, "" ); 1.913 + opaq = cmp->in(2); 1.914 + // Occasionally it's possible for a pre-loop Opaque1 node to be 1.915 + // optimized away and then another round of loop opts attempted. 1.916 + // We can not optimize this particular loop in that case. 1.917 + if( opaq->Opcode() != Op_Opaque1 ) 1.918 + return; // Cannot find pre-loop! Bail out! 1.919 + } 1.920 + 1.921 + C->set_major_progress(); 1.922 + 1.923 + // Adjust max trip count. The trip count is intentionally rounded 1.924 + // down here (e.g. 15-> 7-> 3-> 1) because if we unwittingly over-unroll, 1.925 + // the main, unrolled, part of the loop will never execute as it is protected 1.926 + // by the min-trip test. See bug 4834191 for a case where we over-unrolled 1.927 + // and later determined that part of the unrolled loop was dead. 1.928 + loop_head->set_trip_count(loop_head->trip_count() / 2); 1.929 + 1.930 + // Double the count of original iterations in the unrolled loop body. 1.931 + loop_head->double_unrolled_count(); 1.932 + 1.933 + // ----------- 1.934 + // Step 2: Cut back the trip counter for an unroll amount of 2. 1.935 + // Loop will normally trip (limit - init)/stride_con. Since it's a 1.936 + // CountedLoop this is exact (stride divides limit-init exactly). 1.937 + // We are going to double the loop body, so we want to knock off any 1.938 + // odd iteration: (trip_cnt & ~1). Then back compute a new limit. 1.939 + Node *span = new (C, 3) SubINode( limit, init ); 1.940 + register_new_node( span, ctrl ); 1.941 + Node *trip = new (C, 3) DivINode( 0, span, strid ); 1.942 + register_new_node( trip, ctrl ); 1.943 + Node *mtwo = _igvn.intcon(-2); 1.944 + set_ctrl(mtwo, C->root()); 1.945 + Node *rond = new (C, 3) AndINode( trip, mtwo ); 1.946 + register_new_node( rond, ctrl ); 1.947 + Node *spn2 = new (C, 3) MulINode( rond, strid ); 1.948 + register_new_node( spn2, ctrl ); 1.949 + Node *lim2 = new (C, 3) AddINode( spn2, init ); 1.950 + register_new_node( lim2, ctrl ); 1.951 + 1.952 + // Hammer in the new limit 1.953 + Node *ctrl2 = loop_end->in(0); 1.954 + Node *cmp2 = new (C, 3) CmpINode( loop_head->incr(), lim2 ); 1.955 + register_new_node( cmp2, ctrl2 ); 1.956 + Node *bol2 = new (C, 2) BoolNode( cmp2, loop_end->test_trip() ); 1.957 + register_new_node( bol2, ctrl2 ); 1.958 + _igvn.hash_delete(loop_end); 1.959 + loop_end->set_req(CountedLoopEndNode::TestValue, bol2); 1.960 + 1.961 + // Step 3: Find the min-trip test guaranteed before a 'main' loop. 1.962 + // Make it a 1-trip test (means at least 2 trips). 1.963 + if( adjust_min_trip ) { 1.964 + // Guard test uses an 'opaque' node which is not shared. Hence I 1.965 + // can edit it's inputs directly. Hammer in the new limit for the 1.966 + // minimum-trip guard. 1.967 + assert( opaq->outcnt() == 1, "" ); 1.968 + _igvn.hash_delete(opaq); 1.969 + opaq->set_req(1, lim2); 1.970 + } 1.971 + 1.972 + // --------- 1.973 + // Step 4: Clone the loop body. Move it inside the loop. This loop body 1.974 + // represents the odd iterations; since the loop trips an even number of 1.975 + // times its backedge is never taken. Kill the backedge. 1.976 + uint dd = dom_depth(loop_head); 1.977 + clone_loop( loop, old_new, dd ); 1.978 + 1.979 + // Make backedges of the clone equal to backedges of the original. 1.980 + // Make the fall-in from the original come from the fall-out of the clone. 1.981 + for (DUIterator_Fast jmax, j = loop_head->fast_outs(jmax); j < jmax; j++) { 1.982 + Node* phi = loop_head->fast_out(j); 1.983 + if( phi->is_Phi() && phi->in(0) == loop_head && phi->outcnt() > 0 ) { 1.984 + Node *newphi = old_new[phi->_idx]; 1.985 + _igvn.hash_delete( phi ); 1.986 + _igvn.hash_delete( newphi ); 1.987 + 1.988 + phi ->set_req(LoopNode:: EntryControl, newphi->in(LoopNode::LoopBackControl)); 1.989 + newphi->set_req(LoopNode::LoopBackControl, phi ->in(LoopNode::LoopBackControl)); 1.990 + phi ->set_req(LoopNode::LoopBackControl, C->top()); 1.991 + } 1.992 + } 1.993 + Node *clone_head = old_new[loop_head->_idx]; 1.994 + _igvn.hash_delete( clone_head ); 1.995 + loop_head ->set_req(LoopNode:: EntryControl, clone_head->in(LoopNode::LoopBackControl)); 1.996 + clone_head->set_req(LoopNode::LoopBackControl, loop_head ->in(LoopNode::LoopBackControl)); 1.997 + loop_head ->set_req(LoopNode::LoopBackControl, C->top()); 1.998 + loop->_head = clone_head; // New loop header 1.999 + 1.1000 + set_idom(loop_head, loop_head ->in(LoopNode::EntryControl), dd); 1.1001 + set_idom(clone_head, clone_head->in(LoopNode::EntryControl), dd); 1.1002 + 1.1003 + // Kill the clone's backedge 1.1004 + Node *newcle = old_new[loop_end->_idx]; 1.1005 + _igvn.hash_delete( newcle ); 1.1006 + Node *one = _igvn.intcon(1); 1.1007 + set_ctrl(one, C->root()); 1.1008 + newcle->set_req(1, one); 1.1009 + // Force clone into same loop body 1.1010 + uint max = loop->_body.size(); 1.1011 + for( uint k = 0; k < max; k++ ) { 1.1012 + Node *old = loop->_body.at(k); 1.1013 + Node *nnn = old_new[old->_idx]; 1.1014 + loop->_body.push(nnn); 1.1015 + if (!has_ctrl(old)) 1.1016 + set_loop(nnn, loop); 1.1017 + } 1.1018 +} 1.1019 + 1.1020 +//------------------------------do_maximally_unroll---------------------------- 1.1021 + 1.1022 +void PhaseIdealLoop::do_maximally_unroll( IdealLoopTree *loop, Node_List &old_new ) { 1.1023 + CountedLoopNode *cl = loop->_head->as_CountedLoop(); 1.1024 + assert( cl->trip_count() > 0, ""); 1.1025 + 1.1026 + // If loop is tripping an odd number of times, peel odd iteration 1.1027 + if( (cl->trip_count() & 1) == 1 ) { 1.1028 + do_peeling( loop, old_new ); 1.1029 + } 1.1030 + 1.1031 + // Now its tripping an even number of times remaining. Double loop body. 1.1032 + // Do not adjust pre-guards; they are not needed and do not exist. 1.1033 + if( cl->trip_count() > 0 ) { 1.1034 + do_unroll( loop, old_new, false ); 1.1035 + } 1.1036 +} 1.1037 + 1.1038 +//------------------------------dominates_backedge--------------------------------- 1.1039 +// Returns true if ctrl is executed on every complete iteration 1.1040 +bool IdealLoopTree::dominates_backedge(Node* ctrl) { 1.1041 + assert(ctrl->is_CFG(), "must be control"); 1.1042 + Node* backedge = _head->as_Loop()->in(LoopNode::LoopBackControl); 1.1043 + return _phase->dom_lca_internal(ctrl, backedge) == ctrl; 1.1044 +} 1.1045 + 1.1046 +//------------------------------add_constraint--------------------------------- 1.1047 +// Constrain the main loop iterations so the condition: 1.1048 +// scale_con * I + offset < limit 1.1049 +// always holds true. That is, either increase the number of iterations in 1.1050 +// the pre-loop or the post-loop until the condition holds true in the main 1.1051 +// loop. Stride, scale, offset and limit are all loop invariant. Further, 1.1052 +// stride and scale are constants (offset and limit often are). 1.1053 +void PhaseIdealLoop::add_constraint( int stride_con, int scale_con, Node *offset, Node *limit, Node *pre_ctrl, Node **pre_limit, Node **main_limit ) { 1.1054 + 1.1055 + // Compute "I :: (limit-offset)/scale_con" 1.1056 + Node *con = new (C, 3) SubINode( limit, offset ); 1.1057 + register_new_node( con, pre_ctrl ); 1.1058 + Node *scale = _igvn.intcon(scale_con); 1.1059 + set_ctrl(scale, C->root()); 1.1060 + Node *X = new (C, 3) DivINode( 0, con, scale ); 1.1061 + register_new_node( X, pre_ctrl ); 1.1062 + 1.1063 + // For positive stride, the pre-loop limit always uses a MAX function 1.1064 + // and the main loop a MIN function. For negative stride these are 1.1065 + // reversed. 1.1066 + 1.1067 + // Also for positive stride*scale the affine function is increasing, so the 1.1068 + // pre-loop must check for underflow and the post-loop for overflow. 1.1069 + // Negative stride*scale reverses this; pre-loop checks for overflow and 1.1070 + // post-loop for underflow. 1.1071 + if( stride_con*scale_con > 0 ) { 1.1072 + // Compute I < (limit-offset)/scale_con 1.1073 + // Adjust main-loop last iteration to be MIN/MAX(main_loop,X) 1.1074 + *main_limit = (stride_con > 0) 1.1075 + ? (Node*)(new (C, 3) MinINode( *main_limit, X )) 1.1076 + : (Node*)(new (C, 3) MaxINode( *main_limit, X )); 1.1077 + register_new_node( *main_limit, pre_ctrl ); 1.1078 + 1.1079 + } else { 1.1080 + // Compute (limit-offset)/scale_con + SGN(-scale_con) <= I 1.1081 + // Add the negation of the main-loop constraint to the pre-loop. 1.1082 + // See footnote [++] below for a derivation of the limit expression. 1.1083 + Node *incr = _igvn.intcon(scale_con > 0 ? -1 : 1); 1.1084 + set_ctrl(incr, C->root()); 1.1085 + Node *adj = new (C, 3) AddINode( X, incr ); 1.1086 + register_new_node( adj, pre_ctrl ); 1.1087 + *pre_limit = (scale_con > 0) 1.1088 + ? (Node*)new (C, 3) MinINode( *pre_limit, adj ) 1.1089 + : (Node*)new (C, 3) MaxINode( *pre_limit, adj ); 1.1090 + register_new_node( *pre_limit, pre_ctrl ); 1.1091 + 1.1092 +// [++] Here's the algebra that justifies the pre-loop limit expression: 1.1093 +// 1.1094 +// NOT( scale_con * I + offset < limit ) 1.1095 +// == 1.1096 +// scale_con * I + offset >= limit 1.1097 +// == 1.1098 +// SGN(scale_con) * I >= (limit-offset)/|scale_con| 1.1099 +// == 1.1100 +// (limit-offset)/|scale_con| <= I * SGN(scale_con) 1.1101 +// == 1.1102 +// (limit-offset)/|scale_con|-1 < I * SGN(scale_con) 1.1103 +// == 1.1104 +// ( if (scale_con > 0) /*common case*/ 1.1105 +// (limit-offset)/scale_con - 1 < I 1.1106 +// else 1.1107 +// (limit-offset)/scale_con + 1 > I 1.1108 +// ) 1.1109 +// ( if (scale_con > 0) /*common case*/ 1.1110 +// (limit-offset)/scale_con + SGN(-scale_con) < I 1.1111 +// else 1.1112 +// (limit-offset)/scale_con + SGN(-scale_con) > I 1.1113 + } 1.1114 +} 1.1115 + 1.1116 + 1.1117 +//------------------------------is_scaled_iv--------------------------------- 1.1118 +// Return true if exp is a constant times an induction var 1.1119 +bool PhaseIdealLoop::is_scaled_iv(Node* exp, Node* iv, int* p_scale) { 1.1120 + if (exp == iv) { 1.1121 + if (p_scale != NULL) { 1.1122 + *p_scale = 1; 1.1123 + } 1.1124 + return true; 1.1125 + } 1.1126 + int opc = exp->Opcode(); 1.1127 + if (opc == Op_MulI) { 1.1128 + if (exp->in(1) == iv && exp->in(2)->is_Con()) { 1.1129 + if (p_scale != NULL) { 1.1130 + *p_scale = exp->in(2)->get_int(); 1.1131 + } 1.1132 + return true; 1.1133 + } 1.1134 + if (exp->in(2) == iv && exp->in(1)->is_Con()) { 1.1135 + if (p_scale != NULL) { 1.1136 + *p_scale = exp->in(1)->get_int(); 1.1137 + } 1.1138 + return true; 1.1139 + } 1.1140 + } else if (opc == Op_LShiftI) { 1.1141 + if (exp->in(1) == iv && exp->in(2)->is_Con()) { 1.1142 + if (p_scale != NULL) { 1.1143 + *p_scale = 1 << exp->in(2)->get_int(); 1.1144 + } 1.1145 + return true; 1.1146 + } 1.1147 + } 1.1148 + return false; 1.1149 +} 1.1150 + 1.1151 +//-----------------------------is_scaled_iv_plus_offset------------------------------ 1.1152 +// Return true if exp is a simple induction variable expression: k1*iv + (invar + k2) 1.1153 +bool PhaseIdealLoop::is_scaled_iv_plus_offset(Node* exp, Node* iv, int* p_scale, Node** p_offset, int depth) { 1.1154 + if (is_scaled_iv(exp, iv, p_scale)) { 1.1155 + if (p_offset != NULL) { 1.1156 + Node *zero = _igvn.intcon(0); 1.1157 + set_ctrl(zero, C->root()); 1.1158 + *p_offset = zero; 1.1159 + } 1.1160 + return true; 1.1161 + } 1.1162 + int opc = exp->Opcode(); 1.1163 + if (opc == Op_AddI) { 1.1164 + if (is_scaled_iv(exp->in(1), iv, p_scale)) { 1.1165 + if (p_offset != NULL) { 1.1166 + *p_offset = exp->in(2); 1.1167 + } 1.1168 + return true; 1.1169 + } 1.1170 + if (exp->in(2)->is_Con()) { 1.1171 + Node* offset2 = NULL; 1.1172 + if (depth < 2 && 1.1173 + is_scaled_iv_plus_offset(exp->in(1), iv, p_scale, 1.1174 + p_offset != NULL ? &offset2 : NULL, depth+1)) { 1.1175 + if (p_offset != NULL) { 1.1176 + Node *ctrl_off2 = get_ctrl(offset2); 1.1177 + Node* offset = new (C, 3) AddINode(offset2, exp->in(2)); 1.1178 + register_new_node(offset, ctrl_off2); 1.1179 + *p_offset = offset; 1.1180 + } 1.1181 + return true; 1.1182 + } 1.1183 + } 1.1184 + } else if (opc == Op_SubI) { 1.1185 + if (is_scaled_iv(exp->in(1), iv, p_scale)) { 1.1186 + if (p_offset != NULL) { 1.1187 + Node *zero = _igvn.intcon(0); 1.1188 + set_ctrl(zero, C->root()); 1.1189 + Node *ctrl_off = get_ctrl(exp->in(2)); 1.1190 + Node* offset = new (C, 3) SubINode(zero, exp->in(2)); 1.1191 + register_new_node(offset, ctrl_off); 1.1192 + *p_offset = offset; 1.1193 + } 1.1194 + return true; 1.1195 + } 1.1196 + if (is_scaled_iv(exp->in(2), iv, p_scale)) { 1.1197 + if (p_offset != NULL) { 1.1198 + *p_scale *= -1; 1.1199 + *p_offset = exp->in(1); 1.1200 + } 1.1201 + return true; 1.1202 + } 1.1203 + } 1.1204 + return false; 1.1205 +} 1.1206 + 1.1207 +//------------------------------do_range_check--------------------------------- 1.1208 +// Eliminate range-checks and other trip-counter vs loop-invariant tests. 1.1209 +void PhaseIdealLoop::do_range_check( IdealLoopTree *loop, Node_List &old_new ) { 1.1210 +#ifndef PRODUCT 1.1211 + if( PrintOpto && VerifyLoopOptimizations ) { 1.1212 + tty->print("Range Check Elimination "); 1.1213 + loop->dump_head(); 1.1214 + } 1.1215 +#endif 1.1216 + assert( RangeCheckElimination, "" ); 1.1217 + CountedLoopNode *cl = loop->_head->as_CountedLoop(); 1.1218 + assert( cl->is_main_loop(), "" ); 1.1219 + 1.1220 + // Find the trip counter; we are iteration splitting based on it 1.1221 + Node *trip_counter = cl->phi(); 1.1222 + // Find the main loop limit; we will trim it's iterations 1.1223 + // to not ever trip end tests 1.1224 + Node *main_limit = cl->limit(); 1.1225 + // Find the pre-loop limit; we will expand it's iterations to 1.1226 + // not ever trip low tests. 1.1227 + Node *ctrl = cl->in(LoopNode::EntryControl); 1.1228 + assert( ctrl->Opcode() == Op_IfTrue || ctrl->Opcode() == Op_IfFalse, "" ); 1.1229 + Node *iffm = ctrl->in(0); 1.1230 + assert( iffm->Opcode() == Op_If, "" ); 1.1231 + Node *p_f = iffm->in(0); 1.1232 + assert( p_f->Opcode() == Op_IfFalse, "" ); 1.1233 + CountedLoopEndNode *pre_end = p_f->in(0)->as_CountedLoopEnd(); 1.1234 + assert( pre_end->loopnode()->is_pre_loop(), "" ); 1.1235 + Node *pre_opaq1 = pre_end->limit(); 1.1236 + // Occasionally it's possible for a pre-loop Opaque1 node to be 1.1237 + // optimized away and then another round of loop opts attempted. 1.1238 + // We can not optimize this particular loop in that case. 1.1239 + if( pre_opaq1->Opcode() != Op_Opaque1 ) 1.1240 + return; 1.1241 + Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1; 1.1242 + Node *pre_limit = pre_opaq->in(1); 1.1243 + 1.1244 + // Where do we put new limit calculations 1.1245 + Node *pre_ctrl = pre_end->loopnode()->in(LoopNode::EntryControl); 1.1246 + 1.1247 + // Ensure the original loop limit is available from the 1.1248 + // pre-loop Opaque1 node. 1.1249 + Node *orig_limit = pre_opaq->original_loop_limit(); 1.1250 + if( orig_limit == NULL || _igvn.type(orig_limit) == Type::TOP ) 1.1251 + return; 1.1252 + 1.1253 + // Need to find the main-loop zero-trip guard 1.1254 + Node *bolzm = iffm->in(1); 1.1255 + assert( bolzm->Opcode() == Op_Bool, "" ); 1.1256 + Node *cmpzm = bolzm->in(1); 1.1257 + assert( cmpzm->is_Cmp(), "" ); 1.1258 + Node *opqzm = cmpzm->in(2); 1.1259 + if( opqzm->Opcode() != Op_Opaque1 ) 1.1260 + return; 1.1261 + assert( opqzm->in(1) == main_limit, "do not understand situation" ); 1.1262 + 1.1263 + // Must know if its a count-up or count-down loop 1.1264 + 1.1265 + // protect against stride not being a constant 1.1266 + if ( !cl->stride_is_con() ) { 1.1267 + return; 1.1268 + } 1.1269 + int stride_con = cl->stride_con(); 1.1270 + Node *zero = _igvn.intcon(0); 1.1271 + Node *one = _igvn.intcon(1); 1.1272 + set_ctrl(zero, C->root()); 1.1273 + set_ctrl(one, C->root()); 1.1274 + 1.1275 + // Range checks that do not dominate the loop backedge (ie. 1.1276 + // conditionally executed) can lengthen the pre loop limit beyond 1.1277 + // the original loop limit. To prevent this, the pre limit is 1.1278 + // (for stride > 0) MINed with the original loop limit (MAXed 1.1279 + // stride < 0) when some range_check (rc) is conditionally 1.1280 + // executed. 1.1281 + bool conditional_rc = false; 1.1282 + 1.1283 + // Check loop body for tests of trip-counter plus loop-invariant vs 1.1284 + // loop-invariant. 1.1285 + for( uint i = 0; i < loop->_body.size(); i++ ) { 1.1286 + Node *iff = loop->_body[i]; 1.1287 + if( iff->Opcode() == Op_If ) { // Test? 1.1288 + 1.1289 + // Test is an IfNode, has 2 projections. If BOTH are in the loop 1.1290 + // we need loop unswitching instead of iteration splitting. 1.1291 + Node *exit = loop->is_loop_exit(iff); 1.1292 + if( !exit ) continue; 1.1293 + int flip = (exit->Opcode() == Op_IfTrue) ? 1 : 0; 1.1294 + 1.1295 + // Get boolean condition to test 1.1296 + Node *i1 = iff->in(1); 1.1297 + if( !i1->is_Bool() ) continue; 1.1298 + BoolNode *bol = i1->as_Bool(); 1.1299 + BoolTest b_test = bol->_test; 1.1300 + // Flip sense of test if exit condition is flipped 1.1301 + if( flip ) 1.1302 + b_test = b_test.negate(); 1.1303 + 1.1304 + // Get compare 1.1305 + Node *cmp = bol->in(1); 1.1306 + 1.1307 + // Look for trip_counter + offset vs limit 1.1308 + Node *rc_exp = cmp->in(1); 1.1309 + Node *limit = cmp->in(2); 1.1310 + jint scale_con= 1; // Assume trip counter not scaled 1.1311 + 1.1312 + Node *limit_c = get_ctrl(limit); 1.1313 + if( loop->is_member(get_loop(limit_c) ) ) { 1.1314 + // Compare might have operands swapped; commute them 1.1315 + b_test = b_test.commute(); 1.1316 + rc_exp = cmp->in(2); 1.1317 + limit = cmp->in(1); 1.1318 + limit_c = get_ctrl(limit); 1.1319 + if( loop->is_member(get_loop(limit_c) ) ) 1.1320 + continue; // Both inputs are loop varying; cannot RCE 1.1321 + } 1.1322 + // Here we know 'limit' is loop invariant 1.1323 + 1.1324 + // 'limit' maybe pinned below the zero trip test (probably from a 1.1325 + // previous round of rce), in which case, it can't be used in the 1.1326 + // zero trip test expression which must occur before the zero test's if. 1.1327 + if( limit_c == ctrl ) { 1.1328 + continue; // Don't rce this check but continue looking for other candidates. 1.1329 + } 1.1330 + 1.1331 + // Check for scaled induction variable plus an offset 1.1332 + Node *offset = NULL; 1.1333 + 1.1334 + if (!is_scaled_iv_plus_offset(rc_exp, trip_counter, &scale_con, &offset)) { 1.1335 + continue; 1.1336 + } 1.1337 + 1.1338 + Node *offset_c = get_ctrl(offset); 1.1339 + if( loop->is_member( get_loop(offset_c) ) ) 1.1340 + continue; // Offset is not really loop invariant 1.1341 + // Here we know 'offset' is loop invariant. 1.1342 + 1.1343 + // As above for the 'limit', the 'offset' maybe pinned below the 1.1344 + // zero trip test. 1.1345 + if( offset_c == ctrl ) { 1.1346 + continue; // Don't rce this check but continue looking for other candidates. 1.1347 + } 1.1348 + 1.1349 + // At this point we have the expression as: 1.1350 + // scale_con * trip_counter + offset :: limit 1.1351 + // where scale_con, offset and limit are loop invariant. Trip_counter 1.1352 + // monotonically increases by stride_con, a constant. Both (or either) 1.1353 + // stride_con and scale_con can be negative which will flip about the 1.1354 + // sense of the test. 1.1355 + 1.1356 + // Adjust pre and main loop limits to guard the correct iteration set 1.1357 + if( cmp->Opcode() == Op_CmpU ) {// Unsigned compare is really 2 tests 1.1358 + if( b_test._test == BoolTest::lt ) { // Range checks always use lt 1.1359 + // The overflow limit: scale*I+offset < limit 1.1360 + add_constraint( stride_con, scale_con, offset, limit, pre_ctrl, &pre_limit, &main_limit ); 1.1361 + // The underflow limit: 0 <= scale*I+offset. 1.1362 + // Some math yields: -scale*I-(offset+1) < 0 1.1363 + Node *plus_one = new (C, 3) AddINode( offset, one ); 1.1364 + register_new_node( plus_one, pre_ctrl ); 1.1365 + Node *neg_offset = new (C, 3) SubINode( zero, plus_one ); 1.1366 + register_new_node( neg_offset, pre_ctrl ); 1.1367 + add_constraint( stride_con, -scale_con, neg_offset, zero, pre_ctrl, &pre_limit, &main_limit ); 1.1368 + if (!conditional_rc) { 1.1369 + conditional_rc = !loop->dominates_backedge(iff); 1.1370 + } 1.1371 + } else { 1.1372 +#ifndef PRODUCT 1.1373 + if( PrintOpto ) 1.1374 + tty->print_cr("missed RCE opportunity"); 1.1375 +#endif 1.1376 + continue; // In release mode, ignore it 1.1377 + } 1.1378 + } else { // Otherwise work on normal compares 1.1379 + switch( b_test._test ) { 1.1380 + case BoolTest::ge: // Convert X >= Y to -X <= -Y 1.1381 + scale_con = -scale_con; 1.1382 + offset = new (C, 3) SubINode( zero, offset ); 1.1383 + register_new_node( offset, pre_ctrl ); 1.1384 + limit = new (C, 3) SubINode( zero, limit ); 1.1385 + register_new_node( limit, pre_ctrl ); 1.1386 + // Fall into LE case 1.1387 + case BoolTest::le: // Convert X <= Y to X < Y+1 1.1388 + limit = new (C, 3) AddINode( limit, one ); 1.1389 + register_new_node( limit, pre_ctrl ); 1.1390 + // Fall into LT case 1.1391 + case BoolTest::lt: 1.1392 + add_constraint( stride_con, scale_con, offset, limit, pre_ctrl, &pre_limit, &main_limit ); 1.1393 + if (!conditional_rc) { 1.1394 + conditional_rc = !loop->dominates_backedge(iff); 1.1395 + } 1.1396 + break; 1.1397 + default: 1.1398 +#ifndef PRODUCT 1.1399 + if( PrintOpto ) 1.1400 + tty->print_cr("missed RCE opportunity"); 1.1401 +#endif 1.1402 + continue; // Unhandled case 1.1403 + } 1.1404 + } 1.1405 + 1.1406 + // Kill the eliminated test 1.1407 + C->set_major_progress(); 1.1408 + Node *kill_con = _igvn.intcon( 1-flip ); 1.1409 + set_ctrl(kill_con, C->root()); 1.1410 + _igvn.hash_delete(iff); 1.1411 + iff->set_req(1, kill_con); 1.1412 + _igvn._worklist.push(iff); 1.1413 + // Find surviving projection 1.1414 + assert(iff->is_If(), ""); 1.1415 + ProjNode* dp = ((IfNode*)iff)->proj_out(1-flip); 1.1416 + // Find loads off the surviving projection; remove their control edge 1.1417 + for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) { 1.1418 + Node* cd = dp->fast_out(i); // Control-dependent node 1.1419 + if( cd->is_Load() ) { // Loads can now float around in the loop 1.1420 + _igvn.hash_delete(cd); 1.1421 + // Allow the load to float around in the loop, or before it 1.1422 + // but NOT before the pre-loop. 1.1423 + cd->set_req(0, ctrl); // ctrl, not NULL 1.1424 + _igvn._worklist.push(cd); 1.1425 + --i; 1.1426 + --imax; 1.1427 + } 1.1428 + } 1.1429 + 1.1430 + } // End of is IF 1.1431 + 1.1432 + } 1.1433 + 1.1434 + // Update loop limits 1.1435 + if (conditional_rc) { 1.1436 + pre_limit = (stride_con > 0) ? (Node*)new (C,3) MinINode(pre_limit, orig_limit) 1.1437 + : (Node*)new (C,3) MaxINode(pre_limit, orig_limit); 1.1438 + register_new_node(pre_limit, pre_ctrl); 1.1439 + } 1.1440 + _igvn.hash_delete(pre_opaq); 1.1441 + pre_opaq->set_req(1, pre_limit); 1.1442 + 1.1443 + // Note:: we are making the main loop limit no longer precise; 1.1444 + // need to round up based on stride. 1.1445 + if( stride_con != 1 && stride_con != -1 ) { // Cutout for common case 1.1446 + // "Standard" round-up logic: ([main_limit-init+(y-1)]/y)*y+init 1.1447 + // Hopefully, compiler will optimize for powers of 2. 1.1448 + Node *ctrl = get_ctrl(main_limit); 1.1449 + Node *stride = cl->stride(); 1.1450 + Node *init = cl->init_trip(); 1.1451 + Node *span = new (C, 3) SubINode(main_limit,init); 1.1452 + register_new_node(span,ctrl); 1.1453 + Node *rndup = _igvn.intcon(stride_con + ((stride_con>0)?-1:1)); 1.1454 + Node *add = new (C, 3) AddINode(span,rndup); 1.1455 + register_new_node(add,ctrl); 1.1456 + Node *div = new (C, 3) DivINode(0,add,stride); 1.1457 + register_new_node(div,ctrl); 1.1458 + Node *mul = new (C, 3) MulINode(div,stride); 1.1459 + register_new_node(mul,ctrl); 1.1460 + Node *newlim = new (C, 3) AddINode(mul,init); 1.1461 + register_new_node(newlim,ctrl); 1.1462 + main_limit = newlim; 1.1463 + } 1.1464 + 1.1465 + Node *main_cle = cl->loopexit(); 1.1466 + Node *main_bol = main_cle->in(1); 1.1467 + // Hacking loop bounds; need private copies of exit test 1.1468 + if( main_bol->outcnt() > 1 ) {// BoolNode shared? 1.1469 + _igvn.hash_delete(main_cle); 1.1470 + main_bol = main_bol->clone();// Clone a private BoolNode 1.1471 + register_new_node( main_bol, main_cle->in(0) ); 1.1472 + main_cle->set_req(1,main_bol); 1.1473 + } 1.1474 + Node *main_cmp = main_bol->in(1); 1.1475 + if( main_cmp->outcnt() > 1 ) { // CmpNode shared? 1.1476 + _igvn.hash_delete(main_bol); 1.1477 + main_cmp = main_cmp->clone();// Clone a private CmpNode 1.1478 + register_new_node( main_cmp, main_cle->in(0) ); 1.1479 + main_bol->set_req(1,main_cmp); 1.1480 + } 1.1481 + // Hack the now-private loop bounds 1.1482 + _igvn.hash_delete(main_cmp); 1.1483 + main_cmp->set_req(2, main_limit); 1.1484 + _igvn._worklist.push(main_cmp); 1.1485 + // The OpaqueNode is unshared by design 1.1486 + _igvn.hash_delete(opqzm); 1.1487 + assert( opqzm->outcnt() == 1, "cannot hack shared node" ); 1.1488 + opqzm->set_req(1,main_limit); 1.1489 + _igvn._worklist.push(opqzm); 1.1490 +} 1.1491 + 1.1492 +//------------------------------DCE_loop_body---------------------------------- 1.1493 +// Remove simplistic dead code from loop body 1.1494 +void IdealLoopTree::DCE_loop_body() { 1.1495 + for( uint i = 0; i < _body.size(); i++ ) 1.1496 + if( _body.at(i)->outcnt() == 0 ) 1.1497 + _body.map( i--, _body.pop() ); 1.1498 +} 1.1499 + 1.1500 + 1.1501 +//------------------------------adjust_loop_exit_prob-------------------------- 1.1502 +// Look for loop-exit tests with the 50/50 (or worse) guesses from the parsing stage. 1.1503 +// Replace with a 1-in-10 exit guess. 1.1504 +void IdealLoopTree::adjust_loop_exit_prob( PhaseIdealLoop *phase ) { 1.1505 + Node *test = tail(); 1.1506 + while( test != _head ) { 1.1507 + uint top = test->Opcode(); 1.1508 + if( top == Op_IfTrue || top == Op_IfFalse ) { 1.1509 + int test_con = ((ProjNode*)test)->_con; 1.1510 + assert(top == (uint)(test_con? Op_IfTrue: Op_IfFalse), "sanity"); 1.1511 + IfNode *iff = test->in(0)->as_If(); 1.1512 + if( iff->outcnt() == 2 ) { // Ignore dead tests 1.1513 + Node *bol = iff->in(1); 1.1514 + if( bol && bol->req() > 1 && bol->in(1) && 1.1515 + ((bol->in(1)->Opcode() == Op_StorePConditional ) || 1.1516 + (bol->in(1)->Opcode() == Op_StoreLConditional ) || 1.1517 + (bol->in(1)->Opcode() == Op_CompareAndSwapI ) || 1.1518 + (bol->in(1)->Opcode() == Op_CompareAndSwapL ) || 1.1519 + (bol->in(1)->Opcode() == Op_CompareAndSwapP ))) 1.1520 + return; // Allocation loops RARELY take backedge 1.1521 + // Find the OTHER exit path from the IF 1.1522 + Node* ex = iff->proj_out(1-test_con); 1.1523 + float p = iff->_prob; 1.1524 + if( !phase->is_member( this, ex ) && iff->_fcnt == COUNT_UNKNOWN ) { 1.1525 + if( top == Op_IfTrue ) { 1.1526 + if( p < (PROB_FAIR + PROB_UNLIKELY_MAG(3))) { 1.1527 + iff->_prob = PROB_STATIC_FREQUENT; 1.1528 + } 1.1529 + } else { 1.1530 + if( p > (PROB_FAIR - PROB_UNLIKELY_MAG(3))) { 1.1531 + iff->_prob = PROB_STATIC_INFREQUENT; 1.1532 + } 1.1533 + } 1.1534 + } 1.1535 + } 1.1536 + } 1.1537 + test = phase->idom(test); 1.1538 + } 1.1539 +} 1.1540 + 1.1541 + 1.1542 +//------------------------------policy_do_remove_empty_loop-------------------- 1.1543 +// Micro-benchmark spamming. Policy is to always remove empty loops. 1.1544 +// The 'DO' part is to replace the trip counter with the value it will 1.1545 +// have on the last iteration. This will break the loop. 1.1546 +bool IdealLoopTree::policy_do_remove_empty_loop( PhaseIdealLoop *phase ) { 1.1547 + // Minimum size must be empty loop 1.1548 + if( _body.size() > 7/*number of nodes in an empty loop*/ ) return false; 1.1549 + 1.1550 + if( !_head->is_CountedLoop() ) return false; // Dead loop 1.1551 + CountedLoopNode *cl = _head->as_CountedLoop(); 1.1552 + if( !cl->loopexit() ) return false; // Malformed loop 1.1553 + if( !phase->is_member(this,phase->get_ctrl(cl->loopexit()->in(CountedLoopEndNode::TestValue)) ) ) 1.1554 + return false; // Infinite loop 1.1555 +#ifndef PRODUCT 1.1556 + if( PrintOpto ) 1.1557 + tty->print_cr("Removing empty loop"); 1.1558 +#endif 1.1559 +#ifdef ASSERT 1.1560 + // Ensure only one phi which is the iv. 1.1561 + Node* iv = NULL; 1.1562 + for (DUIterator_Fast imax, i = cl->fast_outs(imax); i < imax; i++) { 1.1563 + Node* n = cl->fast_out(i); 1.1564 + if (n->Opcode() == Op_Phi) { 1.1565 + assert(iv == NULL, "Too many phis" ); 1.1566 + iv = n; 1.1567 + } 1.1568 + } 1.1569 + assert(iv == cl->phi(), "Wrong phi" ); 1.1570 +#endif 1.1571 + // Replace the phi at loop head with the final value of the last 1.1572 + // iteration. Then the CountedLoopEnd will collapse (backedge never 1.1573 + // taken) and all loop-invariant uses of the exit values will be correct. 1.1574 + Node *phi = cl->phi(); 1.1575 + Node *final = new (phase->C, 3) SubINode( cl->limit(), cl->stride() ); 1.1576 + phase->register_new_node(final,cl->in(LoopNode::EntryControl)); 1.1577 + phase->_igvn.hash_delete(phi); 1.1578 + phase->_igvn.subsume_node(phi,final); 1.1579 + phase->C->set_major_progress(); 1.1580 + return true; 1.1581 +} 1.1582 + 1.1583 + 1.1584 +//============================================================================= 1.1585 +//------------------------------iteration_split_impl--------------------------- 1.1586 +void IdealLoopTree::iteration_split_impl( PhaseIdealLoop *phase, Node_List &old_new ) { 1.1587 + // Check and remove empty loops (spam micro-benchmarks) 1.1588 + if( policy_do_remove_empty_loop(phase) ) 1.1589 + return; // Here we removed an empty loop 1.1590 + 1.1591 + bool should_peel = policy_peeling(phase); // Should we peel? 1.1592 + 1.1593 + bool should_unswitch = policy_unswitching(phase); 1.1594 + 1.1595 + // Non-counted loops may be peeled; exactly 1 iteration is peeled. 1.1596 + // This removes loop-invariant tests (usually null checks). 1.1597 + if( !_head->is_CountedLoop() ) { // Non-counted loop 1.1598 + if (PartialPeelLoop && phase->partial_peel(this, old_new)) { 1.1599 + return; 1.1600 + } 1.1601 + if( should_peel ) { // Should we peel? 1.1602 +#ifndef PRODUCT 1.1603 + if (PrintOpto) tty->print_cr("should_peel"); 1.1604 +#endif 1.1605 + phase->do_peeling(this,old_new); 1.1606 + } else if( should_unswitch ) { 1.1607 + phase->do_unswitching(this, old_new); 1.1608 + } 1.1609 + return; 1.1610 + } 1.1611 + CountedLoopNode *cl = _head->as_CountedLoop(); 1.1612 + 1.1613 + if( !cl->loopexit() ) return; // Ignore various kinds of broken loops 1.1614 + 1.1615 + // Do nothing special to pre- and post- loops 1.1616 + if( cl->is_pre_loop() || cl->is_post_loop() ) return; 1.1617 + 1.1618 + // Compute loop trip count from profile data 1.1619 + compute_profile_trip_cnt(phase); 1.1620 + 1.1621 + // Before attempting fancy unrolling, RCE or alignment, see if we want 1.1622 + // to completely unroll this loop or do loop unswitching. 1.1623 + if( cl->is_normal_loop() ) { 1.1624 + bool should_maximally_unroll = policy_maximally_unroll(phase); 1.1625 + if( should_maximally_unroll ) { 1.1626 + // Here we did some unrolling and peeling. Eventually we will 1.1627 + // completely unroll this loop and it will no longer be a loop. 1.1628 + phase->do_maximally_unroll(this,old_new); 1.1629 + return; 1.1630 + } 1.1631 + if (should_unswitch) { 1.1632 + phase->do_unswitching(this, old_new); 1.1633 + return; 1.1634 + } 1.1635 + } 1.1636 + 1.1637 + 1.1638 + // Counted loops may be peeled, may need some iterations run up 1.1639 + // front for RCE, and may want to align loop refs to a cache 1.1640 + // line. Thus we clone a full loop up front whose trip count is 1.1641 + // at least 1 (if peeling), but may be several more. 1.1642 + 1.1643 + // The main loop will start cache-line aligned with at least 1 1.1644 + // iteration of the unrolled body (zero-trip test required) and 1.1645 + // will have some range checks removed. 1.1646 + 1.1647 + // A post-loop will finish any odd iterations (leftover after 1.1648 + // unrolling), plus any needed for RCE purposes. 1.1649 + 1.1650 + bool should_unroll = policy_unroll(phase); 1.1651 + 1.1652 + bool should_rce = policy_range_check(phase); 1.1653 + 1.1654 + bool should_align = policy_align(phase); 1.1655 + 1.1656 + // If not RCE'ing (iteration splitting) or Aligning, then we do not 1.1657 + // need a pre-loop. We may still need to peel an initial iteration but 1.1658 + // we will not be needing an unknown number of pre-iterations. 1.1659 + // 1.1660 + // Basically, if may_rce_align reports FALSE first time through, 1.1661 + // we will not be able to later do RCE or Aligning on this loop. 1.1662 + bool may_rce_align = !policy_peel_only(phase) || should_rce || should_align; 1.1663 + 1.1664 + // If we have any of these conditions (RCE, alignment, unrolling) met, then 1.1665 + // we switch to the pre-/main-/post-loop model. This model also covers 1.1666 + // peeling. 1.1667 + if( should_rce || should_align || should_unroll ) { 1.1668 + if( cl->is_normal_loop() ) // Convert to 'pre/main/post' loops 1.1669 + phase->insert_pre_post_loops(this,old_new, !may_rce_align); 1.1670 + 1.1671 + // Adjust the pre- and main-loop limits to let the pre and post loops run 1.1672 + // with full checks, but the main-loop with no checks. Remove said 1.1673 + // checks from the main body. 1.1674 + if( should_rce ) 1.1675 + phase->do_range_check(this,old_new); 1.1676 + 1.1677 + // Double loop body for unrolling. Adjust the minimum-trip test (will do 1.1678 + // twice as many iterations as before) and the main body limit (only do 1.1679 + // an even number of trips). If we are peeling, we might enable some RCE 1.1680 + // and we'd rather unroll the post-RCE'd loop SO... do not unroll if 1.1681 + // peeling. 1.1682 + if( should_unroll && !should_peel ) 1.1683 + phase->do_unroll(this,old_new, true); 1.1684 + 1.1685 + // Adjust the pre-loop limits to align the main body 1.1686 + // iterations. 1.1687 + if( should_align ) 1.1688 + Unimplemented(); 1.1689 + 1.1690 + } else { // Else we have an unchanged counted loop 1.1691 + if( should_peel ) // Might want to peel but do nothing else 1.1692 + phase->do_peeling(this,old_new); 1.1693 + } 1.1694 +} 1.1695 + 1.1696 + 1.1697 +//============================================================================= 1.1698 +//------------------------------iteration_split-------------------------------- 1.1699 +void IdealLoopTree::iteration_split( PhaseIdealLoop *phase, Node_List &old_new ) { 1.1700 + // Recursively iteration split nested loops 1.1701 + if( _child ) _child->iteration_split( phase, old_new ); 1.1702 + 1.1703 + // Clean out prior deadwood 1.1704 + DCE_loop_body(); 1.1705 + 1.1706 + 1.1707 + // Look for loop-exit tests with my 50/50 guesses from the Parsing stage. 1.1708 + // Replace with a 1-in-10 exit guess. 1.1709 + if( _parent /*not the root loop*/ && 1.1710 + !_irreducible && 1.1711 + // Also ignore the occasional dead backedge 1.1712 + !tail()->is_top() ) { 1.1713 + adjust_loop_exit_prob(phase); 1.1714 + } 1.1715 + 1.1716 + 1.1717 + // Gate unrolling, RCE and peeling efforts. 1.1718 + if( !_child && // If not an inner loop, do not split 1.1719 + !_irreducible && 1.1720 + !tail()->is_top() ) { // Also ignore the occasional dead backedge 1.1721 + if (!_has_call) { 1.1722 + iteration_split_impl( phase, old_new ); 1.1723 + } else if (policy_unswitching(phase)) { 1.1724 + phase->do_unswitching(this, old_new); 1.1725 + } 1.1726 + } 1.1727 + 1.1728 + // Minor offset re-organization to remove loop-fallout uses of 1.1729 + // trip counter. 1.1730 + if( _head->is_CountedLoop() ) phase->reorg_offsets( this ); 1.1731 + if( _next ) _next->iteration_split( phase, old_new ); 1.1732 +}