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         return true;
 678       } else if (data->is_ArrayStoreData()) {
 679         ciArrayStoreData* array_access = (ciArrayStoreData*) data->as_ArrayStoreData();
 680         array_type = array_access->array()->valid_type();
 681         flat_array = array_access->flat_array();
 682         null_free_array = array_access->null_free_array();
 683         ciCallProfile call_profile = call_profile_at_bci(bci);
 684         if (call_profile.morphism() == 1) {
 685           element_type = call_profile.receiver(0);
 686         } else {
 687           element_type = nullptr;
 688         }
 689         if (!array_access->null_seen()) {
 690           element_ptr = ProfileNeverNull;
 691         } else if (call_profile.count() == 0) {
 692           element_ptr = ProfileAlwaysNull;
 693         } else {
 694           element_ptr = ProfileMaybeNull;
 695         }
 696         return true;
 697       }
 698     }
 699   }
 700   return false;
 701 }
 702 
 703 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) {
 704   if (method_data() != nullptr && method_data()->is_mature()) {
 705     ciProfileData* data = method_data()->bci_to_data(bci);
 706     if (data != nullptr && data->is_ACmpData()) {
 707       ciACmpData* acmp = (ciACmpData*)data->as_ACmpData();
 708       left_type = acmp->left()->valid_type();
 709       right_type = acmp->right()->valid_type();
 710       left_ptr = acmp->left()->ptr_kind();
 711       right_ptr = acmp->right()->ptr_kind();
 712       left_inline_type = acmp->left_inline_type();
 713       right_inline_type = acmp->right_inline_type();
 714       return true;
 715     }
 716   }
 717   return false;
 718 }
 719 
 720 
 721 // ------------------------------------------------------------------
 722 // ciMethod::find_monomorphic_target
 723 //
 724 // Given a certain calling environment, find the monomorphic target
 725 // for the call.  Return null if the call is not monomorphic in
 726 // its calling environment, or if there are only abstract methods.
 727 // The returned method is never abstract.
 728 // Note: If caller uses a non-null result, it must inform dependencies
 729 // via assert_unique_concrete_method or assert_leaf_type.
 730 ciMethod* ciMethod::find_monomorphic_target(ciInstanceKlass* caller,
 731                                             ciInstanceKlass* callee_holder,
 732                                             ciInstanceKlass* actual_recv,
 733                                             bool check_access) {
 734   check_is_loaded();
 735 
 736   if (actual_recv->is_interface()) {
 737     // %%% We cannot trust interface types, yet.  See bug 6312651.
 738     return nullptr;
 739   }
 740 
 741   ciMethod* root_m = resolve_invoke(caller, actual_recv, check_access, true /* allow_abstract */);
 742   if (root_m == nullptr) {
 743     // Something went wrong looking up the actual receiver method.
 744     return nullptr;
 745   }
 746 
 747   // Make certain quick checks even if UseCHA is false.
 748 
 749   // Is it private or final?
 750   if (root_m->can_be_statically_bound()) {
 751     assert(!root_m->is_abstract(), "sanity");
 752     return root_m;
 753   }
 754 
 755   if (actual_recv->is_leaf_type() && actual_recv == root_m->holder()) {
 756     // Easy case.  There is no other place to put a method, so don't bother
 757     // to go through the VM_ENTRY_MARK and all the rest.
 758     if (root_m->is_abstract()) {
 759       return nullptr;
 760     }
 761     return root_m;
 762   }
 763 
 764   // Array methods (clone, hashCode, etc.) are always statically bound.
 765   // If we were to see an array type here, we'd return root_m.
 766   // However, this method processes only ciInstanceKlasses.  (See 4962591.)
 767   // The inline_native_clone intrinsic narrows Object to T[] properly,
 768   // so there is no need to do the same job here.
 769 
 770   if (!UseCHA)  return nullptr;
 771 
 772   VM_ENTRY_MARK;
 773 
 774   methodHandle target;
 775   {
 776     MutexLocker locker(Compile_lock);
 777     InstanceKlass* context = actual_recv->get_instanceKlass();
 778     target = methodHandle(THREAD, Dependencies::find_unique_concrete_method(context,
 779                                                                             root_m->get_Method(),
 780                                                                             callee_holder->get_Klass(),
 781                                                                             this->get_Method()));
 782     assert(target() == nullptr || !target()->is_abstract(), "not allowed");
 783     // %%% Should upgrade this ciMethod API to look for 1 or 2 concrete methods.
 784   }
 785 
 786 #ifndef PRODUCT
 787   LogTarget(Debug, dependencies) lt;
 788   if (lt.is_enabled() && target() != nullptr && target() != root_m->get_Method()) {
 789     LogStream ls(&lt);
 790     ls.print("found a non-root unique target method");
 791     ls.print_cr("  context = %s", actual_recv->get_Klass()->external_name());
 792     ls.print("  method  = ");
 793     target->print_short_name(&ls);
 794     ls.cr();
 795   }
 796 #endif //PRODUCT
 797 
 798   if (target() == nullptr) {
 799     return nullptr;
 800   }
 801 
 802   // Redefinition support.
 803   if (this->is_old() || root_m->is_old() || target->is_old()) {
 804     guarantee(CURRENT_THREAD_ENV->jvmti_state_changed(), "old method not detected");
 805     return nullptr;
 806   }
 807 
 808   if (target() == root_m->get_Method()) {
 809     return root_m;
 810   }
 811   if (!root_m->is_public() &&
 812       !root_m->is_protected()) {
 813     // If we are going to reason about inheritance, it's easiest
 814     // if the method in question is public, protected, or private.
 815     // If the answer is not root_m, it is conservatively correct
 816     // to return null, even if the CHA encountered irrelevant
 817     // methods in other packages.
 818     // %%% TO DO: Work out logic for package-private methods
 819     // with the same name but different vtable indexes.
 820     return nullptr;
 821   }
 822   return CURRENT_THREAD_ENV->get_method(target());
 823 }
 824 
 825 // ------------------------------------------------------------------
 826 // ciMethod::can_be_statically_bound
 827 //
 828 // Tries to determine whether a method can be statically bound in some context.
 829 bool ciMethod::can_be_statically_bound(ciInstanceKlass* context) const {
 830   return (holder() == context) && can_be_statically_bound();
 831 }
 832 
 833 // ------------------------------------------------------------------
 834 // ciMethod::can_omit_stack_trace
 835 //
 836 // Tries to determine whether a method can omit stack trace in throw in compiled code.
 837 bool ciMethod::can_omit_stack_trace() const {
 838   if (!StackTraceInThrowable) {
 839     return true; // stack trace is switched off.
 840   }
 841   if (!OmitStackTraceInFastThrow) {
 842     return false; // Have to provide stack trace.
 843   }
 844   return _can_omit_stack_trace;
 845 }
 846 
 847 // ------------------------------------------------------------------
 848 // ciMethod::resolve_invoke
 849 //
 850 // Given a known receiver klass, find the target for the call.
 851 // Return null if the call has no target or the target is abstract.
 852 ciMethod* ciMethod::resolve_invoke(ciKlass* caller, ciKlass* exact_receiver, bool check_access, bool allow_abstract) {
 853   check_is_loaded();
 854   VM_ENTRY_MARK;
 855 
 856   Klass* caller_klass = caller->get_Klass();
 857   Klass* recv         = exact_receiver->get_Klass();
 858   Klass* resolved     = holder()->get_Klass();
 859   Symbol* h_name      = name()->get_symbol();
 860   Symbol* h_signature = signature()->get_symbol();
 861 
 862   LinkInfo link_info(resolved, h_name, h_signature, caller_klass,
 863                      check_access ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip,
 864                      check_access ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip);
 865   Method* m = nullptr;
 866   // Only do exact lookup if receiver klass has been linked.  Otherwise,
 867   // the vtable has not been setup, and the LinkResolver will fail.
 868   if (recv->is_array_klass()
 869        ||
 870       (InstanceKlass::cast(recv)->is_linked() && !exact_receiver->is_interface())) {
 871     if (holder()->is_interface()) {
 872       m = LinkResolver::resolve_interface_call_or_null(recv, link_info);
 873     } else {
 874       m = LinkResolver::resolve_virtual_call_or_null(recv, link_info);
 875     }
 876   }
 877 
 878   if (m == nullptr) {
 879     // Return null only if there was a problem with lookup (uninitialized class, etc.)
 880     return nullptr;
 881   }
 882 
 883   ciMethod* result = this;
 884   if (m != get_Method()) {
 885     // Redefinition support.
 886     if (this->is_old() || m->is_old()) {
 887       guarantee(CURRENT_THREAD_ENV->jvmti_state_changed(), "old method not detected");
 888       return nullptr;
 889     }
 890 
 891     result = CURRENT_THREAD_ENV->get_method(m);
 892   }
 893 
 894   if (result->is_abstract() && !allow_abstract) {
 895     // Don't return abstract methods because they aren't optimizable or interesting.
 896     return nullptr;
 897   }
 898   return result;
 899 }
 900 
 901 // ------------------------------------------------------------------
 902 // ciMethod::resolve_vtable_index
 903 //
 904 // Given a known receiver klass, find the vtable index for the call.
 905 // Return Method::invalid_vtable_index if the vtable_index is unknown.
 906 int ciMethod::resolve_vtable_index(ciKlass* caller, ciKlass* receiver) {
 907    check_is_loaded();
 908 
 909    int vtable_index = Method::invalid_vtable_index;
 910    // Only do lookup if receiver klass has been linked.  Otherwise,
 911    // the vtable has not been setup, and the LinkResolver will fail.
 912    if (!receiver->is_interface()
 913        && (!receiver->is_instance_klass() ||
 914            receiver->as_instance_klass()->is_linked())) {
 915      VM_ENTRY_MARK;
 916 
 917      Klass* caller_klass = caller->get_Klass();
 918      Klass* recv         = receiver->get_Klass();
 919      Symbol* h_name = name()->get_symbol();
 920      Symbol* h_signature = signature()->get_symbol();
 921 
 922      LinkInfo link_info(recv, h_name, h_signature, caller_klass);
 923      vtable_index = LinkResolver::resolve_virtual_vtable_index(recv, link_info);
 924      if (vtable_index == Method::nonvirtual_vtable_index) {
 925        // A statically bound method.  Return "no such index".
 926        vtable_index = Method::invalid_vtable_index;
 927      }
 928    }
 929 
 930    return vtable_index;
 931 }
 932 
 933 // ------------------------------------------------------------------
 934 // ciMethod::get_field_at_bci
 935 ciField* ciMethod::get_field_at_bci(int bci, bool &will_link) {
 936   ciBytecodeStream iter(this);
 937   iter.reset_to_bci(bci);
 938   iter.next();
 939   return iter.get_field(will_link);
 940 }
 941 
 942 // ------------------------------------------------------------------
 943 // ciMethod::get_method_at_bci
 944 ciMethod* ciMethod::get_method_at_bci(int bci, bool &will_link, ciSignature* *declared_signature) {
 945   ciBytecodeStream iter(this);
 946   iter.reset_to_bci(bci);
 947   iter.next();
 948   return iter.get_method(will_link, declared_signature);
 949 }
 950 
 951 // ------------------------------------------------------------------
 952 ciKlass* ciMethod::get_declared_method_holder_at_bci(int bci) {
 953   ciBytecodeStream iter(this);
 954   iter.reset_to_bci(bci);
 955   iter.next();
 956   return iter.get_declared_method_holder();
 957 }
 958 
 959 // ------------------------------------------------------------------
 960 // Adjust a CounterData count to be commensurate with
 961 // interpreter_invocation_count.  If the MDO exists for
 962 // only 25% of the time the method exists, then the
 963 // counts in the MDO should be scaled by 4X, so that
 964 // they can be usefully and stably compared against the
 965 // invocation counts in methods.
 966 int ciMethod::scale_count(int count, float prof_factor) {
 967   if (count > 0 && method_data() != nullptr) {
 968     int counter_life = method_data()->invocation_count();
 969     int method_life = interpreter_invocation_count();
 970     if (method_life < counter_life) { // may happen because of the snapshot timing
 971       method_life = counter_life;
 972     }
 973     if (counter_life > 0) {
 974       double count_d = (double)count * prof_factor * method_life / counter_life + 0.5;
 975       if (count_d >= static_cast<double>(INT_MAX)) {
 976         // Clamp in case of overflowing int range.
 977         count = INT_MAX;
 978       } else {
 979         count = int(count_d);
 980         count = (count > 0) ? count : 1;
 981       }
 982     } else {
 983       count = 1;
 984     }
 985   }
 986   return count;
 987 }
 988 
 989 
 990 // ------------------------------------------------------------------
 991 // ciMethod::is_special_get_caller_class_method
 992 //
 993 bool ciMethod::is_ignored_by_security_stack_walk() const {
 994   check_is_loaded();
 995   VM_ENTRY_MARK;
 996   return get_Method()->is_ignored_by_security_stack_walk();
 997 }
 998 
 999 // ------------------------------------------------------------------
1000 // ciMethod::needs_clinit_barrier
1001 //
1002 bool ciMethod::needs_clinit_barrier() const {
1003   check_is_loaded();
1004   return is_static() && !holder()->is_initialized();
1005 }
1006 
1007 // ------------------------------------------------------------------
1008 // invokedynamic support
1009 
1010 // ------------------------------------------------------------------
1011 // ciMethod::is_method_handle_intrinsic
1012 //
1013 // Return true if the method is an instance of the JVM-generated
1014 // signature-polymorphic MethodHandle methods, _invokeBasic, _linkToVirtual, etc.
1015 bool ciMethod::is_method_handle_intrinsic() const {
1016   vmIntrinsics::ID iid = _intrinsic_id;  // do not check if loaded
1017   return (MethodHandles::is_signature_polymorphic(iid) &&
1018           MethodHandles::is_signature_polymorphic_intrinsic(iid));
1019 }
1020 
1021 // ------------------------------------------------------------------
1022 // ciMethod::is_compiled_lambda_form
1023 //
1024 // Return true if the method is a generated MethodHandle adapter.
1025 // These are built by Java code.
1026 bool ciMethod::is_compiled_lambda_form() const {
1027   vmIntrinsics::ID iid = _intrinsic_id;  // do not check if loaded
1028   return iid == vmIntrinsics::_compiledLambdaForm;
1029 }
1030 
1031 // ------------------------------------------------------------------
1032 // ciMethod::is_object_constructor
1033 //
1034 bool ciMethod::is_object_constructor() const {
1035    return (name() == ciSymbols::object_initializer_name()
1036            && signature()->return_type()->is_void());
1037    // Note:  We can't test is_static, because that would
1038    // require the method to be loaded.  Sometimes it isn't.
1039 }
1040 
1041 // ------------------------------------------------------------------
1042 // ciMethod::is_scoped
1043 //
1044 // Return true for methods annotated with @Scoped
1045 bool ciMethod::is_scoped() const {
1046    return get_Method()->is_scoped();
1047 }
1048 
1049 // ------------------------------------------------------------------
1050 // ciMethod::has_member_arg
1051 //
1052 // Return true if the method is a linker intrinsic like _linkToVirtual.
1053 // These are built by the JVM.
1054 bool ciMethod::has_member_arg() const {
1055   vmIntrinsics::ID iid = _intrinsic_id;  // do not check if loaded
1056   return (MethodHandles::is_signature_polymorphic(iid) &&
1057           MethodHandles::has_member_arg(iid));
1058 }
1059 
1060 // ------------------------------------------------------------------
1061 // ciMethod::ensure_method_data
1062 //
1063 // Generate new MethodData* objects at compile time.
1064 // Return true if allocation was successful or no MDO is required.
1065 bool ciMethod::ensure_method_data(const methodHandle& h_m) {
1066   EXCEPTION_CONTEXT;
1067   if (is_native() || is_abstract() || h_m()->is_accessor()) {
1068     return true;
1069   }
1070   if (h_m()->method_data() == nullptr) {
1071     Method::build_profiling_method_data(h_m, THREAD);
1072     if (HAS_PENDING_EXCEPTION) {
1073       CLEAR_PENDING_EXCEPTION;
1074     }
1075   }
1076   if (h_m()->method_data() != nullptr) {
1077     _method_data = CURRENT_ENV->get_method_data(h_m()->method_data());
1078     return _method_data->load_data();
1079   } else {
1080     _method_data = CURRENT_ENV->get_empty_methodData();
1081     return false;
1082   }
1083 }
1084 
1085 // public, retroactive version
1086 bool ciMethod::ensure_method_data() {
1087   bool result = true;
1088   if (_method_data == nullptr || _method_data->is_empty()) {
1089     GUARDED_VM_ENTRY({
1090       methodHandle mh(Thread::current(), get_Method());
1091       result = ensure_method_data(mh);
1092     });
1093   }
1094   return result;
1095 }
1096 
1097 
1098 // ------------------------------------------------------------------
1099 // ciMethod::method_data
1100 //
1101 ciMethodData* ciMethod::method_data() {
1102   if (_method_data != nullptr) {
1103     return _method_data;
1104   }
1105   VM_ENTRY_MARK;
1106   ciEnv* env = CURRENT_ENV;
1107   Thread* my_thread = JavaThread::current();
1108   methodHandle h_m(my_thread, get_Method());
1109 
1110   if (h_m()->method_data() != nullptr) {
1111     _method_data = CURRENT_ENV->get_method_data(h_m()->method_data());
1112     _method_data->load_data();
1113   } else {
1114     _method_data = CURRENT_ENV->get_empty_methodData();
1115   }
1116   return _method_data;
1117 
1118 }
1119 
1120 // ------------------------------------------------------------------
1121 // ciMethod::method_data_or_null
1122 // Returns a pointer to ciMethodData if MDO exists on the VM side,
1123 // null otherwise.
1124 ciMethodData* ciMethod::method_data_or_null() {
1125   ciMethodData *md = method_data();
1126   if (md->is_empty()) {
1127     return nullptr;
1128   }
1129   return md;
1130 }
1131 
1132 // ------------------------------------------------------------------
1133 // ciMethod::ensure_method_counters
1134 //
1135 MethodCounters* ciMethod::ensure_method_counters() {
1136   check_is_loaded();
1137   VM_ENTRY_MARK;
1138   methodHandle mh(THREAD, get_Method());
1139   MethodCounters* method_counters = mh->get_method_counters(CHECK_NULL);
1140   return method_counters;
1141 }
1142 
1143 // ------------------------------------------------------------------
1144 // ciMethod::has_option
1145 //
1146 bool ciMethod::has_option(CompileCommandEnum option) {
1147   check_is_loaded();
1148   VM_ENTRY_MARK;
1149   methodHandle mh(THREAD, get_Method());
1150   return CompilerOracle::has_option(mh, option);
1151 }
1152 
1153 // ------------------------------------------------------------------
1154 // ciMethod::has_option_value
1155 //
1156 bool ciMethod::has_option_value(CompileCommandEnum option, double& value) {
1157   check_is_loaded();
1158   VM_ENTRY_MARK;
1159   methodHandle mh(THREAD, get_Method());
1160   return CompilerOracle::has_option_value(mh, option, value);
1161 }
1162 // ------------------------------------------------------------------
1163 // ciMethod::can_be_compiled
1164 //
1165 // Have previous compilations of this method succeeded?
1166 bool ciMethod::can_be_compiled() {
1167   check_is_loaded();
1168   ciEnv* env = CURRENT_ENV;
1169   if (is_c1_compile(env->comp_level())) {
1170     return _is_c1_compilable;
1171   }
1172   return _is_c2_compilable;
1173 }
1174 
1175 // ------------------------------------------------------------------
1176 // ciMethod::has_compiled_code
1177 bool ciMethod::has_compiled_code() {
1178   return inline_instructions_size() > 0;
1179 }
1180 
1181 int ciMethod::highest_osr_comp_level() {
1182   check_is_loaded();
1183   VM_ENTRY_MARK;
1184   return get_Method()->highest_osr_comp_level();
1185 }
1186 
1187 // ------------------------------------------------------------------
1188 // ciMethod::code_size_for_inlining
1189 //
1190 // Code size for inlining decisions.  This method returns a code
1191 // size of 1 for methods which has the ForceInline annotation.
1192 int ciMethod::code_size_for_inlining() {
1193   check_is_loaded();
1194   if (get_Method()->force_inline()) {
1195     return 1;
1196   }
1197   return code_size();
1198 }
1199 
1200 // ------------------------------------------------------------------
1201 // ciMethod::inline_instructions_size
1202 //
1203 // This is a rough metric for "fat" methods, compared before inlining
1204 // with InlineSmallCode.  The CodeBlob::code_size accessor includes
1205 // junk like exception handler, stubs, and constant table, which are
1206 // not highly relevant to an inlined method.  So we use the more
1207 // specific accessor nmethod::insts_size.
1208 // Also some instructions inside the code are excluded from inline
1209 // heuristic (e.g. post call nop instructions; see InlineSkippedInstructionsCounter)
1210 int ciMethod::inline_instructions_size() {
1211   if (_inline_instructions_size == -1) {
1212     if (TrainingData::have_data()) {
1213       GUARDED_VM_ENTRY(
1214         CompLevel level = static_cast<CompLevel>(CURRENT_ENV->comp_level());
1215         methodHandle top_level_mh(Thread::current(), CURRENT_ENV->task()->method());
1216         MethodTrainingData* mtd = MethodTrainingData::find(top_level_mh);
1217         if (mtd != nullptr) {
1218           CompileTrainingData* ctd = mtd->last_toplevel_compile(level);
1219           if (ctd != nullptr) {
1220             methodHandle mh(Thread::current(), get_Method());
1221             MethodTrainingData* this_mtd = MethodTrainingData::find(mh);
1222             if (this_mtd != nullptr) {
1223               auto r = ctd->ci_records().ciMethod__inline_instructions_size.find(this_mtd);
1224               if (r.is_valid()) {
1225                 _inline_instructions_size = r.result();
1226               }
1227             }
1228           }
1229         }
1230       );
1231     }
1232   }
1233   if (_inline_instructions_size == -1) {
1234     GUARDED_VM_ENTRY(
1235       nmethod* code = get_Method()->code();
1236       if (code != nullptr && (code->comp_level() == CompLevel_full_optimization)) {
1237         int isize = code->insts_end() - code->verified_entry_point() - code->skipped_instructions_size();
1238         _inline_instructions_size = isize > 0 ? isize : 0;
1239       } else {
1240         _inline_instructions_size = 0;
1241       }
1242       if (TrainingData::need_data()) {
1243         CompileTrainingData* ctd = CURRENT_ENV->task()->training_data();
1244         if (ctd != nullptr) {
1245           methodHandle mh(Thread::current(), get_Method());
1246           MethodTrainingData* this_mtd = MethodTrainingData::make(mh);
1247           ctd->ci_records().ciMethod__inline_instructions_size.append_if_missing(_inline_instructions_size, this_mtd);
1248         }
1249       }
1250     );
1251   }
1252   return _inline_instructions_size;
1253 }
1254 
1255 // ------------------------------------------------------------------
1256 // ciMethod::log_nmethod_identity
1257 void ciMethod::log_nmethod_identity(xmlStream* log) {
1258   GUARDED_VM_ENTRY(
1259     nmethod* code = get_Method()->code();
1260     if (code != nullptr) {
1261       code->log_identity(log);
1262     }
1263   )
1264 }
1265 
1266 // ------------------------------------------------------------------
1267 // ciMethod::is_not_reached
1268 bool ciMethod::is_not_reached(int bci) {
1269   check_is_loaded();
1270   VM_ENTRY_MARK;
1271   return Interpreter::is_not_reached(
1272                methodHandle(THREAD, get_Method()), bci);
1273 }
1274 
1275 // ------------------------------------------------------------------
1276 // ciMethod::was_never_executed
1277 bool ciMethod::was_executed_more_than(int times) {
1278   VM_ENTRY_MARK;
1279   return get_Method()->was_executed_more_than(times);
1280 }
1281 
1282 // ------------------------------------------------------------------
1283 // ciMethod::has_unloaded_classes_in_signature
1284 bool ciMethod::has_unloaded_classes_in_signature() {
1285   // ciSignature is resolved against some accessing class and
1286   // signature classes aren't required to be local. As a benefit,
1287   // it makes signature classes visible through loader constraints.
1288   // So, encountering an unloaded class signals it is absent both in
1289   // the callee (local) and caller contexts.
1290   return signature()->has_unloaded_classes();
1291 }
1292 
1293 // ------------------------------------------------------------------
1294 // ciMethod::is_klass_loaded
1295 bool ciMethod::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
1296   VM_ENTRY_MARK;
1297   return get_Method()->is_klass_loaded(refinfo_index, bc, must_be_resolved);
1298 }
1299 
1300 // ------------------------------------------------------------------
1301 // ciMethod::check_call
1302 bool ciMethod::check_call(int refinfo_index, bool is_static) const {
1303   // This method is used only in C2 from InlineTree::ok_to_inline,
1304   // and is only used under -Xcomp.
1305   // It appears to fail when applied to an invokeinterface call site.
1306   // FIXME: Remove this method and resolve_method_statically; refactor to use the other LinkResolver entry points.
1307   VM_ENTRY_MARK;
1308   {
1309     ExceptionMark em(THREAD);
1310     HandleMark hm(THREAD);
1311     constantPoolHandle pool (THREAD, get_Method()->constants());
1312     Bytecodes::Code code = (is_static ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual);
1313     Method* spec_method = LinkResolver::resolve_method_statically(code, pool, refinfo_index, THREAD);
1314     if (HAS_PENDING_EXCEPTION) {
1315       CLEAR_PENDING_EXCEPTION;
1316       return false;
1317     } else {
1318       return (spec_method->is_static() == is_static);
1319     }
1320   }
1321   return false;
1322 }
1323 // ------------------------------------------------------------------
1324 // ciMethod::print_codes
1325 //
1326 // Print the bytecodes for this method.
1327 void ciMethod::print_codes_on(outputStream* st) {
1328   check_is_loaded();
1329   GUARDED_VM_ENTRY(get_Method()->print_codes_on(st);)
1330 }
1331 
1332 
1333 #define FETCH_FLAG_FROM_VM(flag_accessor) { \
1334   check_is_loaded(); \
1335   VM_ENTRY_MARK; \
1336   return get_Method()->flag_accessor(); \
1337 }
1338 
1339 bool ciMethod::has_loops      () const {         FETCH_FLAG_FROM_VM(has_loops); }
1340 bool ciMethod::has_jsrs       () const {         FETCH_FLAG_FROM_VM(has_jsrs);  }
1341 bool ciMethod::is_getter      () const {         FETCH_FLAG_FROM_VM(is_getter); }
1342 bool ciMethod::is_setter      () const {         FETCH_FLAG_FROM_VM(is_setter); }
1343 bool ciMethod::is_accessor    () const {         FETCH_FLAG_FROM_VM(is_accessor); }
1344 bool ciMethod::is_empty       () const {         FETCH_FLAG_FROM_VM(is_empty_method); }
1345 
1346 bool ciMethod::is_boxing_method() const {
1347   if (intrinsic_id() != vmIntrinsics::_none && holder()->is_box_klass()) {
1348     switch (intrinsic_id()) {
1349       case vmIntrinsics::_Boolean_valueOf:
1350       case vmIntrinsics::_Byte_valueOf:
1351       case vmIntrinsics::_Character_valueOf:
1352       case vmIntrinsics::_Short_valueOf:
1353       case vmIntrinsics::_Integer_valueOf:
1354       case vmIntrinsics::_Long_valueOf:
1355       case vmIntrinsics::_Float_valueOf:
1356       case vmIntrinsics::_Double_valueOf:
1357         return true;
1358       default:
1359         return false;
1360     }
1361   }
1362   return false;
1363 }
1364 
1365 bool ciMethod::is_unboxing_method() const {
1366   if (intrinsic_id() != vmIntrinsics::_none && holder()->is_box_klass()) {
1367     switch (intrinsic_id()) {
1368       case vmIntrinsics::_booleanValue:
1369       case vmIntrinsics::_byteValue:
1370       case vmIntrinsics::_charValue:
1371       case vmIntrinsics::_shortValue:
1372       case vmIntrinsics::_intValue:
1373       case vmIntrinsics::_longValue:
1374       case vmIntrinsics::_floatValue:
1375       case vmIntrinsics::_doubleValue:
1376         return true;
1377       default:
1378         return false;
1379     }
1380   }
1381   return false;
1382 }
1383 
1384 bool ciMethod::is_vector_method() const {
1385   return (holder() == ciEnv::current()->vector_VectorSupport_klass()) &&
1386          (intrinsic_id() != vmIntrinsics::_none);
1387 }
1388 
1389 BCEscapeAnalyzer  *ciMethod::get_bcea() {
1390 #ifdef COMPILER2
1391   if (_bcea == nullptr) {
1392     _bcea = new (CURRENT_ENV->arena()) BCEscapeAnalyzer(this, nullptr);
1393   }
1394   return _bcea;
1395 #else // COMPILER2
1396   ShouldNotReachHere();
1397   return nullptr;
1398 #endif // COMPILER2
1399 }
1400 
1401 ciMethodBlocks  *ciMethod::get_method_blocks() {
1402   if (_method_blocks == nullptr) {
1403     Arena *arena = CURRENT_ENV->arena();
1404     _method_blocks = new (arena) ciMethodBlocks(arena, this);
1405   }
1406   return _method_blocks;
1407 }
1408 
1409 #undef FETCH_FLAG_FROM_VM
1410 
1411 void ciMethod::dump_name_as_ascii(outputStream* st, Method* method) {
1412   st->print("%s %s %s",
1413             CURRENT_ENV->replay_name(method->method_holder()),
1414             method->name()->as_quoted_ascii(),
1415             method->signature()->as_quoted_ascii());
1416 }
1417 
1418 void ciMethod::dump_name_as_ascii(outputStream* st) {
1419   Method* method = get_Method();
1420   dump_name_as_ascii(st, method);
1421 }
1422 
1423 void ciMethod::dump_replay_data(outputStream* st) {
1424   ResourceMark rm;
1425   Method* method = get_Method();
1426   if (MethodHandles::is_signature_polymorphic_method(method)) {
1427     // ignore for now
1428     return;
1429   }
1430   MethodCounters* mcs = method->method_counters();
1431   st->print("ciMethod ");
1432   dump_name_as_ascii(st);
1433   st->print_cr(" %d %d %d %d %d",
1434                mcs == nullptr ? 0 : mcs->invocation_counter()->raw_counter(),
1435                mcs == nullptr ? 0 : mcs->backedge_counter()->raw_counter(),
1436                interpreter_invocation_count(),
1437                interpreter_throwout_count(),
1438                _inline_instructions_size);
1439 }
1440 
1441 // ------------------------------------------------------------------
1442 // ciMethod::print_codes
1443 //
1444 // Print a range of the bytecodes for this method.
1445 void ciMethod::print_codes_on(int from, int to, outputStream* st) {
1446   check_is_loaded();
1447   GUARDED_VM_ENTRY(get_Method()->print_codes_on(from, to, st);)
1448 }
1449 
1450 // ------------------------------------------------------------------
1451 // ciMethod::print_name
1452 //
1453 // Print the name of this method, including signature and some flags.
1454 void ciMethod::print_name(outputStream* st) {
1455   check_is_loaded();
1456   GUARDED_VM_ENTRY(get_Method()->print_name(st);)
1457 }
1458 
1459 // ------------------------------------------------------------------
1460 // ciMethod::print_short_name
1461 //
1462 // Print the name of this method, without signature.
1463 void ciMethod::print_short_name(outputStream* st) {
1464   if (is_loaded()) {
1465     GUARDED_VM_ENTRY(get_Method()->print_short_name(st););
1466   } else {
1467     // Fall back if method is not loaded.
1468     holder()->print_name_on(st);
1469     st->print("::");
1470     name()->print_symbol_on(st);
1471     if (WizardMode)
1472       signature()->as_symbol()->print_symbol_on(st);
1473   }
1474 }
1475 
1476 // ------------------------------------------------------------------
1477 // ciMethod::print_impl
1478 //
1479 // Implementation of the print method.
1480 void ciMethod::print_impl(outputStream* st) {
1481   ciMetadata::print_impl(st);
1482   st->print(" name=");
1483   name()->print_symbol_on(st);
1484   st->print(" holder=");
1485   holder()->print_name_on(st);
1486   st->print(" signature=");
1487   signature()->as_symbol()->print_symbol_on(st);
1488   if (is_loaded()) {
1489     st->print(" loaded=true");
1490     st->print(" arg_size=%d", arg_size());
1491     st->print(" flags=");
1492     flags().print_member_flags(st);
1493   } else {
1494     st->print(" loaded=false");
1495   }
1496 }
1497 
1498 // ------------------------------------------------------------------
1499 
1500 static BasicType erase_to_word_type(BasicType bt) {
1501   if (is_subword_type(bt))   return T_INT;
1502   if (is_reference_type(bt)) return T_OBJECT;
1503   return bt;
1504 }
1505 
1506 static bool basic_types_match(ciType* t1, ciType* t2) {
1507   if (t1 == t2)  return true;
1508   return erase_to_word_type(t1->basic_type()) == erase_to_word_type(t2->basic_type());
1509 }
1510 
1511 bool ciMethod::is_consistent_info(ciMethod* declared_method, ciMethod* resolved_method) {
1512   bool invoke_through_mh_intrinsic = declared_method->is_method_handle_intrinsic() &&
1513                                   !resolved_method->is_method_handle_intrinsic();
1514 
1515   if (!invoke_through_mh_intrinsic) {
1516     // Method name & descriptor should stay the same.
1517     // Signatures may reference unloaded types and thus they may be not strictly equal.
1518     ciSymbol* declared_signature = declared_method->signature()->as_symbol();
1519     ciSymbol* resolved_signature = resolved_method->signature()->as_symbol();
1520 
1521     return (declared_method->name()->equals(resolved_method->name())) &&
1522            (declared_signature->equals(resolved_signature));
1523   }
1524 
1525   ciMethod* linker = declared_method;
1526   ciMethod* target = resolved_method;
1527   // Linkers have appendix argument which is not passed to callee.
1528   int has_appendix = MethodHandles::has_member_arg(linker->intrinsic_id()) ? 1 : 0;
1529   if (linker->arg_size() != (target->arg_size() + has_appendix)) {
1530     return false; // argument slot count mismatch
1531   }
1532 
1533   ciSignature* linker_sig = linker->signature();
1534   ciSignature* target_sig = target->signature();
1535 
1536   if (linker_sig->count() + (linker->is_static() ? 0 : 1) !=
1537       target_sig->count() + (target->is_static() ? 0 : 1) + has_appendix) {
1538     return false; // argument count mismatch
1539   }
1540 
1541   int sbase = 0, rbase = 0;
1542   switch (linker->intrinsic_id()) {
1543     case vmIntrinsics::_linkToVirtual:
1544     case vmIntrinsics::_linkToInterface:
1545     case vmIntrinsics::_linkToSpecial: {
1546       if (target->is_static()) {
1547         return false;
1548       }
1549       if (linker_sig->type_at(0)->is_primitive_type()) {
1550         return false;  // receiver should be an oop
1551       }
1552       sbase = 1; // skip receiver
1553       break;
1554     }
1555     case vmIntrinsics::_linkToStatic: {
1556       if (!target->is_static()) {
1557         return false;
1558       }
1559       break;
1560     }
1561     case vmIntrinsics::_invokeBasic: {
1562       if (target->is_static()) {
1563         if (target_sig->type_at(0)->is_primitive_type()) {
1564           return false; // receiver should be an oop
1565         }
1566         rbase = 1; // skip receiver
1567       }
1568       break;
1569     }
1570     default:
1571       break;
1572   }
1573   assert(target_sig->count() - rbase == linker_sig->count() - sbase - has_appendix, "argument count mismatch");
1574   int arg_count = target_sig->count() - rbase;
1575   for (int i = 0; i < arg_count; i++) {
1576     if (!basic_types_match(linker_sig->type_at(sbase + i), target_sig->type_at(rbase + i))) {
1577       return false;
1578     }
1579   }
1580   // Only check the return type if the symbolic info has non-void return type.
1581   // I.e. the return value of the resolved method can be dropped.
1582   if (!linker->return_type()->is_void() &&
1583       !basic_types_match(linker->return_type(), target->return_type())) {
1584     return false;
1585   }
1586   return true; // no mismatch found
1587 }
1588 
1589 // ------------------------------------------------------------------
1590 
1591 bool ciMethod::is_scalarized_arg(int idx) const {
1592   VM_ENTRY_MARK;
1593   return get_Method()->is_scalarized_arg(idx);
1594 }
1595 
1596 bool ciMethod::has_scalarized_args() const {
1597   VM_ENTRY_MARK;
1598   return get_Method()->has_scalarized_args();
1599 }
1600 
1601 const GrowableArray<SigEntry>* ciMethod::get_sig_cc() const {
1602   VM_ENTRY_MARK;
1603   if (get_Method()->adapter() == nullptr) {
1604     return nullptr;
1605   }
1606   return get_Method()->adapter()->get_sig_cc();
1607 }
1608 
1609 // ciMethod::is_old
1610 //
1611 // Return true for redefined methods
1612 bool ciMethod::is_old() const {
1613   ASSERT_IN_VM;
1614   return get_Method()->is_old();
1615 }