1 /* 2 * Copyright (c) 1999, 2025, 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 "ci/ciCallProfile.hpp" 26 #include "ci/ciExceptionHandler.hpp" 27 #include "ci/ciInstanceKlass.hpp" 28 #include "ci/ciMethod.hpp" 29 #include "ci/ciMethodBlocks.hpp" 30 #include "ci/ciMethodData.hpp" 31 #include "ci/ciReplay.hpp" 32 #include "ci/ciStreams.hpp" 33 #include "ci/ciSymbol.hpp" 34 #include "ci/ciSymbols.hpp" 35 #include "ci/ciUtilities.inline.hpp" 36 #include "compiler/abstractCompiler.hpp" 37 #include "compiler/compilerDefinitions.inline.hpp" 38 #include "compiler/compilerOracle.hpp" 39 #include "compiler/compileTask.hpp" 40 #include "compiler/methodLiveness.hpp" 41 #include "interpreter/interpreter.hpp" 42 #include "interpreter/linkResolver.hpp" 43 #include "interpreter/oopMapCache.hpp" 44 #include "logging/log.hpp" 45 #include "logging/logStream.hpp" 46 #include "memory/allocation.inline.hpp" 47 #include "memory/resourceArea.hpp" 48 #include "oops/generateOopMap.hpp" 49 #include "oops/method.inline.hpp" 50 #include "oops/oop.inline.hpp" 51 #include "oops/trainingData.hpp" 52 #include "prims/methodHandles.hpp" 53 #include "runtime/deoptimization.hpp" 54 #include "runtime/handles.inline.hpp" 55 #include "utilities/bitMap.inline.hpp" 56 #include "utilities/xmlstream.hpp" 57 #ifdef COMPILER2 58 #include "ci/bcEscapeAnalyzer.hpp" 59 #include "ci/ciTypeFlow.hpp" 60 #include "oops/method.hpp" 61 #endif 62 63 // ciMethod 64 // 65 // This class represents a Method* in the HotSpot virtual 66 // machine. 67 68 69 // ------------------------------------------------------------------ 70 // ciMethod::ciMethod 71 // 72 // Loaded method. 73 ciMethod::ciMethod(const methodHandle& h_m, ciInstanceKlass* holder) : 74 ciMetadata(h_m()), 75 _holder(holder) 76 { 77 assert(h_m() != nullptr, "no null method"); 78 assert(_holder->get_instanceKlass() == h_m->method_holder(), ""); 79 80 // These fields are always filled in in loaded methods. 81 _flags = ciFlags(h_m->access_flags()); 82 83 // Easy to compute, so fill them in now. 84 _max_stack = h_m->max_stack(); 85 _max_locals = h_m->max_locals(); 86 _code_size = h_m->code_size(); 87 _handler_count = h_m->exception_table_length(); 88 _size_of_parameters = h_m->size_of_parameters(); 89 _uses_monitors = h_m->has_monitor_bytecodes(); 90 _balanced_monitors = !_uses_monitors || h_m->guaranteed_monitor_matching(); 91 _is_c1_compilable = !h_m->is_not_c1_compilable(); 92 _is_c2_compilable = !h_m->is_not_c2_compilable(); 93 _can_be_parsed = true; 94 _has_reserved_stack_access = h_m->has_reserved_stack_access(); 95 _is_overpass = h_m->is_overpass(); 96 // Lazy fields, filled in on demand. Require allocation. 97 _code = nullptr; 98 _exception_handlers = nullptr; 99 _liveness = nullptr; 100 _method_blocks = nullptr; 101 #if defined(COMPILER2) 102 _flow = nullptr; 103 _bcea = nullptr; 104 #endif // COMPILER2 105 106 // Check for blackhole intrinsic and then populate the intrinsic ID. 107 CompilerOracle::tag_blackhole_if_possible(h_m); 108 _intrinsic_id = h_m->intrinsic_id(); 109 110 ciEnv *env = CURRENT_ENV; 111 if (env->jvmti_can_hotswap_or_post_breakpoint()) { 112 // 6328518 check hotswap conditions under the right lock. 113 bool should_take_Compile_lock = !Compile_lock->owned_by_self(); 114 ConditionalMutexLocker locker(Compile_lock, should_take_Compile_lock, Mutex::_safepoint_check_flag); 115 if (Dependencies::check_evol_method(h_m()) != nullptr) { 116 _is_c1_compilable = false; 117 _is_c2_compilable = false; 118 _can_be_parsed = false; 119 } 120 } else { 121 DEBUG_ONLY(CompilerThread::current()->check_possible_safepoint()); 122 } 123 124 if (h_m->method_holder()->is_linked()) { 125 _can_be_statically_bound = h_m->can_be_statically_bound(); 126 _can_omit_stack_trace = h_m->can_omit_stack_trace(); 127 } else { 128 // Have to use a conservative value in this case. 129 _can_be_statically_bound = false; 130 _can_omit_stack_trace = true; 131 } 132 133 // Adjust the definition of this condition to be more useful: 134 // %%% take these conditions into account in vtable generation 135 if (!_can_be_statically_bound && h_m->is_private()) 136 _can_be_statically_bound = true; 137 if (_can_be_statically_bound && h_m->is_abstract()) 138 _can_be_statically_bound = false; 139 140 // generating _signature may allow GC and therefore move m. 141 // These fields are always filled in. 142 _name = env->get_symbol(h_m->name()); 143 ciSymbol* sig_symbol = env->get_symbol(h_m->signature()); 144 constantPoolHandle cpool(Thread::current(), h_m->constants()); 145 _signature = new (env->arena()) ciSignature(_holder, cpool, sig_symbol); 146 _method_data = nullptr; 147 // Take a snapshot of these values, so they will be commensurate with the MDO. 148 if (ProfileInterpreter || CompilerConfig::is_c1_profiling()) { 149 int invcnt = h_m->interpreter_invocation_count(); 150 // if the value overflowed report it as max int 151 _interpreter_invocation_count = invcnt < 0 ? max_jint : invcnt ; 152 _interpreter_throwout_count = h_m->interpreter_throwout_count(); 153 } else { 154 _interpreter_invocation_count = 0; 155 _interpreter_throwout_count = 0; 156 } 157 if (_interpreter_invocation_count == 0) 158 _interpreter_invocation_count = 1; 159 _inline_instructions_size = -1; 160 if (ReplayCompiles) { 161 ciReplay::initialize(this); 162 } 163 } 164 165 166 // ------------------------------------------------------------------ 167 // ciMethod::ciMethod 168 // 169 // Unloaded method. 170 ciMethod::ciMethod(ciInstanceKlass* holder, 171 ciSymbol* name, 172 ciSymbol* signature, 173 ciInstanceKlass* accessor) : 174 ciMetadata((Metadata*)nullptr), 175 _name( name), 176 _holder( holder), 177 _method_data( nullptr), 178 _method_blocks( nullptr), 179 _intrinsic_id( vmIntrinsics::_none), 180 _inline_instructions_size(-1), 181 _can_be_statically_bound(false), 182 _can_omit_stack_trace(true), 183 _liveness( nullptr) 184 #if defined(COMPILER2) 185 , 186 _flow( nullptr), 187 _bcea( nullptr) 188 #endif // COMPILER2 189 { 190 // Usually holder and accessor are the same type but in some cases 191 // the holder has the wrong class loader (e.g. invokedynamic call 192 // sites) so we pass the accessor. 193 _signature = new (CURRENT_ENV->arena()) ciSignature(accessor, constantPoolHandle(), signature); 194 } 195 196 197 // ------------------------------------------------------------------ 198 // ciMethod::load_code 199 // 200 // Load the bytecodes and exception handler table for this method. 201 void ciMethod::load_code() { 202 VM_ENTRY_MARK; 203 assert(is_loaded(), "only loaded methods have code"); 204 205 Method* me = get_Method(); 206 Arena* arena = CURRENT_THREAD_ENV->arena(); 207 208 // Load the bytecodes. 209 _code = (address)arena->Amalloc(code_size()); 210 memcpy(_code, me->code_base(), code_size()); 211 212 #if INCLUDE_JVMTI 213 // Revert any breakpoint bytecodes in ci's copy 214 if (me->number_of_breakpoints() > 0) { 215 BreakpointInfo* bp = me->method_holder()->breakpoints(); 216 for (; bp != nullptr; bp = bp->next()) { 217 if (bp->match(me)) { 218 code_at_put(bp->bci(), bp->orig_bytecode()); 219 } 220 } 221 } 222 #endif 223 224 // And load the exception table. 225 ExceptionTable exc_table(me); 226 227 // Allocate one extra spot in our list of exceptions. This 228 // last entry will be used to represent the possibility that 229 // an exception escapes the method. See ciExceptionHandlerStream 230 // for details. 231 _exception_handlers = 232 (ciExceptionHandler**)arena->Amalloc(sizeof(ciExceptionHandler*) 233 * (_handler_count + 1)); 234 if (_handler_count > 0) { 235 for (int i=0; i<_handler_count; i++) { 236 _exception_handlers[i] = new (arena) ciExceptionHandler( 237 holder(), 238 /* start */ exc_table.start_pc(i), 239 /* limit */ exc_table.end_pc(i), 240 /* goto pc */ exc_table.handler_pc(i), 241 /* cp index */ exc_table.catch_type_index(i)); 242 } 243 } 244 245 // Put an entry at the end of our list to represent the possibility 246 // of exceptional exit. 247 _exception_handlers[_handler_count] = 248 new (arena) ciExceptionHandler(holder(), 0, code_size(), -1, 0); 249 250 if (CIPrintMethodCodes) { 251 print_codes(); 252 } 253 } 254 255 256 // ------------------------------------------------------------------ 257 // ciMethod::has_linenumber_table 258 // 259 // length unknown until decompression 260 bool ciMethod::has_linenumber_table() const { 261 check_is_loaded(); 262 VM_ENTRY_MARK; 263 return get_Method()->has_linenumber_table(); 264 } 265 266 267 // ------------------------------------------------------------------ 268 // ciMethod::line_number_from_bci 269 int ciMethod::line_number_from_bci(int bci) const { 270 check_is_loaded(); 271 VM_ENTRY_MARK; 272 return get_Method()->line_number_from_bci(bci); 273 } 274 275 276 // ------------------------------------------------------------------ 277 // ciMethod::vtable_index 278 // 279 // Get the position of this method's entry in the vtable, if any. 280 int ciMethod::vtable_index() { 281 check_is_loaded(); 282 assert(holder()->is_linked(), "must be linked"); 283 VM_ENTRY_MARK; 284 return get_Method()->vtable_index(); 285 } 286 287 // ------------------------------------------------------------------ 288 // ciMethod::uses_balanced_monitors 289 // 290 // Does this method use monitors in a strict stack-disciplined manner? 291 bool ciMethod::has_balanced_monitors() { 292 check_is_loaded(); 293 if (_balanced_monitors) return true; 294 295 // Analyze the method to see if monitors are used properly. 296 VM_ENTRY_MARK; 297 methodHandle method(THREAD, get_Method()); 298 assert(method->has_monitor_bytecodes(), "should have checked this"); 299 300 // Check to see if a previous compilation computed the 301 // monitor-matching analysis. 302 if (method->guaranteed_monitor_matching()) { 303 _balanced_monitors = true; 304 return true; 305 } 306 307 { 308 ExceptionMark em(THREAD); 309 ResourceMark rm(THREAD); 310 GeneratePairingInfo gpi(method); 311 if (!gpi.compute_map(THREAD)) { 312 fatal("Unrecoverable verification or out-of-memory error"); 313 } 314 if (!gpi.monitor_safe()) { 315 return false; 316 } 317 method->set_guaranteed_monitor_matching(); 318 _balanced_monitors = true; 319 } 320 return true; 321 } 322 323 324 // ------------------------------------------------------------------ 325 // ciMethod::get_flow_analysis 326 ciTypeFlow* ciMethod::get_flow_analysis() { 327 #if defined(COMPILER2) 328 if (_flow == nullptr) { 329 ciEnv* env = CURRENT_ENV; 330 _flow = new (env->arena()) ciTypeFlow(env, this); 331 _flow->do_flow(); 332 } 333 return _flow; 334 #else // COMPILER2 335 ShouldNotReachHere(); 336 return nullptr; 337 #endif // COMPILER2 338 } 339 340 341 // ------------------------------------------------------------------ 342 // ciMethod::get_osr_flow_analysis 343 ciTypeFlow* ciMethod::get_osr_flow_analysis(int osr_bci) { 344 #if defined(COMPILER2) 345 // OSR entry points are always place after a call bytecode of some sort 346 assert(osr_bci >= 0, "must supply valid OSR entry point"); 347 ciEnv* env = CURRENT_ENV; 348 ciTypeFlow* flow = new (env->arena()) ciTypeFlow(env, this, osr_bci); 349 flow->do_flow(); 350 return flow; 351 #else // COMPILER2 352 ShouldNotReachHere(); 353 return nullptr; 354 #endif // COMPILER2 355 } 356 357 // ------------------------------------------------------------------ 358 // ciMethod::raw_liveness_at_bci 359 // 360 // Which local variables are live at a specific bci? 361 MethodLivenessResult ciMethod::raw_liveness_at_bci(int bci) { 362 check_is_loaded(); 363 if (_liveness == nullptr) { 364 // Create the liveness analyzer. 365 Arena* arena = CURRENT_ENV->arena(); 366 _liveness = new (arena) MethodLiveness(arena, this); 367 _liveness->compute_liveness(); 368 } 369 return _liveness->get_liveness_at(bci); 370 } 371 372 // ------------------------------------------------------------------ 373 // ciMethod::liveness_at_bci 374 // 375 // Which local variables are live at a specific bci? When debugging 376 // will return true for all locals in some cases to improve debug 377 // information. 378 MethodLivenessResult ciMethod::liveness_at_bci(int bci) { 379 if (CURRENT_ENV->should_retain_local_variables() || DeoptimizeALot) { 380 // Keep all locals live for the user's edification and amusement. 381 MethodLivenessResult result(_max_locals); 382 result.set_range(0, _max_locals); 383 result.set_is_valid(); 384 return result; 385 } 386 return raw_liveness_at_bci(bci); 387 } 388 389 // ciMethod::live_local_oops_at_bci 390 // 391 // find all the live oops in the locals array for a particular bci 392 // Compute what the interpreter believes by using the interpreter 393 // oopmap generator. This is used as a double check during osr to 394 // guard against conservative result from MethodLiveness making us 395 // think a dead oop is live. MethodLiveness is conservative in the 396 // sense that it may consider locals to be live which cannot be live, 397 // like in the case where a local could contain an oop or a primitive 398 // along different paths. In that case the local must be dead when 399 // those paths merge. Since the interpreter's viewpoint is used when 400 // gc'ing an interpreter frame we need to use its viewpoint during 401 // OSR when loading the locals. 402 403 ResourceBitMap ciMethod::live_local_oops_at_bci(int bci) { 404 VM_ENTRY_MARK; 405 InterpreterOopMap mask; 406 OopMapCache::compute_one_oop_map(methodHandle(THREAD, get_Method()), bci, &mask); 407 int mask_size = max_locals(); 408 ResourceBitMap result(mask_size); 409 int i; 410 for (i = 0; i < mask_size ; i++ ) { 411 if (mask.is_oop(i)) result.set_bit(i); 412 } 413 return result; 414 } 415 416 417 #ifdef COMPILER1 418 // ------------------------------------------------------------------ 419 // ciMethod::bci_block_start 420 // 421 // Marks all bcis where a new basic block starts 422 const BitMap& ciMethod::bci_block_start() { 423 check_is_loaded(); 424 if (_liveness == nullptr) { 425 // Create the liveness analyzer. 426 Arena* arena = CURRENT_ENV->arena(); 427 _liveness = new (arena) MethodLiveness(arena, this); 428 _liveness->compute_liveness(); 429 } 430 431 return _liveness->get_bci_block_start(); 432 } 433 #endif // COMPILER1 434 435 436 // ------------------------------------------------------------------ 437 // ciMethod::check_overflow 438 // 439 // Check whether the profile counter is overflowed and adjust if true. 440 // For invoke* it will turn negative values into max_jint, 441 // and for checkcast/aastore/instanceof turn positive values into min_jint. 442 int ciMethod::check_overflow(int c, Bytecodes::Code code) { 443 switch (code) { 444 case Bytecodes::_aastore: // fall-through 445 case Bytecodes::_checkcast: // fall-through 446 case Bytecodes::_instanceof: { 447 if (VM_Version::profile_all_receivers_at_type_check()) { 448 return (c < 0 ? max_jint : c); // always non-negative 449 } 450 return (c > 0 ? min_jint : c); // always non-positive 451 } 452 default: { 453 assert(Bytecodes::is_invoke(code), "%s", Bytecodes::name(code)); 454 return (c < 0 ? max_jint : c); // always non-negative 455 } 456 } 457 } 458 459 460 // ------------------------------------------------------------------ 461 // ciMethod::call_profile_at_bci 462 // 463 // Get the ciCallProfile for the invocation of this method. 464 // Also reports receiver types for non-call type checks (if TypeProfileCasts). 465 ciCallProfile ciMethod::call_profile_at_bci(int bci) { 466 ResourceMark rm; 467 ciCallProfile result; 468 if (method_data() != nullptr && method_data()->is_mature()) { 469 ciProfileData* data = method_data()->bci_to_data(bci); 470 if (data != nullptr && data->is_CounterData()) { 471 // Every profiled call site has a counter. 472 int count = check_overflow(data->as_CounterData()->count(), java_code_at_bci(bci)); 473 474 if (!data->is_ReceiverTypeData()) { 475 result._receiver_count[0] = 0; // that's a definite zero 476 } else { // ReceiverTypeData is a subclass of CounterData 477 ciReceiverTypeData* call = (ciReceiverTypeData*)data->as_ReceiverTypeData(); 478 // In addition, virtual call sites have receiver type information 479 int receivers_count_total = 0; 480 int morphism = 0; 481 // Precompute morphism for the possible fixup 482 for (uint i = 0; i < call->row_limit(); i++) { 483 ciKlass* receiver = call->receiver(i); 484 if (receiver == nullptr) continue; 485 morphism++; 486 } 487 int epsilon = 0; 488 // For a call, it is assumed that either the type of the receiver(s) 489 // is recorded or an associated counter is incremented, but not both. With 490 // tiered compilation, however, both can happen due to the interpreter and 491 // C1 profiling invocations differently. Address that inconsistency here. 492 if (morphism == 1 && count > 0) { 493 epsilon = count; 494 count = 0; 495 } 496 for (uint i = 0; i < call->row_limit(); i++) { 497 ciKlass* receiver = call->receiver(i); 498 if (receiver == nullptr) continue; 499 int rcount = saturated_add(call->receiver_count(i), epsilon); 500 if (rcount == 0) rcount = 1; // Should be valid value 501 receivers_count_total = saturated_add(receivers_count_total, rcount); 502 // Add the receiver to result data. 503 result.add_receiver(receiver, rcount); 504 // If we extend profiling to record methods, 505 // we will set result._method also. 506 } 507 // Determine call site's morphism. 508 // The call site count is 0 with known morphism (only 1 or 2 receivers) 509 // or < 0 in the case of a type check failure for checkcast, aastore, instanceof. 510 // The call site count is > 0 in the case of a polymorphic virtual call. 511 if (morphism > 0 && morphism == result._limit) { 512 // The morphism <= MorphismLimit. 513 if ((morphism < ciCallProfile::MorphismLimit) || 514 (morphism == ciCallProfile::MorphismLimit && count == 0)) { 515 #ifdef ASSERT 516 if (count > 0) { 517 this->print_short_name(tty); 518 tty->print_cr(" @ bci:%d", bci); 519 this->print_codes(); 520 assert(false, "this call site should not be polymorphic"); 521 } 522 #endif 523 result._morphism = morphism; 524 } 525 } 526 // Make the count consistent if this is a call profile. If count is 527 // zero or less, presume that this is a typecheck profile and 528 // do nothing. Otherwise, increase count to be the sum of all 529 // receiver's counts. 530 if (count >= 0) { 531 count = saturated_add(count, receivers_count_total); 532 } 533 } 534 result._count = count; 535 } 536 } 537 return result; 538 } 539 540 // ------------------------------------------------------------------ 541 // Add new receiver and sort data by receiver's profile count. 542 void ciCallProfile::add_receiver(ciKlass* receiver, int receiver_count) { 543 // Add new receiver and sort data by receiver's counts when we have space 544 // for it otherwise replace the less called receiver (less called receiver 545 // is placed to the last array element which is not used). 546 // First array's element contains most called receiver. 547 int i = _limit; 548 for (; i > 0 && receiver_count > _receiver_count[i-1]; i--) { 549 _receiver[i] = _receiver[i-1]; 550 _receiver_count[i] = _receiver_count[i-1]; 551 } 552 _receiver[i] = receiver; 553 _receiver_count[i] = receiver_count; 554 if (_limit < MorphismLimit) _limit++; 555 } 556 557 558 void ciMethod::assert_virtual_call_type_ok(int bci) { 559 assert(java_code_at_bci(bci) == Bytecodes::_invokevirtual || 560 java_code_at_bci(bci) == Bytecodes::_invokeinterface, "unexpected bytecode %s", Bytecodes::name(java_code_at_bci(bci))); 561 } 562 563 void ciMethod::assert_call_type_ok(int bci) { 564 assert(java_code_at_bci(bci) == Bytecodes::_invokestatic || 565 java_code_at_bci(bci) == Bytecodes::_invokespecial || 566 java_code_at_bci(bci) == Bytecodes::_invokedynamic, "unexpected bytecode %s", Bytecodes::name(java_code_at_bci(bci))); 567 } 568 569 /** 570 * Check whether profiling provides a type for the argument i to the 571 * call at bci bci 572 * 573 * @param [in]bci bci of the call 574 * @param [in]i argument number 575 * @param [out]type profiled type of argument, null if none 576 * @param [out]ptr_kind whether always null, never null or maybe null 577 * @return true if profiling exists 578 * 579 */ 580 bool ciMethod::argument_profiled_type(int bci, int i, ciKlass*& type, ProfilePtrKind& ptr_kind) { 581 if (MethodData::profile_parameters() && method_data() != nullptr && method_data()->is_mature()) { 582 ciProfileData* data = method_data()->bci_to_data(bci); 583 if (data != nullptr) { 584 if (data->is_VirtualCallTypeData()) { 585 assert_virtual_call_type_ok(bci); 586 ciVirtualCallTypeData* call = (ciVirtualCallTypeData*)data->as_VirtualCallTypeData(); 587 if (i >= call->number_of_arguments()) { 588 return false; 589 } 590 type = call->valid_argument_type(i); 591 ptr_kind = call->argument_ptr_kind(i); 592 return true; 593 } else if (data->is_CallTypeData()) { 594 assert_call_type_ok(bci); 595 ciCallTypeData* call = (ciCallTypeData*)data->as_CallTypeData(); 596 if (i >= call->number_of_arguments()) { 597 return false; 598 } 599 type = call->valid_argument_type(i); 600 ptr_kind = call->argument_ptr_kind(i); 601 return true; 602 } 603 } 604 } 605 return false; 606 } 607 608 /** 609 * Check whether profiling provides a type for the return value from 610 * the call at bci bci 611 * 612 * @param [in]bci bci of the call 613 * @param [out]type profiled type of argument, null if none 614 * @param [out]ptr_kind whether always null, never null or maybe null 615 * @return true if profiling exists 616 * 617 */ 618 bool ciMethod::return_profiled_type(int bci, ciKlass*& type, ProfilePtrKind& ptr_kind) { 619 if (MethodData::profile_return() && method_data() != nullptr && method_data()->is_mature()) { 620 ciProfileData* data = method_data()->bci_to_data(bci); 621 if (data != nullptr) { 622 if (data->is_VirtualCallTypeData()) { 623 assert_virtual_call_type_ok(bci); 624 ciVirtualCallTypeData* call = (ciVirtualCallTypeData*)data->as_VirtualCallTypeData(); 625 if (call->has_return()) { 626 type = call->valid_return_type(); 627 ptr_kind = call->return_ptr_kind(); 628 return true; 629 } 630 } else if (data->is_CallTypeData()) { 631 assert_call_type_ok(bci); 632 ciCallTypeData* call = (ciCallTypeData*)data->as_CallTypeData(); 633 if (call->has_return()) { 634 type = call->valid_return_type(); 635 ptr_kind = call->return_ptr_kind(); 636 } 637 return true; 638 } 639 } 640 } 641 return false; 642 } 643 644 /** 645 * Check whether profiling provides a type for the parameter i 646 * 647 * @param [in]i parameter number 648 * @param [out]type profiled type of parameter, null if none 649 * @param [out]ptr_kind whether always null, never null or maybe null 650 * @return true if profiling exists 651 * 652 */ 653 bool ciMethod::parameter_profiled_type(int i, ciKlass*& type, ProfilePtrKind& ptr_kind) { 654 if (MethodData::profile_parameters() && method_data() != nullptr && method_data()->is_mature()) { 655 ciParametersTypeData* parameters = method_data()->parameters_type_data(); 656 if (parameters != nullptr && i < parameters->number_of_parameters()) { 657 type = parameters->valid_parameter_type(i); 658 ptr_kind = parameters->parameter_ptr_kind(i); 659 return true; 660 } 661 } 662 return false; 663 } 664 665 666 // ------------------------------------------------------------------ 667 // ciMethod::find_monomorphic_target 668 // 669 // Given a certain calling environment, find the monomorphic target 670 // for the call. Return null if the call is not monomorphic in 671 // its calling environment, or if there are only abstract methods. 672 // The returned method is never abstract. 673 // Note: If caller uses a non-null result, it must inform dependencies 674 // via assert_unique_concrete_method or assert_leaf_type. 675 ciMethod* ciMethod::find_monomorphic_target(ciInstanceKlass* caller, 676 ciInstanceKlass* callee_holder, 677 ciInstanceKlass* actual_recv, 678 bool check_access) { 679 check_is_loaded(); 680 681 if (actual_recv->is_interface()) { 682 // %%% We cannot trust interface types, yet. See bug 6312651. 683 return nullptr; 684 } 685 686 ciMethod* root_m = resolve_invoke(caller, actual_recv, check_access, true /* allow_abstract */); 687 if (root_m == nullptr) { 688 // Something went wrong looking up the actual receiver method. 689 return nullptr; 690 } 691 692 // Make certain quick checks even if UseCHA is false. 693 694 // Is it private or final? 695 if (root_m->can_be_statically_bound()) { 696 assert(!root_m->is_abstract(), "sanity"); 697 return root_m; 698 } 699 700 if (actual_recv->is_leaf_type() && actual_recv == root_m->holder()) { 701 // Easy case. There is no other place to put a method, so don't bother 702 // to go through the VM_ENTRY_MARK and all the rest. 703 if (root_m->is_abstract()) { 704 return nullptr; 705 } 706 return root_m; 707 } 708 709 // Array methods (clone, hashCode, etc.) are always statically bound. 710 // If we were to see an array type here, we'd return root_m. 711 // However, this method processes only ciInstanceKlasses. (See 4962591.) 712 // The inline_native_clone intrinsic narrows Object to T[] properly, 713 // so there is no need to do the same job here. 714 715 if (!UseCHA) return nullptr; 716 717 VM_ENTRY_MARK; 718 719 methodHandle target; 720 { 721 MutexLocker locker(Compile_lock); 722 InstanceKlass* context = actual_recv->get_instanceKlass(); 723 target = methodHandle(THREAD, Dependencies::find_unique_concrete_method(context, 724 root_m->get_Method(), 725 callee_holder->get_Klass(), 726 this->get_Method())); 727 assert(target() == nullptr || !target()->is_abstract(), "not allowed"); 728 // %%% Should upgrade this ciMethod API to look for 1 or 2 concrete methods. 729 } 730 731 #ifndef PRODUCT 732 LogTarget(Debug, dependencies) lt; 733 if (lt.is_enabled() && target() != nullptr && target() != root_m->get_Method()) { 734 LogStream ls(<); 735 ls.print("found a non-root unique target method"); 736 ls.print_cr(" context = %s", actual_recv->get_Klass()->external_name()); 737 ls.print(" method = "); 738 target->print_short_name(&ls); 739 ls.cr(); 740 } 741 #endif //PRODUCT 742 743 if (target() == nullptr) { 744 return nullptr; 745 } 746 747 // Redefinition support. 748 if (this->is_old() || root_m->is_old() || target->is_old()) { 749 guarantee(CURRENT_THREAD_ENV->jvmti_state_changed(), "old method not detected"); 750 return nullptr; 751 } 752 753 if (target() == root_m->get_Method()) { 754 return root_m; 755 } 756 if (!root_m->is_public() && 757 !root_m->is_protected()) { 758 // If we are going to reason about inheritance, it's easiest 759 // if the method in question is public, protected, or private. 760 // If the answer is not root_m, it is conservatively correct 761 // to return null, even if the CHA encountered irrelevant 762 // methods in other packages. 763 // %%% TO DO: Work out logic for package-private methods 764 // with the same name but different vtable indexes. 765 return nullptr; 766 } 767 return CURRENT_THREAD_ENV->get_method(target()); 768 } 769 770 // ------------------------------------------------------------------ 771 // ciMethod::can_be_statically_bound 772 // 773 // Tries to determine whether a method can be statically bound in some context. 774 bool ciMethod::can_be_statically_bound(ciInstanceKlass* context) const { 775 return (holder() == context) && can_be_statically_bound(); 776 } 777 778 // ------------------------------------------------------------------ 779 // ciMethod::can_omit_stack_trace 780 // 781 // Tries to determine whether a method can omit stack trace in throw in compiled code. 782 bool ciMethod::can_omit_stack_trace() const { 783 if (!StackTraceInThrowable) { 784 return true; // stack trace is switched off. 785 } 786 if (!OmitStackTraceInFastThrow) { 787 return false; // Have to provide stack trace. 788 } 789 return _can_omit_stack_trace; 790 } 791 792 // ------------------------------------------------------------------ 793 // ciMethod::resolve_invoke 794 // 795 // Given a known receiver klass, find the target for the call. 796 // Return null if the call has no target or the target is abstract. 797 ciMethod* ciMethod::resolve_invoke(ciKlass* caller, ciKlass* exact_receiver, bool check_access, bool allow_abstract) { 798 check_is_loaded(); 799 VM_ENTRY_MARK; 800 801 Klass* caller_klass = caller->get_Klass(); 802 Klass* recv = exact_receiver->get_Klass(); 803 Klass* resolved = holder()->get_Klass(); 804 Symbol* h_name = name()->get_symbol(); 805 Symbol* h_signature = signature()->get_symbol(); 806 807 LinkInfo link_info(resolved, h_name, h_signature, caller_klass, 808 check_access ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip, 809 check_access ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip); 810 Method* m = nullptr; 811 // Only do exact lookup if receiver klass has been linked. Otherwise, 812 // the vtable has not been setup, and the LinkResolver will fail. 813 if (recv->is_array_klass() 814 || 815 (InstanceKlass::cast(recv)->is_linked() && !exact_receiver->is_interface())) { 816 if (holder()->is_interface()) { 817 m = LinkResolver::resolve_interface_call_or_null(recv, link_info); 818 } else { 819 m = LinkResolver::resolve_virtual_call_or_null(recv, link_info); 820 } 821 } 822 823 if (m == nullptr) { 824 // Return null only if there was a problem with lookup (uninitialized class, etc.) 825 return nullptr; 826 } 827 828 ciMethod* result = this; 829 if (m != get_Method()) { 830 // Redefinition support. 831 if (this->is_old() || m->is_old()) { 832 guarantee(CURRENT_THREAD_ENV->jvmti_state_changed(), "old method not detected"); 833 return nullptr; 834 } 835 836 result = CURRENT_THREAD_ENV->get_method(m); 837 } 838 839 if (result->is_abstract() && !allow_abstract) { 840 // Don't return abstract methods because they aren't optimizable or interesting. 841 return nullptr; 842 } 843 return result; 844 } 845 846 // ------------------------------------------------------------------ 847 // ciMethod::resolve_vtable_index 848 // 849 // Given a known receiver klass, find the vtable index for the call. 850 // Return Method::invalid_vtable_index if the vtable_index is unknown. 851 int ciMethod::resolve_vtable_index(ciKlass* caller, ciKlass* receiver) { 852 check_is_loaded(); 853 854 int vtable_index = Method::invalid_vtable_index; 855 // Only do lookup if receiver klass has been linked. Otherwise, 856 // the vtable has not been setup, and the LinkResolver will fail. 857 if (!receiver->is_interface() 858 && (!receiver->is_instance_klass() || 859 receiver->as_instance_klass()->is_linked())) { 860 VM_ENTRY_MARK; 861 862 Klass* caller_klass = caller->get_Klass(); 863 Klass* recv = receiver->get_Klass(); 864 Symbol* h_name = name()->get_symbol(); 865 Symbol* h_signature = signature()->get_symbol(); 866 867 LinkInfo link_info(recv, h_name, h_signature, caller_klass); 868 vtable_index = LinkResolver::resolve_virtual_vtable_index(recv, link_info); 869 if (vtable_index == Method::nonvirtual_vtable_index) { 870 // A statically bound method. Return "no such index". 871 vtable_index = Method::invalid_vtable_index; 872 } 873 } 874 875 return vtable_index; 876 } 877 878 // ------------------------------------------------------------------ 879 // ciMethod::get_field_at_bci 880 ciField* ciMethod::get_field_at_bci(int bci, bool &will_link) { 881 ciBytecodeStream iter(this); 882 iter.reset_to_bci(bci); 883 iter.next(); 884 return iter.get_field(will_link); 885 } 886 887 // ------------------------------------------------------------------ 888 // ciMethod::get_method_at_bci 889 ciMethod* ciMethod::get_method_at_bci(int bci, bool &will_link, ciSignature* *declared_signature) { 890 ciBytecodeStream iter(this); 891 iter.reset_to_bci(bci); 892 iter.next(); 893 return iter.get_method(will_link, declared_signature); 894 } 895 896 // ------------------------------------------------------------------ 897 ciKlass* ciMethod::get_declared_method_holder_at_bci(int bci) { 898 ciBytecodeStream iter(this); 899 iter.reset_to_bci(bci); 900 iter.next(); 901 return iter.get_declared_method_holder(); 902 } 903 904 // ------------------------------------------------------------------ 905 // Adjust a CounterData count to be commensurate with 906 // interpreter_invocation_count. If the MDO exists for 907 // only 25% of the time the method exists, then the 908 // counts in the MDO should be scaled by 4X, so that 909 // they can be usefully and stably compared against the 910 // invocation counts in methods. 911 int ciMethod::scale_count(int count, float prof_factor) { 912 if (count > 0 && method_data() != nullptr) { 913 int counter_life = method_data()->invocation_count(); 914 int method_life = interpreter_invocation_count(); 915 if (method_life < counter_life) { // may happen because of the snapshot timing 916 method_life = counter_life; 917 } 918 if (counter_life > 0) { 919 double count_d = (double)count * prof_factor * method_life / counter_life + 0.5; 920 if (count_d >= static_cast<double>(INT_MAX)) { 921 // Clamp in case of overflowing int range. 922 count = INT_MAX; 923 } else { 924 count = int(count_d); 925 count = (count > 0) ? count : 1; 926 } 927 } else { 928 count = 1; 929 } 930 } 931 return count; 932 } 933 934 935 // ------------------------------------------------------------------ 936 // ciMethod::is_special_get_caller_class_method 937 // 938 bool ciMethod::is_ignored_by_security_stack_walk() const { 939 check_is_loaded(); 940 VM_ENTRY_MARK; 941 return get_Method()->is_ignored_by_security_stack_walk(); 942 } 943 944 // ------------------------------------------------------------------ 945 // ciMethod::needs_clinit_barrier 946 // 947 bool ciMethod::needs_clinit_barrier() const { 948 check_is_loaded(); 949 return is_static() && !holder()->is_initialized(); 950 } 951 952 // ------------------------------------------------------------------ 953 // invokedynamic support 954 955 // ------------------------------------------------------------------ 956 // ciMethod::is_method_handle_intrinsic 957 // 958 // Return true if the method is an instance of the JVM-generated 959 // signature-polymorphic MethodHandle methods, _invokeBasic, _linkToVirtual, etc. 960 bool ciMethod::is_method_handle_intrinsic() const { 961 vmIntrinsics::ID iid = _intrinsic_id; // do not check if loaded 962 return (MethodHandles::is_signature_polymorphic(iid) && 963 MethodHandles::is_signature_polymorphic_intrinsic(iid)); 964 } 965 966 // ------------------------------------------------------------------ 967 // ciMethod::is_compiled_lambda_form 968 // 969 // Return true if the method is a generated MethodHandle adapter. 970 // These are built by Java code. 971 bool ciMethod::is_compiled_lambda_form() const { 972 vmIntrinsics::ID iid = _intrinsic_id; // do not check if loaded 973 return iid == vmIntrinsics::_compiledLambdaForm; 974 } 975 976 // ------------------------------------------------------------------ 977 // ciMethod::is_object_initializer 978 // 979 bool ciMethod::is_object_initializer() const { 980 return name() == ciSymbols::object_initializer_name(); 981 } 982 983 // ------------------------------------------------------------------ 984 // ciMethod::is_scoped 985 // 986 // Return true for methods annotated with @Scoped 987 bool ciMethod::is_scoped() const { 988 return get_Method()->is_scoped(); 989 } 990 991 // ------------------------------------------------------------------ 992 // ciMethod::has_member_arg 993 // 994 // Return true if the method is a linker intrinsic like _linkToVirtual. 995 // These are built by the JVM. 996 bool ciMethod::has_member_arg() const { 997 vmIntrinsics::ID iid = _intrinsic_id; // do not check if loaded 998 return (MethodHandles::is_signature_polymorphic(iid) && 999 MethodHandles::has_member_arg(iid)); 1000 } 1001 1002 // ------------------------------------------------------------------ 1003 // ciMethod::ensure_method_data 1004 // 1005 // Generate new MethodData* objects at compile time. 1006 // Return true if allocation was successful or no MDO is required. 1007 bool ciMethod::ensure_method_data(const methodHandle& h_m) { 1008 EXCEPTION_CONTEXT; 1009 if (is_native() || is_abstract() || h_m()->is_accessor()) { 1010 return true; 1011 } 1012 if (h_m()->method_data() == nullptr) { 1013 Method::build_profiling_method_data(h_m, THREAD); 1014 if (HAS_PENDING_EXCEPTION) { 1015 CLEAR_PENDING_EXCEPTION; 1016 } 1017 } 1018 if (h_m()->method_data() != nullptr) { 1019 _method_data = CURRENT_ENV->get_method_data(h_m()->method_data()); 1020 return _method_data->load_data(); 1021 } else { 1022 _method_data = CURRENT_ENV->get_empty_methodData(); 1023 return false; 1024 } 1025 } 1026 1027 // public, retroactive version 1028 bool ciMethod::ensure_method_data() { 1029 bool result = true; 1030 if (_method_data == nullptr || _method_data->is_empty()) { 1031 GUARDED_VM_ENTRY({ 1032 methodHandle mh(Thread::current(), get_Method()); 1033 result = ensure_method_data(mh); 1034 }); 1035 } 1036 return result; 1037 } 1038 1039 1040 // ------------------------------------------------------------------ 1041 // ciMethod::method_data 1042 // 1043 ciMethodData* ciMethod::method_data() { 1044 if (_method_data != nullptr) { 1045 return _method_data; 1046 } 1047 VM_ENTRY_MARK; 1048 ciEnv* env = CURRENT_ENV; 1049 Thread* my_thread = JavaThread::current(); 1050 methodHandle h_m(my_thread, get_Method()); 1051 1052 if (h_m()->method_data() != nullptr) { 1053 _method_data = CURRENT_ENV->get_method_data(h_m()->method_data()); 1054 _method_data->load_data(); 1055 } else { 1056 _method_data = CURRENT_ENV->get_empty_methodData(); 1057 } 1058 return _method_data; 1059 1060 } 1061 1062 // ------------------------------------------------------------------ 1063 // ciMethod::method_data_or_null 1064 // Returns a pointer to ciMethodData if MDO exists on the VM side, 1065 // null otherwise. 1066 ciMethodData* ciMethod::method_data_or_null() { 1067 ciMethodData *md = method_data(); 1068 if (md->is_empty()) { 1069 return nullptr; 1070 } 1071 return md; 1072 } 1073 1074 // ------------------------------------------------------------------ 1075 // ciMethod::ensure_method_counters 1076 // 1077 MethodCounters* ciMethod::ensure_method_counters() { 1078 check_is_loaded(); 1079 VM_ENTRY_MARK; 1080 methodHandle mh(THREAD, get_Method()); 1081 MethodCounters* method_counters = mh->get_method_counters(CHECK_NULL); 1082 return method_counters; 1083 } 1084 1085 // ------------------------------------------------------------------ 1086 // ciMethod::has_option 1087 // 1088 bool ciMethod::has_option(CompileCommandEnum option) { 1089 check_is_loaded(); 1090 VM_ENTRY_MARK; 1091 methodHandle mh(THREAD, get_Method()); 1092 return CompilerOracle::has_option(mh, option); 1093 } 1094 1095 // ------------------------------------------------------------------ 1096 // ciMethod::has_option_value 1097 // 1098 bool ciMethod::has_option_value(CompileCommandEnum option, double& value) { 1099 check_is_loaded(); 1100 VM_ENTRY_MARK; 1101 methodHandle mh(THREAD, get_Method()); 1102 return CompilerOracle::has_option_value(mh, option, value); 1103 } 1104 // ------------------------------------------------------------------ 1105 // ciMethod::can_be_compiled 1106 // 1107 // Have previous compilations of this method succeeded? 1108 bool ciMethod::can_be_compiled() { 1109 check_is_loaded(); 1110 ciEnv* env = CURRENT_ENV; 1111 if (is_c1_compile(env->comp_level())) { 1112 return _is_c1_compilable; 1113 } 1114 return _is_c2_compilable; 1115 } 1116 1117 // ------------------------------------------------------------------ 1118 // ciMethod::has_compiled_code 1119 bool ciMethod::has_compiled_code() { 1120 return inline_instructions_size() > 0; 1121 } 1122 1123 int ciMethod::highest_osr_comp_level() { 1124 check_is_loaded(); 1125 VM_ENTRY_MARK; 1126 return get_Method()->highest_osr_comp_level(); 1127 } 1128 1129 // ------------------------------------------------------------------ 1130 // ciMethod::code_size_for_inlining 1131 // 1132 // Code size for inlining decisions. This method returns a code 1133 // size of 1 for methods which has the ForceInline annotation. 1134 int ciMethod::code_size_for_inlining() { 1135 check_is_loaded(); 1136 if (get_Method()->force_inline()) { 1137 return 1; 1138 } 1139 return code_size(); 1140 } 1141 1142 // ------------------------------------------------------------------ 1143 // ciMethod::inline_instructions_size 1144 // 1145 // This is a rough metric for "fat" methods, compared before inlining 1146 // with InlineSmallCode. The CodeBlob::code_size accessor includes 1147 // junk like exception handler, stubs, and constant table, which are 1148 // not highly relevant to an inlined method. So we use the more 1149 // specific accessor nmethod::insts_size. 1150 // Also some instructions inside the code are excluded from inline 1151 // heuristic (e.g. post call nop instructions; see InlineSkippedInstructionsCounter) 1152 int ciMethod::inline_instructions_size() { 1153 if (_inline_instructions_size == -1) { 1154 if (TrainingData::have_data()) { 1155 GUARDED_VM_ENTRY( 1156 CompLevel level = static_cast<CompLevel>(CURRENT_ENV->comp_level()); 1157 methodHandle top_level_mh(Thread::current(), CURRENT_ENV->task()->method()); 1158 MethodTrainingData* mtd = MethodTrainingData::find(top_level_mh); 1159 if (mtd != nullptr) { 1160 CompileTrainingData* ctd = mtd->last_toplevel_compile(level); 1161 if (ctd != nullptr) { 1162 methodHandle mh(Thread::current(), get_Method()); 1163 MethodTrainingData* this_mtd = MethodTrainingData::find(mh); 1164 if (this_mtd != nullptr) { 1165 auto r = ctd->ci_records().ciMethod__inline_instructions_size.find(this_mtd); 1166 if (r.is_valid()) { 1167 _inline_instructions_size = r.result(); 1168 } 1169 } 1170 } 1171 } 1172 ); 1173 } 1174 } 1175 if (_inline_instructions_size == -1) { 1176 GUARDED_VM_ENTRY( 1177 nmethod* code = get_Method()->code(); 1178 if (code != nullptr && (code->comp_level() == CompLevel_full_optimization)) { 1179 int isize = code->insts_end() - code->verified_entry_point() - code->skipped_instructions_size(); 1180 _inline_instructions_size = isize > 0 ? isize : 0; 1181 } else { 1182 _inline_instructions_size = 0; 1183 } 1184 if (TrainingData::need_data()) { 1185 CompileTrainingData* ctd = CURRENT_ENV->task()->training_data(); 1186 if (ctd != nullptr) { 1187 methodHandle mh(Thread::current(), get_Method()); 1188 MethodTrainingData* this_mtd = MethodTrainingData::make(mh); 1189 ctd->ci_records().ciMethod__inline_instructions_size.append_if_missing(_inline_instructions_size, this_mtd); 1190 } 1191 } 1192 ); 1193 } 1194 return _inline_instructions_size; 1195 } 1196 1197 // ------------------------------------------------------------------ 1198 // ciMethod::log_nmethod_identity 1199 void ciMethod::log_nmethod_identity(xmlStream* log) { 1200 GUARDED_VM_ENTRY( 1201 nmethod* code = get_Method()->code(); 1202 if (code != nullptr) { 1203 code->log_identity(log); 1204 } 1205 ) 1206 } 1207 1208 // ------------------------------------------------------------------ 1209 // ciMethod::is_not_reached 1210 bool ciMethod::is_not_reached(int bci) { 1211 check_is_loaded(); 1212 VM_ENTRY_MARK; 1213 return Interpreter::is_not_reached( 1214 methodHandle(THREAD, get_Method()), bci); 1215 } 1216 1217 // ------------------------------------------------------------------ 1218 // ciMethod::was_never_executed 1219 bool ciMethod::was_executed_more_than(int times) { 1220 VM_ENTRY_MARK; 1221 return get_Method()->was_executed_more_than(times); 1222 } 1223 1224 // ------------------------------------------------------------------ 1225 // ciMethod::has_unloaded_classes_in_signature 1226 bool ciMethod::has_unloaded_classes_in_signature() { 1227 // ciSignature is resolved against some accessing class and 1228 // signature classes aren't required to be local. As a benefit, 1229 // it makes signature classes visible through loader constraints. 1230 // So, encountering an unloaded class signals it is absent both in 1231 // the callee (local) and caller contexts. 1232 return signature()->has_unloaded_classes(); 1233 } 1234 1235 // ------------------------------------------------------------------ 1236 // ciMethod::is_klass_loaded 1237 bool ciMethod::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const { 1238 VM_ENTRY_MARK; 1239 return get_Method()->is_klass_loaded(refinfo_index, bc, must_be_resolved); 1240 } 1241 1242 // ------------------------------------------------------------------ 1243 // ciMethod::check_call 1244 bool ciMethod::check_call(int refinfo_index, bool is_static) const { 1245 // This method is used only in C2 from InlineTree::ok_to_inline, 1246 // and is only used under -Xcomp. 1247 // It appears to fail when applied to an invokeinterface call site. 1248 // FIXME: Remove this method and resolve_method_statically; refactor to use the other LinkResolver entry points. 1249 VM_ENTRY_MARK; 1250 { 1251 ExceptionMark em(THREAD); 1252 HandleMark hm(THREAD); 1253 constantPoolHandle pool (THREAD, get_Method()->constants()); 1254 Bytecodes::Code code = (is_static ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual); 1255 Method* spec_method = LinkResolver::resolve_method_statically(code, pool, refinfo_index, THREAD); 1256 if (HAS_PENDING_EXCEPTION) { 1257 CLEAR_PENDING_EXCEPTION; 1258 return false; 1259 } else { 1260 return (spec_method->is_static() == is_static); 1261 } 1262 } 1263 return false; 1264 } 1265 // ------------------------------------------------------------------ 1266 // ciMethod::print_codes 1267 // 1268 // Print the bytecodes for this method. 1269 void ciMethod::print_codes_on(outputStream* st) { 1270 check_is_loaded(); 1271 GUARDED_VM_ENTRY(get_Method()->print_codes_on(st);) 1272 } 1273 1274 1275 #define FETCH_FLAG_FROM_VM(flag_accessor) { \ 1276 check_is_loaded(); \ 1277 VM_ENTRY_MARK; \ 1278 return get_Method()->flag_accessor(); \ 1279 } 1280 1281 bool ciMethod::has_loops () const { FETCH_FLAG_FROM_VM(has_loops); } 1282 bool ciMethod::has_jsrs () const { FETCH_FLAG_FROM_VM(has_jsrs); } 1283 bool ciMethod::is_getter () const { FETCH_FLAG_FROM_VM(is_getter); } 1284 bool ciMethod::is_setter () const { FETCH_FLAG_FROM_VM(is_setter); } 1285 bool ciMethod::is_accessor () const { FETCH_FLAG_FROM_VM(is_accessor); } 1286 bool ciMethod::is_empty () const { FETCH_FLAG_FROM_VM(is_empty_method); } 1287 1288 bool ciMethod::is_boxing_method() const { 1289 if (intrinsic_id() != vmIntrinsics::_none && holder()->is_box_klass()) { 1290 switch (intrinsic_id()) { 1291 case vmIntrinsics::_Boolean_valueOf: 1292 case vmIntrinsics::_Byte_valueOf: 1293 case vmIntrinsics::_Character_valueOf: 1294 case vmIntrinsics::_Short_valueOf: 1295 case vmIntrinsics::_Integer_valueOf: 1296 case vmIntrinsics::_Long_valueOf: 1297 case vmIntrinsics::_Float_valueOf: 1298 case vmIntrinsics::_Double_valueOf: 1299 return true; 1300 default: 1301 return false; 1302 } 1303 } 1304 return false; 1305 } 1306 1307 bool ciMethod::is_unboxing_method() const { 1308 if (intrinsic_id() != vmIntrinsics::_none && holder()->is_box_klass()) { 1309 switch (intrinsic_id()) { 1310 case vmIntrinsics::_booleanValue: 1311 case vmIntrinsics::_byteValue: 1312 case vmIntrinsics::_charValue: 1313 case vmIntrinsics::_shortValue: 1314 case vmIntrinsics::_intValue: 1315 case vmIntrinsics::_longValue: 1316 case vmIntrinsics::_floatValue: 1317 case vmIntrinsics::_doubleValue: 1318 return true; 1319 default: 1320 return false; 1321 } 1322 } 1323 return false; 1324 } 1325 1326 bool ciMethod::is_vector_method() const { 1327 return (holder() == ciEnv::current()->vector_VectorSupport_klass()) && 1328 (intrinsic_id() != vmIntrinsics::_none); 1329 } 1330 1331 BCEscapeAnalyzer *ciMethod::get_bcea() { 1332 #ifdef COMPILER2 1333 if (_bcea == nullptr) { 1334 _bcea = new (CURRENT_ENV->arena()) BCEscapeAnalyzer(this, nullptr); 1335 } 1336 return _bcea; 1337 #else // COMPILER2 1338 ShouldNotReachHere(); 1339 return nullptr; 1340 #endif // COMPILER2 1341 } 1342 1343 ciMethodBlocks *ciMethod::get_method_blocks() { 1344 if (_method_blocks == nullptr) { 1345 Arena *arena = CURRENT_ENV->arena(); 1346 _method_blocks = new (arena) ciMethodBlocks(arena, this); 1347 } 1348 return _method_blocks; 1349 } 1350 1351 #undef FETCH_FLAG_FROM_VM 1352 1353 void ciMethod::dump_name_as_ascii(outputStream* st, Method* method) { 1354 st->print("%s %s %s", 1355 CURRENT_ENV->replay_name(method->method_holder()), 1356 method->name()->as_quoted_ascii(), 1357 method->signature()->as_quoted_ascii()); 1358 } 1359 1360 void ciMethod::dump_name_as_ascii(outputStream* st) { 1361 Method* method = get_Method(); 1362 dump_name_as_ascii(st, method); 1363 } 1364 1365 void ciMethod::dump_replay_data(outputStream* st) { 1366 ResourceMark rm; 1367 Method* method = get_Method(); 1368 if (MethodHandles::is_signature_polymorphic_method(method)) { 1369 // ignore for now 1370 return; 1371 } 1372 MethodCounters* mcs = method->method_counters(); 1373 st->print("ciMethod "); 1374 dump_name_as_ascii(st); 1375 st->print_cr(" %d %d %d %d %d", 1376 mcs == nullptr ? 0 : mcs->invocation_counter()->raw_counter(), 1377 mcs == nullptr ? 0 : mcs->backedge_counter()->raw_counter(), 1378 interpreter_invocation_count(), 1379 interpreter_throwout_count(), 1380 _inline_instructions_size); 1381 } 1382 1383 // ------------------------------------------------------------------ 1384 // ciMethod::print_codes 1385 // 1386 // Print a range of the bytecodes for this method. 1387 void ciMethod::print_codes_on(int from, int to, outputStream* st) { 1388 check_is_loaded(); 1389 GUARDED_VM_ENTRY(get_Method()->print_codes_on(from, to, st);) 1390 } 1391 1392 // ------------------------------------------------------------------ 1393 // ciMethod::print_name 1394 // 1395 // Print the name of this method, including signature and some flags. 1396 void ciMethod::print_name(outputStream* st) { 1397 check_is_loaded(); 1398 GUARDED_VM_ENTRY(get_Method()->print_name(st);) 1399 } 1400 1401 // ------------------------------------------------------------------ 1402 // ciMethod::print_short_name 1403 // 1404 // Print the name of this method, without signature. 1405 void ciMethod::print_short_name(outputStream* st) { 1406 if (is_loaded()) { 1407 GUARDED_VM_ENTRY(get_Method()->print_short_name(st);); 1408 } else { 1409 // Fall back if method is not loaded. 1410 holder()->print_name_on(st); 1411 st->print("::"); 1412 name()->print_symbol_on(st); 1413 if (WizardMode) 1414 signature()->as_symbol()->print_symbol_on(st); 1415 } 1416 } 1417 1418 // ------------------------------------------------------------------ 1419 // ciMethod::print_impl 1420 // 1421 // Implementation of the print method. 1422 void ciMethod::print_impl(outputStream* st) { 1423 ciMetadata::print_impl(st); 1424 st->print(" name="); 1425 name()->print_symbol_on(st); 1426 st->print(" holder="); 1427 holder()->print_name_on(st); 1428 st->print(" signature="); 1429 signature()->as_symbol()->print_symbol_on(st); 1430 if (is_loaded()) { 1431 st->print(" loaded=true"); 1432 st->print(" arg_size=%d", arg_size()); 1433 st->print(" flags="); 1434 flags().print_member_flags(st); 1435 } else { 1436 st->print(" loaded=false"); 1437 } 1438 } 1439 1440 // ------------------------------------------------------------------ 1441 1442 static BasicType erase_to_word_type(BasicType bt) { 1443 if (is_subword_type(bt)) return T_INT; 1444 if (is_reference_type(bt)) return T_OBJECT; 1445 return bt; 1446 } 1447 1448 static bool basic_types_match(ciType* t1, ciType* t2) { 1449 if (t1 == t2) return true; 1450 return erase_to_word_type(t1->basic_type()) == erase_to_word_type(t2->basic_type()); 1451 } 1452 1453 bool ciMethod::is_consistent_info(ciMethod* declared_method, ciMethod* resolved_method) { 1454 bool invoke_through_mh_intrinsic = declared_method->is_method_handle_intrinsic() && 1455 !resolved_method->is_method_handle_intrinsic(); 1456 1457 if (!invoke_through_mh_intrinsic) { 1458 // Method name & descriptor should stay the same. 1459 // Signatures may reference unloaded types and thus they may be not strictly equal. 1460 ciSymbol* declared_signature = declared_method->signature()->as_symbol(); 1461 ciSymbol* resolved_signature = resolved_method->signature()->as_symbol(); 1462 1463 return (declared_method->name()->equals(resolved_method->name())) && 1464 (declared_signature->equals(resolved_signature)); 1465 } 1466 1467 ciMethod* linker = declared_method; 1468 ciMethod* target = resolved_method; 1469 // Linkers have appendix argument which is not passed to callee. 1470 int has_appendix = MethodHandles::has_member_arg(linker->intrinsic_id()) ? 1 : 0; 1471 if (linker->arg_size() != (target->arg_size() + has_appendix)) { 1472 return false; // argument slot count mismatch 1473 } 1474 1475 ciSignature* linker_sig = linker->signature(); 1476 ciSignature* target_sig = target->signature(); 1477 1478 if (linker_sig->count() + (linker->is_static() ? 0 : 1) != 1479 target_sig->count() + (target->is_static() ? 0 : 1) + has_appendix) { 1480 return false; // argument count mismatch 1481 } 1482 1483 int sbase = 0, rbase = 0; 1484 switch (linker->intrinsic_id()) { 1485 case vmIntrinsics::_linkToVirtual: 1486 case vmIntrinsics::_linkToInterface: 1487 case vmIntrinsics::_linkToSpecial: { 1488 if (target->is_static()) { 1489 return false; 1490 } 1491 if (linker_sig->type_at(0)->is_primitive_type()) { 1492 return false; // receiver should be an oop 1493 } 1494 sbase = 1; // skip receiver 1495 break; 1496 } 1497 case vmIntrinsics::_linkToStatic: { 1498 if (!target->is_static()) { 1499 return false; 1500 } 1501 break; 1502 } 1503 case vmIntrinsics::_invokeBasic: { 1504 if (target->is_static()) { 1505 if (target_sig->type_at(0)->is_primitive_type()) { 1506 return false; // receiver should be an oop 1507 } 1508 rbase = 1; // skip receiver 1509 } 1510 break; 1511 } 1512 default: 1513 break; 1514 } 1515 assert(target_sig->count() - rbase == linker_sig->count() - sbase - has_appendix, "argument count mismatch"); 1516 int arg_count = target_sig->count() - rbase; 1517 for (int i = 0; i < arg_count; i++) { 1518 if (!basic_types_match(linker_sig->type_at(sbase + i), target_sig->type_at(rbase + i))) { 1519 return false; 1520 } 1521 } 1522 // Only check the return type if the symbolic info has non-void return type. 1523 // I.e. the return value of the resolved method can be dropped. 1524 if (!linker->return_type()->is_void() && 1525 !basic_types_match(linker->return_type(), target->return_type())) { 1526 return false; 1527 } 1528 return true; // no mismatch found 1529 } 1530 1531 // ------------------------------------------------------------------ 1532 // ciMethod::is_old 1533 // 1534 // Return true for redefined methods 1535 bool ciMethod::is_old() const { 1536 ASSERT_IN_VM; 1537 return get_Method()->is_old(); 1538 }