src/share/vm/opto/loopnode.cpp

Thu, 24 May 2018 17:06:56 +0800

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

mercurial