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