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