src/share/vm/opto/loopnode.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 8856
ac27a9c85bea
child 9041
95a08233f46c
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial