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