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