src/share/vm/opto/lcm.cpp

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

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

#7046 C2 supports long branch
Contributed-by: fujie

aoqi@0 1 /*
dlong@7598 2 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@1 25 /*
aoqi@1 26 * This file has been modified by Loongson Technology in 2015. These
aoqi@1 27 * modifications are Copyright (c) 2015 Loongson Technology, and are made
aoqi@1 28 * available on the same license terms set forth above.
aoqi@1 29 */
aoqi@1 30
aoqi@0 31 #include "precompiled.hpp"
aoqi@0 32 #include "memory/allocation.inline.hpp"
aoqi@0 33 #include "opto/block.hpp"
aoqi@0 34 #include "opto/c2compiler.hpp"
aoqi@0 35 #include "opto/callnode.hpp"
aoqi@0 36 #include "opto/cfgnode.hpp"
aoqi@0 37 #include "opto/machnode.hpp"
aoqi@0 38 #include "opto/runtime.hpp"
dlong@7598 39 #if defined AD_MD_HPP
dlong@7598 40 # include AD_MD_HPP
dlong@7598 41 #elif defined TARGET_ARCH_MODEL_x86_32
aoqi@0 42 # include "adfiles/ad_x86_32.hpp"
dlong@7598 43 #elif defined TARGET_ARCH_MODEL_x86_64
aoqi@0 44 # include "adfiles/ad_x86_64.hpp"
dlong@7598 45 #elif defined TARGET_ARCH_MODEL_sparc
kvn@7332 46 # include "adfiles/ad_sparc.hpp"
dlong@7598 47 #elif defined TARGET_ARCH_MODEL_zero
kvn@7332 48 # include "adfiles/ad_zero.hpp"
dlong@7598 49 #elif defined TARGET_ARCH_MODEL_ppc_64
kvn@7332 50 # include "adfiles/ad_ppc_64.hpp"
aoqi@7994 51 #elif defined TARGET_ARCH_MODEL_mips_64
aoqi@1 52 # include "adfiles/ad_mips_64.hpp"
aoqi@1 53 #endif
aoqi@0 54
aoqi@0 55 // Optimization - Graph Style
aoqi@0 56
aoqi@0 57 // Check whether val is not-null-decoded compressed oop,
aoqi@0 58 // i.e. will grab into the base of the heap if it represents NULL.
aoqi@0 59 static bool accesses_heap_base_zone(Node *val) {
aoqi@0 60 if (Universe::narrow_oop_base() > 0) { // Implies UseCompressedOops.
aoqi@0 61 if (val && val->is_Mach()) {
aoqi@0 62 if (val->as_Mach()->ideal_Opcode() == Op_DecodeN) {
aoqi@0 63 // This assumes all Decodes with TypePtr::NotNull are matched to nodes that
aoqi@0 64 // decode NULL to point to the heap base (Decode_NN).
aoqi@0 65 if (val->bottom_type()->is_oopptr()->ptr() == TypePtr::NotNull) {
aoqi@0 66 return true;
aoqi@0 67 }
aoqi@0 68 }
aoqi@0 69 // Must recognize load operation with Decode matched in memory operand.
aoqi@0 70 // We should not reach here exept for PPC/AIX, as os::zero_page_read_protected()
aoqi@0 71 // returns true everywhere else. On PPC, no such memory operands
aoqi@0 72 // exist, therefore we did not yet implement a check for such operands.
aoqi@0 73 NOT_AIX(Unimplemented());
aoqi@0 74 }
aoqi@0 75 }
aoqi@0 76 return false;
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 static bool needs_explicit_null_check_for_read(Node *val) {
aoqi@0 80 // On some OSes (AIX) the page at address 0 is only write protected.
aoqi@0 81 // If so, only Store operations will trap.
aoqi@0 82 if (os::zero_page_read_protected()) {
aoqi@0 83 return false; // Implicit null check will work.
aoqi@0 84 }
aoqi@0 85 // Also a read accessing the base of a heap-based compressed heap will trap.
aoqi@0 86 if (accesses_heap_base_zone(val) && // Hits the base zone page.
aoqi@0 87 Universe::narrow_oop_use_implicit_null_checks()) { // Base zone page is protected.
aoqi@0 88 return false;
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 return true;
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 //------------------------------implicit_null_check----------------------------
aoqi@0 95 // Detect implicit-null-check opportunities. Basically, find NULL checks
aoqi@0 96 // with suitable memory ops nearby. Use the memory op to do the NULL check.
aoqi@0 97 // I can generate a memory op if there is not one nearby.
aoqi@0 98 // The proj is the control projection for the not-null case.
aoqi@0 99 // The val is the pointer being checked for nullness or
aoqi@0 100 // decodeHeapOop_not_null node if it did not fold into address.
aoqi@0 101 void PhaseCFG::implicit_null_check(Block* block, Node *proj, Node *val, int allowed_reasons) {
aoqi@0 102 // Assume if null check need for 0 offset then always needed
aoqi@0 103 // Intel solaris doesn't support any null checks yet and no
aoqi@0 104 // mechanism exists (yet) to set the switches at an os_cpu level
aoqi@0 105 if( !ImplicitNullChecks || MacroAssembler::needs_explicit_null_check(0)) return;
aoqi@0 106
aoqi@0 107 // Make sure the ptr-is-null path appears to be uncommon!
aoqi@0 108 float f = block->end()->as_MachIf()->_prob;
aoqi@0 109 if( proj->Opcode() == Op_IfTrue ) f = 1.0f - f;
aoqi@0 110 if( f > PROB_UNLIKELY_MAG(4) ) return;
aoqi@0 111
aoqi@0 112 uint bidx = 0; // Capture index of value into memop
aoqi@0 113 bool was_store; // Memory op is a store op
aoqi@0 114
aoqi@0 115 // Get the successor block for if the test ptr is non-null
aoqi@0 116 Block* not_null_block; // this one goes with the proj
aoqi@0 117 Block* null_block;
aoqi@0 118 if (block->get_node(block->number_of_nodes()-1) == proj) {
aoqi@0 119 null_block = block->_succs[0];
aoqi@0 120 not_null_block = block->_succs[1];
aoqi@0 121 } else {
aoqi@0 122 assert(block->get_node(block->number_of_nodes()-2) == proj, "proj is one or the other");
aoqi@0 123 not_null_block = block->_succs[0];
aoqi@0 124 null_block = block->_succs[1];
aoqi@0 125 }
aoqi@0 126 while (null_block->is_Empty() == Block::empty_with_goto) {
aoqi@0 127 null_block = null_block->_succs[0];
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 // Search the exception block for an uncommon trap.
aoqi@0 131 // (See Parse::do_if and Parse::do_ifnull for the reason
aoqi@0 132 // we need an uncommon trap. Briefly, we need a way to
aoqi@0 133 // detect failure of this optimization, as in 6366351.)
aoqi@0 134 {
aoqi@0 135 bool found_trap = false;
aoqi@0 136 for (uint i1 = 0; i1 < null_block->number_of_nodes(); i1++) {
aoqi@0 137 Node* nn = null_block->get_node(i1);
aoqi@0 138 if (nn->is_MachCall() &&
aoqi@0 139 nn->as_MachCall()->entry_point() == SharedRuntime::uncommon_trap_blob()->entry_point()) {
aoqi@0 140 const Type* trtype = nn->in(TypeFunc::Parms)->bottom_type();
aoqi@0 141 if (trtype->isa_int() && trtype->is_int()->is_con()) {
aoqi@0 142 jint tr_con = trtype->is_int()->get_con();
aoqi@0 143 Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(tr_con);
aoqi@0 144 Deoptimization::DeoptAction action = Deoptimization::trap_request_action(tr_con);
aoqi@0 145 assert((int)reason < (int)BitsPerInt, "recode bit map");
aoqi@0 146 if (is_set_nth_bit(allowed_reasons, (int) reason)
aoqi@0 147 && action != Deoptimization::Action_none) {
aoqi@0 148 // This uncommon trap is sure to recompile, eventually.
aoqi@0 149 // When that happens, C->too_many_traps will prevent
aoqi@0 150 // this transformation from happening again.
aoqi@0 151 found_trap = true;
aoqi@0 152 }
aoqi@0 153 }
aoqi@0 154 break;
aoqi@0 155 }
aoqi@0 156 }
aoqi@0 157 if (!found_trap) {
aoqi@0 158 // We did not find an uncommon trap.
aoqi@0 159 return;
aoqi@0 160 }
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 // Check for decodeHeapOop_not_null node which did not fold into address
aoqi@0 164 bool is_decoden = ((intptr_t)val) & 1;
aoqi@0 165 val = (Node*)(((intptr_t)val) & ~1);
aoqi@0 166
aoqi@0 167 assert(!is_decoden || (val->in(0) == NULL) && val->is_Mach() &&
aoqi@0 168 (val->as_Mach()->ideal_Opcode() == Op_DecodeN), "sanity");
aoqi@0 169
aoqi@0 170 // Search the successor block for a load or store who's base value is also
aoqi@0 171 // the tested value. There may be several.
aoqi@0 172 Node_List *out = new Node_List(Thread::current()->resource_area());
aoqi@0 173 MachNode *best = NULL; // Best found so far
aoqi@0 174 for (DUIterator i = val->outs(); val->has_out(i); i++) {
aoqi@0 175 Node *m = val->out(i);
aoqi@0 176 if( !m->is_Mach() ) continue;
aoqi@0 177 MachNode *mach = m->as_Mach();
aoqi@0 178 was_store = false;
aoqi@0 179 int iop = mach->ideal_Opcode();
aoqi@0 180 switch( iop ) {
aoqi@0 181 case Op_LoadB:
aoqi@0 182 case Op_LoadUB:
aoqi@0 183 case Op_LoadUS:
aoqi@0 184 case Op_LoadD:
aoqi@0 185 case Op_LoadF:
aoqi@0 186 case Op_LoadI:
aoqi@0 187 case Op_LoadL:
aoqi@0 188 case Op_LoadP:
aoqi@0 189 case Op_LoadN:
aoqi@0 190 case Op_LoadS:
aoqi@0 191 case Op_LoadKlass:
aoqi@0 192 case Op_LoadNKlass:
aoqi@0 193 case Op_LoadRange:
aoqi@0 194 case Op_LoadD_unaligned:
aoqi@0 195 case Op_LoadL_unaligned:
aoqi@0 196 assert(mach->in(2) == val, "should be address");
aoqi@0 197 break;
aoqi@0 198 case Op_StoreB:
aoqi@0 199 case Op_StoreC:
aoqi@0 200 case Op_StoreCM:
aoqi@0 201 case Op_StoreD:
aoqi@0 202 case Op_StoreF:
aoqi@0 203 case Op_StoreI:
aoqi@0 204 case Op_StoreL:
aoqi@0 205 case Op_StoreP:
aoqi@0 206 case Op_StoreN:
aoqi@0 207 case Op_StoreNKlass:
aoqi@0 208 was_store = true; // Memory op is a store op
aoqi@0 209 // Stores will have their address in slot 2 (memory in slot 1).
aoqi@0 210 // If the value being nul-checked is in another slot, it means we
aoqi@0 211 // are storing the checked value, which does NOT check the value!
aoqi@0 212 if( mach->in(2) != val ) continue;
aoqi@0 213 break; // Found a memory op?
aoqi@0 214 case Op_StrComp:
aoqi@0 215 case Op_StrEquals:
aoqi@0 216 case Op_StrIndexOf:
aoqi@0 217 case Op_AryEq:
aoqi@0 218 case Op_EncodeISOArray:
aoqi@0 219 // Not a legit memory op for implicit null check regardless of
aoqi@0 220 // embedded loads
aoqi@0 221 continue;
aoqi@0 222 default: // Also check for embedded loads
aoqi@0 223 if( !mach->needs_anti_dependence_check() )
aoqi@0 224 continue; // Not an memory op; skip it
aoqi@0 225 if( must_clone[iop] ) {
aoqi@0 226 // Do not move nodes which produce flags because
aoqi@0 227 // RA will try to clone it to place near branch and
aoqi@0 228 // it will cause recompilation, see clone_node().
aoqi@0 229 continue;
aoqi@0 230 }
aoqi@0 231 {
aoqi@0 232 // Check that value is used in memory address in
aoqi@0 233 // instructions with embedded load (CmpP val1,(val2+off)).
aoqi@0 234 Node* base;
aoqi@0 235 Node* index;
aoqi@0 236 const MachOper* oper = mach->memory_inputs(base, index);
aoqi@0 237 if (oper == NULL || oper == (MachOper*)-1) {
aoqi@0 238 continue; // Not an memory op; skip it
aoqi@0 239 }
aoqi@0 240 if (val == base ||
aoqi@0 241 val == index && val->bottom_type()->isa_narrowoop()) {
aoqi@0 242 break; // Found it
aoqi@0 243 } else {
aoqi@0 244 continue; // Skip it
aoqi@0 245 }
aoqi@0 246 }
aoqi@0 247 break;
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 // On some OSes (AIX) the page at address 0 is only write protected.
aoqi@0 251 // If so, only Store operations will trap.
aoqi@0 252 // But a read accessing the base of a heap-based compressed heap will trap.
aoqi@0 253 if (!was_store && needs_explicit_null_check_for_read(val)) {
aoqi@0 254 continue;
aoqi@0 255 }
aoqi@0 256
kvn@8615 257 // Check that node's control edge is not-null block's head or dominates it,
kvn@8615 258 // otherwise we can't hoist it because there are other control dependencies.
kvn@8615 259 Node* ctrl = mach->in(0);
kvn@8615 260 if (ctrl != NULL && !(ctrl == not_null_block->head() ||
kvn@8615 261 get_block_for_node(ctrl)->dominates(not_null_block))) {
kvn@8615 262 continue;
kvn@8615 263 }
kvn@8615 264
aoqi@0 265 // check if the offset is not too high for implicit exception
aoqi@0 266 {
aoqi@0 267 intptr_t offset = 0;
aoqi@0 268 const TypePtr *adr_type = NULL; // Do not need this return value here
aoqi@0 269 const Node* base = mach->get_base_and_disp(offset, adr_type);
aoqi@0 270 if (base == NULL || base == NodeSentinel) {
aoqi@0 271 // Narrow oop address doesn't have base, only index
aoqi@0 272 if( val->bottom_type()->isa_narrowoop() &&
aoqi@0 273 MacroAssembler::needs_explicit_null_check(offset) )
aoqi@0 274 continue; // Give up if offset is beyond page size
aoqi@0 275 // cannot reason about it; is probably not implicit null exception
aoqi@0 276 } else {
aoqi@0 277 const TypePtr* tptr;
aoqi@0 278 if (UseCompressedOops && (Universe::narrow_oop_shift() == 0 ||
aoqi@0 279 Universe::narrow_klass_shift() == 0)) {
aoqi@0 280 // 32-bits narrow oop can be the base of address expressions
aoqi@0 281 tptr = base->get_ptr_type();
aoqi@0 282 } else {
aoqi@0 283 // only regular oops are expected here
aoqi@0 284 tptr = base->bottom_type()->is_ptr();
aoqi@0 285 }
aoqi@0 286 // Give up if offset is not a compile-time constant
aoqi@0 287 if( offset == Type::OffsetBot || tptr->_offset == Type::OffsetBot )
aoqi@0 288 continue;
aoqi@0 289 offset += tptr->_offset; // correct if base is offseted
aoqi@0 290 if( MacroAssembler::needs_explicit_null_check(offset) )
aoqi@0 291 continue; // Give up is reference is beyond 4K page size
aoqi@0 292 }
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 // Check ctrl input to see if the null-check dominates the memory op
aoqi@0 296 Block *cb = get_block_for_node(mach);
aoqi@0 297 cb = cb->_idom; // Always hoist at least 1 block
aoqi@0 298 if( !was_store ) { // Stores can be hoisted only one block
aoqi@0 299 while( cb->_dom_depth > (block->_dom_depth + 1))
aoqi@0 300 cb = cb->_idom; // Hoist loads as far as we want
aoqi@0 301 // The non-null-block should dominate the memory op, too. Live
aoqi@0 302 // range spilling will insert a spill in the non-null-block if it is
aoqi@0 303 // needs to spill the memory op for an implicit null check.
aoqi@0 304 if (cb->_dom_depth == (block->_dom_depth + 1)) {
aoqi@0 305 if (cb != not_null_block) continue;
aoqi@0 306 cb = cb->_idom;
aoqi@0 307 }
aoqi@0 308 }
aoqi@0 309 if( cb != block ) continue;
aoqi@0 310
aoqi@0 311 // Found a memory user; see if it can be hoisted to check-block
aoqi@0 312 uint vidx = 0; // Capture index of value into memop
aoqi@0 313 uint j;
aoqi@0 314 for( j = mach->req()-1; j > 0; j-- ) {
aoqi@0 315 if( mach->in(j) == val ) {
aoqi@0 316 vidx = j;
aoqi@0 317 // Ignore DecodeN val which could be hoisted to where needed.
aoqi@0 318 if( is_decoden ) continue;
aoqi@0 319 }
aoqi@0 320 // Block of memory-op input
aoqi@0 321 Block *inb = get_block_for_node(mach->in(j));
aoqi@0 322 Block *b = block; // Start from nul check
aoqi@0 323 while( b != inb && b->_dom_depth > inb->_dom_depth )
aoqi@0 324 b = b->_idom; // search upwards for input
aoqi@0 325 // See if input dominates null check
aoqi@0 326 if( b != inb )
aoqi@0 327 break;
aoqi@0 328 }
aoqi@0 329 if( j > 0 )
aoqi@0 330 continue;
aoqi@0 331 Block *mb = get_block_for_node(mach);
aoqi@0 332 // Hoisting stores requires more checks for the anti-dependence case.
aoqi@0 333 // Give up hoisting if we have to move the store past any load.
aoqi@0 334 if( was_store ) {
aoqi@0 335 Block *b = mb; // Start searching here for a local load
aoqi@0 336 // mach use (faulting) trying to hoist
aoqi@0 337 // n might be blocker to hoisting
aoqi@0 338 while( b != block ) {
aoqi@0 339 uint k;
aoqi@0 340 for( k = 1; k < b->number_of_nodes(); k++ ) {
aoqi@0 341 Node *n = b->get_node(k);
aoqi@0 342 if( n->needs_anti_dependence_check() &&
aoqi@0 343 n->in(LoadNode::Memory) == mach->in(StoreNode::Memory) )
aoqi@0 344 break; // Found anti-dependent load
aoqi@0 345 }
aoqi@0 346 if( k < b->number_of_nodes() )
aoqi@0 347 break; // Found anti-dependent load
aoqi@0 348 // Make sure control does not do a merge (would have to check allpaths)
aoqi@0 349 if( b->num_preds() != 2 ) break;
aoqi@0 350 b = get_block_for_node(b->pred(1)); // Move up to predecessor block
aoqi@0 351 }
aoqi@0 352 if( b != block ) continue;
aoqi@0 353 }
aoqi@0 354
aoqi@0 355 // Make sure this memory op is not already being used for a NullCheck
aoqi@0 356 Node *e = mb->end();
aoqi@0 357 if( e->is_MachNullCheck() && e->in(1) == mach )
aoqi@0 358 continue; // Already being used as a NULL check
aoqi@0 359
aoqi@0 360 // Found a candidate! Pick one with least dom depth - the highest
aoqi@0 361 // in the dom tree should be closest to the null check.
aoqi@0 362 if (best == NULL || get_block_for_node(mach)->_dom_depth < get_block_for_node(best)->_dom_depth) {
aoqi@0 363 best = mach;
aoqi@0 364 bidx = vidx;
aoqi@0 365 }
aoqi@0 366 }
aoqi@0 367 // No candidate!
aoqi@0 368 if (best == NULL) {
aoqi@0 369 return;
aoqi@0 370 }
aoqi@0 371
aoqi@0 372 // ---- Found an implicit null check
aoqi@0 373 extern int implicit_null_checks;
aoqi@0 374 implicit_null_checks++;
aoqi@0 375
aoqi@0 376 if( is_decoden ) {
aoqi@0 377 // Check if we need to hoist decodeHeapOop_not_null first.
aoqi@0 378 Block *valb = get_block_for_node(val);
aoqi@0 379 if( block != valb && block->_dom_depth < valb->_dom_depth ) {
aoqi@0 380 // Hoist it up to the end of the test block.
aoqi@0 381 valb->find_remove(val);
aoqi@0 382 block->add_inst(val);
aoqi@0 383 map_node_to_block(val, block);
aoqi@0 384 // DecodeN on x86 may kill flags. Check for flag-killing projections
aoqi@0 385 // that also need to be hoisted.
aoqi@0 386 for (DUIterator_Fast jmax, j = val->fast_outs(jmax); j < jmax; j++) {
aoqi@0 387 Node* n = val->fast_out(j);
aoqi@0 388 if( n->is_MachProj() ) {
aoqi@0 389 get_block_for_node(n)->find_remove(n);
aoqi@0 390 block->add_inst(n);
aoqi@0 391 map_node_to_block(n, block);
aoqi@0 392 }
aoqi@0 393 }
aoqi@0 394 }
aoqi@0 395 }
aoqi@0 396 // Hoist the memory candidate up to the end of the test block.
aoqi@0 397 Block *old_block = get_block_for_node(best);
aoqi@0 398 old_block->find_remove(best);
aoqi@0 399 block->add_inst(best);
aoqi@0 400 map_node_to_block(best, block);
aoqi@0 401
kvn@8615 402 // Move the control dependence if it is pinned to not-null block.
kvn@8615 403 // Don't change it in other cases: NULL or dominating control.
kvn@8615 404 if (best->in(0) == not_null_block->head()) {
kvn@8615 405 // Set it to control edge of null check.
kvn@8615 406 best->set_req(0, proj->in(0)->in(0));
kvn@8615 407 }
aoqi@0 408
aoqi@0 409 // Check for flag-killing projections that also need to be hoisted
aoqi@0 410 // Should be DU safe because no edge updates.
aoqi@0 411 for (DUIterator_Fast jmax, j = best->fast_outs(jmax); j < jmax; j++) {
aoqi@0 412 Node* n = best->fast_out(j);
aoqi@0 413 if( n->is_MachProj() ) {
aoqi@0 414 get_block_for_node(n)->find_remove(n);
aoqi@0 415 block->add_inst(n);
aoqi@0 416 map_node_to_block(n, block);
aoqi@0 417 }
aoqi@0 418 }
aoqi@0 419
aoqi@0 420 // proj==Op_True --> ne test; proj==Op_False --> eq test.
aoqi@0 421 // One of two graph shapes got matched:
aoqi@0 422 // (IfTrue (If (Bool NE (CmpP ptr NULL))))
aoqi@0 423 // (IfFalse (If (Bool EQ (CmpP ptr NULL))))
aoqi@0 424 // NULL checks are always branch-if-eq. If we see a IfTrue projection
aoqi@0 425 // then we are replacing a 'ne' test with a 'eq' NULL check test.
aoqi@0 426 // We need to flip the projections to keep the same semantics.
aoqi@0 427 if( proj->Opcode() == Op_IfTrue ) {
aoqi@0 428 // Swap order of projections in basic block to swap branch targets
aoqi@0 429 Node *tmp1 = block->get_node(block->end_idx()+1);
aoqi@0 430 Node *tmp2 = block->get_node(block->end_idx()+2);
aoqi@0 431 block->map_node(tmp2, block->end_idx()+1);
aoqi@0 432 block->map_node(tmp1, block->end_idx()+2);
aoqi@0 433 Node *tmp = new (C) Node(C->top()); // Use not NULL input
aoqi@0 434 tmp1->replace_by(tmp);
aoqi@0 435 tmp2->replace_by(tmp1);
aoqi@0 436 tmp->replace_by(tmp2);
aoqi@0 437 tmp->destruct();
aoqi@0 438 }
aoqi@0 439
aoqi@0 440 // Remove the existing null check; use a new implicit null check instead.
aoqi@0 441 // Since schedule-local needs precise def-use info, we need to correct
aoqi@0 442 // it as well.
aoqi@0 443 Node *old_tst = proj->in(0);
aoqi@0 444 MachNode *nul_chk = new (C) MachNullCheckNode(old_tst->in(0),best,bidx);
aoqi@0 445 block->map_node(nul_chk, block->end_idx());
aoqi@0 446 map_node_to_block(nul_chk, block);
aoqi@0 447 // Redirect users of old_test to nul_chk
aoqi@0 448 for (DUIterator_Last i2min, i2 = old_tst->last_outs(i2min); i2 >= i2min; --i2)
aoqi@0 449 old_tst->last_out(i2)->set_req(0, nul_chk);
aoqi@0 450 // Clean-up any dead code
thartmann@7559 451 for (uint i3 = 0; i3 < old_tst->req(); i3++) {
thartmann@7559 452 Node* in = old_tst->in(i3);
aoqi@0 453 old_tst->set_req(i3, NULL);
thartmann@7559 454 if (in->outcnt() == 0) {
thartmann@7559 455 // Remove dead input node
thartmann@7559 456 in->disconnect_inputs(NULL, C);
thartmann@7559 457 block->find_remove(in);
thartmann@7559 458 }
thartmann@7559 459 }
aoqi@0 460
aoqi@0 461 latency_from_uses(nul_chk);
aoqi@0 462 latency_from_uses(best);
kvn@8615 463
kvn@8615 464 // insert anti-dependences to defs in this block
kvn@8615 465 if (! best->needs_anti_dependence_check()) {
kvn@8615 466 for (uint k = 1; k < block->number_of_nodes(); k++) {
kvn@8615 467 Node *n = block->get_node(k);
kvn@8615 468 if (n->needs_anti_dependence_check() &&
kvn@8615 469 n->in(LoadNode::Memory) == best->in(StoreNode::Memory)) {
kvn@8615 470 // Found anti-dependent load
kvn@8615 471 insert_anti_dependences(block, n);
kvn@8615 472 }
kvn@8615 473 }
kvn@8615 474 }
aoqi@0 475 }
aoqi@0 476
aoqi@0 477
aoqi@0 478 //------------------------------select-----------------------------------------
aoqi@0 479 // Select a nice fellow from the worklist to schedule next. If there is only
aoqi@0 480 // one choice, then use it. Projections take top priority for correctness
aoqi@0 481 // reasons - if I see a projection, then it is next. There are a number of
aoqi@0 482 // other special cases, for instructions that consume condition codes, et al.
aoqi@0 483 // These are chosen immediately. Some instructions are required to immediately
aoqi@0 484 // precede the last instruction in the block, and these are taken last. Of the
aoqi@0 485 // remaining cases (most), choose the instruction with the greatest latency
aoqi@0 486 // (that is, the most number of pseudo-cycles required to the end of the
aoqi@0 487 // routine). If there is a tie, choose the instruction with the most inputs.
aoqi@0 488 Node* PhaseCFG::select(Block* block, Node_List &worklist, GrowableArray<int> &ready_cnt, VectorSet &next_call, uint sched_slot) {
aoqi@0 489
aoqi@0 490 // If only a single entry on the stack, use it
aoqi@0 491 uint cnt = worklist.size();
aoqi@0 492 if (cnt == 1) {
aoqi@0 493 Node *n = worklist[0];
aoqi@0 494 worklist.map(0,worklist.pop());
aoqi@0 495 return n;
aoqi@0 496 }
aoqi@0 497
aoqi@0 498 uint choice = 0; // Bigger is most important
aoqi@0 499 uint latency = 0; // Bigger is scheduled first
aoqi@0 500 uint score = 0; // Bigger is better
aoqi@0 501 int idx = -1; // Index in worklist
aoqi@0 502 int cand_cnt = 0; // Candidate count
aoqi@0 503
aoqi@0 504 for( uint i=0; i<cnt; i++ ) { // Inspect entire worklist
aoqi@0 505 // Order in worklist is used to break ties.
aoqi@0 506 // See caller for how this is used to delay scheduling
aoqi@0 507 // of induction variable increments to after the other
aoqi@0 508 // uses of the phi are scheduled.
aoqi@0 509 Node *n = worklist[i]; // Get Node on worklist
aoqi@0 510
aoqi@0 511 int iop = n->is_Mach() ? n->as_Mach()->ideal_Opcode() : 0;
aoqi@0 512 if( n->is_Proj() || // Projections always win
aoqi@0 513 n->Opcode()== Op_Con || // So does constant 'Top'
aoqi@0 514 iop == Op_CreateEx || // Create-exception must start block
aoqi@0 515 iop == Op_CheckCastPP
aoqi@0 516 ) {
aoqi@0 517 worklist.map(i,worklist.pop());
aoqi@0 518 return n;
aoqi@0 519 }
aoqi@0 520
aoqi@0 521 // Final call in a block must be adjacent to 'catch'
aoqi@0 522 Node *e = block->end();
aoqi@0 523 if( e->is_Catch() && e->in(0)->in(0) == n )
aoqi@0 524 continue;
aoqi@0 525
aoqi@0 526 // Memory op for an implicit null check has to be at the end of the block
aoqi@0 527 if( e->is_MachNullCheck() && e->in(1) == n )
aoqi@0 528 continue;
aoqi@0 529
aoqi@0 530 // Schedule IV increment last.
aoqi@0 531 if (e->is_Mach() && e->as_Mach()->ideal_Opcode() == Op_CountedLoopEnd &&
aoqi@0 532 e->in(1)->in(1) == n && n->is_iteratively_computed())
aoqi@0 533 continue;
aoqi@0 534
aoqi@0 535 uint n_choice = 2;
aoqi@0 536
aoqi@0 537 // See if this instruction is consumed by a branch. If so, then (as the
aoqi@0 538 // branch is the last instruction in the basic block) force it to the
aoqi@0 539 // end of the basic block
aoqi@0 540 if ( must_clone[iop] ) {
aoqi@0 541 // See if any use is a branch
aoqi@0 542 bool found_machif = false;
aoqi@0 543
aoqi@0 544 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
aoqi@0 545 Node* use = n->fast_out(j);
aoqi@0 546
aoqi@0 547 // The use is a conditional branch, make them adjacent
aoqi@0 548 if (use->is_MachIf() && get_block_for_node(use) == block) {
aoqi@0 549 found_machif = true;
aoqi@0 550 break;
aoqi@0 551 }
aoqi@0 552
aoqi@0 553 // More than this instruction pending for successor to be ready,
aoqi@0 554 // don't choose this if other opportunities are ready
aoqi@0 555 if (ready_cnt.at(use->_idx) > 1)
aoqi@0 556 n_choice = 1;
aoqi@0 557 }
aoqi@0 558
aoqi@0 559 // loop terminated, prefer not to use this instruction
aoqi@0 560 if (found_machif)
aoqi@0 561 continue;
aoqi@0 562 }
aoqi@0 563
aoqi@0 564 // See if this has a predecessor that is "must_clone", i.e. sets the
aoqi@0 565 // condition code. If so, choose this first
aoqi@0 566 for (uint j = 0; j < n->req() ; j++) {
aoqi@0 567 Node *inn = n->in(j);
aoqi@0 568 if (inn) {
aoqi@0 569 if (inn->is_Mach() && must_clone[inn->as_Mach()->ideal_Opcode()] ) {
aoqi@0 570 n_choice = 3;
aoqi@0 571 break;
aoqi@0 572 }
aoqi@0 573 }
aoqi@0 574 }
aoqi@0 575
aoqi@0 576 // MachTemps should be scheduled last so they are near their uses
aoqi@0 577 if (n->is_MachTemp()) {
aoqi@0 578 n_choice = 1;
aoqi@0 579 }
aoqi@0 580
aoqi@0 581 uint n_latency = get_latency_for_node(n);
aoqi@0 582 uint n_score = n->req(); // Many inputs get high score to break ties
aoqi@0 583
aoqi@0 584 // Keep best latency found
aoqi@0 585 cand_cnt++;
aoqi@0 586 if (choice < n_choice ||
aoqi@0 587 (choice == n_choice &&
aoqi@0 588 ((StressLCM && Compile::randomized_select(cand_cnt)) ||
aoqi@0 589 (!StressLCM &&
aoqi@0 590 (latency < n_latency ||
aoqi@0 591 (latency == n_latency &&
aoqi@0 592 (score < n_score))))))) {
aoqi@0 593 choice = n_choice;
aoqi@0 594 latency = n_latency;
aoqi@0 595 score = n_score;
aoqi@0 596 idx = i; // Also keep index in worklist
aoqi@0 597 }
aoqi@0 598 } // End of for all ready nodes in worklist
aoqi@0 599
aoqi@0 600 assert(idx >= 0, "index should be set");
aoqi@0 601 Node *n = worklist[(uint)idx]; // Get the winner
aoqi@0 602
aoqi@0 603 worklist.map((uint)idx, worklist.pop()); // Compress worklist
aoqi@0 604 return n;
aoqi@0 605 }
aoqi@0 606
aoqi@0 607
aoqi@0 608 //------------------------------set_next_call----------------------------------
aoqi@0 609 void PhaseCFG::set_next_call(Block* block, Node* n, VectorSet& next_call) {
aoqi@0 610 if( next_call.test_set(n->_idx) ) return;
aoqi@0 611 for( uint i=0; i<n->len(); i++ ) {
aoqi@0 612 Node *m = n->in(i);
aoqi@0 613 if( !m ) continue; // must see all nodes in block that precede call
aoqi@0 614 if (get_block_for_node(m) == block) {
aoqi@0 615 set_next_call(block, m, next_call);
aoqi@0 616 }
aoqi@0 617 }
aoqi@0 618 }
aoqi@0 619
aoqi@0 620 //------------------------------needed_for_next_call---------------------------
aoqi@0 621 // Set the flag 'next_call' for each Node that is needed for the next call to
aoqi@0 622 // be scheduled. This flag lets me bias scheduling so Nodes needed for the
aoqi@0 623 // next subroutine call get priority - basically it moves things NOT needed
aoqi@0 624 // for the next call till after the call. This prevents me from trying to
aoqi@0 625 // carry lots of stuff live across a call.
aoqi@0 626 void PhaseCFG::needed_for_next_call(Block* block, Node* this_call, VectorSet& next_call) {
aoqi@0 627 // Find the next control-defining Node in this block
aoqi@0 628 Node* call = NULL;
aoqi@0 629 for (DUIterator_Fast imax, i = this_call->fast_outs(imax); i < imax; i++) {
aoqi@0 630 Node* m = this_call->fast_out(i);
aoqi@0 631 if (get_block_for_node(m) == block && // Local-block user
aoqi@0 632 m != this_call && // Not self-start node
aoqi@0 633 m->is_MachCall()) {
aoqi@0 634 call = m;
aoqi@0 635 break;
aoqi@0 636 }
aoqi@0 637 }
aoqi@0 638 if (call == NULL) return; // No next call (e.g., block end is near)
aoqi@0 639 // Set next-call for all inputs to this call
aoqi@0 640 set_next_call(block, call, next_call);
aoqi@0 641 }
aoqi@0 642
aoqi@0 643 //------------------------------add_call_kills-------------------------------------
aoqi@0 644 // helper function that adds caller save registers to MachProjNode
aoqi@0 645 static void add_call_kills(MachProjNode *proj, RegMask& regs, const char* save_policy, bool exclude_soe) {
aoqi@0 646 // Fill in the kill mask for the call
aoqi@0 647 for( OptoReg::Name r = OptoReg::Name(0); r < _last_Mach_Reg; r=OptoReg::add(r,1) ) {
aoqi@0 648 if( !regs.Member(r) ) { // Not already defined by the call
aoqi@0 649 // Save-on-call register?
aoqi@0 650 if ((save_policy[r] == 'C') ||
aoqi@0 651 (save_policy[r] == 'A') ||
aoqi@0 652 ((save_policy[r] == 'E') && exclude_soe)) {
aoqi@0 653 proj->_rout.Insert(r);
aoqi@0 654 }
aoqi@0 655 }
aoqi@0 656 }
aoqi@0 657 }
aoqi@0 658
aoqi@0 659
aoqi@0 660 //------------------------------sched_call-------------------------------------
aoqi@0 661 uint PhaseCFG::sched_call(Block* block, uint node_cnt, Node_List& worklist, GrowableArray<int>& ready_cnt, MachCallNode* mcall, VectorSet& next_call) {
aoqi@0 662 RegMask regs;
aoqi@0 663
aoqi@0 664 // Schedule all the users of the call right now. All the users are
aoqi@0 665 // projection Nodes, so they must be scheduled next to the call.
aoqi@0 666 // Collect all the defined registers.
aoqi@0 667 for (DUIterator_Fast imax, i = mcall->fast_outs(imax); i < imax; i++) {
aoqi@0 668 Node* n = mcall->fast_out(i);
aoqi@0 669 assert( n->is_MachProj(), "" );
aoqi@0 670 int n_cnt = ready_cnt.at(n->_idx)-1;
aoqi@0 671 ready_cnt.at_put(n->_idx, n_cnt);
aoqi@0 672 assert( n_cnt == 0, "" );
aoqi@0 673 // Schedule next to call
aoqi@0 674 block->map_node(n, node_cnt++);
aoqi@0 675 // Collect defined registers
aoqi@0 676 regs.OR(n->out_RegMask());
aoqi@0 677 // Check for scheduling the next control-definer
aoqi@0 678 if( n->bottom_type() == Type::CONTROL )
aoqi@0 679 // Warm up next pile of heuristic bits
aoqi@0 680 needed_for_next_call(block, n, next_call);
aoqi@0 681
aoqi@0 682 // Children of projections are now all ready
aoqi@0 683 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
aoqi@0 684 Node* m = n->fast_out(j); // Get user
aoqi@0 685 if(get_block_for_node(m) != block) {
aoqi@0 686 continue;
aoqi@0 687 }
aoqi@0 688 if( m->is_Phi() ) continue;
aoqi@0 689 int m_cnt = ready_cnt.at(m->_idx)-1;
aoqi@0 690 ready_cnt.at_put(m->_idx, m_cnt);
aoqi@0 691 if( m_cnt == 0 )
aoqi@0 692 worklist.push(m);
aoqi@0 693 }
aoqi@0 694
aoqi@0 695 }
aoqi@0 696
aoqi@0 697 // Act as if the call defines the Frame Pointer.
aoqi@0 698 // Certainly the FP is alive and well after the call.
aoqi@0 699 regs.Insert(_matcher.c_frame_pointer());
aoqi@0 700
aoqi@0 701 // Set all registers killed and not already defined by the call.
aoqi@0 702 uint r_cnt = mcall->tf()->range()->cnt();
aoqi@0 703 int op = mcall->ideal_Opcode();
aoqi@0 704 MachProjNode *proj = new (C) MachProjNode( mcall, r_cnt+1, RegMask::Empty, MachProjNode::fat_proj );
aoqi@0 705 map_node_to_block(proj, block);
aoqi@0 706 block->insert_node(proj, node_cnt++);
aoqi@0 707
aoqi@0 708 // Select the right register save policy.
csahu@8316 709 const char *save_policy = NULL;
aoqi@0 710 switch (op) {
aoqi@0 711 case Op_CallRuntime:
aoqi@0 712 case Op_CallLeaf:
aoqi@0 713 case Op_CallLeafNoFP:
aoqi@0 714 // Calling C code so use C calling convention
aoqi@0 715 save_policy = _matcher._c_reg_save_policy;
aoqi@0 716 break;
aoqi@0 717
aoqi@0 718 case Op_CallStaticJava:
aoqi@0 719 case Op_CallDynamicJava:
aoqi@0 720 // Calling Java code so use Java calling convention
aoqi@0 721 save_policy = _matcher._register_save_policy;
aoqi@0 722 break;
aoqi@0 723
aoqi@0 724 default:
aoqi@0 725 ShouldNotReachHere();
aoqi@0 726 }
aoqi@0 727
aoqi@0 728 // When using CallRuntime mark SOE registers as killed by the call
aoqi@0 729 // so values that could show up in the RegisterMap aren't live in a
aoqi@0 730 // callee saved register since the register wouldn't know where to
aoqi@0 731 // find them. CallLeaf and CallLeafNoFP are ok because they can't
aoqi@0 732 // have debug info on them. Strictly speaking this only needs to be
aoqi@0 733 // done for oops since idealreg2debugmask takes care of debug info
aoqi@0 734 // references but there no way to handle oops differently than other
aoqi@0 735 // pointers as far as the kill mask goes.
aoqi@0 736 bool exclude_soe = op == Op_CallRuntime;
aoqi@0 737
aoqi@0 738 // If the call is a MethodHandle invoke, we need to exclude the
aoqi@0 739 // register which is used to save the SP value over MH invokes from
aoqi@0 740 // the mask. Otherwise this register could be used for
aoqi@0 741 // deoptimization information.
aoqi@0 742 if (op == Op_CallStaticJava) {
aoqi@0 743 MachCallStaticJavaNode* mcallstaticjava = (MachCallStaticJavaNode*) mcall;
aoqi@0 744 if (mcallstaticjava->_method_handle_invoke)
aoqi@0 745 proj->_rout.OR(Matcher::method_handle_invoke_SP_save_mask());
aoqi@0 746 }
aoqi@0 747
aoqi@0 748 add_call_kills(proj, regs, save_policy, exclude_soe);
aoqi@0 749
aoqi@0 750 return node_cnt;
aoqi@0 751 }
aoqi@0 752
aoqi@0 753
aoqi@0 754 //------------------------------schedule_local---------------------------------
aoqi@0 755 // Topological sort within a block. Someday become a real scheduler.
aoqi@0 756 bool PhaseCFG::schedule_local(Block* block, GrowableArray<int>& ready_cnt, VectorSet& next_call) {
aoqi@0 757 // Already "sorted" are the block start Node (as the first entry), and
aoqi@0 758 // the block-ending Node and any trailing control projections. We leave
aoqi@0 759 // these alone. PhiNodes and ParmNodes are made to follow the block start
aoqi@0 760 // Node. Everything else gets topo-sorted.
aoqi@0 761
aoqi@0 762 #ifndef PRODUCT
aoqi@0 763 if (trace_opto_pipelining()) {
aoqi@0 764 tty->print_cr("# --- schedule_local B%d, before: ---", block->_pre_order);
aoqi@0 765 for (uint i = 0;i < block->number_of_nodes(); i++) {
aoqi@0 766 tty->print("# ");
aoqi@0 767 block->get_node(i)->fast_dump();
aoqi@0 768 }
aoqi@0 769 tty->print_cr("#");
aoqi@0 770 }
aoqi@0 771 #endif
aoqi@0 772
aoqi@0 773 // RootNode is already sorted
aoqi@0 774 if (block->number_of_nodes() == 1) {
aoqi@0 775 return true;
aoqi@0 776 }
aoqi@0 777
aoqi@0 778 // Move PhiNodes and ParmNodes from 1 to cnt up to the start
aoqi@0 779 uint node_cnt = block->end_idx();
aoqi@0 780 uint phi_cnt = 1;
aoqi@0 781 uint i;
aoqi@0 782 for( i = 1; i<node_cnt; i++ ) { // Scan for Phi
aoqi@0 783 Node *n = block->get_node(i);
aoqi@0 784 if( n->is_Phi() || // Found a PhiNode or ParmNode
aoqi@0 785 (n->is_Proj() && n->in(0) == block->head()) ) {
aoqi@0 786 // Move guy at 'phi_cnt' to the end; makes a hole at phi_cnt
aoqi@0 787 block->map_node(block->get_node(phi_cnt), i);
aoqi@0 788 block->map_node(n, phi_cnt++); // swap Phi/Parm up front
aoqi@0 789 } else { // All others
aoqi@0 790 // Count block-local inputs to 'n'
aoqi@0 791 uint cnt = n->len(); // Input count
aoqi@0 792 uint local = 0;
aoqi@0 793 for( uint j=0; j<cnt; j++ ) {
aoqi@0 794 Node *m = n->in(j);
aoqi@0 795 if( m && get_block_for_node(m) == block && !m->is_top() )
aoqi@0 796 local++; // One more block-local input
aoqi@0 797 }
aoqi@0 798 ready_cnt.at_put(n->_idx, local); // Count em up
aoqi@0 799
aoqi@0 800 #ifdef ASSERT
aoqi@0 801 if( UseConcMarkSweepGC || UseG1GC ) {
aoqi@0 802 if( n->is_Mach() && n->as_Mach()->ideal_Opcode() == Op_StoreCM ) {
aoqi@0 803 // Check the precedence edges
aoqi@0 804 for (uint prec = n->req(); prec < n->len(); prec++) {
aoqi@0 805 Node* oop_store = n->in(prec);
aoqi@0 806 if (oop_store != NULL) {
aoqi@0 807 assert(get_block_for_node(oop_store)->_dom_depth <= block->_dom_depth, "oop_store must dominate card-mark");
aoqi@0 808 }
aoqi@0 809 }
aoqi@0 810 }
aoqi@0 811 }
aoqi@0 812 #endif
aoqi@0 813
aoqi@0 814 // A few node types require changing a required edge to a precedence edge
aoqi@0 815 // before allocation.
aoqi@0 816 if( n->is_Mach() && n->req() > TypeFunc::Parms &&
aoqi@0 817 (n->as_Mach()->ideal_Opcode() == Op_MemBarAcquire ||
aoqi@0 818 n->as_Mach()->ideal_Opcode() == Op_MemBarVolatile) ) {
aoqi@0 819 // MemBarAcquire could be created without Precedent edge.
aoqi@0 820 // del_req() replaces the specified edge with the last input edge
aoqi@0 821 // and then removes the last edge. If the specified edge > number of
aoqi@0 822 // edges the last edge will be moved outside of the input edges array
aoqi@0 823 // and the edge will be lost. This is why this code should be
aoqi@0 824 // executed only when Precedent (== TypeFunc::Parms) edge is present.
aoqi@0 825 Node *x = n->in(TypeFunc::Parms);
aoqi@0 826 n->del_req(TypeFunc::Parms);
aoqi@0 827 n->add_prec(x);
aoqi@0 828 }
aoqi@0 829 }
aoqi@0 830 }
aoqi@0 831 for(uint i2=i; i2< block->number_of_nodes(); i2++ ) // Trailing guys get zapped count
aoqi@0 832 ready_cnt.at_put(block->get_node(i2)->_idx, 0);
aoqi@0 833
aoqi@0 834 // All the prescheduled guys do not hold back internal nodes
aoqi@0 835 uint i3;
aoqi@0 836 for(i3 = 0; i3<phi_cnt; i3++ ) { // For all pre-scheduled
aoqi@0 837 Node *n = block->get_node(i3); // Get pre-scheduled
aoqi@0 838 for (DUIterator_Fast jmax, j = n->fast_outs(jmax); j < jmax; j++) {
aoqi@0 839 Node* m = n->fast_out(j);
aoqi@0 840 if (get_block_for_node(m) == block) { // Local-block user
aoqi@0 841 int m_cnt = ready_cnt.at(m->_idx)-1;
aoqi@0 842 ready_cnt.at_put(m->_idx, m_cnt); // Fix ready count
aoqi@0 843 }
aoqi@0 844 }
aoqi@0 845 }
aoqi@0 846
aoqi@0 847 Node_List delay;
aoqi@0 848 // Make a worklist
aoqi@0 849 Node_List worklist;
aoqi@0 850 for(uint i4=i3; i4<node_cnt; i4++ ) { // Put ready guys on worklist
aoqi@0 851 Node *m = block->get_node(i4);
aoqi@0 852 if( !ready_cnt.at(m->_idx) ) { // Zero ready count?
aoqi@0 853 if (m->is_iteratively_computed()) {
aoqi@0 854 // Push induction variable increments last to allow other uses
aoqi@0 855 // of the phi to be scheduled first. The select() method breaks
aoqi@0 856 // ties in scheduling by worklist order.
aoqi@0 857 delay.push(m);
aoqi@0 858 } else if (m->is_Mach() && m->as_Mach()->ideal_Opcode() == Op_CreateEx) {
aoqi@0 859 // Force the CreateEx to the top of the list so it's processed
aoqi@0 860 // first and ends up at the start of the block.
aoqi@0 861 worklist.insert(0, m);
aoqi@0 862 } else {
aoqi@0 863 worklist.push(m); // Then on to worklist!
aoqi@0 864 }
aoqi@0 865 }
aoqi@0 866 }
aoqi@0 867 while (delay.size()) {
aoqi@0 868 Node* d = delay.pop();
aoqi@0 869 worklist.push(d);
aoqi@0 870 }
aoqi@0 871
aoqi@0 872 // Warm up the 'next_call' heuristic bits
aoqi@0 873 needed_for_next_call(block, block->head(), next_call);
aoqi@0 874
aoqi@0 875 #ifndef PRODUCT
aoqi@0 876 if (trace_opto_pipelining()) {
aoqi@0 877 for (uint j=0; j< block->number_of_nodes(); j++) {
aoqi@0 878 Node *n = block->get_node(j);
aoqi@0 879 int idx = n->_idx;
aoqi@0 880 tty->print("# ready cnt:%3d ", ready_cnt.at(idx));
aoqi@0 881 tty->print("latency:%3d ", get_latency_for_node(n));
aoqi@0 882 tty->print("%4d: %s\n", idx, n->Name());
aoqi@0 883 }
aoqi@0 884 }
aoqi@0 885 #endif
aoqi@0 886
aoqi@0 887 uint max_idx = (uint)ready_cnt.length();
aoqi@0 888 // Pull from worklist and schedule
aoqi@0 889 while( worklist.size() ) { // Worklist is not ready
aoqi@0 890
aoqi@0 891 #ifndef PRODUCT
aoqi@0 892 if (trace_opto_pipelining()) {
aoqi@0 893 tty->print("# ready list:");
aoqi@0 894 for( uint i=0; i<worklist.size(); i++ ) { // Inspect entire worklist
aoqi@0 895 Node *n = worklist[i]; // Get Node on worklist
aoqi@0 896 tty->print(" %d", n->_idx);
aoqi@0 897 }
aoqi@0 898 tty->cr();
aoqi@0 899 }
aoqi@0 900 #endif
aoqi@0 901
aoqi@0 902 // Select and pop a ready guy from worklist
aoqi@0 903 Node* n = select(block, worklist, ready_cnt, next_call, phi_cnt);
aoqi@0 904 block->map_node(n, phi_cnt++); // Schedule him next
aoqi@0 905
aoqi@0 906 #ifndef PRODUCT
aoqi@0 907 if (trace_opto_pipelining()) {
aoqi@0 908 tty->print("# select %d: %s", n->_idx, n->Name());
aoqi@0 909 tty->print(", latency:%d", get_latency_for_node(n));
aoqi@0 910 n->dump();
aoqi@0 911 if (Verbose) {
aoqi@0 912 tty->print("# ready list:");
aoqi@0 913 for( uint i=0; i<worklist.size(); i++ ) { // Inspect entire worklist
aoqi@0 914 Node *n = worklist[i]; // Get Node on worklist
aoqi@0 915 tty->print(" %d", n->_idx);
aoqi@0 916 }
aoqi@0 917 tty->cr();
aoqi@0 918 }
aoqi@0 919 }
aoqi@0 920
aoqi@0 921 #endif
aoqi@0 922 if( n->is_MachCall() ) {
aoqi@0 923 MachCallNode *mcall = n->as_MachCall();
aoqi@0 924 phi_cnt = sched_call(block, phi_cnt, worklist, ready_cnt, mcall, next_call);
aoqi@0 925 continue;
aoqi@0 926 }
aoqi@0 927
aoqi@0 928 if (n->is_Mach() && n->as_Mach()->has_call()) {
aoqi@0 929 RegMask regs;
aoqi@0 930 regs.Insert(_matcher.c_frame_pointer());
aoqi@0 931 regs.OR(n->out_RegMask());
aoqi@0 932
aoqi@0 933 MachProjNode *proj = new (C) MachProjNode( n, 1, RegMask::Empty, MachProjNode::fat_proj );
aoqi@0 934 map_node_to_block(proj, block);
aoqi@0 935 block->insert_node(proj, phi_cnt++);
aoqi@0 936
aoqi@0 937 add_call_kills(proj, regs, _matcher._c_reg_save_policy, false);
aoqi@0 938 }
aoqi@0 939
aoqi@0 940 // Children are now all ready
aoqi@0 941 for (DUIterator_Fast i5max, i5 = n->fast_outs(i5max); i5 < i5max; i5++) {
aoqi@0 942 Node* m = n->fast_out(i5); // Get user
aoqi@0 943 if (get_block_for_node(m) != block) {
aoqi@0 944 continue;
aoqi@0 945 }
aoqi@0 946 if( m->is_Phi() ) continue;
aoqi@0 947 if (m->_idx >= max_idx) { // new node, skip it
aoqi@0 948 assert(m->is_MachProj() && n->is_Mach() && n->as_Mach()->has_call(), "unexpected node types");
aoqi@0 949 continue;
aoqi@0 950 }
aoqi@0 951 int m_cnt = ready_cnt.at(m->_idx)-1;
aoqi@0 952 ready_cnt.at_put(m->_idx, m_cnt);
aoqi@0 953 if( m_cnt == 0 )
aoqi@0 954 worklist.push(m);
aoqi@0 955 }
aoqi@0 956 }
aoqi@0 957
aoqi@0 958 if( phi_cnt != block->end_idx() ) {
aoqi@0 959 // did not schedule all. Retry, Bailout, or Die
aoqi@0 960 if (C->subsume_loads() == true && !C->failing()) {
aoqi@0 961 // Retry with subsume_loads == false
aoqi@0 962 // If this is the first failure, the sentinel string will "stick"
aoqi@0 963 // to the Compile object, and the C2Compiler will see it and retry.
aoqi@0 964 C->record_failure(C2Compiler::retry_no_subsuming_loads());
aoqi@0 965 }
aoqi@0 966 // assert( phi_cnt == end_idx(), "did not schedule all" );
aoqi@0 967 return false;
aoqi@0 968 }
aoqi@0 969
aoqi@0 970 #ifndef PRODUCT
aoqi@0 971 if (trace_opto_pipelining()) {
aoqi@0 972 tty->print_cr("#");
aoqi@0 973 tty->print_cr("# after schedule_local");
aoqi@0 974 for (uint i = 0;i < block->number_of_nodes();i++) {
aoqi@0 975 tty->print("# ");
aoqi@0 976 block->get_node(i)->fast_dump();
aoqi@0 977 }
aoqi@0 978 tty->cr();
aoqi@0 979 }
aoqi@0 980 #endif
aoqi@0 981
aoqi@0 982
aoqi@0 983 return true;
aoqi@0 984 }
aoqi@0 985
aoqi@0 986 //--------------------------catch_cleanup_fix_all_inputs-----------------------
aoqi@0 987 static void catch_cleanup_fix_all_inputs(Node *use, Node *old_def, Node *new_def) {
aoqi@0 988 for (uint l = 0; l < use->len(); l++) {
aoqi@0 989 if (use->in(l) == old_def) {
aoqi@0 990 if (l < use->req()) {
aoqi@0 991 use->set_req(l, new_def);
aoqi@0 992 } else {
aoqi@0 993 use->rm_prec(l);
aoqi@0 994 use->add_prec(new_def);
aoqi@0 995 l--;
aoqi@0 996 }
aoqi@0 997 }
aoqi@0 998 }
aoqi@0 999 }
aoqi@0 1000
aoqi@0 1001 //------------------------------catch_cleanup_find_cloned_def------------------
aoqi@0 1002 Node* PhaseCFG::catch_cleanup_find_cloned_def(Block *use_blk, Node *def, Block *def_blk, int n_clone_idx) {
aoqi@0 1003 assert( use_blk != def_blk, "Inter-block cleanup only");
aoqi@0 1004
aoqi@0 1005 // The use is some block below the Catch. Find and return the clone of the def
aoqi@0 1006 // that dominates the use. If there is no clone in a dominating block, then
aoqi@0 1007 // create a phi for the def in a dominating block.
aoqi@0 1008
aoqi@0 1009 // Find which successor block dominates this use. The successor
aoqi@0 1010 // blocks must all be single-entry (from the Catch only; I will have
aoqi@0 1011 // split blocks to make this so), hence they all dominate.
aoqi@0 1012 while( use_blk->_dom_depth > def_blk->_dom_depth+1 )
aoqi@0 1013 use_blk = use_blk->_idom;
aoqi@0 1014
aoqi@0 1015 // Find the successor
aoqi@0 1016 Node *fixup = NULL;
aoqi@0 1017
aoqi@0 1018 uint j;
aoqi@0 1019 for( j = 0; j < def_blk->_num_succs; j++ )
aoqi@0 1020 if( use_blk == def_blk->_succs[j] )
aoqi@0 1021 break;
aoqi@0 1022
aoqi@0 1023 if( j == def_blk->_num_succs ) {
aoqi@0 1024 // Block at same level in dom-tree is not a successor. It needs a
aoqi@0 1025 // PhiNode, the PhiNode uses from the def and IT's uses need fixup.
aoqi@0 1026 Node_Array inputs = new Node_List(Thread::current()->resource_area());
aoqi@0 1027 for(uint k = 1; k < use_blk->num_preds(); k++) {
aoqi@0 1028 Block* block = get_block_for_node(use_blk->pred(k));
aoqi@0 1029 inputs.map(k, catch_cleanup_find_cloned_def(block, def, def_blk, n_clone_idx));
aoqi@0 1030 }
aoqi@0 1031
aoqi@0 1032 // Check to see if the use_blk already has an identical phi inserted.
aoqi@0 1033 // If it exists, it will be at the first position since all uses of a
aoqi@0 1034 // def are processed together.
aoqi@0 1035 Node *phi = use_blk->get_node(1);
aoqi@0 1036 if( phi->is_Phi() ) {
aoqi@0 1037 fixup = phi;
aoqi@0 1038 for (uint k = 1; k < use_blk->num_preds(); k++) {
aoqi@0 1039 if (phi->in(k) != inputs[k]) {
aoqi@0 1040 // Not a match
aoqi@0 1041 fixup = NULL;
aoqi@0 1042 break;
aoqi@0 1043 }
aoqi@0 1044 }
aoqi@0 1045 }
aoqi@0 1046
aoqi@0 1047 // If an existing PhiNode was not found, make a new one.
aoqi@0 1048 if (fixup == NULL) {
aoqi@0 1049 Node *new_phi = PhiNode::make(use_blk->head(), def);
aoqi@0 1050 use_blk->insert_node(new_phi, 1);
aoqi@0 1051 map_node_to_block(new_phi, use_blk);
aoqi@0 1052 for (uint k = 1; k < use_blk->num_preds(); k++) {
aoqi@0 1053 new_phi->set_req(k, inputs[k]);
aoqi@0 1054 }
aoqi@0 1055 fixup = new_phi;
aoqi@0 1056 }
aoqi@0 1057
aoqi@0 1058 } else {
aoqi@0 1059 // Found the use just below the Catch. Make it use the clone.
aoqi@0 1060 fixup = use_blk->get_node(n_clone_idx);
aoqi@0 1061 }
aoqi@0 1062
aoqi@0 1063 return fixup;
aoqi@0 1064 }
aoqi@0 1065
aoqi@0 1066 //--------------------------catch_cleanup_intra_block--------------------------
aoqi@0 1067 // Fix all input edges in use that reference "def". The use is in the same
aoqi@0 1068 // block as the def and both have been cloned in each successor block.
aoqi@0 1069 static void catch_cleanup_intra_block(Node *use, Node *def, Block *blk, int beg, int n_clone_idx) {
aoqi@0 1070
aoqi@0 1071 // Both the use and def have been cloned. For each successor block,
aoqi@0 1072 // get the clone of the use, and make its input the clone of the def
aoqi@0 1073 // found in that block.
aoqi@0 1074
aoqi@0 1075 uint use_idx = blk->find_node(use);
aoqi@0 1076 uint offset_idx = use_idx - beg;
aoqi@0 1077 for( uint k = 0; k < blk->_num_succs; k++ ) {
aoqi@0 1078 // Get clone in each successor block
aoqi@0 1079 Block *sb = blk->_succs[k];
aoqi@0 1080 Node *clone = sb->get_node(offset_idx+1);
aoqi@0 1081 assert( clone->Opcode() == use->Opcode(), "" );
aoqi@0 1082
aoqi@0 1083 // Make use-clone reference the def-clone
aoqi@0 1084 catch_cleanup_fix_all_inputs(clone, def, sb->get_node(n_clone_idx));
aoqi@0 1085 }
aoqi@0 1086 }
aoqi@0 1087
aoqi@0 1088 //------------------------------catch_cleanup_inter_block---------------------
aoqi@0 1089 // Fix all input edges in use that reference "def". The use is in a different
aoqi@0 1090 // block than the def.
aoqi@0 1091 void PhaseCFG::catch_cleanup_inter_block(Node *use, Block *use_blk, Node *def, Block *def_blk, int n_clone_idx) {
aoqi@0 1092 if( !use_blk ) return; // Can happen if the use is a precedence edge
aoqi@0 1093
aoqi@0 1094 Node *new_def = catch_cleanup_find_cloned_def(use_blk, def, def_blk, n_clone_idx);
aoqi@0 1095 catch_cleanup_fix_all_inputs(use, def, new_def);
aoqi@0 1096 }
aoqi@0 1097
aoqi@0 1098 //------------------------------call_catch_cleanup-----------------------------
aoqi@0 1099 // If we inserted any instructions between a Call and his CatchNode,
aoqi@0 1100 // clone the instructions on all paths below the Catch.
aoqi@0 1101 void PhaseCFG::call_catch_cleanup(Block* block) {
aoqi@0 1102
aoqi@0 1103 // End of region to clone
aoqi@0 1104 uint end = block->end_idx();
aoqi@0 1105 if( !block->get_node(end)->is_Catch() ) return;
aoqi@0 1106 // Start of region to clone
aoqi@0 1107 uint beg = end;
aoqi@0 1108 while(!block->get_node(beg-1)->is_MachProj() ||
aoqi@0 1109 !block->get_node(beg-1)->in(0)->is_MachCall() ) {
aoqi@0 1110 beg--;
aoqi@0 1111 assert(beg > 0,"Catch cleanup walking beyond block boundary");
aoqi@0 1112 }
aoqi@0 1113 // Range of inserted instructions is [beg, end)
aoqi@0 1114 if( beg == end ) return;
aoqi@0 1115
aoqi@0 1116 // Clone along all Catch output paths. Clone area between the 'beg' and
aoqi@0 1117 // 'end' indices.
aoqi@0 1118 for( uint i = 0; i < block->_num_succs; i++ ) {
aoqi@0 1119 Block *sb = block->_succs[i];
aoqi@0 1120 // Clone the entire area; ignoring the edge fixup for now.
aoqi@0 1121 for( uint j = end; j > beg; j-- ) {
aoqi@0 1122 Node *clone = block->get_node(j-1)->clone();
aoqi@0 1123 sb->insert_node(clone, 1);
aoqi@0 1124 map_node_to_block(clone, sb);
aph@8614 1125 if (clone->needs_anti_dependence_check()) {
aph@8614 1126 insert_anti_dependences(sb, clone);
aph@8614 1127 }
aoqi@0 1128 }
aoqi@0 1129 }
aoqi@0 1130
aoqi@0 1131
aoqi@0 1132 // Fixup edges. Check the def-use info per cloned Node
aoqi@0 1133 for(uint i2 = beg; i2 < end; i2++ ) {
aoqi@0 1134 uint n_clone_idx = i2-beg+1; // Index of clone of n in each successor block
aoqi@0 1135 Node *n = block->get_node(i2); // Node that got cloned
aoqi@0 1136 // Need DU safe iterator because of edge manipulation in calls.
aoqi@0 1137 Unique_Node_List *out = new Unique_Node_List(Thread::current()->resource_area());
aoqi@0 1138 for (DUIterator_Fast j1max, j1 = n->fast_outs(j1max); j1 < j1max; j1++) {
aoqi@0 1139 out->push(n->fast_out(j1));
aoqi@0 1140 }
aoqi@0 1141 uint max = out->size();
aoqi@0 1142 for (uint j = 0; j < max; j++) {// For all users
aoqi@0 1143 Node *use = out->pop();
aoqi@0 1144 Block *buse = get_block_for_node(use);
aoqi@0 1145 if( use->is_Phi() ) {
aoqi@0 1146 for( uint k = 1; k < use->req(); k++ )
aoqi@0 1147 if( use->in(k) == n ) {
aoqi@0 1148 Block* b = get_block_for_node(buse->pred(k));
aoqi@0 1149 Node *fixup = catch_cleanup_find_cloned_def(b, n, block, n_clone_idx);
aoqi@0 1150 use->set_req(k, fixup);
aoqi@0 1151 }
aoqi@0 1152 } else {
aoqi@0 1153 if (block == buse) {
aoqi@0 1154 catch_cleanup_intra_block(use, n, block, beg, n_clone_idx);
aoqi@0 1155 } else {
aoqi@0 1156 catch_cleanup_inter_block(use, buse, n, block, n_clone_idx);
aoqi@0 1157 }
aoqi@0 1158 }
aoqi@0 1159 } // End for all users
aoqi@0 1160
aoqi@0 1161 } // End of for all Nodes in cloned area
aoqi@0 1162
aoqi@0 1163 // Remove the now-dead cloned ops
aoqi@0 1164 for(uint i3 = beg; i3 < end; i3++ ) {
aoqi@0 1165 block->get_node(beg)->disconnect_inputs(NULL, C);
aoqi@0 1166 block->remove_node(beg);
aoqi@0 1167 }
aoqi@0 1168
aoqi@0 1169 // If the successor blocks have a CreateEx node, move it back to the top
aoqi@0 1170 for(uint i4 = 0; i4 < block->_num_succs; i4++ ) {
aoqi@0 1171 Block *sb = block->_succs[i4];
aoqi@0 1172 uint new_cnt = end - beg;
aoqi@0 1173 // Remove any newly created, but dead, nodes.
aoqi@0 1174 for( uint j = new_cnt; j > 0; j-- ) {
aoqi@0 1175 Node *n = sb->get_node(j);
aoqi@0 1176 if (n->outcnt() == 0 &&
aoqi@0 1177 (!n->is_Proj() || n->as_Proj()->in(0)->outcnt() == 1) ){
aoqi@0 1178 n->disconnect_inputs(NULL, C);
aoqi@0 1179 sb->remove_node(j);
aoqi@0 1180 new_cnt--;
aoqi@0 1181 }
aoqi@0 1182 }
aoqi@0 1183 // If any newly created nodes remain, move the CreateEx node to the top
aoqi@0 1184 if (new_cnt > 0) {
aoqi@0 1185 Node *cex = sb->get_node(1+new_cnt);
aoqi@0 1186 if( cex->is_Mach() && cex->as_Mach()->ideal_Opcode() == Op_CreateEx ) {
aoqi@0 1187 sb->remove_node(1+new_cnt);
aoqi@0 1188 sb->insert_node(cex, 1);
aoqi@0 1189 }
aoqi@0 1190 }
aoqi@0 1191 }
aoqi@0 1192 }

mercurial