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