1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "compiler/compileLog.hpp" 27 #include "interpreter/linkResolver.hpp" 28 #include "oops/method.hpp" 29 #include "opto/addnode.hpp" 30 #include "opto/castnode.hpp" 31 #include "opto/idealGraphPrinter.hpp" 32 #include "opto/locknode.hpp" 33 #include "opto/memnode.hpp" 34 #include "opto/opaquenode.hpp" 35 #include "opto/parse.hpp" 36 #include "opto/rootnode.hpp" 37 #include "opto/runtime.hpp" 38 #include "runtime/arguments.hpp" 39 #include "runtime/handles.inline.hpp" 40 #include "runtime/sharedRuntime.hpp" 41 #include "utilities/copy.hpp" 42 43 // Static array so we can figure out which bytecodes stop us from compiling 44 // the most. Some of the non-static variables are needed in bytecodeInfo.cpp 45 // and eventually should be encapsulated in a proper class (gri 8/18/98). 46 47 int nodes_created = 0; 48 int methods_parsed = 0; 49 int methods_seen = 0; 50 int blocks_parsed = 0; 51 int blocks_seen = 0; 52 53 int explicit_null_checks_inserted = 0; 54 int explicit_null_checks_elided = 0; 55 int all_null_checks_found = 0, implicit_null_checks = 0; 56 int implicit_null_throws = 0; 57 58 int reclaim_idx = 0; 59 int reclaim_in = 0; 60 int reclaim_node = 0; 61 62 #ifndef PRODUCT 63 bool Parse::BytecodeParseHistogram::_initialized = false; 64 uint Parse::BytecodeParseHistogram::_bytecodes_parsed [Bytecodes::number_of_codes]; 65 uint Parse::BytecodeParseHistogram::_nodes_constructed[Bytecodes::number_of_codes]; 66 uint Parse::BytecodeParseHistogram::_nodes_transformed[Bytecodes::number_of_codes]; 67 uint Parse::BytecodeParseHistogram::_new_values [Bytecodes::number_of_codes]; 68 #endif 69 70 //------------------------------print_statistics------------------------------- 71 #ifndef PRODUCT 72 void Parse::print_statistics() { 73 tty->print_cr("--- Compiler Statistics ---"); 74 tty->print("Methods seen: %d Methods parsed: %d", methods_seen, methods_parsed); 75 tty->print(" Nodes created: %d", nodes_created); 76 tty->cr(); 77 if (methods_seen != methods_parsed) 78 tty->print_cr("Reasons for parse failures (NOT cumulative):"); 79 tty->print_cr("Blocks parsed: %d Blocks seen: %d", blocks_parsed, blocks_seen); 80 81 if( explicit_null_checks_inserted ) 82 tty->print_cr("%d original NULL checks - %d elided (%2d%%); optimizer leaves %d,", explicit_null_checks_inserted, explicit_null_checks_elided, (100*explicit_null_checks_elided)/explicit_null_checks_inserted, all_null_checks_found); 83 if( all_null_checks_found ) 84 tty->print_cr("%d made implicit (%2d%%)", implicit_null_checks, 85 (100*implicit_null_checks)/all_null_checks_found); 86 if( implicit_null_throws ) 87 tty->print_cr("%d implicit null exceptions at runtime", 88 implicit_null_throws); 89 90 if( PrintParseStatistics && BytecodeParseHistogram::initialized() ) { 91 BytecodeParseHistogram::print(); 92 } 93 } 94 #endif 95 96 //------------------------------ON STACK REPLACEMENT--------------------------- 97 98 // Construct a node which can be used to get incoming state for 99 // on stack replacement. 100 Node *Parse::fetch_interpreter_state(int index, 101 BasicType bt, 102 Node *local_addrs, 103 Node *local_addrs_base) { 104 Node *mem = memory(Compile::AliasIdxRaw); 105 Node *adr = basic_plus_adr( local_addrs_base, local_addrs, -index*wordSize ); 106 Node *ctl = control(); 107 108 // Very similar to LoadNode::make, except we handle un-aligned longs and 109 // doubles on Sparc. Intel can handle them just fine directly. 110 Node *l; 111 switch (bt) { // Signature is flattened 112 case T_INT: l = new (C) LoadINode(ctl, mem, adr, TypeRawPtr::BOTTOM, TypeInt::INT, MemNode::unordered); break; 113 case T_FLOAT: l = new (C) LoadFNode(ctl, mem, adr, TypeRawPtr::BOTTOM, Type::FLOAT, MemNode::unordered); break; 114 case T_ADDRESS: l = new (C) LoadPNode(ctl, mem, adr, TypeRawPtr::BOTTOM, TypeRawPtr::BOTTOM, MemNode::unordered); break; 115 case T_OBJECT: l = new (C) LoadPNode(ctl, mem, adr, TypeRawPtr::BOTTOM, TypeInstPtr::BOTTOM, MemNode::unordered); break; 116 case T_LONG: 117 case T_DOUBLE: { 118 // Since arguments are in reverse order, the argument address 'adr' 119 // refers to the back half of the long/double. Recompute adr. 120 adr = basic_plus_adr(local_addrs_base, local_addrs, -(index+1)*wordSize); 121 if (Matcher::misaligned_doubles_ok) { 122 l = (bt == T_DOUBLE) 123 ? (Node*)new (C) LoadDNode(ctl, mem, adr, TypeRawPtr::BOTTOM, Type::DOUBLE, MemNode::unordered) 124 : (Node*)new (C) LoadLNode(ctl, mem, adr, TypeRawPtr::BOTTOM, TypeLong::LONG, MemNode::unordered); 125 } else { 126 l = (bt == T_DOUBLE) 127 ? (Node*)new (C) LoadD_unalignedNode(ctl, mem, adr, TypeRawPtr::BOTTOM, MemNode::unordered) 128 : (Node*)new (C) LoadL_unalignedNode(ctl, mem, adr, TypeRawPtr::BOTTOM, MemNode::unordered); 129 } 130 break; 131 } 132 default: ShouldNotReachHere(); 133 } 134 return _gvn.transform(l); 135 } 136 137 // Helper routine to prevent the interpreter from handing 138 // unexpected typestate to an OSR method. 139 // The Node l is a value newly dug out of the interpreter frame. 140 // The type is the type predicted by ciTypeFlow. Note that it is 141 // not a general type, but can only come from Type::get_typeflow_type. 142 // The safepoint is a map which will feed an uncommon trap. 143 Node* Parse::check_interpreter_type(Node* l, const Type* type, 144 SafePointNode* &bad_type_exit) { 145 146 const TypeOopPtr* tp = type->isa_oopptr(); 147 148 // TypeFlow may assert null-ness if a type appears unloaded. 149 if (type == TypePtr::NULL_PTR || 150 (tp != NULL && !tp->klass()->is_loaded())) { 151 // Value must be null, not a real oop. 152 Node* chk = _gvn.transform( new (C) CmpPNode(l, null()) ); 153 Node* tst = _gvn.transform( new (C) BoolNode(chk, BoolTest::eq) ); 154 IfNode* iff = create_and_map_if(control(), tst, PROB_MAX, COUNT_UNKNOWN); 155 set_control(_gvn.transform( new (C) IfTrueNode(iff) )); 156 Node* bad_type = _gvn.transform( new (C) IfFalseNode(iff) ); 157 bad_type_exit->control()->add_req(bad_type); 158 l = null(); 159 } 160 161 // Typeflow can also cut off paths from the CFG, based on 162 // types which appear unloaded, or call sites which appear unlinked. 163 // When paths are cut off, values at later merge points can rise 164 // toward more specific classes. Make sure these specific classes 165 // are still in effect. 166 if (tp != NULL && tp->klass() != C->env()->Object_klass()) { 167 // TypeFlow asserted a specific object type. Value must have that type. 168 Node* bad_type_ctrl = NULL; 169 l = gen_checkcast(l, makecon(TypeKlassPtr::make(tp->klass())), &bad_type_ctrl); 170 bad_type_exit->control()->add_req(bad_type_ctrl); 171 } 172 173 BasicType bt_l = _gvn.type(l)->basic_type(); 174 BasicType bt_t = type->basic_type(); 175 assert(_gvn.type(l)->higher_equal(type), "must constrain OSR typestate"); 176 return l; 177 } 178 179 // Helper routine which sets up elements of the initial parser map when 180 // performing a parse for on stack replacement. Add values into map. 181 // The only parameter contains the address of a interpreter arguments. 182 void Parse::load_interpreter_state(Node* osr_buf) { 183 int index; 184 int max_locals = jvms()->loc_size(); 185 int max_stack = jvms()->stk_size(); 186 187 188 // Mismatch between method and jvms can occur since map briefly held 189 // an OSR entry state (which takes up one RawPtr word). 190 assert(max_locals == method()->max_locals(), "sanity"); 191 assert(max_stack >= method()->max_stack(), "sanity"); 192 assert((int)jvms()->endoff() == TypeFunc::Parms + max_locals + max_stack, "sanity"); 193 assert((int)jvms()->endoff() == (int)map()->req(), "sanity"); 194 195 // Find the start block. 196 Block* osr_block = start_block(); 197 assert(osr_block->start() == osr_bci(), "sanity"); 198 199 // Set initial BCI. 200 set_parse_bci(osr_block->start()); 201 202 // Set initial stack depth. 203 set_sp(osr_block->start_sp()); 204 205 // Check bailouts. We currently do not perform on stack replacement 206 // of loops in catch blocks or loops which branch with a non-empty stack. 207 if (sp() != 0) { 208 C->record_method_not_compilable("OSR starts with non-empty stack"); 209 return; 210 } 211 // Do not OSR inside finally clauses: 212 if (osr_block->has_trap_at(osr_block->start())) { 213 C->record_method_not_compilable("OSR starts with an immediate trap"); 214 return; 215 } 216 217 // Commute monitors from interpreter frame to compiler frame. 218 assert(jvms()->monitor_depth() == 0, "should be no active locks at beginning of osr"); 219 int mcnt = osr_block->flow()->monitor_count(); 220 Node *monitors_addr = basic_plus_adr(osr_buf, osr_buf, (max_locals+mcnt*2-1)*wordSize); 221 for (index = 0; index < mcnt; index++) { 222 // Make a BoxLockNode for the monitor. 223 Node *box = _gvn.transform(new (C) BoxLockNode(next_monitor())); 224 225 226 // Displaced headers and locked objects are interleaved in the 227 // temp OSR buffer. We only copy the locked objects out here. 228 // Fetch the locked object from the OSR temp buffer and copy to our fastlock node. 229 Node *lock_object = fetch_interpreter_state(index*2, T_OBJECT, monitors_addr, osr_buf); 230 // Try and copy the displaced header to the BoxNode 231 Node *displaced_hdr = fetch_interpreter_state((index*2) + 1, T_ADDRESS, monitors_addr, osr_buf); 232 233 234 store_to_memory(control(), box, displaced_hdr, T_ADDRESS, Compile::AliasIdxRaw, MemNode::unordered); 235 236 // Build a bogus FastLockNode (no code will be generated) and push the 237 // monitor into our debug info. 238 const FastLockNode *flock = _gvn.transform(new (C) FastLockNode( 0, lock_object, box ))->as_FastLock(); 239 map()->push_monitor(flock); 240 241 // If the lock is our method synchronization lock, tuck it away in 242 // _sync_lock for return and rethrow exit paths. 243 if (index == 0 && method()->is_synchronized()) { 244 _synch_lock = flock; 245 } 246 } 247 248 // Use the raw liveness computation to make sure that unexpected 249 // values don't propagate into the OSR frame. 250 MethodLivenessResult live_locals = method()->liveness_at_bci(osr_bci()); 251 if (!live_locals.is_valid()) { 252 // Degenerate or breakpointed method. 253 C->record_method_not_compilable("OSR in empty or breakpointed method"); 254 return; 255 } 256 257 // Extract the needed locals from the interpreter frame. 258 Node *locals_addr = basic_plus_adr(osr_buf, osr_buf, (max_locals-1)*wordSize); 259 260 // find all the locals that the interpreter thinks contain live oops 261 const BitMap live_oops = method()->live_local_oops_at_bci(osr_bci()); 262 for (index = 0; index < max_locals; index++) { 263 264 if (!live_locals.at(index)) { 265 continue; 266 } 267 268 const Type *type = osr_block->local_type_at(index); 269 270 if (type->isa_oopptr() != NULL) { 271 272 // 6403625: Verify that the interpreter oopMap thinks that the oop is live 273 // else we might load a stale oop if the MethodLiveness disagrees with the 274 // result of the interpreter. If the interpreter says it is dead we agree 275 // by making the value go to top. 276 // 277 278 if (!live_oops.at(index)) { 279 if (C->log() != NULL) { 280 C->log()->elem("OSR_mismatch local_index='%d'",index); 281 } 282 set_local(index, null()); 283 // and ignore it for the loads 284 continue; 285 } 286 } 287 288 // Filter out TOP, HALF, and BOTTOM. (Cf. ensure_phi.) 289 if (type == Type::TOP || type == Type::HALF) { 290 continue; 291 } 292 // If the type falls to bottom, then this must be a local that 293 // is mixing ints and oops or some such. Forcing it to top 294 // makes it go dead. 295 if (type == Type::BOTTOM) { 296 continue; 297 } 298 // Construct code to access the appropriate local. 299 BasicType bt = type->basic_type(); 300 if (type == TypePtr::NULL_PTR) { 301 // Ptr types are mixed together with T_ADDRESS but NULL is 302 // really for T_OBJECT types so correct it. 303 bt = T_OBJECT; 304 } 305 Node *value = fetch_interpreter_state(index, bt, locals_addr, osr_buf); 306 set_local(index, value); 307 } 308 309 // Extract the needed stack entries from the interpreter frame. 310 for (index = 0; index < sp(); index++) { 311 const Type *type = osr_block->stack_type_at(index); 312 if (type != Type::TOP) { 313 // Currently the compiler bails out when attempting to on stack replace 314 // at a bci with a non-empty stack. We should not reach here. 315 ShouldNotReachHere(); 316 } 317 } 318 319 // End the OSR migration 320 make_runtime_call(RC_LEAF, OptoRuntime::osr_end_Type(), 321 CAST_FROM_FN_PTR(address, SharedRuntime::OSR_migration_end), 322 "OSR_migration_end", TypeRawPtr::BOTTOM, 323 osr_buf); 324 325 // Now that the interpreter state is loaded, make sure it will match 326 // at execution time what the compiler is expecting now: 327 SafePointNode* bad_type_exit = clone_map(); 328 bad_type_exit->set_control(new (C) RegionNode(1)); 329 330 assert(osr_block->flow()->jsrs()->size() == 0, "should be no jsrs live at osr point"); 331 for (index = 0; index < max_locals; index++) { 332 if (stopped()) break; 333 Node* l = local(index); 334 if (l->is_top()) continue; // nothing here 335 const Type *type = osr_block->local_type_at(index); 336 if (type->isa_oopptr() != NULL) { 337 if (!live_oops.at(index)) { 338 // skip type check for dead oops 339 continue; 340 } 341 } 342 if (osr_block->flow()->local_type_at(index)->is_return_address()) { 343 // In our current system it's illegal for jsr addresses to be 344 // live into an OSR entry point because the compiler performs 345 // inlining of jsrs. ciTypeFlow has a bailout that detect this 346 // case and aborts the compile if addresses are live into an OSR 347 // entry point. Because of that we can assume that any address 348 // locals at the OSR entry point are dead. Method liveness 349 // isn't precise enought to figure out that they are dead in all 350 // cases so simply skip checking address locals all 351 // together. Any type check is guaranteed to fail since the 352 // interpreter type is the result of a load which might have any 353 // value and the expected type is a constant. 354 continue; 355 } 356 set_local(index, check_interpreter_type(l, type, bad_type_exit)); 357 } 358 359 for (index = 0; index < sp(); index++) { 360 if (stopped()) break; 361 Node* l = stack(index); 362 if (l->is_top()) continue; // nothing here 363 const Type *type = osr_block->stack_type_at(index); 364 set_stack(index, check_interpreter_type(l, type, bad_type_exit)); 365 } 366 367 if (bad_type_exit->control()->req() > 1) { 368 // Build an uncommon trap here, if any inputs can be unexpected. 369 bad_type_exit->set_control(_gvn.transform( bad_type_exit->control() )); 370 record_for_igvn(bad_type_exit->control()); 371 SafePointNode* types_are_good = map(); 372 set_map(bad_type_exit); 373 // The unexpected type happens because a new edge is active 374 // in the CFG, which typeflow had previously ignored. 375 // E.g., Object x = coldAtFirst() && notReached()? "str": new Integer(123). 376 // This x will be typed as Integer if notReached is not yet linked. 377 // It could also happen due to a problem in ciTypeFlow analysis. 378 uncommon_trap(Deoptimization::Reason_constraint, 379 Deoptimization::Action_reinterpret); 380 set_map(types_are_good); 381 } 382 } 383 384 //------------------------------Parse------------------------------------------ 385 // Main parser constructor. 386 Parse::Parse(JVMState* caller, ciMethod* parse_method, float expected_uses, Parse* parent) 387 : _exits(caller), _parent(parent) 388 { 389 // Init some variables 390 _caller = caller; 391 _method = parse_method; 392 _expected_uses = expected_uses; 393 _depth = 1 + (caller->has_method() ? caller->depth() : 0); 394 _wrote_final = false; 395 _wrote_volatile = false; 396 _wrote_stable = false; 397 _wrote_fields = false; 398 _alloc_with_final = NULL; 399 _entry_bci = InvocationEntryBci; 400 _tf = NULL; 401 _block = NULL; 402 debug_only(_block_count = -1); 403 debug_only(_blocks = (Block*)-1); 404 #ifndef PRODUCT 405 if (PrintCompilation || PrintOpto) { 406 // Make sure I have an inline tree, so I can print messages about it. 407 JVMState* ilt_caller = is_osr_parse() ? caller->caller() : caller; 408 InlineTree::find_subtree_from_root(C->ilt(), ilt_caller, parse_method); 409 } 410 _max_switch_depth = 0; 411 _est_switch_depth = 0; 412 #endif 413 414 _tf = TypeFunc::make(method()); 415 _iter.reset_to_method(method()); 416 _flow = method()->get_flow_analysis(); 417 if (_flow->failing()) { 418 C->record_method_not_compilable_all_tiers(_flow->failure_reason()); 419 } 420 421 #ifndef PRODUCT 422 if (_flow->has_irreducible_entry()) { 423 C->set_parsed_irreducible_loop(true); 424 } 425 #endif 426 427 if (_expected_uses <= 0) { 428 _prof_factor = 1; 429 } else { 430 float prof_total = parse_method->interpreter_invocation_count(); 431 if (prof_total <= _expected_uses) { 432 _prof_factor = 1; 433 } else { 434 _prof_factor = _expected_uses / prof_total; 435 } 436 } 437 438 CompileLog* log = C->log(); 439 if (log != NULL) { 440 log->begin_head("parse method='%d' uses='%g'", 441 log->identify(parse_method), expected_uses); 442 if (depth() == 1 && C->is_osr_compilation()) { 443 log->print(" osr_bci='%d'", C->entry_bci()); 444 } 445 log->stamp(); 446 log->end_head(); 447 } 448 449 // Accumulate deoptimization counts. 450 // (The range_check and store_check counts are checked elsewhere.) 451 ciMethodData* md = method()->method_data(); 452 for (uint reason = 0; reason < md->trap_reason_limit(); reason++) { 453 uint md_count = md->trap_count(reason); 454 if (md_count != 0) { 455 if (md_count == md->trap_count_limit()) 456 md_count += md->overflow_trap_count(); 457 uint total_count = C->trap_count(reason); 458 uint old_count = total_count; 459 total_count += md_count; 460 // Saturate the add if it overflows. 461 if (total_count < old_count || total_count < md_count) 462 total_count = (uint)-1; 463 C->set_trap_count(reason, total_count); 464 if (log != NULL) 465 log->elem("observe trap='%s' count='%d' total='%d'", 466 Deoptimization::trap_reason_name(reason), 467 md_count, total_count); 468 } 469 } 470 // Accumulate total sum of decompilations, also. 471 C->set_decompile_count(C->decompile_count() + md->decompile_count()); 472 473 _count_invocations = C->do_count_invocations(); 474 _method_data_update = C->do_method_data_update(); 475 476 if (log != NULL && method()->has_exception_handlers()) { 477 log->elem("observe that='has_exception_handlers'"); 478 } 479 480 assert(method()->can_be_compiled(), "Can not parse this method, cutout earlier"); 481 assert(method()->has_balanced_monitors(), "Can not parse unbalanced monitors, cutout earlier"); 482 483 // Always register dependence if JVMTI is enabled, because 484 // either breakpoint setting or hotswapping of methods may 485 // cause deoptimization. 486 if (C->env()->jvmti_can_hotswap_or_post_breakpoint()) { 487 C->dependencies()->assert_evol_method(method()); 488 } 489 490 methods_seen++; 491 492 // Do some special top-level things. 493 if (depth() == 1 && C->is_osr_compilation()) { 494 _entry_bci = C->entry_bci(); 495 _flow = method()->get_osr_flow_analysis(osr_bci()); 496 if (_flow->failing()) { 497 C->record_method_not_compilable(_flow->failure_reason()); 498 #ifndef PRODUCT 499 if (PrintOpto && (Verbose || WizardMode)) { 500 tty->print_cr("OSR @%d type flow bailout: %s", _entry_bci, _flow->failure_reason()); 501 if (Verbose) { 502 method()->print(); 503 method()->print_codes(); 504 _flow->print(); 505 } 506 } 507 #endif 508 } 509 _tf = C->tf(); // the OSR entry type is different 510 } 511 512 #ifdef ASSERT 513 if (depth() == 1) { 514 assert(C->is_osr_compilation() == this->is_osr_parse(), "OSR in sync"); 515 if (C->tf() != tf()) { 516 MutexLockerEx ml(Compile_lock, Mutex::_no_safepoint_check_flag); 517 assert(C->env()->system_dictionary_modification_counter_changed(), 518 "Must invalidate if TypeFuncs differ"); 519 } 520 } else { 521 assert(!this->is_osr_parse(), "no recursive OSR"); 522 } 523 #endif 524 525 methods_parsed++; 526 #ifndef PRODUCT 527 // add method size here to guarantee that inlined methods are added too 528 if (TimeCompiler) 529 _total_bytes_compiled += method()->code_size(); 530 531 show_parse_info(); 532 #endif 533 534 if (failing()) { 535 if (log) log->done("parse"); 536 return; 537 } 538 539 gvn().set_type(root(), root()->bottom_type()); 540 gvn().transform(top()); 541 542 // Import the results of the ciTypeFlow. 543 init_blocks(); 544 545 // Merge point for all normal exits 546 build_exits(); 547 548 // Setup the initial JVM state map. 549 SafePointNode* entry_map = create_entry_map(); 550 551 // Check for bailouts during map initialization 552 if (failing() || entry_map == NULL) { 553 if (log) log->done("parse"); 554 return; 555 } 556 557 Node_Notes* caller_nn = C->default_node_notes(); 558 // Collect debug info for inlined calls unless -XX:-DebugInlinedCalls. 559 if (DebugInlinedCalls || depth() == 1) { 560 C->set_default_node_notes(make_node_notes(caller_nn)); 561 } 562 563 if (is_osr_parse()) { 564 Node* osr_buf = entry_map->in(TypeFunc::Parms+0); 565 entry_map->set_req(TypeFunc::Parms+0, top()); 566 set_map(entry_map); 567 load_interpreter_state(osr_buf); 568 } else { 569 set_map(entry_map); 570 do_method_entry(); 571 } 572 if (depth() == 1) { 573 // Add check to deoptimize the nmethod if RTM state was changed 574 rtm_deopt(); 575 } 576 577 // Check for bailouts during method entry. 578 if (failing()) { 579 if (log) log->done("parse"); 580 C->set_default_node_notes(caller_nn); 581 return; 582 } 583 584 entry_map = map(); // capture any changes performed by method setup code 585 assert(jvms()->endoff() == map()->req(), "map matches JVMS layout"); 586 587 // We begin parsing as if we have just encountered a jump to the 588 // method entry. 589 Block* entry_block = start_block(); 590 assert(entry_block->start() == (is_osr_parse() ? osr_bci() : 0), ""); 591 set_map_clone(entry_map); 592 merge_common(entry_block, entry_block->next_path_num()); 593 594 #ifndef PRODUCT 595 BytecodeParseHistogram *parse_histogram_obj = new (C->env()->arena()) BytecodeParseHistogram(this, C); 596 set_parse_histogram( parse_histogram_obj ); 597 #endif 598 599 // Parse all the basic blocks. 600 do_all_blocks(); 601 602 C->set_default_node_notes(caller_nn); 603 604 // Check for bailouts during conversion to graph 605 if (failing()) { 606 if (log) log->done("parse"); 607 return; 608 } 609 610 // Fix up all exiting control flow. 611 set_map(entry_map); 612 do_exits(); 613 614 if (log) log->done("parse nodes='%d' live='%d' memory='%d'", 615 C->unique(), C->live_nodes(), C->node_arena()->used()); 616 } 617 618 //---------------------------do_all_blocks------------------------------------- 619 void Parse::do_all_blocks() { 620 bool has_irreducible = flow()->has_irreducible_entry(); 621 622 // Walk over all blocks in Reverse Post-Order. 623 while (true) { 624 bool progress = false; 625 for (int rpo = 0; rpo < block_count(); rpo++) { 626 Block* block = rpo_at(rpo); 627 628 if (block->is_parsed()) continue; 629 630 if (!block->is_merged()) { 631 // Dead block, no state reaches this block 632 continue; 633 } 634 635 // Prepare to parse this block. 636 load_state_from(block); 637 638 if (stopped()) { 639 // Block is dead. 640 continue; 641 } 642 643 blocks_parsed++; 644 645 progress = true; 646 if (block->is_loop_head() || block->is_handler() || has_irreducible && !block->is_ready()) { 647 // Not all preds have been parsed. We must build phis everywhere. 648 // (Note that dead locals do not get phis built, ever.) 649 ensure_phis_everywhere(); 650 651 if (block->is_SEL_head() && 652 (UseLoopPredicate || LoopLimitCheck)) { 653 // Add predicate to single entry (not irreducible) loop head. 654 assert(!block->has_merged_backedge(), "only entry paths should be merged for now"); 655 // Need correct bci for predicate. 656 // It is fine to set it here since do_one_block() will set it anyway. 657 set_parse_bci(block->start()); 658 add_predicate(); 659 // Add new region for back branches. 660 int edges = block->pred_count() - block->preds_parsed() + 1; // +1 for original region 661 RegionNode *r = new (C) RegionNode(edges+1); 662 _gvn.set_type(r, Type::CONTROL); 663 record_for_igvn(r); 664 r->init_req(edges, control()); 665 set_control(r); 666 // Add new phis. 667 ensure_phis_everywhere(); 668 } 669 670 // Leave behind an undisturbed copy of the map, for future merges. 671 set_map(clone_map()); 672 } 673 674 if (control()->is_Region() && !block->is_loop_head() && !has_irreducible && !block->is_handler()) { 675 // In the absence of irreducible loops, the Region and Phis 676 // associated with a merge that doesn't involve a backedge can 677 // be simplified now since the RPO parsing order guarantees 678 // that any path which was supposed to reach here has already 679 // been parsed or must be dead. 680 Node* c = control(); 681 Node* result = _gvn.transform_no_reclaim(control()); 682 if (c != result && TraceOptoParse) { 683 tty->print_cr("Block #%d replace %d with %d", block->rpo(), c->_idx, result->_idx); 684 } 685 if (result != top()) { 686 record_for_igvn(result); 687 } 688 } 689 690 // Parse the block. 691 do_one_block(); 692 693 // Check for bailouts. 694 if (failing()) return; 695 } 696 697 // with irreducible loops multiple passes might be necessary to parse everything 698 if (!has_irreducible || !progress) { 699 break; 700 } 701 } 702 703 blocks_seen += block_count(); 704 705 #ifndef PRODUCT 706 // Make sure there are no half-processed blocks remaining. 707 // Every remaining unprocessed block is dead and may be ignored now. 708 for (int rpo = 0; rpo < block_count(); rpo++) { 709 Block* block = rpo_at(rpo); 710 if (!block->is_parsed()) { 711 if (TraceOptoParse) { 712 tty->print_cr("Skipped dead block %d at bci:%d", rpo, block->start()); 713 } 714 assert(!block->is_merged(), "no half-processed blocks"); 715 } 716 } 717 #endif 718 } 719 720 //-------------------------------build_exits---------------------------------- 721 // Build normal and exceptional exit merge points. 722 void Parse::build_exits() { 723 // make a clone of caller to prevent sharing of side-effects 724 _exits.set_map(_exits.clone_map()); 725 _exits.clean_stack(_exits.sp()); 726 _exits.sync_jvms(); 727 728 RegionNode* region = new (C) RegionNode(1); 729 record_for_igvn(region); 730 gvn().set_type_bottom(region); 731 _exits.set_control(region); 732 733 // Note: iophi and memphi are not transformed until do_exits. 734 Node* iophi = new (C) PhiNode(region, Type::ABIO); 735 Node* memphi = new (C) PhiNode(region, Type::MEMORY, TypePtr::BOTTOM); 736 gvn().set_type_bottom(iophi); 737 gvn().set_type_bottom(memphi); 738 _exits.set_i_o(iophi); 739 _exits.set_all_memory(memphi); 740 741 // Add a return value to the exit state. (Do not push it yet.) 742 if (tf()->range()->cnt() > TypeFunc::Parms) { 743 const Type* ret_type = tf()->range()->field_at(TypeFunc::Parms); 744 // Don't "bind" an unloaded return klass to the ret_phi. If the klass 745 // becomes loaded during the subsequent parsing, the loaded and unloaded 746 // types will not join when we transform and push in do_exits(). 747 const TypeOopPtr* ret_oop_type = ret_type->isa_oopptr(); 748 if (ret_oop_type && !ret_oop_type->klass()->is_loaded()) { 749 ret_type = TypeOopPtr::BOTTOM; 750 } 751 int ret_size = type2size[ret_type->basic_type()]; 752 Node* ret_phi = new (C) PhiNode(region, ret_type); 753 gvn().set_type_bottom(ret_phi); 754 _exits.ensure_stack(ret_size); 755 assert((int)(tf()->range()->cnt() - TypeFunc::Parms) == ret_size, "good tf range"); 756 assert(method()->return_type()->size() == ret_size, "tf agrees w/ method"); 757 _exits.set_argument(0, ret_phi); // here is where the parser finds it 758 // Note: ret_phi is not yet pushed, until do_exits. 759 } 760 } 761 762 763 //----------------------------build_start_state------------------------------- 764 // Construct a state which contains only the incoming arguments from an 765 // unknown caller. The method & bci will be NULL & InvocationEntryBci. 766 JVMState* Compile::build_start_state(StartNode* start, const TypeFunc* tf) { 767 int arg_size = tf->domain()->cnt(); 768 int max_size = MAX2(arg_size, (int)tf->range()->cnt()); 769 JVMState* jvms = new (this) JVMState(max_size - TypeFunc::Parms); 770 SafePointNode* map = new (this) SafePointNode(max_size, NULL); 771 record_for_igvn(map); 772 assert(arg_size == TypeFunc::Parms + (is_osr_compilation() ? 1 : method()->arg_size()), "correct arg_size"); 773 Node_Notes* old_nn = default_node_notes(); 774 if (old_nn != NULL && has_method()) { 775 Node_Notes* entry_nn = old_nn->clone(this); 776 JVMState* entry_jvms = new(this) JVMState(method(), old_nn->jvms()); 777 entry_jvms->set_offsets(0); 778 entry_jvms->set_bci(entry_bci()); 779 entry_nn->set_jvms(entry_jvms); 780 set_default_node_notes(entry_nn); 781 } 782 uint i; 783 for (i = 0; i < (uint)arg_size; i++) { 784 Node* parm = initial_gvn()->transform(new (this) ParmNode(start, i)); 785 map->init_req(i, parm); 786 // Record all these guys for later GVN. 787 record_for_igvn(parm); 788 } 789 for (; i < map->req(); i++) { 790 map->init_req(i, top()); 791 } 792 assert(jvms->argoff() == TypeFunc::Parms, "parser gets arguments here"); 793 set_default_node_notes(old_nn); 794 map->set_jvms(jvms); 795 jvms->set_map(map); 796 return jvms; 797 } 798 799 //-----------------------------make_node_notes--------------------------------- 800 Node_Notes* Parse::make_node_notes(Node_Notes* caller_nn) { 801 if (caller_nn == NULL) return NULL; 802 Node_Notes* nn = caller_nn->clone(C); 803 JVMState* caller_jvms = nn->jvms(); 804 JVMState* jvms = new (C) JVMState(method(), caller_jvms); 805 jvms->set_offsets(0); 806 jvms->set_bci(_entry_bci); 807 nn->set_jvms(jvms); 808 return nn; 809 } 810 811 812 //--------------------------return_values-------------------------------------- 813 void Compile::return_values(JVMState* jvms) { 814 GraphKit kit(jvms); 815 Node* ret = new (this) ReturnNode(TypeFunc::Parms, 816 kit.control(), 817 kit.i_o(), 818 kit.reset_memory(), 819 kit.frameptr(), 820 kit.returnadr()); 821 // Add zero or 1 return values 822 int ret_size = tf()->range()->cnt() - TypeFunc::Parms; 823 if (ret_size > 0) { 824 kit.inc_sp(-ret_size); // pop the return value(s) 825 kit.sync_jvms(); 826 ret->add_req(kit.argument(0)); 827 // Note: The second dummy edge is not needed by a ReturnNode. 828 } 829 // bind it to root 830 root()->add_req(ret); 831 record_for_igvn(ret); 832 initial_gvn()->transform_no_reclaim(ret); 833 } 834 835 //------------------------rethrow_exceptions----------------------------------- 836 // Bind all exception states in the list into a single RethrowNode. 837 void Compile::rethrow_exceptions(JVMState* jvms) { 838 GraphKit kit(jvms); 839 if (!kit.has_exceptions()) return; // nothing to generate 840 // Load my combined exception state into the kit, with all phis transformed: 841 SafePointNode* ex_map = kit.combine_and_pop_all_exception_states(); 842 Node* ex_oop = kit.use_exception_state(ex_map); 843 RethrowNode* exit = new (this) RethrowNode(kit.control(), 844 kit.i_o(), kit.reset_memory(), 845 kit.frameptr(), kit.returnadr(), 846 // like a return but with exception input 847 ex_oop); 848 // bind to root 849 root()->add_req(exit); 850 record_for_igvn(exit); 851 initial_gvn()->transform_no_reclaim(exit); 852 } 853 854 //---------------------------do_exceptions------------------------------------- 855 // Process exceptions arising from the current bytecode. 856 // Send caught exceptions to the proper handler within this method. 857 // Unhandled exceptions feed into _exit. 858 void Parse::do_exceptions() { 859 if (!has_exceptions()) return; 860 861 if (failing()) { 862 // Pop them all off and throw them away. 863 while (pop_exception_state() != NULL) ; 864 return; 865 } 866 867 PreserveJVMState pjvms(this, false); 868 869 SafePointNode* ex_map; 870 while ((ex_map = pop_exception_state()) != NULL) { 871 if (!method()->has_exception_handlers()) { 872 // Common case: Transfer control outward. 873 // Doing it this early allows the exceptions to common up 874 // even between adjacent method calls. 875 throw_to_exit(ex_map); 876 } else { 877 // Have to look at the exception first. 878 assert(stopped(), "catch_inline_exceptions trashes the map"); 879 catch_inline_exceptions(ex_map); 880 stop_and_kill_map(); // we used up this exception state; kill it 881 } 882 } 883 884 // We now return to our regularly scheduled program: 885 } 886 887 //---------------------------throw_to_exit------------------------------------- 888 // Merge the given map into an exception exit from this method. 889 // The exception exit will handle any unlocking of receiver. 890 // The ex_oop must be saved within the ex_map, unlike merge_exception. 891 void Parse::throw_to_exit(SafePointNode* ex_map) { 892 // Pop the JVMS to (a copy of) the caller. 893 GraphKit caller; 894 caller.set_map_clone(_caller->map()); 895 caller.set_bci(_caller->bci()); 896 caller.set_sp(_caller->sp()); 897 // Copy out the standard machine state: 898 for (uint i = 0; i < TypeFunc::Parms; i++) { 899 caller.map()->set_req(i, ex_map->in(i)); 900 } 901 // ...and the exception: 902 Node* ex_oop = saved_ex_oop(ex_map); 903 SafePointNode* caller_ex_map = caller.make_exception_state(ex_oop); 904 // Finally, collect the new exception state in my exits: 905 _exits.add_exception_state(caller_ex_map); 906 } 907 908 //------------------------------do_exits--------------------------------------- 909 void Parse::do_exits() { 910 set_parse_bci(InvocationEntryBci); 911 912 // Now peephole on the return bits 913 Node* region = _exits.control(); 914 _exits.set_control(gvn().transform(region)); 915 916 Node* iophi = _exits.i_o(); 917 _exits.set_i_o(gvn().transform(iophi)); 918 919 // Figure out if we need to emit the trailing barrier. The barrier is only 920 // needed in the constructors, and only in three cases: 921 // 922 // 1. The constructor wrote a final. The effects of all initializations 923 // must be committed to memory before any code after the constructor 924 // publishes the reference to the newly constructed object. Rather 925 // than wait for the publication, we simply block the writes here. 926 // Rather than put a barrier on only those writes which are required 927 // to complete, we force all writes to complete. 928 // 929 // 2. On PPC64, also add MemBarRelease for constructors which write 930 // volatile fields. As support_IRIW_for_not_multiple_copy_atomic_cpu 931 // is set on PPC64, no sync instruction is issued after volatile 932 // stores. We want to guarantee the same behavior as on platforms 933 // with total store order, although this is not required by the Java 934 // memory model. So as with finals, we add a barrier here. 935 // 936 // 3. Experimental VM option is used to force the barrier if any field 937 // was written out in the constructor. 938 // 939 // "All bets are off" unless the first publication occurs after a 940 // normal return from the constructor. We do not attempt to detect 941 // such unusual early publications. But no barrier is needed on 942 // exceptional returns, since they cannot publish normally. 943 // 944 if (method()->is_initializer() && 945 (wrote_final() || 946 PPC64_ONLY(wrote_volatile() ||) 947 (AlwaysSafeConstructors && wrote_fields()))) { 948 _exits.insert_mem_bar(Op_MemBarRelease, alloc_with_final()); 949 #ifndef PRODUCT 950 if (PrintOpto && (Verbose || WizardMode)) { 951 method()->print_name(); 952 tty->print_cr(" writes finals and needs a memory barrier"); 953 } 954 #endif 955 } 956 957 // Any method can write a @Stable field; insert memory barriers after 958 // those also. If there is a predecessor allocation node, bind the 959 // barrier there. 960 if (wrote_stable()) { 961 _exits.insert_mem_bar(Op_MemBarRelease, alloc_with_final()); 962 #ifndef PRODUCT 963 if (PrintOpto && (Verbose || WizardMode)) { 964 method()->print_name(); 965 tty->print_cr(" writes @Stable and needs a memory barrier"); 966 } 967 #endif 968 } 969 970 for (MergeMemStream mms(_exits.merged_memory()); mms.next_non_empty(); ) { 971 // transform each slice of the original memphi: 972 mms.set_memory(_gvn.transform(mms.memory())); 973 } 974 975 if (tf()->range()->cnt() > TypeFunc::Parms) { 976 const Type* ret_type = tf()->range()->field_at(TypeFunc::Parms); 977 Node* ret_phi = _gvn.transform( _exits.argument(0) ); 978 assert(_exits.control()->is_top() || !_gvn.type(ret_phi)->empty(), "return value must be well defined"); 979 _exits.push_node(ret_type->basic_type(), ret_phi); 980 } 981 982 // Note: Logic for creating and optimizing the ReturnNode is in Compile. 983 984 // Unlock along the exceptional paths. 985 // This is done late so that we can common up equivalent exceptions 986 // (e.g., null checks) arising from multiple points within this method. 987 // See GraphKit::add_exception_state, which performs the commoning. 988 bool do_synch = method()->is_synchronized() && GenerateSynchronizationCode; 989 990 // record exit from a method if compiled while Dtrace is turned on. 991 if (do_synch || C->env()->dtrace_method_probes()) { 992 // First move the exception list out of _exits: 993 GraphKit kit(_exits.transfer_exceptions_into_jvms()); 994 SafePointNode* normal_map = kit.map(); // keep this guy safe 995 // Now re-collect the exceptions into _exits: 996 SafePointNode* ex_map; 997 while ((ex_map = kit.pop_exception_state()) != NULL) { 998 Node* ex_oop = kit.use_exception_state(ex_map); 999 // Force the exiting JVM state to have this method at InvocationEntryBci. 1000 // The exiting JVM state is otherwise a copy of the calling JVMS. 1001 JVMState* caller = kit.jvms(); 1002 JVMState* ex_jvms = caller->clone_shallow(C); 1003 ex_jvms->set_map(kit.clone_map()); 1004 ex_jvms->map()->set_jvms(ex_jvms); 1005 ex_jvms->set_bci( InvocationEntryBci); 1006 kit.set_jvms(ex_jvms); 1007 if (do_synch) { 1008 // Add on the synchronized-method box/object combo 1009 kit.map()->push_monitor(_synch_lock); 1010 // Unlock! 1011 kit.shared_unlock(_synch_lock->box_node(), _synch_lock->obj_node()); 1012 } 1013 if (C->env()->dtrace_method_probes()) { 1014 kit.make_dtrace_method_exit(method()); 1015 } 1016 // Done with exception-path processing. 1017 ex_map = kit.make_exception_state(ex_oop); 1018 assert(ex_jvms->same_calls_as(ex_map->jvms()), "sanity"); 1019 // Pop the last vestige of this method: 1020 ex_map->set_jvms(caller->clone_shallow(C)); 1021 ex_map->jvms()->set_map(ex_map); 1022 _exits.push_exception_state(ex_map); 1023 } 1024 assert(_exits.map() == normal_map, "keep the same return state"); 1025 } 1026 1027 { 1028 // Capture very early exceptions (receiver null checks) from caller JVMS 1029 GraphKit caller(_caller); 1030 SafePointNode* ex_map; 1031 while ((ex_map = caller.pop_exception_state()) != NULL) { 1032 _exits.add_exception_state(ex_map); 1033 } 1034 } 1035 } 1036 1037 //-----------------------------create_entry_map------------------------------- 1038 // Initialize our parser map to contain the types at method entry. 1039 // For OSR, the map contains a single RawPtr parameter. 1040 // Initial monitor locking for sync. methods is performed by do_method_entry. 1041 SafePointNode* Parse::create_entry_map() { 1042 // Check for really stupid bail-out cases. 1043 uint len = TypeFunc::Parms + method()->max_locals() + method()->max_stack(); 1044 if (len >= 32760) { 1045 C->record_method_not_compilable_all_tiers("too many local variables"); 1046 return NULL; 1047 } 1048 1049 // If this is an inlined method, we may have to do a receiver null check. 1050 if (_caller->has_method() && is_normal_parse() && !method()->is_static()) { 1051 GraphKit kit(_caller); 1052 kit.null_check_receiver_before_call(method()); 1053 _caller = kit.transfer_exceptions_into_jvms(); 1054 if (kit.stopped()) { 1055 _exits.add_exception_states_from(_caller); 1056 _exits.set_jvms(_caller); 1057 return NULL; 1058 } 1059 } 1060 1061 assert(method() != NULL, "parser must have a method"); 1062 1063 // Create an initial safepoint to hold JVM state during parsing 1064 JVMState* jvms = new (C) JVMState(method(), _caller->has_method() ? _caller : NULL); 1065 set_map(new (C) SafePointNode(len, jvms)); 1066 jvms->set_map(map()); 1067 record_for_igvn(map()); 1068 assert(jvms->endoff() == len, "correct jvms sizing"); 1069 1070 SafePointNode* inmap = _caller->map(); 1071 assert(inmap != NULL, "must have inmap"); 1072 1073 uint i; 1074 1075 // Pass thru the predefined input parameters. 1076 for (i = 0; i < TypeFunc::Parms; i++) { 1077 map()->init_req(i, inmap->in(i)); 1078 } 1079 1080 if (depth() == 1) { 1081 assert(map()->memory()->Opcode() == Op_Parm, ""); 1082 // Insert the memory aliasing node 1083 set_all_memory(reset_memory()); 1084 } 1085 assert(merged_memory(), ""); 1086 1087 // Now add the locals which are initially bound to arguments: 1088 uint arg_size = tf()->domain()->cnt(); 1089 ensure_stack(arg_size - TypeFunc::Parms); // OSR methods have funny args 1090 for (i = TypeFunc::Parms; i < arg_size; i++) { 1091 map()->init_req(i, inmap->argument(_caller, i - TypeFunc::Parms)); 1092 } 1093 1094 // Clear out the rest of the map (locals and stack) 1095 for (i = arg_size; i < len; i++) { 1096 map()->init_req(i, top()); 1097 } 1098 1099 SafePointNode* entry_map = stop(); 1100 return entry_map; 1101 } 1102 1103 //-----------------------------do_method_entry-------------------------------- 1104 // Emit any code needed in the pseudo-block before BCI zero. 1105 // The main thing to do is lock the receiver of a synchronized method. 1106 void Parse::do_method_entry() { 1107 set_parse_bci(InvocationEntryBci); // Pseudo-BCP 1108 set_sp(0); // Java Stack Pointer 1109 1110 NOT_PRODUCT( count_compiled_calls(true/*at_method_entry*/, false/*is_inline*/); ) 1111 1112 if (C->env()->dtrace_method_probes()) { 1113 make_dtrace_method_entry(method()); 1114 } 1115 1116 // If the method is synchronized, we need to construct a lock node, attach 1117 // it to the Start node, and pin it there. 1118 if (method()->is_synchronized()) { 1119 // Insert a FastLockNode right after the Start which takes as arguments 1120 // the current thread pointer, the "this" pointer & the address of the 1121 // stack slot pair used for the lock. The "this" pointer is a projection 1122 // off the start node, but the locking spot has to be constructed by 1123 // creating a ConLNode of 0, and boxing it with a BoxLockNode. The BoxLockNode 1124 // becomes the second argument to the FastLockNode call. The 1125 // FastLockNode becomes the new control parent to pin it to the start. 1126 1127 // Setup Object Pointer 1128 Node *lock_obj = NULL; 1129 if(method()->is_static()) { 1130 ciInstance* mirror = _method->holder()->java_mirror(); 1131 const TypeInstPtr *t_lock = TypeInstPtr::make(mirror); 1132 lock_obj = makecon(t_lock); 1133 } else { // Else pass the "this" pointer, 1134 lock_obj = local(0); // which is Parm0 from StartNode 1135 } 1136 // Clear out dead values from the debug info. 1137 kill_dead_locals(); 1138 // Build the FastLockNode 1139 _synch_lock = shared_lock(lock_obj); 1140 } 1141 1142 // Feed profiling data for parameters to the type system so it can 1143 // propagate it as speculative types 1144 record_profiled_parameters_for_speculation(); 1145 1146 if (depth() == 1) { 1147 increment_and_test_invocation_counter(Tier2CompileThreshold); 1148 } 1149 } 1150 1151 //------------------------------init_blocks------------------------------------ 1152 // Initialize our parser map to contain the types/monitors at method entry. 1153 void Parse::init_blocks() { 1154 // Create the blocks. 1155 _block_count = flow()->block_count(); 1156 _blocks = NEW_RESOURCE_ARRAY(Block, _block_count); 1157 Copy::zero_to_bytes(_blocks, sizeof(Block)*_block_count); 1158 1159 int rpo; 1160 1161 // Initialize the structs. 1162 for (rpo = 0; rpo < block_count(); rpo++) { 1163 Block* block = rpo_at(rpo); 1164 block->init_node(this, rpo); 1165 } 1166 1167 // Collect predecessor and successor information. 1168 for (rpo = 0; rpo < block_count(); rpo++) { 1169 Block* block = rpo_at(rpo); 1170 block->init_graph(this); 1171 } 1172 } 1173 1174 //-------------------------------init_node------------------------------------- 1175 void Parse::Block::init_node(Parse* outer, int rpo) { 1176 _flow = outer->flow()->rpo_at(rpo); 1177 _pred_count = 0; 1178 _preds_parsed = 0; 1179 _count = 0; 1180 assert(pred_count() == 0 && preds_parsed() == 0, "sanity"); 1181 assert(!(is_merged() || is_parsed() || is_handler() || has_merged_backedge()), "sanity"); 1182 assert(_live_locals.size() == 0, "sanity"); 1183 1184 // entry point has additional predecessor 1185 if (flow()->is_start()) _pred_count++; 1186 assert(flow()->is_start() == (this == outer->start_block()), ""); 1187 } 1188 1189 //-------------------------------init_graph------------------------------------ 1190 void Parse::Block::init_graph(Parse* outer) { 1191 // Create the successor list for this parser block. 1192 GrowableArray<ciTypeFlow::Block*>* tfs = flow()->successors(); 1193 GrowableArray<ciTypeFlow::Block*>* tfe = flow()->exceptions(); 1194 int ns = tfs->length(); 1195 int ne = tfe->length(); 1196 _num_successors = ns; 1197 _all_successors = ns+ne; 1198 _successors = (ns+ne == 0) ? NULL : NEW_RESOURCE_ARRAY(Block*, ns+ne); 1199 int p = 0; 1200 for (int i = 0; i < ns+ne; i++) { 1201 ciTypeFlow::Block* tf2 = (i < ns) ? tfs->at(i) : tfe->at(i-ns); 1202 Block* block2 = outer->rpo_at(tf2->rpo()); 1203 _successors[i] = block2; 1204 1205 // Accumulate pred info for the other block, too. 1206 if (i < ns) { 1207 block2->_pred_count++; 1208 } else { 1209 block2->_is_handler = true; 1210 } 1211 1212 #ifdef ASSERT 1213 // A block's successors must be distinguishable by BCI. 1214 // That is, no bytecode is allowed to branch to two different 1215 // clones of the same code location. 1216 for (int j = 0; j < i; j++) { 1217 Block* block1 = _successors[j]; 1218 if (block1 == block2) continue; // duplicates are OK 1219 assert(block1->start() != block2->start(), "successors have unique bcis"); 1220 } 1221 #endif 1222 } 1223 1224 // Note: We never call next_path_num along exception paths, so they 1225 // never get processed as "ready". Also, the input phis of exception 1226 // handlers get specially processed, so that 1227 } 1228 1229 //---------------------------successor_for_bci--------------------------------- 1230 Parse::Block* Parse::Block::successor_for_bci(int bci) { 1231 for (int i = 0; i < all_successors(); i++) { 1232 Block* block2 = successor_at(i); 1233 if (block2->start() == bci) return block2; 1234 } 1235 // We can actually reach here if ciTypeFlow traps out a block 1236 // due to an unloaded class, and concurrently with compilation the 1237 // class is then loaded, so that a later phase of the parser is 1238 // able to see more of the bytecode CFG. Or, the flow pass and 1239 // the parser can have a minor difference of opinion about executability 1240 // of bytecodes. For example, "obj.field = null" is executable even 1241 // if the field's type is an unloaded class; the flow pass used to 1242 // make a trap for such code. 1243 return NULL; 1244 } 1245 1246 1247 //-----------------------------stack_type_at----------------------------------- 1248 const Type* Parse::Block::stack_type_at(int i) const { 1249 return get_type(flow()->stack_type_at(i)); 1250 } 1251 1252 1253 //-----------------------------local_type_at----------------------------------- 1254 const Type* Parse::Block::local_type_at(int i) const { 1255 // Make dead locals fall to bottom. 1256 if (_live_locals.size() == 0) { 1257 MethodLivenessResult live_locals = flow()->outer()->method()->liveness_at_bci(start()); 1258 // This bitmap can be zero length if we saw a breakpoint. 1259 // In such cases, pretend they are all live. 1260 ((Block*)this)->_live_locals = live_locals; 1261 } 1262 if (_live_locals.size() > 0 && !_live_locals.at(i)) 1263 return Type::BOTTOM; 1264 1265 return get_type(flow()->local_type_at(i)); 1266 } 1267 1268 1269 #ifndef PRODUCT 1270 1271 //----------------------------name_for_bc-------------------------------------- 1272 // helper method for BytecodeParseHistogram 1273 static const char* name_for_bc(int i) { 1274 return Bytecodes::is_defined(i) ? Bytecodes::name(Bytecodes::cast(i)) : "xxxunusedxxx"; 1275 } 1276 1277 //----------------------------BytecodeParseHistogram------------------------------------ 1278 Parse::BytecodeParseHistogram::BytecodeParseHistogram(Parse *p, Compile *c) { 1279 _parser = p; 1280 _compiler = c; 1281 if( ! _initialized ) { _initialized = true; reset(); } 1282 } 1283 1284 //----------------------------current_count------------------------------------ 1285 int Parse::BytecodeParseHistogram::current_count(BPHType bph_type) { 1286 switch( bph_type ) { 1287 case BPH_transforms: { return _parser->gvn().made_progress(); } 1288 case BPH_values: { return _parser->gvn().made_new_values(); } 1289 default: { ShouldNotReachHere(); return 0; } 1290 } 1291 } 1292 1293 //----------------------------initialized-------------------------------------- 1294 bool Parse::BytecodeParseHistogram::initialized() { return _initialized; } 1295 1296 //----------------------------reset-------------------------------------------- 1297 void Parse::BytecodeParseHistogram::reset() { 1298 int i = Bytecodes::number_of_codes; 1299 while (i-- > 0) { _bytecodes_parsed[i] = 0; _nodes_constructed[i] = 0; _nodes_transformed[i] = 0; _new_values[i] = 0; } 1300 } 1301 1302 //----------------------------set_initial_state-------------------------------- 1303 // Record info when starting to parse one bytecode 1304 void Parse::BytecodeParseHistogram::set_initial_state( Bytecodes::Code bc ) { 1305 if( PrintParseStatistics && !_parser->is_osr_parse() ) { 1306 _initial_bytecode = bc; 1307 _initial_node_count = _compiler->unique(); 1308 _initial_transforms = current_count(BPH_transforms); 1309 _initial_values = current_count(BPH_values); 1310 } 1311 } 1312 1313 //----------------------------record_change-------------------------------- 1314 // Record results of parsing one bytecode 1315 void Parse::BytecodeParseHistogram::record_change() { 1316 if( PrintParseStatistics && !_parser->is_osr_parse() ) { 1317 ++_bytecodes_parsed[_initial_bytecode]; 1318 _nodes_constructed [_initial_bytecode] += (_compiler->unique() - _initial_node_count); 1319 _nodes_transformed [_initial_bytecode] += (current_count(BPH_transforms) - _initial_transforms); 1320 _new_values [_initial_bytecode] += (current_count(BPH_values) - _initial_values); 1321 } 1322 } 1323 1324 1325 //----------------------------print-------------------------------------------- 1326 void Parse::BytecodeParseHistogram::print(float cutoff) { 1327 ResourceMark rm; 1328 // print profile 1329 int total = 0; 1330 int i = 0; 1331 for( i = 0; i < Bytecodes::number_of_codes; ++i ) { total += _bytecodes_parsed[i]; } 1332 int abs_sum = 0; 1333 tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 1334 tty->print_cr("Histogram of %d parsed bytecodes:", total); 1335 if( total == 0 ) { return; } 1336 tty->cr(); 1337 tty->print_cr("absolute: count of compiled bytecodes of this type"); 1338 tty->print_cr("relative: percentage contribution to compiled nodes"); 1339 tty->print_cr("nodes : Average number of nodes constructed per bytecode"); 1340 tty->print_cr("rnodes : Significance towards total nodes constructed, (nodes*relative)"); 1341 tty->print_cr("transforms: Average amount of tranform progress per bytecode compiled"); 1342 tty->print_cr("values : Average number of node values improved per bytecode"); 1343 tty->print_cr("name : Bytecode name"); 1344 tty->cr(); 1345 tty->print_cr(" absolute relative nodes rnodes transforms values name"); 1346 tty->print_cr("----------------------------------------------------------------------"); 1347 while (--i > 0) { 1348 int abs = _bytecodes_parsed[i]; 1349 float rel = abs * 100.0F / total; 1350 float nodes = _bytecodes_parsed[i] == 0 ? 0 : (1.0F * _nodes_constructed[i])/_bytecodes_parsed[i]; 1351 float rnodes = _bytecodes_parsed[i] == 0 ? 0 : rel * nodes; 1352 float xforms = _bytecodes_parsed[i] == 0 ? 0 : (1.0F * _nodes_transformed[i])/_bytecodes_parsed[i]; 1353 float values = _bytecodes_parsed[i] == 0 ? 0 : (1.0F * _new_values [i])/_bytecodes_parsed[i]; 1354 if (cutoff <= rel) { 1355 tty->print_cr("%10d %7.2f%% %6.1f %6.2f %6.1f %6.1f %s", abs, rel, nodes, rnodes, xforms, values, name_for_bc(i)); 1356 abs_sum += abs; 1357 } 1358 } 1359 tty->print_cr("----------------------------------------------------------------------"); 1360 float rel_sum = abs_sum * 100.0F / total; 1361 tty->print_cr("%10d %7.2f%% (cutoff = %.2f%%)", abs_sum, rel_sum, cutoff); 1362 tty->print_cr("----------------------------------------------------------------------"); 1363 tty->cr(); 1364 } 1365 #endif 1366 1367 //----------------------------load_state_from---------------------------------- 1368 // Load block/map/sp. But not do not touch iter/bci. 1369 void Parse::load_state_from(Block* block) { 1370 set_block(block); 1371 // load the block's JVM state: 1372 set_map(block->start_map()); 1373 set_sp( block->start_sp()); 1374 } 1375 1376 1377 //-----------------------------record_state------------------------------------ 1378 void Parse::Block::record_state(Parse* p) { 1379 assert(!is_merged(), "can only record state once, on 1st inflow"); 1380 assert(start_sp() == p->sp(), "stack pointer must agree with ciTypeFlow"); 1381 set_start_map(p->stop()); 1382 } 1383 1384 1385 //------------------------------do_one_block----------------------------------- 1386 void Parse::do_one_block() { 1387 if (TraceOptoParse) { 1388 Block *b = block(); 1389 int ns = b->num_successors(); 1390 int nt = b->all_successors(); 1391 1392 tty->print("Parsing block #%d at bci [%d,%d), successors: ", 1393 block()->rpo(), block()->start(), block()->limit()); 1394 for (int i = 0; i < nt; i++) { 1395 tty->print((( i < ns) ? " %d" : " %d(e)"), b->successor_at(i)->rpo()); 1396 } 1397 if (b->is_loop_head()) tty->print(" lphd"); 1398 tty->cr(); 1399 } 1400 1401 assert(block()->is_merged(), "must be merged before being parsed"); 1402 block()->mark_parsed(); 1403 ++_blocks_parsed; 1404 1405 // Set iterator to start of block. 1406 iter().reset_to_bci(block()->start()); 1407 1408 CompileLog* log = C->log(); 1409 1410 // Parse bytecodes 1411 while (!stopped() && !failing()) { 1412 iter().next(); 1413 1414 // Learn the current bci from the iterator: 1415 set_parse_bci(iter().cur_bci()); 1416 1417 if (bci() == block()->limit()) { 1418 // Do not walk into the next block until directed by do_all_blocks. 1419 merge(bci()); 1420 break; 1421 } 1422 assert(bci() < block()->limit(), "bci still in block"); 1423 1424 if (log != NULL) { 1425 // Output an optional context marker, to help place actions 1426 // that occur during parsing of this BC. If there is no log 1427 // output until the next context string, this context string 1428 // will be silently ignored. 1429 log->set_context("bc code='%d' bci='%d'", (int)bc(), bci()); 1430 } 1431 1432 if (block()->has_trap_at(bci())) { 1433 // We must respect the flow pass's traps, because it will refuse 1434 // to produce successors for trapping blocks. 1435 int trap_index = block()->flow()->trap_index(); 1436 assert(trap_index != 0, "trap index must be valid"); 1437 uncommon_trap(trap_index); 1438 break; 1439 } 1440 1441 NOT_PRODUCT( parse_histogram()->set_initial_state(bc()); ); 1442 1443 #ifdef ASSERT 1444 int pre_bc_sp = sp(); 1445 int inputs, depth; 1446 bool have_se = !stopped() && compute_stack_effects(inputs, depth); 1447 assert(!have_se || pre_bc_sp >= inputs, err_msg_res("have enough stack to execute this BC: pre_bc_sp=%d, inputs=%d", pre_bc_sp, inputs)); 1448 #endif //ASSERT 1449 1450 do_one_bytecode(); 1451 1452 assert(!have_se || stopped() || failing() || (sp() - pre_bc_sp) == depth, 1453 err_msg_res("incorrect depth prediction: sp=%d, pre_bc_sp=%d, depth=%d", sp(), pre_bc_sp, depth)); 1454 1455 do_exceptions(); 1456 1457 NOT_PRODUCT( parse_histogram()->record_change(); ); 1458 1459 if (log != NULL) 1460 log->clear_context(); // skip marker if nothing was printed 1461 1462 // Fall into next bytecode. Each bytecode normally has 1 sequential 1463 // successor which is typically made ready by visiting this bytecode. 1464 // If the successor has several predecessors, then it is a merge 1465 // point, starts a new basic block, and is handled like other basic blocks. 1466 } 1467 } 1468 1469 1470 //------------------------------merge------------------------------------------ 1471 void Parse::set_parse_bci(int bci) { 1472 set_bci(bci); 1473 Node_Notes* nn = C->default_node_notes(); 1474 if (nn == NULL) return; 1475 1476 // Collect debug info for inlined calls unless -XX:-DebugInlinedCalls. 1477 if (!DebugInlinedCalls && depth() > 1) { 1478 return; 1479 } 1480 1481 // Update the JVMS annotation, if present. 1482 JVMState* jvms = nn->jvms(); 1483 if (jvms != NULL && jvms->bci() != bci) { 1484 // Update the JVMS. 1485 jvms = jvms->clone_shallow(C); 1486 jvms->set_bci(bci); 1487 nn->set_jvms(jvms); 1488 } 1489 } 1490 1491 //------------------------------merge------------------------------------------ 1492 // Merge the current mapping into the basic block starting at bci 1493 void Parse::merge(int target_bci) { 1494 Block* target = successor_for_bci(target_bci); 1495 if (target == NULL) { handle_missing_successor(target_bci); return; } 1496 assert(!target->is_ready(), "our arrival must be expected"); 1497 int pnum = target->next_path_num(); 1498 merge_common(target, pnum); 1499 } 1500 1501 //-------------------------merge_new_path-------------------------------------- 1502 // Merge the current mapping into the basic block, using a new path 1503 void Parse::merge_new_path(int target_bci) { 1504 Block* target = successor_for_bci(target_bci); 1505 if (target == NULL) { handle_missing_successor(target_bci); return; } 1506 assert(!target->is_ready(), "new path into frozen graph"); 1507 int pnum = target->add_new_path(); 1508 merge_common(target, pnum); 1509 } 1510 1511 //-------------------------merge_exception------------------------------------- 1512 // Merge the current mapping into the basic block starting at bci 1513 // The ex_oop must be pushed on the stack, unlike throw_to_exit. 1514 void Parse::merge_exception(int target_bci) { 1515 assert(sp() == 1, "must have only the throw exception on the stack"); 1516 Block* target = successor_for_bci(target_bci); 1517 if (target == NULL) { handle_missing_successor(target_bci); return; } 1518 assert(target->is_handler(), "exceptions are handled by special blocks"); 1519 int pnum = target->add_new_path(); 1520 merge_common(target, pnum); 1521 } 1522 1523 //--------------------handle_missing_successor--------------------------------- 1524 void Parse::handle_missing_successor(int target_bci) { 1525 #ifndef PRODUCT 1526 Block* b = block(); 1527 int trap_bci = b->flow()->has_trap()? b->flow()->trap_bci(): -1; 1528 tty->print_cr("### Missing successor at bci:%d for block #%d (trap_bci:%d)", target_bci, b->rpo(), trap_bci); 1529 #endif 1530 ShouldNotReachHere(); 1531 } 1532 1533 //--------------------------merge_common--------------------------------------- 1534 void Parse::merge_common(Parse::Block* target, int pnum) { 1535 if (TraceOptoParse) { 1536 tty->print("Merging state at block #%d bci:%d", target->rpo(), target->start()); 1537 } 1538 1539 // Zap extra stack slots to top 1540 assert(sp() == target->start_sp(), ""); 1541 clean_stack(sp()); 1542 1543 if (!target->is_merged()) { // No prior mapping at this bci 1544 if (TraceOptoParse) { tty->print(" with empty state"); } 1545 1546 // If this path is dead, do not bother capturing it as a merge. 1547 // It is "as if" we had 1 fewer predecessors from the beginning. 1548 if (stopped()) { 1549 if (TraceOptoParse) tty->print_cr(", but path is dead and doesn't count"); 1550 return; 1551 } 1552 1553 // Record that a new block has been merged. 1554 ++_blocks_merged; 1555 1556 // Make a region if we know there are multiple or unpredictable inputs. 1557 // (Also, if this is a plain fall-through, we might see another region, 1558 // which must not be allowed into this block's map.) 1559 if (pnum > PhiNode::Input // Known multiple inputs. 1560 || target->is_handler() // These have unpredictable inputs. 1561 || target->is_loop_head() // Known multiple inputs 1562 || control()->is_Region()) { // We must hide this guy. 1563 1564 int current_bci = bci(); 1565 set_parse_bci(target->start()); // Set target bci 1566 if (target->is_SEL_head()) { 1567 DEBUG_ONLY( target->mark_merged_backedge(block()); ) 1568 if (target->start() == 0) { 1569 // Add loop predicate for the special case when 1570 // there are backbranches to the method entry. 1571 add_predicate(); 1572 } 1573 } 1574 // Add a Region to start the new basic block. Phis will be added 1575 // later lazily. 1576 int edges = target->pred_count(); 1577 if (edges < pnum) edges = pnum; // might be a new path! 1578 RegionNode *r = new (C) RegionNode(edges+1); 1579 gvn().set_type(r, Type::CONTROL); 1580 record_for_igvn(r); 1581 // zap all inputs to NULL for debugging (done in Node(uint) constructor) 1582 // for (int j = 1; j < edges+1; j++) { r->init_req(j, NULL); } 1583 r->init_req(pnum, control()); 1584 set_control(r); 1585 set_parse_bci(current_bci); // Restore bci 1586 } 1587 1588 // Convert the existing Parser mapping into a mapping at this bci. 1589 store_state_to(target); 1590 assert(target->is_merged(), "do not come here twice"); 1591 1592 } else { // Prior mapping at this bci 1593 if (TraceOptoParse) { tty->print(" with previous state"); } 1594 #ifdef ASSERT 1595 if (target->is_SEL_head()) { 1596 target->mark_merged_backedge(block()); 1597 } 1598 #endif 1599 // We must not manufacture more phis if the target is already parsed. 1600 bool nophi = target->is_parsed(); 1601 1602 SafePointNode* newin = map();// Hang on to incoming mapping 1603 Block* save_block = block(); // Hang on to incoming block; 1604 load_state_from(target); // Get prior mapping 1605 1606 assert(newin->jvms()->locoff() == jvms()->locoff(), "JVMS layouts agree"); 1607 assert(newin->jvms()->stkoff() == jvms()->stkoff(), "JVMS layouts agree"); 1608 assert(newin->jvms()->monoff() == jvms()->monoff(), "JVMS layouts agree"); 1609 assert(newin->jvms()->endoff() == jvms()->endoff(), "JVMS layouts agree"); 1610 1611 // Iterate over my current mapping and the old mapping. 1612 // Where different, insert Phi functions. 1613 // Use any existing Phi functions. 1614 assert(control()->is_Region(), "must be merging to a region"); 1615 RegionNode* r = control()->as_Region(); 1616 1617 // Compute where to merge into 1618 // Merge incoming control path 1619 r->init_req(pnum, newin->control()); 1620 1621 if (pnum == 1) { // Last merge for this Region? 1622 if (!block()->flow()->is_irreducible_entry()) { 1623 Node* result = _gvn.transform_no_reclaim(r); 1624 if (r != result && TraceOptoParse) { 1625 tty->print_cr("Block #%d replace %d with %d", block()->rpo(), r->_idx, result->_idx); 1626 } 1627 } 1628 record_for_igvn(r); 1629 } 1630 1631 // Update all the non-control inputs to map: 1632 assert(TypeFunc::Parms == newin->jvms()->locoff(), "parser map should contain only youngest jvms"); 1633 bool check_elide_phi = target->is_SEL_backedge(save_block); 1634 for (uint j = 1; j < newin->req(); j++) { 1635 Node* m = map()->in(j); // Current state of target. 1636 Node* n = newin->in(j); // Incoming change to target state. 1637 PhiNode* phi; 1638 if (m->is_Phi() && m->as_Phi()->region() == r) 1639 phi = m->as_Phi(); 1640 else 1641 phi = NULL; 1642 if (m != n) { // Different; must merge 1643 switch (j) { 1644 // Frame pointer and Return Address never changes 1645 case TypeFunc::FramePtr:// Drop m, use the original value 1646 case TypeFunc::ReturnAdr: 1647 break; 1648 case TypeFunc::Memory: // Merge inputs to the MergeMem node 1649 assert(phi == NULL, "the merge contains phis, not vice versa"); 1650 merge_memory_edges(n->as_MergeMem(), pnum, nophi); 1651 continue; 1652 default: // All normal stuff 1653 if (phi == NULL) { 1654 const JVMState* jvms = map()->jvms(); 1655 if (EliminateNestedLocks && 1656 jvms->is_mon(j) && jvms->is_monitor_box(j)) { 1657 // BoxLock nodes are not commoning. 1658 // Use old BoxLock node as merged box. 1659 assert(newin->jvms()->is_monitor_box(j), "sanity"); 1660 // This assert also tests that nodes are BoxLock. 1661 assert(BoxLockNode::same_slot(n, m), "sanity"); 1662 C->gvn_replace_by(n, m); 1663 } else if (!check_elide_phi || !target->can_elide_SEL_phi(j)) { 1664 phi = ensure_phi(j, nophi); 1665 } 1666 } 1667 break; 1668 } 1669 } 1670 // At this point, n might be top if: 1671 // - there is no phi (because TypeFlow detected a conflict), or 1672 // - the corresponding control edges is top (a dead incoming path) 1673 // It is a bug if we create a phi which sees a garbage value on a live path. 1674 1675 if (phi != NULL) { 1676 assert(n != top() || r->in(pnum) == top(), "live value must not be garbage"); 1677 assert(phi->region() == r, ""); 1678 phi->set_req(pnum, n); // Then add 'n' to the merge 1679 if (pnum == PhiNode::Input) { 1680 // Last merge for this Phi. 1681 // So far, Phis have had a reasonable type from ciTypeFlow. 1682 // Now _gvn will join that with the meet of current inputs. 1683 // BOTTOM is never permissible here, 'cause pessimistically 1684 // Phis of pointers cannot lose the basic pointer type. 1685 debug_only(const Type* bt1 = phi->bottom_type()); 1686 assert(bt1 != Type::BOTTOM, "should not be building conflict phis"); 1687 map()->set_req(j, _gvn.transform_no_reclaim(phi)); 1688 debug_only(const Type* bt2 = phi->bottom_type()); 1689 assert(bt2->higher_equal_speculative(bt1), "must be consistent with type-flow"); 1690 record_for_igvn(phi); 1691 } 1692 } 1693 } // End of for all values to be merged 1694 1695 if (pnum == PhiNode::Input && 1696 !r->in(0)) { // The occasional useless Region 1697 assert(control() == r, ""); 1698 set_control(r->nonnull_req()); 1699 } 1700 1701 // newin has been subsumed into the lazy merge, and is now dead. 1702 set_block(save_block); 1703 1704 stop(); // done with this guy, for now 1705 } 1706 1707 if (TraceOptoParse) { 1708 tty->print_cr(" on path %d", pnum); 1709 } 1710 1711 // Done with this parser state. 1712 assert(stopped(), ""); 1713 } 1714 1715 1716 //--------------------------merge_memory_edges--------------------------------- 1717 void Parse::merge_memory_edges(MergeMemNode* n, int pnum, bool nophi) { 1718 // (nophi means we must not create phis, because we already parsed here) 1719 assert(n != NULL, ""); 1720 // Merge the inputs to the MergeMems 1721 MergeMemNode* m = merged_memory(); 1722 1723 assert(control()->is_Region(), "must be merging to a region"); 1724 RegionNode* r = control()->as_Region(); 1725 1726 PhiNode* base = NULL; 1727 MergeMemNode* remerge = NULL; 1728 for (MergeMemStream mms(m, n); mms.next_non_empty2(); ) { 1729 Node *p = mms.force_memory(); 1730 Node *q = mms.memory2(); 1731 if (mms.is_empty() && nophi) { 1732 // Trouble: No new splits allowed after a loop body is parsed. 1733 // Instead, wire the new split into a MergeMem on the backedge. 1734 // The optimizer will sort it out, slicing the phi. 1735 if (remerge == NULL) { 1736 assert(base != NULL, ""); 1737 assert(base->in(0) != NULL, "should not be xformed away"); 1738 remerge = MergeMemNode::make(C, base->in(pnum)); 1739 gvn().set_type(remerge, Type::MEMORY); 1740 base->set_req(pnum, remerge); 1741 } 1742 remerge->set_memory_at(mms.alias_idx(), q); 1743 continue; 1744 } 1745 assert(!q->is_MergeMem(), ""); 1746 PhiNode* phi; 1747 if (p != q) { 1748 phi = ensure_memory_phi(mms.alias_idx(), nophi); 1749 } else { 1750 if (p->is_Phi() && p->as_Phi()->region() == r) 1751 phi = p->as_Phi(); 1752 else 1753 phi = NULL; 1754 } 1755 // Insert q into local phi 1756 if (phi != NULL) { 1757 assert(phi->region() == r, ""); 1758 p = phi; 1759 phi->set_req(pnum, q); 1760 if (mms.at_base_memory()) { 1761 base = phi; // delay transforming it 1762 } else if (pnum == 1) { 1763 record_for_igvn(phi); 1764 p = _gvn.transform_no_reclaim(phi); 1765 } 1766 mms.set_memory(p);// store back through the iterator 1767 } 1768 } 1769 // Transform base last, in case we must fiddle with remerging. 1770 if (base != NULL && pnum == 1) { 1771 record_for_igvn(base); 1772 m->set_base_memory( _gvn.transform_no_reclaim(base) ); 1773 } 1774 } 1775 1776 1777 //------------------------ensure_phis_everywhere------------------------------- 1778 void Parse::ensure_phis_everywhere() { 1779 ensure_phi(TypeFunc::I_O); 1780 1781 // Ensure a phi on all currently known memories. 1782 for (MergeMemStream mms(merged_memory()); mms.next_non_empty(); ) { 1783 ensure_memory_phi(mms.alias_idx()); 1784 debug_only(mms.set_memory()); // keep the iterator happy 1785 } 1786 1787 // Note: This is our only chance to create phis for memory slices. 1788 // If we miss a slice that crops up later, it will have to be 1789 // merged into the base-memory phi that we are building here. 1790 // Later, the optimizer will comb out the knot, and build separate 1791 // phi-loops for each memory slice that matters. 1792 1793 // Monitors must nest nicely and not get confused amongst themselves. 1794 // Phi-ify everything up to the monitors, though. 1795 uint monoff = map()->jvms()->monoff(); 1796 uint nof_monitors = map()->jvms()->nof_monitors(); 1797 1798 assert(TypeFunc::Parms == map()->jvms()->locoff(), "parser map should contain only youngest jvms"); 1799 bool check_elide_phi = block()->is_SEL_head(); 1800 for (uint i = TypeFunc::Parms; i < monoff; i++) { 1801 if (!check_elide_phi || !block()->can_elide_SEL_phi(i)) { 1802 ensure_phi(i); 1803 } 1804 } 1805 1806 // Even monitors need Phis, though they are well-structured. 1807 // This is true for OSR methods, and also for the rare cases where 1808 // a monitor object is the subject of a replace_in_map operation. 1809 // See bugs 4426707 and 5043395. 1810 for (uint m = 0; m < nof_monitors; m++) { 1811 ensure_phi(map()->jvms()->monitor_obj_offset(m)); 1812 } 1813 } 1814 1815 1816 //-----------------------------add_new_path------------------------------------ 1817 // Add a previously unaccounted predecessor to this block. 1818 int Parse::Block::add_new_path() { 1819 // If there is no map, return the lowest unused path number. 1820 if (!is_merged()) return pred_count()+1; // there will be a map shortly 1821 1822 SafePointNode* map = start_map(); 1823 if (!map->control()->is_Region()) 1824 return pred_count()+1; // there may be a region some day 1825 RegionNode* r = map->control()->as_Region(); 1826 1827 // Add new path to the region. 1828 uint pnum = r->req(); 1829 r->add_req(NULL); 1830 1831 for (uint i = 1; i < map->req(); i++) { 1832 Node* n = map->in(i); 1833 if (i == TypeFunc::Memory) { 1834 // Ensure a phi on all currently known memories. 1835 for (MergeMemStream mms(n->as_MergeMem()); mms.next_non_empty(); ) { 1836 Node* phi = mms.memory(); 1837 if (phi->is_Phi() && phi->as_Phi()->region() == r) { 1838 assert(phi->req() == pnum, "must be same size as region"); 1839 phi->add_req(NULL); 1840 } 1841 } 1842 } else { 1843 if (n->is_Phi() && n->as_Phi()->region() == r) { 1844 assert(n->req() == pnum, "must be same size as region"); 1845 n->add_req(NULL); 1846 } 1847 } 1848 } 1849 1850 return pnum; 1851 } 1852 1853 //------------------------------ensure_phi------------------------------------- 1854 // Turn the idx'th entry of the current map into a Phi 1855 PhiNode *Parse::ensure_phi(int idx, bool nocreate) { 1856 SafePointNode* map = this->map(); 1857 Node* region = map->control(); 1858 assert(region->is_Region(), ""); 1859 1860 Node* o = map->in(idx); 1861 assert(o != NULL, ""); 1862 1863 if (o == top()) return NULL; // TOP always merges into TOP 1864 1865 if (o->is_Phi() && o->as_Phi()->region() == region) { 1866 return o->as_Phi(); 1867 } 1868 1869 // Now use a Phi here for merging 1870 assert(!nocreate, "Cannot build a phi for a block already parsed."); 1871 const JVMState* jvms = map->jvms(); 1872 const Type* t; 1873 if (jvms->is_loc(idx)) { 1874 t = block()->local_type_at(idx - jvms->locoff()); 1875 } else if (jvms->is_stk(idx)) { 1876 t = block()->stack_type_at(idx - jvms->stkoff()); 1877 } else if (jvms->is_mon(idx)) { 1878 assert(!jvms->is_monitor_box(idx), "no phis for boxes"); 1879 t = TypeInstPtr::BOTTOM; // this is sufficient for a lock object 1880 } else if ((uint)idx < TypeFunc::Parms) { 1881 t = o->bottom_type(); // Type::RETURN_ADDRESS or such-like. 1882 } else { 1883 assert(false, "no type information for this phi"); 1884 } 1885 1886 // If the type falls to bottom, then this must be a local that 1887 // is mixing ints and oops or some such. Forcing it to top 1888 // makes it go dead. 1889 if (t == Type::BOTTOM) { 1890 map->set_req(idx, top()); 1891 return NULL; 1892 } 1893 1894 // Do not create phis for top either. 1895 // A top on a non-null control flow must be an unused even after the.phi. 1896 if (t == Type::TOP || t == Type::HALF) { 1897 map->set_req(idx, top()); 1898 return NULL; 1899 } 1900 1901 PhiNode* phi = PhiNode::make(region, o, t); 1902 gvn().set_type(phi, t); 1903 if (C->do_escape_analysis()) record_for_igvn(phi); 1904 map->set_req(idx, phi); 1905 return phi; 1906 } 1907 1908 //--------------------------ensure_memory_phi---------------------------------- 1909 // Turn the idx'th slice of the current memory into a Phi 1910 PhiNode *Parse::ensure_memory_phi(int idx, bool nocreate) { 1911 MergeMemNode* mem = merged_memory(); 1912 Node* region = control(); 1913 assert(region->is_Region(), ""); 1914 1915 Node *o = (idx == Compile::AliasIdxBot)? mem->base_memory(): mem->memory_at(idx); 1916 assert(o != NULL && o != top(), ""); 1917 1918 PhiNode* phi; 1919 if (o->is_Phi() && o->as_Phi()->region() == region) { 1920 phi = o->as_Phi(); 1921 if (phi == mem->base_memory() && idx >= Compile::AliasIdxRaw) { 1922 // clone the shared base memory phi to make a new memory split 1923 assert(!nocreate, "Cannot build a phi for a block already parsed."); 1924 const Type* t = phi->bottom_type(); 1925 const TypePtr* adr_type = C->get_adr_type(idx); 1926 phi = phi->slice_memory(adr_type); 1927 gvn().set_type(phi, t); 1928 } 1929 return phi; 1930 } 1931 1932 // Now use a Phi here for merging 1933 assert(!nocreate, "Cannot build a phi for a block already parsed."); 1934 const Type* t = o->bottom_type(); 1935 const TypePtr* adr_type = C->get_adr_type(idx); 1936 phi = PhiNode::make(region, o, t, adr_type); 1937 gvn().set_type(phi, t); 1938 if (idx == Compile::AliasIdxBot) 1939 mem->set_base_memory(phi); 1940 else 1941 mem->set_memory_at(idx, phi); 1942 return phi; 1943 } 1944 1945 //------------------------------call_register_finalizer----------------------- 1946 // Check the klass of the receiver and call register_finalizer if the 1947 // class need finalization. 1948 void Parse::call_register_finalizer() { 1949 Node* receiver = local(0); 1950 assert(receiver != NULL && receiver->bottom_type()->isa_instptr() != NULL, 1951 "must have non-null instance type"); 1952 1953 const TypeInstPtr *tinst = receiver->bottom_type()->isa_instptr(); 1954 if (tinst != NULL && tinst->klass()->is_loaded() && !tinst->klass_is_exact()) { 1955 // The type isn't known exactly so see if CHA tells us anything. 1956 ciInstanceKlass* ik = tinst->klass()->as_instance_klass(); 1957 if (!Dependencies::has_finalizable_subclass(ik)) { 1958 // No finalizable subclasses so skip the dynamic check. 1959 C->dependencies()->assert_has_no_finalizable_subclasses(ik); 1960 return; 1961 } 1962 } 1963 1964 // Insert a dynamic test for whether the instance needs 1965 // finalization. In general this will fold up since the concrete 1966 // class is often visible so the access flags are constant. 1967 Node* klass_addr = basic_plus_adr( receiver, receiver, oopDesc::klass_offset_in_bytes() ); 1968 Node* klass = _gvn.transform( LoadKlassNode::make(_gvn, immutable_memory(), klass_addr, TypeInstPtr::KLASS) ); 1969 1970 Node* access_flags_addr = basic_plus_adr(klass, klass, in_bytes(Klass::access_flags_offset())); 1971 Node* access_flags = make_load(NULL, access_flags_addr, TypeInt::INT, T_INT, MemNode::unordered); 1972 1973 Node* mask = _gvn.transform(new (C) AndINode(access_flags, intcon(JVM_ACC_HAS_FINALIZER))); 1974 Node* check = _gvn.transform(new (C) CmpINode(mask, intcon(0))); 1975 Node* test = _gvn.transform(new (C) BoolNode(check, BoolTest::ne)); 1976 1977 IfNode* iff = create_and_map_if(control(), test, PROB_MAX, COUNT_UNKNOWN); 1978 1979 RegionNode* result_rgn = new (C) RegionNode(3); 1980 record_for_igvn(result_rgn); 1981 1982 Node *skip_register = _gvn.transform(new (C) IfFalseNode(iff)); 1983 result_rgn->init_req(1, skip_register); 1984 1985 Node *needs_register = _gvn.transform(new (C) IfTrueNode(iff)); 1986 set_control(needs_register); 1987 if (stopped()) { 1988 // There is no slow path. 1989 result_rgn->init_req(2, top()); 1990 } else { 1991 Node *call = make_runtime_call(RC_NO_LEAF, 1992 OptoRuntime::register_finalizer_Type(), 1993 OptoRuntime::register_finalizer_Java(), 1994 NULL, TypePtr::BOTTOM, 1995 receiver); 1996 make_slow_call_ex(call, env()->Throwable_klass(), true); 1997 1998 Node* fast_io = call->in(TypeFunc::I_O); 1999 Node* fast_mem = call->in(TypeFunc::Memory); 2000 // These two phis are pre-filled with copies of of the fast IO and Memory 2001 Node* io_phi = PhiNode::make(result_rgn, fast_io, Type::ABIO); 2002 Node* mem_phi = PhiNode::make(result_rgn, fast_mem, Type::MEMORY, TypePtr::BOTTOM); 2003 2004 result_rgn->init_req(2, control()); 2005 io_phi ->init_req(2, i_o()); 2006 mem_phi ->init_req(2, reset_memory()); 2007 2008 set_all_memory( _gvn.transform(mem_phi) ); 2009 set_i_o( _gvn.transform(io_phi) ); 2010 } 2011 2012 set_control( _gvn.transform(result_rgn) ); 2013 } 2014 2015 // Add check to deoptimize if RTM state is not ProfileRTM 2016 void Parse::rtm_deopt() { 2017 #if INCLUDE_RTM_OPT 2018 if (C->profile_rtm()) { 2019 assert(C->method() != NULL, "only for normal compilations"); 2020 assert(!C->method()->method_data()->is_empty(), "MDO is needed to record RTM state"); 2021 assert(depth() == 1, "generate check only for main compiled method"); 2022 2023 // Set starting bci for uncommon trap. 2024 set_parse_bci(is_osr_parse() ? osr_bci() : 0); 2025 2026 // Load the rtm_state from the MethodData. 2027 const TypePtr* adr_type = TypeMetadataPtr::make(C->method()->method_data()); 2028 Node* mdo = makecon(adr_type); 2029 int offset = MethodData::rtm_state_offset_in_bytes(); 2030 Node* adr_node = basic_plus_adr(mdo, mdo, offset); 2031 Node* rtm_state = make_load(control(), adr_node, TypeInt::INT, T_INT, adr_type, MemNode::unordered); 2032 2033 // Separate Load from Cmp by Opaque. 2034 // In expand_macro_nodes() it will be replaced either 2035 // with this load when there are locks in the code 2036 // or with ProfileRTM (cmp->in(2)) otherwise so that 2037 // the check will fold. 2038 Node* profile_state = makecon(TypeInt::make(ProfileRTM)); 2039 Node* opq = _gvn.transform( new (C) Opaque3Node(C, rtm_state, Opaque3Node::RTM_OPT) ); 2040 Node* chk = _gvn.transform( new (C) CmpINode(opq, profile_state) ); 2041 Node* tst = _gvn.transform( new (C) BoolNode(chk, BoolTest::eq) ); 2042 // Branch to failure if state was changed 2043 { BuildCutout unless(this, tst, PROB_ALWAYS); 2044 uncommon_trap(Deoptimization::Reason_rtm_state_change, 2045 Deoptimization::Action_make_not_entrant); 2046 } 2047 } 2048 #endif 2049 } 2050 2051 //------------------------------return_current--------------------------------- 2052 // Append current _map to _exit_return 2053 void Parse::return_current(Node* value) { 2054 if (RegisterFinalizersAtInit && 2055 method()->intrinsic_id() == vmIntrinsics::_Object_init) { 2056 call_register_finalizer(); 2057 } 2058 2059 // Do not set_parse_bci, so that return goo is credited to the return insn. 2060 set_bci(InvocationEntryBci); 2061 if (method()->is_synchronized() && GenerateSynchronizationCode) { 2062 shared_unlock(_synch_lock->box_node(), _synch_lock->obj_node()); 2063 } 2064 if (C->env()->dtrace_method_probes()) { 2065 make_dtrace_method_exit(method()); 2066 } 2067 SafePointNode* exit_return = _exits.map(); 2068 exit_return->in( TypeFunc::Control )->add_req( control() ); 2069 exit_return->in( TypeFunc::I_O )->add_req( i_o () ); 2070 Node *mem = exit_return->in( TypeFunc::Memory ); 2071 for (MergeMemStream mms(mem->as_MergeMem(), merged_memory()); mms.next_non_empty2(); ) { 2072 if (mms.is_empty()) { 2073 // get a copy of the base memory, and patch just this one input 2074 const TypePtr* adr_type = mms.adr_type(C); 2075 Node* phi = mms.force_memory()->as_Phi()->slice_memory(adr_type); 2076 assert(phi->as_Phi()->region() == mms.base_memory()->in(0), ""); 2077 gvn().set_type_bottom(phi); 2078 phi->del_req(phi->req()-1); // prepare to re-patch 2079 mms.set_memory(phi); 2080 } 2081 mms.memory()->add_req(mms.memory2()); 2082 } 2083 2084 // frame pointer is always same, already captured 2085 if (value != NULL) { 2086 // If returning oops to an interface-return, there is a silent free 2087 // cast from oop to interface allowed by the Verifier. Make it explicit 2088 // here. 2089 Node* phi = _exits.argument(0); 2090 const TypeInstPtr *tr = phi->bottom_type()->isa_instptr(); 2091 if( tr && tr->klass()->is_loaded() && 2092 tr->klass()->is_interface() ) { 2093 const TypeInstPtr *tp = value->bottom_type()->isa_instptr(); 2094 if (tp && tp->klass()->is_loaded() && 2095 !tp->klass()->is_interface()) { 2096 // sharpen the type eagerly; this eases certain assert checking 2097 if (tp->higher_equal(TypeInstPtr::NOTNULL)) 2098 tr = tr->join_speculative(TypeInstPtr::NOTNULL)->is_instptr(); 2099 value = _gvn.transform(new (C) CheckCastPPNode(0,value,tr)); 2100 } 2101 } 2102 phi->add_req(value); 2103 } 2104 2105 stop_and_kill_map(); // This CFG path dies here 2106 } 2107 2108 2109 //------------------------------add_safepoint---------------------------------- 2110 void Parse::add_safepoint() { 2111 // See if we can avoid this safepoint. No need for a SafePoint immediately 2112 // after a Call (except Leaf Call) or another SafePoint. 2113 Node *proj = control(); 2114 bool add_poll_param = SafePointNode::needs_polling_address_input(); 2115 uint parms = add_poll_param ? TypeFunc::Parms+1 : TypeFunc::Parms; 2116 if( proj->is_Proj() ) { 2117 Node *n0 = proj->in(0); 2118 if( n0->is_Catch() ) { 2119 n0 = n0->in(0)->in(0); 2120 assert( n0->is_Call(), "expect a call here" ); 2121 } 2122 if( n0->is_Call() ) { 2123 if( n0->as_Call()->guaranteed_safepoint() ) 2124 return; 2125 } else if( n0->is_SafePoint() && n0->req() >= parms ) { 2126 return; 2127 } 2128 } 2129 2130 // Clear out dead values from the debug info. 2131 kill_dead_locals(); 2132 2133 // Clone the JVM State 2134 SafePointNode *sfpnt = new (C) SafePointNode(parms, NULL); 2135 2136 // Capture memory state BEFORE a SafePoint. Since we can block at a 2137 // SafePoint we need our GC state to be safe; i.e. we need all our current 2138 // write barriers (card marks) to not float down after the SafePoint so we 2139 // must read raw memory. Likewise we need all oop stores to match the card 2140 // marks. If deopt can happen, we need ALL stores (we need the correct JVM 2141 // state on a deopt). 2142 2143 // We do not need to WRITE the memory state after a SafePoint. The control 2144 // edge will keep card-marks and oop-stores from floating up from below a 2145 // SafePoint and our true dependency added here will keep them from floating 2146 // down below a SafePoint. 2147 2148 // Clone the current memory state 2149 Node* mem = MergeMemNode::make(C, map()->memory()); 2150 2151 mem = _gvn.transform(mem); 2152 2153 // Pass control through the safepoint 2154 sfpnt->init_req(TypeFunc::Control , control()); 2155 // Fix edges normally used by a call 2156 sfpnt->init_req(TypeFunc::I_O , top() ); 2157 sfpnt->init_req(TypeFunc::Memory , mem ); 2158 sfpnt->init_req(TypeFunc::ReturnAdr, top() ); 2159 sfpnt->init_req(TypeFunc::FramePtr , top() ); 2160 2161 // Create a node for the polling address 2162 if( add_poll_param ) { 2163 Node *polladr = ConPNode::make(C, (address)os::get_polling_page()); 2164 sfpnt->init_req(TypeFunc::Parms+0, _gvn.transform(polladr)); 2165 } 2166 2167 // Fix up the JVM State edges 2168 add_safepoint_edges(sfpnt); 2169 Node *transformed_sfpnt = _gvn.transform(sfpnt); 2170 set_control(transformed_sfpnt); 2171 2172 // Provide an edge from root to safepoint. This makes the safepoint 2173 // appear useful until the parse has completed. 2174 if( OptoRemoveUseless && transformed_sfpnt->is_SafePoint() ) { 2175 assert(C->root() != NULL, "Expect parse is still valid"); 2176 C->root()->add_prec(transformed_sfpnt); 2177 } 2178 } 2179 2180 #ifndef PRODUCT 2181 //------------------------show_parse_info-------------------------------------- 2182 void Parse::show_parse_info() { 2183 InlineTree* ilt = NULL; 2184 if (C->ilt() != NULL) { 2185 JVMState* caller_jvms = is_osr_parse() ? caller()->caller() : caller(); 2186 ilt = InlineTree::find_subtree_from_root(C->ilt(), caller_jvms, method()); 2187 } 2188 if (PrintCompilation && Verbose) { 2189 if (depth() == 1) { 2190 if( ilt->count_inlines() ) { 2191 tty->print(" __inlined %d (%d bytes)", ilt->count_inlines(), 2192 ilt->count_inline_bcs()); 2193 tty->cr(); 2194 } 2195 } else { 2196 if (method()->is_synchronized()) tty->print("s"); 2197 if (method()->has_exception_handlers()) tty->print("!"); 2198 // Check this is not the final compiled version 2199 if (C->trap_can_recompile()) { 2200 tty->print("-"); 2201 } else { 2202 tty->print(" "); 2203 } 2204 method()->print_short_name(); 2205 if (is_osr_parse()) { 2206 tty->print(" @ %d", osr_bci()); 2207 } 2208 tty->print(" (%d bytes)",method()->code_size()); 2209 if (ilt->count_inlines()) { 2210 tty->print(" __inlined %d (%d bytes)", ilt->count_inlines(), 2211 ilt->count_inline_bcs()); 2212 } 2213 tty->cr(); 2214 } 2215 } 2216 if (PrintOpto && (depth() == 1 || PrintOptoInlining)) { 2217 // Print that we succeeded; suppress this message on the first osr parse. 2218 2219 if (method()->is_synchronized()) tty->print("s"); 2220 if (method()->has_exception_handlers()) tty->print("!"); 2221 // Check this is not the final compiled version 2222 if (C->trap_can_recompile() && depth() == 1) { 2223 tty->print("-"); 2224 } else { 2225 tty->print(" "); 2226 } 2227 if( depth() != 1 ) { tty->print(" "); } // missing compile count 2228 for (int i = 1; i < depth(); ++i) { tty->print(" "); } 2229 method()->print_short_name(); 2230 if (is_osr_parse()) { 2231 tty->print(" @ %d", osr_bci()); 2232 } 2233 if (ilt->caller_bci() != -1) { 2234 tty->print(" @ %d", ilt->caller_bci()); 2235 } 2236 tty->print(" (%d bytes)",method()->code_size()); 2237 if (ilt->count_inlines()) { 2238 tty->print(" __inlined %d (%d bytes)", ilt->count_inlines(), 2239 ilt->count_inline_bcs()); 2240 } 2241 tty->cr(); 2242 } 2243 } 2244 2245 2246 //------------------------------dump------------------------------------------- 2247 // Dump information associated with the bytecodes of current _method 2248 void Parse::dump() { 2249 if( method() != NULL ) { 2250 // Iterate over bytecodes 2251 ciBytecodeStream iter(method()); 2252 for( Bytecodes::Code bc = iter.next(); bc != ciBytecodeStream::EOBC() ; bc = iter.next() ) { 2253 dump_bci( iter.cur_bci() ); 2254 tty->cr(); 2255 } 2256 } 2257 } 2258 2259 // Dump information associated with a byte code index, 'bci' 2260 void Parse::dump_bci(int bci) { 2261 // Output info on merge-points, cloning, and within _jsr..._ret 2262 // NYI 2263 tty->print(" bci:%d", bci); 2264 } 2265 2266 #endif