annotate src/share/vm/opto/loopTransform.cpp @ 1563:c18cbe5936b8

6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
author trims
date Thu, 27 May 2010 19:08:38 -0700
parents c047da02984c
children 6027dddc26c6
rev   line source
duke@0 1 /*
trims@1563 2 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1563 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1563 20 * or visit www.oracle.com if you need additional information or have any
trims@1563 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 #include "incls/_precompiled.incl"
duke@0 26 #include "incls/_loopTransform.cpp.incl"
duke@0 27
duke@0 28 //------------------------------is_loop_exit-----------------------------------
duke@0 29 // Given an IfNode, return the loop-exiting projection or NULL if both
duke@0 30 // arms remain in the loop.
duke@0 31 Node *IdealLoopTree::is_loop_exit(Node *iff) const {
duke@0 32 if( iff->outcnt() != 2 ) return NULL; // Ignore partially dead tests
duke@0 33 PhaseIdealLoop *phase = _phase;
duke@0 34 // Test is an IfNode, has 2 projections. If BOTH are in the loop
duke@0 35 // we need loop unswitching instead of peeling.
duke@0 36 if( !is_member(phase->get_loop( iff->raw_out(0) )) )
duke@0 37 return iff->raw_out(0);
duke@0 38 if( !is_member(phase->get_loop( iff->raw_out(1) )) )
duke@0 39 return iff->raw_out(1);
duke@0 40 return NULL;
duke@0 41 }
duke@0 42
duke@0 43
duke@0 44 //=============================================================================
duke@0 45
duke@0 46
duke@0 47 //------------------------------record_for_igvn----------------------------
duke@0 48 // Put loop body on igvn work list
duke@0 49 void IdealLoopTree::record_for_igvn() {
duke@0 50 for( uint i = 0; i < _body.size(); i++ ) {
duke@0 51 Node *n = _body.at(i);
duke@0 52 _phase->_igvn._worklist.push(n);
duke@0 53 }
duke@0 54 }
duke@0 55
duke@0 56 //------------------------------compute_profile_trip_cnt----------------------------
duke@0 57 // Compute loop trip count from profile data as
duke@0 58 // (backedge_count + loop_exit_count) / loop_exit_count
duke@0 59 void IdealLoopTree::compute_profile_trip_cnt( PhaseIdealLoop *phase ) {
duke@0 60 if (!_head->is_CountedLoop()) {
duke@0 61 return;
duke@0 62 }
duke@0 63 CountedLoopNode* head = _head->as_CountedLoop();
duke@0 64 if (head->profile_trip_cnt() != COUNT_UNKNOWN) {
duke@0 65 return; // Already computed
duke@0 66 }
duke@0 67 float trip_cnt = (float)max_jint; // default is big
duke@0 68
duke@0 69 Node* back = head->in(LoopNode::LoopBackControl);
duke@0 70 while (back != head) {
duke@0 71 if ((back->Opcode() == Op_IfTrue || back->Opcode() == Op_IfFalse) &&
duke@0 72 back->in(0) &&
duke@0 73 back->in(0)->is_If() &&
duke@0 74 back->in(0)->as_If()->_fcnt != COUNT_UNKNOWN &&
duke@0 75 back->in(0)->as_If()->_prob != PROB_UNKNOWN) {
duke@0 76 break;
duke@0 77 }
duke@0 78 back = phase->idom(back);
duke@0 79 }
duke@0 80 if (back != head) {
duke@0 81 assert((back->Opcode() == Op_IfTrue || back->Opcode() == Op_IfFalse) &&
duke@0 82 back->in(0), "if-projection exists");
duke@0 83 IfNode* back_if = back->in(0)->as_If();
duke@0 84 float loop_back_cnt = back_if->_fcnt * back_if->_prob;
duke@0 85
duke@0 86 // Now compute a loop exit count
duke@0 87 float loop_exit_cnt = 0.0f;
duke@0 88 for( uint i = 0; i < _body.size(); i++ ) {
duke@0 89 Node *n = _body[i];
duke@0 90 if( n->is_If() ) {
duke@0 91 IfNode *iff = n->as_If();
duke@0 92 if( iff->_fcnt != COUNT_UNKNOWN && iff->_prob != PROB_UNKNOWN ) {
duke@0 93 Node *exit = is_loop_exit(iff);
duke@0 94 if( exit ) {
duke@0 95 float exit_prob = iff->_prob;
duke@0 96 if (exit->Opcode() == Op_IfFalse) exit_prob = 1.0 - exit_prob;
duke@0 97 if (exit_prob > PROB_MIN) {
duke@0 98 float exit_cnt = iff->_fcnt * exit_prob;
duke@0 99 loop_exit_cnt += exit_cnt;
duke@0 100 }
duke@0 101 }
duke@0 102 }
duke@0 103 }
duke@0 104 }
duke@0 105 if (loop_exit_cnt > 0.0f) {
duke@0 106 trip_cnt = (loop_back_cnt + loop_exit_cnt) / loop_exit_cnt;
duke@0 107 } else {
duke@0 108 // No exit count so use
duke@0 109 trip_cnt = loop_back_cnt;
duke@0 110 }
duke@0 111 }
duke@0 112 #ifndef PRODUCT
duke@0 113 if (TraceProfileTripCount) {
duke@0 114 tty->print_cr("compute_profile_trip_cnt lp: %d cnt: %f\n", head->_idx, trip_cnt);
duke@0 115 }
duke@0 116 #endif
duke@0 117 head->set_profile_trip_cnt(trip_cnt);
duke@0 118 }
duke@0 119
duke@0 120 //---------------------is_invariant_addition-----------------------------
duke@0 121 // Return nonzero index of invariant operand for an Add or Sub
twisti@643 122 // of (nonconstant) invariant and variant values. Helper for reassociate_invariants.
duke@0 123 int IdealLoopTree::is_invariant_addition(Node* n, PhaseIdealLoop *phase) {
duke@0 124 int op = n->Opcode();
duke@0 125 if (op == Op_AddI || op == Op_SubI) {
duke@0 126 bool in1_invar = this->is_invariant(n->in(1));
duke@0 127 bool in2_invar = this->is_invariant(n->in(2));
duke@0 128 if (in1_invar && !in2_invar) return 1;
duke@0 129 if (!in1_invar && in2_invar) return 2;
duke@0 130 }
duke@0 131 return 0;
duke@0 132 }
duke@0 133
duke@0 134 //---------------------reassociate_add_sub-----------------------------
duke@0 135 // Reassociate invariant add and subtract expressions:
duke@0 136 //
duke@0 137 // inv1 + (x + inv2) => ( inv1 + inv2) + x
duke@0 138 // (x + inv2) + inv1 => ( inv1 + inv2) + x
duke@0 139 // inv1 + (x - inv2) => ( inv1 - inv2) + x
duke@0 140 // inv1 - (inv2 - x) => ( inv1 - inv2) + x
duke@0 141 // (x + inv2) - inv1 => (-inv1 + inv2) + x
duke@0 142 // (x - inv2) + inv1 => ( inv1 - inv2) + x
duke@0 143 // (x - inv2) - inv1 => (-inv1 - inv2) + x
duke@0 144 // inv1 + (inv2 - x) => ( inv1 + inv2) - x
duke@0 145 // inv1 - (x - inv2) => ( inv1 + inv2) - x
duke@0 146 // (inv2 - x) + inv1 => ( inv1 + inv2) - x
duke@0 147 // (inv2 - x) - inv1 => (-inv1 + inv2) - x
duke@0 148 // inv1 - (x + inv2) => ( inv1 - inv2) - x
duke@0 149 //
duke@0 150 Node* IdealLoopTree::reassociate_add_sub(Node* n1, PhaseIdealLoop *phase) {
duke@0 151 if (!n1->is_Add() && !n1->is_Sub() || n1->outcnt() == 0) return NULL;
duke@0 152 if (is_invariant(n1)) return NULL;
duke@0 153 int inv1_idx = is_invariant_addition(n1, phase);
duke@0 154 if (!inv1_idx) return NULL;
duke@0 155 // Don't mess with add of constant (igvn moves them to expression tree root.)
duke@0 156 if (n1->is_Add() && n1->in(2)->is_Con()) return NULL;
duke@0 157 Node* inv1 = n1->in(inv1_idx);
duke@0 158 Node* n2 = n1->in(3 - inv1_idx);
duke@0 159 int inv2_idx = is_invariant_addition(n2, phase);
duke@0 160 if (!inv2_idx) return NULL;
duke@0 161 Node* x = n2->in(3 - inv2_idx);
duke@0 162 Node* inv2 = n2->in(inv2_idx);
duke@0 163
duke@0 164 bool neg_x = n2->is_Sub() && inv2_idx == 1;
duke@0 165 bool neg_inv2 = n2->is_Sub() && inv2_idx == 2;
duke@0 166 bool neg_inv1 = n1->is_Sub() && inv1_idx == 2;
duke@0 167 if (n1->is_Sub() && inv1_idx == 1) {
duke@0 168 neg_x = !neg_x;
duke@0 169 neg_inv2 = !neg_inv2;
duke@0 170 }
duke@0 171 Node* inv1_c = phase->get_ctrl(inv1);
duke@0 172 Node* inv2_c = phase->get_ctrl(inv2);
duke@0 173 Node* n_inv1;
duke@0 174 if (neg_inv1) {
duke@0 175 Node *zero = phase->_igvn.intcon(0);
duke@0 176 phase->set_ctrl(zero, phase->C->root());
duke@0 177 n_inv1 = new (phase->C, 3) SubINode(zero, inv1);
duke@0 178 phase->register_new_node(n_inv1, inv1_c);
duke@0 179 } else {
duke@0 180 n_inv1 = inv1;
duke@0 181 }
duke@0 182 Node* inv;
duke@0 183 if (neg_inv2) {
duke@0 184 inv = new (phase->C, 3) SubINode(n_inv1, inv2);
duke@0 185 } else {
duke@0 186 inv = new (phase->C, 3) AddINode(n_inv1, inv2);
duke@0 187 }
duke@0 188 phase->register_new_node(inv, phase->get_early_ctrl(inv));
duke@0 189
duke@0 190 Node* addx;
duke@0 191 if (neg_x) {
duke@0 192 addx = new (phase->C, 3) SubINode(inv, x);
duke@0 193 } else {
duke@0 194 addx = new (phase->C, 3) AddINode(x, inv);
duke@0 195 }
duke@0 196 phase->register_new_node(addx, phase->get_ctrl(x));
duke@0 197 phase->_igvn.hash_delete(n1);
duke@0 198 phase->_igvn.subsume_node(n1, addx);
duke@0 199 return addx;
duke@0 200 }
duke@0 201
duke@0 202 //---------------------reassociate_invariants-----------------------------
duke@0 203 // Reassociate invariant expressions:
duke@0 204 void IdealLoopTree::reassociate_invariants(PhaseIdealLoop *phase) {
duke@0 205 for (int i = _body.size() - 1; i >= 0; i--) {
duke@0 206 Node *n = _body.at(i);
duke@0 207 for (int j = 0; j < 5; j++) {
duke@0 208 Node* nn = reassociate_add_sub(n, phase);
duke@0 209 if (nn == NULL) break;
duke@0 210 n = nn; // again
duke@0 211 };
duke@0 212 }
duke@0 213 }
duke@0 214
duke@0 215 //------------------------------policy_peeling---------------------------------
duke@0 216 // Return TRUE or FALSE if the loop should be peeled or not. Peel if we can
duke@0 217 // make some loop-invariant test (usually a null-check) happen before the loop.
duke@0 218 bool IdealLoopTree::policy_peeling( PhaseIdealLoop *phase ) const {
duke@0 219 Node *test = ((IdealLoopTree*)this)->tail();
duke@0 220 int body_size = ((IdealLoopTree*)this)->_body.size();
duke@0 221 int uniq = phase->C->unique();
duke@0 222 // Peeling does loop cloning which can result in O(N^2) node construction
duke@0 223 if( body_size > 255 /* Prevent overflow for large body_size */
duke@0 224 || (body_size * body_size + uniq > MaxNodeLimit) ) {
duke@0 225 return false; // too large to safely clone
duke@0 226 }
duke@0 227 while( test != _head ) { // Scan till run off top of loop
duke@0 228 if( test->is_If() ) { // Test?
duke@0 229 Node *ctrl = phase->get_ctrl(test->in(1));
duke@0 230 if (ctrl->is_top())
duke@0 231 return false; // Found dead test on live IF? No peeling!
duke@0 232 // Standard IF only has one input value to check for loop invariance
duke@0 233 assert( test->Opcode() == Op_If || test->Opcode() == Op_CountedLoopEnd, "Check this code when new subtype is added");
duke@0 234 // Condition is not a member of this loop?
duke@0 235 if( !is_member(phase->get_loop(ctrl)) &&
duke@0 236 is_loop_exit(test) )
duke@0 237 return true; // Found reason to peel!
duke@0 238 }
duke@0 239 // Walk up dominators to loop _head looking for test which is
duke@0 240 // executed on every path thru loop.
duke@0 241 test = phase->idom(test);
duke@0 242 }
duke@0 243 return false;
duke@0 244 }
duke@0 245
duke@0 246 //------------------------------peeled_dom_test_elim---------------------------
duke@0 247 // If we got the effect of peeling, either by actually peeling or by making
duke@0 248 // a pre-loop which must execute at least once, we can remove all
duke@0 249 // loop-invariant dominated tests in the main body.
duke@0 250 void PhaseIdealLoop::peeled_dom_test_elim( IdealLoopTree *loop, Node_List &old_new ) {
duke@0 251 bool progress = true;
duke@0 252 while( progress ) {
duke@0 253 progress = false; // Reset for next iteration
duke@0 254 Node *prev = loop->_head->in(LoopNode::LoopBackControl);//loop->tail();
duke@0 255 Node *test = prev->in(0);
duke@0 256 while( test != loop->_head ) { // Scan till run off top of loop
duke@0 257
duke@0 258 int p_op = prev->Opcode();
duke@0 259 if( (p_op == Op_IfFalse || p_op == Op_IfTrue) &&
duke@0 260 test->is_If() && // Test?
duke@0 261 !test->in(1)->is_Con() && // And not already obvious?
duke@0 262 // Condition is not a member of this loop?
duke@0 263 !loop->is_member(get_loop(get_ctrl(test->in(1))))){
duke@0 264 // Walk loop body looking for instances of this test
duke@0 265 for( uint i = 0; i < loop->_body.size(); i++ ) {
duke@0 266 Node *n = loop->_body.at(i);
duke@0 267 if( n->is_If() && n->in(1) == test->in(1) /*&& n != loop->tail()->in(0)*/ ) {
duke@0 268 // IfNode was dominated by version in peeled loop body
duke@0 269 progress = true;
duke@0 270 dominated_by( old_new[prev->_idx], n );
duke@0 271 }
duke@0 272 }
duke@0 273 }
duke@0 274 prev = test;
duke@0 275 test = idom(test);
duke@0 276 } // End of scan tests in loop
duke@0 277
duke@0 278 } // End of while( progress )
duke@0 279 }
duke@0 280
duke@0 281 //------------------------------do_peeling-------------------------------------
duke@0 282 // Peel the first iteration of the given loop.
duke@0 283 // Step 1: Clone the loop body. The clone becomes the peeled iteration.
duke@0 284 // The pre-loop illegally has 2 control users (old & new loops).
duke@0 285 // Step 2: Make the old-loop fall-in edges point to the peeled iteration.
duke@0 286 // Do this by making the old-loop fall-in edges act as if they came
duke@0 287 // around the loopback from the prior iteration (follow the old-loop
duke@0 288 // backedges) and then map to the new peeled iteration. This leaves
duke@0 289 // the pre-loop with only 1 user (the new peeled iteration), but the
duke@0 290 // peeled-loop backedge has 2 users.
duke@0 291 // Step 3: Cut the backedge on the clone (so its not a loop) and remove the
duke@0 292 // extra backedge user.
duke@0 293 void PhaseIdealLoop::do_peeling( IdealLoopTree *loop, Node_List &old_new ) {
duke@0 294
duke@0 295 C->set_major_progress();
duke@0 296 // Peeling a 'main' loop in a pre/main/post situation obfuscates the
duke@0 297 // 'pre' loop from the main and the 'pre' can no longer have it's
duke@0 298 // iterations adjusted. Therefore, we need to declare this loop as
duke@0 299 // no longer a 'main' loop; it will need new pre and post loops before
duke@0 300 // we can do further RCE.
duke@0 301 Node *h = loop->_head;
duke@0 302 if( h->is_CountedLoop() ) {
duke@0 303 CountedLoopNode *cl = h->as_CountedLoop();
duke@0 304 assert(cl->trip_count() > 0, "peeling a fully unrolled loop");
duke@0 305 cl->set_trip_count(cl->trip_count() - 1);
duke@0 306 if( cl->is_main_loop() ) {
duke@0 307 cl->set_normal_loop();
duke@0 308 #ifndef PRODUCT
duke@0 309 if( PrintOpto && VerifyLoopOptimizations ) {
duke@0 310 tty->print("Peeling a 'main' loop; resetting to 'normal' ");
duke@0 311 loop->dump_head();
duke@0 312 }
duke@0 313 #endif
duke@0 314 }
duke@0 315 }
duke@0 316
duke@0 317 // Step 1: Clone the loop body. The clone becomes the peeled iteration.
duke@0 318 // The pre-loop illegally has 2 control users (old & new loops).
duke@0 319 clone_loop( loop, old_new, dom_depth(loop->_head) );
duke@0 320
duke@0 321
duke@0 322 // Step 2: Make the old-loop fall-in edges point to the peeled iteration.
duke@0 323 // Do this by making the old-loop fall-in edges act as if they came
duke@0 324 // around the loopback from the prior iteration (follow the old-loop
duke@0 325 // backedges) and then map to the new peeled iteration. This leaves
duke@0 326 // the pre-loop with only 1 user (the new peeled iteration), but the
duke@0 327 // peeled-loop backedge has 2 users.
duke@0 328 for (DUIterator_Fast jmax, j = loop->_head->fast_outs(jmax); j < jmax; j++) {
duke@0 329 Node* old = loop->_head->fast_out(j);
duke@0 330 if( old->in(0) == loop->_head && old->req() == 3 &&
duke@0 331 (old->is_Loop() || old->is_Phi()) ) {
duke@0 332 Node *new_exit_value = old_new[old->in(LoopNode::LoopBackControl)->_idx];
duke@0 333 if( !new_exit_value ) // Backedge value is ALSO loop invariant?
duke@0 334 // Then loop body backedge value remains the same.
duke@0 335 new_exit_value = old->in(LoopNode::LoopBackControl);
duke@0 336 _igvn.hash_delete(old);
duke@0 337 old->set_req(LoopNode::EntryControl, new_exit_value);
duke@0 338 }
duke@0 339 }
duke@0 340
duke@0 341
duke@0 342 // Step 3: Cut the backedge on the clone (so its not a loop) and remove the
duke@0 343 // extra backedge user.
duke@0 344 Node *nnn = old_new[loop->_head->_idx];
duke@0 345 _igvn.hash_delete(nnn);
duke@0 346 nnn->set_req(LoopNode::LoopBackControl, C->top());
duke@0 347 for (DUIterator_Fast j2max, j2 = nnn->fast_outs(j2max); j2 < j2max; j2++) {
duke@0 348 Node* use = nnn->fast_out(j2);
duke@0 349 if( use->in(0) == nnn && use->req() == 3 && use->is_Phi() ) {
duke@0 350 _igvn.hash_delete(use);
duke@0 351 use->set_req(LoopNode::LoopBackControl, C->top());
duke@0 352 }
duke@0 353 }
duke@0 354
duke@0 355
duke@0 356 // Step 4: Correct dom-depth info. Set to loop-head depth.
duke@0 357 int dd = dom_depth(loop->_head);
duke@0 358 set_idom(loop->_head, loop->_head->in(1), dd);
duke@0 359 for (uint j3 = 0; j3 < loop->_body.size(); j3++) {
duke@0 360 Node *old = loop->_body.at(j3);
duke@0 361 Node *nnn = old_new[old->_idx];
duke@0 362 if (!has_ctrl(nnn))
duke@0 363 set_idom(nnn, idom(nnn), dd-1);
duke@0 364 // While we're at it, remove any SafePoints from the peeled code
duke@0 365 if( old->Opcode() == Op_SafePoint ) {
duke@0 366 Node *nnn = old_new[old->_idx];
duke@0 367 lazy_replace(nnn,nnn->in(TypeFunc::Control));
duke@0 368 }
duke@0 369 }
duke@0 370
duke@0 371 // Now force out all loop-invariant dominating tests. The optimizer
duke@0 372 // finds some, but we _know_ they are all useless.
duke@0 373 peeled_dom_test_elim(loop,old_new);
duke@0 374
duke@0 375 loop->record_for_igvn();
duke@0 376 }
duke@0 377
duke@0 378 //------------------------------policy_maximally_unroll------------------------
duke@0 379 // Return exact loop trip count, or 0 if not maximally unrolling
duke@0 380 bool IdealLoopTree::policy_maximally_unroll( PhaseIdealLoop *phase ) const {
duke@0 381 CountedLoopNode *cl = _head->as_CountedLoop();
duke@0 382 assert( cl->is_normal_loop(), "" );
duke@0 383
duke@0 384 Node *init_n = cl->init_trip();
duke@0 385 Node *limit_n = cl->limit();
duke@0 386
duke@0 387 // Non-constant bounds
duke@0 388 if( init_n == NULL || !init_n->is_Con() ||
duke@0 389 limit_n == NULL || !limit_n->is_Con() ||
duke@0 390 // protect against stride not being a constant
duke@0 391 !cl->stride_is_con() ) {
duke@0 392 return false;
duke@0 393 }
duke@0 394 int init = init_n->get_int();
duke@0 395 int limit = limit_n->get_int();
duke@0 396 int span = limit - init;
duke@0 397 int stride = cl->stride_con();
duke@0 398
duke@0 399 if (init >= limit || stride > span) {
duke@0 400 // return a false (no maximally unroll) and the regular unroll/peel
duke@0 401 // route will make a small mess which CCP will fold away.
duke@0 402 return false;
duke@0 403 }
duke@0 404 uint trip_count = span/stride; // trip_count can be greater than 2 Gig.
duke@0 405 assert( (int)trip_count*stride == span, "must divide evenly" );
duke@0 406
duke@0 407 // Real policy: if we maximally unroll, does it get too big?
duke@0 408 // Allow the unrolled mess to get larger than standard loop
duke@0 409 // size. After all, it will no longer be a loop.
duke@0 410 uint body_size = _body.size();
duke@0 411 uint unroll_limit = (uint)LoopUnrollLimit * 4;
duke@0 412 assert( (intx)unroll_limit == LoopUnrollLimit * 4, "LoopUnrollLimit must fit in 32bits");
duke@0 413 cl->set_trip_count(trip_count);
duke@0 414 if( trip_count <= unroll_limit && body_size <= unroll_limit ) {
duke@0 415 uint new_body_size = body_size * trip_count;
duke@0 416 if (new_body_size <= unroll_limit &&
duke@0 417 body_size == new_body_size / trip_count &&
duke@0 418 // Unrolling can result in a large amount of node construction
duke@0 419 new_body_size < MaxNodeLimit - phase->C->unique()) {
duke@0 420 return true; // maximally unroll
duke@0 421 }
duke@0 422 }
duke@0 423
duke@0 424 return false; // Do not maximally unroll
duke@0 425 }
duke@0 426
duke@0 427
duke@0 428 //------------------------------policy_unroll----------------------------------
duke@0 429 // Return TRUE or FALSE if the loop should be unrolled or not. Unroll if
duke@0 430 // the loop is a CountedLoop and the body is small enough.
duke@0 431 bool IdealLoopTree::policy_unroll( PhaseIdealLoop *phase ) const {
duke@0 432
duke@0 433 CountedLoopNode *cl = _head->as_CountedLoop();
duke@0 434 assert( cl->is_normal_loop() || cl->is_main_loop(), "" );
duke@0 435
duke@0 436 // protect against stride not being a constant
duke@0 437 if( !cl->stride_is_con() ) return false;
duke@0 438
duke@0 439 // protect against over-unrolling
duke@0 440 if( cl->trip_count() <= 1 ) return false;
duke@0 441
duke@0 442 int future_unroll_ct = cl->unrolled_count() * 2;
duke@0 443
duke@0 444 // Don't unroll if the next round of unrolling would push us
duke@0 445 // over the expected trip count of the loop. One is subtracted
duke@0 446 // from the expected trip count because the pre-loop normally
duke@0 447 // executes 1 iteration.
duke@0 448 if (UnrollLimitForProfileCheck > 0 &&
duke@0 449 cl->profile_trip_cnt() != COUNT_UNKNOWN &&
duke@0 450 future_unroll_ct > UnrollLimitForProfileCheck &&
duke@0 451 (float)future_unroll_ct > cl->profile_trip_cnt() - 1.0) {
duke@0 452 return false;
duke@0 453 }
duke@0 454
duke@0 455 // When unroll count is greater than LoopUnrollMin, don't unroll if:
duke@0 456 // the residual iterations are more than 10% of the trip count
duke@0 457 // and rounds of "unroll,optimize" are not making significant progress
duke@0 458 // Progress defined as current size less than 20% larger than previous size.
duke@0 459 if (UseSuperWord && cl->node_count_before_unroll() > 0 &&
duke@0 460 future_unroll_ct > LoopUnrollMin &&
duke@0 461 (future_unroll_ct - 1) * 10.0 > cl->profile_trip_cnt() &&
duke@0 462 1.2 * cl->node_count_before_unroll() < (double)_body.size()) {
duke@0 463 return false;
duke@0 464 }
duke@0 465
duke@0 466 Node *init_n = cl->init_trip();
duke@0 467 Node *limit_n = cl->limit();
duke@0 468 // Non-constant bounds.
duke@0 469 // Protect against over-unrolling when init or/and limit are not constant
duke@0 470 // (so that trip_count's init value is maxint) but iv range is known.
duke@0 471 if( init_n == NULL || !init_n->is_Con() ||
duke@0 472 limit_n == NULL || !limit_n->is_Con() ) {
duke@0 473 Node* phi = cl->phi();
duke@0 474 if( phi != NULL ) {
duke@0 475 assert(phi->is_Phi() && phi->in(0) == _head, "Counted loop should have iv phi.");
duke@0 476 const TypeInt* iv_type = phase->_igvn.type(phi)->is_int();
duke@0 477 int next_stride = cl->stride_con() * 2; // stride after this unroll
duke@0 478 if( next_stride > 0 ) {
duke@0 479 if( iv_type->_lo + next_stride <= iv_type->_lo || // overflow
duke@0 480 iv_type->_lo + next_stride > iv_type->_hi ) {
duke@0 481 return false; // over-unrolling
duke@0 482 }
duke@0 483 } else if( next_stride < 0 ) {
duke@0 484 if( iv_type->_hi + next_stride >= iv_type->_hi || // overflow
duke@0 485 iv_type->_hi + next_stride < iv_type->_lo ) {
duke@0 486 return false; // over-unrolling
duke@0 487 }
duke@0 488 }
duke@0 489 }
duke@0 490 }
duke@0 491
duke@0 492 // Adjust body_size to determine if we unroll or not
duke@0 493 uint body_size = _body.size();
duke@0 494 // Key test to unroll CaffeineMark's Logic test
duke@0 495 int xors_in_loop = 0;
duke@0 496 // Also count ModL, DivL and MulL which expand mightly
duke@0 497 for( uint k = 0; k < _body.size(); k++ ) {
duke@0 498 switch( _body.at(k)->Opcode() ) {
duke@0 499 case Op_XorI: xors_in_loop++; break; // CaffeineMark's Logic test
duke@0 500 case Op_ModL: body_size += 30; break;
duke@0 501 case Op_DivL: body_size += 30; break;
duke@0 502 case Op_MulL: body_size += 10; break;
duke@0 503 }
duke@0 504 }
duke@0 505
duke@0 506 // Check for being too big
duke@0 507 if( body_size > (uint)LoopUnrollLimit ) {
duke@0 508 if( xors_in_loop >= 4 && body_size < (uint)LoopUnrollLimit*4) return true;
duke@0 509 // Normal case: loop too big
duke@0 510 return false;
duke@0 511 }
duke@0 512
duke@0 513 // Check for stride being a small enough constant
duke@0 514 if( abs(cl->stride_con()) > (1<<3) ) return false;
duke@0 515
duke@0 516 // Unroll once! (Each trip will soon do double iterations)
duke@0 517 return true;
duke@0 518 }
duke@0 519
duke@0 520 //------------------------------policy_align-----------------------------------
duke@0 521 // Return TRUE or FALSE if the loop should be cache-line aligned. Gather the
duke@0 522 // expression that does the alignment. Note that only one array base can be
twisti@643 523 // aligned in a loop (unless the VM guarantees mutual alignment). Note that
duke@0 524 // if we vectorize short memory ops into longer memory ops, we may want to
duke@0 525 // increase alignment.
duke@0 526 bool IdealLoopTree::policy_align( PhaseIdealLoop *phase ) const {
duke@0 527 return false;
duke@0 528 }
duke@0 529
duke@0 530 //------------------------------policy_range_check-----------------------------
duke@0 531 // Return TRUE or FALSE if the loop should be range-check-eliminated.
duke@0 532 // Actually we do iteration-splitting, a more powerful form of RCE.
duke@0 533 bool IdealLoopTree::policy_range_check( PhaseIdealLoop *phase ) const {
duke@0 534 if( !RangeCheckElimination ) return false;
duke@0 535
duke@0 536 CountedLoopNode *cl = _head->as_CountedLoop();
duke@0 537 // If we unrolled with no intention of doing RCE and we later
duke@0 538 // changed our minds, we got no pre-loop. Either we need to
duke@0 539 // make a new pre-loop, or we gotta disallow RCE.
duke@0 540 if( cl->is_main_no_pre_loop() ) return false; // Disallowed for now.
duke@0 541 Node *trip_counter = cl->phi();
duke@0 542
duke@0 543 // Check loop body for tests of trip-counter plus loop-invariant vs
duke@0 544 // loop-invariant.
duke@0 545 for( uint i = 0; i < _body.size(); i++ ) {
duke@0 546 Node *iff = _body[i];
duke@0 547 if( iff->Opcode() == Op_If ) { // Test?
duke@0 548
duke@0 549 // Comparing trip+off vs limit
duke@0 550 Node *bol = iff->in(1);
duke@0 551 if( bol->req() != 2 ) continue; // dead constant test
cfang@1250 552 if (!bol->is_Bool()) {
cfang@1250 553 assert(UseLoopPredicate && bol->Opcode() == Op_Conv2B, "predicate check only");
cfang@1250 554 continue;
cfang@1250 555 }
duke@0 556 Node *cmp = bol->in(1);
duke@0 557
duke@0 558 Node *rc_exp = cmp->in(1);
duke@0 559 Node *limit = cmp->in(2);
duke@0 560
duke@0 561 Node *limit_c = phase->get_ctrl(limit);
duke@0 562 if( limit_c == phase->C->top() )
duke@0 563 return false; // Found dead test on live IF? No RCE!
duke@0 564 if( is_member(phase->get_loop(limit_c) ) ) {
duke@0 565 // Compare might have operands swapped; commute them
duke@0 566 rc_exp = cmp->in(2);
duke@0 567 limit = cmp->in(1);
duke@0 568 limit_c = phase->get_ctrl(limit);
duke@0 569 if( is_member(phase->get_loop(limit_c) ) )
duke@0 570 continue; // Both inputs are loop varying; cannot RCE
duke@0 571 }
duke@0 572
duke@0 573 if (!phase->is_scaled_iv_plus_offset(rc_exp, trip_counter, NULL, NULL)) {
duke@0 574 continue;
duke@0 575 }
duke@0 576 // Yeah! Found a test like 'trip+off vs limit'
duke@0 577 // Test is an IfNode, has 2 projections. If BOTH are in the loop
duke@0 578 // we need loop unswitching instead of iteration splitting.
duke@0 579 if( is_loop_exit(iff) )
duke@0 580 return true; // Found reason to split iterations
duke@0 581 } // End of is IF
duke@0 582 }
duke@0 583
duke@0 584 return false;
duke@0 585 }
duke@0 586
duke@0 587 //------------------------------policy_peel_only-------------------------------
duke@0 588 // Return TRUE or FALSE if the loop should NEVER be RCE'd or aligned. Useful
duke@0 589 // for unrolling loops with NO array accesses.
duke@0 590 bool IdealLoopTree::policy_peel_only( PhaseIdealLoop *phase ) const {
duke@0 591
duke@0 592 for( uint i = 0; i < _body.size(); i++ )
duke@0 593 if( _body[i]->is_Mem() )
duke@0 594 return false;
duke@0 595
duke@0 596 // No memory accesses at all!
duke@0 597 return true;
duke@0 598 }
duke@0 599
duke@0 600 //------------------------------clone_up_backedge_goo--------------------------
duke@0 601 // If Node n lives in the back_ctrl block and cannot float, we clone a private
duke@0 602 // version of n in preheader_ctrl block and return that, otherwise return n.
duke@0 603 Node *PhaseIdealLoop::clone_up_backedge_goo( Node *back_ctrl, Node *preheader_ctrl, Node *n ) {
duke@0 604 if( get_ctrl(n) != back_ctrl ) return n;
duke@0 605
duke@0 606 Node *x = NULL; // If required, a clone of 'n'
duke@0 607 // Check for 'n' being pinned in the backedge.
duke@0 608 if( n->in(0) && n->in(0) == back_ctrl ) {
duke@0 609 x = n->clone(); // Clone a copy of 'n' to preheader
duke@0 610 x->set_req( 0, preheader_ctrl ); // Fix x's control input to preheader
duke@0 611 }
duke@0 612
duke@0 613 // Recursive fixup any other input edges into x.
duke@0 614 // If there are no changes we can just return 'n', otherwise
duke@0 615 // we need to clone a private copy and change it.
duke@0 616 for( uint i = 1; i < n->req(); i++ ) {
duke@0 617 Node *g = clone_up_backedge_goo( back_ctrl, preheader_ctrl, n->in(i) );
duke@0 618 if( g != n->in(i) ) {
duke@0 619 if( !x )
duke@0 620 x = n->clone();
duke@0 621 x->set_req(i, g);
duke@0 622 }
duke@0 623 }
duke@0 624 if( x ) { // x can legally float to pre-header location
duke@0 625 register_new_node( x, preheader_ctrl );
duke@0 626 return x;
duke@0 627 } else { // raise n to cover LCA of uses
duke@0 628 set_ctrl( n, find_non_split_ctrl(back_ctrl->in(0)) );
duke@0 629 }
duke@0 630 return n;
duke@0 631 }
duke@0 632
duke@0 633 //------------------------------insert_pre_post_loops--------------------------
duke@0 634 // Insert pre and post loops. If peel_only is set, the pre-loop can not have
duke@0 635 // more iterations added. It acts as a 'peel' only, no lower-bound RCE, no
duke@0 636 // alignment. Useful to unroll loops that do no array accesses.
duke@0 637 void PhaseIdealLoop::insert_pre_post_loops( IdealLoopTree *loop, Node_List &old_new, bool peel_only ) {
duke@0 638
duke@0 639 C->set_major_progress();
duke@0 640
duke@0 641 // Find common pieces of the loop being guarded with pre & post loops
duke@0 642 CountedLoopNode *main_head = loop->_head->as_CountedLoop();
duke@0 643 assert( main_head->is_normal_loop(), "" );
duke@0 644 CountedLoopEndNode *main_end = main_head->loopexit();
duke@0 645 assert( main_end->outcnt() == 2, "1 true, 1 false path only" );
duke@0 646 uint dd_main_head = dom_depth(main_head);
duke@0 647 uint max = main_head->outcnt();
duke@0 648
duke@0 649 Node *pre_header= main_head->in(LoopNode::EntryControl);
duke@0 650 Node *init = main_head->init_trip();
duke@0 651 Node *incr = main_end ->incr();
duke@0 652 Node *limit = main_end ->limit();
duke@0 653 Node *stride = main_end ->stride();
duke@0 654 Node *cmp = main_end ->cmp_node();
duke@0 655 BoolTest::mask b_test = main_end->test_trip();
duke@0 656
duke@0 657 // Need only 1 user of 'bol' because I will be hacking the loop bounds.
duke@0 658 Node *bol = main_end->in(CountedLoopEndNode::TestValue);
duke@0 659 if( bol->outcnt() != 1 ) {
duke@0 660 bol = bol->clone();
duke@0 661 register_new_node(bol,main_end->in(CountedLoopEndNode::TestControl));
duke@0 662 _igvn.hash_delete(main_end);
duke@0 663 main_end->set_req(CountedLoopEndNode::TestValue, bol);
duke@0 664 }
duke@0 665 // Need only 1 user of 'cmp' because I will be hacking the loop bounds.
duke@0 666 if( cmp->outcnt() != 1 ) {
duke@0 667 cmp = cmp->clone();
duke@0 668 register_new_node(cmp,main_end->in(CountedLoopEndNode::TestControl));
duke@0 669 _igvn.hash_delete(bol);
duke@0 670 bol->set_req(1, cmp);
duke@0 671 }
duke@0 672
duke@0 673 //------------------------------
duke@0 674 // Step A: Create Post-Loop.
duke@0 675 Node* main_exit = main_end->proj_out(false);
duke@0 676 assert( main_exit->Opcode() == Op_IfFalse, "" );
duke@0 677 int dd_main_exit = dom_depth(main_exit);
duke@0 678
duke@0 679 // Step A1: Clone the loop body. The clone becomes the post-loop. The main
duke@0 680 // loop pre-header illegally has 2 control users (old & new loops).
duke@0 681 clone_loop( loop, old_new, dd_main_exit );
duke@0 682 assert( old_new[main_end ->_idx]->Opcode() == Op_CountedLoopEnd, "" );
duke@0 683 CountedLoopNode *post_head = old_new[main_head->_idx]->as_CountedLoop();
duke@0 684 post_head->set_post_loop(main_head);
duke@0 685
kvn@403 686 // Reduce the post-loop trip count.
kvn@403 687 CountedLoopEndNode* post_end = old_new[main_end ->_idx]->as_CountedLoopEnd();
kvn@403 688 post_end->_prob = PROB_FAIR;
kvn@403 689
duke@0 690 // Build the main-loop normal exit.
duke@0 691 IfFalseNode *new_main_exit = new (C, 1) IfFalseNode(main_end);
duke@0 692 _igvn.register_new_node_with_optimizer( new_main_exit );
duke@0 693 set_idom(new_main_exit, main_end, dd_main_exit );
duke@0 694 set_loop(new_main_exit, loop->_parent);
duke@0 695
duke@0 696 // Step A2: Build a zero-trip guard for the post-loop. After leaving the
duke@0 697 // main-loop, the post-loop may not execute at all. We 'opaque' the incr
duke@0 698 // (the main-loop trip-counter exit value) because we will be changing
duke@0 699 // the exit value (via unrolling) so we cannot constant-fold away the zero
duke@0 700 // trip guard until all unrolling is done.
kvn@216 701 Node *zer_opaq = new (C, 2) Opaque1Node(C, incr);
duke@0 702 Node *zer_cmp = new (C, 3) CmpINode( zer_opaq, limit );
duke@0 703 Node *zer_bol = new (C, 2) BoolNode( zer_cmp, b_test );
duke@0 704 register_new_node( zer_opaq, new_main_exit );
duke@0 705 register_new_node( zer_cmp , new_main_exit );
duke@0 706 register_new_node( zer_bol , new_main_exit );
duke@0 707
duke@0 708 // Build the IfNode
duke@0 709 IfNode *zer_iff = new (C, 2) IfNode( new_main_exit, zer_bol, PROB_FAIR, COUNT_UNKNOWN );
duke@0 710 _igvn.register_new_node_with_optimizer( zer_iff );
duke@0 711 set_idom(zer_iff, new_main_exit, dd_main_exit);
duke@0 712 set_loop(zer_iff, loop->_parent);
duke@0 713
duke@0 714 // Plug in the false-path, taken if we need to skip post-loop
duke@0 715 _igvn.hash_delete( main_exit );
duke@0 716 main_exit->set_req(0, zer_iff);
duke@0 717 _igvn._worklist.push(main_exit);
duke@0 718 set_idom(main_exit, zer_iff, dd_main_exit);
duke@0 719 set_idom(main_exit->unique_out(), zer_iff, dd_main_exit);
duke@0 720 // Make the true-path, must enter the post loop
duke@0 721 Node *zer_taken = new (C, 1) IfTrueNode( zer_iff );
duke@0 722 _igvn.register_new_node_with_optimizer( zer_taken );
duke@0 723 set_idom(zer_taken, zer_iff, dd_main_exit);
duke@0 724 set_loop(zer_taken, loop->_parent);
duke@0 725 // Plug in the true path
duke@0 726 _igvn.hash_delete( post_head );
duke@0 727 post_head->set_req(LoopNode::EntryControl, zer_taken);
duke@0 728 set_idom(post_head, zer_taken, dd_main_exit);
duke@0 729
duke@0 730 // Step A3: Make the fall-in values to the post-loop come from the
duke@0 731 // fall-out values of the main-loop.
duke@0 732 for (DUIterator_Fast imax, i = main_head->fast_outs(imax); i < imax; i++) {
duke@0 733 Node* main_phi = main_head->fast_out(i);
duke@0 734 if( main_phi->is_Phi() && main_phi->in(0) == main_head && main_phi->outcnt() >0 ) {
duke@0 735 Node *post_phi = old_new[main_phi->_idx];
duke@0 736 Node *fallmain = clone_up_backedge_goo(main_head->back_control(),
duke@0 737 post_head->init_control(),
duke@0 738 main_phi->in(LoopNode::LoopBackControl));
duke@0 739 _igvn.hash_delete(post_phi);
duke@0 740 post_phi->set_req( LoopNode::EntryControl, fallmain );
duke@0 741 }
duke@0 742 }
duke@0 743
duke@0 744 // Update local caches for next stanza
duke@0 745 main_exit = new_main_exit;
duke@0 746
duke@0 747
duke@0 748 //------------------------------
duke@0 749 // Step B: Create Pre-Loop.
duke@0 750
duke@0 751 // Step B1: Clone the loop body. The clone becomes the pre-loop. The main
duke@0 752 // loop pre-header illegally has 2 control users (old & new loops).
duke@0 753 clone_loop( loop, old_new, dd_main_head );
duke@0 754 CountedLoopNode* pre_head = old_new[main_head->_idx]->as_CountedLoop();
duke@0 755 CountedLoopEndNode* pre_end = old_new[main_end ->_idx]->as_CountedLoopEnd();
duke@0 756 pre_head->set_pre_loop(main_head);
duke@0 757 Node *pre_incr = old_new[incr->_idx];
duke@0 758
kvn@403 759 // Reduce the pre-loop trip count.
kvn@403 760 pre_end->_prob = PROB_FAIR;
kvn@403 761
duke@0 762 // Find the pre-loop normal exit.
duke@0 763 Node* pre_exit = pre_end->proj_out(false);
duke@0 764 assert( pre_exit->Opcode() == Op_IfFalse, "" );
duke@0 765 IfFalseNode *new_pre_exit = new (C, 1) IfFalseNode(pre_end);
duke@0 766 _igvn.register_new_node_with_optimizer( new_pre_exit );
duke@0 767 set_idom(new_pre_exit, pre_end, dd_main_head);
duke@0 768 set_loop(new_pre_exit, loop->_parent);
duke@0 769
duke@0 770 // Step B2: Build a zero-trip guard for the main-loop. After leaving the
duke@0 771 // pre-loop, the main-loop may not execute at all. Later in life this
duke@0 772 // zero-trip guard will become the minimum-trip guard when we unroll
duke@0 773 // the main-loop.
kvn@216 774 Node *min_opaq = new (C, 2) Opaque1Node(C, limit);
duke@0 775 Node *min_cmp = new (C, 3) CmpINode( pre_incr, min_opaq );
duke@0 776 Node *min_bol = new (C, 2) BoolNode( min_cmp, b_test );
duke@0 777 register_new_node( min_opaq, new_pre_exit );
duke@0 778 register_new_node( min_cmp , new_pre_exit );
duke@0 779 register_new_node( min_bol , new_pre_exit );
duke@0 780
kvn@403 781 // Build the IfNode (assume the main-loop is executed always).
kvn@403 782 IfNode *min_iff = new (C, 2) IfNode( new_pre_exit, min_bol, PROB_ALWAYS, COUNT_UNKNOWN );
duke@0 783 _igvn.register_new_node_with_optimizer( min_iff );
duke@0 784 set_idom(min_iff, new_pre_exit, dd_main_head);
duke@0 785 set_loop(min_iff, loop->_parent);
duke@0 786
duke@0 787 // Plug in the false-path, taken if we need to skip main-loop
duke@0 788 _igvn.hash_delete( pre_exit );
duke@0 789 pre_exit->set_req(0, min_iff);
duke@0 790 set_idom(pre_exit, min_iff, dd_main_head);
duke@0 791 set_idom(pre_exit->unique_out(), min_iff, dd_main_head);
duke@0 792 // Make the true-path, must enter the main loop
duke@0 793 Node *min_taken = new (C, 1) IfTrueNode( min_iff );
duke@0 794 _igvn.register_new_node_with_optimizer( min_taken );
duke@0 795 set_idom(min_taken, min_iff, dd_main_head);
duke@0 796 set_loop(min_taken, loop->_parent);
duke@0 797 // Plug in the true path
duke@0 798 _igvn.hash_delete( main_head );
duke@0 799 main_head->set_req(LoopNode::EntryControl, min_taken);
duke@0 800 set_idom(main_head, min_taken, dd_main_head);
duke@0 801
duke@0 802 // Step B3: Make the fall-in values to the main-loop come from the
duke@0 803 // fall-out values of the pre-loop.
duke@0 804 for (DUIterator_Fast i2max, i2 = main_head->fast_outs(i2max); i2 < i2max; i2++) {
duke@0 805 Node* main_phi = main_head->fast_out(i2);
duke@0 806 if( main_phi->is_Phi() && main_phi->in(0) == main_head && main_phi->outcnt() > 0 ) {
duke@0 807 Node *pre_phi = old_new[main_phi->_idx];
duke@0 808 Node *fallpre = clone_up_backedge_goo(pre_head->back_control(),
duke@0 809 main_head->init_control(),
duke@0 810 pre_phi->in(LoopNode::LoopBackControl));
duke@0 811 _igvn.hash_delete(main_phi);
duke@0 812 main_phi->set_req( LoopNode::EntryControl, fallpre );
duke@0 813 }
duke@0 814 }
duke@0 815
duke@0 816 // Step B4: Shorten the pre-loop to run only 1 iteration (for now).
duke@0 817 // RCE and alignment may change this later.
duke@0 818 Node *cmp_end = pre_end->cmp_node();
duke@0 819 assert( cmp_end->in(2) == limit, "" );
duke@0 820 Node *pre_limit = new (C, 3) AddINode( init, stride );
duke@0 821
duke@0 822 // Save the original loop limit in this Opaque1 node for
duke@0 823 // use by range check elimination.
kvn@216 824 Node *pre_opaq = new (C, 3) Opaque1Node(C, pre_limit, limit);
duke@0 825
duke@0 826 register_new_node( pre_limit, pre_head->in(0) );
duke@0 827 register_new_node( pre_opaq , pre_head->in(0) );
duke@0 828
duke@0 829 // Since no other users of pre-loop compare, I can hack limit directly
duke@0 830 assert( cmp_end->outcnt() == 1, "no other users" );
duke@0 831 _igvn.hash_delete(cmp_end);
duke@0 832 cmp_end->set_req(2, peel_only ? pre_limit : pre_opaq);
duke@0 833
duke@0 834 // Special case for not-equal loop bounds:
duke@0 835 // Change pre loop test, main loop test, and the
duke@0 836 // main loop guard test to use lt or gt depending on stride
duke@0 837 // direction:
duke@0 838 // positive stride use <
duke@0 839 // negative stride use >
duke@0 840
duke@0 841 if (pre_end->in(CountedLoopEndNode::TestValue)->as_Bool()->_test._test == BoolTest::ne) {
duke@0 842
duke@0 843 BoolTest::mask new_test = (main_end->stride_con() > 0) ? BoolTest::lt : BoolTest::gt;
duke@0 844 // Modify pre loop end condition
duke@0 845 Node* pre_bol = pre_end->in(CountedLoopEndNode::TestValue)->as_Bool();
duke@0 846 BoolNode* new_bol0 = new (C, 2) BoolNode(pre_bol->in(1), new_test);
duke@0 847 register_new_node( new_bol0, pre_head->in(0) );
duke@0 848 _igvn.hash_delete(pre_end);
duke@0 849 pre_end->set_req(CountedLoopEndNode::TestValue, new_bol0);
duke@0 850 // Modify main loop guard condition
duke@0 851 assert(min_iff->in(CountedLoopEndNode::TestValue) == min_bol, "guard okay");
duke@0 852 BoolNode* new_bol1 = new (C, 2) BoolNode(min_bol->in(1), new_test);
duke@0 853 register_new_node( new_bol1, new_pre_exit );
duke@0 854 _igvn.hash_delete(min_iff);
duke@0 855 min_iff->set_req(CountedLoopEndNode::TestValue, new_bol1);
duke@0 856 // Modify main loop end condition
duke@0 857 BoolNode* main_bol = main_end->in(CountedLoopEndNode::TestValue)->as_Bool();
duke@0 858 BoolNode* new_bol2 = new (C, 2) BoolNode(main_bol->in(1), new_test);
duke@0 859 register_new_node( new_bol2, main_end->in(CountedLoopEndNode::TestControl) );
duke@0 860 _igvn.hash_delete(main_end);
duke@0 861 main_end->set_req(CountedLoopEndNode::TestValue, new_bol2);
duke@0 862 }
duke@0 863
duke@0 864 // Flag main loop
duke@0 865 main_head->set_main_loop();
duke@0 866 if( peel_only ) main_head->set_main_no_pre_loop();
duke@0 867
duke@0 868 // It's difficult to be precise about the trip-counts
duke@0 869 // for the pre/post loops. They are usually very short,
duke@0 870 // so guess that 4 trips is a reasonable value.
duke@0 871 post_head->set_profile_trip_cnt(4.0);
duke@0 872 pre_head->set_profile_trip_cnt(4.0);
duke@0 873
duke@0 874 // Now force out all loop-invariant dominating tests. The optimizer
duke@0 875 // finds some, but we _know_ they are all useless.
duke@0 876 peeled_dom_test_elim(loop,old_new);
duke@0 877 }
duke@0 878
duke@0 879 //------------------------------is_invariant-----------------------------
duke@0 880 // Return true if n is invariant
duke@0 881 bool IdealLoopTree::is_invariant(Node* n) const {
cfang@1250 882 Node *n_c = _phase->has_ctrl(n) ? _phase->get_ctrl(n) : n;
duke@0 883 if (n_c->is_top()) return false;
duke@0 884 return !is_member(_phase->get_loop(n_c));
duke@0 885 }
duke@0 886
duke@0 887
duke@0 888 //------------------------------do_unroll--------------------------------------
duke@0 889 // Unroll the loop body one step - make each trip do 2 iterations.
duke@0 890 void PhaseIdealLoop::do_unroll( IdealLoopTree *loop, Node_List &old_new, bool adjust_min_trip ) {
duke@0 891 assert( LoopUnrollLimit, "" );
duke@0 892 #ifndef PRODUCT
duke@0 893 if( PrintOpto && VerifyLoopOptimizations ) {
duke@0 894 tty->print("Unrolling ");
duke@0 895 loop->dump_head();
duke@0 896 }
duke@0 897 #endif
duke@0 898 CountedLoopNode *loop_head = loop->_head->as_CountedLoop();
duke@0 899 CountedLoopEndNode *loop_end = loop_head->loopexit();
duke@0 900 assert( loop_end, "" );
duke@0 901
duke@0 902 // Remember loop node count before unrolling to detect
duke@0 903 // if rounds of unroll,optimize are making progress
duke@0 904 loop_head->set_node_count_before_unroll(loop->_body.size());
duke@0 905
duke@0 906 Node *ctrl = loop_head->in(LoopNode::EntryControl);
duke@0 907 Node *limit = loop_head->limit();
duke@0 908 Node *init = loop_head->init_trip();
duke@0 909 Node *strid = loop_head->stride();
duke@0 910
duke@0 911 Node *opaq = NULL;
duke@0 912 if( adjust_min_trip ) { // If not maximally unrolling, need adjustment
duke@0 913 assert( loop_head->is_main_loop(), "" );
duke@0 914 assert( ctrl->Opcode() == Op_IfTrue || ctrl->Opcode() == Op_IfFalse, "" );
duke@0 915 Node *iff = ctrl->in(0);
duke@0 916 assert( iff->Opcode() == Op_If, "" );
duke@0 917 Node *bol = iff->in(1);
duke@0 918 assert( bol->Opcode() == Op_Bool, "" );
duke@0 919 Node *cmp = bol->in(1);
duke@0 920 assert( cmp->Opcode() == Op_CmpI, "" );
duke@0 921 opaq = cmp->in(2);
duke@0 922 // Occasionally it's possible for a pre-loop Opaque1 node to be
duke@0 923 // optimized away and then another round of loop opts attempted.
duke@0 924 // We can not optimize this particular loop in that case.
duke@0 925 if( opaq->Opcode() != Op_Opaque1 )
duke@0 926 return; // Cannot find pre-loop! Bail out!
duke@0 927 }
duke@0 928
duke@0 929 C->set_major_progress();
duke@0 930
duke@0 931 // Adjust max trip count. The trip count is intentionally rounded
duke@0 932 // down here (e.g. 15-> 7-> 3-> 1) because if we unwittingly over-unroll,
duke@0 933 // the main, unrolled, part of the loop will never execute as it is protected
duke@0 934 // by the min-trip test. See bug 4834191 for a case where we over-unrolled
duke@0 935 // and later determined that part of the unrolled loop was dead.
duke@0 936 loop_head->set_trip_count(loop_head->trip_count() / 2);
duke@0 937
duke@0 938 // Double the count of original iterations in the unrolled loop body.
duke@0 939 loop_head->double_unrolled_count();
duke@0 940
duke@0 941 // -----------
duke@0 942 // Step 2: Cut back the trip counter for an unroll amount of 2.
duke@0 943 // Loop will normally trip (limit - init)/stride_con. Since it's a
duke@0 944 // CountedLoop this is exact (stride divides limit-init exactly).
duke@0 945 // We are going to double the loop body, so we want to knock off any
duke@0 946 // odd iteration: (trip_cnt & ~1). Then back compute a new limit.
duke@0 947 Node *span = new (C, 3) SubINode( limit, init );
duke@0 948 register_new_node( span, ctrl );
duke@0 949 Node *trip = new (C, 3) DivINode( 0, span, strid );
duke@0 950 register_new_node( trip, ctrl );
duke@0 951 Node *mtwo = _igvn.intcon(-2);
duke@0 952 set_ctrl(mtwo, C->root());
duke@0 953 Node *rond = new (C, 3) AndINode( trip, mtwo );
duke@0 954 register_new_node( rond, ctrl );
duke@0 955 Node *spn2 = new (C, 3) MulINode( rond, strid );
duke@0 956 register_new_node( spn2, ctrl );
duke@0 957 Node *lim2 = new (C, 3) AddINode( spn2, init );
duke@0 958 register_new_node( lim2, ctrl );
duke@0 959
duke@0 960 // Hammer in the new limit
duke@0 961 Node *ctrl2 = loop_end->in(0);
duke@0 962 Node *cmp2 = new (C, 3) CmpINode( loop_head->incr(), lim2 );
duke@0 963 register_new_node( cmp2, ctrl2 );
duke@0 964 Node *bol2 = new (C, 2) BoolNode( cmp2, loop_end->test_trip() );
duke@0 965 register_new_node( bol2, ctrl2 );
duke@0 966 _igvn.hash_delete(loop_end);
duke@0 967 loop_end->set_req(CountedLoopEndNode::TestValue, bol2);
duke@0 968
duke@0 969 // Step 3: Find the min-trip test guaranteed before a 'main' loop.
duke@0 970 // Make it a 1-trip test (means at least 2 trips).
duke@0 971 if( adjust_min_trip ) {
duke@0 972 // Guard test uses an 'opaque' node which is not shared. Hence I
duke@0 973 // can edit it's inputs directly. Hammer in the new limit for the
duke@0 974 // minimum-trip guard.
duke@0 975 assert( opaq->outcnt() == 1, "" );
duke@0 976 _igvn.hash_delete(opaq);
duke@0 977 opaq->set_req(1, lim2);
duke@0 978 }
duke@0 979
duke@0 980 // ---------
duke@0 981 // Step 4: Clone the loop body. Move it inside the loop. This loop body
duke@0 982 // represents the odd iterations; since the loop trips an even number of
duke@0 983 // times its backedge is never taken. Kill the backedge.
duke@0 984 uint dd = dom_depth(loop_head);
duke@0 985 clone_loop( loop, old_new, dd );
duke@0 986
duke@0 987 // Make backedges of the clone equal to backedges of the original.
duke@0 988 // Make the fall-in from the original come from the fall-out of the clone.
duke@0 989 for (DUIterator_Fast jmax, j = loop_head->fast_outs(jmax); j < jmax; j++) {
duke@0 990 Node* phi = loop_head->fast_out(j);
duke@0 991 if( phi->is_Phi() && phi->in(0) == loop_head && phi->outcnt() > 0 ) {
duke@0 992 Node *newphi = old_new[phi->_idx];
duke@0 993 _igvn.hash_delete( phi );
duke@0 994 _igvn.hash_delete( newphi );
duke@0 995
duke@0 996 phi ->set_req(LoopNode:: EntryControl, newphi->in(LoopNode::LoopBackControl));
duke@0 997 newphi->set_req(LoopNode::LoopBackControl, phi ->in(LoopNode::LoopBackControl));
duke@0 998 phi ->set_req(LoopNode::LoopBackControl, C->top());
duke@0 999 }
duke@0 1000 }
duke@0 1001 Node *clone_head = old_new[loop_head->_idx];
duke@0 1002 _igvn.hash_delete( clone_head );
duke@0 1003 loop_head ->set_req(LoopNode:: EntryControl, clone_head->in(LoopNode::LoopBackControl));
duke@0 1004 clone_head->set_req(LoopNode::LoopBackControl, loop_head ->in(LoopNode::LoopBackControl));
duke@0 1005 loop_head ->set_req(LoopNode::LoopBackControl, C->top());
duke@0 1006 loop->_head = clone_head; // New loop header
duke@0 1007
duke@0 1008 set_idom(loop_head, loop_head ->in(LoopNode::EntryControl), dd);
duke@0 1009 set_idom(clone_head, clone_head->in(LoopNode::EntryControl), dd);
duke@0 1010
duke@0 1011 // Kill the clone's backedge
duke@0 1012 Node *newcle = old_new[loop_end->_idx];
duke@0 1013 _igvn.hash_delete( newcle );
duke@0 1014 Node *one = _igvn.intcon(1);
duke@0 1015 set_ctrl(one, C->root());
duke@0 1016 newcle->set_req(1, one);
duke@0 1017 // Force clone into same loop body
duke@0 1018 uint max = loop->_body.size();
duke@0 1019 for( uint k = 0; k < max; k++ ) {
duke@0 1020 Node *old = loop->_body.at(k);
duke@0 1021 Node *nnn = old_new[old->_idx];
duke@0 1022 loop->_body.push(nnn);
duke@0 1023 if (!has_ctrl(old))
duke@0 1024 set_loop(nnn, loop);
duke@0 1025 }
never@370 1026
never@370 1027 loop->record_for_igvn();
duke@0 1028 }
duke@0 1029
duke@0 1030 //------------------------------do_maximally_unroll----------------------------
duke@0 1031
duke@0 1032 void PhaseIdealLoop::do_maximally_unroll( IdealLoopTree *loop, Node_List &old_new ) {
duke@0 1033 CountedLoopNode *cl = loop->_head->as_CountedLoop();
duke@0 1034 assert( cl->trip_count() > 0, "");
duke@0 1035
duke@0 1036 // If loop is tripping an odd number of times, peel odd iteration
duke@0 1037 if( (cl->trip_count() & 1) == 1 ) {
duke@0 1038 do_peeling( loop, old_new );
duke@0 1039 }
duke@0 1040
duke@0 1041 // Now its tripping an even number of times remaining. Double loop body.
duke@0 1042 // Do not adjust pre-guards; they are not needed and do not exist.
duke@0 1043 if( cl->trip_count() > 0 ) {
duke@0 1044 do_unroll( loop, old_new, false );
duke@0 1045 }
duke@0 1046 }
duke@0 1047
duke@0 1048 //------------------------------dominates_backedge---------------------------------
duke@0 1049 // Returns true if ctrl is executed on every complete iteration
duke@0 1050 bool IdealLoopTree::dominates_backedge(Node* ctrl) {
duke@0 1051 assert(ctrl->is_CFG(), "must be control");
duke@0 1052 Node* backedge = _head->as_Loop()->in(LoopNode::LoopBackControl);
duke@0 1053 return _phase->dom_lca_internal(ctrl, backedge) == ctrl;
duke@0 1054 }
duke@0 1055
duke@0 1056 //------------------------------add_constraint---------------------------------
duke@0 1057 // Constrain the main loop iterations so the condition:
duke@0 1058 // scale_con * I + offset < limit
duke@0 1059 // always holds true. That is, either increase the number of iterations in
duke@0 1060 // the pre-loop or the post-loop until the condition holds true in the main
duke@0 1061 // loop. Stride, scale, offset and limit are all loop invariant. Further,
duke@0 1062 // stride and scale are constants (offset and limit often are).
duke@0 1063 void PhaseIdealLoop::add_constraint( int stride_con, int scale_con, Node *offset, Node *limit, Node *pre_ctrl, Node **pre_limit, Node **main_limit ) {
duke@0 1064
duke@0 1065 // Compute "I :: (limit-offset)/scale_con"
duke@0 1066 Node *con = new (C, 3) SubINode( limit, offset );
duke@0 1067 register_new_node( con, pre_ctrl );
duke@0 1068 Node *scale = _igvn.intcon(scale_con);
duke@0 1069 set_ctrl(scale, C->root());
duke@0 1070 Node *X = new (C, 3) DivINode( 0, con, scale );
duke@0 1071 register_new_node( X, pre_ctrl );
duke@0 1072
duke@0 1073 // For positive stride, the pre-loop limit always uses a MAX function
duke@0 1074 // and the main loop a MIN function. For negative stride these are
duke@0 1075 // reversed.
duke@0 1076
duke@0 1077 // Also for positive stride*scale the affine function is increasing, so the
duke@0 1078 // pre-loop must check for underflow and the post-loop for overflow.
duke@0 1079 // Negative stride*scale reverses this; pre-loop checks for overflow and
duke@0 1080 // post-loop for underflow.
duke@0 1081 if( stride_con*scale_con > 0 ) {
duke@0 1082 // Compute I < (limit-offset)/scale_con
duke@0 1083 // Adjust main-loop last iteration to be MIN/MAX(main_loop,X)
duke@0 1084 *main_limit = (stride_con > 0)
duke@0 1085 ? (Node*)(new (C, 3) MinINode( *main_limit, X ))
duke@0 1086 : (Node*)(new (C, 3) MaxINode( *main_limit, X ));
duke@0 1087 register_new_node( *main_limit, pre_ctrl );
duke@0 1088
duke@0 1089 } else {
duke@0 1090 // Compute (limit-offset)/scale_con + SGN(-scale_con) <= I
duke@0 1091 // Add the negation of the main-loop constraint to the pre-loop.
duke@0 1092 // See footnote [++] below for a derivation of the limit expression.
duke@0 1093 Node *incr = _igvn.intcon(scale_con > 0 ? -1 : 1);
duke@0 1094 set_ctrl(incr, C->root());
duke@0 1095 Node *adj = new (C, 3) AddINode( X, incr );
duke@0 1096 register_new_node( adj, pre_ctrl );
duke@0 1097 *pre_limit = (scale_con > 0)
duke@0 1098 ? (Node*)new (C, 3) MinINode( *pre_limit, adj )
duke@0 1099 : (Node*)new (C, 3) MaxINode( *pre_limit, adj );
duke@0 1100 register_new_node( *pre_limit, pre_ctrl );
duke@0 1101
duke@0 1102 // [++] Here's the algebra that justifies the pre-loop limit expression:
duke@0 1103 //
duke@0 1104 // NOT( scale_con * I + offset < limit )
duke@0 1105 // ==
duke@0 1106 // scale_con * I + offset >= limit
duke@0 1107 // ==
duke@0 1108 // SGN(scale_con) * I >= (limit-offset)/|scale_con|
duke@0 1109 // ==
duke@0 1110 // (limit-offset)/|scale_con| <= I * SGN(scale_con)
duke@0 1111 // ==
duke@0 1112 // (limit-offset)/|scale_con|-1 < I * SGN(scale_con)
duke@0 1113 // ==
duke@0 1114 // ( if (scale_con > 0) /*common case*/
duke@0 1115 // (limit-offset)/scale_con - 1 < I
duke@0 1116 // else
duke@0 1117 // (limit-offset)/scale_con + 1 > I
duke@0 1118 // )
duke@0 1119 // ( if (scale_con > 0) /*common case*/
duke@0 1120 // (limit-offset)/scale_con + SGN(-scale_con) < I
duke@0 1121 // else
duke@0 1122 // (limit-offset)/scale_con + SGN(-scale_con) > I
duke@0 1123 }
duke@0 1124 }
duke@0 1125
duke@0 1126
duke@0 1127 //------------------------------is_scaled_iv---------------------------------
duke@0 1128 // Return true if exp is a constant times an induction var
duke@0 1129 bool PhaseIdealLoop::is_scaled_iv(Node* exp, Node* iv, int* p_scale) {
duke@0 1130 if (exp == iv) {
duke@0 1131 if (p_scale != NULL) {
duke@0 1132 *p_scale = 1;
duke@0 1133 }
duke@0 1134 return true;
duke@0 1135 }
duke@0 1136 int opc = exp->Opcode();
duke@0 1137 if (opc == Op_MulI) {
duke@0 1138 if (exp->in(1) == iv && exp->in(2)->is_Con()) {
duke@0 1139 if (p_scale != NULL) {
duke@0 1140 *p_scale = exp->in(2)->get_int();
duke@0 1141 }
duke@0 1142 return true;
duke@0 1143 }
duke@0 1144 if (exp->in(2) == iv && exp->in(1)->is_Con()) {
duke@0 1145 if (p_scale != NULL) {
duke@0 1146 *p_scale = exp->in(1)->get_int();
duke@0 1147 }
duke@0 1148 return true;
duke@0 1149 }
duke@0 1150 } else if (opc == Op_LShiftI) {
duke@0 1151 if (exp->in(1) == iv && exp->in(2)->is_Con()) {
duke@0 1152 if (p_scale != NULL) {
duke@0 1153 *p_scale = 1 << exp->in(2)->get_int();
duke@0 1154 }
duke@0 1155 return true;
duke@0 1156 }
duke@0 1157 }
duke@0 1158 return false;
duke@0 1159 }
duke@0 1160
duke@0 1161 //-----------------------------is_scaled_iv_plus_offset------------------------------
duke@0 1162 // Return true if exp is a simple induction variable expression: k1*iv + (invar + k2)
duke@0 1163 bool PhaseIdealLoop::is_scaled_iv_plus_offset(Node* exp, Node* iv, int* p_scale, Node** p_offset, int depth) {
duke@0 1164 if (is_scaled_iv(exp, iv, p_scale)) {
duke@0 1165 if (p_offset != NULL) {
duke@0 1166 Node *zero = _igvn.intcon(0);
duke@0 1167 set_ctrl(zero, C->root());
duke@0 1168 *p_offset = zero;
duke@0 1169 }
duke@0 1170 return true;
duke@0 1171 }
duke@0 1172 int opc = exp->Opcode();
duke@0 1173 if (opc == Op_AddI) {
duke@0 1174 if (is_scaled_iv(exp->in(1), iv, p_scale)) {
duke@0 1175 if (p_offset != NULL) {
duke@0 1176 *p_offset = exp->in(2);
duke@0 1177 }
duke@0 1178 return true;
duke@0 1179 }
duke@0 1180 if (exp->in(2)->is_Con()) {
duke@0 1181 Node* offset2 = NULL;
duke@0 1182 if (depth < 2 &&
duke@0 1183 is_scaled_iv_plus_offset(exp->in(1), iv, p_scale,
duke@0 1184 p_offset != NULL ? &offset2 : NULL, depth+1)) {
duke@0 1185 if (p_offset != NULL) {
duke@0 1186 Node *ctrl_off2 = get_ctrl(offset2);
duke@0 1187 Node* offset = new (C, 3) AddINode(offset2, exp->in(2));
duke@0 1188 register_new_node(offset, ctrl_off2);
duke@0 1189 *p_offset = offset;
duke@0 1190 }
duke@0 1191 return true;
duke@0 1192 }
duke@0 1193 }
duke@0 1194 } else if (opc == Op_SubI) {
duke@0 1195 if (is_scaled_iv(exp->in(1), iv, p_scale)) {
duke@0 1196 if (p_offset != NULL) {
duke@0 1197 Node *zero = _igvn.intcon(0);
duke@0 1198 set_ctrl(zero, C->root());
duke@0 1199 Node *ctrl_off = get_ctrl(exp->in(2));
duke@0 1200 Node* offset = new (C, 3) SubINode(zero, exp->in(2));
duke@0 1201 register_new_node(offset, ctrl_off);
duke@0 1202 *p_offset = offset;
duke@0 1203 }
duke@0 1204 return true;
duke@0 1205 }
duke@0 1206 if (is_scaled_iv(exp->in(2), iv, p_scale)) {
duke@0 1207 if (p_offset != NULL) {
duke@0 1208 *p_scale *= -1;
duke@0 1209 *p_offset = exp->in(1);
duke@0 1210 }
duke@0 1211 return true;
duke@0 1212 }
duke@0 1213 }
duke@0 1214 return false;
duke@0 1215 }
duke@0 1216
duke@0 1217 //------------------------------do_range_check---------------------------------
duke@0 1218 // Eliminate range-checks and other trip-counter vs loop-invariant tests.
duke@0 1219 void PhaseIdealLoop::do_range_check( IdealLoopTree *loop, Node_List &old_new ) {
duke@0 1220 #ifndef PRODUCT
duke@0 1221 if( PrintOpto && VerifyLoopOptimizations ) {
duke@0 1222 tty->print("Range Check Elimination ");
duke@0 1223 loop->dump_head();
duke@0 1224 }
duke@0 1225 #endif
duke@0 1226 assert( RangeCheckElimination, "" );
duke@0 1227 CountedLoopNode *cl = loop->_head->as_CountedLoop();
duke@0 1228 assert( cl->is_main_loop(), "" );
duke@0 1229
duke@0 1230 // Find the trip counter; we are iteration splitting based on it
duke@0 1231 Node *trip_counter = cl->phi();
duke@0 1232 // Find the main loop limit; we will trim it's iterations
duke@0 1233 // to not ever trip end tests
duke@0 1234 Node *main_limit = cl->limit();
duke@0 1235 // Find the pre-loop limit; we will expand it's iterations to
duke@0 1236 // not ever trip low tests.
duke@0 1237 Node *ctrl = cl->in(LoopNode::EntryControl);
duke@0 1238 assert( ctrl->Opcode() == Op_IfTrue || ctrl->Opcode() == Op_IfFalse, "" );
duke@0 1239 Node *iffm = ctrl->in(0);
duke@0 1240 assert( iffm->Opcode() == Op_If, "" );
duke@0 1241 Node *p_f = iffm->in(0);
duke@0 1242 assert( p_f->Opcode() == Op_IfFalse, "" );
duke@0 1243 CountedLoopEndNode *pre_end = p_f->in(0)->as_CountedLoopEnd();
duke@0 1244 assert( pre_end->loopnode()->is_pre_loop(), "" );
duke@0 1245 Node *pre_opaq1 = pre_end->limit();
duke@0 1246 // Occasionally it's possible for a pre-loop Opaque1 node to be
duke@0 1247 // optimized away and then another round of loop opts attempted.
duke@0 1248 // We can not optimize this particular loop in that case.
duke@0 1249 if( pre_opaq1->Opcode() != Op_Opaque1 )
duke@0 1250 return;
duke@0 1251 Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1;
duke@0 1252 Node *pre_limit = pre_opaq->in(1);
duke@0 1253
duke@0 1254 // Where do we put new limit calculations
duke@0 1255 Node *pre_ctrl = pre_end->loopnode()->in(LoopNode::EntryControl);
duke@0 1256
duke@0 1257 // Ensure the original loop limit is available from the
duke@0 1258 // pre-loop Opaque1 node.
duke@0 1259 Node *orig_limit = pre_opaq->original_loop_limit();
duke@0 1260 if( orig_limit == NULL || _igvn.type(orig_limit) == Type::TOP )
duke@0 1261 return;
duke@0 1262
duke@0 1263 // Need to find the main-loop zero-trip guard
duke@0 1264 Node *bolzm = iffm->in(1);
duke@0 1265 assert( bolzm->Opcode() == Op_Bool, "" );
duke@0 1266 Node *cmpzm = bolzm->in(1);
duke@0 1267 assert( cmpzm->is_Cmp(), "" );
duke@0 1268 Node *opqzm = cmpzm->in(2);
duke@0 1269 if( opqzm->Opcode() != Op_Opaque1 )
duke@0 1270 return;
duke@0 1271 assert( opqzm->in(1) == main_limit, "do not understand situation" );
duke@0 1272
duke@0 1273 // Must know if its a count-up or count-down loop
duke@0 1274
duke@0 1275 // protect against stride not being a constant
duke@0 1276 if ( !cl->stride_is_con() ) {
duke@0 1277 return;
duke@0 1278 }
duke@0 1279 int stride_con = cl->stride_con();
duke@0 1280 Node *zero = _igvn.intcon(0);
duke@0 1281 Node *one = _igvn.intcon(1);
duke@0 1282 set_ctrl(zero, C->root());
duke@0 1283 set_ctrl(one, C->root());
duke@0 1284
duke@0 1285 // Range checks that do not dominate the loop backedge (ie.
duke@0 1286 // conditionally executed) can lengthen the pre loop limit beyond
duke@0 1287 // the original loop limit. To prevent this, the pre limit is
duke@0 1288 // (for stride > 0) MINed with the original loop limit (MAXed
duke@0 1289 // stride < 0) when some range_check (rc) is conditionally
duke@0 1290 // executed.
duke@0 1291 bool conditional_rc = false;
duke@0 1292
duke@0 1293 // Check loop body for tests of trip-counter plus loop-invariant vs
duke@0 1294 // loop-invariant.
duke@0 1295 for( uint i = 0; i < loop->_body.size(); i++ ) {
duke@0 1296 Node *iff = loop->_body[i];
duke@0 1297 if( iff->Opcode() == Op_If ) { // Test?
duke@0 1298
duke@0 1299 // Test is an IfNode, has 2 projections. If BOTH are in the loop
duke@0 1300 // we need loop unswitching instead of iteration splitting.
duke@0 1301 Node *exit = loop->is_loop_exit(iff);
duke@0 1302 if( !exit ) continue;
duke@0 1303 int flip = (exit->Opcode() == Op_IfTrue) ? 1 : 0;
duke@0 1304
duke@0 1305 // Get boolean condition to test
duke@0 1306 Node *i1 = iff->in(1);
duke@0 1307 if( !i1->is_Bool() ) continue;
duke@0 1308 BoolNode *bol = i1->as_Bool();
duke@0 1309 BoolTest b_test = bol->_test;
duke@0 1310 // Flip sense of test if exit condition is flipped
duke@0 1311 if( flip )
duke@0 1312 b_test = b_test.negate();
duke@0 1313
duke@0 1314 // Get compare
duke@0 1315 Node *cmp = bol->in(1);
duke@0 1316
duke@0 1317 // Look for trip_counter + offset vs limit
duke@0 1318 Node *rc_exp = cmp->in(1);
duke@0 1319 Node *limit = cmp->in(2);
duke@0 1320 jint scale_con= 1; // Assume trip counter not scaled
duke@0 1321
duke@0 1322 Node *limit_c = get_ctrl(limit);
duke@0 1323 if( loop->is_member(get_loop(limit_c) ) ) {
duke@0 1324 // Compare might have operands swapped; commute them
duke@0 1325 b_test = b_test.commute();
duke@0 1326 rc_exp = cmp->in(2);
duke@0 1327 limit = cmp->in(1);
duke@0 1328 limit_c = get_ctrl(limit);
duke@0 1329 if( loop->is_member(get_loop(limit_c) ) )
duke@0 1330 continue; // Both inputs are loop varying; cannot RCE
duke@0 1331 }
duke@0 1332 // Here we know 'limit' is loop invariant
duke@0 1333
duke@0 1334 // 'limit' maybe pinned below the zero trip test (probably from a
duke@0 1335 // previous round of rce), in which case, it can't be used in the
duke@0 1336 // zero trip test expression which must occur before the zero test's if.
duke@0 1337 if( limit_c == ctrl ) {
duke@0 1338 continue; // Don't rce this check but continue looking for other candidates.
duke@0 1339 }
duke@0 1340
duke@0 1341 // Check for scaled induction variable plus an offset
duke@0 1342 Node *offset = NULL;
duke@0 1343
duke@0 1344 if (!is_scaled_iv_plus_offset(rc_exp, trip_counter, &scale_con, &offset)) {
duke@0 1345 continue;
duke@0 1346 }
duke@0 1347
duke@0 1348 Node *offset_c = get_ctrl(offset);
duke@0 1349 if( loop->is_member( get_loop(offset_c) ) )
duke@0 1350 continue; // Offset is not really loop invariant
duke@0 1351 // Here we know 'offset' is loop invariant.
duke@0 1352
duke@0 1353 // As above for the 'limit', the 'offset' maybe pinned below the
duke@0 1354 // zero trip test.
duke@0 1355 if( offset_c == ctrl ) {
duke@0 1356 continue; // Don't rce this check but continue looking for other candidates.
duke@0 1357 }
duke@0 1358
duke@0 1359 // At this point we have the expression as:
duke@0 1360 // scale_con * trip_counter + offset :: limit
duke@0 1361 // where scale_con, offset and limit are loop invariant. Trip_counter
duke@0 1362 // monotonically increases by stride_con, a constant. Both (or either)
duke@0 1363 // stride_con and scale_con can be negative which will flip about the
duke@0 1364 // sense of the test.
duke@0 1365
duke@0 1366 // Adjust pre and main loop limits to guard the correct iteration set
duke@0 1367 if( cmp->Opcode() == Op_CmpU ) {// Unsigned compare is really 2 tests
duke@0 1368 if( b_test._test == BoolTest::lt ) { // Range checks always use lt
duke@0 1369 // The overflow limit: scale*I+offset < limit
duke@0 1370 add_constraint( stride_con, scale_con, offset, limit, pre_ctrl, &pre_limit, &main_limit );
duke@0 1371 // The underflow limit: 0 <= scale*I+offset.
duke@0 1372 // Some math yields: -scale*I-(offset+1) < 0
duke@0 1373 Node *plus_one = new (C, 3) AddINode( offset, one );
duke@0 1374 register_new_node( plus_one, pre_ctrl );
duke@0 1375 Node *neg_offset = new (C, 3) SubINode( zero, plus_one );
duke@0 1376 register_new_node( neg_offset, pre_ctrl );
duke@0 1377 add_constraint( stride_con, -scale_con, neg_offset, zero, pre_ctrl, &pre_limit, &main_limit );
duke@0 1378 if (!conditional_rc) {
duke@0 1379 conditional_rc = !loop->dominates_backedge(iff);
duke@0 1380 }
duke@0 1381 } else {
duke@0 1382 #ifndef PRODUCT
duke@0 1383 if( PrintOpto )
duke@0 1384 tty->print_cr("missed RCE opportunity");
duke@0 1385 #endif
duke@0 1386 continue; // In release mode, ignore it
duke@0 1387 }
duke@0 1388 } else { // Otherwise work on normal compares
duke@0 1389 switch( b_test._test ) {
duke@0 1390 case BoolTest::ge: // Convert X >= Y to -X <= -Y
duke@0 1391 scale_con = -scale_con;
duke@0 1392 offset = new (C, 3) SubINode( zero, offset );
duke@0 1393 register_new_node( offset, pre_ctrl );
duke@0 1394 limit = new (C, 3) SubINode( zero, limit );
duke@0 1395 register_new_node( limit, pre_ctrl );
duke@0 1396 // Fall into LE case
duke@0 1397 case BoolTest::le: // Convert X <= Y to X < Y+1
duke@0 1398 limit = new (C, 3) AddINode( limit, one );
duke@0 1399 register_new_node( limit, pre_ctrl );
duke@0 1400 // Fall into LT case
duke@0 1401 case BoolTest::lt:
duke@0 1402 add_constraint( stride_con, scale_con, offset, limit, pre_ctrl, &pre_limit, &main_limit );
duke@0 1403 if (!conditional_rc) {
duke@0 1404 conditional_rc = !loop->dominates_backedge(iff);
duke@0 1405 }
duke@0 1406 break;
duke@0 1407 default:
duke@0 1408 #ifndef PRODUCT
duke@0 1409 if( PrintOpto )
duke@0 1410 tty->print_cr("missed RCE opportunity");
duke@0 1411 #endif
duke@0 1412 continue; // Unhandled case
duke@0 1413 }
duke@0 1414 }
duke@0 1415
duke@0 1416 // Kill the eliminated test
duke@0 1417 C->set_major_progress();
duke@0 1418 Node *kill_con = _igvn.intcon( 1-flip );
duke@0 1419 set_ctrl(kill_con, C->root());
duke@0 1420 _igvn.hash_delete(iff);
duke@0 1421 iff->set_req(1, kill_con);
duke@0 1422 _igvn._worklist.push(iff);
duke@0 1423 // Find surviving projection
duke@0 1424 assert(iff->is_If(), "");
duke@0 1425 ProjNode* dp = ((IfNode*)iff)->proj_out(1-flip);
duke@0 1426 // Find loads off the surviving projection; remove their control edge
duke@0 1427 for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) {
duke@0 1428 Node* cd = dp->fast_out(i); // Control-dependent node
duke@0 1429 if( cd->is_Load() ) { // Loads can now float around in the loop
duke@0 1430 _igvn.hash_delete(cd);
duke@0 1431 // Allow the load to float around in the loop, or before it
duke@0 1432 // but NOT before the pre-loop.
duke@0 1433 cd->set_req(0, ctrl); // ctrl, not NULL
duke@0 1434 _igvn._worklist.push(cd);
duke@0 1435 --i;
duke@0 1436 --imax;
duke@0 1437 }
duke@0 1438 }
duke@0 1439
duke@0 1440 } // End of is IF
duke@0 1441
duke@0 1442 }
duke@0 1443
duke@0 1444 // Update loop limits
duke@0 1445 if (conditional_rc) {
duke@0 1446 pre_limit = (stride_con > 0) ? (Node*)new (C,3) MinINode(pre_limit, orig_limit)
duke@0 1447 : (Node*)new (C,3) MaxINode(pre_limit, orig_limit);
duke@0 1448 register_new_node(pre_limit, pre_ctrl);
duke@0 1449 }
duke@0 1450 _igvn.hash_delete(pre_opaq);
duke@0 1451 pre_opaq->set_req(1, pre_limit);
duke@0 1452
duke@0 1453 // Note:: we are making the main loop limit no longer precise;
duke@0 1454 // need to round up based on stride.
duke@0 1455 if( stride_con != 1 && stride_con != -1 ) { // Cutout for common case
duke@0 1456 // "Standard" round-up logic: ([main_limit-init+(y-1)]/y)*y+init
duke@0 1457 // Hopefully, compiler will optimize for powers of 2.
duke@0 1458 Node *ctrl = get_ctrl(main_limit);
duke@0 1459 Node *stride = cl->stride();
duke@0 1460 Node *init = cl->init_trip();
duke@0 1461 Node *span = new (C, 3) SubINode(main_limit,init);
duke@0 1462 register_new_node(span,ctrl);
duke@0 1463 Node *rndup = _igvn.intcon(stride_con + ((stride_con>0)?-1:1));
duke@0 1464 Node *add = new (C, 3) AddINode(span,rndup);
duke@0 1465 register_new_node(add,ctrl);
duke@0 1466 Node *div = new (C, 3) DivINode(0,add,stride);
duke@0 1467 register_new_node(div,ctrl);
duke@0 1468 Node *mul = new (C, 3) MulINode(div,stride);
duke@0 1469 register_new_node(mul,ctrl);
duke@0 1470 Node *newlim = new (C, 3) AddINode(mul,init);
duke@0 1471 register_new_node(newlim,ctrl);
duke@0 1472 main_limit = newlim;
duke@0 1473 }
duke@0 1474
duke@0 1475 Node *main_cle = cl->loopexit();
duke@0 1476 Node *main_bol = main_cle->in(1);
duke@0 1477 // Hacking loop bounds; need private copies of exit test
duke@0 1478 if( main_bol->outcnt() > 1 ) {// BoolNode shared?
duke@0 1479 _igvn.hash_delete(main_cle);
duke@0 1480 main_bol = main_bol->clone();// Clone a private BoolNode
duke@0 1481 register_new_node( main_bol, main_cle->in(0) );
duke@0 1482 main_cle->set_req(1,main_bol);
duke@0 1483 }
duke@0 1484 Node *main_cmp = main_bol->in(1);
duke@0 1485 if( main_cmp->outcnt() > 1 ) { // CmpNode shared?
duke@0 1486 _igvn.hash_delete(main_bol);
duke@0 1487 main_cmp = main_cmp->clone();// Clone a private CmpNode
duke@0 1488 register_new_node( main_cmp, main_cle->in(0) );
duke@0 1489 main_bol->set_req(1,main_cmp);
duke@0 1490 }
duke@0 1491 // Hack the now-private loop bounds
duke@0 1492 _igvn.hash_delete(main_cmp);
duke@0 1493 main_cmp->set_req(2, main_limit);
duke@0 1494 _igvn._worklist.push(main_cmp);
duke@0 1495 // The OpaqueNode is unshared by design
duke@0 1496 _igvn.hash_delete(opqzm);
duke@0 1497 assert( opqzm->outcnt() == 1, "cannot hack shared node" );
duke@0 1498 opqzm->set_req(1,main_limit);
duke@0 1499 _igvn._worklist.push(opqzm);
duke@0 1500 }
duke@0 1501
duke@0 1502 //------------------------------DCE_loop_body----------------------------------
duke@0 1503 // Remove simplistic dead code from loop body
duke@0 1504 void IdealLoopTree::DCE_loop_body() {
duke@0 1505 for( uint i = 0; i < _body.size(); i++ )
duke@0 1506 if( _body.at(i)->outcnt() == 0 )
duke@0 1507 _body.map( i--, _body.pop() );
duke@0 1508 }
duke@0 1509
duke@0 1510
duke@0 1511 //------------------------------adjust_loop_exit_prob--------------------------
duke@0 1512 // Look for loop-exit tests with the 50/50 (or worse) guesses from the parsing stage.
duke@0 1513 // Replace with a 1-in-10 exit guess.
duke@0 1514 void IdealLoopTree::adjust_loop_exit_prob( PhaseIdealLoop *phase ) {
duke@0 1515 Node *test = tail();
duke@0 1516 while( test != _head ) {
duke@0 1517 uint top = test->Opcode();
duke@0 1518 if( top == Op_IfTrue || top == Op_IfFalse ) {
duke@0 1519 int test_con = ((ProjNode*)test)->_con;
duke@0 1520 assert(top == (uint)(test_con? Op_IfTrue: Op_IfFalse), "sanity");
duke@0 1521 IfNode *iff = test->in(0)->as_If();
duke@0 1522 if( iff->outcnt() == 2 ) { // Ignore dead tests
duke@0 1523 Node *bol = iff->in(1);
duke@0 1524 if( bol && bol->req() > 1 && bol->in(1) &&
duke@0 1525 ((bol->in(1)->Opcode() == Op_StorePConditional ) ||
kvn@423 1526 (bol->in(1)->Opcode() == Op_StoreIConditional ) ||
duke@0 1527 (bol->in(1)->Opcode() == Op_StoreLConditional ) ||
duke@0 1528 (bol->in(1)->Opcode() == Op_CompareAndSwapI ) ||
duke@0 1529 (bol->in(1)->Opcode() == Op_CompareAndSwapL ) ||
coleenp@113 1530 (bol->in(1)->Opcode() == Op_CompareAndSwapP ) ||
coleenp@113 1531 (bol->in(1)->Opcode() == Op_CompareAndSwapN )))
duke@0 1532 return; // Allocation loops RARELY take backedge
duke@0 1533 // Find the OTHER exit path from the IF
duke@0 1534 Node* ex = iff->proj_out(1-test_con);
duke@0 1535 float p = iff->_prob;
duke@0 1536 if( !phase->is_member( this, ex ) && iff->_fcnt == COUNT_UNKNOWN ) {
duke@0 1537 if( top == Op_IfTrue ) {
duke@0 1538 if( p < (PROB_FAIR + PROB_UNLIKELY_MAG(3))) {
duke@0 1539 iff->_prob = PROB_STATIC_FREQUENT;
duke@0 1540 }
duke@0 1541 } else {
duke@0 1542 if( p > (PROB_FAIR - PROB_UNLIKELY_MAG(3))) {
duke@0 1543 iff->_prob = PROB_STATIC_INFREQUENT;
duke@0 1544 }
duke@0 1545 }
duke@0 1546 }
duke@0 1547 }
duke@0 1548 }
duke@0 1549 test = phase->idom(test);
duke@0 1550 }
duke@0 1551 }
duke@0 1552
duke@0 1553
duke@0 1554 //------------------------------policy_do_remove_empty_loop--------------------
duke@0 1555 // Micro-benchmark spamming. Policy is to always remove empty loops.
duke@0 1556 // The 'DO' part is to replace the trip counter with the value it will
duke@0 1557 // have on the last iteration. This will break the loop.
duke@0 1558 bool IdealLoopTree::policy_do_remove_empty_loop( PhaseIdealLoop *phase ) {
duke@0 1559 // Minimum size must be empty loop
duke@0 1560 if( _body.size() > 7/*number of nodes in an empty loop*/ ) return false;
duke@0 1561
duke@0 1562 if( !_head->is_CountedLoop() ) return false; // Dead loop
duke@0 1563 CountedLoopNode *cl = _head->as_CountedLoop();
duke@0 1564 if( !cl->loopexit() ) return false; // Malformed loop
duke@0 1565 if( !phase->is_member(this,phase->get_ctrl(cl->loopexit()->in(CountedLoopEndNode::TestValue)) ) )
duke@0 1566 return false; // Infinite loop
duke@0 1567 #ifndef PRODUCT
duke@0 1568 if( PrintOpto )
duke@0 1569 tty->print_cr("Removing empty loop");
duke@0 1570 #endif
duke@0 1571 #ifdef ASSERT
duke@0 1572 // Ensure only one phi which is the iv.
duke@0 1573 Node* iv = NULL;
duke@0 1574 for (DUIterator_Fast imax, i = cl->fast_outs(imax); i < imax; i++) {
duke@0 1575 Node* n = cl->fast_out(i);
duke@0 1576 if (n->Opcode() == Op_Phi) {
duke@0 1577 assert(iv == NULL, "Too many phis" );
duke@0 1578 iv = n;
duke@0 1579 }
duke@0 1580 }
duke@0 1581 assert(iv == cl->phi(), "Wrong phi" );
duke@0 1582 #endif
duke@0 1583 // Replace the phi at loop head with the final value of the last
duke@0 1584 // iteration. Then the CountedLoopEnd will collapse (backedge never
duke@0 1585 // taken) and all loop-invariant uses of the exit values will be correct.
duke@0 1586 Node *phi = cl->phi();
duke@0 1587 Node *final = new (phase->C, 3) SubINode( cl->limit(), cl->stride() );
duke@0 1588 phase->register_new_node(final,cl->in(LoopNode::EntryControl));
duke@0 1589 phase->_igvn.hash_delete(phi);
duke@0 1590 phase->_igvn.subsume_node(phi,final);
duke@0 1591 phase->C->set_major_progress();
duke@0 1592 return true;
duke@0 1593 }
duke@0 1594
duke@0 1595
duke@0 1596 //=============================================================================
duke@0 1597 //------------------------------iteration_split_impl---------------------------
never@404 1598 bool IdealLoopTree::iteration_split_impl( PhaseIdealLoop *phase, Node_List &old_new ) {
duke@0 1599 // Check and remove empty loops (spam micro-benchmarks)
duke@0 1600 if( policy_do_remove_empty_loop(phase) )
cfang@1250 1601 return true; // Here we removed an empty loop
duke@0 1602
duke@0 1603 bool should_peel = policy_peeling(phase); // Should we peel?
duke@0 1604
duke@0 1605 bool should_unswitch = policy_unswitching(phase);
duke@0 1606
duke@0 1607 // Non-counted loops may be peeled; exactly 1 iteration is peeled.
duke@0 1608 // This removes loop-invariant tests (usually null checks).
duke@0 1609 if( !_head->is_CountedLoop() ) { // Non-counted loop
duke@0 1610 if (PartialPeelLoop && phase->partial_peel(this, old_new)) {
never@404 1611 // Partial peel succeeded so terminate this round of loop opts
never@404 1612 return false;
duke@0 1613 }
duke@0 1614 if( should_peel ) { // Should we peel?
duke@0 1615 #ifndef PRODUCT
duke@0 1616 if (PrintOpto) tty->print_cr("should_peel");
duke@0 1617 #endif
duke@0 1618 phase->do_peeling(this,old_new);
duke@0 1619 } else if( should_unswitch ) {
duke@0 1620 phase->do_unswitching(this, old_new);
duke@0 1621 }
never@404 1622 return true;
duke@0 1623 }
duke@0 1624 CountedLoopNode *cl = _head->as_CountedLoop();
duke@0 1625
never@404 1626 if( !cl->loopexit() ) return true; // Ignore various kinds of broken loops
duke@0 1627
duke@0 1628 // Do nothing special to pre- and post- loops
never@404 1629 if( cl->is_pre_loop() || cl->is_post_loop() ) return true;
duke@0 1630
duke@0 1631 // Compute loop trip count from profile data
duke@0 1632 compute_profile_trip_cnt(phase);
duke@0 1633
duke@0 1634 // Before attempting fancy unrolling, RCE or alignment, see if we want
duke@0 1635 // to completely unroll this loop or do loop unswitching.
duke@0 1636 if( cl->is_normal_loop() ) {
cfang@837 1637 if (should_unswitch) {
cfang@837 1638 phase->do_unswitching(this, old_new);
cfang@837 1639 return true;
cfang@837 1640 }
duke@0 1641 bool should_maximally_unroll = policy_maximally_unroll(phase);
duke@0 1642 if( should_maximally_unroll ) {
duke@0 1643 // Here we did some unrolling and peeling. Eventually we will
duke@0 1644 // completely unroll this loop and it will no longer be a loop.
duke@0 1645 phase->do_maximally_unroll(this,old_new);
never@404 1646 return true;
duke@0 1647 }
duke@0 1648 }
duke@0 1649
duke@0 1650
duke@0 1651 // Counted loops may be peeled, may need some iterations run up
duke@0 1652 // front for RCE, and may want to align loop refs to a cache
duke@0 1653 // line. Thus we clone a full loop up front whose trip count is
duke@0 1654 // at least 1 (if peeling), but may be several more.
duke@0 1655
duke@0 1656 // The main loop will start cache-line aligned with at least 1
duke@0 1657 // iteration of the unrolled body (zero-trip test required) and
duke@0 1658 // will have some range checks removed.
duke@0 1659
duke@0 1660 // A post-loop will finish any odd iterations (leftover after
duke@0 1661 // unrolling), plus any needed for RCE purposes.
duke@0 1662
duke@0 1663 bool should_unroll = policy_unroll(phase);
duke@0 1664
duke@0 1665 bool should_rce = policy_range_check(phase);
duke@0 1666
duke@0 1667 bool should_align = policy_align(phase);
duke@0 1668
duke@0 1669 // If not RCE'ing (iteration splitting) or Aligning, then we do not
duke@0 1670 // need a pre-loop. We may still need to peel an initial iteration but
duke@0 1671 // we will not be needing an unknown number of pre-iterations.
duke@0 1672 //
duke@0 1673 // Basically, if may_rce_align reports FALSE first time through,
duke@0 1674 // we will not be able to later do RCE or Aligning on this loop.
duke@0 1675 bool may_rce_align = !policy_peel_only(phase) || should_rce || should_align;
duke@0 1676
duke@0 1677 // If we have any of these conditions (RCE, alignment, unrolling) met, then
duke@0 1678 // we switch to the pre-/main-/post-loop model. This model also covers
duke@0 1679 // peeling.
duke@0 1680 if( should_rce || should_align || should_unroll ) {
duke@0 1681 if( cl->is_normal_loop() ) // Convert to 'pre/main/post' loops
duke@0 1682 phase->insert_pre_post_loops(this,old_new, !may_rce_align);
duke@0 1683
duke@0 1684 // Adjust the pre- and main-loop limits to let the pre and post loops run
duke@0 1685 // with full checks, but the main-loop with no checks. Remove said
duke@0 1686 // checks from the main body.
duke@0 1687 if( should_rce )
duke@0 1688 phase->do_range_check(this,old_new);
duke@0 1689
duke@0 1690 // Double loop body for unrolling. Adjust the minimum-trip test (will do
duke@0 1691 // twice as many iterations as before) and the main body limit (only do
duke@0 1692 // an even number of trips). If we are peeling, we might enable some RCE
duke@0 1693 // and we'd rather unroll the post-RCE'd loop SO... do not unroll if
duke@0 1694 // peeling.
cfang@1250 1695 if( should_unroll && !should_peel )
cfang@1250 1696 phase->do_unroll(this,old_new, true);
duke@0 1697
duke@0 1698 // Adjust the pre-loop limits to align the main body
duke@0 1699 // iterations.
duke@0 1700 if( should_align )
duke@0 1701 Unimplemented();
duke@0 1702
duke@0 1703 } else { // Else we have an unchanged counted loop
duke@0 1704 if( should_peel ) // Might want to peel but do nothing else
duke@0 1705 phase->do_peeling(this,old_new);
duke@0 1706 }
never@404 1707 return true;
duke@0 1708 }
duke@0 1709
duke@0 1710
duke@0 1711 //=============================================================================
duke@0 1712 //------------------------------iteration_split--------------------------------
never@404 1713 bool IdealLoopTree::iteration_split( PhaseIdealLoop *phase, Node_List &old_new ) {
duke@0 1714 // Recursively iteration split nested loops
never@404 1715 if( _child && !_child->iteration_split( phase, old_new ))
never@404 1716 return false;
duke@0 1717
duke@0 1718 // Clean out prior deadwood
duke@0 1719 DCE_loop_body();
duke@0 1720
duke@0 1721
duke@0 1722 // Look for loop-exit tests with my 50/50 guesses from the Parsing stage.
duke@0 1723 // Replace with a 1-in-10 exit guess.
duke@0 1724 if( _parent /*not the root loop*/ &&
duke@0 1725 !_irreducible &&
duke@0 1726 // Also ignore the occasional dead backedge
duke@0 1727 !tail()->is_top() ) {
duke@0 1728 adjust_loop_exit_prob(phase);
duke@0 1729 }
duke@0 1730
duke@0 1731
duke@0 1732 // Gate unrolling, RCE and peeling efforts.
duke@0 1733 if( !_child && // If not an inner loop, do not split
duke@0 1734 !_irreducible &&
kvn@39 1735 _allow_optimizations &&
duke@0 1736 !tail()->is_top() ) { // Also ignore the occasional dead backedge
duke@0 1737 if (!_has_call) {
cfang@1250 1738 if (!iteration_split_impl( phase, old_new )) {
cfang@1250 1739 return false;
cfang@1250 1740 }
duke@0 1741 } else if (policy_unswitching(phase)) {
duke@0 1742 phase->do_unswitching(this, old_new);
duke@0 1743 }
duke@0 1744 }
duke@0 1745
duke@0 1746 // Minor offset re-organization to remove loop-fallout uses of
duke@0 1747 // trip counter.
duke@0 1748 if( _head->is_CountedLoop() ) phase->reorg_offsets( this );
never@404 1749 if( _next && !_next->iteration_split( phase, old_new ))
never@404 1750 return false;
never@404 1751 return true;
duke@0 1752 }
cfang@1250 1753
cfang@1250 1754 //-------------------------------is_uncommon_trap_proj----------------------------
cfang@1250 1755 // Return true if proj is the form of "proj->[region->..]call_uct"
cfang@1250 1756 bool PhaseIdealLoop::is_uncommon_trap_proj(ProjNode* proj, bool must_reason_predicate) {
cfang@1250 1757 int path_limit = 10;
cfang@1250 1758 assert(proj, "invalid argument");
cfang@1250 1759 Node* out = proj;
cfang@1250 1760 for (int ct = 0; ct < path_limit; ct++) {
cfang@1250 1761 out = out->unique_ctrl_out();
cfang@1250 1762 if (out == NULL || out->is_Root() || out->is_Start())
cfang@1250 1763 return false;
cfang@1250 1764 if (out->is_CallStaticJava()) {
cfang@1250 1765 int req = out->as_CallStaticJava()->uncommon_trap_request();
cfang@1250 1766 if (req != 0) {
cfang@1250 1767 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(req);
cfang@1250 1768 if (!must_reason_predicate || reason == Deoptimization::Reason_predicate){
cfang@1250 1769 return true;
cfang@1250 1770 }
cfang@1250 1771 }
cfang@1250 1772 return false; // don't do further after call
cfang@1250 1773 }
cfang@1250 1774 }
cfang@1250 1775 return false;
cfang@1250 1776 }
cfang@1250 1777
cfang@1250 1778 //-------------------------------is_uncommon_trap_if_pattern-------------------------
cfang@1250 1779 // Return true for "if(test)-> proj -> ...
cfang@1250 1780 // |
cfang@1250 1781 // V
cfang@1250 1782 // other_proj->[region->..]call_uct"
cfang@1250 1783 //
cfang@1250 1784 // "must_reason_predicate" means the uct reason must be Reason_predicate
cfang@1250 1785 bool PhaseIdealLoop::is_uncommon_trap_if_pattern(ProjNode *proj, bool must_reason_predicate) {
cfang@1250 1786 Node *in0 = proj->in(0);
cfang@1250 1787 if (!in0->is_If()) return false;
kvn@1367 1788 // Variation of a dead If node.
kvn@1367 1789 if (in0->outcnt() < 2) return false;
cfang@1250 1790 IfNode* iff = in0->as_If();
cfang@1250 1791
cfang@1250 1792 // we need "If(Conv2B(Opaque1(...)))" pattern for must_reason_predicate
cfang@1250 1793 if (must_reason_predicate) {
cfang@1250 1794 if (iff->in(1)->Opcode() != Op_Conv2B ||
cfang@1250 1795 iff->in(1)->in(1)->Opcode() != Op_Opaque1) {
cfang@1250 1796 return false;
cfang@1250 1797 }
cfang@1250 1798 }
cfang@1250 1799
cfang@1250 1800 ProjNode* other_proj = iff->proj_out(1-proj->_con)->as_Proj();
cfang@1250 1801 return is_uncommon_trap_proj(other_proj, must_reason_predicate);
cfang@1250 1802 }
cfang@1250 1803
cfang@1250 1804 //------------------------------create_new_if_for_predicate------------------------
cfang@1250 1805 // create a new if above the uct_if_pattern for the predicate to be promoted.
cfang@1250 1806 //
cfang@1250 1807 // before after
cfang@1250 1808 // ---------- ----------
cfang@1250 1809 // ctrl ctrl
cfang@1250 1810 // | |
cfang@1250 1811 // | |
cfang@1250 1812 // v v
cfang@1250 1813 // iff new_iff
cfang@1250 1814 // / \ / \
cfang@1250 1815 // / \ / \
cfang@1250 1816 // v v v v
cfang@1250 1817 // uncommon_proj cont_proj if_uct if_cont
cfang@1250 1818 // \ | | | |
cfang@1250 1819 // \ | | | |
cfang@1250 1820 // v v v | v
cfang@1250 1821 // rgn loop | iff
cfang@1250 1822 // | | / \
cfang@1250 1823 // | | / \
cfang@1250 1824 // v | v v
cfang@1250 1825 // uncommon_trap | uncommon_proj cont_proj
cfang@1250 1826 // \ \ | |
cfang@1250 1827 // \ \ | |
cfang@1250 1828 // v v v v
cfang@1250 1829 // rgn loop
cfang@1250 1830 // |
cfang@1250 1831 // |
cfang@1250 1832 // v
cfang@1250 1833 // uncommon_trap
cfang@1250 1834 //
cfang@1250 1835 //
cfang@1250 1836 // We will create a region to guard the uct call if there is no one there.
cfang@1250 1837 // The true projecttion (if_cont) of the new_iff is returned.
cfang@1250 1838 ProjNode* PhaseIdealLoop::create_new_if_for_predicate(ProjNode* cont_proj) {
cfang@1250 1839 assert(is_uncommon_trap_if_pattern(cont_proj, true), "must be a uct if pattern!");
cfang@1250 1840 IfNode* iff = cont_proj->in(0)->as_If();
cfang@1250 1841
cfang@1250 1842 ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con);
cfang@1250 1843 Node *rgn = uncommon_proj->unique_ctrl_out();
cfang@1250 1844 assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
cfang@1250 1845
cfang@1250 1846 if (!rgn->is_Region()) { // create a region to guard the call
cfang@1250 1847 assert(rgn->is_Call(), "must be call uct");
cfang@1250 1848 CallNode* call = rgn->as_Call();
cfang@1250 1849 rgn = new (C, 1) RegionNode(1);
cfang@1250 1850 _igvn.set_type(rgn, rgn->bottom_type());
cfang@1250 1851 rgn->add_req(uncommon_proj);
cfang@1250 1852 set_idom(rgn, idom(uncommon_proj), dom_depth(uncommon_proj)+1);
cfang@1250 1853 _igvn.hash_delete(call);
cfang@1250 1854 call->set_req(0, rgn);
cfang@1250 1855 }
cfang@1250 1856
cfang@1250 1857 // Create new_iff
cfang@1250 1858 uint iffdd = dom_depth(iff);
cfang@1250 1859 IdealLoopTree* lp = get_loop(iff);
cfang@1250 1860 IfNode *new_iff = new (C, 2) IfNode(iff->in(0), NULL, iff->_prob, iff->_fcnt);
cfang@1250 1861 register_node(new_iff, lp, idom(iff), iffdd);
cfang@1250 1862 Node *if_cont = new (C, 1) IfTrueNode(new_iff);
cfang@1250 1863 Node *if_uct = new (C, 1) IfFalseNode(new_iff);
cfang@1250 1864 if (cont_proj->is_IfFalse()) {
cfang@1250 1865 // Swap
cfang@1250 1866 Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp;
cfang@1250 1867 }
cfang@1250 1868 register_node(if_cont, lp, new_iff, iffdd);
cfang@1250 1869 register_node(if_uct, get_loop(rgn), new_iff, iffdd);
cfang@1250 1870
cfang@1250 1871 // if_cont to iff
cfang@1250 1872 _igvn.hash_delete(iff);
cfang@1250 1873 iff->set_req(0, if_cont);
cfang@1250 1874 set_idom(iff, if_cont, dom_depth(iff));
cfang@1250 1875
cfang@1250 1876 // if_uct to rgn
cfang@1250 1877 _igvn.hash_delete(rgn);
cfang@1250 1878 rgn->add_req(if_uct);
cfang@1250 1879 Node* ridom = idom(rgn);
cfang@1250 1880 Node* nrdom = dom_lca(ridom, new_iff);
cfang@1250 1881 set_idom(rgn, nrdom, dom_depth(rgn));
cfang@1250 1882
cfang@1250 1883 // rgn must have no phis
cfang@1250 1884 assert(!rgn->as_Region()->has_phi(), "region must have no phis");
cfang@1250 1885
cfang@1250 1886 return if_cont->as_Proj();
cfang@1250 1887 }
cfang@1250 1888
cfang@1250 1889 //------------------------------find_predicate_insertion_point--------------------------
cfang@1250 1890 // Find a good location to insert a predicate
cfang@1250 1891 ProjNode* PhaseIdealLoop::find_predicate_insertion_point(Node* start_c) {
cfang@1250 1892 if (start_c == C->root() || !start_c->is_Proj())
cfang@1250 1893 return NULL;
cfang@1250 1894 if (is_uncommon_trap_if_pattern(start_c->as_Proj(), true/*Reason_Predicate*/)) {
cfang@1250 1895 return start_c->as_Proj();
cfang@1250 1896 }
cfang@1250 1897 return NULL;
cfang@1250 1898 }
cfang@1250 1899
cfang@1250 1900 //------------------------------Invariance-----------------------------------
cfang@1250 1901 // Helper class for loop_predication_impl to compute invariance on the fly and
cfang@1250 1902 // clone invariants.
cfang@1250 1903 class Invariance : public StackObj {
cfang@1250 1904 VectorSet _visited, _invariant;
cfang@1250 1905 Node_Stack _stack;
cfang@1250 1906 VectorSet _clone_visited;
cfang@1250 1907 Node_List _old_new; // map of old to new (clone)
cfang@1250 1908 IdealLoopTree* _lpt;
cfang@1250 1909 PhaseIdealLoop* _phase;
cfang@1250 1910
cfang@1250 1911 // Helper function to set up the invariance for invariance computation
cfang@1250 1912 // If n is a known invariant, set up directly. Otherwise, look up the
cfang@1250 1913 // the possibility to push n onto the stack for further processing.
cfang@1250 1914 void visit(Node* use, Node* n) {
cfang@1250 1915 if (_lpt->is_invariant(n)) { // known invariant
cfang@1250 1916 _invariant.set(n->_idx);
cfang@1250 1917 } else if (!n->is_CFG()) {
cfang@1250 1918 Node *n_ctrl = _phase->ctrl_or_self(n);
cfang@1250 1919 Node *u_ctrl = _phase->ctrl_or_self(use); // self if use is a CFG
cfang@1250 1920 if (_phase->is_dominator(n_ctrl, u_ctrl)) {
cfang@1250 1921 _stack.push(n, n->in(0) == NULL ? 1 : 0);
cfang@1250 1922 }
cfang@1250 1923 }
cfang@1250 1924 }
cfang@1250 1925
cfang@1250 1926 // Compute invariance for "the_node" and (possibly) all its inputs recursively
cfang@1250 1927 // on the fly
cfang@1250 1928 void compute_invariance(Node* n) {
cfang@1250 1929 assert(_visited.test(n->_idx), "must be");
cfang@1250 1930 visit(n, n);
cfang@1250 1931 while (_stack.is_nonempty()) {
cfang@1250 1932 Node* n = _stack.node();
cfang@1250 1933 uint idx = _stack.index();
cfang@1250 1934 if (idx == n->req()) { // all inputs are processed
cfang@1250 1935 _stack.pop();
cfang@1250 1936 // n is invariant if it's inputs are all invariant
cfang@1250 1937 bool all_inputs_invariant = true;
cfang@1250 1938 for (uint i = 0; i < n->req(); i++) {
cfang@1250 1939 Node* in = n->in(i);
cfang@1250 1940 if (in == NULL) continue;
cfang@1250 1941 assert(_visited.test(in->_idx), "must have visited input");
cfang@1250 1942 if (!_invariant.test(in->_idx)) { // bad guy
cfang@1250 1943 all_inputs_invariant = false;
cfang@1250 1944 break;
cfang@1250 1945 }
cfang@1250 1946 }
cfang@1250 1947 if (all_inputs_invariant) {
cfang@1250 1948 _invariant.set(n->_idx); // I am a invariant too
cfang@1250 1949 }
cfang@1250 1950 } else { // process next input
cfang@1250 1951 _stack.set_index(idx + 1);
cfang@1250 1952 Node* m = n->in(idx);
cfang@1250 1953 if (m != NULL && !_visited.test_set(m->_idx)) {
cfang@1250 1954 visit(n, m);
cfang@1250 1955 }
cfang@1250 1956 }
cfang@1250 1957 }
cfang@1250 1958 }
cfang@1250 1959
cfang@1250 1960 // Helper function to set up _old_new map for clone_nodes.
cfang@1250 1961 // If n is a known invariant, set up directly ("clone" of n == n).
cfang@1250 1962 // Otherwise, push n onto the stack for real cloning.
cfang@1250 1963 void clone_visit(Node* n) {
cfang@1250 1964 assert(_invariant.test(n->_idx), "must be invariant");
cfang@1250 1965 if (_lpt->is_invariant(n)) { // known invariant
cfang@1250 1966 _old_new.map(n->_idx, n);
cfang@1250 1967 } else{ // to be cloned
cfang@1250 1968 assert (!n->is_CFG(), "should not see CFG here");
cfang@1250 1969 _stack.push(n, n->in(0) == NULL ? 1 : 0);
cfang@1250 1970 }
cfang@1250 1971 }
cfang@1250 1972
cfang@1250 1973 // Clone "n" and (possibly) all its inputs recursively
cfang@1250 1974 void clone_nodes(Node* n, Node* ctrl) {
cfang@1250 1975 clone_visit(n);
cfang@1250 1976 while (_stack.is_nonempty()) {
cfang@1250 1977 Node* n = _stack.node();
cfang@1250 1978 uint idx = _stack.index();
cfang@1250 1979 if (idx == n->req()) { // all inputs processed, clone n!
cfang@1250 1980 _stack.pop();
cfang@1250 1981 // clone invariant node
cfang@1250 1982 Node* n_cl = n->clone();
cfang@1250 1983 _old_new.map(n->_idx, n_cl);
cfang@1250 1984 _phase->register_new_node(n_cl, ctrl);
cfang@1250 1985 for (uint i = 0; i < n->req(); i++) {
cfang@1250 1986 Node* in = n_cl->in(i);
cfang@1250 1987 if (in == NULL) continue;
cfang@1250 1988 n_cl->set_req(i, _old_new[in->_idx]);
cfang@1250 1989 }
cfang@1250 1990 } else { // process next input
cfang@1250 1991 _stack.set_index(idx + 1);
cfang@1250 1992 Node* m = n->in(idx);
cfang@1250 1993 if (m != NULL && !_clone_visited.test_set(m->_idx)) {
cfang@1250 1994 clone_visit(m); // visit the input
cfang@1250 1995 }
cfang@1250 1996 }
cfang@1250 1997 }
cfang@1250 1998 }
cfang@1250 1999
cfang@1250 2000 public:
cfang@1250 2001 Invariance(Arena* area, IdealLoopTree* lpt) :
cfang@1250 2002 _lpt(lpt), _phase(lpt->_phase),
cfang@1250 2003 _visited(area), _invariant(area), _stack(area, 10 /* guess */),
cfang@1250 2004 _clone_visited(area), _old_new(area)
cfang@1250 2005 {}
cfang@1250 2006
cfang@1250 2007 // Map old to n for invariance computation and clone
cfang@1250 2008 void map_ctrl(Node* old, Node* n) {
cfang@1250 2009 assert(old->is_CFG() && n->is_CFG(), "must be");
cfang@1250 2010 _old_new.map(old->_idx, n); // "clone" of old is n
cfang@1250 2011 _invariant.set(old->_idx); // old is invariant
cfang@1250 2012 _clone_visited.set(old->_idx);
cfang@1250 2013 }
cfang@1250 2014
cfang@1250 2015 // Driver function to compute invariance
cfang@1250 2016 bool is_invariant(Node* n) {
cfang@1250 2017 if (!_visited.test_set(n->_idx))
cfang@1250 2018 compute_invariance(n);
cfang@1250 2019 return (_invariant.test(n->_idx) != 0);
cfang@1250 2020 }
cfang@1250 2021
cfang@1250 2022 // Driver function to clone invariant
cfang@1250 2023 Node* clone(Node* n, Node* ctrl) {
cfang@1250 2024 assert(ctrl->is_CFG(), "must be");
cfang@1250 2025 assert(_invariant.test(n->_idx), "must be an invariant");
cfang@1250 2026 if (!_clone_visited.test(n->_idx))
cfang@1250 2027 clone_nodes(n, ctrl);
cfang@1250 2028 return _old_new[n->_idx];
cfang@1250 2029 }
cfang@1250 2030 };
cfang@1250 2031
cfang@1250 2032 //------------------------------is_range_check_if -----------------------------------
cfang@1250 2033 // Returns true if the predicate of iff is in "scale*iv + offset u< load_range(ptr)" format
cfang@1250 2034 // Note: this function is particularly designed for loop predication. We require load_range
cfang@1250 2035 // and offset to be loop invariant computed on the fly by "invar"
cfang@1250 2036 bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const {
cfang@1250 2037 if (!is_loop_exit(iff)) {
cfang@1250 2038 return false;
cfang@1250 2039 }
cfang@1250 2040 if (!iff->in(1)->is_Bool()) {
cfang@1250 2041 return false;
cfang@1250 2042 }
cfang@1250 2043 const BoolNode *bol = iff->in(1)->as_Bool();
cfang@1250 2044 if (bol->_test._test != BoolTest::lt) {
cfang@1250 2045 return false;
cfang@1250 2046 }
cfang@1250 2047 if (!bol->in(1)->is_Cmp()) {
cfang@1250 2048 return false;
cfang@1250 2049 }
cfang@1250 2050 const CmpNode *cmp = bol->in(1)->as_Cmp();
cfang@1250 2051 if (cmp->Opcode() != Op_CmpU ) {
cfang@1250 2052 return false;
cfang@1250 2053 }
cfang@1250 2054 if (cmp->in(2)->Opcode() != Op_LoadRange) {
cfang@1250 2055 return false;
cfang@1250 2056 }
cfang@1250 2057 LoadRangeNode* lr = (LoadRangeNode*)cmp->in(2);
cfang@1250 2058 if (!invar.is_invariant(lr)) { // loadRange must be invariant
cfang@1250 2059 return false;
cfang@1250 2060 }
cfang@1250 2061 Node *iv = _head->as_CountedLoop()->phi();
cfang@1250 2062 int scale = 0;
cfang@1250 2063 Node *offset = NULL;
cfang@1250 2064 if (!phase->is_scaled_iv_plus_offset(cmp->in(1), iv, &scale, &offset)) {
cfang@1250 2065 return false;
cfang@1250 2066 }
cfang@1250 2067 if(offset && !invar.is_invariant(offset)) { // offset must be invariant
cfang@1250 2068 return false;
cfang@1250 2069 }
cfang@1250 2070 return true;
cfang@1250 2071 }
cfang@1250 2072
cfang@1250 2073 //------------------------------rc_predicate-----------------------------------
cfang@1250 2074 // Create a range check predicate
cfang@1250 2075 //
cfang@1250 2076 // for (i = init; i < limit; i += stride) {
cfang@1250 2077 // a[scale*i+offset]
cfang@1250 2078 // }
cfang@1250 2079 //
cfang@1250 2080 // Compute max(scale*i + offset) for init <= i < limit and build the predicate
cfang@1250 2081 // as "max(scale*i + offset) u< a.length".
cfang@1250 2082 //
cfang@1250 2083 // There are two cases for max(scale*i + offset):
cfang@1250 2084 // (1) stride*scale > 0
cfang@1250 2085 // max(scale*i + offset) = scale*(limit-stride) + offset
cfang@1250 2086 // (2) stride*scale < 0
cfang@1250 2087 // max(scale*i + offset) = scale*init + offset
cfang@1250 2088 BoolNode* PhaseIdealLoop::rc_predicate(Node* ctrl,
cfang@1250 2089 int scale, Node* offset,
cfang@1250 2090 Node* init, Node* limit, Node* stride,
never@1390 2091 Node* range, bool upper) {
never@1390 2092 DEBUG_ONLY(ttyLocker ttyl);
never@1390 2093 if (TraceLoopPredicate) tty->print("rc_predicate ");
never@1390 2094
cfang@1250 2095 Node* max_idx_expr = init;
cfang@1250 2096 int stride_con = stride->get_int();
never@1390 2097 if ((stride_con > 0) == (scale > 0) == upper) {
cfang@1250 2098 max_idx_expr = new (C, 3) SubINode(limit, stride);
cfang@1250 2099 register_new_node(max_idx_expr, ctrl);
never@1390 2100 if (TraceLoopPredicate) tty->print("(limit - stride) ");
never@1390 2101 } else {
never@1390 2102 if (TraceLoopPredicate) tty->print("init ");
cfang@1250 2103 }
cfang@1250 2104
cfang@1250 2105 if (scale != 1) {
cfang@1250 2106 ConNode* con_scale = _igvn.intcon(scale);
cfang@1250 2107 max_idx_expr = new (C, 3) MulINode(max_idx_expr, con_scale);
cfang@1250 2108 register_new_node(max_idx_expr, ctrl);
never@1390 2109 if (TraceLoopPredicate) tty->print("* %d ", scale);
cfang@1250 2110 }
cfang@1250 2111
cfang@1250 2112 if (offset && (!offset->is_Con() || offset->get_int() != 0)){
cfang@1250 2113 max_idx_expr = new (C, 3) AddINode(max_idx_expr, offset);
cfang@1250 2114 register_new_node(max_idx_expr, ctrl);
never@1390 2115 if (TraceLoopPredicate)
never@1390 2116 if (offset->is_Con()) tty->print("+ %d ", offset->get_int());
never@1390 2117 else tty->print("+ offset ");
cfang@1250 2118 }
cfang@1250 2119
cfang@1250 2120 CmpUNode* cmp = new (C, 3) CmpUNode(max_idx_expr, range);
cfang@1250 2121 register_new_node(cmp, ctrl);
cfang@1250 2122 BoolNode* bol = new (C, 2) BoolNode(cmp, BoolTest::lt);
cfang@1250 2123 register_new_node(bol, ctrl);
never@1390 2124
never@1390 2125 if (TraceLoopPredicate) tty->print_cr("<u range");
cfang@1250 2126 return bol;
cfang@1250 2127 }
cfang@1250 2128
cfang@1250 2129 //------------------------------ loop_predication_impl--------------------------
cfang@1250 2130 // Insert loop predicates for null checks and range checks
cfang@1250 2131 bool PhaseIdealLoop::loop_predication_impl(IdealLoopTree *loop) {
cfang@1250 2132 if (!UseLoopPredicate) return false;
cfang@1250 2133
never@1362 2134 if (!loop->_head->is_Loop()) {
never@1362 2135 // Could be a simple region when irreducible loops are present.
never@1362 2136 return false;
never@1362 2137 }
never@1362 2138
never@1362 2139 CountedLoopNode *cl = NULL;
never@1362 2140 if (loop->_head->is_CountedLoop()) {
never@1362 2141 cl = loop->_head->as_CountedLoop();
never@1362 2142 // do nothing for iteration-splitted loops
never@1362 2143 if (!cl->is_normal_loop()) return false;
never@1362 2144 }
never@1362 2145
cfang@1250 2146 // Too many traps seen?
cfang@1250 2147 bool tmt = C->too_many_traps(C->method(), 0, Deoptimization::Reason_predicate);
cfang@1250 2148 int tc = C->trap_count(Deoptimization::Reason_predicate);
cfang@1250 2149 if (tmt || tc > 0) {
cfang@1250 2150 if (TraceLoopPredicate) {
cfang@1250 2151 tty->print_cr("too many predicate traps: %d", tc);
cfang@1250 2152 C->method()->print(); // which method has too many predicate traps
cfang@1250 2153 tty->print_cr("");
cfang@1250 2154 }
cfang@1250 2155 return false;
cfang@1250 2156 }
cfang@1250 2157
cfang@1250 2158 LoopNode *lpn = loop->_head->as_Loop();
cfang@1250 2159 Node* entry = lpn->in(LoopNode::EntryControl);
cfang@1250 2160
cfang@1250 2161 ProjNode *predicate_proj = find_predicate_insertion_point(entry);
cfang@1250 2162 if (!predicate_proj){
cfang@1250 2163 #ifndef PRODUCT
cfang@1250 2164 if (TraceLoopPredicate) {
cfang@1250 2165 tty->print("missing predicate:");
cfang@1250 2166 loop->dump_head();
cfang@1250 2167 }
cfang@1250 2168 #endif
cfang@1250 2169 return false;
cfang@1250 2170 }
cfang@1250 2171
cfang@1250 2172 ConNode* zero = _igvn.intcon(0);
cfang@1250 2173 set_ctrl(zero, C->root());
cfang@1250 2174 Node *cond_false = new (C, 2) Conv2BNode(zero);
cfang@1250 2175 register_new_node(cond_false, C->root());
cfang@1250 2176 ConNode* one = _igvn.intcon(1);
cfang@1250 2177 set_ctrl(one, C->root());
cfang@1250 2178 Node *cond_true = new (C, 2) Conv2BNode(one);
cfang@1250 2179 register_new_node(cond_true, C->root());
cfang@1250 2180
cfang@1250 2181 ResourceArea *area = Thread::current()->resource_area();
cfang@1250 2182 Invariance invar(area, loop);
cfang@1250 2183
cfang@1250 2184 // Create list of if-projs such that a newer proj dominates all older
cfang@1250 2185 // projs in the list, and they all dominate loop->tail()
cfang@1250 2186 Node_List if_proj_list(area);
cfang@1250 2187 LoopNode *head = loop->_head->as_Loop();
cfang@1250 2188 Node *current_proj = loop->tail(); //start from tail
cfang@1250 2189 while ( current_proj != head ) {
cfang@1250 2190 if (loop == get_loop(current_proj) && // still in the loop ?
cfang@1250 2191 current_proj->is_Proj() && // is a projection ?
cfang@1250 2192 current_proj->in(0)->Opcode() == Op_If) { // is a if projection ?
cfang@1250 2193 if_proj_list.push(current_proj);
cfang@1250 2194 }
cfang@1250 2195 current_proj = idom(current_proj);
cfang@1250 2196 }
cfang@1250 2197
cfang@1250 2198 bool hoisted = false; // true if at least one proj is promoted
cfang@1250 2199 while (if_proj_list.size() > 0) {
cfang@1250 2200 // Following are changed to nonnull when a predicate can be hoisted
cfang@1250 2201 ProjNode* new_predicate_proj = NULL;
cfang@1250 2202
cfang@1250 2203 ProjNode* proj = if_proj_list.pop()->as_Proj();
cfang@1250 2204 IfNode* iff = proj->in(0)->as_If();
cfang@1250 2205
cfang@1250 2206 if (!is_uncommon_trap_if_pattern(proj)) {
cfang@1250 2207 if (loop->is_loop_exit(iff)) {
cfang@1250 2208 // stop processing the remaining projs in the list because the execution of them
cfang@1250 2209 // depends on the condition of "iff" (iff->in(1)).
cfang@1250 2210 break;
cfang@1250 2211 } else {
cfang@1250 2212 // Both arms are inside the loop. There are two cases:
cfang@1250 2213 // (1) there is one backward branch. In this case, any remaining proj
cfang@1250 2214 // in the if_proj list post-dominates "iff". So, the condition of "iff"
cfang@1250 2215 // does not determine the execution the remining projs directly, and we
cfang@1250 2216 // can safely continue.
cfang@1250 2217 // (2) both arms are forwarded, i.e. a diamond shape. In this case, "proj"
cfang@1250 2218 // does not dominate loop->tail(), so it can not be in the if_proj list.
cfang@1250 2219 continue;
cfang@1250 2220 }
cfang@1250 2221 }
cfang@1250 2222
cfang@1250 2223 Node* test = iff->in(1);
cfang@1250 2224 if (!test->is_Bool()){ //Conv2B, ...
cfang@1250 2225 continue;
cfang@1250 2226 }
cfang@1250 2227 BoolNode* bol = test->as_Bool();
cfang@1250 2228 if (invar.is_invariant(bol)) {
cfang@1250 2229 // Invariant test
cfang@1250 2230 new_predicate_proj = create_new_if_for_predicate(predicate_proj);
cfang@1250 2231 Node* ctrl = new_predicate_proj->in(0)->as_If()->in(0);
never@1390 2232 BoolNode* new_predicate_bol = invar.clone(bol, ctrl)->as_Bool();
never@1390 2233
never@1390 2234 // Negate test if necessary
never@1390 2235 bool negated = false;
never@1390 2236 if (proj->_con != predicate_proj->_con) {
never@1390 2237 new_predicate_bol = new (C, 2) BoolNode(new_predicate_bol->in(1), new_predicate_bol->_test.negate());
never@1390 2238 register_new_node(new_predicate_bol, ctrl);
never@1390 2239 negated = true;
never@1390 2240 }
never@1390 2241 IfNode* new_predicate_iff = new_predicate_proj->in(0)->as_If();
never@1390 2242 _igvn.hash_delete(new_predicate_iff);
never@1390 2243 new_predicate_iff->set_req(1, new_predicate_bol);
never@1390 2244 if (TraceLoopPredicate) tty->print_cr("invariant if%s: %d", negated ? " negated" : "", new_predicate_iff->_idx);
never@1390 2245
cfang@1250 2246 } else if (cl != NULL && loop->is_range_check_if(iff, this, invar)) {
never@1390 2247 assert(proj->_con == predicate_proj->_con, "must match");
never@1390 2248
never@1390 2249 // Range check for counted loops
cfang@1250 2250 const Node* cmp = bol->in(1)->as_Cmp();
cfang@1250 2251 Node* idx = cmp->in(1);
cfang@1250 2252 assert(!invar.is_invariant(idx), "index is variant");
cfang@1250 2253 assert(cmp->in(2)->Opcode() == Op_LoadRange, "must be");
never@1390 2254 Node* ld_rng = cmp->in(2); // LoadRangeNode
cfang@1250 2255 assert(invar.is_invariant(ld_rng), "load range must be invariant");
cfang@1250 2256 int scale = 1;
cfang@1250 2257 Node* offset = zero;
cfang@1250 2258 bool ok = is_scaled_iv_plus_offset(idx, cl->phi(), &scale, &offset);
cfang@1250 2259 assert(ok, "must be index expression");
never@1390 2260
never@1390 2261 Node* init = cl->init_trip();
never@1390 2262 Node* limit = cl->limit();
never@1390 2263 Node* stride = cl->stride();
never@1390 2264
never@1390 2265 // Build if's for the upper and lower bound tests. The
never@1390 2266 // lower_bound test will dominate the upper bound test and all
never@1390 2267 // cloned or created nodes will use the lower bound test as
never@1390 2268 // their declared control.
never@1390 2269 ProjNode* lower_bound_proj = create_new_if_for_predicate(predicate_proj);
never@1390 2270 ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj);
never@1390 2271 assert(upper_bound_proj->in(0)->as_If()->in(0) == lower_bound_proj, "should dominate");
never@1390 2272 Node *ctrl = lower_bound_proj->in(0)->as_If()->in(0);
never@1390 2273
never@1390 2274 // Perform cloning to keep Invariance state correct since the
never@1390 2275 // late schedule will place invariant things in the loop.
never@1390 2276 ld_rng = invar.clone(ld_rng, ctrl);
cfang@1250 2277 if (offset && offset != zero) {
cfang@1250 2278 assert(invar.is_invariant(offset), "offset must be loop invariant");
cfang@1250 2279 offset = invar.clone(offset, ctrl);
cfang@1250 2280 }
cfang@1250 2281
never@1390 2282 // Test the lower bound
never@1390 2283 Node* lower_bound_bol = rc_predicate(ctrl, scale, offset, init, limit, stride, ld_rng, false);
never@1390 2284 IfNode* lower_bound_iff = lower_bound_proj->in(0)->as_If();
never@1390 2285 _igvn.hash_delete(lower_bound_iff);
never@1390 2286 lower_bound_iff->set_req(1, lower_bound_bol);
never@1390 2287 if (TraceLoopPredicate) tty->print_cr("lower bound check if: %d", lower_bound_iff->_idx);
never@1390 2288
never@1390 2289 // Test the upper bound
never@1390 2290 Node* upper_bound_bol = rc_predicate(ctrl, scale, offset, init, limit, stride, ld_rng, true);
never@1390 2291 IfNode* upper_bound_iff = upper_bound_proj->in(0)->as_If();
never@1390 2292 _igvn.hash_delete(upper_bound_iff);
never@1390 2293 upper_bound_iff->set_req(1, upper_bound_bol);
never@1390 2294 if (TraceLoopPredicate) tty->print_cr("upper bound check if: %d", lower_bound_iff->_idx);
never@1390 2295
never@1390 2296 // Fall through into rest of the clean up code which will move
never@1390 2297 // any dependent nodes onto the upper bound test.
never@1390 2298 new_predicate_proj = upper_bound_proj;
never@1390 2299 } else {
cfang@1250 2300 // The other proj of the "iff" is a uncommon trap projection, and we can assume
cfang@1250 2301 // the other proj will not be executed ("executed" means uct raised).
cfang@1250 2302 continue;
never@1390 2303 }
cfang@1250 2304
never@1390 2305 // Success - attach condition (new_predicate_bol) to predicate if
never@1390 2306 invar.map_ctrl(proj, new_predicate_proj); // so that invariance test can be appropriate
never@1390 2307
never@1390 2308 // Eliminate the old if in the loop body
never@1390 2309 _igvn.hash_delete(iff);
never@1390 2310 iff->set_req(1, proj->is_IfFalse() ? cond_false : cond_true);
never@1390 2311
never@1390 2312 Node* ctrl = new_predicate_proj; // new control
never@1390 2313 ProjNode* dp = proj; // old control
never@1390 2314 assert(get_loop(dp) == loop, "guaranteed at the time of collecting proj");
never@1390 2315 // Find nodes (depends only on the test) off the surviving projection;
never@1390 2316 // move them outside the loop with the control of proj_clone
never@1390 2317 for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) {
never@1390 2318 Node* cd = dp->fast_out(i); // Control-dependent node
never@1390 2319 if (cd->depends_only_on_test()) {
never@1390 2320 assert(cd->in(0) == dp, "");
never@1390 2321 _igvn.hash_delete(cd);
never@1390 2322 cd->set_req(0, ctrl); // ctrl, not NULL
never@1390 2323 set_early_ctrl(cd);
never@1390 2324 _igvn._worklist.push(cd);
never@1390 2325 IdealLoopTree *new_loop = get_loop(get_ctrl(cd));
never@1390 2326 if (new_loop != loop) {
never@1390 2327 if (!loop->_child) loop->_body.yank(cd);
never@1390 2328 if (!new_loop->_child ) new_loop->_body.push(cd);
never@1390 2329 }
never@1390 2330 --i;
never@1390 2331 --imax;
cfang@1250 2332 }
never@1390 2333 }
cfang@1250 2334
never@1390 2335 hoisted = true;
never@1390 2336 C->set_major_progress();
cfang@1250 2337 } // end while
cfang@1250 2338
cfang@1250 2339 #ifndef PRODUCT
never@1390 2340 // report that the loop predication has been actually performed
never@1390 2341 // for this loop
never@1390 2342 if (TraceLoopPredicate && hoisted) {
never@1390 2343 tty->print("Loop Predication Performed:");
never@1390 2344 loop->dump_head();
never@1390 2345 }
cfang@1250 2346 #endif
cfang@1250 2347
cfang@1250 2348 return hoisted;
cfang@1250 2349 }
cfang@1250 2350
cfang@1250 2351 //------------------------------loop_predication--------------------------------
cfang@1250 2352 // driver routine for loop predication optimization
cfang@1250 2353 bool IdealLoopTree::loop_predication( PhaseIdealLoop *phase) {
cfang@1250 2354 bool hoisted = false;
cfang@1250 2355 // Recursively promote predicates
cfang@1250 2356 if ( _child ) {
cfang@1250 2357 hoisted = _child->loop_predication( phase);
cfang@1250 2358 }
cfang@1250 2359
cfang@1250 2360 // self
cfang@1250 2361 if (!_irreducible && !tail()->is_top()) {
cfang@1250 2362 hoisted |= phase->loop_predication_impl(this);
cfang@1250 2363 }
cfang@1250 2364
cfang@1250 2365 if ( _next ) { //sibling
cfang@1250 2366 hoisted |= _next->loop_predication( phase);
cfang@1250 2367 }
cfang@1250 2368
cfang@1250 2369 return hoisted;
cfang@1250 2370 }