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