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