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())  return false;
 739   VM_ENTRY_MARK;
 740   return get_methodOop()->is_method_handle_invoke();
 741 }
 742 
 743 // ------------------------------------------------------------------
 744 // ciMethod::is_method_handle_adapter
 745 //
 746 // Return true if the method is a generated MethodHandle adapter.
 747 // These are built by MethodHandleCompiler.
 748 bool ciMethod::is_method_handle_adapter() const {
 749   if (!is_loaded())  return false;
 750   VM_ENTRY_MARK;
 751   return get_methodOop()->is_method_handle_adapter();
 752 }
 753 
 754 ciInstance* ciMethod::method_handle_type() {
 755   check_is_loaded();
 756   VM_ENTRY_MARK;
 757   oop mtype = get_methodOop()->method_handle_type();
 758   return CURRENT_THREAD_ENV->get_object(mtype)->as_instance();
 759 }
 760 
 761 
 762 // ------------------------------------------------------------------
 763 // ciMethod::build_method_data
 764 //
 765 // Generate new methodDataOop objects at compile time.
 766 void ciMethod::build_method_data(methodHandle h_m) {
 767   EXCEPTION_CONTEXT;
 768   if (is_native() || is_abstract() || h_m()->is_accessor()) return;
 769   if (h_m()->method_data() == NULL) {
 770     methodOopDesc::build_interpreter_method_data(h_m, THREAD);
 771     if (HAS_PENDING_EXCEPTION) {
 772       CLEAR_PENDING_EXCEPTION;
 773     }
 774   }
 775   if (h_m()->method_data() != NULL) {
 776     _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
 777     _method_data->load_data();
 778   } else {
 779     _method_data = CURRENT_ENV->get_empty_methodData();
 780   }
 781 }
 782 
 783 // public, retroactive version
 784 void ciMethod::build_method_data() {
 785   if (_method_data == NULL || _method_data->is_empty()) {
 786     GUARDED_VM_ENTRY({
 787       build_method_data(get_methodOop());
 788     });
 789   }
 790 }
 791 
 792 
 793 // ------------------------------------------------------------------
 794 // ciMethod::method_data
 795 //
 796 ciMethodData* ciMethod::method_data() {
 797   if (_method_data != NULL) {
 798     return _method_data;
 799   }
 800   VM_ENTRY_MARK;
 801   ciEnv* env = CURRENT_ENV;
 802   Thread* my_thread = JavaThread::current();
 803   methodHandle h_m(my_thread, get_methodOop());
 804 
 805   // Create an MDO for the inlinee
 806   if (TieredCompilation && is_c1_compile(env->comp_level())) {
 807     build_method_data(h_m);
 808   }
 809 
 810   if (h_m()->method_data() != NULL) {
 811     _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
 812     _method_data->load_data();
 813   } else {
 814     _method_data = CURRENT_ENV->get_empty_methodData();
 815   }
 816   return _method_data;
 817 
 818 }
 819 
 820 
 821 // ------------------------------------------------------------------
 822 // ciMethod::will_link
 823 //
 824 // Will this method link in a specific calling context?
 825 bool ciMethod::will_link(ciKlass* accessing_klass,
 826                          ciKlass* declared_method_holder,
 827                          Bytecodes::Code bc) {
 828   if (!is_loaded()) {
 829     // Method lookup failed.
 830     return false;
 831   }
 832 
 833   // The link checks have been front-loaded into the get_method
 834   // call.  This method (ciMethod::will_link()) will be removed
 835   // in the future.
 836 
 837   return true;
 838 }
 839 
 840 // ------------------------------------------------------------------
 841 // ciMethod::should_exclude
 842 //
 843 // Should this method be excluded from compilation?
 844 bool ciMethod::should_exclude() {
 845   check_is_loaded();
 846   VM_ENTRY_MARK;
 847   methodHandle mh(THREAD, get_methodOop());
 848   bool ignore;
 849   return CompilerOracle::should_exclude(mh, ignore);
 850 }
 851 
 852 // ------------------------------------------------------------------
 853 // ciMethod::should_inline
 854 //
 855 // Should this method be inlined during compilation?
 856 bool ciMethod::should_inline() {
 857   check_is_loaded();
 858   VM_ENTRY_MARK;
 859   methodHandle mh(THREAD, get_methodOop());
 860   return CompilerOracle::should_inline(mh);
 861 }
 862 
 863 // ------------------------------------------------------------------
 864 // ciMethod::should_not_inline
 865 //
 866 // Should this method be disallowed from inlining during compilation?
 867 bool ciMethod::should_not_inline() {
 868   check_is_loaded();
 869   VM_ENTRY_MARK;
 870   methodHandle mh(THREAD, get_methodOop());
 871   return CompilerOracle::should_not_inline(mh);
 872 }
 873 
 874 // ------------------------------------------------------------------
 875 // ciMethod::should_print_assembly
 876 //
 877 // Should the compiler print the generated code for this method?
 878 bool ciMethod::should_print_assembly() {
 879   check_is_loaded();
 880   VM_ENTRY_MARK;
 881   methodHandle mh(THREAD, get_methodOop());
 882   return CompilerOracle::should_print(mh);
 883 }
 884 
 885 // ------------------------------------------------------------------
 886 // ciMethod::break_at_execute
 887 //
 888 // Should the compiler insert a breakpoint into the generated code
 889 // method?
 890 bool ciMethod::break_at_execute() {
 891   check_is_loaded();
 892   VM_ENTRY_MARK;
 893   methodHandle mh(THREAD, get_methodOop());
 894   return CompilerOracle::should_break_at(mh);
 895 }
 896 
 897 // ------------------------------------------------------------------
 898 // ciMethod::has_option
 899 //
 900 bool ciMethod::has_option(const char* option) {
 901   check_is_loaded();
 902   VM_ENTRY_MARK;
 903   methodHandle mh(THREAD, get_methodOop());
 904   return CompilerOracle::has_option_string(mh, option);
 905 }
 906 
 907 // ------------------------------------------------------------------
 908 // ciMethod::can_be_compiled
 909 //
 910 // Have previous compilations of this method succeeded?
 911 bool ciMethod::can_be_compiled() {
 912   check_is_loaded();
 913   ciEnv* env = CURRENT_ENV;
 914   if (is_c1_compile(env->comp_level())) {
 915     return _is_c1_compilable;
 916   }
 917   return _is_c2_compilable;
 918 }
 919 
 920 // ------------------------------------------------------------------
 921 // ciMethod::set_not_compilable
 922 //
 923 // Tell the VM that this method cannot be compiled at all.
 924 void ciMethod::set_not_compilable() {
 925   check_is_loaded();
 926   VM_ENTRY_MARK;
 927   ciEnv* env = CURRENT_ENV;
 928   if (is_c1_compile(env->comp_level())) {
 929     _is_c1_compilable = false;
 930   } else {
 931     _is_c2_compilable = false;
 932   }
 933   get_methodOop()->set_not_compilable(env->comp_level());
 934 }
 935 
 936 // ------------------------------------------------------------------
 937 // ciMethod::can_be_osr_compiled
 938 //
 939 // Have previous compilations of this method succeeded?
 940 //
 941 // Implementation note: the VM does not currently keep track
 942 // of failed OSR compilations per bci.  The entry_bci parameter
 943 // is currently unused.
 944 bool ciMethod::can_be_osr_compiled(int entry_bci) {
 945   check_is_loaded();
 946   VM_ENTRY_MARK;
 947   ciEnv* env = CURRENT_ENV;
 948   return !get_methodOop()->is_not_osr_compilable(env->comp_level());
 949 }
 950 
 951 // ------------------------------------------------------------------
 952 // ciMethod::has_compiled_code
 953 bool ciMethod::has_compiled_code() {
 954   VM_ENTRY_MARK;
 955   return get_methodOop()->code() != NULL;
 956 }
 957 
 958 int ciMethod::comp_level() {
 959   check_is_loaded();
 960   VM_ENTRY_MARK;
 961   nmethod* nm = get_methodOop()->code();
 962   if (nm != NULL) return nm->comp_level();
 963   return 0;
 964 }
 965 
 966 // ------------------------------------------------------------------
 967 // ciMethod::instructions_size
 968 //
 969 // This is a rough metric for "fat" methods, compared before inlining
 970 // with InlineSmallCode.  The CodeBlob::code_size accessor includes
 971 // junk like exception handler, stubs, and constant table, which are
 972 // not highly relevant to an inlined method.  So we use the more
 973 // specific accessor nmethod::insts_size.
 974 int ciMethod::instructions_size(int comp_level) {
 975   GUARDED_VM_ENTRY(
 976     nmethod* code = get_methodOop()->code();
 977     if (code != NULL && (comp_level == CompLevel_any || comp_level == code->comp_level())) {
 978       return code->code_end() - code->verified_entry_point();
 979     }
 980     return 0;
 981   )
 982 }
 983 
 984 // ------------------------------------------------------------------
 985 // ciMethod::log_nmethod_identity
 986 void ciMethod::log_nmethod_identity(xmlStream* log) {
 987   GUARDED_VM_ENTRY(
 988     nmethod* code = get_methodOop()->code();
 989     if (code != NULL) {
 990       code->log_identity(log);
 991     }
 992   )
 993 }
 994 
 995 // ------------------------------------------------------------------
 996 // ciMethod::is_not_reached
 997 bool ciMethod::is_not_reached(int bci) {
 998   check_is_loaded();
 999   VM_ENTRY_MARK;
1000   return Interpreter::is_not_reached(
1001                methodHandle(THREAD, get_methodOop()), bci);
1002 }
1003 
1004 // ------------------------------------------------------------------
1005 // ciMethod::was_never_executed
1006 bool ciMethod::was_executed_more_than(int times) {
1007   VM_ENTRY_MARK;
1008   return get_methodOop()->was_executed_more_than(times);
1009 }
1010 
1011 // ------------------------------------------------------------------
1012 // ciMethod::has_unloaded_classes_in_signature
1013 bool ciMethod::has_unloaded_classes_in_signature() {
1014   VM_ENTRY_MARK;
1015   {
1016     EXCEPTION_MARK;
1017     methodHandle m(THREAD, get_methodOop());
1018     bool has_unloaded = methodOopDesc::has_unloaded_classes_in_signature(m, (JavaThread *)THREAD);
1019     if( HAS_PENDING_EXCEPTION ) {
1020       CLEAR_PENDING_EXCEPTION;
1021       return true;     // Declare that we may have unloaded classes
1022     }
1023     return has_unloaded;
1024   }
1025 }
1026 
1027 // ------------------------------------------------------------------
1028 // ciMethod::is_klass_loaded
1029 bool ciMethod::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
1030   VM_ENTRY_MARK;
1031   return get_methodOop()->is_klass_loaded(refinfo_index, must_be_resolved);
1032 }
1033 
1034 // ------------------------------------------------------------------
1035 // ciMethod::check_call
1036 bool ciMethod::check_call(int refinfo_index, bool is_static) const {
1037   VM_ENTRY_MARK;
1038   {
1039     EXCEPTION_MARK;
1040     HandleMark hm(THREAD);
1041     constantPoolHandle pool (THREAD, get_methodOop()->constants());
1042     methodHandle spec_method;
1043     KlassHandle  spec_klass;
1044     LinkResolver::resolve_method(spec_method, spec_klass, pool, refinfo_index, THREAD);
1045     if (HAS_PENDING_EXCEPTION) {
1046       CLEAR_PENDING_EXCEPTION;
1047       return false;
1048     } else {
1049       return (spec_method->is_static() == is_static);
1050     }
1051   }
1052   return false;
1053 }
1054 
1055 // ------------------------------------------------------------------
1056 // ciMethod::print_codes
1057 //
1058 // Print the bytecodes for this method.
1059 void ciMethod::print_codes_on(outputStream* st) {
1060   check_is_loaded();
1061   GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(st);)
1062 }
1063 
1064 
1065 #define FETCH_FLAG_FROM_VM(flag_accessor) { \
1066   check_is_loaded(); \
1067   VM_ENTRY_MARK; \
1068   return get_methodOop()->flag_accessor(); \
1069 }
1070 
1071 bool ciMethod::is_empty_method() const {         FETCH_FLAG_FROM_VM(is_empty_method); }
1072 bool ciMethod::is_vanilla_constructor() const {  FETCH_FLAG_FROM_VM(is_vanilla_constructor); }
1073 bool ciMethod::has_loops      () const {         FETCH_FLAG_FROM_VM(has_loops); }
1074 bool ciMethod::has_jsrs       () const {         FETCH_FLAG_FROM_VM(has_jsrs);  }
1075 bool ciMethod::is_accessor    () const {         FETCH_FLAG_FROM_VM(is_accessor); }
1076 bool ciMethod::is_initializer () const {         FETCH_FLAG_FROM_VM(is_initializer); }
1077 
1078 BCEscapeAnalyzer  *ciMethod::get_bcea() {
1079 #ifdef COMPILER2
1080   if (_bcea == NULL) {
1081     _bcea = new (CURRENT_ENV->arena()) BCEscapeAnalyzer(this, NULL);
1082   }
1083   return _bcea;
1084 #else // COMPILER2
1085   ShouldNotReachHere();
1086   return NULL;
1087 #endif // COMPILER2
1088 }
1089 
1090 ciMethodBlocks  *ciMethod::get_method_blocks() {
1091   Arena *arena = CURRENT_ENV->arena();
1092   if (_method_blocks == NULL) {
1093     _method_blocks = new (arena) ciMethodBlocks(arena, this);
1094   }
1095   return _method_blocks;
1096 }
1097 
1098 #undef FETCH_FLAG_FROM_VM
1099 
1100 
1101 // ------------------------------------------------------------------
1102 // ciMethod::print_codes
1103 //
1104 // Print a range of the bytecodes for this method.
1105 void ciMethod::print_codes_on(int from, int to, outputStream* st) {
1106   check_is_loaded();
1107   GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(from, to, st);)
1108 }
1109 
1110 // ------------------------------------------------------------------
1111 // ciMethod::print_name
1112 //
1113 // Print the name of this method, including signature and some flags.
1114 void ciMethod::print_name(outputStream* st) {
1115   check_is_loaded();
1116   GUARDED_VM_ENTRY(get_methodOop()->print_name(st);)
1117 }
1118 
1119 // ------------------------------------------------------------------
1120 // ciMethod::print_short_name
1121 //
1122 // Print the name of this method, without signature.
1123 void ciMethod::print_short_name(outputStream* st) {
1124   check_is_loaded();
1125   GUARDED_VM_ENTRY(get_methodOop()->print_short_name(st);)
1126 }
1127 
1128 // ------------------------------------------------------------------
1129 // ciMethod::print_impl
1130 //
1131 // Implementation of the print method.
1132 void ciMethod::print_impl(outputStream* st) {
1133   ciObject::print_impl(st);
1134   st->print(" name=");
1135   name()->print_symbol_on(st);
1136   st->print(" holder=");
1137   holder()->print_name_on(st);
1138   st->print(" signature=");
1139   signature()->as_symbol()->print_symbol_on(st);
1140   if (is_loaded()) {
1141     st->print(" loaded=true flags=");
1142     flags().print_member_flags(st);
1143   } else {
1144     st->print(" loaded=false");
1145   }
1146 }