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