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