1 /*
   2  * Copyright (c) 1999, 2010, 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 "incls/_precompiled.incl"
  26 #include "incls/_ciMethod.cpp.incl"
  27 
  28 // ciMethod
  29 //
  30 // This class represents a methodOop in the HotSpot virtual
  31 // machine.
  32 
  33 
  34 // ------------------------------------------------------------------
  35 // ciMethod::ciMethod
  36 //
  37 // Loaded method.
  38 ciMethod::ciMethod(methodHandle h_m) : ciObject(h_m) {
  39   assert(h_m() != NULL, "no null method");
  40 
  41   // These fields are always filled in in loaded methods.
  42   _flags = ciFlags(h_m()->access_flags());
  43 
  44   // Easy to compute, so fill them in now.
  45   _max_stack          = h_m()->max_stack();
  46   _max_locals         = h_m()->max_locals();
  47   _code_size          = h_m()->code_size();
  48   _intrinsic_id       = h_m()->intrinsic_id();
  49   _handler_count      = h_m()->exception_table()->length() / 4;
  50   _uses_monitors      = h_m()->access_flags().has_monitor_bytecodes();
  51   _balanced_monitors  = !_uses_monitors || h_m()->access_flags().is_monitor_matching();
  52   _is_c1_compilable   = !h_m()->is_not_c1_compilable();
  53   _is_c2_compilable   = !h_m()->is_not_c2_compilable();
  54   // Lazy fields, filled in on demand.  Require allocation.
  55   _code               = NULL;
  56   _exception_handlers = NULL;
  57   _liveness           = NULL;
  58   _method_blocks = NULL;
  59 #if defined(COMPILER2) || defined(SHARK)
  60   _flow               = NULL;
  61   _bcea               = NULL;
  62 #endif // COMPILER2 || SHARK
  63 
  64   ciEnv *env = CURRENT_ENV;
  65   if (env->jvmti_can_hotswap_or_post_breakpoint() && can_be_compiled()) {
  66     // 6328518 check hotswap conditions under the right lock.
  67     MutexLocker locker(Compile_lock);
  68     if (Dependencies::check_evol_method(h_m()) != NULL) {
  69       _is_c1_compilable = false;
  70       _is_c2_compilable = false;
  71     }
  72   } else {
  73     CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
  74   }
  75 
  76   if (instanceKlass::cast(h_m()->method_holder())->is_linked()) {
  77     _can_be_statically_bound = h_m()->can_be_statically_bound();
  78   } else {
  79     // Have to use a conservative value in this case.
  80     _can_be_statically_bound = false;
  81   }
  82 
  83   // Adjust the definition of this condition to be more useful:
  84   // %%% take these conditions into account in vtable generation
  85   if (!_can_be_statically_bound && h_m()->is_private())
  86     _can_be_statically_bound = true;
  87   if (_can_be_statically_bound && h_m()->is_abstract())
  88     _can_be_statically_bound = false;
  89 
  90   // generating _signature may allow GC and therefore move m.
  91   // These fields are always filled in.
  92   _name = env->get_object(h_m()->name())->as_symbol();
  93   _holder = env->get_object(h_m()->method_holder())->as_instance_klass();
  94   ciSymbol* sig_symbol = env->get_object(h_m()->signature())->as_symbol();
  95   _signature = new (env->arena()) ciSignature(_holder, sig_symbol);
  96   _method_data = NULL;
  97   // Take a snapshot of these values, so they will be commensurate with the MDO.
  98   if (ProfileInterpreter || TieredCompilation) {
  99     int invcnt = h_m()->interpreter_invocation_count();
 100     // if the value overflowed report it as max int
 101     _interpreter_invocation_count = invcnt < 0 ? max_jint : invcnt ;
 102     _interpreter_throwout_count   = h_m()->interpreter_throwout_count();
 103   } else {
 104     _interpreter_invocation_count = 0;
 105     _interpreter_throwout_count = 0;
 106   }
 107   if (_interpreter_invocation_count == 0)
 108     _interpreter_invocation_count = 1;
 109 }
 110 
 111 
 112 // ------------------------------------------------------------------
 113 // ciMethod::ciMethod
 114 //
 115 // Unloaded method.
 116 ciMethod::ciMethod(ciInstanceKlass* holder,
 117                    ciSymbol* name,
 118                    ciSymbol* signature) : ciObject(ciMethodKlass::make()) {
 119   // These fields are always filled in.
 120   _name = name;
 121   _holder = holder;
 122   _signature = new (CURRENT_ENV->arena()) ciSignature(_holder, signature);
 123   _intrinsic_id = vmIntrinsics::_none;
 124   _liveness = NULL;
 125   _can_be_statically_bound = false;
 126   _method_blocks = NULL;
 127   _method_data = NULL;
 128 #if defined(COMPILER2) || defined(SHARK)
 129   _flow = NULL;
 130   _bcea = NULL;
 131 #endif // COMPILER2 || SHARK
 132 }
 133 
 134 
 135 // ------------------------------------------------------------------
 136 // ciMethod::load_code
 137 //
 138 // Load the bytecodes and exception handler table for this method.
 139 void ciMethod::load_code() {
 140   VM_ENTRY_MARK;
 141   assert(is_loaded(), "only loaded methods have code");
 142 
 143   methodOop me = get_methodOop();
 144   Arena* arena = CURRENT_THREAD_ENV->arena();
 145 
 146   // Load the bytecodes.
 147   _code = (address)arena->Amalloc(code_size());
 148   memcpy(_code, me->code_base(), code_size());
 149 
 150   // Revert any breakpoint bytecodes in ci's copy
 151   if (me->number_of_breakpoints() > 0) {
 152     BreakpointInfo* bp = instanceKlass::cast(me->method_holder())->breakpoints();
 153     for (; bp != NULL; bp = bp->next()) {
 154       if (bp->match(me)) {
 155         code_at_put(bp->bci(), bp->orig_bytecode());
 156       }
 157     }
 158   }
 159 
 160   // And load the exception table.
 161   typeArrayOop exc_table = me->exception_table();
 162 
 163   // Allocate one extra spot in our list of exceptions.  This
 164   // last entry will be used to represent the possibility that
 165   // an exception escapes the method.  See ciExceptionHandlerStream
 166   // for details.
 167   _exception_handlers =
 168     (ciExceptionHandler**)arena->Amalloc(sizeof(ciExceptionHandler*)
 169                                          * (_handler_count + 1));
 170   if (_handler_count > 0) {
 171     for (int i=0; i<_handler_count; i++) {
 172       int base = i*4;
 173       _exception_handlers[i] = new (arena) ciExceptionHandler(
 174                                 holder(),
 175             /* start    */      exc_table->int_at(base),
 176             /* limit    */      exc_table->int_at(base+1),
 177             /* goto pc  */      exc_table->int_at(base+2),
 178             /* cp index */      exc_table->int_at(base+3));
 179     }
 180   }
 181 
 182   // Put an entry at the end of our list to represent the possibility
 183   // of exceptional exit.
 184   _exception_handlers[_handler_count] =
 185     new (arena) ciExceptionHandler(holder(), 0, code_size(), -1, 0);
 186 
 187   if (CIPrintMethodCodes) {
 188     print_codes();
 189   }
 190 }
 191 
 192 
 193 // ------------------------------------------------------------------
 194 // ciMethod::has_linenumber_table
 195 //
 196 // length unknown until decompression
 197 bool    ciMethod::has_linenumber_table() const {
 198   check_is_loaded();
 199   VM_ENTRY_MARK;
 200   return get_methodOop()->has_linenumber_table();
 201 }
 202 
 203 
 204 // ------------------------------------------------------------------
 205 // ciMethod::compressed_linenumber_table
 206 u_char* ciMethod::compressed_linenumber_table() const {
 207   check_is_loaded();
 208   VM_ENTRY_MARK;
 209   return get_methodOop()->compressed_linenumber_table();
 210 }
 211 
 212 
 213 // ------------------------------------------------------------------
 214 // ciMethod::line_number_from_bci
 215 int ciMethod::line_number_from_bci(int bci) const {
 216   check_is_loaded();
 217   VM_ENTRY_MARK;
 218   return get_methodOop()->line_number_from_bci(bci);
 219 }
 220 
 221 
 222 // ------------------------------------------------------------------
 223 // ciMethod::vtable_index
 224 //
 225 // Get the position of this method's entry in the vtable, if any.
 226 int ciMethod::vtable_index() {
 227   check_is_loaded();
 228   assert(holder()->is_linked(), "must be linked");
 229   VM_ENTRY_MARK;
 230   return get_methodOop()->vtable_index();
 231 }
 232 
 233 
 234 #ifdef SHARK
 235 // ------------------------------------------------------------------
 236 // ciMethod::itable_index
 237 //
 238 // Get the position of this method's entry in the itable, if any.
 239 int ciMethod::itable_index() {
 240   check_is_loaded();
 241   assert(holder()->is_linked(), "must be linked");
 242   VM_ENTRY_MARK;
 243   return klassItable::compute_itable_index(get_methodOop());
 244 }
 245 #endif // SHARK
 246 
 247 
 248 // ------------------------------------------------------------------
 249 // ciMethod::native_entry
 250 //
 251 // Get the address of this method's native code, if any.
 252 address ciMethod::native_entry() {
 253   check_is_loaded();
 254   assert(flags().is_native(), "must be native method");
 255   VM_ENTRY_MARK;
 256   methodOop method = get_methodOop();
 257   address entry = method->native_function();
 258   assert(entry != NULL, "must be valid entry point");
 259   return entry;
 260 }
 261 
 262 
 263 // ------------------------------------------------------------------
 264 // ciMethod::interpreter_entry
 265 //
 266 // Get the entry point for running this method in the interpreter.
 267 address ciMethod::interpreter_entry() {
 268   check_is_loaded();
 269   VM_ENTRY_MARK;
 270   methodHandle mh(THREAD, get_methodOop());
 271   return Interpreter::entry_for_method(mh);
 272 }
 273 
 274 
 275 // ------------------------------------------------------------------
 276 // ciMethod::uses_balanced_monitors
 277 //
 278 // Does this method use monitors in a strict stack-disciplined manner?
 279 bool ciMethod::has_balanced_monitors() {
 280   check_is_loaded();
 281   if (_balanced_monitors) return true;
 282 
 283   // Analyze the method to see if monitors are used properly.
 284   VM_ENTRY_MARK;
 285   methodHandle method(THREAD, get_methodOop());
 286   assert(method->has_monitor_bytecodes(), "should have checked this");
 287 
 288   // Check to see if a previous compilation computed the
 289   // monitor-matching analysis.
 290   if (method->guaranteed_monitor_matching()) {
 291     _balanced_monitors = true;
 292     return true;
 293   }
 294 
 295   {
 296     EXCEPTION_MARK;
 297     ResourceMark rm(THREAD);
 298     GeneratePairingInfo gpi(method);
 299     gpi.compute_map(CATCH);
 300     if (!gpi.monitor_safe()) {
 301       return false;
 302     }
 303     method->set_guaranteed_monitor_matching();
 304     _balanced_monitors = true;
 305   }
 306   return true;
 307 }
 308 
 309 
 310 // ------------------------------------------------------------------
 311 // ciMethod::get_flow_analysis
 312 ciTypeFlow* ciMethod::get_flow_analysis() {
 313 #if defined(COMPILER2) || defined(SHARK)
 314   if (_flow == NULL) {
 315     ciEnv* env = CURRENT_ENV;
 316     _flow = new (env->arena()) ciTypeFlow(env, this);
 317     _flow->do_flow();
 318   }
 319   return _flow;
 320 #else // COMPILER2 || SHARK
 321   ShouldNotReachHere();
 322   return NULL;
 323 #endif // COMPILER2 || SHARK
 324 }
 325 
 326 
 327 // ------------------------------------------------------------------
 328 // ciMethod::get_osr_flow_analysis
 329 ciTypeFlow* ciMethod::get_osr_flow_analysis(int osr_bci) {
 330 #if defined(COMPILER2) || defined(SHARK)
 331   // OSR entry points are always place after a call bytecode of some sort
 332   assert(osr_bci >= 0, "must supply valid OSR entry point");
 333   ciEnv* env = CURRENT_ENV;
 334   ciTypeFlow* flow = new (env->arena()) ciTypeFlow(env, this, osr_bci);
 335   flow->do_flow();
 336   return flow;
 337 #else // COMPILER2 || SHARK
 338   ShouldNotReachHere();
 339   return NULL;
 340 #endif // COMPILER2 || SHARK
 341 }
 342 
 343 // ------------------------------------------------------------------
 344 // ciMethod::raw_liveness_at_bci
 345 //
 346 // Which local variables are live at a specific bci?
 347 MethodLivenessResult ciMethod::raw_liveness_at_bci(int bci) {
 348   check_is_loaded();
 349   if (_liveness == NULL) {
 350     // Create the liveness analyzer.
 351     Arena* arena = CURRENT_ENV->arena();
 352     _liveness = new (arena) MethodLiveness(arena, this);
 353     _liveness->compute_liveness();
 354   }
 355   return _liveness->get_liveness_at(bci);
 356 }
 357 
 358 // ------------------------------------------------------------------
 359 // ciMethod::liveness_at_bci
 360 //
 361 // Which local variables are live at a specific bci?  When debugging
 362 // will return true for all locals in some cases to improve debug
 363 // information.
 364 MethodLivenessResult ciMethod::liveness_at_bci(int bci) {
 365   MethodLivenessResult result = raw_liveness_at_bci(bci);
 366   if (CURRENT_ENV->jvmti_can_access_local_variables() || DeoptimizeALot || CompileTheWorld) {
 367     // Keep all locals live for the user's edification and amusement.
 368     result.at_put_range(0, result.size(), true);
 369   }
 370   return result;
 371 }
 372 
 373 // ciMethod::live_local_oops_at_bci
 374 //
 375 // find all the live oops in the locals array for a particular bci
 376 // Compute what the interpreter believes by using the interpreter
 377 // oopmap generator. This is used as a double check during osr to
 378 // guard against conservative result from MethodLiveness making us
 379 // think a dead oop is live.  MethodLiveness is conservative in the
 380 // sense that it may consider locals to be live which cannot be live,
 381 // like in the case where a local could contain an oop or  a primitive
 382 // along different paths.  In that case the local must be dead when
 383 // those paths merge. Since the interpreter's viewpoint is used when
 384 // gc'ing an interpreter frame we need to use its viewpoint  during
 385 // OSR when loading the locals.
 386 
 387 BitMap ciMethod::live_local_oops_at_bci(int bci) {
 388   VM_ENTRY_MARK;
 389   InterpreterOopMap mask;
 390   OopMapCache::compute_one_oop_map(get_methodOop(), bci, &mask);
 391   int mask_size = max_locals();
 392   BitMap result(mask_size);
 393   result.clear();
 394   int i;
 395   for (i = 0; i < mask_size ; i++ ) {
 396     if (mask.is_oop(i)) result.set_bit(i);
 397   }
 398   return result;
 399 }
 400 
 401 
 402 #ifdef COMPILER1
 403 // ------------------------------------------------------------------
 404 // ciMethod::bci_block_start
 405 //
 406 // Marks all bcis where a new basic block starts
 407 const BitMap ciMethod::bci_block_start() {
 408   check_is_loaded();
 409   if (_liveness == NULL) {
 410     // Create the liveness analyzer.
 411     Arena* arena = CURRENT_ENV->arena();
 412     _liveness = new (arena) MethodLiveness(arena, this);
 413     _liveness->compute_liveness();
 414   }
 415 
 416   return _liveness->get_bci_block_start();
 417 }
 418 #endif // COMPILER1
 419 
 420 
 421 // ------------------------------------------------------------------
 422 // ciMethod::call_profile_at_bci
 423 //
 424 // Get the ciCallProfile for the invocation of this method.
 425 // Also reports receiver types for non-call type checks (if TypeProfileCasts).
 426 ciCallProfile ciMethod::call_profile_at_bci(int bci) {
 427   ResourceMark rm;
 428   ciCallProfile result;
 429   if (method_data() != NULL && method_data()->is_mature()) {
 430     ciProfileData* data = method_data()->bci_to_data(bci);
 431     if (data != NULL && data->is_CounterData()) {
 432       // Every profiled call site has a counter.
 433       int count = data->as_CounterData()->count();
 434 
 435       if (!data->is_ReceiverTypeData()) {
 436         result._receiver_count[0] = 0;  // that's a definite zero
 437       } else { // ReceiverTypeData is a subclass of CounterData
 438         ciReceiverTypeData* call = (ciReceiverTypeData*)data->as_ReceiverTypeData();
 439         // In addition, virtual call sites have receiver type information
 440         int receivers_count_total = 0;
 441         int morphism = 0;
 442         // Precompute morphism for the possible fixup
 443         for (uint i = 0; i < call->row_limit(); i++) {
 444           ciKlass* receiver = call->receiver(i);
 445           if (receiver == NULL)  continue;
 446           morphism++;
 447         }
 448         int epsilon = 0;
 449         if (TieredCompilation && ProfileInterpreter) {
 450           // Interpreter and C1 treat final and special invokes differently.
 451           // C1 will record a type, whereas the interpreter will just
 452           // increment the count. Detect this case.
 453           if (morphism == 1 && count > 0) {
 454             epsilon = count;
 455             count = 0;
 456           }
 457         }
 458         for (uint i = 0; i < call->row_limit(); i++) {
 459           ciKlass* receiver = call->receiver(i);
 460           if (receiver == NULL)  continue;
 461           int rcount = call->receiver_count(i) + epsilon;
 462           if (rcount == 0) rcount = 1; // Should be valid value
 463           receivers_count_total += rcount;
 464           // Add the receiver to result data.
 465           result.add_receiver(receiver, rcount);
 466           // If we extend profiling to record methods,
 467           // we will set result._method also.
 468         }
 469         // Determine call site's morphism.
 470         // The call site count is 0 with known morphism (onlt 1 or 2 receivers)
 471         // or < 0 in the case of a type check failured for checkcast, aastore, instanceof.
 472         // The call site count is > 0 in the case of a polymorphic virtual call.
 473         if (morphism > 0 && morphism == result._limit) {
 474            // The morphism <= MorphismLimit.
 475            if ((morphism <  ciCallProfile::MorphismLimit) ||
 476                (morphism == ciCallProfile::MorphismLimit && count == 0)) {
 477 #ifdef ASSERT
 478              if (count > 0) {
 479                this->print_short_name(tty);
 480                tty->print_cr(" @ bci:%d", bci);
 481                this->print_codes();
 482                assert(false, "this call site should not be polymorphic");
 483              }
 484 #endif
 485              result._morphism = morphism;
 486            }
 487         }
 488         // Make the count consistent if this is a call profile. If count is
 489         // zero or less, presume that this is a typecheck profile and
 490         // do nothing.  Otherwise, increase count to be the sum of all
 491         // receiver's counts.
 492         if (count >= 0) {
 493           count += receivers_count_total;
 494         }
 495       }
 496       result._count = count;
 497     }
 498   }
 499   return result;
 500 }
 501 
 502 // ------------------------------------------------------------------
 503 // Add new receiver and sort data by receiver's profile count.
 504 void ciCallProfile::add_receiver(ciKlass* receiver, int receiver_count) {
 505   // Add new receiver and sort data by receiver's counts when we have space
 506   // for it otherwise replace the less called receiver (less called receiver
 507   // is placed to the last array element which is not used).
 508   // First array's element contains most called receiver.
 509   int i = _limit;
 510   for (; i > 0 && receiver_count > _receiver_count[i-1]; i--) {
 511     _receiver[i] = _receiver[i-1];
 512     _receiver_count[i] = _receiver_count[i-1];
 513   }
 514   _receiver[i] = receiver;
 515   _receiver_count[i] = receiver_count;
 516   if (_limit < MorphismLimit) _limit++;
 517 }
 518 
 519 // ------------------------------------------------------------------
 520 // ciMethod::find_monomorphic_target
 521 //
 522 // Given a certain calling environment, find the monomorphic target
 523 // for the call.  Return NULL if the call is not monomorphic in
 524 // its calling environment, or if there are only abstract methods.
 525 // The returned method is never abstract.
 526 // Note: If caller uses a non-null result, it must inform dependencies
 527 // via assert_unique_concrete_method or assert_leaf_type.
 528 ciMethod* ciMethod::find_monomorphic_target(ciInstanceKlass* caller,
 529                                             ciInstanceKlass* callee_holder,
 530                                             ciInstanceKlass* actual_recv) {
 531   check_is_loaded();
 532 
 533   if (actual_recv->is_interface()) {
 534     // %%% We cannot trust interface types, yet.  See bug 6312651.
 535     return NULL;
 536   }
 537 
 538   ciMethod* root_m = resolve_invoke(caller, actual_recv);
 539   if (root_m == NULL) {
 540     // Something went wrong looking up the actual receiver method.
 541     return NULL;
 542   }
 543   assert(!root_m->is_abstract(), "resolve_invoke promise");
 544 
 545   // Make certain quick checks even if UseCHA is false.
 546 
 547   // Is it private or final?
 548   if (root_m->can_be_statically_bound()) {
 549     return root_m;
 550   }
 551 
 552   if (actual_recv->is_leaf_type() && actual_recv == root_m->holder()) {
 553     // Easy case.  There is no other place to put a method, so don't bother
 554     // to go through the VM_ENTRY_MARK and all the rest.
 555     return root_m;
 556   }
 557 
 558   // Array methods (clone, hashCode, etc.) are always statically bound.
 559   // If we were to see an array type here, we'd return root_m.
 560   // However, this method processes only ciInstanceKlasses.  (See 4962591.)
 561   // The inline_native_clone intrinsic narrows Object to T[] properly,
 562   // so there is no need to do the same job here.
 563 
 564   if (!UseCHA)  return NULL;
 565 
 566   VM_ENTRY_MARK;
 567 
 568   methodHandle target;
 569   {
 570     MutexLocker locker(Compile_lock);
 571     klassOop context = actual_recv->get_klassOop();
 572     target = Dependencies::find_unique_concrete_method(context,
 573                                                        root_m->get_methodOop());
 574     // %%% Should upgrade this ciMethod API to look for 1 or 2 concrete methods.
 575   }
 576 
 577 #ifndef PRODUCT
 578   if (TraceDependencies && target() != NULL && target() != root_m->get_methodOop()) {
 579     tty->print("found a non-root unique target method");
 580     tty->print_cr("  context = %s", instanceKlass::cast(actual_recv->get_klassOop())->external_name());
 581     tty->print("  method  = ");
 582     target->print_short_name(tty);
 583     tty->cr();
 584   }
 585 #endif //PRODUCT
 586 
 587   if (target() == NULL) {
 588     return NULL;
 589   }
 590   if (target() == root_m->get_methodOop()) {
 591     return root_m;
 592   }
 593   if (!root_m->is_public() &&
 594       !root_m->is_protected()) {
 595     // If we are going to reason about inheritance, it's easiest
 596     // if the method in question is public, protected, or private.
 597     // If the answer is not root_m, it is conservatively correct
 598     // to return NULL, even if the CHA encountered irrelevant
 599     // methods in other packages.
 600     // %%% TO DO: Work out logic for package-private methods
 601     // with the same name but different vtable indexes.
 602     return NULL;
 603   }
 604   return CURRENT_THREAD_ENV->get_object(target())->as_method();
 605 }
 606 
 607 // ------------------------------------------------------------------
 608 // ciMethod::resolve_invoke
 609 //
 610 // Given a known receiver klass, find the target for the call.
 611 // Return NULL if the call has no target or the target is abstract.
 612 ciMethod* ciMethod::resolve_invoke(ciKlass* caller, ciKlass* exact_receiver) {
 613    check_is_loaded();
 614    VM_ENTRY_MARK;
 615 
 616    KlassHandle caller_klass (THREAD, caller->get_klassOop());
 617    KlassHandle h_recv       (THREAD, exact_receiver->get_klassOop());
 618    KlassHandle h_resolved   (THREAD, holder()->get_klassOop());
 619    symbolHandle h_name      (THREAD, name()->get_symbolOop());
 620    symbolHandle h_signature (THREAD, signature()->get_symbolOop());
 621 
 622    methodHandle m;
 623    // Only do exact lookup if receiver klass has been linked.  Otherwise,
 624    // the vtable has not been setup, and the LinkResolver will fail.
 625    if (h_recv->oop_is_javaArray()
 626         ||
 627        instanceKlass::cast(h_recv())->is_linked() && !exact_receiver->is_interface()) {
 628      if (holder()->is_interface()) {
 629        m = LinkResolver::resolve_interface_call_or_null(h_recv, h_resolved, h_name, h_signature, caller_klass);
 630      } else {
 631        m = LinkResolver::resolve_virtual_call_or_null(h_recv, h_resolved, h_name, h_signature, caller_klass);
 632      }
 633    }
 634 
 635    if (m.is_null()) {
 636      // Return NULL only if there was a problem with lookup (uninitialized class, etc.)
 637      return NULL;
 638    }
 639 
 640    ciMethod* result = this;
 641    if (m() != get_methodOop()) {
 642      result = CURRENT_THREAD_ENV->get_object(m())->as_method();
 643    }
 644 
 645    // Don't return abstract methods because they aren't
 646    // optimizable or interesting.
 647    if (result->is_abstract()) {
 648      return NULL;
 649    } else {
 650      return result;
 651    }
 652 }
 653 
 654 // ------------------------------------------------------------------
 655 // ciMethod::resolve_vtable_index
 656 //
 657 // Given a known receiver klass, find the vtable index for the call.
 658 // Return methodOopDesc::invalid_vtable_index if the vtable_index is unknown.
 659 int ciMethod::resolve_vtable_index(ciKlass* caller, ciKlass* receiver) {
 660    check_is_loaded();
 661 
 662    int vtable_index = methodOopDesc::invalid_vtable_index;
 663    // Only do lookup if receiver klass has been linked.  Otherwise,
 664    // the vtable has not been setup, and the LinkResolver will fail.
 665    if (!receiver->is_interface()
 666        && (!receiver->is_instance_klass() ||
 667            receiver->as_instance_klass()->is_linked())) {
 668      VM_ENTRY_MARK;
 669 
 670      KlassHandle caller_klass (THREAD, caller->get_klassOop());
 671      KlassHandle h_recv       (THREAD, receiver->get_klassOop());
 672      symbolHandle h_name      (THREAD, name()->get_symbolOop());
 673      symbolHandle h_signature (THREAD, signature()->get_symbolOop());
 674 
 675      vtable_index = LinkResolver::resolve_virtual_vtable_index(h_recv, h_recv, h_name, h_signature, caller_klass);
 676      if (vtable_index == methodOopDesc::nonvirtual_vtable_index) {
 677        // A statically bound method.  Return "no such index".
 678        vtable_index = methodOopDesc::invalid_vtable_index;
 679      }
 680    }
 681 
 682    return vtable_index;
 683 }
 684 
 685 // ------------------------------------------------------------------
 686 // ciMethod::interpreter_call_site_count
 687 int ciMethod::interpreter_call_site_count(int bci) {
 688   if (method_data() != NULL) {
 689     ResourceMark rm;
 690     ciProfileData* data = method_data()->bci_to_data(bci);
 691     if (data != NULL && data->is_CounterData()) {
 692       return scale_count(data->as_CounterData()->count());
 693     }
 694   }
 695   return -1;  // unknown
 696 }
 697 
 698 // ------------------------------------------------------------------
 699 // Adjust a CounterData count to be commensurate with
 700 // interpreter_invocation_count.  If the MDO exists for
 701 // only 25% of the time the method exists, then the
 702 // counts in the MDO should be scaled by 4X, so that
 703 // they can be usefully and stably compared against the
 704 // invocation counts in methods.
 705 int ciMethod::scale_count(int count, float prof_factor) {
 706   if (count > 0 && method_data() != NULL) {
 707     int counter_life;
 708     int method_life = interpreter_invocation_count();
 709     if (TieredCompilation) {
 710       // In tiered the MDO's life is measured directly, so just use the snapshotted counters
 711       counter_life = MAX2(method_data()->invocation_count(), method_data()->backedge_count());
 712     } else {
 713       int current_mileage = method_data()->current_mileage();
 714       int creation_mileage = method_data()->creation_mileage();
 715       counter_life = current_mileage - creation_mileage;
 716     }
 717 
 718     // counter_life due to backedge_counter could be > method_life
 719     if (counter_life > method_life)
 720       counter_life = method_life;
 721     if (0 < counter_life && counter_life <= method_life) {
 722       count = (int)((double)count * prof_factor * method_life / counter_life + 0.5);
 723       count = (count > 0) ? count : 1;
 724     }
 725   }
 726   return count;
 727 }
 728 
 729 // ------------------------------------------------------------------
 730 // invokedynamic support
 731 
 732 // ------------------------------------------------------------------
 733 // ciMethod::is_method_handle_invoke
 734 //
 735 // Return true if the method is an instance of one of the two
 736 // signature-polymorphic MethodHandle methods, invokeExact or invokeGeneric.
 737 bool ciMethod::is_method_handle_invoke() const {
 738   if (!is_loaded()) {
 739     bool flag = (holder()->name() == ciSymbol::java_dyn_MethodHandle() &&
 740                  methodOopDesc::is_method_handle_invoke_name(name()->sid()));
 741     return flag;
 742   }
 743   VM_ENTRY_MARK;
 744   return get_methodOop()->is_method_handle_invoke();
 745 }
 746 
 747 // ------------------------------------------------------------------
 748 // ciMethod::is_method_handle_adapter
 749 //
 750 // Return true if the method is a generated MethodHandle adapter.
 751 // These are built by MethodHandleCompiler.
 752 bool ciMethod::is_method_handle_adapter() const {
 753   if (!is_loaded())  return false;
 754   VM_ENTRY_MARK;
 755   return get_methodOop()->is_method_handle_adapter();
 756 }
 757 
 758 ciInstance* ciMethod::method_handle_type() {
 759   check_is_loaded();
 760   VM_ENTRY_MARK;
 761   oop mtype = get_methodOop()->method_handle_type();
 762   return CURRENT_THREAD_ENV->get_object(mtype)->as_instance();
 763 }
 764 
 765 
 766 // ------------------------------------------------------------------
 767 // ciMethod::build_method_data
 768 //
 769 // Generate new methodDataOop objects at compile time.
 770 void ciMethod::build_method_data(methodHandle h_m) {
 771   EXCEPTION_CONTEXT;
 772   if (is_native() || is_abstract() || h_m()->is_accessor()) return;
 773   if (h_m()->method_data() == NULL) {
 774     methodOopDesc::build_interpreter_method_data(h_m, THREAD);
 775     if (HAS_PENDING_EXCEPTION) {
 776       CLEAR_PENDING_EXCEPTION;
 777     }
 778   }
 779   if (h_m()->method_data() != NULL) {
 780     _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
 781     _method_data->load_data();
 782   } else {
 783     _method_data = CURRENT_ENV->get_empty_methodData();
 784   }
 785 }
 786 
 787 // public, retroactive version
 788 void ciMethod::build_method_data() {
 789   if (_method_data == NULL || _method_data->is_empty()) {
 790     GUARDED_VM_ENTRY({
 791       build_method_data(get_methodOop());
 792     });
 793   }
 794 }
 795 
 796 
 797 // ------------------------------------------------------------------
 798 // ciMethod::method_data
 799 //
 800 ciMethodData* ciMethod::method_data() {
 801   if (_method_data != NULL) {
 802     return _method_data;
 803   }
 804   VM_ENTRY_MARK;
 805   ciEnv* env = CURRENT_ENV;
 806   Thread* my_thread = JavaThread::current();
 807   methodHandle h_m(my_thread, get_methodOop());
 808 
 809   // Create an MDO for the inlinee
 810   if (TieredCompilation && is_c1_compile(env->comp_level())) {
 811     build_method_data(h_m);
 812   }
 813 
 814   if (h_m()->method_data() != NULL) {
 815     _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
 816     _method_data->load_data();
 817   } else {
 818     _method_data = CURRENT_ENV->get_empty_methodData();
 819   }
 820   return _method_data;
 821 
 822 }
 823 
 824 
 825 // ------------------------------------------------------------------
 826 // ciMethod::will_link
 827 //
 828 // Will this method link in a specific calling context?
 829 bool ciMethod::will_link(ciKlass* accessing_klass,
 830                          ciKlass* declared_method_holder,
 831                          Bytecodes::Code bc) {
 832   if (!is_loaded()) {
 833     // Method lookup failed.
 834     return false;
 835   }
 836 
 837   // The link checks have been front-loaded into the get_method
 838   // call.  This method (ciMethod::will_link()) will be removed
 839   // in the future.
 840 
 841   return true;
 842 }
 843 
 844 // ------------------------------------------------------------------
 845 // ciMethod::should_exclude
 846 //
 847 // Should this method be excluded from compilation?
 848 bool ciMethod::should_exclude() {
 849   check_is_loaded();
 850   VM_ENTRY_MARK;
 851   methodHandle mh(THREAD, get_methodOop());
 852   bool ignore;
 853   return CompilerOracle::should_exclude(mh, ignore);
 854 }
 855 
 856 // ------------------------------------------------------------------
 857 // ciMethod::should_inline
 858 //
 859 // Should this method be inlined during compilation?
 860 bool ciMethod::should_inline() {
 861   check_is_loaded();
 862   VM_ENTRY_MARK;
 863   methodHandle mh(THREAD, get_methodOop());
 864   return CompilerOracle::should_inline(mh);
 865 }
 866 
 867 // ------------------------------------------------------------------
 868 // ciMethod::should_not_inline
 869 //
 870 // Should this method be disallowed from inlining during compilation?
 871 bool ciMethod::should_not_inline() {
 872   check_is_loaded();
 873   VM_ENTRY_MARK;
 874   methodHandle mh(THREAD, get_methodOop());
 875   return CompilerOracle::should_not_inline(mh);
 876 }
 877 
 878 // ------------------------------------------------------------------
 879 // ciMethod::should_print_assembly
 880 //
 881 // Should the compiler print the generated code for this method?
 882 bool ciMethod::should_print_assembly() {
 883   check_is_loaded();
 884   VM_ENTRY_MARK;
 885   methodHandle mh(THREAD, get_methodOop());
 886   return CompilerOracle::should_print(mh);
 887 }
 888 
 889 // ------------------------------------------------------------------
 890 // ciMethod::break_at_execute
 891 //
 892 // Should the compiler insert a breakpoint into the generated code
 893 // method?
 894 bool ciMethod::break_at_execute() {
 895   check_is_loaded();
 896   VM_ENTRY_MARK;
 897   methodHandle mh(THREAD, get_methodOop());
 898   return CompilerOracle::should_break_at(mh);
 899 }
 900 
 901 // ------------------------------------------------------------------
 902 // ciMethod::has_option
 903 //
 904 bool ciMethod::has_option(const char* option) {
 905   check_is_loaded();
 906   VM_ENTRY_MARK;
 907   methodHandle mh(THREAD, get_methodOop());
 908   return CompilerOracle::has_option_string(mh, option);
 909 }
 910 
 911 // ------------------------------------------------------------------
 912 // ciMethod::can_be_compiled
 913 //
 914 // Have previous compilations of this method succeeded?
 915 bool ciMethod::can_be_compiled() {
 916   check_is_loaded();
 917   ciEnv* env = CURRENT_ENV;
 918   if (is_c1_compile(env->comp_level())) {
 919     return _is_c1_compilable;
 920   }
 921   return _is_c2_compilable;
 922 }
 923 
 924 // ------------------------------------------------------------------
 925 // ciMethod::set_not_compilable
 926 //
 927 // Tell the VM that this method cannot be compiled at all.
 928 void ciMethod::set_not_compilable() {
 929   check_is_loaded();
 930   VM_ENTRY_MARK;
 931   ciEnv* env = CURRENT_ENV;
 932   if (is_c1_compile(env->comp_level())) {
 933     _is_c1_compilable = false;
 934   } else {
 935     _is_c2_compilable = false;
 936   }
 937   get_methodOop()->set_not_compilable(env->comp_level());
 938 }
 939 
 940 // ------------------------------------------------------------------
 941 // ciMethod::can_be_osr_compiled
 942 //
 943 // Have previous compilations of this method succeeded?
 944 //
 945 // Implementation note: the VM does not currently keep track
 946 // of failed OSR compilations per bci.  The entry_bci parameter
 947 // is currently unused.
 948 bool ciMethod::can_be_osr_compiled(int entry_bci) {
 949   check_is_loaded();
 950   VM_ENTRY_MARK;
 951   ciEnv* env = CURRENT_ENV;
 952   return !get_methodOop()->is_not_osr_compilable(env->comp_level());
 953 }
 954 
 955 // ------------------------------------------------------------------
 956 // ciMethod::has_compiled_code
 957 bool ciMethod::has_compiled_code() {
 958   VM_ENTRY_MARK;
 959   return get_methodOop()->code() != NULL;
 960 }
 961 
 962 int ciMethod::comp_level() {
 963   check_is_loaded();
 964   VM_ENTRY_MARK;
 965   nmethod* nm = get_methodOop()->code();
 966   if (nm != NULL) return nm->comp_level();
 967   return 0;
 968 }
 969 
 970 // ------------------------------------------------------------------
 971 // ciMethod::instructions_size
 972 //
 973 // This is a rough metric for "fat" methods, compared before inlining
 974 // with InlineSmallCode.  The CodeBlob::code_size accessor includes
 975 // junk like exception handler, stubs, and constant table, which are
 976 // not highly relevant to an inlined method.  So we use the more
 977 // specific accessor nmethod::insts_size.
 978 int ciMethod::instructions_size(int comp_level) {
 979   GUARDED_VM_ENTRY(
 980     nmethod* code = get_methodOop()->code();
 981     if (code != NULL && (comp_level == CompLevel_any || comp_level == code->comp_level())) {
 982       return code->insts_end() - code->verified_entry_point();
 983     }
 984     return 0;
 985   )
 986 }
 987 
 988 // ------------------------------------------------------------------
 989 // ciMethod::log_nmethod_identity
 990 void ciMethod::log_nmethod_identity(xmlStream* log) {
 991   GUARDED_VM_ENTRY(
 992     nmethod* code = get_methodOop()->code();
 993     if (code != NULL) {
 994       code->log_identity(log);
 995     }
 996   )
 997 }
 998 
 999 // ------------------------------------------------------------------
1000 // ciMethod::is_not_reached
1001 bool ciMethod::is_not_reached(int bci) {
1002   check_is_loaded();
1003   VM_ENTRY_MARK;
1004   return Interpreter::is_not_reached(
1005                methodHandle(THREAD, get_methodOop()), bci);
1006 }
1007 
1008 // ------------------------------------------------------------------
1009 // ciMethod::was_never_executed
1010 bool ciMethod::was_executed_more_than(int times) {
1011   VM_ENTRY_MARK;
1012   return get_methodOop()->was_executed_more_than(times);
1013 }
1014 
1015 // ------------------------------------------------------------------
1016 // ciMethod::has_unloaded_classes_in_signature
1017 bool ciMethod::has_unloaded_classes_in_signature() {
1018   VM_ENTRY_MARK;
1019   {
1020     EXCEPTION_MARK;
1021     methodHandle m(THREAD, get_methodOop());
1022     bool has_unloaded = methodOopDesc::has_unloaded_classes_in_signature(m, (JavaThread *)THREAD);
1023     if( HAS_PENDING_EXCEPTION ) {
1024       CLEAR_PENDING_EXCEPTION;
1025       return true;     // Declare that we may have unloaded classes
1026     }
1027     return has_unloaded;
1028   }
1029 }
1030 
1031 // ------------------------------------------------------------------
1032 // ciMethod::is_klass_loaded
1033 bool ciMethod::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
1034   VM_ENTRY_MARK;
1035   return get_methodOop()->is_klass_loaded(refinfo_index, must_be_resolved);
1036 }
1037 
1038 // ------------------------------------------------------------------
1039 // ciMethod::check_call
1040 bool ciMethod::check_call(int refinfo_index, bool is_static) const {
1041   VM_ENTRY_MARK;
1042   {
1043     EXCEPTION_MARK;
1044     HandleMark hm(THREAD);
1045     constantPoolHandle pool (THREAD, get_methodOop()->constants());
1046     methodHandle spec_method;
1047     KlassHandle  spec_klass;
1048     LinkResolver::resolve_method(spec_method, spec_klass, pool, refinfo_index, THREAD);
1049     if (HAS_PENDING_EXCEPTION) {
1050       CLEAR_PENDING_EXCEPTION;
1051       return false;
1052     } else {
1053       return (spec_method->is_static() == is_static);
1054     }
1055   }
1056   return false;
1057 }
1058 
1059 // ------------------------------------------------------------------
1060 // ciMethod::print_codes
1061 //
1062 // Print the bytecodes for this method.
1063 void ciMethod::print_codes_on(outputStream* st) {
1064   check_is_loaded();
1065   GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(st);)
1066 }
1067 
1068 
1069 #define FETCH_FLAG_FROM_VM(flag_accessor) { \
1070   check_is_loaded(); \
1071   VM_ENTRY_MARK; \
1072   return get_methodOop()->flag_accessor(); \
1073 }
1074 
1075 bool ciMethod::is_empty_method() const {         FETCH_FLAG_FROM_VM(is_empty_method); }
1076 bool ciMethod::is_vanilla_constructor() const {  FETCH_FLAG_FROM_VM(is_vanilla_constructor); }
1077 bool ciMethod::has_loops      () const {         FETCH_FLAG_FROM_VM(has_loops); }
1078 bool ciMethod::has_jsrs       () const {         FETCH_FLAG_FROM_VM(has_jsrs);  }
1079 bool ciMethod::is_accessor    () const {         FETCH_FLAG_FROM_VM(is_accessor); }
1080 bool ciMethod::is_initializer () const {         FETCH_FLAG_FROM_VM(is_initializer); }
1081 
1082 BCEscapeAnalyzer  *ciMethod::get_bcea() {
1083 #ifdef COMPILER2
1084   if (_bcea == NULL) {
1085     _bcea = new (CURRENT_ENV->arena()) BCEscapeAnalyzer(this, NULL);
1086   }
1087   return _bcea;
1088 #else // COMPILER2
1089   ShouldNotReachHere();
1090   return NULL;
1091 #endif // COMPILER2
1092 }
1093 
1094 ciMethodBlocks  *ciMethod::get_method_blocks() {
1095   Arena *arena = CURRENT_ENV->arena();
1096   if (_method_blocks == NULL) {
1097     _method_blocks = new (arena) ciMethodBlocks(arena, this);
1098   }
1099   return _method_blocks;
1100 }
1101 
1102 #undef FETCH_FLAG_FROM_VM
1103 
1104 
1105 // ------------------------------------------------------------------
1106 // ciMethod::print_codes
1107 //
1108 // Print a range of the bytecodes for this method.
1109 void ciMethod::print_codes_on(int from, int to, outputStream* st) {
1110   check_is_loaded();
1111   GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(from, to, st);)
1112 }
1113 
1114 // ------------------------------------------------------------------
1115 // ciMethod::print_name
1116 //
1117 // Print the name of this method, including signature and some flags.
1118 void ciMethod::print_name(outputStream* st) {
1119   check_is_loaded();
1120   GUARDED_VM_ENTRY(get_methodOop()->print_name(st);)
1121 }
1122 
1123 // ------------------------------------------------------------------
1124 // ciMethod::print_short_name
1125 //
1126 // Print the name of this method, without signature.
1127 void ciMethod::print_short_name(outputStream* st) {
1128   check_is_loaded();
1129   GUARDED_VM_ENTRY(get_methodOop()->print_short_name(st);)
1130 }
1131 
1132 // ------------------------------------------------------------------
1133 // ciMethod::print_impl
1134 //
1135 // Implementation of the print method.
1136 void ciMethod::print_impl(outputStream* st) {
1137   ciObject::print_impl(st);
1138   st->print(" name=");
1139   name()->print_symbol_on(st);
1140   st->print(" holder=");
1141   holder()->print_name_on(st);
1142   st->print(" signature=");
1143   signature()->as_symbol()->print_symbol_on(st);
1144   if (is_loaded()) {
1145     st->print(" loaded=true flags=");
1146     flags().print_member_flags(st);
1147   } else {
1148     st->print(" loaded=false");
1149   }
1150 }