src/share/vm/opto/loopnode.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7590
0a5d68482373
parent 6876
710a3c8b516e
child 8604
04d83ba48607
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "ci/ciMethodData.hpp"
aoqi@0 27 #include "compiler/compileLog.hpp"
aoqi@0 28 #include "libadt/vectset.hpp"
aoqi@0 29 #include "memory/allocation.inline.hpp"
aoqi@0 30 #include "opto/addnode.hpp"
aoqi@0 31 #include "opto/callnode.hpp"
aoqi@0 32 #include "opto/connode.hpp"
aoqi@0 33 #include "opto/divnode.hpp"
aoqi@0 34 #include "opto/idealGraphPrinter.hpp"
aoqi@0 35 #include "opto/loopnode.hpp"
aoqi@0 36 #include "opto/mulnode.hpp"
aoqi@0 37 #include "opto/rootnode.hpp"
aoqi@0 38 #include "opto/superword.hpp"
aoqi@0 39
aoqi@0 40 //=============================================================================
aoqi@0 41 //------------------------------is_loop_iv-------------------------------------
aoqi@0 42 // Determine if a node is Counted loop induction variable.
aoqi@0 43 // The method is declared in node.hpp.
aoqi@0 44 const Node* Node::is_loop_iv() const {
aoqi@0 45 if (this->is_Phi() && !this->as_Phi()->is_copy() &&
aoqi@0 46 this->as_Phi()->region()->is_CountedLoop() &&
aoqi@0 47 this->as_Phi()->region()->as_CountedLoop()->phi() == this) {
aoqi@0 48 return this;
aoqi@0 49 } else {
aoqi@0 50 return NULL;
aoqi@0 51 }
aoqi@0 52 }
aoqi@0 53
aoqi@0 54 //=============================================================================
aoqi@0 55 //------------------------------dump_spec--------------------------------------
aoqi@0 56 // Dump special per-node info
aoqi@0 57 #ifndef PRODUCT
aoqi@0 58 void LoopNode::dump_spec(outputStream *st) const {
aoqi@0 59 if (is_inner_loop()) st->print( "inner " );
aoqi@0 60 if (is_partial_peel_loop()) st->print( "partial_peel " );
aoqi@0 61 if (partial_peel_has_failed()) st->print( "partial_peel_failed " );
aoqi@0 62 }
aoqi@0 63 #endif
aoqi@0 64
aoqi@0 65 //------------------------------is_valid_counted_loop-------------------------
aoqi@0 66 bool LoopNode::is_valid_counted_loop() const {
aoqi@0 67 if (is_CountedLoop()) {
aoqi@0 68 CountedLoopNode* l = as_CountedLoop();
aoqi@0 69 CountedLoopEndNode* le = l->loopexit();
aoqi@0 70 if (le != NULL &&
aoqi@0 71 le->proj_out(1 /* true */) == l->in(LoopNode::LoopBackControl)) {
aoqi@0 72 Node* phi = l->phi();
aoqi@0 73 Node* exit = le->proj_out(0 /* false */);
aoqi@0 74 if (exit != NULL && exit->Opcode() == Op_IfFalse &&
aoqi@0 75 phi != NULL && phi->is_Phi() &&
aoqi@0 76 phi->in(LoopNode::LoopBackControl) == l->incr() &&
aoqi@0 77 le->loopnode() == l && le->stride_is_con()) {
aoqi@0 78 return true;
aoqi@0 79 }
aoqi@0 80 }
aoqi@0 81 }
aoqi@0 82 return false;
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 //------------------------------get_early_ctrl---------------------------------
aoqi@0 86 // Compute earliest legal control
aoqi@0 87 Node *PhaseIdealLoop::get_early_ctrl( Node *n ) {
aoqi@0 88 assert( !n->is_Phi() && !n->is_CFG(), "this code only handles data nodes" );
aoqi@0 89 uint i;
aoqi@0 90 Node *early;
aoqi@0 91 if (n->in(0) && !n->is_expensive()) {
aoqi@0 92 early = n->in(0);
aoqi@0 93 if (!early->is_CFG()) // Might be a non-CFG multi-def
aoqi@0 94 early = get_ctrl(early); // So treat input as a straight data input
aoqi@0 95 i = 1;
aoqi@0 96 } else {
aoqi@0 97 early = get_ctrl(n->in(1));
aoqi@0 98 i = 2;
aoqi@0 99 }
aoqi@0 100 uint e_d = dom_depth(early);
aoqi@0 101 assert( early, "" );
aoqi@0 102 for (; i < n->req(); i++) {
aoqi@0 103 Node *cin = get_ctrl(n->in(i));
aoqi@0 104 assert( cin, "" );
aoqi@0 105 // Keep deepest dominator depth
aoqi@0 106 uint c_d = dom_depth(cin);
aoqi@0 107 if (c_d > e_d) { // Deeper guy?
aoqi@0 108 early = cin; // Keep deepest found so far
aoqi@0 109 e_d = c_d;
aoqi@0 110 } else if (c_d == e_d && // Same depth?
aoqi@0 111 early != cin) { // If not equal, must use slower algorithm
aoqi@0 112 // If same depth but not equal, one _must_ dominate the other
aoqi@0 113 // and we want the deeper (i.e., dominated) guy.
aoqi@0 114 Node *n1 = early;
aoqi@0 115 Node *n2 = cin;
aoqi@0 116 while (1) {
aoqi@0 117 n1 = idom(n1); // Walk up until break cycle
aoqi@0 118 n2 = idom(n2);
aoqi@0 119 if (n1 == cin || // Walked early up to cin
aoqi@0 120 dom_depth(n2) < c_d)
aoqi@0 121 break; // early is deeper; keep him
aoqi@0 122 if (n2 == early || // Walked cin up to early
aoqi@0 123 dom_depth(n1) < c_d) {
aoqi@0 124 early = cin; // cin is deeper; keep him
aoqi@0 125 break;
aoqi@0 126 }
aoqi@0 127 }
aoqi@0 128 e_d = dom_depth(early); // Reset depth register cache
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 // Return earliest legal location
aoqi@0 133 assert(early == find_non_split_ctrl(early), "unexpected early control");
aoqi@0 134
aoqi@0 135 if (n->is_expensive()) {
aoqi@0 136 assert(n->in(0), "should have control input");
aoqi@0 137 early = get_early_ctrl_for_expensive(n, early);
aoqi@0 138 }
aoqi@0 139
aoqi@0 140 return early;
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 //------------------------------get_early_ctrl_for_expensive---------------------------------
aoqi@0 144 // Move node up the dominator tree as high as legal while still beneficial
aoqi@0 145 Node *PhaseIdealLoop::get_early_ctrl_for_expensive(Node *n, Node* earliest) {
aoqi@0 146 assert(n->in(0) && n->is_expensive(), "expensive node with control input here");
aoqi@0 147 assert(OptimizeExpensiveOps, "optimization off?");
aoqi@0 148
aoqi@0 149 Node* ctl = n->in(0);
aoqi@0 150 assert(ctl->is_CFG(), "expensive input 0 must be cfg");
aoqi@0 151 uint min_dom_depth = dom_depth(earliest);
aoqi@0 152 #ifdef ASSERT
aoqi@0 153 if (!is_dominator(ctl, earliest) && !is_dominator(earliest, ctl)) {
aoqi@0 154 dump_bad_graph("Bad graph detected in get_early_ctrl_for_expensive", n, earliest, ctl);
aoqi@0 155 assert(false, "Bad graph detected in get_early_ctrl_for_expensive");
aoqi@0 156 }
aoqi@0 157 #endif
aoqi@0 158 if (dom_depth(ctl) < min_dom_depth) {
aoqi@0 159 return earliest;
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 while (1) {
aoqi@0 163 Node *next = ctl;
aoqi@0 164 // Moving the node out of a loop on the projection of a If
aoqi@0 165 // confuses loop predication. So once we hit a Loop in a If branch
aoqi@0 166 // that doesn't branch to an UNC, we stop. The code that process
aoqi@0 167 // expensive nodes will notice the loop and skip over it to try to
aoqi@0 168 // move the node further up.
aoqi@0 169 if (ctl->is_CountedLoop() && ctl->in(1) != NULL && ctl->in(1)->in(0) != NULL && ctl->in(1)->in(0)->is_If()) {
aoqi@0 170 if (!ctl->in(1)->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none)) {
aoqi@0 171 break;
aoqi@0 172 }
aoqi@0 173 next = idom(ctl->in(1)->in(0));
aoqi@0 174 } else if (ctl->is_Proj()) {
aoqi@0 175 // We only move it up along a projection if the projection is
aoqi@0 176 // the single control projection for its parent: same code path,
aoqi@0 177 // if it's a If with UNC or fallthrough of a call.
aoqi@0 178 Node* parent_ctl = ctl->in(0);
aoqi@0 179 if (parent_ctl == NULL) {
aoqi@0 180 break;
aoqi@0 181 } else if (parent_ctl->is_CountedLoopEnd() && parent_ctl->as_CountedLoopEnd()->loopnode() != NULL) {
aoqi@0 182 next = parent_ctl->as_CountedLoopEnd()->loopnode()->init_control();
aoqi@0 183 } else if (parent_ctl->is_If()) {
aoqi@0 184 if (!ctl->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none)) {
aoqi@0 185 break;
aoqi@0 186 }
aoqi@0 187 assert(idom(ctl) == parent_ctl, "strange");
aoqi@0 188 next = idom(parent_ctl);
aoqi@0 189 } else if (ctl->is_CatchProj()) {
aoqi@0 190 if (ctl->as_Proj()->_con != CatchProjNode::fall_through_index) {
aoqi@0 191 break;
aoqi@0 192 }
aoqi@0 193 assert(parent_ctl->in(0)->in(0)->is_Call(), "strange graph");
aoqi@0 194 next = parent_ctl->in(0)->in(0)->in(0);
aoqi@0 195 } else {
aoqi@0 196 // Check if parent control has a single projection (this
aoqi@0 197 // control is the only possible successor of the parent
aoqi@0 198 // control). If so, we can try to move the node above the
aoqi@0 199 // parent control.
aoqi@0 200 int nb_ctl_proj = 0;
aoqi@0 201 for (DUIterator_Fast imax, i = parent_ctl->fast_outs(imax); i < imax; i++) {
aoqi@0 202 Node *p = parent_ctl->fast_out(i);
aoqi@0 203 if (p->is_Proj() && p->is_CFG()) {
aoqi@0 204 nb_ctl_proj++;
aoqi@0 205 if (nb_ctl_proj > 1) {
aoqi@0 206 break;
aoqi@0 207 }
aoqi@0 208 }
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 if (nb_ctl_proj > 1) {
aoqi@0 212 break;
aoqi@0 213 }
aoqi@0 214 assert(parent_ctl->is_Start() || parent_ctl->is_MemBar() || parent_ctl->is_Call(), "unexpected node");
aoqi@0 215 assert(idom(ctl) == parent_ctl, "strange");
aoqi@0 216 next = idom(parent_ctl);
aoqi@0 217 }
aoqi@0 218 } else {
aoqi@0 219 next = idom(ctl);
aoqi@0 220 }
aoqi@0 221 if (next->is_Root() || next->is_Start() || dom_depth(next) < min_dom_depth) {
aoqi@0 222 break;
aoqi@0 223 }
aoqi@0 224 ctl = next;
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 if (ctl != n->in(0)) {
aoqi@0 228 _igvn.hash_delete(n);
aoqi@0 229 n->set_req(0, ctl);
aoqi@0 230 _igvn.hash_insert(n);
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 return ctl;
aoqi@0 234 }
aoqi@0 235
aoqi@0 236
aoqi@0 237 //------------------------------set_early_ctrl---------------------------------
aoqi@0 238 // Set earliest legal control
aoqi@0 239 void PhaseIdealLoop::set_early_ctrl( Node *n ) {
aoqi@0 240 Node *early = get_early_ctrl(n);
aoqi@0 241
aoqi@0 242 // Record earliest legal location
aoqi@0 243 set_ctrl(n, early);
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 //------------------------------set_subtree_ctrl-------------------------------
aoqi@0 247 // set missing _ctrl entries on new nodes
aoqi@0 248 void PhaseIdealLoop::set_subtree_ctrl( Node *n ) {
aoqi@0 249 // Already set? Get out.
aoqi@0 250 if( _nodes[n->_idx] ) return;
aoqi@0 251 // Recursively set _nodes array to indicate where the Node goes
aoqi@0 252 uint i;
aoqi@0 253 for( i = 0; i < n->req(); ++i ) {
aoqi@0 254 Node *m = n->in(i);
aoqi@0 255 if( m && m != C->root() )
aoqi@0 256 set_subtree_ctrl( m );
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 // Fixup self
aoqi@0 260 set_early_ctrl( n );
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 //------------------------------is_counted_loop--------------------------------
aoqi@0 264 bool PhaseIdealLoop::is_counted_loop( Node *x, IdealLoopTree *loop ) {
aoqi@0 265 PhaseGVN *gvn = &_igvn;
aoqi@0 266
aoqi@0 267 // Counted loop head must be a good RegionNode with only 3 not NULL
aoqi@0 268 // control input edges: Self, Entry, LoopBack.
aoqi@0 269 if (x->in(LoopNode::Self) == NULL || x->req() != 3 || loop->_irreducible) {
aoqi@0 270 return false;
aoqi@0 271 }
aoqi@0 272 Node *init_control = x->in(LoopNode::EntryControl);
aoqi@0 273 Node *back_control = x->in(LoopNode::LoopBackControl);
aoqi@0 274 if (init_control == NULL || back_control == NULL) // Partially dead
aoqi@0 275 return false;
aoqi@0 276 // Must also check for TOP when looking for a dead loop
aoqi@0 277 if (init_control->is_top() || back_control->is_top())
aoqi@0 278 return false;
aoqi@0 279
aoqi@0 280 // Allow funny placement of Safepoint
aoqi@0 281 if (back_control->Opcode() == Op_SafePoint)
aoqi@0 282 back_control = back_control->in(TypeFunc::Control);
aoqi@0 283
aoqi@0 284 // Controlling test for loop
aoqi@0 285 Node *iftrue = back_control;
aoqi@0 286 uint iftrue_op = iftrue->Opcode();
aoqi@0 287 if (iftrue_op != Op_IfTrue &&
aoqi@0 288 iftrue_op != Op_IfFalse)
aoqi@0 289 // I have a weird back-control. Probably the loop-exit test is in
aoqi@0 290 // the middle of the loop and I am looking at some trailing control-flow
aoqi@0 291 // merge point. To fix this I would have to partially peel the loop.
aoqi@0 292 return false; // Obscure back-control
aoqi@0 293
aoqi@0 294 // Get boolean guarding loop-back test
aoqi@0 295 Node *iff = iftrue->in(0);
aoqi@0 296 if (get_loop(iff) != loop || !iff->in(1)->is_Bool())
aoqi@0 297 return false;
aoqi@0 298 BoolNode *test = iff->in(1)->as_Bool();
aoqi@0 299 BoolTest::mask bt = test->_test._test;
aoqi@0 300 float cl_prob = iff->as_If()->_prob;
aoqi@0 301 if (iftrue_op == Op_IfFalse) {
aoqi@0 302 bt = BoolTest(bt).negate();
aoqi@0 303 cl_prob = 1.0 - cl_prob;
aoqi@0 304 }
aoqi@0 305 // Get backedge compare
aoqi@0 306 Node *cmp = test->in(1);
aoqi@0 307 int cmp_op = cmp->Opcode();
aoqi@0 308 if (cmp_op != Op_CmpI)
aoqi@0 309 return false; // Avoid pointer & float compares
aoqi@0 310
aoqi@0 311 // Find the trip-counter increment & limit. Limit must be loop invariant.
aoqi@0 312 Node *incr = cmp->in(1);
aoqi@0 313 Node *limit = cmp->in(2);
aoqi@0 314
aoqi@0 315 // ---------
aoqi@0 316 // need 'loop()' test to tell if limit is loop invariant
aoqi@0 317 // ---------
aoqi@0 318
aoqi@0 319 if (!is_member(loop, get_ctrl(incr))) { // Swapped trip counter and limit?
aoqi@0 320 Node *tmp = incr; // Then reverse order into the CmpI
aoqi@0 321 incr = limit;
aoqi@0 322 limit = tmp;
aoqi@0 323 bt = BoolTest(bt).commute(); // And commute the exit test
aoqi@0 324 }
aoqi@0 325 if (is_member(loop, get_ctrl(limit))) // Limit must be loop-invariant
aoqi@0 326 return false;
aoqi@0 327 if (!is_member(loop, get_ctrl(incr))) // Trip counter must be loop-variant
aoqi@0 328 return false;
aoqi@0 329
aoqi@0 330 Node* phi_incr = NULL;
aoqi@0 331 // Trip-counter increment must be commutative & associative.
aoqi@0 332 if (incr->is_Phi()) {
aoqi@0 333 if (incr->as_Phi()->region() != x || incr->req() != 3)
aoqi@0 334 return false; // Not simple trip counter expression
aoqi@0 335 phi_incr = incr;
aoqi@0 336 incr = phi_incr->in(LoopNode::LoopBackControl); // Assume incr is on backedge of Phi
aoqi@0 337 if (!is_member(loop, get_ctrl(incr))) // Trip counter must be loop-variant
aoqi@0 338 return false;
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 Node* trunc1 = NULL;
aoqi@0 342 Node* trunc2 = NULL;
aoqi@0 343 const TypeInt* iv_trunc_t = NULL;
aoqi@0 344 if (!(incr = CountedLoopNode::match_incr_with_optional_truncation(incr, &trunc1, &trunc2, &iv_trunc_t))) {
aoqi@0 345 return false; // Funny increment opcode
aoqi@0 346 }
aoqi@0 347 assert(incr->Opcode() == Op_AddI, "wrong increment code");
aoqi@0 348
aoqi@0 349 // Get merge point
aoqi@0 350 Node *xphi = incr->in(1);
aoqi@0 351 Node *stride = incr->in(2);
aoqi@0 352 if (!stride->is_Con()) { // Oops, swap these
aoqi@0 353 if (!xphi->is_Con()) // Is the other guy a constant?
aoqi@0 354 return false; // Nope, unknown stride, bail out
aoqi@0 355 Node *tmp = xphi; // 'incr' is commutative, so ok to swap
aoqi@0 356 xphi = stride;
aoqi@0 357 stride = tmp;
aoqi@0 358 }
aoqi@0 359 // Stride must be constant
aoqi@0 360 int stride_con = stride->get_int();
aoqi@0 361 if (stride_con == 0)
aoqi@0 362 return false; // missed some peephole opt
aoqi@0 363
aoqi@0 364 if (!xphi->is_Phi())
aoqi@0 365 return false; // Too much math on the trip counter
aoqi@0 366 if (phi_incr != NULL && phi_incr != xphi)
aoqi@0 367 return false;
aoqi@0 368 PhiNode *phi = xphi->as_Phi();
aoqi@0 369
aoqi@0 370 // Phi must be of loop header; backedge must wrap to increment
aoqi@0 371 if (phi->region() != x)
aoqi@0 372 return false;
aoqi@0 373 if (trunc1 == NULL && phi->in(LoopNode::LoopBackControl) != incr ||
aoqi@0 374 trunc1 != NULL && phi->in(LoopNode::LoopBackControl) != trunc1) {
aoqi@0 375 return false;
aoqi@0 376 }
aoqi@0 377 Node *init_trip = phi->in(LoopNode::EntryControl);
aoqi@0 378
aoqi@0 379 // If iv trunc type is smaller than int, check for possible wrap.
aoqi@0 380 if (!TypeInt::INT->higher_equal(iv_trunc_t)) {
aoqi@0 381 assert(trunc1 != NULL, "must have found some truncation");
aoqi@0 382
aoqi@0 383 // Get a better type for the phi (filtered thru if's)
aoqi@0 384 const TypeInt* phi_ft = filtered_type(phi);
aoqi@0 385
aoqi@0 386 // Can iv take on a value that will wrap?
aoqi@0 387 //
aoqi@0 388 // Ensure iv's limit is not within "stride" of the wrap value.
aoqi@0 389 //
aoqi@0 390 // Example for "short" type
aoqi@0 391 // Truncation ensures value is in the range -32768..32767 (iv_trunc_t)
aoqi@0 392 // If the stride is +10, then the last value of the induction
aoqi@0 393 // variable before the increment (phi_ft->_hi) must be
aoqi@0 394 // <= 32767 - 10 and (phi_ft->_lo) must be >= -32768 to
aoqi@0 395 // ensure no truncation occurs after the increment.
aoqi@0 396
aoqi@0 397 if (stride_con > 0) {
aoqi@0 398 if (iv_trunc_t->_hi - phi_ft->_hi < stride_con ||
aoqi@0 399 iv_trunc_t->_lo > phi_ft->_lo) {
aoqi@0 400 return false; // truncation may occur
aoqi@0 401 }
aoqi@0 402 } else if (stride_con < 0) {
aoqi@0 403 if (iv_trunc_t->_lo - phi_ft->_lo > stride_con ||
aoqi@0 404 iv_trunc_t->_hi < phi_ft->_hi) {
aoqi@0 405 return false; // truncation may occur
aoqi@0 406 }
aoqi@0 407 }
aoqi@0 408 // No possibility of wrap so truncation can be discarded
aoqi@0 409 // Promote iv type to Int
aoqi@0 410 } else {
aoqi@0 411 assert(trunc1 == NULL && trunc2 == NULL, "no truncation for int");
aoqi@0 412 }
aoqi@0 413
aoqi@0 414 // If the condition is inverted and we will be rolling
aoqi@0 415 // through MININT to MAXINT, then bail out.
aoqi@0 416 if (bt == BoolTest::eq || // Bail out, but this loop trips at most twice!
aoqi@0 417 // Odd stride
aoqi@0 418 bt == BoolTest::ne && stride_con != 1 && stride_con != -1 ||
aoqi@0 419 // Count down loop rolls through MAXINT
aoqi@0 420 (bt == BoolTest::le || bt == BoolTest::lt) && stride_con < 0 ||
aoqi@0 421 // Count up loop rolls through MININT
aoqi@0 422 (bt == BoolTest::ge || bt == BoolTest::gt) && stride_con > 0) {
aoqi@0 423 return false; // Bail out
aoqi@0 424 }
aoqi@0 425
aoqi@0 426 const TypeInt* init_t = gvn->type(init_trip)->is_int();
aoqi@0 427 const TypeInt* limit_t = gvn->type(limit)->is_int();
aoqi@0 428
aoqi@0 429 if (stride_con > 0) {
aoqi@0 430 jlong init_p = (jlong)init_t->_lo + stride_con;
aoqi@0 431 if (init_p > (jlong)max_jint || init_p > (jlong)limit_t->_hi)
aoqi@0 432 return false; // cyclic loop or this loop trips only once
aoqi@0 433 } else {
aoqi@0 434 jlong init_p = (jlong)init_t->_hi + stride_con;
aoqi@0 435 if (init_p < (jlong)min_jint || init_p < (jlong)limit_t->_lo)
aoqi@0 436 return false; // cyclic loop or this loop trips only once
aoqi@0 437 }
aoqi@0 438
iveresov@7590 439 if (phi_incr != NULL) {
iveresov@7590 440 // check if there is a possiblity of IV overflowing after the first increment
iveresov@7590 441 if (stride_con > 0) {
iveresov@7590 442 if (init_t->_hi > max_jint - stride_con) {
iveresov@7590 443 return false;
iveresov@7590 444 }
iveresov@7590 445 } else {
iveresov@7590 446 if (init_t->_lo < min_jint - stride_con) {
iveresov@7590 447 return false;
iveresov@7590 448 }
iveresov@7590 449 }
iveresov@7590 450 }
iveresov@7590 451
aoqi@0 452 // =================================================
aoqi@0 453 // ---- SUCCESS! Found A Trip-Counted Loop! -----
aoqi@0 454 //
aoqi@0 455 assert(x->Opcode() == Op_Loop, "regular loops only");
aoqi@0 456 C->print_method(PHASE_BEFORE_CLOOPS, 3);
aoqi@0 457
aoqi@0 458 Node *hook = new (C) Node(6);
aoqi@0 459
aoqi@0 460 if (LoopLimitCheck) {
aoqi@0 461
aoqi@0 462 // ===================================================
aoqi@0 463 // Generate loop limit check to avoid integer overflow
aoqi@0 464 // in cases like next (cyclic loops):
aoqi@0 465 //
aoqi@0 466 // for (i=0; i <= max_jint; i++) {}
aoqi@0 467 // for (i=0; i < max_jint; i+=2) {}
aoqi@0 468 //
aoqi@0 469 //
aoqi@0 470 // Limit check predicate depends on the loop test:
aoqi@0 471 //
aoqi@0 472 // for(;i != limit; i++) --> limit <= (max_jint)
aoqi@0 473 // for(;i < limit; i+=stride) --> limit <= (max_jint - stride + 1)
aoqi@0 474 // for(;i <= limit; i+=stride) --> limit <= (max_jint - stride )
aoqi@0 475 //
aoqi@0 476
aoqi@0 477 // Check if limit is excluded to do more precise int overflow check.
aoqi@0 478 bool incl_limit = (bt == BoolTest::le || bt == BoolTest::ge);
aoqi@0 479 int stride_m = stride_con - (incl_limit ? 0 : (stride_con > 0 ? 1 : -1));
aoqi@0 480
aoqi@0 481 // If compare points directly to the phi we need to adjust
aoqi@0 482 // the compare so that it points to the incr. Limit have
aoqi@0 483 // to be adjusted to keep trip count the same and the
aoqi@0 484 // adjusted limit should be checked for int overflow.
aoqi@0 485 if (phi_incr != NULL) {
aoqi@0 486 stride_m += stride_con;
aoqi@0 487 }
aoqi@0 488
aoqi@0 489 if (limit->is_Con()) {
aoqi@0 490 int limit_con = limit->get_int();
aoqi@0 491 if ((stride_con > 0 && limit_con > (max_jint - stride_m)) ||
aoqi@0 492 (stride_con < 0 && limit_con < (min_jint - stride_m))) {
aoqi@0 493 // Bailout: it could be integer overflow.
aoqi@0 494 return false;
aoqi@0 495 }
aoqi@0 496 } else if ((stride_con > 0 && limit_t->_hi <= (max_jint - stride_m)) ||
aoqi@0 497 (stride_con < 0 && limit_t->_lo >= (min_jint - stride_m))) {
aoqi@0 498 // Limit's type may satisfy the condition, for example,
aoqi@0 499 // when it is an array length.
aoqi@0 500 } else {
aoqi@0 501 // Generate loop's limit check.
aoqi@0 502 // Loop limit check predicate should be near the loop.
aoqi@0 503 ProjNode *limit_check_proj = find_predicate_insertion_point(init_control, Deoptimization::Reason_loop_limit_check);
aoqi@0 504 if (!limit_check_proj) {
aoqi@0 505 // The limit check predicate is not generated if this method trapped here before.
aoqi@0 506 #ifdef ASSERT
aoqi@0 507 if (TraceLoopLimitCheck) {
aoqi@0 508 tty->print("missing loop limit check:");
aoqi@0 509 loop->dump_head();
aoqi@0 510 x->dump(1);
aoqi@0 511 }
aoqi@0 512 #endif
aoqi@0 513 return false;
aoqi@0 514 }
aoqi@0 515
aoqi@0 516 IfNode* check_iff = limit_check_proj->in(0)->as_If();
aoqi@0 517 Node* cmp_limit;
aoqi@0 518 Node* bol;
aoqi@0 519
aoqi@0 520 if (stride_con > 0) {
aoqi@0 521 cmp_limit = new (C) CmpINode(limit, _igvn.intcon(max_jint - stride_m));
aoqi@0 522 bol = new (C) BoolNode(cmp_limit, BoolTest::le);
aoqi@0 523 } else {
aoqi@0 524 cmp_limit = new (C) CmpINode(limit, _igvn.intcon(min_jint - stride_m));
aoqi@0 525 bol = new (C) BoolNode(cmp_limit, BoolTest::ge);
aoqi@0 526 }
aoqi@0 527 cmp_limit = _igvn.register_new_node_with_optimizer(cmp_limit);
aoqi@0 528 bol = _igvn.register_new_node_with_optimizer(bol);
aoqi@0 529 set_subtree_ctrl(bol);
aoqi@0 530
aoqi@0 531 // Replace condition in original predicate but preserve Opaque node
aoqi@0 532 // so that previous predicates could be found.
aoqi@0 533 assert(check_iff->in(1)->Opcode() == Op_Conv2B &&
aoqi@0 534 check_iff->in(1)->in(1)->Opcode() == Op_Opaque1, "");
aoqi@0 535 Node* opq = check_iff->in(1)->in(1);
aoqi@0 536 _igvn.hash_delete(opq);
aoqi@0 537 opq->set_req(1, bol);
aoqi@0 538 // Update ctrl.
aoqi@0 539 set_ctrl(opq, check_iff->in(0));
aoqi@0 540 set_ctrl(check_iff->in(1), check_iff->in(0));
aoqi@0 541
aoqi@0 542 #ifndef PRODUCT
aoqi@0 543 // report that the loop predication has been actually performed
aoqi@0 544 // for this loop
aoqi@0 545 if (TraceLoopLimitCheck) {
aoqi@0 546 tty->print_cr("Counted Loop Limit Check generated:");
aoqi@0 547 debug_only( bol->dump(2); )
aoqi@0 548 }
aoqi@0 549 #endif
aoqi@0 550 }
aoqi@0 551
aoqi@0 552 if (phi_incr != NULL) {
aoqi@0 553 // If compare points directly to the phi we need to adjust
aoqi@0 554 // the compare so that it points to the incr. Limit have
aoqi@0 555 // to be adjusted to keep trip count the same and we
aoqi@0 556 // should avoid int overflow.
aoqi@0 557 //
aoqi@0 558 // i = init; do {} while(i++ < limit);
aoqi@0 559 // is converted to
aoqi@0 560 // i = init; do {} while(++i < limit+1);
aoqi@0 561 //
aoqi@0 562 limit = gvn->transform(new (C) AddINode(limit, stride));
aoqi@0 563 }
aoqi@0 564
aoqi@0 565 // Now we need to canonicalize loop condition.
aoqi@0 566 if (bt == BoolTest::ne) {
aoqi@0 567 assert(stride_con == 1 || stride_con == -1, "simple increment only");
aoqi@0 568 // 'ne' can be replaced with 'lt' only when init < limit.
aoqi@0 569 if (stride_con > 0 && init_t->_hi < limit_t->_lo)
aoqi@0 570 bt = BoolTest::lt;
aoqi@0 571 // 'ne' can be replaced with 'gt' only when init > limit.
aoqi@0 572 if (stride_con < 0 && init_t->_lo > limit_t->_hi)
aoqi@0 573 bt = BoolTest::gt;
aoqi@0 574 }
aoqi@0 575
aoqi@0 576 if (incl_limit) {
aoqi@0 577 // The limit check guaranties that 'limit <= (max_jint - stride)' so
aoqi@0 578 // we can convert 'i <= limit' to 'i < limit+1' since stride != 0.
aoqi@0 579 //
aoqi@0 580 Node* one = (stride_con > 0) ? gvn->intcon( 1) : gvn->intcon(-1);
aoqi@0 581 limit = gvn->transform(new (C) AddINode(limit, one));
aoqi@0 582 if (bt == BoolTest::le)
aoqi@0 583 bt = BoolTest::lt;
aoqi@0 584 else if (bt == BoolTest::ge)
aoqi@0 585 bt = BoolTest::gt;
aoqi@0 586 else
aoqi@0 587 ShouldNotReachHere();
aoqi@0 588 }
aoqi@0 589 set_subtree_ctrl( limit );
aoqi@0 590
aoqi@0 591 } else { // LoopLimitCheck
aoqi@0 592
aoqi@0 593 // If compare points to incr, we are ok. Otherwise the compare
aoqi@0 594 // can directly point to the phi; in this case adjust the compare so that
aoqi@0 595 // it points to the incr by adjusting the limit.
aoqi@0 596 if (cmp->in(1) == phi || cmp->in(2) == phi)
aoqi@0 597 limit = gvn->transform(new (C) AddINode(limit,stride));
aoqi@0 598
aoqi@0 599 // trip-count for +-tive stride should be: (limit - init_trip + stride - 1)/stride.
aoqi@0 600 // Final value for iterator should be: trip_count * stride + init_trip.
aoqi@0 601 Node *one_p = gvn->intcon( 1);
aoqi@0 602 Node *one_m = gvn->intcon(-1);
aoqi@0 603
aoqi@0 604 Node *trip_count = NULL;
aoqi@0 605 switch( bt ) {
aoqi@0 606 case BoolTest::eq:
aoqi@0 607 ShouldNotReachHere();
aoqi@0 608 case BoolTest::ne: // Ahh, the case we desire
aoqi@0 609 if (stride_con == 1)
aoqi@0 610 trip_count = gvn->transform(new (C) SubINode(limit,init_trip));
aoqi@0 611 else if (stride_con == -1)
aoqi@0 612 trip_count = gvn->transform(new (C) SubINode(init_trip,limit));
aoqi@0 613 else
aoqi@0 614 ShouldNotReachHere();
aoqi@0 615 set_subtree_ctrl(trip_count);
aoqi@0 616 //_loop.map(trip_count->_idx,loop(limit));
aoqi@0 617 break;
aoqi@0 618 case BoolTest::le: // Maybe convert to '<' case
aoqi@0 619 limit = gvn->transform(new (C) AddINode(limit,one_p));
aoqi@0 620 set_subtree_ctrl( limit );
aoqi@0 621 hook->init_req(4, limit);
aoqi@0 622
aoqi@0 623 bt = BoolTest::lt;
aoqi@0 624 // Make the new limit be in the same loop nest as the old limit
aoqi@0 625 //_loop.map(limit->_idx,limit_loop);
aoqi@0 626 // Fall into next case
aoqi@0 627 case BoolTest::lt: { // Maybe convert to '!=' case
aoqi@0 628 if (stride_con < 0) // Count down loop rolls through MAXINT
aoqi@0 629 ShouldNotReachHere();
aoqi@0 630 Node *range = gvn->transform(new (C) SubINode(limit,init_trip));
aoqi@0 631 set_subtree_ctrl( range );
aoqi@0 632 hook->init_req(0, range);
aoqi@0 633
aoqi@0 634 Node *bias = gvn->transform(new (C) AddINode(range,stride));
aoqi@0 635 set_subtree_ctrl( bias );
aoqi@0 636 hook->init_req(1, bias);
aoqi@0 637
aoqi@0 638 Node *bias1 = gvn->transform(new (C) AddINode(bias,one_m));
aoqi@0 639 set_subtree_ctrl( bias1 );
aoqi@0 640 hook->init_req(2, bias1);
aoqi@0 641
aoqi@0 642 trip_count = gvn->transform(new (C) DivINode(0,bias1,stride));
aoqi@0 643 set_subtree_ctrl( trip_count );
aoqi@0 644 hook->init_req(3, trip_count);
aoqi@0 645 break;
aoqi@0 646 }
aoqi@0 647
aoqi@0 648 case BoolTest::ge: // Maybe convert to '>' case
aoqi@0 649 limit = gvn->transform(new (C) AddINode(limit,one_m));
aoqi@0 650 set_subtree_ctrl( limit );
aoqi@0 651 hook->init_req(4 ,limit);
aoqi@0 652
aoqi@0 653 bt = BoolTest::gt;
aoqi@0 654 // Make the new limit be in the same loop nest as the old limit
aoqi@0 655 //_loop.map(limit->_idx,limit_loop);
aoqi@0 656 // Fall into next case
aoqi@0 657 case BoolTest::gt: { // Maybe convert to '!=' case
aoqi@0 658 if (stride_con > 0) // count up loop rolls through MININT
aoqi@0 659 ShouldNotReachHere();
aoqi@0 660 Node *range = gvn->transform(new (C) SubINode(limit,init_trip));
aoqi@0 661 set_subtree_ctrl( range );
aoqi@0 662 hook->init_req(0, range);
aoqi@0 663
aoqi@0 664 Node *bias = gvn->transform(new (C) AddINode(range,stride));
aoqi@0 665 set_subtree_ctrl( bias );
aoqi@0 666 hook->init_req(1, bias);
aoqi@0 667
aoqi@0 668 Node *bias1 = gvn->transform(new (C) AddINode(bias,one_p));
aoqi@0 669 set_subtree_ctrl( bias1 );
aoqi@0 670 hook->init_req(2, bias1);
aoqi@0 671
aoqi@0 672 trip_count = gvn->transform(new (C) DivINode(0,bias1,stride));
aoqi@0 673 set_subtree_ctrl( trip_count );
aoqi@0 674 hook->init_req(3, trip_count);
aoqi@0 675 break;
aoqi@0 676 }
aoqi@0 677 } // switch( bt )
aoqi@0 678
aoqi@0 679 Node *span = gvn->transform(new (C) MulINode(trip_count,stride));
aoqi@0 680 set_subtree_ctrl( span );
aoqi@0 681 hook->init_req(5, span);
aoqi@0 682
aoqi@0 683 limit = gvn->transform(new (C) AddINode(span,init_trip));
aoqi@0 684 set_subtree_ctrl( limit );
aoqi@0 685
aoqi@0 686 } // LoopLimitCheck
aoqi@0 687
aoqi@0 688 // Check for SafePoint on backedge and remove
aoqi@0 689 Node *sfpt = x->in(LoopNode::LoopBackControl);
aoqi@0 690 if (sfpt->Opcode() == Op_SafePoint && is_deleteable_safept(sfpt)) {
aoqi@0 691 lazy_replace( sfpt, iftrue );
aoqi@0 692 if (loop->_safepts != NULL) {
aoqi@0 693 loop->_safepts->yank(sfpt);
aoqi@0 694 }
aoqi@0 695 loop->_tail = iftrue;
aoqi@0 696 }
aoqi@0 697
aoqi@0 698 // Build a canonical trip test.
aoqi@0 699 // Clone code, as old values may be in use.
aoqi@0 700 incr = incr->clone();
aoqi@0 701 incr->set_req(1,phi);
aoqi@0 702 incr->set_req(2,stride);
aoqi@0 703 incr = _igvn.register_new_node_with_optimizer(incr);
aoqi@0 704 set_early_ctrl( incr );
aoqi@0 705 _igvn.hash_delete(phi);
aoqi@0 706 phi->set_req_X( LoopNode::LoopBackControl, incr, &_igvn );
aoqi@0 707
aoqi@0 708 // If phi type is more restrictive than Int, raise to
aoqi@0 709 // Int to prevent (almost) infinite recursion in igvn
aoqi@0 710 // which can only handle integer types for constants or minint..maxint.
aoqi@0 711 if (!TypeInt::INT->higher_equal(phi->bottom_type())) {
aoqi@0 712 Node* nphi = PhiNode::make(phi->in(0), phi->in(LoopNode::EntryControl), TypeInt::INT);
aoqi@0 713 nphi->set_req(LoopNode::LoopBackControl, phi->in(LoopNode::LoopBackControl));
aoqi@0 714 nphi = _igvn.register_new_node_with_optimizer(nphi);
aoqi@0 715 set_ctrl(nphi, get_ctrl(phi));
aoqi@0 716 _igvn.replace_node(phi, nphi);
aoqi@0 717 phi = nphi->as_Phi();
aoqi@0 718 }
aoqi@0 719 cmp = cmp->clone();
aoqi@0 720 cmp->set_req(1,incr);
aoqi@0 721 cmp->set_req(2,limit);
aoqi@0 722 cmp = _igvn.register_new_node_with_optimizer(cmp);
aoqi@0 723 set_ctrl(cmp, iff->in(0));
aoqi@0 724
aoqi@0 725 test = test->clone()->as_Bool();
aoqi@0 726 (*(BoolTest*)&test->_test)._test = bt;
aoqi@0 727 test->set_req(1,cmp);
aoqi@0 728 _igvn.register_new_node_with_optimizer(test);
aoqi@0 729 set_ctrl(test, iff->in(0));
aoqi@0 730
aoqi@0 731 // Replace the old IfNode with a new LoopEndNode
aoqi@0 732 Node *lex = _igvn.register_new_node_with_optimizer(new (C) CountedLoopEndNode( iff->in(0), test, cl_prob, iff->as_If()->_fcnt ));
aoqi@0 733 IfNode *le = lex->as_If();
aoqi@0 734 uint dd = dom_depth(iff);
aoqi@0 735 set_idom(le, le->in(0), dd); // Update dominance for loop exit
aoqi@0 736 set_loop(le, loop);
aoqi@0 737
aoqi@0 738 // Get the loop-exit control
aoqi@0 739 Node *iffalse = iff->as_If()->proj_out(!(iftrue_op == Op_IfTrue));
aoqi@0 740
aoqi@0 741 // Need to swap loop-exit and loop-back control?
aoqi@0 742 if (iftrue_op == Op_IfFalse) {
aoqi@0 743 Node *ift2=_igvn.register_new_node_with_optimizer(new (C) IfTrueNode (le));
aoqi@0 744 Node *iff2=_igvn.register_new_node_with_optimizer(new (C) IfFalseNode(le));
aoqi@0 745
aoqi@0 746 loop->_tail = back_control = ift2;
aoqi@0 747 set_loop(ift2, loop);
aoqi@0 748 set_loop(iff2, get_loop(iffalse));
aoqi@0 749
aoqi@0 750 // Lazy update of 'get_ctrl' mechanism.
aoqi@0 751 lazy_replace_proj( iffalse, iff2 );
aoqi@0 752 lazy_replace_proj( iftrue, ift2 );
aoqi@0 753
aoqi@0 754 // Swap names
aoqi@0 755 iffalse = iff2;
aoqi@0 756 iftrue = ift2;
aoqi@0 757 } else {
aoqi@0 758 _igvn.hash_delete(iffalse);
aoqi@0 759 _igvn.hash_delete(iftrue);
aoqi@0 760 iffalse->set_req_X( 0, le, &_igvn );
aoqi@0 761 iftrue ->set_req_X( 0, le, &_igvn );
aoqi@0 762 }
aoqi@0 763
aoqi@0 764 set_idom(iftrue, le, dd+1);
aoqi@0 765 set_idom(iffalse, le, dd+1);
aoqi@0 766 assert(iff->outcnt() == 0, "should be dead now");
aoqi@0 767 lazy_replace( iff, le ); // fix 'get_ctrl'
aoqi@0 768
aoqi@0 769 // Now setup a new CountedLoopNode to replace the existing LoopNode
aoqi@0 770 CountedLoopNode *l = new (C) CountedLoopNode(init_control, back_control);
aoqi@0 771 l->set_unswitch_count(x->as_Loop()->unswitch_count()); // Preserve
aoqi@0 772 // The following assert is approximately true, and defines the intention
aoqi@0 773 // of can_be_counted_loop. It fails, however, because phase->type
aoqi@0 774 // is not yet initialized for this loop and its parts.
aoqi@0 775 //assert(l->can_be_counted_loop(this), "sanity");
aoqi@0 776 _igvn.register_new_node_with_optimizer(l);
aoqi@0 777 set_loop(l, loop);
aoqi@0 778 loop->_head = l;
aoqi@0 779 // Fix all data nodes placed at the old loop head.
aoqi@0 780 // Uses the lazy-update mechanism of 'get_ctrl'.
aoqi@0 781 lazy_replace( x, l );
aoqi@0 782 set_idom(l, init_control, dom_depth(x));
aoqi@0 783
aoqi@0 784 // Check for immediately preceding SafePoint and remove
aoqi@0 785 Node *sfpt2 = le->in(0);
aoqi@0 786 if (sfpt2->Opcode() == Op_SafePoint && is_deleteable_safept(sfpt2)) {
aoqi@0 787 lazy_replace( sfpt2, sfpt2->in(TypeFunc::Control));
aoqi@0 788 if (loop->_safepts != NULL) {
aoqi@0 789 loop->_safepts->yank(sfpt2);
aoqi@0 790 }
aoqi@0 791 }
aoqi@0 792
aoqi@0 793 // Free up intermediate goo
aoqi@0 794 _igvn.remove_dead_node(hook);
aoqi@0 795
aoqi@0 796 #ifdef ASSERT
aoqi@0 797 assert(l->is_valid_counted_loop(), "counted loop shape is messed up");
aoqi@0 798 assert(l == loop->_head && l->phi() == phi && l->loopexit() == lex, "" );
aoqi@0 799 #endif
aoqi@0 800 #ifndef PRODUCT
aoqi@0 801 if (TraceLoopOpts) {
aoqi@0 802 tty->print("Counted ");
aoqi@0 803 loop->dump_head();
aoqi@0 804 }
aoqi@0 805 #endif
aoqi@0 806
aoqi@0 807 C->print_method(PHASE_AFTER_CLOOPS, 3);
aoqi@0 808
aoqi@0 809 return true;
aoqi@0 810 }
aoqi@0 811
aoqi@0 812 //----------------------exact_limit-------------------------------------------
aoqi@0 813 Node* PhaseIdealLoop::exact_limit( IdealLoopTree *loop ) {
aoqi@0 814 assert(loop->_head->is_CountedLoop(), "");
aoqi@0 815 CountedLoopNode *cl = loop->_head->as_CountedLoop();
aoqi@0 816 assert(cl->is_valid_counted_loop(), "");
aoqi@0 817
aoqi@0 818 if (!LoopLimitCheck || ABS(cl->stride_con()) == 1 ||
aoqi@0 819 cl->limit()->Opcode() == Op_LoopLimit) {
aoqi@0 820 // Old code has exact limit (it could be incorrect in case of int overflow).
aoqi@0 821 // Loop limit is exact with stride == 1. And loop may already have exact limit.
aoqi@0 822 return cl->limit();
aoqi@0 823 }
aoqi@0 824 Node *limit = NULL;
aoqi@0 825 #ifdef ASSERT
aoqi@0 826 BoolTest::mask bt = cl->loopexit()->test_trip();
aoqi@0 827 assert(bt == BoolTest::lt || bt == BoolTest::gt, "canonical test is expected");
aoqi@0 828 #endif
aoqi@0 829 if (cl->has_exact_trip_count()) {
aoqi@0 830 // Simple case: loop has constant boundaries.
aoqi@0 831 // Use jlongs to avoid integer overflow.
aoqi@0 832 int stride_con = cl->stride_con();
aoqi@0 833 jlong init_con = cl->init_trip()->get_int();
aoqi@0 834 jlong limit_con = cl->limit()->get_int();
aoqi@0 835 julong trip_cnt = cl->trip_count();
aoqi@0 836 jlong final_con = init_con + trip_cnt*stride_con;
aoqi@0 837 int final_int = (int)final_con;
aoqi@0 838 // The final value should be in integer range since the loop
aoqi@0 839 // is counted and the limit was checked for overflow.
aoqi@0 840 assert(final_con == (jlong)final_int, "final value should be integer");
aoqi@0 841 limit = _igvn.intcon(final_int);
aoqi@0 842 } else {
aoqi@0 843 // Create new LoopLimit node to get exact limit (final iv value).
aoqi@0 844 limit = new (C) LoopLimitNode(C, cl->init_trip(), cl->limit(), cl->stride());
aoqi@0 845 register_new_node(limit, cl->in(LoopNode::EntryControl));
aoqi@0 846 }
aoqi@0 847 assert(limit != NULL, "sanity");
aoqi@0 848 return limit;
aoqi@0 849 }
aoqi@0 850
aoqi@0 851 //------------------------------Ideal------------------------------------------
aoqi@0 852 // Return a node which is more "ideal" than the current node.
aoqi@0 853 // Attempt to convert into a counted-loop.
aoqi@0 854 Node *LoopNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 855 if (!can_be_counted_loop(phase)) {
aoqi@0 856 phase->C->set_major_progress();
aoqi@0 857 }
aoqi@0 858 return RegionNode::Ideal(phase, can_reshape);
aoqi@0 859 }
aoqi@0 860
aoqi@0 861
aoqi@0 862 //=============================================================================
aoqi@0 863 //------------------------------Ideal------------------------------------------
aoqi@0 864 // Return a node which is more "ideal" than the current node.
aoqi@0 865 // Attempt to convert into a counted-loop.
aoqi@0 866 Node *CountedLoopNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 867 return RegionNode::Ideal(phase, can_reshape);
aoqi@0 868 }
aoqi@0 869
aoqi@0 870 //------------------------------dump_spec--------------------------------------
aoqi@0 871 // Dump special per-node info
aoqi@0 872 #ifndef PRODUCT
aoqi@0 873 void CountedLoopNode::dump_spec(outputStream *st) const {
aoqi@0 874 LoopNode::dump_spec(st);
aoqi@0 875 if (stride_is_con()) {
aoqi@0 876 st->print("stride: %d ",stride_con());
aoqi@0 877 }
aoqi@0 878 if (is_pre_loop ()) st->print("pre of N%d" , _main_idx);
aoqi@0 879 if (is_main_loop()) st->print("main of N%d", _idx);
aoqi@0 880 if (is_post_loop()) st->print("post of N%d", _main_idx);
aoqi@0 881 }
aoqi@0 882 #endif
aoqi@0 883
aoqi@0 884 //=============================================================================
aoqi@0 885 int CountedLoopEndNode::stride_con() const {
aoqi@0 886 return stride()->bottom_type()->is_int()->get_con();
aoqi@0 887 }
aoqi@0 888
aoqi@0 889 //=============================================================================
aoqi@0 890 //------------------------------Value-----------------------------------------
aoqi@0 891 const Type *LoopLimitNode::Value( PhaseTransform *phase ) const {
aoqi@0 892 const Type* init_t = phase->type(in(Init));
aoqi@0 893 const Type* limit_t = phase->type(in(Limit));
aoqi@0 894 const Type* stride_t = phase->type(in(Stride));
aoqi@0 895 // Either input is TOP ==> the result is TOP
aoqi@0 896 if (init_t == Type::TOP) return Type::TOP;
aoqi@0 897 if (limit_t == Type::TOP) return Type::TOP;
aoqi@0 898 if (stride_t == Type::TOP) return Type::TOP;
aoqi@0 899
aoqi@0 900 int stride_con = stride_t->is_int()->get_con();
aoqi@0 901 if (stride_con == 1)
aoqi@0 902 return NULL; // Identity
aoqi@0 903
aoqi@0 904 if (init_t->is_int()->is_con() && limit_t->is_int()->is_con()) {
aoqi@0 905 // Use jlongs to avoid integer overflow.
aoqi@0 906 jlong init_con = init_t->is_int()->get_con();
aoqi@0 907 jlong limit_con = limit_t->is_int()->get_con();
aoqi@0 908 int stride_m = stride_con - (stride_con > 0 ? 1 : -1);
aoqi@0 909 jlong trip_count = (limit_con - init_con + stride_m)/stride_con;
aoqi@0 910 jlong final_con = init_con + stride_con*trip_count;
aoqi@0 911 int final_int = (int)final_con;
aoqi@0 912 // The final value should be in integer range since the loop
aoqi@0 913 // is counted and the limit was checked for overflow.
aoqi@0 914 assert(final_con == (jlong)final_int, "final value should be integer");
aoqi@0 915 return TypeInt::make(final_int);
aoqi@0 916 }
aoqi@0 917
aoqi@0 918 return bottom_type(); // TypeInt::INT
aoqi@0 919 }
aoqi@0 920
aoqi@0 921 //------------------------------Ideal------------------------------------------
aoqi@0 922 // Return a node which is more "ideal" than the current node.
aoqi@0 923 Node *LoopLimitNode::Ideal(PhaseGVN *phase, bool can_reshape) {
aoqi@0 924 if (phase->type(in(Init)) == Type::TOP ||
aoqi@0 925 phase->type(in(Limit)) == Type::TOP ||
aoqi@0 926 phase->type(in(Stride)) == Type::TOP)
aoqi@0 927 return NULL; // Dead
aoqi@0 928
aoqi@0 929 int stride_con = phase->type(in(Stride))->is_int()->get_con();
aoqi@0 930 if (stride_con == 1)
aoqi@0 931 return NULL; // Identity
aoqi@0 932
aoqi@0 933 if (in(Init)->is_Con() && in(Limit)->is_Con())
aoqi@0 934 return NULL; // Value
aoqi@0 935
aoqi@0 936 // Delay following optimizations until all loop optimizations
aoqi@0 937 // done to keep Ideal graph simple.
aoqi@0 938 if (!can_reshape || phase->C->major_progress())
aoqi@0 939 return NULL;
aoqi@0 940
aoqi@0 941 const TypeInt* init_t = phase->type(in(Init) )->is_int();
aoqi@0 942 const TypeInt* limit_t = phase->type(in(Limit))->is_int();
aoqi@0 943 int stride_p;
aoqi@0 944 jlong lim, ini;
aoqi@0 945 julong max;
aoqi@0 946 if (stride_con > 0) {
aoqi@0 947 stride_p = stride_con;
aoqi@0 948 lim = limit_t->_hi;
aoqi@0 949 ini = init_t->_lo;
aoqi@0 950 max = (julong)max_jint;
aoqi@0 951 } else {
aoqi@0 952 stride_p = -stride_con;
aoqi@0 953 lim = init_t->_hi;
aoqi@0 954 ini = limit_t->_lo;
aoqi@0 955 max = (julong)min_jint;
aoqi@0 956 }
aoqi@0 957 julong range = lim - ini + stride_p;
aoqi@0 958 if (range <= max) {
aoqi@0 959 // Convert to integer expression if it is not overflow.
aoqi@0 960 Node* stride_m = phase->intcon(stride_con - (stride_con > 0 ? 1 : -1));
aoqi@0 961 Node *range = phase->transform(new (phase->C) SubINode(in(Limit), in(Init)));
aoqi@0 962 Node *bias = phase->transform(new (phase->C) AddINode(range, stride_m));
aoqi@0 963 Node *trip = phase->transform(new (phase->C) DivINode(0, bias, in(Stride)));
aoqi@0 964 Node *span = phase->transform(new (phase->C) MulINode(trip, in(Stride)));
aoqi@0 965 return new (phase->C) AddINode(span, in(Init)); // exact limit
aoqi@0 966 }
aoqi@0 967
aoqi@0 968 if (is_power_of_2(stride_p) || // divisor is 2^n
aoqi@0 969 !Matcher::has_match_rule(Op_LoopLimit)) { // or no specialized Mach node?
aoqi@0 970 // Convert to long expression to avoid integer overflow
aoqi@0 971 // and let igvn optimizer convert this division.
aoqi@0 972 //
aoqi@0 973 Node* init = phase->transform( new (phase->C) ConvI2LNode(in(Init)));
aoqi@0 974 Node* limit = phase->transform( new (phase->C) ConvI2LNode(in(Limit)));
aoqi@0 975 Node* stride = phase->longcon(stride_con);
aoqi@0 976 Node* stride_m = phase->longcon(stride_con - (stride_con > 0 ? 1 : -1));
aoqi@0 977
aoqi@0 978 Node *range = phase->transform(new (phase->C) SubLNode(limit, init));
aoqi@0 979 Node *bias = phase->transform(new (phase->C) AddLNode(range, stride_m));
aoqi@0 980 Node *span;
aoqi@0 981 if (stride_con > 0 && is_power_of_2(stride_p)) {
aoqi@0 982 // bias >= 0 if stride >0, so if stride is 2^n we can use &(-stride)
aoqi@0 983 // and avoid generating rounding for division. Zero trip guard should
aoqi@0 984 // guarantee that init < limit but sometimes the guard is missing and
aoqi@0 985 // we can get situation when init > limit. Note, for the empty loop
aoqi@0 986 // optimization zero trip guard is generated explicitly which leaves
aoqi@0 987 // only RCE predicate where exact limit is used and the predicate
aoqi@0 988 // will simply fail forcing recompilation.
aoqi@0 989 Node* neg_stride = phase->longcon(-stride_con);
aoqi@0 990 span = phase->transform(new (phase->C) AndLNode(bias, neg_stride));
aoqi@0 991 } else {
aoqi@0 992 Node *trip = phase->transform(new (phase->C) DivLNode(0, bias, stride));
aoqi@0 993 span = phase->transform(new (phase->C) MulLNode(trip, stride));
aoqi@0 994 }
aoqi@0 995 // Convert back to int
aoqi@0 996 Node *span_int = phase->transform(new (phase->C) ConvL2INode(span));
aoqi@0 997 return new (phase->C) AddINode(span_int, in(Init)); // exact limit
aoqi@0 998 }
aoqi@0 999
aoqi@0 1000 return NULL; // No progress
aoqi@0 1001 }
aoqi@0 1002
aoqi@0 1003 //------------------------------Identity---------------------------------------
aoqi@0 1004 // If stride == 1 return limit node.
aoqi@0 1005 Node *LoopLimitNode::Identity( PhaseTransform *phase ) {
aoqi@0 1006 int stride_con = phase->type(in(Stride))->is_int()->get_con();
aoqi@0 1007 if (stride_con == 1 || stride_con == -1)
aoqi@0 1008 return in(Limit);
aoqi@0 1009 return this;
aoqi@0 1010 }
aoqi@0 1011
aoqi@0 1012 //=============================================================================
aoqi@0 1013 //----------------------match_incr_with_optional_truncation--------------------
aoqi@0 1014 // Match increment with optional truncation:
aoqi@0 1015 // CHAR: (i+1)&0x7fff, BYTE: ((i+1)<<8)>>8, or SHORT: ((i+1)<<16)>>16
aoqi@0 1016 // Return NULL for failure. Success returns the increment node.
aoqi@0 1017 Node* CountedLoopNode::match_incr_with_optional_truncation(
aoqi@0 1018 Node* expr, Node** trunc1, Node** trunc2, const TypeInt** trunc_type) {
aoqi@0 1019 // Quick cutouts:
aoqi@0 1020 if (expr == NULL || expr->req() != 3) return NULL;
aoqi@0 1021
aoqi@0 1022 Node *t1 = NULL;
aoqi@0 1023 Node *t2 = NULL;
aoqi@0 1024 const TypeInt* trunc_t = TypeInt::INT;
aoqi@0 1025 Node* n1 = expr;
aoqi@0 1026 int n1op = n1->Opcode();
aoqi@0 1027
aoqi@0 1028 // Try to strip (n1 & M) or (n1 << N >> N) from n1.
aoqi@0 1029 if (n1op == Op_AndI &&
aoqi@0 1030 n1->in(2)->is_Con() &&
aoqi@0 1031 n1->in(2)->bottom_type()->is_int()->get_con() == 0x7fff) {
aoqi@0 1032 // %%% This check should match any mask of 2**K-1.
aoqi@0 1033 t1 = n1;
aoqi@0 1034 n1 = t1->in(1);
aoqi@0 1035 n1op = n1->Opcode();
aoqi@0 1036 trunc_t = TypeInt::CHAR;
aoqi@0 1037 } else if (n1op == Op_RShiftI &&
aoqi@0 1038 n1->in(1) != NULL &&
aoqi@0 1039 n1->in(1)->Opcode() == Op_LShiftI &&
aoqi@0 1040 n1->in(2) == n1->in(1)->in(2) &&
aoqi@0 1041 n1->in(2)->is_Con()) {
aoqi@0 1042 jint shift = n1->in(2)->bottom_type()->is_int()->get_con();
aoqi@0 1043 // %%% This check should match any shift in [1..31].
aoqi@0 1044 if (shift == 16 || shift == 8) {
aoqi@0 1045 t1 = n1;
aoqi@0 1046 t2 = t1->in(1);
aoqi@0 1047 n1 = t2->in(1);
aoqi@0 1048 n1op = n1->Opcode();
aoqi@0 1049 if (shift == 16) {
aoqi@0 1050 trunc_t = TypeInt::SHORT;
aoqi@0 1051 } else if (shift == 8) {
aoqi@0 1052 trunc_t = TypeInt::BYTE;
aoqi@0 1053 }
aoqi@0 1054 }
aoqi@0 1055 }
aoqi@0 1056
aoqi@0 1057 // If (maybe after stripping) it is an AddI, we won:
aoqi@0 1058 if (n1op == Op_AddI) {
aoqi@0 1059 *trunc1 = t1;
aoqi@0 1060 *trunc2 = t2;
aoqi@0 1061 *trunc_type = trunc_t;
aoqi@0 1062 return n1;
aoqi@0 1063 }
aoqi@0 1064
aoqi@0 1065 // failed
aoqi@0 1066 return NULL;
aoqi@0 1067 }
aoqi@0 1068
aoqi@0 1069
aoqi@0 1070 //------------------------------filtered_type--------------------------------
aoqi@0 1071 // Return a type based on condition control flow
aoqi@0 1072 // A successful return will be a type that is restricted due
aoqi@0 1073 // to a series of dominating if-tests, such as:
aoqi@0 1074 // if (i < 10) {
aoqi@0 1075 // if (i > 0) {
aoqi@0 1076 // here: "i" type is [1..10)
aoqi@0 1077 // }
aoqi@0 1078 // }
aoqi@0 1079 // or a control flow merge
aoqi@0 1080 // if (i < 10) {
aoqi@0 1081 // do {
aoqi@0 1082 // phi( , ) -- at top of loop type is [min_int..10)
aoqi@0 1083 // i = ?
aoqi@0 1084 // } while ( i < 10)
aoqi@0 1085 //
aoqi@0 1086 const TypeInt* PhaseIdealLoop::filtered_type( Node *n, Node* n_ctrl) {
aoqi@0 1087 assert(n && n->bottom_type()->is_int(), "must be int");
aoqi@0 1088 const TypeInt* filtered_t = NULL;
aoqi@0 1089 if (!n->is_Phi()) {
aoqi@0 1090 assert(n_ctrl != NULL || n_ctrl == C->top(), "valid control");
aoqi@0 1091 filtered_t = filtered_type_from_dominators(n, n_ctrl);
aoqi@0 1092
aoqi@0 1093 } else {
aoqi@0 1094 Node* phi = n->as_Phi();
aoqi@0 1095 Node* region = phi->in(0);
aoqi@0 1096 assert(n_ctrl == NULL || n_ctrl == region, "ctrl parameter must be region");
aoqi@0 1097 if (region && region != C->top()) {
aoqi@0 1098 for (uint i = 1; i < phi->req(); i++) {
aoqi@0 1099 Node* val = phi->in(i);
aoqi@0 1100 Node* use_c = region->in(i);
aoqi@0 1101 const TypeInt* val_t = filtered_type_from_dominators(val, use_c);
aoqi@0 1102 if (val_t != NULL) {
aoqi@0 1103 if (filtered_t == NULL) {
aoqi@0 1104 filtered_t = val_t;
aoqi@0 1105 } else {
aoqi@0 1106 filtered_t = filtered_t->meet(val_t)->is_int();
aoqi@0 1107 }
aoqi@0 1108 }
aoqi@0 1109 }
aoqi@0 1110 }
aoqi@0 1111 }
aoqi@0 1112 const TypeInt* n_t = _igvn.type(n)->is_int();
aoqi@0 1113 if (filtered_t != NULL) {
aoqi@0 1114 n_t = n_t->join(filtered_t)->is_int();
aoqi@0 1115 }
aoqi@0 1116 return n_t;
aoqi@0 1117 }
aoqi@0 1118
aoqi@0 1119
aoqi@0 1120 //------------------------------filtered_type_from_dominators--------------------------------
aoqi@0 1121 // Return a possibly more restrictive type for val based on condition control flow of dominators
aoqi@0 1122 const TypeInt* PhaseIdealLoop::filtered_type_from_dominators( Node* val, Node *use_ctrl) {
aoqi@0 1123 if (val->is_Con()) {
aoqi@0 1124 return val->bottom_type()->is_int();
aoqi@0 1125 }
aoqi@0 1126 uint if_limit = 10; // Max number of dominating if's visited
aoqi@0 1127 const TypeInt* rtn_t = NULL;
aoqi@0 1128
aoqi@0 1129 if (use_ctrl && use_ctrl != C->top()) {
aoqi@0 1130 Node* val_ctrl = get_ctrl(val);
aoqi@0 1131 uint val_dom_depth = dom_depth(val_ctrl);
aoqi@0 1132 Node* pred = use_ctrl;
aoqi@0 1133 uint if_cnt = 0;
aoqi@0 1134 while (if_cnt < if_limit) {
aoqi@0 1135 if ((pred->Opcode() == Op_IfTrue || pred->Opcode() == Op_IfFalse)) {
aoqi@0 1136 if_cnt++;
aoqi@0 1137 const TypeInt* if_t = IfNode::filtered_int_type(&_igvn, val, pred);
aoqi@0 1138 if (if_t != NULL) {
aoqi@0 1139 if (rtn_t == NULL) {
aoqi@0 1140 rtn_t = if_t;
aoqi@0 1141 } else {
aoqi@0 1142 rtn_t = rtn_t->join(if_t)->is_int();
aoqi@0 1143 }
aoqi@0 1144 }
aoqi@0 1145 }
aoqi@0 1146 pred = idom(pred);
aoqi@0 1147 if (pred == NULL || pred == C->top()) {
aoqi@0 1148 break;
aoqi@0 1149 }
aoqi@0 1150 // Stop if going beyond definition block of val
aoqi@0 1151 if (dom_depth(pred) < val_dom_depth) {
aoqi@0 1152 break;
aoqi@0 1153 }
aoqi@0 1154 }
aoqi@0 1155 }
aoqi@0 1156 return rtn_t;
aoqi@0 1157 }
aoqi@0 1158
aoqi@0 1159
aoqi@0 1160 //------------------------------dump_spec--------------------------------------
aoqi@0 1161 // Dump special per-node info
aoqi@0 1162 #ifndef PRODUCT
aoqi@0 1163 void CountedLoopEndNode::dump_spec(outputStream *st) const {
aoqi@0 1164 if( in(TestValue)->is_Bool() ) {
aoqi@0 1165 BoolTest bt( test_trip()); // Added this for g++.
aoqi@0 1166
aoqi@0 1167 st->print("[");
aoqi@0 1168 bt.dump_on(st);
aoqi@0 1169 st->print("]");
aoqi@0 1170 }
aoqi@0 1171 st->print(" ");
aoqi@0 1172 IfNode::dump_spec(st);
aoqi@0 1173 }
aoqi@0 1174 #endif
aoqi@0 1175
aoqi@0 1176 //=============================================================================
aoqi@0 1177 //------------------------------is_member--------------------------------------
aoqi@0 1178 // Is 'l' a member of 'this'?
aoqi@0 1179 int IdealLoopTree::is_member( const IdealLoopTree *l ) const {
aoqi@0 1180 while( l->_nest > _nest ) l = l->_parent;
aoqi@0 1181 return l == this;
aoqi@0 1182 }
aoqi@0 1183
aoqi@0 1184 //------------------------------set_nest---------------------------------------
aoqi@0 1185 // Set loop tree nesting depth. Accumulate _has_call bits.
aoqi@0 1186 int IdealLoopTree::set_nest( uint depth ) {
aoqi@0 1187 _nest = depth;
aoqi@0 1188 int bits = _has_call;
aoqi@0 1189 if( _child ) bits |= _child->set_nest(depth+1);
aoqi@0 1190 if( bits ) _has_call = 1;
aoqi@0 1191 if( _next ) bits |= _next ->set_nest(depth );
aoqi@0 1192 return bits;
aoqi@0 1193 }
aoqi@0 1194
aoqi@0 1195 //------------------------------split_fall_in----------------------------------
aoqi@0 1196 // Split out multiple fall-in edges from the loop header. Move them to a
aoqi@0 1197 // private RegionNode before the loop. This becomes the loop landing pad.
aoqi@0 1198 void IdealLoopTree::split_fall_in( PhaseIdealLoop *phase, int fall_in_cnt ) {
aoqi@0 1199 PhaseIterGVN &igvn = phase->_igvn;
aoqi@0 1200 uint i;
aoqi@0 1201
aoqi@0 1202 // Make a new RegionNode to be the landing pad.
aoqi@0 1203 Node *landing_pad = new (phase->C) RegionNode( fall_in_cnt+1 );
aoqi@0 1204 phase->set_loop(landing_pad,_parent);
aoqi@0 1205 // Gather all the fall-in control paths into the landing pad
aoqi@0 1206 uint icnt = fall_in_cnt;
aoqi@0 1207 uint oreq = _head->req();
aoqi@0 1208 for( i = oreq-1; i>0; i-- )
aoqi@0 1209 if( !phase->is_member( this, _head->in(i) ) )
aoqi@0 1210 landing_pad->set_req(icnt--,_head->in(i));
aoqi@0 1211
aoqi@0 1212 // Peel off PhiNode edges as well
aoqi@0 1213 for (DUIterator_Fast jmax, j = _head->fast_outs(jmax); j < jmax; j++) {
aoqi@0 1214 Node *oj = _head->fast_out(j);
aoqi@0 1215 if( oj->is_Phi() ) {
aoqi@0 1216 PhiNode* old_phi = oj->as_Phi();
aoqi@0 1217 assert( old_phi->region() == _head, "" );
aoqi@0 1218 igvn.hash_delete(old_phi); // Yank from hash before hacking edges
aoqi@0 1219 Node *p = PhiNode::make_blank(landing_pad, old_phi);
aoqi@0 1220 uint icnt = fall_in_cnt;
aoqi@0 1221 for( i = oreq-1; i>0; i-- ) {
aoqi@0 1222 if( !phase->is_member( this, _head->in(i) ) ) {
aoqi@0 1223 p->init_req(icnt--, old_phi->in(i));
aoqi@0 1224 // Go ahead and clean out old edges from old phi
aoqi@0 1225 old_phi->del_req(i);
aoqi@0 1226 }
aoqi@0 1227 }
aoqi@0 1228 // Search for CSE's here, because ZKM.jar does a lot of
aoqi@0 1229 // loop hackery and we need to be a little incremental
aoqi@0 1230 // with the CSE to avoid O(N^2) node blow-up.
aoqi@0 1231 Node *p2 = igvn.hash_find_insert(p); // Look for a CSE
aoqi@0 1232 if( p2 ) { // Found CSE
aoqi@0 1233 p->destruct(); // Recover useless new node
aoqi@0 1234 p = p2; // Use old node
aoqi@0 1235 } else {
aoqi@0 1236 igvn.register_new_node_with_optimizer(p, old_phi);
aoqi@0 1237 }
aoqi@0 1238 // Make old Phi refer to new Phi.
aoqi@0 1239 old_phi->add_req(p);
aoqi@0 1240 // Check for the special case of making the old phi useless and
aoqi@0 1241 // disappear it. In JavaGrande I have a case where this useless
aoqi@0 1242 // Phi is the loop limit and prevents recognizing a CountedLoop
aoqi@0 1243 // which in turn prevents removing an empty loop.
aoqi@0 1244 Node *id_old_phi = old_phi->Identity( &igvn );
aoqi@0 1245 if( id_old_phi != old_phi ) { // Found a simple identity?
aoqi@0 1246 // Note that I cannot call 'replace_node' here, because
aoqi@0 1247 // that will yank the edge from old_phi to the Region and
aoqi@0 1248 // I'm mid-iteration over the Region's uses.
aoqi@0 1249 for (DUIterator_Last imin, i = old_phi->last_outs(imin); i >= imin; ) {
aoqi@0 1250 Node* use = old_phi->last_out(i);
aoqi@0 1251 igvn.rehash_node_delayed(use);
aoqi@0 1252 uint uses_found = 0;
aoqi@0 1253 for (uint j = 0; j < use->len(); j++) {
aoqi@0 1254 if (use->in(j) == old_phi) {
aoqi@0 1255 if (j < use->req()) use->set_req (j, id_old_phi);
aoqi@0 1256 else use->set_prec(j, id_old_phi);
aoqi@0 1257 uses_found++;
aoqi@0 1258 }
aoqi@0 1259 }
aoqi@0 1260 i -= uses_found; // we deleted 1 or more copies of this edge
aoqi@0 1261 }
aoqi@0 1262 }
aoqi@0 1263 igvn._worklist.push(old_phi);
aoqi@0 1264 }
aoqi@0 1265 }
aoqi@0 1266 // Finally clean out the fall-in edges from the RegionNode
aoqi@0 1267 for( i = oreq-1; i>0; i-- ) {
aoqi@0 1268 if( !phase->is_member( this, _head->in(i) ) ) {
aoqi@0 1269 _head->del_req(i);
aoqi@0 1270 }
aoqi@0 1271 }
aoqi@0 1272 // Transform landing pad
aoqi@0 1273 igvn.register_new_node_with_optimizer(landing_pad, _head);
aoqi@0 1274 // Insert landing pad into the header
aoqi@0 1275 _head->add_req(landing_pad);
aoqi@0 1276 }
aoqi@0 1277
aoqi@0 1278 //------------------------------split_outer_loop-------------------------------
aoqi@0 1279 // Split out the outermost loop from this shared header.
aoqi@0 1280 void IdealLoopTree::split_outer_loop( PhaseIdealLoop *phase ) {
aoqi@0 1281 PhaseIterGVN &igvn = phase->_igvn;
aoqi@0 1282
aoqi@0 1283 // Find index of outermost loop; it should also be my tail.
aoqi@0 1284 uint outer_idx = 1;
aoqi@0 1285 while( _head->in(outer_idx) != _tail ) outer_idx++;
aoqi@0 1286
aoqi@0 1287 // Make a LoopNode for the outermost loop.
aoqi@0 1288 Node *ctl = _head->in(LoopNode::EntryControl);
aoqi@0 1289 Node *outer = new (phase->C) LoopNode( ctl, _head->in(outer_idx) );
aoqi@0 1290 outer = igvn.register_new_node_with_optimizer(outer, _head);
aoqi@0 1291 phase->set_created_loop_node();
aoqi@0 1292
aoqi@0 1293 // Outermost loop falls into '_head' loop
aoqi@0 1294 _head->set_req(LoopNode::EntryControl, outer);
aoqi@0 1295 _head->del_req(outer_idx);
aoqi@0 1296 // Split all the Phis up between '_head' loop and 'outer' loop.
aoqi@0 1297 for (DUIterator_Fast jmax, j = _head->fast_outs(jmax); j < jmax; j++) {
aoqi@0 1298 Node *out = _head->fast_out(j);
aoqi@0 1299 if( out->is_Phi() ) {
aoqi@0 1300 PhiNode *old_phi = out->as_Phi();
aoqi@0 1301 assert( old_phi->region() == _head, "" );
aoqi@0 1302 Node *phi = PhiNode::make_blank(outer, old_phi);
aoqi@0 1303 phi->init_req(LoopNode::EntryControl, old_phi->in(LoopNode::EntryControl));
aoqi@0 1304 phi->init_req(LoopNode::LoopBackControl, old_phi->in(outer_idx));
aoqi@0 1305 phi = igvn.register_new_node_with_optimizer(phi, old_phi);
aoqi@0 1306 // Make old Phi point to new Phi on the fall-in path
aoqi@0 1307 igvn.replace_input_of(old_phi, LoopNode::EntryControl, phi);
aoqi@0 1308 old_phi->del_req(outer_idx);
aoqi@0 1309 }
aoqi@0 1310 }
aoqi@0 1311
aoqi@0 1312 // Use the new loop head instead of the old shared one
aoqi@0 1313 _head = outer;
aoqi@0 1314 phase->set_loop(_head, this);
aoqi@0 1315 }
aoqi@0 1316
aoqi@0 1317 //------------------------------fix_parent-------------------------------------
aoqi@0 1318 static void fix_parent( IdealLoopTree *loop, IdealLoopTree *parent ) {
aoqi@0 1319 loop->_parent = parent;
aoqi@0 1320 if( loop->_child ) fix_parent( loop->_child, loop );
aoqi@0 1321 if( loop->_next ) fix_parent( loop->_next , parent );
aoqi@0 1322 }
aoqi@0 1323
aoqi@0 1324 //------------------------------estimate_path_freq-----------------------------
aoqi@0 1325 static float estimate_path_freq( Node *n ) {
aoqi@0 1326 // Try to extract some path frequency info
aoqi@0 1327 IfNode *iff;
aoqi@0 1328 for( int i = 0; i < 50; i++ ) { // Skip through a bunch of uncommon tests
aoqi@0 1329 uint nop = n->Opcode();
aoqi@0 1330 if( nop == Op_SafePoint ) { // Skip any safepoint
aoqi@0 1331 n = n->in(0);
aoqi@0 1332 continue;
aoqi@0 1333 }
aoqi@0 1334 if( nop == Op_CatchProj ) { // Get count from a prior call
aoqi@0 1335 // Assume call does not always throw exceptions: means the call-site
aoqi@0 1336 // count is also the frequency of the fall-through path.
aoqi@0 1337 assert( n->is_CatchProj(), "" );
aoqi@0 1338 if( ((CatchProjNode*)n)->_con != CatchProjNode::fall_through_index )
aoqi@0 1339 return 0.0f; // Assume call exception path is rare
aoqi@0 1340 Node *call = n->in(0)->in(0)->in(0);
aoqi@0 1341 assert( call->is_Call(), "expect a call here" );
aoqi@0 1342 const JVMState *jvms = ((CallNode*)call)->jvms();
aoqi@0 1343 ciMethodData* methodData = jvms->method()->method_data();
aoqi@0 1344 if (!methodData->is_mature()) return 0.0f; // No call-site data
aoqi@0 1345 ciProfileData* data = methodData->bci_to_data(jvms->bci());
aoqi@0 1346 if ((data == NULL) || !data->is_CounterData()) {
aoqi@0 1347 // no call profile available, try call's control input
aoqi@0 1348 n = n->in(0);
aoqi@0 1349 continue;
aoqi@0 1350 }
aoqi@0 1351 return data->as_CounterData()->count()/FreqCountInvocations;
aoqi@0 1352 }
aoqi@0 1353 // See if there's a gating IF test
aoqi@0 1354 Node *n_c = n->in(0);
aoqi@0 1355 if( !n_c->is_If() ) break; // No estimate available
aoqi@0 1356 iff = n_c->as_If();
aoqi@0 1357 if( iff->_fcnt != COUNT_UNKNOWN ) // Have a valid count?
aoqi@0 1358 // Compute how much count comes on this path
aoqi@0 1359 return ((nop == Op_IfTrue) ? iff->_prob : 1.0f - iff->_prob) * iff->_fcnt;
aoqi@0 1360 // Have no count info. Skip dull uncommon-trap like branches.
aoqi@0 1361 if( (nop == Op_IfTrue && iff->_prob < PROB_LIKELY_MAG(5)) ||
aoqi@0 1362 (nop == Op_IfFalse && iff->_prob > PROB_UNLIKELY_MAG(5)) )
aoqi@0 1363 break;
aoqi@0 1364 // Skip through never-taken branch; look for a real loop exit.
aoqi@0 1365 n = iff->in(0);
aoqi@0 1366 }
aoqi@0 1367 return 0.0f; // No estimate available
aoqi@0 1368 }
aoqi@0 1369
aoqi@0 1370 //------------------------------merge_many_backedges---------------------------
aoqi@0 1371 // Merge all the backedges from the shared header into a private Region.
aoqi@0 1372 // Feed that region as the one backedge to this loop.
aoqi@0 1373 void IdealLoopTree::merge_many_backedges( PhaseIdealLoop *phase ) {
aoqi@0 1374 uint i;
aoqi@0 1375
aoqi@0 1376 // Scan for the top 2 hottest backedges
aoqi@0 1377 float hotcnt = 0.0f;
aoqi@0 1378 float warmcnt = 0.0f;
aoqi@0 1379 uint hot_idx = 0;
aoqi@0 1380 // Loop starts at 2 because slot 1 is the fall-in path
aoqi@0 1381 for( i = 2; i < _head->req(); i++ ) {
aoqi@0 1382 float cnt = estimate_path_freq(_head->in(i));
aoqi@0 1383 if( cnt > hotcnt ) { // Grab hottest path
aoqi@0 1384 warmcnt = hotcnt;
aoqi@0 1385 hotcnt = cnt;
aoqi@0 1386 hot_idx = i;
aoqi@0 1387 } else if( cnt > warmcnt ) { // And 2nd hottest path
aoqi@0 1388 warmcnt = cnt;
aoqi@0 1389 }
aoqi@0 1390 }
aoqi@0 1391
aoqi@0 1392 // See if the hottest backedge is worthy of being an inner loop
aoqi@0 1393 // by being much hotter than the next hottest backedge.
aoqi@0 1394 if( hotcnt <= 0.0001 ||
aoqi@0 1395 hotcnt < 2.0*warmcnt ) hot_idx = 0;// No hot backedge
aoqi@0 1396
aoqi@0 1397 // Peel out the backedges into a private merge point; peel
aoqi@0 1398 // them all except optionally hot_idx.
aoqi@0 1399 PhaseIterGVN &igvn = phase->_igvn;
aoqi@0 1400
aoqi@0 1401 Node *hot_tail = NULL;
aoqi@0 1402 // Make a Region for the merge point
aoqi@0 1403 Node *r = new (phase->C) RegionNode(1);
aoqi@0 1404 for( i = 2; i < _head->req(); i++ ) {
aoqi@0 1405 if( i != hot_idx )
aoqi@0 1406 r->add_req( _head->in(i) );
aoqi@0 1407 else hot_tail = _head->in(i);
aoqi@0 1408 }
aoqi@0 1409 igvn.register_new_node_with_optimizer(r, _head);
aoqi@0 1410 // Plug region into end of loop _head, followed by hot_tail
aoqi@0 1411 while( _head->req() > 3 ) _head->del_req( _head->req()-1 );
aoqi@0 1412 _head->set_req(2, r);
aoqi@0 1413 if( hot_idx ) _head->add_req(hot_tail);
aoqi@0 1414
aoqi@0 1415 // Split all the Phis up between '_head' loop and the Region 'r'
aoqi@0 1416 for (DUIterator_Fast jmax, j = _head->fast_outs(jmax); j < jmax; j++) {
aoqi@0 1417 Node *out = _head->fast_out(j);
aoqi@0 1418 if( out->is_Phi() ) {
aoqi@0 1419 PhiNode* n = out->as_Phi();
aoqi@0 1420 igvn.hash_delete(n); // Delete from hash before hacking edges
aoqi@0 1421 Node *hot_phi = NULL;
aoqi@0 1422 Node *phi = new (phase->C) PhiNode(r, n->type(), n->adr_type());
aoqi@0 1423 // Check all inputs for the ones to peel out
aoqi@0 1424 uint j = 1;
aoqi@0 1425 for( uint i = 2; i < n->req(); i++ ) {
aoqi@0 1426 if( i != hot_idx )
aoqi@0 1427 phi->set_req( j++, n->in(i) );
aoqi@0 1428 else hot_phi = n->in(i);
aoqi@0 1429 }
aoqi@0 1430 // Register the phi but do not transform until whole place transforms
aoqi@0 1431 igvn.register_new_node_with_optimizer(phi, n);
aoqi@0 1432 // Add the merge phi to the old Phi
aoqi@0 1433 while( n->req() > 3 ) n->del_req( n->req()-1 );
aoqi@0 1434 n->set_req(2, phi);
aoqi@0 1435 if( hot_idx ) n->add_req(hot_phi);
aoqi@0 1436 }
aoqi@0 1437 }
aoqi@0 1438
aoqi@0 1439
aoqi@0 1440 // Insert a new IdealLoopTree inserted below me. Turn it into a clone
aoqi@0 1441 // of self loop tree. Turn self into a loop headed by _head and with
aoqi@0 1442 // tail being the new merge point.
aoqi@0 1443 IdealLoopTree *ilt = new IdealLoopTree( phase, _head, _tail );
aoqi@0 1444 phase->set_loop(_tail,ilt); // Adjust tail
aoqi@0 1445 _tail = r; // Self's tail is new merge point
aoqi@0 1446 phase->set_loop(r,this);
aoqi@0 1447 ilt->_child = _child; // New guy has my children
aoqi@0 1448 _child = ilt; // Self has new guy as only child
aoqi@0 1449 ilt->_parent = this; // new guy has self for parent
aoqi@0 1450 ilt->_nest = _nest; // Same nesting depth (for now)
aoqi@0 1451
aoqi@0 1452 // Starting with 'ilt', look for child loop trees using the same shared
aoqi@0 1453 // header. Flatten these out; they will no longer be loops in the end.
aoqi@0 1454 IdealLoopTree **pilt = &_child;
aoqi@0 1455 while( ilt ) {
aoqi@0 1456 if( ilt->_head == _head ) {
aoqi@0 1457 uint i;
aoqi@0 1458 for( i = 2; i < _head->req(); i++ )
aoqi@0 1459 if( _head->in(i) == ilt->_tail )
aoqi@0 1460 break; // Still a loop
aoqi@0 1461 if( i == _head->req() ) { // No longer a loop
aoqi@0 1462 // Flatten ilt. Hang ilt's "_next" list from the end of
aoqi@0 1463 // ilt's '_child' list. Move the ilt's _child up to replace ilt.
aoqi@0 1464 IdealLoopTree **cp = &ilt->_child;
aoqi@0 1465 while( *cp ) cp = &(*cp)->_next; // Find end of child list
aoqi@0 1466 *cp = ilt->_next; // Hang next list at end of child list
aoqi@0 1467 *pilt = ilt->_child; // Move child up to replace ilt
aoqi@0 1468 ilt->_head = NULL; // Flag as a loop UNIONED into parent
aoqi@0 1469 ilt = ilt->_child; // Repeat using new ilt
aoqi@0 1470 continue; // do not advance over ilt->_child
aoqi@0 1471 }
aoqi@0 1472 assert( ilt->_tail == hot_tail, "expected to only find the hot inner loop here" );
aoqi@0 1473 phase->set_loop(_head,ilt);
aoqi@0 1474 }
aoqi@0 1475 pilt = &ilt->_child; // Advance to next
aoqi@0 1476 ilt = *pilt;
aoqi@0 1477 }
aoqi@0 1478
aoqi@0 1479 if( _child ) fix_parent( _child, this );
aoqi@0 1480 }
aoqi@0 1481
aoqi@0 1482 //------------------------------beautify_loops---------------------------------
aoqi@0 1483 // Split shared headers and insert loop landing pads.
aoqi@0 1484 // Insert a LoopNode to replace the RegionNode.
aoqi@0 1485 // Return TRUE if loop tree is structurally changed.
aoqi@0 1486 bool IdealLoopTree::beautify_loops( PhaseIdealLoop *phase ) {
aoqi@0 1487 bool result = false;
aoqi@0 1488 // Cache parts in locals for easy
aoqi@0 1489 PhaseIterGVN &igvn = phase->_igvn;
aoqi@0 1490
aoqi@0 1491 igvn.hash_delete(_head); // Yank from hash before hacking edges
aoqi@0 1492
aoqi@0 1493 // Check for multiple fall-in paths. Peel off a landing pad if need be.
aoqi@0 1494 int fall_in_cnt = 0;
aoqi@0 1495 for( uint i = 1; i < _head->req(); i++ )
aoqi@0 1496 if( !phase->is_member( this, _head->in(i) ) )
aoqi@0 1497 fall_in_cnt++;
aoqi@0 1498 assert( fall_in_cnt, "at least 1 fall-in path" );
aoqi@0 1499 if( fall_in_cnt > 1 ) // Need a loop landing pad to merge fall-ins
aoqi@0 1500 split_fall_in( phase, fall_in_cnt );
aoqi@0 1501
aoqi@0 1502 // Swap inputs to the _head and all Phis to move the fall-in edge to
aoqi@0 1503 // the left.
aoqi@0 1504 fall_in_cnt = 1;
aoqi@0 1505 while( phase->is_member( this, _head->in(fall_in_cnt) ) )
aoqi@0 1506 fall_in_cnt++;
aoqi@0 1507 if( fall_in_cnt > 1 ) {
aoqi@0 1508 // Since I am just swapping inputs I do not need to update def-use info
aoqi@0 1509 Node *tmp = _head->in(1);
aoqi@0 1510 _head->set_req( 1, _head->in(fall_in_cnt) );
aoqi@0 1511 _head->set_req( fall_in_cnt, tmp );
aoqi@0 1512 // Swap also all Phis
aoqi@0 1513 for (DUIterator_Fast imax, i = _head->fast_outs(imax); i < imax; i++) {
aoqi@0 1514 Node* phi = _head->fast_out(i);
aoqi@0 1515 if( phi->is_Phi() ) {
aoqi@0 1516 igvn.hash_delete(phi); // Yank from hash before hacking edges
aoqi@0 1517 tmp = phi->in(1);
aoqi@0 1518 phi->set_req( 1, phi->in(fall_in_cnt) );
aoqi@0 1519 phi->set_req( fall_in_cnt, tmp );
aoqi@0 1520 }
aoqi@0 1521 }
aoqi@0 1522 }
aoqi@0 1523 assert( !phase->is_member( this, _head->in(1) ), "left edge is fall-in" );
aoqi@0 1524 assert( phase->is_member( this, _head->in(2) ), "right edge is loop" );
aoqi@0 1525
aoqi@0 1526 // If I am a shared header (multiple backedges), peel off the many
aoqi@0 1527 // backedges into a private merge point and use the merge point as
aoqi@0 1528 // the one true backedge.
aoqi@0 1529 if( _head->req() > 3 ) {
aoqi@0 1530 // Merge the many backedges into a single backedge but leave
aoqi@0 1531 // the hottest backedge as separate edge for the following peel.
aoqi@0 1532 merge_many_backedges( phase );
aoqi@0 1533 result = true;
aoqi@0 1534 }
aoqi@0 1535
aoqi@0 1536 // If I have one hot backedge, peel off myself loop.
aoqi@0 1537 // I better be the outermost loop.
aoqi@0 1538 if (_head->req() > 3 && !_irreducible) {
aoqi@0 1539 split_outer_loop( phase );
aoqi@0 1540 result = true;
aoqi@0 1541
aoqi@0 1542 } else if (!_head->is_Loop() && !_irreducible) {
aoqi@0 1543 // Make a new LoopNode to replace the old loop head
aoqi@0 1544 Node *l = new (phase->C) LoopNode( _head->in(1), _head->in(2) );
aoqi@0 1545 l = igvn.register_new_node_with_optimizer(l, _head);
aoqi@0 1546 phase->set_created_loop_node();
aoqi@0 1547 // Go ahead and replace _head
aoqi@0 1548 phase->_igvn.replace_node( _head, l );
aoqi@0 1549 _head = l;
aoqi@0 1550 phase->set_loop(_head, this);
aoqi@0 1551 }
aoqi@0 1552
aoqi@0 1553 // Now recursively beautify nested loops
aoqi@0 1554 if( _child ) result |= _child->beautify_loops( phase );
aoqi@0 1555 if( _next ) result |= _next ->beautify_loops( phase );
aoqi@0 1556 return result;
aoqi@0 1557 }
aoqi@0 1558
aoqi@0 1559 //------------------------------allpaths_check_safepts----------------------------
aoqi@0 1560 // Allpaths backwards scan from loop tail, terminating each path at first safepoint
aoqi@0 1561 // encountered. Helper for check_safepts.
aoqi@0 1562 void IdealLoopTree::allpaths_check_safepts(VectorSet &visited, Node_List &stack) {
aoqi@0 1563 assert(stack.size() == 0, "empty stack");
aoqi@0 1564 stack.push(_tail);
aoqi@0 1565 visited.Clear();
aoqi@0 1566 visited.set(_tail->_idx);
aoqi@0 1567 while (stack.size() > 0) {
aoqi@0 1568 Node* n = stack.pop();
aoqi@0 1569 if (n->is_Call() && n->as_Call()->guaranteed_safepoint()) {
aoqi@0 1570 // Terminate this path
aoqi@0 1571 } else if (n->Opcode() == Op_SafePoint) {
aoqi@0 1572 if (_phase->get_loop(n) != this) {
aoqi@0 1573 if (_required_safept == NULL) _required_safept = new Node_List();
aoqi@0 1574 _required_safept->push(n); // save the one closest to the tail
aoqi@0 1575 }
aoqi@0 1576 // Terminate this path
aoqi@0 1577 } else {
aoqi@0 1578 uint start = n->is_Region() ? 1 : 0;
aoqi@0 1579 uint end = n->is_Region() && !n->is_Loop() ? n->req() : start + 1;
aoqi@0 1580 for (uint i = start; i < end; i++) {
aoqi@0 1581 Node* in = n->in(i);
aoqi@0 1582 assert(in->is_CFG(), "must be");
aoqi@0 1583 if (!visited.test_set(in->_idx) && is_member(_phase->get_loop(in))) {
aoqi@0 1584 stack.push(in);
aoqi@0 1585 }
aoqi@0 1586 }
aoqi@0 1587 }
aoqi@0 1588 }
aoqi@0 1589 }
aoqi@0 1590
aoqi@0 1591 //------------------------------check_safepts----------------------------
aoqi@0 1592 // Given dominators, try to find loops with calls that must always be
aoqi@0 1593 // executed (call dominates loop tail). These loops do not need non-call
aoqi@0 1594 // safepoints (ncsfpt).
aoqi@0 1595 //
aoqi@0 1596 // A complication is that a safepoint in a inner loop may be needed
aoqi@0 1597 // by an outer loop. In the following, the inner loop sees it has a
aoqi@0 1598 // call (block 3) on every path from the head (block 2) to the
aoqi@0 1599 // backedge (arc 3->2). So it deletes the ncsfpt (non-call safepoint)
aoqi@0 1600 // in block 2, _but_ this leaves the outer loop without a safepoint.
aoqi@0 1601 //
aoqi@0 1602 // entry 0
aoqi@0 1603 // |
aoqi@0 1604 // v
aoqi@0 1605 // outer 1,2 +->1
aoqi@0 1606 // | |
aoqi@0 1607 // | v
aoqi@0 1608 // | 2<---+ ncsfpt in 2
aoqi@0 1609 // |_/|\ |
aoqi@0 1610 // | v |
aoqi@0 1611 // inner 2,3 / 3 | call in 3
aoqi@0 1612 // / | |
aoqi@0 1613 // v +--+
aoqi@0 1614 // exit 4
aoqi@0 1615 //
aoqi@0 1616 //
aoqi@0 1617 // This method creates a list (_required_safept) of ncsfpt nodes that must
aoqi@0 1618 // be protected is created for each loop. When a ncsfpt maybe deleted, it
aoqi@0 1619 // is first looked for in the lists for the outer loops of the current loop.
aoqi@0 1620 //
aoqi@0 1621 // The insights into the problem:
aoqi@0 1622 // A) counted loops are okay
aoqi@0 1623 // B) innermost loops are okay (only an inner loop can delete
aoqi@0 1624 // a ncsfpt needed by an outer loop)
aoqi@0 1625 // C) a loop is immune from an inner loop deleting a safepoint
aoqi@0 1626 // if the loop has a call on the idom-path
aoqi@0 1627 // D) a loop is also immune if it has a ncsfpt (non-call safepoint) on the
aoqi@0 1628 // idom-path that is not in a nested loop
aoqi@0 1629 // E) otherwise, an ncsfpt on the idom-path that is nested in an inner
aoqi@0 1630 // loop needs to be prevented from deletion by an inner loop
aoqi@0 1631 //
aoqi@0 1632 // There are two analyses:
aoqi@0 1633 // 1) The first, and cheaper one, scans the loop body from
aoqi@0 1634 // tail to head following the idom (immediate dominator)
aoqi@0 1635 // chain, looking for the cases (C,D,E) above.
aoqi@0 1636 // Since inner loops are scanned before outer loops, there is summary
aoqi@0 1637 // information about inner loops. Inner loops can be skipped over
aoqi@0 1638 // when the tail of an inner loop is encountered.
aoqi@0 1639 //
aoqi@0 1640 // 2) The second, invoked if the first fails to find a call or ncsfpt on
aoqi@0 1641 // the idom path (which is rare), scans all predecessor control paths
aoqi@0 1642 // from the tail to the head, terminating a path when a call or sfpt
aoqi@0 1643 // is encountered, to find the ncsfpt's that are closest to the tail.
aoqi@0 1644 //
aoqi@0 1645 void IdealLoopTree::check_safepts(VectorSet &visited, Node_List &stack) {
aoqi@0 1646 // Bottom up traversal
aoqi@0 1647 IdealLoopTree* ch = _child;
aoqi@0 1648 if (_child) _child->check_safepts(visited, stack);
aoqi@0 1649 if (_next) _next ->check_safepts(visited, stack);
aoqi@0 1650
aoqi@0 1651 if (!_head->is_CountedLoop() && !_has_sfpt && _parent != NULL && !_irreducible) {
aoqi@0 1652 bool has_call = false; // call on dom-path
aoqi@0 1653 bool has_local_ncsfpt = false; // ncsfpt on dom-path at this loop depth
aoqi@0 1654 Node* nonlocal_ncsfpt = NULL; // ncsfpt on dom-path at a deeper depth
aoqi@0 1655 // Scan the dom-path nodes from tail to head
aoqi@0 1656 for (Node* n = tail(); n != _head; n = _phase->idom(n)) {
aoqi@0 1657 if (n->is_Call() && n->as_Call()->guaranteed_safepoint()) {
aoqi@0 1658 has_call = true;
aoqi@0 1659 _has_sfpt = 1; // Then no need for a safept!
aoqi@0 1660 break;
aoqi@0 1661 } else if (n->Opcode() == Op_SafePoint) {
aoqi@0 1662 if (_phase->get_loop(n) == this) {
aoqi@0 1663 has_local_ncsfpt = true;
aoqi@0 1664 break;
aoqi@0 1665 }
aoqi@0 1666 if (nonlocal_ncsfpt == NULL) {
aoqi@0 1667 nonlocal_ncsfpt = n; // save the one closest to the tail
aoqi@0 1668 }
aoqi@0 1669 } else {
aoqi@0 1670 IdealLoopTree* nlpt = _phase->get_loop(n);
aoqi@0 1671 if (this != nlpt) {
aoqi@0 1672 // If at an inner loop tail, see if the inner loop has already
aoqi@0 1673 // recorded seeing a call on the dom-path (and stop.) If not,
aoqi@0 1674 // jump to the head of the inner loop.
aoqi@0 1675 assert(is_member(nlpt), "nested loop");
aoqi@0 1676 Node* tail = nlpt->_tail;
aoqi@0 1677 if (tail->in(0)->is_If()) tail = tail->in(0);
aoqi@0 1678 if (n == tail) {
aoqi@0 1679 // If inner loop has call on dom-path, so does outer loop
aoqi@0 1680 if (nlpt->_has_sfpt) {
aoqi@0 1681 has_call = true;
aoqi@0 1682 _has_sfpt = 1;
aoqi@0 1683 break;
aoqi@0 1684 }
aoqi@0 1685 // Skip to head of inner loop
aoqi@0 1686 assert(_phase->is_dominator(_head, nlpt->_head), "inner head dominated by outer head");
aoqi@0 1687 n = nlpt->_head;
aoqi@0 1688 }
aoqi@0 1689 }
aoqi@0 1690 }
aoqi@0 1691 }
aoqi@0 1692 // Record safept's that this loop needs preserved when an
aoqi@0 1693 // inner loop attempts to delete it's safepoints.
aoqi@0 1694 if (_child != NULL && !has_call && !has_local_ncsfpt) {
aoqi@0 1695 if (nonlocal_ncsfpt != NULL) {
aoqi@0 1696 if (_required_safept == NULL) _required_safept = new Node_List();
aoqi@0 1697 _required_safept->push(nonlocal_ncsfpt);
aoqi@0 1698 } else {
aoqi@0 1699 // Failed to find a suitable safept on the dom-path. Now use
aoqi@0 1700 // an all paths walk from tail to head, looking for safepoints to preserve.
aoqi@0 1701 allpaths_check_safepts(visited, stack);
aoqi@0 1702 }
aoqi@0 1703 }
aoqi@0 1704 }
aoqi@0 1705 }
aoqi@0 1706
aoqi@0 1707 //---------------------------is_deleteable_safept----------------------------
aoqi@0 1708 // Is safept not required by an outer loop?
aoqi@0 1709 bool PhaseIdealLoop::is_deleteable_safept(Node* sfpt) {
aoqi@0 1710 assert(sfpt->Opcode() == Op_SafePoint, "");
aoqi@0 1711 IdealLoopTree* lp = get_loop(sfpt)->_parent;
aoqi@0 1712 while (lp != NULL) {
aoqi@0 1713 Node_List* sfpts = lp->_required_safept;
aoqi@0 1714 if (sfpts != NULL) {
aoqi@0 1715 for (uint i = 0; i < sfpts->size(); i++) {
aoqi@0 1716 if (sfpt == sfpts->at(i))
aoqi@0 1717 return false;
aoqi@0 1718 }
aoqi@0 1719 }
aoqi@0 1720 lp = lp->_parent;
aoqi@0 1721 }
aoqi@0 1722 return true;
aoqi@0 1723 }
aoqi@0 1724
aoqi@0 1725 //---------------------------replace_parallel_iv-------------------------------
aoqi@0 1726 // Replace parallel induction variable (parallel to trip counter)
aoqi@0 1727 void PhaseIdealLoop::replace_parallel_iv(IdealLoopTree *loop) {
aoqi@0 1728 assert(loop->_head->is_CountedLoop(), "");
aoqi@0 1729 CountedLoopNode *cl = loop->_head->as_CountedLoop();
aoqi@0 1730 if (!cl->is_valid_counted_loop())
aoqi@0 1731 return; // skip malformed counted loop
aoqi@0 1732 Node *incr = cl->incr();
aoqi@0 1733 if (incr == NULL)
aoqi@0 1734 return; // Dead loop?
aoqi@0 1735 Node *init = cl->init_trip();
aoqi@0 1736 Node *phi = cl->phi();
aoqi@0 1737 int stride_con = cl->stride_con();
aoqi@0 1738
aoqi@0 1739 // Visit all children, looking for Phis
aoqi@0 1740 for (DUIterator i = cl->outs(); cl->has_out(i); i++) {
aoqi@0 1741 Node *out = cl->out(i);
aoqi@0 1742 // Look for other phis (secondary IVs). Skip dead ones
aoqi@0 1743 if (!out->is_Phi() || out == phi || !has_node(out))
aoqi@0 1744 continue;
aoqi@0 1745 PhiNode* phi2 = out->as_Phi();
aoqi@0 1746 Node *incr2 = phi2->in( LoopNode::LoopBackControl );
aoqi@0 1747 // Look for induction variables of the form: X += constant
aoqi@0 1748 if (phi2->region() != loop->_head ||
aoqi@0 1749 incr2->req() != 3 ||
aoqi@0 1750 incr2->in(1) != phi2 ||
aoqi@0 1751 incr2 == incr ||
aoqi@0 1752 incr2->Opcode() != Op_AddI ||
aoqi@0 1753 !incr2->in(2)->is_Con())
aoqi@0 1754 continue;
aoqi@0 1755
aoqi@0 1756 // Check for parallel induction variable (parallel to trip counter)
aoqi@0 1757 // via an affine function. In particular, count-down loops with
aoqi@0 1758 // count-up array indices are common. We only RCE references off
aoqi@0 1759 // the trip-counter, so we need to convert all these to trip-counter
aoqi@0 1760 // expressions.
aoqi@0 1761 Node *init2 = phi2->in( LoopNode::EntryControl );
aoqi@0 1762 int stride_con2 = incr2->in(2)->get_int();
aoqi@0 1763
aoqi@0 1764 // The general case here gets a little tricky. We want to find the
aoqi@0 1765 // GCD of all possible parallel IV's and make a new IV using this
aoqi@0 1766 // GCD for the loop. Then all possible IVs are simple multiples of
aoqi@0 1767 // the GCD. In practice, this will cover very few extra loops.
aoqi@0 1768 // Instead we require 'stride_con2' to be a multiple of 'stride_con',
aoqi@0 1769 // where +/-1 is the common case, but other integer multiples are
aoqi@0 1770 // also easy to handle.
aoqi@0 1771 int ratio_con = stride_con2/stride_con;
aoqi@0 1772
aoqi@0 1773 if ((ratio_con * stride_con) == stride_con2) { // Check for exact
aoqi@0 1774 #ifndef PRODUCT
aoqi@0 1775 if (TraceLoopOpts) {
aoqi@0 1776 tty->print("Parallel IV: %d ", phi2->_idx);
aoqi@0 1777 loop->dump_head();
aoqi@0 1778 }
aoqi@0 1779 #endif
aoqi@0 1780 // Convert to using the trip counter. The parallel induction
aoqi@0 1781 // variable differs from the trip counter by a loop-invariant
aoqi@0 1782 // amount, the difference between their respective initial values.
aoqi@0 1783 // It is scaled by the 'ratio_con'.
aoqi@0 1784 Node* ratio = _igvn.intcon(ratio_con);
aoqi@0 1785 set_ctrl(ratio, C->root());
aoqi@0 1786 Node* ratio_init = new (C) MulINode(init, ratio);
aoqi@0 1787 _igvn.register_new_node_with_optimizer(ratio_init, init);
aoqi@0 1788 set_early_ctrl(ratio_init);
aoqi@0 1789 Node* diff = new (C) SubINode(init2, ratio_init);
aoqi@0 1790 _igvn.register_new_node_with_optimizer(diff, init2);
aoqi@0 1791 set_early_ctrl(diff);
aoqi@0 1792 Node* ratio_idx = new (C) MulINode(phi, ratio);
aoqi@0 1793 _igvn.register_new_node_with_optimizer(ratio_idx, phi);
aoqi@0 1794 set_ctrl(ratio_idx, cl);
aoqi@0 1795 Node* add = new (C) AddINode(ratio_idx, diff);
aoqi@0 1796 _igvn.register_new_node_with_optimizer(add);
aoqi@0 1797 set_ctrl(add, cl);
aoqi@0 1798 _igvn.replace_node( phi2, add );
aoqi@0 1799 // Sometimes an induction variable is unused
aoqi@0 1800 if (add->outcnt() == 0) {
aoqi@0 1801 _igvn.remove_dead_node(add);
aoqi@0 1802 }
aoqi@0 1803 --i; // deleted this phi; rescan starting with next position
aoqi@0 1804 continue;
aoqi@0 1805 }
aoqi@0 1806 }
aoqi@0 1807 }
aoqi@0 1808
aoqi@0 1809 //------------------------------counted_loop-----------------------------------
aoqi@0 1810 // Convert to counted loops where possible
aoqi@0 1811 void IdealLoopTree::counted_loop( PhaseIdealLoop *phase ) {
aoqi@0 1812
aoqi@0 1813 // For grins, set the inner-loop flag here
aoqi@0 1814 if (!_child) {
aoqi@0 1815 if (_head->is_Loop()) _head->as_Loop()->set_inner_loop();
aoqi@0 1816 }
aoqi@0 1817
aoqi@0 1818 if (_head->is_CountedLoop() ||
aoqi@0 1819 phase->is_counted_loop(_head, this)) {
aoqi@0 1820 _has_sfpt = 1; // Indicate we do not need a safepoint here
aoqi@0 1821
aoqi@0 1822 // Look for safepoints to remove.
aoqi@0 1823 Node_List* sfpts = _safepts;
aoqi@0 1824 if (sfpts != NULL) {
aoqi@0 1825 for (uint i = 0; i < sfpts->size(); i++) {
aoqi@0 1826 Node* n = sfpts->at(i);
aoqi@0 1827 assert(phase->get_loop(n) == this, "");
aoqi@0 1828 if (phase->is_deleteable_safept(n)) {
aoqi@0 1829 phase->lazy_replace(n, n->in(TypeFunc::Control));
aoqi@0 1830 }
aoqi@0 1831 }
aoqi@0 1832 }
aoqi@0 1833
aoqi@0 1834 // Look for induction variables
aoqi@0 1835 phase->replace_parallel_iv(this);
aoqi@0 1836
aoqi@0 1837 } else if (_parent != NULL && !_irreducible) {
aoqi@0 1838 // Not a counted loop.
aoqi@0 1839 // Look for a safepoint on the idom-path.
aoqi@0 1840 Node* sfpt = tail();
aoqi@0 1841 for (; sfpt != _head; sfpt = phase->idom(sfpt)) {
aoqi@0 1842 if (sfpt->Opcode() == Op_SafePoint && phase->get_loop(sfpt) == this)
aoqi@0 1843 break; // Found one
aoqi@0 1844 }
aoqi@0 1845 // Delete other safepoints in this loop.
aoqi@0 1846 Node_List* sfpts = _safepts;
aoqi@0 1847 if (sfpts != NULL && sfpt != _head && sfpt->Opcode() == Op_SafePoint) {
aoqi@0 1848 for (uint i = 0; i < sfpts->size(); i++) {
aoqi@0 1849 Node* n = sfpts->at(i);
aoqi@0 1850 assert(phase->get_loop(n) == this, "");
aoqi@0 1851 if (n != sfpt && phase->is_deleteable_safept(n)) {
aoqi@0 1852 phase->lazy_replace(n, n->in(TypeFunc::Control));
aoqi@0 1853 }
aoqi@0 1854 }
aoqi@0 1855 }
aoqi@0 1856 }
aoqi@0 1857
aoqi@0 1858 // Recursively
aoqi@0 1859 if (_child) _child->counted_loop( phase );
aoqi@0 1860 if (_next) _next ->counted_loop( phase );
aoqi@0 1861 }
aoqi@0 1862
aoqi@0 1863 #ifndef PRODUCT
aoqi@0 1864 //------------------------------dump_head--------------------------------------
aoqi@0 1865 // Dump 1 liner for loop header info
aoqi@0 1866 void IdealLoopTree::dump_head( ) const {
aoqi@0 1867 for (uint i=0; i<_nest; i++)
aoqi@0 1868 tty->print(" ");
aoqi@0 1869 tty->print("Loop: N%d/N%d ",_head->_idx,_tail->_idx);
aoqi@0 1870 if (_irreducible) tty->print(" IRREDUCIBLE");
aoqi@0 1871 Node* entry = _head->in(LoopNode::EntryControl);
aoqi@0 1872 if (LoopLimitCheck) {
aoqi@0 1873 Node* predicate = PhaseIdealLoop::find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
aoqi@0 1874 if (predicate != NULL ) {
aoqi@0 1875 tty->print(" limit_check");
aoqi@0 1876 entry = entry->in(0)->in(0);
aoqi@0 1877 }
aoqi@0 1878 }
aoqi@0 1879 if (UseLoopPredicate) {
aoqi@0 1880 entry = PhaseIdealLoop::find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
aoqi@0 1881 if (entry != NULL) {
aoqi@0 1882 tty->print(" predicated");
aoqi@0 1883 }
aoqi@0 1884 }
aoqi@0 1885 if (_head->is_CountedLoop()) {
aoqi@0 1886 CountedLoopNode *cl = _head->as_CountedLoop();
aoqi@0 1887 tty->print(" counted");
aoqi@0 1888
aoqi@0 1889 Node* init_n = cl->init_trip();
aoqi@0 1890 if (init_n != NULL && init_n->is_Con())
aoqi@0 1891 tty->print(" [%d,", cl->init_trip()->get_int());
aoqi@0 1892 else
aoqi@0 1893 tty->print(" [int,");
aoqi@0 1894 Node* limit_n = cl->limit();
aoqi@0 1895 if (limit_n != NULL && limit_n->is_Con())
aoqi@0 1896 tty->print("%d),", cl->limit()->get_int());
aoqi@0 1897 else
aoqi@0 1898 tty->print("int),");
aoqi@0 1899 int stride_con = cl->stride_con();
aoqi@0 1900 if (stride_con > 0) tty->print("+");
aoqi@0 1901 tty->print("%d", stride_con);
aoqi@0 1902
aoqi@0 1903 tty->print(" (%d iters) ", (int)cl->profile_trip_cnt());
aoqi@0 1904
aoqi@0 1905 if (cl->is_pre_loop ()) tty->print(" pre" );
aoqi@0 1906 if (cl->is_main_loop()) tty->print(" main");
aoqi@0 1907 if (cl->is_post_loop()) tty->print(" post");
aoqi@0 1908 }
aoqi@0 1909 tty->cr();
aoqi@0 1910 }
aoqi@0 1911
aoqi@0 1912 //------------------------------dump-------------------------------------------
aoqi@0 1913 // Dump loops by loop tree
aoqi@0 1914 void IdealLoopTree::dump( ) const {
aoqi@0 1915 dump_head();
aoqi@0 1916 if (_child) _child->dump();
aoqi@0 1917 if (_next) _next ->dump();
aoqi@0 1918 }
aoqi@0 1919
aoqi@0 1920 #endif
aoqi@0 1921
aoqi@0 1922 static void log_loop_tree(IdealLoopTree* root, IdealLoopTree* loop, CompileLog* log) {
aoqi@0 1923 if (loop == root) {
aoqi@0 1924 if (loop->_child != NULL) {
aoqi@0 1925 log->begin_head("loop_tree");
aoqi@0 1926 log->end_head();
aoqi@0 1927 if( loop->_child ) log_loop_tree(root, loop->_child, log);
aoqi@0 1928 log->tail("loop_tree");
aoqi@0 1929 assert(loop->_next == NULL, "what?");
aoqi@0 1930 }
aoqi@0 1931 } else {
aoqi@0 1932 Node* head = loop->_head;
aoqi@0 1933 log->begin_head("loop");
aoqi@0 1934 log->print(" idx='%d' ", head->_idx);
aoqi@0 1935 if (loop->_irreducible) log->print("irreducible='1' ");
aoqi@0 1936 if (head->is_Loop()) {
aoqi@0 1937 if (head->as_Loop()->is_inner_loop()) log->print("inner_loop='1' ");
aoqi@0 1938 if (head->as_Loop()->is_partial_peel_loop()) log->print("partial_peel_loop='1' ");
aoqi@0 1939 }
aoqi@0 1940 if (head->is_CountedLoop()) {
aoqi@0 1941 CountedLoopNode* cl = head->as_CountedLoop();
aoqi@0 1942 if (cl->is_pre_loop()) log->print("pre_loop='%d' ", cl->main_idx());
aoqi@0 1943 if (cl->is_main_loop()) log->print("main_loop='%d' ", cl->_idx);
aoqi@0 1944 if (cl->is_post_loop()) log->print("post_loop='%d' ", cl->main_idx());
aoqi@0 1945 }
aoqi@0 1946 log->end_head();
aoqi@0 1947 if( loop->_child ) log_loop_tree(root, loop->_child, log);
aoqi@0 1948 log->tail("loop");
aoqi@0 1949 if( loop->_next ) log_loop_tree(root, loop->_next, log);
aoqi@0 1950 }
aoqi@0 1951 }
aoqi@0 1952
aoqi@0 1953 //---------------------collect_potentially_useful_predicates-----------------------
aoqi@0 1954 // Helper function to collect potentially useful predicates to prevent them from
aoqi@0 1955 // being eliminated by PhaseIdealLoop::eliminate_useless_predicates
aoqi@0 1956 void PhaseIdealLoop::collect_potentially_useful_predicates(
aoqi@0 1957 IdealLoopTree * loop, Unique_Node_List &useful_predicates) {
aoqi@0 1958 if (loop->_child) { // child
aoqi@0 1959 collect_potentially_useful_predicates(loop->_child, useful_predicates);
aoqi@0 1960 }
aoqi@0 1961
aoqi@0 1962 // self (only loops that we can apply loop predication may use their predicates)
aoqi@0 1963 if (loop->_head->is_Loop() &&
aoqi@0 1964 !loop->_irreducible &&
aoqi@0 1965 !loop->tail()->is_top()) {
aoqi@0 1966 LoopNode* lpn = loop->_head->as_Loop();
aoqi@0 1967 Node* entry = lpn->in(LoopNode::EntryControl);
aoqi@0 1968 Node* predicate_proj = find_predicate(entry); // loop_limit_check first
aoqi@0 1969 if (predicate_proj != NULL ) { // right pattern that can be used by loop predication
aoqi@0 1970 assert(entry->in(0)->in(1)->in(1)->Opcode() == Op_Opaque1, "must be");
aoqi@0 1971 useful_predicates.push(entry->in(0)->in(1)->in(1)); // good one
aoqi@0 1972 entry = entry->in(0)->in(0);
aoqi@0 1973 }
aoqi@0 1974 predicate_proj = find_predicate(entry); // Predicate
aoqi@0 1975 if (predicate_proj != NULL ) {
aoqi@0 1976 useful_predicates.push(entry->in(0)->in(1)->in(1)); // good one
aoqi@0 1977 }
aoqi@0 1978 }
aoqi@0 1979
aoqi@0 1980 if (loop->_next) { // sibling
aoqi@0 1981 collect_potentially_useful_predicates(loop->_next, useful_predicates);
aoqi@0 1982 }
aoqi@0 1983 }
aoqi@0 1984
aoqi@0 1985 //------------------------eliminate_useless_predicates-----------------------------
aoqi@0 1986 // Eliminate all inserted predicates if they could not be used by loop predication.
aoqi@0 1987 // Note: it will also eliminates loop limits check predicate since it also uses
aoqi@0 1988 // Opaque1 node (see Parse::add_predicate()).
aoqi@0 1989 void PhaseIdealLoop::eliminate_useless_predicates() {
aoqi@0 1990 if (C->predicate_count() == 0)
aoqi@0 1991 return; // no predicate left
aoqi@0 1992
aoqi@0 1993 Unique_Node_List useful_predicates; // to store useful predicates
aoqi@0 1994 if (C->has_loops()) {
aoqi@0 1995 collect_potentially_useful_predicates(_ltree_root->_child, useful_predicates);
aoqi@0 1996 }
aoqi@0 1997
aoqi@0 1998 for (int i = C->predicate_count(); i > 0; i--) {
aoqi@0 1999 Node * n = C->predicate_opaque1_node(i-1);
aoqi@0 2000 assert(n->Opcode() == Op_Opaque1, "must be");
aoqi@0 2001 if (!useful_predicates.member(n)) { // not in the useful list
aoqi@0 2002 _igvn.replace_node(n, n->in(1));
aoqi@0 2003 }
aoqi@0 2004 }
aoqi@0 2005 }
aoqi@0 2006
aoqi@0 2007 //------------------------process_expensive_nodes-----------------------------
aoqi@0 2008 // Expensive nodes have their control input set to prevent the GVN
aoqi@0 2009 // from commoning them and as a result forcing the resulting node to
aoqi@0 2010 // be in a more frequent path. Use CFG information here, to change the
aoqi@0 2011 // control inputs so that some expensive nodes can be commoned while
aoqi@0 2012 // not executed more frequently.
aoqi@0 2013 bool PhaseIdealLoop::process_expensive_nodes() {
aoqi@0 2014 assert(OptimizeExpensiveOps, "optimization off?");
aoqi@0 2015
aoqi@0 2016 // Sort nodes to bring similar nodes together
aoqi@0 2017 C->sort_expensive_nodes();
aoqi@0 2018
aoqi@0 2019 bool progress = false;
aoqi@0 2020
aoqi@0 2021 for (int i = 0; i < C->expensive_count(); ) {
aoqi@0 2022 Node* n = C->expensive_node(i);
aoqi@0 2023 int start = i;
aoqi@0 2024 // Find nodes similar to n
aoqi@0 2025 i++;
aoqi@0 2026 for (; i < C->expensive_count() && Compile::cmp_expensive_nodes(n, C->expensive_node(i)) == 0; i++);
aoqi@0 2027 int end = i;
aoqi@0 2028 // And compare them two by two
aoqi@0 2029 for (int j = start; j < end; j++) {
aoqi@0 2030 Node* n1 = C->expensive_node(j);
aoqi@0 2031 if (is_node_unreachable(n1)) {
aoqi@0 2032 continue;
aoqi@0 2033 }
aoqi@0 2034 for (int k = j+1; k < end; k++) {
aoqi@0 2035 Node* n2 = C->expensive_node(k);
aoqi@0 2036 if (is_node_unreachable(n2)) {
aoqi@0 2037 continue;
aoqi@0 2038 }
aoqi@0 2039
aoqi@0 2040 assert(n1 != n2, "should be pair of nodes");
aoqi@0 2041
aoqi@0 2042 Node* c1 = n1->in(0);
aoqi@0 2043 Node* c2 = n2->in(0);
aoqi@0 2044
aoqi@0 2045 Node* parent_c1 = c1;
aoqi@0 2046 Node* parent_c2 = c2;
aoqi@0 2047
aoqi@0 2048 // The call to get_early_ctrl_for_expensive() moves the
aoqi@0 2049 // expensive nodes up but stops at loops that are in a if
aoqi@0 2050 // branch. See whether we can exit the loop and move above the
aoqi@0 2051 // If.
aoqi@0 2052 if (c1->is_Loop()) {
aoqi@0 2053 parent_c1 = c1->in(1);
aoqi@0 2054 }
aoqi@0 2055 if (c2->is_Loop()) {
aoqi@0 2056 parent_c2 = c2->in(1);
aoqi@0 2057 }
aoqi@0 2058
aoqi@0 2059 if (parent_c1 == parent_c2) {
aoqi@0 2060 _igvn._worklist.push(n1);
aoqi@0 2061 _igvn._worklist.push(n2);
aoqi@0 2062 continue;
aoqi@0 2063 }
aoqi@0 2064
aoqi@0 2065 // Look for identical expensive node up the dominator chain.
aoqi@0 2066 if (is_dominator(c1, c2)) {
aoqi@0 2067 c2 = c1;
aoqi@0 2068 } else if (is_dominator(c2, c1)) {
aoqi@0 2069 c1 = c2;
aoqi@0 2070 } else if (parent_c1->is_Proj() && parent_c1->in(0)->is_If() &&
aoqi@0 2071 parent_c2->is_Proj() && parent_c1->in(0) == parent_c2->in(0)) {
aoqi@0 2072 // Both branches have the same expensive node so move it up
aoqi@0 2073 // before the if.
aoqi@0 2074 c1 = c2 = idom(parent_c1->in(0));
aoqi@0 2075 }
aoqi@0 2076 // Do the actual moves
aoqi@0 2077 if (n1->in(0) != c1) {
aoqi@0 2078 _igvn.hash_delete(n1);
aoqi@0 2079 n1->set_req(0, c1);
aoqi@0 2080 _igvn.hash_insert(n1);
aoqi@0 2081 _igvn._worklist.push(n1);
aoqi@0 2082 progress = true;
aoqi@0 2083 }
aoqi@0 2084 if (n2->in(0) != c2) {
aoqi@0 2085 _igvn.hash_delete(n2);
aoqi@0 2086 n2->set_req(0, c2);
aoqi@0 2087 _igvn.hash_insert(n2);
aoqi@0 2088 _igvn._worklist.push(n2);
aoqi@0 2089 progress = true;
aoqi@0 2090 }
aoqi@0 2091 }
aoqi@0 2092 }
aoqi@0 2093 }
aoqi@0 2094
aoqi@0 2095 return progress;
aoqi@0 2096 }
aoqi@0 2097
aoqi@0 2098
aoqi@0 2099 //=============================================================================
aoqi@0 2100 //----------------------------build_and_optimize-------------------------------
aoqi@0 2101 // Create a PhaseLoop. Build the ideal Loop tree. Map each Ideal Node to
aoqi@0 2102 // its corresponding LoopNode. If 'optimize' is true, do some loop cleanups.
aoqi@0 2103 void PhaseIdealLoop::build_and_optimize(bool do_split_ifs, bool skip_loop_opts) {
aoqi@0 2104 ResourceMark rm;
aoqi@0 2105
aoqi@0 2106 int old_progress = C->major_progress();
aoqi@0 2107 uint orig_worklist_size = _igvn._worklist.size();
aoqi@0 2108
aoqi@0 2109 // Reset major-progress flag for the driver's heuristics
aoqi@0 2110 C->clear_major_progress();
aoqi@0 2111
aoqi@0 2112 #ifndef PRODUCT
aoqi@0 2113 // Capture for later assert
aoqi@0 2114 uint unique = C->unique();
aoqi@0 2115 _loop_invokes++;
aoqi@0 2116 _loop_work += unique;
aoqi@0 2117 #endif
aoqi@0 2118
aoqi@0 2119 // True if the method has at least 1 irreducible loop
aoqi@0 2120 _has_irreducible_loops = false;
aoqi@0 2121
aoqi@0 2122 _created_loop_node = false;
aoqi@0 2123
aoqi@0 2124 Arena *a = Thread::current()->resource_area();
aoqi@0 2125 VectorSet visited(a);
aoqi@0 2126 // Pre-grow the mapping from Nodes to IdealLoopTrees.
aoqi@0 2127 _nodes.map(C->unique(), NULL);
aoqi@0 2128 memset(_nodes.adr(), 0, wordSize * C->unique());
aoqi@0 2129
aoqi@0 2130 // Pre-build the top-level outermost loop tree entry
aoqi@0 2131 _ltree_root = new IdealLoopTree( this, C->root(), C->root() );
aoqi@0 2132 // Do not need a safepoint at the top level
aoqi@0 2133 _ltree_root->_has_sfpt = 1;
aoqi@0 2134
aoqi@0 2135 // Initialize Dominators.
aoqi@0 2136 // Checked in clone_loop_predicate() during beautify_loops().
aoqi@0 2137 _idom_size = 0;
aoqi@0 2138 _idom = NULL;
aoqi@0 2139 _dom_depth = NULL;
aoqi@0 2140 _dom_stk = NULL;
aoqi@0 2141
aoqi@0 2142 // Empty pre-order array
aoqi@0 2143 allocate_preorders();
aoqi@0 2144
aoqi@0 2145 // Build a loop tree on the fly. Build a mapping from CFG nodes to
aoqi@0 2146 // IdealLoopTree entries. Data nodes are NOT walked.
aoqi@0 2147 build_loop_tree();
aoqi@0 2148 // Check for bailout, and return
aoqi@0 2149 if (C->failing()) {
aoqi@0 2150 return;
aoqi@0 2151 }
aoqi@0 2152
aoqi@0 2153 // No loops after all
aoqi@0 2154 if( !_ltree_root->_child && !_verify_only ) C->set_has_loops(false);
aoqi@0 2155
aoqi@0 2156 // There should always be an outer loop containing the Root and Return nodes.
aoqi@0 2157 // If not, we have a degenerate empty program. Bail out in this case.
aoqi@0 2158 if (!has_node(C->root())) {
aoqi@0 2159 if (!_verify_only) {
aoqi@0 2160 C->clear_major_progress();
aoqi@0 2161 C->record_method_not_compilable("empty program detected during loop optimization");
aoqi@0 2162 }
aoqi@0 2163 return;
aoqi@0 2164 }
aoqi@0 2165
aoqi@0 2166 // Nothing to do, so get out
aoqi@0 2167 bool stop_early = !C->has_loops() && !skip_loop_opts && !do_split_ifs && !_verify_me && !_verify_only;
aoqi@0 2168 bool do_expensive_nodes = C->should_optimize_expensive_nodes(_igvn);
aoqi@0 2169 if (stop_early && !do_expensive_nodes) {
aoqi@0 2170 _igvn.optimize(); // Cleanup NeverBranches
aoqi@0 2171 return;
aoqi@0 2172 }
aoqi@0 2173
aoqi@0 2174 // Set loop nesting depth
aoqi@0 2175 _ltree_root->set_nest( 0 );
aoqi@0 2176
aoqi@0 2177 // Split shared headers and insert loop landing pads.
aoqi@0 2178 // Do not bother doing this on the Root loop of course.
aoqi@0 2179 if( !_verify_me && !_verify_only && _ltree_root->_child ) {
aoqi@0 2180 C->print_method(PHASE_BEFORE_BEAUTIFY_LOOPS, 3);
aoqi@0 2181 if( _ltree_root->_child->beautify_loops( this ) ) {
aoqi@0 2182 // Re-build loop tree!
aoqi@0 2183 _ltree_root->_child = NULL;
aoqi@0 2184 _nodes.clear();
aoqi@0 2185 reallocate_preorders();
aoqi@0 2186 build_loop_tree();
aoqi@0 2187 // Check for bailout, and return
aoqi@0 2188 if (C->failing()) {
aoqi@0 2189 return;
aoqi@0 2190 }
aoqi@0 2191 // Reset loop nesting depth
aoqi@0 2192 _ltree_root->set_nest( 0 );
aoqi@0 2193
aoqi@0 2194 C->print_method(PHASE_AFTER_BEAUTIFY_LOOPS, 3);
aoqi@0 2195 }
aoqi@0 2196 }
aoqi@0 2197
aoqi@0 2198 // Build Dominators for elision of NULL checks & loop finding.
aoqi@0 2199 // Since nodes do not have a slot for immediate dominator, make
aoqi@0 2200 // a persistent side array for that info indexed on node->_idx.
aoqi@0 2201 _idom_size = C->unique();
aoqi@0 2202 _idom = NEW_RESOURCE_ARRAY( Node*, _idom_size );
aoqi@0 2203 _dom_depth = NEW_RESOURCE_ARRAY( uint, _idom_size );
aoqi@0 2204 _dom_stk = NULL; // Allocated on demand in recompute_dom_depth
aoqi@0 2205 memset( _dom_depth, 0, _idom_size * sizeof(uint) );
aoqi@0 2206
aoqi@0 2207 Dominators();
aoqi@0 2208
aoqi@0 2209 if (!_verify_only) {
aoqi@0 2210 // As a side effect, Dominators removed any unreachable CFG paths
aoqi@0 2211 // into RegionNodes. It doesn't do this test against Root, so
aoqi@0 2212 // we do it here.
aoqi@0 2213 for( uint i = 1; i < C->root()->req(); i++ ) {
aoqi@0 2214 if( !_nodes[C->root()->in(i)->_idx] ) { // Dead path into Root?
aoqi@0 2215 _igvn.delete_input_of(C->root(), i);
aoqi@0 2216 i--; // Rerun same iteration on compressed edges
aoqi@0 2217 }
aoqi@0 2218 }
aoqi@0 2219
aoqi@0 2220 // Given dominators, try to find inner loops with calls that must
aoqi@0 2221 // always be executed (call dominates loop tail). These loops do
aoqi@0 2222 // not need a separate safepoint.
aoqi@0 2223 Node_List cisstack(a);
aoqi@0 2224 _ltree_root->check_safepts(visited, cisstack);
aoqi@0 2225 }
aoqi@0 2226
aoqi@0 2227 // Walk the DATA nodes and place into loops. Find earliest control
aoqi@0 2228 // node. For CFG nodes, the _nodes array starts out and remains
aoqi@0 2229 // holding the associated IdealLoopTree pointer. For DATA nodes, the
aoqi@0 2230 // _nodes array holds the earliest legal controlling CFG node.
aoqi@0 2231
aoqi@0 2232 // Allocate stack with enough space to avoid frequent realloc
aoqi@0 2233 int stack_size = (C->unique() >> 1) + 16; // (unique>>1)+16 from Java2D stats
aoqi@0 2234 Node_Stack nstack( a, stack_size );
aoqi@0 2235
aoqi@0 2236 visited.Clear();
aoqi@0 2237 Node_List worklist(a);
aoqi@0 2238 // Don't need C->root() on worklist since
aoqi@0 2239 // it will be processed among C->top() inputs
aoqi@0 2240 worklist.push( C->top() );
aoqi@0 2241 visited.set( C->top()->_idx ); // Set C->top() as visited now
aoqi@0 2242 build_loop_early( visited, worklist, nstack );
aoqi@0 2243
aoqi@0 2244 // Given early legal placement, try finding counted loops. This placement
aoqi@0 2245 // is good enough to discover most loop invariants.
aoqi@0 2246 if( !_verify_me && !_verify_only )
aoqi@0 2247 _ltree_root->counted_loop( this );
aoqi@0 2248
aoqi@0 2249 // Find latest loop placement. Find ideal loop placement.
aoqi@0 2250 visited.Clear();
aoqi@0 2251 init_dom_lca_tags();
aoqi@0 2252 // Need C->root() on worklist when processing outs
aoqi@0 2253 worklist.push( C->root() );
aoqi@0 2254 NOT_PRODUCT( C->verify_graph_edges(); )
aoqi@0 2255 worklist.push( C->top() );
aoqi@0 2256 build_loop_late( visited, worklist, nstack );
aoqi@0 2257
aoqi@0 2258 if (_verify_only) {
aoqi@0 2259 // restore major progress flag
aoqi@0 2260 for (int i = 0; i < old_progress; i++)
aoqi@0 2261 C->set_major_progress();
aoqi@0 2262 assert(C->unique() == unique, "verification mode made Nodes? ? ?");
aoqi@0 2263 assert(_igvn._worklist.size() == orig_worklist_size, "shouldn't push anything");
aoqi@0 2264 return;
aoqi@0 2265 }
aoqi@0 2266
aoqi@0 2267 // clear out the dead code after build_loop_late
aoqi@0 2268 while (_deadlist.size()) {
aoqi@0 2269 _igvn.remove_globally_dead_node(_deadlist.pop());
aoqi@0 2270 }
aoqi@0 2271
aoqi@0 2272 if (stop_early) {
aoqi@0 2273 assert(do_expensive_nodes, "why are we here?");
aoqi@0 2274 if (process_expensive_nodes()) {
aoqi@0 2275 // If we made some progress when processing expensive nodes then
aoqi@0 2276 // the IGVN may modify the graph in a way that will allow us to
aoqi@0 2277 // make some more progress: we need to try processing expensive
aoqi@0 2278 // nodes again.
aoqi@0 2279 C->set_major_progress();
aoqi@0 2280 }
aoqi@0 2281 _igvn.optimize();
aoqi@0 2282 return;
aoqi@0 2283 }
aoqi@0 2284
aoqi@0 2285 // Some parser-inserted loop predicates could never be used by loop
aoqi@0 2286 // predication or they were moved away from loop during some optimizations.
aoqi@0 2287 // For example, peeling. Eliminate them before next loop optimizations.
aoqi@0 2288 if (UseLoopPredicate || LoopLimitCheck) {
aoqi@0 2289 eliminate_useless_predicates();
aoqi@0 2290 }
aoqi@0 2291
aoqi@0 2292 #ifndef PRODUCT
aoqi@0 2293 C->verify_graph_edges();
aoqi@0 2294 if (_verify_me) { // Nested verify pass?
aoqi@0 2295 // Check to see if the verify mode is broken
aoqi@0 2296 assert(C->unique() == unique, "non-optimize mode made Nodes? ? ?");
aoqi@0 2297 return;
aoqi@0 2298 }
aoqi@0 2299 if(VerifyLoopOptimizations) verify();
aoqi@0 2300 if(TraceLoopOpts && C->has_loops()) {
aoqi@0 2301 _ltree_root->dump();
aoqi@0 2302 }
aoqi@0 2303 #endif
aoqi@0 2304
aoqi@0 2305 if (skip_loop_opts) {
aoqi@0 2306 // Cleanup any modified bits
aoqi@0 2307 _igvn.optimize();
aoqi@0 2308
aoqi@0 2309 if (C->log() != NULL) {
aoqi@0 2310 log_loop_tree(_ltree_root, _ltree_root, C->log());
aoqi@0 2311 }
aoqi@0 2312 return;
aoqi@0 2313 }
aoqi@0 2314
aoqi@0 2315 if (ReassociateInvariants) {
aoqi@0 2316 // Reassociate invariants and prep for split_thru_phi
aoqi@0 2317 for (LoopTreeIterator iter(_ltree_root); !iter.done(); iter.next()) {
aoqi@0 2318 IdealLoopTree* lpt = iter.current();
aoqi@0 2319 if (!lpt->is_counted() || !lpt->is_inner()) continue;
aoqi@0 2320
aoqi@0 2321 lpt->reassociate_invariants(this);
aoqi@0 2322
aoqi@0 2323 // Because RCE opportunities can be masked by split_thru_phi,
aoqi@0 2324 // look for RCE candidates and inhibit split_thru_phi
aoqi@0 2325 // on just their loop-phi's for this pass of loop opts
aoqi@0 2326 if (SplitIfBlocks && do_split_ifs) {
aoqi@0 2327 if (lpt->policy_range_check(this)) {
aoqi@0 2328 lpt->_rce_candidate = 1; // = true
aoqi@0 2329 }
aoqi@0 2330 }
aoqi@0 2331 }
aoqi@0 2332 }
aoqi@0 2333
aoqi@0 2334 // Check for aggressive application of split-if and other transforms
aoqi@0 2335 // that require basic-block info (like cloning through Phi's)
aoqi@0 2336 if( SplitIfBlocks && do_split_ifs ) {
aoqi@0 2337 visited.Clear();
aoqi@0 2338 split_if_with_blocks( visited, nstack );
aoqi@0 2339 NOT_PRODUCT( if( VerifyLoopOptimizations ) verify(); );
aoqi@0 2340 }
aoqi@0 2341
aoqi@0 2342 if (!C->major_progress() && do_expensive_nodes && process_expensive_nodes()) {
aoqi@0 2343 C->set_major_progress();
aoqi@0 2344 }
aoqi@0 2345
aoqi@0 2346 // Perform loop predication before iteration splitting
aoqi@0 2347 if (C->has_loops() && !C->major_progress() && (C->predicate_count() > 0)) {
aoqi@0 2348 _ltree_root->_child->loop_predication(this);
aoqi@0 2349 }
aoqi@0 2350
aoqi@0 2351 if (OptimizeFill && UseLoopPredicate && C->has_loops() && !C->major_progress()) {
aoqi@0 2352 if (do_intrinsify_fill()) {
aoqi@0 2353 C->set_major_progress();
aoqi@0 2354 }
aoqi@0 2355 }
aoqi@0 2356
aoqi@0 2357 // Perform iteration-splitting on inner loops. Split iterations to avoid
aoqi@0 2358 // range checks or one-shot null checks.
aoqi@0 2359
aoqi@0 2360 // If split-if's didn't hack the graph too bad (no CFG changes)
aoqi@0 2361 // then do loop opts.
aoqi@0 2362 if (C->has_loops() && !C->major_progress()) {
aoqi@0 2363 memset( worklist.adr(), 0, worklist.Size()*sizeof(Node*) );
aoqi@0 2364 _ltree_root->_child->iteration_split( this, worklist );
aoqi@0 2365 // No verify after peeling! GCM has hoisted code out of the loop.
aoqi@0 2366 // After peeling, the hoisted code could sink inside the peeled area.
aoqi@0 2367 // The peeling code does not try to recompute the best location for
aoqi@0 2368 // all the code before the peeled area, so the verify pass will always
aoqi@0 2369 // complain about it.
aoqi@0 2370 }
aoqi@0 2371 // Do verify graph edges in any case
aoqi@0 2372 NOT_PRODUCT( C->verify_graph_edges(); );
aoqi@0 2373
aoqi@0 2374 if (!do_split_ifs) {
aoqi@0 2375 // We saw major progress in Split-If to get here. We forced a
aoqi@0 2376 // pass with unrolling and not split-if, however more split-if's
aoqi@0 2377 // might make progress. If the unrolling didn't make progress
aoqi@0 2378 // then the major-progress flag got cleared and we won't try
aoqi@0 2379 // another round of Split-If. In particular the ever-common
aoqi@0 2380 // instance-of/check-cast pattern requires at least 2 rounds of
aoqi@0 2381 // Split-If to clear out.
aoqi@0 2382 C->set_major_progress();
aoqi@0 2383 }
aoqi@0 2384
aoqi@0 2385 // Repeat loop optimizations if new loops were seen
aoqi@0 2386 if (created_loop_node()) {
aoqi@0 2387 C->set_major_progress();
aoqi@0 2388 }
aoqi@0 2389
aoqi@0 2390 // Keep loop predicates and perform optimizations with them
aoqi@0 2391 // until no more loop optimizations could be done.
aoqi@0 2392 // After that switch predicates off and do more loop optimizations.
aoqi@0 2393 if (!C->major_progress() && (C->predicate_count() > 0)) {
aoqi@0 2394 C->cleanup_loop_predicates(_igvn);
aoqi@0 2395 #ifndef PRODUCT
aoqi@0 2396 if (TraceLoopOpts) {
aoqi@0 2397 tty->print_cr("PredicatesOff");
aoqi@0 2398 }
aoqi@0 2399 #endif
aoqi@0 2400 C->set_major_progress();
aoqi@0 2401 }
aoqi@0 2402
aoqi@0 2403 // Convert scalar to superword operations at the end of all loop opts.
aoqi@0 2404 if (UseSuperWord && C->has_loops() && !C->major_progress()) {
aoqi@0 2405 // SuperWord transform
aoqi@0 2406 SuperWord sw(this);
aoqi@0 2407 for (LoopTreeIterator iter(_ltree_root); !iter.done(); iter.next()) {
aoqi@0 2408 IdealLoopTree* lpt = iter.current();
aoqi@0 2409 if (lpt->is_counted()) {
aoqi@0 2410 sw.transform_loop(lpt);
aoqi@0 2411 }
aoqi@0 2412 }
aoqi@0 2413 }
aoqi@0 2414
aoqi@0 2415 // Cleanup any modified bits
aoqi@0 2416 _igvn.optimize();
aoqi@0 2417
aoqi@0 2418 // disable assert until issue with split_flow_path is resolved (6742111)
aoqi@0 2419 // assert(!_has_irreducible_loops || C->parsed_irreducible_loop() || C->is_osr_compilation(),
aoqi@0 2420 // "shouldn't introduce irreducible loops");
aoqi@0 2421
aoqi@0 2422 if (C->log() != NULL) {
aoqi@0 2423 log_loop_tree(_ltree_root, _ltree_root, C->log());
aoqi@0 2424 }
aoqi@0 2425 }
aoqi@0 2426
aoqi@0 2427 #ifndef PRODUCT
aoqi@0 2428 //------------------------------print_statistics-------------------------------
aoqi@0 2429 int PhaseIdealLoop::_loop_invokes=0;// Count of PhaseIdealLoop invokes
aoqi@0 2430 int PhaseIdealLoop::_loop_work=0; // Sum of PhaseIdealLoop x unique
aoqi@0 2431 void PhaseIdealLoop::print_statistics() {
aoqi@0 2432 tty->print_cr("PhaseIdealLoop=%d, sum _unique=%d", _loop_invokes, _loop_work);
aoqi@0 2433 }
aoqi@0 2434
aoqi@0 2435 //------------------------------verify-----------------------------------------
aoqi@0 2436 // Build a verify-only PhaseIdealLoop, and see that it agrees with me.
aoqi@0 2437 static int fail; // debug only, so its multi-thread dont care
aoqi@0 2438 void PhaseIdealLoop::verify() const {
aoqi@0 2439 int old_progress = C->major_progress();
aoqi@0 2440 ResourceMark rm;
aoqi@0 2441 PhaseIdealLoop loop_verify( _igvn, this );
aoqi@0 2442 VectorSet visited(Thread::current()->resource_area());
aoqi@0 2443
aoqi@0 2444 fail = 0;
aoqi@0 2445 verify_compare( C->root(), &loop_verify, visited );
aoqi@0 2446 assert( fail == 0, "verify loops failed" );
aoqi@0 2447 // Verify loop structure is the same
aoqi@0 2448 _ltree_root->verify_tree(loop_verify._ltree_root, NULL);
aoqi@0 2449 // Reset major-progress. It was cleared by creating a verify version of
aoqi@0 2450 // PhaseIdealLoop.
aoqi@0 2451 for( int i=0; i<old_progress; i++ )
aoqi@0 2452 C->set_major_progress();
aoqi@0 2453 }
aoqi@0 2454
aoqi@0 2455 //------------------------------verify_compare---------------------------------
aoqi@0 2456 // Make sure me and the given PhaseIdealLoop agree on key data structures
aoqi@0 2457 void PhaseIdealLoop::verify_compare( Node *n, const PhaseIdealLoop *loop_verify, VectorSet &visited ) const {
aoqi@0 2458 if( !n ) return;
aoqi@0 2459 if( visited.test_set( n->_idx ) ) return;
aoqi@0 2460 if( !_nodes[n->_idx] ) { // Unreachable
aoqi@0 2461 assert( !loop_verify->_nodes[n->_idx], "both should be unreachable" );
aoqi@0 2462 return;
aoqi@0 2463 }
aoqi@0 2464
aoqi@0 2465 uint i;
aoqi@0 2466 for( i = 0; i < n->req(); i++ )
aoqi@0 2467 verify_compare( n->in(i), loop_verify, visited );
aoqi@0 2468
aoqi@0 2469 // Check the '_nodes' block/loop structure
aoqi@0 2470 i = n->_idx;
aoqi@0 2471 if( has_ctrl(n) ) { // We have control; verify has loop or ctrl
aoqi@0 2472 if( _nodes[i] != loop_verify->_nodes[i] &&
aoqi@0 2473 get_ctrl_no_update(n) != loop_verify->get_ctrl_no_update(n) ) {
aoqi@0 2474 tty->print("Mismatched control setting for: ");
aoqi@0 2475 n->dump();
aoqi@0 2476 if( fail++ > 10 ) return;
aoqi@0 2477 Node *c = get_ctrl_no_update(n);
aoqi@0 2478 tty->print("We have it as: ");
aoqi@0 2479 if( c->in(0) ) c->dump();
aoqi@0 2480 else tty->print_cr("N%d",c->_idx);
aoqi@0 2481 tty->print("Verify thinks: ");
aoqi@0 2482 if( loop_verify->has_ctrl(n) )
aoqi@0 2483 loop_verify->get_ctrl_no_update(n)->dump();
aoqi@0 2484 else
aoqi@0 2485 loop_verify->get_loop_idx(n)->dump();
aoqi@0 2486 tty->cr();
aoqi@0 2487 }
aoqi@0 2488 } else { // We have a loop
aoqi@0 2489 IdealLoopTree *us = get_loop_idx(n);
aoqi@0 2490 if( loop_verify->has_ctrl(n) ) {
aoqi@0 2491 tty->print("Mismatched loop setting for: ");
aoqi@0 2492 n->dump();
aoqi@0 2493 if( fail++ > 10 ) return;
aoqi@0 2494 tty->print("We have it as: ");
aoqi@0 2495 us->dump();
aoqi@0 2496 tty->print("Verify thinks: ");
aoqi@0 2497 loop_verify->get_ctrl_no_update(n)->dump();
aoqi@0 2498 tty->cr();
aoqi@0 2499 } else if (!C->major_progress()) {
aoqi@0 2500 // Loop selection can be messed up if we did a major progress
aoqi@0 2501 // operation, like split-if. Do not verify in that case.
aoqi@0 2502 IdealLoopTree *them = loop_verify->get_loop_idx(n);
aoqi@0 2503 if( us->_head != them->_head || us->_tail != them->_tail ) {
aoqi@0 2504 tty->print("Unequals loops for: ");
aoqi@0 2505 n->dump();
aoqi@0 2506 if( fail++ > 10 ) return;
aoqi@0 2507 tty->print("We have it as: ");
aoqi@0 2508 us->dump();
aoqi@0 2509 tty->print("Verify thinks: ");
aoqi@0 2510 them->dump();
aoqi@0 2511 tty->cr();
aoqi@0 2512 }
aoqi@0 2513 }
aoqi@0 2514 }
aoqi@0 2515
aoqi@0 2516 // Check for immediate dominators being equal
aoqi@0 2517 if( i >= _idom_size ) {
aoqi@0 2518 if( !n->is_CFG() ) return;
aoqi@0 2519 tty->print("CFG Node with no idom: ");
aoqi@0 2520 n->dump();
aoqi@0 2521 return;
aoqi@0 2522 }
aoqi@0 2523 if( !n->is_CFG() ) return;
aoqi@0 2524 if( n == C->root() ) return; // No IDOM here
aoqi@0 2525
aoqi@0 2526 assert(n->_idx == i, "sanity");
aoqi@0 2527 Node *id = idom_no_update(n);
aoqi@0 2528 if( id != loop_verify->idom_no_update(n) ) {
aoqi@0 2529 tty->print("Unequals idoms for: ");
aoqi@0 2530 n->dump();
aoqi@0 2531 if( fail++ > 10 ) return;
aoqi@0 2532 tty->print("We have it as: ");
aoqi@0 2533 id->dump();
aoqi@0 2534 tty->print("Verify thinks: ");
aoqi@0 2535 loop_verify->idom_no_update(n)->dump();
aoqi@0 2536 tty->cr();
aoqi@0 2537 }
aoqi@0 2538
aoqi@0 2539 }
aoqi@0 2540
aoqi@0 2541 //------------------------------verify_tree------------------------------------
aoqi@0 2542 // Verify that tree structures match. Because the CFG can change, siblings
aoqi@0 2543 // within the loop tree can be reordered. We attempt to deal with that by
aoqi@0 2544 // reordering the verify's loop tree if possible.
aoqi@0 2545 void IdealLoopTree::verify_tree(IdealLoopTree *loop, const IdealLoopTree *parent) const {
aoqi@0 2546 assert( _parent == parent, "Badly formed loop tree" );
aoqi@0 2547
aoqi@0 2548 // Siblings not in same order? Attempt to re-order.
aoqi@0 2549 if( _head != loop->_head ) {
aoqi@0 2550 // Find _next pointer to update
aoqi@0 2551 IdealLoopTree **pp = &loop->_parent->_child;
aoqi@0 2552 while( *pp != loop )
aoqi@0 2553 pp = &((*pp)->_next);
aoqi@0 2554 // Find proper sibling to be next
aoqi@0 2555 IdealLoopTree **nn = &loop->_next;
aoqi@0 2556 while( (*nn) && (*nn)->_head != _head )
aoqi@0 2557 nn = &((*nn)->_next);
aoqi@0 2558
aoqi@0 2559 // Check for no match.
aoqi@0 2560 if( !(*nn) ) {
aoqi@0 2561 // Annoyingly, irreducible loops can pick different headers
aoqi@0 2562 // after a major_progress operation, so the rest of the loop
aoqi@0 2563 // tree cannot be matched.
aoqi@0 2564 if (_irreducible && Compile::current()->major_progress()) return;
aoqi@0 2565 assert( 0, "failed to match loop tree" );
aoqi@0 2566 }
aoqi@0 2567
aoqi@0 2568 // Move (*nn) to (*pp)
aoqi@0 2569 IdealLoopTree *hit = *nn;
aoqi@0 2570 *nn = hit->_next;
aoqi@0 2571 hit->_next = loop;
aoqi@0 2572 *pp = loop;
aoqi@0 2573 loop = hit;
aoqi@0 2574 // Now try again to verify
aoqi@0 2575 }
aoqi@0 2576
aoqi@0 2577 assert( _head == loop->_head , "mismatched loop head" );
aoqi@0 2578 Node *tail = _tail; // Inline a non-updating version of
aoqi@0 2579 while( !tail->in(0) ) // the 'tail()' call.
aoqi@0 2580 tail = tail->in(1);
aoqi@0 2581 assert( tail == loop->_tail, "mismatched loop tail" );
aoqi@0 2582
aoqi@0 2583 // Counted loops that are guarded should be able to find their guards
aoqi@0 2584 if( _head->is_CountedLoop() && _head->as_CountedLoop()->is_main_loop() ) {
aoqi@0 2585 CountedLoopNode *cl = _head->as_CountedLoop();
aoqi@0 2586 Node *init = cl->init_trip();
aoqi@0 2587 Node *ctrl = cl->in(LoopNode::EntryControl);
aoqi@0 2588 assert( ctrl->Opcode() == Op_IfTrue || ctrl->Opcode() == Op_IfFalse, "" );
aoqi@0 2589 Node *iff = ctrl->in(0);
aoqi@0 2590 assert( iff->Opcode() == Op_If, "" );
aoqi@0 2591 Node *bol = iff->in(1);
aoqi@0 2592 assert( bol->Opcode() == Op_Bool, "" );
aoqi@0 2593 Node *cmp = bol->in(1);
aoqi@0 2594 assert( cmp->Opcode() == Op_CmpI, "" );
aoqi@0 2595 Node *add = cmp->in(1);
aoqi@0 2596 Node *opaq;
aoqi@0 2597 if( add->Opcode() == Op_Opaque1 ) {
aoqi@0 2598 opaq = add;
aoqi@0 2599 } else {
aoqi@0 2600 assert( add->Opcode() == Op_AddI || add->Opcode() == Op_ConI , "" );
aoqi@0 2601 assert( add == init, "" );
aoqi@0 2602 opaq = cmp->in(2);
aoqi@0 2603 }
aoqi@0 2604 assert( opaq->Opcode() == Op_Opaque1, "" );
aoqi@0 2605
aoqi@0 2606 }
aoqi@0 2607
aoqi@0 2608 if (_child != NULL) _child->verify_tree(loop->_child, this);
aoqi@0 2609 if (_next != NULL) _next ->verify_tree(loop->_next, parent);
aoqi@0 2610 // Innermost loops need to verify loop bodies,
aoqi@0 2611 // but only if no 'major_progress'
aoqi@0 2612 int fail = 0;
aoqi@0 2613 if (!Compile::current()->major_progress() && _child == NULL) {
aoqi@0 2614 for( uint i = 0; i < _body.size(); i++ ) {
aoqi@0 2615 Node *n = _body.at(i);
aoqi@0 2616 if (n->outcnt() == 0) continue; // Ignore dead
aoqi@0 2617 uint j;
aoqi@0 2618 for( j = 0; j < loop->_body.size(); j++ )
aoqi@0 2619 if( loop->_body.at(j) == n )
aoqi@0 2620 break;
aoqi@0 2621 if( j == loop->_body.size() ) { // Not found in loop body
aoqi@0 2622 // Last ditch effort to avoid assertion: Its possible that we
aoqi@0 2623 // have some users (so outcnt not zero) but are still dead.
aoqi@0 2624 // Try to find from root.
aoqi@0 2625 if (Compile::current()->root()->find(n->_idx)) {
aoqi@0 2626 fail++;
aoqi@0 2627 tty->print("We have that verify does not: ");
aoqi@0 2628 n->dump();
aoqi@0 2629 }
aoqi@0 2630 }
aoqi@0 2631 }
aoqi@0 2632 for( uint i2 = 0; i2 < loop->_body.size(); i2++ ) {
aoqi@0 2633 Node *n = loop->_body.at(i2);
aoqi@0 2634 if (n->outcnt() == 0) continue; // Ignore dead
aoqi@0 2635 uint j;
aoqi@0 2636 for( j = 0; j < _body.size(); j++ )
aoqi@0 2637 if( _body.at(j) == n )
aoqi@0 2638 break;
aoqi@0 2639 if( j == _body.size() ) { // Not found in loop body
aoqi@0 2640 // Last ditch effort to avoid assertion: Its possible that we
aoqi@0 2641 // have some users (so outcnt not zero) but are still dead.
aoqi@0 2642 // Try to find from root.
aoqi@0 2643 if (Compile::current()->root()->find(n->_idx)) {
aoqi@0 2644 fail++;
aoqi@0 2645 tty->print("Verify has that we do not: ");
aoqi@0 2646 n->dump();
aoqi@0 2647 }
aoqi@0 2648 }
aoqi@0 2649 }
aoqi@0 2650 assert( !fail, "loop body mismatch" );
aoqi@0 2651 }
aoqi@0 2652 }
aoqi@0 2653
aoqi@0 2654 #endif
aoqi@0 2655
aoqi@0 2656 //------------------------------set_idom---------------------------------------
aoqi@0 2657 void PhaseIdealLoop::set_idom(Node* d, Node* n, uint dom_depth) {
aoqi@0 2658 uint idx = d->_idx;
aoqi@0 2659 if (idx >= _idom_size) {
aoqi@0 2660 uint newsize = _idom_size<<1;
aoqi@0 2661 while( idx >= newsize ) {
aoqi@0 2662 newsize <<= 1;
aoqi@0 2663 }
aoqi@0 2664 _idom = REALLOC_RESOURCE_ARRAY( Node*, _idom,_idom_size,newsize);
aoqi@0 2665 _dom_depth = REALLOC_RESOURCE_ARRAY( uint, _dom_depth,_idom_size,newsize);
aoqi@0 2666 memset( _dom_depth + _idom_size, 0, (newsize - _idom_size) * sizeof(uint) );
aoqi@0 2667 _idom_size = newsize;
aoqi@0 2668 }
aoqi@0 2669 _idom[idx] = n;
aoqi@0 2670 _dom_depth[idx] = dom_depth;
aoqi@0 2671 }
aoqi@0 2672
aoqi@0 2673 //------------------------------recompute_dom_depth---------------------------------------
aoqi@0 2674 // The dominator tree is constructed with only parent pointers.
aoqi@0 2675 // This recomputes the depth in the tree by first tagging all
aoqi@0 2676 // nodes as "no depth yet" marker. The next pass then runs up
aoqi@0 2677 // the dom tree from each node marked "no depth yet", and computes
aoqi@0 2678 // the depth on the way back down.
aoqi@0 2679 void PhaseIdealLoop::recompute_dom_depth() {
aoqi@0 2680 uint no_depth_marker = C->unique();
aoqi@0 2681 uint i;
aoqi@0 2682 // Initialize depth to "no depth yet"
aoqi@0 2683 for (i = 0; i < _idom_size; i++) {
aoqi@0 2684 if (_dom_depth[i] > 0 && _idom[i] != NULL) {
aoqi@0 2685 _dom_depth[i] = no_depth_marker;
aoqi@0 2686 }
aoqi@0 2687 }
aoqi@0 2688 if (_dom_stk == NULL) {
aoqi@0 2689 uint init_size = C->unique() / 100; // Guess that 1/100 is a reasonable initial size.
aoqi@0 2690 if (init_size < 10) init_size = 10;
aoqi@0 2691 _dom_stk = new GrowableArray<uint>(init_size);
aoqi@0 2692 }
aoqi@0 2693 // Compute new depth for each node.
aoqi@0 2694 for (i = 0; i < _idom_size; i++) {
aoqi@0 2695 uint j = i;
aoqi@0 2696 // Run up the dom tree to find a node with a depth
aoqi@0 2697 while (_dom_depth[j] == no_depth_marker) {
aoqi@0 2698 _dom_stk->push(j);
aoqi@0 2699 j = _idom[j]->_idx;
aoqi@0 2700 }
aoqi@0 2701 // Compute the depth on the way back down this tree branch
aoqi@0 2702 uint dd = _dom_depth[j] + 1;
aoqi@0 2703 while (_dom_stk->length() > 0) {
aoqi@0 2704 uint j = _dom_stk->pop();
aoqi@0 2705 _dom_depth[j] = dd;
aoqi@0 2706 dd++;
aoqi@0 2707 }
aoqi@0 2708 }
aoqi@0 2709 }
aoqi@0 2710
aoqi@0 2711 //------------------------------sort-------------------------------------------
aoqi@0 2712 // Insert 'loop' into the existing loop tree. 'innermost' is a leaf of the
aoqi@0 2713 // loop tree, not the root.
aoqi@0 2714 IdealLoopTree *PhaseIdealLoop::sort( IdealLoopTree *loop, IdealLoopTree *innermost ) {
aoqi@0 2715 if( !innermost ) return loop; // New innermost loop
aoqi@0 2716
aoqi@0 2717 int loop_preorder = get_preorder(loop->_head); // Cache pre-order number
aoqi@0 2718 assert( loop_preorder, "not yet post-walked loop" );
aoqi@0 2719 IdealLoopTree **pp = &innermost; // Pointer to previous next-pointer
aoqi@0 2720 IdealLoopTree *l = *pp; // Do I go before or after 'l'?
aoqi@0 2721
aoqi@0 2722 // Insert at start of list
aoqi@0 2723 while( l ) { // Insertion sort based on pre-order
aoqi@0 2724 if( l == loop ) return innermost; // Already on list!
aoqi@0 2725 int l_preorder = get_preorder(l->_head); // Cache pre-order number
aoqi@0 2726 assert( l_preorder, "not yet post-walked l" );
aoqi@0 2727 // Check header pre-order number to figure proper nesting
aoqi@0 2728 if( loop_preorder > l_preorder )
aoqi@0 2729 break; // End of insertion
aoqi@0 2730 // If headers tie (e.g., shared headers) check tail pre-order numbers.
aoqi@0 2731 // Since I split shared headers, you'd think this could not happen.
aoqi@0 2732 // BUT: I must first do the preorder numbering before I can discover I
aoqi@0 2733 // have shared headers, so the split headers all get the same preorder
aoqi@0 2734 // number as the RegionNode they split from.
aoqi@0 2735 if( loop_preorder == l_preorder &&
aoqi@0 2736 get_preorder(loop->_tail) < get_preorder(l->_tail) )
aoqi@0 2737 break; // Also check for shared headers (same pre#)
aoqi@0 2738 pp = &l->_parent; // Chain up list
aoqi@0 2739 l = *pp;
aoqi@0 2740 }
aoqi@0 2741 // Link into list
aoqi@0 2742 // Point predecessor to me
aoqi@0 2743 *pp = loop;
aoqi@0 2744 // Point me to successor
aoqi@0 2745 IdealLoopTree *p = loop->_parent;
aoqi@0 2746 loop->_parent = l; // Point me to successor
aoqi@0 2747 if( p ) sort( p, innermost ); // Insert my parents into list as well
aoqi@0 2748 return innermost;
aoqi@0 2749 }
aoqi@0 2750
aoqi@0 2751 //------------------------------build_loop_tree--------------------------------
aoqi@0 2752 // I use a modified Vick/Tarjan algorithm. I need pre- and a post- visit
aoqi@0 2753 // bits. The _nodes[] array is mapped by Node index and holds a NULL for
aoqi@0 2754 // not-yet-pre-walked, pre-order # for pre-but-not-post-walked and holds the
aoqi@0 2755 // tightest enclosing IdealLoopTree for post-walked.
aoqi@0 2756 //
aoqi@0 2757 // During my forward walk I do a short 1-layer lookahead to see if I can find
aoqi@0 2758 // a loop backedge with that doesn't have any work on the backedge. This
aoqi@0 2759 // helps me construct nested loops with shared headers better.
aoqi@0 2760 //
aoqi@0 2761 // Once I've done the forward recursion, I do the post-work. For each child
aoqi@0 2762 // I check to see if there is a backedge. Backedges define a loop! I
aoqi@0 2763 // insert an IdealLoopTree at the target of the backedge.
aoqi@0 2764 //
aoqi@0 2765 // During the post-work I also check to see if I have several children
aoqi@0 2766 // belonging to different loops. If so, then this Node is a decision point
aoqi@0 2767 // where control flow can choose to change loop nests. It is at this
aoqi@0 2768 // decision point where I can figure out how loops are nested. At this
aoqi@0 2769 // time I can properly order the different loop nests from my children.
aoqi@0 2770 // Note that there may not be any backedges at the decision point!
aoqi@0 2771 //
aoqi@0 2772 // Since the decision point can be far removed from the backedges, I can't
aoqi@0 2773 // order my loops at the time I discover them. Thus at the decision point
aoqi@0 2774 // I need to inspect loop header pre-order numbers to properly nest my
aoqi@0 2775 // loops. This means I need to sort my childrens' loops by pre-order.
aoqi@0 2776 // The sort is of size number-of-control-children, which generally limits
aoqi@0 2777 // it to size 2 (i.e., I just choose between my 2 target loops).
aoqi@0 2778 void PhaseIdealLoop::build_loop_tree() {
aoqi@0 2779 // Allocate stack of size C->unique()/2 to avoid frequent realloc
aoqi@0 2780 GrowableArray <Node *> bltstack(C->unique() >> 1);
aoqi@0 2781 Node *n = C->root();
aoqi@0 2782 bltstack.push(n);
aoqi@0 2783 int pre_order = 1;
aoqi@0 2784 int stack_size;
aoqi@0 2785
aoqi@0 2786 while ( ( stack_size = bltstack.length() ) != 0 ) {
aoqi@0 2787 n = bltstack.top(); // Leave node on stack
aoqi@0 2788 if ( !is_visited(n) ) {
aoqi@0 2789 // ---- Pre-pass Work ----
aoqi@0 2790 // Pre-walked but not post-walked nodes need a pre_order number.
aoqi@0 2791
aoqi@0 2792 set_preorder_visited( n, pre_order ); // set as visited
aoqi@0 2793
aoqi@0 2794 // ---- Scan over children ----
aoqi@0 2795 // Scan first over control projections that lead to loop headers.
aoqi@0 2796 // This helps us find inner-to-outer loops with shared headers better.
aoqi@0 2797
aoqi@0 2798 // Scan children's children for loop headers.
aoqi@0 2799 for ( int i = n->outcnt() - 1; i >= 0; --i ) {
aoqi@0 2800 Node* m = n->raw_out(i); // Child
aoqi@0 2801 if( m->is_CFG() && !is_visited(m) ) { // Only for CFG children
aoqi@0 2802 // Scan over children's children to find loop
aoqi@0 2803 for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
aoqi@0 2804 Node* l = m->fast_out(j);
aoqi@0 2805 if( is_visited(l) && // Been visited?
aoqi@0 2806 !is_postvisited(l) && // But not post-visited
aoqi@0 2807 get_preorder(l) < pre_order ) { // And smaller pre-order
aoqi@0 2808 // Found! Scan the DFS down this path before doing other paths
aoqi@0 2809 bltstack.push(m);
aoqi@0 2810 break;
aoqi@0 2811 }
aoqi@0 2812 }
aoqi@0 2813 }
aoqi@0 2814 }
aoqi@0 2815 pre_order++;
aoqi@0 2816 }
aoqi@0 2817 else if ( !is_postvisited(n) ) {
aoqi@0 2818 // Note: build_loop_tree_impl() adds out edges on rare occasions,
aoqi@0 2819 // such as com.sun.rsasign.am::a.
aoqi@0 2820 // For non-recursive version, first, process current children.
aoqi@0 2821 // On next iteration, check if additional children were added.
aoqi@0 2822 for ( int k = n->outcnt() - 1; k >= 0; --k ) {
aoqi@0 2823 Node* u = n->raw_out(k);
aoqi@0 2824 if ( u->is_CFG() && !is_visited(u) ) {
aoqi@0 2825 bltstack.push(u);
aoqi@0 2826 }
aoqi@0 2827 }
aoqi@0 2828 if ( bltstack.length() == stack_size ) {
aoqi@0 2829 // There were no additional children, post visit node now
aoqi@0 2830 (void)bltstack.pop(); // Remove node from stack
aoqi@0 2831 pre_order = build_loop_tree_impl( n, pre_order );
aoqi@0 2832 // Check for bailout
aoqi@0 2833 if (C->failing()) {
aoqi@0 2834 return;
aoqi@0 2835 }
aoqi@0 2836 // Check to grow _preorders[] array for the case when
aoqi@0 2837 // build_loop_tree_impl() adds new nodes.
aoqi@0 2838 check_grow_preorders();
aoqi@0 2839 }
aoqi@0 2840 }
aoqi@0 2841 else {
aoqi@0 2842 (void)bltstack.pop(); // Remove post-visited node from stack
aoqi@0 2843 }
aoqi@0 2844 }
aoqi@0 2845 }
aoqi@0 2846
aoqi@0 2847 //------------------------------build_loop_tree_impl---------------------------
aoqi@0 2848 int PhaseIdealLoop::build_loop_tree_impl( Node *n, int pre_order ) {
aoqi@0 2849 // ---- Post-pass Work ----
aoqi@0 2850 // Pre-walked but not post-walked nodes need a pre_order number.
aoqi@0 2851
aoqi@0 2852 // Tightest enclosing loop for this Node
aoqi@0 2853 IdealLoopTree *innermost = NULL;
aoqi@0 2854
aoqi@0 2855 // For all children, see if any edge is a backedge. If so, make a loop
aoqi@0 2856 // for it. Then find the tightest enclosing loop for the self Node.
aoqi@0 2857 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
aoqi@0 2858 Node* m = n->fast_out(i); // Child
aoqi@0 2859 if( n == m ) continue; // Ignore control self-cycles
aoqi@0 2860 if( !m->is_CFG() ) continue;// Ignore non-CFG edges
aoqi@0 2861
aoqi@0 2862 IdealLoopTree *l; // Child's loop
aoqi@0 2863 if( !is_postvisited(m) ) { // Child visited but not post-visited?
aoqi@0 2864 // Found a backedge
aoqi@0 2865 assert( get_preorder(m) < pre_order, "should be backedge" );
aoqi@0 2866 // Check for the RootNode, which is already a LoopNode and is allowed
aoqi@0 2867 // to have multiple "backedges".
aoqi@0 2868 if( m == C->root()) { // Found the root?
aoqi@0 2869 l = _ltree_root; // Root is the outermost LoopNode
aoqi@0 2870 } else { // Else found a nested loop
aoqi@0 2871 // Insert a LoopNode to mark this loop.
aoqi@0 2872 l = new IdealLoopTree(this, m, n);
aoqi@0 2873 } // End of Else found a nested loop
aoqi@0 2874 if( !has_loop(m) ) // If 'm' does not already have a loop set
aoqi@0 2875 set_loop(m, l); // Set loop header to loop now
aoqi@0 2876
aoqi@0 2877 } else { // Else not a nested loop
aoqi@0 2878 if( !_nodes[m->_idx] ) continue; // Dead code has no loop
aoqi@0 2879 l = get_loop(m); // Get previously determined loop
aoqi@0 2880 // If successor is header of a loop (nest), move up-loop till it
aoqi@0 2881 // is a member of some outer enclosing loop. Since there are no
aoqi@0 2882 // shared headers (I've split them already) I only need to go up
aoqi@0 2883 // at most 1 level.
aoqi@0 2884 while( l && l->_head == m ) // Successor heads loop?
aoqi@0 2885 l = l->_parent; // Move up 1 for me
aoqi@0 2886 // If this loop is not properly parented, then this loop
aoqi@0 2887 // has no exit path out, i.e. its an infinite loop.
aoqi@0 2888 if( !l ) {
aoqi@0 2889 // Make loop "reachable" from root so the CFG is reachable. Basically
aoqi@0 2890 // insert a bogus loop exit that is never taken. 'm', the loop head,
aoqi@0 2891 // points to 'n', one (of possibly many) fall-in paths. There may be
aoqi@0 2892 // many backedges as well.
aoqi@0 2893
aoqi@0 2894 // Here I set the loop to be the root loop. I could have, after
aoqi@0 2895 // inserting a bogus loop exit, restarted the recursion and found my
aoqi@0 2896 // new loop exit. This would make the infinite loop a first-class
aoqi@0 2897 // loop and it would then get properly optimized. What's the use of
aoqi@0 2898 // optimizing an infinite loop?
aoqi@0 2899 l = _ltree_root; // Oops, found infinite loop
aoqi@0 2900
aoqi@0 2901 if (!_verify_only) {
aoqi@0 2902 // Insert the NeverBranch between 'm' and it's control user.
aoqi@0 2903 NeverBranchNode *iff = new (C) NeverBranchNode( m );
aoqi@0 2904 _igvn.register_new_node_with_optimizer(iff);
aoqi@0 2905 set_loop(iff, l);
aoqi@0 2906 Node *if_t = new (C) CProjNode( iff, 0 );
aoqi@0 2907 _igvn.register_new_node_with_optimizer(if_t);
aoqi@0 2908 set_loop(if_t, l);
aoqi@0 2909
aoqi@0 2910 Node* cfg = NULL; // Find the One True Control User of m
aoqi@0 2911 for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
aoqi@0 2912 Node* x = m->fast_out(j);
aoqi@0 2913 if (x->is_CFG() && x != m && x != iff)
aoqi@0 2914 { cfg = x; break; }
aoqi@0 2915 }
aoqi@0 2916 assert(cfg != NULL, "must find the control user of m");
aoqi@0 2917 uint k = 0; // Probably cfg->in(0)
aoqi@0 2918 while( cfg->in(k) != m ) k++; // But check incase cfg is a Region
aoqi@0 2919 cfg->set_req( k, if_t ); // Now point to NeverBranch
aoqi@0 2920
aoqi@0 2921 // Now create the never-taken loop exit
aoqi@0 2922 Node *if_f = new (C) CProjNode( iff, 1 );
aoqi@0 2923 _igvn.register_new_node_with_optimizer(if_f);
aoqi@0 2924 set_loop(if_f, l);
aoqi@0 2925 // Find frame ptr for Halt. Relies on the optimizer
aoqi@0 2926 // V-N'ing. Easier and quicker than searching through
aoqi@0 2927 // the program structure.
aoqi@0 2928 Node *frame = new (C) ParmNode( C->start(), TypeFunc::FramePtr );
aoqi@0 2929 _igvn.register_new_node_with_optimizer(frame);
aoqi@0 2930 // Halt & Catch Fire
aoqi@0 2931 Node *halt = new (C) HaltNode( if_f, frame );
aoqi@0 2932 _igvn.register_new_node_with_optimizer(halt);
aoqi@0 2933 set_loop(halt, l);
aoqi@0 2934 C->root()->add_req(halt);
aoqi@0 2935 }
aoqi@0 2936 set_loop(C->root(), _ltree_root);
aoqi@0 2937 }
aoqi@0 2938 }
aoqi@0 2939 // Weeny check for irreducible. This child was already visited (this
aoqi@0 2940 // IS the post-work phase). Is this child's loop header post-visited
aoqi@0 2941 // as well? If so, then I found another entry into the loop.
aoqi@0 2942 if (!_verify_only) {
aoqi@0 2943 while( is_postvisited(l->_head) ) {
aoqi@0 2944 // found irreducible
aoqi@0 2945 l->_irreducible = 1; // = true
aoqi@0 2946 l = l->_parent;
aoqi@0 2947 _has_irreducible_loops = true;
aoqi@0 2948 // Check for bad CFG here to prevent crash, and bailout of compile
aoqi@0 2949 if (l == NULL) {
aoqi@0 2950 C->record_method_not_compilable("unhandled CFG detected during loop optimization");
aoqi@0 2951 return pre_order;
aoqi@0 2952 }
aoqi@0 2953 }
aoqi@0 2954 C->set_has_irreducible_loop(_has_irreducible_loops);
aoqi@0 2955 }
aoqi@0 2956
aoqi@0 2957 // This Node might be a decision point for loops. It is only if
aoqi@0 2958 // it's children belong to several different loops. The sort call
aoqi@0 2959 // does a trivial amount of work if there is only 1 child or all
aoqi@0 2960 // children belong to the same loop. If however, the children
aoqi@0 2961 // belong to different loops, the sort call will properly set the
aoqi@0 2962 // _parent pointers to show how the loops nest.
aoqi@0 2963 //
aoqi@0 2964 // In any case, it returns the tightest enclosing loop.
aoqi@0 2965 innermost = sort( l, innermost );
aoqi@0 2966 }
aoqi@0 2967
aoqi@0 2968 // Def-use info will have some dead stuff; dead stuff will have no
aoqi@0 2969 // loop decided on.
aoqi@0 2970
aoqi@0 2971 // Am I a loop header? If so fix up my parent's child and next ptrs.
aoqi@0 2972 if( innermost && innermost->_head == n ) {
aoqi@0 2973 assert( get_loop(n) == innermost, "" );
aoqi@0 2974 IdealLoopTree *p = innermost->_parent;
aoqi@0 2975 IdealLoopTree *l = innermost;
aoqi@0 2976 while( p && l->_head == n ) {
aoqi@0 2977 l->_next = p->_child; // Put self on parents 'next child'
aoqi@0 2978 p->_child = l; // Make self as first child of parent
aoqi@0 2979 l = p; // Now walk up the parent chain
aoqi@0 2980 p = l->_parent;
aoqi@0 2981 }
aoqi@0 2982 } else {
aoqi@0 2983 // Note that it is possible for a LoopNode to reach here, if the
aoqi@0 2984 // backedge has been made unreachable (hence the LoopNode no longer
aoqi@0 2985 // denotes a Loop, and will eventually be removed).
aoqi@0 2986
aoqi@0 2987 // Record tightest enclosing loop for self. Mark as post-visited.
aoqi@0 2988 set_loop(n, innermost);
aoqi@0 2989 // Also record has_call flag early on
aoqi@0 2990 if( innermost ) {
aoqi@0 2991 if( n->is_Call() && !n->is_CallLeaf() && !n->is_macro() ) {
aoqi@0 2992 // Do not count uncommon calls
aoqi@0 2993 if( !n->is_CallStaticJava() || !n->as_CallStaticJava()->_name ) {
aoqi@0 2994 Node *iff = n->in(0)->in(0);
aoqi@0 2995 // No any calls for vectorized loops.
aoqi@0 2996 if( UseSuperWord || !iff->is_If() ||
aoqi@0 2997 (n->in(0)->Opcode() == Op_IfFalse &&
aoqi@0 2998 (1.0 - iff->as_If()->_prob) >= 0.01) ||
aoqi@0 2999 (iff->as_If()->_prob >= 0.01) )
aoqi@0 3000 innermost->_has_call = 1;
aoqi@0 3001 }
aoqi@0 3002 } else if( n->is_Allocate() && n->as_Allocate()->_is_scalar_replaceable ) {
aoqi@0 3003 // Disable loop optimizations if the loop has a scalar replaceable
aoqi@0 3004 // allocation. This disabling may cause a potential performance lost
aoqi@0 3005 // if the allocation is not eliminated for some reason.
aoqi@0 3006 innermost->_allow_optimizations = false;
aoqi@0 3007 innermost->_has_call = 1; // = true
aoqi@0 3008 } else if (n->Opcode() == Op_SafePoint) {
aoqi@0 3009 // Record all safepoints in this loop.
aoqi@0 3010 if (innermost->_safepts == NULL) innermost->_safepts = new Node_List();
aoqi@0 3011 innermost->_safepts->push(n);
aoqi@0 3012 }
aoqi@0 3013 }
aoqi@0 3014 }
aoqi@0 3015
aoqi@0 3016 // Flag as post-visited now
aoqi@0 3017 set_postvisited(n);
aoqi@0 3018 return pre_order;
aoqi@0 3019 }
aoqi@0 3020
aoqi@0 3021
aoqi@0 3022 //------------------------------build_loop_early-------------------------------
aoqi@0 3023 // Put Data nodes into some loop nest, by setting the _nodes[]->loop mapping.
aoqi@0 3024 // First pass computes the earliest controlling node possible. This is the
aoqi@0 3025 // controlling input with the deepest dominating depth.
aoqi@0 3026 void PhaseIdealLoop::build_loop_early( VectorSet &visited, Node_List &worklist, Node_Stack &nstack ) {
aoqi@0 3027 while (worklist.size() != 0) {
aoqi@0 3028 // Use local variables nstack_top_n & nstack_top_i to cache values
aoqi@0 3029 // on nstack's top.
aoqi@0 3030 Node *nstack_top_n = worklist.pop();
aoqi@0 3031 uint nstack_top_i = 0;
aoqi@0 3032 //while_nstack_nonempty:
aoqi@0 3033 while (true) {
aoqi@0 3034 // Get parent node and next input's index from stack's top.
aoqi@0 3035 Node *n = nstack_top_n;
aoqi@0 3036 uint i = nstack_top_i;
aoqi@0 3037 uint cnt = n->req(); // Count of inputs
aoqi@0 3038 if (i == 0) { // Pre-process the node.
aoqi@0 3039 if( has_node(n) && // Have either loop or control already?
aoqi@0 3040 !has_ctrl(n) ) { // Have loop picked out already?
aoqi@0 3041 // During "merge_many_backedges" we fold up several nested loops
aoqi@0 3042 // into a single loop. This makes the members of the original
aoqi@0 3043 // loop bodies pointing to dead loops; they need to move up
aoqi@0 3044 // to the new UNION'd larger loop. I set the _head field of these
aoqi@0 3045 // dead loops to NULL and the _parent field points to the owning
aoqi@0 3046 // loop. Shades of UNION-FIND algorithm.
aoqi@0 3047 IdealLoopTree *ilt;
aoqi@0 3048 while( !(ilt = get_loop(n))->_head ) {
aoqi@0 3049 // Normally I would use a set_loop here. But in this one special
aoqi@0 3050 // case, it is legal (and expected) to change what loop a Node
aoqi@0 3051 // belongs to.
aoqi@0 3052 _nodes.map(n->_idx, (Node*)(ilt->_parent) );
aoqi@0 3053 }
aoqi@0 3054 // Remove safepoints ONLY if I've already seen I don't need one.
aoqi@0 3055 // (the old code here would yank a 2nd safepoint after seeing a
aoqi@0 3056 // first one, even though the 1st did not dominate in the loop body
aoqi@0 3057 // and thus could be avoided indefinitely)
aoqi@0 3058 if( !_verify_only && !_verify_me && ilt->_has_sfpt && n->Opcode() == Op_SafePoint &&
aoqi@0 3059 is_deleteable_safept(n)) {
aoqi@0 3060 Node *in = n->in(TypeFunc::Control);
aoqi@0 3061 lazy_replace(n,in); // Pull safepoint now
aoqi@0 3062 if (ilt->_safepts != NULL) {
aoqi@0 3063 ilt->_safepts->yank(n);
aoqi@0 3064 }
aoqi@0 3065 // Carry on with the recursion "as if" we are walking
aoqi@0 3066 // only the control input
aoqi@0 3067 if( !visited.test_set( in->_idx ) ) {
aoqi@0 3068 worklist.push(in); // Visit this guy later, using worklist
aoqi@0 3069 }
aoqi@0 3070 // Get next node from nstack:
aoqi@0 3071 // - skip n's inputs processing by setting i > cnt;
aoqi@0 3072 // - we also will not call set_early_ctrl(n) since
aoqi@0 3073 // has_node(n) == true (see the condition above).
aoqi@0 3074 i = cnt + 1;
aoqi@0 3075 }
aoqi@0 3076 }
aoqi@0 3077 } // if (i == 0)
aoqi@0 3078
aoqi@0 3079 // Visit all inputs
aoqi@0 3080 bool done = true; // Assume all n's inputs will be processed
aoqi@0 3081 while (i < cnt) {
aoqi@0 3082 Node *in = n->in(i);
aoqi@0 3083 ++i;
aoqi@0 3084 if (in == NULL) continue;
aoqi@0 3085 if (in->pinned() && !in->is_CFG())
aoqi@0 3086 set_ctrl(in, in->in(0));
aoqi@0 3087 int is_visited = visited.test_set( in->_idx );
aoqi@0 3088 if (!has_node(in)) { // No controlling input yet?
aoqi@0 3089 assert( !in->is_CFG(), "CFG Node with no controlling input?" );
aoqi@0 3090 assert( !is_visited, "visit only once" );
aoqi@0 3091 nstack.push(n, i); // Save parent node and next input's index.
aoqi@0 3092 nstack_top_n = in; // Process current input now.
aoqi@0 3093 nstack_top_i = 0;
aoqi@0 3094 done = false; // Not all n's inputs processed.
aoqi@0 3095 break; // continue while_nstack_nonempty;
aoqi@0 3096 } else if (!is_visited) {
aoqi@0 3097 // This guy has a location picked out for him, but has not yet
aoqi@0 3098 // been visited. Happens to all CFG nodes, for instance.
aoqi@0 3099 // Visit him using the worklist instead of recursion, to break
aoqi@0 3100 // cycles. Since he has a location already we do not need to
aoqi@0 3101 // find his location before proceeding with the current Node.
aoqi@0 3102 worklist.push(in); // Visit this guy later, using worklist
aoqi@0 3103 }
aoqi@0 3104 }
aoqi@0 3105 if (done) {
aoqi@0 3106 // All of n's inputs have been processed, complete post-processing.
aoqi@0 3107
aoqi@0 3108 // Compute earliest point this Node can go.
aoqi@0 3109 // CFG, Phi, pinned nodes already know their controlling input.
aoqi@0 3110 if (!has_node(n)) {
aoqi@0 3111 // Record earliest legal location
aoqi@0 3112 set_early_ctrl( n );
aoqi@0 3113 }
aoqi@0 3114 if (nstack.is_empty()) {
aoqi@0 3115 // Finished all nodes on stack.
aoqi@0 3116 // Process next node on the worklist.
aoqi@0 3117 break;
aoqi@0 3118 }
aoqi@0 3119 // Get saved parent node and next input's index.
aoqi@0 3120 nstack_top_n = nstack.node();
aoqi@0 3121 nstack_top_i = nstack.index();
aoqi@0 3122 nstack.pop();
aoqi@0 3123 }
aoqi@0 3124 } // while (true)
aoqi@0 3125 }
aoqi@0 3126 }
aoqi@0 3127
aoqi@0 3128 //------------------------------dom_lca_internal--------------------------------
aoqi@0 3129 // Pair-wise LCA
aoqi@0 3130 Node *PhaseIdealLoop::dom_lca_internal( Node *n1, Node *n2 ) const {
aoqi@0 3131 if( !n1 ) return n2; // Handle NULL original LCA
aoqi@0 3132 assert( n1->is_CFG(), "" );
aoqi@0 3133 assert( n2->is_CFG(), "" );
aoqi@0 3134 // find LCA of all uses
aoqi@0 3135 uint d1 = dom_depth(n1);
aoqi@0 3136 uint d2 = dom_depth(n2);
aoqi@0 3137 while (n1 != n2) {
aoqi@0 3138 if (d1 > d2) {
aoqi@0 3139 n1 = idom(n1);
aoqi@0 3140 d1 = dom_depth(n1);
aoqi@0 3141 } else if (d1 < d2) {
aoqi@0 3142 n2 = idom(n2);
aoqi@0 3143 d2 = dom_depth(n2);
aoqi@0 3144 } else {
aoqi@0 3145 // Here d1 == d2. Due to edits of the dominator-tree, sections
aoqi@0 3146 // of the tree might have the same depth. These sections have
aoqi@0 3147 // to be searched more carefully.
aoqi@0 3148
aoqi@0 3149 // Scan up all the n1's with equal depth, looking for n2.
aoqi@0 3150 Node *t1 = idom(n1);
aoqi@0 3151 while (dom_depth(t1) == d1) {
aoqi@0 3152 if (t1 == n2) return n2;
aoqi@0 3153 t1 = idom(t1);
aoqi@0 3154 }
aoqi@0 3155 // Scan up all the n2's with equal depth, looking for n1.
aoqi@0 3156 Node *t2 = idom(n2);
aoqi@0 3157 while (dom_depth(t2) == d2) {
aoqi@0 3158 if (t2 == n1) return n1;
aoqi@0 3159 t2 = idom(t2);
aoqi@0 3160 }
aoqi@0 3161 // Move up to a new dominator-depth value as well as up the dom-tree.
aoqi@0 3162 n1 = t1;
aoqi@0 3163 n2 = t2;
aoqi@0 3164 d1 = dom_depth(n1);
aoqi@0 3165 d2 = dom_depth(n2);
aoqi@0 3166 }
aoqi@0 3167 }
aoqi@0 3168 return n1;
aoqi@0 3169 }
aoqi@0 3170
aoqi@0 3171 //------------------------------compute_idom-----------------------------------
aoqi@0 3172 // Locally compute IDOM using dom_lca call. Correct only if the incoming
aoqi@0 3173 // IDOMs are correct.
aoqi@0 3174 Node *PhaseIdealLoop::compute_idom( Node *region ) const {
aoqi@0 3175 assert( region->is_Region(), "" );
aoqi@0 3176 Node *LCA = NULL;
aoqi@0 3177 for( uint i = 1; i < region->req(); i++ ) {
aoqi@0 3178 if( region->in(i) != C->top() )
aoqi@0 3179 LCA = dom_lca( LCA, region->in(i) );
aoqi@0 3180 }
aoqi@0 3181 return LCA;
aoqi@0 3182 }
aoqi@0 3183
aoqi@0 3184 bool PhaseIdealLoop::verify_dominance(Node* n, Node* use, Node* LCA, Node* early) {
aoqi@0 3185 bool had_error = false;
aoqi@0 3186 #ifdef ASSERT
aoqi@0 3187 if (early != C->root()) {
aoqi@0 3188 // Make sure that there's a dominance path from LCA to early
aoqi@0 3189 Node* d = LCA;
aoqi@0 3190 while (d != early) {
aoqi@0 3191 if (d == C->root()) {
aoqi@0 3192 dump_bad_graph("Bad graph detected in compute_lca_of_uses", n, early, LCA);
aoqi@0 3193 tty->print_cr("*** Use %d isn't dominated by def %d ***", use->_idx, n->_idx);
aoqi@0 3194 had_error = true;
aoqi@0 3195 break;
aoqi@0 3196 }
aoqi@0 3197 d = idom(d);
aoqi@0 3198 }
aoqi@0 3199 }
aoqi@0 3200 #endif
aoqi@0 3201 return had_error;
aoqi@0 3202 }
aoqi@0 3203
aoqi@0 3204
aoqi@0 3205 Node* PhaseIdealLoop::compute_lca_of_uses(Node* n, Node* early, bool verify) {
aoqi@0 3206 // Compute LCA over list of uses
aoqi@0 3207 bool had_error = false;
aoqi@0 3208 Node *LCA = NULL;
aoqi@0 3209 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax && LCA != early; i++) {
aoqi@0 3210 Node* c = n->fast_out(i);
aoqi@0 3211 if (_nodes[c->_idx] == NULL)
aoqi@0 3212 continue; // Skip the occasional dead node
aoqi@0 3213 if( c->is_Phi() ) { // For Phis, we must land above on the path
aoqi@0 3214 for( uint j=1; j<c->req(); j++ ) {// For all inputs
aoqi@0 3215 if( c->in(j) == n ) { // Found matching input?
aoqi@0 3216 Node *use = c->in(0)->in(j);
aoqi@0 3217 if (_verify_only && use->is_top()) continue;
aoqi@0 3218 LCA = dom_lca_for_get_late_ctrl( LCA, use, n );
aoqi@0 3219 if (verify) had_error = verify_dominance(n, use, LCA, early) || had_error;
aoqi@0 3220 }
aoqi@0 3221 }
aoqi@0 3222 } else {
aoqi@0 3223 // For CFG data-users, use is in the block just prior
aoqi@0 3224 Node *use = has_ctrl(c) ? get_ctrl(c) : c->in(0);
aoqi@0 3225 LCA = dom_lca_for_get_late_ctrl( LCA, use, n );
aoqi@0 3226 if (verify) had_error = verify_dominance(n, use, LCA, early) || had_error;
aoqi@0 3227 }
aoqi@0 3228 }
aoqi@0 3229 assert(!had_error, "bad dominance");
aoqi@0 3230 return LCA;
aoqi@0 3231 }
aoqi@0 3232
aoqi@0 3233 //------------------------------get_late_ctrl----------------------------------
aoqi@0 3234 // Compute latest legal control.
aoqi@0 3235 Node *PhaseIdealLoop::get_late_ctrl( Node *n, Node *early ) {
aoqi@0 3236 assert(early != NULL, "early control should not be NULL");
aoqi@0 3237
aoqi@0 3238 Node* LCA = compute_lca_of_uses(n, early);
aoqi@0 3239 #ifdef ASSERT
aoqi@0 3240 if (LCA == C->root() && LCA != early) {
aoqi@0 3241 // def doesn't dominate uses so print some useful debugging output
aoqi@0 3242 compute_lca_of_uses(n, early, true);
aoqi@0 3243 }
aoqi@0 3244 #endif
aoqi@0 3245
aoqi@0 3246 // if this is a load, check for anti-dependent stores
aoqi@0 3247 // We use a conservative algorithm to identify potential interfering
aoqi@0 3248 // instructions and for rescheduling the load. The users of the memory
aoqi@0 3249 // input of this load are examined. Any use which is not a load and is
aoqi@0 3250 // dominated by early is considered a potentially interfering store.
aoqi@0 3251 // This can produce false positives.
aoqi@0 3252 if (n->is_Load() && LCA != early) {
aoqi@0 3253 Node_List worklist;
aoqi@0 3254
aoqi@0 3255 Node *mem = n->in(MemNode::Memory);
aoqi@0 3256 for (DUIterator_Fast imax, i = mem->fast_outs(imax); i < imax; i++) {
aoqi@0 3257 Node* s = mem->fast_out(i);
aoqi@0 3258 worklist.push(s);
aoqi@0 3259 }
aoqi@0 3260 while(worklist.size() != 0 && LCA != early) {
aoqi@0 3261 Node* s = worklist.pop();
aoqi@0 3262 if (s->is_Load()) {
aoqi@0 3263 continue;
aoqi@0 3264 } else if (s->is_MergeMem()) {
aoqi@0 3265 for (DUIterator_Fast imax, i = s->fast_outs(imax); i < imax; i++) {
aoqi@0 3266 Node* s1 = s->fast_out(i);
aoqi@0 3267 worklist.push(s1);
aoqi@0 3268 }
aoqi@0 3269 } else {
aoqi@0 3270 Node *sctrl = has_ctrl(s) ? get_ctrl(s) : s->in(0);
aoqi@0 3271 assert(sctrl != NULL || s->outcnt() == 0, "must have control");
aoqi@0 3272 if (sctrl != NULL && !sctrl->is_top() && is_dominator(early, sctrl)) {
aoqi@0 3273 LCA = dom_lca_for_get_late_ctrl(LCA, sctrl, n);
aoqi@0 3274 }
aoqi@0 3275 }
aoqi@0 3276 }
aoqi@0 3277 }
aoqi@0 3278
aoqi@0 3279 assert(LCA == find_non_split_ctrl(LCA), "unexpected late control");
aoqi@0 3280 return LCA;
aoqi@0 3281 }
aoqi@0 3282
aoqi@0 3283 // true if CFG node d dominates CFG node n
aoqi@0 3284 bool PhaseIdealLoop::is_dominator(Node *d, Node *n) {
aoqi@0 3285 if (d == n)
aoqi@0 3286 return true;
aoqi@0 3287 assert(d->is_CFG() && n->is_CFG(), "must have CFG nodes");
aoqi@0 3288 uint dd = dom_depth(d);
aoqi@0 3289 while (dom_depth(n) >= dd) {
aoqi@0 3290 if (n == d)
aoqi@0 3291 return true;
aoqi@0 3292 n = idom(n);
aoqi@0 3293 }
aoqi@0 3294 return false;
aoqi@0 3295 }
aoqi@0 3296
aoqi@0 3297 //------------------------------dom_lca_for_get_late_ctrl_internal-------------
aoqi@0 3298 // Pair-wise LCA with tags.
aoqi@0 3299 // Tag each index with the node 'tag' currently being processed
aoqi@0 3300 // before advancing up the dominator chain using idom().
aoqi@0 3301 // Later calls that find a match to 'tag' know that this path has already
aoqi@0 3302 // been considered in the current LCA (which is input 'n1' by convention).
aoqi@0 3303 // Since get_late_ctrl() is only called once for each node, the tag array
aoqi@0 3304 // does not need to be cleared between calls to get_late_ctrl().
aoqi@0 3305 // Algorithm trades a larger constant factor for better asymptotic behavior
aoqi@0 3306 //
aoqi@0 3307 Node *PhaseIdealLoop::dom_lca_for_get_late_ctrl_internal( Node *n1, Node *n2, Node *tag ) {
aoqi@0 3308 uint d1 = dom_depth(n1);
aoqi@0 3309 uint d2 = dom_depth(n2);
aoqi@0 3310
aoqi@0 3311 do {
aoqi@0 3312 if (d1 > d2) {
aoqi@0 3313 // current lca is deeper than n2
aoqi@0 3314 _dom_lca_tags.map(n1->_idx, tag);
aoqi@0 3315 n1 = idom(n1);
aoqi@0 3316 d1 = dom_depth(n1);
aoqi@0 3317 } else if (d1 < d2) {
aoqi@0 3318 // n2 is deeper than current lca
aoqi@0 3319 Node *memo = _dom_lca_tags[n2->_idx];
aoqi@0 3320 if( memo == tag ) {
aoqi@0 3321 return n1; // Return the current LCA
aoqi@0 3322 }
aoqi@0 3323 _dom_lca_tags.map(n2->_idx, tag);
aoqi@0 3324 n2 = idom(n2);
aoqi@0 3325 d2 = dom_depth(n2);
aoqi@0 3326 } else {
aoqi@0 3327 // Here d1 == d2. Due to edits of the dominator-tree, sections
aoqi@0 3328 // of the tree might have the same depth. These sections have
aoqi@0 3329 // to be searched more carefully.
aoqi@0 3330
aoqi@0 3331 // Scan up all the n1's with equal depth, looking for n2.
aoqi@0 3332 _dom_lca_tags.map(n1->_idx, tag);
aoqi@0 3333 Node *t1 = idom(n1);
aoqi@0 3334 while (dom_depth(t1) == d1) {
aoqi@0 3335 if (t1 == n2) return n2;
aoqi@0 3336 _dom_lca_tags.map(t1->_idx, tag);
aoqi@0 3337 t1 = idom(t1);
aoqi@0 3338 }
aoqi@0 3339 // Scan up all the n2's with equal depth, looking for n1.
aoqi@0 3340 _dom_lca_tags.map(n2->_idx, tag);
aoqi@0 3341 Node *t2 = idom(n2);
aoqi@0 3342 while (dom_depth(t2) == d2) {
aoqi@0 3343 if (t2 == n1) return n1;
aoqi@0 3344 _dom_lca_tags.map(t2->_idx, tag);
aoqi@0 3345 t2 = idom(t2);
aoqi@0 3346 }
aoqi@0 3347 // Move up to a new dominator-depth value as well as up the dom-tree.
aoqi@0 3348 n1 = t1;
aoqi@0 3349 n2 = t2;
aoqi@0 3350 d1 = dom_depth(n1);
aoqi@0 3351 d2 = dom_depth(n2);
aoqi@0 3352 }
aoqi@0 3353 } while (n1 != n2);
aoqi@0 3354 return n1;
aoqi@0 3355 }
aoqi@0 3356
aoqi@0 3357 //------------------------------init_dom_lca_tags------------------------------
aoqi@0 3358 // Tag could be a node's integer index, 32bits instead of 64bits in some cases
aoqi@0 3359 // Intended use does not involve any growth for the array, so it could
aoqi@0 3360 // be of fixed size.
aoqi@0 3361 void PhaseIdealLoop::init_dom_lca_tags() {
aoqi@0 3362 uint limit = C->unique() + 1;
aoqi@0 3363 _dom_lca_tags.map( limit, NULL );
aoqi@0 3364 #ifdef ASSERT
aoqi@0 3365 for( uint i = 0; i < limit; ++i ) {
aoqi@0 3366 assert(_dom_lca_tags[i] == NULL, "Must be distinct from each node pointer");
aoqi@0 3367 }
aoqi@0 3368 #endif // ASSERT
aoqi@0 3369 }
aoqi@0 3370
aoqi@0 3371 //------------------------------clear_dom_lca_tags------------------------------
aoqi@0 3372 // Tag could be a node's integer index, 32bits instead of 64bits in some cases
aoqi@0 3373 // Intended use does not involve any growth for the array, so it could
aoqi@0 3374 // be of fixed size.
aoqi@0 3375 void PhaseIdealLoop::clear_dom_lca_tags() {
aoqi@0 3376 uint limit = C->unique() + 1;
aoqi@0 3377 _dom_lca_tags.map( limit, NULL );
aoqi@0 3378 _dom_lca_tags.clear();
aoqi@0 3379 #ifdef ASSERT
aoqi@0 3380 for( uint i = 0; i < limit; ++i ) {
aoqi@0 3381 assert(_dom_lca_tags[i] == NULL, "Must be distinct from each node pointer");
aoqi@0 3382 }
aoqi@0 3383 #endif // ASSERT
aoqi@0 3384 }
aoqi@0 3385
aoqi@0 3386 //------------------------------build_loop_late--------------------------------
aoqi@0 3387 // Put Data nodes into some loop nest, by setting the _nodes[]->loop mapping.
aoqi@0 3388 // Second pass finds latest legal placement, and ideal loop placement.
aoqi@0 3389 void PhaseIdealLoop::build_loop_late( VectorSet &visited, Node_List &worklist, Node_Stack &nstack ) {
aoqi@0 3390 while (worklist.size() != 0) {
aoqi@0 3391 Node *n = worklist.pop();
aoqi@0 3392 // Only visit once
aoqi@0 3393 if (visited.test_set(n->_idx)) continue;
aoqi@0 3394 uint cnt = n->outcnt();
aoqi@0 3395 uint i = 0;
aoqi@0 3396 while (true) {
aoqi@0 3397 assert( _nodes[n->_idx], "no dead nodes" );
aoqi@0 3398 // Visit all children
aoqi@0 3399 if (i < cnt) {
aoqi@0 3400 Node* use = n->raw_out(i);
aoqi@0 3401 ++i;
aoqi@0 3402 // Check for dead uses. Aggressively prune such junk. It might be
aoqi@0 3403 // dead in the global sense, but still have local uses so I cannot
aoqi@0 3404 // easily call 'remove_dead_node'.
aoqi@0 3405 if( _nodes[use->_idx] != NULL || use->is_top() ) { // Not dead?
aoqi@0 3406 // Due to cycles, we might not hit the same fixed point in the verify
aoqi@0 3407 // pass as we do in the regular pass. Instead, visit such phis as
aoqi@0 3408 // simple uses of the loop head.
aoqi@0 3409 if( use->in(0) && (use->is_CFG() || use->is_Phi()) ) {
aoqi@0 3410 if( !visited.test(use->_idx) )
aoqi@0 3411 worklist.push(use);
aoqi@0 3412 } else if( !visited.test_set(use->_idx) ) {
aoqi@0 3413 nstack.push(n, i); // Save parent and next use's index.
aoqi@0 3414 n = use; // Process all children of current use.
aoqi@0 3415 cnt = use->outcnt();
aoqi@0 3416 i = 0;
aoqi@0 3417 }
aoqi@0 3418 } else {
aoqi@0 3419 // Do not visit around the backedge of loops via data edges.
aoqi@0 3420 // push dead code onto a worklist
aoqi@0 3421 _deadlist.push(use);
aoqi@0 3422 }
aoqi@0 3423 } else {
aoqi@0 3424 // All of n's children have been processed, complete post-processing.
aoqi@0 3425 build_loop_late_post(n);
aoqi@0 3426 if (nstack.is_empty()) {
aoqi@0 3427 // Finished all nodes on stack.
aoqi@0 3428 // Process next node on the worklist.
aoqi@0 3429 break;
aoqi@0 3430 }
aoqi@0 3431 // Get saved parent node and next use's index. Visit the rest of uses.
aoqi@0 3432 n = nstack.node();
aoqi@0 3433 cnt = n->outcnt();
aoqi@0 3434 i = nstack.index();
aoqi@0 3435 nstack.pop();
aoqi@0 3436 }
aoqi@0 3437 }
aoqi@0 3438 }
aoqi@0 3439 }
aoqi@0 3440
aoqi@0 3441 //------------------------------build_loop_late_post---------------------------
aoqi@0 3442 // Put Data nodes into some loop nest, by setting the _nodes[]->loop mapping.
aoqi@0 3443 // Second pass finds latest legal placement, and ideal loop placement.
aoqi@0 3444 void PhaseIdealLoop::build_loop_late_post( Node *n ) {
aoqi@0 3445
aoqi@0 3446 if (n->req() == 2 && n->Opcode() == Op_ConvI2L && !C->major_progress() && !_verify_only) {
aoqi@0 3447 _igvn._worklist.push(n); // Maybe we'll normalize it, if no more loops.
aoqi@0 3448 }
aoqi@0 3449
aoqi@0 3450 #ifdef ASSERT
aoqi@0 3451 if (_verify_only && !n->is_CFG()) {
aoqi@0 3452 // Check def-use domination.
aoqi@0 3453 compute_lca_of_uses(n, get_ctrl(n), true /* verify */);
aoqi@0 3454 }
aoqi@0 3455 #endif
aoqi@0 3456
aoqi@0 3457 // CFG and pinned nodes already handled
aoqi@0 3458 if( n->in(0) ) {
aoqi@0 3459 if( n->in(0)->is_top() ) return; // Dead?
aoqi@0 3460
aoqi@0 3461 // We'd like +VerifyLoopOptimizations to not believe that Mod's/Loads
aoqi@0 3462 // _must_ be pinned (they have to observe their control edge of course).
aoqi@0 3463 // Unlike Stores (which modify an unallocable resource, the memory
aoqi@0 3464 // state), Mods/Loads can float around. So free them up.
aoqi@0 3465 bool pinned = true;
aoqi@0 3466 switch( n->Opcode() ) {
aoqi@0 3467 case Op_DivI:
aoqi@0 3468 case Op_DivF:
aoqi@0 3469 case Op_DivD:
aoqi@0 3470 case Op_ModI:
aoqi@0 3471 case Op_ModF:
aoqi@0 3472 case Op_ModD:
aoqi@0 3473 case Op_LoadB: // Same with Loads; they can sink
aoqi@0 3474 case Op_LoadUB: // during loop optimizations.
aoqi@0 3475 case Op_LoadUS:
aoqi@0 3476 case Op_LoadD:
aoqi@0 3477 case Op_LoadF:
aoqi@0 3478 case Op_LoadI:
aoqi@0 3479 case Op_LoadKlass:
aoqi@0 3480 case Op_LoadNKlass:
aoqi@0 3481 case Op_LoadL:
aoqi@0 3482 case Op_LoadS:
aoqi@0 3483 case Op_LoadP:
aoqi@0 3484 case Op_LoadN:
aoqi@0 3485 case Op_LoadRange:
aoqi@0 3486 case Op_LoadD_unaligned:
aoqi@0 3487 case Op_LoadL_unaligned:
aoqi@0 3488 case Op_StrComp: // Does a bunch of load-like effects
aoqi@0 3489 case Op_StrEquals:
aoqi@0 3490 case Op_StrIndexOf:
aoqi@0 3491 case Op_AryEq:
aoqi@0 3492 pinned = false;
aoqi@0 3493 }
aoqi@0 3494 if( pinned ) {
aoqi@0 3495 IdealLoopTree *chosen_loop = get_loop(n->is_CFG() ? n : get_ctrl(n));
aoqi@0 3496 if( !chosen_loop->_child ) // Inner loop?
aoqi@0 3497 chosen_loop->_body.push(n); // Collect inner loops
aoqi@0 3498 return;
aoqi@0 3499 }
aoqi@0 3500 } else { // No slot zero
aoqi@0 3501 if( n->is_CFG() ) { // CFG with no slot 0 is dead
aoqi@0 3502 _nodes.map(n->_idx,0); // No block setting, it's globally dead
aoqi@0 3503 return;
aoqi@0 3504 }
aoqi@0 3505 assert(!n->is_CFG() || n->outcnt() == 0, "");
aoqi@0 3506 }
aoqi@0 3507
aoqi@0 3508 // Do I have a "safe range" I can select over?
aoqi@0 3509 Node *early = get_ctrl(n);// Early location already computed
aoqi@0 3510
aoqi@0 3511 // Compute latest point this Node can go
aoqi@0 3512 Node *LCA = get_late_ctrl( n, early );
aoqi@0 3513 // LCA is NULL due to uses being dead
aoqi@0 3514 if( LCA == NULL ) {
aoqi@0 3515 #ifdef ASSERT
aoqi@0 3516 for (DUIterator i1 = n->outs(); n->has_out(i1); i1++) {
aoqi@0 3517 assert( _nodes[n->out(i1)->_idx] == NULL, "all uses must also be dead");
aoqi@0 3518 }
aoqi@0 3519 #endif
aoqi@0 3520 _nodes.map(n->_idx, 0); // This node is useless
aoqi@0 3521 _deadlist.push(n);
aoqi@0 3522 return;
aoqi@0 3523 }
aoqi@0 3524 assert(LCA != NULL && !LCA->is_top(), "no dead nodes");
aoqi@0 3525
aoqi@0 3526 Node *legal = LCA; // Walk 'legal' up the IDOM chain
aoqi@0 3527 Node *least = legal; // Best legal position so far
aoqi@0 3528 while( early != legal ) { // While not at earliest legal
aoqi@0 3529 #ifdef ASSERT
aoqi@0 3530 if (legal->is_Start() && !early->is_Root()) {
aoqi@0 3531 // Bad graph. Print idom path and fail.
aoqi@0 3532 dump_bad_graph("Bad graph detected in build_loop_late", n, early, LCA);
aoqi@0 3533 assert(false, "Bad graph detected in build_loop_late");
aoqi@0 3534 }
aoqi@0 3535 #endif
aoqi@0 3536 // Find least loop nesting depth
aoqi@0 3537 legal = idom(legal); // Bump up the IDOM tree
aoqi@0 3538 // Check for lower nesting depth
aoqi@0 3539 if( get_loop(legal)->_nest < get_loop(least)->_nest )
aoqi@0 3540 least = legal;
aoqi@0 3541 }
aoqi@0 3542 assert(early == legal || legal != C->root(), "bad dominance of inputs");
aoqi@0 3543
aoqi@0 3544 // Try not to place code on a loop entry projection
aoqi@0 3545 // which can inhibit range check elimination.
aoqi@0 3546 if (least != early) {
aoqi@0 3547 Node* ctrl_out = least->unique_ctrl_out();
aoqi@0 3548 if (ctrl_out && ctrl_out->is_CountedLoop() &&
aoqi@0 3549 least == ctrl_out->in(LoopNode::EntryControl)) {
aoqi@0 3550 Node* least_dom = idom(least);
aoqi@0 3551 if (get_loop(least_dom)->is_member(get_loop(least))) {
aoqi@0 3552 least = least_dom;
aoqi@0 3553 }
aoqi@0 3554 }
aoqi@0 3555 }
aoqi@0 3556
aoqi@0 3557 #ifdef ASSERT
aoqi@0 3558 // If verifying, verify that 'verify_me' has a legal location
aoqi@0 3559 // and choose it as our location.
aoqi@0 3560 if( _verify_me ) {
aoqi@0 3561 Node *v_ctrl = _verify_me->get_ctrl_no_update(n);
aoqi@0 3562 Node *legal = LCA;
aoqi@0 3563 while( early != legal ) { // While not at earliest legal
aoqi@0 3564 if( legal == v_ctrl ) break; // Check for prior good location
aoqi@0 3565 legal = idom(legal) ;// Bump up the IDOM tree
aoqi@0 3566 }
aoqi@0 3567 // Check for prior good location
aoqi@0 3568 if( legal == v_ctrl ) least = legal; // Keep prior if found
aoqi@0 3569 }
aoqi@0 3570 #endif
aoqi@0 3571
aoqi@0 3572 // Assign discovered "here or above" point
aoqi@0 3573 least = find_non_split_ctrl(least);
aoqi@0 3574 set_ctrl(n, least);
aoqi@0 3575
aoqi@0 3576 // Collect inner loop bodies
aoqi@0 3577 IdealLoopTree *chosen_loop = get_loop(least);
aoqi@0 3578 if( !chosen_loop->_child ) // Inner loop?
aoqi@0 3579 chosen_loop->_body.push(n);// Collect inner loops
aoqi@0 3580 }
aoqi@0 3581
aoqi@0 3582 #ifdef ASSERT
aoqi@0 3583 void PhaseIdealLoop::dump_bad_graph(const char* msg, Node* n, Node* early, Node* LCA) {
aoqi@0 3584 tty->print_cr("%s", msg);
aoqi@0 3585 tty->print("n: "); n->dump();
aoqi@0 3586 tty->print("early(n): "); early->dump();
aoqi@0 3587 if (n->in(0) != NULL && !n->in(0)->is_top() &&
aoqi@0 3588 n->in(0) != early && !n->in(0)->is_Root()) {
aoqi@0 3589 tty->print("n->in(0): "); n->in(0)->dump();
aoqi@0 3590 }
aoqi@0 3591 for (uint i = 1; i < n->req(); i++) {
aoqi@0 3592 Node* in1 = n->in(i);
aoqi@0 3593 if (in1 != NULL && in1 != n && !in1->is_top()) {
aoqi@0 3594 tty->print("n->in(%d): ", i); in1->dump();
aoqi@0 3595 Node* in1_early = get_ctrl(in1);
aoqi@0 3596 tty->print("early(n->in(%d)): ", i); in1_early->dump();
aoqi@0 3597 if (in1->in(0) != NULL && !in1->in(0)->is_top() &&
aoqi@0 3598 in1->in(0) != in1_early && !in1->in(0)->is_Root()) {
aoqi@0 3599 tty->print("n->in(%d)->in(0): ", i); in1->in(0)->dump();
aoqi@0 3600 }
aoqi@0 3601 for (uint j = 1; j < in1->req(); j++) {
aoqi@0 3602 Node* in2 = in1->in(j);
aoqi@0 3603 if (in2 != NULL && in2 != n && in2 != in1 && !in2->is_top()) {
aoqi@0 3604 tty->print("n->in(%d)->in(%d): ", i, j); in2->dump();
aoqi@0 3605 Node* in2_early = get_ctrl(in2);
aoqi@0 3606 tty->print("early(n->in(%d)->in(%d)): ", i, j); in2_early->dump();
aoqi@0 3607 if (in2->in(0) != NULL && !in2->in(0)->is_top() &&
aoqi@0 3608 in2->in(0) != in2_early && !in2->in(0)->is_Root()) {
aoqi@0 3609 tty->print("n->in(%d)->in(%d)->in(0): ", i, j); in2->in(0)->dump();
aoqi@0 3610 }
aoqi@0 3611 }
aoqi@0 3612 }
aoqi@0 3613 }
aoqi@0 3614 }
aoqi@0 3615 tty->cr();
aoqi@0 3616 tty->print("LCA(n): "); LCA->dump();
aoqi@0 3617 for (uint i = 0; i < n->outcnt(); i++) {
aoqi@0 3618 Node* u1 = n->raw_out(i);
aoqi@0 3619 if (u1 == n)
aoqi@0 3620 continue;
aoqi@0 3621 tty->print("n->out(%d): ", i); u1->dump();
aoqi@0 3622 if (u1->is_CFG()) {
aoqi@0 3623 for (uint j = 0; j < u1->outcnt(); j++) {
aoqi@0 3624 Node* u2 = u1->raw_out(j);
aoqi@0 3625 if (u2 != u1 && u2 != n && u2->is_CFG()) {
aoqi@0 3626 tty->print("n->out(%d)->out(%d): ", i, j); u2->dump();
aoqi@0 3627 }
aoqi@0 3628 }
aoqi@0 3629 } else {
aoqi@0 3630 Node* u1_later = get_ctrl(u1);
aoqi@0 3631 tty->print("later(n->out(%d)): ", i); u1_later->dump();
aoqi@0 3632 if (u1->in(0) != NULL && !u1->in(0)->is_top() &&
aoqi@0 3633 u1->in(0) != u1_later && !u1->in(0)->is_Root()) {
aoqi@0 3634 tty->print("n->out(%d)->in(0): ", i); u1->in(0)->dump();
aoqi@0 3635 }
aoqi@0 3636 for (uint j = 0; j < u1->outcnt(); j++) {
aoqi@0 3637 Node* u2 = u1->raw_out(j);
aoqi@0 3638 if (u2 == n || u2 == u1)
aoqi@0 3639 continue;
aoqi@0 3640 tty->print("n->out(%d)->out(%d): ", i, j); u2->dump();
aoqi@0 3641 if (!u2->is_CFG()) {
aoqi@0 3642 Node* u2_later = get_ctrl(u2);
aoqi@0 3643 tty->print("later(n->out(%d)->out(%d)): ", i, j); u2_later->dump();
aoqi@0 3644 if (u2->in(0) != NULL && !u2->in(0)->is_top() &&
aoqi@0 3645 u2->in(0) != u2_later && !u2->in(0)->is_Root()) {
aoqi@0 3646 tty->print("n->out(%d)->in(0): ", i); u2->in(0)->dump();
aoqi@0 3647 }
aoqi@0 3648 }
aoqi@0 3649 }
aoqi@0 3650 }
aoqi@0 3651 }
aoqi@0 3652 tty->cr();
aoqi@0 3653 int ct = 0;
aoqi@0 3654 Node *dbg_legal = LCA;
aoqi@0 3655 while(!dbg_legal->is_Start() && ct < 100) {
aoqi@0 3656 tty->print("idom[%d] ",ct); dbg_legal->dump();
aoqi@0 3657 ct++;
aoqi@0 3658 dbg_legal = idom(dbg_legal);
aoqi@0 3659 }
aoqi@0 3660 tty->cr();
aoqi@0 3661 }
aoqi@0 3662 #endif
aoqi@0 3663
aoqi@0 3664 #ifndef PRODUCT
aoqi@0 3665 //------------------------------dump-------------------------------------------
aoqi@0 3666 void PhaseIdealLoop::dump( ) const {
aoqi@0 3667 ResourceMark rm;
aoqi@0 3668 Arena* arena = Thread::current()->resource_area();
aoqi@0 3669 Node_Stack stack(arena, C->unique() >> 2);
aoqi@0 3670 Node_List rpo_list;
aoqi@0 3671 VectorSet visited(arena);
aoqi@0 3672 visited.set(C->top()->_idx);
aoqi@0 3673 rpo( C->root(), stack, visited, rpo_list );
aoqi@0 3674 // Dump root loop indexed by last element in PO order
aoqi@0 3675 dump( _ltree_root, rpo_list.size(), rpo_list );
aoqi@0 3676 }
aoqi@0 3677
aoqi@0 3678 void PhaseIdealLoop::dump( IdealLoopTree *loop, uint idx, Node_List &rpo_list ) const {
aoqi@0 3679 loop->dump_head();
aoqi@0 3680
aoqi@0 3681 // Now scan for CFG nodes in the same loop
aoqi@0 3682 for( uint j=idx; j > 0; j-- ) {
aoqi@0 3683 Node *n = rpo_list[j-1];
aoqi@0 3684 if( !_nodes[n->_idx] ) // Skip dead nodes
aoqi@0 3685 continue;
aoqi@0 3686 if( get_loop(n) != loop ) { // Wrong loop nest
aoqi@0 3687 if( get_loop(n)->_head == n && // Found nested loop?
aoqi@0 3688 get_loop(n)->_parent == loop )
aoqi@0 3689 dump(get_loop(n),rpo_list.size(),rpo_list); // Print it nested-ly
aoqi@0 3690 continue;
aoqi@0 3691 }
aoqi@0 3692
aoqi@0 3693 // Dump controlling node
aoqi@0 3694 for( uint x = 0; x < loop->_nest; x++ )
aoqi@0 3695 tty->print(" ");
aoqi@0 3696 tty->print("C");
aoqi@0 3697 if( n == C->root() ) {
aoqi@0 3698 n->dump();
aoqi@0 3699 } else {
aoqi@0 3700 Node* cached_idom = idom_no_update(n);
aoqi@0 3701 Node *computed_idom = n->in(0);
aoqi@0 3702 if( n->is_Region() ) {
aoqi@0 3703 computed_idom = compute_idom(n);
aoqi@0 3704 // computed_idom() will return n->in(0) when idom(n) is an IfNode (or
aoqi@0 3705 // any MultiBranch ctrl node), so apply a similar transform to
aoqi@0 3706 // the cached idom returned from idom_no_update.
aoqi@0 3707 cached_idom = find_non_split_ctrl(cached_idom);
aoqi@0 3708 }
aoqi@0 3709 tty->print(" ID:%d",computed_idom->_idx);
aoqi@0 3710 n->dump();
aoqi@0 3711 if( cached_idom != computed_idom ) {
aoqi@0 3712 tty->print_cr("*** BROKEN IDOM! Computed as: %d, cached as: %d",
aoqi@0 3713 computed_idom->_idx, cached_idom->_idx);
aoqi@0 3714 }
aoqi@0 3715 }
aoqi@0 3716 // Dump nodes it controls
aoqi@0 3717 for( uint k = 0; k < _nodes.Size(); k++ ) {
aoqi@0 3718 // (k < C->unique() && get_ctrl(find(k)) == n)
aoqi@0 3719 if (k < C->unique() && _nodes[k] == (Node*)((intptr_t)n + 1)) {
aoqi@0 3720 Node *m = C->root()->find(k);
aoqi@0 3721 if( m && m->outcnt() > 0 ) {
aoqi@0 3722 if (!(has_ctrl(m) && get_ctrl_no_update(m) == n)) {
aoqi@0 3723 tty->print_cr("*** BROKEN CTRL ACCESSOR! _nodes[k] is %p, ctrl is %p",
aoqi@0 3724 _nodes[k], has_ctrl(m) ? get_ctrl_no_update(m) : NULL);
aoqi@0 3725 }
aoqi@0 3726 for( uint j = 0; j < loop->_nest; j++ )
aoqi@0 3727 tty->print(" ");
aoqi@0 3728 tty->print(" ");
aoqi@0 3729 m->dump();
aoqi@0 3730 }
aoqi@0 3731 }
aoqi@0 3732 }
aoqi@0 3733 }
aoqi@0 3734 }
aoqi@0 3735
aoqi@0 3736 // Collect a R-P-O for the whole CFG.
aoqi@0 3737 // Result list is in post-order (scan backwards for RPO)
aoqi@0 3738 void PhaseIdealLoop::rpo( Node *start, Node_Stack &stk, VectorSet &visited, Node_List &rpo_list ) const {
aoqi@0 3739 stk.push(start, 0);
aoqi@0 3740 visited.set(start->_idx);
aoqi@0 3741
aoqi@0 3742 while (stk.is_nonempty()) {
aoqi@0 3743 Node* m = stk.node();
aoqi@0 3744 uint idx = stk.index();
aoqi@0 3745 if (idx < m->outcnt()) {
aoqi@0 3746 stk.set_index(idx + 1);
aoqi@0 3747 Node* n = m->raw_out(idx);
aoqi@0 3748 if (n->is_CFG() && !visited.test_set(n->_idx)) {
aoqi@0 3749 stk.push(n, 0);
aoqi@0 3750 }
aoqi@0 3751 } else {
aoqi@0 3752 rpo_list.push(m);
aoqi@0 3753 stk.pop();
aoqi@0 3754 }
aoqi@0 3755 }
aoqi@0 3756 }
aoqi@0 3757 #endif
aoqi@0 3758
aoqi@0 3759
aoqi@0 3760 //=============================================================================
aoqi@0 3761 //------------------------------LoopTreeIterator-----------------------------------
aoqi@0 3762
aoqi@0 3763 // Advance to next loop tree using a preorder, left-to-right traversal.
aoqi@0 3764 void LoopTreeIterator::next() {
aoqi@0 3765 assert(!done(), "must not be done.");
aoqi@0 3766 if (_curnt->_child != NULL) {
aoqi@0 3767 _curnt = _curnt->_child;
aoqi@0 3768 } else if (_curnt->_next != NULL) {
aoqi@0 3769 _curnt = _curnt->_next;
aoqi@0 3770 } else {
aoqi@0 3771 while (_curnt != _root && _curnt->_next == NULL) {
aoqi@0 3772 _curnt = _curnt->_parent;
aoqi@0 3773 }
aoqi@0 3774 if (_curnt == _root) {
aoqi@0 3775 _curnt = NULL;
aoqi@0 3776 assert(done(), "must be done.");
aoqi@0 3777 } else {
aoqi@0 3778 assert(_curnt->_next != NULL, "must be more to do");
aoqi@0 3779 _curnt = _curnt->_next;
aoqi@0 3780 }
aoqi@0 3781 }
aoqi@0 3782 }

mercurial