annotate src/hotspot/share/opto/loopopts.cpp @ 54048:744dc9c33676

8217417: Decorator name typo: C2_TIGHLY_COUPLED_ALLOC Summary: Fixed typo in decorator name, variables, and comments. Reviewed-by: tschatzl
author kbarrett
date Mon, 11 Mar 2019 02:05:07 -0400
parents 1884ecc20c38
children 6c0ab8bd8da5
rev   line source
duke@1 1 /*
dlong@48595 2 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
trims@5547 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@5547 20 * or visit www.oracle.com if you need additional information or have any
trims@5547 21 * questions.
duke@1 22 *
duke@1 23 */
duke@1 24
stefank@7397 25 #include "precompiled.hpp"
eosterlund@50180 26 #include "gc/shared/barrierSet.hpp"
eosterlund@50180 27 #include "gc/shared/c2/barrierSetC2.hpp"
stefank@7397 28 #include "memory/allocation.inline.hpp"
jprovino@37248 29 #include "memory/resourceArea.hpp"
stefank@7397 30 #include "opto/addnode.hpp"
roland@48145 31 #include "opto/callnode.hpp"
thartmann@35574 32 #include "opto/castnode.hpp"
stefank@7397 33 #include "opto/connode.hpp"
roland@35545 34 #include "opto/castnode.hpp"
stefank@7397 35 #include "opto/divnode.hpp"
stefank@7397 36 #include "opto/loopnode.hpp"
kvn@10971 37 #include "opto/matcher.hpp"
stefank@7397 38 #include "opto/mulnode.hpp"
morris@23528 39 #include "opto/movenode.hpp"
morris@23528 40 #include "opto/opaquenode.hpp"
stefank@7397 41 #include "opto/rootnode.hpp"
stefank@7397 42 #include "opto/subnode.hpp"
pliden@50525 43 #include "utilities/macros.hpp"
pliden@50525 44 #if INCLUDE_ZGC
pliden@50525 45 #include "gc/z/c2/zBarrierSetC2.hpp"
pliden@50525 46 #endif
duke@1 47
duke@1 48 //=============================================================================
duke@1 49 //------------------------------split_thru_phi---------------------------------
duke@1 50 // Split Node 'n' through merge point if there is enough win.
duke@1 51 Node *PhaseIdealLoop::split_thru_phi( Node *n, Node *region, int policy ) {
never@242 52 if (n->Opcode() == Op_ConvI2L && n->bottom_type() != TypeLong::LONG) {
never@242 53 // ConvI2L may have type information on it which is unsafe to push up
never@242 54 // so disable this for now
never@242 55 return NULL;
never@242 56 }
rbackman@21577 57
roland@36067 58 // Splitting range check CastIIs through a loop induction Phi can
roland@36067 59 // cause new Phis to be created that are left unrelated to the loop
roland@36067 60 // induction Phi and prevent optimizations (vectorization)
roland@36067 61 if (n->Opcode() == Op_CastII && n->as_CastII()->has_range_check() &&
roland@36067 62 region->is_CountedLoop() && n->in(1) == region->as_CountedLoop()->phi()) {
roland@36067 63 return NULL;
roland@36067 64 }
roland@36067 65
duke@1 66 int wins = 0;
kvn@8732 67 assert(!n->is_CFG(), "");
kvn@8732 68 assert(region->is_Region(), "");
kvn@237 69
kvn@237 70 const Type* type = n->bottom_type();
kvn@237 71 const TypeOopPtr *t_oop = _igvn.type(n)->isa_oopptr();
kvn@237 72 Node *phi;
kvn@8732 73 if (t_oop != NULL && t_oop->is_known_instance_field()) {
kvn@237 74 int iid = t_oop->instance_id();
kvn@237 75 int index = C->get_alias_index(t_oop);
kvn@237 76 int offset = t_oop->offset();
thartmann@24923 77 phi = new PhiNode(region, type, NULL, iid, index, offset);
kvn@237 78 } else {
kvn@4895 79 phi = PhiNode::make_blank(region, n);
kvn@237 80 }
duke@1 81 uint old_unique = C->unique();
kvn@8732 82 for (uint i = 1; i < region->req(); i++) {
duke@1 83 Node *x;
duke@1 84 Node* the_clone = NULL;
kvn@8732 85 if (region->in(i) == C->top()) {
duke@1 86 x = C->top(); // Dead path? Use a dead data op
duke@1 87 } else {
duke@1 88 x = n->clone(); // Else clone up the data op
duke@1 89 the_clone = x; // Remember for possible deletion.
duke@1 90 // Alter data node to use pre-phi inputs
kvn@8732 91 if (n->in(0) == region)
duke@1 92 x->set_req( 0, region->in(i) );
kvn@8732 93 for (uint j = 1; j < n->req(); j++) {
duke@1 94 Node *in = n->in(j);
kvn@8732 95 if (in->is_Phi() && in->in(0) == region)
duke@1 96 x->set_req( j, in->in(i) ); // Use pre-Phi input for the clone
duke@1 97 }
duke@1 98 }
duke@1 99 // Check for a 'win' on some paths
duke@1 100 const Type *t = x->Value(&_igvn);
duke@1 101
duke@1 102 bool singleton = t->singleton();
duke@1 103
duke@1 104 // A TOP singleton indicates that there are no possible values incoming
duke@1 105 // along a particular edge. In most cases, this is OK, and the Phi will
duke@1 106 // be eliminated later in an Ideal call. However, we can't allow this to
duke@1 107 // happen if the singleton occurs on loop entry, as the elimination of
duke@1 108 // the PhiNode may cause the resulting node to migrate back to a previous
duke@1 109 // loop iteration.
kvn@8732 110 if (singleton && t == Type::TOP) {
duke@1 111 // Is_Loop() == false does not confirm the absence of a loop (e.g., an
duke@1 112 // irreducible loop may not be indicated by an affirmative is_Loop());
duke@1 113 // therefore, the only top we can split thru a phi is on a backedge of
duke@1 114 // a loop.
duke@1 115 singleton &= region->is_Loop() && (i != LoopNode::EntryControl);
duke@1 116 }
duke@1 117
kvn@8732 118 if (singleton) {
duke@1 119 wins++;
duke@1 120 x = ((PhaseGVN&)_igvn).makecon(t);
duke@1 121 } else {
duke@1 122 // We now call Identity to try to simplify the cloned node.
duke@1 123 // Note that some Identity methods call phase->type(this).
duke@1 124 // Make sure that the type array is big enough for
duke@1 125 // our new node, even though we may throw the node away.
duke@1 126 // (Note: This tweaking with igvn only works because x is a new node.)
duke@1 127 _igvn.set_type(x, t);
kvn@1055 128 // If x is a TypeNode, capture any more-precise type permanently into Node
twisti@2131 129 // otherwise it will be not updated during igvn->transform since
kvn@1055 130 // igvn->type(x) is set to x->Value() already.
kvn@1055 131 x->raise_bottom_type(t);
rkennke@52429 132 Node *y = _igvn.apply_identity(x);
kvn@8732 133 if (y != x) {
duke@1 134 wins++;
duke@1 135 x = y;
duke@1 136 } else {
duke@1 137 y = _igvn.hash_find(x);
kvn@8732 138 if (y) {
duke@1 139 wins++;
duke@1 140 x = y;
duke@1 141 } else {
duke@1 142 // Else x is a new node we are keeping
duke@1 143 // We do not need register_new_node_with_optimizer
duke@1 144 // because set_type has already been called.
duke@1 145 _igvn._worklist.push(x);
duke@1 146 }
duke@1 147 }
duke@1 148 }
duke@1 149 if (x != the_clone && the_clone != NULL)
duke@1 150 _igvn.remove_dead_node(the_clone);
duke@1 151 phi->set_req( i, x );
duke@1 152 }
duke@1 153 // Too few wins?
kvn@8732 154 if (wins <= policy) {
duke@1 155 _igvn.remove_dead_node(phi);
duke@1 156 return NULL;
duke@1 157 }
duke@1 158
duke@1 159 // Record Phi
duke@1 160 register_new_node( phi, region );
duke@1 161
kvn@8732 162 for (uint i2 = 1; i2 < phi->req(); i2++) {
duke@1 163 Node *x = phi->in(i2);
duke@1 164 // If we commoned up the cloned 'x' with another existing Node,
duke@1 165 // the existing Node picks up a new use. We need to make the
duke@1 166 // existing Node occur higher up so it dominates its uses.
duke@1 167 Node *old_ctrl;
duke@1 168 IdealLoopTree *old_loop;
duke@1 169
kvn@8732 170 if (x->is_Con()) {
kvn@8732 171 // Constant's control is always root.
kvn@8732 172 set_ctrl(x, C->root());
kvn@8732 173 continue;
kvn@8732 174 }
duke@1 175 // The occasional new node
kvn@8732 176 if (x->_idx >= old_unique) { // Found a new, unplaced node?
kvn@8732 177 old_ctrl = NULL;
kvn@8732 178 old_loop = NULL; // Not in any prior loop
duke@1 179 } else {
kvn@8732 180 old_ctrl = get_ctrl(x);
duke@1 181 old_loop = get_loop(old_ctrl); // Get prior loop
duke@1 182 }
duke@1 183 // New late point must dominate new use
kvn@8732 184 Node *new_ctrl = dom_lca(old_ctrl, region->in(i2));
kvn@8732 185 if (new_ctrl == old_ctrl) // Nothing is changed
kvn@8732 186 continue;
kvn@8732 187
kvn@8732 188 IdealLoopTree *new_loop = get_loop(new_ctrl);
kvn@8732 189
kvn@8732 190 // Don't move x into a loop if its uses are
kvn@8732 191 // outside of loop. Otherwise x will be cloned
kvn@8732 192 // for each use outside of this loop.
kvn@8732 193 IdealLoopTree *use_loop = get_loop(region);
kvn@8732 194 if (!new_loop->is_member(use_loop) &&
kvn@8732 195 (old_loop == NULL || !new_loop->is_member(old_loop))) {
kvn@8732 196 // Take early control, later control will be recalculated
kvn@8732 197 // during next iteration of loop optimizations.
kvn@8732 198 new_ctrl = get_early_ctrl(x);
kvn@8732 199 new_loop = get_loop(new_ctrl);
kvn@8732 200 }
duke@1 201 // Set new location
duke@1 202 set_ctrl(x, new_ctrl);
duke@1 203 // If changing loop bodies, see if we need to collect into new body
kvn@8732 204 if (old_loop != new_loop) {
kvn@8732 205 if (old_loop && !old_loop->_child)
duke@1 206 old_loop->_body.yank(x);
kvn@8732 207 if (!new_loop->_child)
duke@1 208 new_loop->_body.push(x); // Collect body info
duke@1 209 }
duke@1 210 }
duke@1 211
duke@1 212 return phi;
duke@1 213 }
duke@1 214
duke@1 215 //------------------------------dominated_by------------------------------------
duke@1 216 // Replace the dominated test with an obvious true or false. Place it on the
duke@1 217 // IGVN worklist for later cleanup. Move control-dependent data Nodes on the
duke@1 218 // live path up to the dominating control.
kvn@10253 219 void PhaseIdealLoop::dominated_by( Node *prevdom, Node *iff, bool flip, bool exclude_loop_predicate ) {
twisti@34174 220 if (VerifyLoopOptimizations && PrintOpto) { tty->print_cr("dominating test"); }
duke@1 221
duke@1 222 // prevdom is the dominating projection of the dominating test.
duke@1 223 assert( iff->is_If(), "" );
roland@34164 224 assert(iff->Opcode() == Op_If || iff->Opcode() == Op_CountedLoopEnd || iff->Opcode() == Op_RangeCheck, "Check this code when new subtype is added");
duke@1 225 int pop = prevdom->Opcode();
duke@1 226 assert( pop == Op_IfFalse || pop == Op_IfTrue, "" );
kvn@8732 227 if (flip) {
kvn@8732 228 if (pop == Op_IfTrue)
kvn@8732 229 pop = Op_IfFalse;
kvn@8732 230 else
kvn@8732 231 pop = Op_IfTrue;
kvn@8732 232 }
duke@1 233 // 'con' is set to true or false to kill the dominated test.
duke@1 234 Node *con = _igvn.makecon(pop == Op_IfTrue ? TypeInt::ONE : TypeInt::ZERO);
duke@1 235 set_ctrl(con, C->root()); // Constant gets a new use
duke@1 236 // Hack the dominated test
kvn@12958 237 _igvn.replace_input_of(iff, 1, con);
duke@1 238
duke@1 239 // If I dont have a reachable TRUE and FALSE path following the IfNode then
duke@1 240 // I can assume this path reaches an infinite loop. In this case it's not
duke@1 241 // important to optimize the data Nodes - either the whole compilation will
duke@1 242 // be tossed or this path (and all data Nodes) will go dead.
kvn@8732 243 if (iff->outcnt() != 2) return;
duke@1 244
duke@1 245 // Make control-dependent data Nodes on the live path (path that will remain
duke@1 246 // once the dominated IF is removed) become control-dependent on the
duke@1 247 // dominating projection.
dlong@48595 248 Node* dp = iff->as_If()->proj_out_or_null(pop == Op_IfTrue);
kvn@10253 249
kvn@10253 250 // Loop predicates may have depending checks which should not
kvn@10253 251 // be skipped. For example, range check predicate has two checks
kvn@10253 252 // for lower and upper bounds.
morris@16375 253 if (dp == NULL)
morris@16375 254 return;
morris@16375 255
morris@16375 256 ProjNode* dp_proj = dp->as_Proj();
morris@16375 257 ProjNode* unc_proj = iff->as_If()->proj_out(1 - dp_proj->_con)->as_Proj();
kvn@10253 258 if (exclude_loop_predicate &&
roland@30183 259 (unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_predicate) != NULL ||
roland@50923 260 unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_profile_predicate) != NULL ||
roland@30183 261 unc_proj->is_uncommon_trap_proj(Deoptimization::Reason_range_check) != NULL)) {
roland@28036 262 // If this is a range check (IfNode::is_range_check), do not
roland@28036 263 // reorder because Compile::allow_range_check_smearing might have
roland@28036 264 // changed the check.
kvn@10253 265 return; // Let IGVN transformation change control dependence.
roland@28036 266 }
kvn@10253 267
duke@1 268 IdealLoopTree *old_loop = get_loop(dp);
duke@1 269
duke@1 270 for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) {
duke@1 271 Node* cd = dp->fast_out(i); // Control-dependent node
kvn@8732 272 if (cd->depends_only_on_test()) {
kvn@8732 273 assert(cd->in(0) == dp, "");
kvn@12958 274 _igvn.replace_input_of(cd, 0, prevdom);
kvn@8732 275 set_early_ctrl(cd);
duke@1 276 IdealLoopTree *new_loop = get_loop(get_ctrl(cd));
kvn@8732 277 if (old_loop != new_loop) {
kvn@8732 278 if (!old_loop->_child) old_loop->_body.yank(cd);
kvn@8732 279 if (!new_loop->_child) new_loop->_body.push(cd);
duke@1 280 }
duke@1 281 --i;
duke@1 282 --imax;
duke@1 283 }
duke@1 284 }
duke@1 285 }
duke@1 286
duke@1 287 //------------------------------has_local_phi_input----------------------------
duke@1 288 // Return TRUE if 'n' has Phi inputs from its local block and no other
duke@1 289 // block-local inputs (all non-local-phi inputs come from earlier blocks)
duke@1 290 Node *PhaseIdealLoop::has_local_phi_input( Node *n ) {
duke@1 291 Node *n_ctrl = get_ctrl(n);
duke@1 292 // See if some inputs come from a Phi in this block, or from before
duke@1 293 // this block.
duke@1 294 uint i;
duke@1 295 for( i = 1; i < n->req(); i++ ) {
duke@1 296 Node *phi = n->in(i);
duke@1 297 if( phi->is_Phi() && phi->in(0) == n_ctrl )
duke@1 298 break;
duke@1 299 }
duke@1 300 if( i >= n->req() )
duke@1 301 return NULL; // No Phi inputs; nowhere to clone thru
duke@1 302
duke@1 303 // Check for inputs created between 'n' and the Phi input. These
duke@1 304 // must split as well; they have already been given the chance
duke@1 305 // (courtesy of a post-order visit) and since they did not we must
duke@1 306 // recover the 'cost' of splitting them by being very profitable
duke@1 307 // when splitting 'n'. Since this is unlikely we simply give up.
duke@1 308 for( i = 1; i < n->req(); i++ ) {
duke@1 309 Node *m = n->in(i);
duke@1 310 if( get_ctrl(m) == n_ctrl && !m->is_Phi() ) {
duke@1 311 // We allow the special case of AddP's with no local inputs.
duke@1 312 // This allows us to split-up address expressions.
duke@1 313 if (m->is_AddP() &&
duke@1 314 get_ctrl(m->in(2)) != n_ctrl &&
duke@1 315 get_ctrl(m->in(3)) != n_ctrl) {
duke@1 316 // Move the AddP up to dominating point
roland@48145 317 Node* c = find_non_split_ctrl(idom(n_ctrl));
roland@48145 318 if (c->is_OuterStripMinedLoop()) {
roland@48145 319 c->as_Loop()->verify_strip_mined(1);
roland@48145 320 c = c->in(LoopNode::EntryControl);
roland@48145 321 }
roland@48145 322 set_ctrl_and_loop(m, c);
duke@1 323 continue;
duke@1 324 }
duke@1 325 return NULL;
duke@1 326 }
roland@47590 327 assert(m->is_Phi() || is_dominator(get_ctrl(m), n_ctrl), "m has strange control");
duke@1 328 }
duke@1 329
duke@1 330 return n_ctrl;
duke@1 331 }
duke@1 332
duke@1 333 //------------------------------remix_address_expressions----------------------
duke@1 334 // Rework addressing expressions to get the most loop-invariant stuff
duke@1 335 // moved out. We'd like to do all associative operators, but it's especially
duke@1 336 // important (common) to do address expressions.
duke@1 337 Node *PhaseIdealLoop::remix_address_expressions( Node *n ) {
duke@1 338 if (!has_ctrl(n)) return NULL;
duke@1 339 Node *n_ctrl = get_ctrl(n);
duke@1 340 IdealLoopTree *n_loop = get_loop(n_ctrl);
duke@1 341
duke@1 342 // See if 'n' mixes loop-varying and loop-invariant inputs and
duke@1 343 // itself is loop-varying.
duke@1 344
duke@1 345 // Only interested in binary ops (and AddP)
duke@1 346 if( n->req() < 3 || n->req() > 4 ) return NULL;
duke@1 347
duke@1 348 Node *n1_ctrl = get_ctrl(n->in( 1));
duke@1 349 Node *n2_ctrl = get_ctrl(n->in( 2));
duke@1 350 Node *n3_ctrl = get_ctrl(n->in(n->req() == 3 ? 2 : 3));
duke@1 351 IdealLoopTree *n1_loop = get_loop( n1_ctrl );
duke@1 352 IdealLoopTree *n2_loop = get_loop( n2_ctrl );
duke@1 353 IdealLoopTree *n3_loop = get_loop( n3_ctrl );
duke@1 354
duke@1 355 // Does one of my inputs spin in a tighter loop than self?
duke@1 356 if( (n_loop->is_member( n1_loop ) && n_loop != n1_loop) ||
duke@1 357 (n_loop->is_member( n2_loop ) && n_loop != n2_loop) ||
duke@1 358 (n_loop->is_member( n3_loop ) && n_loop != n3_loop) )
duke@1 359 return NULL; // Leave well enough alone
duke@1 360
duke@1 361 // Is at least one of my inputs loop-invariant?
duke@1 362 if( n1_loop == n_loop &&
duke@1 363 n2_loop == n_loop &&
duke@1 364 n3_loop == n_loop )
duke@1 365 return NULL; // No loop-invariant inputs
duke@1 366
duke@1 367
duke@1 368 int n_op = n->Opcode();
duke@1 369
duke@1 370 // Replace expressions like ((V+I) << 2) with (V<<2 + I<<2).
duke@1 371 if( n_op == Op_LShiftI ) {
duke@1 372 // Scale is loop invariant
duke@1 373 Node *scale = n->in(2);
duke@1 374 Node *scale_ctrl = get_ctrl(scale);
duke@1 375 IdealLoopTree *scale_loop = get_loop(scale_ctrl );
duke@1 376 if( n_loop == scale_loop || !scale_loop->is_member( n_loop ) )
duke@1 377 return NULL;
duke@1 378 const TypeInt *scale_t = scale->bottom_type()->isa_int();
duke@1 379 if( scale_t && scale_t->is_con() && scale_t->get_con() >= 16 )
duke@1 380 return NULL; // Dont bother with byte/short masking
duke@1 381 // Add must vary with loop (else shift would be loop-invariant)
duke@1 382 Node *add = n->in(1);
duke@1 383 Node *add_ctrl = get_ctrl(add);
duke@1 384 IdealLoopTree *add_loop = get_loop(add_ctrl);
duke@1 385 //assert( n_loop == add_loop, "" );
duke@1 386 if( n_loop != add_loop ) return NULL; // happens w/ evil ZKM loops
duke@1 387
duke@1 388 // Convert I-V into I+ (0-V); same for V-I
duke@1 389 if( add->Opcode() == Op_SubI &&
duke@1 390 _igvn.type( add->in(1) ) != TypeInt::ZERO ) {
duke@1 391 Node *zero = _igvn.intcon(0);
duke@1 392 set_ctrl(zero, C->root());
thartmann@24923 393 Node *neg = new SubINode( _igvn.intcon(0), add->in(2) );
duke@1 394 register_new_node( neg, get_ctrl(add->in(2) ) );
thartmann@24923 395 add = new AddINode( add->in(1), neg );
duke@1 396 register_new_node( add, add_ctrl );
duke@1 397 }
duke@1 398 if( add->Opcode() != Op_AddI ) return NULL;
duke@1 399 // See if one add input is loop invariant
duke@1 400 Node *add_var = add->in(1);
duke@1 401 Node *add_var_ctrl = get_ctrl(add_var);
duke@1 402 IdealLoopTree *add_var_loop = get_loop(add_var_ctrl );
duke@1 403 Node *add_invar = add->in(2);
duke@1 404 Node *add_invar_ctrl = get_ctrl(add_invar);
duke@1 405 IdealLoopTree *add_invar_loop = get_loop(add_invar_ctrl );
duke@1 406 if( add_var_loop == n_loop ) {
duke@1 407 } else if( add_invar_loop == n_loop ) {
duke@1 408 // Swap to find the invariant part
duke@1 409 add_invar = add_var;
duke@1 410 add_invar_ctrl = add_var_ctrl;
duke@1 411 add_invar_loop = add_var_loop;
duke@1 412 add_var = add->in(2);
duke@1 413 Node *add_var_ctrl = get_ctrl(add_var);
duke@1 414 IdealLoopTree *add_var_loop = get_loop(add_var_ctrl );
duke@1 415 } else // Else neither input is loop invariant
duke@1 416 return NULL;
duke@1 417 if( n_loop == add_invar_loop || !add_invar_loop->is_member( n_loop ) )
duke@1 418 return NULL; // No invariant part of the add?
duke@1 419
duke@1 420 // Yes! Reshape address expression!
thartmann@24923 421 Node *inv_scale = new LShiftINode( add_invar, scale );
never@3267 422 Node *inv_scale_ctrl =
never@3267 423 dom_depth(add_invar_ctrl) > dom_depth(scale_ctrl) ?
never@3267 424 add_invar_ctrl : scale_ctrl;
never@3267 425 register_new_node( inv_scale, inv_scale_ctrl );
thartmann@24923 426 Node *var_scale = new LShiftINode( add_var, scale );
duke@1 427 register_new_node( var_scale, n_ctrl );
thartmann@24923 428 Node *var_add = new AddINode( var_scale, inv_scale );
duke@1 429 register_new_node( var_add, n_ctrl );
kvn@5901 430 _igvn.replace_node( n, var_add );
duke@1 431 return var_add;
duke@1 432 }
duke@1 433
duke@1 434 // Replace (I+V) with (V+I)
duke@1 435 if( n_op == Op_AddI ||
duke@1 436 n_op == Op_AddL ||
duke@1 437 n_op == Op_AddF ||
duke@1 438 n_op == Op_AddD ||
duke@1 439 n_op == Op_MulI ||
duke@1 440 n_op == Op_MulL ||
duke@1 441 n_op == Op_MulF ||
duke@1 442 n_op == Op_MulD ) {
duke@1 443 if( n2_loop == n_loop ) {
duke@1 444 assert( n1_loop != n_loop, "" );
duke@1 445 n->swap_edges(1, 2);
duke@1 446 }
duke@1 447 }
duke@1 448
duke@1 449 // Replace ((I1 +p V) +p I2) with ((I1 +p I2) +p V),
duke@1 450 // but not if I2 is a constant.
duke@1 451 if( n_op == Op_AddP ) {
duke@1 452 if( n2_loop == n_loop && n3_loop != n_loop ) {
duke@1 453 if( n->in(2)->Opcode() == Op_AddP && !n->in(3)->is_Con() ) {
duke@1 454 Node *n22_ctrl = get_ctrl(n->in(2)->in(2));
duke@1 455 Node *n23_ctrl = get_ctrl(n->in(2)->in(3));
duke@1 456 IdealLoopTree *n22loop = get_loop( n22_ctrl );
duke@1 457 IdealLoopTree *n23_loop = get_loop( n23_ctrl );
duke@1 458 if( n22loop != n_loop && n22loop->is_member(n_loop) &&
duke@1 459 n23_loop == n_loop ) {
thartmann@24923 460 Node *add1 = new AddPNode( n->in(1), n->in(2)->in(2), n->in(3) );
duke@1 461 // Stuff new AddP in the loop preheader
duke@1 462 register_new_node( add1, n_loop->_head->in(LoopNode::EntryControl) );
thartmann@24923 463 Node *add2 = new AddPNode( n->in(1), add1, n->in(2)->in(3) );
duke@1 464 register_new_node( add2, n_ctrl );
kvn@5901 465 _igvn.replace_node( n, add2 );
duke@1 466 return add2;
duke@1 467 }
duke@1 468 }
duke@1 469 }
duke@1 470
duke@1 471 // Replace (I1 +p (I2 + V)) with ((I1 +p I2) +p V)
roland@33082 472 if (n2_loop != n_loop && n3_loop == n_loop) {
roland@33082 473 if (n->in(3)->Opcode() == Op_AddX) {
duke@1 474 Node *V = n->in(3)->in(1);
duke@1 475 Node *I = n->in(3)->in(2);
roland@33082 476 if (is_member(n_loop,get_ctrl(V))) {
duke@1 477 } else {
duke@1 478 Node *tmp = V; V = I; I = tmp;
duke@1 479 }
roland@33082 480 if (!is_member(n_loop,get_ctrl(I))) {
roland@33082 481 Node *add1 = new AddPNode(n->in(1), n->in(2), I);
duke@1 482 // Stuff new AddP in the loop preheader
roland@33082 483 register_new_node(add1, n_loop->_head->in(LoopNode::EntryControl));
roland@33082 484 Node *add2 = new AddPNode(n->in(1), add1, V);
roland@33082 485 register_new_node(add2, n_ctrl);
roland@33082 486 _igvn.replace_node(n, add2);
duke@1 487 return add2;
duke@1 488 }
duke@1 489 }
duke@1 490 }
duke@1 491 }
duke@1 492
duke@1 493 return NULL;
duke@1 494 }
duke@1 495
vdeshpande@52992 496 // Optimize ((in1[2*i] * in2[2*i]) + (in1[2*i+1] * in2[2*i+1]))
vdeshpande@52992 497 Node *PhaseIdealLoop::convert_add_to_muladd(Node* n) {
vdeshpande@52992 498 assert(n->Opcode() == Op_AddI, "sanity");
vdeshpande@52992 499 Node * nn = NULL;
vdeshpande@52992 500 Node * in1 = n->in(1);
vdeshpande@52992 501 Node * in2 = n->in(2);
vdeshpande@52992 502 if (in1->Opcode() == Op_MulI && in2->Opcode() == Op_MulI) {
vdeshpande@52992 503 IdealLoopTree* loop_n = get_loop(get_ctrl(n));
vdeshpande@52992 504 if (loop_n->_head->as_Loop()->is_valid_counted_loop() &&
vdeshpande@52992 505 Matcher::match_rule_supported(Op_MulAddS2I) &&
vdeshpande@52992 506 Matcher::match_rule_supported(Op_MulAddVS2VI)) {
vdeshpande@52992 507 Node* mul_in1 = in1->in(1);
vdeshpande@52992 508 Node* mul_in2 = in1->in(2);
vdeshpande@52992 509 Node* mul_in3 = in2->in(1);
vdeshpande@52992 510 Node* mul_in4 = in2->in(2);
vdeshpande@52992 511 if (mul_in1->Opcode() == Op_LoadS &&
vdeshpande@52992 512 mul_in2->Opcode() == Op_LoadS &&
vdeshpande@52992 513 mul_in3->Opcode() == Op_LoadS &&
vdeshpande@52992 514 mul_in4->Opcode() == Op_LoadS) {
vdeshpande@52992 515 IdealLoopTree* loop1 = get_loop(get_ctrl(mul_in1));
vdeshpande@52992 516 IdealLoopTree* loop2 = get_loop(get_ctrl(mul_in2));
vdeshpande@52992 517 IdealLoopTree* loop3 = get_loop(get_ctrl(mul_in3));
vdeshpande@52992 518 IdealLoopTree* loop4 = get_loop(get_ctrl(mul_in4));
vdeshpande@52992 519 IdealLoopTree* loop5 = get_loop(get_ctrl(in1));
vdeshpande@52992 520 IdealLoopTree* loop6 = get_loop(get_ctrl(in2));
vdeshpande@52992 521 // All nodes should be in the same counted loop.
vdeshpande@52992 522 if (loop_n == loop1 && loop_n == loop2 && loop_n == loop3 &&
vdeshpande@52992 523 loop_n == loop4 && loop_n == loop5 && loop_n == loop6) {
vdeshpande@52992 524 Node* adr1 = mul_in1->in(MemNode::Address);
vdeshpande@52992 525 Node* adr2 = mul_in2->in(MemNode::Address);
vdeshpande@52992 526 Node* adr3 = mul_in3->in(MemNode::Address);
vdeshpande@52992 527 Node* adr4 = mul_in4->in(MemNode::Address);
vdeshpande@52992 528 if (adr1->is_AddP() && adr2->is_AddP() && adr3->is_AddP() && adr4->is_AddP()) {
vdeshpande@52992 529 if ((adr1->in(AddPNode::Base) == adr3->in(AddPNode::Base)) &&
vdeshpande@52992 530 (adr2->in(AddPNode::Base) == adr4->in(AddPNode::Base))) {
vdeshpande@52992 531 nn = new MulAddS2INode(mul_in1, mul_in2, mul_in3, mul_in4);
vdeshpande@52992 532 register_new_node(nn, get_ctrl(n));
vdeshpande@52992 533 _igvn.replace_node(n, nn);
vdeshpande@52992 534 return nn;
vdeshpande@53336 535 } else if ((adr1->in(AddPNode::Base) == adr4->in(AddPNode::Base)) &&
vdeshpande@53336 536 (adr2->in(AddPNode::Base) == adr3->in(AddPNode::Base))) {
vdeshpande@53336 537 nn = new MulAddS2INode(mul_in1, mul_in2, mul_in4, mul_in3);
vdeshpande@53336 538 register_new_node(nn, get_ctrl(n));
vdeshpande@53336 539 _igvn.replace_node(n, nn);
vdeshpande@53336 540 return nn;
vdeshpande@52992 541 }
vdeshpande@52992 542 }
vdeshpande@52992 543 }
vdeshpande@52992 544 }
vdeshpande@52992 545 }
vdeshpande@52992 546 }
vdeshpande@52992 547 return nn;
vdeshpande@52992 548 }
vdeshpande@52992 549
duke@1 550 //------------------------------conditional_move-------------------------------
duke@1 551 // Attempt to replace a Phi with a conditional move. We have some pretty
duke@1 552 // strict profitability requirements. All Phis at the merge point must
duke@1 553 // be converted, so we can remove the control flow. We need to limit the
duke@1 554 // number of c-moves to a small handful. All code that was in the side-arms
duke@1 555 // of the CFG diamond is now speculatively executed. This code has to be
duke@1 556 // "cheap enough". We are pretty much limited to CFG diamonds that merge
duke@1 557 // 1 or 2 items with a total of 1 or 2 ops executed speculatively.
duke@1 558 Node *PhaseIdealLoop::conditional_move( Node *region ) {
duke@1 559
kvn@10971 560 assert(region->is_Region(), "sanity check");
kvn@10971 561 if (region->req() != 3) return NULL;
duke@1 562
duke@1 563 // Check for CFG diamond
duke@1 564 Node *lp = region->in(1);
duke@1 565 Node *rp = region->in(2);
kvn@10971 566 if (!lp || !rp) return NULL;
duke@1 567 Node *lp_c = lp->in(0);
kvn@10971 568 if (lp_c == NULL || lp_c != rp->in(0) || !lp_c->is_If()) return NULL;
duke@1 569 IfNode *iff = lp_c->as_If();
duke@1 570
duke@1 571 // Check for ops pinned in an arm of the diamond.
duke@1 572 // Can't remove the control flow in this case
kvn@10971 573 if (lp->outcnt() > 1) return NULL;
kvn@10971 574 if (rp->outcnt() > 1) return NULL;
kvn@10971 575
kvn@10971 576 IdealLoopTree* r_loop = get_loop(region);
kvn@10971 577 assert(r_loop == get_loop(iff), "sanity");
kvn@10971 578 // Always convert to CMOVE if all results are used only outside this loop.
kvn@10971 579 bool used_inside_loop = (r_loop == _ltree_root);
duke@1 580
duke@1 581 // Check profitability
duke@1 582 int cost = 0;
kvn@208 583 int phis = 0;
duke@1 584 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
duke@1 585 Node *out = region->fast_out(i);
kvn@10971 586 if (!out->is_Phi()) continue; // Ignore other control edges, etc
kvn@208 587 phis++;
duke@1 588 PhiNode* phi = out->as_Phi();
kvn@10971 589 BasicType bt = phi->type()->basic_type();
kvn@10971 590 switch (bt) {
iveresov@33469 591 case T_DOUBLE:
kvn@48309 592 case T_FLOAT:
iveresov@33469 593 if (C->use_cmove()) {
iveresov@33469 594 continue; //TODO: maybe we want to add some cost
iveresov@33469 595 }
kvn@10971 596 cost += Matcher::float_cmove_cost(); // Could be very expensive
kvn@10971 597 break;
kvn@10971 598 case T_LONG: {
kvn@10971 599 cost += Matcher::long_cmove_cost(); // May encodes as 2 CMOV's
kvn@10971 600 }
duke@1 601 case T_INT: // These all CMOV fine
kvn@10971 602 case T_ADDRESS: { // (RawPtr)
duke@1 603 cost++;
duke@1 604 break;
kvn@10971 605 }
kvn@1055 606 case T_NARROWOOP: // Fall through
duke@1 607 case T_OBJECT: { // Base oops are OK, but not derived oops
kvn@1055 608 const TypeOopPtr *tp = phi->type()->make_ptr()->isa_oopptr();
duke@1 609 // Derived pointers are Bad (tm): what's the Base (for GC purposes) of a
duke@1 610 // CMOVE'd derived pointer? It's a CMOVE'd derived base. Thus
duke@1 611 // CMOVE'ing a derived pointer requires we also CMOVE the base. If we
duke@1 612 // have a Phi for the base here that we convert to a CMOVE all is well
duke@1 613 // and good. But if the base is dead, we'll not make a CMOVE. Later
duke@1 614 // the allocator will have to produce a base by creating a CMOVE of the
duke@1 615 // relevant bases. This puts the allocator in the business of
duke@1 616 // manufacturing expensive instructions, generally a bad plan.
duke@1 617 // Just Say No to Conditionally-Moved Derived Pointers.
kvn@10971 618 if (tp && tp->offset() != 0)
duke@1 619 return NULL;
duke@1 620 cost++;
duke@1 621 break;
duke@1 622 }
duke@1 623 default:
duke@1 624 return NULL; // In particular, can't do memory or I/O
duke@1 625 }
duke@1 626 // Add in cost any speculative ops
kvn@10971 627 for (uint j = 1; j < region->req(); j++) {
duke@1 628 Node *proj = region->in(j);
duke@1 629 Node *inp = phi->in(j);
duke@1 630 if (get_ctrl(inp) == proj) { // Found local op
duke@1 631 cost++;
duke@1 632 // Check for a chain of dependent ops; these will all become
duke@1 633 // speculative in a CMOV.
kvn@10971 634 for (uint k = 1; k < inp->req(); k++)
duke@1 635 if (get_ctrl(inp->in(k)) == proj)
kvn@10971 636 cost += ConditionalMoveLimit; // Too much speculative goo
duke@1 637 }
duke@1 638 }
kvn@1055 639 // See if the Phi is used by a Cmp or Narrow oop Decode/Encode.
kvn@1055 640 // This will likely Split-If, a higher-payoff operation.
duke@1 641 for (DUIterator_Fast kmax, k = phi->fast_outs(kmax); k < kmax; k++) {
duke@1 642 Node* use = phi->fast_out(k);
roland@13969 643 if (use->is_Cmp() || use->is_DecodeNarrowPtr() || use->is_EncodeNarrowPtr())
kvn@10971 644 cost += ConditionalMoveLimit;
kvn@10971 645 // Is there a use inside the loop?
kvn@10971 646 // Note: check only basic types since CMoveP is pinned.
kvn@10971 647 if (!used_inside_loop && is_java_primitive(bt)) {
kvn@10971 648 IdealLoopTree* u_loop = get_loop(has_ctrl(use) ? get_ctrl(use) : use);
kvn@10971 649 if (r_loop == u_loop || r_loop->is_member(u_loop)) {
kvn@10971 650 used_inside_loop = true;
kvn@10971 651 }
kvn@10971 652 }
duke@1 653 }
iveresov@33469 654 }//for
kvn@208 655 Node* bol = iff->in(1);
kvn@10971 656 assert(bol->Opcode() == Op_Bool, "");
kvn@208 657 int cmp_op = bol->in(1)->Opcode();
kvn@208 658 // It is expensive to generate flags from a float compare.
kvn@208 659 // Avoid duplicated float compare.
kvn@10971 660 if (phis > 1 && (cmp_op == Op_CmpF || cmp_op == Op_CmpD)) return NULL;
kvn@10971 661
kvn@10971 662 float infrequent_prob = PROB_UNLIKELY_MAG(3);
kvn@10971 663 // Ignore cost and blocks frequency if CMOVE can be moved outside the loop.
kvn@10971 664 if (used_inside_loop) {
kvn@10971 665 if (cost >= ConditionalMoveLimit) return NULL; // Too much goo
kvn@10971 666
kvn@10971 667 // BlockLayoutByFrequency optimization moves infrequent branch
kvn@10971 668 // from hot path. No point in CMOV'ing in such case (110 is used
kvn@10971 669 // instead of 100 to take into account not exactness of float value).
kvn@10971 670 if (BlockLayoutByFrequency) {
kvn@10971 671 infrequent_prob = MAX2(infrequent_prob, (float)BlockLayoutMinDiamondPercentage/110.0f);
kvn@10971 672 }
kvn@10971 673 }
kvn@10971 674 // Check for highly predictable branch. No point in CMOV'ing if
kvn@10971 675 // we are going to predict accurately all the time.
kvn@48309 676 if (C->use_cmove() && (cmp_op == Op_CmpF || cmp_op == Op_CmpD)) {
kvn@48309 677 //keep going
kvn@48309 678 } else if (iff->_prob < infrequent_prob ||
kvn@10971 679 iff->_prob > (1.0f - infrequent_prob))
kvn@10971 680 return NULL;
duke@1 681
duke@1 682 // --------------
duke@1 683 // Now replace all Phis with CMOV's
duke@1 684 Node *cmov_ctrl = iff->in(0);
duke@1 685 uint flip = (lp->Opcode() == Op_IfTrue);
roland@47590 686 Node_List wq;
kvn@10971 687 while (1) {
duke@1 688 PhiNode* phi = NULL;
duke@1 689 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
duke@1 690 Node *out = region->fast_out(i);
duke@1 691 if (out->is_Phi()) {
duke@1 692 phi = out->as_Phi();
duke@1 693 break;
duke@1 694 }
duke@1 695 }
duke@1 696 if (phi == NULL) break;
twisti@34174 697 if (PrintOpto && VerifyLoopOptimizations) { tty->print_cr("CMOV"); }
duke@1 698 // Move speculative ops
roland@47590 699 wq.push(phi);
roland@47590 700 while (wq.size() > 0) {
roland@47590 701 Node *n = wq.pop();
roland@47590 702 for (uint j = 1; j < n->req(); j++) {
roland@47590 703 Node* m = n->in(j);
roland@47590 704 if (m != NULL && !is_dominator(get_ctrl(m), cmov_ctrl)) {
duke@1 705 #ifndef PRODUCT
roland@47590 706 if (PrintOpto && VerifyLoopOptimizations) {
roland@47590 707 tty->print(" speculate: ");
roland@47590 708 m->dump();
roland@47590 709 }
roland@47590 710 #endif
roland@47590 711 set_ctrl(m, cmov_ctrl);
roland@47590 712 wq.push(m);
duke@1 713 }
duke@1 714 }
duke@1 715 }
thartmann@25930 716 Node *cmov = CMoveNode::make(cmov_ctrl, iff->in(1), phi->in(1+flip), phi->in(2-flip), _igvn.type(phi));
duke@1 717 register_new_node( cmov, cmov_ctrl );
kvn@5901 718 _igvn.replace_node( phi, cmov );
duke@1 719 #ifndef PRODUCT
kvn@10971 720 if (TraceLoopOpts) {
kvn@10971 721 tty->print("CMOV ");
kvn@10971 722 r_loop->dump_head();
kvn@10988 723 if (Verbose) {
kvn@10971 724 bol->in(1)->dump(1);
kvn@10971 725 cmov->dump(1);
kvn@10988 726 }
kvn@10971 727 }
kvn@10971 728 if (VerifyLoopOptimizations) verify();
duke@1 729 #endif
duke@1 730 }
duke@1 731
duke@1 732 // The useless CFG diamond will fold up later; see the optimization in
duke@1 733 // RegionNode::Ideal.
duke@1 734 _igvn._worklist.push(region);
duke@1 735
duke@1 736 return iff->in(1);
duke@1 737 }
duke@1 738
roland@32372 739 static void enqueue_cfg_uses(Node* m, Unique_Node_List& wq) {
roland@32372 740 for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax; i++) {
roland@32372 741 Node* u = m->fast_out(i);
roland@32372 742 if (u->is_CFG()) {
roland@32372 743 if (u->Opcode() == Op_NeverBranch) {
roland@32372 744 u = ((NeverBranchNode*)u)->proj_out(0);
roland@32372 745 enqueue_cfg_uses(u, wq);
roland@32372 746 } else {
roland@32372 747 wq.push(u);
roland@32372 748 }
roland@32372 749 }
roland@32372 750 }
roland@32372 751 }
roland@32372 752
roland@32372 753 // Try moving a store out of a loop, right before the loop
roland@32372 754 Node* PhaseIdealLoop::try_move_store_before_loop(Node* n, Node *n_ctrl) {
roland@32372 755 // Store has to be first in the loop body
roland@32372 756 IdealLoopTree *n_loop = get_loop(n_ctrl);
roland@48964 757 if (n->is_Store() && n_loop != _ltree_root &&
roland@48964 758 n_loop->is_loop() && n_loop->_head->is_Loop() &&
roland@48964 759 n->in(0) != NULL) {
roland@32372 760 Node* address = n->in(MemNode::Address);
roland@32372 761 Node* value = n->in(MemNode::ValueIn);
roland@32372 762 Node* mem = n->in(MemNode::Memory);
roland@32372 763 IdealLoopTree* address_loop = get_loop(get_ctrl(address));
roland@32372 764 IdealLoopTree* value_loop = get_loop(get_ctrl(value));
roland@32372 765
roland@32372 766 // - address and value must be loop invariant
roland@32372 767 // - memory must be a memory Phi for the loop
roland@32372 768 // - Store must be the only store on this memory slice in the
roland@32372 769 // loop: if there's another store following this one then value
roland@32372 770 // written at iteration i by the second store could be overwritten
roland@32372 771 // at iteration i+n by the first store: it's not safe to move the
roland@32372 772 // first store out of the loop
roland@33062 773 // - nothing must observe the memory Phi: it guarantees no read
roland@33062 774 // before the store, we are also guaranteed the store post
roland@33062 775 // dominates the loop head (ignoring a possible early
roland@33062 776 // exit). Otherwise there would be extra Phi involved between the
roland@33062 777 // loop's Phi and the store.
roland@33062 778 // - there must be no early exit from the loop before the Store
roland@33062 779 // (such an exit most of the time would be an extra use of the
roland@33062 780 // memory Phi but sometimes is a bottom memory Phi that takes the
roland@33062 781 // store as input).
roland@32372 782
roland@32372 783 if (!n_loop->is_member(address_loop) &&
roland@32372 784 !n_loop->is_member(value_loop) &&
roland@32372 785 mem->is_Phi() && mem->in(0) == n_loop->_head &&
roland@32372 786 mem->outcnt() == 1 &&
roland@32372 787 mem->in(LoopNode::LoopBackControl) == n) {
roland@32372 788
roland@33062 789 assert(n_loop->_tail != NULL, "need a tail");
roland@33062 790 assert(is_dominator(n_ctrl, n_loop->_tail), "store control must not be in a branch in the loop");
roland@33062 791
roland@33062 792 // Verify that there's no early exit of the loop before the store.
roland@32372 793 bool ctrl_ok = false;
roland@32372 794 {
roland@32372 795 // Follow control from loop head until n, we exit the loop or
roland@32372 796 // we reach the tail
roland@32372 797 ResourceMark rm;
roland@32372 798 Unique_Node_List wq;
roland@32372 799 wq.push(n_loop->_head);
roland@33062 800
roland@32372 801 for (uint next = 0; next < wq.size(); ++next) {
roland@32372 802 Node *m = wq.at(next);
roland@32372 803 if (m == n->in(0)) {
roland@32372 804 ctrl_ok = true;
roland@32372 805 continue;
roland@32372 806 }
roland@32372 807 assert(!has_ctrl(m), "should be CFG");
roland@32372 808 if (!n_loop->is_member(get_loop(m)) || m == n_loop->_tail) {
roland@32372 809 ctrl_ok = false;
roland@32372 810 break;
roland@32372 811 }
roland@32372 812 enqueue_cfg_uses(m, wq);
roland@33062 813 if (wq.size() > 10) {
roland@33062 814 ctrl_ok = false;
roland@33062 815 break;
roland@33062 816 }
roland@32372 817 }
roland@32372 818 }
roland@33062 819 if (ctrl_ok) {
roland@33062 820 // move the Store
roland@33062 821 _igvn.replace_input_of(mem, LoopNode::LoopBackControl, mem);
roland@48145 822 _igvn.replace_input_of(n, 0, n_loop->_head->as_Loop()->skip_strip_mined()->in(LoopNode::EntryControl));
roland@33062 823 _igvn.replace_input_of(n, MemNode::Memory, mem->in(LoopNode::EntryControl));
roland@33062 824 // Disconnect the phi now. An empty phi can confuse other
roland@33062 825 // optimizations in this pass of loop opts.
roland@33062 826 _igvn.replace_node(mem, mem->in(LoopNode::EntryControl));
roland@33062 827 n_loop->_body.yank(mem);
roland@32372 828
roland@33062 829 set_ctrl_and_loop(n, n->in(0));
roland@32372 830
roland@33062 831 return n;
roland@33062 832 }
roland@32372 833 }
roland@32372 834 }
roland@32372 835 return NULL;
roland@32372 836 }
roland@32372 837
roland@32372 838 // Try moving a store out of a loop, right after the loop
roland@32372 839 void PhaseIdealLoop::try_move_store_after_loop(Node* n) {
roland@32465 840 if (n->is_Store() && n->in(0) != NULL) {
roland@32372 841 Node *n_ctrl = get_ctrl(n);
roland@32372 842 IdealLoopTree *n_loop = get_loop(n_ctrl);
roland@32372 843 // Store must be in a loop
roland@32372 844 if (n_loop != _ltree_root && !n_loop->_irreducible) {
roland@32372 845 Node* address = n->in(MemNode::Address);
roland@32372 846 Node* value = n->in(MemNode::ValueIn);
roland@32372 847 IdealLoopTree* address_loop = get_loop(get_ctrl(address));
roland@32372 848 // address must be loop invariant
roland@32372 849 if (!n_loop->is_member(address_loop)) {
roland@32372 850 // Store must be last on this memory slice in the loop and
roland@32372 851 // nothing in the loop must observe it
roland@32372 852 Node* phi = NULL;
roland@32372 853 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
roland@32372 854 Node* u = n->fast_out(i);
roland@32372 855 if (has_ctrl(u)) { // control use?
roland@32372 856 IdealLoopTree *u_loop = get_loop(get_ctrl(u));
roland@32372 857 if (!n_loop->is_member(u_loop)) {
roland@32372 858 continue;
roland@32372 859 }
roland@32372 860 if (u->is_Phi() && u->in(0) == n_loop->_head) {
roland@32372 861 assert(_igvn.type(u) == Type::MEMORY, "bad phi");
roland@33078 862 // multiple phis on the same slice are possible
roland@33078 863 if (phi != NULL) {
roland@33078 864 return;
roland@33078 865 }
roland@32372 866 phi = u;
roland@32372 867 continue;
roland@32372 868 }
roland@32372 869 }
roland@33078 870 return;
roland@32372 871 }
roland@32372 872 if (phi != NULL) {
roland@32372 873 // Nothing in the loop before the store (next iteration)
roland@32372 874 // must observe the stored value
roland@32372 875 bool mem_ok = true;
roland@32372 876 {
roland@32372 877 ResourceMark rm;
roland@32372 878 Unique_Node_List wq;
roland@32372 879 wq.push(phi);
roland@32372 880 for (uint next = 0; next < wq.size() && mem_ok; ++next) {
roland@32372 881 Node *m = wq.at(next);
roland@32372 882 for (DUIterator_Fast imax, i = m->fast_outs(imax); i < imax && mem_ok; i++) {
roland@32372 883 Node* u = m->fast_out(i);
roland@32372 884 if (u->is_Store() || u->is_Phi()) {
roland@32372 885 if (u != n) {
roland@32372 886 wq.push(u);
roland@32372 887 mem_ok = (wq.size() <= 10);
roland@32372 888 }
roland@32372 889 } else {
roland@32372 890 mem_ok = false;
roland@32372 891 break;
roland@32372 892 }
roland@32372 893 }
roland@32372 894 }
roland@32372 895 }
roland@32372 896 if (mem_ok) {
thartmann@47623 897 // Move the store out of the loop if the LCA of all
thartmann@47623 898 // users (except for the phi) is outside the loop.
thartmann@47623 899 Node* hook = new Node(1);
roland@32372 900 _igvn.rehash_node_delayed(phi);
thartmann@47623 901 int count = phi->replace_edge(n, hook);
roland@32372 902 assert(count > 0, "inconsistent phi");
roland@32372 903
thartmann@47623 904 // Compute latest point this store can go
thartmann@47623 905 Node* lca = get_late_ctrl(n, get_ctrl(n));
thartmann@47623 906 if (n_loop->is_member(get_loop(lca))) {
thartmann@47623 907 // LCA is in the loop - bail out
thartmann@47623 908 _igvn.replace_node(hook, n);
thartmann@47623 909 return;
roland@32372 910 }
roland@48145 911 #ifdef ASSERT
roland@48145 912 if (n_loop->_head->is_Loop() && n_loop->_head->as_Loop()->is_strip_mined()) {
roland@48145 913 assert(n_loop->_head->Opcode() == Op_CountedLoop, "outer loop is a strip mined");
roland@48145 914 n_loop->_head->as_Loop()->verify_strip_mined(1);
roland@48145 915 Node* outer = n_loop->_head->as_CountedLoop()->outer_loop();
roland@48145 916 IdealLoopTree* outer_loop = get_loop(outer);
roland@48145 917 assert(n_loop->_parent == outer_loop, "broken loop tree");
roland@48145 918 assert(get_loop(lca) == outer_loop, "safepoint in outer loop consume all memory state");
roland@48145 919 }
roland@48145 920 #endif
roland@32372 921
thartmann@47623 922 // Move store out of the loop
thartmann@47623 923 _igvn.replace_node(hook, n->in(MemNode::Memory));
thartmann@47623 924 _igvn.replace_input_of(n, 0, lca);
thartmann@47623 925 set_ctrl_and_loop(n, lca);
roland@32372 926
roland@32372 927 // Disconnect the phi now. An empty phi can confuse other
roland@32372 928 // optimizations in this pass of loop opts..
roland@32372 929 if (phi->in(LoopNode::LoopBackControl) == phi) {
roland@32372 930 _igvn.replace_node(phi, phi->in(LoopNode::EntryControl));
roland@32372 931 n_loop->_body.yank(phi);
roland@32372 932 }
roland@32372 933 }
roland@32372 934 }
roland@32372 935 }
roland@32372 936 }
roland@32372 937 }
roland@32372 938 }
roland@32372 939
duke@1 940 //------------------------------split_if_with_blocks_pre-----------------------
duke@1 941 // Do the real work in a non-recursive function. Data nodes want to be
duke@1 942 // cloned in the pre-order so they can feed each other nicely.
duke@1 943 Node *PhaseIdealLoop::split_if_with_blocks_pre( Node *n ) {
roland@52712 944 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
roland@52712 945 Node* bs_res = bs->split_if_pre(this, n);
roland@52712 946 if (bs_res != NULL) {
roland@52712 947 return bs_res;
roland@52712 948 }
duke@1 949 // Cloning these guys is unlikely to win
duke@1 950 int n_op = n->Opcode();
duke@1 951 if( n_op == Op_MergeMem ) return n;
duke@1 952 if( n->is_Proj() ) return n;
duke@1 953 // Do not clone-up CmpFXXX variations, as these are always
duke@1 954 // followed by a CmpI
duke@1 955 if( n->is_Cmp() ) return n;
duke@1 956 // Attempt to use a conditional move instead of a phi/branch
duke@1 957 if( ConditionalMoveLimit > 0 && n_op == Op_Region ) {
duke@1 958 Node *cmov = conditional_move( n );
duke@1 959 if( cmov ) return cmov;
duke@1 960 }
kvn@961 961 if( n->is_CFG() || n->is_LoadStore() )
kvn@961 962 return n;
duke@1 963 if( n_op == Op_Opaque1 || // Opaque nodes cannot be mod'd
duke@1 964 n_op == Op_Opaque2 ) {
duke@1 965 if( !C->major_progress() ) // If chance of no more loop opts...
duke@1 966 _igvn._worklist.push(n); // maybe we'll remove them
duke@1 967 return n;
duke@1 968 }
duke@1 969
duke@1 970 if( n->is_Con() ) return n; // No cloning for Con nodes
duke@1 971
duke@1 972 Node *n_ctrl = get_ctrl(n);
duke@1 973 if( !n_ctrl ) return n; // Dead node
duke@1 974
roland@32372 975 Node* res = try_move_store_before_loop(n, n_ctrl);
roland@32372 976 if (res != NULL) {
roland@32372 977 return n;
roland@32372 978 }
roland@32372 979
duke@1 980 // Attempt to remix address expressions for loop invariants
duke@1 981 Node *m = remix_address_expressions( n );
duke@1 982 if( m ) return m;
duke@1 983
vdeshpande@52992 984 if (n_op == Op_AddI) {
vdeshpande@52992 985 Node *nn = convert_add_to_muladd( n );
vdeshpande@52992 986 if ( nn ) return nn;
vdeshpande@52992 987 }
vdeshpande@52992 988
roland@35545 989 if (n->is_ConstraintCast()) {
thartmann@46532 990 Node* dom_cast = n->as_ConstraintCast()->dominating_cast(&_igvn, this);
vlivanov@45425 991 // ConstraintCastNode::dominating_cast() uses node control input to determine domination.
vlivanov@45425 992 // Node control inputs don't necessarily agree with loop control info (due to
vlivanov@45425 993 // transformations happened in between), thus additional dominance check is needed
vlivanov@45425 994 // to keep loop info valid.
vlivanov@45425 995 if (dom_cast != NULL && is_dominator(get_ctrl(dom_cast), get_ctrl(n))) {
roland@35545 996 _igvn.replace_node(n, dom_cast);
roland@35545 997 return dom_cast;
roland@35545 998 }
roland@35545 999 }
roland@35545 1000
duke@1 1001 // Determine if the Node has inputs from some local Phi.
duke@1 1002 // Returns the block to clone thru.
duke@1 1003 Node *n_blk = has_local_phi_input( n );
duke@1 1004 if( !n_blk ) return n;
roland@32372 1005
duke@1 1006 // Do not clone the trip counter through on a CountedLoop
duke@1 1007 // (messes up the canonical shape).
duke@1 1008 if( n_blk->is_CountedLoop() && n->Opcode() == Op_AddI ) return n;
duke@1 1009
duke@1 1010 // Check for having no control input; not pinned. Allow
duke@1 1011 // dominating control.
roland@32372 1012 if (n->in(0)) {
duke@1 1013 Node *dom = idom(n_blk);
roland@32372 1014 if (dom_lca(n->in(0), dom) != n->in(0)) {
duke@1 1015 return n;
roland@32372 1016 }
duke@1 1017 }
duke@1 1018 // Policy: when is it profitable. You must get more wins than
duke@1 1019 // policy before it is considered profitable. Policy is usually 0,
duke@1 1020 // so 1 win is considered profitable. Big merges will require big
duke@1 1021 // cloning, so get a larger policy.
duke@1 1022 int policy = n_blk->req() >> 2;
duke@1 1023
duke@1 1024 // If the loop is a candidate for range check elimination,
duke@1 1025 // delay splitting through it's phi until a later loop optimization
duke@1 1026 if (n_blk->is_CountedLoop()) {
duke@1 1027 IdealLoopTree *lp = get_loop(n_blk);
duke@1 1028 if (lp && lp->_rce_candidate) {
duke@1 1029 return n;
duke@1 1030 }
duke@1 1031 }
duke@1 1032
phedlin@53308 1033 if (must_throttle_split_if()) return n;
duke@1 1034
duke@1 1035 // Split 'n' through the merge point if it is profitable
duke@1 1036 Node *phi = split_thru_phi( n, n_blk, policy );
kvn@10971 1037 if (!phi) return n;
duke@1 1038
duke@1 1039 // Found a Phi to split thru!
duke@1 1040 // Replace 'n' with the new phi
kvn@5901 1041 _igvn.replace_node( n, phi );
duke@1 1042 // Moved a load around the loop, 'en-registering' something.
kvn@10971 1043 if (n_blk->is_Loop() && n->is_Load() &&
kvn@10971 1044 !phi->in(LoopNode::LoopBackControl)->is_Load())
duke@1 1045 C->set_major_progress();
duke@1 1046
duke@1 1047 return phi;
duke@1 1048 }
duke@1 1049
duke@1 1050 static bool merge_point_too_heavy(Compile* C, Node* region) {
duke@1 1051 // Bail out if the region and its phis have too many users.
duke@1 1052 int weight = 0;
duke@1 1053 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
duke@1 1054 weight += region->fast_out(i)->outcnt();
duke@1 1055 }
vlivanov@27707 1056 int nodes_left = C->max_node_limit() - C->live_nodes();
duke@1 1057 if (weight * 8 > nodes_left) {
twisti@34174 1058 if (PrintOpto) {
duke@1 1059 tty->print_cr("*** Split-if bails out: %d nodes, region weight %d", C->unique(), weight);
twisti@34174 1060 }
duke@1 1061 return true;
duke@1 1062 } else {
duke@1 1063 return false;
duke@1 1064 }
duke@1 1065 }
duke@1 1066
duke@1 1067 static bool merge_point_safe(Node* region) {
duke@1 1068 // 4799512: Stop split_if_with_blocks from splitting a block with a ConvI2LNode
duke@1 1069 // having a PhiNode input. This sidesteps the dangerous case where the split
duke@1 1070 // ConvI2LNode may become TOP if the input Value() does not
duke@1 1071 // overlap the ConvI2L range, leaving a node which may not dominate its
duke@1 1072 // uses.
duke@1 1073 // A better fix for this problem can be found in the BugTraq entry, but
duke@1 1074 // expediency for Mantis demands this hack.
cfang@3187 1075 // 6855164: If the merge point has a FastLockNode with a PhiNode input, we stop
cfang@3187 1076 // split_if_with_blocks from splitting a block because we could not move around
cfang@3187 1077 // the FastLockNode.
duke@1 1078 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
duke@1 1079 Node* n = region->fast_out(i);
duke@1 1080 if (n->is_Phi()) {
duke@1 1081 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
duke@1 1082 Node* m = n->fast_out(j);
cfang@3187 1083 if (m->is_FastLock())
duke@1 1084 return false;
rkennke@52925 1085 #if INCLUDE_SHENANDOAHGC
rkennke@52925 1086 if (m->is_ShenandoahBarrier() && m->has_out_with(Op_FastLock)) {
rkennke@52925 1087 return false;
rkennke@52925 1088 }
rkennke@52925 1089 #endif
cfang@3187 1090 #ifdef _LP64
cfang@3187 1091 if (m->Opcode() == Op_ConvI2L)
cfang@3187 1092 return false;
thartmann@35574 1093 if (m->is_CastII() && m->isa_CastII()->has_range_check()) {
thartmann@35574 1094 return false;
thartmann@35574 1095 }
cfang@3187 1096 #endif
duke@1 1097 }
duke@1 1098 }
duke@1 1099 }
duke@1 1100 return true;
duke@1 1101 }
duke@1 1102
duke@1 1103
duke@1 1104 //------------------------------place_near_use---------------------------------
duke@1 1105 // Place some computation next to use but not inside inner loops.
duke@1 1106 // For inner loop uses move it to the preheader area.
roland@49482 1107 Node *PhaseIdealLoop::place_near_use(Node *useblock) const {
duke@1 1108 IdealLoopTree *u_loop = get_loop( useblock );
roland@49482 1109 if (u_loop->_irreducible) {
roland@49482 1110 return useblock;
roland@49482 1111 }
roland@49482 1112 if (u_loop->_child) {
roland@49482 1113 if (useblock == u_loop->_head && u_loop->_head->is_OuterStripMinedLoop()) {
roland@49482 1114 return u_loop->_head->in(LoopNode::EntryControl);
roland@49482 1115 }
roland@49482 1116 return useblock;
roland@49482 1117 }
roland@49482 1118 return u_loop->_head->as_Loop()->skip_strip_mined()->in(LoopNode::EntryControl);
duke@1 1119 }
duke@1 1120
duke@1 1121
roland@36065 1122 bool PhaseIdealLoop::identical_backtoback_ifs(Node *n) {
roland@48713 1123 if (!n->is_If() || n->is_CountedLoopEnd()) {
roland@36065 1124 return false;
roland@36065 1125 }
roland@36065 1126 if (!n->in(0)->is_Region()) {
roland@36065 1127 return false;
roland@36065 1128 }
roland@36065 1129 Node* region = n->in(0);
roland@36065 1130 Node* dom = idom(region);
roland@36065 1131 if (!dom->is_If() || dom->in(1) != n->in(1)) {
roland@36065 1132 return false;
roland@36065 1133 }
roland@36065 1134 IfNode* dom_if = dom->as_If();
roland@36065 1135 Node* proj_true = dom_if->proj_out(1);
roland@36065 1136 Node* proj_false = dom_if->proj_out(0);
roland@36065 1137
roland@36065 1138 for (uint i = 1; i < region->req(); i++) {
roland@36065 1139 if (is_dominator(proj_true, region->in(i))) {
roland@36065 1140 continue;
roland@36065 1141 }
roland@36065 1142 if (is_dominator(proj_false, region->in(i))) {
roland@36065 1143 continue;
roland@36065 1144 }
roland@36065 1145 return false;
roland@36065 1146 }
roland@36065 1147
roland@36065 1148 return true;
roland@36065 1149 }
roland@36065 1150
phedlin@53308 1151
phedlin@53308 1152 bool PhaseIdealLoop::can_split_if(Node* n_ctrl) {
phedlin@53308 1153 if (must_throttle_split_if()) {
phedlin@53308 1154 return false;
roland@36065 1155 }
roland@36065 1156
roland@36065 1157 // Do not do 'split-if' if irreducible loops are present.
roland@36065 1158 if (_has_irreducible_loops) {
roland@36065 1159 return false;
roland@36065 1160 }
roland@36065 1161
roland@36065 1162 if (merge_point_too_heavy(C, n_ctrl)) {
roland@36065 1163 return false;
roland@36065 1164 }
roland@36065 1165
roland@36065 1166 // Do not do 'split-if' if some paths are dead. First do dead code
roland@36065 1167 // elimination and then see if its still profitable.
roland@36065 1168 for (uint i = 1; i < n_ctrl->req(); i++) {
roland@36065 1169 if (n_ctrl->in(i) == C->top()) {
roland@36065 1170 return false;
roland@36065 1171 }
roland@36065 1172 }
roland@36065 1173
roland@36065 1174 // If trying to do a 'Split-If' at the loop head, it is only
roland@36065 1175 // profitable if the cmp folds up on BOTH paths. Otherwise we
roland@36065 1176 // risk peeling a loop forever.
roland@36065 1177
roland@36065 1178 // CNC - Disabled for now. Requires careful handling of loop
roland@36065 1179 // body selection for the cloned code. Also, make sure we check
roland@36065 1180 // for any input path not being in the same loop as n_ctrl. For
roland@36065 1181 // irreducible loops we cannot check for 'n_ctrl->is_Loop()'
roland@36065 1182 // because the alternative loop entry points won't be converted
roland@36065 1183 // into LoopNodes.
roland@36065 1184 IdealLoopTree *n_loop = get_loop(n_ctrl);
roland@36065 1185 for (uint j = 1; j < n_ctrl->req(); j++) {
roland@36065 1186 if (get_loop(n_ctrl->in(j)) != n_loop) {
roland@36065 1187 return false;
roland@36065 1188 }
roland@36065 1189 }
roland@36065 1190
roland@36065 1191 // Check for safety of the merge point.
roland@36065 1192 if (!merge_point_safe(n_ctrl)) {
roland@36065 1193 return false;
roland@36065 1194 }
roland@36065 1195
roland@36065 1196 return true;
roland@36065 1197 }
roland@36065 1198
duke@1 1199 //------------------------------split_if_with_blocks_post----------------------
duke@1 1200 // Do the real work in a non-recursive function. CFG hackery wants to be
duke@1 1201 // in the post-order, so it can dirty the I-DOM info and not use the dirtied
duke@1 1202 // info.
pliden@50525 1203 void PhaseIdealLoop::split_if_with_blocks_post(Node *n, bool last_round) {
duke@1 1204
duke@1 1205 // Cloning Cmp through Phi's involves the split-if transform.
duke@1 1206 // FastLock is not used by an If
pliden@50525 1207 if (n->is_Cmp() && !n->is_FastLock() && !last_round) {
duke@1 1208 Node *n_ctrl = get_ctrl(n);
duke@1 1209 // Determine if the Node has inputs from some local Phi.
duke@1 1210 // Returns the block to clone thru.
roland@36065 1211 Node *n_blk = has_local_phi_input(n);
roland@36065 1212 if (n_blk != n_ctrl) {
roland@36065 1213 return;
roland@36065 1214 }
duke@1 1215
roland@36065 1216 if (!can_split_if(n_ctrl)) {
duke@1 1217 return;
roland@36065 1218 }
duke@1 1219
roland@36065 1220 if (n->outcnt() != 1) {
roland@36065 1221 return; // Multiple bool's from 1 compare?
roland@36065 1222 }
duke@1 1223 Node *bol = n->unique_out();
roland@36065 1224 assert(bol->is_Bool(), "expect a bool here");
roland@36065 1225 if (bol->outcnt() != 1) {
roland@36065 1226 return;// Multiple branches from 1 compare?
roland@36065 1227 }
duke@1 1228 Node *iff = bol->unique_out();
duke@1 1229
duke@1 1230 // Check some safety conditions
roland@36065 1231 if (iff->is_If()) { // Classic split-if?
roland@36065 1232 if (iff->in(0) != n_ctrl) {
roland@36065 1233 return; // Compare must be in same blk as if
roland@36065 1234 }
duke@1 1235 } else if (iff->is_CMove()) { // Trying to split-up a CMOVE
kvn@11447 1236 // Can't split CMove with different control edge.
roland@36065 1237 if (iff->in(0) != NULL && iff->in(0) != n_ctrl ) {
roland@36065 1238 return;
roland@36065 1239 }
roland@36065 1240 if (get_ctrl(iff->in(2)) == n_ctrl ||
roland@36065 1241 get_ctrl(iff->in(3)) == n_ctrl) {
duke@1 1242 return; // Inputs not yet split-up
roland@36065 1243 }
roland@36065 1244 if (get_loop(n_ctrl) != get_loop(get_ctrl(iff))) {
duke@1 1245 return; // Loop-invar test gates loop-varying CMOVE
duke@1 1246 }
duke@1 1247 } else {
duke@1 1248 return; // some other kind of node, such as an Allocate
duke@1 1249 }
duke@1 1250
duke@1 1251 // When is split-if profitable? Every 'win' on means some control flow
duke@1 1252 // goes dead, so it's almost always a win.
duke@1 1253 int policy = 0;
roland@36065 1254 // Split compare 'n' through the merge point if it is profitable
roland@36065 1255 Node *phi = split_thru_phi( n, n_ctrl, policy);
roland@36065 1256 if (!phi) {
duke@1 1257 return;
duke@1 1258 }
duke@1 1259
duke@1 1260 // Found a Phi to split thru!
duke@1 1261 // Replace 'n' with the new phi
roland@36065 1262 _igvn.replace_node(n, phi);
duke@1 1263
duke@1 1264 // Now split the bool up thru the phi
roland@36065 1265 Node *bolphi = split_thru_phi(bol, n_ctrl, -1);
morris@16375 1266 guarantee(bolphi != NULL, "null boolean phi node");
morris@16375 1267
roland@36065 1268 _igvn.replace_node(bol, bolphi);
roland@36065 1269 assert(iff->in(1) == bolphi, "");
morris@16375 1270
roland@36065 1271 if (bolphi->Value(&_igvn)->singleton()) {
duke@1 1272 return;
roland@36065 1273 }
duke@1 1274
duke@1 1275 // Conditional-move? Must split up now
roland@36065 1276 if (!iff->is_If()) {
roland@36065 1277 Node *cmovphi = split_thru_phi(iff, n_ctrl, -1);
roland@36065 1278 _igvn.replace_node(iff, cmovphi);
duke@1 1279 return;
duke@1 1280 }
duke@1 1281
duke@1 1282 // Now split the IF
roland@36065 1283 do_split_if(iff);
roland@36065 1284 return;
roland@36065 1285 }
roland@36065 1286
roland@36065 1287 // Two identical ifs back to back can be merged
roland@36065 1288 if (identical_backtoback_ifs(n) && can_split_if(n->in(0))) {
roland@36065 1289 Node *n_ctrl = n->in(0);
roland@36065 1290 PhiNode* bolphi = PhiNode::make_blank(n_ctrl, n->in(1));
roland@36065 1291 IfNode* dom_if = idom(n_ctrl)->as_If();
roland@36065 1292 Node* proj_true = dom_if->proj_out(1);
roland@36065 1293 Node* proj_false = dom_if->proj_out(0);
roland@36065 1294 Node* con_true = _igvn.makecon(TypeInt::ONE);
roland@36065 1295 Node* con_false = _igvn.makecon(TypeInt::ZERO);
roland@36065 1296
roland@36065 1297 for (uint i = 1; i < n_ctrl->req(); i++) {
roland@36065 1298 if (is_dominator(proj_true, n_ctrl->in(i))) {
roland@36065 1299 bolphi->init_req(i, con_true);
roland@36065 1300 } else {
roland@36065 1301 assert(is_dominator(proj_false, n_ctrl->in(i)), "bad if");
roland@36065 1302 bolphi->init_req(i, con_false);
roland@36065 1303 }
roland@36065 1304 }
roland@36065 1305 register_new_node(bolphi, n_ctrl);
roland@36065 1306 _igvn.replace_input_of(n, 1, bolphi);
roland@36065 1307
roland@36065 1308 // Now split the IF
roland@36065 1309 do_split_if(n);
duke@1 1310 return;
duke@1 1311 }
duke@1 1312
duke@1 1313 // Check for an IF ready to split; one that has its
duke@1 1314 // condition codes input coming from a Phi at the block start.
duke@1 1315 int n_op = n->Opcode();
duke@1 1316
duke@1 1317 // Check for an IF being dominated by another IF same test
roland@34164 1318 if (n_op == Op_If ||
roland@34164 1319 n_op == Op_RangeCheck) {
duke@1 1320 Node *bol = n->in(1);
duke@1 1321 uint max = bol->outcnt();
duke@1 1322 // Check for same test used more than once?
roland@28036 1323 if (max > 1 && bol->is_Bool()) {
duke@1 1324 // Search up IDOMs to see if this IF is dominated.
duke@1 1325 Node *cutoff = get_ctrl(bol);
duke@1 1326
duke@1 1327 // Now search up IDOMs till cutoff, looking for a dominating test
duke@1 1328 Node *prevdom = n;
duke@1 1329 Node *dom = idom(prevdom);
roland@28036 1330 while (dom != cutoff) {
roland@28036 1331 if (dom->req() > 1 && dom->in(1) == bol && prevdom->in(0) == dom) {
duke@1 1332 // Replace the dominated test with an obvious true or false.
duke@1 1333 // Place it on the IGVN worklist for later cleanup.
duke@1 1334 C->set_major_progress();
roland@28036 1335 dominated_by(prevdom, n, false, true);
duke@1 1336 #ifndef PRODUCT
duke@1 1337 if( VerifyLoopOptimizations ) verify();
duke@1 1338 #endif
duke@1 1339 return;
duke@1 1340 }
duke@1 1341 prevdom = dom;
duke@1 1342 dom = idom(prevdom);
duke@1 1343 }
duke@1 1344 }
duke@1 1345 }
duke@1 1346
duke@1 1347 // See if a shared loop-varying computation has no loop-varying uses.
duke@1 1348 // Happens if something is only used for JVM state in uncommon trap exits,
duke@1 1349 // like various versions of induction variable+offset. Clone the
duke@1 1350 // computation per usage to allow it to sink out of the loop.
duke@1 1351 if (has_ctrl(n) && !n->in(0)) {// n not dead and has no control edge (can float about)
duke@1 1352 Node *n_ctrl = get_ctrl(n);
duke@1 1353 IdealLoopTree *n_loop = get_loop(n_ctrl);
duke@1 1354 if( n_loop != _ltree_root ) {
duke@1 1355 DUIterator_Fast imax, i = n->fast_outs(imax);
duke@1 1356 for (; i < imax; i++) {
duke@1 1357 Node* u = n->fast_out(i);
duke@1 1358 if( !has_ctrl(u) ) break; // Found control user
duke@1 1359 IdealLoopTree *u_loop = get_loop(get_ctrl(u));
duke@1 1360 if( u_loop == n_loop ) break; // Found loop-varying use
duke@1 1361 if( n_loop->is_member( u_loop ) ) break; // Found use in inner loop
duke@1 1362 if( u->Opcode() == Op_Opaque1 ) break; // Found loop limit, bugfix for 4677003
duke@1 1363 }
duke@1 1364 bool did_break = (i < imax); // Did we break out of the previous loop?
duke@1 1365 if (!did_break && n->outcnt() > 1) { // All uses in outer loops!
kvn@11447 1366 Node *late_load_ctrl = NULL;
duke@1 1367 if (n->is_Load()) {
duke@1 1368 // If n is a load, get and save the result from get_late_ctrl(),
duke@1 1369 // to be later used in calculating the control for n's clones.
duke@1 1370 clear_dom_lca_tags();
duke@1 1371 late_load_ctrl = get_late_ctrl(n, n_ctrl);
duke@1 1372 }
duke@1 1373 // If n is a load, and the late control is the same as the current
duke@1 1374 // control, then the cloning of n is a pointless exercise, because
duke@1 1375 // GVN will ensure that we end up where we started.
duke@1 1376 if (!n->is_Load() || late_load_ctrl != n_ctrl) {
rkennke@52925 1377 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
duke@1 1378 for (DUIterator_Last jmin, j = n->last_outs(jmin); j >= jmin; ) {
duke@1 1379 Node *u = n->last_out(j); // Clone private computation per use
kvn@12958 1380 _igvn.rehash_node_delayed(u);
duke@1 1381 Node *x = n->clone(); // Clone computation
duke@1 1382 Node *x_ctrl = NULL;
duke@1 1383 if( u->is_Phi() ) {
duke@1 1384 // Replace all uses of normal nodes. Replace Phi uses
twisti@2131 1385 // individually, so the separate Nodes can sink down
duke@1 1386 // different paths.
duke@1 1387 uint k = 1;
duke@1 1388 while( u->in(k) != n ) k++;
duke@1 1389 u->set_req( k, x );
duke@1 1390 // x goes next to Phi input path
duke@1 1391 x_ctrl = u->in(0)->in(k);
duke@1 1392 --j;
duke@1 1393 } else { // Normal use
duke@1 1394 // Replace all uses
duke@1 1395 for( uint k = 0; k < u->req(); k++ ) {
duke@1 1396 if( u->in(k) == n ) {
duke@1 1397 u->set_req( k, x );
duke@1 1398 --j;
duke@1 1399 }
duke@1 1400 }
duke@1 1401 x_ctrl = get_ctrl(u);
duke@1 1402 }
duke@1 1403
duke@1 1404 // Find control for 'x' next to use but not inside inner loops.
duke@1 1405 // For inner loop uses get the preheader area.
duke@1 1406 x_ctrl = place_near_use(x_ctrl);
duke@1 1407
rkennke@52925 1408 if (bs->sink_node(this, n, x, x_ctrl, n_ctrl)) {
rkennke@52925 1409 continue;
rkennke@52925 1410 }
rkennke@52925 1411
duke@1 1412 if (n->is_Load()) {
duke@1 1413 // For loads, add a control edge to a CFG node outside of the loop
duke@1 1414 // to force them to not combine and return back inside the loop
duke@1 1415 // during GVN optimization (4641526).
duke@1 1416 //
duke@1 1417 // Because we are setting the actual control input, factor in
duke@1 1418 // the result from get_late_ctrl() so we respect any
duke@1 1419 // anti-dependences. (6233005).
duke@1 1420 x_ctrl = dom_lca(late_load_ctrl, x_ctrl);
duke@1 1421
duke@1 1422 // Don't allow the control input to be a CFG splitting node.
duke@1 1423 // Such nodes should only have ProjNodes as outs, e.g. IfNode
duke@1 1424 // should only have IfTrueNode and IfFalseNode (4985384).
duke@1 1425 x_ctrl = find_non_split_ctrl(x_ctrl);
duke@1 1426 assert(dom_depth(n_ctrl) <= dom_depth(x_ctrl), "n is later than its clone");
duke@1 1427
duke@1 1428 x->set_req(0, x_ctrl);
duke@1 1429 }
duke@1 1430 register_new_node(x, x_ctrl);
duke@1 1431
duke@1 1432 // Some institutional knowledge is needed here: 'x' is
duke@1 1433 // yanked because if the optimizer runs GVN on it all the
duke@1 1434 // cloned x's will common up and undo this optimization and
duke@1 1435 // be forced back in the loop. This is annoying because it
duke@1 1436 // makes +VerifyOpto report false-positives on progress. I
duke@1 1437 // tried setting control edges on the x's to force them to
duke@1 1438 // not combine, but the matching gets worried when it tries
duke@1 1439 // to fold a StoreP and an AddP together (as part of an
duke@1 1440 // address expression) and the AddP and StoreP have
duke@1 1441 // different controls.
roland@13969 1442 if (!x->is_Load() && !x->is_DecodeNarrowPtr()) _igvn._worklist.yank(x);
duke@1 1443 }
duke@1 1444 _igvn.remove_dead_node(n);
duke@1 1445 }
duke@1 1446 }
duke@1 1447 }
duke@1 1448 }
duke@1 1449
roland@32372 1450 try_move_store_after_loop(n);
roland@32372 1451
duke@1 1452 // Check for Opaque2's who's loop has disappeared - who's input is in the
duke@1 1453 // same loop nest as their output. Remove 'em, they are no longer useful.
duke@1 1454 if( n_op == Op_Opaque2 &&
duke@1 1455 n->in(1) != NULL &&
duke@1 1456 get_loop(get_ctrl(n)) == get_loop(get_ctrl(n->in(1))) ) {
kvn@5901 1457 _igvn.replace_node( n, n->in(1) );
duke@1 1458 }
pliden@50525 1459
pliden@50525 1460 #if INCLUDE_ZGC
pliden@50525 1461 if (UseZGC) {
pliden@50525 1462 ZBarrierSetC2::loop_optimize_gc_barrier(this, n, last_round);
pliden@50525 1463 }
pliden@50525 1464 #endif
duke@1 1465 }
duke@1 1466
duke@1 1467 //------------------------------split_if_with_blocks---------------------------
duke@1 1468 // Check for aggressive application of 'split-if' optimization,
duke@1 1469 // using basic block level info.
pliden@50525 1470 void PhaseIdealLoop::split_if_with_blocks(VectorSet &visited, Node_Stack &nstack, bool last_round) {
phedlin@53308 1471 Node* root = C->root();
phedlin@53308 1472 visited.set(root->_idx); // first, mark root as visited
duke@1 1473 // Do pre-visit work for root
phedlin@53308 1474 Node* n = split_if_with_blocks_pre(root);
phedlin@53308 1475 uint cnt = n->outcnt();
phedlin@53308 1476 uint i = 0;
phedlin@53308 1477
duke@1 1478 while (true) {
duke@1 1479 // Visit all children
duke@1 1480 if (i < cnt) {
duke@1 1481 Node* use = n->raw_out(i);
duke@1 1482 ++i;
duke@1 1483 if (use->outcnt() != 0 && !visited.test_set(use->_idx)) {
duke@1 1484 // Now do pre-visit work for this use
phedlin@53308 1485 use = split_if_with_blocks_pre(use);
duke@1 1486 nstack.push(n, i); // Save parent and next use's index.
duke@1 1487 n = use; // Process all children of current use.
duke@1 1488 cnt = use->outcnt();
duke@1 1489 i = 0;
duke@1 1490 }
duke@1 1491 }
duke@1 1492 else {
duke@1 1493 // All of n's children have been processed, complete post-processing.
duke@1 1494 if (cnt != 0 && !n->is_Con()) {
duke@1 1495 assert(has_node(n), "no dead nodes");
phedlin@53308 1496 split_if_with_blocks_post(n, last_round);
phedlin@53308 1497 }
phedlin@53308 1498 if (must_throttle_split_if()) {
phedlin@53308 1499 nstack.clear();
duke@1 1500 }
duke@1 1501 if (nstack.is_empty()) {
duke@1 1502 // Finished all nodes on stack.
duke@1 1503 break;
duke@1 1504 }
duke@1 1505 // Get saved parent node and next use's index. Visit the rest of uses.
duke@1 1506 n = nstack.node();
duke@1 1507 cnt = n->outcnt();
duke@1 1508 i = nstack.index();
duke@1 1509 nstack.pop();
duke@1 1510 }
duke@1 1511 }
duke@1 1512 }
duke@1 1513
duke@1 1514
duke@1 1515 //=============================================================================
duke@1 1516 //
duke@1 1517 // C L O N E A L O O P B O D Y
duke@1 1518 //
duke@1 1519
duke@1 1520 //------------------------------clone_iff--------------------------------------
duke@1 1521 // Passed in a Phi merging (recursively) some nearly equivalent Bool/Cmps.
duke@1 1522 // "Nearly" because all Nodes have been cloned from the original in the loop,
duke@1 1523 // but the fall-in edges to the Cmp are different. Clone bool/Cmp pairs
duke@1 1524 // through the Phi recursively, and return a Bool.
roland@48135 1525 Node* PhaseIdealLoop::clone_iff(PhiNode *phi, IdealLoopTree *loop) {
duke@1 1526
duke@1 1527 // Convert this Phi into a Phi merging Bools
duke@1 1528 uint i;
roland@48135 1529 for (i = 1; i < phi->req(); i++) {
duke@1 1530 Node *b = phi->in(i);
roland@48135 1531 if (b->is_Phi()) {
roland@48135 1532 _igvn.replace_input_of(phi, i, clone_iff(b->as_Phi(), loop));
duke@1 1533 } else {
roland@48135 1534 assert(b->is_Bool() || b->Opcode() == Op_Opaque4, "");
duke@1 1535 }
duke@1 1536 }
duke@1 1537
roland@48135 1538 Node* n = phi->in(1);
roland@48135 1539 Node* sample_opaque = NULL;
roland@48135 1540 Node *sample_bool = NULL;
roland@48135 1541 if (n->Opcode() == Op_Opaque4) {
roland@48135 1542 sample_opaque = n;
roland@48135 1543 sample_bool = n->in(1);
roland@48135 1544 assert(sample_bool->is_Bool(), "wrong type");
roland@48135 1545 } else {
roland@48135 1546 sample_bool = n;
roland@48135 1547 }
roland@48135 1548 Node *sample_cmp = sample_bool->in(1);
duke@1 1549
duke@1 1550 // Make Phis to merge the Cmp's inputs.
roland@48135 1551 PhiNode *phi1 = new PhiNode(phi->in(0), Type::TOP);
roland@48135 1552 PhiNode *phi2 = new PhiNode(phi->in(0), Type::TOP);
roland@48135 1553 for (i = 1; i < phi->req(); i++) {
roland@48135 1554 Node *n1 = sample_opaque == NULL ? phi->in(i)->in(1)->in(1) : phi->in(i)->in(1)->in(1)->in(1);
roland@48135 1555 Node *n2 = sample_opaque == NULL ? phi->in(i)->in(1)->in(2) : phi->in(i)->in(1)->in(1)->in(2);
roland@48135 1556 phi1->set_req(i, n1);
roland@48135 1557 phi2->set_req(i, n2);
roland@48135 1558 phi1->set_type(phi1->type()->meet_speculative(n1->bottom_type()));
roland@48135 1559 phi2->set_type(phi2->type()->meet_speculative(n2->bottom_type()));
duke@1 1560 }
duke@1 1561 // See if these Phis have been made before.
duke@1 1562 // Register with optimizer
duke@1 1563 Node *hit1 = _igvn.hash_find_insert(phi1);
roland@48135 1564 if (hit1) { // Hit, toss just made Phi
duke@1 1565 _igvn.remove_dead_node(phi1); // Remove new phi
roland@48135 1566 assert(hit1->is_Phi(), "" );
duke@1 1567 phi1 = (PhiNode*)hit1; // Use existing phi
duke@1 1568 } else { // Miss
duke@1 1569 _igvn.register_new_node_with_optimizer(phi1);
duke@1 1570 }
duke@1 1571 Node *hit2 = _igvn.hash_find_insert(phi2);
roland@48135 1572 if (hit2) { // Hit, toss just made Phi
duke@1 1573 _igvn.remove_dead_node(phi2); // Remove new phi
roland@48135 1574 assert(hit2->is_Phi(), "" );
duke@1 1575 phi2 = (PhiNode*)hit2; // Use existing phi
duke@1 1576 } else { // Miss
duke@1 1577 _igvn.register_new_node_with_optimizer(phi2);
duke@1 1578 }
duke@1 1579 // Register Phis with loop/block info
duke@1 1580 set_ctrl(phi1, phi->in(0));
duke@1 1581 set_ctrl(phi2, phi->in(0));
duke@1 1582 // Make a new Cmp
duke@1 1583 Node *cmp = sample_cmp->clone();
roland@48135 1584 cmp->set_req(1, phi1);
roland@48135 1585 cmp->set_req(2, phi2);
duke@1 1586 _igvn.register_new_node_with_optimizer(cmp);
duke@1 1587 set_ctrl(cmp, phi->in(0));
duke@1 1588
duke@1 1589 // Make a new Bool
duke@1 1590 Node *b = sample_bool->clone();
duke@1 1591 b->set_req(1,cmp);
duke@1 1592 _igvn.register_new_node_with_optimizer(b);
duke@1 1593 set_ctrl(b, phi->in(0));
duke@1 1594
roland@48135 1595 if (sample_opaque != NULL) {
roland@48135 1596 Node* opaque = sample_opaque->clone();
roland@48135 1597 opaque->set_req(1, b);
roland@48135 1598 _igvn.register_new_node_with_optimizer(opaque);
roland@48135 1599 set_ctrl(opaque, phi->in(0));
roland@48135 1600 return opaque;
roland@48135 1601 }
roland@48135 1602
roland@48135 1603 assert(b->is_Bool(), "");
roland@48135 1604 return b;
duke@1 1605 }
duke@1 1606
duke@1 1607 //------------------------------clone_bool-------------------------------------
duke@1 1608 // Passed in a Phi merging (recursively) some nearly equivalent Bool/Cmps.
duke@1 1609 // "Nearly" because all Nodes have been cloned from the original in the loop,
duke@1 1610 // but the fall-in edges to the Cmp are different. Clone bool/Cmp pairs
duke@1 1611 // through the Phi recursively, and return a Bool.
duke@1 1612 CmpNode *PhaseIdealLoop::clone_bool( PhiNode *phi, IdealLoopTree *loop ) {
duke@1 1613 uint i;
duke@1 1614 // Convert this Phi into a Phi merging Bools
duke@1 1615 for( i = 1; i < phi->req(); i++ ) {
duke@1 1616 Node *b = phi->in(i);
duke@1 1617 if( b->is_Phi() ) {
kvn@12958 1618 _igvn.replace_input_of(phi, i, clone_bool( b->as_Phi(), loop ));
duke@1 1619 } else {
duke@1 1620 assert( b->is_Cmp() || b->is_top(), "inputs are all Cmp or TOP" );
duke@1 1621 }
duke@1 1622 }
duke@1 1623
duke@1 1624 Node *sample_cmp = phi->in(1);
duke@1 1625
duke@1 1626 // Make Phis to merge the Cmp's inputs.
thartmann@24923 1627 PhiNode *phi1 = new PhiNode( phi->in(0), Type::TOP );
thartmann@24923 1628 PhiNode *phi2 = new PhiNode( phi->in(0), Type::TOP );
duke@1 1629 for( uint j = 1; j < phi->req(); j++ ) {
duke@1 1630 Node *cmp_top = phi->in(j); // Inputs are all Cmp or TOP
duke@1 1631 Node *n1, *n2;
duke@1 1632 if( cmp_top->is_Cmp() ) {
duke@1 1633 n1 = cmp_top->in(1);
duke@1 1634 n2 = cmp_top->in(2);
duke@1 1635 } else {
duke@1 1636 n1 = n2 = cmp_top;
duke@1 1637 }
duke@1 1638 phi1->set_req( j, n1 );
duke@1 1639 phi2->set_req( j, n2 );
roland@22799 1640 phi1->set_type(phi1->type()->meet_speculative(n1->bottom_type()));
roland@22799 1641 phi2->set_type(phi2->type()->meet_speculative(n2->bottom_type()));
duke@1 1642 }
duke@1 1643
duke@1 1644 // See if these Phis have been made before.
duke@1 1645 // Register with optimizer
duke@1 1646 Node *hit1 = _igvn.hash_find_insert(phi1);
duke@1 1647 if( hit1 ) { // Hit, toss just made Phi
duke@1 1648 _igvn.remove_dead_node(phi1); // Remove new phi
duke@1 1649 assert( hit1->is_Phi(), "" );
duke@1 1650 phi1 = (PhiNode*)hit1; // Use existing phi
duke@1 1651 } else { // Miss
duke@1 1652 _igvn.register_new_node_with_optimizer(phi1);
duke@1 1653 }
duke@1 1654 Node *hit2 = _igvn.hash_find_insert(phi2);
duke@1 1655 if( hit2 ) { // Hit, toss just made Phi
duke@1 1656 _igvn.remove_dead_node(phi2); // Remove new phi
duke@1 1657 assert( hit2->is_Phi(), "" );
duke@1 1658 phi2 = (PhiNode*)hit2; // Use existing phi
duke@1 1659 } else { // Miss
duke@1 1660 _igvn.register_new_node_with_optimizer(phi2);
duke@1 1661 }
duke@1 1662 // Register Phis with loop/block info
duke@1 1663 set_ctrl(phi1, phi->in(0));
duke@1 1664 set_ctrl(phi2, phi->in(0));
duke@1 1665 // Make a new Cmp
duke@1 1666 Node *cmp = sample_cmp->clone();
duke@1 1667 cmp->set_req( 1, phi1 );
duke@1 1668 cmp->set_req( 2, phi2 );
duke@1 1669 _igvn.register_new_node_with_optimizer(cmp);
duke@1 1670 set_ctrl(cmp, phi->in(0));
duke@1 1671
duke@1 1672 assert( cmp->is_Cmp(), "" );
duke@1 1673 return (CmpNode*)cmp;
duke@1 1674 }
duke@1 1675
duke@1 1676 //------------------------------sink_use---------------------------------------
duke@1 1677 // If 'use' was in the loop-exit block, it now needs to be sunk
duke@1 1678 // below the post-loop merge point.
duke@1 1679 void PhaseIdealLoop::sink_use( Node *use, Node *post_loop ) {
duke@1 1680 if (!use->is_CFG() && get_ctrl(use) == post_loop->in(2)) {
duke@1 1681 set_ctrl(use, post_loop);
duke@1 1682 for (DUIterator j = use->outs(); use->has_out(j); j++)
duke@1 1683 sink_use(use->out(j), post_loop);
duke@1 1684 }
duke@1 1685 }
duke@1 1686
roland@48145 1687 void PhaseIdealLoop::clone_loop_handle_data_uses(Node* old, Node_List &old_new,
roland@48145 1688 IdealLoopTree* loop, IdealLoopTree* outer_loop,
roland@48145 1689 Node_List*& split_if_set, Node_List*& split_bool_set,
roland@48145 1690 Node_List*& split_cex_set, Node_List& worklist,
roland@48145 1691 uint new_counter, CloneLoopMode mode) {
roland@48145 1692 Node* nnn = old_new[old->_idx];
roland@48145 1693 // Copy uses to a worklist, so I can munge the def-use info
roland@48145 1694 // with impunity.
roland@48145 1695 for (DUIterator_Fast jmax, j = old->fast_outs(jmax); j < jmax; j++)
roland@48145 1696 worklist.push(old->fast_out(j));
roland@48145 1697
roland@48145 1698 while( worklist.size() ) {
roland@48145 1699 Node *use = worklist.pop();
roland@48145 1700 if (!has_node(use)) continue; // Ignore dead nodes
roland@48145 1701 if (use->in(0) == C->top()) continue;
roland@48145 1702 IdealLoopTree *use_loop = get_loop( has_ctrl(use) ? get_ctrl(use) : use );
roland@48145 1703 // Check for data-use outside of loop - at least one of OLD or USE
roland@48145 1704 // must not be a CFG node.
roland@48145 1705 #ifdef ASSERT
roland@48145 1706 if (loop->_head->as_Loop()->is_strip_mined() && outer_loop->is_member(use_loop) && !loop->is_member(use_loop) && old_new[use->_idx] == NULL) {
roland@48145 1707 Node* sfpt = loop->_head->as_CountedLoop()->outer_safepoint();
roland@48145 1708 assert(mode == ControlAroundStripMined && use == sfpt, "missed a node");
roland@48145 1709 }
roland@48145 1710 #endif
roland@48145 1711 if (!loop->is_member(use_loop) && !outer_loop->is_member(use_loop) && (!old->is_CFG() || !use->is_CFG())) {
roland@48145 1712
roland@48145 1713 // If the Data use is an IF, that means we have an IF outside of the
roland@48145 1714 // loop that is switching on a condition that is set inside of the
roland@48145 1715 // loop. Happens if people set a loop-exit flag; then test the flag
roland@48145 1716 // in the loop to break the loop, then test is again outside of the
roland@48145 1717 // loop to determine which way the loop exited.
roland@48145 1718 // Loop predicate If node connects to Bool node through Opaque1 node.
roland@48145 1719 if (use->is_If() || use->is_CMove() || C->is_predicate_opaq(use) || use->Opcode() == Op_Opaque4) {
roland@48145 1720 // Since this code is highly unlikely, we lazily build the worklist
roland@48145 1721 // of such Nodes to go split.
roland@48145 1722 if (!split_if_set) {
roland@48145 1723 ResourceArea *area = Thread::current()->resource_area();
roland@48145 1724 split_if_set = new Node_List(area);
roland@48145 1725 }
roland@48145 1726 split_if_set->push(use);
roland@48145 1727 }
roland@48145 1728 if (use->is_Bool()) {
roland@48145 1729 if (!split_bool_set) {
roland@48145 1730 ResourceArea *area = Thread::current()->resource_area();
roland@48145 1731 split_bool_set = new Node_List(area);
roland@48145 1732 }
roland@48145 1733 split_bool_set->push(use);
roland@48145 1734 }
roland@48145 1735 if (use->Opcode() == Op_CreateEx) {
roland@48145 1736 if (!split_cex_set) {
roland@48145 1737 ResourceArea *area = Thread::current()->resource_area();
roland@48145 1738 split_cex_set = new Node_List(area);
roland@48145 1739 }
roland@48145 1740 split_cex_set->push(use);
roland@48145 1741 }
roland@48145 1742
roland@48145 1743
roland@48145 1744 // Get "block" use is in
roland@48145 1745 uint idx = 0;
roland@48145 1746 while( use->in(idx) != old ) idx++;
roland@48145 1747 Node *prev = use->is_CFG() ? use : get_ctrl(use);
roland@48145 1748 assert(!loop->is_member(get_loop(prev)) && !outer_loop->is_member(get_loop(prev)), "" );
roland@48145 1749 Node *cfg = prev->_idx >= new_counter
roland@48145 1750 ? prev->in(2)
roland@48145 1751 : idom(prev);
roland@48145 1752 if( use->is_Phi() ) // Phi use is in prior block
roland@48145 1753 cfg = prev->in(idx); // NOT in block of Phi itself
roland@48145 1754 if (cfg->is_top()) { // Use is dead?
roland@48145 1755 _igvn.replace_input_of(use, idx, C->top());
roland@48145 1756 continue;
roland@48145 1757 }
roland@48145 1758
roland@50248 1759 // If use is referenced through control edge... (idx == 0)
roland@50248 1760 if (mode == IgnoreStripMined && idx == 0) {
roland@50248 1761 LoopNode *head = loop->_head->as_Loop();
roland@50248 1762 if (head->is_strip_mined() && is_dominator(head->outer_loop_exit(), prev)) {
roland@50248 1763 // That node is outside the inner loop, leave it outside the
roland@50248 1764 // outer loop as well to not confuse verification code.
roland@50248 1765 assert(!loop->_parent->is_member(use_loop), "should be out of the outer loop");
roland@50248 1766 _igvn.replace_input_of(use, 0, head->outer_loop_exit());
roland@50248 1767 continue;
roland@50248 1768 }
roland@50248 1769 }
roland@50248 1770
roland@48145 1771 while(!outer_loop->is_member(get_loop(cfg))) {
roland@48145 1772 prev = cfg;
roland@48145 1773 cfg = cfg->_idx >= new_counter ? cfg->in(2) : idom(cfg);
roland@48145 1774 }
roland@48145 1775 // If the use occurs after merging several exits from the loop, then
roland@48145 1776 // old value must have dominated all those exits. Since the same old
roland@48145 1777 // value was used on all those exits we did not need a Phi at this
roland@48145 1778 // merge point. NOW we do need a Phi here. Each loop exit value
roland@48145 1779 // is now merged with the peeled body exit; each exit gets its own
roland@48145 1780 // private Phi and those Phis need to be merged here.
roland@48145 1781 Node *phi;
roland@48145 1782 if( prev->is_Region() ) {
roland@48145 1783 if( idx == 0 ) { // Updating control edge?
roland@48145 1784 phi = prev; // Just use existing control
roland@48145 1785 } else { // Else need a new Phi
roland@48145 1786 phi = PhiNode::make( prev, old );
roland@48145 1787 // Now recursively fix up the new uses of old!
roland@48145 1788 for( uint i = 1; i < prev->req(); i++ ) {
roland@48145 1789 worklist.push(phi); // Onto worklist once for each 'old' input
roland@48145 1790 }
roland@48145 1791 }
roland@48145 1792 } else {
roland@48145 1793 // Get new RegionNode merging old and new loop exits
roland@48145 1794 prev = old_new[prev->_idx];
roland@48145 1795 assert( prev, "just made this in step 7" );
roland@48145 1796 if( idx == 0) { // Updating control edge?
roland@48145 1797 phi = prev; // Just use existing control
roland@48145 1798 } else { // Else need a new Phi
roland@48145 1799 // Make a new Phi merging data values properly
roland@48145 1800 phi = PhiNode::make( prev, old );
roland@48145 1801 phi->set_req( 1, nnn );
roland@48145 1802 }
roland@48145 1803 }
roland@48145 1804 // If inserting a new Phi, check for prior hits
roland@48145 1805 if( idx != 0 ) {
roland@48145 1806 Node *hit = _igvn.hash_find_insert(phi);
roland@48145 1807 if( hit == NULL ) {
roland@48145 1808 _igvn.register_new_node_with_optimizer(phi); // Register new phi
roland@48145 1809 } else { // or
roland@48145 1810 // Remove the new phi from the graph and use the hit
roland@48145 1811 _igvn.remove_dead_node(phi);
roland@48145 1812 phi = hit; // Use existing phi
roland@48145 1813 }
roland@48145 1814 set_ctrl(phi, prev);
roland@48145 1815 }
roland@48145 1816 // Make 'use' use the Phi instead of the old loop body exit value
roland@48145 1817 _igvn.replace_input_of(use, idx, phi);
roland@48145 1818 if( use->_idx >= new_counter ) { // If updating new phis
roland@48145 1819 // Not needed for correctness, but prevents a weak assert
roland@48145 1820 // in AddPNode from tripping (when we end up with different
roland@48145 1821 // base & derived Phis that will become the same after
roland@48145 1822 // IGVN does CSE).
roland@48145 1823 Node *hit = _igvn.hash_find_insert(use);
roland@48145 1824 if( hit ) // Go ahead and re-hash for hits.
roland@48145 1825 _igvn.replace_node( use, hit );
roland@48145 1826 }
roland@48145 1827
roland@48145 1828 // If 'use' was in the loop-exit block, it now needs to be sunk
roland@48145 1829 // below the post-loop merge point.
roland@48145 1830 sink_use( use, prev );
roland@48145 1831 }
roland@48145 1832 }
roland@48145 1833 }
roland@48145 1834
roland@50503 1835 static void clone_outer_loop_helper(Node* n, const IdealLoopTree *loop, const IdealLoopTree* outer_loop,
roland@50503 1836 const Node_List &old_new, Unique_Node_List& wq, PhaseIdealLoop* phase,
roland@50503 1837 bool check_old_new) {
roland@50503 1838 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
roland@50503 1839 Node* u = n->fast_out(j);
roland@50503 1840 assert(check_old_new || old_new[u->_idx] == NULL, "shouldn't have been cloned");
roland@50503 1841 if (!u->is_CFG() && (!check_old_new || old_new[u->_idx] == NULL)) {
roland@50503 1842 Node* c = phase->get_ctrl(u);
roland@50503 1843 IdealLoopTree* u_loop = phase->get_loop(c);
roland@50503 1844 assert(!loop->is_member(u_loop), "can be in outer loop or out of both loops only");
roland@50503 1845 if (outer_loop->is_member(u_loop)) {
roland@50503 1846 wq.push(u);
roland@50503 1847 }
roland@50503 1848 }
roland@50503 1849 }
roland@50503 1850 }
roland@50503 1851
roland@48145 1852 void PhaseIdealLoop::clone_outer_loop(LoopNode* head, CloneLoopMode mode, IdealLoopTree *loop,
roland@48145 1853 IdealLoopTree* outer_loop, int dd, Node_List &old_new,
roland@48145 1854 Node_List& extra_data_nodes) {
roland@48145 1855 if (head->is_strip_mined() && mode != IgnoreStripMined) {
roland@48145 1856 CountedLoopNode* cl = head->as_CountedLoop();
roland@48145 1857 Node* l = cl->outer_loop();
roland@48145 1858 Node* tail = cl->outer_loop_tail();
roland@48145 1859 IfNode* le = cl->outer_loop_end();
roland@48145 1860 Node* sfpt = cl->outer_safepoint();
roland@48145 1861 CountedLoopEndNode* cle = cl->loopexit();
roland@48145 1862 CountedLoopNode* new_cl = old_new[cl->_idx]->as_CountedLoop();
dlong@48603 1863 CountedLoopEndNode* new_cle = new_cl->as_CountedLoop()->loopexit_or_null();
roland@48145 1864 Node* cle_out = cle->proj_out(false);
roland@48145 1865
roland@48145 1866 Node* new_sfpt = NULL;
roland@48145 1867 Node* new_cle_out = cle_out->clone();
roland@48145 1868 old_new.map(cle_out->_idx, new_cle_out);
roland@48145 1869 if (mode == CloneIncludesStripMined) {
roland@48145 1870 // clone outer loop body
roland@48145 1871 Node* new_l = l->clone();
roland@48145 1872 Node* new_tail = tail->clone();
roland@48145 1873 IfNode* new_le = le->clone()->as_If();
roland@48145 1874 new_sfpt = sfpt->clone();
roland@48145 1875
roland@48145 1876 set_loop(new_l, outer_loop->_parent);
roland@48145 1877 set_idom(new_l, new_l->in(LoopNode::EntryControl), dd);
roland@48145 1878 set_loop(new_cle_out, outer_loop->_parent);
roland@48145 1879 set_idom(new_cle_out, new_cle, dd);
roland@48145 1880 set_loop(new_sfpt, outer_loop->_parent);
roland@48145 1881 set_idom(new_sfpt, new_cle_out, dd);
roland@48145 1882 set_loop(new_le, outer_loop->_parent);
roland@48145 1883 set_idom(new_le, new_sfpt, dd);
roland@48145 1884 set_loop(new_tail, outer_loop->_parent);
roland@48145 1885 set_idom(new_tail, new_le, dd);
roland@48145 1886 set_idom(new_cl, new_l, dd);
roland@48145 1887
roland@48145 1888 old_new.map(l->_idx, new_l);
roland@48145 1889 old_new.map(tail->_idx, new_tail);
roland@48145 1890 old_new.map(le->_idx, new_le);
roland@48145 1891 old_new.map(sfpt->_idx, new_sfpt);
roland@48145 1892
roland@48145 1893 new_l->set_req(LoopNode::LoopBackControl, new_tail);
roland@48145 1894 new_l->set_req(0, new_l);
roland@48145 1895 new_tail->set_req(0, new_le);
roland@48145 1896 new_le->set_req(0, new_sfpt);
roland@48145 1897 new_sfpt->set_req(0, new_cle_out);
roland@48145 1898 new_cle_out->set_req(0, new_cle);
roland@48145 1899 new_cl->set_req(LoopNode::EntryControl, new_l);
roland@48145 1900
roland@48145 1901 _igvn.register_new_node_with_optimizer(new_l);
roland@48145 1902 _igvn.register_new_node_with_optimizer(new_tail);
roland@48145 1903 _igvn.register_new_node_with_optimizer(new_le);
roland@48145 1904 } else {
roland@48145 1905 Node *newhead = old_new[loop->_head->_idx];
roland@48145 1906 newhead->as_Loop()->clear_strip_mined();
roland@48145 1907 _igvn.replace_input_of(newhead, LoopNode::EntryControl, newhead->in(LoopNode::EntryControl)->in(LoopNode::EntryControl));
roland@48145 1908 set_idom(newhead, newhead->in(LoopNode::EntryControl), dd);
roland@48145 1909 }
roland@48145 1910 // Look at data node that were assigned a control in the outer
roland@48145 1911 // loop: they are kept in the outer loop by the safepoint so start
roland@48145 1912 // from the safepoint node's inputs.
roland@48145 1913 IdealLoopTree* outer_loop = get_loop(l);
roland@48145 1914 Node_Stack stack(2);
roland@48145 1915 stack.push(sfpt, 1);
roland@48145 1916 uint new_counter = C->unique();
roland@48145 1917 while (stack.size() > 0) {
roland@48145 1918 Node* n = stack.node();
roland@48145 1919 uint i = stack.index();
roland@48145 1920 while (i < n->req() &&
roland@48145 1921 (n->in(i) == NULL ||
roland@48145 1922 !has_ctrl(n->in(i)) ||
roland@48145 1923 get_loop(get_ctrl(n->in(i))) != outer_loop ||
roland@48145 1924 (old_new[n->in(i)->_idx] != NULL && old_new[n->in(i)->_idx]->_idx >= new_counter))) {
roland@48145 1925 i++;
roland@48145 1926 }
roland@48145 1927 if (i < n->req()) {
roland@48145 1928 stack.set_index(i+1);
roland@48145 1929 stack.push(n->in(i), 0);
roland@48145 1930 } else {
roland@48145 1931 assert(old_new[n->_idx] == NULL || n == sfpt || old_new[n->_idx]->_idx < new_counter, "no clone yet");
roland@48145 1932 Node* m = n == sfpt ? new_sfpt : n->clone();
roland@48145 1933 if (m != NULL) {
roland@48145 1934 for (uint i = 0; i < n->req(); i++) {
roland@48145 1935 if (m->in(i) != NULL && old_new[m->in(i)->_idx] != NULL) {
roland@48145 1936 m->set_req(i, old_new[m->in(i)->_idx]);
roland@48145 1937 }
roland@48145 1938 }
roland@48145 1939 } else {
roland@48145 1940 assert(n == sfpt && mode != CloneIncludesStripMined, "where's the safepoint clone?");
roland@48145 1941 }
roland@48145 1942 if (n != sfpt) {
roland@48145 1943 extra_data_nodes.push(n);
roland@48145 1944 _igvn.register_new_node_with_optimizer(m);
roland@48145 1945 assert(get_ctrl(n) == cle_out, "what other control?");
roland@48145 1946 set_ctrl(m, new_cle_out);
roland@48145 1947 old_new.map(n->_idx, m);
roland@48145 1948 }
roland@48145 1949 stack.pop();
roland@48145 1950 }
roland@48145 1951 }
roland@48145 1952 if (mode == CloneIncludesStripMined) {
roland@48145 1953 _igvn.register_new_node_with_optimizer(new_sfpt);
roland@48145 1954 _igvn.register_new_node_with_optimizer(new_cle_out);
roland@48145 1955 }
roland@50503 1956 // Some other transformation may have pessimistically assign some
roland@50503 1957 // data nodes to the outer loop. Set their control so they are out
roland@50503 1958 // of the outer loop.
roland@50503 1959 ResourceMark rm;
roland@50503 1960 Unique_Node_List wq;
roland@50503 1961 for (uint i = 0; i < extra_data_nodes.size(); i++) {
roland@50503 1962 Node* old = extra_data_nodes.at(i);
roland@50503 1963 clone_outer_loop_helper(old, loop, outer_loop, old_new, wq, this, true);
roland@50503 1964 }
roland@50503 1965 Node* new_ctrl = cl->outer_loop_exit();
roland@50503 1966 assert(get_loop(new_ctrl) != outer_loop, "must be out of the loop nest");
roland@50503 1967 for (uint i = 0; i < wq.size(); i++) {
roland@50503 1968 Node* n = wq.at(i);
roland@50503 1969 set_ctrl(n, new_ctrl);
roland@50503 1970 clone_outer_loop_helper(n, loop, outer_loop, old_new, wq, this, false);
roland@50503 1971 }
roland@48145 1972 } else {
roland@48145 1973 Node *newhead = old_new[loop->_head->_idx];
roland@48145 1974 set_idom(newhead, newhead->in(LoopNode::EntryControl), dd);
roland@48145 1975 }
roland@48145 1976 }
roland@48145 1977
duke@1 1978 //------------------------------clone_loop-------------------------------------
duke@1 1979 //
duke@1 1980 // C L O N E A L O O P B O D Y
duke@1 1981 //
duke@1 1982 // This is the basic building block of the loop optimizations. It clones an
duke@1 1983 // entire loop body. It makes an old_new loop body mapping; with this mapping
duke@1 1984 // you can find the new-loop equivalent to an old-loop node. All new-loop
duke@1 1985 // nodes are exactly equal to their old-loop counterparts, all edges are the
duke@1 1986 // same. All exits from the old-loop now have a RegionNode that merges the
duke@1 1987 // equivalent new-loop path. This is true even for the normal "loop-exit"
duke@1 1988 // condition. All uses of loop-invariant old-loop values now come from (one
duke@1 1989 // or more) Phis that merge their new-loop equivalents.
duke@1 1990 //
duke@1 1991 // This operation leaves the graph in an illegal state: there are two valid
duke@1 1992 // control edges coming from the loop pre-header to both loop bodies. I'll
duke@1 1993 // definitely have to hack the graph after running this transform.
duke@1 1994 //
duke@1 1995 // From this building block I will further edit edges to perform loop peeling
duke@1 1996 // or loop unrolling or iteration splitting (Range-Check-Elimination), etc.
duke@1 1997 //
duke@1 1998 // Parameter side_by_size_idom:
duke@1 1999 // When side_by_size_idom is NULL, the dominator tree is constructed for
duke@1 2000 // the clone loop to dominate the original. Used in construction of
duke@1 2001 // pre-main-post loop sequence.
duke@1 2002 // When nonnull, the clone and original are side-by-side, both are
duke@1 2003 // dominated by the side_by_side_idom node. Used in construction of
duke@1 2004 // unswitched loops.
duke@1 2005 void PhaseIdealLoop::clone_loop( IdealLoopTree *loop, Node_List &old_new, int dd,
roland@48145 2006 CloneLoopMode mode, Node* side_by_side_idom) {
roland@48145 2007
roland@48145 2008 LoopNode* head = loop->_head->as_Loop();
roland@48145 2009 head->verify_strip_mined(1);
duke@1 2010
kvn@30593 2011 if (C->do_vector_loop() && PrintOpto) {
kvn@30593 2012 const char* mname = C->method()->name()->as_quoted_ascii();
kvn@30593 2013 if (mname != NULL) {
kvn@30593 2014 tty->print("PhaseIdealLoop::clone_loop: for vectorize method %s\n", mname);
kvn@30593 2015 }
kvn@30593 2016 }
kvn@30593 2017
kvn@30593 2018 CloneMap& cm = C->clone_map();
kvn@30593 2019 Dict* dict = cm.dict();
kvn@30593 2020 if (C->do_vector_loop()) {
kvn@30593 2021 cm.set_clone_idx(cm.max_gen()+1);
kvn@30593 2022 #ifndef PRODUCT
kvn@30593 2023 if (PrintOpto) {
kvn@30593 2024 tty->print_cr("PhaseIdealLoop::clone_loop: _clone_idx %d", cm.clone_idx());
kvn@30593 2025 loop->dump_head();
kvn@30593 2026 }
kvn@30593 2027 #endif
kvn@30593 2028 }
kvn@30593 2029
duke@1 2030 // Step 1: Clone the loop body. Make the old->new mapping.
duke@1 2031 uint i;
duke@1 2032 for( i = 0; i < loop->_body.size(); i++ ) {
duke@1 2033 Node *old = loop->_body.at(i);
duke@1 2034 Node *nnn = old->clone();
duke@1 2035 old_new.map( old->_idx, nnn );
kvn@30593 2036 if (C->do_vector_loop()) {
kvn@30593 2037 cm.verify_insert_and_clone(old, nnn, cm.clone_idx());
kvn@30593 2038 }
duke@1 2039 _igvn.register_new_node_with_optimizer(nnn);
duke@1 2040 }
duke@1 2041
roland@48145 2042 IdealLoopTree* outer_loop = (head->is_strip_mined() && mode != IgnoreStripMined) ? get_loop(head->as_CountedLoop()->outer_loop()) : loop;
duke@1 2043
duke@1 2044 // Step 2: Fix the edges in the new body. If the old input is outside the
duke@1 2045 // loop use it. If the old input is INside the loop, use the corresponding
duke@1 2046 // new node instead.
duke@1 2047 for( i = 0; i < loop->_body.size(); i++ ) {
duke@1 2048 Node *old = loop->_body.at(i);
duke@1 2049 Node *nnn = old_new[old->_idx];
duke@1 2050 // Fix CFG/Loop controlling the new node
duke@1 2051 if (has_ctrl(old)) {
duke@1 2052 set_ctrl(nnn, old_new[get_ctrl(old)->_idx]);
duke@1 2053 } else {
roland@48145 2054 set_loop(nnn, outer_loop->_parent);
duke@1 2055 if (old->outcnt() > 0) {
duke@1 2056 set_idom( nnn, old_new[idom(old)->_idx], dd );
duke@1 2057 }
duke@1 2058 }
duke@1 2059 // Correct edges to the new node
duke@1 2060 for( uint j = 0; j < nnn->req(); j++ ) {
duke@1 2061 Node *n = nnn->in(j);
duke@1 2062 if( n ) {
duke@1 2063 IdealLoopTree *old_in_loop = get_loop( has_ctrl(n) ? get_ctrl(n) : n );
duke@1 2064 if( loop->is_member( old_in_loop ) )
duke@1 2065 nnn->set_req(j, old_new[n->_idx]);
duke@1 2066 }
duke@1 2067 }
duke@1 2068 _igvn.hash_find_insert(nnn);
duke@1 2069 }
duke@1 2070
roland@48145 2071 ResourceArea *area = Thread::current()->resource_area();
roland@50503 2072 Node_List extra_data_nodes(area); // data nodes in the outer strip mined loop
roland@48145 2073 clone_outer_loop(head, mode, loop, outer_loop, dd, old_new, extra_data_nodes);
duke@1 2074
duke@1 2075 // Step 3: Now fix control uses. Loop varying control uses have already
duke@1 2076 // been fixed up (as part of all input edges in Step 2). Loop invariant
duke@1 2077 // control uses must be either an IfFalse or an IfTrue. Make a merge
duke@1 2078 // point to merge the old and new IfFalse/IfTrue nodes; make the use
duke@1 2079 // refer to this.
duke@1 2080 Node_List worklist(area);
duke@1 2081 uint new_counter = C->unique();
duke@1 2082 for( i = 0; i < loop->_body.size(); i++ ) {
duke@1 2083 Node* old = loop->_body.at(i);
duke@1 2084 if( !old->is_CFG() ) continue;
duke@1 2085
duke@1 2086 // Copy uses to a worklist, so I can munge the def-use info
duke@1 2087 // with impunity.
duke@1 2088 for (DUIterator_Fast jmax, j = old->fast_outs(jmax); j < jmax; j++)
duke@1 2089 worklist.push(old->fast_out(j));
duke@1 2090
duke@1 2091 while( worklist.size() ) { // Visit all uses
duke@1 2092 Node *use = worklist.pop();
duke@1 2093 if (!has_node(use)) continue; // Ignore dead nodes
duke@1 2094 IdealLoopTree *use_loop = get_loop( has_ctrl(use) ? get_ctrl(use) : use );
duke@1 2095 if( !loop->is_member( use_loop ) && use->is_CFG() ) {
duke@1 2096 // Both OLD and USE are CFG nodes here.
duke@1 2097 assert( use->is_Proj(), "" );
roland@48145 2098 Node* nnn = old_new[old->_idx];
roland@48145 2099
roland@48145 2100 Node* newuse = NULL;
roland@48145 2101 if (head->is_strip_mined() && mode != IgnoreStripMined) {
roland@48145 2102 CountedLoopNode* cl = head->as_CountedLoop();
roland@48145 2103 CountedLoopEndNode* cle = cl->loopexit();
dlong@48595 2104 Node* cle_out = cle->proj_out_or_null(false);
roland@48145 2105 if (use == cle_out) {
roland@48145 2106 IfNode* le = cl->outer_loop_end();
roland@48145 2107 use = le->proj_out(false);
roland@48145 2108 use_loop = get_loop(use);
roland@48145 2109 if (mode == CloneIncludesStripMined) {
roland@48145 2110 nnn = old_new[le->_idx];
roland@48145 2111 } else {
roland@48145 2112 newuse = old_new[cle_out->_idx];
roland@48145 2113 }
roland@48145 2114 }
roland@48145 2115 }
roland@48145 2116 if (newuse == NULL) {
roland@48145 2117 newuse = use->clone();
roland@48145 2118 }
duke@1 2119
duke@1 2120 // Clone the loop exit control projection
kvn@30593 2121 if (C->do_vector_loop()) {
kvn@30593 2122 cm.verify_insert_and_clone(use, newuse, cm.clone_idx());
kvn@30593 2123 }
duke@1 2124 newuse->set_req(0,nnn);
duke@1 2125 _igvn.register_new_node_with_optimizer(newuse);
duke@1 2126 set_loop(newuse, use_loop);
duke@1 2127 set_idom(newuse, nnn, dom_depth(nnn) + 1 );
duke@1 2128
duke@1 2129 // We need a Region to merge the exit from the peeled body and the
duke@1 2130 // exit from the old loop body.
thartmann@24923 2131 RegionNode *r = new RegionNode(3);
duke@1 2132 // Map the old use to the new merge point
duke@1 2133 old_new.map( use->_idx, r );
duke@1 2134 uint dd_r = MIN2(dom_depth(newuse),dom_depth(use));
duke@1 2135 assert( dd_r >= dom_depth(dom_lca(newuse,use)), "" );
duke@1 2136
duke@1 2137 // The original user of 'use' uses 'r' instead.
duke@1 2138 for (DUIterator_Last lmin, l = use->last_outs(lmin); l >= lmin;) {
duke@1 2139 Node* useuse = use->last_out(l);
kvn@12958 2140 _igvn.rehash_node_delayed(useuse);
duke@1 2141 uint uses_found = 0;
duke@1 2142 if( useuse->in(0) == use ) {
duke@1 2143 useuse->set_req(0, r);
duke@1 2144 uses_found++;
duke@1 2145 if( useuse->is_CFG() ) {
duke@1 2146 assert( dom_depth(useuse) > dd_r, "" );
duke@1 2147 set_idom(useuse, r, dom_depth(useuse));
duke@1 2148 }
duke@1 2149 }
duke@1 2150 for( uint k = 1; k < useuse->req(); k++ ) {
duke@1 2151 if( useuse->in(k) == use ) {
duke@1 2152 useuse->set_req(k, r);
duke@1 2153 uses_found++;
roland@48145 2154 if (useuse->is_Loop() && k == LoopNode::EntryControl) {
roland@48145 2155 assert(dom_depth(useuse) > dd_r , "");
roland@48145 2156 set_idom(useuse, r, dom_depth(useuse));
roland@48145 2157 }
duke@1 2158 }
duke@1 2159 }
duke@1 2160 l -= uses_found; // we deleted 1 or more copies of this edge
duke@1 2161 }
duke@1 2162
duke@1 2163 // Now finish up 'r'
duke@1 2164 r->set_req( 1, newuse );
duke@1 2165 r->set_req( 2, use );
duke@1 2166 _igvn.register_new_node_with_optimizer(r);
duke@1 2167 set_loop(r, use_loop);
duke@1 2168 set_idom(r, !side_by_side_idom ? newuse->in(0) : side_by_side_idom, dd_r);
duke@1 2169 } // End of if a loop-exit test
duke@1 2170 }
duke@1 2171 }
duke@1 2172
duke@1 2173 // Step 4: If loop-invariant use is not control, it must be dominated by a
duke@1 2174 // loop exit IfFalse/IfTrue. Find "proper" loop exit. Make a Region
duke@1 2175 // there if needed. Make a Phi there merging old and new used values.
duke@1 2176 Node_List *split_if_set = NULL;
duke@1 2177 Node_List *split_bool_set = NULL;
duke@1 2178 Node_List *split_cex_set = NULL;
duke@1 2179 for( i = 0; i < loop->_body.size(); i++ ) {
duke@1 2180 Node* old = loop->_body.at(i);
roland@48145 2181 clone_loop_handle_data_uses(old, old_new, loop, outer_loop, split_if_set,
roland@48145 2182 split_bool_set, split_cex_set, worklist, new_counter,
roland@48145 2183 mode);
roland@48145 2184 }
duke@1 2185
roland@48145 2186 for (i = 0; i < extra_data_nodes.size(); i++) {
roland@48145 2187 Node* old = extra_data_nodes.at(i);
roland@48145 2188 clone_loop_handle_data_uses(old, old_new, loop, outer_loop, split_if_set,
roland@48145 2189 split_bool_set, split_cex_set, worklist, new_counter,
roland@48145 2190 mode);
duke@1 2191 }
duke@1 2192
duke@1 2193 // Check for IFs that need splitting/cloning. Happens if an IF outside of
duke@1 2194 // the loop uses a condition set in the loop. The original IF probably
duke@1 2195 // takes control from one or more OLD Regions (which in turn get from NEW
duke@1 2196 // Regions). In any case, there will be a set of Phis for each merge point
duke@1 2197 // from the IF up to where the original BOOL def exists the loop.
roland@48135 2198 if (split_if_set) {
roland@48135 2199 while (split_if_set->size()) {
duke@1 2200 Node *iff = split_if_set->pop();
roland@48135 2201 if (iff->in(1)->is_Phi()) {
roland@48135 2202 Node *b = clone_iff(iff->in(1)->as_Phi(), loop);
kvn@12958 2203 _igvn.replace_input_of(iff, 1, b);
duke@1 2204 }
duke@1 2205 }
duke@1 2206 }
roland@48135 2207 if (split_bool_set) {
roland@48135 2208 while (split_bool_set->size()) {
duke@1 2209 Node *b = split_bool_set->pop();
duke@1 2210 Node *phi = b->in(1);
roland@48135 2211 assert(phi->is_Phi(), "");
roland@48135 2212 CmpNode *cmp = clone_bool((PhiNode*)phi, loop);
kvn@12958 2213 _igvn.replace_input_of(b, 1, cmp);
duke@1 2214 }
duke@1 2215 }
roland@48135 2216 if (split_cex_set) {
roland@48135 2217 while (split_cex_set->size()) {
duke@1 2218 Node *b = split_cex_set->pop();
roland@48135 2219 assert(b->in(0)->is_Region(), "");
roland@48135 2220 assert(b->in(1)->is_Phi(), "");
roland@48135 2221 assert(b->in(0)->in(0) == b->in(1)->in(0), "");
roland@48135 2222 split_up(b, b->in(0), NULL);
duke@1 2223 }
duke@1 2224 }
duke@1 2225
duke@1 2226 }
duke@1 2227
duke@1 2228
duke@1 2229 //---------------------- stride_of_possible_iv -------------------------------------
duke@1 2230 // Looks for an iff/bool/comp with one operand of the compare
duke@1 2231 // being a cycle involving an add and a phi,
duke@1 2232 // with an optional truncation (left-shift followed by a right-shift)
duke@1 2233 // of the add. Returns zero if not an iv.
duke@1 2234 int PhaseIdealLoop::stride_of_possible_iv(Node* iff) {
duke@1 2235 Node* trunc1 = NULL;
duke@1 2236 Node* trunc2 = NULL;
duke@1 2237 const TypeInt* ttype = NULL;
duke@1 2238 if (!iff->is_If() || iff->in(1) == NULL || !iff->in(1)->is_Bool()) {
duke@1 2239 return 0;
duke@1 2240 }
duke@1 2241 BoolNode* bl = iff->in(1)->as_Bool();
duke@1 2242 Node* cmp = bl->in(1);
jwilhelm@46630 2243 if (!cmp || (cmp->Opcode() != Op_CmpI && cmp->Opcode() != Op_CmpU)) {
duke@1 2244 return 0;
duke@1 2245 }
duke@1 2246 // Must have an invariant operand
duke@1 2247 if (is_member(get_loop(iff), get_ctrl(cmp->in(2)))) {
duke@1 2248 return 0;
duke@1 2249 }
duke@1 2250 Node* add2 = NULL;
duke@1 2251 Node* cmp1 = cmp->in(1);
duke@1 2252 if (cmp1->is_Phi()) {
duke@1 2253 // (If (Bool (CmpX phi:(Phi ...(Optional-trunc(AddI phi add2))) )))
duke@1 2254 Node* phi = cmp1;
duke@1 2255 for (uint i = 1; i < phi->req(); i++) {
duke@1 2256 Node* in = phi->in(i);
duke@1 2257 Node* add = CountedLoopNode::match_incr_with_optional_truncation(in,
duke@1 2258 &trunc1, &trunc2, &ttype);
duke@1 2259 if (add && add->in(1) == phi) {
duke@1 2260 add2 = add->in(2);
duke@1 2261 break;
duke@1 2262 }
duke@1 2263 }
duke@1 2264 } else {
duke@1 2265 // (If (Bool (CmpX addtrunc:(Optional-trunc((AddI (Phi ...addtrunc...) add2)) )))
duke@1 2266 Node* addtrunc = cmp1;
duke@1 2267 Node* add = CountedLoopNode::match_incr_with_optional_truncation(addtrunc,
duke@1 2268 &trunc1, &trunc2, &ttype);
duke@1 2269 if (add && add->in(1)->is_Phi()) {
duke@1 2270 Node* phi = add->in(1);
duke@1 2271 for (uint i = 1; i < phi->req(); i++) {
duke@1 2272 if (phi->in(i) == addtrunc) {
duke@1 2273 add2 = add->in(2);
duke@1 2274 break;
duke@1 2275 }
duke@1 2276 }
duke@1 2277 }
duke@1 2278 }
duke@1 2279 if (add2 != NULL) {
duke@1 2280 const TypeInt* add2t = _igvn.type(add2)->is_int();
duke@1 2281 if (add2t->is_con()) {
duke@1 2282 return add2t->get_con();
duke@1 2283 }
duke@1 2284 }
duke@1 2285 return 0;
duke@1 2286 }
duke@1 2287
duke@1 2288
duke@1 2289 //---------------------- stay_in_loop -------------------------------------
duke@1 2290 // Return the (unique) control output node that's in the loop (if it exists.)
duke@1 2291 Node* PhaseIdealLoop::stay_in_loop( Node* n, IdealLoopTree *loop) {
duke@1 2292 Node* unique = NULL;
duke@1 2293 if (!n) return NULL;
duke@1 2294 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
duke@1 2295 Node* use = n->fast_out(i);
duke@1 2296 if (!has_ctrl(use) && loop->is_member(get_loop(use))) {
duke@1 2297 if (unique != NULL) {
duke@1 2298 return NULL;
duke@1 2299 }
duke@1 2300 unique = use;
duke@1 2301 }
duke@1 2302 }
duke@1 2303 return unique;
duke@1 2304 }
duke@1 2305
duke@1 2306 //------------------------------ register_node -------------------------------------
duke@1 2307 // Utility to register node "n" with PhaseIdealLoop
duke@1 2308 void PhaseIdealLoop::register_node(Node* n, IdealLoopTree *loop, Node* pred, int ddepth) {
duke@1 2309 _igvn.register_new_node_with_optimizer(n);
duke@1 2310 loop->_body.push(n);
duke@1 2311 if (n->is_CFG()) {
duke@1 2312 set_loop(n, loop);
duke@1 2313 set_idom(n, pred, ddepth);
duke@1 2314 } else {
duke@1 2315 set_ctrl(n, pred);
duke@1 2316 }
duke@1 2317 }
duke@1 2318
duke@1 2319 //------------------------------ proj_clone -------------------------------------
duke@1 2320 // Utility to create an if-projection
duke@1 2321 ProjNode* PhaseIdealLoop::proj_clone(ProjNode* p, IfNode* iff) {
duke@1 2322 ProjNode* c = p->clone()->as_Proj();
duke@1 2323 c->set_req(0, iff);
duke@1 2324 return c;
duke@1 2325 }
duke@1 2326
duke@1 2327 //------------------------------ short_circuit_if -------------------------------------
duke@1 2328 // Force the iff control output to be the live_proj
duke@1 2329 Node* PhaseIdealLoop::short_circuit_if(IfNode* iff, ProjNode* live_proj) {
morris@16375 2330 guarantee(live_proj != NULL, "null projection");
duke@1 2331 int proj_con = live_proj->_con;
duke@1 2332 assert(proj_con == 0 || proj_con == 1, "false or true projection");
duke@1 2333 Node *con = _igvn.intcon(proj_con);
duke@1 2334 set_ctrl(con, C->root());
duke@1 2335 if (iff) {
duke@1 2336 iff->set_req(1, con);
duke@1 2337 }
duke@1 2338 return con;
duke@1 2339 }
duke@1 2340
duke@1 2341 //------------------------------ insert_if_before_proj -------------------------------------
duke@1 2342 // Insert a new if before an if projection (* - new node)
duke@1 2343 //
duke@1 2344 // before
duke@1 2345 // if(test)
duke@1 2346 // / \
duke@1 2347 // v v
duke@1 2348 // other-proj proj (arg)
duke@1 2349 //
duke@1 2350 // after
duke@1 2351 // if(test)
duke@1 2352 // / \
duke@1 2353 // / v
duke@1 2354 // | * proj-clone
duke@1 2355 // v |
duke@1 2356 // other-proj v
duke@1 2357 // * new_if(relop(cmp[IU](left,right)))
duke@1 2358 // / \
duke@1 2359 // v v
duke@1 2360 // * new-proj proj
duke@1 2361 // (returned)
duke@1 2362 //
duke@1 2363 ProjNode* PhaseIdealLoop::insert_if_before_proj(Node* left, bool Signed, BoolTest::mask relop, Node* right, ProjNode* proj) {
duke@1 2364 IfNode* iff = proj->in(0)->as_If();
duke@1 2365 IdealLoopTree *loop = get_loop(proj);
duke@1 2366 ProjNode *other_proj = iff->proj_out(!proj->is_IfTrue())->as_Proj();
duke@1 2367 int ddepth = dom_depth(proj);
duke@1 2368
kvn@12958 2369 _igvn.rehash_node_delayed(iff);
kvn@12958 2370 _igvn.rehash_node_delayed(proj);
duke@1 2371
duke@1 2372 proj->set_req(0, NULL); // temporary disconnect
duke@1 2373 ProjNode* proj2 = proj_clone(proj, iff);
duke@1 2374 register_node(proj2, loop, iff, ddepth);
duke@1 2375
thartmann@24923 2376 Node* cmp = Signed ? (Node*) new CmpINode(left, right) : (Node*) new CmpUNode(left, right);
duke@1 2377 register_node(cmp, loop, proj2, ddepth);
duke@1 2378
thartmann@24923 2379 BoolNode* bol = new BoolNode(cmp, relop);
duke@1 2380 register_node(bol, loop, proj2, ddepth);
duke@1 2381
roland@34164 2382 int opcode = iff->Opcode();
roland@34164 2383 assert(opcode == Op_If || opcode == Op_RangeCheck, "unexpected opcode");
roland@34164 2384 IfNode* new_if = (opcode == Op_If) ? new IfNode(proj2, bol, iff->_prob, iff->_fcnt):
roland@34164 2385 new RangeCheckNode(proj2, bol, iff->_prob, iff->_fcnt);
duke@1 2386 register_node(new_if, loop, proj2, ddepth);
duke@1 2387
duke@1 2388 proj->set_req(0, new_if); // reattach
duke@1 2389 set_idom(proj, new_if, ddepth);
duke@1 2390
duke@1 2391 ProjNode* new_exit = proj_clone(other_proj, new_if)->as_Proj();
morris@16375 2392 guarantee(new_exit != NULL, "null exit node");
duke@1 2393 register_node(new_exit, get_loop(other_proj), new_if, ddepth);
duke@1 2394
duke@1 2395 return new_exit;
duke@1 2396 }
duke@1 2397
duke@1 2398 //------------------------------ insert_region_before_proj -------------------------------------
duke@1 2399 // Insert a region before an if projection (* - new node)
duke@1 2400 //
duke@1 2401 // before
duke@1 2402 // if(test)
duke@1 2403 // / |
duke@1 2404 // v |
duke@1 2405 // proj v
duke@1 2406 // other-proj
duke@1 2407 //
duke@1 2408 // after
duke@1 2409 // if(test)
duke@1 2410 // / |
duke@1 2411 // v |
duke@1 2412 // * proj-clone v
duke@1 2413 // | other-proj
duke@1 2414 // v
duke@1 2415 // * new-region
duke@1 2416 // |
duke@1 2417 // v
duke@1 2418 // * dum_if
duke@1 2419 // / \
duke@1 2420 // v \
duke@1 2421 // * dum-proj v
duke@1 2422 // proj
duke@1 2423 //
duke@1 2424 RegionNode* PhaseIdealLoop::insert_region_before_proj(ProjNode* proj) {
duke@1 2425 IfNode* iff = proj->in(0)->as_If();
duke@1 2426 IdealLoopTree *loop = get_loop(proj);
duke@1 2427 ProjNode *other_proj = iff->proj_out(!proj->is_IfTrue())->as_Proj();
duke@1 2428 int ddepth = dom_depth(proj);
duke@1 2429
kvn@12958 2430 _igvn.rehash_node_delayed(iff);
kvn@12958 2431 _igvn.rehash_node_delayed(proj);
duke@1 2432
duke@1 2433 proj->set_req(0, NULL); // temporary disconnect
duke@1 2434 ProjNode* proj2 = proj_clone(proj, iff);
duke@1 2435 register_node(proj2, loop, iff, ddepth);
duke@1 2436
thartmann@24923 2437 RegionNode* reg = new RegionNode(2);
duke@1 2438 reg->set_req(1, proj2);
duke@1 2439 register_node(reg, loop, iff, ddepth);
duke@1 2440
thartmann@24923 2441 IfNode* dum_if = new IfNode(reg, short_circuit_if(NULL, proj), iff->_prob, iff->_fcnt);
duke@1 2442 register_node(dum_if, loop, reg, ddepth);
duke@1 2443
duke@1 2444 proj->set_req(0, dum_if); // reattach
duke@1 2445 set_idom(proj, dum_if, ddepth);
duke@1 2446
duke@1 2447 ProjNode* dum_proj = proj_clone(other_proj, dum_if);
duke@1 2448 register_node(dum_proj, loop, dum_if, ddepth);
duke@1 2449
duke@1 2450 return reg;
duke@1 2451 }
duke@1 2452
duke@1 2453 //------------------------------ insert_cmpi_loop_exit -------------------------------------
duke@1 2454 // Clone a signed compare loop exit from an unsigned compare and
duke@1 2455 // insert it before the unsigned cmp on the stay-in-loop path.
duke@1 2456 // All new nodes inserted in the dominator tree between the original
duke@1 2457 // if and it's projections. The original if test is replaced with
duke@1 2458 // a constant to force the stay-in-loop path.
duke@1 2459 //
duke@1 2460 // This is done to make sure that the original if and it's projections
duke@1 2461 // still dominate the same set of control nodes, that the ctrl() relation
duke@1 2462 // from data nodes to them is preserved, and that their loop nesting is
duke@1 2463 // preserved.
duke@1 2464 //
duke@1 2465 // before
duke@1 2466 // if(i <u limit) unsigned compare loop exit
duke@1 2467 // / |
duke@1 2468 // v v
duke@1 2469 // exit-proj stay-in-loop-proj
duke@1 2470 //
duke@1 2471 // after
duke@1 2472 // if(stay-in-loop-const) original if
duke@1 2473 // / |
duke@1 2474 // / v
duke@1 2475 // / if(i < limit) new signed test
duke@1 2476 // / / |
duke@1 2477 // / / v
duke@1 2478 // / / if(i <u limit) new cloned unsigned test
duke@1 2479 // / / / |
duke@1 2480 // v v v |
duke@1 2481 // region |
duke@1 2482 // | |
duke@1 2483 // dum-if |
duke@1 2484 // / | |
duke@1 2485 // ether | |
duke@1 2486 // v v
duke@1 2487 // exit-proj stay-in-loop-proj
duke@1 2488 //
duke@1 2489 IfNode* PhaseIdealLoop::insert_cmpi_loop_exit(IfNode* if_cmpu, IdealLoopTree *loop) {
duke@1 2490 const bool Signed = true;
duke@1 2491 const bool Unsigned = false;
duke@1 2492
duke@1 2493 BoolNode* bol = if_cmpu->in(1)->as_Bool();
duke@1 2494 if (bol->_test._test != BoolTest::lt) return NULL;
duke@1 2495 CmpNode* cmpu = bol->in(1)->as_Cmp();
duke@1 2496 if (cmpu->Opcode() != Op_CmpU) return NULL;
duke@1 2497 int stride = stride_of_possible_iv(if_cmpu);
duke@1 2498 if (stride == 0) return NULL;
duke@1 2499
morris@16375 2500 Node* lp_proj = stay_in_loop(if_cmpu, loop);
morris@16375 2501 guarantee(lp_proj != NULL, "null loop node");
morris@16375 2502
morris@16375 2503 ProjNode* lp_continue = lp_proj->as_Proj();
duke@1 2504 ProjNode* lp_exit = if_cmpu->proj_out(!lp_continue->is_IfTrue())->as_Proj();
duke@1 2505
duke@1 2506 Node* limit = NULL;
duke@1 2507 if (stride > 0) {
duke@1 2508 limit = cmpu->in(2);
duke@1 2509 } else {
duke@1 2510 limit = _igvn.makecon(TypeInt::ZERO);
duke@1 2511 set_ctrl(limit, C->root());
duke@1 2512 }
duke@1 2513 // Create a new region on the exit path
duke@1 2514 RegionNode* reg = insert_region_before_proj(lp_exit);
morris@16375 2515 guarantee(reg != NULL, "null region node");
duke@1 2516
duke@1 2517 // Clone the if-cmpu-true-false using a signed compare
duke@1 2518 BoolTest::mask rel_i = stride > 0 ? bol->_test._test : BoolTest::ge;
duke@1 2519 ProjNode* cmpi_exit = insert_if_before_proj(cmpu->in(1), Signed, rel_i, limit, lp_continue);
duke@1 2520 reg->add_req(cmpi_exit);
duke@1 2521
duke@1 2522 // Clone the if-cmpu-true-false
duke@1 2523 BoolTest::mask rel_u = bol->_test._test;
duke@1 2524 ProjNode* cmpu_exit = insert_if_before_proj(cmpu->in(1), Unsigned, rel_u, cmpu->in(2), lp_continue);
duke@1 2525 reg->add_req(cmpu_exit);
duke@1 2526
duke@1 2527 // Force original if to stay in loop.
duke@1 2528 short_circuit_if(if_cmpu, lp_continue);
duke@1 2529
duke@1 2530 return cmpi_exit->in(0)->as_If();
duke@1 2531 }
duke@1 2532
duke@1 2533 //------------------------------ remove_cmpi_loop_exit -------------------------------------
duke@1 2534 // Remove a previously inserted signed compare loop exit.
duke@1 2535 void PhaseIdealLoop::remove_cmpi_loop_exit(IfNode* if_cmp, IdealLoopTree *loop) {
duke@1 2536 Node* lp_proj = stay_in_loop(if_cmp, loop);
duke@1 2537 assert(if_cmp->in(1)->in(1)->Opcode() == Op_CmpI &&
duke@1 2538 stay_in_loop(lp_proj, loop)->is_If() &&
duke@1 2539 stay_in_loop(lp_proj, loop)->in(1)->in(1)->Opcode() == Op_CmpU, "inserted cmpi before cmpu");
duke@1 2540 Node *con = _igvn.makecon(lp_proj->is_IfTrue() ? TypeInt::ONE : TypeInt::ZERO);
duke@1 2541 set_ctrl(con, C->root());
duke@1 2542 if_cmp->set_req(1, con);
duke@1 2543 }
duke@1 2544
duke@1 2545 //------------------------------ scheduled_nodelist -------------------------------------
duke@1 2546 // Create a post order schedule of nodes that are in the
duke@1 2547 // "member" set. The list is returned in "sched".
duke@1 2548 // The first node in "sched" is the loop head, followed by
duke@1 2549 // nodes which have no inputs in the "member" set, and then
duke@1 2550 // followed by the nodes that have an immediate input dependence
duke@1 2551 // on a node in "sched".
duke@1 2552 void PhaseIdealLoop::scheduled_nodelist( IdealLoopTree *loop, VectorSet& member, Node_List &sched ) {
duke@1 2553
duke@1 2554 assert(member.test(loop->_head->_idx), "loop head must be in member set");
duke@1 2555 Arena *a = Thread::current()->resource_area();
duke@1 2556 VectorSet visited(a);
duke@1 2557 Node_Stack nstack(a, loop->_body.size());
duke@1 2558
duke@1 2559 Node* n = loop->_head; // top of stack is cached in "n"
duke@1 2560 uint idx = 0;
duke@1 2561 visited.set(n->_idx);
duke@1 2562
duke@1 2563 // Initially push all with no inputs from within member set
duke@1 2564 for(uint i = 0; i < loop->_body.size(); i++ ) {
duke@1 2565 Node *elt = loop->_body.at(i);
duke@1 2566 if (member.test(elt->_idx)) {
duke@1 2567 bool found = false;
duke@1 2568 for (uint j = 0; j < elt->req(); j++) {
duke@1 2569 Node* def = elt->in(j);
duke@1 2570 if (def && member.test(def->_idx) && def != elt) {
duke@1 2571 found = true;
duke@1 2572 break;
duke@1 2573 }
duke@1 2574 }
duke@1 2575 if (!found && elt != loop->_head) {
duke@1 2576 nstack.push(n, idx);
duke@1 2577 n = elt;
duke@1 2578 assert(!visited.test(n->_idx), "not seen yet");
duke@1 2579 visited.set(n->_idx);
duke@1 2580 }
duke@1 2581 }
duke@1 2582 }
duke@1 2583
duke@1 2584 // traverse out's that are in the member set
duke@1 2585 while (true) {
duke@1 2586 if (idx < n->outcnt()) {
duke@1 2587 Node* use = n->raw_out(idx);
duke@1 2588 idx++;
duke@1 2589 if (!visited.test_set(use->_idx)) {
duke@1 2590 if (member.test(use->_idx)) {
duke@1 2591 nstack.push(n, idx);
duke@1 2592 n = use;
duke@1 2593 idx = 0;
duke@1 2594 }
duke@1 2595 }
duke@1 2596 } else {
duke@1 2597 // All outputs processed
duke@1 2598 sched.push(n);
duke@1 2599 if (nstack.is_empty()) break;
duke@1 2600 n = nstack.node();
duke@1 2601 idx = nstack.index();
duke@1 2602 nstack.pop();
duke@1 2603 }
duke@1 2604 }
duke@1 2605 }
duke@1 2606
duke@1 2607
duke@1 2608 //------------------------------ has_use_in_set -------------------------------------
duke@1 2609 // Has a use in the vector set
duke@1 2610 bool PhaseIdealLoop::has_use_in_set( Node* n, VectorSet& vset ) {
duke@1 2611 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
duke@1 2612 Node* use = n->fast_out(j);
duke@1 2613 if (vset.test(use->_idx)) {
duke@1 2614 return true;
duke@1 2615 }
duke@1 2616 }
duke@1 2617 return false;
duke@1 2618 }
duke@1 2619
duke@1 2620
duke@1 2621 //------------------------------ has_use_internal_to_set -------------------------------------
duke@1 2622 // Has use internal to the vector set (ie. not in a phi at the loop head)
duke@1 2623 bool PhaseIdealLoop::has_use_internal_to_set( Node* n, VectorSet& vset, IdealLoopTree *loop ) {
duke@1 2624 Node* head = loop->_head;
duke@1 2625 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
duke@1 2626 Node* use = n->fast_out(j);
duke@1 2627 if (vset.test(use->_idx) && !(use->is_Phi() && use->in(0) == head)) {
duke@1 2628 return true;
duke@1 2629 }
duke@1 2630 }
duke@1 2631 return false;
duke@1 2632 }
duke@1 2633
duke@1 2634
duke@1 2635 //------------------------------ clone_for_use_outside_loop -------------------------------------
duke@1 2636 // clone "n" for uses that are outside of loop
kvn@17620 2637 int PhaseIdealLoop::clone_for_use_outside_loop( IdealLoopTree *loop, Node* n, Node_List& worklist ) {
kvn@17620 2638 int cloned = 0;
duke@1 2639 assert(worklist.size() == 0, "should be empty");
duke@1 2640 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
duke@1 2641 Node* use = n->fast_out(j);
duke@1 2642 if( !loop->is_member(get_loop(has_ctrl(use) ? get_ctrl(use) : use)) ) {
duke@1 2643 worklist.push(use);
duke@1 2644 }
duke@1 2645 }
duke@1 2646 while( worklist.size() ) {
duke@1 2647 Node *use = worklist.pop();
duke@1 2648 if (!has_node(use) || use->in(0) == C->top()) continue;
duke@1 2649 uint j;
duke@1 2650 for (j = 0; j < use->req(); j++) {
duke@1 2651 if (use->in(j) == n) break;
duke@1 2652 }
duke@1 2653 assert(j < use->req(), "must be there");
duke@1 2654
duke@1 2655 // clone "n" and insert it between the inputs of "n" and the use outside the loop
duke@1 2656 Node* n_clone = n->clone();
kvn@12958 2657 _igvn.replace_input_of(use, j, n_clone);
kvn@17620 2658 cloned++;
never@959 2659 Node* use_c;
duke@1 2660 if (!use->is_Phi()) {
never@959 2661 use_c = has_ctrl(use) ? get_ctrl(use) : use->in(0);
duke@1 2662 } else {
duke@1 2663 // Use in a phi is considered a use in the associated predecessor block
never@959 2664 use_c = use->in(0)->in(j);
duke@1 2665 }
never@959 2666 set_ctrl(n_clone, use_c);
never@959 2667 assert(!loop->is_member(get_loop(use_c)), "should be outside loop");
never@959 2668 get_loop(use_c)->_body.push(n_clone);
duke@1 2669 _igvn.register_new_node_with_optimizer(n_clone);
duke@1 2670 #if !defined(PRODUCT)
duke@1 2671 if (TracePartialPeeling) {
duke@1 2672 tty->print_cr("loop exit cloning old: %d new: %d newbb: %d", n->_idx, n_clone->_idx, get_ctrl(n_clone)->_idx);
duke@1 2673 }
duke@1 2674 #endif
duke@1 2675 }
kvn@17620 2676 return cloned;
duke@1 2677 }
duke@1 2678
duke@1 2679
duke@1 2680 //------------------------------ clone_for_special_use_inside_loop -------------------------------------
duke@1 2681 // clone "n" for special uses that are in the not_peeled region.
duke@1 2682 // If these def-uses occur in separate blocks, the code generator
duke@1 2683 // marks the method as not compilable. For example, if a "BoolNode"
duke@1 2684 // is in a different basic block than the "IfNode" that uses it, then
duke@1 2685 // the compilation is aborted in the code generator.
duke@1 2686 void PhaseIdealLoop::clone_for_special_use_inside_loop( IdealLoopTree *loop, Node* n,
duke@1 2687 VectorSet& not_peel, Node_List& sink_list, Node_List& worklist ) {
duke@1 2688 if (n->is_Phi() || n->is_Load()) {
duke@1 2689 return;
duke@1 2690 }
duke@1 2691 assert(worklist.size() == 0, "should be empty");
duke@1 2692 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
duke@1 2693 Node* use = n->fast_out(j);
duke@1 2694 if ( not_peel.test(use->_idx) &&
duke@1 2695 (use->is_If() || use->is_CMove() || use->is_Bool()) &&
duke@1 2696 use->in(1) == n) {
duke@1 2697 worklist.push(use);
duke@1 2698 }
duke@1 2699 }
duke@1 2700 if (worklist.size() > 0) {
duke@1 2701 // clone "n" and insert it between inputs of "n" and the use
duke@1 2702 Node* n_clone = n->clone();
duke@1 2703 loop->_body.push(n_clone);
duke@1 2704 _igvn.register_new_node_with_optimizer(n_clone);
duke@1 2705 set_ctrl(n_clone, get_ctrl(n));
duke@1 2706 sink_list.push(n_clone);
duke@1 2707 not_peel <<= n_clone->_idx; // add n_clone to not_peel set.
duke@1 2708 #if !defined(PRODUCT)
duke@1 2709 if (TracePartialPeeling) {
duke@1 2710 tty->print_cr("special not_peeled cloning old: %d new: %d", n->_idx, n_clone->_idx);
duke@1 2711 }
duke@1 2712 #endif
duke@1 2713 while( worklist.size() ) {
duke@1 2714 Node *use = worklist.pop();
kvn@12958 2715 _igvn.rehash_node_delayed(use);
duke@1 2716 for (uint j = 1; j < use->req(); j++) {
duke@1 2717 if (use->in(j) == n) {
duke@1 2718 use->set_req(j, n_clone);
duke@1 2719 }
duke@1 2720 }
duke@1 2721 }
duke@1 2722 }
duke@1 2723 }
duke@1 2724
duke@1 2725
duke@1 2726 //------------------------------ insert_phi_for_loop -------------------------------------
duke@1 2727 // Insert phi(lp_entry_val, back_edge_val) at use->in(idx) for loop lp if phi does not already exist
duke@1 2728 void PhaseIdealLoop::insert_phi_for_loop( Node* use, uint idx, Node* lp_entry_val, Node* back_edge_val, LoopNode* lp ) {
duke@1 2729 Node *phi = PhiNode::make(lp, back_edge_val);
duke@1 2730 phi->set_req(LoopNode::EntryControl, lp_entry_val);
duke@1 2731 // Use existing phi if it already exists
duke@1 2732 Node *hit = _igvn.hash_find_insert(phi);
duke@1 2733 if( hit == NULL ) {
duke@1 2734 _igvn.register_new_node_with_optimizer(phi);
duke@1 2735 set_ctrl(phi, lp);
duke@1 2736 } else {
duke@1 2737 // Remove the new phi from the graph and use the hit
duke@1 2738 _igvn.remove_dead_node(phi);
duke@1 2739 phi = hit;
duke@1 2740 }
kvn@12958 2741 _igvn.replace_input_of(use, idx, phi);
duke@1 2742 }
duke@1 2743
duke@1 2744 #ifdef ASSERT
duke@1 2745 //------------------------------ is_valid_loop_partition -------------------------------------
duke@1 2746 // Validate the loop partition sets: peel and not_peel
duke@1 2747 bool PhaseIdealLoop::is_valid_loop_partition( IdealLoopTree *loop, VectorSet& peel, Node_List& peel_list,
duke@1 2748 VectorSet& not_peel ) {
duke@1 2749 uint i;
duke@1 2750 // Check that peel_list entries are in the peel set
duke@1 2751 for (i = 0; i < peel_list.size(); i++) {
duke@1 2752 if (!peel.test(peel_list.at(i)->_idx)) {
duke@1 2753 return false;
duke@1 2754 }
duke@1 2755 }
duke@1 2756 // Check at loop members are in one of peel set or not_peel set
duke@1 2757 for (i = 0; i < loop->_body.size(); i++ ) {
duke@1 2758 Node *def = loop->_body.at(i);
duke@1 2759 uint di = def->_idx;
duke@1 2760 // Check that peel set elements are in peel_list
duke@1 2761 if (peel.test(di)) {
duke@1 2762 if (not_peel.test(di)) {
duke@1 2763 return false;
duke@1 2764 }
duke@1 2765 // Must be in peel_list also
duke@1 2766 bool found = false;
duke@1 2767 for (uint j = 0; j < peel_list.size(); j++) {
duke@1 2768 if (peel_list.at(j)->_idx == di) {
duke@1 2769 found = true;
duke@1 2770 break;
duke@1 2771 }
duke@1 2772 }
duke@1 2773 if (!found) {
duke@1 2774 return false;
duke@1 2775 }
duke@1 2776 } else if (not_peel.test(di)) {
duke@1 2777 if (peel.test(di)) {
duke@1 2778 return false;
duke@1 2779 }
duke@1 2780 } else {
duke@1 2781 return false;
duke@1 2782 }
duke@1 2783 }
duke@1 2784 return true;
duke@1 2785 }
duke@1 2786
duke@1 2787 //------------------------------ is_valid_clone_loop_exit_use -------------------------------------
duke@1 2788 // Ensure a use outside of loop is of the right form
duke@1 2789 bool PhaseIdealLoop::is_valid_clone_loop_exit_use( IdealLoopTree *loop, Node* use, uint exit_idx) {
duke@1 2790 Node *use_c = has_ctrl(use) ? get_ctrl(use) : use;
duke@1 2791 return (use->is_Phi() &&
duke@1 2792 use_c->is_Region() && use_c->req() == 3 &&
duke@1 2793 (use_c->in(exit_idx)->Opcode() == Op_IfTrue ||
duke@1 2794 use_c->in(exit_idx)->Opcode() == Op_IfFalse ||
duke@1 2795 use_c->in(exit_idx)->Opcode() == Op_JumpProj) &&
duke@1 2796 loop->is_member( get_loop( use_c->in(exit_idx)->in(0) ) ) );
duke@1 2797 }
duke@1 2798
duke@1 2799 //------------------------------ is_valid_clone_loop_form -------------------------------------
duke@1 2800 // Ensure that all uses outside of loop are of the right form
duke@1 2801 bool PhaseIdealLoop::is_valid_clone_loop_form( IdealLoopTree *loop, Node_List& peel_list,
duke@1 2802 uint orig_exit_idx, uint clone_exit_idx) {
duke@1 2803 uint len = peel_list.size();
duke@1 2804 for (uint i = 0; i < len; i++) {
duke@1 2805 Node *def = peel_list.at(i);
duke@1 2806
duke@1 2807 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
duke@1 2808 Node *use = def->fast_out(j);
duke@1 2809 Node *use_c = has_ctrl(use) ? get_ctrl(use) : use;
duke@1 2810 if (!loop->is_member(get_loop(use_c))) {
duke@1 2811 // use is not in the loop, check for correct structure
duke@1 2812 if (use->in(0) == def) {
duke@1 2813 // Okay
duke@1 2814 } else if (!is_valid_clone_loop_exit_use(loop, use, orig_exit_idx)) {
duke@1 2815 return false;
duke@1 2816 }
duke@1 2817 }
duke@1 2818 }
duke@1 2819 }
duke@1 2820 return true;
duke@1 2821 }
duke@1 2822 #endif
duke@1 2823
duke@1 2824 //------------------------------ partial_peel -------------------------------------
duke@1 2825 // Partially peel (aka loop rotation) the top portion of a loop (called
duke@1 2826 // the peel section below) by cloning it and placing one copy just before
duke@1 2827 // the new loop head and the other copy at the bottom of the new loop.
duke@1 2828 //
duke@1 2829 // before after where it came from
duke@1 2830 //
duke@1 2831 // stmt1 stmt1
duke@1 2832 // loop: stmt2 clone
duke@1 2833 // stmt2 if condA goto exitA clone
duke@1 2834 // if condA goto exitA new_loop: new
duke@1 2835 // stmt3 stmt3 clone
duke@1 2836 // if !condB goto loop if condB goto exitB clone
duke@1 2837 // exitB: stmt2 orig
duke@1 2838 // stmt4 if !condA goto new_loop orig
duke@1 2839 // exitA: goto exitA
duke@1 2840 // exitB:
duke@1 2841 // stmt4
duke@1 2842 // exitA:
duke@1 2843 //
duke@1 2844 // Step 1: find the cut point: an exit test on probable
duke@1 2845 // induction variable.
duke@1 2846 // Step 2: schedule (with cloning) operations in the peel
duke@1 2847 // section that can be executed after the cut into
duke@1 2848 // the section that is not peeled. This may need
duke@1 2849 // to clone operations into exit blocks. For
duke@1 2850 // instance, a reference to A[i] in the not-peel
duke@1 2851 // section and a reference to B[i] in an exit block
duke@1 2852 // may cause a left-shift of i by 2 to be placed
duke@1 2853 // in the peel block. This step will clone the left
duke@1 2854 // shift into the exit block and sink the left shift
duke@1 2855 // from the peel to the not-peel section.
duke@1 2856 // Step 3: clone the loop, retarget the control, and insert
duke@1 2857 // phis for values that are live across the new loop
duke@1 2858 // head. This is very dependent on the graph structure
duke@1 2859 // from clone_loop. It creates region nodes for
duke@1 2860 // exit control and associated phi nodes for values
duke@1 2861 // flow out of the loop through that exit. The region
duke@1 2862 // node is dominated by the clone's control projection.
duke@1 2863 // So the clone's peel section is placed before the
duke@1 2864 // new loop head, and the clone's not-peel section is
duke@1 2865 // forms the top part of the new loop. The original
duke@1 2866 // peel section forms the tail of the new loop.
duke@1 2867 // Step 4: update the dominator tree and recompute the
duke@1 2868 // dominator depth.
duke@1 2869 //
duke@1 2870 // orig
duke@1 2871 //
kvn@9101 2872 // stmt1
kvn@9101 2873 // |
kvn@9101 2874 // v
kvn@9101 2875 // loop predicate
kvn@9101 2876 // |
kvn@9101 2877 // v
duke@1 2878 // loop<----+
duke@1 2879 // | |
duke@1 2880 // stmt2 |
duke@1 2881 // | |
duke@1 2882 // v |
duke@1 2883 // ifA |
duke@1 2884 // / | |
duke@1 2885 // v v |
duke@1 2886 // false true ^ <-- last_peel
duke@1 2887 // / | |
duke@1 2888 // / ===|==cut |
duke@1 2889 // / stmt3 | <-- first_not_peel
duke@1 2890 // / | |
duke@1 2891 // | v |
duke@1 2892 // v ifB |
duke@1 2893 // exitA: / \ |
duke@1 2894 // / \ |
duke@1 2895 // v v |
duke@1 2896 // false true |
duke@1 2897 // / \ |
duke@1 2898 // / ----+
duke@1 2899 // |
duke@1 2900 // v
duke@1 2901 // exitB:
duke@1 2902 // stmt4
duke@1 2903 //
duke@1 2904 //
duke@1 2905 // after clone loop
duke@1 2906 //
duke@1 2907 // stmt1
kvn@9101 2908 // |
kvn@9101 2909 // v
kvn@9101 2910 // loop predicate
duke@1 2911 // / \
duke@1 2912 // clone / \ orig
duke@1 2913 // / \
duke@1 2914 // / \
duke@1 2915 // v v
duke@1 2916 // +---->loop loop<----+
duke@1 2917 // | | | |
duke@1 2918 // | stmt2 stmt2 |
duke@1 2919 // | | | |
duke@1 2920 // | v v |
duke@1 2921 // | ifA ifA |
duke@1 2922 // | | \ / | |
duke@1 2923 // | v v v v |
duke@1 2924 // ^ true false false true ^ <-- last_peel
duke@1 2925 // | | ^ \ / | |
duke@1 2926 // | cut==|== \ \ / ===|==cut |
duke@1 2927 // | stmt3 \ \ / stmt3 | <-- first_not_peel
duke@1 2928 // | | dom | | | |
duke@1 2929 // | v \ 1v v2 v |
duke@1 2930 // | ifB regionA ifB |
duke@1 2931 // | / \ | / \ |
duke@1 2932 // | / \ v / \ |
duke@1 2933 // | v v exitA: v v |
duke@1 2934 // | true false false true |
duke@1 2935 // | / ^ \ / \ |
duke@1 2936 // +---- \ \ / ----+
duke@1 2937 // dom \ /
duke@1 2938 // \ 1v v2
duke@1 2939 // regionB
duke@1 2940 // |
duke@1 2941 // v
duke@1 2942 // exitB:
duke@1 2943 // stmt4
duke@1 2944 //
duke@1 2945 //
duke@1 2946 // after partial peel
duke@1 2947 //
duke@1 2948 // stmt1
kvn@9101 2949 // |
kvn@9101 2950 // v
kvn@9101 2951 // loop predicate
duke@1 2952 // /
duke@1 2953 // clone / orig
duke@1 2954 // / TOP
duke@1 2955 // / \
duke@1 2956 // v v
kvn@9101 2957 // TOP->loop loop----+
duke@1 2958 // | | |
duke@1 2959 // stmt2 stmt2 |
duke@1 2960 // | | |
duke@1 2961 // v v |
duke@1 2962 // ifA ifA |
duke@1 2963 // | \ / | |
duke@1 2964 // v v v v |
duke@1 2965 // true false false true | <-- last_peel
duke@1 2966 // | ^ \ / +------|---+
duke@1 2967 // +->newloop \ \ / === ==cut | |
duke@1 2968 // | stmt3 \ \ / TOP | |
duke@1 2969 // | | dom | | stmt3 | | <-- first_not_peel
duke@1 2970 // | v \ 1v v2 v | |
duke@1 2971 // | ifB regionA ifB ^ v
duke@1 2972 // | / \ | / \ | |
duke@1 2973 // | / \ v / \ | |
duke@1 2974 // | v v exitA: v v | |
duke@1 2975 // | true false false true | |
duke@1 2976 // | / ^ \ / \ | |
duke@1 2977 // | | \ \ / v | |
duke@1 2978 // | | dom \ / TOP | |
duke@1 2979 // | | \ 1v v2 | |
duke@1 2980 // ^ v regionB | |
duke@1 2981 // | | | | |
duke@1 2982 // | | v ^ v
duke@1 2983 // | | exitB: | |
duke@1 2984 // | | stmt4 | |
duke@1 2985 // | +------------>-----------------+ |
duke@1 2986 // | |
duke@1 2987 // +-----------------<---------------------+
duke@1 2988 //
duke@1 2989 //
duke@1 2990 // final graph
duke@1 2991 //
duke@1 2992 // stmt1
duke@1 2993 // |
duke@1 2994 // v
kvn@9330 2995 // loop predicate
kvn@9330 2996 // |
kvn@9330 2997 // v
kvn@9101 2998 // stmt2 clone
kvn@9101 2999 // |
kvn@9101 3000 // v
duke@1 3001 // ........> ifA clone
duke@1 3002 // : / |
duke@1 3003 // dom / |
duke@1 3004 // : v v
duke@1 3005 // : false true
duke@1 3006 // : | |
kvn@9101 3007 // : | v
duke@1 3008 // : | newloop<-----+
duke@1 3009 // : | | |
duke@1 3010 // : | stmt3 clone |
duke@1 3011 // : | | |
duke@1 3012 // : | v |
duke@1 3013 // : | ifB |
duke@1 3014 // : | / \ |
duke@1 3015 // : | v v |
duke@1 3016 // : | false true |
duke@1 3017 // : | | | |
duke@1 3018 // : | v stmt2 |
duke@1 3019 // : | exitB: | |
duke@1 3020 // : | stmt4 v |
duke@1 3021 // : | ifA orig |
duke@1 3022 // : | / \ |
duke@1 3023 // : | / \ |
duke@1 3024 // : | v v |
duke@1 3025 // : | false true |
duke@1 3026 // : | / \ |
duke@1 3027 // : v v -----+
duke@1 3028 // RegionA
duke@1 3029 // |
duke@1 3030 // v
duke@1 3031 // exitA
duke@1 3032 //
duke@1 3033 bool PhaseIdealLoop::partial_peel( IdealLoopTree *loop, Node_List &old_new ) {
duke@1 3034
kvn@9101 3035 assert(!loop->_head->is_CountedLoop(), "Non-counted loop only");
rasbold@355 3036 if (!loop->_head->is_Loop()) {
rasbold@355 3037 return false; }
rasbold@355 3038
duke@1 3039 LoopNode *head = loop->_head->as_Loop();
duke@1 3040
duke@1 3041 if (head->is_partial_peel_loop() || head->partial_peel_has_failed()) {
duke@1 3042 return false;
duke@1 3043 }
duke@1 3044
duke@1 3045 // Check for complex exit control
duke@1 3046 for(uint ii = 0; ii < loop->_body.size(); ii++ ) {
duke@1 3047 Node *n = loop->_body.at(ii);
duke@1 3048 int opc = n->Opcode();
duke@1 3049 if (n->is_Call() ||
duke@1 3050 opc == Op_Catch ||
duke@1 3051 opc == Op_CatchProj ||
duke@1 3052 opc == Op_Jump ||
rbackman@22911 3053 opc == Op_JumpProj) {
duke@1 3054 #if !defined(PRODUCT)
duke@1 3055 if (TracePartialPeeling) {
duke@1 3056 tty->print_cr("\nExit control too complex: lp: %d", head->_idx);
duke@1 3057 }
duke@1 3058 #endif
duke@1 3059 return false;
duke@1 3060 }
duke@1 3061 }
duke@1 3062
duke@1 3063 int dd = dom_depth(head);
duke@1 3064
duke@1 3065 // Step 1: find cut point
duke@1 3066
duke@1 3067 // Walk up dominators to loop head looking for first loop exit
duke@1 3068 // which is executed on every path thru loop.
duke@1 3069 IfNode *peel_if = NULL;
duke@1 3070 IfNode *peel_if_cmpu = NULL;
duke@1 3071
duke@1 3072 Node *iff = loop->tail();
duke@1 3073 while( iff != head ) {
duke@1 3074 if( iff->is_If() ) {
duke@1 3075 Node *ctrl = get_ctrl(iff->in(1));
duke@1 3076 if (ctrl->is_top()) return false; // Dead test on live IF.
duke@1 3077 // If loop-varying exit-test, check for induction variable
duke@1 3078 if( loop->is_member(get_loop(ctrl)) &&
duke@1 3079 loop->is_loop_exit(iff) &&
duke@1 3080 is_possible_iv_test(iff)) {
duke@1 3081 Node* cmp = iff->in(1)->in(1);
duke@1 3082 if (cmp->Opcode() == Op_CmpI) {
duke@1 3083 peel_if = iff->as_If();
duke@1 3084 } else {
duke@1 3085 assert(cmp->Opcode() == Op_CmpU, "must be CmpI or CmpU");
duke@1 3086 peel_if_cmpu = iff->as_If();
duke@1 3087 }
duke@1 3088 }
duke@1 3089 }
duke@1 3090 iff = idom(iff);
duke@1 3091 }
duke@1 3092 // Prefer signed compare over unsigned compare.
duke@1 3093 IfNode* new_peel_if = NULL;
duke@1 3094 if (peel_if == NULL) {
duke@1 3095 if (!PartialPeelAtUnsignedTests || peel_if_cmpu == NULL) {
duke@1 3096 return false; // No peel point found
duke@1 3097 }
duke@1 3098 new_peel_if = insert_cmpi_loop_exit(peel_if_cmpu, loop);
duke@1 3099 if (new_peel_if == NULL) {
duke@1 3100 return false; // No peel point found
duke@1 3101 }
duke@1 3102 peel_if = new_peel_if;
duke@1 3103 }
duke@1 3104 Node* last_peel = stay_in_loop(peel_if, loop);
duke@1 3105 Node* first_not_peeled = stay_in_loop(last_peel, loop);
duke@1 3106 if (first_not_peeled == NULL || first_not_peeled == head) {
duke@1 3107 return false;
duke@1 3108 }
duke@1 3109
duke@1 3110 #if !defined(PRODUCT)
kvn@8732 3111 if (TraceLoopOpts) {
kvn@8732 3112 tty->print("PartialPeel ");
kvn@8732 3113 loop->dump_head();
kvn@8732 3114 }
kvn@8732 3115
duke@1 3116 if (TracePartialPeeling) {
duke@1 3117 tty->print_cr("before partial peel one iteration");
duke@1 3118 Node_List wl;
duke@1 3119 Node* t = head->in(2);
duke@1 3120 while (true) {
duke@1 3121 wl.push(t);
duke@1 3122 if (t == head) break;
duke@1 3123 t = idom(t);
duke@1 3124 }
duke@1 3125 while (wl.size() > 0) {
duke@1 3126 Node* tt = wl.pop();
duke@1 3127 tt->dump();
duke@1 3128 if (tt == last_peel) tty->print_cr("-- cut --");
duke@1 3129 }
duke@1 3130 }
duke@1 3131 #endif
duke@1 3132 ResourceArea *area = Thread::current()->resource_area();
duke@1 3133 VectorSet peel(area);
duke@1 3134 VectorSet not_peel(area);
duke@1 3135 Node_List peel_list(area);
duke@1 3136 Node_List worklist(area);
duke@1 3137 Node_List sink_list(area);
duke@1 3138
duke@1 3139 // Set of cfg nodes to peel are those that are executable from
duke@1 3140 // the head through last_peel.
duke@1 3141 assert(worklist.size() == 0, "should be empty");
duke@1 3142 worklist.push(head);
duke@1 3143 peel.set(head->_idx);
duke@1 3144 while (worklist.size() > 0) {
duke@1 3145 Node *n = worklist.pop();
duke@1 3146 if (n != last_peel) {
duke@1 3147 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
duke@1 3148 Node* use = n->fast_out(j);
duke@1 3149 if (use->is_CFG() &&
duke@1 3150 loop->is_member(get_loop(use)) &&
duke@1 3151 !peel.test_set(use->_idx)) {
duke@1 3152 worklist.push(use);
duke@1 3153 }
duke@1 3154 }
duke@1 3155 }
duke@1 3156 }
duke@1 3157
duke@1 3158 // Set of non-cfg nodes to peel are those that are control
duke@1 3159 // dependent on the cfg nodes.
duke@1 3160 uint i;
duke@1 3161 for(i = 0; i < loop->_body.size(); i++ ) {
duke@1 3162 Node *n = loop->_body.at(i);
duke@1 3163 Node *n_c = has_ctrl(n) ? get_ctrl(n) : n;
duke@1 3164 if (peel.test(n_c->_idx)) {
duke@1 3165 peel.set(n->_idx);
duke@1 3166 } else {
duke@1 3167 not_peel.set(n->_idx);
duke@1 3168 }
duke@1 3169 }
duke@1 3170
duke@1 3171 // Step 2: move operations from the peeled section down into the
duke@1 3172 // not-peeled section
duke@1 3173
duke@1 3174 // Get a post order schedule of nodes in the peel region
duke@1 3175 // Result in right-most operand.
duke@1 3176 scheduled_nodelist(loop, peel, peel_list );
duke@1 3177
duke@1 3178 assert(is_valid_loop_partition(loop, peel, peel_list, not_peel), "bad partition");
duke@1 3179
duke@1 3180 // For future check for too many new phis
duke@1 3181 uint old_phi_cnt = 0;
duke@1 3182 for (DUIterator_Fast jmax, j = head->fast_outs(jmax); j < jmax; j++) {
duke@1 3183 Node* use = head->fast_out(j);
duke@1 3184 if (use->is_Phi()) old_phi_cnt++;
duke@1 3185 }
duke@1 3186
duke@1 3187 #if !defined(PRODUCT)
duke@1 3188 if (TracePartialPeeling) {
duke@1 3189 tty->print_cr("\npeeled list");
duke@1 3190 }
duke@1 3191 #endif
duke@1 3192
duke@1 3193 // Evacuate nodes in peel region into the not_peeled region if possible
duke@1 3194 uint new_phi_cnt = 0;
kvn@17620 3195 uint cloned_for_outside_use = 0;
duke@1 3196 for (i = 0; i < peel_list.size();) {
duke@1 3197 Node* n = peel_list.at(i);
duke@1 3198 #if !defined(PRODUCT)
duke@1 3199 if (TracePartialPeeling) n->dump();
duke@1 3200 #endif
duke@1 3201 bool incr = true;
duke@1 3202 if ( !n->is_CFG() ) {
duke@1 3203
duke@1 3204 if ( has_use_in_set(n, not_peel) ) {
duke@1 3205
duke@1 3206 // If not used internal to the peeled region,
duke@1 3207 // move "n" from peeled to not_peeled region.
duke@1 3208
duke@1 3209 if ( !has_use_internal_to_set(n, peel, loop) ) {
duke@1 3210
duke@1 3211 // if not pinned and not a load (which maybe anti-dependent on a store)
duke@1 3212 // and not a CMove (Matcher expects only bool->cmove).
rkennke@52925 3213 if (n->in(0) == NULL && !n->is_Load() && !n->is_CMove() && n->Opcode() != Op_ShenandoahWBMemProj) {
kvn@17620 3214 cloned_for_outside_use += clone_for_use_outside_loop( loop, n, worklist );
duke@1 3215 sink_list.push(n);
duke@1 3216 peel >>= n->_idx; // delete n from peel set.
duke@1 3217 not_peel <<= n->_idx; // add n to not_peel set.
duke@1 3218 peel_list.remove(i);
duke@1 3219 incr = false;
duke@1 3220 #if !defined(PRODUCT)
duke@1 3221 if (TracePartialPeeling) {
duke@1 3222 tty->print_cr("sink to not_peeled region: %d newbb: %d",
duke@1 3223 n->_idx, get_ctrl(n)->_idx);
duke@1 3224 }
duke@1 3225 #endif
duke@1 3226 }
duke@1 3227 } else {
duke@1 3228 // Otherwise check for special def-use cases that span
duke@1 3229 // the peel/not_peel boundary such as bool->if
duke@1 3230 clone_for_special_use_inside_loop( loop, n, not_peel, sink_list, worklist );
duke@1 3231 new_phi_cnt++;
duke@1 3232 }
duke@1 3233 }
duke@1 3234 }
duke@1 3235 if (incr) i++;
duke@1 3236 }
duke@1 3237
duke@1 3238 if (new_phi_cnt > old_phi_cnt + PartialPeelNewPhiDelta) {
duke@1 3239 #if !defined(PRODUCT)
duke@1 3240 if (TracePartialPeeling) {
duke@1 3241 tty->print_cr("\nToo many new phis: %d old %d new cmpi: %c",
duke@1 3242 new_phi_cnt, old_phi_cnt, new_peel_if != NULL?'T':'F');
duke@1 3243 }
duke@1 3244 #endif
duke@1 3245 if (new_peel_if != NULL) {
duke@1 3246 remove_cmpi_loop_exit(new_peel_if, loop);
duke@1 3247 }
duke@1 3248 // Inhibit more partial peeling on this loop
duke@1 3249 assert(!head->is_partial_peel_loop(), "not partial peeled");
duke@1 3250 head->mark_partial_peel_failed();
kvn@17620 3251 if (cloned_for_outside_use > 0) {
kvn@17620 3252 // Terminate this round of loop opts because
kvn@17620 3253 // the graph outside this loop was changed.
kvn@17620 3254 C->set_major_progress();
kvn@17620 3255 return true;
kvn@17620 3256 }
duke@1 3257 return false;
duke@1 3258 }
duke@1 3259
duke@1 3260 // Step 3: clone loop, retarget control, and insert new phis
duke@1 3261
duke@1 3262 // Create new loop head for new phis and to hang
duke@1 3263 // the nodes being moved (sinked) from the peel region.
thartmann@24923 3264 LoopNode* new_head = new LoopNode(last_peel, last_peel);
kvn@8732 3265 new_head->set_unswitch_count(head->unswitch_count()); // Preserve
duke@1 3266 _igvn.register_new_node_with_optimizer(new_head);
duke@1 3267 assert(first_not_peeled->in(0) == last_peel, "last_peel <- first_not_peeled");
thartmann@25913 3268 _igvn.replace_input_of(first_not_peeled, 0, new_head);
duke@1 3269 set_loop(new_head, loop);
duke@1 3270 loop->_body.push(new_head);
duke@1 3271 not_peel.set(new_head->_idx);
duke@1 3272 set_idom(new_head, last_peel, dom_depth(first_not_peeled));
duke@1 3273 set_idom(first_not_peeled, new_head, dom_depth(first_not_peeled));
duke@1 3274
duke@1 3275 while (sink_list.size() > 0) {
duke@1 3276 Node* n = sink_list.pop();
duke@1 3277 set_ctrl(n, new_head);
duke@1 3278 }
duke@1 3279
duke@1 3280 assert(is_valid_loop_partition(loop, peel, peel_list, not_peel), "bad partition");
duke@1 3281
roland@48145 3282 clone_loop(loop, old_new, dd, IgnoreStripMined);
duke@1 3283
duke@1 3284 const uint clone_exit_idx = 1;
duke@1 3285 const uint orig_exit_idx = 2;
duke@1 3286 assert(is_valid_clone_loop_form( loop, peel_list, orig_exit_idx, clone_exit_idx ), "bad clone loop");
duke@1 3287
duke@1 3288 Node* head_clone = old_new[head->_idx];
duke@1 3289 LoopNode* new_head_clone = old_new[new_head->_idx]->as_Loop();
duke@1 3290 Node* orig_tail_clone = head_clone->in(2);
duke@1 3291
duke@1 3292 // Add phi if "def" node is in peel set and "use" is not
duke@1 3293
duke@1 3294 for(i = 0; i < peel_list.size(); i++ ) {
duke@1 3295 Node *def = peel_list.at(i);
duke@1 3296 if (!def->is_CFG()) {
duke@1 3297 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
duke@1 3298 Node *use = def->fast_out(j);
duke@1 3299 if (has_node(use) && use->in(0) != C->top() &&
duke@1 3300 (!peel.test(use->_idx) ||
duke@1 3301 (use->is_Phi() && use->in(0) == head)) ) {
duke@1 3302 worklist.push(use);
duke@1 3303 }
duke@1 3304 }
duke@1 3305 while( worklist.size() ) {
duke@1 3306 Node *use = worklist.pop();
duke@1 3307 for (uint j = 1; j < use->req(); j++) {
duke@1 3308 Node* n = use->in(j);
duke@1 3309 if (n == def) {
duke@1 3310
duke@1 3311 // "def" is in peel set, "use" is not in peel set
duke@1 3312 // or "use" is in the entry boundary (a phi) of the peel set
duke@1 3313
duke@1 3314 Node* use_c = has_ctrl(use) ? get_ctrl(use) : use;
duke@1 3315
duke@1 3316 if ( loop->is_member(get_loop( use_c )) ) {
duke@1 3317 // use is in loop
duke@1 3318 if (old_new[use->_idx] != NULL) { // null for dead code
duke@1 3319 Node* use_clone = old_new[use->_idx];
kvn@12958 3320 _igvn.replace_input_of(use, j, C->top());
duke@1 3321 insert_phi_for_loop( use_clone, j, old_new[def->_idx], def, new_head_clone );
duke@1 3322 }
duke@1 3323 } else {
duke@1 3324 assert(is_valid_clone_loop_exit_use(loop, use, orig_exit_idx), "clone loop format");
duke@1 3325 // use is not in the loop, check if the live range includes the cut
duke@1 3326 Node* lp_if = use_c->in(orig_exit_idx)->in(0);
duke@1 3327 if (not_peel.test(lp_if->_idx)) {
duke@1 3328 assert(j == orig_exit_idx, "use from original loop");
duke@1 3329 insert_phi_for_loop( use, clone_exit_idx, old_new[def->_idx], def, new_head_clone );
duke@1 3330 }
duke@1 3331 }
duke@1 3332 }
duke@1 3333 }
duke@1 3334 }
duke@1 3335 }
duke@1 3336 }
duke@1 3337
duke@1 3338 // Step 3b: retarget control
duke@1 3339
duke@1 3340 // Redirect control to the new loop head if a cloned node in
duke@1 3341 // the not_peeled region has control that points into the peeled region.
duke@1 3342 // This necessary because the cloned peeled region will be outside
duke@1 3343 // the loop.
duke@1 3344 // from to
duke@1 3345 // cloned-peeled <---+
duke@1 3346 // new_head_clone: | <--+
duke@1 3347 // cloned-not_peeled in(0) in(0)
duke@1 3348 // orig-peeled
duke@1 3349
duke@1 3350 for(i = 0; i < loop->_body.size(); i++ ) {
duke@1 3351 Node *n = loop->_body.at(i);
duke@1 3352 if (!n->is_CFG() && n->in(0) != NULL &&
duke@1 3353 not_peel.test(n->_idx) && peel.test(n->in(0)->_idx)) {
duke@1 3354 Node* n_clone = old_new[n->_idx];
kvn@12958 3355 _igvn.replace_input_of(n_clone, 0, new_head_clone);
duke@1 3356 }
duke@1 3357 }
duke@1 3358
duke@1 3359 // Backedge of the surviving new_head (the clone) is original last_peel
kvn@12958 3360 _igvn.replace_input_of(new_head_clone, LoopNode::LoopBackControl, last_peel);
duke@1 3361
duke@1 3362 // Cut first node in original not_peel set
kvn@12958 3363 _igvn.rehash_node_delayed(new_head); // Multiple edge updates:
kvn@12958 3364 new_head->set_req(LoopNode::EntryControl, C->top()); // use rehash_node_delayed / set_req instead of
kvn@12958 3365 new_head->set_req(LoopNode::LoopBackControl, C->top()); // multiple replace_input_of calls
duke@1 3366
duke@1 3367 // Copy head_clone back-branch info to original head
duke@1 3368 // and remove original head's loop entry and
duke@1 3369 // clone head's back-branch
kvn@12958 3370 _igvn.rehash_node_delayed(head); // Multiple edge updates
kvn@12958 3371 head->set_req(LoopNode::EntryControl, head_clone->in(LoopNode::LoopBackControl));
duke@1 3372 head->set_req(LoopNode::LoopBackControl, C->top());
kvn@12958 3373 _igvn.replace_input_of(head_clone, LoopNode::LoopBackControl, C->top());
duke@1 3374
duke@1 3375 // Similarly modify the phis
duke@1 3376 for (DUIterator_Fast kmax, k = head->fast_outs(kmax); k < kmax; k++) {
duke@1 3377 Node* use = head->fast_out(k);
duke@1 3378 if (use->is_Phi() && use->outcnt() > 0) {
duke@1 3379 Node* use_clone = old_new[use->_idx];
kvn@12958 3380 _igvn.rehash_node_delayed(use); // Multiple edge updates
kvn@12958 3381 use->set_req(LoopNode::EntryControl, use_clone->in(LoopNode::LoopBackControl));
duke@1 3382 use->set_req(LoopNode::LoopBackControl, C->top());
kvn@12958 3383 _igvn.replace_input_of(use_clone, LoopNode::LoopBackControl, C->top());
duke@1 3384 }
duke@1 3385 }
duke@1 3386
duke@1 3387 // Step 4: update dominator tree and dominator depth
duke@1 3388
duke@1 3389 set_idom(head, orig_tail_clone, dd);
duke@1 3390 recompute_dom_depth();
duke@1 3391
duke@1 3392 // Inhibit more partial peeling on this loop
duke@1 3393 new_head_clone->set_partial_peel_loop();
duke@1 3394 C->set_major_progress();
kvn@24020 3395 loop->record_for_igvn();
duke@1 3396
duke@1 3397 #if !defined(PRODUCT)
duke@1 3398 if (TracePartialPeeling) {
duke@1 3399 tty->print_cr("\nafter partial peel one iteration");
duke@1 3400 Node_List wl(area);
duke@1 3401 Node* t = last_peel;
duke@1 3402 while (true) {
duke@1 3403 wl.push(t);
duke@1 3404 if (t == head_clone) break;
duke@1 3405 t = idom(t);
duke@1 3406 }
duke@1 3407 while (wl.size() > 0) {
duke@1 3408 Node* tt = wl.pop();
duke@1 3409 if (tt == head) tty->print_cr("orig head");
duke@1 3410 else if (tt == new_head_clone) tty->print_cr("new head");
duke@1 3411 else if (tt == head_clone) tty->print_cr("clone head");
duke@1 3412 tt->dump();
duke@1 3413 }
duke@1 3414 }
duke@1 3415 #endif
duke@1 3416 return true;
duke@1 3417 }
duke@1 3418
duke@1 3419 //------------------------------reorg_offsets----------------------------------
duke@1 3420 // Reorganize offset computations to lower register pressure. Mostly
duke@1 3421 // prevent loop-fallout uses of the pre-incremented trip counter (which are
duke@1 3422 // then alive with the post-incremented trip counter forcing an extra
duke@1 3423 // register move)
kvn@8732 3424 void PhaseIdealLoop::reorg_offsets(IdealLoopTree *loop) {
kvn@8732 3425 // Perform it only for canonical counted loops.
kvn@8732 3426 // Loop's shape could be messed up by iteration_split_impl.
kvn@8732 3427 if (!loop->_head->is_CountedLoop())
kvn@8732 3428 return;
kvn@8732 3429 if (!loop->_head->as_Loop()->is_valid_counted_loop())
kvn@8732 3430 return;
duke@1 3431
duke@1 3432 CountedLoopNode *cl = loop->_head->as_CountedLoop();
duke@1 3433 CountedLoopEndNode *cle = cl->loopexit();
duke@1 3434 Node *exit = cle->proj_out(false);
kvn@8732 3435 Node *phi = cl->phi();
duke@1 3436
duke@1 3437 // Check for the special case of folks using the pre-incremented
duke@1 3438 // trip-counter on the fall-out path (forces the pre-incremented
duke@1 3439 // and post-incremented trip counter to be live at the same time).
duke@1 3440 // Fix this by adjusting to use the post-increment trip counter.
never@1399 3441
duke@1 3442 bool progress = true;
duke@1 3443 while (progress) {
duke@1 3444 progress = false;
duke@1 3445 for (DUIterator_Fast imax, i = phi->fast_outs(imax); i < imax; i++) {
duke@1 3446 Node* use = phi->fast_out(i); // User of trip-counter
duke@1 3447 if (!has_ctrl(use)) continue;
duke@1 3448 Node *u_ctrl = get_ctrl(use);
kvn@8732 3449 if (use->is_Phi()) {
duke@1 3450 u_ctrl = NULL;
kvn@8732 3451 for (uint j = 1; j < use->req(); j++)
kvn@8732 3452 if (use->in(j) == phi)
kvn@8732 3453 u_ctrl = dom_lca(u_ctrl, use->in(0)->in(j));
duke@1 3454 }
duke@1 3455 IdealLoopTree *u_loop = get_loop(u_ctrl);
duke@1 3456 // Look for loop-invariant use
kvn@8732 3457 if (u_loop == loop) continue;
kvn@8732 3458 if (loop->is_member(u_loop)) continue;
duke@1 3459 // Check that use is live out the bottom. Assuming the trip-counter
duke@1 3460 // update is right at the bottom, uses of of the loop middle are ok.
kvn@8732 3461 if (dom_lca(exit, u_ctrl) != exit) continue;
duke@1 3462 // Hit! Refactor use to use the post-incremented tripcounter.
duke@1 3463 // Compute a post-increment tripcounter.
roland@51828 3464 Node* c = exit;
roland@51828 3465 if (cl->is_strip_mined()) {
roland@51828 3466 IdealLoopTree* outer_loop = get_loop(cl->outer_loop());
roland@51828 3467 if (!outer_loop->is_member(u_loop)) {
roland@51828 3468 c = cl->outer_loop_exit();
roland@51828 3469 }
roland@51828 3470 }
roland@51828 3471 Node *opaq = new Opaque2Node(C, cle->incr());
roland@51828 3472 register_new_node(opaq, c);
duke@1 3473 Node *neg_stride = _igvn.intcon(-cle->stride_con());
duke@1 3474 set_ctrl(neg_stride, C->root());
roland@51828 3475 Node *post = new AddINode(opaq, neg_stride);
roland@51828 3476 register_new_node(post, c);
kvn@12958 3477 _igvn.rehash_node_delayed(use);
kvn@8732 3478 for (uint j = 1; j < use->req(); j++) {
kvn@8732 3479 if (use->in(j) == phi)
duke@1 3480 use->set_req(j, post);
kvn@8732 3481 }
duke@1 3482 // Since DU info changed, rerun loop
duke@1 3483 progress = true;
duke@1 3484 break;
duke@1 3485 }
duke@1 3486 }
duke@1 3487
duke@1 3488 }